Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: esm #60

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ npm install magnet-uri
Parse a magnet URI and return an object of keys/values.

```js
const magnet = require('magnet-uri')
import magnet from 'magnet-uri'

// "Leaves of Grass" by Walt Whitman
const uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.example4.com%3A80&tr=udp%3A%2F%2Ftracker.example5.com%3A80&tr=udp%3A%2F%2Ftracker.example3.com%3A6969&tr=udp%3A%2F%2Ftracker.example2.com%3A80&tr=udp%3A%2F%2Ftracker.example1.com%3A1337'
Expand Down Expand Up @@ -70,7 +70,7 @@ The `parsed` magnet link object looks like this:
Convert an object of key/values into a magnet URI string.

```js
const magnet = require('magnet-uri')
import magnet from 'magnet-uri'

const uri = magnet.encode({
xt: [
Expand Down
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*! magnet-uri. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
module.exports = magnetURIDecode
module.exports.decode = magnetURIDecode
module.exports.encode = magnetURIEncode

const base32 = require('thirty-two')
const bep53Range = require('bep53-range')
import base32 from 'thirty-two'
import bep53Range from 'bep53-range'

/**
* Parse a magnet URI and return an object of keys/values
Expand Down Expand Up @@ -175,3 +171,6 @@ function magnetURIEncode (obj) {

return result
}

export default magnetURIDecode
export { magnetURIDecode as decode, magnetURIEncode as encode }
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"bugs": {
"url": "https://github.com/webtorrent/magnet-uri/issues"
},
"type": "module",
"dependencies": {
"bep53-range": "^1.1.1",
"thirty-two": "^1.0.2"
Expand Down Expand Up @@ -43,7 +44,12 @@
"webtorrent"
],
"license": "MIT",
"main": "index.js",
"engines": {
"node": ">=12.20.0"
},
"exports": {
"import": "./index.js"
},
"repository": {
"type": "git",
"url": "git://github.com/webtorrent/magnet-uri.git"
Expand Down
4 changes: 2 additions & 2 deletions test/decode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const magnet = require('../')
const test = require('tape')
import magnet from '../index.js'
import test from 'tape'

const leavesOfGrass = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&xt=urn:btmh:1220d2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.example4.com%3A80&tr=udp%3A%2F%2Ftracker.example5.com%3A80&tr=udp%3A%2F%2Ftracker.example3.com%3A6969&tr=udp%3A%2F%2Ftracker.example2.com%3A80&tr=udp%3A%2F%2Ftracker.example1.com%3A1337'

Expand Down
34 changes: 17 additions & 17 deletions test/encode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const magnet = require('../')
const test = require('tape')
import { encode, decode } from '../index.js'
import test from 'tape'

test('encode: complicated magnet uri (multiple xt params, and as, xs)', t => {
const uri = magnet.encode({
const uri = encode({
xt: [
'urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1',
'urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY',
Expand Down Expand Up @@ -59,11 +59,11 @@ test('encode: simple magnet uri using convenience names', t => {
keywords: ['hey', 'hey2']
}

const result = magnet.encode(obj)
const result = encode(obj)

t.equal(result, 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&xt=urn:btmh:1220d2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.example1.com%3A1337&tr=udp%3A%2F%2Ftracker.example2.com%3A80&tr=udp%3A%2F%2Ftracker.example3.com%3A6969&tr=udp%3A%2F%2Ftracker.example4.com%3A80&tr=udp%3A%2F%2Ftracker.example5.com%3A80&ws=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&kt=hey+hey2')

t.deepEqual(magnet.decode(result), obj)
t.deepEqual(decode(result), obj)

t.end()
})
Expand All @@ -72,9 +72,9 @@ test('encode: using infoHashBuffer', t => {
const obj = {
infoHashBuffer: Buffer.from('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex')
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
t.deepEqual(magnet.decode(result), {
t.deepEqual(decode(result), {
infoHashBuffer: Buffer.from('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex'),
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
Expand All @@ -89,9 +89,9 @@ test('encode: using infoHashV2Buffer', t => {
const obj = {
infoHashV2Buffer: Buffer.from('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b', 'hex')
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xt=urn:btmh:1220d2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b')
t.deepEqual(magnet.decode(result), {
t.deepEqual(decode(result), {
infoHashV2: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b',
infoHashV2Buffer: Buffer.from('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b', 'hex'),
xt: 'urn:btmh:1220d2474e86c95b19b8bcfdb92bc12c9d44667cfa36d2474e86c95b19b8bcfdb92b',
Expand All @@ -108,9 +108,9 @@ test('encode: select-only', t => {
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
so: [0, 2, 4, 6, 7, 8]
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&so=0,2,4,6-8')
t.deepEqual(magnet.decode(result), {
t.deepEqual(decode(result), {
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
infoHashBuffer: Buffer.from('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex'),
Expand All @@ -128,9 +128,9 @@ test('encode: peer-address single value', t => {
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
'x.pe': '123.213.32.10:47450'
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&x.pe=123.213.32.10:47450')
t.deepEqual(magnet.decode(result), {
t.deepEqual(decode(result), {
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
'x.pe': '123.213.32.10:47450',
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
Expand All @@ -147,9 +147,9 @@ test('encode: peer-address multiple values', t => {
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
'x.pe': ['123.213.32.10:47450', '[2001:db8::2]:55013']
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&x.pe=123.213.32.10:47450&x.pe=[2001:db8::2]:55013')
t.deepEqual(magnet.decode(result), {
t.deepEqual(decode(result), {
xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
'x.pe': ['123.213.32.10:47450', '[2001:db8::2]:55013'],
infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
Expand All @@ -166,7 +166,7 @@ test('encode: using publicKey', t => {
const obj = {
publicKey
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xs=urn:btpk:9a36edf0988ddc1a0fc02d4e8652cce87a71aaac71fce936e650a597c0fb72e0')
t.end()
})
Expand All @@ -176,7 +176,7 @@ test('encode: using publicKeyBuffer', t => {
const obj = {
publicKeyBuffer
}
const result = magnet.encode(obj)
const result = encode(obj)
t.equal(result, 'magnet:?xs=urn:btpk:9a36edf0988ddc1a0fc02d4e8652cce87a71aaac71fce936e650a597c0fb72e0')
t.end()
})