( options: WebSocketRecordReplayOptions<E>, )
| 62 | }) |
| 63 | |
| 64 | export const makeWebSocketExecutor = <E>( |
| 65 | options: WebSocketRecordReplayOptions<E>, |
| 66 | ): Effect.Effect<WebSocketExecutor<E>, never, Scope.Scope> => |
| 67 | Effect.gen(function* () { |
| 68 | const mode = options.mode ?? (yield* resolveAutoMode(options.cassette, options.name)) |
| 69 | const redactor = options.redactor ?? make() |
| 70 | const openSnapshot = (request: WebSocketRequest) => { |
| 71 | const snapshot = redactor.request({ |
| 72 | method: "GET", |
| 73 | url: request.url, |
| 74 | headers: headersRecord(request.headers), |
| 75 | body: "", |
| 76 | }) |
| 77 | return { url: snapshot.url, headers: snapshot.headers } |
| 78 | } |
| 79 | const redactEvent = (event: WebSocketEvent) => { |
| 80 | if (event.kind === "binary") return event |
| 81 | const body = |
| 82 | event.direction === "client" |
| 83 | ? redactor.request({ method: "WEBSOCKET", url: "", headers: {}, body: event.body }).body |
| 84 | : redactor.response({ status: 101, headers: {}, body: event.body }).body |
| 85 | return { ...event, body } |
| 86 | } |
| 87 | |
| 88 | if (mode === "passthrough") return options.live |
| 89 | |
| 90 | if (mode === "record") { |
| 91 | return { |
| 92 | open: (request) => |
| 93 | Effect.gen(function* () { |
| 94 | const events: WebSocketEvent[] = [] |
| 95 | const connection = yield* options.live.open(request) |
| 96 | const closed = yield* Ref.make(false) |
| 97 | const closeLock = yield* Semaphore.make(1) |
| 98 | return { |
| 99 | sendText: (message) => |
| 100 | Effect.sync(() => events.push(redactEvent(textEvent("client", message)))).pipe( |
| 101 | Effect.andThen(connection.sendText(message)), |
| 102 | ), |
| 103 | messages: connection.messages.pipe( |
| 104 | Stream.tap((message) => |
| 105 | Effect.sync(() => |
| 106 | events.push( |
| 107 | typeof message === "string" |
| 108 | ? redactEvent(textEvent("server", message)) |
| 109 | : { |
| 110 | direction: "server", |
| 111 | kind: "binary", |
| 112 | body: Buffer.from(message).toString("base64"), |
| 113 | bodyEncoding: "base64", |
| 114 | }, |
| 115 | ), |
| 116 | ), |
| 117 | ), |
| 118 | ), |
| 119 | close: closeLock.withPermit( |
| 120 | Effect.gen(function* () { |
| 121 | if (yield* Ref.get(closed)) return |
nothing calls this directly
no test coverage detected