Skip to content

Commit 8d673ba

Browse files
authored
chore: switch to esm (#339)
BREAKING CHANGE: only named exports are used, deep imports/requires are not possible
1 parent 0658ccc commit 8d673ba

Some content is hidden

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

77 files changed

+1143
-1108
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
test-node:
2323
needs: check
2424
runs-on: ${{ matrix.os }}
25-
name: Test ${{ matrix.project }} node
25+
name: test ${{ matrix.project }} node ${{ matrix.node }}
2626
strategy:
2727
matrix:
2828
os: [windows-latest, ubuntu-latest, macos-latest]
@@ -37,6 +37,7 @@ jobs:
3737
with:
3838
node-version: ${{ matrix.node }}
3939
- run: npm install
40+
- run: npm run build
4041
- run: npm run test -- --scope=${{ matrix.project }} -- -- --cov -t node
4142
test-browser:
4243
needs: check
@@ -60,6 +61,7 @@ jobs:
6061
with:
6162
node-version: 16
6263
- run: npm install
64+
- run: npm run build
6365
- run: npm run test -- --scope=${{ matrix.project }} -- -- -t ${{ matrix.type }} -- --browser ${{ matrix.browser }}
6466
# test-electron:
6567
# needs: check
@@ -80,6 +82,7 @@ jobs:
8082
# with:
8183
# node-version: 16
8284
# - run: npm install
85+
# - run: npm run build
8386
# - uses: GabrielBB/xvfb-action@v1
8487
# with:
8588
# run: npm run test -- --scope=${{ matrix.project }} -- -- -t ${{ matrix.type }} --bail

packages/ipfs-repo-migrations/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ This framework:
7777
### Use in Node.js
7878

7979
```js
80-
const migrations = require('ipfs-repo-migrations')
80+
const migrations from 'ipfs-repo-migrations')
8181
```
8282

8383
### Use in a browser with browserify, webpack or any other bundler
8484

8585
```js
86-
const migrations = require('ipfs-repo-migrations')
86+
const migrations from 'ipfs-repo-migrations')
8787
```
8888

8989
## Usage
9090

9191
Example:
9292

9393
```js
94-
const migrations = require('ipfs-repo-migrations')
94+
const migrations from 'ipfs-repo-migrations')
9595

9696
const repoPath = 'some/repo/path'
9797
const currentRepoVersion = 7
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
'use strict'
1+
import { migration as migration8 } from './migration-8/index.js'
2+
import { migration as migration9 } from './migration-9/index.js'
3+
import { migration as migration10 } from './migration-10/index.js'
4+
import { migration as migration11 } from './migration-11/index.js'
25

36
/**
47
* @type {import('../src/types').Migration}
@@ -12,16 +15,16 @@ const emptyMigration = {
1215
empty: true
1316
}
1417

15-
module.exports = [
18+
export default [
1619
Object.assign({ version: 1 }, emptyMigration),
1720
Object.assign({ version: 2 }, emptyMigration),
1821
Object.assign({ version: 3 }, emptyMigration),
1922
Object.assign({ version: 4 }, emptyMigration),
2023
Object.assign({ version: 5 }, emptyMigration),
2124
Object.assign({ version: 6 }, emptyMigration),
2225
Object.assign({ version: 7 }, emptyMigration),
23-
require('./migration-8'),
24-
require('./migration-9'),
25-
require('./migration-10'),
26-
require('./migration-11')
26+
migration8,
27+
migration9,
28+
migration10,
29+
migration11
2730
]

packages/ipfs-repo-migrations/migrations/migration-10/index.js

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

3-
const {
4-
findLevelJs
5-
} = require('../../src/utils')
6-
const { fromString } = require('uint8arrays/from-string')
7-
const { toString } = require('uint8arrays/to-string')
2+
import { findLevelJs } from '../../src/utils.js'
3+
import { fromString } from 'uint8arrays/from-string'
4+
import { toString } from 'uint8arrays/to-string'
85

96
/**
107
* @typedef {import('../../src/types').Migration} Migration
@@ -131,7 +128,7 @@ async function process (backends, onProgress, fn) {
131128
}
132129

133130
/** @type {Migration} */
134-
module.exports = {
131+
export const migration = {
135132
version: 10,
136133
description: 'Migrates datastore-level keys to binary',
137134
migrate: (backends, onProgress = () => {}) => {
@@ -174,7 +171,7 @@ function withEach (db, fn) {
174171

175172
try {
176173
req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
177-
} catch (err) {
174+
} catch (/** @type {any} */ err) {
178175
error = err
179176
transaction.abort()
180177
return

packages/ipfs-repo-migrations/migrations/migration-11/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict'
21

3-
const { Key } = require('interface-datastore')
2+
import { Key } from 'interface-datastore/key'
43

54
const MFS_ROOT_KEY = new Key('/local/filesroot')
65

@@ -14,9 +13,11 @@ async function storeMfsRootInDatastore (backends, onProgress = () => {}) {
1413
await backends.root.open()
1514
await backends.datastore.open()
1615

17-
const root = await backends.root.get(MFS_ROOT_KEY)
18-
await backends.datastore.put(MFS_ROOT_KEY, root)
19-
await backends.root.delete(MFS_ROOT_KEY)
16+
if (await backends.root.has(MFS_ROOT_KEY)) {
17+
const root = await backends.root.get(MFS_ROOT_KEY)
18+
await backends.datastore.put(MFS_ROOT_KEY, root)
19+
await backends.root.delete(MFS_ROOT_KEY)
20+
}
2021

2122
await backends.datastore.close()
2223
await backends.root.close()
@@ -34,9 +35,11 @@ async function storeMfsRootInRoot (backends, onProgress = () => {}) {
3435
await backends.root.open()
3536
await backends.datastore.open()
3637

37-
const root = await backends.datastore.get(MFS_ROOT_KEY)
38-
await backends.root.put(MFS_ROOT_KEY, root)
39-
await backends.datastore.delete(MFS_ROOT_KEY)
38+
if (await backends.datastore.has(MFS_ROOT_KEY)) {
39+
const root = await backends.datastore.get(MFS_ROOT_KEY)
40+
await backends.root.put(MFS_ROOT_KEY, root)
41+
await backends.datastore.delete(MFS_ROOT_KEY)
42+
}
4043

4144
await backends.datastore.close()
4245
await backends.root.close()
@@ -45,7 +48,7 @@ async function storeMfsRootInRoot (backends, onProgress = () => {}) {
4548
}
4649

4750
/** @type {import('../../src/types').Migration} */
48-
module.exports = {
51+
export const migration = {
4952
version: 11,
5053
description: 'Store mfs root in the datastore',
5154
migrate: storeMfsRootInDatastore,

packages/ipfs-repo-migrations/migrations/migration-8/index.js

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

3-
const { CID } = require('multiformats/cid')
4-
const Key = require('interface-datastore').Key
5-
const log = require('debug')('ipfs:repo:migrator:migration-8')
2+
import { CID } from 'multiformats/cid'
3+
import { Key } from 'interface-datastore/key'
4+
import debug from 'debug'
5+
import length from 'it-length'
6+
import { base32 } from 'multiformats/bases/base32'
7+
import * as raw from 'multiformats/codecs/raw'
8+
import * as mhd from 'multiformats/hashes/digest'
69

7-
const length = require('it-length')
8-
const { base32 } = require('multiformats/bases/base32')
9-
const raw = require('multiformats/codecs/raw')
10-
const mhd = require('multiformats/hashes/digest')
10+
const log = debug('ipfs:repo:migrator:migration-8')
1111

1212
/**
1313
* @typedef {import('../../src/types').Migration} Migration
@@ -41,7 +41,7 @@ function keyToMultihash (key) {
4141
const multihashStr = base32.encode(multihash).slice(1).toUpperCase()
4242

4343
return new Key(`/${multihashStr}`, false)
44-
} catch (err) {
44+
} catch (/** @type {any} */ err) {
4545
return key
4646
}
4747
}
@@ -105,7 +105,7 @@ async function process (backends, onProgress, keyFunction) {
105105
}
106106

107107
/** @type {Migration} */
108-
module.exports = {
108+
export const migration = {
109109
version: 8,
110110
description: 'Transforms key names into base32 encoding and converts Block store to use bare multihashes encoded as base32',
111111
migrate: (backends, onProgress = () => {}) => {

packages/ipfs-repo-migrations/migrations/migration-9/index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
'use strict'
2-
3-
const { CID } = require('multiformats/cid')
4-
const dagPb = require('@ipld/dag-pb')
5-
const cbor = require('cborg')
6-
const pinset = require('./pin-set')
7-
const { cidToKey, PIN_DS_KEY, PinTypes } = require('./utils')
8-
const length = require('it-length')
9-
const { sha256 } = require('multiformats/hashes/sha2')
10-
const mhd = require('multiformats/hashes/digest')
11-
const { base32 } = require('multiformats/bases/base32')
1+
2+
import { CID } from 'multiformats/cid'
3+
import * as dagPb from '@ipld/dag-pb'
4+
import * as cbor from 'cborg'
5+
import * as pinset from './pin-set.js'
6+
import { cidToKey, PIN_DS_KEY, PinTypes } from './utils.js'
7+
import length from 'it-length'
8+
import { sha256 } from 'multiformats/hashes/sha2'
9+
import * as mhd from 'multiformats/hashes/digest'
10+
import { base32 } from 'multiformats/bases/base32'
1211

1312
/**
1413
* @typedef {import('../../src/types').Migration} Migration
@@ -153,7 +152,7 @@ async function process (backends, onProgress, fn) {
153152
}
154153

155154
/** @type {Migration} */
156-
module.exports = {
155+
export const migration = {
157156
version: 9,
158157
description: 'Migrates pins to datastore',
159158
migrate: (backends, onProgress = () => {}) => {

packages/ipfs-repo-migrations/migrations/migration-9/pin-set.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
'use strict'
2-
3-
const { CID } = require('multiformats/cid')
4-
const {
5-
ipfs: {
6-
pin: {
7-
Set: PinSet
8-
}
9-
}
10-
} = require('./pin')
111

2+
import { CID } from 'multiformats/cid'
3+
import { ipfs } from './pin.js'
124
// @ts-ignore
13-
const fnv1a = require('fnv1a')
14-
const varint = require('varint')
15-
const dagPb = require('@ipld/dag-pb')
16-
const { DEFAULT_FANOUT, MAX_ITEMS, EMPTY_KEY } = require('./utils')
17-
const { concat: uint8ArrayConcat } = require('uint8arrays/concat')
18-
const { compare: uint8ArrayCompare } = require('uint8arrays/compare')
19-
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
20-
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
21-
const { sha256 } = require('multiformats/hashes/sha2')
5+
import fnv1a from 'fnv1a'
6+
import varint from 'varint'
7+
import * as dagPb from '@ipld/dag-pb'
8+
import { DEFAULT_FANOUT, MAX_ITEMS, EMPTY_KEY } from './utils.js'
9+
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
10+
import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
11+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
12+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
13+
import { sha256 } from 'multiformats/hashes/sha2'
14+
15+
const PinSet = ipfs.pin.Set
2216

2317
/**
2418
* @typedef {import('interface-datastore').Datastore} Datastore
@@ -125,7 +119,7 @@ async function * walkItems (blockstore, node) {
125119
* @param {PBNode} rootNode
126120
* @param {string} name
127121
*/
128-
async function * loadSet (blockstore, rootNode, name) {
122+
export async function * loadSet (blockstore, rootNode, name) {
129123
const link = rootNode.Links.find(l => l.Name === name)
130124

131125
if (!link) {
@@ -253,7 +247,7 @@ function storeItems (blockstore, items) {
253247
* @param {string} type
254248
* @param {CID[]} cids
255249
*/
256-
async function storeSet (blockstore, type, cids) {
250+
export async function storeSet (blockstore, type, cids) {
257251
const rootNode = await storeItems(blockstore, cids.map(cid => {
258252
return {
259253
key: cid
@@ -273,8 +267,3 @@ async function storeSet (blockstore, type, cids) {
273267
Hash: cid
274268
}
275269
}
276-
277-
module.exports = {
278-
loadSet,
279-
storeSet
280-
}

packages/ipfs-repo-migrations/migrations/migration-9/pin.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
/*eslint-disable*/
2-
"use strict";
3-
4-
var $protobuf = require("protobufjs/minimal");
2+
import $protobuf from "protobufjs/minimal.js";
53

64
// Common aliases
7-
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
5+
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
86

97
// Exported root namespace
10-
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
8+
const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
119

12-
$root.ipfs = (function() {
10+
export const ipfs = $root.ipfs = (() => {
1311

1412
/**
1513
* Namespace ipfs.
1614
* @exports ipfs
1715
* @namespace
1816
*/
19-
var ipfs = {};
17+
const ipfs = {};
2018

2119
ipfs.pin = (function() {
2220

@@ -25,7 +23,7 @@ $root.ipfs = (function() {
2523
* @memberof ipfs
2624
* @namespace
2725
*/
28-
var pin = {};
26+
const pin = {};
2927

3028
pin.Set = (function() {
3129

@@ -207,4 +205,4 @@ $root.ipfs = (function() {
207205
return ipfs;
208206
})();
209207

210-
module.exports = $root;
208+
export { $root as default };
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1-
'use strict'
21

3-
const { Key } = require('interface-datastore')
4-
const { base32 } = require('multiformats/bases/base32')
5-
const { CID } = require('multiformats')
2+
import { Key } from 'interface-datastore/key'
3+
import { base32 } from 'multiformats/bases/base32'
4+
import { CID } from 'multiformats/cid'
65

7-
const PIN_DS_KEY = new Key('/local/pins')
8-
const DEFAULT_FANOUT = 256
9-
const MAX_ITEMS = 8192
10-
const EMPTY_KEY = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
6+
export const PIN_DS_KEY = new Key('/local/pins')
7+
export const DEFAULT_FANOUT = 256
8+
export const MAX_ITEMS = 8192
9+
export const EMPTY_KEY = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
1110

12-
const PinTypes = {
11+
export const PinTypes = {
1312
direct: 'direct',
1413
recursive: 'recursive'
1514
}
1615

1716
/**
1817
* @param {import('multiformats').CID} cid
1918
*/
20-
function cidToKey (cid) {
19+
export function cidToKey (cid) {
2120
return new Key(`/${base32.encode(cid.multihash.bytes).toUpperCase().substring(1)}`)
2221
}
23-
24-
module.exports = {
25-
PIN_DS_KEY,
26-
DEFAULT_FANOUT,
27-
MAX_ITEMS,
28-
EMPTY_KEY,
29-
PinTypes,
30-
cidToKey
31-
}

0 commit comments

Comments
 (0)