refactor: make fetch use op_fetch_read instead of op_read (#7529)

This commit is contained in:
Ryan Dahl 2020-09-17 10:11:55 -04:00 committed by GitHub
parent 1e6d37f88c
commit 46bf660e36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 28 deletions

View file

@ -5,7 +5,6 @@
const { notImplemented } = window.__bootstrap.util;
const { getHeaderValueParams, isTypedArray } = window.__bootstrap.webUtil;
const { Blob, bytesSymbol: blobBytesSymbol } = window.__bootstrap.blob;
const { read } = window.__bootstrap.io;
const { close } = window.__bootstrap.resources;
const Body = window.__bootstrap.body;
const { ReadableStream } = window.__bootstrap.streams;
@ -283,6 +282,7 @@
body,
clientRid,
);
const rid = fetchResponse.bodyRid;
if (
NULL_BODY_STATUS.includes(fetchResponse.status) ||
@ -294,25 +294,27 @@
responseBody = null;
} else {
responseBody = new ReadableStream({
type: "bytes",
async pull(controller) {
try {
const b = new Uint8Array(1024 * 32);
const result = await read(fetchResponse.bodyRid, b);
if (result === null) {
const result = await core.jsonOpAsync("op_fetch_read", { rid });
if (!result || !result.chunk) {
controller.close();
return close(fetchResponse.bodyRid);
close(rid);
} else {
// TODO(ry) This is terribly inefficient. Make this zero-copy.
const chunk = new Uint8Array(result.chunk);
controller.enqueue(chunk);
}
controller.enqueue(b.subarray(0, result));
} catch (e) {
controller.error(e);
controller.close();
close(fetchResponse.bodyRid);
close(rid);
}
},
cancel() {
// When reader.cancel() is called
close(fetchResponse.bodyRid);
close(rid);
},
});
}