-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobj.js
654 lines (588 loc) · 18.9 KB
/
obj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import { assert } from "./util.js";
function getClassName(obj) {
let proto = Object.getPrototypeOf(obj);
return (proto && (proto[Symbol.toStringTag] || proto.constructor.name)) || "Object";
}
function isPrivateProperty(propName) {
return propName.startsWith("_") || propName == "constructor";
}
// This is only set as a function on classes that are known to be observers
function subscribe(subscriber) {
subscriber(this);
if (!this._subscribers) {
this._subscribers = new Set();
}
this._subscribers.add(subscriber);
return () => this._subscribers.remove(subscriber);
}
class RemoteClass {
constructor(className) {
this.className = className;
}
_notifySubscribers() {
if (this._subscribers) {
for (let subscriber of this._subscribers) {
try {
subscriber(this);
} catch (ex) {
console.error(ex);
this._subscribers.delete(subscriber);
}
}
}
}
}
export default class BaseProtocol {
callRemote(method, payload) {
throw new Error("Implement this");
}
registerIncomingCall(method, listener) {
throw new Error("Implement this");
}
/**
* Called by the wire protocol implementation,
* before calling any of the other functions.
*
* @param jpcProtocol {JPCProtocol} Wire protocol implementation
*/
start(startObject) {
this.registerIncomingCall("class", this.getClassDescription.bind(this));
this.registerIncomingCall("start", this.mapOutgoingObjects.bind(this, startObject));
this.registerIncomingCall("new", this.newObjListener.bind(this));
this.registerIncomingCall("call", this.callListener.bind(this));
this.registerIncomingCall("iter", this.iterListener.bind(this));
this.registerIncomingCall("func", this.funcListener.bind(this));
this.registerIncomingCall("get", this.getterListener.bind(this));
this.registerIncomingCall("set", this.setterListener.bind(this));
this.registerIncomingCall("observe", this.observeListener.bind(this));
this.registerIncomingCall("del", payload => {
this.deleteLocalObject(payload.idRemote);
});
if (globalThis && "FinalizationRegistry" in globalThis) {
this._localObjectRegistry = new FinalizationRegistry(id => {
this._localIDsToObjects.delete(id);
});
this._remoteObjectRegistry = new FinalizationRegistry(id => {
this._remoteObjects.delete(id);
this.callRemote("del", {
idRemote: id,
}).catch(console.error);
});
// TODO Free everything when client disconnects
// See <https://github.com/tc39/proposal-weakrefs/blob/master/reference.md#notes-on-cleanup-callbacks>
// and <https://github.com/tc39/proposal-weakrefs/issues/125>
} else { // not supported, use dummy
console.warn("FinalizationRegistry is not supported. This will leak everything. Please update node.js.");
this._localObjectRegistry = {
register: () => {},
};
this._remoteObjectRegistry = {
register: () => {},
};
}
}
///////////////////////////////////////////
// Stub object
// A local JS object representing a remote object
/**
* { Map className {string} -> prototype object {obj} }
*/
_remoteClasses = new Map();
/**
* Generates a stub class
*
* @param classDescrJSON {JSON} Describes the remote class, see PROTOCOL.md
* @param parent {RemoteClass?} The class this class inherits from
*/
registerRemoteClass(classDescrJSON, parent) {
let existing = this._remoteClasses.get(classDescrJSON.className);
if (existing) {
return existing;
}
let proto;
if (parent) {
proto = Object.create(parent);
proto.className = classDescrJSON.className;
} else {
proto = new RemoteClass(classDescrJSON.className);
}
if (classDescrJSON.iterator) {
proto[Symbol.asyncIterator] = this.makeIterator(classDescrJSON.iterator);
}
if (classDescrJSON.observable) {
proto.subscribe = subscribe;
}
for (let func of classDescrJSON.functions) {
proto[func.name] = this.makeFunction(func.name);
}
for (let getter of classDescrJSON.getters) {
Object.defineProperty(proto, getter.name, {
enumerable: true,
get: this.makeGetter(getter.name),
});
if (getter.hasSetter) {
let setterName = "set" + getter.name[0].toUpperCase() + getter.name.substr(1);
proto[setterName] = this.makeSetter(getter.name);
}
}
proto.newRemote = this.makeNewObj(classDescrJSON.className); // TODO static function
this._remoteClasses.set(classDescrJSON.className, proto);
return proto;
}
/**
* Generates a stub object instance
*
* @param objDescrJSON {JSON} Describes the remote object, see PROTOCOL.md
* @returns {StubObj}
*/
async makeStub(objDescrJSON) {
let proto = this._remoteClasses.get(objDescrJSON.className);
if (!proto) {
let classDescrJSON = await this.callRemote("class", {
className: objDescrJSON.className,
});
for (let descr of classDescrJSON) {
proto = this.registerRemoteClass(descr, proto);
}
}
let stub = Object.create(proto);
stub.id = objDescrJSON.idLocal;
this.addRemoteObject(objDescrJSON.idLocal, stub);
await this.updateObjectProperties(stub, objDescrJSON.properties);
return stub;
}
async updateObjectProperties(obj, properties) {
for (let propName in properties) {
Object.defineProperty(obj, propName, {
configurable: true,
enumerable: true,
writable: false,
value: await this.mapIncomingObjects(properties[propName]),
});
}
}
makeCallable(id) {
return async (...args) => {
return await this.mapIncomingObjects(await this.callRemote("call", {
obj: id,
args: this.mapOutgoingObjects(args),
}));
}
}
makeIterator(symbolName) {
let self = this;
return async function*() {
let remote = await self.mapIncomingObjects(await self.callRemote("iter", {
obj: this.id,
symbol: symbolName,
}));
// This object will probably be an iterable iterator,
// but we don't want to remote that call.
Object.getPrototypeOf(remote)[Symbol.asyncIterator] = function() { return this; };
for await (let value of remote) {
yield value;
}
}
}
makeFunction(functionName) {
let self = this;
return async function(...args) {
// this == stub object
return await self.mapIncomingObjects(await self.callRemote("func", {
obj: this.id,
name: functionName,
args: self.mapOutgoingObjects(args),
}));
}
}
makeGetter(propName) {
let self = this;
return async function() {
// this == stub object
return await self.mapIncomingObjects(await self.callRemote("get", {
obj: this.id,
name: propName,
}));
}
}
makeSetter(propName) {
let self = this;
return async function(val) {
// this == stub object
return self.callRemote("set", {
obj: this.id,
name: propName,
value: self.mapOutgoingObjects(val),
});
}
}
makeNewObj(className) {
let self = this;
return async function(...args) {
// this == stub object
return await self.mapIncomingObjects(await self.callRemote("new", {
className: className,
args: self.mapOutgoingObjects(args),
}));
}
}
/**
* @param value {any} string, number, boolean,
* array, JSON obj,
* Object description or Object references, as defined by PROTOCOL.md
* @return {any} same as value, just Object descriptions and Object references
* replaced with `StubObject`s.
*/
async mapIncomingObjects(value) {
if (typeof(value) == "string" ||
typeof(value) == "number" ||
typeof(value) == "boolean" ||
value == null) {
return value;
} else if (Array.isArray(value)) {
return Promise.all(value.map(el => this.mapIncomingObjects(el)));
} else if (typeof(value) == "object") {
let obj = value;
if (obj.idLocal) {
let stub = this.getRemoteObject(obj.idLocal);
if (stub) {
return stub;
}
if (obj.className == "Function") {
let stub = this.makeCallable(obj.idLocal);
this.addRemoteObject(obj.idLocal, stub);
return stub;
}
return await this.makeStub(obj);
} else if (obj.idRemote) {
return this.getLocalObject(obj.idRemote);
} else if (obj.plainObject) {
let plainObject = {};
for (let propName in obj.plainObject) {
plainObject[propName] = await this.mapIncomingObjects(obj.plainObject[propName]);
}
return plainObject;
}
}
}
///////////////////////////////////////////
// Local object
// Passing a normal local JS object to the remote side
async newObjListener(payload) {
assert(typeof(payload.className) == "string", "Need class name");
let classCtor = global[payload.className];
let obj;
let args = payload.args;
if (typeof(args) == "undefined") {
obj = classCtor();
} else {
assert(Array.isArray(args), "Constructor arguments must be an array of values");
args = await this.mapIncomingObjects(args);
obj = classCtor(...args);
}
return this.createObjectDescription(obj, this.getOrCreateIDForLocalObject(obj));
}
async callListener(payload) {
assert(typeof(payload.obj) == "string", "Need object ID");
let func = this.getLocalObject(payload.obj);
let args = await this.mapIncomingObjects(payload.args);
// may throw
let result = func(...args);
if (result instanceof Promise) {
result = await result;
}
return this.mapOutgoingObjects(result);
}
async iterListener(payload) {
let symbol = payload.symbol;
assert(typeof(symbol) == "string", "Need symbol name");
assert(typeof(payload.obj) == "string", "Need object ID");
let obj = this.getLocalObject(payload.obj);
// may throw
let result = obj[Symbol[symbol]]();
return this.mapOutgoingObjects(result);
}
async funcListener(payload) {
let name = payload.name;
assert(typeof(name) == "string", "Need function name");
assert(typeof(payload.obj) == "string", "Need object ID");
let obj = this.getLocalObject(payload.obj);
let args = await this.mapIncomingObjects(payload.args);
// may throw
let result = obj[name](...args);
if (result instanceof Promise) {
result = await result;
}
return this.mapOutgoingObjects(result);
}
async getterListener(payload) {
let name = payload.name;
assert(typeof(name) == "string", "Need property getter name");
assert(typeof(payload.obj) == "string", "Need object ID");
let obj = this.getLocalObject(payload.obj);
// may throw
let value = obj[name];
return this.mapOutgoingObjects(value);
}
async setterListener(payload) {
let name = payload.name;
assert(typeof(name) == "string", "Need property setter name");
assert(typeof(payload.obj) == "string", "Need object ID");
let obj = this.getLocalObject(payload.obj);
let value = await this.mapIncomingObjects(payload.value);
// may throw
obj[payload.name] = value;
}
async observeListener(objDescrJSON) {
/* We get the first notification for the object when we subscribe to it;
this happens when we first encounter the object on the server.
At this point the remote stub does not yet exist on the client,
since we have not yet sent the object description to the client.
The subscription callback doesn't know that because the object already
has a local id by this point, since we need that id in order to establish
the subscription without creating a reference loop. */
let obj = this.getRemoteObject(objDescrJSON.idLocal);
if (obj) {
await this.updateObjectProperties(obj, objDescrJSON.properties);
obj._notifySubscribers();
} else {
await this.makeStub(objDescrJSON); // also fills properties
}
}
/**
* @param value {any} string, number, boolean,
* array, JSON obj, or
* local JS object
* @return {any} same as value, just local objects replaced with
* Object descriptions and Object references, as defined by PROTOCOL.md
*/
mapOutgoingObjects(value) {
if (typeof(value) == "string" ||
typeof(value) == "number" ||
typeof(value) == "boolean" ||
value == null) {
return value;
} else if (Array.isArray(value)) {
return value.map(el => this.mapOutgoingObjects(el));
} else if (typeof(value) == "function") {
let id = this.getOrCreateIDForLocalObject(value);
return {
idLocal: this.getOrCreateIDForLocalObject(value),
className: "Function",
};
} else if (typeof(value) == "object") {
let obj = value;
if (obj instanceof RemoteClass) { // TODO check working?
return { // Object reference for remote object
idRemote: obj.id,
};
}
if (getClassName(obj) == "Object") { // JSON object -- TODO better way to check?
let plainObject = {};
for (let propName in obj) {
plainObject[propName] = this.mapOutgoingObjects(obj[propName]);
}
return {
plainObject: plainObject,
};
}
return this.createObjectDescription(obj, this.getOrCreateIDForLocalObject(obj));
}
}
/**
* Notifies the remote end when an observable updates itself.
*/
observe(id) {
let props = {};
let obj = this.getLocalObject(id);
for (let propName in obj) {
if (isPrivateProperty(propName)) {
continue;
}
let value = obj[propName];
if (typeof value == "function") {
continue;
}
props[propName] = this.mapOutgoingObjects(value);
}
this.callRemote("observe", {
idLocal: id,
className: getClassName(obj),
properties: props,
});
}
/**
* Return an object instance to the remote party that they did not see yet.
*
* @param obj {Object} local object
* @returns {JSON} Object description, see PROTOCOL.md
*/
createObjectDescription(obj, id) {
let className = getClassName(obj);
assert(className, "Could not find class name for local object");
let props = null;
if (typeof obj.subscribe == "function") {
obj.subscribe(this.observe.bind(this, id));
} else {
for (let propName of Object.getOwnPropertyNames(obj)) {
if (isPrivateProperty(propName)) {
continue;
}
let property = Object.getOwnPropertyDescriptor(obj, propName);
if (property.get ||
typeof(property.value) == "function") {
continue;
}
if ( !props) {
props = {};
}
props[propName] = this.mapOutgoingObjects(obj[propName]);
}
}
return {
idLocal: id,
className: className,
properties: props,
};
}
getClassDescription({className}) {
let classDescrJSON = [];
let proto = this._localClasses.get(className);
while (proto) { // should always succeed; loop normally exits by break
let descr = {
className: className,
iterator: null,
observable: false,
functions: [],
getters: [],
properties: [],
};
if (Symbol.asyncInterator in proto) {
descr.iterator = "asyncIterator";
} else if (Symbol.iterator in proto) {
descr.iterator = "iterator";
}
for (let propName of Object.getOwnPropertyNames(proto)) {
if (isPrivateProperty(propName)) {
continue;
}
let property = Object.getOwnPropertyDescriptor(proto, propName);
if (typeof(property.value) == "function") {
if (propName == "subscribe") {
descr.observable = true;
} else {
descr.functions.push({
name: propName,
});
}
continue;
}
if (typeof(property.get) == "function") {
descr.getters.push({
name: propName,
hasSetter: typeof(property.set) == "function",
});
continue;
}
descr.properties.push({
name: propName,
});
}
classDescrJSON.unshift(descr);
className = getClassName(proto);
if (className == "Object") {
break;
}
proto = Object.getPrototypeOf(proto);
}
return classDescrJSON;
}
///////////////////////////////////////////////
// ID to objects
/**
* {Map ID {string} -> remoteObject {WeakRef<StubObject>} }
*/
_remoteObjects = new Map();
/**
* {Map ID {string} -> localObject {obj | WeakRef<obj>} }
*/
_localIDsToObjects = new Map();
/**
* {WeakMap localObj {obj} -> ID {string} }
*/
_localObjectsToIDs = new WeakMap();
/**
* {Map name {string} -> class {Prototype}
*/
_localClasses = new Map();
generateNewObjID() {
let id;
do {
id = (Math.random() * 1e20).toFixed();
} while (this._localIDsToObjects.has(id));
return id;
}
/**
* @param id {string} ID of object refererence
* @returns {StubObj?} remote object
*/
getRemoteObject(id) {
let ref = this._remoteObjects.get(id);
return ref && ref.deref();
}
/**
* @param id {string} ID of object refererence
* @returns {obj} local object
*/
getLocalObject(id) {
let obj = this._localIDsToObjects.get(id);
assert(obj, `Local object with ID ${ id } is unknown here.`);
if (obj instanceof WeakRef) {
obj = obj.deref();
assert(obj, `Local object with ID ${ id } is unknown here.`);
this._localIDsToObjects.set(id, obj);
}
return obj;
}
/**
* @param obj {Object} Local object
* @returns {string} ID
*/
getOrCreateIDForLocalObject(obj) {
let id = this._localObjectsToIDs.get(obj);
if (!id) {
id = this.generateNewObjID();
this._localObjectsToIDs.set(obj, id);
this._localObjectRegistry.register(obj, id);
this._localClasses.set(getClassName(obj), Object.getPrototypeOf(obj));
}
this._localIDsToObjects.set(id, obj);
return id;
}
/**
* @param id {string} ID for remote object, as set by the remote side
* @param obj {StubObj} Remote object
*/
addRemoteObject(id, obj) {
let existing = this.getRemoteObject(id);
assert( !existing, `Remote object ID ${ id } already exists.`);
this._remoteObjects.set(id, new WeakRef(obj));
this._remoteObjectRegistry.register(obj, id);
}
/**
* Remote side says that it no longer needs this object.
* Drop the reference to it.
* @param id {string} ID of object refererence
*/
deleteLocalObject(id) {
let obj = this._localIDsToObjects.get(id);
assert(obj, `Local object with ID ${ id } is unknown here.`);
if (obj instanceof WeakRef) {
return;
}
// Keep weak references in case the object gets promoted again
this._localIDsToObjects.set(id, new WeakRef(obj));
}
_localObjectRegistry = null;
_remoteObjectRegistry = null;
}