|
| 1 | +import {deserialize, serialize} from 'v8' |
| 2 | +import {ChildProcess} from 'child_process' |
| 3 | +import InterceptedArray from './array.js' |
| 4 | +import IPOSMessaging from './messaging.js' |
| 5 | + |
| 6 | +export default class IPOS { |
| 7 | + private fields: Map<string, object> |
| 8 | + private fieldsReverseMap: Map<object, string> |
| 9 | + private processes: Set<ChildProcess> |
| 10 | + private processMessagingMap: Map<ChildProcess, IPOSMessaging> |
| 11 | + private readonly proxy |
| 12 | + private messaging?: IPOSMessaging; |
| 13 | + |
| 14 | + static new(): IPOS { |
| 15 | + return new IPOS() |
| 16 | + } |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.fields = new Map() |
| 20 | + this.fieldsReverseMap = new Map() |
| 21 | + this.processes = new Set() |
| 22 | + this.processMessagingMap = new Map() |
| 23 | + |
| 24 | + // proxy makes all "target.fields" available as "actual" fields |
| 25 | + this.proxy = new Proxy(this, { |
| 26 | + get(target, name: string) { |
| 27 | + if (Reflect.has(target, name)) { |
| 28 | + return Reflect.get(target, name) |
| 29 | + } else if (target.fields.has(name)) { |
| 30 | + return target.fields.get(name) |
| 31 | + } |
| 32 | + } |
| 33 | + }) |
| 34 | + // was called on child process |
| 35 | + if (process.send) { |
| 36 | + this.messaging?.listenForType('sync', message => { |
| 37 | + console.log(message) |
| 38 | + if (message.fields) |
| 39 | + this.fields = deserialize(message.fields) |
| 40 | + }) |
| 41 | + |
| 42 | + // register with parent process |
| 43 | + this.messaging = new IPOSMessaging(process) |
| 44 | + this.messaging.send('register') |
| 45 | + } |
| 46 | + return this.proxy |
| 47 | + } |
| 48 | + |
| 49 | + public create(key: string, value: object): void { |
| 50 | + if (Array.isArray(value)) { |
| 51 | + value = new InterceptedArray((object, method, ...args) => |
| 52 | + this.sendMethodCall(object, method, ...args) |
| 53 | + ) |
| 54 | + } |
| 55 | + /*Object.getOwnPropertyNames(Object.getPrototypeOf(value)) |
| 56 | + .filter(methodName => !(methodName.startsWith('__') && methodName.endsWith('__'))) |
| 57 | + .forEach(methodName => { |
| 58 | + if (!value[methodName]) return |
| 59 | + const method = value[methodName] |
| 60 | + value[methodName] = function (...args: any) { |
| 61 | + // interception |
| 62 | + console.log(methodName) |
| 63 | + method.call(value, ...args) |
| 64 | + } |
| 65 | + }) |
| 66 | + */ |
| 67 | + this.fields.set(key, value) |
| 68 | + this.fieldsReverseMap.set(value, key) |
| 69 | + // todo: send update message |
| 70 | + } |
| 71 | + |
| 72 | + public delete(key: string): boolean { |
| 73 | + return this.fields.delete(key) |
| 74 | + // todo: send update message |
| 75 | + } |
| 76 | + |
| 77 | + public addProcess(process: ChildProcess) { |
| 78 | + if (!process.send) |
| 79 | + throw new Error(`Process must have an ipc channel. Activate by passing "stdio: [<stdin>, <stdout>, <stderr>, 'ipc']" as an option.`) |
| 80 | + const messaging = new IPOSMessaging(process) |
| 81 | + |
| 82 | + let registered = false |
| 83 | + messaging.listenForType('register', () => { |
| 84 | + if (registered) return |
| 85 | + registered = true |
| 86 | + |
| 87 | + this.processes.add(process) |
| 88 | + this.processMessagingMap.set(process, messaging) |
| 89 | + this.syncProcess(process) |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + private syncProcess(process: ChildProcess) { |
| 94 | + console.log('sending sync') |
| 95 | + this.processMessagingMap.get(process)?.send('sync', { |
| 96 | + field: serialize(this.fields) |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + private sendMethodCall(object: object, method: string, ...args: any) { |
| 101 | + this.processMessagingMap.forEach(processMessaging => { |
| 102 | + processMessaging.send('update', { |
| 103 | + do: method, |
| 104 | + on: this.fieldsReverseMap.get(object), |
| 105 | + with: serialize(args) |
| 106 | + }) |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + // serializes types, that "JSON.stringify()" doesn't properly handle |
| 111 | + /*private static serialize(value: any): any | void { |
| 112 | + // todo: handle other builtins |
| 113 | + if (['string', 'number'].includes(typeof value)) { |
| 114 | + return value |
| 115 | + } else if (typeof value === 'function') { |
| 116 | + return value.toString() |
| 117 | + } else if (Array.isArray(value)) { |
| 118 | + return value.map(serialize) |
| 119 | + } else if (value.constructor === {}.constructor) { |
| 120 | + return Object.fromEntries( |
| 121 | + Array.from( |
| 122 | + Object.entries(value) |
| 123 | + .map(([key, value]) => |
| 124 | + [key, this.serialize(value)] |
| 125 | + ) |
| 126 | + ) |
| 127 | + ) |
| 128 | + } else { |
| 129 | + if (!value.stringify && !value.serialize) |
| 130 | + throw new Error( |
| 131 | + `Class: \`${value.constructor.name}\` must have methods to serialize and deserialize objects. (\`.stringify()\`, \`.serialize()\`)` |
| 132 | + ) |
| 133 | + // return value.toString() |
| 134 | + } |
| 135 | + }*/ |
| 136 | +} |
0 commit comments