Skip to content

Commit e6d3189

Browse files
authored
feat: esm (#431)
BREAKING CHANGE: ESM only * feat: esm * fix: linter oops
1 parent 4b5299b commit e6d3189

29 files changed

+192
-171
lines changed

bin/cmd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

3-
const minimist = require('minimist')
4-
const Server = require('../').Server
3+
import minimist from 'minimist'
4+
import { Server } from '../index.js'
55

66
const argv = minimist(process.argv.slice(2), {
77
alias: {

client.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const debug = require('debug')('bittorrent-tracker:client')
2-
const EventEmitter = require('events')
3-
const once = require('once')
4-
const parallel = require('run-parallel')
5-
const Peer = require('simple-peer')
6-
const queueMicrotask = require('queue-microtask')
7-
8-
const common = require('./lib/common')
9-
const HTTPTracker = require('./lib/client/http-tracker') // empty object in browser
10-
const UDPTracker = require('./lib/client/udp-tracker') // empty object in browser
11-
const WebSocketTracker = require('./lib/client/websocket-tracker')
1+
import Debug from 'debug'
2+
import EventEmitter from 'events'
3+
import once from 'once'
4+
import parallel from 'run-parallel'
5+
import Peer from 'simple-peer'
6+
import queueMicrotask from 'queue-microtask'
7+
8+
import common from './lib/common.js'
9+
import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser
10+
import UDPTracker from './lib/client/udp-tracker.js' // empty object in browser
11+
import WebSocketTracker from './lib/client/websocket-tracker.js'
12+
13+
const debug = Debug('bittorrent-tracker:client')
1214

1315
/**
1416
* BitTorrent tracker client.
@@ -289,4 +291,4 @@ Client.scrape = (opts, cb) => {
289291
return client
290292
}
291293

292-
module.exports = Client
294+
export default Client

examples/express-embed/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

3-
const Server = require('../..').Server
4-
const express = require('express')
3+
import { Server } from '../../index.js'
4+
import express from 'express'
55
const app = express()
66

77
// https://wiki.theory.org/BitTorrentSpecification#peer_id

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*! bittorrent-tracker. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
2-
const Client = require('./client')
3-
const Server = require('./server')
2+
import Client from './client.js'
3+
import Server from './server.js'
44

5-
module.exports = Client
6-
module.exports.Client = Client
7-
module.exports.Server = Server
5+
export default Client
6+
export { Client, Server }

lib/client/http-tracker.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
const arrayRemove = require('unordered-array-remove')
2-
const bencode = require('bencode')
3-
const clone = require('clone')
4-
const compact2string = require('compact2string')
5-
const debug = require('debug')('bittorrent-tracker:http-tracker')
6-
const get = require('simple-get')
7-
const Socks = require('socks')
8-
9-
const common = require('../common')
10-
const Tracker = require('./tracker')
11-
1+
import arrayRemove from 'unordered-array-remove'
2+
import bencode from 'bencode'
3+
import clone from 'clone'
4+
import Debug from 'debug'
5+
import get from 'simple-get'
6+
import Socks from 'socks'
7+
8+
import common from '../common.js'
9+
import Tracker from './tracker.js'
10+
import compact2string from 'compact2string'
11+
12+
const debug = Debug('bittorrent-tracker:http-tracker')
1213
const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/
1314

1415
/**
@@ -256,4 +257,4 @@ class HTTPTracker extends Tracker {
256257

257258
HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
258259

259-
module.exports = HTTPTracker
260+
export default HTTPTracker

lib/client/tracker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const EventEmitter = require('events')
1+
import EventEmitter from 'events'
22

33
class Tracker extends EventEmitter {
44
constructor (client, announceUrl) {
@@ -25,4 +25,4 @@ class Tracker extends EventEmitter {
2525
}
2626
}
2727

28-
module.exports = Tracker
28+
export default Tracker

lib/client/udp-tracker.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const arrayRemove = require('unordered-array-remove')
2-
const BN = require('bn.js')
3-
const clone = require('clone')
4-
const compact2string = require('compact2string')
5-
const debug = require('debug')('bittorrent-tracker:udp-tracker')
6-
const dgram = require('dgram')
7-
const randombytes = require('randombytes')
8-
const Socks = require('socks')
9-
10-
const common = require('../common')
11-
const Tracker = require('./tracker')
1+
import arrayRemove from 'unordered-array-remove'
2+
import BN from 'bn.js'
3+
import clone from 'clone'
4+
import Debug from 'debug'
5+
import dgram from 'dgram'
6+
import randombytes from 'randombytes'
7+
import Socks from 'socks'
8+
9+
import common from '../common.js'
10+
import Tracker from './tracker.js'
11+
import compact2string from 'compact2string'
12+
13+
const debug = Debug('bittorrent-tracker:udp-tracker')
1214

1315
/**
1416
* UDP torrent tracker client (for an individual tracker)
@@ -329,4 +331,4 @@ function toUInt64 (n) {
329331

330332
function noop () {}
331333

332-
module.exports = UDPTracker
334+
export default UDPTracker

lib/client/websocket-tracker.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const clone = require('clone')
2-
const debug = require('debug')('bittorrent-tracker:websocket-tracker')
3-
const Peer = require('simple-peer')
4-
const randombytes = require('randombytes')
5-
const Socket = require('simple-websocket')
6-
const Socks = require('socks')
1+
import clone from 'clone'
2+
import Debug from 'debug'
3+
import Peer from 'simple-peer'
4+
import randombytes from 'randombytes'
5+
import Socket from 'simple-websocket'
6+
import Socks from 'socks'
77

8-
const common = require('../common')
9-
const Tracker = require('./tracker')
8+
import common from '../common.js'
9+
import Tracker from './tracker.js'
10+
11+
const debug = Debug('bittorrent-tracker:websocket-tracker')
1012

1113
// Use a socket pool, so tracker clients share WebSocket objects for the same server.
1214
// In practice, WebSockets are pretty slow to establish, so this gives a nice performance
@@ -439,4 +441,4 @@ WebSocketTracker._socketPool = socketPool
439441

440442
function noop () {}
441443

442-
module.exports = WebSocketTracker
444+
export default WebSocketTracker

lib/common-node.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
* These are separate from common.js so they can be skipped when bundling for the browser.
44
*/
55

6-
const querystring = require('querystring')
6+
import querystring from 'querystring'
77

8-
exports.IPV4_RE = /^[\d.]+$/
9-
exports.IPV6_RE = /^[\da-fA-F:]+$/
10-
exports.REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/
8+
export const IPV4_RE = /^[\d.]+$/
9+
export const IPV6_RE = /^[\da-fA-F:]+$/
10+
export const REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/
1111

12-
exports.CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
13-
exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
14-
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
15-
exports.EVENT_IDS = {
12+
export const CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
13+
export const ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
14+
export const EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
15+
export const EVENT_IDS = {
1616
0: 'update',
1717
1: 'completed',
1818
2: 'started',
1919
3: 'stopped',
2020
4: 'paused'
2121
}
22-
exports.EVENT_NAMES = {
22+
export const EVENT_NAMES = {
2323
update: 'update',
2424
completed: 'complete',
2525
started: 'start',
@@ -31,36 +31,35 @@ exports.EVENT_NAMES = {
3131
* Client request timeout. How long to wait before considering a request to a
3232
* tracker server to have timed out.
3333
*/
34-
exports.REQUEST_TIMEOUT = 15000
34+
export const REQUEST_TIMEOUT = 15000
3535

3636
/**
3737
* Client destroy timeout. How long to wait before forcibly cleaning up all
3838
* pending requests, open sockets, etc.
3939
*/
40-
exports.DESTROY_TIMEOUT = 1000
40+
export const DESTROY_TIMEOUT = 1000
4141

42-
function toUInt32 (n) {
42+
export function toUInt32 (n) {
4343
const buf = Buffer.allocUnsafe(4)
4444
buf.writeUInt32BE(n, 0)
4545
return buf
4646
}
47-
exports.toUInt32 = toUInt32
4847

4948
/**
5049
* `querystring.parse` using `unescape` instead of decodeURIComponent, since bittorrent
5150
* clients send non-UTF8 querystrings
5251
* @param {string} q
5352
* @return {Object}
5453
*/
55-
exports.querystringParse = q => querystring.parse(q, null, null, { decodeURIComponent: unescape })
54+
export const querystringParse = q => querystring.parse(q, null, null, { decodeURIComponent: unescape })
5655

5756
/**
5857
* `querystring.stringify` using `escape` instead of encodeURIComponent, since bittorrent
5958
* clients send non-UTF8 querystrings
6059
* @param {Object} obj
6160
* @return {string}
6261
*/
63-
exports.querystringStringify = obj => {
62+
export const querystringStringify = obj => {
6463
let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
6564
ret = ret.replace(/[@*/+]/g, char => // `escape` doesn't encode the characters @*/+ so we do it manually
6665
`%${char.charCodeAt(0).toString(16).toUpperCase()}`)

lib/common.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/**
22
* Functions/constants needed by both the client and server.
33
*/
4+
import * as common from './common-node.js'
45

5-
exports.DEFAULT_ANNOUNCE_PEERS = 50
6-
exports.MAX_ANNOUNCE_PEERS = 82
6+
export const DEFAULT_ANNOUNCE_PEERS = 50
7+
export const MAX_ANNOUNCE_PEERS = 82
78

8-
exports.binaryToHex = str => {
9+
export const binaryToHex = str => {
910
if (typeof str !== 'string') {
1011
str = String(str)
1112
}
1213
return Buffer.from(str, 'binary').toString('hex')
1314
}
1415

15-
exports.hexToBinary = str => {
16+
export const hexToBinary = str => {
1617
if (typeof str !== 'string') {
1718
str = String(str)
1819
}
@@ -31,7 +32,7 @@ exports.hexToBinary = str => {
3132
// Bug reports:
3233
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
3334
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
34-
exports.parseUrl = str => {
35+
export const parseUrl = str => {
3536
const url = new URL(str.replace(/^udp:/, 'http:'))
3637

3738
if (str.match(/^udp:/)) {
@@ -45,5 +46,11 @@ exports.parseUrl = str => {
4546
return url
4647
}
4748

48-
const config = require('./common-node')
49-
Object.assign(exports, config)
49+
export default {
50+
DEFAULT_ANNOUNCE_PEERS,
51+
MAX_ANNOUNCE_PEERS,
52+
binaryToHex,
53+
hexToBinary,
54+
parseUrl,
55+
...common
56+
}

0 commit comments

Comments
 (0)