Skip to content

Commit b111818

Browse files
authored
feat: esm (#131)
BREAKING CHANGE: ESM only
1 parent 5094a03 commit b111818

19 files changed

Lines changed: 80 additions & 69 deletions

benchmark/bencode.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const bench = require('nanobench')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import bench from 'nanobench'
44

5-
const bencode = require('../')
5+
import bencode from '../'
66

77
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
88
const object = bencode.decode(buffer)

benchmark/buffer-vs-string.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const bencode = require('../')
4-
const bench = require('nanobench')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import bencode from '../'
4+
import bench from 'nanobench'
55

66
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
77
const str = buffer.toString('ascii')

benchmark/compare-decode.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const bench = require('nanobench')
4-
5-
const bencode = require('../')
6-
const bencoding = require('bencoding')
7-
const bncode = require('bncode')
8-
const btparse = require('btparse')
9-
const dht = require('dht.js/lib/dht/bencode')
10-
const dhtBencode = require('dht-bencode')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import bench from 'nanobench'
4+
5+
import bencode from '../'
6+
import bencoding from 'bencoding'
7+
import bncode from 'bncode'
8+
import btparse from 'btparse'
9+
import dht from 'dht.js/lib/dht/bencode'
10+
import dhtBencode from 'dht-bencode'
1111

1212
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
1313

benchmark/compare-encode.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const bench = require('nanobench')
4-
5-
const bencode = require('../')
6-
const bencoding = require('bencoding')
7-
const bncode = require('bncode')
8-
const dht = require('dht.js/lib/dht/bencode')
9-
const dhtBencode = require('dht-bencode')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import bench from 'nanobench'
4+
5+
import bencode from '../'
6+
import bencoding from 'bencoding'
7+
import bncode from 'bncode'
8+
import dht from 'dht.js/lib/dht/bencode'
9+
import dhtBencode from 'dht-bencode'
1010

1111
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
1212
const object = bencode.decode(buffer)

benchmark/encoding-length.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const bencode = require('..')
4-
const bench = require('nanobench')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import bencode from '..'
4+
import bench from 'nanobench'
55

66
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
77
const torrent = bencode.decode(buffer)

lib/decode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,4 @@ decode.buffer = function () {
165165
: decode.data.slice(sep, end)
166166
}
167167

168-
module.exports = decode
168+
export default decode

lib/encode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getType } = require('./util.js')
1+
import { getType } from './util.js'
22

33
/**
44
* Encodes data in bencode.
@@ -131,4 +131,4 @@ encode.listSet = function (buffers, data) {
131131
buffers.push(buffE)
132132
}
133133

134-
module.exports = encode
134+
export default encode

lib/encoding-length.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { digitCount, getType } = require('./util.js')
1+
import { digitCount, getType } from './util.js'
22

33
function listLength (list) {
44
let length = 1 + 1 // type marker + end-of-type marker
@@ -66,4 +66,4 @@ function encodingLength (value) {
6666
}
6767
}
6868

69-
module.exports = encodingLength
69+
export default encodingLength

lib/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const bencode = module.exports
2-
3-
bencode.encode = require('./encode.js')
4-
bencode.decode = require('./decode.js')
5-
1+
import encode from './encode.js'
2+
import decode from './decode.js'
3+
import byteLength from './encoding-length.js'
64
/**
75
* Determines the amount of bytes
86
* needed to encode the given value
97
* @param {Object|Array|Buffer|String|Number|Boolean} value
108
* @return {Number} byteCount
119
*/
12-
bencode.byteLength = bencode.encodingLength = require('./encoding-length.js')
10+
const encodingLength = byteLength
11+
export default { encode, decode, byteLength, encodingLength }

lib/util.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const util = module.exports
2-
3-
util.digitCount = function digitCount (value) {
1+
export function digitCount (value) {
42
// Add a digit for negative numbers, as the sign will be prefixed
53
const sign = value < 0 ? 1 : 0
64
// Guard against negative numbers & zero going into log10(),
@@ -9,7 +7,7 @@ util.digitCount = function digitCount (value) {
97
return Math.floor(Math.log10(value)) + 1 + sign
108
}
119

12-
util.getType = function getType (value) {
10+
export function getType (value) {
1311
if (Buffer.isBuffer(value)) return 'buffer'
1412
if (ArrayBuffer.isView(value)) return 'arraybufferview'
1513
if (Array.isArray(value)) return 'array'

0 commit comments

Comments
 (0)