Skip to content

Commit

Permalink
Merge pull request #43 from ThaUnknown/esm
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ESM only

feat: esm
  • Loading branch information
ThaUnknown authored Dec 5, 2022
2 parents 0753ae5 + f806ff9 commit 14e51df
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 166 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install bittorrent-peerid
## usage

```js
const peerid = require('bittorrent-peerid')
import peerid from 'bittorrent-peerid'
const parsed = peerid('-AZ2200-6wfG2wk6wWLc')

console.log(parsed.client, parsed.version)
Expand Down
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*! bittorrent-peerid. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
const utils = require('./lib/utils')
import { decodeBitCometClient, decodeBitSpiritClient, getAzStyleVersionNumber, identifyAwkwardClient, isAzStyle, isMainlineStyle, isPossibleSpoofClient, isShadowStyle } from './lib/utils.js'

/**
* Parses and returns the client type and version of a bittorrent peer id.
* Throws an exception if the peer id is invalid.
*
* @param {Buffer|string} peerId (as Buffer or hex/utf8 string)
*/
module.exports = peerId => {
export default (peerId) => {
let buffer

if (Buffer.isBuffer(peerId)) {
Expand All @@ -32,14 +32,14 @@ module.exports = peerId => {

// If the client reuses parts of the peer ID of other peers, then try to determine this
// first (before we misidentify the client).
if (utils.isPossibleSpoofClient(peerId)) {
if ((client = utils.decodeBitSpiritClient(peerId, buffer))) return client
if ((client = utils.decodeBitCometClient(peerId, buffer))) return client
if (isPossibleSpoofClient(peerId)) {
if ((client = decodeBitSpiritClient(peerId, buffer))) return client
if ((client = decodeBitCometClient(peerId, buffer))) return client
return { client: 'BitSpirit?' }
}

// See if the client uses Az style identification
if (utils.isAzStyle(peerId)) {
if (isAzStyle(peerId)) {
if ((client = getAzStyleClientName(peerId))) {
const version = getAzStyleClientVersion(client, peerId)

Expand Down Expand Up @@ -76,24 +76,24 @@ module.exports = peerId => {
}

// See if the client uses Shadow style identification
if (utils.isShadowStyle(peerId)) {
if (isShadowStyle(peerId)) {
if ((client = getShadowStyleClientName(peerId))) {
// TODO: handle shadow style client version numbers
return { client }
}
}

// See if the client uses Mainline style identification
if (utils.isMainlineStyle(peerId)) {
if (isMainlineStyle(peerId)) {
if ((client = getMainlineStyleClientName(peerId))) {
// TODO: handle mainline style client version numbers
return { client }
}
}

// Check for BitSpirit / BitComet disregarding spoof mode
if ((client = utils.decodeBitSpiritClient(peerId, buffer))) return client
if ((client = utils.decodeBitCometClient(peerId, buffer))) return client
if ((client = decodeBitSpiritClient(peerId, buffer))) return client
if ((client = decodeBitCometClient(peerId, buffer))) return client

// See if the client identifies itself using a particular substring
const data = getSimpleClient(peerId)
Expand All @@ -108,7 +108,7 @@ module.exports = peerId => {
}

// See if client is known to be awkward / nonstandard
if ((client = utils.identifyAwkwardClient(peerId, buffer))) {
if ((client = identifyAwkwardClient(peerId, buffer))) {
return client
}

Expand Down Expand Up @@ -242,7 +242,7 @@ function getAzStyleClientVersion (client, peerId) {
const version = azStyleClientVersions[client]
if (!version) return null

return utils.getAzStyleVersionNumber(peerId.substring(3, 7), version)
return getAzStyleVersionNumber(peerId.substring(3, 7), version)
}

(() => {
Expand Down
Loading

0 comments on commit 14e51df

Please sign in to comment.