Skip to content

Commit bd6a57a

Browse files
committed
chore: not dynamic imports flagged
1 parent 06956bd commit bd6a57a

File tree

3 files changed

+40
-48
lines changed

3 files changed

+40
-48
lines changed

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,31 @@ import * as dagCbor from '@ipld/dag-cbor'
55
import * as raw from 'multiformats/codecs/raw'
66
import { identity } from 'multiformats/hashes/identity'
77

8+
import dagPbResolver from './unixfs-v1/index.js'
9+
import rawResolver from './raw.js'
10+
import dagCborResolver from './dag-cbor.js'
11+
import identifyResolver from './identity.js'
12+
813
/**
914
* @typedef {import('../types').Resolver} Resolver
1015
* @typedef {import('../types').Resolve} Resolve
1116
*/
1217

1318
/**
14-
* @param {number} key
15-
* @returns {Promise<Resolver|undefined>}
19+
* @type {{ [ key: string ]: Resolver }}
1620
*/
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-
}
21+
const resolvers = {
22+
[dagPb.code]: dagPbResolver,
23+
[raw.code]: rawResolver,
24+
[dagCbor.code]: dagCborResolver,
25+
[identity.code]: identifyResolver
2926
}
3027

3128
/**
3229
* @type {Resolve}
3330
*/
34-
async function resolve (cid, name, path, toResolve, depth, blockstore, options) {
35-
const resolver = await importResolver(cid.code)
31+
function resolve (cid, name, path, toResolve, depth, blockstore, options) {
32+
const resolver = resolvers[cid.code]
3633

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

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { UnixFS } from 'ipfs-unixfs'
33
import findShardCid from '../../utils/find-cid-in-shard.js'
44
import { decode } from '@ipld/dag-pb'
55

6+
import contentFile from './content/file.js'
7+
import contentDirectory from './content/directory.js'
8+
import contentHamtShardedDirectory from './content/hamt-sharded-directory.js'
9+
610
/**
711
* @typedef {import('../../types').Resolve} Resolve
812
* @typedef {import('../../types').Resolver} Resolver
@@ -21,22 +25,18 @@ const findLinkCid = (node, name) => {
2125
}
2226

2327
/**
24-
* @param {string} key
25-
* @returns {Promise<UnixfsV1Resolver|undefined>}
28+
* @type {{ [key: string]: UnixfsV1Resolver }}
2629
*/
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:
30+
const contentExporters = {
31+
raw: contentFile,
32+
file: contentFile,
33+
directory: contentDirectory,
34+
'hamt-sharded-directory': contentHamtShardedDirectory,
35+
metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => {
36+
return () => []
37+
},
38+
symlink: (cid, node, unixfs, path, resolve, depth, blockstore) => {
39+
return () => []
4040
}
4141
}
4242

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

97-
const contentExporter = await importContentExporters(unixfs.type)
98-
9997
return {
10098
entry: {
10199
type: unixfs.isDirectory() ? 'directory' : 'file',
102100
name,
103101
path,
104102
cid,
105103
// @ts-ignore
106-
content: contentExporter(cid, node, unixfs, path, resolve, depth, blockstore),
104+
content: contentExporters[unixfs.type](cid, node, unixfs, path, resolve, depth, blockstore),
107105
unixfs,
108106
depth,
109107
node,

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import parallelBatch from 'it-parallel-batch'
66
import * as rawCodec from 'multiformats/codecs/raw'
77
import * as dagPb from '@ipld/dag-pb'
88

9+
import dagFlat from './flat.js'
10+
import dagBalanced from './balanced.js'
11+
import dagTrickle from './trickle.js'
12+
913
/**
1014
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1115
* @typedef {import('../../types').File} File
@@ -16,19 +20,12 @@ import * as dagPb from '@ipld/dag-pb'
1620
*/
1721

1822
/**
19-
* @param {string} key
20-
* @returns {Promise<FileDAGBuilder|undefined>}
23+
* @type {{ [key: string]: FileDAGBuilder}}
2124
*/
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-
}
25+
const dagBuilders = {
26+
flat: dagFlat,
27+
balanced: dagBalanced,
28+
trickle: dagTrickle
3229
}
3330

3431
/**
@@ -197,8 +194,8 @@ const reduce = (file, blockstore, options) => {
197194
/**
198195
* @type {import('../../types').UnixFSV1DagBuilder<File>}
199196
*/
200-
async function fileBuilder (file, block, options) {
201-
const dagBuilder = await importDagBuilder(options.strategy)
197+
function fileBuilder (file, block, options) {
198+
const dagBuilder = dagBuilders[options.strategy]
202199

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

0 commit comments

Comments
 (0)