pi-windsurf-beta 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/wire.ts ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Manual protobuf + Connect-RPC streaming envelope helpers.
3
+ * Zero dependencies — pure Buffer math.
4
+ *
5
+ * Connect-RPC streaming wire format (HTTPS POST body):
6
+ * ┌─────────────┬────────────────┬──────────────┐
7
+ * │ flags 1byte │ length 4B BE │ payload │
8
+ * └─────────────┴────────────────┴──────────────┘
9
+ * flags bit 0x01 = payload is gzip-compressed
10
+ * flags bit 0x02 = end-of-stream (trailer frame)
11
+ */
12
+
13
+ import * as zlib from "zlib";
14
+
15
+ // ----------------------------------------------------------------------------
16
+ // Proto wire encode
17
+ // ----------------------------------------------------------------------------
18
+
19
+ export function encodeVarint(value: number | bigint): Buffer {
20
+ const v0 = BigInt(value);
21
+ if (v0 < 0n) throw new RangeError(`encodeVarint: negative input not supported (got ${value})`);
22
+ const bytes: number[] = [];
23
+ let v = v0;
24
+ while (v > 127n) {
25
+ bytes.push(Number(v & 0x7fn) | 0x80);
26
+ v >>= 7n;
27
+ }
28
+ bytes.push(Number(v));
29
+ return Buffer.from(bytes);
30
+ }
31
+
32
+ export function encodeTag(fieldNum: number, wire: number): Buffer {
33
+ return encodeVarint((fieldNum << 3) | wire);
34
+ }
35
+
36
+ export function encodeString(fieldNum: number, s: string): Buffer {
37
+ const buf = Buffer.from(s, "utf8");
38
+ return Buffer.concat([encodeTag(fieldNum, 2), encodeVarint(buf.length), buf]);
39
+ }
40
+
41
+ export function encodeMessage(fieldNum: number, body: Buffer): Buffer {
42
+ return Buffer.concat([encodeTag(fieldNum, 2), encodeVarint(body.length), body]);
43
+ }
44
+
45
+ export function encodeVarintField(fieldNum: number, v: number | bigint): Buffer {
46
+ return Buffer.concat([encodeTag(fieldNum, 0), encodeVarint(v)]);
47
+ }
48
+
49
+ export function encodeTimestampBody(): Buffer {
50
+ const now = Date.now();
51
+ const seconds = Math.floor(now / 1000);
52
+ const nanos = (now % 1000) * 1_000_000;
53
+ return Buffer.concat([
54
+ encodeVarintField(1, seconds),
55
+ nanos > 0 ? encodeVarintField(2, nanos) : Buffer.alloc(0),
56
+ ]);
57
+ }
58
+
59
+ // ----------------------------------------------------------------------------
60
+ // Proto wire decode
61
+ // ----------------------------------------------------------------------------
62
+
63
+ export function decodeVarint(buf: Buffer, offset: number): [bigint, number] {
64
+ let res = 0n;
65
+ let shift = 0n;
66
+ let i = offset;
67
+ while (i < buf.length) {
68
+ const b = buf[i++];
69
+ res |= BigInt(b & 0x7f) << shift;
70
+ if (!(b & 0x80)) return [res, i];
71
+ shift += 7n;
72
+ }
73
+ throw new Error("truncated varint");
74
+ }
75
+
76
+ export interface ProtoField {
77
+ num: number;
78
+ wire: number;
79
+ value: bigint | Buffer;
80
+ }
81
+
82
+ export function* iterFields(buf: Buffer): Generator<ProtoField> {
83
+ let i = 0;
84
+ while (i < buf.length) {
85
+ const [tagBig, ai] = decodeVarint(buf, i);
86
+ i = ai;
87
+ const tag = Number(tagBig);
88
+ const num = tag >> 3;
89
+ const wire = tag & 0x7;
90
+ if (wire === 0) {
91
+ const [v, bi] = decodeVarint(buf, i);
92
+ i = bi;
93
+ yield { num, wire, value: v };
94
+ } else if (wire === 1) {
95
+ if (i + 8 > buf.length) return;
96
+ yield { num, wire, value: buf.slice(i, i + 8) };
97
+ i += 8;
98
+ } else if (wire === 2) {
99
+ const [n, ci] = decodeVarint(buf, i);
100
+ i = ci;
101
+ const len = Number(n);
102
+ if (len < 0 || i + len > buf.length) return;
103
+ yield { num, wire, value: buf.slice(i, i + len) };
104
+ i += len;
105
+ } else if (wire === 5) {
106
+ if (i + 4 > buf.length) return;
107
+ yield { num, wire, value: buf.slice(i, i + 4) };
108
+ i += 4;
109
+ } else if (wire === 3 || wire === 4) {
110
+ return;
111
+ } else {
112
+ return;
113
+ }
114
+ }
115
+ }
116
+
117
+ // ----------------------------------------------------------------------------
118
+ // Connect-streaming envelope
119
+ // ----------------------------------------------------------------------------
120
+
121
+ export function frameConnectStream(body: Buffer, compress = true): Buffer {
122
+ let payload = body;
123
+ let flags = 0;
124
+ if (compress) {
125
+ payload = zlib.gzipSync(body);
126
+ flags |= 0x01;
127
+ }
128
+ const header = Buffer.alloc(5);
129
+ header[0] = flags;
130
+ header.writeUInt32BE(payload.length, 1);
131
+ return Buffer.concat([header, payload]);
132
+ }