Skip to content

Commit

Permalink
feat: esm
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Nov 14, 2022
1 parent 5094a03 commit 40fd333
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 69 deletions.
8 changes: 4 additions & 4 deletions benchmark/bencode.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 4 additions & 4 deletions benchmark/buffer-vs-string.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
20 changes: 10 additions & 10 deletions benchmark/compare-decode.js
Original file line number Diff line number Diff line change
@@ -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'))

Expand Down
18 changes: 9 additions & 9 deletions benchmark/compare-encode.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 4 additions & 4 deletions benchmark/encoding-length.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ decode.buffer = function () {
: decode.data.slice(sep, end)
}

module.exports = decode
export default decode
4 changes: 2 additions & 2 deletions lib/encode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getType } = require('./util.js')
import { getType } from './util.js'

/**
* Encodes data in bencode.
Expand Down Expand Up @@ -131,4 +131,4 @@ encode.listSet = function (buffers, data) {
buffers.push(buffE)
}

module.exports = encode
export default encode
4 changes: 2 additions & 2 deletions lib/encoding-length.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -66,4 +66,4 @@ function encodingLength (value) {
}
}

module.exports = encodingLength
export default encodingLength
11 changes: 5 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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 }
6 changes: 2 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"bugs": {
"url": "https://github.com/themasch/node-bencode/issues"
},
"type": "module",
"contributors": [
{
"name": "Mark Schmale",
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions test/BEP-0023.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/abstract-encoding.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/data.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down
6 changes: 3 additions & 3 deletions test/decode.buffer.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions test/decode.utf8.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions test/encode.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 8 additions & 4 deletions test/encoding-length.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
4 changes: 2 additions & 2 deletions test/null-values.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 40fd333

Please sign in to comment.