Skip to content

Commit b247295

Browse files
committed
Parallelize canonical R2 persistence
1 parent 42032cb commit b247295

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

packages/runtime-cloudflare/src/worker.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -392,23 +392,20 @@ async function readMarkdownRevision(bucket: R2Bucket, pointer: MarkdownPointer):
392392
const manifestObject = await bucket.get(pointer.manifestKey)
393393
if (!manifestObject) throw new Error(`R2 Markdown manifest is missing: ${pointer.manifestKey}`)
394394
const manifest = await manifestObject.json<MarkdownManifest>()
395-
const files: RuntimeFile[] = []
396-
for (const file of manifest.files) {
395+
return Promise.all(manifest.files.map(async (file): Promise<RuntimeFile> => {
397396
const object = await bucket.get(file.objectKey)
398397
if (!object) throw new Error(`R2 Markdown object is missing: ${file.objectKey}`)
399-
files.push({ path: file.path, bytes: new Uint8Array(await object.arrayBuffer()) })
400-
}
401-
return files
398+
return { path: file.path, bytes: new Uint8Array(await object.arrayBuffer()) }
399+
}))
402400
}
403401

404402
async function persistMarkdownRevision(bucket: R2Bucket, files: RuntimeFile[], current?: MarkdownPointer): Promise<MarkdownPointer> {
405-
const manifestFiles: MarkdownManifestFile[] = []
406-
for (const file of files) {
403+
const manifestFiles = await Promise.all(files.map(async (file): Promise<MarkdownManifestFile> => {
407404
const sha256 = await sha256Hex(file.bytes)
408405
const objectKey = `${R2_MARKDOWN_OBJECT_PREFIX}/${sha256}`
409406
if (!await bucket.head(objectKey)) await bucket.put(objectKey, file.bytes)
410-
manifestFiles.push({ path: file.path, objectKey, sha256, size: file.bytes.byteLength })
411-
}
407+
return { path: file.path, objectKey, sha256, size: file.bytes.byteLength }
408+
}))
412409
if (current) {
413410
const currentManifest = await readMarkdownManifest(bucket, current)
414411
if (currentManifest && JSON.stringify(currentManifest.files) === JSON.stringify(manifestFiles)) return current

0 commit comments

Comments
 (0)