Created
August 3, 2021 22:19
-
-
Save isaacs/6adfaf0ea3d9df4fef07e9ac5700bf74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/index.js b/index.js | |
index 56cbd66..b88f64b 100644 | |
--- a/index.js | |
+++ b/index.js | |
@@ -8,6 +8,7 @@ const EOF = Symbol('EOF') | |
const MAYBE_EMIT_END = Symbol('maybeEmitEnd') | |
const EMITTED_END = Symbol('emittedEnd') | |
const EMITTING_END = Symbol('emittingEnd') | |
+const EMITTED_ERROR = Symbol('emittedError') | |
const CLOSED = Symbol('closed') | |
const READ = Symbol('read') | |
const FLUSH = Symbol('flush') | |
@@ -66,6 +67,7 @@ module.exports = class Minipass extends Stream { | |
this[EMITTED_END] = false | |
this[EMITTING_END] = false | |
this[CLOSED] = false | |
+ this[EMITTED_ERROR] = null | |
this.writable = true | |
this.readable = true | |
this[BUFFERLENGTH] = 0 | |
@@ -339,6 +341,8 @@ module.exports = class Minipass extends Stream { | |
else if (isEndish(ev) && this[EMITTED_END]) { | |
super.emit(ev) | |
this.removeAllListeners(ev) | |
+ } else if (ev === 'error' && this[EMITTED_ERROR]) { | |
+ fn.call(this, this[EMITTED_ERROR]) | |
} | |
} | |
} | |
@@ -400,6 +404,9 @@ module.exports = class Minipass extends Stream { | |
// don't emit close before 'end' and 'finish' | |
if (!this[EMITTED_END] && !this[DESTROYED]) | |
return | |
+ } else if (ev === 'error') { | |
+ this[EMITTED_ERROR] = data | |
+ return super.emit(ev, data) | |
} | |
// TODO: replace with a spread operator when Node v4 support drops | |
@@ -452,8 +459,8 @@ module.exports = class Minipass extends Stream { | |
promise () { | |
return new Promise((resolve, reject) => { | |
this.on(DESTROYED, () => reject(new Error('stream destroyed'))) | |
- this.on('end', () => resolve()) | |
this.on('error', er => reject(er)) | |
+ this.on('end', () => resolve()) | |
}) | |
} | |
diff --git a/test/error-before-promise.js b/test/error-before-promise.js | |
new file mode 100644 | |
index 0000000..cd411fd | |
--- /dev/null | |
+++ b/test/error-before-promise.js | |
@@ -0,0 +1,11 @@ | |
+const MP = require('../') | |
+const t = require('tap') | |
+ | |
+t.test('emit an error before calling stream.promise()', t => { | |
+ const mp = new MP() | |
+ const poop = new Error('poop') | |
+ mp.once('error', er => t.equal(er, poop)) | |
+ mp.emit('error', poop) | |
+ mp.end() | |
+ return t.rejects(mp.promise(), poop) | |
+}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment