Skip to content

Commit

Permalink
Revert "chore: switch to ESM (#248)" (#262)
Browse files Browse the repository at this point in the history
This reverts commit a12c9d1.
  • Loading branch information
ThaUnknown authored Dec 4, 2022
1 parent a12c9d1 commit 520c2de
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 101 deletions.
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
import DHT from 'bittorrent-dht'
import magnet from 'magnet-uri'
const DHT = require('bittorrent-dht')
const magnet = require('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
import ed from 'ed25519-supercop'
const ed = require('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
import DHT from 'bittorrent-dht'
const DHT = require('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
import ed from 'bittorrent-dht-sodium'
const ed = require('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 = {
}
}

import DHT from 'bittorrent-dht'
const dht = new DHT()
const DHT = require('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
import ed from 'bittorrent-dht-sodium'
const ed = require('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: 11 additions & 13 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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 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')

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 @@ -795,4 +793,4 @@ function toBuffer (str) {
throw new Error('Pass a buffer or a string')
}

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

module.exports = Client
module.exports.Client = Client
module.exports.Server = Server
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "bittorrent-dht",
"description": "Simple, robust, BitTorrent DHT implementation",
"type": "module",
"version": "10.0.6",
"author": {
"name": "WebTorrent LLC",
Expand Down Expand Up @@ -43,12 +42,10 @@
"peer-to-peer"
],
"engines": {
"node": ">=12.20.0"
"node": ">=10"
},
"license": "MIT",
"exports": {
"import": "./index.js"
},
"main": "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.
*/
export { default } from './client.js'
module.exports = require('./client')
6 changes: 3 additions & 3 deletions test/abort.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

// 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 @@
import crypto from 'node:crypto'
import ed from 'bittorrent-dht-sodium'
import ip from 'ip'
const crypto = require('crypto')
const ed = require('bittorrent-dht-sodium')
const ip = require('ip')

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

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

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

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

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

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

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

export const fill = (n, s) => {
exports.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 @@ export const fill = (n, s) => {
return b
}

export const sign = keypair => {
exports.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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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 @@
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'
const common = require('./common')
const DHT = require('../')
const ed = require('bittorrent-dht-sodium')
const test = require('tape')
const crypto = require('crypto')

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 @@
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'
const common = require('./common')
const DHT = require('../')
const test = require('tape')
const crypto = require('crypto')
const ed = require('bittorrent-dht-sodium')

// 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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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 @@
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 common = require('./common')
const DHT = require('../')
const once = require('once')
const parallel = require('run-parallel')
const test = require('tape')

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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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 @@
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../../index.js'
const test = require('tape')
const DHT = require('../../')
const ed = require('bittorrent-dht-sodium')

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 @@
import test from 'tape'
import DHT from '../../index.js'
const DHT = require('../../')
const test = require('tape')

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 @@
import test from 'tape'
import DHT from '../index.js'
const test = require('tape')
const DHT = require('../')

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 @@
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'
const common = require('./common')
const DHT = require('../')
const test = require('tape')
const ed = require('bittorrent-dht-sodium')
const bencode = require('bencode')

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 @@
import ed from 'bittorrent-dht-sodium'
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const ed = require('bittorrent-dht-sodium')
const test = require('tape')

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 @@
import test from 'tape'
import DHT from '../index.js'
import * as common from './common.js'
const common = require('./common')
const DHT = require('../')
const test = require('tape')

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

0 comments on commit 520c2de

Please sign in to comment.