Skip to content

Commit

Permalink
refactor: switch to ESM (#90)
Browse files Browse the repository at this point in the history
BREAKING CHANGES: ESM only

* chore: update imports and export index.js

esm import/export syntax
Signed-off-by: Lakshya Singh <[email protected]>

* chore: update imports in tests

esm import syntax with path
Signed-off-by: Lakshya Singh <[email protected]>

* chore: bump bitfield for esm

4.1.0 is esm based while 4.0.0 was commonjs
Signed-off-by: Lakshya Singh <[email protected]>

* chore: update package.json for esm

specify minimum nodejs version for esm support
exports defined
type change to module
Signed-off-by: Lakshya Singh <[email protected]>

* chore: update readme with esm syntax

Signed-off-by: Lakshya Singh <[email protected]>
  • Loading branch information
king-11 committed Apr 28, 2022
1 parent 6d180d5 commit fce2548
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 30 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ duplex streams | a.pipe(b).pipe(a)
(Images from the ["harnessing streams"](https://github.com/substack/lxjs-stream-examples/blob/master/slides.markdown) talk by substack.)

```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 Down Expand Up @@ -305,9 +305,9 @@ Here is an example of the **ut_metadata** extension being used with
**bittorrent-protocol**:

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

net.createServer(socket => {
const wire = new Protocol()
Expand Down
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*! bittorrent-protocol. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
const arrayRemove = require('unordered-array-remove')
const bencode = require('bencode')
const BitField = require('bitfield').default
const crypto = require('crypto')
const debug = require('debug')('bittorrent-protocol')
const randombytes = require('randombytes')
const sha1 = require('simple-sha1')
const speedometer = require('speedometer')
const stream = require('readable-stream')
const RC4 = require('rc4')
import bencode from 'bencode'
import BitField from 'bitfield'
import crypto from 'crypto'
import Debug from 'debug'
import randombytes from 'randombytes'
import RC4 from 'rc4'
import stream from 'readable-stream'
import sha1 from 'simple-sha1'
import speedometer from 'speedometer'
import arrayRemove from 'unordered-array-remove'

const debug = Debug('bittorrent-protocol')

const BITFIELD_GROW = 400000
const KEEP_ALIVE_TIMEOUT = 55000
Expand Down Expand Up @@ -1353,4 +1355,4 @@ class Wire extends stream.Duplex {
}
}

module.exports = Wire
export default Wire
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "bittorrent-protocol",
"type": "module",
"description": "Simple, robust, BitTorrent peer wire protocol implementation",
"version": "3.5.3",
"author": {
Expand All @@ -12,7 +13,7 @@
},
"dependencies": {
"bencode": "^2.0.2",
"bitfield": "^4.0.0",
"bitfield": "^4.1.0",
"debug": "^4.3.4",
"randombytes": "^2.1.0",
"rc4": "^0.1.5",
Expand All @@ -27,6 +28,12 @@
"standard": "*",
"tape": "5.5.3"
},
"engines": {
"node": ">=12.20.0"
},
"exports": {
"import": "./index.js"
},
"keywords": [
"bittorrent",
"p2p",
Expand All @@ -38,7 +45,6 @@
"wire"
],
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "git://github.com/webtorrent/bittorrent-protocol.git"
Expand Down
4 changes: 2 additions & 2 deletions test/extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Protocol = require('../')
const test = require('tape')
import test from 'tape'
import Protocol from '../index.js'

test('Extension.prototype.name', t => {
t.plan(2)
Expand Down
4 changes: 2 additions & 2 deletions test/no-timeout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Protocol = require('../')
const test = require('tape')
import test from 'tape'
import Protocol from '../index.js'

test('No timeout when peer is good', t => {
t.plan(3)
Expand Down
4 changes: 2 additions & 2 deletions test/protocol.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Protocol = require('../')
const test = require('tape')
import test from 'tape'
import Protocol from '../index.js'

test('Handshake', t => {
t.plan(4)
Expand Down
4 changes: 2 additions & 2 deletions test/state-change-on-end.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Protocol = require('../')
const test = require('tape')
import test from 'tape'
import Protocol from '../index.js'

test('State changes correctly on wire \'end\'', t => {
t.plan(11)
Expand Down
4 changes: 2 additions & 2 deletions test/timeout-destroy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Protocol = require('../')
const test = require('tape')
import test from 'tape'
import Protocol from '../index.js'

test('Timeout and destroy when peer does not respond', t => {
t.plan(4)
Expand Down
4 changes: 2 additions & 2 deletions test/timeout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Protocol = require('../')
const test = require('tape')
import test from 'tape'
import Protocol from '../index.js'

test('Timeout when peer does not respond', t => {
t.plan(9)
Expand Down

0 comments on commit fce2548

Please sign in to comment.