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

feat: esm #263

Merged
merged 9 commits into from
Dec 5, 2022
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ npm install magnet-uri
```

```js
const DHT = require('bittorrent-dht')
const magnet = require('magnet-uri')
import DHT from 'bittorrent-dht'
import magnet from 'magnet-uri'

const uri = 'magnet:?xt=urn:btih:e3811b9539cacff680e418124272177c47477157'
const parsed = magnet(uri)
Expand Down Expand Up @@ -93,7 +93,7 @@ boolean whether the `signature` and value `buffers` were generated by the
For example, for `dht_store` you can do:

``` js
const ed = require('ed25519-supercop')
import ed from 'ed25519-supercop'
const dht = new DHT({ verify: ed.verify })
```

Expand Down Expand Up @@ -224,7 +224,7 @@ will just be the hash of the content.
Here is a simple example of creating some immutable content on the dht:

``` js
const DHT = require('bittorrent-dht')
import DHT from 'bittorrent-dht'
const dht = new DHT()
const value = Buffer.alloc(200).fill('abc')

Expand Down Expand Up @@ -255,7 +255,7 @@ To make a mutable update, you will need to create an elliptic key and pack
values precisely according to the specification, like so:

``` js
const ed = require('bittorrent-dht-sodium')
import ed from 'bittorrent-dht-sodium'
const keypair = ed.keygen()

const value = Buffer.alloc(200).fill('whatever') // the payload you want to send
Expand All @@ -268,8 +268,8 @@ const opts = {
}
}

const DHT = require('bittorrent-dht')
const dht = new DHT
import DHT from 'bittorrent-dht'
const dht = new DHT()

