Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(upload-client): allow large byte uploads #1532

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] })
}
Loading