diff --git a/.gitignore b/.gitignore index 9d2f2fe4..1531bdf9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,42 +1,6 @@ -# While testing new npm -package-lock.json -yarn.lock - -# Logs -logs -*.log - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -.nyc_output/ - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release - -# Dependency directory -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules - dist - -test/test-repo-for* -docs - -test/test-repo/datastore - -*.flamegraph -types +.docs +.coverage +package-lock.json +yarn.lock diff --git a/README.md b/README.md index 9904ab6d..2804e0bc 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ # ipfs-bitswap -[![ipfs.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io) -[![IRC](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) -[![Discord](https://img.shields.io/discord/806902334369824788?style=flat-square)](https://discord.gg/ipfs) +[![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech) +[![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech) [![codecov](https://img.shields.io/codecov/c/github/ipfs/js-ipfs-bitswap.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-ipfs-bitswap) -[![CI](https://img.shields.io/github/workflow/status/ipfs/js-ipfs-bitswap/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/ipfs/js-ipfs-bitswap/actions/workflows/js-test-and-release.yml) +[![CI](https://img.shields.io/github/actions/workflow/status/ipfs/js-ipfs-bitswap/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/ipfs/js-ipfs-bitswap/actions/workflows/js-test-and-release.yml?query=branch%3Amaster) > JavaScript implementation of the Bitswap data exchange protocol used by IPFS ## Table of contents - [Install](#install) + - [Browser ` +``` + ```bash > npm install ipfs-bitswap ``` @@ -43,14 +52,6 @@ const { createBitswap } from 'ipfs-bitswap' ### Use in a browser using a script tag -Loading this module through a script tag will make the `IpfsBitswap` object available in the global namespace. - -```html - - - -``` - ## API See @@ -199,6 +200,10 @@ Use the browser Chrome to open and inspect the generated graph. ![Flame graph](https://ipfs.io/ipfs/QmVbyLgYfkLewNtzTAFwAEMmP2hTJgs8sSqsRTBNBjyQ1y) +## API Docs + +- + ## License Licensed under either of diff --git a/package.json b/package.json index 0cea6945..0a07a152 100644 --- a/package.json +++ b/package.json @@ -162,26 +162,26 @@ "dep-check": "aegir dep-check -i rimraf", "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/message/message.js src/message/message.proto", - "generate:proto-types": "pbts -o src/message/message.d.ts src/message/message.js" + "generate:proto-types": "pbts -o src/message/message.d.ts src/message/message.js", + "docs": "aegir docs" }, "dependencies": { "@libp2p/interface-connection": "^3.0.1", - "@libp2p/interface-peer-id": "^1.0.4", + "@libp2p/interface-peer-id": "^2.0.0", "@libp2p/interface-peer-store": "^1.2.0", - "@libp2p/topology": "^3.0.0", + "@libp2p/logger": "^2.0.5", + "@libp2p/topology": "^4.0.0", "@libp2p/tracked-map": "^3.0.0", "@multiformats/multiaddr": "^11.0.0", "@vascosantos/moving-average": "^1.1.0", "abortable-iterator": "^4.0.2", "any-signal": "^3.0.0", - "blockstore-core": "^2.0.1", - "debug": "^4.2.0", - "err-code": "^3.0.1", - "interface-blockstore": "^3.0.0", + "blockstore-core": "^3.0.0", + "interface-blockstore": "^4.0.0", "it-length-prefixed": "^8.0.2", "it-pipe": "^2.0.4", "just-debounce-it": "^3.0.1", - "multiformats": "^10.0.1", + "multiformats": "^11.0.0", "protobufjs": "^7.0.0", "readable-stream": "^4.0.0", "timeout-abort-controller": "^3.0.0", @@ -191,16 +191,15 @@ }, "devDependencies": { "@chainsafe/libp2p-noise": "^10.0.1", - "@libp2p/kad-dht": "^6.0.0", + "@libp2p/kad-dht": "^7.0.0", "@libp2p/mplex": "^7.0.0", - "@libp2p/peer-id": "^1.1.8", - "@libp2p/peer-id-factory": "^1.0.8", + "@libp2p/peer-id": "^2.0.0", + "@libp2p/peer-id-factory": "^2.0.0", "@libp2p/tcp": "^6.0.0", "@nodeutils/defaults-deep": "^1.1.0", - "@types/debug": "^4.1.5", "@types/stats-lite": "^2.2.0", "@types/varint": "^6.0.0", - "aegir": "^37.0.4", + "aegir": "^37.10.0", "assert": "^2.0.0", "benchmark": "^2.1.4", "delay": "^5.0.0", @@ -221,7 +220,7 @@ "promisify-es6": "^1.0.3", "protobufjs-cli": "^1.0.0", "rimraf": "^3.0.2", - "sinon": "^14.0.0", + "sinon": "^15.0.1", "stats-lite": "^2.2.0", "url": "^0.11.0", "util": "^0.12.3", diff --git a/src/bitswap.js b/src/bitswap.js index d262f9f2..30693ca8 100644 --- a/src/bitswap.js +++ b/src/bitswap.js @@ -43,7 +43,7 @@ const statsKeys = [ */ export class Bitswap extends BaseBlockstore { /** - * @param {import('libp2p').Libp2p} libp2p + * @param {import('@libp2p/interface-libp2p').Libp2p} libp2p * @param {Blockstore} blockstore * @param {object} [options] * @param {boolean} [options.statsEnabled=false] diff --git a/src/decision-engine/index.js b/src/decision-engine/index.js index 64bd10d6..df39864a 100644 --- a/src/decision-engine/index.js +++ b/src/decision-engine/index.js @@ -36,7 +36,7 @@ export class DecisionEngine { * @param {import('interface-blockstore').Blockstore} blockstore * @param {import('../network').Network} network * @param {import('../stats').Stats} stats - * @param {import('libp2p').Libp2p} libp2p + * @param {import('@libp2p/interface-libp2p').Libp2p} libp2p * @param {object} [opts] * @param {number} [opts.targetMessageSize] * @param {number} [opts.maxSizeReplaceHasWithBlock] diff --git a/src/index.js b/src/index.js index 672e08c0..cf3e0aa9 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ import { Bitswap } from './bitswap.js' */ /** - * @param {import('libp2p').Libp2p} libp2p + * @param {import('@libp2p/interface-libp2p').Libp2p} libp2p * @param {Blockstore} blockstore * @param {object} [options] * @param {boolean} [options.statsEnabled=false] diff --git a/src/message/index.js b/src/message/index.js index 820f1a2f..cbdc0eb9 100644 --- a/src/message/index.js +++ b/src/message/index.js @@ -7,7 +7,7 @@ import ve from '../utils/varint-encoder.js' import { isMapEqual } from '../utils/index.js' import { Message } from './message.js' import { BitswapMessageEntry as Entry } from './entry.js' -import errcode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' /** * @typedef {import('../types').MultihashHasherLoader} MultihashHasherLoader @@ -291,7 +291,7 @@ BitswapMessage.deserialize = async (raw, hashLoader) => { const hasher = hashAlg === sha256.code ? sha256 : hashLoader && await hashLoader.getHasher(hashAlg) if (!hasher) { - throw errcode(new Error('Unknown hash algorithm'), 'ERR_UNKNOWN_HASH_ALG') + throw new CodeError('Unknown hash algorithm', 'ERR_UNKNOWN_HASH_ALG') } // const hashLen = values[3] // We haven't need to use this so far diff --git a/src/network.js b/src/network.js index b4a24d1f..2b646727 100644 --- a/src/network.js +++ b/src/network.js @@ -32,7 +32,7 @@ const DEFAULT_INCOMING_STREAM_TIMEOUT = 30000 export class Network { /** - * @param {import('libp2p').Libp2p} libp2p + * @param {import('@libp2p/interface-libp2p').Libp2p} libp2p * @param {import('./bitswap').Bitswap} bitswap * @param {import('./stats').Stats} stats * @param {object} [options] @@ -84,7 +84,7 @@ export class Network { this._registrarIds = [] for (const protocol of this._protocols) { - this._registrarIds.push(await this._libp2p.registrar.register(protocol, topology)) + this._registrarIds.push(await this._libp2p.register(protocol, topology)) } // All existing connections are like new ones for us @@ -102,7 +102,7 @@ export class Network { // unregister protocol and handlers if (this._registrarIds != null) { for (const id of this._registrarIds) { - this._libp2p.registrar.unregister(id) + this._libp2p.unregister(id) } this._registrarIds = [] diff --git a/src/stats/index.js b/src/stats/index.js index a0cc7a52..69569122 100644 --- a/src/stats/index.js +++ b/src/stats/index.js @@ -23,7 +23,7 @@ const defaultOptions = { export class Stats extends EventEmitter { /** - * @param {import('libp2p').Libp2p} libp2p + * @param {import('@libp2p/interface-libp2p').Libp2p} libp2p * @param {string[]} [initialCounters] * @param {object} _options * @param {boolean} _options.enabled diff --git a/src/utils/index.js b/src/utils/index.js index a6df237f..1200567b 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,4 +1,4 @@ -import debug from 'debug' +import { logger as createLogger } from '@libp2p/logger' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' import { BitswapMessageEntry } from '../message/entry.js' @@ -17,9 +17,7 @@ export const logger = (id, subsystem) => { name.push(`${id.toString().slice(0, 8)}`) } - return Object.assign(debug(name.join(':')), { - error: debug(name.concat(['error']).join(':')) - }) + return createLogger(name.join(':')) } /** diff --git a/src/want-manager/index.js b/src/want-manager/index.js index a7d0f58b..35886792 100644 --- a/src/want-manager/index.js +++ b/src/want-manager/index.js @@ -17,7 +17,7 @@ export class WantManager { * @param {PeerId} peerId * @param {import('../network').Network} network * @param {import('../stats').Stats} stats - * @param {import('libp2p').Libp2p} libp2p + * @param {import('@libp2p/interface-libp2p').Libp2p} libp2p */ constructor (peerId, network, stats, libp2p) { /** @type {Map} */ diff --git a/src/wantlist/index.js b/src/wantlist/index.js index 4ef85228..2098191c 100644 --- a/src/wantlist/index.js +++ b/src/wantlist/index.js @@ -30,7 +30,7 @@ const sortBy = (fn, list) => { export class Wantlist { /** * @param {import('../stats').Stats} [stats] - * @param {import('libp2p').Libp2p} [libp2p] + * @param {import('@libp2p/interface-libp2p').Libp2p} [libp2p] */ constructor (stats, libp2p) { /** @type {Map} */ diff --git a/test/bitswap-stats.js b/test/bitswap-stats.js index 3ab26aaa..bfc34a4e 100644 --- a/test/bitswap-stats.js +++ b/test/bitswap-stats.js @@ -11,7 +11,7 @@ import { makeBlocks } from './utils/make-blocks.js' import { makePeerIds } from './utils/make-peer-id.js' /** - * @typedef {import('libp2p').Libp2p} Libp2p + * @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p * @typedef {import('multiformats/cid').CID} CID */ diff --git a/test/bitswap.js b/test/bitswap.js index c412f3fe..6069a668 100644 --- a/test/bitswap.js +++ b/test/bitswap.js @@ -12,7 +12,7 @@ import { BitswapMessage as Message } from '../src/message/index.js' import { createEd25519PeerId } from '@libp2p/peer-id-factory' /** - * @typedef {import('libp2p').Libp2p} Libp2p + * @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p */ /** @@ -34,10 +34,8 @@ describe('start/stop', () => { const libp2p = { handle: () => {}, unhandle: () => {}, - registrar: { - register: () => {}, - unregister: () => {} - }, + register: () => {}, + unregister: () => {}, getConnections: () => [] } // @ts-ignore not a full libp2p diff --git a/test/network/network.node.js b/test/network/network.node.js index 3c5c9e21..3b48dad5 100644 --- a/test/network/network.node.js +++ b/test/network/network.node.js @@ -14,7 +14,7 @@ import { CID } from 'multiformats/cid' import delay from 'delay' /** - * @typedef {import('libp2p').Libp2p} Libp2p + * @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p * @typedef {import('../../src/bitswap').Bitswap} Bitswap */ @@ -304,11 +304,8 @@ describe('network', () => { contentRouting: { findProviders: mockFindProviders }, - // @ts-expect-error incomplete implementation - registrar: { - register: sinon.stub(), - unregister: sinon.stub() - }, + register: sinon.stub(), + unregister: sinon.stub(), getConnections: () => [], dial: mockDial, handle: sinon.stub() @@ -345,9 +342,8 @@ describe('network', () => { const libp2p = { handle: sinon.stub(), - registrar: { - register: sinon.stub() - }, + register: sinon.stub(), + unregister: sinon.stub(), getConnections: () => [] } diff --git a/test/utils/create-libp2p-node.js b/test/utils/create-libp2p-node.js index c7768fc6..d7a5a4a5 100644 --- a/test/utils/create-libp2p-node.js +++ b/test/utils/create-libp2p-node.js @@ -10,7 +10,7 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory' import defaultsDeep from '@nodeutils/defaults-deep' /** - * @typedef {import('libp2p').Libp2p} Libp2p + * @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p * @typedef {import('libp2p').Libp2pOptions & { DHT?: boolean}} NodeOptions */ diff --git a/test/utils/distribution-test.js b/test/utils/distribution-test.js index 051e0802..c9a32520 100644 --- a/test/utils/distribution-test.js +++ b/test/utils/distribution-test.js @@ -10,7 +10,7 @@ import { connectAll } from './connect-all.js' import all from 'it-all' /** - * @typedef {import('libp2p').Libp2p} Libp2p + * @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p * @typedef {import('../../src/types').IPFSBitswap} Bitswap */ diff --git a/test/utils/mocks.js b/test/utils/mocks.js index 00b0c5f8..8933f2ec 100644 --- a/test/utils/mocks.js +++ b/test/utils/mocks.js @@ -14,7 +14,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' * @typedef {import('../../src/message').BitswapMessage} Message * @typedef {import('multiformats/cid').CID} CID * @typedef {import('@multiformats/multiaddr').Multiaddr} Multiaddr - * @typedef {import('libp2p').Libp2p} Libp2p + * @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p * @typedef {import('@libp2p/interface-peer-id').PeerId} PeerId * @typedef {import('@libp2p/interface-peer-store').PeerStore} PeerStore */ @@ -22,7 +22,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' /** * Create a mock libp2p node * - * @returns {import('libp2p').Libp2p} + * @returns {import('@libp2p/interface-libp2p').Libp2p} */ export const mockLibp2pNode = () => { const buf = uint8ArrayFromString('122019318b6e5e0cf93a2314bf01269a2cc23cd3dcd452d742cdb9379d8646f6e4a9', 'base16') @@ -33,10 +33,8 @@ export const mockLibp2pNode = () => { multiaddrs: [], handle () {}, unhandle () {}, - registrar: { - register () {}, - unregister () {} - }, + register () {}, + unregister () {}, contentRouting: { provide: async (/** @type {CID} */ cid) => {}, // eslint-disable-line require-await findProviders: async (/** @type {CID} */ cid, /** @type {number} **/ timeout) => { return [] } // eslint-disable-line require-await