1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/cache): prevent cache insert if body is not fully written (#16138)

This commit is contained in:
Marcos Casagrande 2022-10-05 13:01:24 +02:00 committed by GitHub
parent b5425ae2d3
commit 3a3a848406
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -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);
});

View file

@ -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);
}
}