server/sdk: wip support for alternative fork main, fork names. add initial workerData message channel.

This commit is contained in:
Koushik Dutta
2024-07-30 22:48:52 -07:00
parent 2013830677
commit d0b46c35a9
12 changed files with 67 additions and 53 deletions

View File

@@ -161,6 +161,7 @@ export interface PluginRemoteLoadZipOptions {
debug?: boolean;
zipHash: string;
fork?: boolean;
main?: string;
clusterId: string;
clusterSecret: string;

View File

@@ -93,6 +93,11 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
throw new Error(`unknown service ${name}`);
},
async onLoadZip(scrypted: ScryptedStatic, params: any, packageJson: any, getZip: () => Promise<Buffer>, zipOptions: PluginRemoteLoadZipOptions) {
const mainFile = zipOptions?.main || 'main';
const mainNodejs = `${mainFile}.nodejs.js`;
const pluginMainNodeJs = `/plugin/${mainNodejs}`;
const pluginIdMainNodeJs = `/${pluginId}/${mainNodejs}`;
const { clusterId, clusterSecret, zipHash } = zipOptions;
const { zipFile, unzippedPath } = await prepareZip(getPluginVolume(pluginId), zipHash, getZip);
@@ -266,7 +271,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
// params.window = window;
params.exports = exports;
const entry = pluginReader('main.nodejs.js.map')
const entry = pluginReader(`${mainNodejs}.map`)
const map = entry?.toString();
// plugins may install their own sourcemap support during startup, so
@@ -287,11 +292,11 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
installSourceMapSupport({
environment: 'node',
retrieveSourceMap(source) {
if (source === '/plugin/main.nodejs.js' || source === `/${pluginId}/main.nodejs.js`) {
if (source === pluginMainNodeJs || source === pluginIdMainNodeJs) {
if (!map)
return null;
return {
url: '/plugin/main.nodejs.js',
url: pluginMainNodeJs,
map,
}
}
@@ -314,7 +319,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
await pong(time);
};
const main = pluginReader('main.nodejs.js');
const main = pluginReader(mainNodejs);
const script = main.toString();
scrypted.connect = (socket, options) => {
@@ -323,7 +328,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
const pluginRemoteAPI: PluginRemote = scrypted.pluginRemoteAPI;
scrypted.fork = () => {
scrypted.fork = (options) => {
const ntw = new NodeThreadWorker(mainFilename, pluginId, {
packageJson,
env: process.env,
@@ -331,6 +336,8 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
zipFile,
unzippedPath,
zipHash,
}, {
name: options?.name,
});
const result = (async () => {
@@ -389,7 +396,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
}
try {
const filename = zipOptions?.debug ? '/plugin/main.nodejs.js' : `/${pluginId}/main.nodejs.js`;
const filename = zipOptions?.debug ? pluginMainNodeJs : pluginIdMainNodeJs;
evalLocal(peer, script, filename, params);
if (zipOptions?.fork) {

View File

@@ -8,13 +8,20 @@ export class NodeThreadWorker extends EventEmitter implements RuntimeWorker {
worker: worker_threads.Worker;
port: worker_threads.MessagePort;
constructor(mainFilename: string, public pluginId: string, options: RuntimeWorkerOptions) {
constructor(mainFilename: string, public pluginId: string, options: RuntimeWorkerOptions, workerOptions?: worker_threads.WorkerOptions) {
super();
const { env } = options;
const message = new worker_threads.MessageChannel();
const { port1, port2 } = message;
this.worker = new worker_threads.Worker(mainFilename, {
argv: ['child-thread', this.pluginId],
env: Object.assign({}, process.env, env),
workerData: {
port: port1,
},
transferList: [port1],
...workerOptions,
});
this.worker.on('exit', () => {
@@ -27,8 +34,6 @@ export class NodeThreadWorker extends EventEmitter implements RuntimeWorker {
this.emit('error', e);
});
const message = new worker_threads.MessageChannel();
const { port1, port2 } = message;
this.port = port2;
this.port.on('messageerror', e => {
this.emit('error', e);
@@ -36,10 +41,6 @@ export class NodeThreadWorker extends EventEmitter implements RuntimeWorker {
this.port.on('close', () => {
this.emit('error', new Error('port closed'));
});
this.worker.postMessage({
port: port1,
}, [port1]);
}
get pid() {

View File

@@ -12,27 +12,25 @@ function start(mainFilename: string) {
module.paths.push(getPluginNodePath(pluginId));
if (process.argv[2] === 'child-thread') {
worker_threads.parentPort.once('message', message => {
const { port } = message as { port: worker_threads.MessagePort };
const peer = startPluginRemote(mainFilename, pluginId, (message, reject) => {
try {
port.postMessage(v8.serialize(message));
}
catch (e) {
reject?.(e);
}
});
peer.transportSafeArgumentTypes.add(Buffer.name);
peer.transportSafeArgumentTypes.add(Uint8Array.name);
port.on('message', message => peer.handleMessage(v8.deserialize(message)));
port.on('messageerror', e => {
console.error('message error', e);
process.exit(1);
});
port.on('close', () => {
console.error('port closed');
process.exit(1);
});
const { port } = worker_threads.workerData as { port: worker_threads.MessagePort };
const peer = startPluginRemote(mainFilename, pluginId, (message, reject) => {
try {
port.postMessage(v8.serialize(message));
}
catch (e) {
reject?.(e);
}
});
peer.transportSafeArgumentTypes.add(Buffer.name);
peer.transportSafeArgumentTypes.add(Uint8Array.name);
port.on('message', message => peer.handleMessage(v8.deserialize(message)));
port.on('messageerror', e => {
console.error('message error', e);
process.exit(1);
});
port.on('close', () => {
console.error('port closed');
process.exit(1);
});
}
else {