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

http: Make OutgoingMessage more streamlike #45672

Merged
merged 10 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/_http_outgoing.js
Expand Up @@ -88,6 +88,7 @@ const kCorked = Symbol('corked');
const kUniqueHeaders = Symbol('kUniqueHeaders');
const kBytesWritten = Symbol('kBytesWritten');
const kEndCalled = Symbol('kEndCalled');
const kErrored = Symbol('errored');

const nop = () => {};

Expand Down Expand Up @@ -147,10 +148,26 @@ function OutgoingMessage() {
this._keepAliveTimeout = 0;

this._onPendingData = nop;
ronag marked this conversation as resolved.
Show resolved Hide resolved

this[kErrored] = null;
}
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
ObjectSetPrototypeOf(OutgoingMessage, Stream);

ObjectDefineProperty(OutgoingMessage.prototype, 'errored', {
__proto__: null,
get() {
return this[kErrored];
}
ronag marked this conversation as resolved.
Show resolved Hide resolved
});

ObjectDefineProperty(OutgoingMessage.prototype, 'closed', {
__proto__: null,
get() {
return this._closed;
}
ronag marked this conversation as resolved.
Show resolved Hide resolved
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
__proto__: null,
get() {
Expand Down Expand Up @@ -320,6 +337,8 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
}
this.destroyed = true;

this[kErrored] = error;

if (this.socket) {
this.socket.destroy(error);
} else {
Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-http-client-incomingmessage-destroy.js
Expand Up @@ -20,6 +20,13 @@ server.listen(0, () => {
get({
port: server.address().port
}, common.mustCall((res) => {
res.destroy(new Error('Destroy test'));
const err = new Error('Destroy test')
ronag marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(res.errored, null);
res.destroy(err);
assert.strictEqual(res.closed, false);
assert.strictEqual(res.errored, err);
res.on('close', () => {
assert.strictEqual(res.closed, true);
})
ronag marked this conversation as resolved.
Show resolved Hide resolved
}));
});
71 changes: 51 additions & 20 deletions test/parallel/test-http-outgoing-destroyed.js
@@ -1,24 +1,55 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');

const server = http.createServer(common.mustCall((req, res) => {
req.pipe(res);
res.on('error', common.mustNotCall());
res.on('close', common.mustCall(() => {
res.end('asd');
process.nextTick(() => {
server.close();
});
}));
})).listen(0, () => {
http
.request({
port: server.address().port,
method: 'PUT'
})
.on('response', (res) => {
res.destroy();
})
.write('asd');
});
{
const server = http.createServer(common.mustCall((req, res) => {
assert.strictEqual(res.closed, false);
req.pipe(res);
res.on('error', common.mustNotCall());
res.on('close', common.mustCall(() => {
assert.strictEqual(res.closed, true);
res.end('asd');
process.nextTick(() => {
server.close();
});
}));
})).listen(0, () => {
http
.request({
port: server.address().port,
method: 'PUT'
})
.on('response', (res) => {
res.destroy();
})
.write('asd');
});
}

{
const server = http.createServer(common.mustCall((req, res) => {
assert.strictEqual(res.closed, true);
req.pipe(res);
res.on('error', common.mustNotCall());
res.on('close', common.mustCall(() => {
assert.strictEqual(res.closed, true);
process.nextTick(() => {
server.close();
});
}));
const err = new Error('Destroy test');
res.destroy(err);
assert.strictEqual(res.errored, err);
})).listen(0, () => {
http
.request({
port: server.address().port,
method: 'PUT'
})
.on('error', common.mustCall())
.write('asd');
});

}