-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import module from 'module'; | ||
|
||
const require = module.createRequire(import.meta.url); | ||
require("../dist/scrypted-main.js"); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { getDenoPath } from '@scrypted/deno'; | ||
import child_process from 'child_process'; | ||
import path from 'path'; | ||
import { RpcMessage, RpcPeer } from "../../rpc"; | ||
import { createRpcDuplexSerializer } from '../../rpc-serializer'; | ||
import { ChildProcessWorker } from "./child-process-worker"; | ||
import { RuntimeWorkerOptions } from "./runtime-worker"; | ||
|
||
export class DenoWorker extends ChildProcessWorker { | ||
serializer: ReturnType<typeof createRpcDuplexSerializer>; | ||
|
||
constructor(mainFilename: string, pluginId: string, options: RuntimeWorkerOptions) { | ||
super(pluginId, options); | ||
|
||
const { env, pluginDebug } = options; | ||
|
||
const execArgv: string[] = []; | ||
if (pluginDebug) { | ||
execArgv.push(`--inspect=0.0.0.0:${pluginDebug.inspectPort}`); | ||
} | ||
|
||
const args = [ | ||
'--unstable-byonm', '--unstable-bare-node-builtins', '--unstable-sloppy-imports', | ||
'run', | ||
...execArgv, | ||
'--allow-all', | ||
path.join(__dirname, '../../../deno', 'deno-plugin-remote.ts'), | ||
// TODO: send this across. | ||
// mainFilename.replace('dist', 'src').replace('.js', '.ts'), | ||
'child', this.pluginId | ||
]; | ||
this.worker = child_process.spawn(getDenoPath(), args, { | ||
// stdio: ['pipe', 'pipe', 'pipe', 'pipe', 'pipe'], | ||
stdio: ['pipe', 'pipe', 'pipe', 'ipc'], | ||
env: Object.assign({ | ||
SCRYPTED_MAIN_FILENAME: mainFilename, | ||
}, process.env, env), | ||
serialization: 'json', | ||
// execArgv, | ||
}); | ||
|
||
this.worker.stderr.on('data', (data) => { | ||
console.error(`stderr: ${data}`); | ||
}); | ||
this.worker.stdout.on('data', (data) => { | ||
console.log(`stdout: ${data}`); | ||
}); | ||
|
||
this.setupWorker(); | ||
} | ||
|
||
kill(): void { | ||
|
||
} | ||
|
||
setupRpcPeer(peer: RpcPeer): void { | ||
this.worker.on('message', (message, sendHandle) => { | ||
if ((message as any).type && sendHandle) { | ||
peer.handleMessage(message as any, { | ||
sendHandle, | ||
}); | ||
} | ||
else if (sendHandle) { | ||
this.emit('rpc', message, sendHandle); | ||
} | ||
else { | ||
peer.handleMessage(message as any); | ||
} | ||
}); | ||
peer.transportSafeArgumentTypes.add(Buffer.name); | ||
peer.transportSafeArgumentTypes.add(Uint8Array.name); | ||
} | ||
|
||
send(message: RpcMessage, reject?: (e: Error) => void, serializationContext?: any): void { | ||
try { | ||
if (!this.worker) | ||
throw new Error('fork worker has been killed'); | ||
this.worker.send(message, serializationContext?.sendHandle, e => { | ||
if (e && reject) | ||
reject(e); | ||
}); | ||
} | ||
catch (e) { | ||
reject?.(e); | ||
} | ||
} | ||
|
||
get pid() { | ||
return this.worker?.pid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import start from './scrypted-main-exports'; | ||
|
||
start(__filename); | ||
if (process.versions.deno) | ||
start(process.env.SCRYPTED_MAIN_FILENAME); | ||
else | ||
start(__filename); |