feat(tracker): add messages, draft mapping method

This commit is contained in:
sylenien 2022-09-08 17:22:56 +02:00
parent 5899a78322
commit 849b33f921
31 changed files with 1447 additions and 504 deletions

View file

@ -2,7 +2,7 @@
package messages
func IsReplayerType(id int) bool {
return 0 == id || 4 == id || 5 == id || 6 == id || 7 == id || 8 == id || 9 == id || 10 == id || 11 == id || 12 == id || 13 == id || 14 == id || 15 == id || 16 == id || 18 == id || 19 == id || 20 == id || 22 == id || 37 == id || 38 == id || 39 == id || 40 == id || 41 == id || 44 == id || 45 == id || 46 == id || 47 == id || 48 == id || 49 == id || 54 == id || 55 == id || 59 == id || 60 == id || 61 == id || 67 == id || 69 == id || 70 == id || 71 == id || 72 == id || 73 == id || 74 == id || 75 == id || 76 == id || 77 == id || 79 == id || 90 == id || 93 == id || 96 == id || 100 == id || 102 == id || 103 == id || 105 == id
return 0 == id || 4 == id || 5 == id || 6 == id || 7 == id || 8 == id || 9 == id || 10 == id || 11 == id || 12 == id || 13 == id || 14 == id || 15 == id || 16 == id || 18 == id || 19 == id || 20 == id || 22 == id || 37 == id || 38 == id || 39 == id || 40 == id || 41 == id || 44 == id || 45 == id || 46 == id || 47 == id || 48 == id || 49 == id || 54 == id || 55 == id || 59 == id || 60 == id || 61 == id || 67 == id || 69 == id || 70 == id || 71 == id || 72 == id || 73 == id || 74 == id || 75 == id || 76 == id || 77 == id || 79 == id || 83 == id || 90 == id || 93 == id || 96 == id || 100 == id || 102 == id || 103 == id || 105 == id
}
func IsIOSType(id int) bool {

View file

@ -158,6 +158,8 @@ const (
MsgZustand = 79
MsgReplaceVCSS = 83
MsgIOSBatchMeta = 107
MsgIOSSessionStart = 90
@ -3066,6 +3068,36 @@ func (msg *Zustand) EncodeWithIndex() []byte {
return data
}
type ReplaceVCSS struct {
message
ID uint64
Styles string
SheetID string
BaseURL string
}
func (msg *ReplaceVCSS) Encode() []byte {
buf := make([]byte, 41+len(msg.Styles)+len(msg.SheetID)+len(msg.BaseURL))
buf[0] = 83
p := 1
p = WriteUint(msg.ID, buf, p)
p = WriteString(msg.Styles, buf, p)
p = WriteString(msg.SheetID, buf, p)
p = WriteString(msg.BaseURL, buf, p)
return buf[:p]
}
func (msg *ReplaceVCSS) EncodeWithIndex() []byte {
encoded := msg.Encode()
if IsIOSType(msg.TypeID()) {
return encoded
}
data := make([]byte, len(encoded)+8)
copy(data[8:], encoded[:])
binary.LittleEndian.PutUint64(data[0:], msg.Meta().Index)
return data
}
func (msg *Zustand) Decode() Message {
return msg
}
@ -3074,6 +3106,14 @@ func (msg *Zustand) TypeID() int {
return 79
}
func (msg *ReplaceVCSS) Decode() Message {
return msg
}
func (msg *ReplaceVCSS) TypeID() int {
return 83
}
type IOSBatchMeta struct {
message
Timestamp uint64

View file

@ -1318,6 +1318,24 @@ func DecodeZustand(reader io.Reader) (Message, error) {
return msg, err
}
func DecodeReplaceVCSS(reader io.Reader) (Message, error) {
var err error = nil
msg := &ReplaceVCSS{}
if msg.ID, err = ReadUint(reader); err != nil {
return nil, err
}
if msg.Styles, err = ReadString(reader); err != nil {
return nil, err
}
if msg.SheetID, err = ReadString(reader); err != nil {
return nil, err
}
if msg.BaseURL, err = ReadString(reader); err != nil {
return nil, err
}
return msg, err
}
func DecodeIOSBatchMeta(reader io.Reader) (Message, error) {
var err error = nil
msg := &IOSBatchMeta{}
@ -1954,6 +1972,9 @@ func ReadMessage(t uint64, reader io.Reader) (Message, error) {
case 79:
return DecodeZustand(reader)
case 83:
return DecodeReplaceVCSS(reader)
case 107:
return DecodeIOSBatchMeta(reader)

View file

@ -752,6 +752,16 @@ class Zustand(Message):
self.state = state
class ReplaceVCSS(Message):
__id__ = 83
def __init__(self, id, styles, sheet_id, base_url):
self.id = id
self.styles = styles
self.sheet_id = sheet_id
self.base_url = base_url
class IOSBatchMeta(Message):
__id__ = 107
@ -960,5 +970,3 @@ class IOSIssueEvent(Message):
self.context_string = context_string
self.context = context
self.payload = payload

View file

@ -668,6 +668,14 @@ class MessageCodec(Codec):
state=self.read_string(reader)
)
if message_id == 83:
return ReplaceVCSS(
id=self.read_uint(reader),
styles=self.read_string(reader),
sheet_id=self.read_string(reader),
base_url=self.read_string(reader)
)
if message_id == 107:
return IOSBatchMeta(
timestamp=self.read_uint(reader),
@ -839,4 +847,3 @@ class MessageCodec(Codec):
context=self.read_string(reader),
payload=self.read_string(reader)
)

View file

@ -537,6 +537,20 @@ export default class RawMessageReader extends PrimitiveReader {
};
}
case 83: {
const id = this.readUint(); if (id === null) { return resetPointer() }
const styles = this.readString(); if (styles === null) { return resetPointer() }
const sheetID = this.readString(); if (sheetID === null) { return resetPointer() }
const baseURL = this.readString(); if (baseURL === null) { return resetPointer() }
return {
tp: "replace_vcss",
id,
styles,
sheetID,
baseURL,
};
}
case 90: {
const timestamp = this.readUint(); if (timestamp === null) { return resetPointer() }
const projectID = this.readUint(); if (projectID === null) { return resetPointer() }

View file

@ -48,6 +48,7 @@ import type {
RawAdoptedSsAddOwner,
RawAdoptedSsRemoveOwner,
RawZustand,
RawReplaceVcss,
RawIosSessionStart,
RawIosCustomEvent,
RawIosScreenChanges,
@ -150,6 +151,8 @@ export type AdoptedSsRemoveOwner = RawAdoptedSsRemoveOwner & Timed
export type Zustand = RawZustand & Timed
export type ReplaceVcss = RawReplaceVcss & Timed
export type IosSessionStart = RawIosSessionStart & Timed
export type IosCustomEvent = RawIosCustomEvent & Timed

View file

@ -306,6 +306,14 @@ export interface RawZustand {
state: string,
}
export interface RawReplaceVcss {
tp: "replace_vcss",
id: number,
styles: string,
sheetID: string,
baseURL: string,
}
export interface RawIosSessionStart {
tp: "ios_session_start",
timestamp: number,
@ -377,4 +385,4 @@ export interface RawIosNetworkCall {
}
export type RawMessage = RawTimestamp | RawSetPageLocation | RawSetViewportSize | RawSetViewportScroll | RawCreateDocument | RawCreateElementNode | RawCreateTextNode | RawMoveNode | RawRemoveNode | RawSetNodeAttribute | RawRemoveNodeAttribute | RawSetNodeData | RawSetCssData | RawSetNodeScroll | RawSetInputValue | RawSetInputChecked | RawMouseMove | RawConsoleLog | RawCssInsertRule | RawCssDeleteRule | RawFetch | RawProfiler | RawOTable | RawRedux | RawVuex | RawMobX | RawNgRx | RawGraphQl | RawPerformanceTrack | RawConnectionInformation | RawSetPageVisibility | RawLongTask | RawSetNodeAttributeURLBased | RawSetCssDataURLBased | RawCssInsertRuleURLBased | RawMouseClick | RawCreateIFrameDocument | RawAdoptedSsReplaceURLBased | RawAdoptedSsReplace | RawAdoptedSsInsertRuleURLBased | RawAdoptedSsInsertRule | RawAdoptedSsDeleteRule | RawAdoptedSsAddOwner | RawAdoptedSsRemoveOwner | RawZustand | RawIosSessionStart | RawIosCustomEvent | RawIosScreenChanges | RawIosClickEvent | RawIosPerformanceEvent | RawIosLog | RawIosNetworkCall;
export type RawMessage = RawTimestamp | RawSetPageLocation | RawSetViewportSize | RawSetViewportScroll | RawCreateDocument | RawCreateElementNode | RawCreateTextNode | RawMoveNode | RawRemoveNode | RawSetNodeAttribute | RawRemoveNodeAttribute | RawSetNodeData | RawSetCssData | RawSetNodeScroll | RawSetInputValue | RawSetInputChecked | RawMouseMove | RawConsoleLog | RawCssInsertRule | RawCssDeleteRule | RawFetch | RawProfiler | RawOTable | RawRedux | RawVuex | RawMobX | RawNgRx | RawGraphQl | RawPerformanceTrack | RawConnectionInformation | RawSetPageVisibility | RawLongTask | RawSetNodeAttributeURLBased | RawSetCssDataURLBased | RawCssInsertRuleURLBased | RawMouseClick | RawCreateIFrameDocument | RawAdoptedSsReplaceURLBased | RawAdoptedSsReplace | RawAdoptedSsInsertRuleURLBased | RawAdoptedSsInsertRule | RawAdoptedSsDeleteRule | RawAdoptedSsAddOwner | RawAdoptedSsRemoveOwner | RawZustand | RawReplaceVcss | RawIosSessionStart | RawIosCustomEvent | RawIosScreenChanges | RawIosClickEvent | RawIosPerformanceEvent | RawIosLog | RawIosNetworkCall;

View file

@ -61,6 +61,7 @@ export const TP_MAP = {
76: "adopted_ss_add_owner",
77: "adopted_ss_remove_owner",
79: "zustand",
83: "replace_vcss",
90: "ios_session_start",
93: "ios_custom_event",
96: "ios_screen_changes",
@ -69,5 +70,3 @@ export const TP_MAP = {
103: "ios_log",
105: "ios_network_call",
} as const

View file

@ -388,8 +388,15 @@ type TrZustand = [
state: string,
]
type TrReplaceVCSS = [
type: 83,
id: number,
styles: string,
sheetID: string,
baseURL: string,
]
export type TrackerMessage = TrBatchMetadata | TrPartitionedMessage | TrTimestamp | TrSetPageLocation | TrSetViewportSize | TrSetViewportScroll | TrCreateDocument | TrCreateElementNode | TrCreateTextNode | TrMoveNode | TrRemoveNode | TrSetNodeAttribute | TrRemoveNodeAttribute | TrSetNodeData | TrSetNodeScroll | TrSetInputTarget | TrSetInputValue | TrSetInputChecked | TrMouseMove | TrConsoleLog | TrPageLoadTiming | TrPageRenderTiming | TrJSException | TrRawCustomEvent | TrUserID | TrUserAnonymousID | TrMetadata | TrCSSInsertRule | TrCSSDeleteRule | TrFetch | TrProfiler | TrOTable | TrStateAction | TrRedux | TrVuex | TrMobX | TrNgRx | TrGraphQL | TrPerformanceTrack | TrResourceTiming | TrConnectionInformation | TrSetPageVisibility | TrLongTask | TrSetNodeAttributeURLBased | TrSetCSSDataURLBased | TrTechnicalInfo | TrCustomIssue | TrCSSInsertRuleURLBased | TrMouseClick | TrCreateIFrameDocument | TrAdoptedSSReplaceURLBased | TrAdoptedSSInsertRuleURLBased | TrAdoptedSSDeleteRule | TrAdoptedSSAddOwner | TrAdoptedSSRemoveOwner | TrZustand
export type TrackerMessage = TrBatchMetadata | TrPartitionedMessage | TrTimestamp | TrSetPageLocation | TrSetViewportSize | TrSetViewportScroll | TrCreateDocument | TrCreateElementNode | TrCreateTextNode | TrMoveNode | TrRemoveNode | TrSetNodeAttribute | TrRemoveNodeAttribute | TrSetNodeData | TrSetNodeScroll | TrSetInputTarget | TrSetInputValue | TrSetInputChecked | TrMouseMove | TrConsoleLog | TrPageLoadTiming | TrPageRenderTiming | TrJSException | TrRawCustomEvent | TrUserID | TrUserAnonymousID | TrMetadata | TrCSSInsertRule | TrCSSDeleteRule | TrFetch | TrProfiler | TrOTable | TrStateAction | TrRedux | TrVuex | TrMobX | TrNgRx | TrGraphQL | TrPerformanceTrack | TrResourceTiming | TrConnectionInformation | TrSetPageVisibility | TrLongTask | TrSetNodeAttributeURLBased | TrSetCSSDataURLBased | TrTechnicalInfo | TrCustomIssue | TrCSSInsertRuleURLBased | TrMouseClick | TrCreateIFrameDocument | TrAdoptedSSReplaceURLBased | TrAdoptedSSInsertRuleURLBased | TrAdoptedSSDeleteRule | TrAdoptedSSAddOwner | TrAdoptedSSRemoveOwner | TrZustand | TrReplaceVCSS
export default function translate(tMsg: TrackerMessage): RawMessage | null {
switch(tMsg[0]) {
@ -764,6 +771,16 @@ export default function translate(tMsg: TrackerMessage): RawMessage | null {
}
}
case 83: {
return {
tp: "replace_vcss",
id: tMsg[1],
styles: tMsg[2],
sheetID: tMsg[3],
baseURL: tMsg[4],
}
}
default:
return null
}

View file

@ -460,3 +460,10 @@ message 79, 'Zustand' do
string 'Mutation'
string 'State'
end
message 83, 'ReplaceVCSS' do
uint 'ID'
string 'Styles'
string 'SheetID'
string 'BaseURL'
end

View file

@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tracker_1 = require("@openreplay/tracker/cjs");
const index_js_1 = require("./syncod/index.js");
function processMutationAndState(app, options, encoder, mutation, state) {
if (options.filter(mutation, state)) {
try {
const _mutation = encoder.encode(options.mutationTransformer(mutation));
const _state = encoder.encode(options.transformer(state));
const _table = encoder.commit();
for (let key in _table)
app.send(tracker_1.Messages.OTable(key, _table[key]));
app.send(tracker_1.Messages.Zustand(_mutation, _state));
}
catch (e) {
encoder.clear();
app.debug.error(e);
}
}
}
function default_1(opts = {}) {
const options = Object.assign({
filter: () => true,
transformer: state => state,
mutationTransformer: mutation => mutation,
}, opts);
return (app) => {
if (app === null) {
return Function.prototype;
}
const encoder = new index_js_1.Encoder(index_js_1.sha1, 50);
const state = {};
return (storeName = Math.random().toString(36).substring(2, 9)) => (config) => (set, get, api) => config((...args) => {
set(...args);
const newState = get();
state[storeName] = newState;
const triggeredActions = args.map(action => { var _a; return (_a = action.toString) === null || _a === void 0 ? void 0 : _a.call(action); });
processMutationAndState(app, options, encoder, triggeredActions, state);
}, get, api);
};
}
exports.default = default_1;

View file

@ -0,0 +1 @@
{ "type": "commonjs" }

View file

@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chars = {};
[
"DEL",
"UNDEF",
"TRUE",
"FALSE",
"NUMBER",
"BIGINT",
"FUNCTION",
"STRING",
"SYMBOL",
"NULL",
"OBJECT",
"ARRAY"
].forEach((k, i) => (chars[k] = String.fromCharCode(i + 0xe000)));
exports.default = chars;

View file

@ -0,0 +1,213 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chars_js_1 = require("./chars.js");
// @ts-ignore
// @ts-ignore
class Encoder {
// @ts-ignore
constructor(hash, slen = Infinity) {
// @ts-ignore
this._hash = hash;
// @ts-ignore
this._slen = slen;
// @ts-ignore
this._refmap = new Map();
// @ts-ignore
this._refset = new Set();
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_ref_str(str) {
// @ts-ignore
if (str.length < this._slen && str.indexOf(chars_js_1.default.DEL) === -1) {
// @ts-ignore
return str;
// @ts-ignore
}
// @ts-ignore
let ref = this._refmap.get(str);
// @ts-ignore
if (ref === undefined) {
// @ts-ignore
ref = this._hash(str);
// @ts-ignore
this._refmap.set(str, ref);
// @ts-ignore
}
// @ts-ignore
return ref;
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_prim(obj) {
// @ts-ignore
switch (typeof obj) {
// @ts-ignore
case "undefined":
// @ts-ignore
return chars_js_1.default.UNDEF;
// @ts-ignore
case "boolean":
// @ts-ignore
return obj ? chars_js_1.default.TRUE : chars_js_1.default.FALSE;
// @ts-ignore
case "number":
// @ts-ignore
return chars_js_1.default.NUMBER + obj.toString();
// @ts-ignore
case "bigint":
// @ts-ignore
return chars_js_1.default.BIGINT + obj.toString();
// @ts-ignore
case "function":
// @ts-ignore
return chars_js_1.default.FUNCTION;
// @ts-ignore
case "string":
// @ts-ignore
return chars_js_1.default.STRING + this._ref_str(obj);
// @ts-ignore
case "symbol":
// @ts-ignore
return chars_js_1.default.SYMBOL + this._ref_str(obj.toString().slice(7, -1));
// @ts-ignore
}
// @ts-ignore
if (obj === null) {
// @ts-ignore
return chars_js_1.default.NULL;
// @ts-ignore
}
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_obj(obj, ref = this._refmap.get(obj)) {
// @ts-ignore
return (Array.isArray(obj) ? chars_js_1.default.ARRAY : chars_js_1.default.OBJECT) + ref;
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_term(obj) {
// @ts-ignore
return this._encode_prim(obj) || this._encode_obj(obj);
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_deep(obj, depth) {
// @ts-ignore
const enc = this._encode_prim(obj);
// @ts-ignore
if (enc !== undefined) {
// @ts-ignore
return enc;
// @ts-ignore
}
// @ts-ignore
const ref = this._refmap.get(obj);
// @ts-ignore
switch (typeof ref) {
// @ts-ignore
case "number":
// @ts-ignore
return (depth - ref).toString();
// @ts-ignore
case "string":
// @ts-ignore
return this._encode_obj(obj, ref);
// @ts-ignore
}
// @ts-ignore
this._refmap.set(obj, depth);
// @ts-ignore
const hash = this._hash(
// @ts-ignore
(Array.isArray(obj)
// @ts-ignore
? obj.map(v => this._encode_deep(v, depth + 1))
// @ts-ignore
: Object.keys(obj)
// @ts-ignore
.sort()
// @ts-ignore
.map(
// @ts-ignore
k =>
// @ts-ignore
this._ref_str(k) + chars_js_1.default.DEL + this._encode_deep(obj[k], depth + 1)
// @ts-ignore
)
// @ts-ignore
).join(chars_js_1.default.DEL)
// @ts-ignore
);
// @ts-ignore
this._refmap.set(obj, hash);
// @ts-ignore
return this._encode_obj(obj, hash);
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
encode(obj) {
// @ts-ignore
return this._encode_deep(obj, 0);
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
commit() {
// @ts-ignore
const dict = {};
// @ts-ignore
this._refmap.forEach((ref, obj) => {
// @ts-ignore
if (this._refset.has(ref)) {
// @ts-ignore
return;
// @ts-ignore
}
// @ts-ignore
this._refset.add(ref);
// @ts-ignore
if (typeof obj !== "string") {
// @ts-ignore
obj = (Array.isArray(obj)
// @ts-ignore
? obj.map(v => this._encode_term(v))
// @ts-ignore
: Object.keys(obj).map(
// @ts-ignore
k => this._ref_str(k) + chars_js_1.default.DEL + this._encode_term(obj[k])
// @ts-ignore
)
// @ts-ignore
).join(chars_js_1.default.DEL);
// @ts-ignore
}
// @ts-ignore
dict[ref] = obj;
// @ts-ignore
});
// @ts-ignore
this._refmap.clear();
// @ts-ignore
return dict;
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
clear() {
// @ts-ignore
this._refmap.clear();
// @ts-ignore
this._refset.clear();
// @ts-ignore
}
}
exports.default = Encoder;
// @ts-ignore

View file

@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sha1 = exports.Encoder = void 0;
// TODO: SSR solution for all asayer libraries
const encoder_js_1 = require("./encoder.js");
exports.Encoder = encoder_js_1.default;
const sha1_js_1 = require("./sha1.js");
exports.sha1 = sha1_js_1.default;

View file

@ -0,0 +1,96 @@
"use strict";
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS PUB 180-1
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
Object.defineProperty(exports, "__esModule", { value: true });
function core_sha1(x, len) {
x[len >> 5] |= 0x80 << (24 - (len % 32));
x[(((len + 64) >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16)
w[j] = x[i + j];
else
w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
function sha1_ft(t, b, c, d) {
if (t < 20)
return (b & c) | (~b & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
function sha1_kt(t) {
return t < 20
? 1518500249
: t < 40
? 1859775393
: t < 60
? -1894007588
: -899497514;
}
function safe_add(x, y) {
var lsw = (x & 0xffff) + (y & 0xffff);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xffff);
}
function rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
function str2binb(str) {
var bin = Array();
var mask = (1 << 16) - 1;
for (var i = 0; i < str.length * 16; i += 16)
bin[i >> 5] |= (str.charCodeAt(i / 16) & mask) << (32 - 16 - (i % 32));
return bin;
}
function binb2b64(binarray) {
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for (var i = 0; i < binarray.length * 4; i += 3) {
var triplet = (((binarray[i >> 2] >> (8 * (3 - (i % 4)))) & 0xff) << 16) |
(((binarray[(i + 1) >> 2] >> (8 * (3 - ((i + 1) % 4)))) & 0xff) << 8) |
((binarray[(i + 2) >> 2] >> (8 * (3 - ((i + 2) % 4)))) & 0xff);
for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 <= binarray.length * 32)
str += tab.charAt((triplet >> (6 * (3 - j))) & 0x3f);
}
}
return str;
}
function default_1(s) {
return binb2b64(core_sha1(str2binb(s), s.length * 16));
}
exports.default = default_1;

View file

@ -0,0 +1,7 @@
import { App } from "@openreplay/tracker";
export interface Options {
filter: (mutation: any, state: any) => boolean;
transformer: (state: any) => any;
mutationTransformer: (mutation: any) => any;
}
export default function (opts?: Partial<Options>): (app: App | null) => Function;

View file

@ -0,0 +1,39 @@
import { Messages } from "@openreplay/tracker";
import { Encoder, sha1 } from "./syncod/index.js";
function processMutationAndState(app, options, encoder, mutation, state) {
if (options.filter(mutation, state)) {
try {
const _mutation = encoder.encode(options.mutationTransformer(mutation));
const _state = encoder.encode(options.transformer(state));
const _table = encoder.commit();
for (let key in _table)
app.send(Messages.OTable(key, _table[key]));
app.send(Messages.Zustand(_mutation, _state));
}
catch (e) {
encoder.clear();
app.debug.error(e);
}
}
}
export default function (opts = {}) {
const options = Object.assign({
filter: () => true,
transformer: state => state,
mutationTransformer: mutation => mutation,
}, opts);
return (app) => {
if (app === null) {
return Function.prototype;
}
const encoder = new Encoder(sha1, 50);
const state = {};
return (storeName = Math.random().toString(36).substring(2, 9)) => (config) => (set, get, api) => config((...args) => {
set(...args);
const newState = get();
state[storeName] = newState;
const triggeredActions = args.map(action => { var _a; return (_a = action.toString) === null || _a === void 0 ? void 0 : _a.call(action); });
processMutationAndState(app, options, encoder, triggeredActions, state);
}, get, api);
};
}

View file

@ -0,0 +1,2 @@
declare const chars: {};
export default chars;

View file

@ -0,0 +1,16 @@
const chars = {};
[
"DEL",
"UNDEF",
"TRUE",
"FALSE",
"NUMBER",
"BIGINT",
"FUNCTION",
"STRING",
"SYMBOL",
"NULL",
"OBJECT",
"ARRAY"
].forEach((k, i) => (chars[k] = String.fromCharCode(i + 0xe000)));
export default chars;

View file

@ -0,0 +1,11 @@
export default class Encoder {
constructor(hash: any, slen?: number);
_ref_str(str: any): any;
_encode_prim(obj: any): any;
_encode_obj(obj: any, ref?: any): any;
_encode_term(obj: any): any;
_encode_deep(obj: any, depth: any): any;
encode(obj: any): any;
commit(): {};
clear(): void;
}

View file

@ -0,0 +1,210 @@
import _ from "./chars.js";
// @ts-ignore
// @ts-ignore
export default class Encoder {
// @ts-ignore
constructor(hash, slen = Infinity) {
// @ts-ignore
this._hash = hash;
// @ts-ignore
this._slen = slen;
// @ts-ignore
this._refmap = new Map();
// @ts-ignore
this._refset = new Set();
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_ref_str(str) {
// @ts-ignore
if (str.length < this._slen && str.indexOf(_.DEL) === -1) {
// @ts-ignore
return str;
// @ts-ignore
}
// @ts-ignore
let ref = this._refmap.get(str);
// @ts-ignore
if (ref === undefined) {
// @ts-ignore
ref = this._hash(str);
// @ts-ignore
this._refmap.set(str, ref);
// @ts-ignore
}
// @ts-ignore
return ref;
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_prim(obj) {
// @ts-ignore
switch (typeof obj) {
// @ts-ignore
case "undefined":
// @ts-ignore
return _.UNDEF;
// @ts-ignore
case "boolean":
// @ts-ignore
return obj ? _.TRUE : _.FALSE;
// @ts-ignore
case "number":
// @ts-ignore
return _.NUMBER + obj.toString();
// @ts-ignore
case "bigint":
// @ts-ignore
return _.BIGINT + obj.toString();
// @ts-ignore
case "function":
// @ts-ignore
return _.FUNCTION;
// @ts-ignore
case "string":
// @ts-ignore
return _.STRING + this._ref_str(obj);
// @ts-ignore
case "symbol":
// @ts-ignore
return _.SYMBOL + this._ref_str(obj.toString().slice(7, -1));
// @ts-ignore
}
// @ts-ignore
if (obj === null) {
// @ts-ignore
return _.NULL;
// @ts-ignore
}
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_obj(obj, ref = this._refmap.get(obj)) {
// @ts-ignore
return (Array.isArray(obj) ? _.ARRAY : _.OBJECT) + ref;
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_term(obj) {
// @ts-ignore
return this._encode_prim(obj) || this._encode_obj(obj);
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
_encode_deep(obj, depth) {
// @ts-ignore
const enc = this._encode_prim(obj);
// @ts-ignore
if (enc !== undefined) {
// @ts-ignore
return enc;
// @ts-ignore
}
// @ts-ignore
const ref = this._refmap.get(obj);
// @ts-ignore
switch (typeof ref) {
// @ts-ignore
case "number":
// @ts-ignore
return (depth - ref).toString();
// @ts-ignore
case "string":
// @ts-ignore
return this._encode_obj(obj, ref);
// @ts-ignore
}
// @ts-ignore
this._refmap.set(obj, depth);
// @ts-ignore
const hash = this._hash(
// @ts-ignore
(Array.isArray(obj)
// @ts-ignore
? obj.map(v => this._encode_deep(v, depth + 1))
// @ts-ignore
: Object.keys(obj)
// @ts-ignore
.sort()
// @ts-ignore
.map(
// @ts-ignore
k =>
// @ts-ignore
this._ref_str(k) + _.DEL + this._encode_deep(obj[k], depth + 1)
// @ts-ignore
)
// @ts-ignore
).join(_.DEL)
// @ts-ignore
);
// @ts-ignore
this._refmap.set(obj, hash);
// @ts-ignore
return this._encode_obj(obj, hash);
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
encode(obj) {
// @ts-ignore
return this._encode_deep(obj, 0);
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
commit() {
// @ts-ignore
const dict = {};
// @ts-ignore
this._refmap.forEach((ref, obj) => {
// @ts-ignore
if (this._refset.has(ref)) {
// @ts-ignore
return;
// @ts-ignore
}
// @ts-ignore
this._refset.add(ref);
// @ts-ignore
if (typeof obj !== "string") {
// @ts-ignore
obj = (Array.isArray(obj)
// @ts-ignore
? obj.map(v => this._encode_term(v))
// @ts-ignore
: Object.keys(obj).map(
// @ts-ignore
k => this._ref_str(k) + _.DEL + this._encode_term(obj[k])
// @ts-ignore
)
// @ts-ignore
).join(_.DEL);
// @ts-ignore
}
// @ts-ignore
dict[ref] = obj;
// @ts-ignore
});
// @ts-ignore
this._refmap.clear();
// @ts-ignore
return dict;
// @ts-ignore
}
// @ts-ignore
// @ts-ignore
clear() {
// @ts-ignore
this._refmap.clear();
// @ts-ignore
this._refset.clear();
// @ts-ignore
}
}
// @ts-ignore

View file

@ -0,0 +1,3 @@
import Encoder from "./encoder.js";
import sha1 from "./sha1.js";
export { Encoder, sha1 };

View file

@ -0,0 +1,4 @@
// TODO: SSR solution for all asayer libraries
import Encoder from "./encoder.js";
import sha1 from "./sha1.js";
export { Encoder, sha1 };

View file

@ -0,0 +1 @@
export default function (s: any): string;

View file

@ -0,0 +1,93 @@
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS PUB 180-1
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
function core_sha1(x, len) {
x[len >> 5] |= 0x80 << (24 - (len % 32));
x[(((len + 64) >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16)
w[j] = x[i + j];
else
w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
function sha1_ft(t, b, c, d) {
if (t < 20)
return (b & c) | (~b & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
function sha1_kt(t) {
return t < 20
? 1518500249
: t < 40
? 1859775393
: t < 60
? -1894007588
: -899497514;
}
function safe_add(x, y) {
var lsw = (x & 0xffff) + (y & 0xffff);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xffff);
}
function rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
function str2binb(str) {
var bin = Array();
var mask = (1 << 16) - 1;
for (var i = 0; i < str.length * 16; i += 16)
bin[i >> 5] |= (str.charCodeAt(i / 16) & mask) << (32 - 16 - (i % 32));
return bin;
}
function binb2b64(binarray) {
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for (var i = 0; i < binarray.length * 4; i += 3) {
var triplet = (((binarray[i >> 2] >> (8 * (3 - (i % 4)))) & 0xff) << 16) |
(((binarray[(i + 1) >> 2] >> (8 * (3 - ((i + 1) % 4)))) & 0xff) << 8) |
((binarray[(i + 2) >> 2] >> (8 * (3 - ((i + 2) % 4)))) & 0xff);
for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 <= binarray.length * 32)
str += tab.charAt((triplet >> (6 * (3 - j))) & 0x3f);
}
}
return str;
}
export default function (s) {
return binb2b64(core_sha1(str2binb(s), s.length * 16));
}

View file

@ -57,6 +57,7 @@ export declare const enum Type {
AdoptedSSAddOwner = 76,
AdoptedSSRemoveOwner = 77,
Zustand = 79,
ReplaceVCSS = 83,
}
@ -445,6 +446,15 @@ export type Zustand = [
/*state:*/ string,
]
export type ReplaceVCSS = [
/*type:*/ Type.ReplaceVCSS,
/*id:*/ number,
/*styles:*/ string,
/*sheetID:*/ string,
/*baseURL:*/ string,
]
type Message = BatchMetadata | PartitionedMessage | Timestamp | SetPageLocation | SetViewportSize | SetViewportScroll | CreateDocument | CreateElementNode | CreateTextNode | MoveNode | RemoveNode | SetNodeAttribute | RemoveNodeAttribute | SetNodeData | SetNodeScroll | SetInputTarget | SetInputValue | SetInputChecked | MouseMove | ConsoleLog | PageLoadTiming | PageRenderTiming | JSException | RawCustomEvent | UserID | UserAnonymousID | Metadata | CSSInsertRule | CSSDeleteRule | Fetch | Profiler | OTable | StateAction | Redux | Vuex | MobX | NgRx | GraphQL | PerformanceTrack | ResourceTiming | ConnectionInformation | SetPageVisibility | LongTask | SetNodeAttributeURLBased | SetCSSDataURLBased | TechnicalInfo | CustomIssue | CSSInsertRuleURLBased | MouseClick | CreateIFrameDocument | AdoptedSSReplaceURLBased | AdoptedSSInsertRuleURLBased | AdoptedSSDeleteRule | AdoptedSSAddOwner | AdoptedSSRemoveOwner | Zustand | ReplaceVCSS
type Message = BatchMetadata | PartitionedMessage | Timestamp | SetPageLocation | SetViewportSize | SetViewportScroll | CreateDocument | CreateElementNode | CreateTextNode | MoveNode | RemoveNode | SetNodeAttribute | RemoveNodeAttribute | SetNodeData | SetNodeScroll | SetInputTarget | SetInputValue | SetInputChecked | MouseMove | ConsoleLog | PageLoadTiming | PageRenderTiming | JSException | RawCustomEvent | UserID | UserAnonymousID | Metadata | CSSInsertRule | CSSDeleteRule | Fetch | Profiler | OTable | StateAction | Redux | Vuex | MobX | NgRx | GraphQL | PerformanceTrack | ResourceTiming | ConnectionInformation | SetPageVisibility | LongTask | SetNodeAttributeURLBased | SetCSSDataURLBased | TechnicalInfo | CustomIssue | CSSInsertRuleURLBased | MouseClick | CreateIFrameDocument | AdoptedSSReplaceURLBased | AdoptedSSInsertRuleURLBased | AdoptedSSDeleteRule | AdoptedSSAddOwner | AdoptedSSRemoveOwner | Zustand
export default Message

View file

@ -718,3 +718,17 @@ export function Zustand(
]
}
export function ReplaceVCSS(
id: number,
styles: string,
sheetID: string,
baseURL: string,
): Messages.ReplaceVCSS {
return [
Messages.Type.ReplaceVCSS,
id,
styles,
sheetID,
baseURL,
]
}

View file

@ -1,5 +1,10 @@
import type App from '../app/index.js'
import { CSSInsertRuleURLBased, CSSDeleteRule, TechnicalInfo } from '../app/messages.gen.js'
import {
CSSInsertRuleURLBased,
CSSDeleteRule,
TechnicalInfo,
ReplaceVCSS,
} from '../app/messages.gen.js'
import { hasTag } from '../app/guards.js'
export default function (app: App | null) {
@ -11,8 +16,7 @@ export default function (app: App | null) {
return
}
const processOperation = app.safe(
(stylesheet: CSSStyleSheet | CSSGroupingRule, index: number, rule?: string) => {
const processOperation = app.safe((stylesheet: CSSStyleSheet, index: number, rule?: string) => {
const sendMessage =
typeof rule === 'string'
? (nodeID: number) =>
@ -26,8 +30,32 @@ export default function (app: App | null) {
if (nodeID !== undefined) {
sendMessage(nodeID)
} // else error?
},
)
})
const replaceVirtualCss = app.safe((ctx: CSSGroupingRule) => {
let uppermostRuleset = ctx.parentRule
while (uppermostRuleset?.parentRule) {
uppermostRuleset = uppermostRuleset.parentRule
}
if (uppermostRuleset?.parentStyleSheet?.ownerNode) {
const entireStyle = uppermostRuleset.cssText
const parentNodeID = app.nodes.getID(uppermostRuleset.parentStyleSheet.ownerNode)
const ruleList = uppermostRuleset.parentStyleSheet.cssRules
let id = -1
for (let i = 0; i < ruleList.length; i++) {
const rule = ruleList.item(i)
if (rule === uppermostRuleset) {
id = i
break
}
}
if (parentNodeID && id >= 0) {
app.send(ReplaceVCSS(parentNodeID, entireStyle, id.toString(), app.getBaseHref()))
}
} else {
app.debug.error('Owner Node not found')
}
})
const patchContext = (context: typeof globalThis) => {
const { insertRule, deleteRule } = context.CSSStyleSheet.prototype
@ -46,15 +74,15 @@ export default function (app: App | null) {
context.CSSGroupingRule.prototype.insertRule = function (rule: string, index = 0) {
const result = groupInsertRule.call(this, rule, index) as number
const entireStyle = this.parentStyleSheet?.cssRules
// const nodeID = getID(this.parentStyleSheet?.ownerNode)
// app.send(updateGroup(nodeID, entireStyle.csstext))
replaceVirtualCss(this)
return result
}
context.CSSGroupingRule.prototype.deleteRule = function (index = 0) {
const result = groupDeleteRule.call(this, index) as number
replaceVirtualCss(this)
return result
}
}

View file

@ -233,8 +233,11 @@ export default class MessageEncoder extends PrimitiveEncoder {
return this.string(msg[1]) && this.string(msg[2])
break
case Messages.Type.ReplaceVCSS:
return this.uint(msg[1]) && this.string(msg[2]) && this.string(msg[3]) && this.string(msg[4])
break
}
}
}