escape json if needed

This commit is contained in:
Zack Radisic
2025-09-10 19:18:40 -07:00
parent 24b2929c9a
commit 41fbeacee1

View File

@@ -210,8 +210,30 @@ pub const Entry = struct {
return bun.strings.percentEncodeWrite(utf8_input, writer);
}
// On the server, we don't need to do anything
try writer.appendSlice(utf8_input);
// On the server, escape special characters for JSON
var remaining = utf8_input;
while (remaining.len > 0) {
if (std.mem.indexOfAny(u8, remaining, "\"\\\n\r\t")) |index| {
// Write everything before the special character
if (index > 0) {
try writer.appendSlice(remaining[0..index]);
}
// Write the escaped character
switch (remaining[index]) {
'"' => try writer.appendSlice("\\\""),
'\\' => try writer.appendSlice("\\\\"),
'\n' => try writer.appendSlice("\\n"),
'\r' => try writer.appendSlice("\\r"),
'\t' => try writer.appendSlice("\\t"),
else => unreachable,
}
remaining = remaining[index + 1 ..];
} else {
// No special characters found, write the rest
try writer.appendSlice(remaining);
break;
}
}
}
fn joinVLQ(map: *const Entry, kind: ChunkKind, j: *StringJoiner, arena: Allocator, side: bake.Side) !void {