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

Proxy events #7

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const knownProperties = [
];

module.exports = (fromStream, toStream) => {
if (toStream._readableState.autoDestroy) {
throw new Error('The second stream must have the `autoDestroy` option set to `false`');
}

const fromProperties = new Set(Object.keys(fromStream).concat(knownProperties));

const properties = {};
Expand Down Expand Up @@ -50,8 +54,24 @@ module.exports = (fromStream, toStream) => {
Object.defineProperties(toStream, properties);

fromStream.once('aborted', () => {
toStream.destroy();

toStream.emit('aborted');
});

fromStream.once('close', () => {
if (fromStream.complete) {
if (toStream.readable) {
toStream.once('end', () => {
toStream.emit('close');
});
} else {
toStream.emit('close');
}
} else {
toStream.emit('close');
}
});

return toStream;
};
28 changes: 0 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,6 @@ myStream.destroy();

Please note that `myStream` and `responseStream` never throws. The error is passed to the request instead.

**Note #2:** The `aborted` and `close` events are not proxied. You have to add them manually:

```js
const stream = require('stream');
const mimicResponse = require('mimic-response');

const responseStream = getHttpResponseStream();
const myStream = new stream.PassThrough({
destroy(error, callback) {
responseStream.destroy();

callback(error);
}
});

responseStream.once('aborted', () => {
myStream.destroy();

myStream.emit('aborted');
});

responseStream.once('closed', () => {
myStream.emit('closed');
});

responseStream.pipe(myStream);
```

#### from

Type: `Stream`
Expand Down
94 changes: 91 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import pify from 'pify';
import pEvent from 'p-event';
import mimicResponse from '.';

const nodejsMajorVersion = process.versions.node.split('.')[0];
let server;

test.before(async () => {
Expand All @@ -18,6 +19,14 @@ test.before(async () => {
response.end('sdf');
}, 2);
});

server.get('/aborted', (_request, response) => {
response.write('a');

setTimeout(() => {
response.socket.destroy();
}, 2);
});
});

test('normal', async t => {
Expand All @@ -27,13 +36,12 @@ test('normal', async t => {
return this;
};

const toStream = new stream.PassThrough();
const toStream = new stream.PassThrough({autoDestroy: false});
mimicResponse(response, toStream);

t.is(toStream.statusCode, 200);
t.is(toStream.unicorn, '🦄');
t.is(toStream.getContext(), response.getContext());
t.false(toStream.complete);

response.resume();
await pEvent(response, 'end');
Expand All @@ -53,7 +61,7 @@ test('do not overwrite prototype properties', async t => {
return origOn.call(this, name, handler);
};

const toStream = new stream.PassThrough();
const toStream = new stream.PassThrough({autoDestroy: false});
mimicResponse(response, toStream);

t.false(Object.keys(toStream).includes('on'));
Expand All @@ -65,5 +73,85 @@ test('do not overwrite prototype properties', async t => {
response.resume();
await pEvent(response, 'end');

await new Promise(resolve => setImmediate(resolve));

t.true(toStream.complete);
});

test('`aborted` event', async t => {
const response = await pify(http.get, {errorFirst: false})(`${server.url}/aborted`);

const toStream = new stream.PassThrough({autoDestroy: false});
mimicResponse(response, toStream);

await pEvent(toStream, 'aborted');

t.true(toStream.destroyed);
});

test('autoDestroy must be false', async t => {
const response = await pify(http.get, {errorFirst: false})(`${server.url}/aborted`);

const toStream = new stream.PassThrough({autoDestroy: true});

t.throws(() => mimicResponse(response, toStream), {
message: 'The second stream must have the `autoDestroy` option set to `false`'
});
});

test('`close` event', async t => {
{
const response = await pify(http.get, {errorFirst: false})(server.url);

const toStream = new stream.PassThrough({autoDestroy: false});
mimicResponse(response, toStream);

t.true(response.readable);

if (nodejsMajorVersion > 11) {
t.false(response.readableEnded);
}

response.pipe(toStream);
toStream.resume();

await pEvent(toStream, 'close');

t.false(response.readable);
t.false(toStream.readable);

if (nodejsMajorVersion > 11) {
t.true(response.readableEnded);
t.true(toStream.readableEnded);
}
}

{
const response = await pify(http.get, {errorFirst: false})(`${server.url}/aborted`);

const toStream = new stream.PassThrough({autoDestroy: false});
mimicResponse(response, toStream);

t.true(response.readable);

if (nodejsMajorVersion > 11) {
t.false(response.readableEnded);
}

response.pipe(toStream);
toStream.resume();

await pEvent(toStream, 'close');

if (nodejsMajorVersion < 12) {
t.false(response.readable);
t.true(toStream.readable);
} else if (nodejsMajorVersion < 13) {
t.true(response.readableEnded);
t.false(toStream.readableEnded);
} else {
t.false(response.readableEnded);
t.false(toStream.readableEnded);
}
}
});