Skip to content
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function buildRiviere(options = {}) {
let counter;

if (!length && ctx.body && ctx.body.readable) {
ctx.body = ctx.body.pipe((counter = Counter())).on('error', ctx.onerror);
ctx.body = ctx.body.pipe((counter = Counter())).on('error', err => ctx.onerror(err));
}

const res = ctx.res;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@workablehr/riviere",
"version": "1.22.0",
"version": "1.22.1",
"description": "log inbound/outbound HTTP traffic",
"types": "./types/riviere.d.ts",
"main": "index.js",
Expand Down
41 changes: 40 additions & 1 deletion test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ describe('riviere', () => {
response: {},
body: readable,
res: writable,
onerror: () => {}
// Non-arrow so a binding regression would surface as wrong `this`
onerror: function() {}
};

const next = () => {};
Expand All @@ -133,4 +134,42 @@ describe('riviere', () => {
await end;
ctx.state.calculatedContentLength.should.equal(26);
});

it('should call ctx.onerror bound to ctx when streamed body errors', async function() {
const readable = new Readable({ read() {} });
const writable = new Writable({ write() {} });

const middleware = riviere({
logger: {
info: sandbox.spy()
}
});

const streamError = new Error('client disconnect');
const onerror = sandbox.spy(function(err) {
// Mimic Koa's Context#onerror which uses `this.app.emit(...)`
this.app.emit('error', err, this);
});

const ctx = {
state: {},
response: {},
body: readable,
res: writable,
app: {
emit: sandbox.spy()
},
onerror
};

await middleware(ctx, () => {});

ctx.body.emit('error', streamError);

onerror.calledOnce.should.equal(true);
onerror.calledWith(streamError).should.equal(true);
onerror.thisValues[0].should.equal(ctx);
ctx.app.emit.calledOnce.should.equal(true);
ctx.app.emit.calledWith('error', streamError, ctx).should.equal(true);
});
});
Loading