feat: esm (#431)

BREAKING CHANGE: ESM only

* feat: esm

* fix: linter oops
This commit is contained in:
Cas 2022-12-05 23:06:54 +01:00 committed by GitHub
parent 4b5299b691
commit e6d3189edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 186 additions and 165 deletions

View File

@ -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: {

View File

@ -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')
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'
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 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.
@ -289,4 +291,4 @@ Client.scrape = (opts, cb) => {
return client
}
module.exports = Client
export default Client

View File

@ -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

View File

@ -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 }

View File

@ -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')
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'
const common = require('../common')
const Tracker = require('./tracker')
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)[^/]*$/
/**
@ -256,4 +257,4 @@ class HTTPTracker extends Tracker {
HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
module.exports = HTTPTracker
export default HTTPTracker

View File

@ -1,4 +1,4 @@
const EventEmitter = require('events')
import EventEmitter from 'events'
class Tracker extends EventEmitter {
constructor (client, announceUrl) {
@ -25,4 +25,4 @@ class Tracker extends EventEmitter {
}
}
module.exports = Tracker
export default Tracker

View File

@ -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')
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'
const common = require('../common')
const Tracker = require('./tracker')
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)
@ -329,4 +331,4 @@ function toUInt64 (n) {
function noop () {}
module.exports = UDPTracker
export default UDPTracker

View File

@ -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
@ -439,4 +441,4 @@ WebSocketTracker._socketPool = socketPool
function noop () {}
module.exports = WebSocketTracker
export default WebSocketTracker

View File

@ -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',
@ -31,20 +31,19 @@ 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
@ -52,7 +51,7 @@ exports.toUInt32 = toUInt32
* @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
@ -60,7 +59,7 @@ exports.querystringParse = q => querystring.parse(q, null, null, { decodeURIComp
* @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()}`)

View File

@ -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)
}
@ -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:/)) {
@ -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
}

View File

@ -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 = {}

View File

@ -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')

View File

@ -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 = {}

View File

@ -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()
@ -159,4 +161,4 @@ class Swarm {
}
}
module.exports = Swarm
export default Swarm

View File

@ -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",
@ -60,7 +61,10 @@
"wrtc": "0.4.7"
},
"engines": {
"node": ">=12"
"node": ">=12.20.0"
},
"exports": {
"import": "./index.js"
},
"keywords": [
"bittorrent",

View File

@ -1,19 +1,21 @@
const bencode = require('bencode')
const debug = require('debug')('bittorrent-tracker:server')
const dgram = require('dgram')
const EventEmitter = require('events')
const http = require('http')
const peerid = require('bittorrent-peerid')
const series = require('run-series')
const string2compact = require('string2compact')
const WebSocketServer = require('ws').Server
import bencode from 'bencode'
import Debug from 'debug'
import dgram from 'dgram'
import EventEmitter from 'events'
import http from 'http'
import peerid from 'bittorrent-peerid'
import series from 'run-series'
import string2compact from 'string2compact'
import ws from 'ws'
const common = require('./lib/common')
const Swarm = require('./lib/server/swarm')
const parseHttpRequest = require('./lib/server/parse-http')
const parseUdpRequest = require('./lib/server/parse-udp')
const parseWebSocketRequest = require('./lib/server/parse-websocket')
import common from './lib/common.js'
import Swarm from './lib/server/swarm.js'
import parseHttpRequest from './lib/server/parse-http.js'
import parseUdpRequest from './lib/server/parse-udp.js'
import parseWebSocketRequest from './lib/server/parse-websocket.js'
const { Server: WebSocketServer } = ws
const debug = Debug('bittorrent-tracker:server')
const hasOwnProperty = Object.prototype.hasOwnProperty
/**
@ -805,4 +807,4 @@ function toNumber (x) {
function noop () {}
module.exports = Server
export default Server

View File

@ -1,7 +1,7 @@
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
const peerId = Buffer.from('01234567890123456789')

View File

@ -1,8 +1,8 @@
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const magnet = require('magnet-uri')
const test = require('tape')
import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import magnet from 'magnet-uri'
import test from 'tape'
const peerId = Buffer.from('01234567890123456789')

View File

@ -1,7 +1,7 @@
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
const peerId = Buffer.from('01234567890123456789')
const port = 6681

View File

@ -1,9 +1,9 @@
const Client = require('../')
const common = require('./common')
const http = require('http')
const fixtures = require('webtorrent-fixtures')
const net = require('net')
const test = require('tape')
import Client from '../index.js'
import common from './common.js'
import http from 'http'
import fixtures from 'webtorrent-fixtures'
import net from 'net'
import test from 'tape'
const peerId1 = Buffer.from('01234567890123456789')
const peerId2 = Buffer.from('12345678901234567890')

View File

@ -1,6 +1,6 @@
const Server = require('../').Server
import { Server } from '../index.js'
exports.createServer = (t, opts, cb) => {
export const createServer = (t, opts, cb) => {
if (typeof opts === 'string') opts = { serverType: opts }
opts.http = (opts.serverType === 'http')
@ -27,7 +27,7 @@ exports.createServer = (t, opts, cb) => {
})
}
exports.mockWebsocketTracker = client => {
export const mockWebsocketTracker = client => {
client._trackers[0]._generateOffers = (numwant, cb) => {
const offers = []
for (let i = 0; i < numwant; i++) {
@ -38,3 +38,5 @@ exports.mockWebsocketTracker = client => {
})
}
}
export default { mockWebsocketTracker, createServer }

View File

@ -1,7 +1,7 @@
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
const peerId = Buffer.from('01234567890123456789')
const port = 6881

View File

@ -1,7 +1,7 @@
const Client = require('../')
const common = require('./common')
const test = require('tape')
const wrtc = require('wrtc')
import Client from '../index.js'
import common from './common.js'
import test from 'tape'
import wrtc from 'wrtc'
const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
const peerId = Buffer.from('01234567890123456789')

View File

@ -1,7 +1,7 @@
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
const peerId = Buffer.from('01234567890123456789')

View File

@ -1,5 +1,5 @@
const common = require('../lib/common')
const test = require('tape')
import common from '../lib/common.js'
import test from 'tape'
// https://github.com/webtorrent/webtorrent/issues/196
test('encode special chars +* in http tracker urls', t => {

View File

@ -1,8 +1,8 @@
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
const Server = require('../server')
import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
import Server from '../server.js'
const peerId = Buffer.from('01234567890123456789')

View File

@ -1,16 +1,15 @@
const bencode = require('bencode')
const Client = require('../')
const common = require('./common')
const commonLib = require('../lib/common')
const commonTest = require('./common')
const fixtures = require('webtorrent-fixtures')
const get = require('simple-get')
const test = require('tape')
import bencode from 'bencode'
import Client from '../index.js'
import common from './common.js'
import commonLib from '../lib/common.js'
import fixtures from 'webtorrent-fixtures'
import get from 'simple-get'
import test from 'tape'
const peerId = Buffer.from('01234567890123456789')
function testSingle (t, serverType) {
commonTest.createServer(t, serverType, (server, announceUrl) => {
common.createServer(t, serverType, (server, announceUrl) => {
const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
@ -52,7 +51,7 @@ test('ws: single info_hash scrape', t => {
})
function clientScrapeStatic (t, serverType) {
commonTest.createServer(t, serverType, (server, announceUrl) => {
common.createServer(t, serverType, (server, announceUrl) => {
const client = Client.scrape({
announce: announceUrl,
infoHash: fixtures.leaves.parsedTorrent.infoHash,
@ -116,7 +115,7 @@ function clientScrapeMulti (t, serverType) {
const infoHash1 = fixtures.leaves.parsedTorrent.infoHash
const infoHash2 = fixtures.alice.parsedTorrent.infoHash
commonTest.createServer(t, serverType, (server, announceUrl) => {
common.createServer(t, serverType, (server, announceUrl) => {
Client.scrape({
infoHash: [infoHash1, infoHash2],
announce: announceUrl
@ -156,7 +155,7 @@ test('server: multiple info_hash scrape (manual http request)', t => {
const binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash)
const binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash)
commonTest.createServer(t, 'http', (server, announceUrl) => {
common.createServer(t, 'http', (server, announceUrl) => {
const scrapeUrl = announceUrl.replace('/announce', '/scrape')
const url = `${scrapeUrl}?${commonLib.querystringStringify({
@ -192,7 +191,7 @@ test('server: all info_hash scrape (manual http request)', t => {
const binaryInfoHash = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash)
commonTest.createServer(t, 'http', (server, announceUrl) => {
common.createServer(t, 'http', (server, announceUrl) => {
const scrapeUrl = announceUrl.replace('/announce', '/scrape')
// announce a torrent to the tracker

View File

@ -1,7 +1,7 @@
const Client = require('../')
const common = require('./common')
const test = require('tape')
const wrtc = require('wrtc')
import Client from '../index.js'
import common from './common.js'
import test from 'tape'
import wrtc from 'wrtc'
const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
const peerId = Buffer.from('01234567890123456789')

View File

@ -1,8 +1,8 @@
const Client = require('../')
const commonTest = require('./common')
const fixtures = require('webtorrent-fixtures')
const get = require('simple-get')
const test = require('tape')
import Client from '../index.js'
import commonTest from './common.js'
import fixtures from 'webtorrent-fixtures'
import get from 'simple-get'
import test from 'tape'
const peerId = Buffer.from('-WW0091-4ea5886ce160')
const unknownPeerId = Buffer.from('01234567890123456789')