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:
parent
b5425ae2d3
commit
3a3a848406
2 changed files with 29 additions and 2 deletions
|
@ -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);
|
||||
});
|
||||
|
|
2
ext/cache/01_cache.js
vendored
2
ext/cache/01_cache.js
vendored
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue