Skip to content

Commit

Permalink
Always decycle objects before cloning (#977)
Browse files Browse the repository at this point in the history
- Fixes #862 
- Fixes #474 
- Fixes #914
  • Loading branch information
jifeon authored and indexzero committed Jul 27, 2017
1 parent 57322d3 commit 9a57be3
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/winston/common.js
Expand Up @@ -77,15 +77,10 @@ exports.longestElement = function (xs) {
// i.e. JSON objects that are either literals or objects (no Arrays, etc)
//
exports.clone = function (obj) {
//
// We only need to clone reference types (Object)
//
var copy = {};

if (obj instanceof Error) {
// With potential custom Error objects, this might not be exactly correct,
// but probably close-enough for purposes of this lib.
copy = { message: obj.message };
var copy = { message: obj.message };
Object.getOwnPropertyNames(obj).forEach(function (key) {
copy[key] = obj[key];
});
Expand All @@ -99,6 +94,15 @@ exports.clone = function (obj) {
return new Date(obj.getTime());
}

return clone(cycle.decycle(obj));
};

function clone(obj) {
//
// We only need to clone reference types (Object)
//
var copy = {};

for (var i in obj) {
if (Array.isArray(obj[i])) {
copy[i] = obj[i].slice(0);
Expand All @@ -115,7 +119,7 @@ exports.clone = function (obj) {
}

return copy;
};
}

//
// ### function log (options)
Expand All @@ -140,7 +144,7 @@ exports.log = function (options) {
timestamp = options.timestamp ? timestampFn() : null,
showLevel = options.showLevel === undefined ? true : options.showLevel,
meta = options.meta !== null && options.meta !== undefined && !(options.meta instanceof Error)
? exports.clone(cycle.decycle(options.meta))
? exports.clone(options.meta)
: options.meta || null,
output;

Expand Down

0 comments on commit 9a57be3

Please sign in to comment.