Skip to content

Commit 985bbf3

Browse files
committed
chore: switch to ESM
BREAKING CHANGE: built content includes ESM and CJS
1 parent 84ecce0 commit 985bbf3

Some content is hidden

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

66 files changed

+410
-469
lines changed

packages/ipfs-unixfs-exporter/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
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": {
11-
"prepare": "aegir build --no-bundle",
12+
"prepare": "aegir build",
1213
"test": "aegir test",
1314
"build": "aegir build",
15+
"postbuild": "cd dist && npx json -I -f package.json -e 'this.types=\"src/index.d.ts\"'",
1416
"clean": "rimraf ./dist",
1517
"lint": "aegir ts -p check && aegir lint",
1618
"coverage": "nyc -s npm run test -t node && nyc report --reporter=html",
@@ -36,7 +38,7 @@
3638
"@types/mocha": "^8.2.1",
3739
"@types/sinon": "^10.0.0",
3840
"abort-controller": "^3.0.0",
39-
"aegir": "^34.0.0",
41+
"aegir": "https://gitpkg.now.sh/ipfs/aegir?feat/build-esm-modules",
4042
"copy": "^0.3.2",
4143
"crypto-browserify": "^3.12.0",
4244
"detect-node": "^2.0.4",
@@ -45,6 +47,7 @@
4547
"it-all": "^1.0.5",
4648
"it-buffer-stream": "^2.0.0",
4749
"it-first": "^1.0.6",
50+
"json": "^11.0.0",
4851
"merge-options": "^3.0.4",
4952
"murmurhash3js-revisited": "^3.0.0",
5053
"native-abort-controller": "^1.0.3",
@@ -68,10 +71,6 @@
6871
"uint8arrays": "^2.1.7"
6972
},
7073
"types": "dist/src/index.d.ts",
71-
"files": [
72-
"src",
73-
"dist"
74-
],
7574
"eslintConfig": {
7675
"extends": "ipfs"
7776
}

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

Lines changed: 7 additions & 15 deletions
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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 5 additions & 7 deletions
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
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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+
// TODO: Lazy Load
9+
import unixFs1Resolver from './unixfs-v1/index.js'
10+
import rawResolver from './raw.js'
11+
import dagCborResolver from './dag-cbor.js'
12+
import identityResolver from './identity.js'
913

1014
/**
1115
* @typedef {import('../types').Resolver} Resolver
@@ -16,10 +20,10 @@ const { identity } = require('multiformats/hashes/identity')
1620
* @type {{ [ key: string ]: Resolver }}
1721
*/
1822
const resolvers = {
19-
[dagPb.code]: require('./unixfs-v1'),
20-
[raw.code]: require('./raw'),
21-
[dagCbor.code]: require('./dag-cbor'),
22-
[identity.code]: require('./identity')
23+
[dagPb.code]: unixFs1Resolver,
24+
[raw.code]: rawResolver,
25+
[dagCbor.code]: dagCborResolver,
26+
[identity.code]: identityResolver
2327
}
2428

2529
/**
@@ -35,4 +39,4 @@ function resolve (cid, name, path, toResolve, depth, blockstore, options) {
3539
return resolver(cid, name, path, toResolve, resolve, depth, blockstore, options)
3640
}
3741

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

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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 8 additions & 10 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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+
// TODO Lazy load
7+
import contentFile from './content/file.js'
8+
import contentDirectory from './content/directory.js'
9+
import contentHamtShardedDirectory from './content/hamt-sharded-directory.js'
710

811
/**
912
* @typedef {import('../../types').Resolve} Resolve
@@ -26,10 +29,10 @@ const findLinkCid = (node, name) => {
2629
* @type {{ [key: string]: UnixfsV1Resolver }}
2730
*/
2831
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'),
32+
raw: contentFile,
33+
file: contentFile,
34+
directory: contentDirectory,
35+
'hamt-sharded-directory': contentHamtShardedDirectory,
3336
metadata: (cid, node, unixfs, path, resolve, depth, blockstore) => {
3437
return () => []
3538
},
@@ -109,4 +112,4 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, blocks
109112
}
110113
}
111114

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

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const { Bucket, createHAMT } = require('hamt-sharding')
4-
const { decode } = require('@ipld/dag-pb')
3+
import { Bucket, createHAMT } from 'hamt-sharding'
4+
import { decode } from '@ipld/dag-pb'
55
// @ts-ignore - no types available
6-
const mur = require('murmurhash3js-revisited')
7-
const uint8ArrayFromString = require('uint8arrays/from-string')
6+
import mur from 'murmurhash3js-revisited'
7+
import uint8ArrayFromString from 'uint8arrays/from-string.js'
88

99
/**
1010
* @typedef {import('interface-blockstore').Blockstore} Blockstore
@@ -152,4 +152,4 @@ const findShardCid = async (node, name, blockstore, context, options) => {
152152
return findShardCid(node, name, blockstore, context, options)
153153
}
154154

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

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

Lines changed: 2 additions & 4 deletions
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

packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/* eslint-env mocha */
2-
'use strict'
3-
4-
const { expect } = require('aegir/utils/chai')
5-
const { UnixFS } = require('ipfs-unixfs')
6-
const all = require('it-all')
7-
const last = require('it-last')
8-
const randomBytes = require('it-buffer-stream')
9-
const { exporter, walkPath } = require('../src')
10-
const { importer } = require('ipfs-unixfs-importer')
11-
const dagPb = require('@ipld/dag-pb')
12-
const blockApi = require('./helpers/block')
13-
const uint8ArrayConcat = require('uint8arrays/concat')
14-
const asAsyncIterable = require('./helpers/as-async-iterable')
15-
const { CID } = require('multiformats/cid')
16-
const { sha256 } = require('multiformats/hashes/sha2')
2+
// @ts-ignore needs types properly fixed
3+
import { expect } from 'aegir/utils/chai.js'
4+
import { UnixFS } from 'ipfs-unixfs'
5+
import all from 'it-all'
6+
import last from 'it-last'
7+
import randomBytes from 'it-buffer-stream'
8+
import { exporter, walkPath } from '../src/index.js'
9+
import { importer } from 'ipfs-unixfs-importer'
10+
import * as dagPb from '@ipld/dag-pb'
11+
import blockApi from './helpers/block.js'
12+
import uint8ArrayConcat from 'uint8arrays/concat.js'
13+
import asAsyncIterable from './helpers/as-async-iterable.js'
14+
import { CID } from 'multiformats/cid'
15+
import { sha256 } from 'multiformats/hashes/sha2'
1716

1817
const SHARD_SPLIT_THRESHOLD = 10
1918

0 commit comments

Comments
 (0)