|
| 1 | +import { deflateRawSync } from 'node:zlib'; |
| 2 | +import { FrameCodec, frameCodecFromDictionary, FrameCodecRaw, FrameCodecCompressed } from './frame_codec'; |
| 3 | +import { DictionaryCache } from './dictionary_cache'; |
| 4 | + |
| 5 | +// frame_codec is what every compressed frame passes through, so these cover the |
| 6 | +// two things a wrong answer costs: a frame decoded against the wrong bytes is |
| 7 | +// not an error, it is silently different content, and a frame trusted for its |
| 8 | +// size can be a small input that expands into a large allocation. |
| 9 | +describe('frame codec', () => { |
| 10 | + const dict = new TextEncoder().encode('{"push":{"channel":"demo","pub":{"data":{"event":"price.changed"'); |
| 11 | + const payload = '{"push":{"channel":"demo","pub":{"data":{"event":"price.changed","v":1}}}}'; |
| 12 | + |
| 13 | + // What a server puts on the wire: a marker byte, then raw DEFLATE against the |
| 14 | + // shared dictionary. |
| 15 | + const compressedFrame = (text: string, against: Uint8Array = dict) => |
| 16 | + new Uint8Array([FrameCodecCompressed, ...deflateRawSync(Buffer.from(text), { dictionary: Buffer.from(against) })]); |
| 17 | + const rawFrame = (text: string) => |
| 18 | + new Uint8Array([FrameCodecRaw, ...new TextEncoder().encode(text)]); |
| 19 | + |
| 20 | + it('decodes a frame compressed against its dictionary', () => { |
| 21 | + const c = new FrameCodec('id', dict); |
| 22 | + expect(c.decode(compressedFrame(payload), true)).toEqual(payload); |
| 23 | + }); |
| 24 | + |
| 25 | + it('passes a raw frame through untouched', () => { |
| 26 | + // The server declines to compress whatever would not shrink, so every |
| 27 | + // client has to read this marker even on a compressed connection. |
| 28 | + const c = new FrameCodec('id', dict); |
| 29 | + expect(c.decode(rawFrame(payload), true)).toEqual(payload); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns bytes rather than text on a binary connection', () => { |
| 33 | + const c = new FrameCodec('id', dict); |
| 34 | + const out = c.decode(compressedFrame(payload), false); |
| 35 | + expect(out).toBeInstanceOf(Uint8Array); |
| 36 | + expect(new TextDecoder().decode(out)).toEqual(payload); |
| 37 | + }); |
| 38 | + |
| 39 | + it('refuses a marker it does not know', () => { |
| 40 | + const c = new FrameCodec('id', dict); |
| 41 | + expect(() => c.decode(new Uint8Array([0x7f, 1, 2, 3]), true)).toThrow(/unknown frame codec/); |
| 42 | + }); |
| 43 | + |
| 44 | + it('refuses an empty frame', () => { |
| 45 | + const c = new FrameCodec('id', dict); |
| 46 | + expect(() => c.decode(new Uint8Array([]), true)).toThrow(/empty frame/); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not reproduce the payload when the dictionary differs', () => { |
| 50 | + // Compressed against the real dictionary, decoded against other bytes. The |
| 51 | + // frame has to reference the dictionary for this to diverge at all - a |
| 52 | + // payload with nothing in common with it compresses to literals alone and |
| 53 | + // decodes identically whatever dictionary is installed, which is the same |
| 54 | + // property that makes a substituted dictionary able to rewrite content: |
| 55 | + // what changes is whatever the back references point at. |
| 56 | + const other = new TextEncoder().encode('unrelated bytes entirely, nothing in common at all'); |
| 57 | + const c = new FrameCodec('id', other); |
| 58 | + let out: any; |
| 59 | + try { |
| 60 | + out = c.decode(compressedFrame(payload, dict), true); |
| 61 | + } catch (e) { |
| 62 | + return; // rejected outright, which is the good case |
| 63 | + } |
| 64 | + expect(out).not.toEqual(payload); |
| 65 | + }); |
| 66 | + |
| 67 | + it('refuses a frame that expands past the limit', () => { |
| 68 | + // A few kilobytes of zeros expand to more than the ceiling. Nothing legitimate |
| 69 | + // reaches it, so the only sender that gets here is one trying to make the |
| 70 | + // client allocate on command. |
| 71 | + const bomb = new Uint8Array([FrameCodecCompressed, |
| 72 | + ...deflateRawSync(Buffer.alloc(17 * 1024 * 1024), { dictionary: Buffer.from(dict) })]); |
| 73 | + const c = new FrameCodec('id', dict); |
| 74 | + expect(() => c.decode(bomb, true)).toThrow(/too large/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('counts what it received and what it expanded to', () => { |
| 78 | + const c = new FrameCodec('id', dict); |
| 79 | + const frame = compressedFrame(payload); |
| 80 | + c.decode(frame, true); |
| 81 | + const s = c.getStats(); |
| 82 | + expect(s.frames).toEqual(1); |
| 83 | + expect(s.bytesReceived).toEqual(frame.length); |
| 84 | + expect(s.bytesDecompressed).toEqual(payload.length); |
| 85 | + expect(s.bytesDecompressed).toBeGreaterThan(s.bytesReceived); // it did compress |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('building a codec from a connect reply dictionary', () => { |
| 90 | + // Repetitive, like a real dictionary built from sampled traffic - a few dozen |
| 91 | + // bytes would not compress, which would make the size assertions vacuous. |
| 92 | + const dict = new TextEncoder().encode( |
| 93 | + '{"push":{"channel":"demo","pub":{"data":{"event":"price.changed","symbol":"","venue":"NASDAQ"'.repeat(20)); |
| 94 | + const packed = new Uint8Array(deflateRawSync(Buffer.from(dict))); |
| 95 | + |
| 96 | + it('takes raw bytes, as a Protobuf connection carries them', () => { |
| 97 | + const c = frameCodecFromDictionary({ id: 'abc', data: packed }); |
| 98 | + expect(c).not.toBeNull(); |
| 99 | + expect(c!.id).toEqual('abc'); |
| 100 | + expect(c!.dictionary).toEqual(dict); |
| 101 | + }); |
| 102 | + |
| 103 | + it('takes base64, as a JSON connection carries them', () => { |
| 104 | + // A bytes field holds raw JSON on a JSON connection, so it cannot hold |
| 105 | + // binary - the same dictionary arrives base64 encoded instead. |
| 106 | + const b64 = Buffer.from(packed).toString('base64'); |
| 107 | + const c = frameCodecFromDictionary({ id: 'abc', data_b64: b64 }); |
| 108 | + expect(c).not.toBeNull(); |
| 109 | + expect(c!.dictionary).toEqual(dict); |
| 110 | + }); |
| 111 | + |
| 112 | + it('charges the dictionary at its encoded size, not its inflated one', () => { |
| 113 | + const b64 = Buffer.from(packed).toString('base64'); |
| 114 | + const c = frameCodecFromDictionary({ id: 'abc', data_b64: b64 }); |
| 115 | + expect(c!.getStats().dictionaryBytes).toEqual(b64.length); |
| 116 | + expect(c!.getStats().dictionaryBytes).toBeLessThan(dict.length); |
| 117 | + }); |
| 118 | + |
| 119 | + it('resolves an id with no content from the cache', () => { |
| 120 | + // The warm path: the server recognised the id this client advertised, so it |
| 121 | + // sent nothing and the bytes come from here. |
| 122 | + const cache = new DictionaryCache(); |
| 123 | + cache.put('abc', dict); |
| 124 | + const c = frameCodecFromDictionary({ id: 'abc' }, cache); |
| 125 | + expect(c).not.toBeNull(); |
| 126 | + expect(c!.dictionary).toEqual(dict); |
| 127 | + // Nothing crossed the wire, so nothing is charged for it. |
| 128 | + expect(c!.getStats().dictionaryBytes).toEqual(0); |
| 129 | + }); |
| 130 | + |
| 131 | + it('returns null for an id the client does not hold', () => { |
| 132 | + // Nothing after this frame could be decoded, so the caller has to notice |
| 133 | + // rather than install a codec built on nothing. |
| 134 | + expect(frameCodecFromDictionary({ id: 'unknown' }, new DictionaryCache())).toBeNull(); |
| 135 | + expect(frameCodecFromDictionary({ id: 'unknown' })).toBeNull(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('returns null for content that is not valid deflate', () => { |
| 139 | + expect(frameCodecFromDictionary({ id: 'abc', data: new Uint8Array([1, 2, 3, 4]) })).toBeNull(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns null for nothing at all', () => { |
| 143 | + expect(frameCodecFromDictionary(null)).toBeNull(); |
| 144 | + expect(frameCodecFromDictionary(undefined)).toBeNull(); |
| 145 | + }); |
| 146 | +}); |
0 commit comments