diff --git a/README.md b/README.md index 2262ca0..0135ba4 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ This package should be used with [bittorrent-protocol](https://www.npmjs.com/pac Say you're already using `bittorrent-protocol`. Your code might look something like this: ```js -const Protocol = require('bittorrent-protocol') -const net = require('net') +import Protocol from 'bittorrent-protocol' +import net from 'net' net.createServer(socket => { var wire = new Protocol() @@ -48,9 +48,9 @@ net.createServer(socket => { To add support for BEP 9, simply modify your code like this: ```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() diff --git a/index.js b/index.js index 45ea867..c38731c 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,17 @@ /*! ut_metadata. MIT License. WebTorrent LLC */ -const { EventEmitter } = require('events') -const bencode = require('bencode') -const BitField = require('bitfield').default -const debug = require('debug')('ut_metadata') -const sha1 = require('simple-sha1') +import { EventEmitter } from 'events' +import bencode from 'bencode' +import BitField from 'bitfield' +import Debug from 'debug' +import sha1 from 'simple-sha1' + +const debug = Debug('ut_metadata') const MAX_METADATA_SIZE = 1E7 // 10 MB const BITFIELD_GROW = 1E3 const PIECE_LENGTH = 1 << 14 // 16 KiB -module.exports = metadata => { +export default metadata => { class utMetadata extends EventEmitter { constructor (wire) { super() diff --git a/package.json b/package.json index a6f1ed0..ec1a192 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "bugs": { "url": "https://github.com/webtorrent/ut_metadata/issues" }, + "type": "module", "dependencies": { "bencode": "^2.0.1", "bitfield": "^4.0.0", @@ -37,7 +38,12 @@ "ut_metadata" ], "license": "MIT", - "main": "index.js", + "engines": { + "node": ">=12.20.0" + }, + "exports": { + "import": "./index.js" + }, "repository": { "type": "git", "url": "git://github.com/webtorrent/ut_metadata.git" diff --git a/test/basic.js b/test/basic.js index cd4d176..ece37f2 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,8 +1,10 @@ -const { leavesMetadata } = require('webtorrent-fixtures') -const bencode = require('bencode') -const Protocol = require('bittorrent-protocol') -const test = require('tape') -const utMetadata = require('../') +import fixtures from 'webtorrent-fixtures' +import bencode from 'bencode' +import Protocol from 'bittorrent-protocol' +import test from 'tape' +import utMetadata from '../index.js' + +const { leavesMetadata } = fixtures test('wire.use(utMetadata())', t => { const wire = new Protocol() diff --git a/test/fetch.js b/test/fetch.js index 2363c47..2aa4780 100644 --- a/test/fetch.js +++ b/test/fetch.js @@ -1,8 +1,10 @@ -const { leavesMetadata, sintel } = require('webtorrent-fixtures') -const bencode = require('bencode') -const Protocol = require('bittorrent-protocol') -const test = require('tape') -const utMetadata = require('../') +import fixtures from 'webtorrent-fixtures' +import bencode from 'bencode' +import Protocol from 'bittorrent-protocol' +import test from 'tape' +import utMetadata from '../index.js' + +const { leavesMetadata, sintel } = fixtures const id1 = Buffer.from('01234567890123456789') const id2 = Buffer.from('12345678901234567890') diff --git a/test/package.json b/test/package.json index 3464324..911f067 100644 --- a/test/package.json +++ b/test/package.json @@ -1,6 +1,7 @@ { "name": "test", "version": "0.0.0", + "type": "module", "browserify": { "transform": ["brfs"] }