0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(ext/cache): acquire reader lock before async op (#16126)

This commit is contained in:
Marcos Casagrande 2022-10-02 01:21:48 +02:00 committed by GitHub
parent 048c06f84f
commit a55b194638
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View file

@ -94,3 +94,25 @@ Deno.test(async function cacheApi() {
assert(await caches.delete(cacheName));
assertFalse(await caches.has(cacheName));
});
Deno.test(async function cachePutReaderLock() {
const cacheName = "cache-v1";
const cache = await caches.open(cacheName);
const response = new Response("consumed");
const promise = cache.put(
new Request("https://example.com/"),
response,
);
assertRejects(
async () => {
await response.arrayBuffer();
},
TypeError,
"Body already consumed.",
);
await promise;
});

View file

@ -119,8 +119,10 @@
// Step 8.
if (innerResponse.body !== null && innerResponse.body.unusable()) {
throw new TypeError("Response body must not already used");
throw new TypeError("Response body is already used");
}
// acquire lock before async op
const reader = innerResponse.body?.stream.getReader();
// Remove fragment from request URL before put.
reqUrl.hash = "";
@ -138,8 +140,7 @@
responseStatusText: innerResponse.statusMessage,
},
);
if (innerResponse.body) {
const reader = innerResponse.body.stream.getReader();
if (reader) {
while (true) {
const { value, done } = await reader.read();
if (done) {