Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 9 additions & 11 deletions packages/interop/src/car.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 11 additions & 13 deletions packages/interop/src/unixfs-bitswap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
33 changes: 16 additions & 17 deletions packages/interop/src/unixfs-files.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

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

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

return cid
}

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

return CID.parse(result.cid.toString())
}

async function expectSameCid (data: () => FileCandidate, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
async function expectSameCid (data: () => ByteStream, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
const heliaCid = await importToHelia(data(), {
// these are the default kubo options
cidVersion: 0,
Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down
28 changes: 18 additions & 10 deletions packages/mfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,31 @@
}

async writeBytes (bytes: Uint8Array, path: string, options?: Partial<WriteOptions>): Promise<void> {
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<WriteOptions>): Promise<void> {
const cid = await this.unixfs.addFile({
content: bytes,
mode: options?.mode,
mtime: options?.mtime
}, options)
const cid = await this.unixfs.addByteStream(bytes, options)

Check warning on line 294 in packages/mfs/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/mfs/src/index.ts#L294

Added line #L294 was not covered by tests

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)
}

Check warning on line 304 in packages/mfs/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/mfs/src/index.ts#L297-L304

Added lines #L297 - L304 were not covered by tests
}

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