Skip to content

fix: use simpler blockstore interface #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2023
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
4 changes: 2 additions & 2 deletions packages/ipfs-unixfs-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"build": "aegir build",
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check -i interface-blockstore",
"dep-check": "aegir dep-check",
"release": "aegir release"
},
"dependencies": {
Expand All @@ -140,7 +140,7 @@
"@multiformats/murmur3": "^2.0.0",
"err-code": "^3.0.1",
"hamt-sharding": "^3.0.0",
"interface-blockstore": "^4.0.0",
"interface-blockstore": "^4.0.1",
"ipfs-unixfs": "^11.0.0",
"it-filter": "^2.0.0",
"it-last": "^2.0.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/ipfs-unixfs-exporter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import resolve from './resolvers/index.js'
import last from 'it-last'
import type { UnixFS } from 'ipfs-unixfs'
import type { PBNode } from '@ipld/dag-pb'
import type { Blockstore } from 'interface-blockstore'
import type { Blockstore as InterfaceBlockstore } from 'interface-blockstore'
import type { Bucket } from 'hamt-sharding'

export interface ExporterOptions {
Expand Down Expand Up @@ -79,6 +79,12 @@ export interface ShardTraversalContext {
lastBucket: Bucket<boolean>
}

export interface BlockstoreOptions {
signal?: AbortSignal
}

export type Blockstore = Pick<InterfaceBlockstore, 'has' | 'put' | 'get'>

const toPathComponents = (path: string = ''): string[] => {
// split on / unless escaped with \
return (path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import parallel from 'it-parallel'
import { pipe } from 'it-pipe'
import map from 'it-map'
import PQueue from 'p-queue'
import type { Blockstore } from 'interface-blockstore'
import type { ExporterOptions, UnixfsV1FileContent, UnixfsV1Resolver } from '../../../index.js'
import type { ExporterOptions, UnixfsV1FileContent, UnixfsV1Resolver, Blockstore } from '../../../index.js'

async function walkDAG (blockstore: Blockstore, node: dagPb.PBNode | Uint8Array, queue: Pushable<Uint8Array>, streamPosition: bigint, start: bigint, end: bigint, walkQueue: PQueue, options: ExporterOptions): Promise<void> {
// a `raw` node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ import parallel from 'it-parallel'
import { pipe } from 'it-pipe'
import map from 'it-map'
import { decode, PBNode } from '@ipld/dag-pb'
import type { Blockstore } from 'interface-blockstore'
import type { ExporterOptions, Resolve, UnixfsV1DirectoryContent, UnixfsV1Resolver } from '../../../index.js'

/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
* @typedef {import('../../../types').Resolve} Resolve
* @typedef {import('../../../types').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
* @typedef {import('../../../types').UnixfsV1Resolver} UnixfsV1Resolver
* @typedef {import('@ipld/dag-pb').PBNode} PBNode
*/
import type { ExporterOptions, Resolve, UnixfsV1DirectoryContent, UnixfsV1Resolver, Blockstore } from '../../../index.js'

const hamtShardedDirectoryContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth, blockstore) => {
function yieldHamtDirectoryContent (options: ExporterOptions = {}): UnixfsV1DirectoryContent {
Expand Down
3 changes: 1 addition & 2 deletions packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import { Bucket, BucketPosition, createHAMT } from 'hamt-sharding'
import { decode, PBLink, PBNode } from '@ipld/dag-pb'
import { murmur3128 } from '@multiformats/murmur3'
import type { Blockstore } from 'interface-blockstore'
import type { ExporterOptions, ShardTraversalContext } from '../index.js'
import type { ExporterOptions, ShardTraversalContext, Blockstore } from '../index.js'
import type { CID } from 'multiformats/cid'

// FIXME: this is copy/pasted from ipfs-unixfs-importer/src/options.js
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-unixfs-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@
"build": "aegir build",
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check -i interface-blockstore",
"dep-check": "aegir dep-check",
"release": "aegir release"
},
"dependencies": {
"@ipld/dag-pb": "^4.0.0",
"@multiformats/murmur3": "^2.0.0",
"err-code": "^3.0.1",
"hamt-sharding": "^3.0.0",
"interface-blockstore": "^4.0.0",
"ipfs-unixfs": "^11.0.0",
"interface-blockstore": "^4.0.1",
"it-all": "^2.0.0",
"it-batch": "^2.0.0",
"it-first": "^2.0.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/ipfs-unixfs-importer/src/dag-builder/dir.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { UnixFS } from 'ipfs-unixfs'
import { persist } from '../utils/persist.js'
import { encode, prepare } from '@ipld/dag-pb'
import type { Directory, InProgressImportResult } from '../index.js'
import type { Blockstore } from 'interface-blockstore'
import type { Directory, InProgressImportResult, Blockstore } from '../index.js'
import type { Version } from 'multiformats/cid'

export interface DirBuilderOptions {
Expand Down
7 changes: 3 additions & 4 deletions packages/ipfs-unixfs-importer/src/dag-builder/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { persist } from '../utils/persist.js'
import { encode, PBLink, prepare } from '@ipld/dag-pb'
import parallelBatch from 'it-parallel-batch'
import * as rawCodec from 'multiformats/codecs/raw'
import type { BufferImporter, File, InProgressImportResult } from '../index.js'
import type { Blockstore } from 'interface-blockstore'
import type { BufferImporter, File, InProgressImportResult, Blockstore } from '../index.js'
import type { FileLayout, Reducer } from '../layout/index.js'
import type { Version } from 'multiformats/cid'

Expand Down Expand Up @@ -49,9 +48,9 @@ const reduce = (file: File, blockstore: Blockstore, options: ReduceOptions): Red
const leaf = leaves[0]

if (file.mtime !== undefined || file.mode !== undefined) {
// only one leaf node which is a buffer - we have metadata so convert it into a
// only one leaf node which is a raw leaf - we have metadata so convert it into a
// UnixFS entry otherwise we'll have nowhere to store the metadata
let buffer = await blockstore.get(leaf.cid)
let buffer = await blockstore.get(leaf.cid, options)

leaf.unixfs = new UnixFS({
type: 'file',
Expand Down
3 changes: 1 addition & 2 deletions packages/ipfs-unixfs-importer/src/dag-builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { dirBuilder, DirBuilderOptions } from './dir.js'
import { fileBuilder, FileBuilderOptions } from './file.js'
import errCode from 'err-code'
import type { Directory, File, FileCandidate, ImportCandidate, InProgressImportResult } from '../index.js'
import type { Blockstore } from 'interface-blockstore'
import type { Directory, File, FileCandidate, ImportCandidate, InProgressImportResult, Blockstore } from '../index.js'
import type { ChunkValidator } from './validate-chunks.js'
import type { Chunker } from '../chunker/index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/ipfs-unixfs-importer/src/dir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Blockstore } from 'interface-blockstore'
import type { Blockstore, ImportResult, InProgressImportResult } from './index.js'
import type { Mtime, UnixFS } from 'ipfs-unixfs'
import { CID } from 'multiformats/cid'
import type { ImportResult, InProgressImportResult } from './index.js'
import type { PersistOptions } from './utils/persist.js'

export interface DirProps {
Expand Down
8 changes: 7 additions & 1 deletion packages/ipfs-unixfs-importer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DAGBuilder, defaultDagBuilder } from './dag-builder/index.js'
import { defaultTreeBuilder } from './tree-builder.js'
import type { UnixFS, Mtime } from 'ipfs-unixfs'
import type { CID, Version as CIDVersion } from 'multiformats/cid'
import type { Blockstore } from 'interface-blockstore'
import type { Blockstore as InterfaceBlockstore } from 'interface-blockstore'
import { ChunkValidator, defaultChunkValidator } from './dag-builder/validate-chunks.js'
import { fixedSize } from './chunker/fixed-size.js'
import type { Chunker } from './chunker/index.js'
Expand All @@ -16,6 +16,12 @@ import type { AwaitIterable } from 'blockstore-core/base'
export type ByteStream = AwaitIterable<Uint8Array>
export type ImportContent = ByteStream | Uint8Array

export interface BlockstoreOptions {
signal?: AbortSignal
}

export type Blockstore = Pick<InterfaceBlockstore, 'has' | 'put' | 'get'>

export interface FileCandidate {
path?: string
content: ImportContent
Expand Down
3 changes: 1 addition & 2 deletions packages/ipfs-unixfs-importer/src/tree-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { DirFlat } from './dir-flat.js'
import { flatToShard } from './flat-to-shard.js'
import { Dir } from './dir.js'
import { toPathComponents } from './utils/to-path-components.js'
import type { ImportResult, InProgressImportResult, TreeBuilder } from './index.js'
import type { Blockstore } from 'interface-blockstore'
import type { ImportResult, InProgressImportResult, TreeBuilder, Blockstore } from './index.js'
import type { PersistOptions } from './utils/persist.js'

export interface AddToTreeOptions extends PersistOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-importer/src/utils/persist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CID } from 'multiformats/cid'
import * as dagPb from '@ipld/dag-pb'
import { sha256 } from 'multiformats/hashes/sha2'
import type { Blockstore } from 'interface-blockstore'
import type { Blockstore } from '../index.js'
import type { BlockCodec } from 'multiformats/codecs/interface'
import type { Version as CIDVersion } from 'multiformats/cid'

Expand Down