From 0fb0419eeb939294d75928bbd2abc00d05b479f4 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sat, 24 Jul 2021 12:27:37 -0400 Subject: [PATCH] Remove legacy node version shims Signed-off-by: Matthew Peveler --- lib/API.js | 5 - lib/API/Extra.js | 26 +- lib/API/Serve.js | 21 +- lib/API/pm2-plus/helpers.js | 8 - lib/God.js | 17 +- lib/God/ForkMode.js | 7 +- lib/ProcessContainerForkLegacy.js | 37 --- lib/ProcessContainerLegacy.js | 309 ----------------------- test/interface/bus.fork.spec.mocha.js | 4 - test/interface/bus.spec.mocha.js | 4 - test/programmatic/custom_action.mocha.js | 5 - test/programmatic/graceful.mocha.js | 4 - 12 files changed, 22 insertions(+), 425 deletions(-) delete mode 100644 lib/ProcessContainerForkLegacy.js delete mode 100644 lib/ProcessContainerLegacy.js diff --git a/lib/API.js b/lib/API.js index f0931a50f..414e56e33 100644 --- a/lib/API.js +++ b/lib/API.js @@ -27,7 +27,6 @@ var UX = require('./API/UX'); var pkg = require('../package.json'); var hf = require('./API/Modules/flagExt.js'); var Configuration = require('./Configuration.js'); -const semver = require('semver') const sexec = require('./tools/sexec.js') var IMMUTABLE_MSG = chalk.bold.blue('Use --update-env to update environment variables'); @@ -322,10 +321,6 @@ class API { } if (!opts) opts = {}; - if (semver.lt(process.version, '6.0.0')) { - Common.printOut(conf.PREFIX_MSG_WARNING + 'Node 4 is deprecated, please upgrade to use pm2 to have all features'); - } - var that = this; if (util.isArray(opts.watch) && opts.watch.length === 0) opts.watch = (opts.rawArgs ? !!~opts.rawArgs.indexOf('--watch') : !!~process.argv.indexOf('--watch')) || false; diff --git a/lib/API/Extra.js b/lib/API/Extra.js index cf9a0fa1b..ee4054262 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -14,7 +14,6 @@ var fs = require('fs'); var fmt = require('../tools/fmt.js'); var dayjs = require('dayjs'); var pkg = require('../../package.json'); -const semver = require('semver'); const copyDirSync = require('../tools/copydirSync.js') module.exports = function(CLI) { @@ -755,24 +754,19 @@ module.exports = function(CLI) { CLI.prototype.inspect = function(app_name, cb) { const that = this; - if(semver.satisfies(process.versions.node, '>= 8.0.0')) { - this.trigger(app_name, 'internal:inspect', function (err, res) { - - if(res && res[0]) { - if (res[0].data.return === '') { - Common.printOut(`Inspect disabled on ${app_name}`); - } else { - Common.printOut(`Inspect enabled on ${app_name} => go to chrome : chrome://inspect !!!`); - } + this.trigger(app_name, 'internal:inspect', function (err, res) { + + if(res && res[0]) { + if (res[0].data.return === '') { + Common.printOut(`Inspect disabled on ${app_name}`); } else { - Common.printOut(`Unable to activate inspect mode on ${app_name} !!!`); + Common.printOut(`Inspect enabled on ${app_name} => go to chrome : chrome://inspect !!!`); } + } else { + Common.printOut(`Unable to activate inspect mode on ${app_name} !!!`); + } - that.exitCli(cst.SUCCESS_EXIT); - }); - } else { - Common.printOut('Inspect is available for node version >=8.x !'); that.exitCli(cst.SUCCESS_EXIT); - } + }); }; }; diff --git a/lib/API/Serve.js b/lib/API/Serve.js index bd7d04f91..2259ca595 100644 --- a/lib/API/Serve.js +++ b/lib/API/Serve.js @@ -10,19 +10,13 @@ var http = require('http'); var url = require('url'); var path = require('path'); var debug = require('debug')('pm2:serve'); -var semver = require('semver') - -var isNode4 = require('semver').lt(process.version, '6.0.0') - -if (!isNode4) { - var probe = require('@pm2/io'); - var errorMeter = probe.meter({ - name : '404/sec', - samples : 1, - timeframe : 60 - }) -} +var probe = require('@pm2/io'); +var errorMeter = probe.meter({ + name : '404/sec', + samples : 1, + timeframe : 60 +}) /** * list of supported content types. */ @@ -276,8 +270,7 @@ function serveFile(uri, request, response) { console.error('[%s] Error while serving %s with content-type %s : %s', new Date(), filePath, contentType, error.message || error); } - if (!isNode4) - errorMeter.mark(); + errorMeter.mark(); if (error.code === 'ENOENT') { if (options.spa && !request.wantHomepage) { request.wantHomepage = true; diff --git a/lib/API/pm2-plus/helpers.js b/lib/API/pm2-plus/helpers.js index d7ce32fbf..a312a3651 100644 --- a/lib/API/pm2-plus/helpers.js +++ b/lib/API/pm2-plus/helpers.js @@ -5,7 +5,6 @@ var Common = require('../../Common.js'); const chalk = require('chalk'); const forEach = require('async/forEach'); const open = require('../../tools/open.js'); -const semver = require('semver'); const Modules = require('../Modules'); function processesAreAlreadyMonitored(CLI, cb) { @@ -37,10 +36,6 @@ module.exports = function(CLI) { const modules = ['event-loop-inspector'] this.gl_is_km_linked = false - if (semver.satisfies(process.version, '< 10.0.0')) { - modules.push('v8-profiler-node8') - } - forEach(modules, (_module, next) => { Modules.uninstall(this, _module, () => { next() @@ -64,9 +59,6 @@ module.exports = function(CLI) { if (opts.type === 'enterprise' || opts.type === 'plus') { modules = ['pm2-logrotate', 'pm2-server-monit'] - if (semver.satisfies(process.version, '< 8.0.0')) { - modules.push('v8-profiler-node8') - } if (opts.type === 'enterprise') { modules.push('deep-metrics') } diff --git a/lib/God.js b/lib/God.js index da24f79f8..deb823276 100644 --- a/lib/God.js +++ b/lib/God.js @@ -25,23 +25,14 @@ var Utility = require('./Utility'); var cst = require('../constants.js'); var timesLimit = require('async/timesLimit'); var Configuration = require('./Configuration.js'); -var semver = require('semver'); /** * Override cluster module configuration */ -if (semver.lt(process.version, '10.0.0')) { - cluster.setupMaster({ - windowsHide: true, - exec : path.resolve(path.dirname(module.filename), 'ProcessContainerLegacy.js') - }); -} -else { - cluster.setupMaster({ - windowsHide: true, - exec : path.resolve(path.dirname(module.filename), 'ProcessContainer.js') - }); -} +cluster.setupMaster({ + windowsHide: true, + exec : path.resolve(path.dirname(module.filename), 'ProcessContainer.js') +}); /** * Expose God diff --git a/lib/God/ForkMode.js b/lib/God/ForkMode.js index 1187cc71f..f0c010655 100644 --- a/lib/God/ForkMode.js +++ b/lib/God/ForkMode.js @@ -55,12 +55,7 @@ module.exports = function ForkMode(God) { } if (interpreter === 'node' || RegExp('node$').test(interpreter)) { - if (semver.lt(process.version, '10.0.0')) { - args.push(path.resolve(path.dirname(module.filename), '..', 'ProcessContainerForkLegacy.js')); - } - else { - args.push(path.resolve(path.dirname(module.filename), '..', 'ProcessContainerFork.js')); - } + args.push(path.resolve(path.dirname(module.filename), '..', 'ProcessContainerFork.js')); } else args.push(pm2_env.pm_exec_path); diff --git a/lib/ProcessContainerForkLegacy.js b/lib/ProcessContainerForkLegacy.js deleted file mode 100644 index 3437f17f0..000000000 --- a/lib/ProcessContainerForkLegacy.js +++ /dev/null @@ -1,37 +0,0 @@ - /** - * Copyright 2013-2021 the PM2 project authors. All rights reserved. - * Use of this source code is governed by a license that - * can be found in the LICENSE file. - */ -// Inject custom modules -var ProcessUtils = require('./ProcessUtils') -ProcessUtils.injectModules() - -if (typeof(process.env.source_map_support) != "undefined" && - process.env.source_map_support !== "false") { - require('source-map-support').install(); -} - -// Rename the process -process.title = process.env.PROCESS_TITLE || 'node ' + process.env.pm_exec_path; - -if (process.connected && - process.send && - process.versions && - process.versions.node) - process.send({ - 'node_version': process.versions.node - }); - -// Require the real application -if (process.env.pm_exec_path) { - require('module')._load(process.env.pm_exec_path, null, true); -} -else - throw new Error('Could not _load() the script'); - -// Change some values to make node think that the user's application -// was started directly such as `node app.js` -process.mainModule = process.mainModule || {}; -process.mainModule.loaded = false; -require.main = process.mainModule; diff --git a/lib/ProcessContainerLegacy.js b/lib/ProcessContainerLegacy.js deleted file mode 100644 index 239284670..000000000 --- a/lib/ProcessContainerLegacy.js +++ /dev/null @@ -1,309 +0,0 @@ -/** - * Copyright 2013-2021 the PM2 project authors. All rights reserved. - * Use of this source code is governed by a license that - * can be found in the LICENSE file. - * - * This file wrap target application - * - redirect stdin, stderr to bus + log files - * - rename process - * - pid - */ - -var p = require('path'); -var cst = require('../constants'); -var Utility = require('./Utility.js'); -var ProcessUtils = require('./ProcessUtils'); - -// Load all env-vars from master. -var pm2_env = JSON.parse(process.env.pm2_env); -for(var k in pm2_env) { - process.env[k] = pm2_env[k]; -} - -// Rename process -process.title = process.env.PROCESS_TITLE || 'node ' + pm2_env.pm_exec_path; - -delete process.env.pm2_env; - -/** - * Main entrance to wrap the desired code - */ -(function ProcessContainer() { - var fs = require('fs'); - - ProcessUtils.injectModules() - - var stdFile = pm2_env.pm_log_path; - var outFile = pm2_env.pm_out_log_path; - var errFile = pm2_env.pm_err_log_path; - var pidFile = pm2_env.pm_pid_path; - var script = pm2_env.pm_exec_path; - - var original_send = process.send; - - if (typeof(process.env.source_map_support) != 'undefined' && - process.env.source_map_support !== 'false') { - require('source-map-support').install(); - } - - process.send = function() { - if (process.connected) - original_send.apply(this, arguments); - }; - - //send node version - if (process.versions && process.versions.node) { - process.send({ - 'node_version': process.versions.node - }); - } - - if (cst.MODIFY_REQUIRE) - require.main.filename = pm2_env.pm_exec_path; - - // Resets global paths for require() - require('module')._initPaths(); - - try { - fs.writeFileSync(pidFile, process.pid.toString()); - } catch (e) { - console.error(e.stack || e); - } - - // Add args to process if args specified on start - if (process.env.args != null) - process.argv = process.argv.concat(pm2_env.args); - - // stdio, including: out, err and entire (both out and err if necessary). - var stds = { - out: outFile, - err: errFile - }; - stdFile && (stds.std = stdFile); - - // uid/gid management - if (pm2_env.uid || pm2_env.gid) { - try { - if (pm2_env.uid) - process.setuid(pm2_env.uid); - if (process.env.gid) - process.setgid(pm2_env.gid); - } catch(e) { - setTimeout(function() { - console.error('%s on call %s', e.message, e.syscall); - console.error('%s is not accessible', pm2_env.uid); - return process.exit(1); - }, 100); - } - } - - exec(script, stds); -})(); - -/** - * Description - * @method exec - * @param {} script - * @param {} stds - * @return - */ -function exec(script, stds) { - if (p.extname(script) == '.coffee') { - try { - require('coffee-script/register'); - } catch (e) { - console.error('Failed to load CoffeeScript interpreter:', e.message || e); - } - } - - if (p.extname(script) == '.ls') { - try { - require('livescript'); - } catch (e) { - console.error('Failed to load LiveScript interpreter:', e.message || e); - } - } - - if (p.extname(script) == '.ts' || p.extname(script) == '.tsx') { - try { - require('ts-node/register'); - } catch (e) { - console.error('Failed to load Typescript interpreter:', e.message || e); - } - } - - process.on('message', function (msg) { - if (msg.type === 'log:reload') { - for (var k in stds){ - if (typeof stds[k] == 'object' && !isNaN(stds[k].fd)){ - if (stds[k].destroy) stds[k].destroy(); - else if (stds[k].end) stds[k].end(); - else if (stds[k].close) stds[k].close(); - stds[k] = stds[k]._file; - } - } - Utility.startLogging(stds, function (err) { - if (err) - return console.error('Failed to reload logs:', err.stack); - console.log('Reloading log...'); - }); - } - }); - - var dayjs = null; - - if (pm2_env.log_date_format) - dayjs = require('dayjs'); - - Utility.startLogging(stds, function (err) { - if (err) { - process.send({ - type : 'process:exception', - data : { - message: err.message, - syscall: 'ProcessContainer.startLogging' - } - }); - throw err; - return; - } - - process.stderr.write = (function(write) { - return function(string, encoding, cb) { - var log_data = null; - - // Disable logs if specified - if (pm2_env.disable_logs === true) { - return cb ? cb() : false; - } - - if (pm2_env.log_type && pm2_env.log_type === 'json') { - log_data = JSON.stringify({ - message : string.toString(), - timestamp : pm2_env.log_date_format && dayjs ? - dayjs().format(pm2_env.log_date_format) : new Date().toISOString(), - type : 'err', - process_id : pm2_env.pm_id, - app_name : pm2_env.name - }) + '\n'; - } - else if (pm2_env.log_date_format && dayjs) - log_data = `${dayjs().format(pm2_env.log_date_format)}: ${string.toString()}`; - else - log_data = string.toString(); - - process.send({ - type : 'log:err', - topic : 'log:err', - data : log_data - }); - - if (Utility.checkPathIsNull(pm2_env.pm_err_log_path) && - (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path))) - return cb ? cb() : false; - - stds.std && stds.std.write && stds.std.write(log_data, encoding); - stds.err && stds.err.write && stds.err.write(log_data, encoding, cb); - }; - })(process.stderr.write); - - process.stdout.write = (function(write) { - return function(string, encoding, cb) { - var log_data = null; - - // Disable logs if specified - if (pm2_env.disable_logs === true) { - return cb ? cb() : false; - } - - if (pm2_env.log_type && pm2_env.log_type === 'json') { - log_data = JSON.stringify({ - message : string.toString(), - timestamp : pm2_env.log_date_format && dayjs ? - dayjs().format(pm2_env.log_date_format) : new Date().toISOString(), - type : 'out', - process_id : pm2_env.pm_id, - app_name : pm2_env.name - }) + '\n'; - } - else if (pm2_env.log_date_format && dayjs) - log_data = `${dayjs().format(pm2_env.log_date_format)}: ${string.toString()}`; - else - log_data = string.toString(); - - process.send({ - type : 'log:out', - data : log_data - }); - - if (Utility.checkPathIsNull(pm2_env.pm_out_log_path) && - (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path))) - return cb ? cb() : null; - - stds.std && stds.std.write && stds.std.write(log_data, encoding); - stds.out && stds.out.write && stds.out.write(log_data, encoding, cb); - }; - })(process.stdout.write); - - function getUncaughtExceptionListener(listener) { - return function uncaughtListener(err) { - var error = err && err.stack ? err.stack : err; - - if (listener === 'unhandledRejection') { - error = 'You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:\n' + error; - } - - logError(['std', 'err'], error); - - // Notify master that an uncaughtException has been catched - try { - if (err) { - var errObj = {}; - - Object.getOwnPropertyNames(err).forEach(function(key) { - errObj[key] = err[key]; - }); - } - - process.send({ - type : 'log:err', - topic : 'log:err', - data : '\n' + error + '\n' - }); - process.send({ - type : 'process:exception', - data : errObj !== undefined ? errObj : {message: 'No error but ' + listener + ' was caught!'} - }); - } catch(e) { - logError(['std', 'err'], 'Channel is already closed can\'t broadcast error:\n' + e.stack); - } - - if (!process.listeners(listener).filter(function (listener) { - return listener !== uncaughtListener; - }).length) { - if (listener == 'uncaughtException') { - process.emit('disconnect'); - process.exit(cst.CODE_UNCAUGHTEXCEPTION); - } - } - } - } - - process.on('uncaughtException', getUncaughtExceptionListener('uncaughtException')); - process.on('unhandledRejection', getUncaughtExceptionListener('unhandledRejection')); - - // Change dir to fix process.cwd - process.chdir(pm2_env.pm_cwd || process.env.PWD || p.dirname(script)); - - require('module')._load(script, null, true); - - function logError(types, error){ - try { - types.forEach(function(type){ - stds[type] && typeof stds[type].write == 'function' && stds[type].write(error + '\n'); - }); - } catch(e) { } - } - }); - -} diff --git a/test/interface/bus.fork.spec.mocha.js b/test/interface/bus.fork.spec.mocha.js index 67d2e068d..72e35829b 100644 --- a/test/interface/bus.fork.spec.mocha.js +++ b/test/interface/bus.fork.spec.mocha.js @@ -3,10 +3,6 @@ var should = require('should'); var PM2 = require('../..'); var Plan = require('../helpers/plan.js'); -if (require('semver').lt(process.version, '6.0.0')) { - return process.exit(0) -} - var PROCESS_ARCH = Object.keys({ pm_id : 0, name : 'app' diff --git a/test/interface/bus.spec.mocha.js b/test/interface/bus.spec.mocha.js index da8e8e78f..2448b06af 100644 --- a/test/interface/bus.spec.mocha.js +++ b/test/interface/bus.spec.mocha.js @@ -3,10 +3,6 @@ var should = require('should'); var PM2 = require('../..'); var Plan = require('../helpers/plan.js'); -if (require('semver').lt(process.version, '6.0.0')) { - return process.exit(0) -} - const PATH_FIXTURES = process.cwd() + '/test/interface/fixtures/'; var PROCESS_ARCH = Object.keys({ diff --git a/test/programmatic/custom_action.mocha.js b/test/programmatic/custom_action.mocha.js index db18c723f..6df8aaf3c 100644 --- a/test/programmatic/custom_action.mocha.js +++ b/test/programmatic/custom_action.mocha.js @@ -3,11 +3,6 @@ process.chdir(__dirname); var pm2 = require('../..'); var should = require('should'); -var semver = require('semver') - -if (semver.lt(process.version, '6.0.0')) { - return process.exit(0) -} describe('Custom actions via CLI/API', function() { before(function(done) { diff --git a/test/programmatic/graceful.mocha.js b/test/programmatic/graceful.mocha.js index 4777e4a90..91de677c6 100644 --- a/test/programmatic/graceful.mocha.js +++ b/test/programmatic/graceful.mocha.js @@ -5,15 +5,11 @@ var PM2 = require('../..'); var should = require('should'); var path = require('path'); var Plan = require('../helpers/plan.js'); -var semver = require('semver'); var sexec = require('../../lib/tools/sexec.js') process.chdir(__dirname); describe('Wait ready / Graceful start / restart', function() { - if (!semver.satisfies(process.version, '>= 4.0.0')) - process.exit(0); - this.retries(2) var pm2 = new PM2.custom({