(packet: Packet)
| 58 | * trim trailing ones only when strictly safe (no data and no endpoint). |
| 59 | */ |
| 60 | export function encodeFrame(packet: Packet): string { |
| 61 | const typePart = String(packet.type); |
| 62 | const idPart = packet.id === undefined ? '' : String(packet.id); |
| 63 | const endpointPart = packet.endpoint ?? ''; |
| 64 | const dataPart = packet.data ?? ''; |
| 65 | |
| 66 | // Always emit at least `<type>:<id>:<endpoint>`. Append `:<data>` only if |
| 67 | // data is present — trailing empty segment is legal but noisy, and the |
| 68 | // reference parser is happy with either form. |
| 69 | if (dataPart === '') { |
| 70 | return `${typePart}:${idPart}:${endpointPart}`; |
| 71 | } |
| 72 | return `${typePart}:${idPart}:${endpointPart}:${dataPart}`; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Decode a raw wire string into a Packet, or return null for anything |
no outgoing calls
no test coverage detected