Skip to content

Commit f5fb13f

Browse files
authored
Merge branch 'master' into rusha
2 parents 9f2dcb3 + d9bf302 commit f5fb13f

4 files changed

Lines changed: 59 additions & 100 deletions

File tree

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
pull-requests: write
1313

1414
steps:
15-
- uses: actions/stale@v5
15+
- uses: actions/stale@v6
1616
with:
1717
repo-token: ${{ secrets.GITHUB_TOKEN }}
1818
stale-issue-message: 'Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?'

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## [5.0.9](https://github.com/webtorrent/create-torrent/compare/v5.0.8...v5.0.9) (2022-11-10)
2+
3+
4+
### Bug Fixes
5+
6+
* hash faster than callback ([#186](https://github.com/webtorrent/create-torrent/issues/186)) ([298f356](https://github.com/webtorrent/create-torrent/commit/298f356c9ba7832c22eee954025ab50bf29dc922))
7+
8+
## [5.0.8](https://github.com/webtorrent/create-torrent/compare/v5.0.7...v5.0.8) (2022-11-10)
9+
10+
11+
### Bug Fixes
12+
13+
* **deps:** update dependency minimist to ^1.2.7 ([#162](https://github.com/webtorrent/create-torrent/issues/162)) ([b81eb98](https://github.com/webtorrent/create-torrent/commit/b81eb98b3a0b0358c40cac387a3a324ae0b4616a))
14+
15+
## [5.0.7](https://github.com/webtorrent/create-torrent/compare/v5.0.6...v5.0.7) (2022-11-10)
16+
17+
18+
### Bug Fixes
19+
20+
* drop block-stream, drop streamx ([#185](https://github.com/webtorrent/create-torrent/issues/185)) ([4e0669c](https://github.com/webtorrent/create-torrent/commit/4e0669c3b2f92a4c0d115bcb6b01c26a21f9dbd6))
21+
122
## [5.0.6](https://github.com/webtorrent/create-torrent/compare/v5.0.5...v5.0.6) (2022-09-02)
223

324

index.js

Lines changed: 33 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
/*! create-torrent. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
22
const bencode = require('bencode')
3-
const BlockStream = require('block-stream2')
3+
const blockIterator = require('block-iterator')
44
const calcPieceLength = require('piece-length')
55
const corePath = require('path')
6-
const { BlobReadStream } = require('fast-blob-stream')
76
const isFile = require('is-file')
87
const junk = require('junk')
98
const joinIterator = require('join-async-iterator')
10-
const once = require('once')
119
const parallel = require('run-parallel')
1210
const queueMicrotask = require('queue-microtask')
1311
const { sha1 } = require('uint8-util')
14-
const { Transform, PassThrough, Readable } = require('streamx')
12+
require('fast-readable-async-iterator')
1513

1614
const getFiles = require('./get-files') // browser exclude
1715

@@ -176,10 +174,10 @@ function _parseInput (input, opts, cb) {
176174
const file = {}
177175

178176
if (isBlob(item)) {
179-
file.getStream = getBlobStream(item)
177+
file.getStream = item.stream()
180178
file.length = item.size
181179
} else if (Buffer.isBuffer(item)) {
182-
file.getStream = getBufferStream(item)
180+
file.getStream = [item] // wrap in iterable to write entire buffer at once instead of unwrapping all bytes
183181
file.length = item.length
184182
} else if (isReadable(item)) {
185183
file.getStream = getStreamStream(item, file)
@@ -206,73 +204,42 @@ function _parseInput (input, opts, cb) {
206204

207205
const MAX_OUTSTANDING_HASHES = 5
208206

209-
function getPieceList (files, pieceLength, estimatedTorrentLength, opts, cb) {
210-
cb = once(cb)
207+
async function getPieceList (files, pieceLength, estimatedTorrentLength, opts, cb) {
211208
const pieces = []
212209
let length = 0
213210
let hashedLength = 0
214211

215212
const streams = files.map(file => file.getStream)
216213

214+
const onProgress = opts.onProgress
215+
217216
let remainingHashes = 0
218217
let pieceNum = 0
219218
let ended = false
220219

221-
const multistream = Readable.from(joinIterator(streams))
222-
const blockstream = new BlockStream(pieceLength, { zeroPadding: false })
223-
224-
multistream.on('error', onError)
225-
226-
multistream
227-
.pipe(blockstream)
228-
.on('data', onData)
229-
.on('end', onEnd)
230-
.on('error', onError)
231-
232-
function onData (chunk) {
233-
length += chunk.length
234-
235-
const i = pieceNum
236-
sha1(chunk, hash => {
237-
pieces[i] = hash
238-
remainingHashes -= 1
239-
if (remainingHashes < MAX_OUTSTANDING_HASHES) {
240-
blockstream.resume()
241-
}
242-
hashedLength += chunk.length
243-
if (opts.onProgress) opts.onProgress(hashedLength, estimatedTorrentLength)
244-
maybeDone()
245-
})
246-
remainingHashes += 1
247-
if (remainingHashes >= MAX_OUTSTANDING_HASHES) {
248-
blockstream.pause()
220+
const iterator = blockIterator(joinIterator(streams), pieceLength, { zeroPadding: false })
221+
try {
222+
for await (const chunk of iterator) {
223+
await new Promise(resolve => {
224+
length += chunk.length
225+
const i = pieceNum
226+
++pieceNum
227+
if (++remainingHashes < MAX_OUTSTANDING_HASHES) resolve()
228+
sha1(chunk, hash => {
229+
pieces[i] = hash
230+
--remainingHashes
231+
hashedLength += chunk.length
232+
if (onProgress) onProgress(hashedLength, estimatedTorrentLength)
233+
resolve()
234+
if (ended && remainingHashes === 0) cb(null, Buffer.from(pieces.join(''), 'hex'), length)
235+
})
236+
})
249237
}
250-
pieceNum += 1
251-
}
252-
253-
function onEnd () {
238+
if (remainingHashes === 0) return cb(null, Buffer.from(pieces.join(''), 'hex'), length)
254239
ended = true
255-
maybeDone()
256-
}
257-
258-
function onError (err) {
259-
cleanup()
240+
} catch (err) {
260241
cb(err)
261242
}
262-
263-
function cleanup () {
264-
multistream.removeListener('error', onError)
265-
blockstream.removeListener('data', onData)
266-
blockstream.removeListener('end', onEnd)
267-
blockstream.removeListener('error', onError)
268-
}
269-
270-
function maybeDone () {
271-
if (ended && remainingHashes === 0) {
272-
cleanup()
273-
cb(null, Buffer.from(pieces.join(''), 'hex'), length)
274-
}
275-
}
276243
}
277244

278245
function onFiles (files, opts, cb) {
@@ -409,45 +376,18 @@ function isReadable (obj) {
409376
}
410377

411378
/**
412-
* Convert a `File` to a lazy readable stream.
413-
* @param {File|Blob} file
414-
* @return {function}
415-
*/
416-
function getBlobStream (file) {
417-
return () => new BlobReadStream(file)
418-
}
419-
420-
/**
421-
* Convert a `Buffer` to a lazy readable stream.
422-
* @param {Buffer} buffer
423-
* @return {function}
424-
*/
425-
function getBufferStream (buffer) {
426-
return () => {
427-
const s = new PassThrough()
428-
s.end(buffer)
429-
return s
430-
}
431-
}
432-
433-
/**
434-
* Convert a readable stream to a lazy readable stream. Adds instrumentation to track
379+
* Convert a readable stream to a lazy async iterator. Adds instrumentation to track
435380
* the number of bytes in the stream and set `file.length`.
436381
*
382+
* @generator
437383
* @param {Stream} readable
438384
* @param {Object} file
439-
* @return {function}
385+
* @return {Uint8Array} stream data/chunk
440386
*/
441-
function getStreamStream (readable, file) {
442-
return () => {
443-
const counter = new Transform()
444-
counter._transform = function (data, cb) {
445-
file.length += data.length
446-
this.push(data)
447-
cb()
448-
}
449-
readable.pipe(counter)
450-
return counter
387+
async function * getStreamStream (readable, file) {
388+
for await (const chunk of readable) {
389+
file.length += chunk.length
390+
yield chunk
451391
}
452392
}
453393

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "create-torrent",
33
"description": "Create .torrent files",
4-
"version": "5.0.6",
4+
"version": "5.0.9",
55
"author": {
66
"name": "WebTorrent LLC",
77
"email": "feross@webtorrent.io",
@@ -19,17 +19,15 @@
1919
},
2020
"dependencies": {
2121
"bencode": "^2.0.3",
22-
"block-stream2": "^2.1.0",
23-
"fast-blob-stream": "^1.1.1",
22+
"block-iterator": "^1.0.1",
23+
"fast-readable-async-iterator": "^1.1.1",
2424
"is-file": "^1.0.0",
2525
"join-async-iterator": "^1.1.1",
2626
"junk": "^3.1.0",
27-
"minimist": "^1.2.5",
28-
"once": "^1.4.0",
27+
"minimist": "^1.2.7",
2928
"piece-length": "^2.0.1",
3029
"queue-microtask": "^1.2.3",
3130
"run-parallel": "^1.2.0",
32-
"streamx": "^2.12.4",
3331
"uint8-util": "^1.0.1"
3432
},
3533
"devDependencies": {

0 commit comments

Comments
 (0)