feat(plugin-infra): support worker thread in server side (#3462)

This commit is contained in:
Alex Yang
2023-07-29 13:57:23 -07:00
committed by GitHub
parent 77dab70ff7
commit ac59e28fcd
13 changed files with 152 additions and 70 deletions

View File

@@ -0,0 +1,45 @@
import { join, resolve } from 'node:path';
import { parentPort } from 'node:worker_threads';
import type { ServerContext } from '@toeverything/plugin-infra/server';
import { AsyncCall } from 'async-call-rpc';
import { MessageEventChannel } from '../shared/utils';
if (!parentPort) {
throw new Error('parentPort is null');
}
const commandProxy: Record<string, (...args: any[]) => Promise<any>> = {};
parentPort.start();
const mainThread = AsyncCall<{
log: (...args: any[]) => Promise<void>;
}>(commandProxy, {
channel: new MessageEventChannel(parentPort),
});
globalThis.console.log = mainThread.log;
globalThis.console.error = mainThread.log;
globalThis.console.info = mainThread.log;
globalThis.console.debug = mainThread.log;
globalThis.console.warn = mainThread.log;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const bookmarkPluginModule = require(join(
process.env.PLUGIN_DIR ?? resolve(__dirname, './plugins'),
'./bookmark/index.js'
));
const serverContext: ServerContext = {
registerCommand: (command, fn) => {
console.log('register command', command);
commandProxy[command] = fn;
},
unregisterCommand: command => {
console.log('unregister command', command);
delete commandProxy[command];
},
};
bookmarkPluginModule.entry(serverContext);