diff --git a/README.md b/README.md index c67527fc..a172b8fe 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ The return value of `parseTorrent` will contain as much info as possible about t torrent. The only property that is guaranteed to be present is `infoHash`. ```js -const parseTorrent = require('parse-torrent') -const fs = require('fs') +import parseTorrent from 'parse-torrent' +import fs from 'fs' // info hash (as a hex string) parseTorrent('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36') @@ -113,7 +113,7 @@ The reverse works too. To convert an object of keys/value to a magnet uri or .to buffer, use `toMagnetURI` and `toTorrentFile`. ```js -const parseTorrent = require('parse-torrent') +import parseTorrent from 'parse-torrent' const uri = parseTorrent.toMagnetURI({ infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' diff --git a/bin/cmd.js b/bin/cmd.js index 330be042..ba31fab7 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -const stdin = require('get-stdin') -const parseTorrent = require('../') +import stdin from 'get-stdin' +import parseTorrent from '../index.js' function usage () { console.error('Usage: parse-torrent /path/to/torrent') diff --git a/index.js b/index.js index 9782e3c4..ba94aa48 100644 --- a/index.js +++ b/index.js @@ -1,20 +1,14 @@ /*! parse-torrent. MIT License. WebTorrent LLC */ /* global Blob */ -const bencode = require('bencode') -const blobToBuffer = require('blob-to-buffer') -const fs = require('fs') // browser exclude -const get = require('simple-get') -const magnet = require('magnet-uri') -const path = require('path') -const sha1 = require('simple-sha1') -const queueMicrotask = require('queue-microtask') - -module.exports = parseTorrent -module.exports.remote = parseTorrentRemote - -module.exports.toMagnetURI = magnet.encode -module.exports.toTorrentFile = encodeTorrentFile +import bencode from 'bencode' +import blobToBuffer from 'blob-to-buffer' +import fs from 'fs' // browser exclude +import get from 'simple-get' +import magnet from 'magnet-uri' +import path from 'path' +import sha1 from 'simple-sha1' +import queueMicrotask from 'queue-microtask' /** * Parse a torrent identifier (magnet uri, .torrent file, info hash) @@ -267,3 +261,7 @@ function ensure (bool, fieldName) { // Workaround Browserify v13 bug // https://github.com/substack/node-browserify/issues/1483 ;(() => { Buffer.alloc(0) })() + +export default parseTorrent +const toMagnetURI = magnet.encode +export { parseTorrentRemote as remote, encodeTorrentFile as toTorrentFile, toMagnetURI } diff --git a/package.json b/package.json index 979aa86a..6f938f59 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "bugs": { "url": "https://github.com/webtorrent/parse-torrent/issues" }, + "type": "module", "dependencies": { "bencode": "^2.0.2", "blob-to-buffer": "^1.2.9", @@ -42,7 +43,12 @@ "webtorrent" ], "license": "MIT", - "main": "index.js", + "engines": { + "node": ">=12.20.0" + }, + "exports": { + "import": "./index.js" + }, "repository": { "type": "git", "url": "git://github.com/webtorrent/parse-torrent.git" diff --git a/test/basic.js b/test/basic.js index 895e0f33..22090d19 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,9 +1,9 @@ /* global Blob */ -const extend = require('xtend') -const fixtures = require('webtorrent-fixtures') -const parseTorrent = require('../') -const test = require('tape') +import extend from 'xtend' +import fixtures from 'webtorrent-fixtures' +import parseTorrent, { remote } from '../index.js' +import test from 'tape' test('Test supported torrentInfo types', t => { let parsed @@ -126,7 +126,7 @@ test('parse single file torrent from Blob', t => { t.plan(4) const leavesBlob = makeBlobShim(fixtures.leaves.torrent) - parseTorrent.remote(leavesBlob, (err, parsed) => { + remote(leavesBlob, (err, parsed) => { t.error(err) t.equal(parsed.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(parsed.name, fixtures.leaves.parsedTorrent.name) diff --git a/test/corrupt.js b/test/corrupt.js index 05d1a474..b5e31d29 100644 --- a/test/corrupt.js +++ b/test/corrupt.js @@ -1,6 +1,6 @@ -const fixtures = require('webtorrent-fixtures') -const parseTorrent = require('../') -const test = require('tape') +import fixtures from 'webtorrent-fixtures' +import parseTorrent from '../index.js' +import test from 'tape' test('exception thrown when torrent file is missing `name` field', t => { t.throws(() => { diff --git a/test/dedupe-announce.js b/test/dedupe-announce.js index c77259ba..c4359ba1 100644 --- a/test/dedupe-announce.js +++ b/test/dedupe-announce.js @@ -1,7 +1,11 @@ -const fs = require('fs') -const parseTorrent = require('../') -const path = require('path') -const test = require('tape') +import fs from 'fs' +import parseTorrent from '../index.js' +import path, { dirname } from 'path' +import test from 'tape' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) const leavesDuplicateTracker = fs.readFileSync(path.join(__dirname, 'torrents/leaves-duplicate-tracker.torrent')) diff --git a/test/empty-announce-list.js b/test/empty-announce-list.js index 70febfdc..d2dab7f2 100644 --- a/test/empty-announce-list.js +++ b/test/empty-announce-list.js @@ -1,7 +1,11 @@ -const fs = require('fs') -const parseTorrent = require('../') -const path = require('path') -const test = require('tape') +import fs from 'fs' +import parseTorrent from '../index.js' +import path, { dirname } from 'path' +import test from 'tape' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) const leavesAnnounceList = fs.readFileSync(path.join(__dirname, 'torrents/leaves-empty-announce-list.torrent')) diff --git a/test/empty-url-list.js b/test/empty-url-list.js index 0a28e804..d92d2295 100644 --- a/test/empty-url-list.js +++ b/test/empty-url-list.js @@ -1,7 +1,11 @@ -const fs = require('fs') -const parseTorrent = require('../') -const path = require('path') -const test = require('tape') +import fs from 'fs' +import parseTorrent from '../index.js' +import path, { dirname } from 'path' +import test from 'tape' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) const leavesUrlList = fs.readFileSync(path.join(__dirname, 'torrents/leaves-empty-url-list.torrent')) diff --git a/test/encode.js b/test/encode.js index bd150e2e..f911c596 100644 --- a/test/encode.js +++ b/test/encode.js @@ -1,10 +1,10 @@ -const fixtures = require('webtorrent-fixtures') -const parseTorrent = require('../') -const test = require('tape') +import fixtures from 'webtorrent-fixtures' +import parseTorrent, { toTorrentFile } from '../index.js' +import test from 'tape' test('parseTorrent.toTorrentFile', t => { const parsedTorrent = parseTorrent(fixtures.leaves.torrent) - const buf = parseTorrent.toTorrentFile(parsedTorrent) + const buf = toTorrentFile(parsedTorrent) const doubleParsedTorrent = parseTorrent(buf) t.deepEqual(doubleParsedTorrent, parsedTorrent) @@ -14,7 +14,7 @@ test('parseTorrent.toTorrentFile', t => { test('parseTorrent.toTorrentFile w/ comment field', t => { const parsedTorrent = parseTorrent(fixtures.leaves.torrent) parsedTorrent.comment = 'hi there!' - const buf = parseTorrent.toTorrentFile(parsedTorrent) + const buf = toTorrentFile(parsedTorrent) const doubleParsedTorrent = parseTorrent(buf) t.deepEqual(doubleParsedTorrent, parsedTorrent) diff --git a/test/magnet-metadata.js b/test/magnet-metadata.js index cf1ad958..77463884 100644 --- a/test/magnet-metadata.js +++ b/test/magnet-metadata.js @@ -1,6 +1,6 @@ -const fixtures = require('webtorrent-fixtures') -const parseTorrent = require('../') -const test = require('tape') +import fixtures from 'webtorrent-fixtures' +import parseTorrent from '../index.js' +import test from 'tape' const leavesMagnetParsed = { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36', diff --git a/test/no-announce-list.js b/test/no-announce-list.js index c752e43d..34039839 100644 --- a/test/no-announce-list.js +++ b/test/no-announce-list.js @@ -1,7 +1,11 @@ -const fs = require('fs') -const parseTorrent = require('../') -const path = require('path') -const test = require('tape') +import fs from 'fs' +import parseTorrent from '../index.js' +import path, { dirname } from 'path' +import test from 'tape' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) const bitloveIntro = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent')) diff --git a/test/node/basic.js b/test/node/basic.js index 926c39c8..2e9a506e 100644 --- a/test/node/basic.js +++ b/test/node/basic.js @@ -1,7 +1,7 @@ -const fixtures = require('webtorrent-fixtures') -const http = require('http') -const parseTorrent = require('../../') -const test = require('tape') +import fixtures from 'webtorrent-fixtures' +import http from 'http' +import { remote } from '../../index.js' +import test from 'tape' test('http url to a torrent file, string', t => { t.plan(3) @@ -14,7 +14,7 @@ test('http url to a torrent file, string', t => { server.listen(0, () => { const port = server.address().port const url = `http://127.0.0.1:${port}` - parseTorrent.remote(url, (err, parsedTorrent) => { + remote(url, (err, parsedTorrent) => { t.error(err) t.deepEqual(parsedTorrent, fixtures.leaves.parsedTorrent) server.close() @@ -25,7 +25,7 @@ test('http url to a torrent file, string', t => { test('filesystem path to a torrent file, string', t => { t.plan(2) - parseTorrent.remote(fixtures.leaves.torrentPath, (err, parsedTorrent) => { + remote(fixtures.leaves.torrentPath, (err, parsedTorrent) => { t.error(err) t.deepEqual(parsedTorrent, fixtures.leaves.parsedTorrent) }) diff --git a/test/non-torrent-magnet.js b/test/non-torrent-magnet.js index 37979c13..d124463c 100644 --- a/test/non-torrent-magnet.js +++ b/test/non-torrent-magnet.js @@ -1,5 +1,5 @@ -const parseTorrent = require('../') -const test = require('tape') +import parseTorrent from '../index.js' +import test from 'tape' test('exception thrown with non-bittorrent URNs', function (t) { // Non-bittorrent URNs (examples from Wikipedia) diff --git a/test/package.json b/test/package.json index 3464324b..911f067f 100644 --- a/test/package.json +++ b/test/package.json @@ -1,6 +1,7 @@ { "name": "test", "version": "0.0.0", + "type": "module", "browserify": { "transform": ["brfs"] } diff --git a/test/webseed.js b/test/webseed.js index d9590543..1a6151d0 100644 --- a/test/webseed.js +++ b/test/webseed.js @@ -1,7 +1,11 @@ -const fs = require('fs') -const parseTorrent = require('../') -const path = require('path') -const test = require('tape') +import fs from 'fs' +import parseTorrent, { toTorrentFile } from '../index.js' +import path, { dirname } from 'path' +import test from 'tape' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) const leavesUrlList = fs.readFileSync(path.join(__dirname, 'torrents/leaves-url-list.torrent')) @@ -13,7 +17,7 @@ test('parse url-list for webseed support', t => { test('parseTorrent.toTorrentFile url-list for webseed support', t => { const parsedTorrent = parseTorrent(leavesUrlList) - const buf = parseTorrent.toTorrentFile(parsedTorrent) + const buf = toTorrentFile(parsedTorrent) const doubleParsedTorrent = parseTorrent(buf) t.deepEqual(doubleParsedTorrent.urlList, ['http://www2.hn.psu.edu/faculty/jmanis/whitman/leaves-of-grass6x9.pdf']) t.end()