Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: convert to typescript (#300)
Browse files Browse the repository at this point in the history
- Converts to typescript
- Only named exports
- No more CJS, only ESM
- Runs tests on all supported environments
- Adds auto-publish
- Adds dependabot

BREAKING CHANGE: switch to named exports, ESM only
  • Loading branch information
achingbrain authored Mar 4, 2022
1 parent 3997d67 commit 9696346
Show file tree
Hide file tree
Showing 101 changed files with 10,444 additions and 11,257 deletions.
File renamed without changes.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![Build status](https://github.com/libp2p/js-libp2p-kad-dht/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/libp2p/js-libp2p-kad-dht/actions/workflows/test.yml)
[![Build Status](https://github.com/libp2p/js-libp2p-kad-dht/actions/workflows/js-test-and-release.yml/badge.svg?branch=main)](https://github.com/libp2p/js-libp2p-kad-dht/actions/workflows/js-test-and-release.yml)
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-kad-dht/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-kad-dht?branch=master)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-kad-dht.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-dht)
[![Bundle Size](https://flat.badgen.net/bundlephobia/minzip/libp2p-kad-dht)](https://bundlephobia.com/result?p=libp2p-kad-dht)
Expand All @@ -15,10 +15,6 @@

> JavaScript implementation of the Kademlia DHT for libp2p, based on [go-libp2p-kad-dht](https://github.com/libp2p/go-libp2p-kad-dht).
## Lead Maintainer <!-- omit in toc -->

[Vasco Santos](https://github.com/vasco-santos).

## Table of Contents <!-- omit in toc -->

- [Install](#install)
Expand All @@ -32,18 +28,19 @@
- [Spec](#spec)
- [Contribute](#contribute)
- [License](#license)
- [Contribution](#contribution)
## Install

### npm

```sh
> npm i libp2p-kad-dht
> npm i @libp2p/kad-dht
```

### Use in Node.js

```js
import { create } from 'libp2p-kad-dht'
import { create } from '@libp2p/kad-dht'
```

## API
Expand All @@ -55,7 +52,7 @@ The libp2p-kad-dht module offers 3 APIs: Peer Routing, Content Routing and Peer
### Custom secondary DHT in libp2p

```js
import { create } from 'libp2p-kad-dht'
import { create } from '@libp2p/kad-dht'

/**
* @param {Libp2p} libp2p
Expand Down Expand Up @@ -98,4 +95,11 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c

## License

MIT - Protocol Labs 2017
Licensed under either of

* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
132 changes: 56 additions & 76 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "libp2p-kad-dht",
"name": "@libp2p/kad-dht",
"version": "0.28.6",
"description": "JavaScript implementation of the Kad-DHT for libp2p",
"license": "Apache-2.0 OR MIT",
Expand All @@ -18,28 +18,24 @@
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*"
],
"src/*": [
"*",
"dist/*",
"dist/src/*"
]
}
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist"
"dist/src",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
},
"ignorePatterns": [
"src/message/dht.d.ts"
]
Expand Down Expand Up @@ -126,49 +122,48 @@
]
},
"scripts": {
"prepare": "npm run build",
"lint": "aegir ts -p check && aegir lint",
"build": "npm run build:proto && npm run build:proto-types && aegir build",
"build:proto": "pbjs -t static-module -w commonjs -r libp2p-dht-message --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/message/dht.js ./src/message/dht.proto",
"build:proto-types": "pbts -o src/message/dht.d.ts src/message/dht.js",
"test": "aegir test -t node",
"test:node": "aegir test -t node",
"docs": "aegir docs",
"release": "aegir release --docs -t node",
"release-minor": "aegir release --type minor --docs -t node",
"release-major": "aegir release --type major --docs -t node",
"coverage": "aegir coverage",
"coverage-publish": "aegir-coverage publish",
"sim": "node test/simulation/index.js",
"dep-check": "aegir dep-check"
"lint": "aegir lint",
"build": "tsc",
"postbuild": "npm run build:copy-proto-files",
"generate": "npm run generate:proto && npm run generate:proto-types",
"generate:proto": "pbjs -t static-module -w es6 -r libp2p-dht-message --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/message/dht.js ./src/message/dht.proto",
"generate:proto-types": "pbts -o src/message/dht.d.ts src/message/dht.js",
"build:copy-proto-files": "cp src/message/dht.js src/message/dht.d.ts dist/src/message",
"pretest": "npm run build",
"test": "aegir test -t node -f ./dist/test",
"test:node": "aegir test -t node -f ./dist/test",
"dep-check": "aegir dep-check",
"release": "semantic-release"
},
"dependencies": {
"@libp2p/crypto": "^0.22.7",
"@libp2p/interfaces": "^1.3.2",
"@libp2p/logger": "^1.0.2",
"@libp2p/peer-id": "^1.0.4",
"@libp2p/record": "^1.0.0",
"@libp2p/topology": "^1.0.3",
"@multiformats/multiaddr": "^10.0.0",
"any-signal": "^3.0.0",
"datastore-core": "^7.0.0",
"debug": "^4.3.1",
"err-code": "^3.0.0",
"hashlru": "^2.3.0",
"interface-datastore": "^6.0.2",
"it-all": "^1.0.5",
"it-drain": "^1.0.4",
"it-first": "^1.0.4",
"it-length": "^1.0.3",
"it-length-prefixed": "^5.0.2",
"it-length-prefixed": "^7.0.0",
"it-map": "^1.0.5",
"it-merge": "^1.0.3",
"it-parallel": "^2.0.1",
"it-pipe": "^1.1.0",
"it-pipe": "^2.0.3",
"it-stream-types": "^1.0.4",
"it-take": "^1.0.2",
"k-bucket": "^5.1.0",
"libp2p-crypto": "^0.21.0",
"libp2p-interfaces": "^4.0.0",
"libp2p-record": "^0.10.4",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.5",
"p-defer": "^3.0.0",
"p-map": "^4.0.0",
"p-queue": "^6.6.2",
"peer-id": "^0.16.0",
"p-defer": "^4.0.0",
"p-map": "^5.3.0",
"p-queue": "^7.2.0",
"private-ip": "^2.3.3",
"protobufjs": "^6.10.2",
"streaming-iterables": "^6.0.0",
Expand All @@ -177,44 +172,29 @@
"varint": "^6.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"aegir": "^36.0.1",
"async-iterator-all": "^1.0.0",
"datastore-level": "^7.0.1",
"@libp2p/interface-compliance-tests": "^1.1.3",
"@libp2p/peer-id-factory": "^1.0.4",
"@libp2p/peer-store": "^1.0.1",
"@types/lodash.random": "^3.2.6",
"@types/lodash.range": "^3.2.6",
"@types/node": "^16.11.7",
"@types/varint": "^6.0.0",
"@types/which": "^2.0.1",
"aegir": "^36.1.3",
"datastore-level": "^8.0.0",
"delay": "^5.0.0",
"execa": "^5.1.1",
"execa": "^6.0.0",
"it-filter": "^1.0.3",
"it-last": "^1.0.6",
"it-pair": "^1.0.0",
"libp2p": "next",
"it-pair": "^2.0.2",
"libp2p": "^0.36.2",
"lodash.random": "^3.2.0",
"lodash.range": "^3.2.0",
"p-retry": "^4.2.0",
"sinon": "^12.0.1",
"p-retry": "^5.0.0",
"sinon": "^13.0.1",
"which": "^2.0.2"
},
"browser": {
"./src/routing-table/generated-prefix-list.json": "./src/routing-table/generated-prefix-list-browser.json"
},
"contributors": [
"Vasco Santos <[email protected]>",
"achingbrain <[email protected]>",
"David Dias <[email protected]>",
"Jacob Heun <[email protected]>",
"kumavis <[email protected]>",
"dirkmc <[email protected]>",
"Alan Shaw <[email protected]>",
"Friedel Ziegelmayer <[email protected]>",
"Josh Field <[email protected]>",
"Hugo Dias <[email protected]>",
"Blake Byrnes <[email protected]>",
"Richard Schneider <[email protected]>",
"John Hiesey <[email protected]>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>",
"Lars Gierth <[email protected]>",
"Marcus Bernales <[email protected]>",
"Pedro Teixeira <[email protected]>",
"Qmstream <[email protected]>",
"Thomas Eizinger <[email protected]>"
]
"./dist/src/routing-table/generated-prefix-list.js": "./dist/src/routing-table/generated-prefix-list-browser.js"
}
}
40 changes: 19 additions & 21 deletions src/constants.js → src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
'use strict'

// MaxRecordAge specifies the maximum time that any node will hold onto a record
// from the time its received. This does not apply to any other forms of validity that
// the record may contain.
// For example, a record may contain an ipns entry with an EOL saying its valid
// until the year 2020 (a great time in the future). For that record to stick around
// it must be rebroadcasted more frequently than once every 'MaxRecordAge'

const second = exports.second = 1000
const minute = exports.minute = 60 * second
const hour = exports.hour = 60 * minute
export const second = 1000
export const minute = 60 * second
export const hour = 60 * minute

exports.MAX_RECORD_AGE = 36 * hour
export const MAX_RECORD_AGE = 36 * hour

exports.PROTOCOL_DHT = '/kad/1.0.0'
export const PROTOCOL_DHT = '/kad/1.0.0'

exports.RECORD_KEY_PREFIX = '/dht/record'
export const RECORD_KEY_PREFIX = '/dht/record'

exports.PROVIDER_KEY_PREFIX = '/dht/provider'
export const PROVIDER_KEY_PREFIX = '/dht/provider'

exports.PROVIDERS_LRU_CACHE_SIZE = 256
export const PROVIDERS_LRU_CACHE_SIZE = 256

exports.PROVIDERS_VALIDITY = 24 * hour
export const PROVIDERS_VALIDITY = 24 * hour

exports.PROVIDERS_CLEANUP_INTERVAL = hour
export const PROVIDERS_CLEANUP_INTERVAL = hour

exports.READ_MESSAGE_TIMEOUT = 10 * second
export const READ_MESSAGE_TIMEOUT = 10 * second

// The number of records that will be retrieved on a call to getMany()
exports.GET_MANY_RECORD_COUNT = 16
export const GET_MANY_RECORD_COUNT = 16

// K is the maximum number of requests to perform before returning failure
exports.K = 20
export const K = 20

// Alpha is the concurrency for asynchronous requests
exports.ALPHA = 3
export const ALPHA = 3

// How often we look for our closest DHT neighbours
exports.QUERY_SELF_INTERVAL = Number(5 * minute)
export const QUERY_SELF_INTERVAL = Number(5 * minute)

// How long to look for our closest DHT neighbours for
exports.QUERY_SELF_TIMEOUT = Number(30 * second)
export const QUERY_SELF_TIMEOUT = Number(30 * second)

// How often we try to find new peers
exports.TABLE_REFRESH_INTERVAL = Number(5 * minute)
export const TABLE_REFRESH_INTERVAL = Number(5 * minute)

// How how long to look for new peers for
exports.TABLE_REFRESH_QUERY_TIMEOUT = Number(30 * second)
export const TABLE_REFRESH_QUERY_TIMEOUT = Number(30 * second)

// When a timeout is not specified, run a query for this long
exports.DEFAULT_QUERY_TIMEOUT = Number(30 * second)
export const DEFAULT_QUERY_TIMEOUT = Number(30 * second)
Loading

0 comments on commit 9696346

Please sign in to comment.