mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-04 15:09:40 +00:00
LibWeb: Implement ReadableStreamBYOBRequest.respondWithNewView
The AO behind this prototype was added in commit ed1076d9ca
,
so we can now trivially expose the prototype as well.
This commit is contained in:
parent
3c11a1551d
commit
5a99a6afb4
|
@ -0,0 +1,4 @@
|
|||
abcdefghijklmnopqrstuvwxyz
|
||||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
0123456789!@#$%^&*()-=_+,<
|
||||
Done!
|
|
@ -0,0 +1,80 @@
|
|||
<script src="../include.js"></script>
|
||||
<script>
|
||||
const CHUNK1 = "abcdefghijklmnopqrstuvwxyz";
|
||||
const CHUNK2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const CHUNK3 = "0123456789!@#$%^&*()-=_+,<";
|
||||
|
||||
const readStream = stream => {
|
||||
const reader = stream.getReader({ mode: "byob" });
|
||||
|
||||
let buffer = new ArrayBuffer(256);
|
||||
let offset = 0;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const processText = ({ done, value }) => {
|
||||
if (done) {
|
||||
println("Done!");
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
buffer = value.buffer;
|
||||
offset += value.byteLength;
|
||||
|
||||
value = new TextDecoder().decode(value);
|
||||
println(value);
|
||||
|
||||
return reader
|
||||
.read(new Uint8Array(buffer, offset, buffer.byteLength - offset))
|
||||
.then(processText);
|
||||
};
|
||||
|
||||
reader
|
||||
.read(new Uint8Array(buffer, offset, buffer.byteLength - offset))
|
||||
.then(processText);
|
||||
});
|
||||
};
|
||||
|
||||
const writeStream = (controller, data) => {
|
||||
const view = controller.byobRequest.view;
|
||||
|
||||
const target = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
||||
const source = Uint8Array.from(Array.from(data).map(ch => ch.charCodeAt(0)));
|
||||
|
||||
for (let i = 0; i < source.length; ++i) {
|
||||
target[i] = source[i];
|
||||
}
|
||||
|
||||
controller.byobRequest.respondWithNewView(target.subarray(0, source.length));
|
||||
};
|
||||
|
||||
asyncTest(done => {
|
||||
const stream = new ReadableStream({
|
||||
type: "bytes",
|
||||
|
||||
start(controller) {
|
||||
pullCount = 0;
|
||||
},
|
||||
|
||||
pull(controller) {
|
||||
const view = controller.byobRequest.view;
|
||||
++pullCount;
|
||||
|
||||
if (pullCount == 1) {
|
||||
writeStream(controller, CHUNK1);
|
||||
} else if (pullCount == 2) {
|
||||
writeStream(controller, CHUNK2);
|
||||
} else if (pullCount == 3) {
|
||||
writeStream(controller, CHUNK3);
|
||||
} else {
|
||||
controller.close();
|
||||
controller.byobRequest.respond(0);
|
||||
}
|
||||
},
|
||||
|
||||
cancel() {},
|
||||
});
|
||||
|
||||
readStream(stream).then(done);
|
||||
});
|
||||
</script>
|
|
@ -60,4 +60,21 @@ WebIDL::ExceptionOr<void> ReadableStreamBYOBRequest::respond(WebIDL::UnsignedLon
|
|||
return readable_byte_stream_controller_respond(*m_controller, bytes_written);
|
||||
}
|
||||
|
||||
// https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view
|
||||
WebIDL::ExceptionOr<void> ReadableStreamBYOBRequest::respond_with_new_view(JS::Handle<WebIDL::ArrayBufferView> const& view)
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// 1. If this.[[controller]] is undefined, throw a TypeError exception.
|
||||
if (!m_controller)
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Controller is undefined"_string };
|
||||
|
||||
// 2. If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
|
||||
if (view->viewed_array_buffer()->is_detached())
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Unable to respond with a detached ArrayBuffer"_string };
|
||||
|
||||
// 3. Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
|
||||
return TRY(readable_byte_stream_controller_respond_with_new_view(realm, *m_controller, *view));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ public:
|
|||
void set_view(JS::GCPtr<WebIDL::ArrayBufferView> value) { m_view = value; }
|
||||
|
||||
WebIDL::ExceptionOr<void> respond(WebIDL::UnsignedLongLong bytes_written);
|
||||
WebIDL::ExceptionOr<void> respond_with_new_view(JS::Handle<WebIDL::ArrayBufferView> const& view);
|
||||
|
||||
private:
|
||||
explicit ReadableStreamBYOBRequest(JS::Realm&);
|
||||
|
|
|
@ -4,5 +4,5 @@ interface ReadableStreamBYOBRequest {
|
|||
readonly attribute ArrayBufferView? view;
|
||||
|
||||
undefined respond([EnforceRange] unsigned long long bytesWritten);
|
||||
// FIXME: undefined respondWithNewView(ArrayBufferView view);
|
||||
undefined respondWithNewView(ArrayBufferView view);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue