File size: 6,965 Bytes
3e05655 | 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 162 163 164 165 166 167 168 169 170 171 172 173 174 | import { Effect, Option, Ref, Scope, Semaphore, Stream, SynchronizedRef } from "effect"
import type { Headers } from "effect/unstable/http"
import * as CassetteService from "./cassette.js"
import { canonicalizeJson, decodeJson, safeText } from "./matching.js"
import { makeReplayState, resolveAutoMode } from "./recorder.js"
import type { RecordReplayMode } from "./internal-effect.js"
import { make, type Redactor } from "./redactor.js"
import { webSocketInteractions, type CassetteMetadata, type WebSocketEvent } from "./schema.js"
export interface WebSocketRequest {
readonly url: string
readonly headers: Headers.Headers
}
export interface WebSocketConnection<E> {
readonly sendText: (message: string) => Effect.Effect<void, E>
readonly messages: Stream.Stream<string | Uint8Array, E>
readonly close: Effect.Effect<void>
}
export interface WebSocketExecutor<E> {
readonly open: (request: WebSocketRequest) => Effect.Effect<WebSocketConnection<E>, E>
}
export interface WebSocketRecordReplayOptions<E> {
readonly name: string
readonly mode?: RecordReplayMode
readonly metadata?: CassetteMetadata
readonly cassette: CassetteService.Interface
readonly live: WebSocketExecutor<E>
readonly redactor?: Redactor
readonly compareClientMessagesAsJson?: boolean
}
const headersRecord = (headers: Headers.Headers): Record<string, string> =>
Object.fromEntries(
Object.entries(headers as Record<string, unknown>).filter(
(entry): entry is [string, string] => typeof entry[1] === "string",
),
)
const textEvent = (direction: "client" | "server", body: string): WebSocketEvent => ({
direction,
kind: "text",
body,
})
const decodeEvent = (event: WebSocketEvent) =>
event.kind === "text" ? event.body : new Uint8Array(Buffer.from(event.body, "base64"))
const jsonOrText = (value: string) => Option.match(decodeJson(value), { onNone: () => value, onSome: canonicalizeJson })
const assertClientEvent = (actual: string, expected: WebSocketEvent | undefined, index: number, asJson: boolean) =>
Effect.sync(() => {
const matches =
expected?.direction === "client" &&
expected.kind === "text" &&
JSON.stringify(asJson ? jsonOrText(actual) : actual) ===
JSON.stringify(asJson ? jsonOrText(expected.body) : expected.body)
if (matches) return
throw new Error(`WebSocket client frame ${index + 1}: expected ${safeText(expected)}, received ${safeText(actual)}`)
})
export const makeWebSocketExecutor = <E>(
options: WebSocketRecordReplayOptions<E>,
): Effect.Effect<WebSocketExecutor<E>, never, Scope.Scope> =>
Effect.gen(function* () {
const mode = options.mode ?? (yield* resolveAutoMode(options.cassette, options.name))
const redactor = options.redactor ?? make()
const openSnapshot = (request: WebSocketRequest) => {
const snapshot = redactor.request({
method: "GET",
url: request.url,
headers: headersRecord(request.headers),
body: "",
})
return { url: snapshot.url, headers: snapshot.headers }
}
const redactEvent = (event: WebSocketEvent) => {
if (event.kind === "binary") return event
const body =
event.direction === "client"
? redactor.request({ method: "WEBSOCKET", url: "", headers: {}, body: event.body }).body
: redactor.response({ status: 101, headers: {}, body: event.body }).body
return { ...event, body }
}
if (mode === "passthrough") return options.live
if (mode === "record") {
return {
open: (request) =>
Effect.gen(function* () {
const events: WebSocketEvent[] = []
const connection = yield* options.live.open(request)
const closed = yield* Ref.make(false)
const closeLock = yield* Semaphore.make(1)
return {
sendText: (message) =>
Effect.sync(() => events.push(redactEvent(textEvent("client", message)))).pipe(
Effect.andThen(connection.sendText(message)),
),
messages: connection.messages.pipe(
Stream.tap((message) =>
Effect.sync(() =>
events.push(
typeof message === "string"
? redactEvent(textEvent("server", message))
: {
direction: "server",
kind: "binary",
body: Buffer.from(message).toString("base64"),
bodyEncoding: "base64",
},
),
),
),
),
close: closeLock.withPermit(
Effect.gen(function* () {
if (yield* Ref.get(closed)) return
yield* connection.close
yield* options.cassette
.append(
options.name,
{ transport: "websocket", open: openSnapshot(request), events },
options.metadata,
)
.pipe(Effect.orDie)
yield* Ref.set(closed, true)
}),
),
}
}),
}
}
const replay = yield* makeReplayState(options.cassette, options.name, webSocketInteractions)
return {
open: (request) =>
Effect.gen(function* () {
const claimed = yield* replay
.claim((interaction, index) =>
Effect.sync(() => {
const incoming = canonicalizeJson(openSnapshot(request))
if (interaction && JSON.stringify(incoming) === JSON.stringify(canonicalizeJson(interaction.open)))
return
throw new Error(`WebSocket open ${index + 1} does not match ${safeText(incoming)}`)
}),
)
.pipe(Effect.orDie)
const client = claimed.interaction.events.filter((event) => event.direction === "client")
const server = claimed.interaction.events.filter((event) => event.direction === "server")
const position = yield* SynchronizedRef.make(0)
return {
sendText: (message) =>
SynchronizedRef.updateEffect(position, (index) =>
assertClientEvent(message, client[index], index, options.compareClientMessagesAsJson === true).pipe(
Effect.as(index + 1),
),
),
messages: Stream.fromIterable(server).pipe(Stream.map(decodeEvent)),
close: Effect.gen(function* () {
const used = yield* SynchronizedRef.get(position)
if (used !== client.length)
return yield* Effect.die(
new Error(`WebSocket client frame count: expected ${client.length}, received ${used}`),
)
}),
}
}),
}
})
|