| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 95d6ad6 commit aa32e13
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -299,9 +299,13 @@ Writable.prototype.write = function(chunk, encoding, cb) { | |||
| 299 | 299 | if (typeof cb !== 'function') | |
| 300 | 300 | cb = nop; | |
| 301 | 301 | ||
| 302 | - if (state.ending) | ||
| 302 | + if (state.ending) { | ||
| 303 | 303 | writeAfterEnd(this, cb); | |
| 304 | - else if (isBuf || validChunk(this, state, chunk, cb)) { | ||
| 304 | + } else if (state.destroyed) { | ||
| 305 | + const err = new ERR_STREAM_DESTROYED('write'); | ||
| 306 | + process.nextTick(cb, err); | ||
| 307 | + errorOrDestroy(this, err); | ||
| 308 | + } else if (isBuf || validChunk(this, state, chunk, cb)) { | ||
| 305 | 309 | state.pendingcb++; | |
| 306 | 310 | ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); | |
| 307 | 311 | } | |
@@ -733,7 +737,21 @@ Object.defineProperty(Writable.prototype, 'writableFinished', { | |||
| 733 | 737 | } | |
| 734 | 738 | }); | |
| 735 | 739 | ||
| 736 | - Writable.prototype.destroy = destroyImpl.destroy; | ||
| 740 | + const destroy = destroyImpl.destroy; | ||
| 741 | + Writable.prototype.destroy = function(err, cb) { | ||
| 742 | + const state = this._writableState; | ||
| 743 | + if (!state.destroyed) { | ||
| 744 | + for (let entry = state.bufferedRequest; entry; entry = entry.next) { | ||
| 745 | + process.nextTick(entry.callback, new ERR_STREAM_DESTROYED('write')); | ||
| 746 | + } | ||
| 747 | + state.bufferedRequest = null; | ||
| 748 | + state.lastBufferedRequest = null; | ||
| 749 | + state.bufferedRequestCount = 0; | ||
| 750 | + } | ||
| 751 | + destroy.call(this, err, cb); | ||
| 752 | + return this; | ||
| 753 | + }; | ||
| 754 | + | ||
| 737 | 755 | Writable.prototype._undestroy = destroyImpl.undestroy; | |
| 738 | 756 | Writable.prototype._destroy = function(err, cb) { | |
| 739 | 757 | cb(err); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,7 +39,11 @@ server.on('stream', common.mustCall((stream) => { | |||
| 39 | 39 | code: 'ERR_STREAM_WRITE_AFTER_END', | |
| 40 | 40 | message: 'write after end' | |
| 41 | 41 | })); | |
| 42 | - assert.strictEqual(stream.write('data'), false); | ||
| 42 | + assert.strictEqual(stream.write('data', common.expectsError({ | ||
| 43 | + type: Error, | ||
| 44 | + code: 'ERR_STREAM_WRITE_AFTER_END', | ||
| 45 | + message: 'write after end' | ||
| 46 | + })), false); | ||
| 43 | 47 | })); | |
| 44 | 48 | ||
| 45 | 49 | server.listen(0, common.mustCall(() => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -232,3 +232,49 @@ const assert = require('assert'); | |||
| 232 | 232 | write._undestroy(); | |
| 233 | 233 | write.end(); | |
| 234 | 234 | } | |
| 235 | + | ||
| 236 | + { | ||
| 237 | + const write = new Writable(); | ||
| 238 | + | ||
| 239 | + write.destroy(); | ||
| 240 | + write.on('error', common.expectsError({ | ||
| 241 | + type: Error, | ||
| 242 | + code: 'ERR_STREAM_DESTROYED', | ||
| 243 | + message: 'Cannot call write after a stream was destroyed' | ||
| 244 | + })); | ||
| 245 | + write.write('asd', common.expectsError({ | ||
| 246 | + type: Error, | ||
| 247 | + code: 'ERR_STREAM_DESTROYED', | ||
| 248 | + message: 'Cannot call write after a stream was destroyed' | ||
| 249 | + })); | ||
| 250 | + } | ||
| 251 | + | ||
| 252 | + { | ||
| 253 | + const write = new Writable({ | ||
| 254 | + write(chunk, enc, cb) { cb(); } | ||
| 255 | + }); | ||
| 256 | + | ||
| 257 | + write.on('error', common.expectsError({ | ||
| 258 | + type: Error, | ||
| 259 | + code: 'ERR_STREAM_DESTROYED', | ||
| 260 | + message: 'Cannot call write after a stream was destroyed' | ||
| 261 | + })); | ||
| 262 | + | ||
| 263 | + write.cork(); | ||
| 264 | + write.write('asd', common.mustCall()); | ||
| 265 | + write.uncork(); | ||
| 266 | + | ||
| 267 | + write.cork(); | ||
| 268 | + write.write('asd', common.expectsError({ | ||
| 269 | + type: Error, | ||
| 270 | + code: 'ERR_STREAM_DESTROYED', | ||
| 271 | + message: 'Cannot call write after a stream was destroyed' | ||
| 272 | + })); | ||
| 273 | + write.destroy(); | ||
| 274 | + write.write('asd', common.expectsError({ | ||
| 275 | + type: Error, | ||
| 276 | + code: 'ERR_STREAM_DESTROYED', | ||
| 277 | + message: 'Cannot call write after a stream was destroyed' | ||
| 278 | + })); | ||
| 279 | + write.uncork(); | ||
| 280 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,16 @@ for (const withPendingData of [ false, true ]) { | |||
| 24 | 24 | w.on('drain', () => drains++); | |
| 25 | 25 | w.on('finish', () => finished = true); | |
| 26 | 26 | ||
| 27 | - w.write('abc', () => chunksWritten++); | ||
| 27 | + function onWrite(err) { | ||
| 28 | + if (err) { | ||
| 29 | + assert.strictEqual(w.destroyed, true); | ||
| 30 | + assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED'); | ||
| 31 | + } else { | ||
| 32 | + chunksWritten++; | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + w.write('abc', onWrite); | ||
| 28 | 37 | assert.strictEqual(chunksWritten, 0); | |
| 29 | 38 | assert.strictEqual(drains, 0); | |
| 30 | 39 | callbacks.shift()(); | |
@@ -34,14 +43,14 @@ for (const withPendingData of [ false, true ]) { | |||
| 34 | 43 | if (withPendingData) { | |
| 35 | 44 | // Test 2 cases: There either is or is not data still in the write queue. | |
| 36 | 45 | // (The second write will never actually get executed either way.) | |
| 37 | - w.write('def', () => chunksWritten++); | ||
| 46 | + w.write('def', onWrite); | ||
| 38 | 47 | } | |
| 39 | 48 | if (useEnd) { | |
| 40 | 49 | // Again, test 2 cases: Either we indicate that we want to end the | |
| 41 | 50 | // writable or not. | |
| 42 | - w.end('ghi', () => chunksWritten++); | ||
| 51 | + w.end('ghi', onWrite); | ||
| 43 | 52 | } else { | |
| 44 | - w.write('ghi', () => chunksWritten++); | ||
| 53 | + w.write('ghi', onWrite); | ||
| 45 | 54 | } | |
| 46 | 55 | ||
| 47 | 56 | assert.strictEqual(chunksWritten, 1); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments