more robust message parsing

This commit is contained in:
ennmichael 2023-02-28 21:58:59 +01:00
parent 6565aa6fa0
commit a513408866
No known key found for this signature in database
GPG Key ID: 6E6E183431A26AF7

View File

@ -178,10 +178,10 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
if (!(json instanceof Array)) { if (!(json instanceof Array)) {
throw new ProtocolError(`incoming message is not an array: ${data}`) throw new ProtocolError(`incoming message is not an array: ${data}`)
} }
if (json.length === 3) { if (json.length === 0) {
if (json[0] !== "EVENT") { throw new ProtocolError(`incoming message is an empty array: ${data}`)
throw new ProtocolError(`expected "EVENT" message, but got: ${data}`) }
} if (json[0] === "EVENT") {
if (typeof json[1] !== "string") { if (typeof json[1] !== "string") {
throw new ProtocolError( throw new ProtocolError(
`second element of "EVENT" should be a string, but wasn't: ${data}` `second element of "EVENT" should be a string, but wasn't: ${data}`
@ -199,10 +199,8 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
signed: await SignedEvent.verify(raw), signed: await SignedEvent.verify(raw),
raw, raw,
} }
} else if (json.length === 2) { }
if (json[0] !== "NOTICE") { if (json[0] === "NOTICE") {
throw new ProtocolError(`expected "NOTICE" message, but got: ${data}`)
}
if (typeof json[1] !== "string") { if (typeof json[1] !== "string") {
throw new ProtocolError( throw new ProtocolError(
`second element of "NOTICE" should be a string, but wasn't: ${data}` `second element of "NOTICE" should be a string, but wasn't: ${data}`
@ -212,11 +210,8 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
kind: IncomingKind.Notice, kind: IncomingKind.Notice,
notice: json[1], notice: json[1],
} }
} else {
throw new ProtocolError(
`incoming message has unexpected number of elements: ${data}`
)
} }
throw new ProtocolError(`unknown incoming message: ${data}`)
} }
function serializeOutgoingMessage(msg: OutgoingMessage): string { function serializeOutgoingMessage(msg: OutgoingMessage): string {