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 7 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
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,6 +4,7 @@
"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
},
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-with-dist",
"copy": "^0.3.2",
"crypto-browserify": "^3.12.0",
"detect-node": "^2.0.4",
Expand All @@ -52,7 +53,7 @@
"readable-stream": "^3.6.0",
"rimraf": "^3.0.2",
"sinon": "^11.1.1",
"uint8arrays": "^2.1.2",
"@vascosantos/uint8arrays": "^3.0.3",
"util": "^0.12.3"
},
"dependencies": {
Expand All @@ -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
38 changes: 22 additions & 16 deletions packages/ipfs-unixfs-exporter/src/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
'use strict'
import errCode from 'err-code'

const errCode = require('err-code')

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 * 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'

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

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

Choose a reason for hiding this comment

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

Could just import these as normal, I don't think there's a pressing need for a dynamic import.

case raw.code:
return (await (import('./raw.js'))).default
case dagCbor.code:
return (await (import('./dag-cbor.js'))).default
case identity.code:
return (await (import('./identity.js'))).default
default:
}
}

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

if (!resolver) {
throw errCode(new Error(`No resolver for code ${cid.code}`), 'ERR_NO_RESOLVER')
Expand All @@ -35,4 +41,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
42 changes: 23 additions & 19 deletions packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

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 errCode from 'err-code'
import { UnixFS } from 'ipfs-unixfs'
import findShardCid from '../../utils/find-cid-in-shard.js'
import { decode } from '@ipld/dag-pb'

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

/**
* @type {{ [key: string]: UnixfsV1Resolver }}
* @param {string} key
* @returns {Promise<UnixfsV1Resolver|undefined>}
*/
const contentExporters = {
raw: require('./content/file'),
file: require('./content/file'),
directory: require('./content/directory'),
'hamt-sharded-directory': require('./content/hamt-sharded-directory'),
metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => {
return () => []
},
symlink: (cid, node, unixfs, path, resolve, depth, blockstore) => {
return () => []
const importContentExporters = async (key) => {
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as the resolvers above, we're just introducing async for no reason.

switch (key) {
case 'raw':
case 'file':
return (await (import('./content/file.js'))).default
case 'directory':
return (await (import('./content/directory.js'))).default
case 'hamt-sharded-directory':
return (await (import('./content/hamt-sharded-directory.js'))).default
case 'metadata':
case 'symlink':
return () => () => []
default:
}
}

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

const contentExporter = await importContentExporters(unixfs.type)

return {
entry: {
type: unixfs.isDirectory() ? 'directory' : 'file',
name,
path,
cid,
// @ts-ignore
content: contentExporters[unixfs.type](cid, node, unixfs, path, resolve, depth, blockstore),
content: contentExporter(cid, node, unixfs, path, resolve, depth, blockstore),
unixfs,
depth,
node,
Expand All @@ -109,4 +113,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 { fromString as uint8ArrayFromString } from '@vascosantos/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
Loading