Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Mohammed6777 opened this issue Feb 13, 2024 · 1 comment

Comments

@Mohammed6777
Copy link

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

@Mohammed6777
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant