diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-09-17 10:11:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-17 16:11:55 +0200 |
commit | 46bf660e361c854c7a139b488a4630a2eb1cab28 (patch) | |
tree | 90eecfec72221a2c9c385ebba2cc3a3c4cacf008 /cli/rt/26_fetch.js | |
parent | 1e6d37f88c80ab3cb2d6fe43206feb61135ff574 (diff) |
refactor: make fetch use op_fetch_read instead of op_read (#7529)
Diffstat (limited to 'cli/rt/26_fetch.js')
-rw-r--r-- | cli/rt/26_fetch.js | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/cli/rt/26_fetch.js b/cli/rt/26_fetch.js index 1c1d8c78f..ca36d7ba4 100644 --- a/cli/rt/26_fetch.js +++ b/cli/rt/26_fetch.js @@ -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); }, }); } |