Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Feb 21, 2024
1 parent 73fabee commit 749d391
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 54 deletions.
27 changes: 27 additions & 0 deletions packages/car/test/fixtures/dag-walkers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as dagPb from '@ipld/dag-pb'
import * as raw from 'multiformats/codecs/raw'
import type { DAGWalker } from '@helia/interface'

/**
* Dag walker for dag-pb CIDs
*/
const dagPbWalker: DAGWalker = {
codec: dagPb.code,
* walk (block) {
const node = dagPb.decode(block)

yield * node.Links.map(l => l.Hash)
}
}

const rawWalker: DAGWalker = {
codec: raw.code,
* walk () {
// no embedded CIDs in a raw block
}
}

export const dagWalkers = {
[dagPb.code]: dagPbWalker,
[raw.code]: rawWalker
}
28 changes: 1 addition & 27 deletions packages/car/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,23 @@

import { type UnixFS, unixfs } from '@helia/unixfs'
import { CarReader } from '@ipld/car'
import * as dagPb from '@ipld/dag-pb'
import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import { fixedSize } from 'ipfs-unixfs-importer/chunker'
import toBuffer from 'it-to-buffer'
import * as raw from 'multiformats/codecs/raw'
import { car, type Car } from '../src/index.js'
import { dagWalkers } from './fixtures/dag-walkers.js'
import { largeFile, smallFile } from './fixtures/files.js'
import { memoryCarWriter } from './fixtures/memory-car.js'
import type { DAGWalker } from '@helia/interface'
import type { Blockstore } from 'interface-blockstore'

/**
* Dag walker for dag-pb CIDs
*/
const dagPbWalker: DAGWalker = {
codec: dagPb.code,
* walk (block) {
const node = dagPb.decode(block)

yield * node.Links.map(l => l.Hash)
}
}

const rawWalker: DAGWalker = {
codec: raw.code,
* walk () {
// no embedded CIDs in a raw block
}
}

describe('import/export car file', () => {
let blockstore: Blockstore
let c: Car
let u: UnixFS
let dagWalkers: Record<number, DAGWalker>

beforeEach(async () => {
blockstore = new MemoryBlockstore()
dagWalkers = {
[dagPb.code]: dagPbWalker,
[raw.code]: rawWalker
}

c = car({ blockstore, dagWalkers })
u = unixfs({ blockstore })
Expand Down
28 changes: 1 addition & 27 deletions packages/car/test/stream.spec.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,22 @@
/* eslint-env mocha */

import { type UnixFS, unixfs } from '@helia/unixfs'
import * as dagPb from '@ipld/dag-pb'
import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import toBuffer from 'it-to-buffer'
import * as raw from 'multiformats/codecs/raw'
import { car, type Car } from '../src/index.js'
import { dagWalkers } from './fixtures/dag-walkers.js'
import { smallFile } from './fixtures/files.js'
import { memoryCarWriter } from './fixtures/memory-car.js'
import type { DAGWalker } from '@helia/interface'
import type { Blockstore } from 'interface-blockstore'

/**
* Dag walker for dag-pb CIDs
*/
const dagPbWalker: DAGWalker = {
codec: dagPb.code,
* walk (block) {
const node = dagPb.decode(block)

yield * node.Links.map(l => l.Hash)
}
}

const rawWalker: DAGWalker = {
codec: raw.code,
* walk () {
// no embedded CIDs in a raw block
}
}

describe('stream car file', () => {
let blockstore: Blockstore
let c: Car
let u: UnixFS
let dagWalkers: Record<number, DAGWalker>

beforeEach(async () => {
blockstore = new MemoryBlockstore()
dagWalkers = {
[dagPb.code]: dagPbWalker,
[raw.code]: rawWalker
}

c = car({ blockstore, dagWalkers })
u = unixfs({ blockstore })
Expand Down

0 comments on commit 749d391

Please sign in to comment.