diff --git a/packages/interop/src/car.spec.ts b/packages/interop/src/car.spec.ts index 174debd46..d7bc5910e 100644 --- a/packages/interop/src/car.spec.ts +++ b/packages/interop/src/car.spec.ts @@ -12,7 +12,7 @@ import { createKuboNode } from './fixtures/create-kubo.js' import { memoryCarWriter } from './fixtures/memory-car.js' import type { Car } from '@helia/car' import type { HeliaLibp2p } from 'helia' -import type { FileCandidate } from 'ipfs-unixfs-importer' +import type { ByteStream, FileCandidate } from 'ipfs-unixfs-importer' import type { KuboNode } from 'ipfsd-ctl' describe('@helia/car', () => { @@ -46,18 +46,16 @@ describe('@helia/car', () => { const size = chunkSize * 10 const input: Uint8Array[] = [] - const candidate: FileCandidate = { - content: (async function * () { - for (let i = 0; i < size; i += chunkSize) { - const buf = new Uint8Array(chunkSize) - input.push(buf) + const bytes: ByteStream = (async function * () { + for (let i = 0; i < size; i += chunkSize) { + const buf = new Uint8Array(chunkSize) + input.push(buf) - yield buf - } - }()) - } + yield buf + } + }()) - const cid = await u.addFile(candidate) + const cid = await u.addByteStream(bytes) const writer = memoryCarWriter(cid) await c.export(cid, writer) diff --git a/packages/interop/src/unixfs-bitswap.spec.ts b/packages/interop/src/unixfs-bitswap.spec.ts index 03b1e1ade..03fbe38c8 100644 --- a/packages/interop/src/unixfs-bitswap.spec.ts +++ b/packages/interop/src/unixfs-bitswap.spec.ts @@ -7,7 +7,7 @@ import { CID } from 'multiformats/cid' import { createHeliaNode } from './fixtures/create-helia.js' import { createKuboNode } from './fixtures/create-kubo.js' import type { HeliaLibp2p } from 'helia' -import type { FileCandidate } from 'ipfs-unixfs-importer' +import type { ByteStream, FileCandidate } from 'ipfs-unixfs-importer' import type { KuboNode } from 'ipfsd-ctl' describe('@helia/unixfs - bitswap', () => { @@ -39,22 +39,20 @@ describe('@helia/unixfs - bitswap', () => { const size = chunkSize * 10 const input: Uint8Array[] = [] - const candidate: FileCandidate = { - content: (async function * () { - for (let i = 0; i < size; i += chunkSize) { - const buf = new Uint8Array(chunkSize) - input.push(buf) + const bytes: ByteStream = (async function * () { + for (let i = 0; i < size; i += chunkSize) { + const buf = new Uint8Array(chunkSize) + input.push(buf) - yield buf - } - }()) - } + yield buf + } + }()) - const cid = await unixFs.addFile(candidate) + const cid = await unixFs.addByteStream(bytes) - const bytes = await toBuffer(kubo.api.cat(CID.parse(cid.toString()))) + const output = await toBuffer(kubo.api.cat(CID.parse(cid.toString()))) - expect(bytes).to.equalBytes(toBuffer(input)) + expect(output).to.equalBytes(toBuffer(input)) }) it('should add a large file to kubo and fetch it from helia', async () => { diff --git a/packages/interop/src/unixfs-files.spec.ts b/packages/interop/src/unixfs-files.spec.ts index 696a56b01..cb1dc7361 100644 --- a/packages/interop/src/unixfs-files.spec.ts +++ b/packages/interop/src/unixfs-files.spec.ts @@ -1,14 +1,15 @@ /* eslint-env mocha */ -import { type AddOptions, type UnixFS, unixfs } from '@helia/unixfs' +import { unixfs } from '@helia/unixfs' import { expect } from 'aegir/chai' import { fixedSize } from 'ipfs-unixfs-importer/chunker' import { balanced } from 'ipfs-unixfs-importer/layout' import { CID } from 'multiformats/cid' import { createHeliaNode } from './fixtures/create-helia.js' import { createKuboNode } from './fixtures/create-kubo.js' +import type { AddOptions, UnixFS } from '@helia/unixfs' import type { HeliaLibp2p } from 'helia' -import type { FileCandidate } from 'ipfs-unixfs-importer' +import type { ByteStream } from 'ipfs-unixfs-importer' import type { KuboNode } from 'ipfsd-ctl' import type { AddOptions as KuboAddOptions } from 'kubo-rpc-client' @@ -17,19 +18,19 @@ describe('@helia/unixfs - files', () => { let unixFs: UnixFS let kubo: KuboNode - async function importToHelia (data: FileCandidate, opts?: Partial): Promise { - const cid = await unixFs.addFile(data, opts) + async function importToHelia (data: ByteStream, opts?: Partial): Promise { + const cid = await unixFs.addByteStream(data, opts) return cid } - async function importToKubo (data: FileCandidate, opts?: KuboAddOptions): Promise { - const result = await kubo.api.add(data.content, opts) + async function importToKubo (data: ByteStream, opts?: KuboAddOptions): Promise { + const result = await kubo.api.add(data, opts) return CID.parse(result.cid.toString()) } - async function expectSameCid (data: () => FileCandidate, heliaOpts: Partial = {}, kuboOpts: KuboAddOptions = {}): Promise { + async function expectSameCid (data: () => ByteStream, heliaOpts: Partial = {}, kuboOpts: KuboAddOptions = {}): Promise { const heliaCid = await importToHelia(data(), { // these are the default kubo options cidVersion: 0, @@ -65,9 +66,9 @@ describe('@helia/unixfs - files', () => { }) it('should create the same CID for a small file', async () => { - const candidate = (): FileCandidate => ({ - content: Uint8Array.from([0, 1, 2, 3, 4]) - }) + const candidate = (): ByteStream => (async function * () { + yield Uint8Array.from([0, 1, 2, 3, 4]) + }()) await expectSameCid(candidate) }) @@ -76,13 +77,11 @@ describe('@helia/unixfs - files', () => { const chunkSize = 1024 * 1024 const size = chunkSize * 10 - const candidate = (): FileCandidate => ({ - content: (async function * () { - for (let i = 0; i < size; i += chunkSize) { - yield new Uint8Array(chunkSize) - } - }()) - }) + const candidate = (): ByteStream => (async function * () { + for (let i = 0; i < size; i += chunkSize) { + yield new Uint8Array(chunkSize) + } + }()) await expectSameCid(candidate) }) diff --git a/packages/mfs/src/index.ts b/packages/mfs/src/index.ts index 32a1d69d3..1c50f2df2 100644 --- a/packages/mfs/src/index.ts +++ b/packages/mfs/src/index.ts @@ -277,23 +277,31 @@ class DefaultMFS implements MFS { } async writeBytes (bytes: Uint8Array, path: string, options?: Partial): Promise { - const cid = await this.unixfs.addFile({ - content: bytes, - mode: options?.mode, - mtime: options?.mtime - }, options) + const cid = await this.unixfs.addBytes(bytes, options) await this.cp(cid, path, options) + + if (options?.mode != null) { + await this.chmod(path, options.mode, options) + } + + if (options?.mtime != null) { + await this.touch(path, options) + } } async writeByteStream (bytes: ByteStream, path: string, options?: Partial): Promise { - const cid = await this.unixfs.addFile({ - content: bytes, - mode: options?.mode, - mtime: options?.mtime - }, options) + const cid = await this.unixfs.addByteStream(bytes, options) await this.cp(cid, path, options) + + if (options?.mode != null) { + await this.chmod(path, options.mode, options) + } + + if (options?.mtime != null) { + await this.touch(path, options) + } } async * cat (path: string, options: Partial = {}): AsyncIterable {