MCPcopy Create free account
hub / github.com/anomalyco/opencode / makeWebSocketExecutor

Function makeWebSocketExecutor

packages/http-recorder/src/websocket.ts:64–173  ·  view source on GitHub ↗
(
  options: WebSocketRecordReplayOptions<E>,
)

Source from the content-addressed store, hash-verified

62 })
63
64export 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

Callers

nothing calls this directly

Calls 15

resolveAutoModeFunction · 0.85
textEventFunction · 0.85
makeReplayStateFunction · 0.85
canonicalizeJsonFunction · 0.85
safeTextFunction · 0.85
assertClientEventFunction · 0.85
openMethod · 0.80
syncMethod · 0.80
pushMethod · 0.80
asMethod · 0.80
makeFunction · 0.70
redactEventFunction · 0.70

Tested by

no test coverage detected