capnweb 0.0.0-1c87560
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/LICENSE.txt +21 -0
- package/README.md +726 -0
- package/dist/index-workers.cjs +2655 -0
- package/dist/index-workers.cjs.map +1 -0
- package/dist/index-workers.d.cts +2 -0
- package/dist/index-workers.d.ts +2 -0
- package/dist/index-workers.js +2621 -0
- package/dist/index-workers.js.map +1 -0
- package/dist/index.cjs +2632 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +372 -0
- package/dist/index.d.ts +372 -0
- package/dist/index.js +2618 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,2655 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var cfw = require('cloudflare:workers');
|
|
4
|
+
|
|
5
|
+
function _interopNamespace(e) {
|
|
6
|
+
if (e && e.__esModule) return e;
|
|
7
|
+
var n = Object.create(null);
|
|
8
|
+
if (e) {
|
|
9
|
+
Object.keys(e).forEach(function (k) {
|
|
10
|
+
if (k !== 'default') {
|
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return e[k]; }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var cfw__namespace = /*#__PURE__*/_interopNamespace(cfw);
|
|
24
|
+
|
|
25
|
+
// src/symbols.ts
|
|
26
|
+
var WORKERS_MODULE_SYMBOL = /* @__PURE__ */ Symbol("workers-module");
|
|
27
|
+
globalThis[WORKERS_MODULE_SYMBOL] = cfw__namespace;
|
|
28
|
+
|
|
29
|
+
// src/core.ts
|
|
30
|
+
if (!Symbol.dispose) {
|
|
31
|
+
Symbol.dispose = /* @__PURE__ */ Symbol.for("dispose");
|
|
32
|
+
}
|
|
33
|
+
if (!Symbol.asyncDispose) {
|
|
34
|
+
Symbol.asyncDispose = /* @__PURE__ */ Symbol.for("asyncDispose");
|
|
35
|
+
}
|
|
36
|
+
if (!Promise.withResolvers) {
|
|
37
|
+
Promise.withResolvers = function() {
|
|
38
|
+
let resolve;
|
|
39
|
+
let reject;
|
|
40
|
+
const promise = new Promise((res, rej) => {
|
|
41
|
+
resolve = res;
|
|
42
|
+
reject = rej;
|
|
43
|
+
});
|
|
44
|
+
return { promise, resolve, reject };
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
var workersModule = globalThis[WORKERS_MODULE_SYMBOL];
|
|
48
|
+
var RpcTarget = workersModule ? workersModule.RpcTarget : class {
|
|
49
|
+
};
|
|
50
|
+
var AsyncFunction = (async function() {
|
|
51
|
+
}).constructor;
|
|
52
|
+
function typeForRpc(value) {
|
|
53
|
+
switch (typeof value) {
|
|
54
|
+
case "boolean":
|
|
55
|
+
case "number":
|
|
56
|
+
case "string":
|
|
57
|
+
return "primitive";
|
|
58
|
+
case "undefined":
|
|
59
|
+
return "undefined";
|
|
60
|
+
case "object":
|
|
61
|
+
case "function":
|
|
62
|
+
break;
|
|
63
|
+
case "bigint":
|
|
64
|
+
return "bigint";
|
|
65
|
+
default:
|
|
66
|
+
return "unsupported";
|
|
67
|
+
}
|
|
68
|
+
if (value === null) {
|
|
69
|
+
return "primitive";
|
|
70
|
+
}
|
|
71
|
+
let prototype = Object.getPrototypeOf(value);
|
|
72
|
+
switch (prototype) {
|
|
73
|
+
case Object.prototype:
|
|
74
|
+
return "object";
|
|
75
|
+
case Function.prototype:
|
|
76
|
+
case AsyncFunction.prototype:
|
|
77
|
+
return "function";
|
|
78
|
+
case Array.prototype:
|
|
79
|
+
return "array";
|
|
80
|
+
case Date.prototype:
|
|
81
|
+
return "date";
|
|
82
|
+
case Uint8Array.prototype:
|
|
83
|
+
return "bytes";
|
|
84
|
+
// TODO: All other structured clone types.
|
|
85
|
+
case RpcStub.prototype:
|
|
86
|
+
return "stub";
|
|
87
|
+
case RpcPromise.prototype:
|
|
88
|
+
return "rpc-promise";
|
|
89
|
+
// TODO: Promise<T> or thenable
|
|
90
|
+
default:
|
|
91
|
+
if (workersModule) {
|
|
92
|
+
if (prototype == workersModule.RpcStub.prototype || value instanceof workersModule.ServiceStub) {
|
|
93
|
+
return "rpc-target";
|
|
94
|
+
} else if (prototype == workersModule.RpcPromise.prototype || prototype == workersModule.RpcProperty.prototype) {
|
|
95
|
+
return "rpc-thenable";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (value instanceof RpcTarget) {
|
|
99
|
+
return "rpc-target";
|
|
100
|
+
}
|
|
101
|
+
if (value instanceof Error) {
|
|
102
|
+
return "error";
|
|
103
|
+
}
|
|
104
|
+
return "unsupported";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function mapNotLoaded() {
|
|
108
|
+
throw new Error("RPC map() implementation was not loaded.");
|
|
109
|
+
}
|
|
110
|
+
var mapImpl = { applyMap: mapNotLoaded, sendMap: mapNotLoaded };
|
|
111
|
+
var StubHook = class {
|
|
112
|
+
};
|
|
113
|
+
var ErrorStubHook = class extends StubHook {
|
|
114
|
+
constructor(error) {
|
|
115
|
+
super();
|
|
116
|
+
this.error = error;
|
|
117
|
+
}
|
|
118
|
+
call(path, args) {
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
map(path, captures, instructions) {
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
get(path) {
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
dup() {
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
pull() {
|
|
131
|
+
return Promise.reject(this.error);
|
|
132
|
+
}
|
|
133
|
+
ignoreUnhandledRejections() {
|
|
134
|
+
}
|
|
135
|
+
dispose() {
|
|
136
|
+
}
|
|
137
|
+
onBroken(callback) {
|
|
138
|
+
try {
|
|
139
|
+
callback(this.error);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
Promise.resolve(err);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
var DISPOSED_HOOK = new ErrorStubHook(
|
|
146
|
+
new Error("Attempted to use RPC stub after it has been disposed.")
|
|
147
|
+
);
|
|
148
|
+
var doCall = (hook, path, params) => {
|
|
149
|
+
return hook.call(path, params);
|
|
150
|
+
};
|
|
151
|
+
function withCallInterceptor(interceptor, callback) {
|
|
152
|
+
let oldValue = doCall;
|
|
153
|
+
doCall = interceptor;
|
|
154
|
+
try {
|
|
155
|
+
return callback();
|
|
156
|
+
} finally {
|
|
157
|
+
doCall = oldValue;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
var RAW_STUB = /* @__PURE__ */ Symbol("realStub");
|
|
161
|
+
var PROXY_HANDLERS = {
|
|
162
|
+
apply(target, thisArg, argumentsList) {
|
|
163
|
+
let stub = target.raw;
|
|
164
|
+
return new RpcPromise(doCall(
|
|
165
|
+
stub.hook,
|
|
166
|
+
stub.pathIfPromise || [],
|
|
167
|
+
RpcPayload.fromAppParams(argumentsList)
|
|
168
|
+
), []);
|
|
169
|
+
},
|
|
170
|
+
get(target, prop, receiver) {
|
|
171
|
+
let stub = target.raw;
|
|
172
|
+
if (prop === RAW_STUB) {
|
|
173
|
+
return stub;
|
|
174
|
+
} else if (prop in RpcPromise.prototype) {
|
|
175
|
+
return stub[prop];
|
|
176
|
+
} else if (typeof prop === "string") {
|
|
177
|
+
return new RpcPromise(
|
|
178
|
+
stub.hook,
|
|
179
|
+
stub.pathIfPromise ? [...stub.pathIfPromise, prop] : [prop]
|
|
180
|
+
);
|
|
181
|
+
} else if (prop === Symbol.dispose && (!stub.pathIfPromise || stub.pathIfPromise.length == 0)) {
|
|
182
|
+
return () => {
|
|
183
|
+
stub.hook.dispose();
|
|
184
|
+
stub.hook = DISPOSED_HOOK;
|
|
185
|
+
};
|
|
186
|
+
} else {
|
|
187
|
+
return void 0;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
has(target, prop) {
|
|
191
|
+
let stub = target.raw;
|
|
192
|
+
if (prop === RAW_STUB) {
|
|
193
|
+
return true;
|
|
194
|
+
} else if (prop in RpcPromise.prototype) {
|
|
195
|
+
return prop in stub;
|
|
196
|
+
} else if (typeof prop === "string") {
|
|
197
|
+
return true;
|
|
198
|
+
} else if (prop === Symbol.dispose && (!stub.pathIfPromise || stub.pathIfPromise.length == 0)) {
|
|
199
|
+
return true;
|
|
200
|
+
} else {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
construct(target, args) {
|
|
205
|
+
throw new Error("An RPC stub cannot be used as a constructor.");
|
|
206
|
+
},
|
|
207
|
+
defineProperty(target, property, attributes) {
|
|
208
|
+
throw new Error("Can't define properties on RPC stubs.");
|
|
209
|
+
},
|
|
210
|
+
deleteProperty(target, p) {
|
|
211
|
+
throw new Error("Can't delete properties on RPC stubs.");
|
|
212
|
+
},
|
|
213
|
+
getOwnPropertyDescriptor(target, p) {
|
|
214
|
+
return void 0;
|
|
215
|
+
},
|
|
216
|
+
getPrototypeOf(target) {
|
|
217
|
+
return Object.getPrototypeOf(target.raw);
|
|
218
|
+
},
|
|
219
|
+
isExtensible(target) {
|
|
220
|
+
return false;
|
|
221
|
+
},
|
|
222
|
+
ownKeys(target) {
|
|
223
|
+
return [];
|
|
224
|
+
},
|
|
225
|
+
preventExtensions(target) {
|
|
226
|
+
return true;
|
|
227
|
+
},
|
|
228
|
+
set(target, p, newValue, receiver) {
|
|
229
|
+
throw new Error("Can't assign properties on RPC stubs.");
|
|
230
|
+
},
|
|
231
|
+
setPrototypeOf(target, v) {
|
|
232
|
+
throw new Error("Can't override prototype of RPC stubs.");
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var RpcStub = class _RpcStub extends RpcTarget {
|
|
236
|
+
// Although `hook` and `path` are declared `public` here, they are effectively hidden by the
|
|
237
|
+
// proxy.
|
|
238
|
+
constructor(hook, pathIfPromise) {
|
|
239
|
+
super();
|
|
240
|
+
if (!(hook instanceof StubHook)) {
|
|
241
|
+
let value = hook;
|
|
242
|
+
if (value instanceof RpcTarget || value instanceof Function) {
|
|
243
|
+
hook = TargetStubHook.create(value, void 0);
|
|
244
|
+
} else {
|
|
245
|
+
hook = new PayloadStubHook(RpcPayload.fromAppReturn(value));
|
|
246
|
+
}
|
|
247
|
+
if (pathIfPromise) {
|
|
248
|
+
throw new TypeError("RpcStub constructor expected one argument, received two.");
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
this.hook = hook;
|
|
252
|
+
this.pathIfPromise = pathIfPromise;
|
|
253
|
+
let func = () => {
|
|
254
|
+
};
|
|
255
|
+
func.raw = this;
|
|
256
|
+
return new Proxy(func, PROXY_HANDLERS);
|
|
257
|
+
}
|
|
258
|
+
hook;
|
|
259
|
+
pathIfPromise;
|
|
260
|
+
dup() {
|
|
261
|
+
let target = this[RAW_STUB];
|
|
262
|
+
if (target.pathIfPromise) {
|
|
263
|
+
return new _RpcStub(target.hook.get(target.pathIfPromise));
|
|
264
|
+
} else {
|
|
265
|
+
return new _RpcStub(target.hook.dup());
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
onRpcBroken(callback) {
|
|
269
|
+
this[RAW_STUB].hook.onBroken(callback);
|
|
270
|
+
}
|
|
271
|
+
map(func) {
|
|
272
|
+
let { hook, pathIfPromise } = this[RAW_STUB];
|
|
273
|
+
return mapImpl.sendMap(hook, pathIfPromise || [], func);
|
|
274
|
+
}
|
|
275
|
+
toString() {
|
|
276
|
+
return "[object RpcStub]";
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
var RpcPromise = class extends RpcStub {
|
|
280
|
+
// TODO: Support passing target value or promise to constructor.
|
|
281
|
+
constructor(hook, pathIfPromise) {
|
|
282
|
+
super(hook, pathIfPromise);
|
|
283
|
+
}
|
|
284
|
+
then(onfulfilled, onrejected) {
|
|
285
|
+
return pullPromise(this).then(...arguments);
|
|
286
|
+
}
|
|
287
|
+
catch(onrejected) {
|
|
288
|
+
return pullPromise(this).catch(...arguments);
|
|
289
|
+
}
|
|
290
|
+
finally(onfinally) {
|
|
291
|
+
return pullPromise(this).finally(...arguments);
|
|
292
|
+
}
|
|
293
|
+
toString() {
|
|
294
|
+
return "[object RpcPromise]";
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
function unwrapStubTakingOwnership(stub) {
|
|
298
|
+
let { hook, pathIfPromise } = stub[RAW_STUB];
|
|
299
|
+
if (pathIfPromise && pathIfPromise.length > 0) {
|
|
300
|
+
return hook.get(pathIfPromise);
|
|
301
|
+
} else {
|
|
302
|
+
return hook;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function unwrapStubAndDup(stub) {
|
|
306
|
+
let { hook, pathIfPromise } = stub[RAW_STUB];
|
|
307
|
+
if (pathIfPromise) {
|
|
308
|
+
return hook.get(pathIfPromise);
|
|
309
|
+
} else {
|
|
310
|
+
return hook.dup();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function unwrapStubNoProperties(stub) {
|
|
314
|
+
let { hook, pathIfPromise } = stub[RAW_STUB];
|
|
315
|
+
if (pathIfPromise && pathIfPromise.length > 0) {
|
|
316
|
+
return void 0;
|
|
317
|
+
}
|
|
318
|
+
return hook;
|
|
319
|
+
}
|
|
320
|
+
function unwrapStubOrParent(stub) {
|
|
321
|
+
return stub[RAW_STUB].hook;
|
|
322
|
+
}
|
|
323
|
+
function unwrapStubAndPath(stub) {
|
|
324
|
+
return stub[RAW_STUB];
|
|
325
|
+
}
|
|
326
|
+
async function pullPromise(promise) {
|
|
327
|
+
let { hook, pathIfPromise } = promise[RAW_STUB];
|
|
328
|
+
if (pathIfPromise.length > 0) {
|
|
329
|
+
hook = hook.get(pathIfPromise);
|
|
330
|
+
}
|
|
331
|
+
let payload = await hook.pull();
|
|
332
|
+
return payload.deliverResolve();
|
|
333
|
+
}
|
|
334
|
+
var RpcPayload = class _RpcPayload {
|
|
335
|
+
// Private constructor; use factory functions above to construct.
|
|
336
|
+
constructor(value, source, stubs, promises) {
|
|
337
|
+
this.value = value;
|
|
338
|
+
this.source = source;
|
|
339
|
+
this.stubs = stubs;
|
|
340
|
+
this.promises = promises;
|
|
341
|
+
}
|
|
342
|
+
// Create a payload from a value passed as params to an RPC from the app.
|
|
343
|
+
//
|
|
344
|
+
// The payload does NOT take ownership of any stubs in `value`, and but promises not to modify
|
|
345
|
+
// `value`. If the payload is delivered locally, `value` will be deep-copied first, so as not
|
|
346
|
+
// to have the sender and recipient end up sharing the same mutable object. `value` will not be
|
|
347
|
+
// touched again after the call returns synchronously (returns a promise) -- by that point,
|
|
348
|
+
// the value has either been copied or serialized to the wire.
|
|
349
|
+
static fromAppParams(value) {
|
|
350
|
+
return new _RpcPayload(value, "params");
|
|
351
|
+
}
|
|
352
|
+
// Create a payload from a value return from an RPC implementation by the app.
|
|
353
|
+
//
|
|
354
|
+
// Unlike fromAppParams(), in this case the payload takes ownership of all stubs in `value`, and
|
|
355
|
+
// may hold onto `value` for an arbitrarily long time (e.g. to serve pipelined requests). It
|
|
356
|
+
// will still avoid modifying `value` and will make a deep copy if it is delivered locally.
|
|
357
|
+
static fromAppReturn(value) {
|
|
358
|
+
return new _RpcPayload(value, "return");
|
|
359
|
+
}
|
|
360
|
+
// Combine an array of payloads into a single payload whose value is an array. Ownership of all
|
|
361
|
+
// stubs is transferred from the inputs to the outputs, hence if the output is disposed, the
|
|
362
|
+
// inputs should not be. (In case of exception, nothing is disposed, though.)
|
|
363
|
+
static fromArray(array) {
|
|
364
|
+
let stubs = [];
|
|
365
|
+
let promises = [];
|
|
366
|
+
let resultArray = [];
|
|
367
|
+
for (let payload of array) {
|
|
368
|
+
payload.ensureDeepCopied();
|
|
369
|
+
for (let stub of payload.stubs) {
|
|
370
|
+
stubs.push(stub);
|
|
371
|
+
}
|
|
372
|
+
for (let promise of payload.promises) {
|
|
373
|
+
if (promise.parent === payload) {
|
|
374
|
+
promise = {
|
|
375
|
+
parent: resultArray,
|
|
376
|
+
property: resultArray.length,
|
|
377
|
+
promise: promise.promise
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
promises.push(promise);
|
|
381
|
+
}
|
|
382
|
+
resultArray.push(payload.value);
|
|
383
|
+
}
|
|
384
|
+
return new _RpcPayload(resultArray, "owned", stubs, promises);
|
|
385
|
+
}
|
|
386
|
+
// Create a payload from a value parsed off the wire using Evaluator.evaluate().
|
|
387
|
+
//
|
|
388
|
+
// A payload is constructed with a null value and the given stubs and promises arrays. The value
|
|
389
|
+
// is expected to be filled in by the evaluator, and the stubs and promises arrays are expected
|
|
390
|
+
// to be extended with stubs found during parsing. (This weird usage model is necessary so that
|
|
391
|
+
// if the root value turns out to be a promise, its `parent` in `promises` can be the payload
|
|
392
|
+
// object itself.)
|
|
393
|
+
//
|
|
394
|
+
// When done, the payload takes ownership of the final value and all the stubs within. It may
|
|
395
|
+
// modify the value in preparation for delivery, and may deliver the value directly to the app
|
|
396
|
+
// without copying.
|
|
397
|
+
static forEvaluate(stubs, promises) {
|
|
398
|
+
return new _RpcPayload(null, "owned", stubs, promises);
|
|
399
|
+
}
|
|
400
|
+
// Deep-copy the given value, including dup()ing all stubs.
|
|
401
|
+
//
|
|
402
|
+
// If `value` is a function, it should be bound to `oldParent` as its `this`.
|
|
403
|
+
//
|
|
404
|
+
// If deep-copying from a branch of some other RpcPayload, it must be provided, to make sure
|
|
405
|
+
// RpcTargets found within don't get duplicate stubs.
|
|
406
|
+
static deepCopyFrom(value, oldParent, owner) {
|
|
407
|
+
let result = new _RpcPayload(null, "owned", [], []);
|
|
408
|
+
result.value = result.deepCopy(
|
|
409
|
+
value,
|
|
410
|
+
oldParent,
|
|
411
|
+
"value",
|
|
412
|
+
result,
|
|
413
|
+
/*dupStubs=*/
|
|
414
|
+
true,
|
|
415
|
+
owner
|
|
416
|
+
);
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
419
|
+
// For `source === "return"` payloads only, this tracks any StubHooks created around RpcTargets
|
|
420
|
+
// found in the payload at the time that it is serialized (or deep-copied) for return, so that we
|
|
421
|
+
// can make sure they are not disposed before the pipeline ends.
|
|
422
|
+
//
|
|
423
|
+
// This is initialized on first use.
|
|
424
|
+
rpcTargets;
|
|
425
|
+
// Get the StubHook representing the given RpcTarget found inside this payload.
|
|
426
|
+
getHookForRpcTarget(target, parent, dupStubs = true) {
|
|
427
|
+
if (this.source === "params") {
|
|
428
|
+
return TargetStubHook.create(target, parent);
|
|
429
|
+
} else if (this.source === "return") {
|
|
430
|
+
let hook = this.rpcTargets?.get(target);
|
|
431
|
+
if (hook) {
|
|
432
|
+
if (dupStubs) {
|
|
433
|
+
return hook.dup();
|
|
434
|
+
} else {
|
|
435
|
+
this.rpcTargets?.delete(target);
|
|
436
|
+
return hook;
|
|
437
|
+
}
|
|
438
|
+
} else {
|
|
439
|
+
hook = TargetStubHook.create(target, parent);
|
|
440
|
+
if (dupStubs) {
|
|
441
|
+
if (!this.rpcTargets) {
|
|
442
|
+
this.rpcTargets = /* @__PURE__ */ new Map();
|
|
443
|
+
}
|
|
444
|
+
this.rpcTargets.set(target, hook);
|
|
445
|
+
return hook.dup();
|
|
446
|
+
} else {
|
|
447
|
+
return hook;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
} else {
|
|
451
|
+
throw new Error("owned payload shouldn't contain raw RpcTargets");
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
deepCopy(value, oldParent, property, parent, dupStubs, owner) {
|
|
455
|
+
let kind = typeForRpc(value);
|
|
456
|
+
switch (kind) {
|
|
457
|
+
case "unsupported":
|
|
458
|
+
return value;
|
|
459
|
+
case "primitive":
|
|
460
|
+
case "bigint":
|
|
461
|
+
case "date":
|
|
462
|
+
case "bytes":
|
|
463
|
+
case "error":
|
|
464
|
+
case "undefined":
|
|
465
|
+
return value;
|
|
466
|
+
case "array": {
|
|
467
|
+
let array = value;
|
|
468
|
+
let len = array.length;
|
|
469
|
+
let result = new Array(len);
|
|
470
|
+
for (let i = 0; i < len; i++) {
|
|
471
|
+
result[i] = this.deepCopy(array[i], array, i, result, dupStubs, owner);
|
|
472
|
+
}
|
|
473
|
+
return result;
|
|
474
|
+
}
|
|
475
|
+
case "object": {
|
|
476
|
+
let result = {};
|
|
477
|
+
let object = value;
|
|
478
|
+
for (let i in object) {
|
|
479
|
+
result[i] = this.deepCopy(object[i], object, i, result, dupStubs, owner);
|
|
480
|
+
}
|
|
481
|
+
return result;
|
|
482
|
+
}
|
|
483
|
+
case "stub":
|
|
484
|
+
case "rpc-promise": {
|
|
485
|
+
let stub = value;
|
|
486
|
+
let hook;
|
|
487
|
+
if (dupStubs) {
|
|
488
|
+
hook = unwrapStubAndDup(stub);
|
|
489
|
+
} else {
|
|
490
|
+
hook = unwrapStubTakingOwnership(stub);
|
|
491
|
+
}
|
|
492
|
+
if (stub instanceof RpcPromise) {
|
|
493
|
+
let promise = new RpcPromise(hook, []);
|
|
494
|
+
this.promises.push({ parent, property, promise });
|
|
495
|
+
return promise;
|
|
496
|
+
} else {
|
|
497
|
+
let newStub = new RpcStub(hook);
|
|
498
|
+
this.stubs.push(newStub);
|
|
499
|
+
return newStub;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
case "function":
|
|
503
|
+
case "rpc-target": {
|
|
504
|
+
let target = value;
|
|
505
|
+
let stub;
|
|
506
|
+
if (owner) {
|
|
507
|
+
stub = new RpcStub(owner.getHookForRpcTarget(target, oldParent, dupStubs));
|
|
508
|
+
} else {
|
|
509
|
+
stub = new RpcStub(TargetStubHook.create(target, oldParent));
|
|
510
|
+
}
|
|
511
|
+
this.stubs.push(stub);
|
|
512
|
+
return stub;
|
|
513
|
+
}
|
|
514
|
+
case "rpc-thenable": {
|
|
515
|
+
let target = value;
|
|
516
|
+
let promise;
|
|
517
|
+
if (owner) {
|
|
518
|
+
promise = new RpcPromise(owner.getHookForRpcTarget(target, oldParent, dupStubs), []);
|
|
519
|
+
} else {
|
|
520
|
+
promise = new RpcPromise(TargetStubHook.create(target, oldParent), []);
|
|
521
|
+
}
|
|
522
|
+
this.promises.push({ parent, property, promise });
|
|
523
|
+
return promise;
|
|
524
|
+
}
|
|
525
|
+
default:
|
|
526
|
+
throw new Error("unreachable");
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
// Ensures that if the value originally came from an unowned source, we have replaced it with a
|
|
530
|
+
// deep copy.
|
|
531
|
+
ensureDeepCopied() {
|
|
532
|
+
if (this.source !== "owned") {
|
|
533
|
+
let dupStubs = this.source === "params";
|
|
534
|
+
this.stubs = [];
|
|
535
|
+
this.promises = [];
|
|
536
|
+
try {
|
|
537
|
+
this.value = this.deepCopy(this.value, void 0, "value", this, dupStubs, this);
|
|
538
|
+
} catch (err) {
|
|
539
|
+
this.stubs = void 0;
|
|
540
|
+
this.promises = void 0;
|
|
541
|
+
throw err;
|
|
542
|
+
}
|
|
543
|
+
this.source = "owned";
|
|
544
|
+
if (this.rpcTargets && this.rpcTargets.size > 0) {
|
|
545
|
+
throw new Error("Not all rpcTargets were accounted for in deep-copy?");
|
|
546
|
+
}
|
|
547
|
+
this.rpcTargets = void 0;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
// Resolve all promises in this payload and then assign the final value into `parent[property]`.
|
|
551
|
+
deliverTo(parent, property, promises) {
|
|
552
|
+
this.ensureDeepCopied();
|
|
553
|
+
if (this.value instanceof RpcPromise) {
|
|
554
|
+
_RpcPayload.deliverRpcPromiseTo(this.value, parent, property, promises);
|
|
555
|
+
} else {
|
|
556
|
+
parent[property] = this.value;
|
|
557
|
+
for (let record of this.promises) {
|
|
558
|
+
_RpcPayload.deliverRpcPromiseTo(record.promise, record.parent, record.property, promises);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
static deliverRpcPromiseTo(promise, parent, property, promises) {
|
|
563
|
+
let hook = unwrapStubNoProperties(promise);
|
|
564
|
+
if (!hook) {
|
|
565
|
+
throw new Error("property promises should have been resolved earlier");
|
|
566
|
+
}
|
|
567
|
+
let inner = hook.pull();
|
|
568
|
+
if (inner instanceof _RpcPayload) {
|
|
569
|
+
inner.deliverTo(parent, property, promises);
|
|
570
|
+
} else {
|
|
571
|
+
promises.push(inner.then((payload) => {
|
|
572
|
+
let subPromises = [];
|
|
573
|
+
payload.deliverTo(parent, property, subPromises);
|
|
574
|
+
if (subPromises.length > 0) {
|
|
575
|
+
return Promise.all(subPromises);
|
|
576
|
+
}
|
|
577
|
+
}));
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
// Call the given function with the payload as an argument. The call is made synchronously if
|
|
581
|
+
// possible, in order to maintain e-order. However, if any RpcPromises exist in the payload,
|
|
582
|
+
// they are awaited and substituted before calling the function. The result of the call is
|
|
583
|
+
// wrapped into another payload.
|
|
584
|
+
//
|
|
585
|
+
// The payload is automatically disposed after the call completes. The caller should not call
|
|
586
|
+
// dispose().
|
|
587
|
+
async deliverCall(func, thisArg) {
|
|
588
|
+
try {
|
|
589
|
+
let promises = [];
|
|
590
|
+
this.deliverTo(this, "value", promises);
|
|
591
|
+
if (promises.length > 0) {
|
|
592
|
+
await Promise.all(promises);
|
|
593
|
+
}
|
|
594
|
+
let result = Function.prototype.apply.call(func, thisArg, this.value);
|
|
595
|
+
if (result instanceof RpcPromise) {
|
|
596
|
+
return _RpcPayload.fromAppReturn(result);
|
|
597
|
+
} else {
|
|
598
|
+
return _RpcPayload.fromAppReturn(await result);
|
|
599
|
+
}
|
|
600
|
+
} finally {
|
|
601
|
+
this.dispose();
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
// Produce a promise for this payload for return to the application. Any RpcPromises in the
|
|
605
|
+
// payload are awaited and substituted with their results first.
|
|
606
|
+
//
|
|
607
|
+
// The returned object will have a disposer which disposes the payload. The caller should not
|
|
608
|
+
// separately dispose it.
|
|
609
|
+
async deliverResolve() {
|
|
610
|
+
try {
|
|
611
|
+
let promises = [];
|
|
612
|
+
this.deliverTo(this, "value", promises);
|
|
613
|
+
if (promises.length > 0) {
|
|
614
|
+
await Promise.all(promises);
|
|
615
|
+
}
|
|
616
|
+
let result = this.value;
|
|
617
|
+
if (result instanceof Object) {
|
|
618
|
+
if (!(Symbol.dispose in result)) {
|
|
619
|
+
Object.defineProperty(result, Symbol.dispose, {
|
|
620
|
+
// NOTE: Using `this.dispose.bind(this)` here causes Playwright's build of
|
|
621
|
+
// Chromium 140.0.7339.16 to fail when the object is assigned to a `using` variable,
|
|
622
|
+
// with the error:
|
|
623
|
+
// TypeError: Symbol(Symbol.dispose) is not a function
|
|
624
|
+
// I cannot reproduce this problem in Chrome 140.0.7339.127 nor in Node or workerd,
|
|
625
|
+
// so maybe it was a short-lived V8 bug or something. To be safe, though, we use
|
|
626
|
+
// `() => this.dispose()`, which seems to always work.
|
|
627
|
+
value: () => this.dispose(),
|
|
628
|
+
writable: true,
|
|
629
|
+
enumerable: false,
|
|
630
|
+
configurable: true
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return result;
|
|
635
|
+
} catch (err) {
|
|
636
|
+
this.dispose();
|
|
637
|
+
throw err;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
dispose() {
|
|
641
|
+
if (this.source === "owned") {
|
|
642
|
+
this.stubs.forEach((stub) => stub[Symbol.dispose]());
|
|
643
|
+
this.promises.forEach((promise) => promise.promise[Symbol.dispose]());
|
|
644
|
+
} else if (this.source === "return") {
|
|
645
|
+
this.disposeImpl(this.value, void 0);
|
|
646
|
+
if (this.rpcTargets && this.rpcTargets.size > 0) {
|
|
647
|
+
throw new Error("Not all rpcTargets were accounted for in disposeImpl()?");
|
|
648
|
+
}
|
|
649
|
+
} else ;
|
|
650
|
+
this.source = "owned";
|
|
651
|
+
this.stubs = [];
|
|
652
|
+
this.promises = [];
|
|
653
|
+
}
|
|
654
|
+
// Recursive dispose, called only when `source` is "return".
|
|
655
|
+
disposeImpl(value, parent) {
|
|
656
|
+
let kind = typeForRpc(value);
|
|
657
|
+
switch (kind) {
|
|
658
|
+
case "unsupported":
|
|
659
|
+
case "primitive":
|
|
660
|
+
case "bigint":
|
|
661
|
+
case "bytes":
|
|
662
|
+
case "date":
|
|
663
|
+
case "error":
|
|
664
|
+
case "undefined":
|
|
665
|
+
return;
|
|
666
|
+
case "array": {
|
|
667
|
+
let array = value;
|
|
668
|
+
let len = array.length;
|
|
669
|
+
for (let i = 0; i < len; i++) {
|
|
670
|
+
this.disposeImpl(array[i], array);
|
|
671
|
+
}
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
case "object": {
|
|
675
|
+
let object = value;
|
|
676
|
+
for (let i in object) {
|
|
677
|
+
this.disposeImpl(object[i], object);
|
|
678
|
+
}
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
case "stub":
|
|
682
|
+
case "rpc-promise": {
|
|
683
|
+
let stub = value;
|
|
684
|
+
let hook = unwrapStubNoProperties(stub);
|
|
685
|
+
if (hook) {
|
|
686
|
+
hook.dispose();
|
|
687
|
+
}
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
case "function":
|
|
691
|
+
case "rpc-target": {
|
|
692
|
+
let target = value;
|
|
693
|
+
let hook = this.rpcTargets?.get(target);
|
|
694
|
+
if (hook) {
|
|
695
|
+
hook.dispose();
|
|
696
|
+
this.rpcTargets.delete(target);
|
|
697
|
+
} else {
|
|
698
|
+
disposeRpcTarget(target);
|
|
699
|
+
}
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
case "rpc-thenable":
|
|
703
|
+
return;
|
|
704
|
+
default:
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
// Ignore unhandled rejections in all promises in this payload -- that is, all promises that
|
|
709
|
+
// *would* be awaited if this payload were to be delivered. See the similarly-named method of
|
|
710
|
+
// StubHook for explanation.
|
|
711
|
+
ignoreUnhandledRejections() {
|
|
712
|
+
if (this.stubs) {
|
|
713
|
+
this.stubs.forEach((stub) => {
|
|
714
|
+
unwrapStubOrParent(stub).ignoreUnhandledRejections();
|
|
715
|
+
});
|
|
716
|
+
this.promises.forEach(
|
|
717
|
+
(promise) => unwrapStubOrParent(promise.promise).ignoreUnhandledRejections()
|
|
718
|
+
);
|
|
719
|
+
} else {
|
|
720
|
+
this.ignoreUnhandledRejectionsImpl(this.value);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
ignoreUnhandledRejectionsImpl(value) {
|
|
724
|
+
let kind = typeForRpc(value);
|
|
725
|
+
switch (kind) {
|
|
726
|
+
case "unsupported":
|
|
727
|
+
case "primitive":
|
|
728
|
+
case "bigint":
|
|
729
|
+
case "bytes":
|
|
730
|
+
case "date":
|
|
731
|
+
case "error":
|
|
732
|
+
case "undefined":
|
|
733
|
+
case "function":
|
|
734
|
+
case "rpc-target":
|
|
735
|
+
return;
|
|
736
|
+
case "array": {
|
|
737
|
+
let array = value;
|
|
738
|
+
let len = array.length;
|
|
739
|
+
for (let i = 0; i < len; i++) {
|
|
740
|
+
this.ignoreUnhandledRejectionsImpl(array[i]);
|
|
741
|
+
}
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
case "object": {
|
|
745
|
+
let object = value;
|
|
746
|
+
for (let i in object) {
|
|
747
|
+
this.ignoreUnhandledRejectionsImpl(object[i]);
|
|
748
|
+
}
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
case "stub":
|
|
752
|
+
case "rpc-promise":
|
|
753
|
+
unwrapStubOrParent(value).ignoreUnhandledRejections();
|
|
754
|
+
return;
|
|
755
|
+
case "rpc-thenable":
|
|
756
|
+
value.then((_) => {
|
|
757
|
+
}, (_) => {
|
|
758
|
+
});
|
|
759
|
+
return;
|
|
760
|
+
default:
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
function followPath(value, parent, path, owner) {
|
|
766
|
+
for (let i = 0; i < path.length; i++) {
|
|
767
|
+
parent = value;
|
|
768
|
+
let part = path[i];
|
|
769
|
+
if (part in Object.prototype) {
|
|
770
|
+
value = void 0;
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
let kind = typeForRpc(value);
|
|
774
|
+
switch (kind) {
|
|
775
|
+
case "object":
|
|
776
|
+
case "function":
|
|
777
|
+
if (Object.hasOwn(value, part)) {
|
|
778
|
+
value = value[part];
|
|
779
|
+
} else {
|
|
780
|
+
value = void 0;
|
|
781
|
+
}
|
|
782
|
+
break;
|
|
783
|
+
case "array":
|
|
784
|
+
if (Number.isInteger(part) && part >= 0) {
|
|
785
|
+
value = value[part];
|
|
786
|
+
} else {
|
|
787
|
+
value = void 0;
|
|
788
|
+
}
|
|
789
|
+
break;
|
|
790
|
+
case "rpc-target":
|
|
791
|
+
case "rpc-thenable": {
|
|
792
|
+
if (Object.hasOwn(value, part)) {
|
|
793
|
+
throw new TypeError(
|
|
794
|
+
`Attempted to access property '${part}', which is an instance property of the RpcTarget. To avoid leaking private internals, instance properties cannot be accessed over RPC. If you want to make this property available over RPC, define it as a method or getter on the class, instead of an instance property.`
|
|
795
|
+
);
|
|
796
|
+
} else {
|
|
797
|
+
value = value[part];
|
|
798
|
+
}
|
|
799
|
+
owner = null;
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
case "stub":
|
|
803
|
+
case "rpc-promise": {
|
|
804
|
+
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
805
|
+
return { hook, remainingPath: pathIfPromise ? pathIfPromise.concat(path.slice(i)) : path.slice(i) };
|
|
806
|
+
}
|
|
807
|
+
case "primitive":
|
|
808
|
+
case "bigint":
|
|
809
|
+
case "bytes":
|
|
810
|
+
case "date":
|
|
811
|
+
case "error":
|
|
812
|
+
value = void 0;
|
|
813
|
+
break;
|
|
814
|
+
case "undefined":
|
|
815
|
+
value = value[part];
|
|
816
|
+
break;
|
|
817
|
+
case "unsupported": {
|
|
818
|
+
if (i === 0) {
|
|
819
|
+
throw new TypeError(`RPC stub points at a non-serializable type.`);
|
|
820
|
+
} else {
|
|
821
|
+
let prefix = path.slice(0, i).join(".");
|
|
822
|
+
let remainder = path.slice(0, i).join(".");
|
|
823
|
+
throw new TypeError(
|
|
824
|
+
`'${prefix}' is not a serializable type, so property ${remainder} cannot be accessed.`
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
default:
|
|
829
|
+
throw new TypeError("unreachable");
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
if (value instanceof RpcPromise) {
|
|
833
|
+
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
834
|
+
return { hook, remainingPath: pathIfPromise || [] };
|
|
835
|
+
}
|
|
836
|
+
return {
|
|
837
|
+
value,
|
|
838
|
+
parent,
|
|
839
|
+
owner
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
var ValueStubHook = class extends StubHook {
|
|
843
|
+
call(path, args) {
|
|
844
|
+
try {
|
|
845
|
+
let { value, owner } = this.getValue();
|
|
846
|
+
let followResult = followPath(value, void 0, path, owner);
|
|
847
|
+
if (followResult.hook) {
|
|
848
|
+
return followResult.hook.call(followResult.remainingPath, args);
|
|
849
|
+
}
|
|
850
|
+
if (typeof followResult.value != "function") {
|
|
851
|
+
throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
852
|
+
}
|
|
853
|
+
let promise = args.deliverCall(followResult.value, followResult.parent);
|
|
854
|
+
return new PromiseStubHook(promise.then((payload) => {
|
|
855
|
+
return new PayloadStubHook(payload);
|
|
856
|
+
}));
|
|
857
|
+
} catch (err) {
|
|
858
|
+
return new ErrorStubHook(err);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
map(path, captures, instructions) {
|
|
862
|
+
try {
|
|
863
|
+
let followResult;
|
|
864
|
+
try {
|
|
865
|
+
let { value, owner } = this.getValue();
|
|
866
|
+
followResult = followPath(value, void 0, path, owner);
|
|
867
|
+
;
|
|
868
|
+
} catch (err) {
|
|
869
|
+
for (let cap of captures) {
|
|
870
|
+
cap.dispose();
|
|
871
|
+
}
|
|
872
|
+
throw err;
|
|
873
|
+
}
|
|
874
|
+
if (followResult.hook) {
|
|
875
|
+
return followResult.hook.map(followResult.remainingPath, captures, instructions);
|
|
876
|
+
}
|
|
877
|
+
return mapImpl.applyMap(
|
|
878
|
+
followResult.value,
|
|
879
|
+
followResult.parent,
|
|
880
|
+
followResult.owner,
|
|
881
|
+
captures,
|
|
882
|
+
instructions
|
|
883
|
+
);
|
|
884
|
+
} catch (err) {
|
|
885
|
+
return new ErrorStubHook(err);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
get(path) {
|
|
889
|
+
try {
|
|
890
|
+
let { value, owner } = this.getValue();
|
|
891
|
+
if (path.length === 0 && owner === null) {
|
|
892
|
+
throw new Error("Can't dup an RpcTarget stub as a promise.");
|
|
893
|
+
}
|
|
894
|
+
let followResult = followPath(value, void 0, path, owner);
|
|
895
|
+
if (followResult.hook) {
|
|
896
|
+
return followResult.hook.get(followResult.remainingPath);
|
|
897
|
+
}
|
|
898
|
+
return new PayloadStubHook(RpcPayload.deepCopyFrom(
|
|
899
|
+
followResult.value,
|
|
900
|
+
followResult.parent,
|
|
901
|
+
followResult.owner
|
|
902
|
+
));
|
|
903
|
+
} catch (err) {
|
|
904
|
+
return new ErrorStubHook(err);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
var PayloadStubHook = class _PayloadStubHook extends ValueStubHook {
|
|
909
|
+
constructor(payload) {
|
|
910
|
+
super();
|
|
911
|
+
this.payload = payload;
|
|
912
|
+
}
|
|
913
|
+
payload;
|
|
914
|
+
// cleared when disposed
|
|
915
|
+
getPayload() {
|
|
916
|
+
if (this.payload) {
|
|
917
|
+
return this.payload;
|
|
918
|
+
} else {
|
|
919
|
+
throw new Error("Attempted to use an RPC StubHook after it was disposed.");
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
getValue() {
|
|
923
|
+
let payload = this.getPayload();
|
|
924
|
+
return { value: payload.value, owner: payload };
|
|
925
|
+
}
|
|
926
|
+
dup() {
|
|
927
|
+
let thisPayload = this.getPayload();
|
|
928
|
+
return new _PayloadStubHook(RpcPayload.deepCopyFrom(
|
|
929
|
+
thisPayload.value,
|
|
930
|
+
void 0,
|
|
931
|
+
thisPayload
|
|
932
|
+
));
|
|
933
|
+
}
|
|
934
|
+
pull() {
|
|
935
|
+
return this.getPayload();
|
|
936
|
+
}
|
|
937
|
+
ignoreUnhandledRejections() {
|
|
938
|
+
if (this.payload) {
|
|
939
|
+
this.payload.ignoreUnhandledRejections();
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
dispose() {
|
|
943
|
+
if (this.payload) {
|
|
944
|
+
this.payload.dispose();
|
|
945
|
+
this.payload = void 0;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
onBroken(callback) {
|
|
949
|
+
if (this.payload) {
|
|
950
|
+
if (this.payload.value instanceof RpcStub) {
|
|
951
|
+
this.payload.value.onRpcBroken(callback);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
function disposeRpcTarget(target) {
|
|
957
|
+
if (Symbol.dispose in target) {
|
|
958
|
+
try {
|
|
959
|
+
target[Symbol.dispose]();
|
|
960
|
+
} catch (err) {
|
|
961
|
+
Promise.reject(err);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
var TargetStubHook = class _TargetStubHook extends ValueStubHook {
|
|
966
|
+
// Constructs a TargetStubHook that is not duplicated from an existing hook.
|
|
967
|
+
//
|
|
968
|
+
// If `value` is a function, `parent` is bound as its "this".
|
|
969
|
+
static create(value, parent) {
|
|
970
|
+
if (typeof value !== "function") {
|
|
971
|
+
parent = void 0;
|
|
972
|
+
}
|
|
973
|
+
return new _TargetStubHook(value, parent);
|
|
974
|
+
}
|
|
975
|
+
constructor(target, parent, dupFrom) {
|
|
976
|
+
super();
|
|
977
|
+
this.target = target;
|
|
978
|
+
this.parent = parent;
|
|
979
|
+
if (dupFrom) {
|
|
980
|
+
if (dupFrom.refcount) {
|
|
981
|
+
this.refcount = dupFrom.refcount;
|
|
982
|
+
++this.refcount.count;
|
|
983
|
+
}
|
|
984
|
+
} else if (Symbol.dispose in target) {
|
|
985
|
+
this.refcount = { count: 1 };
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
target;
|
|
989
|
+
// cleared when disposed
|
|
990
|
+
parent;
|
|
991
|
+
// `this` parameter when calling `target`
|
|
992
|
+
refcount;
|
|
993
|
+
// undefined if not needed (because target has no disposer)
|
|
994
|
+
getTarget() {
|
|
995
|
+
if (this.target) {
|
|
996
|
+
return this.target;
|
|
997
|
+
} else {
|
|
998
|
+
throw new Error("Attempted to use an RPC StubHook after it was disposed.");
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
getValue() {
|
|
1002
|
+
return { value: this.getTarget(), owner: null };
|
|
1003
|
+
}
|
|
1004
|
+
dup() {
|
|
1005
|
+
return new _TargetStubHook(this.getTarget(), this.parent, this);
|
|
1006
|
+
}
|
|
1007
|
+
pull() {
|
|
1008
|
+
let target = this.getTarget();
|
|
1009
|
+
if ("then" in target) {
|
|
1010
|
+
return Promise.resolve(target).then((resolution) => {
|
|
1011
|
+
return RpcPayload.fromAppReturn(resolution);
|
|
1012
|
+
});
|
|
1013
|
+
} else {
|
|
1014
|
+
return Promise.reject(new Error("Tried to resolve a non-promise stub."));
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
ignoreUnhandledRejections() {
|
|
1018
|
+
}
|
|
1019
|
+
dispose() {
|
|
1020
|
+
if (this.target) {
|
|
1021
|
+
if (this.refcount) {
|
|
1022
|
+
if (--this.refcount.count == 0) {
|
|
1023
|
+
disposeRpcTarget(this.target);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
this.target = void 0;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
onBroken(callback) {
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
var PromiseStubHook = class _PromiseStubHook extends StubHook {
|
|
1033
|
+
promise;
|
|
1034
|
+
resolution;
|
|
1035
|
+
constructor(promise) {
|
|
1036
|
+
super();
|
|
1037
|
+
this.promise = promise.then((res) => {
|
|
1038
|
+
this.resolution = res;
|
|
1039
|
+
return res;
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
call(path, args) {
|
|
1043
|
+
args.ensureDeepCopied();
|
|
1044
|
+
return new _PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
|
|
1045
|
+
}
|
|
1046
|
+
map(path, captures, instructions) {
|
|
1047
|
+
return new _PromiseStubHook(this.promise.then(
|
|
1048
|
+
(hook) => hook.map(path, captures, instructions),
|
|
1049
|
+
(err) => {
|
|
1050
|
+
for (let cap of captures) {
|
|
1051
|
+
cap.dispose();
|
|
1052
|
+
}
|
|
1053
|
+
throw err;
|
|
1054
|
+
}
|
|
1055
|
+
));
|
|
1056
|
+
}
|
|
1057
|
+
get(path) {
|
|
1058
|
+
return new _PromiseStubHook(this.promise.then((hook) => hook.get(path)));
|
|
1059
|
+
}
|
|
1060
|
+
dup() {
|
|
1061
|
+
if (this.resolution) {
|
|
1062
|
+
return this.resolution.dup();
|
|
1063
|
+
} else {
|
|
1064
|
+
return new _PromiseStubHook(this.promise.then((hook) => hook.dup()));
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
pull() {
|
|
1068
|
+
if (this.resolution) {
|
|
1069
|
+
return this.resolution.pull();
|
|
1070
|
+
} else {
|
|
1071
|
+
return this.promise.then((hook) => hook.pull());
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
ignoreUnhandledRejections() {
|
|
1075
|
+
if (this.resolution) {
|
|
1076
|
+
this.resolution.ignoreUnhandledRejections();
|
|
1077
|
+
} else {
|
|
1078
|
+
this.promise.then((res) => {
|
|
1079
|
+
res.ignoreUnhandledRejections();
|
|
1080
|
+
}, (err) => {
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
dispose() {
|
|
1085
|
+
if (this.resolution) {
|
|
1086
|
+
this.resolution.dispose();
|
|
1087
|
+
} else {
|
|
1088
|
+
this.promise.then((hook) => {
|
|
1089
|
+
hook.dispose();
|
|
1090
|
+
}, (err) => {
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
onBroken(callback) {
|
|
1095
|
+
if (this.resolution) {
|
|
1096
|
+
this.resolution.onBroken(callback);
|
|
1097
|
+
} else {
|
|
1098
|
+
this.promise.then((hook) => {
|
|
1099
|
+
hook.onBroken(callback);
|
|
1100
|
+
}, callback);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
// src/serialize.ts
|
|
1106
|
+
var NullExporter = class {
|
|
1107
|
+
exportStub(stub) {
|
|
1108
|
+
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
1109
|
+
}
|
|
1110
|
+
exportPromise(stub) {
|
|
1111
|
+
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
1112
|
+
}
|
|
1113
|
+
getImport(hook) {
|
|
1114
|
+
return void 0;
|
|
1115
|
+
}
|
|
1116
|
+
unexport(ids) {
|
|
1117
|
+
}
|
|
1118
|
+
onSendError(error) {
|
|
1119
|
+
}
|
|
1120
|
+
};
|
|
1121
|
+
var NULL_EXPORTER = new NullExporter();
|
|
1122
|
+
var ERROR_TYPES = {
|
|
1123
|
+
Error,
|
|
1124
|
+
EvalError,
|
|
1125
|
+
RangeError,
|
|
1126
|
+
ReferenceError,
|
|
1127
|
+
SyntaxError,
|
|
1128
|
+
TypeError,
|
|
1129
|
+
URIError,
|
|
1130
|
+
AggregateError
|
|
1131
|
+
// TODO: DOMError? Others?
|
|
1132
|
+
};
|
|
1133
|
+
var Devaluator = class _Devaluator {
|
|
1134
|
+
constructor(exporter, source) {
|
|
1135
|
+
this.exporter = exporter;
|
|
1136
|
+
this.source = source;
|
|
1137
|
+
}
|
|
1138
|
+
// Devaluate the given value.
|
|
1139
|
+
// * value: The value to devaluate.
|
|
1140
|
+
// * parent: The value's parent object, which would be used as `this` if the value were called
|
|
1141
|
+
// as a function.
|
|
1142
|
+
// * exporter: Callbacks to the RPC session for exporting capabilities found in this message.
|
|
1143
|
+
// * source: The RpcPayload which contains the value, and therefore owns stubs within.
|
|
1144
|
+
//
|
|
1145
|
+
// Returns: The devaluated value, ready to be JSON-serialized.
|
|
1146
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
1147
|
+
let devaluator = new _Devaluator(exporter, source);
|
|
1148
|
+
try {
|
|
1149
|
+
return devaluator.devaluateImpl(value, parent, 0);
|
|
1150
|
+
} catch (err) {
|
|
1151
|
+
if (devaluator.exports) {
|
|
1152
|
+
try {
|
|
1153
|
+
exporter.unexport(devaluator.exports);
|
|
1154
|
+
} catch (err2) {
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
throw err;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
exports;
|
|
1161
|
+
devaluateImpl(value, parent, depth) {
|
|
1162
|
+
if (depth >= 64) {
|
|
1163
|
+
throw new Error(
|
|
1164
|
+
"Serialization exceeded maximum allowed depth. (Does the message contain cycles?)"
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
let kind = typeForRpc(value);
|
|
1168
|
+
switch (kind) {
|
|
1169
|
+
case "unsupported": {
|
|
1170
|
+
let msg;
|
|
1171
|
+
try {
|
|
1172
|
+
msg = `Cannot serialize value: ${value}`;
|
|
1173
|
+
} catch (err) {
|
|
1174
|
+
msg = "Cannot serialize value: (couldn't stringify value)";
|
|
1175
|
+
}
|
|
1176
|
+
throw new TypeError(msg);
|
|
1177
|
+
}
|
|
1178
|
+
case "primitive":
|
|
1179
|
+
if (typeof value === "number" && !isFinite(value)) {
|
|
1180
|
+
if (value === Infinity) {
|
|
1181
|
+
return ["inf"];
|
|
1182
|
+
} else if (value === -Infinity) {
|
|
1183
|
+
return ["-inf"];
|
|
1184
|
+
} else {
|
|
1185
|
+
return ["nan"];
|
|
1186
|
+
}
|
|
1187
|
+
} else {
|
|
1188
|
+
return value;
|
|
1189
|
+
}
|
|
1190
|
+
case "object": {
|
|
1191
|
+
let object = value;
|
|
1192
|
+
let result = {};
|
|
1193
|
+
for (let key in object) {
|
|
1194
|
+
result[key] = this.devaluateImpl(object[key], object, depth + 1);
|
|
1195
|
+
}
|
|
1196
|
+
return result;
|
|
1197
|
+
}
|
|
1198
|
+
case "array": {
|
|
1199
|
+
let array = value;
|
|
1200
|
+
let len = array.length;
|
|
1201
|
+
let result = new Array(len);
|
|
1202
|
+
for (let i = 0; i < len; i++) {
|
|
1203
|
+
result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1204
|
+
}
|
|
1205
|
+
return [result];
|
|
1206
|
+
}
|
|
1207
|
+
case "bigint":
|
|
1208
|
+
return ["bigint", value.toString()];
|
|
1209
|
+
case "date":
|
|
1210
|
+
return ["date", value.getTime()];
|
|
1211
|
+
case "bytes": {
|
|
1212
|
+
let bytes = value;
|
|
1213
|
+
if (bytes.toBase64) {
|
|
1214
|
+
return ["bytes", bytes.toBase64({ omitPadding: true })];
|
|
1215
|
+
} else {
|
|
1216
|
+
return [
|
|
1217
|
+
"bytes",
|
|
1218
|
+
btoa(String.fromCharCode.apply(null, bytes).replace(/=*$/, ""))
|
|
1219
|
+
];
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
case "error": {
|
|
1223
|
+
let e = value;
|
|
1224
|
+
let rewritten = this.exporter.onSendError(e);
|
|
1225
|
+
if (rewritten) {
|
|
1226
|
+
e = rewritten;
|
|
1227
|
+
}
|
|
1228
|
+
let result = ["error", e.name, e.message];
|
|
1229
|
+
if (rewritten && rewritten.stack) {
|
|
1230
|
+
result.push(rewritten.stack);
|
|
1231
|
+
}
|
|
1232
|
+
return result;
|
|
1233
|
+
}
|
|
1234
|
+
case "undefined":
|
|
1235
|
+
return ["undefined"];
|
|
1236
|
+
case "stub":
|
|
1237
|
+
case "rpc-promise": {
|
|
1238
|
+
if (!this.source) {
|
|
1239
|
+
throw new Error("Can't serialize RPC stubs in this context.");
|
|
1240
|
+
}
|
|
1241
|
+
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
1242
|
+
let importId = this.exporter.getImport(hook);
|
|
1243
|
+
if (importId !== void 0) {
|
|
1244
|
+
if (pathIfPromise) {
|
|
1245
|
+
if (pathIfPromise.length > 0) {
|
|
1246
|
+
return ["pipeline", importId, pathIfPromise];
|
|
1247
|
+
} else {
|
|
1248
|
+
return ["pipeline", importId];
|
|
1249
|
+
}
|
|
1250
|
+
} else {
|
|
1251
|
+
return ["import", importId];
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
if (pathIfPromise) {
|
|
1255
|
+
hook = hook.get(pathIfPromise);
|
|
1256
|
+
} else {
|
|
1257
|
+
hook = hook.dup();
|
|
1258
|
+
}
|
|
1259
|
+
return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
|
|
1260
|
+
}
|
|
1261
|
+
case "function":
|
|
1262
|
+
case "rpc-target": {
|
|
1263
|
+
if (!this.source) {
|
|
1264
|
+
throw new Error("Can't serialize RPC stubs in this context.");
|
|
1265
|
+
}
|
|
1266
|
+
let hook = this.source.getHookForRpcTarget(value, parent);
|
|
1267
|
+
return this.devaluateHook("export", hook);
|
|
1268
|
+
}
|
|
1269
|
+
case "rpc-thenable": {
|
|
1270
|
+
if (!this.source) {
|
|
1271
|
+
throw new Error("Can't serialize RPC stubs in this context.");
|
|
1272
|
+
}
|
|
1273
|
+
let hook = this.source.getHookForRpcTarget(value, parent);
|
|
1274
|
+
return this.devaluateHook("promise", hook);
|
|
1275
|
+
}
|
|
1276
|
+
default:
|
|
1277
|
+
throw new Error("unreachable");
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
devaluateHook(type, hook) {
|
|
1281
|
+
if (!this.exports) this.exports = [];
|
|
1282
|
+
let exportId = type === "promise" ? this.exporter.exportPromise(hook) : this.exporter.exportStub(hook);
|
|
1283
|
+
this.exports.push(exportId);
|
|
1284
|
+
return [type, exportId];
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
function serialize(value) {
|
|
1288
|
+
return JSON.stringify(Devaluator.devaluate(value));
|
|
1289
|
+
}
|
|
1290
|
+
var NullImporter = class {
|
|
1291
|
+
importStub(idx) {
|
|
1292
|
+
throw new Error("Cannot deserialize RPC stubs without an RPC session.");
|
|
1293
|
+
}
|
|
1294
|
+
importPromise(idx) {
|
|
1295
|
+
throw new Error("Cannot deserialize RPC stubs without an RPC session.");
|
|
1296
|
+
}
|
|
1297
|
+
getExport(idx) {
|
|
1298
|
+
return void 0;
|
|
1299
|
+
}
|
|
1300
|
+
};
|
|
1301
|
+
var NULL_IMPORTER = new NullImporter();
|
|
1302
|
+
var Evaluator = class _Evaluator {
|
|
1303
|
+
constructor(importer) {
|
|
1304
|
+
this.importer = importer;
|
|
1305
|
+
}
|
|
1306
|
+
stubs = [];
|
|
1307
|
+
promises = [];
|
|
1308
|
+
evaluate(value) {
|
|
1309
|
+
let payload = RpcPayload.forEvaluate(this.stubs, this.promises);
|
|
1310
|
+
try {
|
|
1311
|
+
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1312
|
+
return payload;
|
|
1313
|
+
} catch (err) {
|
|
1314
|
+
payload.dispose();
|
|
1315
|
+
throw err;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
// Evaluate the value without destroying it.
|
|
1319
|
+
evaluateCopy(value) {
|
|
1320
|
+
return this.evaluate(structuredClone(value));
|
|
1321
|
+
}
|
|
1322
|
+
evaluateImpl(value, parent, property) {
|
|
1323
|
+
if (value instanceof Array) {
|
|
1324
|
+
if (value.length == 1 && value[0] instanceof Array) {
|
|
1325
|
+
let result = value[0];
|
|
1326
|
+
for (let i = 0; i < result.length; i++) {
|
|
1327
|
+
result[i] = this.evaluateImpl(result[i], result, i);
|
|
1328
|
+
}
|
|
1329
|
+
return result;
|
|
1330
|
+
} else switch (value[0]) {
|
|
1331
|
+
case "bigint":
|
|
1332
|
+
if (typeof value[1] == "string") {
|
|
1333
|
+
return BigInt(value[1]);
|
|
1334
|
+
}
|
|
1335
|
+
break;
|
|
1336
|
+
case "date":
|
|
1337
|
+
if (typeof value[1] == "number") {
|
|
1338
|
+
return new Date(value[1]);
|
|
1339
|
+
}
|
|
1340
|
+
break;
|
|
1341
|
+
case "bytes": {
|
|
1342
|
+
let b64 = Uint8Array;
|
|
1343
|
+
if (typeof value[1] == "string") {
|
|
1344
|
+
if (b64.fromBase64) {
|
|
1345
|
+
return b64.fromBase64(value[1]);
|
|
1346
|
+
} else {
|
|
1347
|
+
let bs = atob(value[1]);
|
|
1348
|
+
let len = bs.length;
|
|
1349
|
+
let bytes = new Uint8Array(len);
|
|
1350
|
+
for (let i = 0; i < len; i++) {
|
|
1351
|
+
bytes[i] = bs.charCodeAt(i);
|
|
1352
|
+
}
|
|
1353
|
+
return bytes;
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
break;
|
|
1357
|
+
}
|
|
1358
|
+
case "error":
|
|
1359
|
+
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1360
|
+
let cls = ERROR_TYPES[value[1]] || Error;
|
|
1361
|
+
let result = new cls(value[2]);
|
|
1362
|
+
if (typeof value[3] === "string") {
|
|
1363
|
+
result.stack = value[3];
|
|
1364
|
+
}
|
|
1365
|
+
return result;
|
|
1366
|
+
}
|
|
1367
|
+
break;
|
|
1368
|
+
case "undefined":
|
|
1369
|
+
if (value.length === 1) {
|
|
1370
|
+
return void 0;
|
|
1371
|
+
}
|
|
1372
|
+
break;
|
|
1373
|
+
case "inf":
|
|
1374
|
+
return Infinity;
|
|
1375
|
+
case "-inf":
|
|
1376
|
+
return -Infinity;
|
|
1377
|
+
case "nan":
|
|
1378
|
+
return NaN;
|
|
1379
|
+
case "import":
|
|
1380
|
+
case "pipeline": {
|
|
1381
|
+
if (value.length < 2 || value.length > 4) {
|
|
1382
|
+
break;
|
|
1383
|
+
}
|
|
1384
|
+
if (typeof value[1] != "number") {
|
|
1385
|
+
break;
|
|
1386
|
+
}
|
|
1387
|
+
let hook = this.importer.getExport(value[1]);
|
|
1388
|
+
if (!hook) {
|
|
1389
|
+
throw new Error(`no such entry on exports table: ${value[1]}`);
|
|
1390
|
+
}
|
|
1391
|
+
let isPromise = value[0] == "pipeline";
|
|
1392
|
+
let addStub = (hook2) => {
|
|
1393
|
+
if (isPromise) {
|
|
1394
|
+
let promise = new RpcPromise(hook2, []);
|
|
1395
|
+
this.promises.push({ promise, parent, property });
|
|
1396
|
+
return promise;
|
|
1397
|
+
} else {
|
|
1398
|
+
let stub = new RpcPromise(hook2, []);
|
|
1399
|
+
this.stubs.push(stub);
|
|
1400
|
+
return stub;
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
if (value.length == 2) {
|
|
1404
|
+
if (isPromise) {
|
|
1405
|
+
return addStub(hook.get([]));
|
|
1406
|
+
} else {
|
|
1407
|
+
return addStub(hook.dup());
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
let path = value[2];
|
|
1411
|
+
if (!(path instanceof Array)) {
|
|
1412
|
+
break;
|
|
1413
|
+
}
|
|
1414
|
+
if (!path.every(
|
|
1415
|
+
(part) => {
|
|
1416
|
+
return typeof part == "string" || typeof part == "number";
|
|
1417
|
+
}
|
|
1418
|
+
)) {
|
|
1419
|
+
break;
|
|
1420
|
+
}
|
|
1421
|
+
if (value.length == 3) {
|
|
1422
|
+
return addStub(hook.get(path));
|
|
1423
|
+
}
|
|
1424
|
+
let args = value[3];
|
|
1425
|
+
if (!(args instanceof Array)) {
|
|
1426
|
+
break;
|
|
1427
|
+
}
|
|
1428
|
+
let subEval = new _Evaluator(this.importer);
|
|
1429
|
+
args = subEval.evaluate([args]);
|
|
1430
|
+
return addStub(hook.call(path, args));
|
|
1431
|
+
}
|
|
1432
|
+
case "remap": {
|
|
1433
|
+
if (value.length !== 5 || typeof value[1] !== "number" || !(value[2] instanceof Array) || !(value[3] instanceof Array) || !(value[4] instanceof Array)) {
|
|
1434
|
+
break;
|
|
1435
|
+
}
|
|
1436
|
+
let hook = this.importer.getExport(value[1]);
|
|
1437
|
+
if (!hook) {
|
|
1438
|
+
throw new Error(`no such entry on exports table: ${value[1]}`);
|
|
1439
|
+
}
|
|
1440
|
+
let path = value[2];
|
|
1441
|
+
if (!path.every(
|
|
1442
|
+
(part) => {
|
|
1443
|
+
return typeof part == "string" || typeof part == "number";
|
|
1444
|
+
}
|
|
1445
|
+
)) {
|
|
1446
|
+
break;
|
|
1447
|
+
}
|
|
1448
|
+
let captures = value[3].map((cap) => {
|
|
1449
|
+
if (!(cap instanceof Array) || cap.length !== 2 || cap[0] !== "import" && cap[0] !== "export" || typeof cap[1] !== "number") {
|
|
1450
|
+
throw new TypeError(`unknown map capture: ${JSON.stringify(cap)}`);
|
|
1451
|
+
}
|
|
1452
|
+
if (cap[0] === "export") {
|
|
1453
|
+
return this.importer.importStub(cap[1]);
|
|
1454
|
+
} else {
|
|
1455
|
+
let exp = this.importer.getExport(cap[1]);
|
|
1456
|
+
if (!exp) {
|
|
1457
|
+
throw new Error(`no such entry on exports table: ${cap[1]}`);
|
|
1458
|
+
}
|
|
1459
|
+
return exp.dup();
|
|
1460
|
+
}
|
|
1461
|
+
});
|
|
1462
|
+
let instructions = value[4];
|
|
1463
|
+
let resultHook = hook.map(path, captures, instructions);
|
|
1464
|
+
let promise = new RpcPromise(resultHook, []);
|
|
1465
|
+
this.promises.push({ promise, parent, property });
|
|
1466
|
+
return promise;
|
|
1467
|
+
}
|
|
1468
|
+
case "export":
|
|
1469
|
+
case "promise":
|
|
1470
|
+
if (typeof value[1] == "number") {
|
|
1471
|
+
if (value[0] == "promise") {
|
|
1472
|
+
let hook = this.importer.importPromise(value[1]);
|
|
1473
|
+
let promise = new RpcPromise(hook, []);
|
|
1474
|
+
this.promises.push({ parent, property, promise });
|
|
1475
|
+
return promise;
|
|
1476
|
+
} else {
|
|
1477
|
+
let hook = this.importer.importStub(value[1]);
|
|
1478
|
+
let stub = new RpcStub(hook);
|
|
1479
|
+
this.stubs.push(stub);
|
|
1480
|
+
return stub;
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
break;
|
|
1484
|
+
}
|
|
1485
|
+
throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
|
|
1486
|
+
} else if (value instanceof Object) {
|
|
1487
|
+
let result = value;
|
|
1488
|
+
for (let key in result) {
|
|
1489
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1490
|
+
this.evaluateImpl(result[key], result, key);
|
|
1491
|
+
delete result[key];
|
|
1492
|
+
} else {
|
|
1493
|
+
result[key] = this.evaluateImpl(result[key], result, key);
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
return result;
|
|
1497
|
+
} else {
|
|
1498
|
+
return value;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
function deserialize(value) {
|
|
1503
|
+
let payload = new Evaluator(NULL_IMPORTER).evaluate(JSON.parse(value));
|
|
1504
|
+
payload.dispose();
|
|
1505
|
+
return payload.value;
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
// src/rpc.ts
|
|
1509
|
+
var ImportTableEntry = class {
|
|
1510
|
+
constructor(session, importId, pulling) {
|
|
1511
|
+
this.session = session;
|
|
1512
|
+
this.importId = importId;
|
|
1513
|
+
if (pulling) {
|
|
1514
|
+
this.activePull = Promise.withResolvers();
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
localRefcount = 0;
|
|
1518
|
+
remoteRefcount = 1;
|
|
1519
|
+
activePull;
|
|
1520
|
+
resolution;
|
|
1521
|
+
// List of integer indexes into session.onBrokenCallbacks which are callbacks registered on
|
|
1522
|
+
// this import. Initialized on first use (so `undefined` is the same as an empty list).
|
|
1523
|
+
onBrokenRegistrations;
|
|
1524
|
+
resolve(resolution) {
|
|
1525
|
+
if (this.localRefcount == 0) {
|
|
1526
|
+
resolution.dispose();
|
|
1527
|
+
return;
|
|
1528
|
+
}
|
|
1529
|
+
this.resolution = resolution;
|
|
1530
|
+
this.sendRelease();
|
|
1531
|
+
if (this.onBrokenRegistrations) {
|
|
1532
|
+
for (let i of this.onBrokenRegistrations) {
|
|
1533
|
+
let callback = this.session.onBrokenCallbacks[i];
|
|
1534
|
+
let endIndex = this.session.onBrokenCallbacks.length;
|
|
1535
|
+
resolution.onBroken(callback);
|
|
1536
|
+
if (this.session.onBrokenCallbacks[endIndex] === callback) {
|
|
1537
|
+
delete this.session.onBrokenCallbacks[endIndex];
|
|
1538
|
+
} else {
|
|
1539
|
+
delete this.session.onBrokenCallbacks[i];
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
this.onBrokenRegistrations = void 0;
|
|
1543
|
+
}
|
|
1544
|
+
if (this.activePull) {
|
|
1545
|
+
this.activePull.resolve();
|
|
1546
|
+
this.activePull = void 0;
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
async awaitResolution() {
|
|
1550
|
+
if (!this.activePull) {
|
|
1551
|
+
this.session.sendPull(this.importId);
|
|
1552
|
+
this.activePull = Promise.withResolvers();
|
|
1553
|
+
}
|
|
1554
|
+
await this.activePull.promise;
|
|
1555
|
+
return this.resolution.pull();
|
|
1556
|
+
}
|
|
1557
|
+
dispose() {
|
|
1558
|
+
if (this.resolution) {
|
|
1559
|
+
this.resolution.dispose();
|
|
1560
|
+
} else {
|
|
1561
|
+
this.abort(new Error("RPC was canceled because the RpcPromise was disposed."));
|
|
1562
|
+
this.sendRelease();
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
abort(error) {
|
|
1566
|
+
if (!this.resolution) {
|
|
1567
|
+
this.resolution = new ErrorStubHook(error);
|
|
1568
|
+
if (this.activePull) {
|
|
1569
|
+
this.activePull.reject(error);
|
|
1570
|
+
this.activePull = void 0;
|
|
1571
|
+
}
|
|
1572
|
+
this.onBrokenRegistrations = void 0;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
onBroken(callback) {
|
|
1576
|
+
if (this.resolution) {
|
|
1577
|
+
this.resolution.onBroken(callback);
|
|
1578
|
+
} else {
|
|
1579
|
+
let index = this.session.onBrokenCallbacks.length;
|
|
1580
|
+
this.session.onBrokenCallbacks.push(callback);
|
|
1581
|
+
if (!this.onBrokenRegistrations) this.onBrokenRegistrations = [];
|
|
1582
|
+
this.onBrokenRegistrations.push(index);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
sendRelease() {
|
|
1586
|
+
if (this.remoteRefcount > 0) {
|
|
1587
|
+
this.session.sendRelease(this.importId, this.remoteRefcount);
|
|
1588
|
+
this.remoteRefcount = 0;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
};
|
|
1592
|
+
var RpcImportHook = class _RpcImportHook extends StubHook {
|
|
1593
|
+
// undefined when we're disposed
|
|
1594
|
+
// `pulling` is true if we already expect that this import is going to be resolved later, and
|
|
1595
|
+
// null if this import is not allowed to be pulled (i.e. it's a stub not a promise).
|
|
1596
|
+
constructor(isPromise, entry) {
|
|
1597
|
+
super();
|
|
1598
|
+
this.isPromise = isPromise;
|
|
1599
|
+
++entry.localRefcount;
|
|
1600
|
+
this.entry = entry;
|
|
1601
|
+
}
|
|
1602
|
+
entry;
|
|
1603
|
+
collectPath(path) {
|
|
1604
|
+
return this;
|
|
1605
|
+
}
|
|
1606
|
+
getEntry() {
|
|
1607
|
+
if (this.entry) {
|
|
1608
|
+
return this.entry;
|
|
1609
|
+
} else {
|
|
1610
|
+
throw new Error("This RpcImportHook was already disposed.");
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
// -------------------------------------------------------------------------------------
|
|
1614
|
+
// implements StubHook
|
|
1615
|
+
call(path, args) {
|
|
1616
|
+
let entry = this.getEntry();
|
|
1617
|
+
if (entry.resolution) {
|
|
1618
|
+
return entry.resolution.call(path, args);
|
|
1619
|
+
} else {
|
|
1620
|
+
return entry.session.sendCall(entry.importId, path, args);
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
map(path, captures, instructions) {
|
|
1624
|
+
let entry;
|
|
1625
|
+
try {
|
|
1626
|
+
entry = this.getEntry();
|
|
1627
|
+
} catch (err) {
|
|
1628
|
+
for (let cap of captures) {
|
|
1629
|
+
cap.dispose();
|
|
1630
|
+
}
|
|
1631
|
+
throw err;
|
|
1632
|
+
}
|
|
1633
|
+
if (entry.resolution) {
|
|
1634
|
+
return entry.resolution.map(path, captures, instructions);
|
|
1635
|
+
} else {
|
|
1636
|
+
return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
get(path) {
|
|
1640
|
+
let entry = this.getEntry();
|
|
1641
|
+
if (entry.resolution) {
|
|
1642
|
+
return entry.resolution.get(path);
|
|
1643
|
+
} else {
|
|
1644
|
+
return entry.session.sendCall(entry.importId, path);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
dup() {
|
|
1648
|
+
return new _RpcImportHook(false, this.getEntry());
|
|
1649
|
+
}
|
|
1650
|
+
pull() {
|
|
1651
|
+
let entry = this.getEntry();
|
|
1652
|
+
if (!this.isPromise) {
|
|
1653
|
+
throw new Error("Can't pull this hook because it's not a promise hook.");
|
|
1654
|
+
}
|
|
1655
|
+
if (entry.resolution) {
|
|
1656
|
+
return entry.resolution.pull();
|
|
1657
|
+
}
|
|
1658
|
+
return entry.awaitResolution();
|
|
1659
|
+
}
|
|
1660
|
+
ignoreUnhandledRejections() {
|
|
1661
|
+
}
|
|
1662
|
+
dispose() {
|
|
1663
|
+
let entry = this.entry;
|
|
1664
|
+
this.entry = void 0;
|
|
1665
|
+
if (entry) {
|
|
1666
|
+
if (--entry.localRefcount === 0) {
|
|
1667
|
+
entry.dispose();
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
onBroken(callback) {
|
|
1672
|
+
if (this.entry) {
|
|
1673
|
+
this.entry.onBroken(callback);
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
};
|
|
1677
|
+
var RpcMainHook = class extends RpcImportHook {
|
|
1678
|
+
session;
|
|
1679
|
+
constructor(entry) {
|
|
1680
|
+
super(false, entry);
|
|
1681
|
+
this.session = entry.session;
|
|
1682
|
+
}
|
|
1683
|
+
dispose() {
|
|
1684
|
+
if (this.session) {
|
|
1685
|
+
let session = this.session;
|
|
1686
|
+
this.session = void 0;
|
|
1687
|
+
session.shutdown();
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
var RpcSessionImpl = class {
|
|
1692
|
+
constructor(transport, mainHook, options) {
|
|
1693
|
+
this.transport = transport;
|
|
1694
|
+
this.options = options;
|
|
1695
|
+
this.exports.push({ hook: mainHook, refcount: 1 });
|
|
1696
|
+
this.imports.push(new ImportTableEntry(this, 0, false));
|
|
1697
|
+
let rejectFunc;
|
|
1698
|
+
let abortPromise = new Promise((resolve, reject) => {
|
|
1699
|
+
rejectFunc = reject;
|
|
1700
|
+
});
|
|
1701
|
+
this.cancelReadLoop = rejectFunc;
|
|
1702
|
+
this.readLoop(abortPromise).catch((err) => this.abort(err));
|
|
1703
|
+
}
|
|
1704
|
+
exports = [];
|
|
1705
|
+
reverseExports = /* @__PURE__ */ new Map();
|
|
1706
|
+
imports = [];
|
|
1707
|
+
abortReason;
|
|
1708
|
+
cancelReadLoop;
|
|
1709
|
+
// We assign positive numbers to imports we initiate, and negative numbers to exports we
|
|
1710
|
+
// initiate. So the next import ID is just `imports.length`, but the next export ID needs
|
|
1711
|
+
// to be tracked explicitly.
|
|
1712
|
+
nextExportId = -1;
|
|
1713
|
+
// If set, call this when all incoming calls are complete.
|
|
1714
|
+
onBatchDone;
|
|
1715
|
+
// How many promises is our peer expecting us to resolve?
|
|
1716
|
+
pullCount = 0;
|
|
1717
|
+
// Sparse array of onBrokenCallback registrations. Items are strictly appended to the end but
|
|
1718
|
+
// may be deleted from the middle (hence leaving the array sparse).
|
|
1719
|
+
onBrokenCallbacks = [];
|
|
1720
|
+
// Should only be called once immediately after construction.
|
|
1721
|
+
getMainImport() {
|
|
1722
|
+
return new RpcMainHook(this.imports[0]);
|
|
1723
|
+
}
|
|
1724
|
+
shutdown() {
|
|
1725
|
+
this.abort(new Error("RPC session was shut down by disposing the main stub"), false);
|
|
1726
|
+
}
|
|
1727
|
+
exportStub(hook) {
|
|
1728
|
+
if (this.abortReason) throw this.abortReason;
|
|
1729
|
+
let existingExportId = this.reverseExports.get(hook);
|
|
1730
|
+
if (existingExportId !== void 0) {
|
|
1731
|
+
++this.exports[existingExportId].refcount;
|
|
1732
|
+
return existingExportId;
|
|
1733
|
+
} else {
|
|
1734
|
+
let exportId = this.nextExportId--;
|
|
1735
|
+
this.exports[exportId] = { hook, refcount: 1 };
|
|
1736
|
+
this.reverseExports.set(hook, exportId);
|
|
1737
|
+
return exportId;
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
exportPromise(hook) {
|
|
1741
|
+
if (this.abortReason) throw this.abortReason;
|
|
1742
|
+
let exportId = this.nextExportId--;
|
|
1743
|
+
this.exports[exportId] = { hook, refcount: 1 };
|
|
1744
|
+
this.reverseExports.set(hook, exportId);
|
|
1745
|
+
this.ensureResolvingExport(exportId);
|
|
1746
|
+
return exportId;
|
|
1747
|
+
}
|
|
1748
|
+
unexport(ids) {
|
|
1749
|
+
for (let id of ids) {
|
|
1750
|
+
this.releaseExport(id, 1);
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
releaseExport(exportId, refcount) {
|
|
1754
|
+
let entry = this.exports[exportId];
|
|
1755
|
+
if (!entry) {
|
|
1756
|
+
throw new Error(`no such export ID: ${exportId}`);
|
|
1757
|
+
}
|
|
1758
|
+
if (entry.refcount < refcount) {
|
|
1759
|
+
throw new Error(`refcount would go negative: ${entry.refcount} < ${refcount}`);
|
|
1760
|
+
}
|
|
1761
|
+
entry.refcount -= refcount;
|
|
1762
|
+
if (entry.refcount === 0) {
|
|
1763
|
+
delete this.exports[exportId];
|
|
1764
|
+
this.reverseExports.delete(entry.hook);
|
|
1765
|
+
entry.hook.dispose();
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
onSendError(error) {
|
|
1769
|
+
if (this.options.onSendError) {
|
|
1770
|
+
return this.options.onSendError(error);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
ensureResolvingExport(exportId) {
|
|
1774
|
+
let exp = this.exports[exportId];
|
|
1775
|
+
if (!exp) {
|
|
1776
|
+
throw new Error(`no such export ID: ${exportId}`);
|
|
1777
|
+
}
|
|
1778
|
+
if (!exp.pull) {
|
|
1779
|
+
let resolve = async () => {
|
|
1780
|
+
let hook = exp.hook;
|
|
1781
|
+
for (; ; ) {
|
|
1782
|
+
let payload = await hook.pull();
|
|
1783
|
+
if (payload.value instanceof RpcStub) {
|
|
1784
|
+
let { hook: inner, pathIfPromise } = unwrapStubAndPath(payload.value);
|
|
1785
|
+
if (pathIfPromise && pathIfPromise.length == 0) {
|
|
1786
|
+
if (this.getImport(hook) === void 0) {
|
|
1787
|
+
hook = inner;
|
|
1788
|
+
continue;
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
return payload;
|
|
1793
|
+
}
|
|
1794
|
+
};
|
|
1795
|
+
++this.pullCount;
|
|
1796
|
+
exp.pull = resolve().then(
|
|
1797
|
+
(payload) => {
|
|
1798
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1799
|
+
this.send(["resolve", exportId, value]);
|
|
1800
|
+
},
|
|
1801
|
+
(error) => {
|
|
1802
|
+
this.send(["reject", exportId, Devaluator.devaluate(error, void 0, this)]);
|
|
1803
|
+
}
|
|
1804
|
+
).catch(
|
|
1805
|
+
(error) => {
|
|
1806
|
+
try {
|
|
1807
|
+
this.send(["reject", exportId, Devaluator.devaluate(error, void 0, this)]);
|
|
1808
|
+
} catch (error2) {
|
|
1809
|
+
this.abort(error2);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
).finally(() => {
|
|
1813
|
+
if (--this.pullCount === 0) {
|
|
1814
|
+
if (this.onBatchDone) {
|
|
1815
|
+
this.onBatchDone.resolve();
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
getImport(hook) {
|
|
1822
|
+
if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this) {
|
|
1823
|
+
return hook.entry.importId;
|
|
1824
|
+
} else {
|
|
1825
|
+
return void 0;
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
importStub(idx) {
|
|
1829
|
+
if (this.abortReason) throw this.abortReason;
|
|
1830
|
+
let entry = this.imports[idx];
|
|
1831
|
+
if (!entry) {
|
|
1832
|
+
entry = new ImportTableEntry(this, idx, false);
|
|
1833
|
+
this.imports[idx] = entry;
|
|
1834
|
+
}
|
|
1835
|
+
return new RpcImportHook(
|
|
1836
|
+
/*isPromise=*/
|
|
1837
|
+
false,
|
|
1838
|
+
entry
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
importPromise(idx) {
|
|
1842
|
+
if (this.abortReason) throw this.abortReason;
|
|
1843
|
+
if (this.imports[idx]) {
|
|
1844
|
+
return new ErrorStubHook(new Error(
|
|
1845
|
+
"Bug in RPC system: The peer sent a promise reusing an existing export ID."
|
|
1846
|
+
));
|
|
1847
|
+
}
|
|
1848
|
+
let entry = new ImportTableEntry(this, idx, true);
|
|
1849
|
+
this.imports[idx] = entry;
|
|
1850
|
+
return new RpcImportHook(
|
|
1851
|
+
/*isPromise=*/
|
|
1852
|
+
true,
|
|
1853
|
+
entry
|
|
1854
|
+
);
|
|
1855
|
+
}
|
|
1856
|
+
getExport(idx) {
|
|
1857
|
+
return this.exports[idx]?.hook;
|
|
1858
|
+
}
|
|
1859
|
+
send(msg) {
|
|
1860
|
+
if (this.abortReason !== void 0) {
|
|
1861
|
+
return;
|
|
1862
|
+
}
|
|
1863
|
+
let msgText;
|
|
1864
|
+
try {
|
|
1865
|
+
msgText = JSON.stringify(msg);
|
|
1866
|
+
} catch (err) {
|
|
1867
|
+
try {
|
|
1868
|
+
this.abort(err);
|
|
1869
|
+
} catch (err2) {
|
|
1870
|
+
}
|
|
1871
|
+
throw err;
|
|
1872
|
+
}
|
|
1873
|
+
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1874
|
+
}
|
|
1875
|
+
sendCall(id, path, args) {
|
|
1876
|
+
if (this.abortReason) throw this.abortReason;
|
|
1877
|
+
let value = ["pipeline", id, path];
|
|
1878
|
+
if (args) {
|
|
1879
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
1880
|
+
value.push(devalue[0]);
|
|
1881
|
+
}
|
|
1882
|
+
this.send(["push", value]);
|
|
1883
|
+
let entry = new ImportTableEntry(this, this.imports.length, false);
|
|
1884
|
+
this.imports.push(entry);
|
|
1885
|
+
return new RpcImportHook(
|
|
1886
|
+
/*isPromise=*/
|
|
1887
|
+
true,
|
|
1888
|
+
entry
|
|
1889
|
+
);
|
|
1890
|
+
}
|
|
1891
|
+
sendMap(id, path, captures, instructions) {
|
|
1892
|
+
if (this.abortReason) {
|
|
1893
|
+
for (let cap of captures) {
|
|
1894
|
+
cap.dispose();
|
|
1895
|
+
}
|
|
1896
|
+
throw this.abortReason;
|
|
1897
|
+
}
|
|
1898
|
+
let devaluedCaptures = captures.map((hook) => {
|
|
1899
|
+
let importId = this.getImport(hook);
|
|
1900
|
+
if (importId !== void 0) {
|
|
1901
|
+
return ["import", importId];
|
|
1902
|
+
} else {
|
|
1903
|
+
return ["export", this.exportStub(hook)];
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
let value = ["remap", id, path, devaluedCaptures, instructions];
|
|
1907
|
+
this.send(["push", value]);
|
|
1908
|
+
let entry = new ImportTableEntry(this, this.imports.length, false);
|
|
1909
|
+
this.imports.push(entry);
|
|
1910
|
+
return new RpcImportHook(
|
|
1911
|
+
/*isPromise=*/
|
|
1912
|
+
true,
|
|
1913
|
+
entry
|
|
1914
|
+
);
|
|
1915
|
+
}
|
|
1916
|
+
sendPull(id) {
|
|
1917
|
+
if (this.abortReason) throw this.abortReason;
|
|
1918
|
+
this.send(["pull", id]);
|
|
1919
|
+
}
|
|
1920
|
+
sendRelease(id, remoteRefcount) {
|
|
1921
|
+
if (this.abortReason) return;
|
|
1922
|
+
this.send(["release", id, remoteRefcount]);
|
|
1923
|
+
delete this.imports[id];
|
|
1924
|
+
}
|
|
1925
|
+
abort(error, trySendAbortMessage = true) {
|
|
1926
|
+
if (this.abortReason !== void 0) return;
|
|
1927
|
+
this.cancelReadLoop(error);
|
|
1928
|
+
if (trySendAbortMessage) {
|
|
1929
|
+
try {
|
|
1930
|
+
this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
|
|
1931
|
+
});
|
|
1932
|
+
} catch (err) {
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
if (error === void 0) {
|
|
1936
|
+
error = "undefined";
|
|
1937
|
+
}
|
|
1938
|
+
this.abortReason = error;
|
|
1939
|
+
if (this.onBatchDone) {
|
|
1940
|
+
this.onBatchDone.reject(error);
|
|
1941
|
+
}
|
|
1942
|
+
if (this.transport.abort) {
|
|
1943
|
+
try {
|
|
1944
|
+
this.transport.abort(error);
|
|
1945
|
+
} catch (err) {
|
|
1946
|
+
Promise.resolve(err);
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
for (let i in this.onBrokenCallbacks) {
|
|
1950
|
+
try {
|
|
1951
|
+
this.onBrokenCallbacks[i](error);
|
|
1952
|
+
} catch (err) {
|
|
1953
|
+
Promise.resolve(err);
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
for (let i in this.imports) {
|
|
1957
|
+
this.imports[i].abort(error);
|
|
1958
|
+
}
|
|
1959
|
+
for (let i in this.exports) {
|
|
1960
|
+
this.exports[i].hook.dispose();
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
async readLoop(abortPromise) {
|
|
1964
|
+
while (!this.abortReason) {
|
|
1965
|
+
let msg = JSON.parse(await Promise.race([this.transport.receive(), abortPromise]));
|
|
1966
|
+
if (this.abortReason) break;
|
|
1967
|
+
if (msg instanceof Array) {
|
|
1968
|
+
switch (msg[0]) {
|
|
1969
|
+
case "push":
|
|
1970
|
+
if (msg.length > 1) {
|
|
1971
|
+
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
1972
|
+
let hook = new PayloadStubHook(payload);
|
|
1973
|
+
hook.ignoreUnhandledRejections();
|
|
1974
|
+
this.exports.push({ hook, refcount: 1 });
|
|
1975
|
+
continue;
|
|
1976
|
+
}
|
|
1977
|
+
break;
|
|
1978
|
+
case "pull": {
|
|
1979
|
+
let exportId = msg[1];
|
|
1980
|
+
if (typeof exportId == "number") {
|
|
1981
|
+
this.ensureResolvingExport(exportId);
|
|
1982
|
+
continue;
|
|
1983
|
+
}
|
|
1984
|
+
break;
|
|
1985
|
+
}
|
|
1986
|
+
case "resolve":
|
|
1987
|
+
// ["resolve", ExportId, Expression]
|
|
1988
|
+
case "reject": {
|
|
1989
|
+
let importId = msg[1];
|
|
1990
|
+
if (typeof importId == "number" && msg.length > 2) {
|
|
1991
|
+
let imp = this.imports[importId];
|
|
1992
|
+
if (imp) {
|
|
1993
|
+
if (msg[0] == "resolve") {
|
|
1994
|
+
imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
1995
|
+
} else {
|
|
1996
|
+
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
1997
|
+
payload.dispose();
|
|
1998
|
+
imp.resolve(new ErrorStubHook(payload.value));
|
|
1999
|
+
}
|
|
2000
|
+
} else {
|
|
2001
|
+
if (msg[0] == "resolve") {
|
|
2002
|
+
new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
continue;
|
|
2006
|
+
}
|
|
2007
|
+
break;
|
|
2008
|
+
}
|
|
2009
|
+
case "release": {
|
|
2010
|
+
let exportId = msg[1];
|
|
2011
|
+
let refcount = msg[2];
|
|
2012
|
+
if (typeof exportId == "number" && typeof refcount == "number") {
|
|
2013
|
+
this.releaseExport(exportId, refcount);
|
|
2014
|
+
continue;
|
|
2015
|
+
}
|
|
2016
|
+
break;
|
|
2017
|
+
}
|
|
2018
|
+
case "abort": {
|
|
2019
|
+
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2020
|
+
payload.dispose();
|
|
2021
|
+
this.abort(payload, false);
|
|
2022
|
+
break;
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
throw new Error(`bad RPC message: ${JSON.stringify(msg)}`);
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
async drain() {
|
|
2030
|
+
if (this.abortReason) {
|
|
2031
|
+
throw this.abortReason;
|
|
2032
|
+
}
|
|
2033
|
+
if (this.pullCount > 0) {
|
|
2034
|
+
let { promise, resolve, reject } = Promise.withResolvers();
|
|
2035
|
+
this.onBatchDone = { resolve, reject };
|
|
2036
|
+
await promise;
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
getStats() {
|
|
2040
|
+
let result = { imports: 0, exports: 0 };
|
|
2041
|
+
for (let i in this.imports) {
|
|
2042
|
+
++result.imports;
|
|
2043
|
+
}
|
|
2044
|
+
for (let i in this.exports) {
|
|
2045
|
+
++result.exports;
|
|
2046
|
+
}
|
|
2047
|
+
return result;
|
|
2048
|
+
}
|
|
2049
|
+
};
|
|
2050
|
+
var RpcSession = class {
|
|
2051
|
+
#session;
|
|
2052
|
+
#mainStub;
|
|
2053
|
+
constructor(transport, localMain, options = {}) {
|
|
2054
|
+
let mainHook;
|
|
2055
|
+
if (localMain) {
|
|
2056
|
+
mainHook = new PayloadStubHook(RpcPayload.fromAppReturn(localMain));
|
|
2057
|
+
} else {
|
|
2058
|
+
mainHook = new ErrorStubHook(new Error("This connection has no main object."));
|
|
2059
|
+
}
|
|
2060
|
+
this.#session = new RpcSessionImpl(transport, mainHook, options);
|
|
2061
|
+
this.#mainStub = new RpcStub(this.#session.getMainImport());
|
|
2062
|
+
}
|
|
2063
|
+
getRemoteMain() {
|
|
2064
|
+
return this.#mainStub;
|
|
2065
|
+
}
|
|
2066
|
+
getStats() {
|
|
2067
|
+
return this.#session.getStats();
|
|
2068
|
+
}
|
|
2069
|
+
drain() {
|
|
2070
|
+
return this.#session.drain();
|
|
2071
|
+
}
|
|
2072
|
+
};
|
|
2073
|
+
|
|
2074
|
+
// src/websocket.ts
|
|
2075
|
+
function newWebSocketRpcSession(webSocket, localMain, options) {
|
|
2076
|
+
if (typeof webSocket === "string") {
|
|
2077
|
+
webSocket = new WebSocket(webSocket);
|
|
2078
|
+
}
|
|
2079
|
+
let transport = new WebSocketTransport(webSocket);
|
|
2080
|
+
let rpc = new RpcSession(transport, localMain, options);
|
|
2081
|
+
return rpc.getRemoteMain();
|
|
2082
|
+
}
|
|
2083
|
+
function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
2084
|
+
if (request.headers.get("Upgrade")?.toLowerCase() !== "websocket") {
|
|
2085
|
+
return new Response("This endpoint only accepts WebSocket requests.", { status: 400 });
|
|
2086
|
+
}
|
|
2087
|
+
let pair = new WebSocketPair();
|
|
2088
|
+
let server = pair[0];
|
|
2089
|
+
server.accept();
|
|
2090
|
+
newWebSocketRpcSession(server, localMain, options);
|
|
2091
|
+
return new Response(null, {
|
|
2092
|
+
status: 101,
|
|
2093
|
+
webSocket: pair[1]
|
|
2094
|
+
});
|
|
2095
|
+
}
|
|
2096
|
+
var WebSocketTransport = class {
|
|
2097
|
+
constructor(webSocket) {
|
|
2098
|
+
this.#webSocket = webSocket;
|
|
2099
|
+
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2100
|
+
this.#sendQueue = [];
|
|
2101
|
+
webSocket.addEventListener("open", (event) => {
|
|
2102
|
+
try {
|
|
2103
|
+
for (let message of this.#sendQueue) {
|
|
2104
|
+
webSocket.send(message);
|
|
2105
|
+
}
|
|
2106
|
+
} catch (err) {
|
|
2107
|
+
this.#receivedError(err);
|
|
2108
|
+
}
|
|
2109
|
+
this.#sendQueue = void 0;
|
|
2110
|
+
});
|
|
2111
|
+
}
|
|
2112
|
+
webSocket.addEventListener("message", (event) => {
|
|
2113
|
+
if (this.#error) ; else if (typeof event.data === "string") {
|
|
2114
|
+
if (this.#receiveResolver) {
|
|
2115
|
+
this.#receiveResolver(event.data);
|
|
2116
|
+
this.#receiveResolver = void 0;
|
|
2117
|
+
this.#receiveRejecter = void 0;
|
|
2118
|
+
} else {
|
|
2119
|
+
this.#receiveQueue.push(event.data);
|
|
2120
|
+
}
|
|
2121
|
+
} else {
|
|
2122
|
+
this.#receivedError(new TypeError("Received non-string message from WebSocket."));
|
|
2123
|
+
}
|
|
2124
|
+
});
|
|
2125
|
+
webSocket.addEventListener("close", (event) => {
|
|
2126
|
+
this.#receivedError(new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
2127
|
+
});
|
|
2128
|
+
webSocket.addEventListener("error", (event) => {
|
|
2129
|
+
this.#receivedError(new Error(`WebSocket connection failed.`));
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
2132
|
+
#webSocket;
|
|
2133
|
+
#sendQueue;
|
|
2134
|
+
// only if not opened yet
|
|
2135
|
+
#receiveResolver;
|
|
2136
|
+
#receiveRejecter;
|
|
2137
|
+
#receiveQueue = [];
|
|
2138
|
+
#error;
|
|
2139
|
+
async send(message) {
|
|
2140
|
+
if (this.#sendQueue === void 0) {
|
|
2141
|
+
this.#webSocket.send(message);
|
|
2142
|
+
} else {
|
|
2143
|
+
this.#sendQueue.push(message);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
async receive() {
|
|
2147
|
+
if (this.#receiveQueue.length > 0) {
|
|
2148
|
+
return this.#receiveQueue.shift();
|
|
2149
|
+
} else if (this.#error) {
|
|
2150
|
+
throw this.#error;
|
|
2151
|
+
} else {
|
|
2152
|
+
return new Promise((resolve, reject) => {
|
|
2153
|
+
this.#receiveResolver = resolve;
|
|
2154
|
+
this.#receiveRejecter = reject;
|
|
2155
|
+
});
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
abort(reason) {
|
|
2159
|
+
let message;
|
|
2160
|
+
if (reason instanceof Error) {
|
|
2161
|
+
message = reason.message;
|
|
2162
|
+
} else {
|
|
2163
|
+
message = `${reason}`;
|
|
2164
|
+
}
|
|
2165
|
+
this.#webSocket.close(3e3, message);
|
|
2166
|
+
if (!this.#error) {
|
|
2167
|
+
this.#error = reason;
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
#receivedError(reason) {
|
|
2171
|
+
if (!this.#error) {
|
|
2172
|
+
this.#error = reason;
|
|
2173
|
+
if (this.#receiveRejecter) {
|
|
2174
|
+
this.#receiveRejecter(reason);
|
|
2175
|
+
this.#receiveResolver = void 0;
|
|
2176
|
+
this.#receiveRejecter = void 0;
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
};
|
|
2181
|
+
|
|
2182
|
+
// src/batch.ts
|
|
2183
|
+
var BatchClientTransport = class {
|
|
2184
|
+
constructor(sendBatch) {
|
|
2185
|
+
this.#promise = this.#scheduleBatch(sendBatch);
|
|
2186
|
+
}
|
|
2187
|
+
#promise;
|
|
2188
|
+
#aborted;
|
|
2189
|
+
#batchToSend = [];
|
|
2190
|
+
#batchToReceive = null;
|
|
2191
|
+
async send(message) {
|
|
2192
|
+
if (this.#batchToSend !== null) {
|
|
2193
|
+
this.#batchToSend.push(message);
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
async receive() {
|
|
2197
|
+
if (!this.#batchToReceive) {
|
|
2198
|
+
await this.#promise;
|
|
2199
|
+
}
|
|
2200
|
+
let msg = this.#batchToReceive.shift();
|
|
2201
|
+
if (msg !== void 0) {
|
|
2202
|
+
return msg;
|
|
2203
|
+
} else {
|
|
2204
|
+
throw new Error("Batch RPC request ended.");
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
abort(reason) {
|
|
2208
|
+
this.#aborted = reason;
|
|
2209
|
+
}
|
|
2210
|
+
async #scheduleBatch(sendBatch) {
|
|
2211
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
2212
|
+
if (this.#aborted !== void 0) {
|
|
2213
|
+
throw this.#aborted;
|
|
2214
|
+
}
|
|
2215
|
+
let batch = this.#batchToSend;
|
|
2216
|
+
this.#batchToSend = null;
|
|
2217
|
+
this.#batchToReceive = await sendBatch(batch);
|
|
2218
|
+
}
|
|
2219
|
+
};
|
|
2220
|
+
function newHttpBatchRpcSession(urlOrRequest, options) {
|
|
2221
|
+
let sendBatch = async (batch) => {
|
|
2222
|
+
let response = await fetch(urlOrRequest, {
|
|
2223
|
+
method: "POST",
|
|
2224
|
+
body: batch.join("\n")
|
|
2225
|
+
});
|
|
2226
|
+
if (!response.ok) {
|
|
2227
|
+
response.body?.cancel();
|
|
2228
|
+
throw new Error(`RPC request failed: ${response.status} ${response.statusText}`);
|
|
2229
|
+
}
|
|
2230
|
+
let body = await response.text();
|
|
2231
|
+
return body == "" ? [] : body.split("\n");
|
|
2232
|
+
};
|
|
2233
|
+
let transport = new BatchClientTransport(sendBatch);
|
|
2234
|
+
let rpc = new RpcSession(transport, void 0, options);
|
|
2235
|
+
return rpc.getRemoteMain();
|
|
2236
|
+
}
|
|
2237
|
+
var BatchServerTransport = class {
|
|
2238
|
+
constructor(batch) {
|
|
2239
|
+
this.#batchToReceive = batch;
|
|
2240
|
+
}
|
|
2241
|
+
#batchToSend = [];
|
|
2242
|
+
#batchToReceive;
|
|
2243
|
+
#allReceived = Promise.withResolvers();
|
|
2244
|
+
async send(message) {
|
|
2245
|
+
this.#batchToSend.push(message);
|
|
2246
|
+
}
|
|
2247
|
+
async receive() {
|
|
2248
|
+
let msg = this.#batchToReceive.shift();
|
|
2249
|
+
if (msg !== void 0) {
|
|
2250
|
+
return msg;
|
|
2251
|
+
} else {
|
|
2252
|
+
this.#allReceived.resolve();
|
|
2253
|
+
return new Promise((r) => {
|
|
2254
|
+
});
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
abort(reason) {
|
|
2258
|
+
this.#allReceived.reject(reason);
|
|
2259
|
+
}
|
|
2260
|
+
whenAllReceived() {
|
|
2261
|
+
return this.#allReceived.promise;
|
|
2262
|
+
}
|
|
2263
|
+
getResponseBody() {
|
|
2264
|
+
return this.#batchToSend.join("\n");
|
|
2265
|
+
}
|
|
2266
|
+
};
|
|
2267
|
+
async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
2268
|
+
if (request.method !== "POST") {
|
|
2269
|
+
return new Response("This endpoint only accepts POST requests.", { status: 405 });
|
|
2270
|
+
}
|
|
2271
|
+
let body = await request.text();
|
|
2272
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2273
|
+
let transport = new BatchServerTransport(batch);
|
|
2274
|
+
let rpc = new RpcSession(transport, localMain, options);
|
|
2275
|
+
await transport.whenAllReceived();
|
|
2276
|
+
await rpc.drain();
|
|
2277
|
+
return new Response(transport.getResponseBody());
|
|
2278
|
+
}
|
|
2279
|
+
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2280
|
+
if (request.method !== "POST") {
|
|
2281
|
+
response.writeHead(405, "This endpoint only accepts POST requests.");
|
|
2282
|
+
}
|
|
2283
|
+
let body = await new Promise((resolve, reject) => {
|
|
2284
|
+
let chunks = [];
|
|
2285
|
+
request.on("data", (chunk) => {
|
|
2286
|
+
chunks.push(chunk);
|
|
2287
|
+
});
|
|
2288
|
+
request.on("end", () => {
|
|
2289
|
+
resolve(Buffer.concat(chunks).toString());
|
|
2290
|
+
});
|
|
2291
|
+
request.on("error", reject);
|
|
2292
|
+
});
|
|
2293
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2294
|
+
let transport = new BatchServerTransport(batch);
|
|
2295
|
+
let rpc = new RpcSession(transport, localMain, options);
|
|
2296
|
+
await transport.whenAllReceived();
|
|
2297
|
+
await rpc.drain();
|
|
2298
|
+
response.writeHead(200, options?.headers);
|
|
2299
|
+
response.end(transport.getResponseBody());
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
// src/messageport.ts
|
|
2303
|
+
function newMessagePortRpcSession(port, localMain, options) {
|
|
2304
|
+
let transport = new MessagePortTransport(port);
|
|
2305
|
+
let rpc = new RpcSession(transport, localMain, options);
|
|
2306
|
+
return rpc.getRemoteMain();
|
|
2307
|
+
}
|
|
2308
|
+
var MessagePortTransport = class {
|
|
2309
|
+
constructor(port) {
|
|
2310
|
+
this.#port = port;
|
|
2311
|
+
port.start();
|
|
2312
|
+
port.addEventListener("message", (event) => {
|
|
2313
|
+
if (this.#error) ; else if (event.data === null) {
|
|
2314
|
+
this.#receivedError(new Error("Peer closed MessagePort connection."));
|
|
2315
|
+
} else if (typeof event.data === "string") {
|
|
2316
|
+
if (this.#receiveResolver) {
|
|
2317
|
+
this.#receiveResolver(event.data);
|
|
2318
|
+
this.#receiveResolver = void 0;
|
|
2319
|
+
this.#receiveRejecter = void 0;
|
|
2320
|
+
} else {
|
|
2321
|
+
this.#receiveQueue.push(event.data);
|
|
2322
|
+
}
|
|
2323
|
+
} else {
|
|
2324
|
+
this.#receivedError(new TypeError("Received non-string message from MessagePort."));
|
|
2325
|
+
}
|
|
2326
|
+
});
|
|
2327
|
+
port.addEventListener("messageerror", (event) => {
|
|
2328
|
+
this.#receivedError(new Error("MessagePort message error."));
|
|
2329
|
+
});
|
|
2330
|
+
}
|
|
2331
|
+
#port;
|
|
2332
|
+
#receiveResolver;
|
|
2333
|
+
#receiveRejecter;
|
|
2334
|
+
#receiveQueue = [];
|
|
2335
|
+
#error;
|
|
2336
|
+
async send(message) {
|
|
2337
|
+
if (this.#error) {
|
|
2338
|
+
throw this.#error;
|
|
2339
|
+
}
|
|
2340
|
+
this.#port.postMessage(message);
|
|
2341
|
+
}
|
|
2342
|
+
async receive() {
|
|
2343
|
+
if (this.#receiveQueue.length > 0) {
|
|
2344
|
+
return this.#receiveQueue.shift();
|
|
2345
|
+
} else if (this.#error) {
|
|
2346
|
+
throw this.#error;
|
|
2347
|
+
} else {
|
|
2348
|
+
return new Promise((resolve, reject) => {
|
|
2349
|
+
this.#receiveResolver = resolve;
|
|
2350
|
+
this.#receiveRejecter = reject;
|
|
2351
|
+
});
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
abort(reason) {
|
|
2355
|
+
try {
|
|
2356
|
+
this.#port.postMessage(null);
|
|
2357
|
+
} catch (err) {
|
|
2358
|
+
}
|
|
2359
|
+
this.#port.close();
|
|
2360
|
+
if (!this.#error) {
|
|
2361
|
+
this.#error = reason;
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
#receivedError(reason) {
|
|
2365
|
+
if (!this.#error) {
|
|
2366
|
+
this.#error = reason;
|
|
2367
|
+
if (this.#receiveRejecter) {
|
|
2368
|
+
this.#receiveRejecter(reason);
|
|
2369
|
+
this.#receiveResolver = void 0;
|
|
2370
|
+
this.#receiveRejecter = void 0;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
};
|
|
2375
|
+
|
|
2376
|
+
// src/map.ts
|
|
2377
|
+
var currentMapBuilder;
|
|
2378
|
+
var MapBuilder = class {
|
|
2379
|
+
context;
|
|
2380
|
+
captureMap = /* @__PURE__ */ new Map();
|
|
2381
|
+
instructions = [];
|
|
2382
|
+
constructor(subject, path) {
|
|
2383
|
+
if (currentMapBuilder) {
|
|
2384
|
+
this.context = {
|
|
2385
|
+
parent: currentMapBuilder,
|
|
2386
|
+
captures: [],
|
|
2387
|
+
subject: currentMapBuilder.capture(subject),
|
|
2388
|
+
path
|
|
2389
|
+
};
|
|
2390
|
+
} else {
|
|
2391
|
+
this.context = {
|
|
2392
|
+
parent: void 0,
|
|
2393
|
+
captures: [],
|
|
2394
|
+
subject,
|
|
2395
|
+
path
|
|
2396
|
+
};
|
|
2397
|
+
}
|
|
2398
|
+
currentMapBuilder = this;
|
|
2399
|
+
}
|
|
2400
|
+
unregister() {
|
|
2401
|
+
currentMapBuilder = this.context.parent;
|
|
2402
|
+
}
|
|
2403
|
+
makeInput() {
|
|
2404
|
+
return new MapVariableHook(this, 0);
|
|
2405
|
+
}
|
|
2406
|
+
makeOutput(result) {
|
|
2407
|
+
let devalued;
|
|
2408
|
+
try {
|
|
2409
|
+
devalued = Devaluator.devaluate(result.value, void 0, this, result);
|
|
2410
|
+
} finally {
|
|
2411
|
+
result.dispose();
|
|
2412
|
+
}
|
|
2413
|
+
this.instructions.push(devalued);
|
|
2414
|
+
if (this.context.parent) {
|
|
2415
|
+
this.context.parent.instructions.push(
|
|
2416
|
+
[
|
|
2417
|
+
"remap",
|
|
2418
|
+
this.context.subject,
|
|
2419
|
+
this.context.path,
|
|
2420
|
+
this.context.captures.map((cap) => ["import", cap]),
|
|
2421
|
+
this.instructions
|
|
2422
|
+
]
|
|
2423
|
+
);
|
|
2424
|
+
return new MapVariableHook(this.context.parent, this.context.parent.instructions.length);
|
|
2425
|
+
} else {
|
|
2426
|
+
return this.context.subject.map(this.context.path, this.context.captures, this.instructions);
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
pushCall(hook, path, params) {
|
|
2430
|
+
let devalued = Devaluator.devaluate(params.value, void 0, this, params);
|
|
2431
|
+
devalued = devalued[0];
|
|
2432
|
+
let subject = this.capture(hook.dup());
|
|
2433
|
+
this.instructions.push(["pipeline", subject, path, devalued]);
|
|
2434
|
+
return new MapVariableHook(this, this.instructions.length);
|
|
2435
|
+
}
|
|
2436
|
+
pushGet(hook, path) {
|
|
2437
|
+
let subject = this.capture(hook.dup());
|
|
2438
|
+
this.instructions.push(["pipeline", subject, path]);
|
|
2439
|
+
return new MapVariableHook(this, this.instructions.length);
|
|
2440
|
+
}
|
|
2441
|
+
capture(hook) {
|
|
2442
|
+
if (hook instanceof MapVariableHook && hook.mapper === this) {
|
|
2443
|
+
return hook.idx;
|
|
2444
|
+
}
|
|
2445
|
+
let result = this.captureMap.get(hook);
|
|
2446
|
+
if (result === void 0) {
|
|
2447
|
+
if (this.context.parent) {
|
|
2448
|
+
let parentIdx = this.context.parent.capture(hook);
|
|
2449
|
+
this.context.captures.push(parentIdx);
|
|
2450
|
+
} else {
|
|
2451
|
+
this.context.captures.push(hook);
|
|
2452
|
+
}
|
|
2453
|
+
result = -this.context.captures.length;
|
|
2454
|
+
this.captureMap.set(hook, result);
|
|
2455
|
+
}
|
|
2456
|
+
return result;
|
|
2457
|
+
}
|
|
2458
|
+
// ---------------------------------------------------------------------------
|
|
2459
|
+
// implements Exporter
|
|
2460
|
+
exportStub(hook) {
|
|
2461
|
+
throw new Error(
|
|
2462
|
+
"Can't construct an RpcTarget or RPC callback inside a mapper function. Try creating a new RpcStub outside the callback first, then using it inside the callback."
|
|
2463
|
+
);
|
|
2464
|
+
}
|
|
2465
|
+
exportPromise(hook) {
|
|
2466
|
+
return this.exportStub(hook);
|
|
2467
|
+
}
|
|
2468
|
+
getImport(hook) {
|
|
2469
|
+
return this.capture(hook);
|
|
2470
|
+
}
|
|
2471
|
+
unexport(ids) {
|
|
2472
|
+
}
|
|
2473
|
+
onSendError(error) {
|
|
2474
|
+
}
|
|
2475
|
+
};
|
|
2476
|
+
mapImpl.sendMap = (hook, path, func) => {
|
|
2477
|
+
let builder = new MapBuilder(hook, path);
|
|
2478
|
+
let result;
|
|
2479
|
+
try {
|
|
2480
|
+
result = RpcPayload.fromAppReturn(withCallInterceptor(builder.pushCall.bind(builder), () => {
|
|
2481
|
+
return func(new RpcPromise(builder.makeInput(), []));
|
|
2482
|
+
}));
|
|
2483
|
+
} finally {
|
|
2484
|
+
builder.unregister();
|
|
2485
|
+
}
|
|
2486
|
+
if (result instanceof Promise) {
|
|
2487
|
+
result.catch((err) => {
|
|
2488
|
+
});
|
|
2489
|
+
throw new Error("RPC map() callbacks cannot be async.");
|
|
2490
|
+
}
|
|
2491
|
+
return new RpcPromise(builder.makeOutput(result), []);
|
|
2492
|
+
};
|
|
2493
|
+
function throwMapperBuilderUseError() {
|
|
2494
|
+
throw new Error(
|
|
2495
|
+
"Attempted to use an abstract placeholder from a mapper function. Please make sure your map function has no side effects."
|
|
2496
|
+
);
|
|
2497
|
+
}
|
|
2498
|
+
var MapVariableHook = class extends StubHook {
|
|
2499
|
+
constructor(mapper, idx) {
|
|
2500
|
+
super();
|
|
2501
|
+
this.mapper = mapper;
|
|
2502
|
+
this.idx = idx;
|
|
2503
|
+
}
|
|
2504
|
+
// We don't have anything we actually need to dispose, so dup() can just return the same hook.
|
|
2505
|
+
dup() {
|
|
2506
|
+
return this;
|
|
2507
|
+
}
|
|
2508
|
+
dispose() {
|
|
2509
|
+
}
|
|
2510
|
+
get(path) {
|
|
2511
|
+
if (path.length == 0) {
|
|
2512
|
+
return this;
|
|
2513
|
+
} else if (currentMapBuilder) {
|
|
2514
|
+
return currentMapBuilder.pushGet(this, path);
|
|
2515
|
+
} else {
|
|
2516
|
+
throwMapperBuilderUseError();
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
// Other methods should never be called.
|
|
2520
|
+
call(path, args) {
|
|
2521
|
+
throwMapperBuilderUseError();
|
|
2522
|
+
}
|
|
2523
|
+
map(path, captures, instructions) {
|
|
2524
|
+
throwMapperBuilderUseError();
|
|
2525
|
+
}
|
|
2526
|
+
pull() {
|
|
2527
|
+
throwMapperBuilderUseError();
|
|
2528
|
+
}
|
|
2529
|
+
ignoreUnhandledRejections() {
|
|
2530
|
+
}
|
|
2531
|
+
onBroken(callback) {
|
|
2532
|
+
throwMapperBuilderUseError();
|
|
2533
|
+
}
|
|
2534
|
+
};
|
|
2535
|
+
var MapApplicator = class {
|
|
2536
|
+
constructor(captures, input) {
|
|
2537
|
+
this.captures = captures;
|
|
2538
|
+
this.variables = [input];
|
|
2539
|
+
}
|
|
2540
|
+
variables;
|
|
2541
|
+
dispose() {
|
|
2542
|
+
for (let variable of this.variables) {
|
|
2543
|
+
variable.dispose();
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
apply(instructions) {
|
|
2547
|
+
try {
|
|
2548
|
+
if (instructions.length < 1) {
|
|
2549
|
+
throw new Error("Invalid empty mapper function.");
|
|
2550
|
+
}
|
|
2551
|
+
for (let instruction of instructions.slice(0, -1)) {
|
|
2552
|
+
let payload = new Evaluator(this).evaluateCopy(instruction);
|
|
2553
|
+
if (payload.value instanceof RpcStub) {
|
|
2554
|
+
let hook = unwrapStubNoProperties(payload.value);
|
|
2555
|
+
if (hook) {
|
|
2556
|
+
this.variables.push(hook);
|
|
2557
|
+
continue;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
this.variables.push(new PayloadStubHook(payload));
|
|
2561
|
+
}
|
|
2562
|
+
return new Evaluator(this).evaluateCopy(instructions[instructions.length - 1]);
|
|
2563
|
+
} finally {
|
|
2564
|
+
for (let variable of this.variables) {
|
|
2565
|
+
variable.dispose();
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2569
|
+
importStub(idx) {
|
|
2570
|
+
throw new Error("A mapper function cannot refer to exports.");
|
|
2571
|
+
}
|
|
2572
|
+
importPromise(idx) {
|
|
2573
|
+
return this.importStub(idx);
|
|
2574
|
+
}
|
|
2575
|
+
getExport(idx) {
|
|
2576
|
+
if (idx < 0) {
|
|
2577
|
+
return this.captures[-idx - 1];
|
|
2578
|
+
} else {
|
|
2579
|
+
return this.variables[idx];
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
};
|
|
2583
|
+
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2584
|
+
let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
|
|
2585
|
+
let mapper = new MapApplicator(captures, inputHook);
|
|
2586
|
+
try {
|
|
2587
|
+
return mapper.apply(instructions);
|
|
2588
|
+
} finally {
|
|
2589
|
+
mapper.dispose();
|
|
2590
|
+
}
|
|
2591
|
+
}
|
|
2592
|
+
mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
|
|
2593
|
+
try {
|
|
2594
|
+
let result;
|
|
2595
|
+
if (input instanceof RpcPromise) {
|
|
2596
|
+
throw new Error("applyMap() can't be called on RpcPromise");
|
|
2597
|
+
} else if (input instanceof Array) {
|
|
2598
|
+
let payloads = [];
|
|
2599
|
+
try {
|
|
2600
|
+
for (let elem of input) {
|
|
2601
|
+
payloads.push(applyMapToElement(elem, input, owner, captures, instructions));
|
|
2602
|
+
}
|
|
2603
|
+
} catch (err) {
|
|
2604
|
+
for (let payload of payloads) {
|
|
2605
|
+
payload.dispose();
|
|
2606
|
+
}
|
|
2607
|
+
throw err;
|
|
2608
|
+
}
|
|
2609
|
+
result = RpcPayload.fromArray(payloads);
|
|
2610
|
+
} else if (input === null || input === void 0) {
|
|
2611
|
+
result = RpcPayload.fromAppReturn(input);
|
|
2612
|
+
} else {
|
|
2613
|
+
result = applyMapToElement(input, parent, owner, captures, instructions);
|
|
2614
|
+
}
|
|
2615
|
+
return new PayloadStubHook(result);
|
|
2616
|
+
} finally {
|
|
2617
|
+
for (let cap of captures) {
|
|
2618
|
+
cap.dispose();
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
};
|
|
2622
|
+
var RpcStub2 = RpcStub;
|
|
2623
|
+
var RpcPromise2 = RpcPromise;
|
|
2624
|
+
var RpcSession2 = RpcSession;
|
|
2625
|
+
var RpcTarget4 = RpcTarget;
|
|
2626
|
+
var newWebSocketRpcSession2 = newWebSocketRpcSession;
|
|
2627
|
+
var newHttpBatchRpcSession2 = newHttpBatchRpcSession;
|
|
2628
|
+
var newMessagePortRpcSession2 = newMessagePortRpcSession;
|
|
2629
|
+
async function newWorkersRpcResponse(request, localMain) {
|
|
2630
|
+
if (request.method === "POST") {
|
|
2631
|
+
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
2632
|
+
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2633
|
+
return response;
|
|
2634
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") {
|
|
2635
|
+
return newWorkersWebSocketRpcResponse(request, localMain);
|
|
2636
|
+
} else {
|
|
2637
|
+
return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
|
|
2641
|
+
exports.RpcPromise = RpcPromise2;
|
|
2642
|
+
exports.RpcSession = RpcSession2;
|
|
2643
|
+
exports.RpcStub = RpcStub2;
|
|
2644
|
+
exports.RpcTarget = RpcTarget4;
|
|
2645
|
+
exports.deserialize = deserialize;
|
|
2646
|
+
exports.newHttpBatchRpcResponse = newHttpBatchRpcResponse;
|
|
2647
|
+
exports.newHttpBatchRpcSession = newHttpBatchRpcSession2;
|
|
2648
|
+
exports.newMessagePortRpcSession = newMessagePortRpcSession2;
|
|
2649
|
+
exports.newWebSocketRpcSession = newWebSocketRpcSession2;
|
|
2650
|
+
exports.newWorkersRpcResponse = newWorkersRpcResponse;
|
|
2651
|
+
exports.newWorkersWebSocketRpcResponse = newWorkersWebSocketRpcResponse;
|
|
2652
|
+
exports.nodeHttpBatchRpcResponse = nodeHttpBatchRpcResponse;
|
|
2653
|
+
exports.serialize = serialize;
|
|
2654
|
+
//# sourceMappingURL=index-workers.cjs.map
|
|
2655
|
+
//# sourceMappingURL=index-workers.cjs.map
|