Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
util: add util._extend for extending objects
Browse files Browse the repository at this point in the history
There were 2 duplicates with such functionality in `cluster` and
`child_process` modules which were replaced by this function.
  • Loading branch information
mmalecki authored and bnoordhuis committed Feb 20, 2012
1 parent 3f40623 commit c6c6f98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
21 changes: 4 additions & 17 deletions lib/child_process.js
Expand Up @@ -22,7 +22,7 @@
var EventEmitter = require('events').EventEmitter;
var net = require('net');
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 Pipe;
Expand Down Expand Up @@ -53,19 +53,6 @@ function createSocket(pipe, readable) {
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) {
target._channel = channel;
Expand Down Expand Up @@ -244,7 +231,7 @@ exports.exec = function(command /*, options, callback */) {
args = ['/s', '/c', '"' + command + '"'];
// Make a shallow copy before patching so we don't clobber the user's
// options object.
options = mergeOptions({}, options);
options = util._extend({}, options);
options.windowsVerbatimArguments = true;
} else {
file = '/bin/sh';
Expand Down Expand Up @@ -281,7 +268,7 @@ exports.execFile = function(file /* args, options, callback */) {
}

// Merge optionArg into options
mergeOptions(options, optionArg);
util._extend(options, optionArg);

var child = spawn(file, args, {
cwd: options.cwd,
Expand Down Expand Up @@ -430,7 +417,7 @@ function ChildProcess() {
maybeExit(self);
};
}
inherits(ChildProcess, EventEmitter);
util.inherits(ChildProcess, EventEmitter);


function setStreamOption(name, index, options) {
Expand Down
20 changes: 4 additions & 16 deletions lib/cluster.js
Expand Up @@ -29,18 +29,6 @@ function isObject(o) {
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;
if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
debug = function(x) {
Expand Down Expand Up @@ -138,7 +126,7 @@ function isInternalMessage(message) {

// Modyfi message object to be internal
function internalMessage(inMessage) {
var outMessage = extendObject({}, inMessage);
var outMessage = util._extend({}, inMessage);

// Add internal prefix to cmd
outMessage.cmd = INTERNAL_PREFIX + (outMessage.cmd || '');
Expand Down Expand Up @@ -172,7 +160,7 @@ var messageHandingObject = {};
function handleMessage(worker, inMessage, inHandle) {

//Remove internal prefix
var message = extendObject({}, inMessage);
var message = util._extend({}, inMessage);
message.cmd = inMessage.cmd.substr(INTERNAL_PREFIX.length);

var respondUsed = false;
Expand Down Expand Up @@ -276,11 +264,11 @@ function Worker(customEnv) {

// Create env object
// first: copy and add uniqueID
var envCopy = extendObject({}, env);
var envCopy = util._extend({}, env);
envCopy['NODE_UNIQUE_ID'] = this.uniqueID;
// second: extend envCopy with the env argument
if (isObject(customEnv)) {
envCopy = extendObject(envCopy, customEnv);
envCopy = util._extend(envCopy, customEnv);
}

// fork worker
Expand Down
12 changes: 12 additions & 0 deletions lib/util.js
Expand Up @@ -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;
};

0 comments on commit c6c6f98

Please sign in to comment.