Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: esm #43

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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