@ctrl/magnet-link 3.1.2 → 4.0.0

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
@@ -71,7 +71,7 @@ const uri = magnetEncode({
71
71
  });
72
72
 
73
73
 
74
- You can also use convenience key names like name (dn), infoHash (xt), infoHashBuffer (xt), announce (tr), and keywords (kt).
74
+ You can also use convenience key names like name (dn), infoHash (xt), infoHashIntArray (xt), announce (tr), and keywords (kt).
75
75
  ```
76
76
 
77
77
  ### See Also
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  export interface MagnetData {
3
2
  /**
4
3
  * Is the info-hash hex encoded, for a total of 40 characters. For compatability with existing links in the wild, clients should also support the 32 character base32 encoded info-hash.
@@ -13,9 +12,9 @@ export interface MagnetData {
13
12
  * Parsed xt= parameter see xt
14
13
  */
15
14
  infoHash?: string;
16
- infoHashBuffer?: Buffer;
15
+ infoHashIntArray?: Uint8Array;
17
16
  infoHashV2?: string;
18
- infoHashV2Buffer?: Buffer;
17
+ infoHashV2IntArray?: Uint8Array;
19
18
  /**
20
19
  * The display name that may be used by the client to display while waiting for metadata
21
20
  */
@@ -68,7 +67,7 @@ export interface MagnetData {
68
67
  urlList?: string[];
69
68
  peerAddresses?: string[];
70
69
  publicKey?: string;
71
- publicKeyBuffer?: Buffer;
70
+ publicKeyIntArray?: Uint8Array;
72
71
  }
73
72
  export declare function magnetDecode(uri: string): MagnetData;
74
73
  export declare function magnetEncode(data: MagnetData): string;
package/dist/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import { base32Decode } from '@ctrl/ts-base32';
1
+ import { base32 } from 'rfc4648';
2
+ import { hexToUint8Array, uint8ArrayToHex } from 'uint8array-extras';
2
3
  import * as bep53Range from './bep53.js';
3
4
  const start = 'magnet:?';
4
5
  export function magnetDecode(uri) {
@@ -39,8 +40,8 @@ export function magnetDecode(uri) {
39
40
  result.infoHash = m[1].toLowerCase();
40
41
  }
41
42
  else if ((m = xt.match(/^urn:btih:(.{32})/))) {
42
- const decodedStr = base32Decode(m[1]);
43
- result.infoHash = Buffer.from(decodedStr).toString('hex');
43
+ const decodedStr = base32.parse(m[1]);
44
+ result.infoHash = uint8ArrayToHex(decodedStr);
44
45
  }
45
46
  else if ((m = xt.match(/^urn:btmh:1220(.{64})/))) {
46
47
  result.infoHashV2 = m[1].toLowerCase();
@@ -57,13 +58,13 @@ export function magnetDecode(uri) {
57
58
  });
58
59
  }
59
60
  if (result.infoHash) {
60
- result.infoHashBuffer = Buffer.from(result.infoHash, 'hex');
61
+ result.infoHashIntArray = hexToUint8Array(result.infoHash);
61
62
  }
62
63
  if (result.infoHashV2) {
63
- result.infoHashV2Buffer = Buffer.from(result.infoHashV2, 'hex');
64
+ result.infoHashV2IntArray = hexToUint8Array(result.infoHashV2);
64
65
  }
65
66
  if (result.publicKey) {
66
- result.publicKeyBuffer = Buffer.from(result.publicKey, 'hex');
67
+ result.publicKeyIntArray = hexToUint8Array(result.publicKey);
67
68
  }
68
69
  if (result.dn) {
69
70
  result.name = result.dn;
@@ -133,14 +134,14 @@ export function magnetEncode(data) {
133
134
  if (obj.xt && Array.isArray(obj.xt)) {
134
135
  xts = new Set(obj.xt);
135
136
  }
136
- if (obj.infoHashBuffer) {
137
- xts.add(`urn:btih:${obj.infoHashBuffer.toString('hex')}`);
137
+ if (obj.infoHashIntArray) {
138
+ xts.add(`urn:btih:${uint8ArrayToHex(obj.infoHashIntArray)}`);
138
139
  }
139
140
  if (obj.infoHash) {
140
141
  xts.add(`urn:btih:${obj.infoHash}`);
141
142
  }
142
- if (obj.infoHashV2Buffer) {
143
- xts.add((obj.xt = `urn:btmh:1220${obj.infoHashV2Buffer.toString('hex')}`));
143
+ if (obj.infoHashV2IntArray) {
144
+ xts.add((obj.xt = `urn:btmh:1220${uint8ArrayToHex(obj.infoHashV2IntArray)}`));
144
145
  }
145
146
  if (obj.infoHashV2) {
146
147
  xts.add(`urn:btmh:1220${obj.infoHashV2}`);
@@ -157,8 +158,8 @@ export function magnetEncode(data) {
157
158
  if (obj.infoHash) {
158
159
  obj.xt = `urn:btih:${obj.infoHash}`;
159
160
  }
160
- if (obj.publicKeyBuffer) {
161
- obj.xs = `urn:btpk:${obj.publicKeyBuffer.toString('hex')}`;
161
+ if (obj.publicKeyIntArray) {
162
+ obj.xs = `urn:btpk:${uint8ArrayToHex(obj.publicKeyIntArray)}`;
162
163
  }
163
164
  if (obj.publicKey) {
164
165
  obj.xs = `urn:btpk:${obj.publicKey}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl/magnet-link",
3
- "version": "3.1.2",
3
+ "version": "4.0.0",
4
4
  "description": "Parse a magnet URI into an object",
5
5
  "author": "Scott Cooper <scttcper@gmail.com>",
6
6
  "homepage": "https://magnet-link.vercel.app",
@@ -20,6 +20,7 @@
20
20
  ],
21
21
  "sideEffects": false,
22
22
  "scripts": {
23
+ "dev": "npm run demo:watch",
23
24
  "demo:build": "npm run build --workspace=demo",
24
25
  "demo:watch": "npm run dev --workspace=demo",
25
26
  "lint": "eslint --ext .ts .",
@@ -31,23 +32,24 @@
31
32
  "test:ci": "vitest run --coverage --reporter=default --reporter=junit --outputFile=./junit.xml"
32
33
  },
33
34
  "dependencies": {
34
- "@ctrl/ts-base32": "^2.1.3"
35
+ "rfc4648": "^1.5.3",
36
+ "uint8array-extras": "^0.5.1"
35
37
  },
36
38
  "devDependencies": {
37
- "@ctrl/eslint-config": "3.7.0",
38
- "@sindresorhus/tsconfig": "3.0.1",
39
- "@types/node": "20.1.0",
40
- "@vitest/coverage-c8": "0.31.0",
41
- "buffer": "6.0.3",
42
- "c8": "7.13.0",
43
- "typescript": "5.0.4",
44
- "vitest": "0.31.0"
39
+ "@ctrl/eslint-config": "4.0.10",
40
+ "@sindresorhus/tsconfig": "5.0.0",
41
+ "@types/node": "20.9.0",
42
+ "@vitest/coverage-v8": "0.34.6",
43
+ "c8": "8.0.1",
44
+ "typescript": "5.2.2",
45
+ "vitest": "0.34.6"
45
46
  },
46
47
  "workspaces": [
47
48
  "demo"
48
49
  ],
49
50
  "publishConfig": {
50
- "access": "public"
51
+ "access": "public",
52
+ "provenance": true
51
53
  },
52
54
  "release": {
53
55
  "branches": [
@@ -55,6 +57,6 @@
55
57
  ]
56
58
  },
57
59
  "engines": {
58
- "node": ">=14.16"
60
+ "node": ">=18"
59
61
  }
60
62
  }