Skip to content
Merged
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
10 changes: 5 additions & 5 deletions src/jsc/bindings/webcore/streams/JSReadableStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ JSC_DEFINE_HOST_FUNCTION(jsReadableStreamPrototypeFunction_cancel, (JSGlobalObje
if (!stream) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promiseRejectedWith(lexicalGlobalObject, createTypeError(lexicalGlobalObject, "ReadableStream.prototype.cancel can only be called on a ReadableStream"_s))));
if (isReadableStreamLocked(stream))
RELEASE_AND_RETURN(scope, JSValue::encode(promiseRejectedWith(lexicalGlobalObject, createTypeError(lexicalGlobalObject, "Cannot cancel a locked ReadableStream"_s))));
RELEASE_AND_RETURN(scope, JSValue::encode(promiseRejectedWith(lexicalGlobalObject, Bun::createError(lexicalGlobalObject, Bun::ErrorCode::ERR_INVALID_STATE_TypeError, "Invalid state: ReadableStream is locked"_s))));
Comment thread
robobun marked this conversation as resolved.
Resolved
auto* promise = readableStreamCancel(lexicalGlobalObject, stream, callFrame->argument(0));
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(promise);
Expand Down Expand Up @@ -667,9 +667,9 @@ JSC_DEFINE_HOST_FUNCTION(jsReadableStreamPrototypeFunction_pipeThrough, (JSGloba
RETURN_IF_EXCEPTION(scope, {});

if (isReadableStreamLocked(stream))
return throwVMTypeError(lexicalGlobalObject, scope, "Cannot pipe a locked ReadableStream"_s);
return Bun::throwError(lexicalGlobalObject, scope, Bun::ErrorCode::ERR_INVALID_STATE_TypeError, "Invalid state: The ReadableStream is locked"_s);
if (isWritableStreamLocked(transformWritable))
return throwVMTypeError(lexicalGlobalObject, scope, "Cannot pipe to a locked WritableStream"_s);
return Bun::throwError(lexicalGlobalObject, scope, Bun::ErrorCode::ERR_INVALID_STATE_TypeError, "Invalid state: The WritableStream is locked"_s);

auto* promise = readableStreamPipeTo(lexicalGlobalObject, stream, transformWritable, options.preventClose, options.preventAbort, options.preventCancel, options.signal);
RETURN_IF_EXCEPTION(scope, {});
Expand All @@ -694,9 +694,9 @@ JSC_DEFINE_HOST_FUNCTION(jsReadableStreamPrototypeFunction_pipeTo, (JSGlobalObje
ConvertedStreamPipeOptions options = convertStreamPipeOptions(vm, lexicalGlobalObject, callFrame->argument(1));
RETURN_IF_EXCEPTION(scope, nullptr);
if (isReadableStreamLocked(stream))
RELEASE_AND_RETURN(scope, promiseRejectedWith(lexicalGlobalObject, createTypeError(lexicalGlobalObject, "Cannot pipe a locked ReadableStream"_s)));
RELEASE_AND_RETURN(scope, promiseRejectedWith(lexicalGlobalObject, Bun::createError(lexicalGlobalObject, Bun::ErrorCode::ERR_INVALID_STATE_TypeError, "Invalid state: The ReadableStream is locked"_s)));
if (isWritableStreamLocked(destination))
RELEASE_AND_RETURN(scope, promiseRejectedWith(lexicalGlobalObject, createTypeError(lexicalGlobalObject, "Cannot pipe to a locked WritableStream"_s)));
RELEASE_AND_RETURN(scope, promiseRejectedWith(lexicalGlobalObject, Bun::createError(lexicalGlobalObject, Bun::ErrorCode::ERR_INVALID_STATE_TypeError, "Invalid state: The WritableStream is locked"_s)));
RELEASE_AND_RETURN(scope, readableStreamPipeTo(lexicalGlobalObject, stream, destination, options.preventClose, options.preventAbort, options.preventCancel, options.signal));
})));
}
Expand Down
31 changes: 31 additions & 0 deletions test/js/web/streams/streams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,37 @@ it("ReadableStream rejects pending reads when the lock is released", async () =>
expect((await reader.read()).value).toBe("456");
});

// A locked stream fails these with a TypeError (WHATWG) that carries Node's
// ERR_INVALID_STATE code and message (node compatibility).
const invalidState = message => expect.objectContaining({ name: "TypeError", code: "ERR_INVALID_STATE", message });

it("a locked ReadableStream fails cancel, pipeTo, pipeThrough, tee and getReader with ERR_INVALID_STATE", async () => {
const stream = new ReadableStream();
stream.getReader();

await expect(stream.cancel()).rejects.toThrow(invalidState("Invalid state: ReadableStream is locked"));
await expect(stream.pipeTo(new WritableStream())).rejects.toThrow(
invalidState("Invalid state: The ReadableStream is locked"),
);
expect(() => stream.pipeThrough(new TransformStream())).toThrow(
invalidState("Invalid state: The ReadableStream is locked"),
);
expect(() => stream.tee()).toThrow(invalidState("Invalid state: ReadableStream is locked"));
expect(() => stream.getReader()).toThrow(invalidState("Invalid state: ReadableStream is locked"));
});

it("pipeTo and pipeThrough into a locked WritableStream fail with ERR_INVALID_STATE", async () => {
const destination = new WritableStream();
destination.getWriter();

await expect(new ReadableStream().pipeTo(destination)).rejects.toThrow(
invalidState("Invalid state: The WritableStream is locked"),
);
expect(() => new ReadableStream().pipeThrough({ readable: new ReadableStream(), writable: destination })).toThrow(
invalidState("Invalid state: The WritableStream is locked"),
);
});

it("new Response(stream).arrayBuffer() (bytes)", async () => {
var queue = [Buffer.from("abdefgh")];
var stream = new ReadableStream({
Expand Down
Loading