oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// The updates wire (live.go's updatesGet): each update framed as a
2// 4-byte big-endian length then the bytes. A body that does not divide
3// cleanly is an error rather than a short read — a client that guessed
4// would be a client that silently lost the last update.
5import test from 'node:test'
6import assert from 'node:assert/strict'
7import { parseFrames, frame } from './frames.mjs'
8
9test('an empty body is no frames', () => {
10 assert.deepEqual(parseFrames(new Uint8Array(0)), [])
11})
12
13test('frames come back in order with their exact bytes', () => {
14 const a = new Uint8Array([1, 2, 3])
15 const b = new Uint8Array([])
16 const c = new Uint8Array([9])
17 const body = new Uint8Array([...frame(a), ...frame(b), ...frame(c)])
18 const got = parseFrames(body)
19 assert.equal(got.length, 3)
20 assert.deepEqual(Array.from(got[0]), [1, 2, 3])
21 assert.deepEqual(Array.from(got[1]), [])
22 assert.deepEqual(Array.from(got[2]), [9])
23})
24
25test('a length prefix is big-endian', () => {
26 const body = new Uint8Array([0, 0, 1, 0, ...new Array(256).fill(7)])
27 assert.equal(parseFrames(body)[0].length, 256)
28})
29
30test('a truncated frame throws rather than returning a short update', () => {
31 assert.throws(() => parseFrames(new Uint8Array([0, 0, 0, 5, 1, 2])), /frame/)
32 assert.throws(() => parseFrames(new Uint8Array([0, 0])), /frame/)
33})
34