Skip to content

Commit

Permalink
Don't overwrite properties on prototype (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson authored and sindresorhus committed Jul 11, 2018
1 parent 44a0267 commit 9256c81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 1 addition & 2 deletions index.js
Expand Up @@ -19,12 +19,11 @@ const knownProps = [
];

module.exports = (fromStream, toStream) => {
const toProps = Object.keys(toStream);
const fromProps = new Set(Object.keys(fromStream).concat(knownProps));

for (const prop of fromProps) {
// Don't overwrite existing properties
if (toProps.indexOf(prop) !== -1) {
if (prop in toStream) {
continue;
}

Expand Down
22 changes: 21 additions & 1 deletion test.js
Expand Up @@ -15,7 +15,7 @@ test.before(async () => {
});
});

test(async t => {
test('normal', async t => {
const response = await pify(http.get, {errorFirst: false})(server.url);
response.unicorn = '🦄';
response.getContext = function () {
Expand All @@ -29,3 +29,23 @@ test(async t => {
t.is(toStream.unicorn, '🦄');
t.is(toStream.getContext(), response.getContext());
});

test('do not overwrite prototype properties', async t => {
const response = await pify(http.get, {errorFirst: false})(server.url);
response.unicorn = '🦄';
response.getContext = function () {
return this;
};
const origOn = response.on;
response.on = function (name, handler) {
return origOn.call(this, name, handler);
};

const toStream = new stream.PassThrough();
m(response, toStream);

t.is(Object.keys(toStream).indexOf('on'), -1);
t.is(toStream.statusCode, 200);
t.is(toStream.unicorn, '🦄');
t.is(toStream.getContext(), response.getContext());
});

0 comments on commit 9256c81

Please sign in to comment.