Skip to content

Commit

Permalink
Merge d604c47 into 8d3cf2b
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizarth committed Apr 3, 2020
2 parents 8d3cf2b + d604c47 commit 97e06d4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/http-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ HttpContext.prototype.createStream = function() {
// since the method returns a ReadableStream
// setup an output to pipe the ReadableStream to
mdm.pipe(res);
res.on('close', function() {
mdm.destroy();
});
}
}
};
Expand Down Expand Up @@ -540,6 +543,9 @@ HttpContext.prototype.done = function(cb) {

out.end();
});
out.on('close', function() {
stream.destroy();
});
// TODO(ritch) support multi-part streams
} else {
cb(new Error(g.f('unsupported stream type: %s', returnStreamDesc.type)));
Expand Down
54 changes: 54 additions & 0 deletions test/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const express = require('express');
const request = require('supertest');
const fs = require('fs');
const es = require('event-stream');
const http = require('http');
const EventSource = require('eventsource');

describe('strong-remoting', function() {
Expand Down Expand Up @@ -180,6 +181,59 @@ describe('a function returning a ReadableStream', function() {
});
});

describe('an http client requesting a stream as JSON', function() {
let server, port;
before(function startTheApp(done) {
server = app.listen(() => {
port = server.address().port;
done();
});
});

after(function stopTheApp() {
server.close();
});

it('should close server stream on client disconnect', function(done) {
const req = http.request({
port,
path: '/StreamClass/createInfiniteStream',
timeout: 100,
headers: {
// Content-type is important! When the client accepts
// `text/event-stream` then all works well.
// In this test, we verify what happens when the client accepts
// only `application/json`.
accept: 'application/json',
},
});

req
.on('error', done)
.on('response', res => {
req.abort();

const timeout = 200;
const timer = setTimeout(() => {
// The stream was kept open, that's a bug
const msg = `Server stream was not closed within ${timeout}ms ` +
'after the connection was closed.';
done(new Error(msg));
// Prevent double call of the callback
done = undefined;
}, timeout);

streamClosed.then(() => {
if (!done) return;
// The event stream was properly closed, the test has passed.
clearTimeout(timer);
done();
});
})
.end();
});
});

describe('an http client requesting a stream as an event source', function() {
before(function(done) {
const server = this.server = app.listen(done);
Expand Down

0 comments on commit 97e06d4

Please sign in to comment.