-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { R2Bucket } from "@cloudflare/workers-types"; | ||
/** | ||
* Workaround for https://github.com/cloudflare/workers-sdk/issues/5360 | ||
*/ | ||
export function patchR2Bucket(bucket: R2Bucket) { | ||
let _mutex: Promise<any> | undefined; | ||
|
||
const _get = bucket.get.bind(bucket); | ||
|
||
async function getAndRead(...args: Parameters<R2Bucket["get"]>) { | ||
const obj = await _get(...args); | ||
if (!obj) { | ||
return obj; | ||
} | ||
const chunks: any[] = []; | ||
for await (const chunk of obj.body) { | ||
chunks.push(chunk); | ||
} | ||
const body = new ReadableStream({ | ||
start(controller) { | ||
for (const chunk of chunks) { | ||
controller.enqueue(chunk); | ||
} | ||
controller.close(); | ||
}, | ||
}); | ||
return { ...obj, body }; | ||
} | ||
|
||
async function get(...args: Parameters<R2Bucket["get"]>) { | ||
if (_mutex) { | ||
await _mutex; | ||
} | ||
try { | ||
_mutex = getAndRead(...args); | ||
const obj = await _mutex; | ||
return obj; | ||
} finally { | ||
_mutex = undefined; | ||
} | ||
} | ||
|
||
return new Proxy(bucket, { | ||
get(target, prop) { | ||
if (prop === "get") { | ||
return get; | ||
} | ||
return Reflect.get(target, prop); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters