From 46bf660e361c854c7a139b488a4630a2eb1cab28 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 17 Sep 2020 10:11:55 -0400 Subject: refactor: make fetch use op_fetch_read instead of op_read (#7529) --- cli/rt/11_streams.js | 10 +--------- cli/rt/26_fetch.js | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 18 deletions(-) (limited to 'cli/rt') diff --git a/cli/rt/11_streams.js b/cli/rt/11_streams.js index e5a5732e5..7f8af19e2 100644 --- a/cli/rt/11_streams.js +++ b/cli/rt/11_streams.js @@ -388,7 +388,7 @@ let { highWaterMark } = strategy; const { type } = underlyingSource; - if (isUnderlyingByteSource(underlyingSource)) { + if (underlyingSource.type == "bytes") { if (size !== undefined) { throw new RangeError( `When underlying source is "bytes", strategy.size must be undefined.`, @@ -1226,14 +1226,6 @@ ); } - function isUnderlyingByteSource( - underlyingSource, - ) { - const { type } = underlyingSource; - const typeString = String(type); - return typeString === "bytes"; - } - function isWritableStream(x) { return !( typeof x !== "object" || 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); }, }); } -- cgit v1.2.3