mirror of
https://github.com/denoland/deno.git
synced 2025-10-01 22:51:14 +00:00
refactor: make fetch use op_fetch_read instead of op_read (#7529)
This commit is contained in:
parent
1e6d37f88c
commit
46bf660e36
4 changed files with 68 additions and 28 deletions
|
@ -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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue