42 lines
962 B
TypeScript
42 lines
962 B
TypeScript
import { ProtocolError } from "./error"
|
|
|
|
/**
|
|
* Raw event to be transferred over the wire.
|
|
*/
|
|
export interface RawEvent {
|
|
id: string
|
|
pubkey: string
|
|
created_at: number
|
|
kind: number
|
|
tags: string[][]
|
|
content: string
|
|
sig: string
|
|
}
|
|
|
|
export function parseRawEvent(data: string): RawEvent {
|
|
const json = parseJson(data)
|
|
if (
|
|
typeof json["id"] !== "string" ||
|
|
typeof json["pubkey"] !== "string" ||
|
|
typeof json["created_at"] !== "number" ||
|
|
typeof json["kind"] !== "number" ||
|
|
!(json["tags"] instanceof Array) ||
|
|
!json["tags"].every(
|
|
(x) => x instanceof Array && x.every((y) => typeof y === "string")
|
|
) ||
|
|
typeof json["content"] !== "string" ||
|
|
typeof json["sig"] !== "string"
|
|
) {
|
|
throw new ProtocolError(`invalid event: ${data}`)
|
|
}
|
|
return json
|
|
}
|
|
|
|
function parseJson(data: string) {
|
|
try {
|
|
return JSON.parse(data)
|
|
} catch (e) {
|
|
throw new ProtocolError(`invalid event json: ${data}`)
|
|
}
|
|
}
|