@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 140 lines 4.57 KB
Raw
1 /**
2 * Pure encoding helpers for the Prometheus remote-write wire format.
3 *
4 * Remote-write bodies are a `prometheus.WriteRequest` protobuf message,
5 * snappy-compressed (raw block format). Both encodings are small and fully
6 * specified, so they are hand-rolled here rather than pulling in protobuf +
7 * snappy dependencies:
8 *
9 * ```proto
10 * message WriteRequest { repeated TimeSeries timeseries = 1; }
11 * message TimeSeries { repeated Label labels = 1; repeated Sample samples = 2; }
12 * message Label { string name = 1; string value = 2; }
13 * message Sample { double value = 1; int64 timestamp = 2; }
14 * ```
15 *
16 * The snappy encoder emits literal-only blocks (no back-references), which is
17 * valid snappy — decoders don't require compressed output. Remote-write
18 * payloads are small enough that the size cost is irrelevant.
19 *
20 * NOT exported from `index.ts` — internal to the RemoteWrite binding.
21 */
22
23 /** One sample of a series being remote-written; timestamp in epoch millis. */
24 export interface EncodableSample {
25 value: number;
26 timestamp: number;
27 }
28
29 /** One fully-resolved time series: sorted-ready labels plus samples. */
30 export interface EncodableSeries {
31 /** Complete label set INCLUDING `__name__`. */
32 labels: Record<string, string>;
33 samples: EncodableSample[];
34 }
35
36 const textEncoder = new TextEncoder();
37
38 const pushVarint = (out: number[], value: number | bigint) => {
39 let v = BigInt(value);
40 if (v < 0n) {
41 // int64 negative values are 10-byte two's-complement varints.
42 v &= 0xffffffffffffffffn;
43 }
44 while (v >= 0x80n) {
45 out.push(Number(v & 0x7fn) | 0x80);
46 v >>= 7n;
47 }
48 out.push(Number(v));
49 };
50
51 const pushLengthDelimited = (out: number[], fieldNumber: number, payload: ArrayLike<number>) => {
52 out.push((fieldNumber << 3) | 2);
53 pushVarint(out, payload.length);
54 for (let i = 0; i < payload.length; i++) {
55 out.push(payload[i]!);
56 }
57 };
58
59 const encodeLabel = (name: string, value: string): number[] => {
60 const out: number[] = [];
61 pushLengthDelimited(out, 1, textEncoder.encode(name));
62 pushLengthDelimited(out, 2, textEncoder.encode(value));
63 return out;
64 };
65
66 const float64Buffer = new DataView(new ArrayBuffer(8));
67
68 const encodeSample = (sample: EncodableSample): number[] => {
69 const out: number[] = [];
70 // field 1: double value (wire type 1, fixed64, little-endian)
71 out.push(0x09);
72 float64Buffer.setFloat64(0, sample.value, true);
73 for (let i = 0; i < 8; i++) {
74 out.push(float64Buffer.getUint8(i));
75 }
76 // field 2: int64 timestamp (wire type 0, varint)
77 out.push(0x10);
78 pushVarint(out, Math.round(sample.timestamp));
79 return out;
80 };
81
82 const encodeTimeSeries = (series: EncodableSeries): number[] => {
83 const out: number[] = [];
84 // Remote-write requires labels sorted by name.
85 const names = Object.keys(series.labels).sort();
86 for (const name of names) {
87 pushLengthDelimited(out, 1, encodeLabel(name, series.labels[name]!));
88 }
89 for (const sample of series.samples) {
90 pushLengthDelimited(out, 2, encodeSample(sample));
91 }
92 return out;
93 };
94
95 /** Encode a `prometheus.WriteRequest` protobuf message. */
96 export const encodeWriteRequest = (timeseries: readonly EncodableSeries[]): Uint8Array => {
97 const out: number[] = [];
98 for (const series of timeseries) {
99 pushLengthDelimited(out, 1, encodeTimeSeries(series));
100 }
101 return Uint8Array.from(out);
102 };
103
104 /**
105 * Snappy-compress `input` using the raw block format with literal-only
106 * elements: `varint(len(input))` followed by literal chunks of at most 64 KiB
107 * each. Valid snappy per the format spec (compression is optional).
108 */
109 export const snappyCompress = (input: Uint8Array): Uint8Array => {
110 const header: number[] = [];
111 pushVarint(header, input.length);
112
113 const chunkSize = 65536;
114 const chunkCount = Math.ceil(input.length / chunkSize);
115 // Each chunk's literal tag: 1 byte for len <= 60, else 1 tag + 2 length bytes.
116 let totalLength = header.length;
117 for (let i = 0; i < chunkCount; i++) {
118 const len = Math.min(chunkSize, input.length - i * chunkSize);
119 totalLength += (len <= 60 ? 1 : 3) + len;
120 }
121
122 const out = new Uint8Array(totalLength);
123 out.set(header, 0);
124 let offset = header.length;
125 for (let i = 0; i < chunkCount; i++) {
126 const start = i * chunkSize;
127 const len = Math.min(chunkSize, input.length - start);
128 if (len <= 60) {
129 out[offset++] = (len - 1) << 2;
130 } else {
131 // tag 61: literal with 2-byte little-endian (length - 1).
132 out[offset++] = 61 << 2;
133 out[offset++] = (len - 1) & 0xff;
134 out[offset++] = ((len - 1) >> 8) & 0xff;
135 }
136 out.set(input.subarray(start, start + len), offset);
137 offset += len;
138 }
139 return out;
140 };