mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -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(
|
await assertRejects(
|
||||||
async () => {
|
async () => {
|
||||||
await cache.put(
|
await cache.put(
|
||||||
new Request("https://example.com/"),
|
new Request("https://example.com/leak"),
|
||||||
new Response(stream),
|
new Response(stream),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -138,3 +138,30 @@ Deno.test(async function cachePutResourceLeak() {
|
||||||
"leak",
|
"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) {
|
while (true) {
|
||||||
const { value, done } = await reader.read();
|
const { value, done } = await reader.read();
|
||||||
if (done) {
|
if (done) {
|
||||||
|
await core.shutdown(rid);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await core.write(rid, value);
|
await core.write(rid, value);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await core.shutdown(rid);
|
|
||||||
core.close(rid);
|
core.close(rid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue