diff --git a/benchmark/bencode.js b/benchmark/bencode.js index becfba2..6cebc2f 100644 --- a/benchmark/bencode.js +++ b/benchmark/bencode.js @@ -1,8 +1,8 @@ -const fs = require('fs') -const path = require('path') -const bench = require('nanobench') +import fs from 'fs' +import path from 'path' +import bench from 'nanobench' -const bencode = require('../') +import bencode from '../' const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent')) const object = bencode.decode(buffer) diff --git a/benchmark/buffer-vs-string.js b/benchmark/buffer-vs-string.js index f459819..4b3ed49 100644 --- a/benchmark/buffer-vs-string.js +++ b/benchmark/buffer-vs-string.js @@ -1,7 +1,7 @@ -const fs = require('fs') -const path = require('path') -const bencode = require('../') -const bench = require('nanobench') +import fs from 'fs' +import path from 'path' +import bencode from '../' +import bench from 'nanobench' const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent')) const str = buffer.toString('ascii') diff --git a/benchmark/compare-decode.js b/benchmark/compare-decode.js index fb54b19..4890647 100644 --- a/benchmark/compare-decode.js +++ b/benchmark/compare-decode.js @@ -1,13 +1,13 @@ -const fs = require('fs') -const path = require('path') -const bench = require('nanobench') - -const bencode = require('../') -const bencoding = require('bencoding') -const bncode = require('bncode') -const btparse = require('btparse') -const dht = require('dht.js/lib/dht/bencode') -const dhtBencode = require('dht-bencode') +import fs from 'fs' +import path from 'path' +import bench from 'nanobench' + +import bencode from '../' +import bencoding from 'bencoding' +import bncode from 'bncode' +import btparse from 'btparse' +import dht from 'dht.js/lib/dht/bencode' +import dhtBencode from 'dht-bencode' const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent')) diff --git a/benchmark/compare-encode.js b/benchmark/compare-encode.js index f20241c..3f18d6c 100644 --- a/benchmark/compare-encode.js +++ b/benchmark/compare-encode.js @@ -1,12 +1,12 @@ -const fs = require('fs') -const path = require('path') -const bench = require('nanobench') - -const bencode = require('../') -const bencoding = require('bencoding') -const bncode = require('bncode') -const dht = require('dht.js/lib/dht/bencode') -const dhtBencode = require('dht-bencode') +import fs from 'fs' +import path from 'path' +import bench from 'nanobench' + +import bencode from '../' +import bencoding from 'bencoding' +import bncode from 'bncode' +import dht from 'dht.js/lib/dht/bencode' +import dhtBencode from 'dht-bencode' const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent')) const object = bencode.decode(buffer) diff --git a/benchmark/encoding-length.js b/benchmark/encoding-length.js index d92f291..0384ff5 100644 --- a/benchmark/encoding-length.js +++ b/benchmark/encoding-length.js @@ -1,7 +1,7 @@ -const fs = require('fs') -const path = require('path') -const bencode = require('..') -const bench = require('nanobench') +import fs from 'fs' +import path from 'path' +import bencode from '..' +import bench from 'nanobench' const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent')) const torrent = bencode.decode(buffer) diff --git a/lib/decode.js b/lib/decode.js index 2f95075..3d3bd40 100644 --- a/lib/decode.js +++ b/lib/decode.js @@ -165,4 +165,4 @@ decode.buffer = function () { : decode.data.slice(sep, end) } -module.exports = decode +export default decode diff --git a/lib/encode.js b/lib/encode.js index da4c3f3..3e6d7ae 100644 --- a/lib/encode.js +++ b/lib/encode.js @@ -1,4 +1,4 @@ -const { getType } = require('./util.js') +import { getType } from './util.js' /** * Encodes data in bencode. @@ -131,4 +131,4 @@ encode.listSet = function (buffers, data) { buffers.push(buffE) } -module.exports = encode +export default encode diff --git a/lib/encoding-length.js b/lib/encoding-length.js index 7feb291..5340fd2 100644 --- a/lib/encoding-length.js +++ b/lib/encoding-length.js @@ -1,4 +1,4 @@ -const { digitCount, getType } = require('./util.js') +import { digitCount, getType } from './util.js' function listLength (list) { let length = 1 + 1 // type marker + end-of-type marker @@ -66,4 +66,4 @@ function encodingLength (value) { } } -module.exports = encodingLength +export default encodingLength diff --git a/lib/index.js b/lib/index.js index 19edb72..ea0bf16 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,12 +1,11 @@ -const bencode = module.exports - -bencode.encode = require('./encode.js') -bencode.decode = require('./decode.js') - +import encode from './encode.js' +import decode from './decode.js' +import byteLength from './encoding-length.js' /** * Determines the amount of bytes * needed to encode the given value * @param {Object|Array|Buffer|String|Number|Boolean} value * @return {Number} byteCount */ -bencode.byteLength = bencode.encodingLength = require('./encoding-length.js') +const encodingLength = byteLength +export default { encode, decode, byteLength, encodingLength } diff --git a/lib/util.js b/lib/util.js index 1036fa3..aee0439 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,4 @@ -const util = module.exports - -util.digitCount = function digitCount (value) { +export function digitCount (value) { // Add a digit for negative numbers, as the sign will be prefixed const sign = value < 0 ? 1 : 0 // Guard against negative numbers & zero going into log10(), @@ -9,7 +7,7 @@ util.digitCount = function digitCount (value) { return Math.floor(Math.log10(value)) + 1 + sign } -util.getType = function getType (value) { +export function getType (value) { if (Buffer.isBuffer(value)) return 'buffer' if (ArrayBuffer.isView(value)) return 'arraybufferview' if (Array.isArray(value)) return 'array' diff --git a/package.json b/package.json index 9ed9926..912282f 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "bugs": { "url": "https://github.com/themasch/node-bencode/issues" }, + "type": "module", "contributors": [ { "name": "Mark Schmale", @@ -38,7 +39,12 @@ "torrent" ], "license": "MIT", - "main": "lib", + "engines": { + "node": ">=12.20.0" + }, + "exports": { + "import": "./lib/index.js" + }, "repository": { "type": "git", "url": "git://github.com/themasch/node-bencode.git" diff --git a/test/BEP-0023.test.js b/test/BEP-0023.test.js index 9ac808e..f54aa01 100644 --- a/test/BEP-0023.test.js +++ b/test/BEP-0023.test.js @@ -1,7 +1,11 @@ -const path = require('path') -const fs = require('fs') -const test = require('tape').test -const bencode = require('../lib/index.js') +import path, { dirname } from 'path' +import fs from 'fs' +import test from 'tape' +import bencode from '../lib/index.js' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) // @see http://www.bittorrent.org/beps/bep_0023.html test('BEP 0023', function (t) { diff --git a/test/abstract-encoding.test.js b/test/abstract-encoding.test.js index 4685d6e..7072b68 100644 --- a/test/abstract-encoding.test.js +++ b/test/abstract-encoding.test.js @@ -1,5 +1,5 @@ -const test = require('tape').test -const bencode = require('../lib/index.js') +import test from 'tape' +import bencode from '../lib/index.js' test('abstract encoding', function (t) { t.test('encodingLength( value )', function (t) { diff --git a/test/data.js b/test/data.js index ccd4cea..661f83e 100644 --- a/test/data.js +++ b/test/data.js @@ -1,4 +1,4 @@ -module.exports = { +export default { binKeyData: Buffer.from('ZDU6ZmlsZXNkMzY6N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3ZDg6Y29tcGxldGVpMGUxMDpkb3dubG9hZGVkaTEwZTEwOmluY29tcGxldGVpMGVlZWU=', 'base64'), binKeyName: Buffer.from('N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3', 'base64'), binResultData: Buffer.from('NzrDtsKxc2Rm', 'base64'), diff --git a/test/decode.buffer.test.js b/test/decode.buffer.test.js index 680f0b7..909e16c 100644 --- a/test/decode.buffer.test.js +++ b/test/decode.buffer.test.js @@ -1,6 +1,6 @@ -const test = require('tape').test -const data = require('./data') -const bencode = require('../lib/index.js') +import test from 'tape' +import data from './data.js' +import bencode from '../lib/index.js' test('bencode#decode(x)', function (t) { t.test('should be able to decode an integer', function (t) { diff --git a/test/decode.utf8.test.js b/test/decode.utf8.test.js index c87f9f6..c1e87f8 100644 --- a/test/decode.utf8.test.js +++ b/test/decode.utf8.test.js @@ -1,6 +1,6 @@ -const test = require('tape').test -const data = require('./data') -const bencode = require('../lib/index.js') +import test from 'tape' +import data from './data.js' +import bencode from '../lib/index.js' test("bencode#decode(x, 'uft8')", function (t) { t.test('should be able to decode an integer', function (t) { diff --git a/test/encode.test.js b/test/encode.test.js index a93881a..14e731a 100644 --- a/test/encode.test.js +++ b/test/encode.test.js @@ -1,6 +1,6 @@ -const test = require('tape').test -const data = require('./data.js') -const bencode = require('../lib/index.js') +import test from 'tape' +import data from './data.js' +import bencode from '../lib/index.js' test('bencode#encode()', function (t) { // prevent the warning showing up in the test diff --git a/test/encoding-length.test.js b/test/encoding-length.test.js index 5aab644..294a4db 100644 --- a/test/encoding-length.test.js +++ b/test/encoding-length.test.js @@ -1,7 +1,11 @@ -const fs = require('fs') -const path = require('path') -const test = require('tape').test -const bencode = require('../lib/index.js') +import fs from 'fs' +import path, { dirname } from 'path' +import test from 'tape' +import bencode from '../lib/index.js' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) const torrent = fs.readFileSync( path.join(__dirname, '..', 'benchmark', 'test.torrent') diff --git a/test/null-values.test.js b/test/null-values.test.js index 79eb9a8..5632ebd 100644 --- a/test/null-values.test.js +++ b/test/null-values.test.js @@ -1,5 +1,5 @@ -const test = require('tape').test -const bencode = require('../lib/index.js') +import test from 'tape' +import bencode from '../lib/index.js' test('Data with null values', function (t) { t.test('should return an empty value when encoding either null or undefined', function (t) {