Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/js/builtins/ReadableStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,62 @@
return this;
}

// https://streams.spec.whatwg.org/#rs-from
// https://streams.spec.whatwg.org/#readable-stream-from-iterable
$overriddenName = "from";
export function from(this, iterable) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const Symbol = globalThis.Symbol;

let iterator;
// `sync` marks an iterator obtained via Symbol.iterator, which CreateAsyncFromSyncIterator
// adapts: its next()/return() results are validated and their values awaited before use.
let sync = false;

// GetIterator(iterable, async): reading Symbol.asyncIterator first means a null or
// undefined argument throws the same TypeError as Node before any sync fallback.
let method = iterable[Symbol.asyncIterator];
if (!$isUndefinedOrNull(method)) {
if (!$isCallable(method)) throw new TypeError("ReadableStream.from: Symbol.asyncIterator is not a function");
iterator = method.$call(iterable);
} else {
method = iterable[Symbol.iterator];
// String() rather than a template literal so a Symbol argument reports
// ERR_ARG_NOT_ITERABLE instead of throwing "Cannot convert a Symbol to a string".
if ($isUndefinedOrNull(method)) throw $ERR_ARG_NOT_ITERABLE(String(iterable) + " must be iterable");
Comment thread
robobun marked this conversation as resolved.
Outdated
if (!$isCallable(method)) throw new TypeError("ReadableStream.from: Symbol.iterator is not a function");
iterator = method.$call(iterable);
sync = true;
}

if (!$isObject(iterator)) throw new TypeError("ReadableStream.from: iterator must be an object");
Comment thread
robobun marked this conversation as resolved.
Outdated

// The next method is captured once, mirroring GetIteratorFromMethod.
const nextMethod = iterator.next;

async function pull(controller) {
const result = nextMethod.$call(iterator);
const iterResult = sync ? result : await result;
if (!$isObject(iterResult)) throw new TypeError("ReadableStream.from: iterator.next() returned a non-object value");
if (iterResult.done) {
controller.close();
} else {
controller.enqueue(sync ? await iterResult.value : iterResult.value);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
robobun marked this conversation as resolved.
}

async function cancel(reason) {
const returnMethod = iterator.return;
if ($isUndefinedOrNull(returnMethod)) return;
if (!$isCallable(returnMethod)) throw new TypeError("ReadableStream.from: iterator.return is not a function");
const result = returnMethod.$call(iterator, reason);
const iterResult = sync ? result : await result;
if (!$isObject(iterResult))
throw new TypeError("ReadableStream.from: iterator.return() returned a non-object value");
}

Check warning on line 160 in src/js/builtins/ReadableStream.ts

View check run for this annotation

Claude / Claude Code Review

Sync-iterator path skips awaiting iterResult.value in pull(done:true) and cancel()

The hand-rolled `CreateAsyncFromSyncIterator` emulation skips awaiting `iterResult.value` in two branches: the `pull()` `done:true` branch (just calls `controller.close()`) and the `cancel()` path (returns after the `$isObject` check). Per spec, `AsyncFromSyncIteratorContinuation` always `PromiseResolve`s and awaits `result.value`, so a sync iterator whose `next()`/`return()` yields `{ value: Promise.reject(err), done: true }` should error the stream / reject `cancel()` — here it resolves cleanl
Comment thread
robobun marked this conversation as resolved.

return new ReadableStream({ pull, cancel }, { highWaterMark: 0 });
}

