util: add util._extend
for extending objects
There were 2 duplicates with such functionality in `cluster` and `child_process` modules which were replaced by this function.
This commit is contained in:
parent
3f4062309e
commit
c6c6f98f1c
@ -22,7 +22,7 @@
|
|||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var net = require('net');
|
var net = require('net');
|
||||||
var Process = process.binding('process_wrap').Process;
|
var Process = process.binding('process_wrap').Process;
|
||||||
var inherits = require('util').inherits;
|
var util = require('util');
|
||||||
var constants; // if (!constants) constants = process.binding('constants');
|
var constants; // if (!constants) constants = process.binding('constants');
|
||||||
|
|
||||||
var Pipe;
|
var Pipe;
|
||||||
@ -53,19 +53,6 @@ function createSocket(pipe, readable) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
function mergeOptions(target, overrides) {
|
|
||||||
if (overrides) {
|
|
||||||
var keys = Object.keys(overrides);
|
|
||||||
for (var i = 0, len = keys.length; i < len; i++) {
|
|
||||||
var k = keys[i];
|
|
||||||
if (overrides[k] !== undefined) {
|
|
||||||
target[k] = overrides[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function setupChannel(target, channel) {
|
function setupChannel(target, channel) {
|
||||||
target._channel = channel;
|
target._channel = channel;
|
||||||
@ -244,7 +231,7 @@ exports.exec = function(command /*, options, callback */) {
|
|||||||
args = ['/s', '/c', '"' + command + '"'];
|
args = ['/s', '/c', '"' + command + '"'];
|
||||||
// Make a shallow copy before patching so we don't clobber the user's
|
// Make a shallow copy before patching so we don't clobber the user's
|
||||||
// options object.
|
// options object.
|
||||||
options = mergeOptions({}, options);
|
options = util._extend({}, options);
|
||||||
options.windowsVerbatimArguments = true;
|
options.windowsVerbatimArguments = true;
|
||||||
} else {
|
} else {
|
||||||
file = '/bin/sh';
|
file = '/bin/sh';
|
||||||
@ -281,7 +268,7 @@ exports.execFile = function(file /* args, options, callback */) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Merge optionArg into options
|
// Merge optionArg into options
|
||||||
mergeOptions(options, optionArg);
|
util._extend(options, optionArg);
|
||||||
|
|
||||||
var child = spawn(file, args, {
|
var child = spawn(file, args, {
|
||||||
cwd: options.cwd,
|
cwd: options.cwd,
|
||||||
@ -430,7 +417,7 @@ function ChildProcess() {
|
|||||||
maybeExit(self);
|
maybeExit(self);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
inherits(ChildProcess, EventEmitter);
|
util.inherits(ChildProcess, EventEmitter);
|
||||||
|
|
||||||
|
|
||||||
function setStreamOption(name, index, options) {
|
function setStreamOption(name, index, options) {
|
||||||
|
@ -29,18 +29,6 @@ function isObject(o) {
|
|||||||
return (typeof o === 'object' && o !== null);
|
return (typeof o === 'object' && o !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function extendObject(origin, add) {
|
|
||||||
// Don't do anything if add isn't an object
|
|
||||||
if (!add) return origin;
|
|
||||||
|
|
||||||
var keys = Object.keys(add),
|
|
||||||
i = keys.length;
|
|
||||||
while (i--) {
|
|
||||||
origin[keys[i]] = add[keys[i]];
|
|
||||||
}
|
|
||||||
return origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
var debug;
|
var debug;
|
||||||
if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
|
if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
|
||||||
debug = function(x) {
|
debug = function(x) {
|
||||||
@ -138,7 +126,7 @@ function isInternalMessage(message) {
|
|||||||
|
|
||||||
// Modyfi message object to be internal
|
// Modyfi message object to be internal
|
||||||
function internalMessage(inMessage) {
|
function internalMessage(inMessage) {
|
||||||
var outMessage = extendObject({}, inMessage);
|
var outMessage = util._extend({}, inMessage);
|
||||||
|
|
||||||
// Add internal prefix to cmd
|
// Add internal prefix to cmd
|
||||||
outMessage.cmd = INTERNAL_PREFIX + (outMessage.cmd || '');
|
outMessage.cmd = INTERNAL_PREFIX + (outMessage.cmd || '');
|
||||||
@ -172,7 +160,7 @@ var messageHandingObject = {};
|
|||||||
function handleMessage(worker, inMessage, inHandle) {
|
function handleMessage(worker, inMessage, inHandle) {
|
||||||
|
|
||||||
//Remove internal prefix
|
//Remove internal prefix
|
||||||
var message = extendObject({}, inMessage);
|
var message = util._extend({}, inMessage);
|
||||||
message.cmd = inMessage.cmd.substr(INTERNAL_PREFIX.length);
|
message.cmd = inMessage.cmd.substr(INTERNAL_PREFIX.length);
|
||||||
|
|
||||||
var respondUsed = false;
|
var respondUsed = false;
|
||||||
@ -276,11 +264,11 @@ function Worker(customEnv) {
|
|||||||
|
|
||||||
// Create env object
|
// Create env object
|
||||||
// first: copy and add uniqueID
|
// first: copy and add uniqueID
|
||||||
var envCopy = extendObject({}, env);
|
var envCopy = util._extend({}, env);
|
||||||
envCopy['NODE_UNIQUE_ID'] = this.uniqueID;
|
envCopy['NODE_UNIQUE_ID'] = this.uniqueID;
|
||||||
// second: extend envCopy with the env argument
|
// second: extend envCopy with the env argument
|
||||||
if (isObject(customEnv)) {
|
if (isObject(customEnv)) {
|
||||||
envCopy = extendObject(envCopy, customEnv);
|
envCopy = util._extend(envCopy, customEnv);
|
||||||
}
|
}
|
||||||
|
|
||||||
// fork worker
|
// fork worker
|
||||||
|
12
lib/util.js
12
lib/util.js
@ -506,3 +506,15 @@ exports.inherits = function(ctor, superCtor) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports._extend = function(origin, add) {
|
||||||
|
// Don't do anything if add isn't an object
|
||||||
|
if (!add) return origin;
|
||||||
|
|
||||||
|
var keys = Object.keys(add);
|
||||||
|
var i = keys.length;
|
||||||
|
while (i--) {
|
||||||
|
origin[keys[i]] = add[keys[i]];
|
||||||
|
}
|
||||||
|
return origin;
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user