| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 59fca4e commit 3a8586b
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -813,6 +813,16 @@ On Linux, positional writes don't work when the file is opened in append mode. | |||
| 813 | 813 | The kernel ignores the position argument and always appends the data to | |
| 814 | 814 | the end of the file. | |
| 815 | 815 | ||
| 816 | + #### `filehandle[Symbol.asyncDispose]()` | ||
| 817 | + | ||
| 818 | + <!-- YAML | ||
| 819 | + added: REPLACEME | ||
| 820 | + --> | ||
| 821 | + | ||
| 822 | + > Stability: 1 - Experimental | ||
| 823 | + | ||
| 824 | + An alias for `filehandle.close()`. | ||
| 825 | + | ||
| 816 | 826 | ### `fsPromises.access(path[, mode])` | |
| 817 | 827 | ||
| 818 | 828 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1902,6 +1902,17 @@ option. In the code example above, data will be in a single chunk if the file | |||
| 1902 | 1902 | has less then 64 KiB of data because no `highWaterMark` option is provided to | |
| 1903 | 1903 | [`fs.createReadStream()`][]. | |
| 1904 | 1904 | ||
| 1905 | + ##### `readable[Symbol.asyncDispose]()` | ||
| 1906 | + | ||
| 1907 | + <!-- YAML | ||
| 1908 | + added: REPLACEME | ||
| 1909 | + --> | ||
| 1910 | + | ||
| 1911 | + > Stability: 1 - Experimental | ||
| 1912 | + | ||
| 1913 | + Calls [`readable.destroy()`][readable-destroy] with an `AbortError` and returns | ||
| 1914 | + a promise that fulfills when the stream is finished. | ||
| 1915 | + | ||
| 1905 | 1916 | ##### `readable.compose(stream[, options])` | |
| 1906 | 1917 | ||
| 1907 | 1918 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ const { | |||
| 14 | 14 | SafeArrayIterator, | |
| 15 | 15 | SafePromisePrototypeFinally, | |
| 16 | 16 | Symbol, | |
| 17 | + SymbolAsyncDispose, | ||
| 17 | 18 | Uint8Array, | |
| 18 | 19 | FunctionPrototypeBind, | |
| 19 | 20 | } = primordials; | |
@@ -241,6 +242,10 @@ class FileHandle extends EventEmitterMixin(JSTransferable) { | |||
| 241 | 242 | return this[kClosePromise]; | |
| 242 | 243 | }; | |
| 243 | 244 | ||
| 245 | + async [SymbolAsyncDispose]() { | ||
| 246 | + return this.close(); | ||
| 247 | + } | ||
| 248 | + | ||
| 244 | 249 | /** | |
| 245 | 250 | * @typedef {import('../webstreams/readablestream').ReadableStream | |
| 246 | 251 | * } ReadableStream | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -228,6 +228,12 @@ function copyPrototype(src, dest, prefix) { | |||
| 228 | 228 | copyPrototype(original.prototype, primordials, `${name}Prototype`); | |
| 229 | 229 | }); | |
| 230 | 230 | ||
| 231 | + // Define Symbol.Dispose and Symbol.AsyncDispose | ||
| 232 | + // Until these are defined by the environment. | ||
| 233 | + // TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8. | ||
| 234 | + primordials.SymbolDispose ??= primordials.SymbolFor('nodejs.dispose'); | ||
| 235 | + primordials.SymbolAsyncDispose ??= primordials.SymbolFor('nodejs.asyncDispose'); | ||
| 236 | + | ||
| 231 | 237 | // Create copies of intrinsic objects that require a valid `this` to call | |
| 232 | 238 | // static methods. | |
| 233 | 239 | // Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,9 @@ const { | |||
| 8 | 8 | SafeMap, | |
| 9 | 9 | SafeWeakMap, | |
| 10 | 10 | StringPrototypeStartsWith, | |
| 11 | + Symbol, | ||
| 12 | + SymbolDispose, | ||
| 13 | + SymbolAsyncDispose, | ||
| 11 | 14 | globalThis, | |
| 12 | 15 | } = primordials; | |
| 13 | 16 | ||
@@ -72,6 +75,8 @@ function prepareExecution(options) { | |||
| 72 | 75 | initializeDeprecations(); | |
| 73 | 76 | require('internal/dns/utils').initializeDns(); | |
| 74 | 77 | ||
| 78 | + setupSymbolDisposePolyfill(); | ||
| 79 | + | ||
| 75 | 80 | if (isMainThread) { | |
| 76 | 81 | assert(internalBinding('worker').isMainThread); | |
| 77 | 82 | // Worker threads will get the manifest in the message handler. | |
@@ -109,6 +114,14 @@ function prepareExecution(options) { | |||
| 109 | 114 | } | |
| 110 | 115 | } | |
| 111 | 116 | ||
| 117 | + function setupSymbolDisposePolyfill() { | ||
| 118 | + // TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8. | ||
| 119 | + // eslint-disable-next-line node-core/prefer-primordials | ||
| 120 | + Symbol.dispose ??= SymbolDispose; | ||
| 121 | + // eslint-disable-next-line node-core/prefer-primordials | ||
| 122 | + Symbol.asyncDispose ??= SymbolAsyncDispose; | ||
| 123 | + } | ||
| 124 | + | ||
| 112 | 125 | function setupUserModules() { | |
| 113 | 126 | initializeCJSLoader(); | |
| 114 | 127 | initializeESMLoader(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,6 +31,7 @@ const { | |||
| 31 | 31 | ObjectSetPrototypeOf, | |
| 32 | 32 | Promise, | |
| 33 | 33 | SafeSet, | |
| 34 | + SymbolAsyncDispose, | ||
| 34 | 35 | SymbolAsyncIterator, | |
| 35 | 36 | Symbol, | |
| 36 | 37 | } = primordials; | |
@@ -66,6 +67,7 @@ const { | |||
| 66 | 67 | ERR_STREAM_PUSH_AFTER_EOF, | |
| 67 | 68 | ERR_STREAM_UNSHIFT_AFTER_END_EVENT, | |
| 68 | 69 | }, | |
| 70 | + AbortError, | ||
| 69 | 71 | } = require('internal/errors'); | |
| 70 | 72 | const { validateObject } = require('internal/validators'); | |
| 71 | 73 | ||
@@ -226,6 +228,15 @@ Readable.prototype[EE.captureRejectionSymbol] = function(err) { | |||
| 226 | 228 | this.destroy(err); | |
| 227 | 229 | }; | |
| 228 | 230 | ||
| 231 | + Readable.prototype[SymbolAsyncDispose] = function() { | ||
| 232 | + let error; | ||
| 233 | + if (!this.destroyed) { | ||
| 234 | + error = this.readableEnded ? null : new AbortError(); | ||
| 235 | + this.destroy(error); | ||
| 236 | + } | ||
| 237 | + return new Promise((resolve, reject) => eos(this, (err) => (err && err !== error ? reject(err) : resolve(null)))); | ||
| 238 | + }; | ||
| 239 | + | ||
| 229 | 240 | // Manually shove something into the read() buffer. | |
| 230 | 241 | // This returns true if the highWaterMark has not been hit yet, | |
| 231 | 242 | // similar to how Writable.write() returns true if you should | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const { promises: fs } = require('fs'); | ||
| 5 | + | ||
| 6 | + async function doOpen() { | ||
| 7 | + const fh = await fs.open(__filename); | ||
| 8 | + fh.on('close', common.mustCall()); | ||
| 9 | + await fh[Symbol.asyncDispose](); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + doOpen().then(common.mustCall()); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const { Readable } = require('stream'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + | ||
| 7 | + { | ||
| 8 | + const read = new Readable({ | ||
| 9 | + read() {} | ||
| 10 | + }); | ||
| 11 | + read.resume(); | ||
| 12 | + | ||
| 13 | + read.on('end', common.mustNotCall('no end event')); | ||
| 14 | + read.on('close', common.mustCall()); | ||
| 15 | + read.on('error', common.mustCall((err) => { | ||
| 16 | + assert.strictEqual(err.name, 'AbortError'); | ||
| 17 | + })); | ||
| 18 | + | ||
| 19 | + read[Symbol.asyncDispose]().then(common.mustCall(() => { | ||
| 20 | + assert.strictEqual(read.errored.name, 'AbortError'); | ||
| 21 | + assert.strictEqual(read.destroyed, true); | ||
| 22 | + })); | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -434,6 +434,8 @@ declare namespace primordials { | |||
| 434 | 434 | export const SymbolFor: typeof Symbol.for | |
| 435 | 435 | export const SymbolKeyFor: typeof Symbol.keyFor | |
| 436 | 436 | export const SymbolAsyncIterator: typeof Symbol.asyncIterator | |
| 437 | + export const SymbolDispose: typeof Symbol // TODO(MoLow): use typeof Symbol.dispose when it's available | ||
| 438 | + export const SymbolAsyncDispose: typeof Symbol // TODO(MoLow): use typeof Symbol.asyncDispose when it's available | ||
| 437 | 439 | export const SymbolHasInstance: typeof Symbol.hasInstance | |
| 438 | 440 | export const SymbolIsConcatSpreadable: typeof Symbol.isConcatSpreadable | |
| 439 | 441 | export const SymbolIterator: typeof Symbol.iterator | |
| Back | FazBrowse Home | New Git URL |
0 commit comments