Skip to content

Commit

Permalink
server: deno runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Aug 3, 2024
1 parent daddf10 commit 5bb8ea0
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
4 changes: 4 additions & 0 deletions server/deno/deno-plugin-remote.ts
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");
10 changes: 10 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.11",
"@scrypted/deno": "^1.45.5",
"@scrypted/ffmpeg-static": "^6.1.0-build1",
"@scrypted/node-pty": "^1.0.18",
"@scrypted/types": "^0.3.44",
Expand Down
91 changes: 91 additions & 0 deletions server/src/plugin/runtime/deno-worker.ts
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;
}
}
2 changes: 2 additions & 0 deletions server/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { getNpmPackageInfo, PluginComponent } from './services/plugin';
import { ServiceControl } from './services/service-control';
import { UsersService } from './services/users';
import { getState, ScryptedStateManager, setState } from './state';
import { DenoWorker } from './plugin/runtime/deno-worker';

interface DeviceProxyPair {
handler: PluginDeviceProxyHandler;
Expand Down Expand Up @@ -102,6 +103,7 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
this.pluginHosts.set('custom', (_, pluginId, options, runtime) => new CustomRuntimeWorker(pluginId, options, runtime));
this.pluginHosts.set('python', (_, pluginId, options) => new PythonRuntimeWorker(pluginId, options));
this.pluginHosts.set('node', (mainFilename, pluginId, options) => new NodeForkWorker(mainFilename, pluginId, options));
this.pluginHosts.set('deno', (mainFilename, pluginId, options) => new DenoWorker(mainFilename, pluginId, options));

app.disable('x-powered-by');

Expand Down
5 changes: 4 additions & 1 deletion server/src/scrypted-main.ts
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);

0 comments on commit 5bb8ea0

Please sign in to comment.