@zk-tech/rrt-bridge 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/dist/config.d.ts +3 -0
- package/dist/index.cjs +1121 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1121 -0
- package/dist/index.js.map +1 -0
- package/dist/rpc/broadcast/index.d.ts +2 -0
- package/dist/rpc/index.d.ts +6 -0
- package/dist/rpc/message/client.d.ts +2 -0
- package/dist/rpc/message/context.d.ts +2 -0
- package/dist/rpc/message/index.d.ts +3 -0
- package/dist/rpc/message/server.d.ts +2 -0
- package/dist/rpc/native/index.d.ts +2 -0
- package/dist/types/channel.d.ts +13 -0
- package/dist/types/functions.d.ts +39 -0
- package/dist/types/index.d.ts +4 -0
- package/package.json +43 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1121 @@
|
|
|
1
|
+
const DEFAULT_TIMEOUT = 6e4;
|
|
2
|
+
function defaultSerialize(i) {
|
|
3
|
+
return i;
|
|
4
|
+
}
|
|
5
|
+
const defaultDeserialize = defaultSerialize;
|
|
6
|
+
const { clearTimeout: clearTimeout$1, setTimeout: setTimeout$1 } = globalThis;
|
|
7
|
+
const random = Math.random.bind(Math);
|
|
8
|
+
function createBirpc(functions, options) {
|
|
9
|
+
const {
|
|
10
|
+
post,
|
|
11
|
+
on,
|
|
12
|
+
eventNames = [],
|
|
13
|
+
serialize = defaultSerialize,
|
|
14
|
+
deserialize = defaultDeserialize,
|
|
15
|
+
resolver,
|
|
16
|
+
timeout = DEFAULT_TIMEOUT
|
|
17
|
+
} = options;
|
|
18
|
+
const rpcPromiseMap = /* @__PURE__ */ new Map();
|
|
19
|
+
let _promise;
|
|
20
|
+
const rpc = new Proxy({}, {
|
|
21
|
+
get(_, method) {
|
|
22
|
+
if (method === "$functions")
|
|
23
|
+
return functions;
|
|
24
|
+
if (method === "then" && !eventNames.includes("then") && !("then" in functions))
|
|
25
|
+
return void 0;
|
|
26
|
+
const sendEvent = (...args) => {
|
|
27
|
+
post(serialize({ m: method, a: args, t: "q" }));
|
|
28
|
+
};
|
|
29
|
+
if (eventNames.includes(method)) {
|
|
30
|
+
sendEvent.asEvent = sendEvent;
|
|
31
|
+
return sendEvent;
|
|
32
|
+
}
|
|
33
|
+
const sendCall = async (...args) => {
|
|
34
|
+
await _promise;
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
var _a;
|
|
37
|
+
const id = nanoid();
|
|
38
|
+
let timeoutId;
|
|
39
|
+
if (timeout >= 0) {
|
|
40
|
+
timeoutId = setTimeout$1(() => {
|
|
41
|
+
var _a2;
|
|
42
|
+
try {
|
|
43
|
+
(_a2 = options.onTimeoutError) == null ? void 0 : _a2.call(options, method, args);
|
|
44
|
+
throw new Error(`[birpc] timeout on calling "${method}"`);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
reject(e);
|
|
47
|
+
}
|
|
48
|
+
rpcPromiseMap.delete(id);
|
|
49
|
+
}, timeout);
|
|
50
|
+
if (typeof timeoutId === "object")
|
|
51
|
+
timeoutId = (_a = timeoutId.unref) == null ? void 0 : _a.call(timeoutId);
|
|
52
|
+
}
|
|
53
|
+
rpcPromiseMap.set(id, { resolve, reject, timeoutId });
|
|
54
|
+
post(serialize({ m: method, a: args, i: id, t: "q" }));
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
sendCall.asEvent = sendEvent;
|
|
58
|
+
return sendCall;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
_promise = on(async (data, ...extra) => {
|
|
62
|
+
const msg = deserialize(data);
|
|
63
|
+
if (msg.t === "q") {
|
|
64
|
+
const { m: method, a: args } = msg;
|
|
65
|
+
let result, error;
|
|
66
|
+
const fn = resolver ? resolver(method, functions[method]) : functions[method];
|
|
67
|
+
if (!fn) {
|
|
68
|
+
error = new Error(`[birpc] function "${method}" not found`);
|
|
69
|
+
} else {
|
|
70
|
+
try {
|
|
71
|
+
result = await fn.apply(rpc, args);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
error = e;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (msg.i) {
|
|
77
|
+
if (error && options.onError)
|
|
78
|
+
options.onError(error, method, args);
|
|
79
|
+
post(serialize({ t: "s", i: msg.i, r: result, e: error }), ...extra);
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
const { i: ack, r: result, e: error } = msg;
|
|
83
|
+
const promise = rpcPromiseMap.get(ack);
|
|
84
|
+
if (promise) {
|
|
85
|
+
clearTimeout$1(promise.timeoutId);
|
|
86
|
+
if (error)
|
|
87
|
+
promise.reject(error);
|
|
88
|
+
else
|
|
89
|
+
promise.resolve(result);
|
|
90
|
+
}
|
|
91
|
+
rpcPromiseMap.delete(ack);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return rpc;
|
|
95
|
+
}
|
|
96
|
+
const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
97
|
+
function nanoid(size = 21) {
|
|
98
|
+
let id = "";
|
|
99
|
+
let i = size;
|
|
100
|
+
while (i--)
|
|
101
|
+
id += urlAlphabet[random() * 64 | 0];
|
|
102
|
+
return id;
|
|
103
|
+
}
|
|
104
|
+
class DoubleIndexedKV {
|
|
105
|
+
constructor() {
|
|
106
|
+
this.keyToValue = /* @__PURE__ */ new Map();
|
|
107
|
+
this.valueToKey = /* @__PURE__ */ new Map();
|
|
108
|
+
}
|
|
109
|
+
set(key, value) {
|
|
110
|
+
this.keyToValue.set(key, value);
|
|
111
|
+
this.valueToKey.set(value, key);
|
|
112
|
+
}
|
|
113
|
+
getByKey(key) {
|
|
114
|
+
return this.keyToValue.get(key);
|
|
115
|
+
}
|
|
116
|
+
getByValue(value) {
|
|
117
|
+
return this.valueToKey.get(value);
|
|
118
|
+
}
|
|
119
|
+
clear() {
|
|
120
|
+
this.keyToValue.clear();
|
|
121
|
+
this.valueToKey.clear();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
class Registry {
|
|
125
|
+
constructor(generateIdentifier) {
|
|
126
|
+
this.generateIdentifier = generateIdentifier;
|
|
127
|
+
this.kv = new DoubleIndexedKV();
|
|
128
|
+
}
|
|
129
|
+
register(value, identifier) {
|
|
130
|
+
if (this.kv.getByValue(value)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (!identifier) {
|
|
134
|
+
identifier = this.generateIdentifier(value);
|
|
135
|
+
}
|
|
136
|
+
this.kv.set(identifier, value);
|
|
137
|
+
}
|
|
138
|
+
clear() {
|
|
139
|
+
this.kv.clear();
|
|
140
|
+
}
|
|
141
|
+
getIdentifier(value) {
|
|
142
|
+
return this.kv.getByValue(value);
|
|
143
|
+
}
|
|
144
|
+
getValue(identifier) {
|
|
145
|
+
return this.kv.getByKey(identifier);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
class ClassRegistry extends Registry {
|
|
149
|
+
constructor() {
|
|
150
|
+
super((c) => c.name);
|
|
151
|
+
this.classToAllowedProps = /* @__PURE__ */ new Map();
|
|
152
|
+
}
|
|
153
|
+
register(value, options) {
|
|
154
|
+
if (typeof options === "object") {
|
|
155
|
+
if (options.allowProps) {
|
|
156
|
+
this.classToAllowedProps.set(value, options.allowProps);
|
|
157
|
+
}
|
|
158
|
+
super.register(value, options.identifier);
|
|
159
|
+
} else {
|
|
160
|
+
super.register(value, options);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
getAllowedProps(value) {
|
|
164
|
+
return this.classToAllowedProps.get(value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function valuesOfObj(record) {
|
|
168
|
+
if ("values" in Object) {
|
|
169
|
+
return Object.values(record);
|
|
170
|
+
}
|
|
171
|
+
const values = [];
|
|
172
|
+
for (const key in record) {
|
|
173
|
+
if (record.hasOwnProperty(key)) {
|
|
174
|
+
values.push(record[key]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return values;
|
|
178
|
+
}
|
|
179
|
+
function find(record, predicate) {
|
|
180
|
+
const values = valuesOfObj(record);
|
|
181
|
+
if ("find" in values) {
|
|
182
|
+
return values.find(predicate);
|
|
183
|
+
}
|
|
184
|
+
const valuesNotNever = values;
|
|
185
|
+
for (let i = 0; i < valuesNotNever.length; i++) {
|
|
186
|
+
const value = valuesNotNever[i];
|
|
187
|
+
if (predicate(value)) {
|
|
188
|
+
return value;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return void 0;
|
|
192
|
+
}
|
|
193
|
+
function forEach(record, run) {
|
|
194
|
+
Object.entries(record).forEach(([key, value]) => run(value, key));
|
|
195
|
+
}
|
|
196
|
+
function includes(arr, value) {
|
|
197
|
+
return arr.indexOf(value) !== -1;
|
|
198
|
+
}
|
|
199
|
+
function findArr(record, predicate) {
|
|
200
|
+
for (let i = 0; i < record.length; i++) {
|
|
201
|
+
const value = record[i];
|
|
202
|
+
if (predicate(value)) {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return void 0;
|
|
207
|
+
}
|
|
208
|
+
class CustomTransformerRegistry {
|
|
209
|
+
constructor() {
|
|
210
|
+
this.transfomers = {};
|
|
211
|
+
}
|
|
212
|
+
register(transformer) {
|
|
213
|
+
this.transfomers[transformer.name] = transformer;
|
|
214
|
+
}
|
|
215
|
+
findApplicable(v) {
|
|
216
|
+
return find(this.transfomers, (transformer) => transformer.isApplicable(v));
|
|
217
|
+
}
|
|
218
|
+
findByName(name) {
|
|
219
|
+
return this.transfomers[name];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const getType$1 = (payload) => Object.prototype.toString.call(payload).slice(8, -1);
|
|
223
|
+
const isUndefined = (payload) => typeof payload === "undefined";
|
|
224
|
+
const isNull = (payload) => payload === null;
|
|
225
|
+
const isPlainObject$1 = (payload) => {
|
|
226
|
+
if (typeof payload !== "object" || payload === null)
|
|
227
|
+
return false;
|
|
228
|
+
if (payload === Object.prototype)
|
|
229
|
+
return false;
|
|
230
|
+
if (Object.getPrototypeOf(payload) === null)
|
|
231
|
+
return true;
|
|
232
|
+
return Object.getPrototypeOf(payload) === Object.prototype;
|
|
233
|
+
};
|
|
234
|
+
const isEmptyObject = (payload) => isPlainObject$1(payload) && Object.keys(payload).length === 0;
|
|
235
|
+
const isArray$1 = (payload) => Array.isArray(payload);
|
|
236
|
+
const isString = (payload) => typeof payload === "string";
|
|
237
|
+
const isNumber = (payload) => typeof payload === "number" && !isNaN(payload);
|
|
238
|
+
const isBoolean = (payload) => typeof payload === "boolean";
|
|
239
|
+
const isRegExp = (payload) => payload instanceof RegExp;
|
|
240
|
+
const isMap = (payload) => payload instanceof Map;
|
|
241
|
+
const isSet = (payload) => payload instanceof Set;
|
|
242
|
+
const isSymbol = (payload) => getType$1(payload) === "Symbol";
|
|
243
|
+
const isDate = (payload) => payload instanceof Date && !isNaN(payload.valueOf());
|
|
244
|
+
const isError = (payload) => payload instanceof Error;
|
|
245
|
+
const isNaNValue = (payload) => typeof payload === "number" && isNaN(payload);
|
|
246
|
+
const isPrimitive = (payload) => isBoolean(payload) || isNull(payload) || isUndefined(payload) || isNumber(payload) || isString(payload) || isSymbol(payload);
|
|
247
|
+
const isBigint = (payload) => typeof payload === "bigint";
|
|
248
|
+
const isInfinite = (payload) => payload === Infinity || payload === -Infinity;
|
|
249
|
+
const isTypedArray = (payload) => ArrayBuffer.isView(payload) && !(payload instanceof DataView);
|
|
250
|
+
const isURL = (payload) => payload instanceof URL;
|
|
251
|
+
const escapeKey = (key) => key.replace(/\./g, "\\.");
|
|
252
|
+
const stringifyPath = (path) => path.map(String).map(escapeKey).join(".");
|
|
253
|
+
const parsePath = (string) => {
|
|
254
|
+
const result = [];
|
|
255
|
+
let segment = "";
|
|
256
|
+
for (let i = 0; i < string.length; i++) {
|
|
257
|
+
let char = string.charAt(i);
|
|
258
|
+
const isEscapedDot = char === "\\" && string.charAt(i + 1) === ".";
|
|
259
|
+
if (isEscapedDot) {
|
|
260
|
+
segment += ".";
|
|
261
|
+
i++;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const isEndOfSegment = char === ".";
|
|
265
|
+
if (isEndOfSegment) {
|
|
266
|
+
result.push(segment);
|
|
267
|
+
segment = "";
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
segment += char;
|
|
271
|
+
}
|
|
272
|
+
const lastSegment = segment;
|
|
273
|
+
result.push(lastSegment);
|
|
274
|
+
return result;
|
|
275
|
+
};
|
|
276
|
+
function simpleTransformation(isApplicable, annotation, transform, untransform) {
|
|
277
|
+
return {
|
|
278
|
+
isApplicable,
|
|
279
|
+
annotation,
|
|
280
|
+
transform,
|
|
281
|
+
untransform
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const simpleRules = [
|
|
285
|
+
simpleTransformation(isUndefined, "undefined", () => null, () => void 0),
|
|
286
|
+
simpleTransformation(isBigint, "bigint", (v) => v.toString(), (v) => {
|
|
287
|
+
if (typeof BigInt !== "undefined") {
|
|
288
|
+
return BigInt(v);
|
|
289
|
+
}
|
|
290
|
+
console.error("Please add a BigInt polyfill.");
|
|
291
|
+
return v;
|
|
292
|
+
}),
|
|
293
|
+
simpleTransformation(isDate, "Date", (v) => v.toISOString(), (v) => new Date(v)),
|
|
294
|
+
simpleTransformation(isError, "Error", (v, superJson) => {
|
|
295
|
+
const baseError = {
|
|
296
|
+
name: v.name,
|
|
297
|
+
message: v.message
|
|
298
|
+
};
|
|
299
|
+
superJson.allowedErrorProps.forEach((prop) => {
|
|
300
|
+
baseError[prop] = v[prop];
|
|
301
|
+
});
|
|
302
|
+
return baseError;
|
|
303
|
+
}, (v, superJson) => {
|
|
304
|
+
const e = new Error(v.message);
|
|
305
|
+
e.name = v.name;
|
|
306
|
+
e.stack = v.stack;
|
|
307
|
+
superJson.allowedErrorProps.forEach((prop) => {
|
|
308
|
+
e[prop] = v[prop];
|
|
309
|
+
});
|
|
310
|
+
return e;
|
|
311
|
+
}),
|
|
312
|
+
simpleTransformation(isRegExp, "regexp", (v) => "" + v, (regex) => {
|
|
313
|
+
const body = regex.slice(1, regex.lastIndexOf("/"));
|
|
314
|
+
const flags = regex.slice(regex.lastIndexOf("/") + 1);
|
|
315
|
+
return new RegExp(body, flags);
|
|
316
|
+
}),
|
|
317
|
+
simpleTransformation(
|
|
318
|
+
isSet,
|
|
319
|
+
"set",
|
|
320
|
+
// (sets only exist in es6+)
|
|
321
|
+
// eslint-disable-next-line es5/no-es6-methods
|
|
322
|
+
(v) => [...v.values()],
|
|
323
|
+
(v) => new Set(v)
|
|
324
|
+
),
|
|
325
|
+
simpleTransformation(isMap, "map", (v) => [...v.entries()], (v) => new Map(v)),
|
|
326
|
+
simpleTransformation((v) => isNaNValue(v) || isInfinite(v), "number", (v) => {
|
|
327
|
+
if (isNaNValue(v)) {
|
|
328
|
+
return "NaN";
|
|
329
|
+
}
|
|
330
|
+
if (v > 0) {
|
|
331
|
+
return "Infinity";
|
|
332
|
+
} else {
|
|
333
|
+
return "-Infinity";
|
|
334
|
+
}
|
|
335
|
+
}, Number),
|
|
336
|
+
simpleTransformation((v) => v === 0 && 1 / v === -Infinity, "number", () => {
|
|
337
|
+
return "-0";
|
|
338
|
+
}, Number),
|
|
339
|
+
simpleTransformation(isURL, "URL", (v) => v.toString(), (v) => new URL(v))
|
|
340
|
+
];
|
|
341
|
+
function compositeTransformation(isApplicable, annotation, transform, untransform) {
|
|
342
|
+
return {
|
|
343
|
+
isApplicable,
|
|
344
|
+
annotation,
|
|
345
|
+
transform,
|
|
346
|
+
untransform
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
const symbolRule = compositeTransformation((s, superJson) => {
|
|
350
|
+
if (isSymbol(s)) {
|
|
351
|
+
const isRegistered = !!superJson.symbolRegistry.getIdentifier(s);
|
|
352
|
+
return isRegistered;
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
}, (s, superJson) => {
|
|
356
|
+
const identifier = superJson.symbolRegistry.getIdentifier(s);
|
|
357
|
+
return ["symbol", identifier];
|
|
358
|
+
}, (v) => v.description, (_, a, superJson) => {
|
|
359
|
+
const value = superJson.symbolRegistry.getValue(a[1]);
|
|
360
|
+
if (!value) {
|
|
361
|
+
throw new Error("Trying to deserialize unknown symbol");
|
|
362
|
+
}
|
|
363
|
+
return value;
|
|
364
|
+
});
|
|
365
|
+
const constructorToName = [
|
|
366
|
+
Int8Array,
|
|
367
|
+
Uint8Array,
|
|
368
|
+
Int16Array,
|
|
369
|
+
Uint16Array,
|
|
370
|
+
Int32Array,
|
|
371
|
+
Uint32Array,
|
|
372
|
+
Float32Array,
|
|
373
|
+
Float64Array,
|
|
374
|
+
Uint8ClampedArray
|
|
375
|
+
].reduce((obj, ctor) => {
|
|
376
|
+
obj[ctor.name] = ctor;
|
|
377
|
+
return obj;
|
|
378
|
+
}, {});
|
|
379
|
+
const typedArrayRule = compositeTransformation(isTypedArray, (v) => ["typed-array", v.constructor.name], (v) => [...v], (v, a) => {
|
|
380
|
+
const ctor = constructorToName[a[1]];
|
|
381
|
+
if (!ctor) {
|
|
382
|
+
throw new Error("Trying to deserialize unknown typed array");
|
|
383
|
+
}
|
|
384
|
+
return new ctor(v);
|
|
385
|
+
});
|
|
386
|
+
function isInstanceOfRegisteredClass(potentialClass, superJson) {
|
|
387
|
+
if (potentialClass == null ? void 0 : potentialClass.constructor) {
|
|
388
|
+
const isRegistered = !!superJson.classRegistry.getIdentifier(potentialClass.constructor);
|
|
389
|
+
return isRegistered;
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
const classRule = compositeTransformation(isInstanceOfRegisteredClass, (clazz, superJson) => {
|
|
394
|
+
const identifier = superJson.classRegistry.getIdentifier(clazz.constructor);
|
|
395
|
+
return ["class", identifier];
|
|
396
|
+
}, (clazz, superJson) => {
|
|
397
|
+
const allowedProps = superJson.classRegistry.getAllowedProps(clazz.constructor);
|
|
398
|
+
if (!allowedProps) {
|
|
399
|
+
return { ...clazz };
|
|
400
|
+
}
|
|
401
|
+
const result = {};
|
|
402
|
+
allowedProps.forEach((prop) => {
|
|
403
|
+
result[prop] = clazz[prop];
|
|
404
|
+
});
|
|
405
|
+
return result;
|
|
406
|
+
}, (v, a, superJson) => {
|
|
407
|
+
const clazz = superJson.classRegistry.getValue(a[1]);
|
|
408
|
+
if (!clazz) {
|
|
409
|
+
throw new Error("Trying to deserialize unknown class - check https://github.com/blitz-js/superjson/issues/116#issuecomment-773996564");
|
|
410
|
+
}
|
|
411
|
+
return Object.assign(Object.create(clazz.prototype), v);
|
|
412
|
+
});
|
|
413
|
+
const customRule = compositeTransformation((value, superJson) => {
|
|
414
|
+
return !!superJson.customTransformerRegistry.findApplicable(value);
|
|
415
|
+
}, (value, superJson) => {
|
|
416
|
+
const transformer = superJson.customTransformerRegistry.findApplicable(value);
|
|
417
|
+
return ["custom", transformer.name];
|
|
418
|
+
}, (value, superJson) => {
|
|
419
|
+
const transformer = superJson.customTransformerRegistry.findApplicable(value);
|
|
420
|
+
return transformer.serialize(value);
|
|
421
|
+
}, (v, a, superJson) => {
|
|
422
|
+
const transformer = superJson.customTransformerRegistry.findByName(a[1]);
|
|
423
|
+
if (!transformer) {
|
|
424
|
+
throw new Error("Trying to deserialize unknown custom value");
|
|
425
|
+
}
|
|
426
|
+
return transformer.deserialize(v);
|
|
427
|
+
});
|
|
428
|
+
const compositeRules = [classRule, symbolRule, customRule, typedArrayRule];
|
|
429
|
+
const transformValue = (value, superJson) => {
|
|
430
|
+
const applicableCompositeRule = findArr(compositeRules, (rule) => rule.isApplicable(value, superJson));
|
|
431
|
+
if (applicableCompositeRule) {
|
|
432
|
+
return {
|
|
433
|
+
value: applicableCompositeRule.transform(value, superJson),
|
|
434
|
+
type: applicableCompositeRule.annotation(value, superJson)
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
const applicableSimpleRule = findArr(simpleRules, (rule) => rule.isApplicable(value, superJson));
|
|
438
|
+
if (applicableSimpleRule) {
|
|
439
|
+
return {
|
|
440
|
+
value: applicableSimpleRule.transform(value, superJson),
|
|
441
|
+
type: applicableSimpleRule.annotation
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
return void 0;
|
|
445
|
+
};
|
|
446
|
+
const simpleRulesByAnnotation = {};
|
|
447
|
+
simpleRules.forEach((rule) => {
|
|
448
|
+
simpleRulesByAnnotation[rule.annotation] = rule;
|
|
449
|
+
});
|
|
450
|
+
const untransformValue = (json, type, superJson) => {
|
|
451
|
+
if (isArray$1(type)) {
|
|
452
|
+
switch (type[0]) {
|
|
453
|
+
case "symbol":
|
|
454
|
+
return symbolRule.untransform(json, type, superJson);
|
|
455
|
+
case "class":
|
|
456
|
+
return classRule.untransform(json, type, superJson);
|
|
457
|
+
case "custom":
|
|
458
|
+
return customRule.untransform(json, type, superJson);
|
|
459
|
+
case "typed-array":
|
|
460
|
+
return typedArrayRule.untransform(json, type, superJson);
|
|
461
|
+
default:
|
|
462
|
+
throw new Error("Unknown transformation: " + type);
|
|
463
|
+
}
|
|
464
|
+
} else {
|
|
465
|
+
const transformation = simpleRulesByAnnotation[type];
|
|
466
|
+
if (!transformation) {
|
|
467
|
+
throw new Error("Unknown transformation: " + type);
|
|
468
|
+
}
|
|
469
|
+
return transformation.untransform(json, superJson);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
const getNthKey = (value, n) => {
|
|
473
|
+
const keys = value.keys();
|
|
474
|
+
while (n > 0) {
|
|
475
|
+
keys.next();
|
|
476
|
+
n--;
|
|
477
|
+
}
|
|
478
|
+
return keys.next().value;
|
|
479
|
+
};
|
|
480
|
+
function validatePath(path) {
|
|
481
|
+
if (includes(path, "__proto__")) {
|
|
482
|
+
throw new Error("__proto__ is not allowed as a property");
|
|
483
|
+
}
|
|
484
|
+
if (includes(path, "prototype")) {
|
|
485
|
+
throw new Error("prototype is not allowed as a property");
|
|
486
|
+
}
|
|
487
|
+
if (includes(path, "constructor")) {
|
|
488
|
+
throw new Error("constructor is not allowed as a property");
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const getDeep = (object, path) => {
|
|
492
|
+
validatePath(path);
|
|
493
|
+
for (let i = 0; i < path.length; i++) {
|
|
494
|
+
const key = path[i];
|
|
495
|
+
if (isSet(object)) {
|
|
496
|
+
object = getNthKey(object, +key);
|
|
497
|
+
} else if (isMap(object)) {
|
|
498
|
+
const row = +key;
|
|
499
|
+
const type = +path[++i] === 0 ? "key" : "value";
|
|
500
|
+
const keyOfRow = getNthKey(object, row);
|
|
501
|
+
switch (type) {
|
|
502
|
+
case "key":
|
|
503
|
+
object = keyOfRow;
|
|
504
|
+
break;
|
|
505
|
+
case "value":
|
|
506
|
+
object = object.get(keyOfRow);
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
} else {
|
|
510
|
+
object = object[key];
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return object;
|
|
514
|
+
};
|
|
515
|
+
const setDeep = (object, path, mapper) => {
|
|
516
|
+
validatePath(path);
|
|
517
|
+
if (path.length === 0) {
|
|
518
|
+
return mapper(object);
|
|
519
|
+
}
|
|
520
|
+
let parent = object;
|
|
521
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
522
|
+
const key = path[i];
|
|
523
|
+
if (isArray$1(parent)) {
|
|
524
|
+
const index = +key;
|
|
525
|
+
parent = parent[index];
|
|
526
|
+
} else if (isPlainObject$1(parent)) {
|
|
527
|
+
parent = parent[key];
|
|
528
|
+
} else if (isSet(parent)) {
|
|
529
|
+
const row = +key;
|
|
530
|
+
parent = getNthKey(parent, row);
|
|
531
|
+
} else if (isMap(parent)) {
|
|
532
|
+
const isEnd = i === path.length - 2;
|
|
533
|
+
if (isEnd) {
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
const row = +key;
|
|
537
|
+
const type = +path[++i] === 0 ? "key" : "value";
|
|
538
|
+
const keyOfRow = getNthKey(parent, row);
|
|
539
|
+
switch (type) {
|
|
540
|
+
case "key":
|
|
541
|
+
parent = keyOfRow;
|
|
542
|
+
break;
|
|
543
|
+
case "value":
|
|
544
|
+
parent = parent.get(keyOfRow);
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const lastKey = path[path.length - 1];
|
|
550
|
+
if (isArray$1(parent)) {
|
|
551
|
+
parent[+lastKey] = mapper(parent[+lastKey]);
|
|
552
|
+
} else if (isPlainObject$1(parent)) {
|
|
553
|
+
parent[lastKey] = mapper(parent[lastKey]);
|
|
554
|
+
}
|
|
555
|
+
if (isSet(parent)) {
|
|
556
|
+
const oldValue = getNthKey(parent, +lastKey);
|
|
557
|
+
const newValue = mapper(oldValue);
|
|
558
|
+
if (oldValue !== newValue) {
|
|
559
|
+
parent.delete(oldValue);
|
|
560
|
+
parent.add(newValue);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
if (isMap(parent)) {
|
|
564
|
+
const row = +path[path.length - 2];
|
|
565
|
+
const keyToRow = getNthKey(parent, row);
|
|
566
|
+
const type = +lastKey === 0 ? "key" : "value";
|
|
567
|
+
switch (type) {
|
|
568
|
+
case "key": {
|
|
569
|
+
const newKey = mapper(keyToRow);
|
|
570
|
+
parent.set(newKey, parent.get(keyToRow));
|
|
571
|
+
if (newKey !== keyToRow) {
|
|
572
|
+
parent.delete(keyToRow);
|
|
573
|
+
}
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
case "value": {
|
|
577
|
+
parent.set(keyToRow, mapper(parent.get(keyToRow)));
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return object;
|
|
583
|
+
};
|
|
584
|
+
function traverse(tree, walker2, origin = []) {
|
|
585
|
+
if (!tree) {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (!isArray$1(tree)) {
|
|
589
|
+
forEach(tree, (subtree, key) => traverse(subtree, walker2, [...origin, ...parsePath(key)]));
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const [nodeValue, children] = tree;
|
|
593
|
+
if (children) {
|
|
594
|
+
forEach(children, (child, key) => {
|
|
595
|
+
traverse(child, walker2, [...origin, ...parsePath(key)]);
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
walker2(nodeValue, origin);
|
|
599
|
+
}
|
|
600
|
+
function applyValueAnnotations(plain, annotations, superJson) {
|
|
601
|
+
traverse(annotations, (type, path) => {
|
|
602
|
+
plain = setDeep(plain, path, (v) => untransformValue(v, type, superJson));
|
|
603
|
+
});
|
|
604
|
+
return plain;
|
|
605
|
+
}
|
|
606
|
+
function applyReferentialEqualityAnnotations(plain, annotations) {
|
|
607
|
+
function apply(identicalPaths, path) {
|
|
608
|
+
const object = getDeep(plain, parsePath(path));
|
|
609
|
+
identicalPaths.map(parsePath).forEach((identicalObjectPath) => {
|
|
610
|
+
plain = setDeep(plain, identicalObjectPath, () => object);
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
if (isArray$1(annotations)) {
|
|
614
|
+
const [root, other] = annotations;
|
|
615
|
+
root.forEach((identicalPath) => {
|
|
616
|
+
plain = setDeep(plain, parsePath(identicalPath), () => plain);
|
|
617
|
+
});
|
|
618
|
+
if (other) {
|
|
619
|
+
forEach(other, apply);
|
|
620
|
+
}
|
|
621
|
+
} else {
|
|
622
|
+
forEach(annotations, apply);
|
|
623
|
+
}
|
|
624
|
+
return plain;
|
|
625
|
+
}
|
|
626
|
+
const isDeep = (object, superJson) => isPlainObject$1(object) || isArray$1(object) || isMap(object) || isSet(object) || isInstanceOfRegisteredClass(object, superJson);
|
|
627
|
+
function addIdentity(object, path, identities) {
|
|
628
|
+
const existingSet = identities.get(object);
|
|
629
|
+
if (existingSet) {
|
|
630
|
+
existingSet.push(path);
|
|
631
|
+
} else {
|
|
632
|
+
identities.set(object, [path]);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function generateReferentialEqualityAnnotations(identitites, dedupe) {
|
|
636
|
+
const result = {};
|
|
637
|
+
let rootEqualityPaths = void 0;
|
|
638
|
+
identitites.forEach((paths) => {
|
|
639
|
+
if (paths.length <= 1) {
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
if (!dedupe) {
|
|
643
|
+
paths = paths.map((path) => path.map(String)).sort((a, b) => a.length - b.length);
|
|
644
|
+
}
|
|
645
|
+
const [representativePath, ...identicalPaths] = paths;
|
|
646
|
+
if (representativePath.length === 0) {
|
|
647
|
+
rootEqualityPaths = identicalPaths.map(stringifyPath);
|
|
648
|
+
} else {
|
|
649
|
+
result[stringifyPath(representativePath)] = identicalPaths.map(stringifyPath);
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
if (rootEqualityPaths) {
|
|
653
|
+
if (isEmptyObject(result)) {
|
|
654
|
+
return [rootEqualityPaths];
|
|
655
|
+
} else {
|
|
656
|
+
return [rootEqualityPaths, result];
|
|
657
|
+
}
|
|
658
|
+
} else {
|
|
659
|
+
return isEmptyObject(result) ? void 0 : result;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
const walker = (object, identities, superJson, dedupe, path = [], objectsInThisPath = [], seenObjects = /* @__PURE__ */ new Map()) => {
|
|
663
|
+
const primitive = isPrimitive(object);
|
|
664
|
+
if (!primitive) {
|
|
665
|
+
addIdentity(object, path, identities);
|
|
666
|
+
const seen = seenObjects.get(object);
|
|
667
|
+
if (seen) {
|
|
668
|
+
return dedupe ? {
|
|
669
|
+
transformedValue: null
|
|
670
|
+
} : seen;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (!isDeep(object, superJson)) {
|
|
674
|
+
const transformed2 = transformValue(object, superJson);
|
|
675
|
+
const result2 = transformed2 ? {
|
|
676
|
+
transformedValue: transformed2.value,
|
|
677
|
+
annotations: [transformed2.type]
|
|
678
|
+
} : {
|
|
679
|
+
transformedValue: object
|
|
680
|
+
};
|
|
681
|
+
if (!primitive) {
|
|
682
|
+
seenObjects.set(object, result2);
|
|
683
|
+
}
|
|
684
|
+
return result2;
|
|
685
|
+
}
|
|
686
|
+
if (includes(objectsInThisPath, object)) {
|
|
687
|
+
return {
|
|
688
|
+
transformedValue: null
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
const transformationResult = transformValue(object, superJson);
|
|
692
|
+
const transformed = (transformationResult == null ? void 0 : transformationResult.value) ?? object;
|
|
693
|
+
const transformedValue = isArray$1(transformed) ? [] : {};
|
|
694
|
+
const innerAnnotations = {};
|
|
695
|
+
forEach(transformed, (value, index) => {
|
|
696
|
+
if (index === "__proto__" || index === "constructor" || index === "prototype") {
|
|
697
|
+
throw new Error(`Detected property ${index}. This is a prototype pollution risk, please remove it from your object.`);
|
|
698
|
+
}
|
|
699
|
+
const recursiveResult = walker(value, identities, superJson, dedupe, [...path, index], [...objectsInThisPath, object], seenObjects);
|
|
700
|
+
transformedValue[index] = recursiveResult.transformedValue;
|
|
701
|
+
if (isArray$1(recursiveResult.annotations)) {
|
|
702
|
+
innerAnnotations[index] = recursiveResult.annotations;
|
|
703
|
+
} else if (isPlainObject$1(recursiveResult.annotations)) {
|
|
704
|
+
forEach(recursiveResult.annotations, (tree, key) => {
|
|
705
|
+
innerAnnotations[escapeKey(index) + "." + key] = tree;
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
const result = isEmptyObject(innerAnnotations) ? {
|
|
710
|
+
transformedValue,
|
|
711
|
+
annotations: !!transformationResult ? [transformationResult.type] : void 0
|
|
712
|
+
} : {
|
|
713
|
+
transformedValue,
|
|
714
|
+
annotations: !!transformationResult ? [transformationResult.type, innerAnnotations] : innerAnnotations
|
|
715
|
+
};
|
|
716
|
+
if (!primitive) {
|
|
717
|
+
seenObjects.set(object, result);
|
|
718
|
+
}
|
|
719
|
+
return result;
|
|
720
|
+
};
|
|
721
|
+
function getType(payload) {
|
|
722
|
+
return Object.prototype.toString.call(payload).slice(8, -1);
|
|
723
|
+
}
|
|
724
|
+
function isArray(payload) {
|
|
725
|
+
return getType(payload) === "Array";
|
|
726
|
+
}
|
|
727
|
+
function isPlainObject(payload) {
|
|
728
|
+
if (getType(payload) !== "Object")
|
|
729
|
+
return false;
|
|
730
|
+
const prototype = Object.getPrototypeOf(payload);
|
|
731
|
+
return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
|
|
732
|
+
}
|
|
733
|
+
function assignProp(carry, key, newVal, originalObject, includeNonenumerable) {
|
|
734
|
+
const propType = {}.propertyIsEnumerable.call(originalObject, key) ? "enumerable" : "nonenumerable";
|
|
735
|
+
if (propType === "enumerable")
|
|
736
|
+
carry[key] = newVal;
|
|
737
|
+
if (includeNonenumerable && propType === "nonenumerable") {
|
|
738
|
+
Object.defineProperty(carry, key, {
|
|
739
|
+
value: newVal,
|
|
740
|
+
enumerable: false,
|
|
741
|
+
writable: true,
|
|
742
|
+
configurable: true
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
function copy(target, options = {}) {
|
|
747
|
+
if (isArray(target)) {
|
|
748
|
+
return target.map((item) => copy(item, options));
|
|
749
|
+
}
|
|
750
|
+
if (!isPlainObject(target)) {
|
|
751
|
+
return target;
|
|
752
|
+
}
|
|
753
|
+
const props = Object.getOwnPropertyNames(target);
|
|
754
|
+
const symbols = Object.getOwnPropertySymbols(target);
|
|
755
|
+
return [...props, ...symbols].reduce((carry, key) => {
|
|
756
|
+
if (isArray(options.props) && !options.props.includes(key)) {
|
|
757
|
+
return carry;
|
|
758
|
+
}
|
|
759
|
+
const val = target[key];
|
|
760
|
+
const newVal = copy(val, options);
|
|
761
|
+
assignProp(carry, key, newVal, target, options.nonenumerable);
|
|
762
|
+
return carry;
|
|
763
|
+
}, {});
|
|
764
|
+
}
|
|
765
|
+
class SuperJSON {
|
|
766
|
+
/**
|
|
767
|
+
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`.
|
|
768
|
+
*/
|
|
769
|
+
constructor({ dedupe = false } = {}) {
|
|
770
|
+
this.classRegistry = new ClassRegistry();
|
|
771
|
+
this.symbolRegistry = new Registry((s) => s.description ?? "");
|
|
772
|
+
this.customTransformerRegistry = new CustomTransformerRegistry();
|
|
773
|
+
this.allowedErrorProps = [];
|
|
774
|
+
this.dedupe = dedupe;
|
|
775
|
+
}
|
|
776
|
+
serialize(object) {
|
|
777
|
+
const identities = /* @__PURE__ */ new Map();
|
|
778
|
+
const output = walker(object, identities, this, this.dedupe);
|
|
779
|
+
const res = {
|
|
780
|
+
json: output.transformedValue
|
|
781
|
+
};
|
|
782
|
+
if (output.annotations) {
|
|
783
|
+
res.meta = {
|
|
784
|
+
...res.meta,
|
|
785
|
+
values: output.annotations
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
const equalityAnnotations = generateReferentialEqualityAnnotations(identities, this.dedupe);
|
|
789
|
+
if (equalityAnnotations) {
|
|
790
|
+
res.meta = {
|
|
791
|
+
...res.meta,
|
|
792
|
+
referentialEqualities: equalityAnnotations
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
return res;
|
|
796
|
+
}
|
|
797
|
+
deserialize(payload) {
|
|
798
|
+
const { json, meta } = payload;
|
|
799
|
+
let result = copy(json);
|
|
800
|
+
if (meta == null ? void 0 : meta.values) {
|
|
801
|
+
result = applyValueAnnotations(result, meta.values, this);
|
|
802
|
+
}
|
|
803
|
+
if (meta == null ? void 0 : meta.referentialEqualities) {
|
|
804
|
+
result = applyReferentialEqualityAnnotations(result, meta.referentialEqualities);
|
|
805
|
+
}
|
|
806
|
+
return result;
|
|
807
|
+
}
|
|
808
|
+
stringify(object) {
|
|
809
|
+
return JSON.stringify(this.serialize(object));
|
|
810
|
+
}
|
|
811
|
+
parse(string) {
|
|
812
|
+
return this.deserialize(JSON.parse(string));
|
|
813
|
+
}
|
|
814
|
+
registerClass(v, options) {
|
|
815
|
+
this.classRegistry.register(v, options);
|
|
816
|
+
}
|
|
817
|
+
registerSymbol(v, identifier) {
|
|
818
|
+
this.symbolRegistry.register(v, identifier);
|
|
819
|
+
}
|
|
820
|
+
registerCustom(transformer, name) {
|
|
821
|
+
this.customTransformerRegistry.register({
|
|
822
|
+
name,
|
|
823
|
+
...transformer
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
allowErrorProps(...props) {
|
|
827
|
+
this.allowedErrorProps.push(...props);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
SuperJSON.defaultInstance = new SuperJSON();
|
|
831
|
+
SuperJSON.serialize = SuperJSON.defaultInstance.serialize.bind(SuperJSON.defaultInstance);
|
|
832
|
+
SuperJSON.deserialize = SuperJSON.defaultInstance.deserialize.bind(SuperJSON.defaultInstance);
|
|
833
|
+
SuperJSON.stringify = SuperJSON.defaultInstance.stringify.bind(SuperJSON.defaultInstance);
|
|
834
|
+
SuperJSON.parse = SuperJSON.defaultInstance.parse.bind(SuperJSON.defaultInstance);
|
|
835
|
+
SuperJSON.registerClass = SuperJSON.defaultInstance.registerClass.bind(SuperJSON.defaultInstance);
|
|
836
|
+
SuperJSON.registerSymbol = SuperJSON.defaultInstance.registerSymbol.bind(SuperJSON.defaultInstance);
|
|
837
|
+
SuperJSON.registerCustom = SuperJSON.defaultInstance.registerCustom.bind(SuperJSON.defaultInstance);
|
|
838
|
+
SuperJSON.allowErrorProps = SuperJSON.defaultInstance.allowErrorProps.bind(SuperJSON.defaultInstance);
|
|
839
|
+
SuperJSON.serialize;
|
|
840
|
+
SuperJSON.deserialize;
|
|
841
|
+
SuperJSON.stringify;
|
|
842
|
+
SuperJSON.parse;
|
|
843
|
+
SuperJSON.registerClass;
|
|
844
|
+
SuperJSON.registerCustom;
|
|
845
|
+
SuperJSON.registerSymbol;
|
|
846
|
+
SuperJSON.allowErrorProps;
|
|
847
|
+
const channelName = "__devtools-bridge:broadcast-channel__";
|
|
848
|
+
const createBroadcastChannel = () => {
|
|
849
|
+
const channel = new BroadcastChannel(channelName);
|
|
850
|
+
const event = bridgeEventMap.broadcast;
|
|
851
|
+
return {
|
|
852
|
+
post: (data) => {
|
|
853
|
+
channel.postMessage(SuperJSON.stringify({ event, data }));
|
|
854
|
+
},
|
|
855
|
+
on: (handler) => {
|
|
856
|
+
channel.onmessage = (e) => {
|
|
857
|
+
const parsed = SuperJSON.parse(e.data);
|
|
858
|
+
if (parsed.event === event) {
|
|
859
|
+
handler(parsed.data);
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
};
|
|
865
|
+
const createNativeChannel = () => {
|
|
866
|
+
const event = bridgeEventMap.native;
|
|
867
|
+
return {
|
|
868
|
+
post: (data) => {
|
|
869
|
+
const customEvent = new CustomEvent(event, {
|
|
870
|
+
detail: data
|
|
871
|
+
});
|
|
872
|
+
window.dispatchEvent(customEvent);
|
|
873
|
+
},
|
|
874
|
+
on: (handler) => {
|
|
875
|
+
const eventHandler = (e) => {
|
|
876
|
+
console.log(e.detail);
|
|
877
|
+
handler(e.detail);
|
|
878
|
+
};
|
|
879
|
+
window.addEventListener(event, eventHandler);
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
};
|
|
883
|
+
function setMessageChannelContext() {
|
|
884
|
+
window.__PERF_MONITOR_DEVTOOLS_MESSAGE_CHANNEL_CONTEXT__ ?? (window.__PERF_MONITOR_DEVTOOLS_MESSAGE_CHANNEL_CONTEXT__ = new MessageChannel());
|
|
885
|
+
}
|
|
886
|
+
function getMessageChannelContext() {
|
|
887
|
+
return window.__PERF_MONITOR_DEVTOOLS_MESSAGE_CHANNEL_CONTEXT__ || null;
|
|
888
|
+
}
|
|
889
|
+
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
890
|
+
var lodash_debounce;
|
|
891
|
+
var hasRequiredLodash_debounce;
|
|
892
|
+
function requireLodash_debounce() {
|
|
893
|
+
if (hasRequiredLodash_debounce) return lodash_debounce;
|
|
894
|
+
hasRequiredLodash_debounce = 1;
|
|
895
|
+
var FUNC_ERROR_TEXT = "Expected a function";
|
|
896
|
+
var NAN = 0 / 0;
|
|
897
|
+
var symbolTag = "[object Symbol]";
|
|
898
|
+
var reTrim = /^\s+|\s+$/g;
|
|
899
|
+
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
|
|
900
|
+
var reIsBinary = /^0b[01]+$/i;
|
|
901
|
+
var reIsOctal = /^0o[0-7]+$/i;
|
|
902
|
+
var freeParseInt = parseInt;
|
|
903
|
+
var freeGlobal = typeof commonjsGlobal == "object" && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal;
|
|
904
|
+
var freeSelf = typeof self == "object" && self && self.Object === Object && self;
|
|
905
|
+
var root = freeGlobal || freeSelf || Function("return this")();
|
|
906
|
+
var objectProto = Object.prototype;
|
|
907
|
+
var objectToString = objectProto.toString;
|
|
908
|
+
var nativeMax = Math.max, nativeMin = Math.min;
|
|
909
|
+
var now = function() {
|
|
910
|
+
return root.Date.now();
|
|
911
|
+
};
|
|
912
|
+
function debounce(func, wait, options) {
|
|
913
|
+
var lastArgs, lastThis, maxWait, result, timerId, lastCallTime, lastInvokeTime = 0, leading = false, maxing = false, trailing = true;
|
|
914
|
+
if (typeof func != "function") {
|
|
915
|
+
throw new TypeError(FUNC_ERROR_TEXT);
|
|
916
|
+
}
|
|
917
|
+
wait = toNumber(wait) || 0;
|
|
918
|
+
if (isObject(options)) {
|
|
919
|
+
leading = !!options.leading;
|
|
920
|
+
maxing = "maxWait" in options;
|
|
921
|
+
maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
|
|
922
|
+
trailing = "trailing" in options ? !!options.trailing : trailing;
|
|
923
|
+
}
|
|
924
|
+
function invokeFunc(time) {
|
|
925
|
+
var args = lastArgs, thisArg = lastThis;
|
|
926
|
+
lastArgs = lastThis = void 0;
|
|
927
|
+
lastInvokeTime = time;
|
|
928
|
+
result = func.apply(thisArg, args);
|
|
929
|
+
return result;
|
|
930
|
+
}
|
|
931
|
+
function leadingEdge(time) {
|
|
932
|
+
lastInvokeTime = time;
|
|
933
|
+
timerId = setTimeout(timerExpired, wait);
|
|
934
|
+
return leading ? invokeFunc(time) : result;
|
|
935
|
+
}
|
|
936
|
+
function remainingWait(time) {
|
|
937
|
+
var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime, result2 = wait - timeSinceLastCall;
|
|
938
|
+
return maxing ? nativeMin(result2, maxWait - timeSinceLastInvoke) : result2;
|
|
939
|
+
}
|
|
940
|
+
function shouldInvoke(time) {
|
|
941
|
+
var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime;
|
|
942
|
+
return lastCallTime === void 0 || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait;
|
|
943
|
+
}
|
|
944
|
+
function timerExpired() {
|
|
945
|
+
var time = now();
|
|
946
|
+
if (shouldInvoke(time)) {
|
|
947
|
+
return trailingEdge(time);
|
|
948
|
+
}
|
|
949
|
+
timerId = setTimeout(timerExpired, remainingWait(time));
|
|
950
|
+
}
|
|
951
|
+
function trailingEdge(time) {
|
|
952
|
+
timerId = void 0;
|
|
953
|
+
if (trailing && lastArgs) {
|
|
954
|
+
return invokeFunc(time);
|
|
955
|
+
}
|
|
956
|
+
lastArgs = lastThis = void 0;
|
|
957
|
+
return result;
|
|
958
|
+
}
|
|
959
|
+
function cancel() {
|
|
960
|
+
if (timerId !== void 0) {
|
|
961
|
+
clearTimeout(timerId);
|
|
962
|
+
}
|
|
963
|
+
lastInvokeTime = 0;
|
|
964
|
+
lastArgs = lastCallTime = lastThis = timerId = void 0;
|
|
965
|
+
}
|
|
966
|
+
function flush() {
|
|
967
|
+
return timerId === void 0 ? result : trailingEdge(now());
|
|
968
|
+
}
|
|
969
|
+
function debounced() {
|
|
970
|
+
var time = now(), isInvoking = shouldInvoke(time);
|
|
971
|
+
lastArgs = arguments;
|
|
972
|
+
lastThis = this;
|
|
973
|
+
lastCallTime = time;
|
|
974
|
+
if (isInvoking) {
|
|
975
|
+
if (timerId === void 0) {
|
|
976
|
+
return leadingEdge(lastCallTime);
|
|
977
|
+
}
|
|
978
|
+
if (maxing) {
|
|
979
|
+
timerId = setTimeout(timerExpired, wait);
|
|
980
|
+
return invokeFunc(lastCallTime);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
if (timerId === void 0) {
|
|
984
|
+
timerId = setTimeout(timerExpired, wait);
|
|
985
|
+
}
|
|
986
|
+
return result;
|
|
987
|
+
}
|
|
988
|
+
debounced.cancel = cancel;
|
|
989
|
+
debounced.flush = flush;
|
|
990
|
+
return debounced;
|
|
991
|
+
}
|
|
992
|
+
function isObject(value) {
|
|
993
|
+
var type = typeof value;
|
|
994
|
+
return !!value && (type == "object" || type == "function");
|
|
995
|
+
}
|
|
996
|
+
function isObjectLike(value) {
|
|
997
|
+
return !!value && typeof value == "object";
|
|
998
|
+
}
|
|
999
|
+
function isSymbol2(value) {
|
|
1000
|
+
return typeof value == "symbol" || isObjectLike(value) && objectToString.call(value) == symbolTag;
|
|
1001
|
+
}
|
|
1002
|
+
function toNumber(value) {
|
|
1003
|
+
if (typeof value == "number") {
|
|
1004
|
+
return value;
|
|
1005
|
+
}
|
|
1006
|
+
if (isSymbol2(value)) {
|
|
1007
|
+
return NAN;
|
|
1008
|
+
}
|
|
1009
|
+
if (isObject(value)) {
|
|
1010
|
+
var other = typeof value.valueOf == "function" ? value.valueOf() : value;
|
|
1011
|
+
value = isObject(other) ? other + "" : other;
|
|
1012
|
+
}
|
|
1013
|
+
if (typeof value != "string") {
|
|
1014
|
+
return value === 0 ? value : +value;
|
|
1015
|
+
}
|
|
1016
|
+
value = value.replace(reTrim, "");
|
|
1017
|
+
var isBinary = reIsBinary.test(value);
|
|
1018
|
+
return isBinary || reIsOctal.test(value) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : reIsBadHex.test(value) ? NAN : +value;
|
|
1019
|
+
}
|
|
1020
|
+
lodash_debounce = debounce;
|
|
1021
|
+
return lodash_debounce;
|
|
1022
|
+
}
|
|
1023
|
+
requireLodash_debounce();
|
|
1024
|
+
const noop = () => null;
|
|
1025
|
+
const createMessageChannelClient = () => {
|
|
1026
|
+
const event = bridgeEventMap.message;
|
|
1027
|
+
const channel = getMessageChannelContext();
|
|
1028
|
+
if (!channel) {
|
|
1029
|
+
return { post: noop, on: noop };
|
|
1030
|
+
}
|
|
1031
|
+
return {
|
|
1032
|
+
post: (data) => {
|
|
1033
|
+
channel.port1.postMessage({ event, data });
|
|
1034
|
+
},
|
|
1035
|
+
on: (handler) => {
|
|
1036
|
+
channel.port1.onmessage = ({ data }) => {
|
|
1037
|
+
if (data.event === event) {
|
|
1038
|
+
handler(data.data);
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
};
|
|
1044
|
+
const createMessageChannelServer = () => {
|
|
1045
|
+
const event = bridgeEventMap.message;
|
|
1046
|
+
const channel = getMessageChannelContext();
|
|
1047
|
+
if (!channel) {
|
|
1048
|
+
return { post: noop, on: noop };
|
|
1049
|
+
}
|
|
1050
|
+
return {
|
|
1051
|
+
post: (data) => {
|
|
1052
|
+
channel.port2.postMessage({ event, data });
|
|
1053
|
+
},
|
|
1054
|
+
on: (handler) => {
|
|
1055
|
+
channel.port2.onmessage = ({ data }) => {
|
|
1056
|
+
if (data.event === event) {
|
|
1057
|
+
handler(data.data);
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
1062
|
+
};
|
|
1063
|
+
const bridgeEventMap = {
|
|
1064
|
+
native: "__devtools-bridge-event-native",
|
|
1065
|
+
message: "__devtools-bridge-event-message",
|
|
1066
|
+
broadcast: "__devtools-bridge-event-broadcast"
|
|
1067
|
+
};
|
|
1068
|
+
const getBridgeChannel = (preset, host) => {
|
|
1069
|
+
const channelMappings = {
|
|
1070
|
+
native: {
|
|
1071
|
+
client: createNativeChannel,
|
|
1072
|
+
backend: createNativeChannel
|
|
1073
|
+
},
|
|
1074
|
+
message: {
|
|
1075
|
+
client: createMessageChannelClient,
|
|
1076
|
+
backend: createMessageChannelServer
|
|
1077
|
+
},
|
|
1078
|
+
broadcast: {
|
|
1079
|
+
client: createBroadcastChannel,
|
|
1080
|
+
backend: createBroadcastChannel
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
if (preset === "message") {
|
|
1084
|
+
setMessageChannelContext();
|
|
1085
|
+
}
|
|
1086
|
+
return channelMappings[preset][host]();
|
|
1087
|
+
};
|
|
1088
|
+
const createClientRpc = (functions, preset) => {
|
|
1089
|
+
const channel = getBridgeChannel(preset, "client");
|
|
1090
|
+
const rpc = createBirpc(functions, {
|
|
1091
|
+
...channel,
|
|
1092
|
+
timeout: -1,
|
|
1093
|
+
eventNames: [
|
|
1094
|
+
"reloadPage",
|
|
1095
|
+
"highlightRenderer",
|
|
1096
|
+
"startHighlight",
|
|
1097
|
+
"stopHighlight",
|
|
1098
|
+
"startInspect",
|
|
1099
|
+
"stopInspect",
|
|
1100
|
+
"updateConfig",
|
|
1101
|
+
"resetConfig",
|
|
1102
|
+
"breakLeakedObjectRefs",
|
|
1103
|
+
"cancelExposingLeakedObjectsToGlobal"
|
|
1104
|
+
]
|
|
1105
|
+
});
|
|
1106
|
+
return rpc;
|
|
1107
|
+
};
|
|
1108
|
+
const createBackendRpc = (functions, preset) => {
|
|
1109
|
+
const channel = getBridgeChannel(preset, "backend");
|
|
1110
|
+
const rpc = createBirpc(functions, {
|
|
1111
|
+
...channel,
|
|
1112
|
+
timeout: -1,
|
|
1113
|
+
eventNames: ["activedRenderers", "highlighting", "memoryLeaks", "reactRenderers", "eventsState"]
|
|
1114
|
+
});
|
|
1115
|
+
return rpc;
|
|
1116
|
+
};
|
|
1117
|
+
export {
|
|
1118
|
+
createBackendRpc,
|
|
1119
|
+
createClientRpc
|
|
1120
|
+
};
|
|
1121
|
+
//# sourceMappingURL=index.js.map
|