$linkTimeConstant;
export function readableStreamToArray(stream: ReadableStream): Promise<unknown[]> {
if (!$isReadableStream(stream)) throw $ERR_INVALID_ARG_TYPE("stream", "ReadableStream", typeof stream);
Expand Down
1 change: 1 addition & 0 deletions src/jsc/bindings/webcore/JSReadableStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ template<> void JSReadableStreamDOMConstructor::initializeProperties(VM& vm, JSD
m_originalName.set(vm, this, nameString);
putDirect(vm, vm.propertyNames->name, nameString, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum);
putDirect(vm, vm.propertyNames->prototype, JSReadableStream::prototype(vm, globalObject), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete);
this->putDirectBuiltinFunction(vm, &globalObject, JSC::Identifier::fromString(vm, "from"_s), readableStreamFromCodeGenerator(vm), static_cast<unsigned>(JSC::PropertyAttribute::Function));
Comment thread
robobun marked this conversation as resolved.
}

template<> FunctionExecutable* JSReadableStreamDOMConstructor::initializeExecutable(VM& vm)
Expand Down
136 changes: 136 additions & 0 deletions test/js/web/streams/streams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "bun";
import { describe, expect, it, test } from "bun:test";
import { bunEnv, bunExe, isMacOS, isWindows, tempDir, tmpdirSync } from "harness";
import { ReadableStream as WebReadableStream } from "node:stream/web";
import { mkfifo } from "mkfifo";
import { createReadStream, realpathSync, unlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";
Expand Down Expand Up @@ -483,6 +484,141 @@ it("exists globally", () => {
expect(typeof CountQueuingStrategy).toBe("function");
});

// https://github.com/oven-sh/bun/issues/32529
describe("ReadableStream.from", () => {
it("is exposed on the global and node:stream/web constructors", () => {
expect(typeof ReadableStream.from).toBe("function");
expect(ReadableStream.from.length).toBe(1);
expect(ReadableStream.from.name).toBe("from");
// node:stream/web re-exports the same constructor as the global
expect(WebReadableStream).toBe(ReadableStream);
expect(typeof WebReadableStream.from).toBe("function");
});

it("creates a stream from a sync iterable (array)", async () => {
const stream = ReadableStream.from([1, 2, 3]);
expect(stream).toBeInstanceOf(ReadableStream);
const out = [];
for await (const chunk of stream) out.push(chunk);
expect(out).toEqual([1, 2, 3]);
});

it("creates a stream from a string", async () => {
const out = [];
for await (const chunk of ReadableStream.from("abc")) out.push(chunk);
expect(out).toEqual(["a", "b", "c"]);
});

it("creates a stream from an async generator", async () => {
async function* gen() {
yield "a";
yield "b";
yield "c";
}
const out = [];
for await (const chunk of ReadableStream.from(gen())) out.push(chunk);
expect(out).toEqual(["a", "b", "c"]);
});

it("enqueues null and undefined chunks without skipping them", async () => {
async function* gen() {
yield 1;
yield undefined;
yield null;
yield 4;
}
const out = [];
for await (const chunk of ReadableStream.from(gen())) out.push(chunk);
expect(out).toEqual([1, undefined, null, 4]);
});

it("awaits promise values yielded by a sync iterable", async () => {
const out = [];
for await (const chunk of ReadableStream.from([Promise.resolve("x"), Promise.resolve("y")])) out.push(chunk);
expect(out).toEqual(["x", "y"]);
});

it("prefers Symbol.asyncIterator over Symbol.iterator", async () => {
const obj = {
[Symbol.iterator]() {
return { next: () => ({ value: "sync", done: false }) };
},
async *[Symbol.asyncIterator]() {
yield "async1";
yield "async2";
},
};
const out = [];
for await (const chunk of ReadableStream.from(obj)) out.push(chunk);
expect(out).toEqual(["async1", "async2"]);
});

it("does not start iterating until the stream is read", async () => {
let started = false;
async function* gen() {
started = true;
yield 1;
}
const stream = ReadableStream.from(gen());
expect(started).toBe(false);
const reader = stream.getReader();
await reader.read();
expect(started).toBe(true);
reader.releaseLock();
});

it("calls iterator.return with the reason when cancelled", async () => {
let returnedWith = Symbol("unset");
const iterable = {
[Symbol.asyncIterator]() {
return this;
},
async next() {
return { value: 1, done: false };
},
async return(value) {
returnedWith = value;
return { value, done: true };
},
};
const reader = ReadableStream.from(iterable).getReader();
await reader.read();
await reader.cancel("my-reason");
expect(returnedWith).toBe("my-reason");
});

it("propagates errors thrown by the iterator", async () => {
async function* gen() {
yield 1;
throw new Error("boom");
}
const out = [];
let error;
try {
for await (const chunk of ReadableStream.from(gen())) out.push(chunk);
} catch (e) {
error = e;
}
expect(out).toEqual([1]);
expect(error?.message).toBe("boom");
});

it.each([123, true, {}, Symbol("x"), 10n])("throws ERR_ARG_NOT_ITERABLE for %p", value => {
let err;
try {
ReadableStream.from(value);
} catch (e) {
err = e;
}
expect(err).toBeInstanceOf(TypeError);
expect(err.code).toBe("ERR_ARG_NOT_ITERABLE");
});

it.each([null, undefined])("throws a TypeError for %p", value => {
expect(() => ReadableStream.from(value)).toThrow(TypeError);
});
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it("new Response(stream).body", async () => {
var stream = new ReadableStream({
pull(controller) {
Expand Down
Loading