+51
-51
@@ -82,58 +82,58 @@ var cp = require('child_process'); | ||
| var execPath = options.execPath || process.env.PFORK_EXEC_PATH; | ||
| var flag = util.getMaxSemiSpaceFlag(execPath); | ||
| var version = util.getVersion(execPath); | ||
| flag && args.unshift(flag + '=64'); | ||
| if (version) { | ||
| version = version.substring(1).split('.'); | ||
| var supportTlsMinV1 = version[0] > 11; | ||
| var supportMaxHeaderSize = (version[0] == 10 && version[1] >= 15) || (version[0] == 11 && version[1] > 5) || version[0] > 11; | ||
| if (supportMaxHeaderSize) { | ||
| args.unshift('--max-http-header-size=' + (options.maxHttpHeadersSize || 256000)); | ||
| util.getArgs(execPath, function(version, flag) { | ||
| flag && args.unshift(flag + '=64'); | ||
| if (version) { | ||
| version = version.substring(1).split('.'); | ||
| var supportTlsMinV1 = version[0] > 11; | ||
| var supportMaxHeaderSize = (version[0] == 10 && version[1] >= 15) || (version[0] == 11 && version[1] > 5) || version[0] > 11; | ||
| if (supportMaxHeaderSize) { | ||
| args.unshift('--max-http-header-size=' + (options.maxHttpHeadersSize || 256000)); | ||
| } | ||
| supportTlsMinV1 && args.unshift('--tls-min-v1.0'); | ||
| } | ||
| supportTlsMinV1 && args.unshift('--tls-min-v1.0'); | ||
| } | ||
| try { | ||
| var spawnOpts = options._detached === false || process.env.PFORK_MODE === 'bind'; | ||
| spawnOpts = spawnOpts ? { stdio: [0, 1, 2, 'ipc'] } : { | ||
| detached: true, | ||
| stdio: ['ipc'] | ||
| }; | ||
| child = cp.spawn(execPath || 'node', args, spawnOpts); | ||
| child.on('error', errorHandler); | ||
| child.on('close', errorHandler); | ||
| child.on('exit', errorHandler); | ||
| child.on('disconnect', errorHandler); | ||
| child.on('message', function(msg) { | ||
| clearTimeout(heartbeatTimeout); | ||
| heartbeatTimeout = null; | ||
| if (msg == MESSAGE) { | ||
| return; | ||
| } | ||
| try { | ||
| if (msg = JSON.parse(msg)) { | ||
| if (msg.type == MESSAGE) { | ||
| callbackHandler(null, msg.data); | ||
| } else if (msg.type == DATA) { | ||
| emitter.emit('data', msg.data); | ||
| } else if (msg.type == ERROR) { | ||
| errorHandler(msg.data); | ||
| } | ||
| } | ||
| } catch(e) {} | ||
| }); | ||
| if (child.stderr) { | ||
| child.stderr.on('data', function(data) { | ||
| if (done || !Buffer.isBuffer(data)) { | ||
| try { | ||
| var spawnOpts = options._detached === false || process.env.PFORK_MODE === 'bind'; | ||
| spawnOpts = spawnOpts ? { stdio: [0, 1, 2, 'ipc'] } : { | ||
| detached: true, | ||
| stdio: ['ipc'] | ||
| }; | ||
| child = cp.spawn(execPath || 'node', args, spawnOpts); | ||
| child.on('error', errorHandler); | ||
| child.on('close', errorHandler); | ||
| child.on('exit', errorHandler); | ||
| child.on('disconnect', errorHandler); | ||
| child.on('message', function(msg) { | ||
| clearTimeout(heartbeatTimeout); | ||
| heartbeatTimeout = null; | ||
| if (msg == MESSAGE) { | ||
| return; | ||
| } | ||
| errMsg = errMsg ? Buffer.concat([errMsg, data]) : data; | ||
| try { | ||
| if (msg = JSON.parse(msg)) { | ||
| if (msg.type == MESSAGE) { | ||
| callbackHandler(null, msg.data); | ||
| } else if (msg.type == DATA) { | ||
| emitter.emit('data', msg.data); | ||
| } else if (msg.type == ERROR) { | ||
| errorHandler(msg.data); | ||
| } | ||
| } | ||
| } catch(e) {} | ||
| }); | ||
| if (child.stderr) { | ||
| child.stderr.on('data', function(data) { | ||
| if (done || !Buffer.isBuffer(data)) { | ||
| return; | ||
| } | ||
| errMsg = errMsg ? Buffer.concat([errMsg, data]) : data; | ||
| }); | ||
| } | ||
| child.unref(); | ||
| keepAlive(); | ||
| promise.kill = killChild; | ||
| } catch(err) { | ||
| errorHandler(err); | ||
| } | ||
| child.unref(); | ||
| keepAlive(); | ||
| promise.kill = killChild; | ||
| } catch(err) { | ||
| errorHandler(err); | ||
| } | ||
| }); | ||
@@ -140,0 +140,0 @@ function keepAlive() { |
+72
-24
@@ -5,2 +5,59 @@ var cp = require('child_process'); | ||
| function execCmd(execPath, args, callback) { | ||
| var data; | ||
| var done; | ||
| var timer; | ||
| var execCb = function() { | ||
| if (!done) { | ||
| clearTimeout(timer); | ||
| done = true; | ||
| callback(data ? String(data) : ''); | ||
| } | ||
| }; | ||
| try { | ||
| var child = cp.spawn(execPath || 'node', args, { detached: true }); | ||
| timer = setTimeout(function() { | ||
| try { | ||
| process.kill(child.pid); | ||
| } catch(e) {} | ||
| }, 5000); | ||
| child.on('error', execCb); | ||
| child.on('close', execCb); | ||
| child.on('exit', execCb); | ||
| child.on('disconnect', execCb); | ||
| child.stdout.on('data', function(chunk) { | ||
| data = data ? Buffer.concat([data, chunk]) : chunk; | ||
| }); | ||
| child.stdout.on('end', execCb); | ||
| child.unref(); | ||
| } catch(err) { | ||
| execCb(err); | ||
| } | ||
| } | ||
| function getVersion(execPath, callback) { | ||
| if (!execPath) { | ||
| return callback(process.version); | ||
| } | ||
| execCmd(execPath, ['-v'], function(str) { | ||
| if (/v\d+\.\d+\.\d+/.test(str)) { | ||
| callback(RegExp['$&']); | ||
| } else { | ||
| callback(''); | ||
| } | ||
| }); | ||
| } | ||
| function getMaxSemiSpaceFlag(execPath, callback) { | ||
| execCmd(execPath, ['--v8-options'], function(v8Options) { | ||
| if (v8Options.indexOf('--max-semi-space-size') !== -1) { | ||
| return callback('--max-semi-space-size'); | ||
| } | ||
| if (v8Options.indexOf('--max_semi_space_size') !== -1) { | ||
| return callback('--max_semi_space_size'); | ||
| } | ||
| callback(''); | ||
| }); | ||
| } | ||
| module.exports = { | ||
@@ -13,28 +70,19 @@ noop: function() {}, | ||
| ERROR: 'pforkError', | ||
| getVersion: function(execPath) { | ||
| if (!execPath) { | ||
| return process.version; | ||
| } | ||
| if (typeof cp.spawnSync === 'function') { | ||
| try { | ||
| if (/v\d+\.\d+\.\d+/.test(String(cp.spawnSync(execPath, ['-v']).output))) { | ||
| return RegExp['$&']; | ||
| } | ||
| } catch (e) {} | ||
| } | ||
| }, | ||
| getMaxSemiSpaceFlag: function(execPath) { | ||
| if (typeof cp.spawnSync !== 'function') { | ||
| return; | ||
| } | ||
| try { | ||
| var v8Options = String(cp.spawnSync(execPath || 'node', ['--v8-options']).output); | ||
| if (v8Options.indexOf('--max-semi-space-size') !== -1) { | ||
| return '--max-semi-space-size'; | ||
| getArgs: function(execPath, callback) { | ||
| var version; | ||
| var flag; | ||
| var execCb = function() { | ||
| if (version != null && flag != null) { | ||
| callback(version, flag); | ||
| } | ||
| if (v8Options.indexOf('--max_semi_space_size') !== -1) { | ||
| return '--max_semi_space_size'; | ||
| } | ||
| } catch (e) {} | ||
| }; | ||
| getVersion(execPath, function(v) { | ||
| version = v; | ||
| execCb(); | ||
| }); | ||
| getMaxSemiSpaceFlag(execPath, function(f) { | ||
| flag = f; | ||
| execCb(); | ||
| }); | ||
| } | ||
| }; |
+1
-1
| { | ||
| "name": "pfork", | ||
| "description": "fork process", | ||
| "version": "0.4.0", | ||
| "version": "0.5.0", | ||
| "main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "author": "avenwu <avenwu@vip.qq.com>", |
12954
9%394
12.89%