summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2022-10-05 13:01:24 +0200
committerGitHub <noreply@github.com>2022-10-05 16:31:24 +0530
commit3a3a8484069c9c6955fcb83ea761f9f74638175a (patch)
tree8040699890d5142126f10b7cff3ee3bb429f5f16
parentb5425ae2d37d3dd123cbc0430785c0f61082e3e3 (diff)
fix(ext/cache): prevent cache insert if body is not fully written (#16138)
-rw-r--r--cli/tests/unit/cache_api_test.ts29
-rw-r--r--ext/cache/01_cache.js2
2 files changed, 29 insertions, 2 deletions
diff --git a/cli/tests/unit/cache_api_test.ts b/cli/tests/unit/cache_api_test.ts
index 16996c821..7bc372003 100644
--- a/cli/tests/unit/cache_api_test.ts
+++ b/cli/tests/unit/cache_api_test.ts
@@ -130,7 +130,7 @@ Deno.test(async function cachePutResourceLeak() {
await assertRejects(
async () => {
await cache.put(
- new Request("https://example.com/"),
+ new Request("https://example.com/leak"),
new Response(stream),
);
},
@@ -138,3 +138,30 @@ Deno.test(async function cachePutResourceLeak() {
"leak",
);
});
+
+Deno.test(async function cachePutFailedBody() {
+ const cacheName = "cache-v1";
+ const cache = await caches.open(cacheName);
+
+ const request = new Request("https://example.com/failed-body");
+ const stream = new ReadableStream({
+ start(controller) {
+ controller.error(new Error("corrupt"));
+ },
+ });
+
+ await assertRejects(
+ async () => {
+ await cache.put(
+ request,
+ new Response(stream),
+ );
+ },
+ Error,
+ "corrupt",
+ );
+
+ const response = await cache.match(request);
+ // if it fails to read the body, the cache should be empty
+ assertEquals(response, undefined);
+});
diff --git a/ext/cache/01_cache.js b/ext/cache/01_cache.js
index c22390a6d..fa0b68037 100644
--- a/ext/cache/01_cache.js
+++ b/ext/cache/01_cache.js
@@ -145,12 +145,12 @@
while (true) {
const { value, done } = await reader.read();
if (done) {
+ await core.shutdown(rid);
break;
}
await core.write(rid, value);
}
} finally {
- await core.shutdown(rid);
core.close(rid);
}
}