From 0516ca810d935635997bc019c5efc767b187d106 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 19 Mar 2023 18:26:28 -0700 Subject: [PATCH] server: add non invoking entry point --- server/.vscode/launch.json | 2 +- server/bin/scrypted-serve | 3 +- server/package-lock.json | 4 +-- server/package.json | 2 +- server/src/scrypted-main-exports.ts | 54 ++++++++++++++++++++++++++++ server/src/scrypted-main.ts | 55 ++--------------------------- 6 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 server/src/scrypted-main-exports.ts diff --git a/server/.vscode/launch.json b/server/.vscode/launch.json index 59e8c52c7d..a55e264481 100644 --- a/server/.vscode/launch.json +++ b/server/.vscode/launch.json @@ -13,7 +13,7 @@ "/**" ], "preLaunchTask": "npm: build", - "program": "${workspaceFolder}/dist/scrypted-main.js", + "program": "${workspaceFolder}/bin/scrypted-serve", "runtimeArgs": [ "--trace-warnings", "--nolazy", diff --git a/server/bin/scrypted-serve b/server/bin/scrypted-serve index a1b3fcb8de..7fe7acaf28 100755 --- a/server/bin/scrypted-serve +++ b/server/bin/scrypted-serve @@ -1,5 +1,4 @@ #!/usr/bin/env node -const { spawn } = require('child_process'); const path = require('path'); const mainPath = path.join(__dirname, '../dist/scrypted-main.js'); -spawn('node', ['--expose-gc', mainPath], { stdio: 'inherit' }); +require(mainPath); diff --git a/server/package-lock.json b/server/package-lock.json index eb1b43ab70..d657eb99ec 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/server", - "version": "0.7.19", + "version": "0.7.20", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/server", - "version": "0.7.19", + "version": "0.7.20", "license": "ISC", "dependencies": { "@mapbox/node-pre-gyp": "^1.0.10", diff --git a/server/package.json b/server/package.json index c2fad7411b..30996833bc 100644 --- a/server/package.json +++ b/server/package.json @@ -60,7 +60,7 @@ "bin": { "scrypted-serve": "bin/scrypted-serve" }, - "main": "dist/scrypted-main.js", + "main": "dist/scrypted-main-exports.js", "scripts": { "preserve": "npm run build", "serve": "node --expose-gc dist/scrypted-main.js", diff --git a/server/src/scrypted-main-exports.ts b/server/src/scrypted-main-exports.ts new file mode 100644 index 0000000000..568c5d6831 --- /dev/null +++ b/server/src/scrypted-main-exports.ts @@ -0,0 +1,54 @@ +import v8 from 'v8'; +import vm from 'vm'; +import process from 'process'; +import semver from 'semver'; +import { RPCResultError, startPeriodicGarbageCollection } from './rpc'; +import { PluginError } from './plugin/plugin-error'; + +function start(mainFilename: string) { + if (!global.gc) { + v8.setFlagsFromString('--expose_gc') + global.gc = vm.runInNewContext("gc"); + } + + if (!semver.gte(process.version, '16.0.0')) { + throw new Error('"node" version out of date. Please update node to v16 or higher.') + } + + // Node 17 changes the dns resolution order to return the record order. + // This causes issues with clients that are on "IPv6" networks that are + // actually busted and fail to connect to npm's IPv6 address. + // The workaround is to favor IPv4. + process.env['NODE_OPTIONS'] = '--dns-result-order=ipv4first'; + + startPeriodicGarbageCollection(); + + if (process.argv[2] === 'child' || process.argv[2] === 'child-thread') { + // plugins should never crash. this handler will be removed, and then readded + // after the plugin source map is retrieved. + process.on('uncaughtException', e => { + console.error('uncaughtException', e); + }); + process.on('unhandledRejection', e => { + console.error('unhandledRejection', e); + }); + + const start = require('./scrypted-plugin-main').default; + start(mainFilename); + } + else { + // unhandled rejections are allowed if they are from a rpc/plugin call. + process.on('unhandledRejection', error => { + if (error?.constructor !== RPCResultError && error?.constructor !== PluginError) { + console.error('wtf', error); + throw error; + } + console.warn('unhandled rejection of RPC Result', error); + }); + + const start = require('./scrypted-server-main').default; + start(mainFilename); + } +} + +export default start; diff --git a/server/src/scrypted-main.ts b/server/src/scrypted-main.ts index dde38e3cf8..7def1c2380 100644 --- a/server/src/scrypted-main.ts +++ b/server/src/scrypted-main.ts @@ -1,54 +1,3 @@ -import v8 from 'v8'; -import vm from 'vm'; -import process from 'process'; -import semver from 'semver'; -import { RPCResultError, startPeriodicGarbageCollection } from './rpc'; -import { PluginError } from './plugin/plugin-error'; +import start from './scrypted-main-exports'; -if (!global.gc) { - v8.setFlagsFromString('--expose_gc') - global.gc = vm.runInNewContext("gc"); -} - -if (!semver.gte(process.version, '16.0.0')) { - throw new Error('"node" version out of date. Please update node to v16 or higher.') -} - -// Node 17 changes the dns resolution order to return the record order. -// This causes issues with clients that are on "IPv6" networks that are -// actually busted and fail to connect to npm's IPv6 address. -// The workaround is to favor IPv4. -process.env['NODE_OPTIONS'] = '--dns-result-order=ipv4first'; - -startPeriodicGarbageCollection(); - -let startPromise: any; - -if (process.argv[2] === 'child' || process.argv[2] === 'child-thread') { - // plugins should never crash. this handler will be removed, and then readded - // after the plugin source map is retrieved. - process.on('uncaughtException', e => { - console.error('uncaughtException', e); - }); - process.on('unhandledRejection', e => { - console.error('unhandledRejection', e); - }); - - const start = require('./scrypted-plugin-main').default; - startPromise = start(__filename); -} -else { - // unhandled rejections are allowed if they are from a rpc/plugin call. - process.on('unhandledRejection', error => { - if (error?.constructor !== RPCResultError && error?.constructor !== PluginError) { - console.error('wtf', error); - throw error; - } - console.warn('unhandled rejection of RPC Result', error); - }); - - const start = require('./scrypted-server-main').default; - startPromise = start(__filename); -} - -export default startPromise; +start(__filename);