| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 253772c commit b9586bf
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -476,14 +476,17 @@ Reads data from the file and stores that in the given buffer. | |||
| 476 | 476 | If the file is not modified concurrently, the end-of-file is reached when the | |
| 477 | 477 | number of bytes read is zero. | |
| 478 | 478 | ||
| 479 | - #### `filehandle.readableWebStream()` | ||
| 479 | + #### `filehandle.readableWebStream([options])` | ||
| 480 | 480 | ||
| 481 | 481 | <!-- YAML | |
| 482 | 482 | added: v17.0.0 | |
| 483 | 483 | changes: | |
| 484 | 484 | - version: REPLACEME | |
| 485 | 485 | pr-url: https://github.com/nodejs/node/pull/57513 | |
| 486 | 486 | description: Marking the API stable. | |
| 487 | + - version: REPLACEME | ||
| 488 | + pr-url: https://github.com/nodejs/node/pull/58548 | ||
| 489 | + description: Added the `autoClose` option. | ||
| 487 | 490 | - version: v22.15.0 | |
| 488 | 491 | pr-url: https://github.com/nodejs/node/pull/55461 | |
| 489 | 492 | description: Removed option to create a 'bytes' stream. Streams are now always 'bytes' streams. | |
@@ -494,6 +497,9 @@ changes: | |||
| 494 | 497 | description: Added option to create a 'bytes' stream. | |
| 495 | 498 | --> | |
| 496 | 499 | ||
| 500 | + * `options` {Object} | ||
| 501 | + * `autoClose` {boolean} When true, causes the {FileHandle} to be closed when the | ||
| 502 | + stream is closed. **Default:** `false` | ||
| 497 | 503 | * Returns: {ReadableStream} | |
| 498 | 504 | ||
| 499 | 505 | Returns a byte-oriented `ReadableStream` that may be used to read the file's | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,7 +84,6 @@ const { | |||
| 84 | 84 | validateEncoding, | |
| 85 | 85 | validateInteger, | |
| 86 | 86 | validateObject, | |
| 87 | - validateString, | ||
| 88 | 87 | kValidateObjectAllowNullable, | |
| 89 | 88 | } = require('internal/validators'); | |
| 90 | 89 | const pathModule = require('path'); | |
@@ -278,9 +277,10 @@ class FileHandle extends EventEmitter { | |||
| 278 | 277 | /** | |
| 279 | 278 | * @typedef {import('../webstreams/readablestream').ReadableStream | |
| 280 | 279 | * } ReadableStream | |
| 280 | + * @param {{ type?: 'bytes', autoClose?: boolean }} [options] | ||
| 281 | 281 | * @returns {ReadableStream} | |
| 282 | 282 | */ | |
| 283 | - readableWebStream(options = { __proto__: null, type: 'bytes' }) { | ||
| 283 | + readableWebStream(options = kEmptyObject) { | ||
| 284 | 284 | if (this[kFd] === -1) | |
| 285 | 285 | throw new ERR_INVALID_STATE('The FileHandle is closed'); | |
| 286 | 286 | if (this[kClosePromise]) | |
@@ -289,20 +289,27 @@ class FileHandle extends EventEmitter { | |||
| 289 | 289 | throw new ERR_INVALID_STATE('The FileHandle is locked'); | |
| 290 | 290 | this[kLocked] = true; | |
| 291 | 291 | ||
| 292 | - if (options.type !== undefined) { | ||
| 293 | - validateString(options.type, 'options.type'); | ||
| 294 | - } | ||
| 295 | - if (options.type !== 'bytes') { | ||
| 292 | + validateObject(options, 'options'); | ||
| 293 | + const { | ||
| 294 | + type = 'bytes', | ||
| 295 | + autoClose = false, | ||
| 296 | + } = options; | ||
| 297 | + | ||
| 298 | + validateBoolean(autoClose, 'options.autoClose'); | ||
| 299 | + | ||
| 300 | + if (type !== 'bytes') { | ||
| 296 | 301 | process.emitWarning( | |
| 297 | 302 | 'A non-"bytes" options.type has no effect. A byte-oriented steam is ' + | |
| 298 | 303 | 'always created.', | |
| 299 | 304 | 'ExperimentalWarning', | |
| 300 | 305 | ); | |
| 301 | 306 | } | |
| 302 | 307 | ||
| 303 | - | ||
| 304 | 308 | const readFn = FunctionPrototypeBind(this.read, this); | |
| 305 | - const ondone = FunctionPrototypeBind(this[kUnref], this); | ||
| 309 | + const ondone = async () => { | ||
| 310 | + this[kUnref](); | ||
| 311 | + if (autoClose) await this.close(); | ||
| 312 | + }; | ||
| 306 | 313 | ||
| 307 | 314 | const ReadableStream = lazyReadableStream(); | |
| 308 | 315 | const readable = new ReadableStream({ | |
@@ -314,15 +321,15 @@ class FileHandle extends EventEmitter { | |||
| 314 | 321 | const { bytesRead } = await readFn(view, view.byteOffset, view.byteLength); | |
| 315 | 322 | ||
| 316 | 323 | if (bytesRead === 0) { | |
| 317 | - ondone(); | ||
| 318 | 324 | controller.close(); | |
| 325 | + await ondone(); | ||
| 319 | 326 | } | |
| 320 | 327 | ||
| 321 | 328 | controller.byobRequest.respond(bytesRead); | |
| 322 | 329 | }, | |
| 323 | 330 | ||
| 324 | - cancel() { | ||
| 325 | - ondone(); | ||
| 331 | + async cancel() { | ||
| 332 | + await ondone(); | ||
| 326 | 333 | }, | |
| 327 | 334 | }); | |
| 328 | 335 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -106,7 +106,9 @@ function testCompileStreamingRejectionUsingFetch(responseCallback, rejection) { | |||
| 106 | 106 | // Response whose body is a ReadableStream instead of calling fetch(). | |
| 107 | 107 | await testCompileStreamingSuccess(async () => { | |
| 108 | 108 | const handle = await fs.open(fixtures.path('simple.wasm')); | |
| 109 | - const stream = handle.readableWebStream(); | ||
| 109 | + // We set the autoClose option to true so that the file handle is closed | ||
| 110 | + // automatically when the stream is completed or canceled. | ||
| 111 | + const stream = handle.readableWebStream({ autoClose: true }); | ||
| 110 | 112 | return Promise.resolve(new Response(stream, { | |
| 111 | 113 | status: 200, | |
| 112 | 114 | headers: { 'Content-Type': 'application/wasm' } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + import '../common/index.mjs'; | ||
| 2 | + import { open } from 'node:fs/promises'; | ||
| 3 | + import { rejects } from 'node:assert'; | ||
| 4 | + | ||
| 5 | + { | ||
| 6 | + const fh = await open(new URL(import.meta.url)); | ||
| 7 | + | ||
| 8 | + // TODO: remove autoClose option when it becomes default | ||
| 9 | + const readableStream = fh.readableWebStream({ autoClose: true }); | ||
| 10 | + | ||
| 11 | + // Consume the stream | ||
| 12 | + await new Response(readableStream).text(); | ||
| 13 | + | ||
| 14 | + // If reading the FileHandle after the stream is consumed fails, | ||
| 15 | + // then we assume the autoClose option worked as expected. | ||
| 16 | + await rejects(fh.read(), { code: 'EBADF' }); | ||
| 17 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments