Skip to content

Commit

Permalink
chore: switch to esm (#370)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses named exports only
  • Loading branch information
achingbrain authored Sep 9, 2021
1 parent 0a08449 commit 63edf01
Show file tree
Hide file tree
Showing 60 changed files with 451 additions and 617 deletions.
1 change: 0 additions & 1 deletion .aegir.js → .aegir.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'
const path = require('path')

/** @type {import('aegir').Options["build"]["config"]} */
const esbuild = {
Expand Down
83 changes: 39 additions & 44 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ on:
push:
branches:
- master
- 'release/**'
pull_request:
branches:
- master
- 'release/**'

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx aegir lint
- run: npx aegir build
- run: npx aegir dep-check
- uses: ipfs/aegir/actions/bundle-size@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- run: npm run build
- run: npm run lint
- run: npm run dep-check
test-node:
needs: check
runs-on: ${{ matrix.os }}
name: Test ${{ matrix.project }} node
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
Expand All @@ -35,44 +31,43 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npx aegir test -t node --bail --cov
- uses: codecov/codecov-action@v1
test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: microsoft/playwright-github-action@v1
- run: npm install
- run: npx aegir test -t browser -t webworker --bail
- uses: codecov/codecov-action@v1
test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: microsoft/playwright-github-action@v1
- run: npm install
- run: npx aegir test -t browser -t webworker --bail -- --browser firefox
test-webkit:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: microsoft/playwright-github-action@v1
- run: npm install
- run: npx aegir test -t browser -t webworker --bail --timeout 10000 -- --browser webkit
test-electron-main:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-main --bail
test-electron-renderer:
- run: npm run test --cov -t node -- --exit
test-browser:
needs: check
runs-on: ubuntu-latest
name: test ${{ matrix.browser }} ${{ matrix.type }}
strategy:
matrix:
browser:
- chromium
- firefox
type:
- browser
- webworker
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
- run: npm run test -- -t ${{ matrix.type }} -- --browser ${{ matrix.browser }}
test-electron:
needs: check
runs-on: ubuntu-latest
name: test ${{ matrix.type }}
strategy:
matrix:
type:
- electron-main
- electron-renderer
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
- run: npm install
- uses: GabrielBB/xvfb-action@v1
with:
run: npm run test -- -t ${{ matrix.type }} --bail --exit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ docs

test/test-repo/datastore

*.flamegraph
*.flamegraph
types
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
### Use in Node.js or in the browser with browserify, webpack or any other bundler

```js
const Bitswap = require('ipfs-bitswap')
const { createBitswap } from 'ipfs-bitswap'
```

### Use in a browser using a script tag
Expand Down
12 changes: 5 additions & 7 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-disable no-console */
'use strict'

const assert = require('assert')
const range = require('lodash.range')

const makeBlock = require('../test/utils/make-blocks')
const genBitswapNetwork = require('../test/utils/mocks').genBitswapNetwork
import assert from 'assert'
import range from 'lodash.range'
import { makeBlocks } from '../test/utils/make-blocks.js'
import { genBitswapNetwork } from '../test/utils/mocks.js'

const nodes = [2, 5, 10, 20]
const blockFactors = [1, 10, 100]
Expand Down Expand Up @@ -37,7 +35,7 @@ async function shutdown (nodeArr) {
}

async function round (nodeArr, blockFactor, n) {
const blocks = await makeBlock(n * blockFactor)
const blocks = await makeBlocks(n * blockFactor)
const cids = blocks.map((b) => b.cid)

console.info('put blockFactor amount of blocks per node')
Expand Down
15 changes: 7 additions & 8 deletions benchmarks/put-get.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint max-nested-callbacks: ["error", 5] */
/* eslint-disable no-console */
'use strict'

const Benchmark = require('benchmark')
const assert = require('assert')
const all = require('it-all')
const drain = require('it-drain')
const makeBlock = require('../test/utils/make-blocks')
const genBitswapNetwork = require('../test/utils/mocks').genBitswapNetwork
import Benchmark from 'benchmark'
import assert from 'assert'
import all from 'it-all'
import drain from 'it-drain'
import { makeBlocks } from '../test/utils/make-blocks.js'
import { genBitswapNetwork } from '../test/utils/mocks.js'

const suite = new Benchmark.Suite('put-get')

Expand All @@ -23,7 +22,7 @@ const blockSizes = [10, 1024, 10 * 1024]

blockCounts.forEach((n) => blockSizes.forEach((k) => {
suite.add(`put-get ${n} blocks of size ${k}`, async (defer) => {
const blocks = await makeBlock(n, k)
const blocks = await makeBlocks(n, k)

await drain(bitswap.putMany(blocks))

Expand Down
74 changes: 34 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
"description": "Node.js implementation of the Bitswap data exchange protocol used by IPFS",
"leadMaintainer": "Dirk McCormick <dirk@protocol.ai>",
"main": "src/index.js",
"types": "types/src/index.d.ts",
"type": "module",
"files": [
"*",
"!**/*.tsbuildinfo"
],
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
},
"ignorePatterns": [
"scripts/*",
"src/types/message/message.*",
"*.test-d.ts"
]
},
"browser": {
"./test/utils/create-libp2p-node": false
},
"types": "dist/src/index.d.ts",
"tsd": {
"directory": "test"
},
"typesVersions": {
"*": {
"src/*": [
Expand All @@ -22,36 +35,20 @@
]
}
},
"eslintConfig": {
"extends": "ipfs",
"ignorePatterns": [
"scripts/*",
"src/types/message/message.*",
"*.test-d.ts"
]
},
"files": [
"dist",
"src"
],
"scripts": {
"prepare": "npm run build",
"build": "npm run build:proto && npm run build:proto-types && npm run build:bundle",
"build:proto": "pbjs -t static-module -w commonjs -r ipfs-bitswap --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/types/message/message.js src/types/message/message.proto",
"build:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js",
"build:bundle": "aegir build",
"test": "aegir test",
"test:browser": "aegir test -t browser -t webworker",
"test:node": "aegir test -t node",
"test:types": "tsd",
"generate": "run-s generate:*",
"generate:proto": "pbjs -t static-module -w es6 -r ipfs-bitswap --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/types/message/message.js src/types/message/message.proto",
"generate:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js",
"build": "aegir build",
"clean": "rimraf dist types",
"lint": "aegir ts -p check && aegir lint",
"check": "aegir ts -p check",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"release": "aegir release --target node",
"release-minor": "aegir release --type minor --target node",
"release-major": "aegir release --type major --target node",
"pretest": "aegir build --esm-tests",
"test": "aegir test --target node -- --exit",
"dep-check": "aegir dep-check -i rimraf",
"bench": "node benchmarks/index",
"coverage": "aegir test -t node --cov && nyc report --reporter=html",
"docs": "aegir docs",
"benchmarks": "node test/benchmarks/get-many"
},
"repository": {
Expand Down Expand Up @@ -79,18 +76,19 @@
"assert": "^2.0.0",
"benchmark": "^2.1.4",
"delay": "^5.0.0",
"interface-datastore": "^5.0.0",
"interface-datastore": "^6.0.2",
"iso-random-stream": "^2.0.0",
"it-all": "^1.0.5",
"it-drain": "^1.0.4",
"libp2p": "libp2p/js-libp2p",
"libp2p-kad-dht": "^0.23.1",
"libp2p": "^0.32.4",
"libp2p-kad-dht": "^0.24.1",
"libp2p-mplex": "^0.10.2",
"libp2p-tcp": "^0.17.1",
"lodash.difference": "^4.5.0",
"lodash.flatten": "^4.4.0",
"lodash.range": "^3.2.0",
"lodash.without": "^4.4.0",
"npm-run-all": "^4.1.5",
"p-defer": "^3.0.0",
"p-event": "^4.2.0",
"p-wait-for": "^3.2.0",
Expand All @@ -100,7 +98,6 @@
"rimraf": "^3.0.2",
"sinon": "^11.1.1",
"stats-lite": "^2.2.0",
"tsd": "^0.17.0",
"url": "^0.11.0",
"util": "^0.12.3",
"uuid": "^8.3.2",
Expand All @@ -110,9 +107,10 @@
"@vascosantos/moving-average": "^1.1.0",
"abort-controller": "^3.0.0",
"any-signal": "^2.1.2",
"blockstore-core": "^1.0.2",
"debug": "^4.2.0",
"err-code": "^3.0.1",
"interface-blockstore": "^1.0.0",
"interface-blockstore": "^2.0.2",
"it-length-prefixed": "^5.0.2",
"it-pipe": "^1.1.0",
"just-debounce-it": "^1.1.0",
Expand All @@ -125,10 +123,6 @@
"uint8arrays": "^3.0.0",
"varint-decoder": "^1.0.0"
},
"pre-push": [
"lint",
"test"
],
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
Expand Down
30 changes: 13 additions & 17 deletions src/bitswap.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use strict'

const WantManager = require('./want-manager')
const Network = require('./network')
const DecisionEngine = require('./decision-engine')
const Notifications = require('./notifications')
const logger = require('./utils').logger
const Stats = require('./stats')
const { AbortController } = require('native-abort-controller')
const { anySignal } = require('any-signal')
const { BlockstoreAdapter } = require('interface-blockstore')
const { CID } = require('multiformats')
import { WantManager } from './want-manager/index.js'
import { Network } from './network.js'
import { DecisionEngine } from './decision-engine/index.js'
import { Notifications } from './notifications.js'
import { logger } from './utils/index.js'
import { Stats } from './stats/index.js'
import { AbortController } from 'native-abort-controller'
import { anySignal } from 'any-signal'
import { BaseBlockstore } from 'blockstore-core/base'
import { CID } from 'multiformats/cid'

/**
* @typedef {import('./types').IPFSBitswap} IPFSBitswap
* @typedef {import('peer-id')} PeerId
* @typedef {import('./types/message')} BitswapMessage
* @typedef {import('./types/message').BitswapMessage} BitswapMessage
* @typedef {import('interface-blockstore').Blockstore} Blockstore
* @typedef {import('interface-blockstore').Pair} Pair
* @typedef {import('interface-blockstore').Options} Options
Expand Down Expand Up @@ -44,7 +42,7 @@ const statsKeys = [
*
* @implements {IPFSBitswap}
*/
class Bitswap extends BlockstoreAdapter {
export class Bitswap extends BaseBlockstore {
/**
* @param {import('libp2p')} libp2p
* @param {Blockstore} blockstore
Expand Down Expand Up @@ -278,7 +276,7 @@ class Bitswap extends BlockstoreAdapter {
const block = await this.blockstore.get(cid, options)

return block
} catch (err) {
} catch (/** @type {any} */ err) {
if (err.code !== 'ERR_NOT_FOUND') {
throw err
}
Expand Down Expand Up @@ -451,5 +449,3 @@ class Bitswap extends BlockstoreAdapter {
return this.blockstore
}
}

module.exports = Bitswap
18 changes: 7 additions & 11 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict'

const SECOND = 1000

module.exports = {
maxProvidersPerRequest: 3,
providerRequestTimeout: 10 * SECOND,
hasBlockTimeout: 15 * SECOND,
provideTimeout: 15 * SECOND,
kMaxPriority: Math.pow(2, 31) - 1,
maxListeners: 1000,
wantlistSendDebounceMs: 1
}
export const maxProvidersPerRequest = 3
export const providerRequestTimeout = 10 * SECOND
export const hasBlockTimeout = 15 * SECOND
export const provideTimeout = 15 * SECOND
export const kMaxPriority = Math.pow(2, 31) - 1
export const maxListeners = 1000
export const wantlistSendDebounceMs = 1
Loading

0 comments on commit 63edf01

Please sign in to comment.