@ctrl/magnet-link 4.1.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,9 +4,10 @@
4
4
 
5
5
  Port of [webtorrent/magnet-uri](https://github.com/webtorrent/magnet-uri) by [feross](https://github.com/feross) that uses fewer dependencies in typescript
6
6
 
7
- __Demo__: https://magnet-link.vercel.app
7
+ **Demo**: https://magnet-link.ep.workers.dev
8
8
 
9
9
  ### Install
10
+
10
11
  ```console
11
12
  npm install @ctrl/magnet-link
12
13
  ```
@@ -14,15 +15,19 @@ npm install @ctrl/magnet-link
14
15
  ### Use
15
16
 
16
17
  #### Encode
18
+
17
19
  ```ts
18
20
  import { magnetDecode } from '@ctrl/magnet-link';
19
21
 
20
- 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';
22
+ const uri =
23
+ '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';
21
24
  const parsed = magnetDecode(uri);
22
- console.log(parsed.dn) // "Leaves of Grass by Walt Whitman.epub"
23
- console.log(parsed.infoHash) // "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"
25
+ console.log(parsed.dn); // "Leaves of Grass by Walt Whitman.epub"
26
+ console.log(parsed.infoHash); // "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"
24
27
  ```
28
+
25
29
  The entire parsed object
30
+
26
31
  ```json
27
32
  {
28
33
  "xt": "urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
@@ -75,6 +80,7 @@ You can also use convenience key names like name (dn), infoHash (xt), infoHashIn
75
80
  ```
76
81
 
77
82
  ### See Also
83
+
78
84
  - magnet-uri https://github.com/webtorrent/magnet-uri
79
85
  - magnet uri spec http://www.bittorrent.org/beps/bep_0053.html
80
86
  - more spec http://www.bittorrent.org/beps/bep_0009.html
package/dist/src/bep53.js CHANGED
@@ -9,10 +9,10 @@ export function composeRange(range) {
9
9
  }, [])
10
10
  .map(cur => (cur.length > 1 ? `${cur[0]}-${cur[cur.length - 1]}` : `${cur[0]}`));
11
11
  }
12
+ const generateRange = (start, end = start) => Array.from({ length: end - start + 1 }, (_, idx) => idx + start);
12
13
  export function parseRange(range) {
13
- const generateRange = (start, end = start) => Array.from({ length: end - start + 1 }, (_, idx) => idx + start);
14
14
  return range.reduce((acc, cur) => {
15
- const r = cur.split('-').map(cur => parseInt(cur, 10));
16
- return acc.concat(generateRange(r[0], r[1]));
15
+ const r = cur.split('-').map(cur => Number.parseInt(cur, 10));
16
+ return [...acc, ...generateRange(r[0], r[1])];
17
17
  }, []);
18
18
  }
package/dist/src/index.js CHANGED
@@ -4,7 +4,7 @@ import * as bep53Range from './bep53.js';
4
4
  const start = 'magnet:?';
