Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Previosuly, attempting to transfer a type: "bytes" ReadableStream like so... #3

@Mohammed6777

Description

@Mohammed6777

Previosuly, attempting to transfer a type: "bytes" ReadableStream like so...

const stream = new ReadableStream({
  type: "bytes",
  pull(controller) {
    controller.enqueue(new Uint8Array([1, 2, 3]));
    controller.close();
  },
});
const stream2 = structuredClone(stream, { transfer: [stream] });

...would fail with...

node:internal/structured_clone:23
  channel.port1.postMessage(value, options?.transfer);
                ^

TypeError: Found invalid object in transferList
    at structuredClone (node:internal/structured_clone:23:17)
    at file:///.../streams.mjs:8:17
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  code: 'ERR_INVALID_TRANSFER_OBJECT'
}

Node.js v19.3.0

This PR updates the ReadableStream constructor to mark byte streams as transferable. When transferred, byte streams become regular streams.

const stream = new ReadableStream({
  type: "bytes",
  pull(controller) {
    controller.enqueue(new Uint8Array([1, 2, 3]));
    controller.close();
  },
});

const stream2 = structuredClone(stream, { transfer: [stream] });
const reader = await stream2.getReader(); // `{ mode: "byob" }` would fail here as
                                          // `stream2` is no longer a byte stream

(tested with Chrome 108.0.5359.124)

Refs: nodejs/node#39062
Refs: https://streams.spec.whatwg.org/#rs-transfer

Originally posted by @mrbbot in nodejs/node#45955

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions