Skip to content

Commit

Permalink
feat: esm (#431)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ESM only

* feat: esm

* fix: linter oops
  • Loading branch information
ThaUnknown authored Dec 5, 2022
1 parent 4b5299b commit e6d3189
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 171 deletions.
4 changes: 2 additions & 2 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const minimist = require('minimist')
const Server = require('../').Server
import minimist from 'minimist'
import { Server } from '../index.js'

const argv = minimist(process.argv.slice(2), {
alias: {
Expand Down
26 changes: 14 additions & 12 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const debug = require('debug')('bittorrent-tracker:client')
const EventEmitter = require('events')
const once = require('once')
const parallel = require('run-parallel')
const Peer = require('simple-peer')
const queueMicrotask = require('queue-microtask')

const common = require('./lib/common')
const HTTPTracker = require('./lib/client/http-tracker') // empty object in browser
const UDPTracker = require('./lib/client/udp-tracker') // empty object in browser
const WebSocketTracker = require('./lib/client/websocket-tracker')
import Debug from 'debug'
import EventEmitter from 'events'
import once from 'once'
import parallel from 'run-parallel'
import Peer from 'simple-peer'
import queueMicrotask from 'queue-microtask'

import common from './lib/common.js'
import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser
import UDPTracker from './lib/client/udp-tracker.js' // empty object in browser
import WebSocketTracker from './lib/client/websocket-tracker.js'

const debug = Debug('bittorrent-tracker:client')

/**
* BitTorrent tracker client.
Expand Down Expand Up @@ -289,4 +291,4 @@ Client.scrape = (opts, cb) => {
return client
}

module.exports = Client
export default Client
4 changes: 2 additions & 2 deletions examples/express-embed/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const Server = require('../..').Server
const express = require('express')
import { Server } from '../../index.js'
import express from 'express'
const app = express()

// https://wiki.theory.org/BitTorrentSpecification#peer_id
Expand Down
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*! bittorrent-tracker. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
const Client = require('./client')
const Server = require('./server')
import Client from './client.js'
import Server from './server.js'

module.exports = Client
module.exports.Client = Client
module.exports.Server = Server
export default Client
export { Client, Server }
25 changes: 13 additions & 12 deletions lib/client/http-tracker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const arrayRemove = require('unordered-array-remove')
const bencode = require('bencode')
const clone = require('clone')
const compact2string = require('compact2string')
const debug = require('debug')('bittorrent-tracker:http-tracker')
const get = require('simple-get')
const Socks = require('socks')

const common = require('../common')
const Tracker = require('./tracker')

import arrayRemove from 'unordered-array-remove'
import bencode from 'bencode'
import clone from 'clone'
import Debug from 'debug'
import get from 'simple-get'
import Socks from 'socks'

import common from '../common.js'
import Tracker from './tracker.js'
import compact2string from 'compact2string'

const debug = Debug('bittorrent-tracker:http-tracker')
const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/

/**
Expand Down Expand Up @@ -256,4 +257,4 @@ class HTTPTracker extends Tracker {

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

module.exports = HTTPTracker
export default HTTPTracker
4 changes: 2 additions & 2 deletions lib/client/tracker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const EventEmitter = require('events')
import EventEmitter from 'events'

class Tracker extends EventEmitter {
constructor (client, announceUrl) {
Expand All @@ -25,4 +25,4 @@ class Tracker extends EventEmitter {
}
}

module.exports = Tracker
export default Tracker
26 changes: 14 additions & 12 deletions lib/client/udp-tracker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const arrayRemove = require('unordered-array-remove')
const BN = require('bn.js')
const clone = require('clone')
const compact2string = require('compact2string')
const debug = require('debug')('bittorrent-tracker:udp-tracker')
const dgram = require('dgram')
const randombytes = require('randombytes')
const Socks = require('socks')

const common = require('../common')
const Tracker = require('./tracker')
import arrayRemove from 'unordered-array-remove'
import BN from 'bn.js'
import clone from 'clone'
import Debug from 'debug'
import dgram from 'dgram'
import randombytes from 'randombytes'
import Socks from 'socks'

import common from '../common.js'
import Tracker from './tracker.js'
import compact2string from 'compact2string'

const debug = Debug('bittorrent-tracker:udp-tracker')

/**
* UDP torrent tracker client (for an individual tracker)
Expand Down Expand Up @@ -329,4 +331,4 @@ function toUInt64 (n) {

function noop () {}

module.exports = UDPTracker
export default UDPTracker
20 changes: 11 additions & 9 deletions lib/client/websocket-tracker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const clone = require('clone')
const debug = require('debug')('bittorrent-tracker:websocket-tracker')
const Peer = require('simple-peer')
const randombytes = require('randombytes')
const Socket = require('simple-websocket')
const Socks = require('socks')
import clone from 'clone'
import Debug from 'debug'
import Peer from 'simple-peer'
import randombytes from 'randombytes'
import Socket from 'simple-websocket'
import Socks from 'socks'

const common = require('../common')
const Tracker = require('./tracker')
import common from '../common.js'
import Tracker from './tracker.js'

const debug = Debug('bittorrent-tracker:websocket-tracker')

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

function noop () {}

module.exports = WebSocketTracker
export default WebSocketTracker
29 changes: 14 additions & 15 deletions lib/common-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
* These are separate from common.js so they can be skipped when bundling for the browser.
*/

const querystring = require('querystring')
import querystring from 'querystring'

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

exports.CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
exports.EVENT_IDS = {
export const CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
export const ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
export const EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
export const EVENT_IDS = {
0: 'update',
1: 'completed',
2: 'started',
3: 'stopped',
4: 'paused'
}
exports.EVENT_NAMES = {
export const EVENT_NAMES = {
update: 'update',
completed: 'complete',
started: 'start',
Expand All @@ -31,36 +31,35 @@ exports.EVENT_NAMES = {
* Client request timeout. How long to wait before considering a request to a
* tracker server to have timed out.
*/
exports.REQUEST_TIMEOUT = 15000
export const REQUEST_TIMEOUT = 15000

/**
* Client destroy timeout. How long to wait before forcibly cleaning up all
* pending requests, open sockets, etc.
*/
exports.DESTROY_TIMEOUT = 1000
export const DESTROY_TIMEOUT = 1000

function toUInt32 (n) {
export function toUInt32 (n) {
const buf = Buffer.allocUnsafe(4)
buf.writeUInt32BE(n, 0)
return buf
}
exports.toUInt32 = toUInt32

/**
* `querystring.parse` using `unescape` instead of decodeURIComponent, since bittorrent
* clients send non-UTF8 querystrings
* @param {string} q
* @return {Object}
*/
exports.querystringParse = q => querystring.parse(q, null, null, { decodeURIComponent: unescape })
export const querystringParse = q => querystring.parse(q, null, null, { decodeURIComponent: unescape })

/**
* `querystring.stringify` using `escape` instead of encodeURIComponent, since bittorrent
* clients send non-UTF8 querystrings
* @param {Object} obj
* @return {string}
*/
exports.querystringStringify = obj => {
export const querystringStringify = obj => {
let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
ret = ret.replace(/[@*/+]/g, char => // `escape` doesn't encode the characters @*/+ so we do it manually
`%${char.charCodeAt(0).toString(16).toUpperCase()}`)
Expand Down
21 changes: 14 additions & 7 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* Functions/constants needed by both the client and server.
*/
import * as common from './common-node.js'

exports.DEFAULT_ANNOUNCE_PEERS = 50
exports.MAX_ANNOUNCE_PEERS = 82
export const DEFAULT_ANNOUNCE_PEERS = 50
export const MAX_ANNOUNCE_PEERS = 82

exports.binaryToHex = str => {
export const binaryToHex = str => {
if (typeof str !== 'string') {
str = String(str)
}
return Buffer.from(str, 'binary').toString('hex')
}

exports.hexToBinary = str => {
export const hexToBinary = str => {
if (typeof str !== 'string') {
str = String(str)
}
Expand All @@ -31,7 +32,7 @@ exports.hexToBinary = str => {
// Bug reports:
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
exports.parseUrl = str => {
export const parseUrl = str => {
const url = new URL(str.replace(/^udp:/, 'http:'))

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

const config = require('./common-node')
Object.assign(exports, config)
export default {
DEFAULT_ANNOUNCE_PEERS,
MAX_ANNOUNCE_PEERS,
binaryToHex,
hexToBinary,
parseUrl,
...common
}
4 changes: 2 additions & 2 deletions lib/server/parse-http.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = parseHttpRequest
import common from '../common.js'

const common = require('../common')
export default parseHttpRequest

function parseHttpRequest (req, opts) {
if (!opts) opts = {}
Expand Down
6 changes: 3 additions & 3 deletions lib/server/parse-udp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = parseUdpRequest
import ipLib from 'ip'
import common from '../common.js'

const ipLib = require('ip')
const common = require('../common')
export default parseUdpRequest

function parseUdpRequest (msg, rinfo) {
if (msg.length < 16) throw new Error('received packet is too short')
Expand Down
4 changes: 2 additions & 2 deletions lib/server/parse-websocket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = parseWebSocketRequest
import common from '../common.js'

const common = require('../common')
export default parseWebSocketRequest

function parseWebSocketRequest (socket, opts, params) {
if (!opts) opts = {}
Expand Down
12 changes: 7 additions & 5 deletions lib/server/swarm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const arrayRemove = require('unordered-array-remove')
const debug = require('debug')('bittorrent-tracker:swarm')
const LRU = require('lru')
const randomIterate = require('random-iterate')
import arrayRemove from 'unordered-array-remove'
import Debug from 'debug'
import LRU from 'lru'
import randomIterate from 'random-iterate'

const debug = Debug('bittorrent-tracker:swarm')

// Regard this as the default implementation of an interface that you
// need to support when overriding Server.createSwarm() and Server.getSwarm()
Expand Down Expand Up @@ -159,4 +161,4 @@ class Swarm {
}
}

module.exports = Swarm
export default Swarm
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"bugs": {
"url": "https://github.com/webtorrent/bittorrent-tracker/issues"
},
"type": "module",
"dependencies": {
"bencode": "^2.0.1",
"bittorrent-peerid": "^1.3.3",
Expand Down Expand Up @@ -60,7 +61,10 @@
"wrtc": "0.4.7"
},
"engines": {
"node": ">=12"
"node": ">=12.20.0"
},
"exports": {
"import": "./index.js"
},
"keywords": [
"bittorrent",
Expand Down
Loading

0 comments on commit e6d3189

Please sign in to comment.