Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 23x 23x 23x 23x 23x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 8x 15x 23x 23x 23x 2x 2x 2x 2x 2x 23x 21x 21x 21x 21x 21x 21x 21x 21x 23x 5x 5x 5x 5x 5x 23x 23x 4x 4x 4x 4x 4x 23x 23x 1x | import { z } from "zod"
import type { EventKey } from "@workspace/shared/twitchEventSub"
export class ConvexActionError extends Error {
constructor(
readonly action: string,
readonly kind: "http" | "backend" | "timeout" | "network",
readonly status?: number,
readonly backendCode?: string
) {
super(
`Convex action ${action}: ${kind}${status ? ` HTTP ${status}` : ""}${backendCode ? ` (${backendCode})` : ""}`
)
}
}
function safeBackendCode(value: unknown): string | undefined {
const text = typeof value === "string" ? value : ""
if (text.includes("Unauthorized Twitch worker.")) return "unauthorizedWorker"
if (text.includes("Invalid event.")) return "invalidEvent"
if (text.includes("provider unavailable")) return "providerUnavailable"
return undefined
}
export class ConvexService {
constructor(
private readonly url: string,
private readonly secret: string,
private readonly request: typeof fetch = fetch,
private readonly signal?: AbortSignal
) {}
async reserve(
messageId: string,
key: EventKey,
broadcasterId: string,
eventJson?: string
) {
return z
.discriminatedUnion("kind", [
z.object({
kind: z.literal("pending"),
template: z.string().optional(),
}),
z.object({ kind: z.enum(["ignored", "terminal"]) }),
])
.parse(
await this.action("twitchEventSubActions:reserveEvent", {
messageId,
key,
broadcasterId,
eventJson,
})
)
}
async begin(messageId: string, attempt: string): Promise<boolean> {
return z
.boolean()
.parse(
await this.action("twitchEventSubActions:beginDispatch", {
messageId,
attempt,
})
)
}
async finish(
messageId: string,
attempt: string,
sent: boolean
): Promise<void> {
await this.action("twitchEventSubActions:finishDispatch", {
messageId,
attempt,
sent,
})
}
async pendingEvents(cursor?: string) {
return z
.object({
events: z.array(
z.object({
messageId: z.string(),
key: z.string(),
broadcasterId: z.string(),
eventJson: z.string(),
})
),
cursor: z.string().nullable(),
})
.parse(
await this.action("twitchEventSubActions:pendingEvents", { cursor })
)
}
async streamOnline(messageId: string, event: unknown): Promise<void> {
await this.action("liveNotificationActions:receiveOnline", {
messageId,
event,
})
}
async subscriptionState(
subscriptionId: string,
revoked: boolean,
key?: EventKey,
broadcasterId?: string
): Promise<void> {
await this.action("twitchEventSubActions:webhookState", {
subscriptionId,
revoked,
key,
broadcasterId,
})
}
private async action(
name: string,
args: Record<string, unknown>
): Promise<unknown> {
const deadline = AbortSignal.timeout(5000)
let response: Response
try {
response = await this.request(new URL("/api/action", this.url), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path: name,
args: { ...args, secret: this.secret },
format: "json",
}),
signal: this.signal
? AbortSignal.any([deadline, this.signal])
: deadline,
redirect: "error",
})
} catch {
throw new ConvexActionError(
name,
deadline.aborted ? "timeout" : "network"
)
}
const body: unknown = await response.json().catch(() => null)
const result = z
.object({
status: z.enum(["success", "error"]),
value: z.unknown().optional(),
errorMessage: z.string().optional(),
})
.safeParse(body)
if (!response.ok)
throw new ConvexActionError(
name,
"http",
response.status,
result.success ? safeBackendCode(result.data.errorMessage) : undefined
)
if (!result.success || result.data.status !== "success")
throw new ConvexActionError(
name,
"backend",
response.status,
result.success ? safeBackendCode(result.data.errorMessage) : undefined
)
return result.data.value
}
}
|