diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-01-15 13:14:54 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-15 13:14:54 -0700 |
commit | 3ff80eb1521c49a43e0fae53840e5a636571ebfe (patch) | |
tree | c2054112d6fea2f102300f5d2c5743dd23f8a9f8 /ext/cache/01_cache.js | |
parent | 72ecfe04198c5e912826663033a8963fbdea4521 (diff) |
chore(ext/cache): remove CachePutResource in preparation for resource rewrite (#21949)
We can use `resourceForReadableStream` to ensure that cached resources
are implemented more efficiently and remove one more resource special
case.
Diffstat (limited to 'ext/cache/01_cache.js')
-rw-r--r-- | ext/cache/01_cache.js | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/ext/cache/01_cache.js b/ext/cache/01_cache.js index 541feb5c1..7d6e9e0ec 100644 --- a/ext/cache/01_cache.js +++ b/ext/cache/01_cache.js @@ -4,7 +4,6 @@ const { op_cache_delete, op_cache_match, op_cache_put, - op_cache_put_finish, op_cache_storage_delete, op_cache_storage_has, op_cache_storage_open, @@ -28,7 +27,11 @@ import { import { toInnerResponse } from "ext:deno_fetch/23_response.js"; import { URLPrototype } from "ext:deno_url/00_url.js"; import { getHeader } from "ext:deno_fetch/20_headers.js"; -import { readableStreamForRid } from "ext:deno_web/06_streams.js"; +import { + getReadableStreamResourceBacking, + readableStreamForRid, + resourceForReadableStream, +} from "ext:deno_web/06_streams.js"; class CacheStorage { constructor() { @@ -130,40 +133,37 @@ class Cache { if (innerResponse.body !== null && innerResponse.body.unusable()) { throw new TypeError("Response body is already used"); } - // acquire lock before async op - const reader = innerResponse.body?.stream.getReader(); + + const stream = innerResponse.body?.stream; + let rid = null; + if (stream) { + const resourceBacking = getReadableStreamResourceBacking( + innerResponse.body?.stream, + ); + if (resourceBacking) { + rid = resourceBacking.rid; + } else { + rid = resourceForReadableStream(stream, innerResponse.body?.length); + } + } // Remove fragment from request URL before put. reqUrl.hash = ""; // Step 9-11. - const rid = await op_cache_put( + // Step 12-19: TODO(@satyarohith): do the insertion in background. + await op_cache_put( { cacheId: this[_id], // deno-lint-ignore prefer-primordials requestUrl: reqUrl.toString(), responseHeaders: innerResponse.headerList, requestHeaders: innerRequest.headerList, - responseHasBody: innerResponse.body !== null, responseStatus: innerResponse.status, responseStatusText: innerResponse.statusMessage, + responseRid: rid, }, ); - if (reader) { - try { - while (true) { - const { value, done } = await reader.read(); - if (done) { - await op_cache_put_finish(rid); - break; - } - await core.writeAll(rid, value); - } - } finally { - core.close(rid); - } - } - // Step 12-19: TODO(@satyarohith): do the insertion in background. } /** See https://w3c.github.io/ServiceWorker/#cache-match */ |