dht.put(opts, function (err, hash) {
console.error('error=', err)
Expand All @@ -289,7 +289,7 @@ If you receive a key/value pair and you want to re-add to the dht it to keep it
alive you can just `put` it again.

``` js
const ed = require('bittorrent-dht-sodium')
import ed from 'bittorrent-dht-sodium'
const dht = new DHT({ verify: ed.verify }) // you MUST specify the "verify" param if you want to get mutable content, otherwise null will be returned

dht.get(key, function (err, res) {
Expand Down
24 changes: 13 additions & 11 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const bencode = require('bencode')
const debug = require('debug')('bittorrent-dht')
const KBucket = require('k-bucket')
const krpc = require('k-rpc')
const low = require('last-one-wins')
const LRU = require('lru')
const randombytes = require('randombytes')
const records = require('record-cache')
const simpleSha1 = require('simple-sha1')
const { EventEmitter } = require('events')
import { EventEmitter } from 'events'
import bencode from 'bencode'
import Debug from 'debug'
import KBucket from 'k-bucket'
import krpc from 'k-rpc'
import low from 'last-one-wins'
import LRU from 'lru'
import randombytes from 'randombytes'
import records from 'record-cache'
import simpleSha1 from 'simple-sha1'

const debug = Debug('bittorret-dht')

const ROTATE_INTERVAL = 5 * 60 * 1000 // rotate secrets every 5 minutes
const BUCKET_OUTDATED_TIMESPAN = 15 * 60 * 1000 // check nodes in bucket in 15 minutes old buckets
Expand Down Expand Up @@ -793,4 +795,4 @@ function toBuffer (str) {
throw new Error('Pass a buffer or a string')
}

module.exports = DHT
export default DHT
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/*! bittorrent-dht. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
const Client = require('./client')
const Server = require('./server')

module.exports = Client
module.exports.Client = Client
module.exports.Server = Server
export { default as Client, default } from './client.js'
export { default as Server } from './server.js'
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "bittorrent-dht",
"description": "Simple, robust, BitTorrent DHT implementation",
"type": "module",
"version": "10.0.7",
"author": {
"name": "WebTorrent LLC",
Expand Down Expand Up @@ -42,10 +43,12 @@
"peer-to-peer"
],
"engines": {
"node": ">=10"
"node": ">=12.20.0"
},
"license": "MIT",
"main": "index.js",
"exports": {
"import": "./index.js"
},
"repository": {
"type": "git",
"url": "git://github.com/webtorrent/bittorrent-dht.git"
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* For now, just export the client, which will work just fine. But, later, it'll
* be important to give out nodes evenly from across the DHT.
*/
module.exports = require('./client')
export { default } from './client.js'
6 changes: 3 additions & 3 deletions test/abort.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('explicitly set nodeId', t => {
const nodeId = common.randomId()
Expand Down
6 changes: 3 additions & 3 deletions test/announce.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('`announce` with {host: false}', t => {
t.plan(3)
Expand Down
6 changes: 3 additions & 3 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('explicitly set nodeId', t => {
const nodeId = common.randomId()
Expand Down
6 changes: 3 additions & 3 deletions test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

// https://github.com/webtorrent/bittorrent-dht/pull/36
test('bootstrap and listen to custom port', t => {
Expand Down
34 changes: 17 additions & 17 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
const crypto = require('crypto')
const ed = require('bittorrent-dht-sodium')
const ip = require('ip')
import crypto from 'node:crypto'
import ed from 'bittorrent-dht-sodium'
import ip from 'ip'

exports.failOnWarningOrError = (t, dht) => {
export const failOnWarningOrError = (t, dht) => {
dht.on('warning', err => { t.fail(err) })
dht.on('error', err => { t.fail(err) })
}

exports.randomHost = () => {
export const randomHost = () => {
return ip.toString(crypto.randomBytes(4))
}

exports.randomPort = () => {
export const randomPort = () => {
return crypto.randomBytes(2).readUInt16LE(0)
}

exports.randomAddr = () => {
return { host: exports.randomHost(), port: exports.randomPort() }
export const randomAddr = () => {
return { host: randomHost(), port: randomPort() }
}

exports.randomId = () => {
export const randomId = () => {
return crypto.randomBytes(20)
}

exports.addRandomNodes = (dht, num) => {
export const addRandomNodes = (dht, num) => {
for (let i = 0; i < num; i++) {
dht.addNode({
id: exports.randomId(),
host: exports.randomHost(),
port: exports.randomPort()
id: randomId(),
host: randomHost(),
port: randomPort()
})
}
}

exports.addRandomPeers = (dht, num) => {
export const addRandomPeers = (dht, num) => {
for (let i = 0; i < num; i++) {
dht._addPeer(exports.randomAddr(), exports.randomId())
dht._addPeer(randomAddr(), randomId())
}
}

exports.fill = (n, s) => {
export const fill = (n, s) => {
const bs = Buffer.from(s)
const b = Buffer.allocUnsafe(n)
for (let i = 0; i < n; i++) {
Expand All @@ -48,7 +48,7 @@ exports.fill = (n, s) => {
return b
}

exports.sign = keypair => {
export const sign = keypair => {
return buf => {
return ed.sign(buf, keypair.sk)
}
Expand Down
6 changes: 3 additions & 3 deletions test/dht_store_immutable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('local immutable put/get', t => {
t.plan(3)
Expand Down
10 changes: 5 additions & 5 deletions test/dht_store_mutable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const common = require('./common')
const DHT = require('../')
const ed = require('bittorrent-dht-sodium')
const test = require('tape')
const crypto = require('crypto')
import crypto from 'node:crypto'
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('local mutable put/get', t => {
t.plan(4)
Expand Down
10 changes: 5 additions & 5 deletions test/dht_test_vectors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
const crypto = require('crypto')
const ed = require('bittorrent-dht-sodium')
import crypto from 'node:crypto'
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

// test vectors from http://bittorrent.org/beps/bep_0044.html
test('dht store test vectors - test 1 (mutable)', t => {
Expand Down
6 changes: 3 additions & 3 deletions test/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('`node` event fires for each added node (100x)', t => {
const dht = new DHT({ bootstrap: false })
Expand Down
10 changes: 5 additions & 5 deletions test/horde.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const common = require('./common')
const DHT = require('../')
const once = require('once')
const parallel = require('run-parallel')
const test = require('tape')
import once from 'once'
import parallel from 'run-parallel'
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

const from = 2
const to = 20
Expand Down
6 changes: 3 additions & 3 deletions test/internal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('`ping` query send and response', t => {
t.plan(2)
Expand Down
6 changes: 3 additions & 3 deletions test/live/bep44.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('tape')
const DHT = require('../../')
const ed = require('bittorrent-dht-sodium')
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../../index.js'

test('Set and get before ready is emitted', t => {
const dht1 = new DHT()
Expand Down
4 changes: 2 additions & 2 deletions test/live/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DHT = require('../../')
const test = require('tape')
import test from 'tape'
import DHT from '../../index.js'

const pride = '1E69917FBAA2C767BCA463A96B5572785C6D8A12'.toLowerCase() // Pride & Prejudice
const leaves = 'D2474E86C95B19B8BCFDB92BC12C9D44667CFA36'.toLowerCase() // Leaves of Grass
Expand Down
4 changes: 2 additions & 2 deletions test/ping.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape')
const DHT = require('../')
import test from 'tape'
import DHT from '../index.js'

test('ping should clear clones', t => {
const dht1 = new DHT({ bootstrap: false })
Expand Down
10 changes: 5 additions & 5 deletions test/put-get.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
const ed = require('bittorrent-dht-sodium')
const bencode = require('bencode')
import bencode from 'bencode'
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('dht store with salt', t => {
t.plan(3)
Expand Down
8 changes: 4 additions & 4 deletions test/to-json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const common = require('./common')
const DHT = require('../')
const ed = require('bittorrent-dht-sodium')
const test = require('tape')
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('dht.toJSON: re-use dht nodes with `bootstrap` option', t => {
t.plan(1)
Expand Down
6 changes: 3 additions & 3 deletions test/updated-bucket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const common = require('./common')
const DHT = require('../')
const test = require('tape')
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'

test('adding a node updates the lastChange property', t => {
t.plan(3)
Expand Down