| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 28ed7d0 commit b7aa5e2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,8 +5,9 @@ | |||
| 5 | 5 | ||
| 6 | 6 | const { | |
| 7 | 7 | ArrayIsArray, | |
| 8 | + ReflectApply, | ||
| 8 | 9 | SymbolAsyncIterator, | |
| 9 | - SymbolIterator | ||
| 10 | + SymbolIterator, | ||
| 10 | 11 | } = primordials; | |
| 11 | 12 | ||
| 12 | 13 | let eos; | |
@@ -77,10 +78,6 @@ function popCallback(streams) { | |||
| 77 | 78 | return streams.pop(); | |
| 78 | 79 | } | |
| 79 | 80 | ||
| 80 | - function isPromise(obj) { | ||
| 81 | - return !!(obj && typeof obj.then === 'function'); | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | 81 | function isReadable(obj) { | |
| 85 | 82 | return !!(obj && typeof obj.pipe === 'function'); | |
| 86 | 83 | } | |
@@ -222,14 +219,19 @@ function pipeline(...streams) { | |||
| 222 | 219 | const pt = new PassThrough({ | |
| 223 | 220 | objectMode: true | |
| 224 | 221 | }); | |
| 225 | - if (isPromise(ret)) { | ||
| 226 | - ret | ||
| 227 | - .then((val) => { | ||
| 222 | + | ||
| 223 | + // Handle Promises/A+ spec, `then` could be a getter that throws on | ||
| 224 | + // second use. | ||
| 225 | + const then = ret?.then; | ||
| 226 | + if (typeof then === 'function') { | ||
| 227 | + ReflectApply(then, ret, [ | ||
| 228 | + (val) => { | ||
| 228 | 229 | value = val; | |
| 229 | 230 | pt.end(val); | |
| 230 | 231 | }, (err) => { | |
| 231 | 232 | pt.destroy(err); | |
| 232 | - }); | ||
| 233 | + } | ||
| 234 | + ]); | ||
| 233 | 235 | } else if (isIterable(ret, true)) { | |
| 234 | 236 | finishCount++; | |
| 235 | 237 | pump(ret, pt, finish); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1240,3 +1240,24 @@ const net = require('net'); | |||
| 1240 | 1240 | }), | |
| 1241 | 1241 | ); | |
| 1242 | 1242 | } | |
| 1243 | + { | ||
| 1244 | + function createThenable() { | ||
| 1245 | + let counter = 0; | ||
| 1246 | + return { | ||
| 1247 | + get then() { | ||
| 1248 | + if (counter++) { | ||
| 1249 | + throw new Error('Cannot access `then` more than once'); | ||
| 1250 | + } | ||
| 1251 | + return Function.prototype; | ||
| 1252 | + }, | ||
| 1253 | + }; | ||
| 1254 | + } | ||
| 1255 | + | ||
| 1256 | + pipeline( | ||
| 1257 | + function* () { | ||
| 1258 | + yield 0; | ||
| 1259 | + }, | ||
| 1260 | + createThenable, | ||
| 1261 | + () => common.mustNotCall(), | ||
| 1262 | + ); | ||
| 1263 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments