nt 0.7.1 → 0.7.2
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 +3 -3
- package/lib/hasher.js +23 -9
- package/lib/schema.js +3 -4
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Read, make, and hash check torrents with node.js!
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/nt)
|
|
6
|
+
[](https://github.com/fent/node-torrent/actions/workflows/nodejs.yml)
|
|
7
7
|
[](https://codecov.io/gh/fent/node-torrent)
|
|
8
8
|
|
|
9
9
|
# Usage
|
|
@@ -202,7 +202,7 @@ Hash checking is finished.
|
|
|
202
202
|
|
|
203
203
|
|
|
204
204
|
# Tests
|
|
205
|
-
Tests are written with [
|
|
205
|
+
Tests are written with [mocha](https://mochajs.org/) and [chai](https://www.chaijs.com/)
|
|
206
206
|
|
|
207
207
|
```bash
|
|
208
208
|
npm test
|
package/lib/hasher.js
CHANGED
|
@@ -3,7 +3,6 @@ const path = require('path');
|
|
|
3
3
|
const crypto = require('crypto');
|
|
4
4
|
const Stream = require('stream').Stream;
|
|
5
5
|
const parallel = require('async').parallel;
|
|
6
|
-
const _ = require('underscore');
|
|
7
6
|
const StreamSpeed = require('streamspeed');
|
|
8
7
|
|
|
9
8
|
const queue = require('./queue');
|
|
@@ -45,11 +44,17 @@ const round = (num, dec) => {
|
|
|
45
44
|
* Allows arrays of objects with a `path` property to be
|
|
46
45
|
* sorted alphabetically
|
|
47
46
|
*
|
|
48
|
-
* @param {Object}
|
|
47
|
+
* @param {Array.<Object>} a
|
|
48
|
+
* @param {Array.<Object>} b
|
|
49
49
|
* @return {string}
|
|
50
50
|
*/
|
|
51
|
-
const sortByPath = (
|
|
52
|
-
|
|
51
|
+
const sortByPath = (a, b) => {
|
|
52
|
+
const len = Math.min(a.path.length, b.path.length);
|
|
53
|
+
for (let i = 0; i < len; i++) {
|
|
54
|
+
if (a.path[i] < b.path[i]) return -1;
|
|
55
|
+
if (a.path[i] > b.path[i]) return 1;
|
|
56
|
+
}
|
|
57
|
+
return a.path.length - b.path.length;
|
|
53
58
|
};
|
|
54
59
|
|
|
55
60
|
|
|
@@ -95,9 +100,18 @@ module.exports = class Hasher extends Stream {
|
|
|
95
100
|
return this.emit('error', err);
|
|
96
101
|
}
|
|
97
102
|
|
|
103
|
+
const fromDisk = typeof list[0] === 'string';
|
|
104
|
+
|
|
98
105
|
const readTasks = (err) => {
|
|
99
|
-
//
|
|
100
|
-
this.files =
|
|
106
|
+
// Create shallow copy of the files array.
|
|
107
|
+
this.files = Array.from(this.files);
|
|
108
|
+
|
|
109
|
+
// When creating a torrent from files on disk, sort them alphabetically.
|
|
110
|
+
// File lists from an existing torrent must keep their original order,
|
|
111
|
+
// since the piece hashes depend on it, so they are not sorted.
|
|
112
|
+
if (fromDisk) {
|
|
113
|
+
this.files.sort(sortByPath);
|
|
114
|
+
}
|
|
101
115
|
|
|
102
116
|
if (err) return this.emit('error', err);
|
|
103
117
|
|
|
@@ -113,7 +127,7 @@ module.exports = class Hasher extends Stream {
|
|
|
113
127
|
};
|
|
114
128
|
|
|
115
129
|
// Check if list containts objects or strings.
|
|
116
|
-
if (
|
|
130
|
+
if (fromDisk) {
|
|
117
131
|
|
|
118
132
|
// Create queue to handle having many files opened.
|
|
119
133
|
const statqueue = queue(fs.stat, this.maxFiles);
|
|
@@ -144,9 +158,9 @@ module.exports = class Hasher extends Stream {
|
|
|
144
158
|
this.filetasks.push({
|
|
145
159
|
length : task.length,
|
|
146
160
|
path : task.path,
|
|
147
|
-
// Piece this task will write to.
|
|
161
|
+
// Piece this task will read from or write to.
|
|
148
162
|
piece,
|
|
149
|
-
// Piece buffer offset to start writing at.
|
|
163
|
+
// Piece buffer offset to start reading or writing at.
|
|
150
164
|
offset,
|
|
151
165
|
});
|
|
152
166
|
|
package/lib/schema.js
CHANGED
|
@@ -23,11 +23,10 @@ const checkBuffer = (torrent, field) => {
|
|
|
23
23
|
*/
|
|
24
24
|
const checkAnnounce = (torrent) => {
|
|
25
25
|
let err;
|
|
26
|
-
if (
|
|
27
|
-
|
|
26
|
+
if (torrent.announce) {
|
|
27
|
+
if ((err = checkBuffer(torrent, 'announce'))) return err;
|
|
28
|
+
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
|
|
28
29
|
}
|
|
29
|
-
if ((err = checkBuffer(torrent, 'announce'))) return err;
|
|
30
|
-
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
|
|
31
30
|
return null;
|
|
32
31
|
};
|
|
33
32
|
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"bittorremt",
|
|
7
7
|
"file"
|
|
8
8
|
],
|
|
9
|
-
"version": "0.7.
|
|
9
|
+
"version": "0.7.2",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "git://github.com/fent/node-torrent.git"
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"lib": "./lib"
|
|
22
22
|
},
|
|
23
23
|
"scripts": {
|
|
24
|
-
"test": "
|
|
24
|
+
"test": "nyc mocha test/*-test.js"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"async": "^3.2.0",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"buffers": "~0.1.1",
|
|
30
30
|
"memorystream": "~0.3.1",
|
|
31
31
|
"ordered-emitter": "^1.0.0",
|
|
32
|
-
"streamspeed": "~2.0.0"
|
|
33
|
-
"underscore": "^1.10.2"
|
|
32
|
+
"streamspeed": "~2.0.0"
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
35
|
+
"chai": "6.2.1",
|
|
36
|
+
"mocha": "12.0.3",
|
|
37
|
+
"nyc": "18.0.0"
|
|
38
38
|
},
|
|
39
39
|
"engines": {
|
|
40
40
|
"node": ">=6"
|