Skip to content

Commit ca57bf8

Browse files
committed
chore: re-add dynamic imports and remove added ts ignores
1 parent 733ccac commit ca57bf8

File tree

12 files changed

+72
-78
lines changed

12 files changed

+72
-78
lines changed

packages/ipfs-unixfs-exporter/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"fs": false
1010
},
1111
"scripts": {
12-
"prepare": "aegir build",
12+
"prepare": "aegir build --no-bundle",
1313
"test": "aegir test",
1414
"build": "aegir build",
1515
"clean": "rimraf ./dist",

packages/ipfs-unixfs-exporter/src/resolvers/index.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@ import * as dagCbor from '@ipld/dag-cbor'
55
import * as raw from 'multiformats/codecs/raw'
66
import { identity } from 'multiformats/hashes/identity'
77

8-
// TODO: Lazy Load
9-
import unixFs1Resolver from './unixfs-v1/index.js'
10-
import rawResolver from './raw.js'
11-
import dagCborResolver from './dag-cbor.js'
12-
import identityResolver from './identity.js'
13-
148
/**
159
* @typedef {import('../types').Resolver} Resolver
1610
* @typedef {import('../types').Resolve} Resolve
1711
*/
1812

1913
/**
20-
* @type {{ [ key: string ]: Resolver }}
14+
* @param {number} key
15+
* @returns {Promise<Resolver|undefined>}
2116
*/
22-
const resolvers = {
23-
[dagPb.code]: unixFs1Resolver,
24-
[raw.code]: rawResolver,
25-
[dagCbor.code]: dagCborResolver,
26-
[identity.code]: identityResolver
17+
const importResolver = async (key) => {
18+
switch (key) {
19+
case dagPb.code:
20+
return (await (import('./unixfs-v1/index.js'))).default
21+
case raw.code:
22+
return (await (import('./raw.js'))).default
23+
case dagCbor.code:
24+
return (await (import('./dag-cbor.js'))).default
25+
case identity.code:
26+
return (await (import('./identity.js'))).default
27+
default:
28+
return
29+
}
2730
}
2831

2932
/**
3033
* @type {Resolve}
3134
*/
32-
function resolve (cid, name, path, toResolve, depth, blockstore, options) {
33-
const resolver = resolvers[cid.code]
35+
async function resolve (cid, name, path, toResolve, depth, blockstore, options) {
36+
const resolver = await importResolver(cid.code)
3437

3538
if (!resolver) {
3639
throw errCode(new Error(`No resolver for code ${cid.code}`), 'ERR_NO_RESOLVER')

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { UnixFS } from 'ipfs-unixfs'
33
import findShardCid from '../../utils/find-cid-in-shard.js'
44
import { decode } from '@ipld/dag-pb'
55

6-
// TODO Lazy load
7-
import contentFile from './content/file.js'
8-
import contentDirectory from './content/directory.js'
9-
import contentHamtShardedDirectory from './content/hamt-sharded-directory.js'
10-
116
/**
127
* @typedef {import('../../types').Resolve} Resolve
138
* @typedef {import('../../types').Resolver} Resolver
@@ -26,18 +21,23 @@ const findLinkCid = (node, name) => {
2621
}
2722

2823
/**
29-
* @type {{ [key: string]: UnixfsV1Resolver }}
24+
* @param {string} key
25+
* @returns {Promise<UnixfsV1Resolver|undefined>}
3026
*/
31-
const contentExporters = {
32-
raw: contentFile,
33-
file: contentFile,
34-
directory: contentDirectory,
35-
'hamt-sharded-directory': contentHamtShardedDirectory,
36-
metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => {
37-
return () => []
38-
},
39-
symlink: (cid, node, unixfs, path, resolve, depth, blockstore) => {
40-
return () => []
27+
const importContentExporters = async (key) => {
28+
switch (key) {
29+
case 'raw':
30+
case 'file':
31+
return (await (import('./content/file.js'))).default
32+
case 'directory':
33+
return (await (import('./content/directory.js'))).default
34+
case 'hamt-sharded-directory':
35+
return (await (import('./content/hamt-sharded-directory.js'))).default
36+
case 'metadata':
37+
case 'symlink':
38+
return () => () => []
39+
default:
40+
return
4141
}
4242
}
4343

@@ -95,14 +95,16 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks
9595
}
9696
}
9797

98+
const contentExporter = await importContentExporters(unixfs.type)
99+
98100
return {
99101
entry: {
100102
type: unixfs.isDirectory() ? 'directory' : 'file',
101103
name,
102104
path,
103105
cid,
104106
// @ts-ignore
105-
content: contentExporters[unixfs.type](cid, node, unixfs, path, resolve, depth, blockstore),
107+
content: contentExporter(cid, node, unixfs, path, resolve, depth, blockstore),
106108
unixfs,
107109
depth,
108110
node,

packages/ipfs-unixfs-exporter/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "aegir/src/config/tsconfig.aegir.json",
33
"compilerOptions": {
44
"outDir": "dist",
5+
"module": "es2020",
56
"importsNotUsedAsValues": "preserve"
67
},
78
"include": [

packages/ipfs-unixfs-importer/src/chunker/index.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import errCode from 'err-code'
22

3-
// TODO: lazy load
4-
import rabinChunker from './rabin.js'
5-
import fixedSizeChunker from './fixed-size.js'
6-
73
/**
84
* @typedef {import('../types').ImporterOptions} ImporterOptions
95
* @typedef {import('../types').Chunker} Chunker
106
*/
117

128
/**
13-
* @type {{ [key: string]: Chunker }}
9+
* @param {string} key
10+
* @returns {Promise<Chunker|undefined>}
1411
*/
15-
const chunkers = {
16-
fixed: fixedSizeChunker,
17-
rabin: rabinChunker
12+
const importChunkers = async (key) => {
13+
switch (key) {
14+
case 'fixed':
15+
return (await (import('./fixed-size.js'))).default
16+
case 'rabin':
17+
return (await (import('./rabin.js'))).default
18+
default:
19+
}
1820
}
1921

2022
/**
2123
* @param {import('../types').ChunkerType} type
2224
* @param {AsyncIterable<Uint8Array>} source
2325
* @param {import('../types').ImporterOptions} options
2426
*/
25-
module.exports = (type, source, options) => {
26-
const chunker = chunkers[type]
27+
module.exports = async (type, source, options) => {
28+
const chunker = await importChunkers(type)
2729

2830
if (!chunker) {
2931
throw errCode(new Error(`Unknkown chunker named ${type}`), 'ERR_UNKNOWN_CHUNKER')

packages/ipfs-unixfs-importer/src/dag-builder/file/index.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import parallelBatch from 'it-parallel-batch'
66
import * as rawCodec from 'multiformats/codecs/raw'
77
import * as dagPb from '@ipld/dag-pb'
88

9-
// TODO: Lazy load
10-
import flatBuilder from './flat.js'
11-
import balancedBuilder from './balanced.js'
12-
import trickleBuilder from './trickle.js'
13-
import bufImporter from './buffer-importer.js'
14-
159
/**
1610
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1711
* @typedef {import('../../types').File} File
@@ -22,12 +16,19 @@ import bufImporter from './buffer-importer.js'
2216
*/
2317

2418
/**
25-
* @type {{ [key: string]: FileDAGBuilder}}
19+
* @param {string} key
20+
* @returns {Promise<FileDAGBuilder|undefined>}
2621
*/
27-
const dagBuilders = {
28-
flat: flatBuilder,
29-
balanced: balancedBuilder,
30-
trickle: trickleBuilder
22+
const importDagBuilder = async (key) => {
23+
switch (key) {
24+
case 'flat':
25+
return (await (import('./flat.js'))).default
26+
case 'balanced':
27+
return (await (import('./balanced.js'))).default
28+
case 'trickle':
29+
return (await (import('./trickle.js'))).default
30+
default:
31+
}
3132
}
3233

3334
/**
@@ -43,7 +44,7 @@ async function * buildFileBatch (file, blockstore, options) {
4344
if (typeof options.bufferImporter === 'function') {
4445
bufferImporter = options.bufferImporter
4546
} else {
46-
bufferImporter = bufImporter
47+
bufferImporter = (await (import('./buffer-importer.js'))).default
4748
}
4849

4950
for await (const entry of parallelBatch(bufferImporter(file, blockstore, options), options.blockWriteConcurrency)) {
@@ -196,8 +197,8 @@ const reduce = (file, blockstore, options) => {
196197
/**
197198
* @type {import('../../types').UnixFSV1DagBuilder<File>}
198199
*/
199-
function fileBuilder (file, block, options) {
200-
const dagBuilder = dagBuilders[options.strategy]
200+
async function fileBuilder (file, block, options) {
201+
const dagBuilder = await importDagBuilder(options.strategy)
201202

202203
if (!dagBuilder) {
203204
throw errCode(new Error(`Unknown importer build strategy name: ${options.strategy}`), 'ERR_BAD_STRATEGY')

packages/ipfs-unixfs-importer/src/dag-builder/index.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import dirBuilder from './dir.js'
22
import fileBuilder from './file/index.js'
33
import errCode from 'err-code'
44

5-
// TODO: dynamic imports
6-
import validateChunks from './validate-chunks.js'
7-
import rabinChunker from '../chunker/rabin.js'
8-
import fixedSizeChunker from '../chunker/fixed-size.js'
9-
105
/**
116
* @typedef {import('../types').File} File
127
* @typedef {import('../types').Directory} Directory
@@ -80,9 +75,9 @@ async function * dagBuilder (source, blockstore, options) {
8075
if (typeof options.chunker === 'function') {
8176
chunker = options.chunker
8277
} else if (options.chunker === 'rabin') {
83-
chunker = rabinChunker
78+
chunker = (await (import('../chunker/rabin.js'))).default
8479
} else {
85-
chunker = fixedSizeChunker
80+
chunker = (await (import('../chunker/fixed-size.js'))).default
8681
}
8782

8883
/**
@@ -93,7 +88,7 @@ async function * dagBuilder (source, blockstore, options) {
9388
if (typeof options.chunkValidator === 'function') {
9489
chunkValidator = options.chunkValidator
9590
} else {
96-
chunkValidator = validateChunks
91+
chunkValidator = (await (import('./validate-chunks.js'))).default
9792
}
9893

9994
/** @type {File} */

packages/ipfs-unixfs-importer/src/dir.js

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
class Dir {
2121
/**
22-
*
2322
* @param {DirProps} props
2423
* @param {ImporterOptions} options
2524
*/

packages/ipfs-unixfs-importer/src/flat-to-shard.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import DirSharded from './dir-sharded.js'
22
import DirFlat from './dir-flat.js'
33

44
/**
5-
* @typedef {import('./dir')} Dir
5+
* @typedef {import('./dir').default} Dir
66
* @typedef {import('./types').ImporterOptions} ImporterOptions
77
*/
88

@@ -17,26 +17,21 @@ async function flatToShard (child, dir, threshold, options) {
1717
let newDir = dir
1818

1919
if (dir instanceof DirFlat && dir.directChildrenCount() >= threshold) {
20-
// @ts-ignore Dir type conflict!?
2120
newDir = await convertToShard(dir, options)
2221
}
2322

24-
// @ts-ignore Dir type conflict!?
2523
const parent = newDir.parent
2624

2725
if (parent) {
2826
if (newDir !== dir) {
2927
if (child) {
30-
// @ts-ignore Dir type conflict!?
3128
child.parent = newDir
3229
}
3330

34-
// @ts-ignore Dir type conflict!?
3531
if (!newDir.parentKey) {
3632
throw new Error('No parent key found')
3733
}
3834

39-
// @ts-ignore Dir type conflict!?
4035
await parent.put(newDir.parentKey, newDir)
4136
}
4237

packages/ipfs-unixfs-importer/src/index.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import parallelBatch from 'it-parallel-batch'
22
import defaultOptions from './options.js'
33

4-
// TODO: Lazy Load
5-
import defaultDagBuilder from './dag-builder/index.js'
6-
import defaultTreeBuilder from './tree-builder.js'
7-
84
/**
95
* @typedef {import('interface-blockstore').Blockstore} Blockstore
106
* @typedef {import('./types').ImportCandidate} ImportCandidate
@@ -36,15 +32,15 @@ export async function * importer (source, blockstore, options = {}) {
3632
if (typeof options.dagBuilder === 'function') {
3733
dagBuilder = options.dagBuilder
3834
} else {
39-
dagBuilder = defaultDagBuilder
35+
dagBuilder = (await (import('./dag-builder/index.js'))).default
4036
}
4137

4238
let treeBuilder
4339

4440
if (typeof options.treeBuilder === 'function') {
4541
treeBuilder = options.treeBuilder
4642
} else {
47-
treeBuilder = defaultTreeBuilder
43+
treeBuilder = (await (import('./tree-builder.js'))).default
4844
}
4945

5046
/** @type {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate>} */

packages/ipfs-unixfs-importer/src/tree-builder.js

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ async function addToTree (elem, tree, options) {
3434

3535
if (last) {
3636
await parent.put(pathElem, elem)
37-
// @ts-ignore Dir type conflict!?
3837
tree = await flatToShard(null, parent, options.shardSplitThreshold, options)
3938
} else {
4039
let dir = await parent.get(pathElem)

packages/ipfs-unixfs-importer/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "aegir/src/config/tsconfig.aegir.json",
33
"compilerOptions": {
44
"outDir": "dist",
5+
"module": "es2020",
56
"importsNotUsedAsValues": "preserve"
67
},
78
"include": [

0 commit comments

Comments
 (0)