GeezerVison is showing github.com — http: make `OutgoingMessage` more streamlike · nodejs/node@cbd710b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_http_outgoing.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,6 +88,7 @@ const kCorked = Symbol('corked'); | 88 | 88 | const kUniqueHeaders = Symbol('kUniqueHeaders'); | 89 | 89 | const kBytesWritten = Symbol('kBytesWritten'); | 90 | 90 | const kEndCalled = Symbol('kEndCalled'); | | 91 | +const kErrored = Symbol('errored'); | 91 | 92 |
| 92 | 93 | const nop = () => {}; | 93 | 94 |
| @@ -147,10 +148,26 @@ function OutgoingMessage() { | 147 | 148 | this._keepAliveTimeout = 0; | 148 | 149 |
| 149 | 150 | this._onPendingData = nop; | | 151 | + | | 152 | + this[kErrored] = null; | 150 | 153 | } | 151 | 154 | ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype); | 152 | 155 | ObjectSetPrototypeOf(OutgoingMessage, Stream); | 153 | 156 |
| | 157 | +ObjectDefineProperty(OutgoingMessage.prototype, 'errored', { | | 158 | + __proto__: null, | | 159 | + get() { | | 160 | + return this[kErrored]; | | 161 | + }, | | 162 | +}); | | 163 | + | | 164 | +ObjectDefineProperty(OutgoingMessage.prototype, 'closed', { | | 165 | + __proto__: null, | | 166 | + get() { | | 167 | + return this._closed; | | 168 | + }, | | 169 | +}); | | 170 | + | 154 | 171 | ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', { | 155 | 172 | __proto__: null, | 156 | 173 | get() { | @@ -320,6 +337,8 @@ OutgoingMessage.prototype.destroy = function destroy(error) { | 320 | 337 | } | 321 | 338 | this.destroyed = true; | 322 | 339 |
| | 340 | + this[kErrored] = error; | | 341 | + | 323 | 342 | if (this.socket) { | 324 | 343 | this.socket.destroy(error); | 325 | 344 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http-client-incomingmessage-destroy.js +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,13 @@ server.listen(0, () => { | 20 | 20 | get({ | 21 | 21 | port: server.address().port | 22 | 22 | }, common.mustCall((res) => { | 23 | | - res.destroy(new Error('Destroy test')); | | 23 | + const err = new Error('Destroy test'); | | 24 | + assert.strictEqual(res.errored, null); | | 25 | + res.destroy(err); | | 26 | + assert.strictEqual(res.closed, false); | | 27 | + assert.strictEqual(res.errored, err); | | 28 | + res.on('close', () => { | | 29 | + assert.strictEqual(res.closed, true); | | 30 | + }); | 24 | 31 | })); | 25 | 32 | }); |
Collapse file Copy file name to clipboard +51-20Lines changed: 51 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | const common = require('../common'); | 3 | 3 | const http = require('http'); | | 4 | +const assert = require('assert'); | 4 | 5 |
| 5 | | -const server = http.createServer(common.mustCall((req, res) => { | 6 | | - req.pipe(res); | 7 | | - res.on('error', common.mustNotCall()); | 8 | | - res.on('close', common.mustCall(() => { | 9 | | - res.end('asd'); | 10 | | - process.nextTick(() => { | 11 | | - server.close(); | 12 | | - }); | 13 | | - })); | 14 | | -})).listen(0, () => { | 15 | | - http | 16 | | - .request({ | 17 | | - port: server.address().port, | 18 | | - method: 'PUT' | 19 | | - }) | 20 | | - .on('response', (res) => { | 21 | | - res.destroy(); | 22 | | - }) | 23 | | - .write('asd'); | 24 | | -}); | | 6 | +{ | | 7 | + const server = http.createServer(common.mustCall((req, res) => { | | 8 | + assert.strictEqual(res.closed, false); | | 9 | + req.pipe(res); | | 10 | + res.on('error', common.mustNotCall()); | | 11 | + res.on('close', common.mustCall(() => { | | 12 | + assert.strictEqual(res.closed, true); | | 13 | + res.end('asd'); | | 14 | + process.nextTick(() => { | | 15 | + server.close(); | | 16 | + }); | | 17 | + })); | | 18 | + })).listen(0, () => { | | 19 | + http | | 20 | + .request({ | | 21 | + port: server.address().port, | | 22 | + method: 'PUT' | | 23 | + }) | | 24 | + .on('response', (res) => { | | 25 | + res.destroy(); | | 26 | + }) | | 27 | + .write('asd'); | | 28 | + }); | | 29 | +} | | 30 | + | | 31 | +{ | | 32 | + const server = http.createServer(common.mustCall((req, res) => { | | 33 | + assert.strictEqual(res.closed, false); | | 34 | + req.pipe(res); | | 35 | + res.on('error', common.mustNotCall()); | | 36 | + res.on('close', common.mustCall(() => { | | 37 | + assert.strictEqual(res.closed, true); | | 38 | + process.nextTick(() => { | | 39 | + server.close(); | | 40 | + }); | | 41 | + })); | | 42 | + const err = new Error('Destroy test'); | | 43 | + res.destroy(err); | | 44 | + assert.strictEqual(res.errored, err); | | 45 | + })).listen(0, () => { | | 46 | + http | | 47 | + .request({ | | 48 | + port: server.address().port, | | 49 | + method: 'PUT' | | 50 | + }) | | 51 | + .on('error', common.mustCall()) | | 52 | + .write('asd'); | | 53 | + }); | | 54 | + | | 55 | +} |
|