Skip to content

Commit

Permalink
add resilience to non-ASCII characters
Browse files Browse the repository at this point in the history
  • Loading branch information
darkgnotic committed Jan 3, 2025
1 parent bf69fe2 commit 0b2a2a5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/zero-cache/src/services/dispatcher/websocket-handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ export function installWebSocketHandoff<P>(

// close messages must be less than or equal to 123 bytes:
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close#reason
function truncate(val: string, maxLen = 123) {
return val.length <= maxLen ? val : val.substring(0, maxLen - 3) + '...';
function truncate(val: string, maxBytes = 123) {
const encoder = new TextEncoder();
if (encoder.encode(val).length <= maxBytes) {
return val;
}
val = val.substring(0, maxBytes - 3);
while (encoder.encode(val).length > maxBytes - 3) {
val.substring(0, val.length - 1);
}
return val + '...';
}

export function installWebSocketReceiver<P>(
Expand Down

0 comments on commit 0b2a2a5

Please sign in to comment.