Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

chore: replace err-code with CodeError #116

Merged
merged 1 commit into from
Jan 13, 2023
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,13 @@
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-pubsub": "^3.0.0",
"@libp2p/interface-registrar": "^2.0.0",
"@libp2p/interfaces": "^3.0.2",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-collections": "^3.0.0",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/topology": "^4.0.0",
"@multiformats/multiaddr": "^11.0.0",
"abortable-iterator": "^4.0.2",
"err-code": "^3.0.1",
"it-length-prefixed": "^8.0.2",
"it-pipe": "^2.0.3",
"it-pushable": "^3.0.0",
Expand Down
36 changes: 18 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from '@libp2p/logger'
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { pipe } from 'it-pipe'
import Queue from 'p-queue'
import { createTopology } from '@libp2p/topology'
Expand Down Expand Up @@ -447,22 +447,22 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
switch (signaturePolicy) {
case 'StrictSign':
if (msg.type !== 'signed') {
throw errcode(new Error('Message type should be "signed" when signature policy is StrictSign but it was not'), codes.ERR_MISSING_SIGNATURE)
throw new CodeError('Message type should be "signed" when signature policy is StrictSign but it was not', codes.ERR_MISSING_SIGNATURE)
}

if (msg.sequenceNumber == null) {
throw errcode(new Error('Need seqno when signature policy is StrictSign but it was missing'), codes.ERR_MISSING_SEQNO)
throw new CodeError('Need seqno when signature policy is StrictSign but it was missing', codes.ERR_MISSING_SEQNO)
}

if (msg.key == null) {
throw errcode(new Error('Need key when signature policy is StrictSign but it was missing'), codes.ERR_MISSING_KEY)
throw new CodeError('Need key when signature policy is StrictSign but it was missing', codes.ERR_MISSING_KEY)
}

return msgId(msg.key, msg.sequenceNumber)
case 'StrictNoSign':
return noSignMsgId(msg.data)
default:
throw errcode(new Error('Cannot get message id: unhandled signature policy'), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
throw new CodeError('Cannot get message id: unhandled signature policy', codes.ERR_UNHANDLED_SIGNATURE_POLICY)
}
}

Expand Down Expand Up @@ -528,51 +528,51 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
switch (signaturePolicy) {
case 'StrictNoSign':
if (message.type !== 'unsigned') {
throw errcode(new Error('Message type should be "unsigned" when signature policy is StrictNoSign but it was not'), codes.ERR_MISSING_SIGNATURE)
throw new CodeError('Message type should be "unsigned" when signature policy is StrictNoSign but it was not', codes.ERR_MISSING_SIGNATURE)
}

// @ts-expect-error should not be present
if (message.signature != null) {
throw errcode(new Error('StrictNoSigning: signature should not be present'), codes.ERR_UNEXPECTED_SIGNATURE)
throw new CodeError('StrictNoSigning: signature should not be present', codes.ERR_UNEXPECTED_SIGNATURE)
}

// @ts-expect-error should not be present
if (message.key != null) {
throw errcode(new Error('StrictNoSigning: key should not be present'), codes.ERR_UNEXPECTED_KEY)
throw new CodeError('StrictNoSigning: key should not be present', codes.ERR_UNEXPECTED_KEY)
}

// @ts-expect-error should not be present
if (message.sequenceNumber != null) {
throw errcode(new Error('StrictNoSigning: seqno should not be present'), codes.ERR_UNEXPECTED_SEQNO)
throw new CodeError('StrictNoSigning: seqno should not be present', codes.ERR_UNEXPECTED_SEQNO)
}
break
case 'StrictSign':
if (message.type !== 'signed') {
throw errcode(new Error('Message type should be "signed" when signature policy is StrictSign but it was not'), codes.ERR_MISSING_SIGNATURE)
throw new CodeError('Message type should be "signed" when signature policy is StrictSign but it was not', codes.ERR_MISSING_SIGNATURE)
}

if (message.signature == null) {
throw errcode(new Error('StrictSigning: Signing required and no signature was present'), codes.ERR_MISSING_SIGNATURE)
throw new CodeError('StrictSigning: Signing required and no signature was present', codes.ERR_MISSING_SIGNATURE)
}

if (message.sequenceNumber == null) {
throw errcode(new Error('StrictSigning: Signing required and no sequenceNumber was present'), codes.ERR_MISSING_SEQNO)
throw new CodeError('StrictSigning: Signing required and no sequenceNumber was present', codes.ERR_MISSING_SEQNO)
}

if (!(await verifySignature(message, this.encodeMessage.bind(this)))) {
throw errcode(new Error('StrictSigning: Invalid message signature'), codes.ERR_INVALID_SIGNATURE)
throw new CodeError('StrictSigning: Invalid message signature', codes.ERR_INVALID_SIGNATURE)
}

break
default:
throw errcode(new Error('Cannot validate message: unhandled signature policy'), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
throw new CodeError('Cannot validate message: unhandled signature policy', codes.ERR_UNHANDLED_SIGNATURE_POLICY)
}

const validatorFn = this.topicValidators.get(message.topic)
if (validatorFn != null) {
const result = await validatorFn(from, message)
if (result === TopicValidatorResult.Reject || result === TopicValidatorResult.Ignore) {
throw errcode(new Error('Message validation failed'), codes.ERR_TOPIC_VALIDATOR_REJECT)
throw new CodeError('Message validation failed', codes.ERR_TOPIC_VALIDATOR_REJECT)
}
}
}
Expand All @@ -592,7 +592,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
...message
})
default:
throw errcode(new Error('Cannot build message: unhandled signature policy'), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
throw new CodeError('Cannot build message: unhandled signature policy', codes.ERR_UNHANDLED_SIGNATURE_POLICY)
}
}

Expand All @@ -603,11 +603,11 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
*/
getSubscribers (topic: string): PeerId[] {
if (!this.started) {
throw errcode(new Error('not started yet'), 'ERR_NOT_STARTED_YET')
throw new CodeError('not started yet', 'ERR_NOT_STARTED_YET')
}

if (topic == null) {
throw errcode(new Error('topic is required'), 'ERR_NOT_VALID_TOPIC')
throw new CodeError('topic is required', 'ERR_NOT_VALID_TOPIC')
}

const peersInTopic = this.topics.get(topic.toString())
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sha256 } from 'multiformats/hashes/sha2'
import type { Message, PubSubRPCMessage } from '@libp2p/interface-pubsub'
import { peerIdFromBytes } from '@libp2p/peer-id'
import { codes } from './errors.js'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'

/**
* Generate a random sequence number
Expand Down Expand Up @@ -68,7 +68,7 @@ export const ensureArray = function <T> (maybeArray: T | T[]) {

export const toMessage = (message: PubSubRPCMessage): Message => {
if (message.from == null) {
throw errcode(new Error('RPC message was missing from'), codes.ERR_MISSING_FROM)
throw new CodeError('RPC message was missing from', codes.ERR_MISSING_FROM)
}

if (message.sequenceNumber == null || message.from == null || message.signature == null || message.key == null) {
Expand Down