Skip to content

chore: switch to ESM #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ jobs:
script:
- npm run depcheck -- $RUN_SINCE -- -- -p

- stage: check
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we are currently living on a fork ok dependency-check in aegir: https://github.com/ipfs/aegir/blob/master/package.json#L59

I am not aware of the history behind this, but apparently it was only released as non stable. They are working now on version 5 with the next label, but I am not sure if this will be fixed: https://www.npmjs.com/package/dependency-check

The problem with the ESM change is that it seems to believe nothing is being used...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like dependency-check uses detective to find which modules are required. Probably needs to use detective-es6 when parsing ESM modules.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think we just need to pass the --detective arg to dependency-check with a path to detective-es6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name: dep-check (unused deps)
script:
- npm run depcheck -- $RUN_SINCE -- -- -- --unused

- stage: test
name: chrome
addons:
Expand Down
14 changes: 7 additions & 7 deletions packages/ipfs-unixfs-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "JavaScript implementation of the UnixFs exporter used by IPFS",
"leadMaintainer": "Alex Potsides <alex.potsides@protocol.ai>",
"main": "src/index.js",
"type": "module",
"browser": {
"fs": false
},
"scripts": {
"prepare": "aegir build --no-bundle",
"prepare": "aegir build",
"test": "aegir test",
"build": "aegir build",
"clean": "rimraf ./dist",
Expand Down Expand Up @@ -36,7 +37,7 @@
"@types/mocha": "^8.2.1",
"@types/sinon": "^10.0.0",
"abort-controller": "^3.0.0",
"aegir": "^34.0.0",
"aegir": "https://gitpkg.now.sh/ipfs/aegir?feat/build-esm-modules-follow-up",
"copy": "^0.3.2",
"crypto-browserify": "^3.12.0",
"detect-node": "^2.0.4",
Expand Down Expand Up @@ -68,11 +69,10 @@
"uint8arrays": "^2.1.7"
},
"types": "dist/src/index.d.ts",
"files": [
"src",
"dist"
],
"eslintConfig": {
"extends": "ipfs"
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
}
}
22 changes: 7 additions & 15 deletions packages/ipfs-unixfs-exporter/src/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -151,9 +149,3 @@ async function * recursive (path, blockstore, options = {}) {
}
}
}

module.exports = {
exporter,
walkPath,
recursive
}
10 changes: 4 additions & 6 deletions packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -72,4 +70,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o
}
}

module.exports = resolve
export default resolve
12 changes: 5 additions & 7 deletions packages/ipfs-unixfs-exporter/src/resolvers/identity.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,4 +50,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o
}
}

module.exports = resolve
export default resolve
26 changes: 15 additions & 11 deletions packages/ipfs-unixfs-exporter/src/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'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')
// 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
Expand All @@ -16,10 +20,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]: unixFs1Resolver,
[raw.code]: rawResolver,
[dagCbor.code]: dagCborResolver,
[identity.code]: identityResolver
}

/**
Expand All @@ -35,4 +39,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
10 changes: 4 additions & 6 deletions packages/ipfs-unixfs-exporter/src/resolvers/raw.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,4 +49,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o
}
}

module.exports = resolve
export default resolve
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
* @typedef {import('../../../types').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
Expand Down Expand Up @@ -31,4 +29,4 @@ const directoryContent = (cid, node, unixfs, path, resolve, depth, blockstore) =
return yieldDirectoryContent
}

module.exports = directoryContent
export default directoryContent
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -126,4 +124,4 @@ const fileContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
return yieldFileContent
}

module.exports = fileContent
export default fileContent
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const { decode } = require('@ipld/dag-pb')
import { decode } from '@ipld/dag-pb'

/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
Expand Down Expand Up @@ -58,4 +56,4 @@ async function * listDirectory (node, path, resolve, depth, blockstore, options)
}
}

module.exports = hamtShardedDirectoryContent
export default hamtShardedDirectoryContent
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,4 +31,4 @@ const rawContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
return yieldRawContent
}

module.exports = rawContent
export default rawContent
23 changes: 13 additions & 10 deletions packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'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')
// 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
Expand All @@ -26,10 +29,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 () => []
},
Expand Down Expand Up @@ -109,4 +112,4 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks
}
}

module.exports = unixFsResolver
export default unixFsResolver
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -28,3 +26,5 @@ module.exports = function extractDataFromBlock (block, blockStart, requestedStar

return block
}

export default extractDataFromBlock
11 changes: 5 additions & 6 deletions packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js
Original file line number Diff line number Diff line change
@@ -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 uint8ArrayFromString from 'uint8arrays/from-string.js'

/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const errCode = require('err-code')
import errCode from 'err-code'

/**
* @param {number} size
Expand Down Expand Up @@ -38,4 +36,4 @@ const validateOffsetAndLength = (size, offset, length) => {
}
}

module.exports = validateOffsetAndLength
export default validateOffsetAndLength
29 changes: 14 additions & 15 deletions packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/* 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')
// @ts-ignore needs types properly fixed
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 uint8ArrayConcat from 'uint8arrays/concat.js'
import asAsyncIterable from './helpers/as-async-iterable.js'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'

const SHARD_SPLIT_THRESHOLD = 10

Expand Down
Loading