Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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