| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 88e621e commit 6f4c9dd
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -476,11 +476,14 @@ 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 | + - version: REPLACEME | ||
| 485 | + pr-url: https://github.com/nodejs/node/pull/58548 | ||
| 486 | + description: Added the `autoClose` option. | ||
| 484 | 487 | - version: v24.0.0 | |
| 485 | 488 | pr-url: https://github.com/nodejs/node/pull/57513 | |
| 486 | 489 | description: Marking the API stable. | |
@@ -496,6 +499,9 @@ changes: | |||
| 496 | 499 | description: Added option to create a 'bytes' stream. | |
| 497 | 500 | --> | |
| 498 | 501 | ||
| 502 | + * `options` {Object} | ||
| 503 | + * `autoClose` {boolean} When true, causes the {FileHandle} to be closed when the | ||
| 504 | + stream is closed. **Default:** `false` | ||
| 499 | 505 | * Returns: {ReadableStream} | |
| 500 | 506 | ||
| 501 | 507 | Returns a byte-oriented `ReadableStream` that may be used to read the file's | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,7 +86,6 @@ const { | |||
| 86 | 86 | validateInteger, | |
| 87 | 87 | validateObject, | |
| 88 | 88 | validateOneOf, | |
| 89 | - validateString, | ||
| 90 | 89 | kValidateObjectAllowNullable, | |
| 91 | 90 | } = require('internal/validators'); | |
| 92 | 91 | const pathModule = require('path'); | |
@@ -279,9 +278,10 @@ class FileHandle extends EventEmitter { | |||
| 279 | 278 | /** | |
| 280 | 279 | * @typedef {import('../webstreams/readablestream').ReadableStream | |
| 281 | 280 | * } ReadableStream | |
| 281 | + * @param {{ type?: 'bytes', autoClose?: boolean }} [options] | ||
| 282 | 282 | * @returns {ReadableStream} | |
| 283 | 283 | */ | |
| 284 | - readableWebStream(options = { __proto__: null, type: 'bytes' }) { | ||
| 284 | + readableWebStream(options = kEmptyObject) { | ||
| 285 | 285 | if (this[kFd] === -1) | |
| 286 | 286 | throw new ERR_INVALID_STATE('The FileHandle is closed'); | |
| 287 | 287 | if (this[kClosePromise]) | |
@@ -290,20 +290,27 @@ class FileHandle extends EventEmitter { | |||
| 290 | 290 | throw new ERR_INVALID_STATE('The FileHandle is locked'); | |
| 291 | 291 | this[kLocked] = true; | |
| 292 | 292 | ||
| 293 | - if (options.type !== undefined) { | ||
| 294 | - validateString(options.type, 'options.type'); | ||
| 295 | - } | ||
| 296 | - if (options.type !== 'bytes') { | ||
| 293 | + validateObject(options, 'options'); | ||
| 294 | + const { | ||
| 295 | + type = 'bytes', | ||
| 296 | + autoClose = false, | ||
| 297 | + } = options; | ||
| 298 | + | ||
| 299 | + validateBoolean(autoClose, 'options.autoClose'); | ||
| 300 | + | ||
| 301 | + if (type !== 'bytes') { | ||
| 297 | 302 | process.emitWarning( | |
| 298 | 303 | 'A non-"bytes" options.type has no effect. A byte-oriented steam is ' + | |
| 299 | 304 | 'always created.', | |
| 300 | 305 | 'ExperimentalWarning', | |
| 301 | 306 | ); | |
| 302 | 307 | } | |
| 303 | 308 | ||
| 304 | - | ||
| 305 | 309 | const readFn = FunctionPrototypeBind(this.read, this); | |
| 306 | - const ondone = FunctionPrototypeBind(this[kUnref], this); | ||
| 310 | + const ondone = async () => { | ||
| 311 | + this[kUnref](); | ||
| 312 | + if (autoClose) await this.close(); | ||
| 313 | + }; | ||
| 307 | 314 | ||
| 308 | 315 | const ReadableStream = lazyReadableStream(); | |
| 309 | 316 | const readable = new ReadableStream({ | |
@@ -315,15 +322,15 @@ class FileHandle extends EventEmitter { | |||
| 315 | 322 | const { bytesRead } = await readFn(view, view.byteOffset, view.byteLength); | |
| 316 | 323 | ||
| 317 | 324 | if (bytesRead === 0) { | |
| 318 | - ondone(); | ||
| 319 | 325 | controller.close(); | |
| 326 | + await ondone(); | ||
| 320 | 327 | } | |
| 321 | 328 | ||
| 322 | 329 | controller.byobRequest.respond(bytesRead); | |
| 323 | 330 | }, | |
| 324 | 331 | ||
| 325 | - cancel() { | ||
| 326 | - ondone(); | ||
| 332 | + async cancel() { | ||
| 333 | + await ondone(); | ||
| 327 | 334 | }, | |
| 328 | 335 | }); | |
| 329 | 336 | ||
| 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,30 @@ | |||
| 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 | + } | ||
| 18 | + | ||
| 19 | + { | ||
| 20 | + await using fh = await open(new URL(import.meta.url)); | ||
| 21 | + | ||
| 22 | + const readableStream = fh.readableWebStream({ autoClose: false }); | ||
| 23 | + | ||
| 24 | + // Consume the stream | ||
| 25 | + await new Response(readableStream).text(); | ||
| 26 | + | ||
| 27 | + // Filehandle must be still open | ||
| 28 | + await fh.read(); | ||
| 29 | + await fh.close(); | ||
| 30 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments