diff --git a/app.js b/app.js index d3b4af0697..686b355602 100644 --- a/app.js +++ b/app.js @@ -37,7 +37,7 @@ var winston = require('winston'); var path = require('path'); var pkg = require('./package.json'); var file = require('./src/file'); -var debug = require('./src/meta/debugParams')().execArgv.length; +var debug = require('./src/meta/debugFork').debugging; global.env = process.env.NODE_ENV || 'production'; diff --git a/public/src/modules/pictureCropper.js b/public/src/modules/pictureCropper.js index 3bd8cf1516..5a02962d1a 100644 --- a/public/src/modules/pictureCropper.js +++ b/public/src/modules/pictureCropper.js @@ -1,7 +1,7 @@ 'use strict'; -define('pictureCropper', ['translator', 'cropper', 'benchpress'], function (translator, cropper, Benchpress) { +define('pictureCropper', ['translator', 'cropper', 'benchpress'], function (translator, Cropper, Benchpress) { var module = {}; module.show = function (data, callback) { @@ -46,7 +46,7 @@ define('pictureCropper', ['translator', 'cropper', 'benchpress'], function (tran var img = document.getElementById('cropped-image'); $(img).css('max-height', cropBoxHeight); - var cropperTool = new cropper.default(img, { + var cropperTool = new Cropper(img, { aspectRatio: data.aspectRatio, autoCropArea: 1, viewMode: 1, @@ -122,7 +122,7 @@ define('pictureCropper', ['translator', 'cropper', 'benchpress'], function (tran $(this).addClass('disabled'); cropperTool.destroy(); - cropperTool = new cropper.default(img, { + cropperTool = new Cropper(img, { viewMode: 1, autoCropArea: 1, ready: function () { diff --git a/src/meta/debugFork.js b/src/meta/debugFork.js new file mode 100644 index 0000000000..575eebd610 --- /dev/null +++ b/src/meta/debugFork.js @@ -0,0 +1,41 @@ +'use strict'; + +var fork = require('child_process').fork; + +var debugArg = process.execArgv.find(function (arg) { + return /^--(debug|inspect)/.test(arg); +}); +var debugging = !!debugArg; + +debugArg = debugArg ? debugArg.replace('-brk', '').split('=') : ['--debug', 5859]; +var lastAddress = parseInt(debugArg[1], 10); + +/** + * child-process.fork, but safe for use in debuggers + * @param {string} modulePath + * @param {string[]} [args] + * @param {any} [options] + */ +function debugFork(modulePath, args, options) { + var execArgv = []; + if (global.v8debug || debugging) { + lastAddress += 1; + + execArgv = [debugArg[0] + '=' + lastAddress, '--nolazy']; + } + + if (!Array.isArray(args)) { + options = args; + args = []; + } + + options = options || {}; + options = Object.assign({}, options, { + execArgv: execArgv, + }); + + return fork(modulePath, args, options); +} +debugFork.debugging = debugging; + +module.exports = debugFork; diff --git a/src/meta/debugParams.js b/src/meta/debugParams.js deleted file mode 100644 index 6b84500bff..0000000000 --- a/src/meta/debugParams.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -module.exports = function (execArgv) { - execArgv = execArgv || process.execArgv; - var debugArg = execArgv.find(function (arg) { - return /^--(debug|inspect)/.test(arg); - }); - if (global.v8debug || debugArg) { - debugArg = debugArg ? debugArg.split('=') : ['--debug', 5859]; - var num = parseInt(debugArg[1], 10) + 1; - - return { execArgv: [debugArg[0] + '=' + num, '--nolazy'] }; - } - - return { execArgv: [] }; -}; diff --git a/src/meta/minifier.js b/src/meta/minifier.js index 3b26f99ec5..c51f41578b 100644 --- a/src/meta/minifier.js +++ b/src/meta/minifier.js @@ -1,7 +1,6 @@ 'use strict'; var fs = require('fs'); -var childProcess = require('child_process'); var os = require('os'); var uglifyjs = require('uglify-js'); var async = require('async'); @@ -11,7 +10,7 @@ var postcss = require('postcss'); var autoprefixer = require('autoprefixer'); var clean = require('postcss-clean'); -var debugParams = require('./debugParams'); +var fork = require('./debugFork'); var Minifier = module.exports; @@ -47,13 +46,12 @@ function getChild() { return free.shift(); } - var forkProcessParams = debugParams(); - var proc = childProcess.fork(__filename, [], Object.assign({}, forkProcessParams, { + var proc = fork(__filename, [], { cwd: __dirname, env: { minifier_child: true, }, - })); + }); pool.push(proc); return proc; diff --git a/src/password.js b/src/password.js index 137382c993..6cc1e1776a 100644 --- a/src/password.js +++ b/src/password.js @@ -1,9 +1,8 @@ 'use strict'; -var fork = require('child_process').fork; var path = require('path'); -var debugParams = require('./meta/debugParams'); +var fork = require('./meta/debugFork'); exports.hash = function (rounds, password, callback) { forkChild({ type: 'hash', rounds: rounds, password: password }, callback); @@ -17,7 +16,7 @@ exports.compare = function (password, hash, callback) { }; function forkChild(message, callback) { - var child = fork(path.join(__dirname, 'bcrypt'), [], debugParams()); + var child = fork(path.join(__dirname, 'bcrypt')); child.on('message', function (msg) { if (msg.err) { diff --git a/test/meta.js b/test/meta.js index 539cb57c98..451184d63b 100644 --- a/test/meta.js +++ b/test/meta.js @@ -276,13 +276,28 @@ describe('meta', function () { }); }); - describe('debug params', function () { - it('should return fork arguments for debug', function (done) { - var debugParams = require('../src/meta/debugParams'); - var data = debugParams(['--debug=5858', '--foo=1']); - assert.equal(data.execArgv[0], '--debug=5859'); - assert.equal(data.execArgv[1], '--nolazy'); + describe('debugFork', function () { + var oldArgv; + before(function () { + oldArgv = process.execArgv; + process.execArgv = ['--debug=5858', '--foo=1']; + }); + + it('should detect debugging', function (done) { + var debugFork = require('../src/meta/debugFork'); + assert(!debugFork.debugging); + + var debugForkPath = require.resolve('../src/meta/debugFork'); + delete require.cache[debugForkPath]; + + debugFork = require('../src/meta/debugFork'); + assert(debugFork.debugging); + done(); }); + + after(function () { + process.execArgv = oldArgv; + }); }); });