diff --git a/README.md b/README.md index 07d25a9..ff18ba3 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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() diff --git a/index.js b/index.js index bf801fb..742608e 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ /*! ut_pex. MIT License. WebTorrent LLC */ -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 @@ -17,7 +17,7 @@ const FLAGS = { isReachable: 0x10 } -module.exports = () => { +export default () => { class utPex extends EventEmitter { constructor (wire) { super() @@ -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 } } /** @@ -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)) } }) @@ -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)) } }) @@ -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 }) } } diff --git a/package.json b/package.json index 49f6f56..aa9c033 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "bugs": { "url": "https://github.com/webtorrent/ut_pex/issues" }, + "type": "module", "dependencies": { "bencode": "^2.0.2", "compact2string": "^1.4.1", @@ -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" diff --git a/test/basic.js b/test/basic.js index d092309..ef5407b 100644 --- a/test/basic.js +++ b/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() @@ -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()