Skip to content

Commit 9be8420

Browse files
committed
fix: media handler hardening and chaos injection
- elevenlabs-audio: add applyChaos integration, flattenHeaders in journal entries, serializeErrorResponse, proxy outcome fix - fal-audio: add applyChaos integration, FalJobMap background sweep with destroy(), flattenHeaders in journal, serializeErrorResponse, null result guard (409), move incrementFixtureMatchCount before chaos eval, proxy outcome fix - fal: serializeErrorResponse, proxy outcome consistency - speech: serializeErrorResponse, handled_by_hook return - transcription: boundary-based multipart parsing instead of regex-over-binary, serializeErrorResponse, handled_by_hook - video: VideoStateMap background sweep with destroy(), getEntry/getCreatedAtUnix for stable timestamps, serializeErrorResponse, handled_by_hook - images/embeddings: serializeErrorResponse, handled_by_hook
1 parent fd1113a commit 9be8420

8 files changed

Lines changed: 269 additions & 66 deletions

File tree

src/elevenlabs-audio.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
isAudioResponse,
55
isTextResponse,
66
isErrorResponse,
7+
serializeErrorResponse,
8+
flattenHeaders,
79
FORMAT_TO_CONTENT_TYPE,
810
getTestId,
911
resolveResponse,
@@ -14,6 +16,7 @@ import { matchFixture } from "./router.js";
1416
import { writeErrorResponse } from "./sse-writer.js";
1517
import { proxyAndRecord } from "./recorder.js";
1618
import type { Journal } from "./journal.js";
19+
import { applyChaos } from "./chaos.js";
1720

1821
export async function handleElevenLabsAudio(
1922
req: http.IncomingMessage,
@@ -36,7 +39,7 @@ export async function handleElevenLabsAudio(
3639
journal.add({
3740
method,
3841
path,
39-
headers: {},
42+
headers: flattenHeaders(req.headers),
4043
body: null,
4144
response: { status: 400, fixture: null },
4245
});
@@ -87,7 +90,7 @@ export async function handleElevenLabsAudio(
8790
journal.add({
8891
method,
8992
path,
90-
headers: {},
93+
headers: flattenHeaders(req.headers),
9194
body: syntheticReq,
9295
response: { status: 400, fixture: null },
9396
});
@@ -113,6 +116,21 @@ export async function handleElevenLabsAudio(
113116
journal.incrementFixtureMatchCount(fixture, fixtures, testId);
114117
}
115118

119+
if (
120+
applyChaos(
121+
res,
122+
fixture,
123+
defaults.chaos,
124+
req.headers,
125+
journal,
126+
{ method, path, headers: flattenHeaders(req.headers), body: syntheticReq },
127+
fixture ? "fixture" : "proxy",
128+
defaults.registry,
129+
defaults.logger,
130+
)
131+
)
132+
return;
133+
116134
// No fixture match
117135
if (!fixture) {
118136
if (defaults.record) {
@@ -127,11 +145,11 @@ export async function handleElevenLabsAudio(
127145
body,
128146
);
129147
if (outcome === "handled_by_hook") return;
130-
if (outcome === "relayed") {
148+
if (outcome !== "not_configured") {
131149
journal.add({
132150
method,
133151
path,
134-
headers: {},
152+
headers: flattenHeaders(req.headers),
135153
body: syntheticReq,
136154
response: { status: res.statusCode ?? 200, fixture: null, source: "proxy" },
137155
});
@@ -147,7 +165,7 @@ export async function handleElevenLabsAudio(
147165
journal.add({
148166
method,
149167
path,
150-
headers: {},
168+
headers: flattenHeaders(req.headers),
151169
body: syntheticReq,
152170
response: {
153171
status: strictStatus,
@@ -173,11 +191,11 @@ export async function handleElevenLabsAudio(
173191
journal.add({
174192
method,
175193
path,
176-
headers: {},
194+
headers: flattenHeaders(req.headers),
177195
body: syntheticReq,
178196
response: { status, fixture },
179197
});
180-
writeErrorResponse(res, status, JSON.stringify(response));
198+
writeErrorResponse(res, status, serializeErrorResponse(response));
181199
return;
182200
}
183201

@@ -187,7 +205,7 @@ export async function handleElevenLabsAudio(
187205
journal.add({
188206
method,
189207
path,
190-
headers: {},
208+
headers: flattenHeaders(req.headers),
191209
body: syntheticReq,
192210
response: { status: 500, fixture },
193211
});
@@ -206,7 +224,7 @@ export async function handleElevenLabsAudio(
206224
journal.add({
207225
method,
208226
path,
209-
headers: {},
227+
headers: flattenHeaders(req.headers),
210228
body: syntheticReq,
211229
response: { status: 200, fixture },
212230
});
@@ -220,7 +238,7 @@ export async function handleElevenLabsAudio(
220238
journal.add({
221239
method,
222240
path,
223-
headers: {},
241+
headers: flattenHeaders(req.headers),
224242
body: syntheticReq,
225243
response: { status: 500, fixture },
226244
});
@@ -257,7 +275,7 @@ export async function handleElevenLabsAudio(
257275
journal.add({
258276
method,
259277
path,
260-
headers: {},
278+
headers: flattenHeaders(req.headers),
261279
body: syntheticReq,
262280
response: { status: 200, fixture },
263281
});
@@ -273,7 +291,7 @@ export async function handleElevenLabsAudio(
273291
journal.add({
274292
method,
275293
path,
276-
headers: {},
294+
headers: flattenHeaders(req.headers),
277295
body: syntheticReq,
278296
response: { status: 200, fixture },
279297
});

src/embeddings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
import {
1717
isEmbeddingResponse,
1818
isErrorResponse,
19+
serializeErrorResponse,
1920
generateDeterministicEmbedding,
2021
buildEmbeddingResponse,
2122
flattenHeaders,
@@ -168,7 +169,7 @@ export async function handleEmbeddings(
168169
body: syntheticReq,
169170
response: { status, fixture },
170171
});
171-
writeErrorResponse(res, status, JSON.stringify(response));
172+
writeErrorResponse(res, status, serializeErrorResponse(response));
172173
return;
173174
}
174175

@@ -222,6 +223,7 @@ export async function handleEmbeddings(
222223
defaults,
223224
raw,
224225
);
226+
if (outcome === "handled_by_hook") return;
225227
if (outcome !== "not_configured") {
226228
journal.add({
227229
method: req.method ?? "POST",

0 commit comments

Comments
 (0)