From 819267f64fe9d4afc89ef729d54d4d15c2e11820 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 19 Aug 2021 11:19:23 +0200 Subject: [PATCH] chore: switch to ESM (#161) Switches the codebase to ESM and exports ESM and CJS code. The main goal is to enable ipfs-unixfs to be built on next generation bundlers like skypack: codepen.io/vascosantos/pen/NWjaYqP?editors=0011 This is a problem that has not been fixed in skypack yet. More details: skypackjs/skypack-cdn#171 Given we will need to export ESM and we will, let's get this to circumvent the issue. We also need to manually change the generated protobuf code due to https://github.com/protobufjs/protobuf.js/issues/1230 BREAKING CHANGE: ./src/dir-sharded is not in the exports map so cannot be imported Co-authored-by: Alex Potsides --- .travis.yml | 5 +- .../{.aegir.js => .aegir.cjs} | 0 packages/ipfs-unixfs-exporter/package.json | 21 ++++---- packages/ipfs-unixfs-exporter/src/index.js | 22 +++----- .../src/resolvers/dag-cbor.js | 10 ++-- .../src/resolvers/identity.js | 12 ++--- .../src/resolvers/index.js | 25 +++++---- .../ipfs-unixfs-exporter/src/resolvers/raw.js | 10 ++-- .../resolvers/unixfs-v1/content/directory.js | 4 +- .../src/resolvers/unixfs-v1/content/file.js | 18 +++---- .../content/hamt-sharded-directory.js | 6 +-- .../src/resolvers/unixfs-v1/content/raw.js | 8 ++- .../src/resolvers/unixfs-v1/index.js | 22 ++++---- .../src/utils/extract-data-from-block.js | 6 +-- .../src/utils/find-cid-in-shard.js | 11 ++-- .../src/utils/validate-offset-and-length.js | 6 +-- .../test/exporter-sharded.spec.js | 28 +++++----- .../test/exporter-subtree.spec.js | 20 ++++---- .../test/exporter.spec.js | 43 ++++++++-------- .../test/helpers/as-async-iterable.js | 4 +- .../test/helpers/block.js | 51 +++++++++++++++++-- .../test/helpers/collect-leaf-cids.js | 6 +-- .../test/import-export-dir-sharding.spec.js | 24 ++++----- .../test/import-export-nested-dir.spec.js | 20 ++++---- .../test/import-export.spec.js | 23 ++++----- .../test/importer.spec.js | 48 ++++++++--------- packages/ipfs-unixfs-exporter/tsconfig.json | 1 + .../{.aegir.js => .aegir.cjs} | 0 packages/ipfs-unixfs-importer/package.json | 19 ++++--- .../src/chunker/fixed-size.js | 8 +-- .../ipfs-unixfs-importer/src/chunker/index.js | 22 ++++---- .../ipfs-unixfs-importer/src/chunker/rabin.js | 12 ++--- .../src/dag-builder/dir.js | 10 ++-- .../src/dag-builder/file/balanced.js | 6 +-- .../src/dag-builder/file/buffer-importer.js | 12 ++--- .../src/dag-builder/file/flat.js | 8 +-- .../src/dag-builder/file/index.js | 30 ++++++----- .../src/dag-builder/file/trickle.js | 8 +-- .../src/dag-builder/index.js | 16 +++--- .../src/dag-builder/validate-chunks.js | 8 ++- packages/ipfs-unixfs-importer/src/dir-flat.js | 12 ++--- .../ipfs-unixfs-importer/src/dir-sharded.js | 14 +++-- packages/ipfs-unixfs-importer/src/dir.js | 6 +-- .../ipfs-unixfs-importer/src/flat-to-shard.js | 12 ++--- packages/ipfs-unixfs-importer/src/index.js | 16 ++---- packages/ipfs-unixfs-importer/src/options.js | 14 ++--- .../ipfs-unixfs-importer/src/tree-builder.js | 12 ++--- .../ipfs-unixfs-importer/src/utils/persist.js | 10 ++-- .../src/utils/to-path-components.js | 4 +- .../test/benchmark.spec.js | 8 ++- .../test/builder-balanced.spec.js | 10 ++-- .../test/builder-flat.spec.js | 5 +- .../test/builder-only-hash.spec.js | 13 +++-- .../test/builder-trickle-dag.spec.js | 7 ++- .../ipfs-unixfs-importer/test/builder.spec.js | 24 ++++----- .../test/chunker-custom.spec.js | 17 +++---- .../test/chunker-fixed-size.spec.js | 16 +++--- .../test/chunker-rabin.spec.js | 17 +++---- .../test/hash-parity-with-go-ipfs.spec.js | 14 +++-- .../test/helpers/as-async-iterable.js | 4 +- .../test/helpers/block.js | 10 ++-- .../finite-pseudorandom-byte-stream.js | 6 +-- .../test/helpers/random-byte-stream.js | 4 +- packages/ipfs-unixfs-importer/tsconfig.json | 1 + .../ipfs-unixfs/{.aegir.js => .aegir.cjs} | 0 packages/ipfs-unixfs/package.json | 21 ++++---- packages/ipfs-unixfs/src/index.js | 23 +++------ packages/ipfs-unixfs/src/unixfs.js | 18 +++---- .../ipfs-unixfs/test/unixfs-format.spec.js | 15 +++--- 69 files changed, 452 insertions(+), 494 deletions(-) rename packages/ipfs-unixfs-exporter/{.aegir.js => .aegir.cjs} (100%) rename packages/ipfs-unixfs-importer/{.aegir.js => .aegir.cjs} (100%) rename packages/ipfs-unixfs/{.aegir.js => .aegir.cjs} (100%) diff --git a/.travis.yml b/.travis.yml index ab015ea4..43654a99 100644 --- a/.travis.yml +++ b/.travis.yml @@ -87,7 +87,10 @@ jobs: name: electron main addons: firefox: latest - script: npm run test -- -- -- -t electron-main + script: + - npm run build + - npx lerna link # use publishConfig.directory + - npm run test -- -- -- -t electron-main -f dist/cjs/node-test/*js - stage: release # only run on changes to master diff --git a/packages/ipfs-unixfs-exporter/.aegir.js b/packages/ipfs-unixfs-exporter/.aegir.cjs similarity index 100% rename from packages/ipfs-unixfs-exporter/.aegir.js rename to packages/ipfs-unixfs-exporter/.aegir.cjs diff --git a/packages/ipfs-unixfs-exporter/package.json b/packages/ipfs-unixfs-exporter/package.json index 511938f3..50976cbc 100644 --- a/packages/ipfs-unixfs-exporter/package.json +++ b/packages/ipfs-unixfs-exporter/package.json @@ -4,13 +4,14 @@ "description": "JavaScript implementation of the UnixFs exporter used by IPFS", "leadMaintainer": "Alex Potsides ", "main": "src/index.js", + "type": "module", "browser": { "fs": false }, "scripts": { "prepare": "aegir build --no-bundle", "test": "aegir test", - "build": "aegir build", + "build": "aegir build --esm-tests", "clean": "rimraf ./dist", "lint": "aegir ts -p check && aegir lint", "coverage": "nyc -s npm run test -t node && nyc report --reporter=html", @@ -32,14 +33,16 @@ "npm": ">=7.0.0" }, "homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme", + "publishConfig": { + "directory": "dist" + }, "devDependencies": { "@types/mocha": "^8.2.1", "@types/sinon": "^10.0.0", "abort-controller": "^3.0.0", - "aegir": "^34.0.0", + "aegir": "^35.0.1", "copy": "^0.3.2", "crypto-browserify": "^3.12.0", - "detect-node": "^2.0.4", "events": "^3.3.0", "ipfs-unixfs-importer": "^8.0.2", "it-all": "^1.0.5", @@ -52,7 +55,6 @@ "readable-stream": "^3.6.0", "rimraf": "^3.0.2", "sinon": "^11.1.1", - "uint8arrays": "^2.1.2", "util": "^0.12.3" }, "dependencies": { @@ -65,14 +67,13 @@ "it-last": "^1.0.5", "multiformats": "^9.4.2", "murmurhash3js-revisited": "^3.0.0", - "uint8arrays": "^2.1.7" + "uint8arrays": "^3.0.0" }, "types": "dist/src/index.d.ts", - "files": [ - "src", - "dist" - ], "eslintConfig": { - "extends": "ipfs" + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } } } diff --git a/packages/ipfs-unixfs-exporter/src/index.js b/packages/ipfs-unixfs-exporter/src/index.js index bc3bfa91..8cdaf2c8 100644 --- a/packages/ipfs-unixfs-exporter/src/index.js +++ b/packages/ipfs-unixfs-exporter/src/index.js @@ -1,9 +1,7 @@ -'use strict' - -const errCode = require('err-code') -const { CID } = require('multiformats/cid') -const resolve = require('./resolvers') -const last = require('it-last') +import errCode from 'err-code' +import { CID } from 'multiformats/cid' +import resolve from './resolvers/index.js' +import last from 'it-last' /** * @typedef {import('ipfs-unixfs').UnixFS} UnixFS @@ -65,7 +63,7 @@ const cidAndRest = (path) => { * @param {Blockstore} blockstore * @param {ExporterOptions} [options] */ -async function * walkPath (path, blockstore, options = {}) { +export async function * walkPath (path, blockstore, options = {}) { let { cid, toResolve @@ -102,7 +100,7 @@ async function * walkPath (path, blockstore, options = {}) { * @param {Blockstore} blockstore * @param {ExporterOptions} [options] */ -async function exporter (path, blockstore, options = {}) { +export async function exporter (path, blockstore, options = {}) { const result = await last(walkPath(path, blockstore, options)) if (!result) { @@ -117,7 +115,7 @@ async function exporter (path, blockstore, options = {}) { * @param {Blockstore} blockstore * @param {ExporterOptions} [options] */ -async function * recursive (path, blockstore, options = {}) { +export async function * recursive (path, blockstore, options = {}) { const node = await exporter(path, blockstore, options) if (!node) { @@ -151,9 +149,3 @@ async function * recursive (path, blockstore, options = {}) { } } } - -module.exports = { - exporter, - walkPath, - recursive -} diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js b/packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js index 6a077d3d..2188f058 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js @@ -1,8 +1,6 @@ -'use strict' - -const { CID } = require('multiformats/cid') -const errCode = require('err-code') -const dagCbor = require('@ipld/dag-cbor') +import { CID } from 'multiformats/cid' +import errCode from 'err-code' +import * as dagCbor from '@ipld/dag-cbor' /** * @typedef {import('../types').Resolver} Resolver @@ -72,4 +70,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o } } -module.exports = resolve +export default resolve diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/identity.js b/packages/ipfs-unixfs-exporter/src/resolvers/identity.js index 2c2d015a..ac1c11b5 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/identity.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/identity.js @@ -1,9 +1,7 @@ -'use strict' - -const errCode = require('err-code') -const extractDataFromBlock = require('../utils/extract-data-from-block') -const validateOffsetAndLength = require('../utils/validate-offset-and-length') -const mh = require('multiformats/hashes/digest') +import errCode from 'err-code' +import extractDataFromBlock from '../utils/extract-data-from-block.js' +import validateOffsetAndLength from '../utils/validate-offset-and-length.js' +import * as mh from 'multiformats/hashes/digest' /** * @typedef {import('../types').ExporterOptions} ExporterOptions @@ -52,4 +50,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o } } -module.exports = resolve +export default resolve diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/index.js b/packages/ipfs-unixfs-exporter/src/resolvers/index.js index 1ec5a034..73a0b895 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/index.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/index.js @@ -1,11 +1,14 @@ -'use strict' +import errCode from 'err-code' -const errCode = require('err-code') +import * as dagPb from '@ipld/dag-pb' +import * as dagCbor from '@ipld/dag-cbor' +import * as raw from 'multiformats/codecs/raw' +import { identity } from 'multiformats/hashes/identity' -const dagPb = require('@ipld/dag-pb') -const dagCbor = require('@ipld/dag-cbor') -const raw = require('multiformats/codecs/raw') -const { identity } = require('multiformats/hashes/identity') +import dagPbResolver from './unixfs-v1/index.js' +import rawResolver from './raw.js' +import dagCborResolver from './dag-cbor.js' +import identifyResolver from './identity.js' /** * @typedef {import('../types').Resolver} Resolver @@ -16,10 +19,10 @@ const { identity } = require('multiformats/hashes/identity') * @type {{ [ key: string ]: Resolver }} */ const resolvers = { - [dagPb.code]: require('./unixfs-v1'), - [raw.code]: require('./raw'), - [dagCbor.code]: require('./dag-cbor'), - [identity.code]: require('./identity') + [dagPb.code]: dagPbResolver, + [raw.code]: rawResolver, + [dagCbor.code]: dagCborResolver, + [identity.code]: identifyResolver } /** @@ -35,4 +38,4 @@ function resolve (cid, name, path, toResolve, depth, blockstore, options) { return resolver(cid, name, path, toResolve, resolve, depth, blockstore, options) } -module.exports = resolve +export default resolve diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/raw.js b/packages/ipfs-unixfs-exporter/src/resolvers/raw.js index a82e511b..5807d32c 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/raw.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/raw.js @@ -1,8 +1,6 @@ -'use strict' - -const errCode = require('err-code') -const extractDataFromBlock = require('../utils/extract-data-from-block') -const validateOffsetAndLength = require('../utils/validate-offset-and-length') +import errCode from 'err-code' +import extractDataFromBlock from '../utils/extract-data-from-block.js' +import validateOffsetAndLength from '../utils/validate-offset-and-length.js' /** * @typedef {import('../types').ExporterOptions} ExporterOptions @@ -51,4 +49,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o } } -module.exports = resolve +export default resolve diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.js b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.js index 9433f0b4..677d39a6 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.js @@ -1,5 +1,3 @@ -'use strict' - /** * @typedef {import('../../../types').ExporterOptions} ExporterOptions * @typedef {import('../../../types').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent @@ -31,4 +29,4 @@ const directoryContent = (cid, node, unixfs, path, resolve, depth, blockstore) = return yieldDirectoryContent } -module.exports = directoryContent +export default directoryContent diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.js b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.js index 2ba6a5ca..6c95e5fd 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.js @@ -1,12 +1,10 @@ -'use strict' - -const extractDataFromBlock = require('../../../utils/extract-data-from-block') -const validateOffsetAndLength = require('../../../utils/validate-offset-and-length') -const { UnixFS } = require('ipfs-unixfs') -const errCode = require('err-code') -const dagPb = require('@ipld/dag-pb') -const dagCbor = require('@ipld/dag-cbor') -const raw = require('multiformats/codecs/raw') +import extractDataFromBlock from '../../../utils/extract-data-from-block.js' +import validateOffsetAndLength from '../../../utils/validate-offset-and-length.js' +import { UnixFS } from 'ipfs-unixfs' +import errCode from 'err-code' +import * as dagPb from '@ipld/dag-pb' +import * as dagCbor from '@ipld/dag-cbor' +import * as raw from 'multiformats/codecs/raw' /** * @typedef {import('../../../types').ExporterOptions} ExporterOptions @@ -126,4 +124,4 @@ const fileContent = (cid, node, unixfs, path, resolve, depth, blockstore) => { return yieldFileContent } -module.exports = fileContent +export default fileContent diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js index 93bf11dd..8e3e8664 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js @@ -1,6 +1,4 @@ -'use strict' - -const { decode } = require('@ipld/dag-pb') +import { decode } from '@ipld/dag-pb' /** * @typedef {import('interface-blockstore').Blockstore} Blockstore @@ -58,4 +56,4 @@ async function * listDirectory (node, path, resolve, depth, blockstore, options) } } -module.exports = hamtShardedDirectoryContent +export default hamtShardedDirectoryContent diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.js b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.js index 95a38870..5c622248 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.js @@ -1,7 +1,5 @@ -'use strict' - -const extractDataFromBlock = require('../../../utils/extract-data-from-block') -const validateOffsetAndLength = require('../../../utils/validate-offset-and-length') +import extractDataFromBlock from '../../../utils/extract-data-from-block.js' +import validateOffsetAndLength from '../../../utils/validate-offset-and-length.js' /** * @typedef {import('../../../types').ExporterOptions} ExporterOptions @@ -33,4 +31,4 @@ const rawContent = (cid, node, unixfs, path, resolve, depth, blockstore) => { return yieldRawContent } -module.exports = rawContent +export default rawContent diff --git a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js index 86b70e91..cdb79de3 100644 --- a/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js +++ b/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js @@ -1,9 +1,11 @@ -'use strict' +import errCode from 'err-code' +import { UnixFS } from 'ipfs-unixfs' +import findShardCid from '../../utils/find-cid-in-shard.js' +import { decode } from '@ipld/dag-pb' -const errCode = require('err-code') -const { UnixFS } = require('ipfs-unixfs') -const findShardCid = require('../../utils/find-cid-in-shard') -const { decode } = require('@ipld/dag-pb') +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 @@ -26,10 +28,10 @@ const findLinkCid = (node, name) => { * @type {{ [key: string]: UnixfsV1Resolver }} */ const contentExporters = { - raw: require('./content/file'), - file: require('./content/file'), - directory: require('./content/directory'), - 'hamt-sharded-directory': require('./content/hamt-sharded-directory'), + raw: contentFile, + file: contentFile, + directory: contentDirectory, + 'hamt-sharded-directory': contentHamtShardedDirectory, metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => { return () => [] }, @@ -109,4 +111,4 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks } } -module.exports = unixFsResolver +export default unixFsResolver diff --git a/packages/ipfs-unixfs-exporter/src/utils/extract-data-from-block.js b/packages/ipfs-unixfs-exporter/src/utils/extract-data-from-block.js index 4de42822..c5d715be 100644 --- a/packages/ipfs-unixfs-exporter/src/utils/extract-data-from-block.js +++ b/packages/ipfs-unixfs-exporter/src/utils/extract-data-from-block.js @@ -1,12 +1,10 @@ -'use strict' - /** * @param {Uint8Array} block * @param {number} blockStart * @param {number} requestedStart * @param {number} requestedEnd */ -module.exports = function extractDataFromBlock (block, blockStart, requestedStart, requestedEnd) { +function extractDataFromBlock (block, blockStart, requestedStart, requestedEnd) { const blockLength = block.length const blockEnd = blockStart + blockLength @@ -28,3 +26,5 @@ module.exports = function extractDataFromBlock (block, blockStart, requestedStar return block } + +export default extractDataFromBlock diff --git a/packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js b/packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js index 5c92bdcd..285a0116 100644 --- a/packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js +++ b/packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js @@ -1,10 +1,9 @@ -'use strict' -const { Bucket, createHAMT } = require('hamt-sharding') -const { decode } = require('@ipld/dag-pb') +import { Bucket, createHAMT } from 'hamt-sharding' +import { decode } from '@ipld/dag-pb' // @ts-ignore - no types available -const mur = require('murmurhash3js-revisited') -const uint8ArrayFromString = require('uint8arrays/from-string') +import mur from 'murmurhash3js-revisited' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' /** * @typedef {import('interface-blockstore').Blockstore} Blockstore @@ -152,4 +151,4 @@ const findShardCid = async (node, name, blockstore, context, options) => { return findShardCid(node, name, blockstore, context, options) } -module.exports = findShardCid +export default findShardCid diff --git a/packages/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.js b/packages/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.js index 1786302f..80e0ffb8 100644 --- a/packages/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.js +++ b/packages/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.js @@ -1,6 +1,4 @@ -'use strict' - -const errCode = require('err-code') +import errCode from 'err-code' /** * @param {number} size @@ -38,4 +36,4 @@ const validateOffsetAndLength = (size, offset, length) => { } } -module.exports = validateOffsetAndLength +export default validateOffsetAndLength diff --git a/packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js b/packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js index 91a43275..b4e6bfd9 100644 --- a/packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js +++ b/packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js @@ -1,19 +1,17 @@ /* eslint-env mocha */ -'use strict' - -const { expect } = require('aegir/utils/chai') -const { UnixFS } = require('ipfs-unixfs') -const all = require('it-all') -const last = require('it-last') -const randomBytes = require('it-buffer-stream') -const { exporter, walkPath } = require('../src') -const { importer } = require('ipfs-unixfs-importer') -const dagPb = require('@ipld/dag-pb') -const blockApi = require('./helpers/block') -const uint8ArrayConcat = require('uint8arrays/concat') -const asAsyncIterable = require('./helpers/as-async-iterable') -const { CID } = require('multiformats/cid') -const { sha256 } = require('multiformats/hashes/sha2') +import { expect } from 'aegir/utils/chai.js' +import { UnixFS } from 'ipfs-unixfs' +import all from 'it-all' +import last from 'it-last' +import randomBytes from 'it-buffer-stream' +import { exporter, walkPath } from '../src/index.js' +import { importer } from 'ipfs-unixfs-importer' +import * as dagPb from '@ipld/dag-pb' +import blockApi from './helpers/block.js' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import asAsyncIterable from './helpers/as-async-iterable.js' +import { CID } from 'multiformats/cid' +import { sha256 } from 'multiformats/hashes/sha2' const SHARD_SPLIT_THRESHOLD = 10 diff --git a/packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js b/packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js index 6e1c6e00..eb0b99d9 100644 --- a/packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js +++ b/packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js @@ -1,19 +1,17 @@ /* eslint-env mocha */ -'use strict' +import { expect } from 'aegir/utils/chai.js' +import { importer } from 'ipfs-unixfs-importer' +import all from 'it-all' +import last from 'it-last' +import blockApi from './helpers/block.js' +import randomBytes from 'it-buffer-stream' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import asAsyncIterable from './helpers/as-async-iterable.js' -const { expect } = require('aegir/utils/chai') -const { importer } = require('ipfs-unixfs-importer') -const all = require('it-all') -const last = require('it-last') -const blockApi = require('./helpers/block') -const randomBytes = require('it-buffer-stream') -const uint8ArrayConcat = require('uint8arrays/concat') -const asAsyncIterable = require('./helpers/as-async-iterable') +import { exporter, walkPath } from './../src/index.js' const ONE_MEG = Math.pow(1024, 2) -const { exporter, walkPath } = require('./../src') - describe('exporter subtree', () => { const block = blockApi() diff --git a/packages/ipfs-unixfs-exporter/test/exporter.spec.js b/packages/ipfs-unixfs-exporter/test/exporter.spec.js index 0b8bbcc1..b9d45a4a 100644 --- a/packages/ipfs-unixfs-exporter/test/exporter.spec.js +++ b/packages/ipfs-unixfs-exporter/test/exporter.spec.js @@ -1,26 +1,25 @@ /* eslint-env mocha */ -'use strict' - -const { expect } = require('aegir/utils/chai') -const { UnixFS } = require('ipfs-unixfs') -const { CID } = require('multiformats/cid') -const dagPb = require('@ipld/dag-pb') -const dagCbor = require('@ipld/dag-cbor') -const { sha256 } = require('multiformats/hashes/sha2') -const { identity } = require('multiformats/hashes/identity') -const raw = require('multiformats/codecs/raw') -const { exporter, recursive } = require('../src') -const { importer } = require('ipfs-unixfs-importer') -const all = require('it-all') -const last = require('it-last') -const first = require('it-first') -const randomBytes = require('it-buffer-stream') -const { AbortController } = require('native-abort-controller') -const blockApi = require('./helpers/block') -const uint8ArrayFromString = require('uint8arrays/from-string') -const uint8ArrayToString = require('uint8arrays/to-string') -const uint8ArrayConcat = require('uint8arrays/concat') -const asAsyncIterable = require('./helpers/as-async-iterable') + +import { expect } from 'aegir/utils/chai.js' +import { UnixFS } from 'ipfs-unixfs' +import { CID } from 'multiformats/cid' +import * as dagPb from '@ipld/dag-pb' +import * as dagCbor from '@ipld/dag-cbor' +import { sha256 } from 'multiformats/hashes/sha2' +import { identity } from 'multiformats/hashes/identity' +import * as raw from 'multiformats/codecs/raw' +import { exporter, recursive } from '../src/index.js' +import { importer } from 'ipfs-unixfs-importer' +import all from 'it-all' +import last from 'it-last' +import first from 'it-first' +import randomBytes from 'it-buffer-stream' +import { AbortController } from 'native-abort-controller' +import blockApi from './helpers/block.js' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import asAsyncIterable from './helpers/as-async-iterable.js' const ONE_MEG = Math.pow(1024, 2) diff --git a/packages/ipfs-unixfs-exporter/test/helpers/as-async-iterable.js b/packages/ipfs-unixfs-exporter/test/helpers/as-async-iterable.js index 7aba5df2..23e1cdea 100644 --- a/packages/ipfs-unixfs-exporter/test/helpers/as-async-iterable.js +++ b/packages/ipfs-unixfs-exporter/test/helpers/as-async-iterable.js @@ -1,5 +1,3 @@ -'use strict' - /** * @param {Uint8Array | Uint8Array[]} arr */ @@ -11,4 +9,4 @@ async function * asAsyncIterable (arr) { yield * arr } -module.exports = asAsyncIterable +export default asAsyncIterable diff --git a/packages/ipfs-unixfs-exporter/test/helpers/block.js b/packages/ipfs-unixfs-exporter/test/helpers/block.js index d6aa505c..a415ed5c 100644 --- a/packages/ipfs-unixfs-exporter/test/helpers/block.js +++ b/packages/ipfs-unixfs-exporter/test/helpers/block.js @@ -1,7 +1,48 @@ -'use strict' +import errCode from 'err-code' +import { BlockstoreAdapter } from 'interface-blockstore' +import { base58btc } from 'multiformats/bases/base58' -/** @type {() => import('interface-blockstore').Blockstore} */ -// @ts-expect-error no types for this deep import -const block = require('ipfs-unixfs-importer/test/helpers/block') +/** + * @typedef {import('multiformats/cid').CID} CID + */ -module.exports = block +function createBlockApi () { + class MockBlockstore extends BlockstoreAdapter { + constructor () { + super() + + /** @type {{[key: string]: Uint8Array}} */ + this._blocks = {} + } + + /** + * @param {CID} cid + * @param {Uint8Array} block + * @param {any} [options] + */ + async put (cid, block, options = {}) { + this._blocks[base58btc.encode(cid.multihash.bytes)] = block + } + + /** + * @param {CID} cid + * @param {any} [options] + */ + async get (cid, options = {}) { + const bytes = this._blocks[base58btc.encode(cid.multihash.bytes)] + + if (bytes === undefined) { + throw errCode(new Error(`Could not find data for CID '${cid}'`), 'ERR_NOT_FOUND') + } + + return bytes + } + } + + /** @type {import('interface-blockstore').Blockstore} */ + const bs = new MockBlockstore() + + return bs +} + +export default createBlockApi diff --git a/packages/ipfs-unixfs-exporter/test/helpers/collect-leaf-cids.js b/packages/ipfs-unixfs-exporter/test/helpers/collect-leaf-cids.js index 259b3133..bc7fe5c7 100644 --- a/packages/ipfs-unixfs-exporter/test/helpers/collect-leaf-cids.js +++ b/packages/ipfs-unixfs-exporter/test/helpers/collect-leaf-cids.js @@ -1,6 +1,4 @@ -'use strict' - -const dagPb = require('@ipld/dag-pb') +import * as dagPb from '@ipld/dag-pb' /** * @typedef {import('@ipld/dag-pb').PBLink} PBLink @@ -10,7 +8,7 @@ const dagPb = require('@ipld/dag-pb') * @param {import('multiformats/cid').CID} cid * @param {import('interface-blockstore').Blockstore} blockstore */ -module.exports = function (cid, blockstore) { +export default function (cid, blockstore) { /** * @param {import('multiformats/cid').CID} cid */ diff --git a/packages/ipfs-unixfs-exporter/test/import-export-dir-sharding.spec.js b/packages/ipfs-unixfs-exporter/test/import-export-dir-sharding.spec.js index b2772159..cb7c309b 100644 --- a/packages/ipfs-unixfs-exporter/test/import-export-dir-sharding.spec.js +++ b/packages/ipfs-unixfs-exporter/test/import-export-dir-sharding.spec.js @@ -1,17 +1,15 @@ /* eslint-env mocha */ -'use strict' - -const { importer } = require('ipfs-unixfs-importer') -const { exporter } = require('../src') - -const { expect } = require('aegir/utils/chai') -const all = require('it-all') -const last = require('it-last') -const blockApi = require('./helpers/block') -const uint8ArrayFromString = require('uint8arrays/from-string') -const uint8ArrayToString = require('uint8arrays/to-string') -const uint8ArrayConcat = require('uint8arrays/concat') -const asAsyncIterable = require('./helpers/as-async-iterable') +import { importer } from 'ipfs-unixfs-importer' +import { exporter } from '../src/index.js' + +import { expect } from 'aegir/utils/chai.js' +import all from 'it-all' +import last from 'it-last' +import blockApi from './helpers/block.js' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import asAsyncIterable from './helpers/as-async-iterable.js' /** * @typedef {import('../src').UnixFSEntry} UnixFSEntry diff --git a/packages/ipfs-unixfs-exporter/test/import-export-nested-dir.spec.js b/packages/ipfs-unixfs-exporter/test/import-export-nested-dir.spec.js index c6018e3f..746e4971 100644 --- a/packages/ipfs-unixfs-exporter/test/import-export-nested-dir.spec.js +++ b/packages/ipfs-unixfs-exporter/test/import-export-nested-dir.spec.js @@ -1,15 +1,13 @@ /* eslint-env mocha */ -'use strict' - -const { expect } = require('aegir/utils/chai') -const all = require('it-all') -const { importer } = require('ipfs-unixfs-importer') -const { exporter } = require('../src') -const blockApi = require('./helpers/block') -const uint8ArrayFromString = require('uint8arrays/from-string') -const uint8ArrayToString = require('uint8arrays/to-string') -const uint8ArrayConcat = require('uint8arrays/concat') -const asAsyncIterable = require('./helpers/as-async-iterable') +import { expect } from 'aegir/utils/chai.js' +import all from 'it-all' +import { importer } from 'ipfs-unixfs-importer' +import { exporter } from '../src/index.js' +import blockApi from './helpers/block.js' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import asAsyncIterable from './helpers/as-async-iterable.js' describe('import and export: directory', () => { const rootHash = 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK' diff --git a/packages/ipfs-unixfs-exporter/test/import-export.spec.js b/packages/ipfs-unixfs-exporter/test/import-export.spec.js index d8571ff0..12644ef8 100644 --- a/packages/ipfs-unixfs-exporter/test/import-export.spec.js +++ b/packages/ipfs-unixfs-exporter/test/import-export.spec.js @@ -1,18 +1,15 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 5] */ -'use strict' - -const { expect } = require('aegir/utils/chai') -// @ts-ignore -const loadFixture = require('aegir/utils/fixtures') -// @ts-ignore -const isNode = require('detect-node') -const bigFile = loadFixture((isNode ? __dirname : 'test') + '/fixtures/1.2MiB.txt') -const blockApi = require('./helpers/block') -const asAsyncIterable = require('./helpers/as-async-iterable') - -const { importer } = require('ipfs-unixfs-importer') -const { exporter } = require('../src') +import { expect } from 'aegir/utils/chai.js' +import loadFixture from 'aegir/utils/fixtures.js' +import blockApi from './helpers/block.js' +import asAsyncIterable from './helpers/as-async-iterable.js' + +import { importer } from 'ipfs-unixfs-importer' +import { exporter } from '../src/index.js' + +/** @type {Uint8Array} */ +const bigFile = loadFixture(('test') + '/fixtures/1.2MiB.txt') const strategies = [ 'flat', diff --git a/packages/ipfs-unixfs-exporter/test/importer.spec.js b/packages/ipfs-unixfs-exporter/test/importer.spec.js index 882a3f85..74efa7da 100644 --- a/packages/ipfs-unixfs-exporter/test/importer.spec.js +++ b/packages/ipfs-unixfs-exporter/test/importer.spec.js @@ -1,32 +1,28 @@ /* eslint-env mocha */ -'use strict' - -const { importer } = require('ipfs-unixfs-importer') -const { exporter, recursive } = require('../src') -const extend = require('merge-options') -const { expect } = require('aegir/utils/chai') -const sinon = require('sinon') -const { UnixFS } = require('ipfs-unixfs') -const collectLeafCids = require('./helpers/collect-leaf-cids') -// @ts-ignore -const loadFixture = require('aegir/utils/fixtures') -// @ts-ignore -const isNode = require('detect-node') + +import { importer } from 'ipfs-unixfs-importer' +import { exporter, recursive } from '../src/index.js' +import extend from 'merge-options' +import { expect } from 'aegir/utils/chai.js' +import sinon from 'sinon' +import { UnixFS, parseMtime } from 'ipfs-unixfs' +import collectLeafCids from './helpers/collect-leaf-cids.js' +import loadFixture from 'aegir/utils/fixtures.js' +import all from 'it-all' +import first from 'it-first' +import blockApi from './helpers/block.js' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import asAsyncIterable from './helpers/as-async-iterable.js' +import last from 'it-last' +import { CID } from 'multiformats/cid' +import { base58btc } from 'multiformats/bases/base58' +import { decode } from '@ipld/dag-pb' + /** @type {Uint8Array} */ -const bigFile = loadFixture((isNode ? __dirname : 'test') + '/fixtures/1.2MiB.txt') +const bigFile = loadFixture('test/fixtures/1.2MiB.txt') /** @type {Uint8Array} */ -const smallFile = loadFixture((isNode ? __dirname : 'test') + '/fixtures/200Bytes.txt') -const all = require('it-all') -const first = require('it-first') -const blockApi = require('./helpers/block') -const uint8ArrayConcat = require('uint8arrays/concat') -const uint8ArrayFromString = require('uint8arrays/from-string') -const asAsyncIterable = require('./helpers/as-async-iterable') -const last = require('it-last') -const { CID } = require('multiformats/cid') -const { base58btc } = require('multiformats/bases/base58') -const { decode } = require('@ipld/dag-pb') -const { parseMtime } = require('ipfs-unixfs') +const smallFile = loadFixture('test/fixtures/200Bytes.txt') /** * @typedef {import('interface-blockstore').Blockstore} Blockstore diff --git a/packages/ipfs-unixfs-exporter/tsconfig.json b/packages/ipfs-unixfs-exporter/tsconfig.json index c6eb8f25..446917f2 100644 --- a/packages/ipfs-unixfs-exporter/tsconfig.json +++ b/packages/ipfs-unixfs-exporter/tsconfig.json @@ -2,6 +2,7 @@ "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { "outDir": "dist", + "module": "es2020", "importsNotUsedAsValues": "preserve" }, "include": [ diff --git a/packages/ipfs-unixfs-importer/.aegir.js b/packages/ipfs-unixfs-importer/.aegir.cjs similarity index 100% rename from packages/ipfs-unixfs-importer/.aegir.js rename to packages/ipfs-unixfs-importer/.aegir.cjs diff --git a/packages/ipfs-unixfs-importer/package.json b/packages/ipfs-unixfs-importer/package.json index 2273f474..e24e3662 100644 --- a/packages/ipfs-unixfs-importer/package.json +++ b/packages/ipfs-unixfs-importer/package.json @@ -4,13 +4,14 @@ "description": "JavaScript implementation of the UnixFs importer used by IPFS", "leadMaintainer": "Alex Potsides ", "main": "src/index.js", + "type": "module", "browser": { "fs": false }, "scripts": { "prepare": "aegir build --no-bundle", "test": "aegir test", - "build": "aegir build", + "build": "aegir build --esm-tests", "clean": "rimraf ./dist", "lint": "aegir ts -p check && aegir lint", "coverage": "nyc -s npm run test -t node && nyc report --reporter=html", @@ -32,9 +33,12 @@ "npm": ">=7.0.0" }, "homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme", + "publishConfig": { + "directory": "dist" + }, "devDependencies": { "@types/mocha": "^8.2.1", - "aegir": "^34.0.0", + "aegir": "^35.0.1", "assert": "^2.0.0", "copy": "^0.3.2", "crypto-browserify": "^3.12.0", @@ -60,14 +64,13 @@ "multiformats": "^9.4.2", "murmurhash3js-revisited": "^3.0.0", "rabin-wasm": "^0.1.4", - "uint8arrays": "^2.1.7" + "uint8arrays": "^3.0.0" }, "types": "dist/src/index.d.ts", - "files": [ - "src", - "dist" - ], "eslintConfig": { - "extends": "ipfs" + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } } } diff --git a/packages/ipfs-unixfs-importer/src/chunker/fixed-size.js b/packages/ipfs-unixfs-importer/src/chunker/fixed-size.js index 8ef07361..fe537399 100644 --- a/packages/ipfs-unixfs-importer/src/chunker/fixed-size.js +++ b/packages/ipfs-unixfs-importer/src/chunker/fixed-size.js @@ -1,12 +1,10 @@ -'use strict' - // @ts-ignore -const BufferList = require('bl/BufferList') +import BufferList from 'bl/BufferList.js' /** * @type {import('../types').Chunker} */ -module.exports = async function * fixedSizeChunker (source, options) { +async function * fixedSizeChunker (source, options) { let bl = new BufferList() let currentLength = 0 let emitted = false @@ -41,3 +39,5 @@ module.exports = async function * fixedSizeChunker (source, options) { yield bl.slice(0, currentLength) } } + +export default fixedSizeChunker diff --git a/packages/ipfs-unixfs-importer/src/chunker/index.js b/packages/ipfs-unixfs-importer/src/chunker/index.js index b7df2f09..065644de 100644 --- a/packages/ipfs-unixfs-importer/src/chunker/index.js +++ b/packages/ipfs-unixfs-importer/src/chunker/index.js @@ -1,6 +1,4 @@ -'use strict' - -const errCode = require('err-code') +import errCode from 'err-code' /** * @typedef {import('../types').ImporterOptions} ImporterOptions @@ -8,11 +6,17 @@ const errCode = require('err-code') */ /** - * @type {{ [key: string]: Chunker }} + * @param {string} key + * @returns {Promise} */ -const chunkers = { - fixed: require('../chunker/fixed-size'), - rabin: require('../chunker/rabin') +const importChunkers = async (key) => { + switch (key) { + case 'fixed': + return (await (import('./fixed-size.js'))).default + case 'rabin': + return (await (import('./rabin.js'))).default + default: + } } /** @@ -20,8 +24,8 @@ const chunkers = { * @param {AsyncIterable} 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') diff --git a/packages/ipfs-unixfs-importer/src/chunker/rabin.js b/packages/ipfs-unixfs-importer/src/chunker/rabin.js index 221659a1..539b878d 100644 --- a/packages/ipfs-unixfs-importer/src/chunker/rabin.js +++ b/packages/ipfs-unixfs-importer/src/chunker/rabin.js @@ -1,10 +1,8 @@ -'use strict' - // @ts-ignore -const BufferList = require('bl/BufferList') +import BufferList from 'bl/BufferList.js' // @ts-ignore -const { create } = require('rabin-wasm') -const errcode = require('err-code') +import { create } from 'rabin-wasm' +import errcode from 'err-code' /** * @typedef {object} RabinOptions @@ -18,7 +16,7 @@ const errcode = require('err-code') /** * @type {import('../types').Chunker} */ -module.exports = async function * rabinChunker (source, options) { +async function * rabinChunker (source, options) { let min, max, avg if (options.minChunkSize && options.maxChunkSize && options.avgChunkSize) { @@ -59,6 +57,8 @@ module.exports = async function * rabinChunker (source, options) { } } +export default rabinChunker + /** * @param {AsyncIterable} source * @param {RabinOptions} options diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/dir.js b/packages/ipfs-unixfs-importer/src/dag-builder/dir.js index ac59de32..6ca92097 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/dir.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/dir.js @@ -1,8 +1,6 @@ -'use strict' - -const { UnixFS } = require('ipfs-unixfs') -const persist = require('../utils/persist') -const { encode, prepare } = require('@ipld/dag-pb') +import { UnixFS } from 'ipfs-unixfs' +import persist from '../utils/persist.js' +import { encode, prepare } from '@ipld/dag-pb' /** * @typedef {import('../types').Directory} Directory @@ -30,4 +28,4 @@ const dirBuilder = async (item, blockstore, options) => { } } -module.exports = dirBuilder +export default dirBuilder diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/balanced.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/balanced.js index cb0ace2d..5f646715 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/balanced.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/balanced.js @@ -1,6 +1,4 @@ -'use strict' - -const batch = require('it-batch') +import batch from 'it-batch' /** * @typedef {import('../../types').FileDAGBuilder} FileDAGBuilder @@ -30,4 +28,4 @@ async function reduceToParents (source, reduce, options) { return roots[0] } -module.exports = balanced +export default balanced diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js index 3b12a5ad..abfcf676 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js @@ -1,9 +1,7 @@ -'use strict' - -const { UnixFS } = require('ipfs-unixfs') -const persist = require('../../utils/persist') -const dagPb = require('@ipld/dag-pb') -const raw = require('multiformats/codecs/raw') +import { UnixFS } from 'ipfs-unixfs' +import persist from '../../utils/persist.js' +import * as dagPb from '@ipld/dag-pb' +import * as raw from 'multiformats/codecs/raw' /** * @typedef {import('../../types').BufferImporter} BufferImporter @@ -52,4 +50,4 @@ async function * bufferImporter (file, block, options) { } } -module.exports = bufferImporter +export default bufferImporter diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/flat.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/flat.js index 549bab0e..b7072218 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/flat.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/flat.js @@ -1,10 +1,10 @@ -'use strict' - -const all = require('it-all') +import all from 'it-all' /** * @type {import('../../types').FileDAGBuilder} */ -module.exports = async function (source, reduce) { +async function flat (source, reduce) { return reduce(await all(source)) } + +export default flat 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 302ef2cd..10c3b26e 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js @@ -1,12 +1,14 @@ -'use strict' - -const errCode = require('err-code') -const { UnixFS } = require('ipfs-unixfs') -const persist = require('../../utils/persist') -const { encode, prepare } = require('@ipld/dag-pb') -const parallelBatch = require('it-parallel-batch') -const rawCodec = require('multiformats/codecs/raw') -const dagPb = require('@ipld/dag-pb') +import errCode from 'err-code' +import { UnixFS } from 'ipfs-unixfs' +import persist from '../../utils/persist.js' +import { encode, prepare } from '@ipld/dag-pb' +import parallelBatch from 'it-parallel-batch' +import * as rawCodec from 'multiformats/codecs/raw' +import * as dagPb from '@ipld/dag-pb' + +import dagFlat from './flat.js' +import dagBalanced from './balanced.js' +import dagTrickle from './trickle.js' /** * @typedef {import('interface-blockstore').Blockstore} Blockstore @@ -21,9 +23,9 @@ const dagPb = require('@ipld/dag-pb') * @type {{ [key: string]: FileDAGBuilder}} */ const dagBuilders = { - flat: require('./flat'), - balanced: require('./balanced'), - trickle: require('./trickle') + flat: dagFlat, + balanced: dagBalanced, + trickle: dagTrickle } /** @@ -39,7 +41,7 @@ async function * buildFileBatch (file, blockstore, options) { if (typeof options.bufferImporter === 'function') { bufferImporter = options.bufferImporter } else { - bufferImporter = require('./buffer-importer') + bufferImporter = (await (import('./buffer-importer.js'))).default } for await (const entry of parallelBatch(bufferImporter(file, blockstore, options), options.blockWriteConcurrency)) { @@ -202,4 +204,4 @@ function fileBuilder (file, block, options) { return dagBuilder(buildFileBatch(file, block, options), reduce(file, block, options), options) } -module.exports = fileBuilder +export default fileBuilder diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/trickle.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/trickle.js index 1de9c98c..677cd6d1 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/trickle.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/trickle.js @@ -1,6 +1,4 @@ -'use strict' - -const batch = require('it-batch') +import batch from 'it-batch' /** * @typedef {import('ipfs-unixfs').UnixFS} UnixFS @@ -14,7 +12,7 @@ const batch = require('it-batch') /** * @type {FileDAGBuilder} */ -module.exports = async function trickleStream (source, reduce, options) { +async function trickleStream (source, reduce, options) { const root = new Root(options.layerRepeat) let iteration = 0 let maxDepth = 1 @@ -47,6 +45,8 @@ module.exports = async function trickleStream (source, reduce, options) { return root.reduce(reduce) } +export default trickleStream + class SubTree { /** * @param {number} maxDepth diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/index.js b/packages/ipfs-unixfs-importer/src/dag-builder/index.js index d52a4939..f0ad2cb5 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/index.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/index.js @@ -1,8 +1,6 @@ -'use strict' - -const dirBuilder = require('./dir') -const fileBuilder = require('./file') -const errCode = require('err-code') +import dirBuilder from './dir.js' +import fileBuilder from './file/index.js' +import errCode from 'err-code' /** * @typedef {import('../types').File} File @@ -77,9 +75,9 @@ async function * dagBuilder (source, blockstore, options) { if (typeof options.chunker === 'function') { chunker = options.chunker } else if (options.chunker === 'rabin') { - chunker = require('../chunker/rabin') + chunker = (await (import('../chunker/rabin.js'))).default } else { - chunker = require('../chunker/fixed-size') + chunker = (await (import('../chunker/fixed-size.js'))).default } /** @@ -90,7 +88,7 @@ async function * dagBuilder (source, blockstore, options) { if (typeof options.chunkValidator === 'function') { chunkValidator = options.chunkValidator } else { - chunkValidator = require('./validate-chunks') + chunkValidator = (await (import('./validate-chunks.js'))).default } /** @type {File} */ @@ -117,4 +115,4 @@ async function * dagBuilder (source, blockstore, options) { } } -module.exports = dagBuilder +export default dagBuilder diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/validate-chunks.js b/packages/ipfs-unixfs-importer/src/dag-builder/validate-chunks.js index 88689a86..ec5a6f13 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/validate-chunks.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/validate-chunks.js @@ -1,7 +1,5 @@ -'use strict' - -const errCode = require('err-code') -const uint8ArrayFromString = require('uint8arrays/from-string') +import errCode from 'err-code' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' /** * @typedef {import('../types').ChunkValidator} ChunkValidator @@ -28,4 +26,4 @@ async function * validateChunks (source) { } } -module.exports = validateChunks +export default validateChunks diff --git a/packages/ipfs-unixfs-importer/src/dir-flat.js b/packages/ipfs-unixfs-importer/src/dir-flat.js index 501478ac..ad1d12c0 100644 --- a/packages/ipfs-unixfs-importer/src/dir-flat.js +++ b/packages/ipfs-unixfs-importer/src/dir-flat.js @@ -1,9 +1,7 @@ -'use strict' - -const { encode, prepare } = require('@ipld/dag-pb') -const { UnixFS } = require('ipfs-unixfs') -const Dir = require('./dir') -const persist = require('./utils/persist') +import { encode, prepare } from '@ipld/dag-pb' +import { UnixFS } from 'ipfs-unixfs' +import Dir from './dir.js' +import persist from './utils/persist.js' /** * @typedef {import('./types').ImporterOptions} ImporterOptions @@ -128,4 +126,4 @@ class DirFlat extends Dir { } } -module.exports = DirFlat +export default DirFlat diff --git a/packages/ipfs-unixfs-importer/src/dir-sharded.js b/packages/ipfs-unixfs-importer/src/dir-sharded.js index 71bc49c7..b3bc8671 100644 --- a/packages/ipfs-unixfs-importer/src/dir-sharded.js +++ b/packages/ipfs-unixfs-importer/src/dir-sharded.js @@ -1,10 +1,8 @@ -'use strict' - -const { encode, prepare } = require('@ipld/dag-pb') -const { UnixFS } = require('ipfs-unixfs') -const Dir = require('./dir') -const persist = require('./utils/persist') -const { createHAMT, Bucket } = require('hamt-sharding') +import { encode, prepare } from '@ipld/dag-pb' +import { UnixFS } from 'ipfs-unixfs' +import Dir from './dir.js' +import persist from './utils/persist.js' +import { createHAMT, Bucket } from 'hamt-sharding' /** * @typedef {import('./types').ImporterOptions} ImporterOptions @@ -82,7 +80,7 @@ class DirSharded extends Dir { } } -module.exports = DirSharded +export default DirSharded /** * @param {Bucket} bucket diff --git a/packages/ipfs-unixfs-importer/src/dir.js b/packages/ipfs-unixfs-importer/src/dir.js index fd8571b5..19fdb281 100644 --- a/packages/ipfs-unixfs-importer/src/dir.js +++ b/packages/ipfs-unixfs-importer/src/dir.js @@ -1,11 +1,10 @@ -'use strict' - /** * @typedef {import('./types').ImporterOptions} ImporterOptions * @typedef {import('./types').ImportResult} ImportResult * @typedef {import('./types').InProgressImportResult} InProgressImportResult * @typedef {import('interface-blockstore').Blockstore} Blockstore * @typedef {import('multiformats/cid').CID} CID + * * @typedef {object} DirProps * @property {boolean} root * @property {boolean} dir @@ -20,7 +19,6 @@ */ class Dir { /** - * * @param {DirProps} props * @param {ImporterOptions} options */ @@ -70,4 +68,4 @@ class Dir { async * flush (blockstore) { } } -module.exports = Dir +export default Dir diff --git a/packages/ipfs-unixfs-importer/src/flat-to-shard.js b/packages/ipfs-unixfs-importer/src/flat-to-shard.js index 590cd62c..c46f3d3a 100644 --- a/packages/ipfs-unixfs-importer/src/flat-to-shard.js +++ b/packages/ipfs-unixfs-importer/src/flat-to-shard.js @@ -1,10 +1,8 @@ -'use strict' - -const DirSharded = require('./dir-sharded') -const DirFlat = require('./dir-flat') +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 */ @@ -15,7 +13,7 @@ const DirFlat = require('./dir-flat') * @param {ImporterOptions} options * @returns {Promise} */ -module.exports = async function flatToShard (child, dir, threshold, options) { +async function flatToShard (child, dir, threshold, options) { let newDir = dir if (dir instanceof DirFlat && dir.directChildrenCount() >= threshold) { @@ -67,3 +65,5 @@ async function convertToShard (oldDir, options) { return newDir } + +export default flatToShard diff --git a/packages/ipfs-unixfs-importer/src/index.js b/packages/ipfs-unixfs-importer/src/index.js index 8d42d754..cc4d379d 100644 --- a/packages/ipfs-unixfs-importer/src/index.js +++ b/packages/ipfs-unixfs-importer/src/index.js @@ -1,7 +1,5 @@ -'use strict' - -const parallelBatch = require('it-parallel-batch') -const defaultOptions = require('./options') +import parallelBatch from 'it-parallel-batch' +import defaultOptions from './options.js' /** * @typedef {import('interface-blockstore').Blockstore} Blockstore @@ -26,7 +24,7 @@ const defaultOptions = require('./options') * @param {Blockstore} blockstore * @param {UserImporterOptions} options */ -async function * importer (source, blockstore, options = {}) { +export async function * importer (source, blockstore, options = {}) { const opts = defaultOptions(options) let dagBuilder @@ -34,7 +32,7 @@ async function * importer (source, blockstore, options = {}) { if (typeof options.dagBuilder === 'function') { dagBuilder = options.dagBuilder } else { - dagBuilder = require('./dag-builder') + dagBuilder = (await (import('./dag-builder/index.js'))).default } let treeBuilder @@ -42,7 +40,7 @@ async function * importer (source, blockstore, options = {}) { if (typeof options.treeBuilder === 'function') { treeBuilder = options.treeBuilder } else { - treeBuilder = require('./tree-builder') + treeBuilder = (await (import('./tree-builder.js'))).default } /** @type {AsyncIterable | Iterable} */ @@ -65,7 +63,3 @@ async function * importer (source, blockstore, options = {}) { } } } - -module.exports = { - importer -} diff --git a/packages/ipfs-unixfs-importer/src/options.js b/packages/ipfs-unixfs-importer/src/options.js index 43bdfb27..de0b0acc 100644 --- a/packages/ipfs-unixfs-importer/src/options.js +++ b/packages/ipfs-unixfs-importer/src/options.js @@ -1,10 +1,10 @@ -'use strict' - -const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) -const { sha256 } = require('multiformats/hashes/sha2') +import mergeOptions from 'merge-options' +import { sha256 } from 'multiformats/hashes/sha2' // @ts-ignore - no types available -const mur = require('murmurhash3js-revisited') -const uint8ArrayFromString = require('uint8arrays/from-string') +import mur from 'murmurhash3js-revisited' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' + +mergeOptions.bind({ ignoreUndefined: true }) /** * @param {Uint8Array} buf @@ -62,6 +62,6 @@ const defaultOptions = { * @param {UserImporterOptions} options * @returns {ImporterOptions} */ -module.exports = function (options = {}) { +export default (options = {}) => { return mergeOptions(defaultOptions, options) } diff --git a/packages/ipfs-unixfs-importer/src/tree-builder.js b/packages/ipfs-unixfs-importer/src/tree-builder.js index b9321bb7..301ecad0 100644 --- a/packages/ipfs-unixfs-importer/src/tree-builder.js +++ b/packages/ipfs-unixfs-importer/src/tree-builder.js @@ -1,9 +1,7 @@ -'use strict' - -const DirFlat = require('./dir-flat') -const flatToShard = require('./flat-to-shard') -const Dir = require('./dir') -const toPathComponents = require('./utils/to-path-components') +import DirFlat from './dir-flat.js' +import flatToShard from './flat-to-shard.js' +import Dir from './dir.js' +import toPathComponents from './utils/to-path-components.js' /** * @typedef {import('./types').ImportResult} ImportResult @@ -117,4 +115,4 @@ async function * treeBuilder (source, block, options) { } } -module.exports = treeBuilder +export default treeBuilder diff --git a/packages/ipfs-unixfs-importer/src/utils/persist.js b/packages/ipfs-unixfs-importer/src/utils/persist.js index 7e1f4300..fd57316c 100644 --- a/packages/ipfs-unixfs-importer/src/utils/persist.js +++ b/packages/ipfs-unixfs-importer/src/utils/persist.js @@ -1,8 +1,6 @@ -'use strict' - -const { CID } = require('multiformats/cid') -const dagPb = require('@ipld/dag-pb') -const { sha256 } = require('multiformats/hashes/sha2') +import { CID } from 'multiformats/cid' +import * as dagPb from '@ipld/dag-pb' +import { sha256 } from 'multiformats/hashes/sha2' /** * @param {Uint8Array} buffer @@ -38,4 +36,4 @@ const persist = async (buffer, blockstore, options) => { return cid } -module.exports = persist +export default persist diff --git a/packages/ipfs-unixfs-importer/src/utils/to-path-components.js b/packages/ipfs-unixfs-importer/src/utils/to-path-components.js index 5e826272..085a53b6 100644 --- a/packages/ipfs-unixfs-importer/src/utils/to-path-components.js +++ b/packages/ipfs-unixfs-importer/src/utils/to-path-components.js @@ -1,5 +1,3 @@ -'use strict' - const toPathComponents = (path = '') => { // split on / unless escaped with \ return (path @@ -8,4 +6,4 @@ const toPathComponents = (path = '') => { .filter(Boolean) } -module.exports = toPathComponents +export default toPathComponents diff --git a/packages/ipfs-unixfs-importer/test/benchmark.spec.js b/packages/ipfs-unixfs-importer/test/benchmark.spec.js index b787798d..5591d1ab 100644 --- a/packages/ipfs-unixfs-importer/test/benchmark.spec.js +++ b/packages/ipfs-unixfs-importer/test/benchmark.spec.js @@ -1,10 +1,8 @@ /* eslint-env mocha */ -'use strict' +import { importer } from '../src/index.js' -const { importer } = require('../src') - -const bufferStream = require('it-buffer-stream') -const blockApi = require('./helpers/block') +import bufferStream from 'it-buffer-stream' +import blockApi from './helpers/block.js' const REPEATS = 10 const FILE_SIZE = Math.pow(2, 20) * 500 // 500MB diff --git a/packages/ipfs-unixfs-importer/test/builder-balanced.spec.js b/packages/ipfs-unixfs-importer/test/builder-balanced.spec.js index 84c072cf..8403ea30 100644 --- a/packages/ipfs-unixfs-importer/test/builder-balanced.spec.js +++ b/packages/ipfs-unixfs-importer/test/builder-balanced.spec.js @@ -1,10 +1,8 @@ /* eslint-env mocha */ -'use strict' - -const { expect } = require('aegir/utils/chai') -const builder = require('../src/dag-builder/file/balanced') -const { CID } = require('multiformats/cid') -const defaultOptions = require('../src/options') +import { expect } from 'aegir/utils/chai.js' +import builder from '../src/dag-builder/file/balanced.js' +import { CID } from 'multiformats/cid' +import defaultOptions from '../src/options.js' /** * @typedef {import('../src/types').InProgressImportResult} InProgressImportResult diff --git a/packages/ipfs-unixfs-importer/test/builder-flat.spec.js b/packages/ipfs-unixfs-importer/test/builder-flat.spec.js index cf0d73dc..032ada55 100644 --- a/packages/ipfs-unixfs-importer/test/builder-flat.spec.js +++ b/packages/ipfs-unixfs-importer/test/builder-flat.spec.js @@ -1,8 +1,7 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') -const builder = require('../src/dag-builder/file/flat') +import { expect } from 'aegir/utils/chai.js' +import builder from '../src/dag-builder/file/flat.js' /** * @param {*} leaves diff --git a/packages/ipfs-unixfs-importer/test/builder-only-hash.spec.js b/packages/ipfs-unixfs-importer/test/builder-only-hash.spec.js index fff31ffc..6891d1be 100644 --- a/packages/ipfs-unixfs-importer/test/builder-only-hash.spec.js +++ b/packages/ipfs-unixfs-importer/test/builder-only-hash.spec.js @@ -1,12 +1,11 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') -const builder = require('../src/dag-builder') -const all = require('it-all') -const blockApi = require('./helpers/block') -const defaultOptions = require('../src/options') -const asAsyncIterable = require('./helpers/as-async-iterable') +import { expect } from 'aegir/utils/chai.js' +import builder from '../src/dag-builder/index.js' +import all from 'it-all' +import blockApi from './helpers/block.js' +import defaultOptions from '../src/options.js' +import asAsyncIterable from './helpers/as-async-iterable.js' describe('builder: onlyHash', () => { const block = blockApi() diff --git a/packages/ipfs-unixfs-importer/test/builder-trickle-dag.spec.js b/packages/ipfs-unixfs-importer/test/builder-trickle-dag.spec.js index 64c27d50..f0adf6d4 100644 --- a/packages/ipfs-unixfs-importer/test/builder-trickle-dag.spec.js +++ b/packages/ipfs-unixfs-importer/test/builder-trickle-dag.spec.js @@ -1,9 +1,8 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') -const builder = require('../src/dag-builder/file/trickle') -const asAsyncIterable = require('./helpers/as-async-iterable') +import { expect } from 'aegir/utils/chai.js' +import builder from '../src/dag-builder/file/trickle.js' +import asAsyncIterable from './helpers/as-async-iterable.js' /** * @param {number} max diff --git a/packages/ipfs-unixfs-importer/test/builder.spec.js b/packages/ipfs-unixfs-importer/test/builder.spec.js index a77e742a..f2ea3370 100644 --- a/packages/ipfs-unixfs-importer/test/builder.spec.js +++ b/packages/ipfs-unixfs-importer/test/builder.spec.js @@ -1,17 +1,15 @@ /* eslint-env mocha */ -'use strict' - -const { expect } = require('aegir/utils/chai') -const mh = require('multiformats/hashes/digest') -const { sha256, sha512 } = require('multiformats/hashes/sha2') -const { decode } = require('@ipld/dag-pb') -const { UnixFS } = require('ipfs-unixfs') -const builder = require('../src/dag-builder') -const first = require('it-first') -const blockApi = require('./helpers/block') -const uint8ArrayFromString = require('uint8arrays/from-string') -const defaultOptions = require('../src/options') -const asAsyncIterable = require('./helpers/as-async-iterable') +import { expect } from 'aegir/utils/chai.js' +import * as mh from 'multiformats/hashes/digest' +import { sha256, sha512 } from 'multiformats/hashes/sha2' +import { decode } from '@ipld/dag-pb' +import { UnixFS } from 'ipfs-unixfs' +import builder from '../src/dag-builder/index.js' +import first from 'it-first' +import blockApi from './helpers/block.js' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import defaultOptions from '../src/options.js' +import asAsyncIterable from './helpers/as-async-iterable.js' describe('builder', () => { const block = blockApi() diff --git a/packages/ipfs-unixfs-importer/test/chunker-custom.spec.js b/packages/ipfs-unixfs-importer/test/chunker-custom.spec.js index 7844a1d1..55d6d833 100644 --- a/packages/ipfs-unixfs-importer/test/chunker-custom.spec.js +++ b/packages/ipfs-unixfs-importer/test/chunker-custom.spec.js @@ -1,14 +1,13 @@ /* eslint-env mocha */ -'use strict' +import { importer } from '../src/index.js' +import { expect } from 'aegir/utils/chai.js' -const { importer } = require('../src') -const { expect } = require('aegir/utils/chai') -const rawCodec = require('multiformats/codecs/raw') -const { sha256 } = require('multiformats/hashes/sha2') -const Block = require('multiformats/block') -const blockApi = require('./helpers/block') -const uint8ArrayFromString = require('uint8arrays/from-string') -const { UnixFS } = require('ipfs-unixfs') +import * as rawCodec from 'multiformats/codecs/raw' +import { sha256 } from 'multiformats/hashes/sha2' +import * as Block from 'multiformats/block' +import blockApi from './helpers/block.js' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { UnixFS } from 'ipfs-unixfs' const iter = async function * () { yield uint8ArrayFromString('one') diff --git a/packages/ipfs-unixfs-importer/test/chunker-fixed-size.spec.js b/packages/ipfs-unixfs-importer/test/chunker-fixed-size.spec.js index 3845fd8d..ab446a3e 100644 --- a/packages/ipfs-unixfs-importer/test/chunker-fixed-size.spec.js +++ b/packages/ipfs-unixfs-importer/test/chunker-fixed-size.spec.js @@ -1,14 +1,12 @@ /* eslint-env mocha */ -'use strict' - -const chunker = require('../src/chunker/fixed-size') -const { expect } = require('aegir/utils/chai') -const all = require('it-all') +import chunker from '../src/chunker/fixed-size.js' +import { expect } from 'aegir/utils/chai.js' +import all from 'it-all' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import defaultOptions from '../src/options.js' +import asAsyncIterable from './helpers/as-async-iterable.js' const rawFile = new Uint8Array(Math.pow(2, 20)) -const uint8ArrayFromString = require('uint8arrays/from-string') -const uint8ArrayConcat = require('uint8arrays/concat') -const defaultOptions = require('../src/options') -const asAsyncIterable = require('./helpers/as-async-iterable') describe('chunker: fixed size', function () { this.timeout(30000) diff --git a/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.js b/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.js index 3418e2b5..9b7f09f9 100644 --- a/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.js +++ b/packages/ipfs-unixfs-importer/test/chunker-rabin.spec.js @@ -1,13 +1,12 @@ /* eslint-env mocha */ -'use strict' - -const chunker = require('../src/chunker/rabin') -const { expect } = require('aegir/utils/chai') -const all = require('it-all') -const uint8ArrayFromString = require('uint8arrays/from-string') -const uint8ArrayConcat = require('uint8arrays/concat') -const asAsyncIterable = require('./helpers/as-async-iterable') -const defaultOptions = require('../src/options') + +import chunker from '../src/chunker/rabin.js' +import { expect } from 'aegir/utils/chai.js' +import all from 'it-all' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' +import asAsyncIterable from './helpers/as-async-iterable.js' +import defaultOptions from '../src/options.js' const rawFile = new Uint8Array(Math.pow(2, 20)).fill(1) diff --git a/packages/ipfs-unixfs-importer/test/hash-parity-with-go-ipfs.spec.js b/packages/ipfs-unixfs-importer/test/hash-parity-with-go-ipfs.spec.js index 1271ed8f..3cc2c5f7 100644 --- a/packages/ipfs-unixfs-importer/test/hash-parity-with-go-ipfs.spec.js +++ b/packages/ipfs-unixfs-importer/test/hash-parity-with-go-ipfs.spec.js @@ -1,13 +1,11 @@ /* eslint-env mocha */ -'use strict' +import { importer } from '../src/index.js' -const { importer } = require('../src') - -const { expect } = require('aegir/utils/chai') -const randomByteStream = require('./helpers/finite-pseudorandom-byte-stream') -const first = require('it-first') -const blockApi = require('./helpers/block') -const defaultOptions = require('../src/options') +import { expect } from 'aegir/utils/chai.js' +import randomByteStream from './helpers/finite-pseudorandom-byte-stream.js' +import first from 'it-first' +import blockApi from './helpers/block.js' +import defaultOptions from '../src/options.js' /** @type {('flat' | 'trickle' | 'balanced')[]} */ const strategies = [ diff --git a/packages/ipfs-unixfs-importer/test/helpers/as-async-iterable.js b/packages/ipfs-unixfs-importer/test/helpers/as-async-iterable.js index 7aba5df2..23e1cdea 100644 --- a/packages/ipfs-unixfs-importer/test/helpers/as-async-iterable.js +++ b/packages/ipfs-unixfs-importer/test/helpers/as-async-iterable.js @@ -1,5 +1,3 @@ -'use strict' - /** * @param {Uint8Array | Uint8Array[]} arr */ @@ -11,4 +9,4 @@ async function * asAsyncIterable (arr) { yield * arr } -module.exports = asAsyncIterable +export default asAsyncIterable diff --git a/packages/ipfs-unixfs-importer/test/helpers/block.js b/packages/ipfs-unixfs-importer/test/helpers/block.js index 0703d2c3..a415ed5c 100644 --- a/packages/ipfs-unixfs-importer/test/helpers/block.js +++ b/packages/ipfs-unixfs-importer/test/helpers/block.js @@ -1,8 +1,6 @@ -'use strict' - -const errCode = require('err-code') -const { BlockstoreAdapter } = require('interface-blockstore') -const { base58btc } = require('multiformats/bases/base58') +import errCode from 'err-code' +import { BlockstoreAdapter } from 'interface-blockstore' +import { base58btc } from 'multiformats/bases/base58' /** * @typedef {import('multiformats/cid').CID} CID @@ -47,4 +45,4 @@ function createBlockApi () { return bs } -module.exports = createBlockApi +export default createBlockApi diff --git a/packages/ipfs-unixfs-importer/test/helpers/finite-pseudorandom-byte-stream.js b/packages/ipfs-unixfs-importer/test/helpers/finite-pseudorandom-byte-stream.js index f1e1ee2d..b3fde114 100644 --- a/packages/ipfs-unixfs-importer/test/helpers/finite-pseudorandom-byte-stream.js +++ b/packages/ipfs-unixfs-importer/test/helpers/finite-pseudorandom-byte-stream.js @@ -1,12 +1,10 @@ -'use strict' - const REPEATABLE_CHUNK_SIZE = 300000 /** * @param {number} maxSize * @param {number} seed */ -module.exports = async function * (maxSize, seed) { +async function * stream (maxSize, seed) { const chunks = Math.ceil(maxSize / REPEATABLE_CHUNK_SIZE) let emitted = 0 const buf = new Uint8Array(REPEATABLE_CHUNK_SIZE) @@ -22,6 +20,8 @@ module.exports = async function * (maxSize, seed) { } } +export default stream + /** * @param {number} seed */ diff --git a/packages/ipfs-unixfs-importer/test/helpers/random-byte-stream.js b/packages/ipfs-unixfs-importer/test/helpers/random-byte-stream.js index e147d2ca..dc2963c3 100644 --- a/packages/ipfs-unixfs-importer/test/helpers/random-byte-stream.js +++ b/packages/ipfs-unixfs-importer/test/helpers/random-byte-stream.js @@ -1,5 +1,3 @@ -'use strict' - /** * @param {number} seed */ @@ -20,4 +18,4 @@ function random (seed) { return x - Math.floor(x) } -module.exports = randomByteStream +export default randomByteStream diff --git a/packages/ipfs-unixfs-importer/tsconfig.json b/packages/ipfs-unixfs-importer/tsconfig.json index c6eb8f25..446917f2 100644 --- a/packages/ipfs-unixfs-importer/tsconfig.json +++ b/packages/ipfs-unixfs-importer/tsconfig.json @@ -2,6 +2,7 @@ "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { "outDir": "dist", + "module": "es2020", "importsNotUsedAsValues": "preserve" }, "include": [ diff --git a/packages/ipfs-unixfs/.aegir.js b/packages/ipfs-unixfs/.aegir.cjs similarity index 100% rename from packages/ipfs-unixfs/.aegir.js rename to packages/ipfs-unixfs/.aegir.cjs diff --git a/packages/ipfs-unixfs/package.json b/packages/ipfs-unixfs/package.json index 45e867b1..852adf9b 100644 --- a/packages/ipfs-unixfs/package.json +++ b/packages/ipfs-unixfs/package.json @@ -4,16 +4,17 @@ "description": "JavaScript implementation of IPFS' unixfs (a Unix FileSystem representation on top of a MerkleDAG)", "leadMaintainer": "Alex Potsides ", "main": "src/index.js", + "type": "module", "browser": { "fs": false }, "scripts": { - "prepare": "run-s prepare:*", - "prepare:proto": "pbjs -t static-module -w commonjs -r ipfs-unixfs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/unixfs.js ./src/unixfs.proto", + "prepare": "aegir build", + "prepare:proto": "pbjs -t static-module -w es6 -r ipfs-unixfs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/unixfs.js ./src/unixfs.proto", "prepare:proto-types": "pbts -o src/unixfs.d.ts src/unixfs.js", "prepare:types": "aegir build --no-bundle", "test": "aegir test", - "build": "aegir build", + "build": "aegir build --esm-tests", "clean": "rimraf ./dist", "lint": "aegir ts -p check && aegir lint", "coverage": "nyc -s aegir test -t node && nyc report --reporter=html", @@ -35,14 +36,17 @@ "npm": ">=7.0.0" }, "homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme", + "publishConfig": { + "directory": "dist" + }, "devDependencies": { "@types/mocha": "^8.2.1", - "aegir": "^34.0.0", + "aegir": "^35.0.1", "copy": "^0.3.2", "mkdirp": "^1.0.4", "npm-run-all": "^4.1.5", "nyc": "^15.0.0", - "uint8arrays": "^2.1.2", + "uint8arrays": "^3.0.0", "util": "^0.12.3" }, "dependencies": { @@ -50,12 +54,11 @@ "protobufjs": "^6.10.2" }, "types": "dist/src/index.d.ts", - "files": [ - "src", - "dist" - ], "eslintConfig": { "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + }, "ignorePatterns": [ "src/unixfs.d.ts" ] diff --git a/packages/ipfs-unixfs/src/index.js b/packages/ipfs-unixfs/src/index.js index 974d6cca..c50a27fd 100644 --- a/packages/ipfs-unixfs/src/index.js +++ b/packages/ipfs-unixfs/src/index.js @@ -1,9 +1,6 @@ -'use strict' - -const { - Data: PBData -} = require('./unixfs') -const errcode = require('err-code') +import errcode from 'err-code' +import * as Pb from './unixfs.js' +const PBData = Pb.Data /** * @typedef {import('./types').Mtime} Mtime @@ -30,7 +27,7 @@ const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8) /** * @param {string | number | undefined} [mode] */ -function parseMode (mode) { +export function parseMode (mode) { if (mode == null) { return undefined } @@ -53,7 +50,7 @@ function parseMode (mode) { /** * @param {any} input */ -function parseMtime (input) { +export function parseMtime (input) { if (input == null) { return undefined } @@ -122,7 +119,7 @@ function parseMtime (input) { return mtime } -class Data { +class UnixFS { /** * Decode from protobuf https://github.com/ipfs/specs/blob/master/UNIXFS.md * @@ -137,7 +134,7 @@ class Data { objects: false }) - const data = new Data({ + const data = new UnixFS({ type: types[decoded.Type], data: decoded.Data, blockSizes: decoded.blocksizes, @@ -330,8 +327,4 @@ class Data { } } -module.exports = { - UnixFS: Data, - parseMode, - parseMtime -} +export { UnixFS } diff --git a/packages/ipfs-unixfs/src/unixfs.js b/packages/ipfs-unixfs/src/unixfs.js index 41d33256..a5e8ab9e 100644 --- a/packages/ipfs-unixfs/src/unixfs.js +++ b/packages/ipfs-unixfs/src/unixfs.js @@ -1,15 +1,13 @@ /*eslint-disable*/ -"use strict"; - -var $protobuf = require("protobufjs/minimal"); +import $protobuf from "protobufjs/minimal.js"; // Common aliases -var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; +const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots["ipfs-unixfs"] || ($protobuf.roots["ipfs-unixfs"] = {}); +const $root = $protobuf.roots["ipfs-unixfs"] || ($protobuf.roots["ipfs-unixfs"] = {}); -$root.Data = (function() { +export const Data = $root.Data = (() => { /** * Properties of a Data. @@ -404,7 +402,7 @@ $root.Data = (function() { * @property {number} HAMTShard=5 HAMTShard value */ Data.DataType = (function() { - var valuesById = {}, values = Object.create(valuesById); + const valuesById = {}, values = Object.create(valuesById); values[valuesById[0] = "Raw"] = 0; values[valuesById[1] = "Directory"] = 1; values[valuesById[2] = "File"] = 2; @@ -417,7 +415,7 @@ $root.Data = (function() { return Data; })(); -$root.UnixTime = (function() { +export const UnixTime = $root.UnixTime = (() => { /** * Properties of an UnixTime. @@ -585,7 +583,7 @@ $root.UnixTime = (function() { return UnixTime; })(); -$root.Metadata = (function() { +export const Metadata = $root.Metadata = (() => { /** * Properties of a Metadata. @@ -717,4 +715,4 @@ $root.Metadata = (function() { return Metadata; })(); -module.exports = $root; +export { $root as default }; diff --git a/packages/ipfs-unixfs/test/unixfs-format.spec.js b/packages/ipfs-unixfs/test/unixfs-format.spec.js index 6e3d4bb6..7714282f 100644 --- a/packages/ipfs-unixfs/test/unixfs-format.spec.js +++ b/packages/ipfs-unixfs/test/unixfs-format.spec.js @@ -1,22 +1,19 @@ /* eslint-env mocha */ -'use strict' -const { expect } = require('aegir/utils/chai') +import { expect } from 'aegir/utils/chai.js' /** @type {(path: string) => Uint8Array} */ -// @ts-ignore -const loadFixture = require('aegir/utils/fixtures') +import loadFixture from 'aegir/utils/fixtures.js' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -const { UnixFS } = require('../src') +import { UnixFS } from '../src/index.js' +import * as Pb from '../src/unixfs.js' +const PBData = Pb.Data const raw = loadFixture('test/fixtures/raw.unixfs') const directory = loadFixture('test/fixtures/directory.unixfs') const file = loadFixture('test/fixtures/file.txt.unixfs') const symlink = loadFixture('test/fixtures/symlink.txt.unixfs') -const { - Data: PBData -} = require('../src/unixfs') -const uint8ArrayFromString = require('uint8arrays/from-string') describe('unixfs-format', () => { it('defaults to file', () => {