Skip to content
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 #339

Merged
merged 5 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
test-node:
needs: check
runs-on: ${{ matrix.os }}
name: Test ${{ matrix.project }} node
name: test ${{ matrix.project }} node ${{ matrix.node }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
Expand All @@ -37,6 +37,7 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run build
- run: npm run test -- --scope=${{ matrix.project }} -- -- --cov -t node
test-browser:
needs: check
Expand All @@ -60,6 +61,7 @@ jobs:
with:
node-version: 16
- run: npm install
- run: npm run build
- run: npm run test -- --scope=${{ matrix.project }} -- -- -t ${{ matrix.type }} -- --browser ${{ matrix.browser }}
# test-electron:
# needs: check
Expand All @@ -80,6 +82,7 @@ jobs:
# with:
# node-version: 16
# - run: npm install
# - run: npm run build
# - uses: GabrielBB/xvfb-action@v1
# with:
# run: npm run test -- --scope=${{ matrix.project }} -- -- -t ${{ matrix.type }} --bail
6 changes: 3 additions & 3 deletions packages/ipfs-repo-migrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ This framework:
### Use in Node.js

```js
const migrations = require('ipfs-repo-migrations')
const migrations from 'ipfs-repo-migrations')
```

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

```js
const migrations = require('ipfs-repo-migrations')
const migrations from 'ipfs-repo-migrations')
```

## Usage

Example:

```js
const migrations = require('ipfs-repo-migrations')
const migrations from 'ipfs-repo-migrations')

const repoPath = 'some/repo/path'
const currentRepoVersion = 7
Expand Down
15 changes: 9 additions & 6 deletions packages/ipfs-repo-migrations/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict'
import { migration as migration8 } from './migration-8/index.js'
import { migration as migration9 } from './migration-9/index.js'
import { migration as migration10 } from './migration-10/index.js'
import { migration as migration11 } from './migration-11/index.js'

/**
* @type {import('../src/types').Migration}
Expand All @@ -12,16 +15,16 @@ const emptyMigration = {
empty: true
}

module.exports = [
export default [
Object.assign({ version: 1 }, emptyMigration),
Object.assign({ version: 2 }, emptyMigration),
Object.assign({ version: 3 }, emptyMigration),
Object.assign({ version: 4 }, emptyMigration),
Object.assign({ version: 5 }, emptyMigration),
Object.assign({ version: 6 }, emptyMigration),
Object.assign({ version: 7 }, emptyMigration),
require('./migration-8'),
require('./migration-9'),
require('./migration-10'),
require('./migration-11')
migration8,
migration9,
migration10,
migration11
]
13 changes: 5 additions & 8 deletions packages/ipfs-repo-migrations/migrations/migration-10/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'

const {
findLevelJs
} = require('../../src/utils')
const { fromString } = require('uint8arrays/from-string')
const { toString } = require('uint8arrays/to-string')
import { findLevelJs } from '../../src/utils.js'
import { fromString } from 'uint8arrays/from-string'
import { toString } from 'uint8arrays/to-string'

/**
* @typedef {import('../../src/types').Migration} Migration
Expand Down Expand Up @@ -131,7 +128,7 @@ async function process (backends, onProgress, fn) {
}

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

try {
req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
} catch (err) {
} catch (/** @type {any} */ err) {
error = err
transaction.abort()
return
Expand Down
21 changes: 12 additions & 9 deletions packages/ipfs-repo-migrations/migrations/migration-11/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

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

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

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

const root = await backends.root.get(MFS_ROOT_KEY)
await backends.datastore.put(MFS_ROOT_KEY, root)
await backends.root.delete(MFS_ROOT_KEY)
if (await backends.root.has(MFS_ROOT_KEY)) {
const root = await backends.root.get(MFS_ROOT_KEY)
await backends.datastore.put(MFS_ROOT_KEY, root)
await backends.root.delete(MFS_ROOT_KEY)
}

await backends.datastore.close()
await backends.root.close()
Expand All @@ -34,9 +35,11 @@ async function storeMfsRootInRoot (backends, onProgress = () => {}) {
await backends.root.open()
await backends.datastore.open()

const root = await backends.datastore.get(MFS_ROOT_KEY)
await backends.root.put(MFS_ROOT_KEY, root)
await backends.datastore.delete(MFS_ROOT_KEY)
if (await backends.datastore.has(MFS_ROOT_KEY)) {
const root = await backends.datastore.get(MFS_ROOT_KEY)
await backends.root.put(MFS_ROOT_KEY, root)
await backends.datastore.delete(MFS_ROOT_KEY)
}

await backends.datastore.close()
await backends.root.close()
Expand All @@ -45,7 +48,7 @@ async function storeMfsRootInRoot (backends, onProgress = () => {}) {
}

/** @type {import('../../src/types').Migration} */
module.exports = {
export const migration = {
version: 11,
description: 'Store mfs root in the datastore',
migrate: storeMfsRootInDatastore,
Expand Down
20 changes: 10 additions & 10 deletions packages/ipfs-repo-migrations/migrations/migration-8/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

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

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

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

return new Key(`/${multihashStr}`, false)
} catch (err) {
} catch (/** @type {any} */ err) {
return key
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ async function process (backends, onProgress, keyFunction) {
}

/** @type {Migration} */
module.exports = {
export const migration = {
version: 8,
description: 'Transforms key names into base32 encoding and converts Block store to use bare multihashes encoded as base32',
migrate: (backends, onProgress = () => {}) => {
Expand Down
23 changes: 11 additions & 12 deletions packages/ipfs-repo-migrations/migrations/migration-9/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

const { CID } = require('multiformats/cid')
const dagPb = require('@ipld/dag-pb')
const cbor = require('cborg')
const pinset = require('./pin-set')
const { cidToKey, PIN_DS_KEY, PinTypes } = require('./utils')
const length = require('it-length')
const { sha256 } = require('multiformats/hashes/sha2')
const mhd = require('multiformats/hashes/digest')
const { base32 } = require('multiformats/bases/base32')

import { CID } from 'multiformats/cid'
import * as dagPb from '@ipld/dag-pb'
import * as cbor from 'cborg'
import * as pinset from './pin-set.js'
import { cidToKey, PIN_DS_KEY, PinTypes } from './utils.js'
import length from 'it-length'
import { sha256 } from 'multiformats/hashes/sha2'
import * as mhd from 'multiformats/hashes/digest'
import { base32 } from 'multiformats/bases/base32'

/**
* @typedef {import('../../src/types').Migration} Migration
Expand Down Expand Up @@ -153,7 +152,7 @@ async function process (backends, onProgress, fn) {
}

/** @type {Migration} */
module.exports = {
export const migration = {
version: 9,
description: 'Migrates pins to datastore',
migrate: (backends, onProgress = () => {}) => {
Expand Down
41 changes: 15 additions & 26 deletions packages/ipfs-repo-migrations/migrations/migration-9/pin-set.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
'use strict'

const { CID } = require('multiformats/cid')
const {
ipfs: {
pin: {
Set: PinSet
}
}
} = require('./pin')

import { CID } from 'multiformats/cid'
import { ipfs } from './pin.js'
// @ts-ignore
const fnv1a = require('fnv1a')
const varint = require('varint')
const dagPb = require('@ipld/dag-pb')
const { DEFAULT_FANOUT, MAX_ITEMS, EMPTY_KEY } = require('./utils')
const { concat: uint8ArrayConcat } = require('uint8arrays/concat')
const { compare: uint8ArrayCompare } = require('uint8arrays/compare')
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { sha256 } = require('multiformats/hashes/sha2')
import fnv1a from 'fnv1a'
import varint from 'varint'
import * as dagPb from '@ipld/dag-pb'
import { DEFAULT_FANOUT, MAX_ITEMS, EMPTY_KEY } from './utils.js'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { sha256 } from 'multiformats/hashes/sha2'

const PinSet = ipfs.pin.Set

/**
* @typedef {import('interface-datastore').Datastore} Datastore
Expand Down Expand Up @@ -125,7 +119,7 @@ async function * walkItems (blockstore, node) {
* @param {PBNode} rootNode
* @param {string} name
*/
async function * loadSet (blockstore, rootNode, name) {
export async function * loadSet (blockstore, rootNode, name) {
const link = rootNode.Links.find(l => l.Name === name)

if (!link) {
Expand Down Expand Up @@ -253,7 +247,7 @@ function storeItems (blockstore, items) {
* @param {string} type
* @param {CID[]} cids
*/
async function storeSet (blockstore, type, cids) {
export async function storeSet (blockstore, type, cids) {
const rootNode = await storeItems(blockstore, cids.map(cid => {
return {
key: cid
Expand All @@ -273,8 +267,3 @@ async function storeSet (blockstore, type, cids) {
Hash: cid
}
}

module.exports = {
loadSet,
storeSet
}
16 changes: 7 additions & 9 deletions packages/ipfs-repo-migrations/migrations/migration-9/pin.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/*eslint-disable*/
"use strict";

var $protobuf = require("protobufjs/minimal");
import $protobuf from "protobufjs/minimal.js";

// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});

$root.ipfs = (function() {
export const ipfs = $root.ipfs = (() => {

/**
* Namespace ipfs.
* @exports ipfs
* @namespace
*/
var ipfs = {};
const ipfs = {};

ipfs.pin = (function() {

Expand All @@ -25,7 +23,7 @@ $root.ipfs = (function() {
* @memberof ipfs
* @namespace
*/
var pin = {};
const pin = {};

pin.Set = (function() {

Expand Down Expand Up @@ -207,4 +205,4 @@ $root.ipfs = (function() {
return ipfs;
})();

module.exports = $root;
export { $root as default };
28 changes: 9 additions & 19 deletions packages/ipfs-repo-migrations/migrations/migration-9/utils.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
'use strict'

const { Key } = require('interface-datastore')
const { base32 } = require('multiformats/bases/base32')
const { CID } = require('multiformats')
import { Key } from 'interface-datastore/key'
import { base32 } from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'

const PIN_DS_KEY = new Key('/local/pins')
const DEFAULT_FANOUT = 256
const MAX_ITEMS = 8192
const EMPTY_KEY = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
export const PIN_DS_KEY = new Key('/local/pins')
export const DEFAULT_FANOUT = 256
export const MAX_ITEMS = 8192
export const EMPTY_KEY = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')

const PinTypes = {
export const PinTypes = {
direct: 'direct',
recursive: 'recursive'
}

/**
* @param {import('multiformats').CID} cid
*/
function cidToKey (cid) {
export function cidToKey (cid) {
return new Key(`/${base32.encode(cid.multihash.bytes).toUpperCase().substring(1)}`)
}

module.exports = {
PIN_DS_KEY,
DEFAULT_FANOUT,
MAX_ITEMS,
EMPTY_KEY,
PinTypes,
cidToKey
}
Loading