-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(upload-client): allow large byte uploads
There was an issue with large byte uploads in tests. This commit fixes the issue by first converting bytes to UnixFS stream before making CAR object in packages/upload-client/test/helpers/car.js
- Loading branch information
1 parent
91d6c8e
commit 0a554cc
Showing
1 changed file
with
14 additions
and
11 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
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] }) | ||
} |