@@ -13,7 +13,7 @@ export const SOCKET_FRAME_PARTS_QUERY_PARAM = "frameParts";
1313// link a multi-megabyte config snapshot outlasts the ping timeout, so the
1414// client drops a healthy server and asks for the same snapshot again. Parts
1515// this size keep a frame arriving within the timeout down to a few KB/s.
16- export const SOCKET_FRAME_PART_LENGTH = 64 * 1024 ;
16+ export const SOCKET_FRAME_PART_BYTES = 64 * 1024 ;
1717
1818// No JSON message starts with a record separator. It marks every part but the last.
1919const PART_PREFIX = "\u001e" ;
@@ -22,20 +22,27 @@ const PART_PREFIX = "\u001e";
2222// ignores pongs, so each held-back part reads to it as one.
2323const PART_RECEIVED_FRAME = '{"_tag":"Pong"}' ;
2424
25+ /** Splits on the UTF-8 size the frame has on the wire, never inside a surrogate pair. */
2526export function splitSocketFrame (
2627 frame : string ,
27- partLength = SOCKET_FRAME_PART_LENGTH ,
28+ partBytes = SOCKET_FRAME_PART_BYTES ,
2829) : Arr . NonEmptyReadonlyArray < string > {
29- if ( frame . length <= partLength ) return [ frame ] ;
30+ // UTF-8 spends at most 3 bytes on a UTF-16 code unit.
31+ if ( frame . length * 3 <= partBytes ) return [ frame ] ;
3032 const parts : Array < string > = [ ] ;
3133 let start = 0 ;
32- while ( frame . length - start > partLength ) {
33- let end = start + partLength ;
34- // A lone surrogate half does not survive the UTF-8 encoding of a text frame.
35- const last = frame . charCodeAt ( end - 1 ) ;
36- if ( last >= 0xd800 && last <= 0xdbff ) end -= 1 ;
37- parts . push ( PART_PREFIX + frame . slice ( start , end ) ) ;
38- start = end ;
34+ let bytes = PART_PREFIX . length ;
35+ for ( let index = 0 ; index < frame . length ; ) {
36+ const code = frame . charCodeAt ( index ) ;
37+ const pair = code >= 0xd800 && code <= 0xdbff && index + 1 < frame . length ;
38+ const size = pair ? 4 : code < 0x80 ? 1 : code < 0x800 ? 2 : 3 ;
39+ if ( bytes + size > partBytes ) {
40+ parts . push ( PART_PREFIX + frame . slice ( start , index ) ) ;
41+ start = index ;
42+ bytes = PART_PREFIX . length ;
43+ }
44+ bytes += size ;
45+ index += pair ? 2 : 1 ;
3946 }
4047 return Arr . append ( parts , frame . slice ( start ) ) ;
4148}
@@ -48,9 +55,7 @@ export function splitLargeSocketFrames(socket: Socket.Socket): Socket.Socket {
4855 reader : socket . reader ,
4956 writer : Effect . map ( socket . writer , ( writer ) => ( {
5057 write : ( chunk ) =>
51- typeof chunk === "string" && chunk . length > SOCKET_FRAME_PART_LENGTH
52- ? writer . writeAll ( splitSocketFrame ( chunk ) )
53- : writer . write ( chunk ) ,
58+ typeof chunk === "string" ? writer . writeAll ( splitSocketFrame ( chunk ) ) : writer . write ( chunk ) ,
5459 writeAll : ( chunks ) => writer . writeAll ( Arr . flatMap ( chunks , split ) ) ,
5560 } ) ) ,
5661 } ) ;
0 commit comments