Skip to content

Commit

Permalink
chore: re-add dynamic imports and remove added ts ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 23, 2021
1 parent 733ccac commit 5616c33
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 78 deletions.
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"fs": false
},
"scripts": {
"prepare": "aegir build",
"prepare": "aegir build --no-bundle",
"test": "aegir test",
"build": "aegir build",
"clean": "rimraf ./dist",
Expand Down
30 changes: 16 additions & 14 deletions packages/ipfs-unixfs-exporter/src/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@ import * as dagCbor from '@ipld/dag-cbor'
import * as raw from 'multiformats/codecs/raw'
import { identity } from 'multiformats/hashes/identity'

// TODO: Lazy Load
import unixFs1Resolver from './unixfs-v1/index.js'
import rawResolver from './raw.js'
import dagCborResolver from './dag-cbor.js'
import identityResolver from './identity.js'

/**
* @typedef {import('../types').Resolver} Resolver
* @typedef {import('../types').Resolve} Resolve
*/

/**
* @type {{ [ key: string ]: Resolver }}
* @param {number} key
* @returns {Promise<Resolver|undefined>}
*/
const resolvers = {
[dagPb.code]: unixFs1Resolver,
[raw.code]: rawResolver,
[dagCbor.code]: dagCborResolver,
[identity.code]: identityResolver
const importResolver = async (key) => {
switch (key) {
case dagPb.code:
return (await (import('./unixfs-v1/index.js'))).default
case raw.code:
return (await (import('./raw.js'))).default
case dagCbor.code:
return (await (import('./dag-cbor.js'))).default
case identity.code:
return (await (import('./identity.js'))).default
default:
}
}

/**
* @type {Resolve}
*/
function resolve (cid, name, path, toResolve, depth, blockstore, options) {
const resolver = resolvers[cid.code]
async function resolve (cid, name, path, toResolve, depth, blockstore, options) {
const resolver = await importResolver(cid.code)

if (!resolver) {
throw errCode(new Error(`No resolver for code ${cid.code}`), 'ERR_NO_RESOLVER')
Expand Down
35 changes: 18 additions & 17 deletions packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { UnixFS } from 'ipfs-unixfs'
import findShardCid from '../../utils/find-cid-in-shard.js'
import { decode } from '@ipld/dag-pb'

// TODO Lazy load
import contentFile from './content/file.js'
import contentDirectory from './content/directory.js'
import contentHamtShardedDirectory from './content/hamt-sharded-directory.js'

/**
* @typedef {import('../../types').Resolve} Resolve
* @typedef {import('../../types').Resolver} Resolver
Expand All @@ -26,18 +21,22 @@ const findLinkCid = (node, name) => {
}

/**
* @type {{ [key: string]: UnixfsV1Resolver }}
* @param {string} key
* @returns {Promise<UnixfsV1Resolver|undefined>}
*/
const contentExporters = {
raw: contentFile,
file: contentFile,
directory: contentDirectory,
'hamt-sharded-directory': contentHamtShardedDirectory,
metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => {
return () => []
},
symlink: (cid, node, unixfs, path, resolve, depth, blockstore) => {
return () => []
const importContentExporters = async (key) => {
switch (key) {
case 'raw':
case 'file':
return (await (import('./content/file.js'))).default
case 'directory':
return (await (import('./content/directory.js'))).default
case 'hamt-sharded-directory':
return (await (import('./content/hamt-sharded-directory.js'))).default
case 'metadata':
case 'symlink':
return () => () => []
default:
}
}

Expand Down Expand Up @@ -95,14 +94,16 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks
}
}

const contentExporter = await importContentExporters(unixfs.type)

return {
entry: {
type: unixfs.isDirectory() ? 'directory' : 'file',
name,
path,
cid,
// @ts-ignore
content: contentExporters[unixfs.type](cid, node, unixfs, path, resolve, depth, blockstore),
content: contentExporter(cid, node, unixfs, path, resolve, depth, blockstore),
unixfs,
depth,
node,
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-unixfs-exporter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"module": "es2020",
"importsNotUsedAsValues": "preserve"
},
"include": [
Expand Down
22 changes: 12 additions & 10 deletions packages/ipfs-unixfs-importer/src/chunker/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import errCode from 'err-code'

// TODO: lazy load
import rabinChunker from './rabin.js'
import fixedSizeChunker from './fixed-size.js'

/**
* @typedef {import('../types').ImporterOptions} ImporterOptions
* @typedef {import('../types').Chunker} Chunker
*/

/**
* @type {{ [key: string]: Chunker }}
* @param {string} key
* @returns {Promise<Chunker|undefined>}
*/
const chunkers = {
fixed: fixedSizeChunker,
rabin: rabinChunker
const importChunkers = async (key) => {
switch (key) {
case 'fixed':
return (await (import('./fixed-size.js'))).default
case 'rabin':
return (await (import('./rabin.js'))).default
default:
}
}

/**
* @param {import('../types').ChunkerType} type
* @param {AsyncIterable<Uint8Array>} source
* @param {import('../types').ImporterOptions} options
*/
module.exports = (type, source, options) => {
const chunker = chunkers[type]
module.exports = async (type, source, options) => {
const chunker = await importChunkers(type)

if (!chunker) {
throw errCode(new Error(`Unknkown chunker named ${type}`), 'ERR_UNKNOWN_CHUNKER')
Expand Down
29 changes: 15 additions & 14 deletions packages/ipfs-unixfs-importer/src/dag-builder/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import parallelBatch from 'it-parallel-batch'
import * as rawCodec from 'multiformats/codecs/raw'
import * as dagPb from '@ipld/dag-pb'

// TODO: Lazy load
import flatBuilder from './flat.js'
import balancedBuilder from './balanced.js'
import trickleBuilder from './trickle.js'
import bufImporter from './buffer-importer.js'

/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
* @typedef {import('../../types').File} File
Expand All @@ -22,12 +16,19 @@ import bufImporter from './buffer-importer.js'
*/

/**
* @type {{ [key: string]: FileDAGBuilder}}
* @param {string} key
* @returns {Promise<FileDAGBuilder|undefined>}
*/
const dagBuilders = {
flat: flatBuilder,
balanced: balancedBuilder,
trickle: trickleBuilder
const importDagBuilder = async (key) => {
switch (key) {
case 'flat':
return (await (import('./flat.js'))).default
case 'balanced':
return (await (import('./balanced.js'))).default
case 'trickle':
return (await (import('./trickle.js'))).default
default:
}
}

/**
Expand All @@ -43,7 +44,7 @@ async function * buildFileBatch (file, blockstore, options) {
if (typeof options.bufferImporter === 'function') {
bufferImporter = options.bufferImporter
} else {
bufferImporter = bufImporter
bufferImporter = (await (import('./buffer-importer.js'))).default
}

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

if (!dagBuilder) {
throw errCode(new Error(`Unknown importer build strategy name: ${options.strategy}`), 'ERR_BAD_STRATEGY')
Expand Down
11 changes: 3 additions & 8 deletions packages/ipfs-unixfs-importer/src/dag-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import dirBuilder from './dir.js'
import fileBuilder from './file/index.js'
import errCode from 'err-code'

// TODO: dynamic imports
import validateChunks from './validate-chunks.js'
import rabinChunker from '../chunker/rabin.js'
import fixedSizeChunker from '../chunker/fixed-size.js'

/**
* @typedef {import('../types').File} File
* @typedef {import('../types').Directory} Directory
Expand Down Expand Up @@ -80,9 +75,9 @@ async function * dagBuilder (source, blockstore, options) {
if (typeof options.chunker === 'function') {
chunker = options.chunker
} else if (options.chunker === 'rabin') {
chunker = rabinChunker
chunker = (await (import('../chunker/rabin.js'))).default
} else {
chunker = fixedSizeChunker
chunker = (await (import('../chunker/fixed-size.js'))).default
}

/**
Expand All @@ -93,7 +88,7 @@ async function * dagBuilder (source, blockstore, options) {
if (typeof options.chunkValidator === 'function') {
chunkValidator = options.chunkValidator
} else {
chunkValidator = validateChunks
chunkValidator = (await (import('./validate-chunks.js'))).default
}

/** @type {File} */
Expand Down
1 change: 0 additions & 1 deletion packages/ipfs-unixfs-importer/src/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class Dir {
/**
*
* @param {DirProps} props
* @param {ImporterOptions} options
*/
Expand Down
7 changes: 1 addition & 6 deletions packages/ipfs-unixfs-importer/src/flat-to-shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DirSharded from './dir-sharded.js'
import DirFlat from './dir-flat.js'

/**
* @typedef {import('./dir')} Dir
* @typedef {import('./dir').default} Dir
* @typedef {import('./types').ImporterOptions} ImporterOptions
*/

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

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

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

if (parent) {
if (newDir !== dir) {
if (child) {
// @ts-ignore Dir type conflict!?
child.parent = newDir
}

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

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

Expand Down
8 changes: 2 additions & 6 deletions packages/ipfs-unixfs-importer/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import parallelBatch from 'it-parallel-batch'
import defaultOptions from './options.js'

// TODO: Lazy Load
import defaultDagBuilder from './dag-builder/index.js'
import defaultTreeBuilder from './tree-builder.js'

/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
* @typedef {import('./types').ImportCandidate} ImportCandidate
Expand Down Expand Up @@ -36,15 +32,15 @@ export async function * importer (source, blockstore, options = {}) {
if (typeof options.dagBuilder === 'function') {
dagBuilder = options.dagBuilder
} else {
dagBuilder = defaultDagBuilder
dagBuilder = (await (import('./dag-builder/index.js'))).default
}

let treeBuilder

if (typeof options.treeBuilder === 'function') {
treeBuilder = options.treeBuilder
} else {
treeBuilder = defaultTreeBuilder
treeBuilder = (await (import('./tree-builder.js'))).default
}

/** @type {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate>} */
Expand Down
1 change: 0 additions & 1 deletion packages/ipfs-unixfs-importer/src/tree-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async function addToTree (elem, tree, options) {

if (last) {
await parent.put(pathElem, elem)
// @ts-ignore Dir type conflict!?
tree = await flatToShard(null, parent, options.shardSplitThreshold, options)
} else {
let dir = await parent.get(pathElem)
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-unixfs-importer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"module": "es2020",
"importsNotUsedAsValues": "preserve"
},
"include": [
Expand Down

0 comments on commit 5616c33

Please sign in to comment.