server: expose worker thread

This commit is contained in:
Koushik Dutta
2022-08-11 09:28:35 -07:00
parent 4c61aeb3c6
commit 8d5376b21f
7 changed files with 39 additions and 36 deletions

View File

@@ -1640,8 +1640,8 @@ export interface ScryptedStatic {
pluginHostAPI: any;
fork?: () => {
result: Promise<any>;
fork?<T>(): {
result: Promise<T>;
worker: {
terminate(): Promise<number>;
};

View File

@@ -1,5 +1,5 @@
/// <reference types="node" />
export declare const TYPES_VERSION = "0.0.63";
export declare const TYPES_VERSION = "0.0.64";
export interface DeviceState {
id?: string;
info?: DeviceInformation;
@@ -1663,8 +1663,8 @@ export interface ScryptedStatic {
mediaManager: MediaManager;
systemManager: SystemManager;
pluginHostAPI: any;
fork?: () => {
result: Promise<any>;
fork?<T>(): {
result: Promise<T>;
worker: {
terminate(): Promise<number>;
};

View File

@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScryptedMimeTypes = exports.ScryptedInterface = exports.MediaPlayerState = exports.SecuritySystemObstruction = exports.SecuritySystemMode = exports.AirQuality = exports.LockState = exports.ThermostatMode = exports.TemperatureUnit = exports.FanMode = exports.HumidityMode = exports.ScryptedDeviceType = exports.ScryptedInterfaceDescriptors = exports.ScryptedInterfaceProperty = exports.DeviceBase = exports.TYPES_VERSION = void 0;
exports.TYPES_VERSION = "0.0.63";
exports.TYPES_VERSION = "0.0.64";
class DeviceBase {
}
exports.DeviceBase = DeviceBase;

View File

@@ -1,5 +1,5 @@
export const TYPES_VERSION = "0.0.63";
export const TYPES_VERSION = "0.0.64";
export interface DeviceState {
@@ -2416,8 +2416,8 @@ export interface ScryptedStatic {
pluginHostAPI: any;
fork?: () => {
result: Promise<any>;
fork?<T>(): {
result: Promise<T>;
worker: {
terminate(): Promise<number>;
};

View File

@@ -10,7 +10,7 @@
"license": "ISC",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.8",
"@scrypted/types": "^0.0.56",
"@scrypted/types": "^0.0.64",
"adm-zip": "^0.5.3",
"axios": "^0.21.1",
"body-parser": "^1.19.0",
@@ -134,9 +134,9 @@
}
},
"node_modules/@scrypted/types": {
"version": "0.0.56",
"resolved": "https://registry.npmjs.org/@scrypted/types/-/types-0.0.56.tgz",
"integrity": "sha512-32txJCj3Dgx0NowtYNZBLQeJ1b5dK6c2wDrX4Cij11hGDaPThsc66roFknEmF1IbmQRQGKVA8acKfQPWb4yCfA=="
"version": "0.0.64",
"resolved": "https://registry.npmjs.org/@scrypted/types/-/types-0.0.64.tgz",
"integrity": "sha512-j0/VYkUkQVpxvpPQl+cEnNqVwiWmjyGNRcy06Unmq8SWwrea/+wYdM92nnVdz0okrAijCgYutzwfCqfMlyNsyA=="
},
"node_modules/@socket.io/component-emitter": {
"version": "3.1.0",
@@ -3085,9 +3085,9 @@
}
},
"@scrypted/types": {
"version": "0.0.56",
"resolved": "https://registry.npmjs.org/@scrypted/types/-/types-0.0.56.tgz",
"integrity": "sha512-32txJCj3Dgx0NowtYNZBLQeJ1b5dK6c2wDrX4Cij11hGDaPThsc66roFknEmF1IbmQRQGKVA8acKfQPWb4yCfA=="
"version": "0.0.64",
"resolved": "https://registry.npmjs.org/@scrypted/types/-/types-0.0.64.tgz",
"integrity": "sha512-j0/VYkUkQVpxvpPQl+cEnNqVwiWmjyGNRcy06Unmq8SWwrea/+wYdM92nnVdz0okrAijCgYutzwfCqfMlyNsyA=="
},
"@socket.io/component-emitter": {
"version": "3.1.0",

View File

@@ -4,7 +4,7 @@
"description": "",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.8",
"@scrypted/types": "^0.0.56",
"@scrypted/types": "^0.0.64",
"adm-zip": "^0.5.3",
"axios": "^0.21.1",
"body-parser": "^1.19.0",

View File

@@ -316,27 +316,32 @@ export function startPluginRemote(pluginId: string, peerSend: (message: RpcMessa
pluginReader = undefined;
const script = main.toString();
scrypted.fork = async () => {
scrypted.fork = () => {
const ntw = new NodeThreadWorker(pluginId, {
env: process.env,
pluginDebug: undefined,
});
const threadPeer = new RpcPeer('main', 'thread', (message, reject) => ntw.send(message, reject));
threadPeer.params.updateStats = (stats: any) => {
// todo: merge.
// this.stats = stats;
return {
worker: ntw.worker,
result: (async() => {
const threadPeer = new RpcPeer('main', 'thread', (message, reject) => ntw.send(message, reject));
threadPeer.params.updateStats = (stats: any) => {
// todo: merge.
// this.stats = stats;
}
ntw.setupRpcPeer(threadPeer);
const remote = await setupPluginRemote(threadPeer, api, pluginId, () => systemManager.getSystemState());
for (const [nativeId, dmd] of deviceManager.nativeIds.entries()) {
await remote.setNativeId(nativeId, dmd.id, dmd.storage);
}
const forkOptions = Object.assign({}, zipOptions);
forkOptions.fork = true;
return remote.loadZip(packageJson, zipData, forkOptions)
})(),
}
ntw.setupRpcPeer(threadPeer);
const remote = await setupPluginRemote(threadPeer, api, pluginId, () => systemManager.getSystemState());
for (const [nativeId, dmd] of deviceManager.nativeIds.entries()) {
await remote.setNativeId(nativeId, dmd.id, dmd.storage);
}
const forkOptions = Object.assign({}, zipOptions);
forkOptions.fork = true;
return remote.loadZip(packageJson, zipData, forkOptions)
}
try {
@@ -345,9 +350,7 @@ export function startPluginRemote(pluginId: string, peerSend: (message: RpcMessa
if (zipOptions?.fork) {
const fork = exports.fork;
const ret = await fork();
ret[RpcPeer.PROPERTY_JSON_DISABLE_SERIALIZATION] = true;
return ret;
return fork();
}
let pluginInstance = exports.default;