Skip to content

Commit

Permalink
perf: drop rusha, buffer (#79) [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown authored Dec 18, 2022
1 parent 956c8f2 commit 92ab67a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventEmitter } from 'events'
import bencode from 'bencode'
import BitField from 'bitfield'
import Debug from 'debug'
import sha1 from 'simple-sha1'
import { hash, arr2text, concat } from 'uint8-util'

const debug = Debug('ut_metadata')

Expand All @@ -29,7 +29,7 @@ export default metadata => {
// malicious peer can't make it grow to fill all memory.
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })

if (Buffer.isBuffer(metadata)) {
if (ArrayBuffer.isView(metadata)) {
this.setMetadata(metadata)
}
}
Expand Down Expand Up @@ -62,7 +62,7 @@ export default metadata => {
let dict
let trailer
try {
const str = buf.toString()
const str = arr2text(buf)
const trailerIndex = str.indexOf('ee') + 2
dict = bencode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex)
Expand Down Expand Up @@ -112,7 +112,7 @@ export default metadata => {
this._fetching = false
}

setMetadata (metadata) {
async setMetadata (metadata) {
if (this._metadataComplete) return true
debug('set metadata')

Expand All @@ -125,7 +125,7 @@ export default metadata => {
} catch (err) {}

// check hash
if (this._infoHash && this._infoHash !== sha1.sync(metadata)) {
if (this._infoHash && this._infoHash !== await hash(metadata, 'hex')) {
return false
}

Expand All @@ -145,8 +145,8 @@ export default metadata => {

_send (dict, trailer) {
let buf = bencode.encode(dict)
if (Buffer.isBuffer(trailer)) {
buf = Buffer.concat([buf, trailer])
if (ArrayBuffer.isView(trailer)) {
buf = concat([buf, trailer])
}
this._wire.extended('ut_metadata', buf)
}
Expand Down Expand Up @@ -185,7 +185,7 @@ export default metadata => {
if (buf.length > PIECE_LENGTH || !this._fetching) {
return
}
buf.copy(this.metadata, piece * PIECE_LENGTH)
this.metadata.set(buf, piece * PIECE_LENGTH)
this._bitfield.set(piece)
this._checkDone()
}
Expand All @@ -203,13 +203,13 @@ export default metadata => {

_requestPieces () {
if (!this._fetching) return
this.metadata = Buffer.alloc(this._metadataSize)
this.metadata = new Uint8Array(this._metadataSize)
for (let piece = 0; piece < this._numPieces; piece++) {
this._request(piece)
}
}

_checkDone () {
async _checkDone () {
let done = true
for (let piece = 0; piece < this._numPieces; piece++) {
if (!this._bitfield.get(piece)) {
Expand All @@ -220,7 +220,7 @@ export default metadata => {
if (!done) return

// attempt to set metadata -- may fail sha1 check
const success = this.setMetadata(this.metadata)
const success = await this.setMetadata(this.metadata)

if (!success) {
this._failedMetadata()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"bencode": "^3.0.0",
"bitfield": "^4.0.0",
"debug": "^4.2.0",
"simple-sha1": "^3.0.1"
"uint8-util": "^2.1.3"
},
"devDependencies": {
"@webtorrent/semantic-release-config": "1.0.8",
Expand Down

0 comments on commit 92ab67a

Please sign in to comment.