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:
parent
048c06f84f
commit
a55b194638
2 changed files with 26 additions and 3 deletions
|
@ -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;
|
||||
});
|
||||
|
|
7
ext/cache/01_cache.js
vendored
7
ext/cache/01_cache.js
vendored
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue