@ctrl/torrent-file 1.3.3 → 2.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/dist/{bencode → src/bencode}/decode.d.ts +0 -0
- package/dist/{bencode → src/bencode}/decode.js +4 -5
- package/dist/{bencode → src/bencode}/encode.d.ts +1 -1
- package/dist/{bencode → src/bencode}/encode.js +4 -15
- package/dist/{bencode → src/bencode}/index.d.ts +0 -0
- package/dist/src/bencode/index.js +2 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/isArrayBuffer.d.ts +11 -0
- package/dist/src/isArrayBuffer.js +16 -0
- package/dist/{torrentFile.d.ts → src/torrentFile.d.ts} +0 -0
- package/dist/{torrentFile.js → src/torrentFile.js} +16 -24
- package/package.json +31 -34
- package/dist/bencode/index.js +0 -14
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -14
File without changes
|
@@ -1,6 +1,3 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.decode = void 0;
|
4
1
|
const INTEGER_START = 0x69; // 'i'
|
5
2
|
const STRING_DELIM = 0x3a; // ':'
|
6
3
|
const DICTIONARY_START = 0x64; // 'd'
|
@@ -15,6 +12,9 @@ function getIntFromBuffer(buffer, start, end) {
|
|
15
12
|
let sign = 1;
|
16
13
|
for (let i = start; i < end; i++) {
|
17
14
|
const num = buffer[i];
|
15
|
+
if (typeof num === 'undefined') {
|
16
|
+
continue;
|
17
|
+
}
|
18
18
|
if (num < 58 && num >= 48) {
|
19
19
|
sum = sum * 10 + (num - 48);
|
20
20
|
continue;
|
@@ -40,7 +40,7 @@ function getIntFromBuffer(buffer, start, end) {
|
|
40
40
|
/**
|
41
41
|
* Decodes bencoded data
|
42
42
|
*/
|
43
|
-
function decode(data, encoding = null) {
|
43
|
+
export function decode(data, encoding = null) {
|
44
44
|
const state = {
|
45
45
|
bytes: 0,
|
46
46
|
position: 0,
|
@@ -54,7 +54,6 @@ function decode(data, encoding = null) {
|
|
54
54
|
state.bytes = state.data.length;
|
55
55
|
return next(state);
|
56
56
|
}
|
57
|
-
exports.decode = decode;
|
58
57
|
function next(state) {
|
59
58
|
switch (state.data[state.position]) {
|
60
59
|
case DICTIONARY_START:
|
@@ -2,4 +2,4 @@
|
|
2
2
|
/**
|
3
3
|
* Encodes data in bencode.
|
4
4
|
*/
|
5
|
-
export declare function encode(data: Buffer | string | any[] | ArrayBuffer | number | boolean | Record<string, unknown> | object, buffer?:
|
5
|
+
export declare function encode(data: Buffer | string | any[] | ArrayBuffer | number | boolean | Record<string, unknown> | object, buffer?: unknown, offset?: number, disableFloatConversionWarning?: boolean): Buffer;
|
@@ -1,15 +1,9 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.encode = void 0;
|
7
1
|
/* eslint-disable @typescript-eslint/ban-types */
|
8
|
-
|
2
|
+
import { isArrayBuffer } from '../isArrayBuffer.js';
|
9
3
|
/**
|
10
4
|
* Encodes data in bencode.
|
11
5
|
*/
|
12
|
-
function encode(data, buffer, offset, disableFloatConversionWarning = false) {
|
6
|
+
export function encode(data, buffer, offset, disableFloatConversionWarning = false) {
|
13
7
|
const buffers = [];
|
14
8
|
const state = {
|
15
9
|
bytes: -1,
|
@@ -24,7 +18,6 @@ function encode(data, buffer, offset, disableFloatConversionWarning = false) {
|
|
24
18
|
}
|
25
19
|
return result;
|
26
20
|
}
|
27
|
-
exports.encode = encode;
|
28
21
|
function getType(value) {
|
29
22
|
if (Buffer.isBuffer(value)) {
|
30
23
|
return 'buffer';
|
@@ -41,7 +34,7 @@ function getType(value) {
|
|
41
34
|
if (value instanceof Boolean) {
|
42
35
|
return 'boolean';
|
43
36
|
}
|
44
|
-
if (
|
37
|
+
if (isArrayBuffer(value)) {
|
45
38
|
return 'arraybuffer';
|
46
39
|
}
|
47
40
|
return typeof value;
|
@@ -102,13 +95,9 @@ function number(state, buffers, data) {
|
|
102
95
|
}
|
103
96
|
function dict(state, buffers, data) {
|
104
97
|
buffers.push(buffD);
|
105
|
-
let j = 0;
|
106
|
-
let k;
|
107
98
|
// sorted dicts
|
108
99
|
const keys = Object.keys(data).sort();
|
109
|
-
const
|
110
|
-
for (; j < kl; j++) {
|
111
|
-
k = keys[j];
|
100
|
+
for (const k of keys) {
|
112
101
|
// filter out null / undefined elements
|
113
102
|
if (data[k] === null || data[k] === undefined) {
|
114
103
|
continue;
|
File without changes
|
@@ -0,0 +1,11 @@
|
|
1
|
+
/**
|
2
|
+
* Check if the given value is an ArrayBuffer.
|
3
|
+
* @param value - The value to check.
|
4
|
+
* @returns `true` if the given value is an ArrayBuffer, else `false`.
|
5
|
+
* @example
|
6
|
+
* isArrayBuffer(new ArrayBuffer())
|
7
|
+
* // => true
|
8
|
+
* isArrayBuffer([])
|
9
|
+
* // => false
|
10
|
+
*/
|
11
|
+
export declare function isArrayBuffer(value: unknown): boolean;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
const hasArrayBuffer = typeof ArrayBuffer === 'function';
|
2
|
+
const { toString } = Object.prototype;
|
3
|
+
/**
|
4
|
+
* Check if the given value is an ArrayBuffer.
|
5
|
+
* @param value - The value to check.
|
6
|
+
* @returns `true` if the given value is an ArrayBuffer, else `false`.
|
7
|
+
* @example
|
8
|
+
* isArrayBuffer(new ArrayBuffer())
|
9
|
+
* // => true
|
10
|
+
* isArrayBuffer([])
|
11
|
+
* // => false
|
12
|
+
*/
|
13
|
+
export function isArrayBuffer(value) {
|
14
|
+
return (hasArrayBuffer &&
|
15
|
+
(value instanceof ArrayBuffer || toString.call(value) === '[object ArrayBuffer]'));
|
16
|
+
}
|
File without changes
|
@@ -1,22 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
const crypto_hash_1 = require("crypto-hash");
|
5
|
-
const path_1 = require("path");
|
6
|
-
const bencode_1 = require("./bencode");
|
1
|
+
import { join, sep } from 'node:path';
|
2
|
+
import { sha1 } from 'crypto-hash';
|
3
|
+
import { decode, encode } from './bencode/index.js';
|
7
4
|
/**
|
8
5
|
* sha1 of torrent file info. This hash is commenly used by torrent clients as the ID of the torrent.
|
9
6
|
*/
|
10
|
-
async function hash(file) {
|
11
|
-
const torrent =
|
12
|
-
return
|
7
|
+
export async function hash(file) {
|
8
|
+
const torrent = decode(file);
|
9
|
+
return sha1(encode(torrent.info));
|
13
10
|
}
|
14
|
-
exports.hash = hash;
|
15
11
|
/**
|
16
12
|
* data about the files the torrent contains
|
17
13
|
*/
|
18
|
-
function files(file) {
|
19
|
-
const torrent =
|
14
|
+
export function files(file) {
|
15
|
+
const torrent = decode(file);
|
20
16
|
const result = {
|
21
17
|
files: [],
|
22
18
|
length: 0,
|
@@ -27,11 +23,9 @@ function files(file) {
|
|
27
23
|
const files = torrent.info.files || [torrent.info];
|
28
24
|
const name = (torrent.info['name.utf-8'] || torrent.info.name).toString();
|
29
25
|
result.files = files.map((file, i) => {
|
30
|
-
const parts = []
|
31
|
-
.concat(name, file['path.utf-8'] || file.path || [])
|
32
|
-
.map(p => p.toString());
|
26
|
+
const parts = [name, ...(file['path.utf-8'] || file.path || [])].map(p => p.toString());
|
33
27
|
return {
|
34
|
-
path:
|
28
|
+
path: join(sep, ...parts).slice(1),
|
35
29
|
name: parts[parts.length - 1],
|
36
30
|
length: file.length,
|
37
31
|
offset: files.slice(0, i).reduce(sumLength, 0),
|
@@ -40,11 +34,10 @@ function files(file) {
|
|
40
34
|
result.length = files.reduce(sumLength, 0);
|
41
35
|
const lastFile = result.files[result.files.length - 1];
|
42
36
|
result.lastPieceLength =
|
43
|
-
(lastFile.offset + lastFile.length) % result.pieceLength || result.pieceLength;
|
37
|
+
(lastFile && (lastFile.offset + lastFile.length) % result.pieceLength) || result.pieceLength;
|
44
38
|
result.pieces = splitPieces(torrent.info.pieces);
|
45
39
|
return result;
|
46
40
|
}
|
47
|
-
exports.files = files;
|
48
41
|
function sumLength(sum, file) {
|
49
42
|
return sum + file.length;
|
50
43
|
}
|
@@ -58,8 +51,8 @@ function splitPieces(buf) {
|
|
58
51
|
/**
|
59
52
|
* torrent file info
|
60
53
|
*/
|
61
|
-
function info(file) {
|
62
|
-
const torrent =
|
54
|
+
export function info(file) {
|
55
|
+
const torrent = decode(file);
|
63
56
|
const result = {
|
64
57
|
name: (torrent.info['name.utf-8'] || torrent.info.name).toString(),
|
65
58
|
announce: [],
|
@@ -81,8 +74,8 @@ function info(file) {
|
|
81
74
|
if (Array.isArray(torrent['announce-list']) &&
|
82
75
|
torrent['announce-list'] &&
|
83
76
|
torrent['announce-list'].length > 0) {
|
84
|
-
torrent['announce-list'].forEach(urls => {
|
85
|
-
urls.forEach(url => {
|
77
|
+
torrent['announce-list'].forEach((urls) => {
|
78
|
+
urls.forEach((url) => {
|
86
79
|
result.announce.push(url.toString());
|
87
80
|
});
|
88
81
|
});
|
@@ -98,10 +91,9 @@ function info(file) {
|
|
98
91
|
// some clients set url-list to empty string
|
99
92
|
torrent['url-list'] = torrent['url-list'].length > 0 ? [torrent['url-list']] : [];
|
100
93
|
}
|
101
|
-
result.urlList = (torrent['url-list'] || []).map(url => url.toString());
|
94
|
+
result.urlList = (torrent['url-list'] || []).map((url) => url.toString());
|
102
95
|
if (result.urlList.length) {
|
103
96
|
result.urlList = Array.from(new Set(result.urlList));
|
104
97
|
}
|
105
98
|
return result;
|
106
99
|
}
|
107
|
-
exports.info = info;
|
package/package.json
CHANGED
@@ -1,53 +1,50 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ctrl/torrent-file",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0",
|
4
4
|
"description": "Parse a torrent file (name, hash, files, pieces)",
|
5
5
|
"author": "Scott Cooper <scttcper@gmail.com>",
|
6
6
|
"license": "MIT",
|
7
7
|
"repository": "scttcper/torrent-file",
|
8
|
-
"
|
9
|
-
"
|
8
|
+
"type": "module",
|
9
|
+
"exports": "./dist/src/index.js",
|
10
|
+
"types": "./dist/src/index.d.ts",
|
10
11
|
"files": [
|
11
|
-
"dist"
|
12
|
+
"dist/src"
|
12
13
|
],
|
13
14
|
"sideEffects": false,
|
14
|
-
"keywords": [
|
15
|
-
"typescript"
|
16
|
-
],
|
15
|
+
"keywords": [],
|
17
16
|
"scripts": {
|
18
|
-
"lint": "eslint --ext .
|
19
|
-
"lint:fix": "eslint --fix --ext .
|
17
|
+
"lint": "eslint --ext .ts .",
|
18
|
+
"lint:fix": "eslint --fix --ext .ts .",
|
20
19
|
"prepare": "npm run build",
|
21
|
-
"build": "tsc
|
22
|
-
"test": "
|
23
|
-
"test:watch": "
|
24
|
-
"test:ci": "
|
20
|
+
"build": "tsc",
|
21
|
+
"test": "ava",
|
22
|
+
"test:watch": "ava --watch",
|
23
|
+
"test:ci": "c8 --reporter=text --reporter=lcov ava"
|
25
24
|
},
|
26
25
|
"dependencies": {
|
27
|
-
"crypto-hash": "^
|
28
|
-
"lodash.isarraybuffer": "^4.4.1"
|
26
|
+
"crypto-hash": "^2.0.1"
|
29
27
|
},
|
30
28
|
"devDependencies": {
|
31
|
-
"@
|
32
|
-
"@
|
33
|
-
"@
|
34
|
-
"@types/
|
35
|
-
"
|
36
|
-
"
|
37
|
-
"
|
38
|
-
"
|
39
|
-
"
|
40
|
-
"typescript": "4.2.4"
|
41
|
-
},
|
42
|
-
"jest": {
|
43
|
-
"testEnvironment": "node"
|
29
|
+
"@sindresorhus/tsconfig": "2.0.0",
|
30
|
+
"@ctrl/eslint-config": "3.1.4",
|
31
|
+
"@types/node": "17.0.0",
|
32
|
+
"@types/parse-torrent": "5.8.4",
|
33
|
+
"ava": "4.0.0-rc.1",
|
34
|
+
"c8": "7.10.0",
|
35
|
+
"tsm": "2.2.1",
|
36
|
+
"parse-torrent": "9.1.4",
|
37
|
+
"typescript": "4.5.4"
|
44
38
|
},
|
45
|
-
"
|
46
|
-
"
|
47
|
-
"
|
39
|
+
"ava": {
|
40
|
+
"files": [
|
41
|
+
"test/**/*.spec.ts"
|
48
42
|
],
|
49
|
-
"
|
50
|
-
"
|
43
|
+
"extensions": {
|
44
|
+
"ts": "module"
|
45
|
+
},
|
46
|
+
"nodeArguments": [
|
47
|
+
"--loader=tsm"
|
51
48
|
]
|
52
49
|
},
|
53
50
|
"publishConfig": {
|
@@ -57,6 +54,6 @@
|
|
57
54
|
"branch": "master"
|
58
55
|
},
|
59
56
|
"engines": {
|
60
|
-
"node": ">=
|
57
|
+
"node": ">=14.16"
|
61
58
|
}
|
62
59
|
}
|
package/dist/bencode/index.js
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
-
}) : (function(o, m, k, k2) {
|
6
|
-
if (k2 === undefined) k2 = k;
|
7
|
-
o[k2] = m[k];
|
8
|
-
}));
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
|
-
};
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
-
__exportStar(require("./decode"), exports);
|
14
|
-
__exportStar(require("./encode"), exports);
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
-
}) : (function(o, m, k, k2) {
|
6
|
-
if (k2 === undefined) k2 = k;
|
7
|
-
o[k2] = m[k];
|
8
|
-
}));
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
|
-
};
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
-
__exportStar(require("./bencode"), exports);
|
14
|
-
__exportStar(require("./torrentFile"), exports);
|