Skip to content

Commit

Permalink
feat: esm (#57)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ESM only
  • Loading branch information
ThaUnknown committed Nov 16, 2022
1 parent 11149a2 commit bbdeae1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -32,8 +32,8 @@ This package should be used with [bittorrent-protocol](https://github.com/feross
Say you're already using `bittorrent-protocol`. Your code might look something like this:

```js
const Protocol = require('bittorrent-protocol')
const net = require('net')
import Protocol from 'bittorrent-protocol'
import net from 'net'

net.createServer(socket => {
const wire = new Protocol()
Expand All @@ -50,9 +50,9 @@ net.createServer(socket => {
To add support for PEX, simply modify your code like this:

```js
const Protocol = require('bittorrent-protocol')
const net = require('net')
const ut_pex = require('ut_pex')
import Protocol from 'bittorrent-protocol'
import net from 'net'
import ut_pex from 'ut_pex'

net.createServer(socket => {
const wire = new Protocol()
Expand Down
24 changes: 12 additions & 12 deletions index.js
@@ -1,9 +1,9 @@
/*! ut_pex. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */

const EventEmitter = require('events').EventEmitter
const compact2string = require('compact2string')
const string2compact = require('string2compact')
const bencode = require('bencode')
import { EventEmitter } from 'events'
import compact2string from 'compact2string'
import string2compact from 'string2compact'
import bencode from 'bencode'

const PEX_INTERVAL = 65000 // just over one minute
const PEX_MAX_PEERS = 50 // max number of peers to advertise per PEX message
Expand All @@ -17,7 +17,7 @@ const FLAGS = {
isReachable: 0x10
}

module.exports = () => {
export default () => {
class utPex extends EventEmitter {
constructor (wire) {
super()
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = () => {
if (!peer.includes(':')) return // disregard invalid peers
if (peer in this._remoteAddedPeers) return // never advertise peer the remote wire already sent us
if (peer in this._localDroppedPeers) delete this._localDroppedPeers[peer]
this._localAddedPeers[peer] = { ip: version, flags: flags }
this._localAddedPeers[peer] = { ip: version, flags }
}

/**
Expand Down Expand Up @@ -138,7 +138,7 @@ module.exports = () => {
delete this._remoteDroppedPeers[peer]
if (!(peer in this._remoteAddedPeers)) {
const flags = message['added.f'][idx]
this._remoteAddedPeers[peer] = { ip: 4, flags: flags }
this._remoteAddedPeers[peer] = { ip: 4, flags }
this.emit('peer', peer, this._decodeFlags(flags))
}
})
Expand All @@ -149,7 +149,7 @@ module.exports = () => {
delete this._remoteDroppedPeers[peer]
if (!(peer in this._remoteAddedPeers)) {
const flags = message['added6.f'][idx]
this._remoteAddedPeers[peer] = { ip: 6, flags: flags }
this._remoteAddedPeers[peer] = { ip: 6, flags }
this.emit('peer', peer, this._decodeFlags(flags))
}
})
Expand Down Expand Up @@ -247,12 +247,12 @@ module.exports = () => {

// send PEX message
this._wire.extended('ut_pex', {
added: added,
added,
'added.f': addedFlags,
dropped: dropped,
added6: added6,
dropped,
added6,
'added6.f': added6Flags,
dropped6: dropped6
dropped6
})
}
}
Expand Down
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -10,6 +10,7 @@
"bugs": {
"url": "https://github.com/webtorrent/ut_pex/issues"
},
"type": "module",
"dependencies": {
"bencode": "^2.0.2",
"compact2string": "^1.4.1",
Expand All @@ -34,7 +35,12 @@
"ut_pex"
],
"license": "MIT",
"main": "index.js",
"engines": {
"node": ">=12.20.0"
},
"exports": {
"import": "./index.js"
},
"repository": {
"type": "git",
"url": "git://github.com/webtorrent/ut_pex.git"
Expand Down
12 changes: 6 additions & 6 deletions test/basic.js
@@ -1,10 +1,10 @@
/* jshint camelcase: false */

const Protocol = require('bittorrent-protocol')
const utPex = require('../')
const test = require('tape')
const string2compact = require('string2compact')
const bencode = require('bencode')
import Protocol from 'bittorrent-protocol'
import utPex from '../index.js'
import test from 'tape'
import string2compact from 'string2compact'
import bencode from 'bencode'

test('wire.use(ut_pex())', (t) => {
const wire = new Protocol()
Expand Down Expand Up @@ -258,7 +258,7 @@ test('hould ignore when onMessage added and address already in remoteAddedPeers'

const peer = '127.0.0.1:6889'
const flags = 0x06
pex._remoteAddedPeers[peer] = { ip: 4, flags: flags }
pex._remoteAddedPeers[peer] = { ip: 4, flags }

pex.on('peer', () => {
t.fail()
Expand Down

0 comments on commit bbdeae1

Please sign in to comment.