Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manage object type on format #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ TransportStream.prototype._write = function _write(info, enc, callback) {
// We trap(and re-throw) any errors generated by the user-provided format, but also
// guarantee that the streams callback is invoked so that we can continue flowing.
try {
transformed = this.format.transform(Object.assign({}, info), this.format.options);
transformed = this.format.transform(
cloneObj(info),
this.format.options
);
} catch (err) {
errState = err;
}
Expand Down Expand Up @@ -142,7 +145,7 @@ TransportStream.prototype._writev = function _writev(chunks, callback) {
// guarantee that the streams callback is invoked so that we can continue flowing.
try {
transformed = this.format.transform(
Object.assign({}, chunks[i].chunk),
cloneObj(chunks[i].chunk),
this.format.options
);
} catch (err) {
Expand Down Expand Up @@ -201,6 +204,18 @@ TransportStream.prototype._accept = function _accept(write) {
return false;
};

function cloneObj(obj) {
const clonedObj = {};

Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj)).forEach((propName) => {
Object.defineProperty(clonedObj, propName, Object.getOwnPropertyDescriptor(obj, propName));
});

Object.setPrototypeOf(clonedObj, Object.getPrototypeOf(obj));

return clonedObj;
}

/**
* _nop is short for "No operation"
* @returns {Boolean} Intentionally false.
Expand Down