5
5
  export function magnetDecode(uri) {
6
6
  // Support 'stream-magnet:' as well
7
- const data = uri.substr(uri.indexOf(start) + start.length);
7
+ const data = uri.slice(uri.indexOf(start) + start.length);
8
8
  const params = data && data.length >= 0 ? data.split('&') : [];
9
9
  const result = {};
10
10
  params.forEach(param => {
@@ -29,7 +29,6 @@ export function magnetDecode(uri) {
29
29
  return;
30
30
  }
31
31
  result[key] = [r, val];
32
- // eslint-disable-next-line no-useless-return
33
32
  return;
34
33
  });
35
34
  if (result.xt) {
@@ -83,14 +82,14 @@ export function magnetDecode(uri) {
83
82
  }
84
83
  result.urlList = [];
85
84
  if (typeof result.as === 'string' || Array.isArray(result.as)) {
86
- result.urlList = result.urlList.concat(result.as);
85
+ result.urlList = [...result.urlList, ...(Array.isArray(result.as) ? result.as : [result.as])];
87
86
  }
88
87
  if (typeof result.ws === 'string' || Array.isArray(result.ws)) {
89
- result.urlList = result.urlList.concat(result.ws);
88
+ result.urlList = [...result.urlList, ...(Array.isArray(result.ws) ? result.ws : [result.ws])];
90
89
  }
91
90
  result.peerAddresses = [];
92
91
  if (typeof result['x.pe'] === 'string' || Array.isArray(result['x.pe'])) {
93
- result.peerAddresses = result.peerAddresses.concat(result['x.pe']);
92
+ result.peerAddresses = [...result.peerAddresses, ...(Array.isArray(result['x.pe']) ? result['x.pe'] : [result['x.pe']])];
94
93
  }
95
94
  result.announce = [...new Set(result.announce)].sort((a, b) => a.localeCompare(b));
96
95
  result.urlList = [...new Set(result.urlList)].sort((a, b) => a.localeCompare(b));
@@ -146,7 +145,7 @@ export function magnetEncode(data) {
146
145
  if (obj.infoHashV2) {
147
146
  xts.add(`urn:btmh:1220${obj.infoHashV2}`);
148
147
  }
149
- const xtsDeduped = Array.from(xts);
148
+ const xtsDeduped = [...xts];
150
149
  if (xtsDeduped.length === 1) {
151
150
  obj.xt = xtsDeduped[0];
152
151
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@ctrl/magnet-link",
3
- "version": "4.1.0",
3
+ "version": "4.2.1",
4
4
  "description": "Parse a magnet URI into an object",
5
5
  "author": "Scott Cooper <scttcper@gmail.com>",
6
- "homepage": "https://magnet-link.vercel.app",
6
+ "homepage": "https://magnet-link.ep.workers.dev",
7
7
  "license": "MIT",
8
8
  "repository": "scttcper/magnet-link",
9
9
  "keywords": [
@@ -23,30 +23,25 @@
23
23
  "dev": "npm run demo:watch",
24
24
  "demo:build": "npm run build --workspace=demo",
25
25
  "demo:watch": "npm run dev --workspace=demo",
26
- "lint": "pnpm run '/^(lint:biome|lint:eslint)$/'",
27
- "lint:biome": "biome check .",
28
- "lint:eslint": "eslint --ext .ts,.tsx .",
29
- "lint:fix": "pnpm run '/^(lint:biome|lint:eslint):fix$/'",
30
- "lint:eslint:fix": "eslint --ext .ts,.tsx . --fix",
31
- "lint:biome:fix": "biome check . --apply",
26
+ "lint": "oxlint . && oxlint .",
27
+ "lint:fix": "oxlint . --fix && oxlint . --fix",
32
28
  "prepare": "npm run build",
33
29
  "build": "tsc",
34
30
  "test": "vitest run",
35
- "test:watch": "vitest",
36
- "test:ci": "vitest run --coverage --reporter=default --reporter=junit --outputFile=./junit.xml"
31
+ "test:watch": "vitest"
37
32
  },
38
33
  "dependencies": {
39
- "rfc4648": "^1.5.3",
40
- "uint8array-extras": "^1.1.0"
34
+ "rfc4648": "^1.5.4",
35
+ "uint8array-extras": "^1.5.0"
41
36
  },
42
37
  "devDependencies": {
43
- "@biomejs/biome": "1.6.0",
44
- "@ctrl/eslint-config-biome": "2.1.1",
45
- "@sindresorhus/tsconfig": "5.0.0",
46
- "@types/node": "20.11.25",
47
- "@vitest/coverage-v8": "1.3.1",
48
- "typescript": "5.4.2",
49
- "vitest": "1.3.1"
38
+ "@ctrl/oxlint-config": "1.4.0",
39
+ "@sindresorhus/tsconfig": "8.1.0",
40
+ "@types/node": "25.2.3",
41
+ "oxfmt": "0.33.0",
42
+ "oxlint": "1.48.0",
43
+ "typescript": "5.9.3",
44
+ "vitest": "4.0.18"
50
45
  },
51
46
  "workspaces": [
52
47
  "demo"
@@ -61,6 +56,6 @@
61
56
  ]
62
57
  },
63
58
  "engines": {
64
- "node": ">=18"
59
+ "node": ">20"
65
60
  }
66
61
  }