GeezerVison is showing github.com — stream: trim webstreams sink wrappers and BYOB transfers · nodejs/node@1ea57db
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/readablestream.js +15-8Lines changed: 15 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2400,13 +2400,16 @@ function readableByteStreamControllerConvertPullIntoDescriptor(desc) { | 2400 | 2400 | if (bytesFilled > byteLength) | 2401 | 2401 | throw new ERR_INVALID_STATE.RangeError('The buffer size is invalid'); | 2402 | 2402 | assert(!(bytesFilled % elementSize)); | 2403 | | - const transferredBuffer = ArrayBufferPrototypeTransfer(buffer); | | 2403 | + // The spec transfers the buffer once more here. Nothing else can reach | | 2404 | + // it by now: it was transferred when the descriptor was created, and | | 2405 | + // the BYOB request that exposes it has been invalidated, which always | | 2406 | + // goes together with a transfer, so the view is built on it as-is. | 2404 | 2407 |
| 2405 | 2408 | if (ctor === Buffer) { | 2406 | | - return Buffer.from(transferredBuffer, byteOffset, bytesFilled / elementSize); | | 2409 | + return Buffer.from(buffer, byteOffset, bytesFilled / elementSize); | 2407 | 2410 | } | 2408 | 2411 |
| 2409 | | - return new ctor(transferredBuffer, byteOffset, bytesFilled / elementSize); | | 2412 | + return new ctor(buffer, byteOffset, bytesFilled / elementSize); | 2410 | 2413 | } | 2411 | 2414 |
| 2412 | 2415 | function isReadableStreamLocked(stream) { | @@ -3439,11 +3442,15 @@ function readableByteStreamControllerEnqueue(controller, chunk) { | 3439 | 3442 | ); | 3440 | 3443 | } | 3441 | 3444 |
| 3442 | | - readableByteStreamControllerInvalidateBYOBRequest(controller); | 3443 | | - | 3444 | | - firstPendingPullInto.buffer = ArrayBufferPrototypeTransfer( | 3445 | | - firstPendingPullInto.buffer, | 3446 | | - ); | | 3445 | + // The transfer detaches the view the BYOB request exposed; without | | 3446 | + // a request the buffer is unreachable and there is nothing to | | 3447 | + // detach. | | 3448 | + if (controller[kState].byobRequest !== null) { | | 3449 | + readableByteStreamControllerInvalidateBYOBRequest(controller); | | 3450 | + firstPendingPullInto.buffer = ArrayBufferPrototypeTransfer( | | 3451 | + firstPendingPullInto.buffer, | | 3452 | + ); | | 3453 | + } | 3447 | 3454 |
| 3448 | 3455 | if (firstPendingPullInto.type === 'none') { | 3449 | 3456 | readableByteStreamControllerEnqueueDetachedPullIntoToQueue( | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/transformstream.js +26-15Lines changed: 26 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ const { | 36 | 36 | } = require('internal/util'); | 37 | 37 |
| 38 | 38 | const { | | 39 | + validateFunction, | 39 | 40 | validateObject, | 40 | 41 | kValidateObjectAllowObjects, | 41 | 42 | kValidateObjectAllowObjectsAndNull, | @@ -49,7 +50,6 @@ const { | 49 | 50 | } = require('internal/worker/js_transferable'); | 50 | 51 |
| 51 | 52 | const { | 52 | | - createPromiseCallback1Param, | 53 | 53 | createRawCallback2Params, | 54 | 54 | customInspect, | 55 | 55 | deferredStartAlgorithm, | @@ -61,8 +61,8 @@ const { | 61 | 61 | kResolvedPromise, | 62 | 62 | kState, | 63 | 63 | kType, | 64 | | - nonOpCancel, | 65 | | - nonOpFlush, | | 64 | + nonOpCallback, | | 65 | + promiseCall1Param, | 66 | 66 | } = require('internal/webstreams/util'); | 67 | 67 |
| 68 | 68 | const { | @@ -275,6 +275,9 @@ ObjectSetPrototypeOf(TransformStreamState.prototype, null); | 275 | 275 |
| 276 | 276 | class TransformStreamControllerState { | 277 | 277 | stream = undefined; | | 278 | + // The user-provided transformer the flush and cancel algorithms are | | 279 | + // methods of. | | 280 | + transformer = undefined; | 278 | 281 | transformAlgorithm = undefined; | 279 | 282 | flushAlgorithm = undefined; | 280 | 283 | cancelAlgorithm = undefined; | @@ -525,11 +528,13 @@ function setupTransformStreamDefaultController( | 525 | 528 | controller, | 526 | 529 | transformAlgorithm, | 527 | 530 | flushAlgorithm, | 528 | | - cancelAlgorithm) { | | 531 | + cancelAlgorithm, | | 532 | + transformer) { | 529 | 533 | assert(isTransformStream(stream)); | 530 | 534 | assert(stream[kState].controller === undefined); | 531 | 535 | const controllerState = new TransformStreamControllerState(); | 532 | 536 | controllerState.stream = stream; | | 537 | + controllerState.transformer = transformer; | 533 | 538 | controllerState.transformAlgorithm = transformAlgorithm; | 534 | 539 | controllerState.flushAlgorithm = flushAlgorithm; | 535 | 540 | controllerState.cancelAlgorithm = cancelAlgorithm; | @@ -547,25 +552,25 @@ function setupTransformStreamDefaultControllerFromTransformer( | 547 | 552 | const transformAlgorithm = transform ? | 548 | 553 | createRawCallback2Params('transformer.transform', transform, transformer) : | 549 | 554 | defaultTransformAlgorithm; | 550 | | - const flushAlgorithm = flush ? | 551 | | - createPromiseCallback1Param('transformer.flush', flush, transformer) : | 552 | | - nonOpFlush; | 553 | | - const cancelAlgorithm = cancel ? | 554 | | - createPromiseCallback1Param('transformer.cancel', cancel, transformer) : | 555 | | - nonOpCancel; | | 555 | + if (flush) | | 556 | + validateFunction(flush, 'transformer.flush'); | | 557 | + if (cancel) | | 558 | + validateFunction(cancel, 'transformer.cancel'); | 556 | 559 |
| 557 | 560 | setupTransformStreamDefaultController( | 558 | 561 | stream, | 559 | 562 | controller, | 560 | 563 | transformAlgorithm, | 561 | | - flushAlgorithm, | 562 | | - cancelAlgorithm); | | 564 | + flush || nonOpCallback, | | 565 | + cancel || nonOpCallback, | | 566 | + transformer); | 563 | 567 | } | 564 | 568 |
| 565 | 569 | function transformStreamDefaultControllerClearAlgorithms(controller) { | 566 | 570 | controller[kState].transformAlgorithm = undefined; | 567 | 571 | controller[kState].flushAlgorithm = undefined; | 568 | 572 | controller[kState].cancelAlgorithm = undefined; | | 573 | + controller[kState].transformer = undefined; | 569 | 574 | } | 570 | 575 |
| 571 | 576 | function transformStreamDefaultControllerEnqueue(controller, chunk) { | @@ -731,7 +736,9 @@ async function transformStreamDefaultSinkAbortAlgorithm( | 731 | 736 |
| 732 | 737 | const { promise, resolve, reject } = PromiseWithResolvers(); | 733 | 738 | controller[kState].finishPromise = promise; | 734 | | - const cancelPromise = controller[kState].cancelAlgorithm(reason); | | 739 | + const cancelPromise = promiseCall1Param( | | 740 | + controller[kState].cancelAlgorithm, controller[kState].transformer, | | 741 | + reason); | 735 | 742 | transformStreamDefaultControllerClearAlgorithms(controller); | 736 | 743 |
| 737 | 744 | PromisePrototypeThen( | @@ -765,7 +772,9 @@ function transformStreamDefaultSinkCloseAlgorithm(writableController) { | 765 | 772 | } | 766 | 773 | const { promise, resolve, reject } = PromiseWithResolvers(); | 767 | 774 | controller[kState].finishPromise = promise; | 768 | | - const flushPromise = controller[kState].flushAlgorithm(controller); | | 775 | + const flushPromise = promiseCall1Param( | | 776 | + controller[kState].flushAlgorithm, controller[kState].transformer, | | 777 | + controller); | 769 | 778 | transformStreamDefaultControllerClearAlgorithms(controller); | 770 | 779 | PromisePrototypeThen( | 771 | 780 | flushPromise, | @@ -812,7 +821,9 @@ function transformStreamDefaultSourceCancelAlgorithm( | 812 | 821 |
| 813 | 822 | const { promise, resolve, reject } = PromiseWithResolvers(); | 814 | 823 | controller[kState].finishPromise = promise; | 815 | | - const cancelPromise = controller[kState].cancelAlgorithm(reason); | | 824 | + const cancelPromise = promiseCall1Param( | | 825 | + controller[kState].cancelAlgorithm, controller[kState].transformer, | | 826 | + reason); | 816 | 827 | transformStreamDefaultControllerClearAlgorithms(controller); | 817 | 828 |
| 818 | 829 | PromisePrototypeThen( | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/util.js +53-32Lines changed: 53 additions & 32 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -323,20 +323,14 @@ function enqueueValueWithSize(controller, value, size) { | 323 | 323 | state.queueTotalSize += coercedSize; | 324 | 324 | } | 325 | 325 |
| 326 | | -// Arity-specialized variants of the promise-callback wrapper. The generic | 327 | | -// rest-parameter + ReflectApply form allocated an arguments array on every | 328 | | -// invocation; these run on per-chunk hot paths (pull/write/transform), so | 329 | | -// each known call-site arity gets its own wrapper. The exact number of | 330 | | -// arguments passed through to the user callback is observable and must be | 331 | | -// preserved. | 332 | | -function createPromiseCallbackNoParams(name, fn, thisArg) { | 333 | | - validateFunction(fn, name); | 334 | | - return async () => FunctionPrototypeCall(fn, thisArg); | 335 | | -} | 336 | | - | 337 | | -// Raw variants that skip the async wrapper's implicit result promise. | 338 | | -// Consumers of a raw callback invoke it inside try/catch and route the | 339 | | -// result through thenAlgorithmResult() below. | | 326 | +// The per-chunk callbacks of a user-provided source, sink or transformer | | 327 | +// (pull, write, transform) are wrapped in a closure whose captured method | | 328 | +// stays a constant for the optimizing compiler. Each known call-site | | 329 | +// arity gets its own wrapper, since a rest-parameter form would allocate | | 330 | +// an arguments array per invocation and the exact number of arguments | | 331 | +// passed to the user callback is observable. Consumers of a raw callback | | 332 | +// invoke it inside try/catch and route the result through | | 333 | +// thenAlgorithmResult() below. | 340 | 334 | function createRawCallback1Param(name, fn, thisArg) { | 341 | 335 | validateFunction(fn, name); | 342 | 336 | return (arg) => FunctionPrototypeCall(fn, thisArg, arg); | @@ -347,6 +341,46 @@ function createRawCallback2Params(name, fn, thisArg) { | 347 | 341 | return (arg1, arg2) => FunctionPrototypeCall(fn, thisArg, arg1, arg2); | 348 | 342 | } | 349 | 343 |
| | 344 | +// Async wrapper of a one-shot callback (the source cancel method). | | 345 | +function createPromiseCallback1Param(name, fn, thisArg) { | | 346 | + validateFunction(fn, name); | | 347 | + return async (arg) => FunctionPrototypeCall(fn, thisArg, arg); | | 348 | +} | | 349 | + | | 350 | +// The one-shot methods of a sink or transformer (close, abort, flush, | | 351 | +// cancel) are instead stored as-is in the controller state next to the | | 352 | +// object they belong to and called the way the reference implementation's | | 353 | +// promiseCall() does: a synchronous throw becomes a rejection and the | | 354 | +// result is adopted through a fresh promise, so a returned promise settles | | 355 | +// the caller two microtasks later and a plain value settles it at once. | | 356 | +function promiseCall1Param(fn, thisArg, arg) { | | 357 | + let result; | | 358 | + try { | | 359 | + result = FunctionPrototypeCall(fn, thisArg, arg); | | 360 | + } catch (error) { | | 361 | + return PromiseReject(error); | | 362 | + } | | 363 | + return promiseResolvedWith(result); | | 364 | +} | | 365 | + | | 366 | +function promiseCallNoParams(fn, thisArg) { | | 367 | + let result; | | 368 | + try { | | 369 | + result = FunctionPrototypeCall(fn, thisArg); | | 370 | + } catch (error) { | | 371 | + return PromiseReject(error); | | 372 | + } | | 373 | + return promiseResolvedWith(result); | | 374 | +} | | 375 | + | | 376 | +function promiseResolvedWith(value) { | | 377 | + if (value === null || | | 378 | + (typeof value !== 'object' && typeof value !== 'function')) { | | 379 | + return PromiseResolve(value); | | 380 | + } | | 381 | + return new Promise((resolve) => resolve(value)); | | 382 | +} | | 383 | + | 350 | 384 | // A single shared, forever-resolved promise used to enqueue a reaction at | 351 | 385 | // the next microtask checkpoint without allocating a fresh promise. | 352 | 386 | const kResolvedPromise = PromiseResolve(); | @@ -385,16 +419,6 @@ function thenAlgorithmResult(result, onFulfilled, onRejected) { | 385 | 419 | } | 386 | 420 | } | 387 | 421 |
| 388 | | -function createPromiseCallback1Param(name, fn, thisArg) { | 389 | | - validateFunction(fn, name); | 390 | | - return async (arg) => FunctionPrototypeCall(fn, thisArg, arg); | 391 | | -} | 392 | | - | 393 | | -function createPromiseCallback2Params(name, fn, thisArg) { | 394 | | - validateFunction(fn, name); | 395 | | - return async (arg1, arg2) => FunctionPrototypeCall(fn, thisArg, arg1, arg2); | 396 | | -} | 397 | | - | 398 | 422 | function isPromisePending(promise) { | 399 | 423 | if (promise === undefined) return false; | 400 | 424 | const details = getPromiseDetails(promise); | @@ -438,12 +462,11 @@ function setPromiseHandled(promise) { | 438 | 462 | PromisePrototypeThen(promise, undefined, () => {}); | 439 | 463 | } | 440 | 464 |
| 441 | | -async function nonOpFlush() {} | 442 | | - | 443 | 465 | // Shared non-op for the start/pull/write algorithm callbacks, which all | 444 | | -// follow the raw-callback contract (see createRawCallback*): the | | 466 | +// follow the raw-callback contract (see createRawCallback*), and for the | | 467 | +// missing one-shot methods of a user-provided sink or transformer: the | 445 | 468 | // non-thenable return takes the allocation-free fast path in | 446 | | -// thenAlgorithmResult(). | | 469 | +// thenAlgorithmResult() and settles promiseCall*() at once. | 447 | 470 | function nonOpCallback() {} | 448 | 471 |
| 449 | 472 | async function nonOpCancel() {} | @@ -463,9 +486,7 @@ module.exports = { | 463 | 486 | canCopyArrayBuffer, | 464 | 487 | cloneAsUint8Array, | 465 | 488 | copyArrayBuffer, | 466 | | - createPromiseCallbackNoParams, | 467 | 489 | createPromiseCallback1Param, | 468 | | - createPromiseCallback2Params, | 469 | 490 | createRawCallback1Param, | 470 | 491 | createRawCallback2Params, | 471 | 492 | customInspect, | @@ -489,9 +510,9 @@ module.exports = { | 489 | 510 | materializeQueue, | 490 | 511 | nonOpCallback, | 491 | 512 | nonOpCancel, | 492 | | - nonOpFlush, | 493 | | - | 494 | 513 | peekQueueValue, | | 514 | + promiseCall1Param, | | 515 | + promiseCallNoParams, | 495 | 516 | rejectedHandledRecord, | 496 | 517 | resetQueue, | 497 | 518 | resolvedRecord, | |
|