// AMF0, the encoding of RTMP's commands and data messages (Adobe's AMF0 specification, as RTMP 1.0 ยง7 uses it): a // command is a sequence of values (its name, a transaction id, an object, its arguments). Read: number, boolean, // string, object, null, undefined, ECMA array, strict array, date, long string, typed object; a reference (0x07) is read // as undefined, never followed, so no value read is cyclic (a publisher's metadata pointing back at itself); anything // else stops the read where it is. Written: number, boolean, string, object (and a plain record), null, undefined. export type Amf0Value = number | boolean | string | null | undefined | Date | Amf0Value[] | { [key: string]: Amf0Value }; const utf8 = new TextDecoder(); const encoder = new TextEncoder(); /** Every value in `bytes`, in order; a value of a type AMF0 commands do not carry ends the read. */ export function decodeAmf0(bytes: Uint8Array): Amf0Value[] { const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); let at = 0; const str = (length: number): string => { const s = utf8.decode(bytes.subarray(at, at + length)); at += length; return s; }; const key = (): string => { const n = view.getUint16(at); at += 2; return str(n); }; const properties = (into: Record): Record => { for (;;) { if (at + 3 > bytes.length) return into; if (view.getUint16(at) === 0 && bytes[at + 2] === 0x09) { at += 3; return into; } const name = key(); into[name] = value(); } }; const value = (): Amf0Value => { const marker = bytes[at++]; switch (marker) { case 0x00: { const n = view.getFloat64(at); at += 8; return n; } case 0x01: return bytes[at++] !== 0; case 0x02: return key(); case 0x03: return properties({}); case 0x05: return null; case 0x06: return undefined; case 0x07: at += 2; return undefined; case 0x08: at += 4; return properties({}); case 0x0a: { const n = view.getUint32(at); at += 4; const list: Amf0Value[] = []; for (let i = 0; i < n && at < bytes.length; i++) list.push(value()); return list; } case 0x0b: { const ms = view.getFloat64(at); at += 10; return new Date(ms); } case 0x0c: { const n = view.getUint32(at); at += 4; return str(n); } case 0x10: key(); return properties({}); default: throw new Amf0End(); } }; const out: Amf0Value[] = []; while (at < bytes.length) { try { out.push(value()); } catch (error) { if (error instanceof Amf0End || error instanceof RangeError) break; throw error; } } return out; } class Amf0End extends Error {} /** `values` as AMF0, in order. */ export function encodeAmf0(...values: Amf0Value[]): Uint8Array { const parts: number[] = []; const u16 = (n: number): void => { parts.push((n >> 8) & 0xff, n & 0xff); }; const text = (s: string): void => { const b = encoder.encode(s); u16(b.length); for (const x of b) parts.push(x); }; const write = (v: Amf0Value): void => { if (typeof v === 'number') { parts.push(0x00); const b = new Uint8Array(8); new DataView(b.buffer).setFloat64(0, v); for (const x of b) parts.push(x); } else if (typeof v === 'boolean') parts.push(0x01, v ? 1 : 0); else if (typeof v === 'string') { parts.push(0x02); text(v); } else if (v === null) parts.push(0x05); else if (v === undefined) parts.push(0x06); else if (v instanceof Date || Array.isArray(v)) throw new Error('amf0: this writer writes no dates or arrays'); else { parts.push(0x03); for (const [k, x] of Object.entries(v)) { text(k); write(x); } parts.push(0x00, 0x00, 0x09); } }; for (const v of values) write(v); return Uint8Array.from(parts); }