Skip to content

Commit 70b8fa9

Browse files
authored
fix: use bytestream methods to add byte streams (#758)
Switch away from `addFile` as it will require paths after #754
1 parent afa9a1a commit 70b8fa9

File tree

4 files changed

+54
-51
lines changed

4 files changed

+54
-51
lines changed

packages/interop/src/car.spec.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createKuboNode } from './fixtures/create-kubo.js'
1212
import { memoryCarWriter } from './fixtures/memory-car.js'
1313
import type { Car } from '@helia/car'
1414
import type { HeliaLibp2p } from 'helia'
15-
import type { FileCandidate } from 'ipfs-unixfs-importer'
15+
import type { ByteStream, FileCandidate } from 'ipfs-unixfs-importer'
1616
import type { KuboNode } from 'ipfsd-ctl'
1717

1818
describe('@helia/car', () => {
@@ -46,18 +46,16 @@ describe('@helia/car', () => {
4646
const size = chunkSize * 10
4747
const input: Uint8Array[] = []
4848

49-
const candidate: FileCandidate = {
50-
content: (async function * () {
51-
for (let i = 0; i < size; i += chunkSize) {
52-
const buf = new Uint8Array(chunkSize)
53-
input.push(buf)
49+
const bytes: ByteStream = (async function * () {
50+
for (let i = 0; i < size; i += chunkSize) {
51+
const buf = new Uint8Array(chunkSize)
52+
input.push(buf)
5453

55-
yield buf
56-
}
57-
}())
58-
}
54+
yield buf
55+
}
56+
}())
5957

60-
const cid = await u.addFile(candidate)
58+
const cid = await u.addByteStream(bytes)
6159
const writer = memoryCarWriter(cid)
6260
await c.export(cid, writer)
6361

packages/interop/src/unixfs-bitswap.spec.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CID } from 'multiformats/cid'
77
import { createHeliaNode } from './fixtures/create-helia.js'
88
import { createKuboNode } from './fixtures/create-kubo.js'
99
import type { HeliaLibp2p } from 'helia'
10-
import type { FileCandidate } from 'ipfs-unixfs-importer'
10+
import type { ByteStream, FileCandidate } from 'ipfs-unixfs-importer'
1111
import type { KuboNode } from 'ipfsd-ctl'
1212

1313
describe('@helia/unixfs - bitswap', () => {
@@ -39,22 +39,20 @@ describe('@helia/unixfs - bitswap', () => {
3939
const size = chunkSize * 10
4040
const input: Uint8Array[] = []
4141

42-
const candidate: FileCandidate = {
43-
content: (async function * () {
44-
for (let i = 0; i < size; i += chunkSize) {
45-
const buf = new Uint8Array(chunkSize)
46-
input.push(buf)
42+
const bytes: ByteStream = (async function * () {
43+
for (let i = 0; i < size; i += chunkSize) {
44+
const buf = new Uint8Array(chunkSize)
45+
input.push(buf)
4746

48-
yield buf
49-
}
50-
}())
51-
}
47+
yield buf
48+
}
49+
}())
5250

53-
const cid = await unixFs.addFile(candidate)
51+
const cid = await unixFs.addByteStream(bytes)
5452

55-
const bytes = await toBuffer(kubo.api.cat(CID.parse(cid.toString())))
53+
const output = await toBuffer(kubo.api.cat(CID.parse(cid.toString())))
5654

57-
expect(bytes).to.equalBytes(toBuffer(input))
55+
expect(output).to.equalBytes(toBuffer(input))
5856
})
5957

6058
it('should add a large file to kubo and fetch it from helia', async () => {

packages/interop/src/unixfs-files.spec.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* eslint-env mocha */
22

3-
import { type AddOptions, type UnixFS, unixfs } from '@helia/unixfs'
3+
import { unixfs } from '@helia/unixfs'
44
import { expect } from 'aegir/chai'
55
import { fixedSize } from 'ipfs-unixfs-importer/chunker'
66
import { balanced } from 'ipfs-unixfs-importer/layout'
77
import { CID } from 'multiformats/cid'
88
import { createHeliaNode } from './fixtures/create-helia.js'
99
import { createKuboNode } from './fixtures/create-kubo.js'
10+
import type { AddOptions, UnixFS } from '@helia/unixfs'
1011
import type { HeliaLibp2p } from 'helia'
11-
import type { FileCandidate } from 'ipfs-unixfs-importer'
12+
import type { ByteStream } from 'ipfs-unixfs-importer'
1213
import type { KuboNode } from 'ipfsd-ctl'
1314
import type { AddOptions as KuboAddOptions } from 'kubo-rpc-client'
1415

@@ -17,19 +18,19 @@ describe('@helia/unixfs - files', () => {
1718
let unixFs: UnixFS
1819
let kubo: KuboNode
1920

20-
async function importToHelia (data: FileCandidate, opts?: Partial<AddOptions>): Promise<CID> {
21-
const cid = await unixFs.addFile(data, opts)
21+
async function importToHelia (data: ByteStream, opts?: Partial<AddOptions>): Promise<CID> {
22+
const cid = await unixFs.addByteStream(data, opts)
2223

2324
return cid
2425
}
2526

26-
async function importToKubo (data: FileCandidate, opts?: KuboAddOptions): Promise<CID> {
27-
const result = await kubo.api.add(data.content, opts)
27+
async function importToKubo (data: ByteStream, opts?: KuboAddOptions): Promise<CID> {
28+
const result = await kubo.api.add(data, opts)
2829

2930
return CID.parse(result.cid.toString())
3031
}
3132

32-
async function expectSameCid (data: () => FileCandidate, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
33+
async function expectSameCid (data: () => ByteStream, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
3334
const heliaCid = await importToHelia(data(), {
3435
// these are the default kubo options
3536
cidVersion: 0,
@@ -65,9 +66,9 @@ describe('@helia/unixfs - files', () => {
6566
})
6667

6768
it('should create the same CID for a small file', async () => {
68-
const candidate = (): FileCandidate => ({
69-
content: Uint8Array.from([0, 1, 2, 3, 4])
70-
})
69+
const candidate = (): ByteStream => (async function * () {
70+
yield Uint8Array.from([0, 1, 2, 3, 4])
71+
}())
7172

7273
await expectSameCid(candidate)
7374
})
@@ -76,13 +77,11 @@ describe('@helia/unixfs - files', () => {
7677
const chunkSize = 1024 * 1024
7778
const size = chunkSize * 10
7879

79-
const candidate = (): FileCandidate => ({
80-
content: (async function * () {
81-
for (let i = 0; i < size; i += chunkSize) {
82-
yield new Uint8Array(chunkSize)
83-
}
84-
}())
85-
})
80+
const candidate = (): ByteStream => (async function * () {
81+
for (let i = 0; i < size; i += chunkSize) {
82+
yield new Uint8Array(chunkSize)
83+
}
84+
}())
8685

8786
await expectSameCid(candidate)
8887
})

packages/mfs/src/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,31 @@ class DefaultMFS implements MFS {
277277
}
278278

279279
async writeBytes (bytes: Uint8Array, path: string, options?: Partial<WriteOptions>): Promise<void> {
280-
const cid = await this.unixfs.addFile({
281-
content: bytes,
282-
mode: options?.mode,
283-
mtime: options?.mtime
284-
}, options)
280+
const cid = await this.unixfs.addBytes(bytes, options)
285281

286282
await this.cp(cid, path, options)
283+
284+
if (options?.mode != null) {
285+
await this.chmod(path, options.mode, options)
286+
}
287+
288+
if (options?.mtime != null) {
289+
await this.touch(path, options)
290+
}
287291
}
288292

289293
async writeByteStream (bytes: ByteStream, path: string, options?: Partial<WriteOptions>): Promise<void> {
290-
const cid = await this.unixfs.addFile({
291-
content: bytes,
292-
mode: options?.mode,
293-
mtime: options?.mtime
294-
}, options)
294+
const cid = await this.unixfs.addByteStream(bytes, options)
295295

296296
await this.cp(cid, path, options)
297+
298+
if (options?.mode != null) {
299+
await this.chmod(path, options.mode, options)
300+
}
301+
302+
if (options?.mtime != null) {
303+
await this.touch(path, options)
304+
}
297305
}
298306

299307
async * cat (path: string, options: Partial<CatOptions> = {}): AsyncIterable<Uint8Array> {

0 commit comments

Comments
 (0)