Skip to content

Commit

Permalink
test(upload-client): allow large byte uploads
Browse files Browse the repository at this point in the history
There was an issue with large byte uploads in tests.

This commit fixes the issue by first converting bytes to UnixFS stream before makeing CAR object in packages/upload-client/test/helpers/car.js
  • Loading branch information
StefanoDeVuono committed Aug 17, 2024
1 parent 91d6c8e commit 572cd3a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/upload-client/test/helpers/car.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { CarWriter } from '@ipld/car'
import * as CAR from '@ucanto/transport/car'
import { toBlock } from './block.js'
import { createFileEncoderStream } from '../../src/unixfs.js'

/**
* @param {Uint8Array} bytes
*/
export async function toCAR(bytes) {
const block = await toBlock(bytes)
// @ts-expect-error old multiformats in @ipld/car
const { writer, out } = CarWriter.create(block.cid)
writer.put(block)
const readableStream = ((createFileEncoderStream(new Blob([bytes]), {})))

let blocks = [], chunks = []
// @ts-expect-error readable Stream is also an async iterable
for await (let block of readableStream) blocks.push(block)
const rootCID = blocks.at(-1)?.cid

const { writer, out } = CarWriter.create(rootCID)

for (const block of blocks) writer.put(block)
writer.close()

const chunks = []
for await (const chunk of out) {
chunks.push(chunk)
}
for await (const chunk of out) chunks.push(chunk)

const blob = new Blob(chunks)
const cid = await CAR.codec.link(new Uint8Array(await blob.arrayBuffer()))

return Object.assign(blob, { cid, roots: [block.cid] })
return Object.assign(blob, { cid, roots: [rootCID] })
}

0 comments on commit 572cd3a

Please sign in to comment.