Skip to content

Commit

Permalink
fix(ext/cache): prevent cache insert if body is not fully written
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed Oct 3, 2022
1 parent 7a77672 commit 08c0a7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion cli/tests/unit/cache_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,38 @@ 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),
);
},
Error,
"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 changes: 1 addition & 1 deletion ext/cache/01_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 08c0a7a

Please sign in to comment.