Skip to content

Commit

Permalink
feat: esm
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Nov 28, 2022
1 parent 0d767eb commit 315e3c5
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 65 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/*! parse-torrent. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
/* 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)
Expand Down Expand Up @@ -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 }
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/corrupt.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
12 changes: 8 additions & 4 deletions test/dedupe-announce.js
Original file line number Diff line number Diff line change
@@ -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'))

Expand Down
12 changes: 8 additions & 4 deletions test/empty-announce-list.js
Original file line number Diff line number Diff line change
@@ -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'))

Expand Down
12 changes: 8 additions & 4 deletions test/empty-url-list.js
Original file line number Diff line number Diff line change
@@ -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'))

Expand Down
10 changes: 5 additions & 5 deletions test/encode.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/magnet-metadata.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
12 changes: 8 additions & 4 deletions test/no-announce-list.js
Original file line number Diff line number Diff line change
@@ -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'))

Expand Down
12 changes: 6 additions & 6 deletions test/node/basic.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
Expand All @@ -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)
})
Expand Down
4 changes: 2 additions & 2 deletions test/non-torrent-magnet.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "test",
"version": "0.0.0",
"type": "module",
"browserify": {
"transform": ["brfs"]
}
Expand Down
14 changes: 9 additions & 5 deletions test/webseed.js
Original file line number Diff line number Diff line change
@@ -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'))

Expand All @@ -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()
Expand Down

0 comments on commit 315e3c5

Please sign in to comment.