Skip to content

Commit

Permalink
perf: drop buffer (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown authored Jan 26, 2023
1 parent e980d2c commit 2e030c7
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import junk from 'junk'
import joinIterator from 'join-async-iterator'
import parallel from 'run-parallel'
import queueMicrotask from 'queue-microtask'
import { hash } from 'uint8-util'
import { hash, hex2arr } from 'uint8-util'
import 'fast-readable-async-iterator'

import getFiles from './get-files.js' // browser exclude
Expand Down Expand Up @@ -122,7 +122,7 @@ function _parseInput (input, opts, cb) {

if (commonPrefix) {
input.forEach(item => {
const pathless = (Buffer.isBuffer(item) || isReadable(item)) && !item[pathSymbol]
const pathless = (ArrayBuffer.isView(item) || isReadable(item)) && !item[pathSymbol]
if (typeof item === 'string' || pathless) return
item[pathSymbol].shift()
})
Expand Down Expand Up @@ -176,7 +176,7 @@ function _parseInput (input, opts, cb) {
if (isBlob(item)) {
file.getStream = item.stream()
file.length = item.size
} else if (Buffer.isBuffer(item)) {
} else if (ArrayBuffer.isView(item)) {
file.getStream = [item] // wrap in iterable to write entire buffer at once instead of unwrapping all bytes
file.length = item.length
} else if (isReadable(item)) {
Expand Down Expand Up @@ -231,11 +231,11 @@ async function getPieceList (files, pieceLength, estimatedTorrentLength, opts, c
hashedLength += chunk.length
if (onProgress) onProgress(hashedLength, estimatedTorrentLength)
resolve()
if (ended && remainingHashes === 0) cb(null, Buffer.from(pieces.join(''), 'hex'), length)
if (ended && remainingHashes === 0) cb(null, hex2arr(pieces.join('')), length)
})
})
}
if (remainingHashes === 0) return cb(null, Buffer.from(pieces.join(''), 'hex'), length)
if (remainingHashes === 0) return cb(null, hex2arr(pieces.join('')), length)
ended = true
} catch (err) {
cb(err)
Expand Down

0 comments on commit 2e030c7

Please sign in to comment.