| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5961253 commit eded1e9
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -952,6 +952,17 @@ added: v12.3.0 | |||
| 952 | 952 | ||
| 953 | 953 | Getter for the property `objectMode` of a given `Writable` stream. | |
| 954 | 954 | ||
| 955 | + ##### `writable[Symbol.asyncDispose]()` | ||
| 956 | + | ||
| 957 | + <!-- YAML | ||
| 958 | + added: REPLACEME | ||
| 959 | + --> | ||
| 960 | + | ||
| 961 | + > Stability: 1 - Experimental | ||
| 962 | + | ||
| 963 | + Calls [`writable.destroy()`][writable-destroy] with an `AbortError` and returns | ||
| 964 | + a promise that fulfills when the stream is finished. | ||
| 965 | + | ||
| 955 | 966 | ##### `writable.write(chunk[, encoding][, callback])` | |
| 956 | 967 | ||
| 957 | 968 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,8 +32,10 @@ const { | |||
| 32 | 32 | ObjectDefineProperties, | |
| 33 | 33 | ObjectDefineProperty, | |
| 34 | 34 | ObjectSetPrototypeOf, | |
| 35 | + Promise, | ||
| 35 | 36 | StringPrototypeToLowerCase, | |
| 36 | 37 | Symbol, | |
| 38 | + SymbolAsyncDispose, | ||
| 37 | 39 | SymbolHasInstance, | |
| 38 | 40 | } = primordials; | |
| 39 | 41 | ||
@@ -44,6 +46,7 @@ const EE = require('events'); | |||
| 44 | 46 | const Stream = require('internal/streams/legacy').Stream; | |
| 45 | 47 | const { Buffer } = require('buffer'); | |
| 46 | 48 | const destroyImpl = require('internal/streams/destroy'); | |
| 49 | + const eos = require('internal/streams/end-of-stream'); | ||
| 47 | 50 | ||
| 48 | 51 | const { | |
| 49 | 52 | addAbortSignal, | |
@@ -54,16 +57,19 @@ const { | |||
| 54 | 57 | getDefaultHighWaterMark, | |
| 55 | 58 | } = require('internal/streams/state'); | |
| 56 | 59 | const { | |
| 57 | - ERR_INVALID_ARG_TYPE, | ||
| 58 | - ERR_METHOD_NOT_IMPLEMENTED, | ||
| 59 | - ERR_MULTIPLE_CALLBACK, | ||
| 60 | - ERR_STREAM_CANNOT_PIPE, | ||
| 61 | - ERR_STREAM_DESTROYED, | ||
| 62 | - ERR_STREAM_ALREADY_FINISHED, | ||
| 63 | - ERR_STREAM_NULL_VALUES, | ||
| 64 | - ERR_STREAM_WRITE_AFTER_END, | ||
| 65 | - ERR_UNKNOWN_ENCODING, | ||
| 66 | - } = require('internal/errors').codes; | ||
| 60 | + AbortError, | ||
| 61 | + codes: { | ||
| 62 | + ERR_INVALID_ARG_TYPE, | ||
| 63 | + ERR_METHOD_NOT_IMPLEMENTED, | ||
| 64 | + ERR_MULTIPLE_CALLBACK, | ||
| 65 | + ERR_STREAM_ALREADY_FINISHED, | ||
| 66 | + ERR_STREAM_CANNOT_PIPE, | ||
| 67 | + ERR_STREAM_DESTROYED, | ||
| 68 | + ERR_STREAM_NULL_VALUES, | ||
| 69 | + ERR_STREAM_WRITE_AFTER_END, | ||
| 70 | + ERR_UNKNOWN_ENCODING, | ||
| 71 | + }, | ||
| 72 | + } = require('internal/errors'); | ||
| 67 | 73 | const { | |
| 68 | 74 | kState, | |
| 69 | 75 | // bitfields | |
@@ -1142,3 +1148,14 @@ Writable.fromWeb = function(writableStream, options) { | |||
| 1142 | 1148 | Writable.toWeb = function(streamWritable) { | |
| 1143 | 1149 | return lazyWebStreams().newWritableStreamFromStreamWritable(streamWritable); | |
| 1144 | 1150 | }; | |
| 1151 | + | ||
| 1152 | + Writable.prototype[SymbolAsyncDispose] = function() { | ||
| 1153 | + let error; | ||
| 1154 | + if (!this.destroyed) { | ||
| 1155 | + error = this.writableFinished ? null : new AbortError(); | ||
| 1156 | + this.destroy(error); | ||
| 1157 | + } | ||
| 1158 | + return new Promise((resolve, reject) => | ||
| 1159 | + eos(this, (err) => (err && err.name !== 'AbortError' ? reject(err) : resolve(null))), | ||
| 1160 | + ); | ||
| 1161 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -269,3 +269,18 @@ const assert = require('assert'); | |||
| 269 | 269 | })); | |
| 270 | 270 | duplex.destroy(); | |
| 271 | 271 | } | |
| 272 | + | ||
| 273 | + { | ||
| 274 | + // Check Symbol.asyncDispose | ||
| 275 | + const duplex = new Duplex({ | ||
| 276 | + write(chunk, enc, cb) { cb(); }, | ||
| 277 | + read() {}, | ||
| 278 | + }); | ||
| 279 | + let count = 0; | ||
| 280 | + duplex.on('error', common.mustCall((e) => { | ||
| 281 | + assert.strictEqual(count++, 0); // Ensure not called twice | ||
| 282 | + assert.strictEqual(e.name, 'AbortError'); | ||
| 283 | + })); | ||
| 284 | + duplex.on('close', common.mustCall()); | ||
| 285 | + duplex[Symbol.asyncDispose]().then(common.mustCall()); | ||
| 286 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -141,3 +141,14 @@ const assert = require('assert'); | |||
| 141 | 141 | ||
| 142 | 142 | transform.destroy(); | |
| 143 | 143 | } | |
| 144 | + | ||
| 145 | + { | ||
| 146 | + const transform = new Transform({ | ||
| 147 | + transform(chunk, enc, cb) {} | ||
| 148 | + }); | ||
| 149 | + transform.on('error', common.mustCall((err) => { | ||
| 150 | + assert.strictEqual(err.name, 'AbortError'); | ||
| 151 | + })); | ||
| 152 | + transform.on('close', common.mustCall()); | ||
| 153 | + transform[Symbol.asyncDispose]().then(common.mustCall()); | ||
| 154 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -487,3 +487,15 @@ const assert = require('assert'); | |||
| 487 | 487 | })); | |
| 488 | 488 | s.destroy(_err); | |
| 489 | 489 | } | |
| 490 | + | ||
| 491 | + { | ||
| 492 | + const write = new Writable({ | ||
| 493 | + write(chunk, enc, cb) { cb(); } | ||
| 494 | + }); | ||
| 495 | + | ||
| 496 | + write.on('error', common.mustCall((e) => { | ||
| 497 | + assert.strictEqual(e.name, 'AbortError'); | ||
| 498 | + assert.strictEqual(write.destroyed, true); | ||
| 499 | + })); | ||
| 500 | + write[Symbol.asyncDispose]().then(common.mustCall()); | ||
| 501 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments