Skip to content

Commit 3bfb34d

Browse files
authored
fix: pass onProgress option to blockstore (#294)
To standardise progress reporting, onProgress is passed the blockstore
1 parent 203933a commit 3bfb34d

File tree

13 files changed

+61
-39
lines changed

13 files changed

+61
-39
lines changed

packages/ipfs-unixfs-exporter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
"it-pushable": "^3.1.0",
151151
"multiformats": "^11.0.0",
152152
"p-queue": "^7.3.0",
153+
"progress-events": "^1.0.0",
153154
"uint8arrays": "^4.0.2"
154155
},
155156
"devDependencies": {

packages/ipfs-unixfs-exporter/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import type { UnixFS } from 'ipfs-unixfs'
66
import type { PBNode } from '@ipld/dag-pb'
77
import type { Blockstore as InterfaceBlockstore } from 'interface-blockstore'
88
import type { Bucket } from 'hamt-sharding'
9+
import type { ProgressOptions } from 'progress-events'
910

10-
export interface ExporterOptions {
11+
export interface ExporterOptions extends ProgressOptions {
1112
offset?: number
1213
length?: number
1314
signal?: AbortSignal

packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as dagCbor from '@ipld/dag-cbor'
44
import type { Resolver } from '../index.js'
55

66
const resolve: Resolver = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
7-
const block = await blockstore.get(cid)
7+
const block = await blockstore.get(cid, options)
88
const object = dagCbor.decode<any>(block)
99
let subObject = object
1010
let subPath = path

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ async function walkDAG (blockstore: Blockstore, node: dagPb.PBNode | Uint8Array,
7272
childOps,
7373
(source) => map(source, (op) => {
7474
return async () => {
75-
const block = await blockstore.get(op.link.Hash, {
76-
signal: options.signal
77-
})
75+
const block = await blockstore.get(op.link.Hash, options)
7876

7977
return {
8078
...op,

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function * listDirectory (node: PBNode, path: string, resolve: Resolve, de
2727
return { entries: result.entry == null ? [] : [result.entry] }
2828
} else {
2929
// descend into subshard
30-
const block = await blockstore.get(link.Hash)
30+
const block = await blockstore.get(link.Hash, options)
3131
node = decode(block)
3232

3333
return { entries: listDirectory(node, path, resolve, depth, blockstore, options) }

packages/ipfs-unixfs-exporter/test/importer.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,13 @@ strategies.forEach((strategy) => {
634634
}
635635
})
636636

637-
it('will call an optional progress function', async () => {
637+
it('will call an optional onProgress function', async () => {
638638
const chunkSize = 2048
639639
const path = '1.2MiB.txt'
640-
const progress = sinon.stub()
640+
const onProgress = sinon.stub()
641641

642642
const options: Partial<ImporterOptions> = {
643-
progress,
643+
onProgress,
644644
chunker: fixedSize({
645645
chunkSize
646646
})
@@ -651,8 +651,9 @@ strategies.forEach((strategy) => {
651651
content: asAsyncIterable(bigFile)
652652
}], block, options))
653653

654-
expect(progress.called).to.equal(true)
655-
expect(progress.args[0]).to.deep.equal([chunkSize, path])
654+
expect(onProgress.called).to.equal(true)
655+
expect(onProgress.getCall(0).args[0]).to.have.property('type', 'unixfs:importer:progress')
656+
expect(onProgress.getCall(0).args[0]).to.have.deep.property('detail', { bytes: chunkSize, path })
656657
})
657658

658659
it('will import files with CID version 1', async () => {

packages/ipfs-unixfs-importer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,15 @@
163163
"@multiformats/murmur3": "^2.0.0",
164164
"err-code": "^3.0.1",
165165
"hamt-sharding": "^3.0.0",
166-
"ipfs-unixfs": "^11.0.0",
167166
"interface-blockstore": "^5.0.0",
168167
"interface-store": "^4.0.0",
168+
"ipfs-unixfs": "^11.0.0",
169169
"it-all": "^2.0.0",
170170
"it-batch": "^2.0.0",
171171
"it-first": "^2.0.0",
172172
"it-parallel-batch": "^2.0.0",
173173
"multiformats": "^11.0.0",
174+
"progress-events": "^1.0.0",
174175
"rabin-wasm": "^0.1.4",
175176
"uint8arraylist": "^2.4.3",
176177
"uint8arrays": "^4.0.2"

packages/ipfs-unixfs-importer/src/dag-builder/buffer-importer.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,46 @@ import { UnixFS } from 'ipfs-unixfs'
22
import { persist, PersistOptions } from '../utils/persist.js'
33
import * as dagPb from '@ipld/dag-pb'
44
import * as raw from 'multiformats/codecs/raw'
5-
import type { BufferImporter, ProgressHandler } from '../index.js'
5+
import type { BufferImporter } from '../index.js'
66
import type { Version } from 'multiformats/cid'
7+
import { CustomProgressEvent } from 'progress-events'
8+
import type { ProgressOptions, ProgressEvent } from 'progress-events'
79

8-
export interface BufferImporterOptions {
10+
/**
11+
* Passed to the onProgress callback while importing files
12+
*/
13+
export interface ImportProgressData {
14+
/**
15+
* The size of the current chunk
16+
*/
17+
bytes: number
18+
19+
/**
20+
* The path of the file being imported, if one was specified
21+
*/
22+
path?: string
23+
}
24+
25+
export type BufferImportProgressEvents =
26+
ProgressEvent<'unixfs:importer:progress', ImportProgressData>
27+
28+
export interface BufferImporterOptions extends ProgressOptions<BufferImportProgressEvents> {
929
cidVersion: Version
1030
rawLeaves: boolean
1131
leafType: 'file' | 'raw'
12-
progress?: ProgressHandler
1332
}
1433

1534
export function defaultBufferImporter (options: BufferImporterOptions): BufferImporter {
1635
return async function * bufferImporter (file, block) {
1736
for await (let buffer of file.content) {
1837
yield async () => {
19-
options.progress?.(buffer.length, file.path)
38+
options.onProgress?.(new CustomProgressEvent<ImportProgressData>('unixfs:importer:progress', { bytes: buffer.length, path: file.path }))
2039
let unixfs
2140

2241
const opts: PersistOptions = {
2342
codec: dagPb,
24-
cidVersion: options.cidVersion
43+
cidVersion: options.cidVersion,
44+
onProgress: options.onProgress
2545
}
2646

2747
if (options.rawLeaves) {

packages/ipfs-unixfs-importer/src/dag-builder/dir.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { persist } from '../utils/persist.js'
33
import { encode, prepare } from '@ipld/dag-pb'
44
import type { Directory, InProgressImportResult, Blockstore } from '../index.js'
55
import type { Version } from 'multiformats/cid'
6+
import type { ProgressOptions } from 'progress-events'
67

7-
export interface DirBuilderOptions {
8+
export interface DirBuilderOptions extends ProgressOptions {
89
cidVersion: Version
910
signal?: AbortSignal
1011
}

packages/ipfs-unixfs-importer/src/dag-builder/file.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as rawCodec from 'multiformats/codecs/raw'
66
import type { BufferImporter, File, InProgressImportResult, Blockstore } from '../index.js'
77
import type { FileLayout, Reducer } from '../layout/index.js'
88
import type { Version } from 'multiformats/cid'
9+
import type { ProgressOptions } from 'progress-events'
910

1011
interface BuildFileBatchOptions {
1112
bufferImporter: BufferImporter
@@ -36,7 +37,7 @@ async function * buildFileBatch (file: File, blockstore: Blockstore, options: Bu
3637
}
3738
}
3839

39-
interface ReduceOptions {
40+
interface ReduceOptions extends ProgressOptions {
4041
reduceSingleLeafToSelf: boolean
4142
cidVersion: Version
4243
signal?: AbortSignal

0 commit comments

Comments
 (0)