Skip to content

Commit 5fab6e9

Browse files
authored
Merge pull request #17 from algorandfoundation/fix/msgpack-responses-fastify
fix: handles binary msgpack responses correctly
2 parents 605b0b5 + ed49ad9 commit 5fab6e9

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

resources/mock-server/src/server.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,23 @@ export async function startServer(
9191
return;
9292
}
9393

94-
const data = await response.text();
94+
// Handle binary msgpack responses correctly - use arrayBuffer() instead of text()
95+
// to avoid UTF-8 corruption of binary data
96+
const contentType = response.headers.get("content-type") || "";
97+
const isBinary = contentType.includes("msgpack") || contentType.includes("octet-stream");
98+
const data = isBinary
99+
? Buffer.from(await response.arrayBuffer())
100+
: await response.text();
95101

96102
fastify.log.debug(`[PollyJS] Response status: ${response.status}`);
97-
fastify.log.debug(`[PollyJS] Response size: ${data.length} bytes`);
103+
fastify.log.debug(`[PollyJS] Response content-type: ${contentType}`);
104+
fastify.log.debug(`[PollyJS] Response is binary: ${isBinary}`);
105+
fastify.log.debug(`[PollyJS] Response size: ${isBinary ? (data as Buffer).length : (data as string).length} bytes`);
98106

99-
// Log response preview (first 200 chars)
100-
const preview = data.length > 200 ? data.substring(0, 200) + "..." : data;
101-
fastify.log.debug(`[PollyJS] Response preview: ${preview}`);
107+
if (!isBinary) {
108+
const preview = (data as string).length > 200 ? (data as string).substring(0, 200) + "..." : data;
109+
fastify.log.debug(`[PollyJS] Response preview: ${preview}`);
110+
}
102111

103112
reply
104113
.code(response.status)

0 commit comments

Comments
 (0)