Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,53 @@
"prettier": "npm:wp-prettier@1.18.2"
},
"dependencies": {
"@types/bunyan": "^1.8.4",
"@types/dockerode": "^3.3.43",
"@types/express": "^4.16.0",
"@types/express-session": "^1.15.10",
"@types/forever-monitor": "^1.7.9",
"@types/fs-extra": "^8.0.0",
"@types/http-proxy": "^1.16.2",
"@types/bunyan": "^1.8.11",
"@types/dockerode": "^4.0.1",
"@types/express": "4.17.1",
"@types/express-session": "1.15.15",
"@types/fs-extra": "^8.1.5",
"@types/http-proxy": "^1.17.17",
"@types/jest": "29",
"@types/lodash": "^4.14.116",
"@types/lodash": "^4.17.24",
"@types/node": "20",
"@types/node-fetch": "^2.1.2",
"@types/react": "^16.4.8",
"@types/react-dom": "^16.0.7",
"@types/node-fetch": "^2.6.13",
"@types/react": "^16.14.70",
"@types/react-dom": "^16.9.25",
"@types/strip-ansi": "^3.0.0",
"@types/striptags": "^3.1.1",
"@types/tar-fs": "^1.16.1",
"@types/useragent": "^2.1.1",
"@types/tar-fs": "^1.16.3",
"bunyan": "^1.8.15",
"docker-parse-image": "^3.0.1",
"dockerode": "^4.0.12",
"express": "^4.16.3",
"express-session": "^1.15.6",
"forever-monitor": "3.0.3",
"dockerode": "^5.0.0",
"express": "^4.22.2",
"express-session": "^1.19.0",
"fs-extra": "^8.0.0",
"get-port": "^5.1.1",
"hot-shots": "^6.3.0",
"http-proxy": "^1.17.0",
"http-proxy": "^1.18.1",
"jest": "29",
"lodash": "^4.17.10",
"lodash": "^4.18.1",
"node-fetch": "^2.6.7",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"strip-ansi": "^5.0.0",
"striptags": "^3.1.1",
"striptags": "^3.2.0",
"tar-fs": "^2.1.4",
"ts-jest": "29.4.11",
"typescript": "5",
"useragent": "^2.3.0"
"typescript": "5"
},
"resolutions": {
"@protobufjs/utf8": "1.1.1",
"ansi-regex": "4.1.1",
"brace-expansion": "1.1.13",
"braces": "3.0.3",
"bunyan/mv/mkdirp": "0.5.6",
"debug": "4.3.1",
"lodash": "4.18.1",
"micromatch": "4.0.8",
"minimatch": "3.1.4",
"minimist": "1.2.8",
"protobufjs": "7.5.5"
"picomatch": "2.3.2",
"protobufjs": "7.5.8"
},
"scripts": {
"audit:critical": "node scripts/audit-critical.js",
Expand Down
60 changes: 37 additions & 23 deletions src/daemon.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
import forever from 'forever-monitor';
import { ChildProcess, spawn } from 'child_process';

import { l } from './logger';

const child = new forever.Monitor( 'build/index.js', {
watch: false,
silent: false,
max: Infinity,
minUptime: 2000,
} ) as any;
const childScript = 'build/index.js';
const minUptime = 2000;

child.on( 'error', ( err: unknown ) => {
l.error( { err }, 'forever: Error during run' );
} );
let stopping = false;
let child: ChildProcess | undefined;

child.on( 'restart', () => {
l.info( 'forever: Restarting' );
} );
function stop( signal: NodeJS.Signals ) {
stopping = true;
if ( child && ! child.killed ) {
child.kill( signal );
}
}

child.on( 'exit:code', ( code: number, signal: string ) => {
l.info( { code, signal }, 'forever: exited child', code, signal );
} );
function start() {
const startedAt = Date.now();
child = spawn( process.execPath, [ childScript ], {
stdio: 'inherit',
} );

child.on( 'exit', ( exitedChild: unknown, spinning: boolean ) => {
l.info( { child: exitedChild, spinning }, 'forever: really exited', exitedChild, spinning );
} );
l.info( { pid: child.pid }, 'daemon: started child process' );

child.on( 'stop', ( childData: unknown ) => {
l.info( { data: childData }, 'forever: child stopping' );
} );
child.on( 'error', ( err: unknown ) => {
l.error( { err }, 'daemon: child process error' );
} );

child.start();
child.on( 'exit', ( code: number | null, signal: NodeJS.Signals | null ) => {
const uptime = Date.now() - startedAt;
l.info( { code, signal, uptime }, 'daemon: child process exited' );

if ( stopping ) {
return;
}

const restartDelay = uptime < minUptime ? minUptime : 0;
setTimeout( start, restartDelay );
} );
}

process.on( 'SIGINT', () => stop( 'SIGINT' ) );
process.on( 'SIGTERM', () => stop( 'SIGTERM' ) );

start();
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import express from 'express';
import fs from 'fs-extra';
import striptags from 'striptags';
import useragent from 'useragent';
import { exec } from 'child_process';

// internal
Expand Down Expand Up @@ -295,15 +294,14 @@ server.on( 'close', () => {
} );

function isBrowser( req: express.Request ): Boolean {
const ua = useragent.lookup( req.header( 'user-agent' ) );
const family = ua.family.toLocaleLowerCase();
const ua = ( req.header( 'user-agent' ) || '' ).toLocaleLowerCase();

return (
family === 'chrome' ||
family === 'safari' ||
family === 'firefox' ||
family === 'chrome mobile' ||
family === 'mobile safari'
ua.includes( 'chrome' ) ||
ua.includes( 'crios' ) ||
ua.includes( 'firefox' ) ||
ua.includes( 'fxios' ) ||
ua.includes( 'safari' )
);
}

Expand Down
Loading