Skip to content

Commit 819267f

Browse files
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 protobufjs/protobuf.js#1230 BREAKING CHANGE: ./src/dir-sharded is not in the exports map so cannot be imported Co-authored-by: Alex Potsides <[email protected]>
1 parent 532a15b commit 819267f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+452
-494
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ jobs:
8787
name: electron main
8888
addons:
8989
firefox: latest
90-
script: npm run test -- -- -- -t electron-main
90+
script:
91+
- npm run build
92+
- npx lerna link # use publishConfig.directory
93+
- npm run test -- -- -- -t electron-main -f dist/cjs/node-test/*js
9194

9295
- stage: release
9396
# only run on changes to master

packages/ipfs-unixfs-exporter/package.json

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"description": "JavaScript implementation of the UnixFs exporter used by IPFS",
55
"leadMaintainer": "Alex Potsides <[email protected]>",
66
"main": "src/index.js",
7+
"type": "module",
78
"browser": {
89
"fs": false
910
},
1011
"scripts": {
1112
"prepare": "aegir build --no-bundle",
1213
"test": "aegir test",
13-
"build": "aegir build",
14+
"build": "aegir build --esm-tests",
1415
"clean": "rimraf ./dist",
1516
"lint": "aegir ts -p check && aegir lint",
1617
"coverage": "nyc -s npm run test -t node && nyc report --reporter=html",
@@ -32,14 +33,16 @@
3233
"npm": ">=7.0.0"
3334
},
3435
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
36+
"publishConfig": {
37+
"directory": "dist"
38+
},
3539
"devDependencies": {
3640
"@types/mocha": "^8.2.1",
3741
"@types/sinon": "^10.0.0",
3842
"abort-controller": "^3.0.0",
39-
"aegir": "^34.0.0",
43+
"aegir": "^35.0.1",
4044
"copy": "^0.3.2",
4145
"crypto-browserify": "^3.12.0",
42-
"detect-node": "^2.0.4",
4346
"events": "^3.3.0",
4447
"ipfs-unixfs-importer": "^8.0.2",
4548
"it-all": "^1.0.5",
@@ -52,7 +55,6 @@
5255
"readable-stream": "^3.6.0",
5356
"rimraf": "^3.0.2",
5457
"sinon": "^11.1.1",
55-
"uint8arrays": "^2.1.2",
5658
"util": "^0.12.3"
5759
},
5860
"dependencies": {
@@ -65,14 +67,13 @@
6567
"it-last": "^1.0.5",
6668
"multiformats": "^9.4.2",
6769
"murmurhash3js-revisited": "^3.0.0",
68-
"uint8arrays": "^2.1.7"
70+
"uint8arrays": "^3.0.0"
6971
},
7072
"types": "dist/src/index.d.ts",
71-
"files": [
72-
"src",
73-
"dist"
74-
],
7573
"eslintConfig": {
76-
"extends": "ipfs"
74+
"extends": "ipfs",
75+
"parserOptions": {
76+
"sourceType": "module"
77+
}
7778
}
7879
}

packages/ipfs-unixfs-exporter/src/index.js

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
const errCode = require('err-code')
4-
const { CID } = require('multiformats/cid')
5-
const resolve = require('./resolvers')
6-
const last = require('it-last')
1+
import errCode from 'err-code'
2+
import { CID } from 'multiformats/cid'
3+
import resolve from './resolvers/index.js'
4+
import last from 'it-last'
75

86
/**
97
* @typedef {import('ipfs-unixfs').UnixFS} UnixFS
@@ -65,7 +63,7 @@ const cidAndRest = (path) => {
6563
* @param {Blockstore} blockstore
6664
* @param {ExporterOptions} [options]
6765
*/
68-
async function * walkPath (path, blockstore, options = {}) {
66+
export async function * walkPath (path, blockstore, options = {}) {
6967
let {
7068
cid,
7169
toResolve
@@ -102,7 +100,7 @@ async function * walkPath (path, blockstore, options = {}) {
102100
* @param {Blockstore} blockstore
103101
* @param {ExporterOptions} [options]
104102
*/
105-
async function exporter (path, blockstore, options = {}) {
103+
export async function exporter (path, blockstore, options = {}) {
106104
const result = await last(walkPath(path, blockstore, options))
107105

108106
if (!result) {
@@ -117,7 +115,7 @@ async function exporter (path, blockstore, options = {}) {
117115
* @param {Blockstore} blockstore
118116
* @param {ExporterOptions} [options]
119117
*/
120-
async function * recursive (path, blockstore, options = {}) {
118+
export async function * recursive (path, blockstore, options = {}) {
121119
const node = await exporter(path, blockstore, options)
122120

123121
if (!node) {
@@ -151,9 +149,3 @@ async function * recursive (path, blockstore, options = {}) {
151149
}
152150
}
153151
}
154-
155-
module.exports = {
156-
exporter,
157-
walkPath,
158-
recursive
159-
}

packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
const { CID } = require('multiformats/cid')
4-
const errCode = require('err-code')
5-
const dagCbor = require('@ipld/dag-cbor')
1+
import { CID } from 'multiformats/cid'
2+
import errCode from 'err-code'
3+
import * as dagCbor from '@ipld/dag-cbor'
64

75
/**
86
* @typedef {import('../types').Resolver} Resolver
@@ -72,4 +70,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o
7270
}
7371
}
7472

75-
module.exports = resolve
73+
export default resolve

packages/ipfs-unixfs-exporter/src/resolvers/identity.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
const errCode = require('err-code')
4-
const extractDataFromBlock = require('../utils/extract-data-from-block')
5-
const validateOffsetAndLength = require('../utils/validate-offset-and-length')
6-
const mh = require('multiformats/hashes/digest')
1+
import errCode from 'err-code'
2+
import extractDataFromBlock from '../utils/extract-data-from-block.js'
3+
import validateOffsetAndLength from '../utils/validate-offset-and-length.js'
4+
import * as mh from 'multiformats/hashes/digest'
75

86
/**
97
* @typedef {import('../types').ExporterOptions} ExporterOptions
@@ -52,4 +50,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o
5250
}
5351
}
5452

55-
module.exports = resolve
53+
export default resolve
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
'use strict'
1+
import errCode from 'err-code'
22

3-
const errCode = require('err-code')
3+
import * as dagPb from '@ipld/dag-pb'
4+
import * as dagCbor from '@ipld/dag-cbor'
5+
import * as raw from 'multiformats/codecs/raw'
6+
import { identity } from 'multiformats/hashes/identity'
47

5-
const dagPb = require('@ipld/dag-pb')
6-
const dagCbor = require('@ipld/dag-cbor')
7-
const raw = require('multiformats/codecs/raw')
8-
const { identity } = require('multiformats/hashes/identity')
8+
import dagPbResolver from './unixfs-v1/index.js'
9+
import rawResolver from './raw.js'
10+
import dagCborResolver from './dag-cbor.js'
11+
import identifyResolver from './identity.js'
912

1013
/**
1114
* @typedef {import('../types').Resolver} Resolver
@@ -16,10 +19,10 @@ const { identity } = require('multiformats/hashes/identity')
1619
* @type {{ [ key: string ]: Resolver }}
1720
*/
1821
const resolvers = {
19-
[dagPb.code]: require('./unixfs-v1'),
20-
[raw.code]: require('./raw'),
21-
[dagCbor.code]: require('./dag-cbor'),
22-
[identity.code]: require('./identity')
22+
[dagPb.code]: dagPbResolver,
23+
[raw.code]: rawResolver,
24+
[dagCbor.code]: dagCborResolver,
25+
[identity.code]: identifyResolver
2326
}
2427

2528
/**
@@ -35,4 +38,4 @@ function resolve (cid, name, path, toResolve, depth, blockstore, options) {
3538
return resolver(cid, name, path, toResolve, resolve, depth, blockstore, options)
3639
}
3740

38-
module.exports = resolve
41+
export default resolve

packages/ipfs-unixfs-exporter/src/resolvers/raw.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
const errCode = require('err-code')
4-
const extractDataFromBlock = require('../utils/extract-data-from-block')
5-
const validateOffsetAndLength = require('../utils/validate-offset-and-length')
1+
import errCode from 'err-code'
2+
import extractDataFromBlock from '../utils/extract-data-from-block.js'
3+
import validateOffsetAndLength from '../utils/validate-offset-and-length.js'
64

75
/**
86
* @typedef {import('../types').ExporterOptions} ExporterOptions
@@ -51,4 +49,4 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, o
5149
}
5250
}
5351

54-
module.exports = resolve
52+
export default resolve

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
/**
42
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
53
* @typedef {import('../../../types').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
@@ -31,4 +29,4 @@ const directoryContent = (cid, node, unixfs, path, resolve, depth, blockstore) =
3129
return yieldDirectoryContent
3230
}
3331

34-
module.exports = directoryContent
32+
export default directoryContent

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
3-
const extractDataFromBlock = require('../../../utils/extract-data-from-block')
4-
const validateOffsetAndLength = require('../../../utils/validate-offset-and-length')
5-
const { UnixFS } = require('ipfs-unixfs')
6-
const errCode = require('err-code')
7-
const dagPb = require('@ipld/dag-pb')
8-
const dagCbor = require('@ipld/dag-cbor')
9-
const raw = require('multiformats/codecs/raw')
1+
import extractDataFromBlock from '../../../utils/extract-data-from-block.js'
2+
import validateOffsetAndLength from '../../../utils/validate-offset-and-length.js'
3+
import { UnixFS } from 'ipfs-unixfs'
4+
import errCode from 'err-code'
5+
import * as dagPb from '@ipld/dag-pb'
6+
import * as dagCbor from '@ipld/dag-cbor'
7+
import * as raw from 'multiformats/codecs/raw'
108

119
/**
1210
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
@@ -126,4 +124,4 @@ const fileContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
126124
return yieldFileContent
127125
}
128126

129-
module.exports = fileContent
127+
export default fileContent

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
const { decode } = require('@ipld/dag-pb')
1+
import { decode } from '@ipld/dag-pb'
42

53
/**
64
* @typedef {import('interface-blockstore').Blockstore} Blockstore
@@ -58,4 +56,4 @@ async function * listDirectory (node, path, resolve, depth, blockstore, options)
5856
}
5957
}
6058

61-
module.exports = hamtShardedDirectoryContent
59+
export default hamtShardedDirectoryContent

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
const extractDataFromBlock = require('../../../utils/extract-data-from-block')
4-
const validateOffsetAndLength = require('../../../utils/validate-offset-and-length')
1+
import extractDataFromBlock from '../../../utils/extract-data-from-block.js'
2+
import validateOffsetAndLength from '../../../utils/validate-offset-and-length.js'
53

64
/**
75
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
@@ -33,4 +31,4 @@ const rawContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
3331
return yieldRawContent
3432
}
3533

36-
module.exports = rawContent
34+
export default rawContent

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
'use strict'
1+
import errCode from 'err-code'
2+
import { UnixFS } from 'ipfs-unixfs'
3+
import findShardCid from '../../utils/find-cid-in-shard.js'
4+
import { decode } from '@ipld/dag-pb'
25

3-
const errCode = require('err-code')
4-
const { UnixFS } = require('ipfs-unixfs')
5-
const findShardCid = require('../../utils/find-cid-in-shard')
6-
const { decode } = require('@ipld/dag-pb')
6+
import contentFile from './content/file.js'
7+
import contentDirectory from './content/directory.js'
8+
import contentHamtShardedDirectory from './content/hamt-sharded-directory.js'
79

810
/**
911
* @typedef {import('../../types').Resolve} Resolve
@@ -26,10 +28,10 @@ const findLinkCid = (node, name) => {
2628
* @type {{ [key: string]: UnixfsV1Resolver }}
2729
*/
2830
const contentExporters = {
29-
raw: require('./content/file'),
30-
file: require('./content/file'),
31-
directory: require('./content/directory'),
32-
'hamt-sharded-directory': require('./content/hamt-sharded-directory'),
31+
raw: contentFile,
32+
file: contentFile,
33+
directory: contentDirectory,
34+
'hamt-sharded-directory': contentHamtShardedDirectory,
3335
metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => {
3436
return () => []
3537
},
@@ -109,4 +111,4 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks
109111
}
110112
}
111113

112-
module.exports = unixFsResolver
114+
export default unixFsResolver

packages/ipfs-unixfs-exporter/src/utils/extract-data-from-block.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
31
/**
42
* @param {Uint8Array} block
53
* @param {number} blockStart
64
* @param {number} requestedStart
75
* @param {number} requestedEnd
86
*/
9-
module.exports = function extractDataFromBlock (block, blockStart, requestedStart, requestedEnd) {
7+
function extractDataFromBlock (block, blockStart, requestedStart, requestedEnd) {
108
const blockLength = block.length
119
const blockEnd = blockStart + blockLength
1210

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

2927
return block
3028
}
29+
30+
export default extractDataFromBlock

packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use strict'
21

3-
const { Bucket, createHAMT } = require('hamt-sharding')
4-
const { decode } = require('@ipld/dag-pb')
2+
import { Bucket, createHAMT } from 'hamt-sharding'
3+
import { decode } from '@ipld/dag-pb'
54
// @ts-ignore - no types available
6-
const mur = require('murmurhash3js-revisited')
7-
const uint8ArrayFromString = require('uint8arrays/from-string')
5+
import mur from 'murmurhash3js-revisited'
6+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
87

98
/**
109
* @typedef {import('interface-blockstore').Blockstore} Blockstore
@@ -152,4 +151,4 @@ const findShardCid = async (node, name, blockstore, context, options) => {
152151
return findShardCid(node, name, blockstore, context, options)
153152
}
154153

155-
module.exports = findShardCid
154+
export default findShardCid

packages/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
const errCode = require('err-code')
1+
import errCode from 'err-code'
42

53
/**
64
* @param {number} size
@@ -38,4 +36,4 @@ const validateOffsetAndLength = (size, offset, length) => {
3836
}
3937
}
4038

41-
module.exports = validateOffsetAndLength
39+
export default validateOffsetAndLength

0 commit comments

Comments
 (0)