Skip to content

Commit

Permalink
server: add non invoking entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Mar 20, 2023
1 parent c62d4bd commit 0516ca8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 59 deletions.
2 changes: 1 addition & 1 deletion server/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"<node_internals>/**"
],
"preLaunchTask": "npm: build",
"program": "${workspaceFolder}/dist/scrypted-main.js",
"program": "${workspaceFolder}/bin/scrypted-serve",
"runtimeArgs": [
"--trace-warnings",
"--nolazy",
Expand Down
3 changes: 1 addition & 2 deletions server/bin/scrypted-serve
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions server/package-lock.json

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

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
54 changes: 54 additions & 0 deletions server/src/scrypted-main-exports.ts
Original file line number Diff line number Diff line change
@@ -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;
55 changes: 2 additions & 53 deletions server/src/scrypted-main.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 0516ca8

Please sign in to comment.