-
Notifications
You must be signed in to change notification settings - Fork 37
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
chore: switch to ESM #161
Changes from 5 commits
985bbf3
f438708
733ccac
5616c33
6b5c08c
1ca3b7e
06956bd
bd6a57a
85fff58
16347de
c1ab1e6
ad16d46
0219739
57ee71d
7743fe6
752d354
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"description": "JavaScript implementation of the UnixFs exporter used by IPFS", | ||
"leadMaintainer": "Alex Potsides <[email protected]>", | ||
"main": "src/index.js", | ||
"type": "module", | ||
"browser": { | ||
"fs": false | ||
}, | ||
|
@@ -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", | ||
|
@@ -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": { | ||
|
@@ -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" | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
@@ -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 |
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 | ||
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
} | ||
} | ||
|
||
|
@@ -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, | ||
|
@@ -109,4 +113,4 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks | |
} | ||
} | ||
|
||
module.exports = unixFsResolver | ||
export default unixFsResolver |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
require
d. Probably needs to use detective-es6 when parsing ESM modules.There was a problem hiding this comment.
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 todependency-check
with a path todetective-es6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ipfs/aegir#869