Line data Source code
1 : import { isCrdtDebugEnabled } from './crdtDebugGate'
2 : import { wireLog } from './crdtLog'
3 : import type { DocFrameTransport } from './docFrameClient'
4 :
5 : /**
6 : * Both of `wireLog.trace`'s sinks drop the detail unless the debug instrument
7 : * is on, so an ungated parse costs every user edit a full `JSON.parse` of the
8 : * `doc_ops` batch `DocFrameClient.send` just serialised, on the main thread,
9 : * for a value nothing reads.
10 : */
11 0 : function traceableFrame(frame: string): Record<string, unknown> | null {
12 0 : if (!isCrdtDebugEnabled()) return null
13 0 : try {
14 0 : const parsed: unknown = JSON.parse(frame)
15 0 : return parsed !== null &&
16 : typeof parsed === 'object' &&
17 : !Array.isArray(parsed)
18 : ? (parsed as Record<string, unknown>)
19 : : null
20 : } catch {
21 0 : return null
22 : }
23 : }
24 :
25 1 : export function createLoggedTransport(
26 : base: DocFrameTransport
27 : ): DocFrameTransport {
28 1 : return {
29 : send(frame) {
30 0 : const delivered = base.send(frame)
31 0 : const parsed = traceableFrame(frame)
32 0 : wireLog.trace('ws_out', 'outbound frame', {
33 : delivered,
34 : frame: parsed,
35 : ...(parsed === null ? { unparsed_chars: frame.length } : {})
36 : })
37 0 : return delivered
38 : },
39 : addEventListener(type, listener) {
40 6 : base.addEventListener(type, listener)
41 : },
42 : removeEventListener(type, listener) {
43 0 : base.removeEventListener(type, listener)
44 : }
45 : }
46 : }
|