| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 254398a commit 20a2a29
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,6 +139,9 @@ function ReadableState(options, stream, isDuplex) { | |||
| 139 | 139 | // Has it been destroyed | |
| 140 | 140 | this.destroyed = false; | |
| 141 | 141 | ||
| 142 | + // Indicates whether the stream has errored. | ||
| 143 | + this.errored = false; | ||
| 144 | + | ||
| 142 | 145 | // Crypto is kind of old and crusty. Historically, its default string | |
| 143 | 146 | // encoding is 'binary' so we have to make this configurable. | |
| 144 | 147 | // Everything else in the universe uses 'utf8', though. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,41 +1,25 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - function needError(stream, err) { | ||
| 4 | - if (!err) { | ||
| 5 | - return false; | ||
| 6 | - } | ||
| 7 | - | ||
| 8 | - const r = stream._readableState; | ||
| 9 | - const w = stream._writableState; | ||
| 10 | - | ||
| 11 | - if ((w && w.errorEmitted) || (r && r.errorEmitted)) { | ||
| 12 | - return false; | ||
| 13 | - } | ||
| 14 | - | ||
| 15 | - if (w) { | ||
| 16 | - w.errorEmitted = true; | ||
| 17 | - } | ||
| 18 | - if (r) { | ||
| 19 | - r.errorEmitted = true; | ||
| 20 | - } | ||
| 21 | - | ||
| 22 | - return true; | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | 3 | // Undocumented cb() API, needed for core, not for public API. | |
| 26 | 4 | // The cb() will be invoked synchronously if _destroy is synchronous. | |
| 5 | + // If cb is passed no 'error' event will be emitted. | ||
| 27 | 6 | function destroy(err, cb) { | |
| 28 | 7 | const r = this._readableState; | |
| 29 | 8 | const w = this._writableState; | |
| 30 | 9 | ||
| 31 | - if (w && err) { | ||
| 32 | - w.errored = true; | ||
| 10 | + if (err) { | ||
| 11 | + if (w) { | ||
| 12 | + w.errored = true; | ||
| 13 | + } | ||
| 14 | + if (r) { | ||
| 15 | + r.errored = true; | ||
| 16 | + } | ||
| 33 | 17 | } | |
| 34 | 18 | ||
| 35 | 19 | if ((w && w.destroyed) || (r && r.destroyed)) { | |
| 36 | 20 | if (cb) { | |
| 37 | 21 | cb(err); | |
| 38 | - } else if (needError(this, err)) { | ||
| 22 | + } else if (err) { | ||
| 39 | 23 | process.nextTick(emitErrorNT, this, err); | |
| 40 | 24 | } | |
| 41 | 25 | ||
@@ -53,17 +37,24 @@ function destroy(err, cb) { | |||
| 53 | 37 | } | |
| 54 | 38 | ||
| 55 | 39 | this._destroy(err || null, (err) => { | |
| 56 | - const emitClose = (w && w.emitClose) || (r && r.emitClose); | ||
| 40 | + if (err) { | ||
| 41 | + if (w) { | ||
| 42 | + w.errored = true; | ||
| 43 | + } | ||
| 44 | + if (r) { | ||
| 45 | + r.errored = true; | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + | ||
| 57 | 49 | if (cb) { | |
| 58 | 50 | // Invoke callback before scheduling emitClose so that callback | |
| 59 | 51 | // can schedule before. | |
| 60 | 52 | cb(err); | |
| 61 | - if (emitClose) { | ||
| 62 | - process.nextTick(emitCloseNT, this); | ||
| 63 | - } | ||
| 64 | - } else if (needError(this, err)) { | ||
| 65 | - process.nextTick(emitClose ? emitErrorCloseNT : emitErrorNT, this, err); | ||
| 66 | - } else if (emitClose) { | ||
| 53 | + // Don't emit 'error' if passed a callback. | ||
| 54 | + process.nextTick(emitCloseNT, this); | ||
| 55 | + } else if (err) { | ||
| 56 | + process.nextTick(emitErrorCloseNT, this, err); | ||
| 57 | + } else { | ||
| 67 | 58 | process.nextTick(emitCloseNT, this); | |
| 68 | 59 | } | |
| 69 | 60 | }); | |
@@ -72,15 +63,34 @@ function destroy(err, cb) { | |||
| 72 | 63 | } | |
| 73 | 64 | ||
| 74 | 65 | function emitErrorCloseNT(self, err) { | |
| 75 | - self.emit('error', err); | ||
| 76 | - self.emit('close'); | ||
| 66 | + emitErrorNT(self, err); | ||
| 67 | + emitCloseNT(self); | ||
| 77 | 68 | } | |
| 78 | 69 | ||
| 79 | 70 | function emitCloseNT(self) { | |
| 80 | - self.emit('close'); | ||
| 71 | + const r = self._readableState; | ||
| 72 | + const w = self._writableState; | ||
| 73 | + | ||
| 74 | + if ((w && w.emitClose) || (r && r.emitClose)) { | ||
| 75 | + self.emit('close'); | ||
| 76 | + } | ||
| 81 | 77 | } | |
| 82 | 78 | ||
| 83 | 79 | function emitErrorNT(self, err) { | |
| 80 | + const r = self._readableState; | ||
| 81 | + const w = self._writableState; | ||
| 82 | + | ||
| 83 | + if ((w && w.errorEmitted) || (r && r.errorEmitted)) { | ||
| 84 | + return; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + if (w) { | ||
| 88 | + w.errorEmitted = true; | ||
| 89 | + } | ||
| 90 | + if (r) { | ||
| 91 | + r.errorEmitted = true; | ||
| 92 | + } | ||
| 93 | + | ||
| 84 | 94 | self.emit('error', err); | |
| 85 | 95 | } | |
| 86 | 96 | ||
@@ -90,6 +100,7 @@ function undestroy() { | |||
| 90 | 100 | ||
| 91 | 101 | if (r) { | |
| 92 | 102 | r.destroyed = false; | |
| 103 | + w.errored = false; | ||
| 93 | 104 | r.reading = false; | |
| 94 | 105 | r.ended = false; | |
| 95 | 106 | r.endEmitted = false; | |
@@ -118,14 +129,17 @@ function errorOrDestroy(stream, err) { | |||
| 118 | 129 | const r = stream._readableState; | |
| 119 | 130 | const w = stream._writableState; | |
| 120 | 131 | ||
| 121 | - if (w & err) { | ||
| 122 | - w.errored = true; | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | 132 | if ((r && r.autoDestroy) || (w && w.autoDestroy)) | |
| 126 | 133 | stream.destroy(err); | |
| 127 | - else if (needError(stream, err)) | ||
| 128 | - stream.emit('error', err); | ||
| 134 | + else if (err) { | ||
| 135 | + if (w) { | ||
| 136 | + w.errored = true; | ||
| 137 | + } | ||
| 138 | + if (r) { | ||
| 139 | + r.errored = true; | ||
| 140 | + } | ||
| 141 | + emitErrorNT(stream, err); | ||
| 142 | + } | ||
| 129 | 143 | } | |
| 130 | 144 | ||
| 131 | 145 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -129,13 +129,20 @@ const assert = require('assert'); | |||
| 129 | 129 | cb(expected); | |
| 130 | 130 | }); | |
| 131 | 131 | ||
| 132 | + let ticked = false; | ||
| 132 | 133 | read.on('end', common.mustNotCall('no end event')); | |
| 133 | 134 | read.on('error', common.mustCall((err) => { | |
| 135 | + assert.strictEqual(ticked, true); | ||
| 136 | + assert.strictEqual(read._readableState.errorEmitted, true); | ||
| 137 | + assert.strictEqual(read._readableState.errored, true); | ||
| 134 | 138 | assert.strictEqual(err, expected); | |
| 135 | 139 | })); | |
| 136 | 140 | ||
| 137 | 141 | read.destroy(); | |
| 142 | + assert.strictEqual(read._readableState.errorEmitted, false); | ||
| 143 | + assert.strictEqual(read._readableState.errored, true); | ||
| 138 | 144 | assert.strictEqual(read.destroyed, true); | |
| 145 | + ticked = true; | ||
| 139 | 146 | } | |
| 140 | 147 | ||
| 141 | 148 | { | |
@@ -174,10 +181,58 @@ const assert = require('assert'); | |||
| 174 | 181 | ||
| 175 | 182 | const expected = new Error('kaboom'); | |
| 176 | 183 | ||
| 177 | - read.on('close', common.mustCall()); | ||
| 184 | + let ticked = false; | ||
| 185 | + read.on('close', common.mustCall(() => { | ||
| 186 | + assert.strictEqual(read._readableState.errorEmitted, false); | ||
| 187 | + assert.strictEqual(ticked, true); | ||
| 188 | + })); | ||
| 189 | + // 'error' should not be emitted since a callback is passed to | ||
| 190 | + // destroy(err, callback); | ||
| 191 | + read.on('error', common.mustNotCall()); | ||
| 192 | + | ||
| 193 | + assert.strictEqual(read._readableState.errored, false); | ||
| 194 | + assert.strictEqual(read._readableState.errorEmitted, false); | ||
| 195 | + | ||
| 178 | 196 | read.destroy(expected, common.mustCall(function(err) { | |
| 197 | + assert.strictEqual(read._readableState.errored, true); | ||
| 179 | 198 | assert.strictEqual(err, expected); | |
| 180 | 199 | })); | |
| 200 | + assert.strictEqual(read._readableState.errorEmitted, false); | ||
| 201 | + assert.strictEqual(read._readableState.errored, true); | ||
| 202 | + ticked = true; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + { | ||
| 206 | + const readable = new Readable({ | ||
| 207 | + destroy: common.mustCall(function(err, cb) { | ||
| 208 | + process.nextTick(cb, new Error('kaboom 1')); | ||
| 209 | + }), | ||
| 210 | + read() {} | ||
| 211 | + }); | ||
| 212 | + | ||
| 213 | + let ticked = false; | ||
| 214 | + readable.on('close', common.mustCall(() => { | ||
| 215 | + assert.strictEqual(ticked, true); | ||
| 216 | + assert.strictEqual(readable._readableState.errorEmitted, true); | ||
| 217 | + })); | ||
| 218 | + readable.on('error', common.mustCall((err) => { | ||
| 219 | + assert.strictEqual(ticked, true); | ||
| 220 | + assert.strictEqual(err.message, 'kaboom 2'); | ||
| 221 | + assert.strictEqual(readable._readableState.errorEmitted, true); | ||
| 222 | + })); | ||
| 223 | + | ||
| 224 | + readable.destroy(); | ||
| 225 | + assert.strictEqual(readable.destroyed, true); | ||
| 226 | + assert.strictEqual(readable._readableState.errored, false); | ||
| 227 | + assert.strictEqual(readable._readableState.errorEmitted, false); | ||
| 228 | + | ||
| 229 | + // Test case where `readable.destroy()` is called again with an error before | ||
| 230 | + // the `_destroy()` callback is called. | ||
| 231 | + readable.destroy(new Error('kaboom 2')); | ||
| 232 | + assert.strictEqual(readable._readableState.errorEmitted, false); | ||
| 233 | + assert.strictEqual(readable._readableState.errored, true); | ||
| 234 | + | ||
| 235 | + ticked = true; | ||
| 181 | 236 | } | |
| 182 | 237 | ||
| 183 | 238 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -157,13 +157,22 @@ const assert = require('assert'); | |||
| 157 | 157 | write(chunk, enc, cb) { cb(); } | |
| 158 | 158 | }); | |
| 159 | 159 | ||
| 160 | - write.on('close', common.mustCall()); | ||
| 161 | - write.on('error', common.mustCall()); | ||
| 160 | + let ticked = false; | ||
| 161 | + write.on('close', common.mustCall(() => { | ||
| 162 | + assert.strictEqual(ticked, true); | ||
| 163 | + })); | ||
| 164 | + write.on('error', common.mustCall((err) => { | ||
| 165 | + assert.strictEqual(ticked, true); | ||
| 166 | + assert.strictEqual(err.message, 'kaboom 1'); | ||
| 167 | + assert.strictEqual(write._writableState.errorEmitted, true); | ||
| 168 | + })); | ||
| 162 | 169 | ||
| 163 | 170 | write.destroy(new Error('kaboom 1')); | |
| 164 | 171 | write.destroy(new Error('kaboom 2')); | |
| 165 | - assert.strictEqual(write._writableState.errorEmitted, true); | ||
| 172 | + assert.strictEqual(write._writableState.errored, true); | ||
| 173 | + assert.strictEqual(write._writableState.errorEmitted, false); | ||
| 166 | 174 | assert.strictEqual(write.destroyed, true); | |
| 175 | + ticked = true; | ||
| 167 | 176 | } | |
| 168 | 177 | ||
| 169 | 178 | { | |
@@ -176,20 +185,29 @@ const assert = require('assert'); | |||
| 176 | 185 | } | |
| 177 | 186 | }); | |
| 178 | 187 | ||
| 179 | - writable.on('close', common.mustCall()); | ||
| 180 | - writable.on('error', common.expectsError({ | ||
| 181 | - type: Error, | ||
| 182 | - message: 'kaboom 2' | ||
| 188 | + let ticked = false; | ||
| 189 | + writable.on('close', common.mustCall(() => { | ||
| 190 | + assert.strictEqual(ticked, true); | ||
| 191 | + assert.strictEqual(writable._writableState.errorEmitted, true); | ||
| 192 | + })); | ||
| 193 | + writable.on('error', common.mustCall((err) => { | ||
| 194 | + assert.strictEqual(ticked, true); | ||
| 195 | + assert.strictEqual(err.message, 'kaboom 2'); | ||
| 196 | + assert.strictEqual(writable._writableState.errorEmitted, true); | ||
| 183 | 197 | })); | |
| 184 | 198 | ||
| 185 | 199 | writable.destroy(); | |
| 186 | 200 | assert.strictEqual(writable.destroyed, true); | |
| 201 | + assert.strictEqual(writable._writableState.errored, false); | ||
| 187 | 202 | assert.strictEqual(writable._writableState.errorEmitted, false); | |
| 188 | 203 | ||
| 189 | 204 | // Test case where `writable.destroy()` is called again with an error before | |
| 190 | 205 | // the `_destroy()` callback is called. | |
| 191 | 206 | writable.destroy(new Error('kaboom 2')); | |
| 192 | - assert.strictEqual(writable._writableState.errorEmitted, true); | ||
| 207 | + assert.strictEqual(writable._writableState.errorEmitted, false); | ||
| 208 | + assert.strictEqual(writable._writableState.errored, true); | ||
| 209 | + | ||
| 210 | + ticked = true; | ||
| 193 | 211 | } | |
| 194 | 212 | ||
| 195 | 213 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments