From 4842293678bc66d4d97adf88da36435d70403088 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 27 Aug 2021 12:24:59 +0100 Subject: [PATCH] fix: remove dynamic imports (#169) Removes dynamic imports because they dont work well when imports cjs also removes unused file --- .../ipfs-unixfs-importer/src/chunker/index.js | 35 ------------------- .../src/dag-builder/file/index.js | 3 +- .../src/dag-builder/index.js | 9 +++-- packages/ipfs-unixfs-importer/src/index.js | 6 ++-- 4 files changed, 12 insertions(+), 41 deletions(-) delete mode 100644 packages/ipfs-unixfs-importer/src/chunker/index.js diff --git a/packages/ipfs-unixfs-importer/src/chunker/index.js b/packages/ipfs-unixfs-importer/src/chunker/index.js deleted file mode 100644 index 065644de..00000000 --- a/packages/ipfs-unixfs-importer/src/chunker/index.js +++ /dev/null @@ -1,35 +0,0 @@ -import errCode from 'err-code' - -/** - * @typedef {import('../types').ImporterOptions} ImporterOptions - * @typedef {import('../types').Chunker} Chunker - */ - -/** - * @param {string} key - * @returns {Promise} - */ -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} source - * @param {import('../types').ImporterOptions} options - */ -module.exports = async (type, source, options) => { - const chunker = await importChunkers(type) - - if (!chunker) { - throw errCode(new Error(`Unknkown chunker named ${type}`), 'ERR_UNKNOWN_CHUNKER') - } - - return chunker(source, options) -} diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js index 10c3b26e..f8ef36cb 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js @@ -9,6 +9,7 @@ import * as dagPb from '@ipld/dag-pb' import dagFlat from './flat.js' import dagBalanced from './balanced.js' import dagTrickle from './trickle.js' +import bufferImporterFn from './buffer-importer.js' /** * @typedef {import('interface-blockstore').Blockstore} Blockstore @@ -41,7 +42,7 @@ async function * buildFileBatch (file, blockstore, options) { if (typeof options.bufferImporter === 'function') { bufferImporter = options.bufferImporter } else { - bufferImporter = (await (import('./buffer-importer.js'))).default + bufferImporter = bufferImporterFn } for await (const entry of parallelBatch(bufferImporter(file, blockstore, options), options.blockWriteConcurrency)) { diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/index.js b/packages/ipfs-unixfs-importer/src/dag-builder/index.js index f0ad2cb5..145c35d5 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/index.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/index.js @@ -1,6 +1,9 @@ import dirBuilder from './dir.js' import fileBuilder from './file/index.js' import errCode from 'err-code' +import rabin from '../chunker/rabin.js' +import fixedSize from '../chunker/fixed-size.js' +import validateChunks from './validate-chunks.js' /** * @typedef {import('../types').File} File @@ -75,9 +78,9 @@ async function * dagBuilder (source, blockstore, options) { if (typeof options.chunker === 'function') { chunker = options.chunker } else if (options.chunker === 'rabin') { - chunker = (await (import('../chunker/rabin.js'))).default + chunker = rabin } else { - chunker = (await (import('../chunker/fixed-size.js'))).default + chunker = fixedSize } /** @@ -88,7 +91,7 @@ async function * dagBuilder (source, blockstore, options) { if (typeof options.chunkValidator === 'function') { chunkValidator = options.chunkValidator } else { - chunkValidator = (await (import('./validate-chunks.js'))).default + chunkValidator = validateChunks } /** @type {File} */ diff --git a/packages/ipfs-unixfs-importer/src/index.js b/packages/ipfs-unixfs-importer/src/index.js index cc4d379d..522aefac 100644 --- a/packages/ipfs-unixfs-importer/src/index.js +++ b/packages/ipfs-unixfs-importer/src/index.js @@ -1,5 +1,7 @@ import parallelBatch from 'it-parallel-batch' import defaultOptions from './options.js' +import dagBuilderFn from './dag-builder/index.js' +import treeBuilderFn from './tree-builder.js' /** * @typedef {import('interface-blockstore').Blockstore} Blockstore @@ -32,7 +34,7 @@ export async function * importer (source, blockstore, options = {}) { if (typeof options.dagBuilder === 'function') { dagBuilder = options.dagBuilder } else { - dagBuilder = (await (import('./dag-builder/index.js'))).default + dagBuilder = dagBuilderFn } let treeBuilder @@ -40,7 +42,7 @@ export async function * importer (source, blockstore, options = {}) { if (typeof options.treeBuilder === 'function') { treeBuilder = options.treeBuilder } else { - treeBuilder = (await (import('./tree-builder.js'))).default + treeBuilder = treeBuilderFn } /** @type {AsyncIterable | Iterable} */