@warmdrift/kgauto-compiler 2.0.0-alpha.17 → 2.0.0-alpha.19
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/dist/chunk-VZGMWKRT.mjs +19 -0
- package/dist/glassbox/index.d.mts +4 -121
- package/dist/glassbox/index.d.ts +4 -121
- package/dist/glassbox/index.mjs +4 -14
- package/dist/glassbox-routes/index.d.mts +73 -0
- package/dist/glassbox-routes/index.d.ts +73 -0
- package/dist/glassbox-routes/index.js +559 -0
- package/dist/glassbox-routes/index.mjs +268 -0
- package/dist/types-DWF6mPGg.d.mts +122 -0
- package/dist/types-xeklorHU.d.ts +122 -0
- package/package.json +7 -2
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/glassbox-routes/index.ts
|
|
21
|
+
var glassbox_routes_exports = {};
|
|
22
|
+
__export(glassbox_routes_exports, {
|
|
23
|
+
createGlassboxRoutes: () => createGlassboxRoutes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(glassbox_routes_exports);
|
|
26
|
+
|
|
27
|
+
// src/glassbox-routes/auth.ts
|
|
28
|
+
var JSON_HEADERS = { "Content-Type": "application/json" };
|
|
29
|
+
function jsonError(status, code) {
|
|
30
|
+
return new Response(JSON.stringify({ error: code }), {
|
|
31
|
+
status,
|
|
32
|
+
headers: JSON_HEADERS
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function tokensEqual(a, b) {
|
|
36
|
+
if (a.length !== b.length) return false;
|
|
37
|
+
let mismatch = 0;
|
|
38
|
+
for (let i = 0; i < a.length; i++) {
|
|
39
|
+
mismatch |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
40
|
+
}
|
|
41
|
+
return mismatch === 0;
|
|
42
|
+
}
|
|
43
|
+
function checkAuth(req, config) {
|
|
44
|
+
const authHeader = req.headers.get("Authorization") ?? "";
|
|
45
|
+
const match = /^Bearer\s+(.+)$/i.exec(authHeader);
|
|
46
|
+
const provided = match?.[1]?.trim() ?? "";
|
|
47
|
+
if (!provided || !tokensEqual(provided, config.installToken)) {
|
|
48
|
+
return jsonError(401, "unauthorized");
|
|
49
|
+
}
|
|
50
|
+
const origin = req.headers.get("Origin") ?? "";
|
|
51
|
+
const expected = `chrome-extension://${config.extensionId}`;
|
|
52
|
+
if (origin !== expected) {
|
|
53
|
+
return jsonError(403, "forbidden_origin");
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/glassbox-routes/proxy.ts
|
|
59
|
+
var JSON_HEADERS2 = {
|
|
60
|
+
"Content-Type": "application/json",
|
|
61
|
+
"Cache-Control": "no-store"
|
|
62
|
+
};
|
|
63
|
+
var DEFAULT_LIMIT = 20;
|
|
64
|
+
var MAX_LIMIT = 100;
|
|
65
|
+
function jsonResponse(status, body) {
|
|
66
|
+
return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS2 });
|
|
67
|
+
}
|
|
68
|
+
function jsonError2(status, code) {
|
|
69
|
+
return jsonResponse(status, { error: code });
|
|
70
|
+
}
|
|
71
|
+
function applyScrub(row, scrub) {
|
|
72
|
+
if (!scrub || row == null || typeof row !== "object") return row;
|
|
73
|
+
try {
|
|
74
|
+
return scrub(row);
|
|
75
|
+
} catch {
|
|
76
|
+
return row;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function parseLimit(raw) {
|
|
80
|
+
if (!raw) return DEFAULT_LIMIT;
|
|
81
|
+
const n = Number.parseInt(raw, 10);
|
|
82
|
+
if (!Number.isFinite(n) || n <= 0) return DEFAULT_LIMIT;
|
|
83
|
+
return Math.min(n, MAX_LIMIT);
|
|
84
|
+
}
|
|
85
|
+
function createProxyHandler(config) {
|
|
86
|
+
const {
|
|
87
|
+
installToken,
|
|
88
|
+
extensionId,
|
|
89
|
+
brainEndpoint,
|
|
90
|
+
brainJwt,
|
|
91
|
+
appId,
|
|
92
|
+
scrub,
|
|
93
|
+
fetch: fetchImpl
|
|
94
|
+
} = config;
|
|
95
|
+
const doFetch = fetchImpl ?? ((...args) => globalThis.fetch(...args));
|
|
96
|
+
const base = brainEndpoint.replace(/\/+$/, "");
|
|
97
|
+
return async function proxy(req) {
|
|
98
|
+
const authFail = checkAuth(req, { installToken, extensionId });
|
|
99
|
+
if (authFail) return authFail;
|
|
100
|
+
const url = new URL(req.url);
|
|
101
|
+
const traceId = url.searchParams.get("traceId");
|
|
102
|
+
const limit = parseLimit(url.searchParams.get("limit"));
|
|
103
|
+
const qs = new URLSearchParams();
|
|
104
|
+
qs.set("app_id", `eq.${appId}`);
|
|
105
|
+
if (traceId) {
|
|
106
|
+
qs.set("trace_id", `eq.${traceId}`);
|
|
107
|
+
} else {
|
|
108
|
+
qs.set("order", "created_at.desc");
|
|
109
|
+
qs.set("limit", String(limit));
|
|
110
|
+
}
|
|
111
|
+
const brainUrl = `${base}/rest/v1/compile_outcomes?${qs.toString()}`;
|
|
112
|
+
let brainRes;
|
|
113
|
+
try {
|
|
114
|
+
brainRes = await doFetch(brainUrl, {
|
|
115
|
+
method: "GET",
|
|
116
|
+
headers: {
|
|
117
|
+
Authorization: `Bearer ${brainJwt}`,
|
|
118
|
+
apikey: brainJwt,
|
|
119
|
+
Accept: "application/json"
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
} catch {
|
|
123
|
+
return jsonError2(502, "brain_unavailable");
|
|
124
|
+
}
|
|
125
|
+
if (brainRes.status === 401 || brainRes.status === 403) {
|
|
126
|
+
return jsonError2(500, "brain_auth_misconfig");
|
|
127
|
+
}
|
|
128
|
+
if (brainRes.status >= 500) {
|
|
129
|
+
return jsonError2(502, "brain_unavailable");
|
|
130
|
+
}
|
|
131
|
+
if (!brainRes.ok) {
|
|
132
|
+
return jsonError2(400, "bad_request");
|
|
133
|
+
}
|
|
134
|
+
let rows;
|
|
135
|
+
try {
|
|
136
|
+
rows = await brainRes.json();
|
|
137
|
+
} catch {
|
|
138
|
+
return jsonError2(502, "brain_unavailable");
|
|
139
|
+
}
|
|
140
|
+
if (!Array.isArray(rows)) {
|
|
141
|
+
return jsonError2(502, "brain_unavailable");
|
|
142
|
+
}
|
|
143
|
+
const scrubbed = rows.map((row) => applyScrub(row, scrub));
|
|
144
|
+
return jsonResponse(200, scrubbed);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/glassbox-routes/stream.ts
|
|
149
|
+
var SSE_HEADERS = {
|
|
150
|
+
"Content-Type": "text/event-stream",
|
|
151
|
+
"Cache-Control": "no-cache, no-transform",
|
|
152
|
+
Connection: "keep-alive",
|
|
153
|
+
"X-Accel-Buffering": "no"
|
|
154
|
+
};
|
|
155
|
+
var JSON_HEADERS3 = { "Content-Type": "application/json" };
|
|
156
|
+
function jsonError3(status, code) {
|
|
157
|
+
return new Response(JSON.stringify({ error: code }), {
|
|
158
|
+
status,
|
|
159
|
+
headers: JSON_HEADERS3
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function applyScrub2(event, scrub) {
|
|
163
|
+
if (!scrub) return event;
|
|
164
|
+
try {
|
|
165
|
+
const out = scrub(event);
|
|
166
|
+
if (out && typeof out === "object" && typeof out.kind === "string" && typeof out.at === "number") {
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
return event;
|
|
170
|
+
} catch {
|
|
171
|
+
return event;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function sseFrame(eventName, data) {
|
|
175
|
+
const safeName = eventName.replace(/[\r\n]/g, "");
|
|
176
|
+
return `event: ${safeName}
|
|
177
|
+
data: ${JSON.stringify(data)}
|
|
178
|
+
|
|
179
|
+
`;
|
|
180
|
+
}
|
|
181
|
+
function createStreamHandler(config, subscribe2) {
|
|
182
|
+
const { installToken, extensionId, scrub } = config;
|
|
183
|
+
return async function stream(req) {
|
|
184
|
+
const authFail = checkAuth(req, { installToken, extensionId });
|
|
185
|
+
if (authFail) return authFail;
|
|
186
|
+
const url = new URL(req.url);
|
|
187
|
+
const traceId = url.searchParams.get("traceId");
|
|
188
|
+
if (!traceId) {
|
|
189
|
+
return jsonError3(400, "missing_trace_id");
|
|
190
|
+
}
|
|
191
|
+
const source = subscribe2(traceId);
|
|
192
|
+
const encoder = new TextEncoder();
|
|
193
|
+
let sourceReader;
|
|
194
|
+
let cancelled = false;
|
|
195
|
+
const body = new ReadableStream({
|
|
196
|
+
async start(controller) {
|
|
197
|
+
controller.enqueue(encoder.encode(sseFrame("ready", {})));
|
|
198
|
+
sourceReader = source.getReader();
|
|
199
|
+
const signal = req.signal;
|
|
200
|
+
if (signal) {
|
|
201
|
+
if (signal.aborted) {
|
|
202
|
+
cancelled = true;
|
|
203
|
+
await sourceReader.cancel();
|
|
204
|
+
try {
|
|
205
|
+
controller.close();
|
|
206
|
+
} catch {
|
|
207
|
+
}
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
signal.addEventListener(
|
|
211
|
+
"abort",
|
|
212
|
+
() => {
|
|
213
|
+
cancelled = true;
|
|
214
|
+
sourceReader?.cancel().catch(() => {
|
|
215
|
+
});
|
|
216
|
+
try {
|
|
217
|
+
controller.close();
|
|
218
|
+
} catch {
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
{ once: true }
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
while (!cancelled) {
|
|
226
|
+
const { value, done } = await sourceReader.read();
|
|
227
|
+
if (done) break;
|
|
228
|
+
const scrubbed = applyScrub2(value, scrub);
|
|
229
|
+
controller.enqueue(
|
|
230
|
+
encoder.encode(sseFrame(scrubbed.kind, scrubbed))
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
} catch {
|
|
234
|
+
} finally {
|
|
235
|
+
try {
|
|
236
|
+
controller.close();
|
|
237
|
+
} catch {
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
sourceReader?.releaseLock();
|
|
241
|
+
} catch {
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
cancel() {
|
|
246
|
+
cancelled = true;
|
|
247
|
+
sourceReader?.cancel().catch(() => {
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
return new Response(body, { status: 200, headers: SSE_HEADERS });
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/glassbox/types.ts
|
|
256
|
+
var GLASSBOX_STREAM_TTL_MS = 6e4;
|
|
257
|
+
|
|
258
|
+
// src/glassbox/pubsub-memory.ts
|
|
259
|
+
var MemoryPubSub = class {
|
|
260
|
+
subscribers = /* @__PURE__ */ new Map();
|
|
261
|
+
async publish(traceId, event) {
|
|
262
|
+
const subs = this.subscribers.get(traceId);
|
|
263
|
+
if (!subs || subs.size === 0) return;
|
|
264
|
+
for (const sub of subs) {
|
|
265
|
+
if (sub.closed) continue;
|
|
266
|
+
try {
|
|
267
|
+
sub.controller.enqueue(event);
|
|
268
|
+
} catch {
|
|
269
|
+
sub.closed = true;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
this.refreshTtl(traceId, sub);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
subscribe(traceId) {
|
|
276
|
+
const self = this;
|
|
277
|
+
let sub;
|
|
278
|
+
return new ReadableStream({
|
|
279
|
+
start(controller) {
|
|
280
|
+
sub = {
|
|
281
|
+
controller,
|
|
282
|
+
ttlTimer: setTimeout(() => {
|
|
283
|
+
self.closeSubscriber(traceId, sub);
|
|
284
|
+
}, GLASSBOX_STREAM_TTL_MS),
|
|
285
|
+
closed: false
|
|
286
|
+
};
|
|
287
|
+
let set = self.subscribers.get(traceId);
|
|
288
|
+
if (!set) {
|
|
289
|
+
set = /* @__PURE__ */ new Set();
|
|
290
|
+
self.subscribers.set(traceId, set);
|
|
291
|
+
}
|
|
292
|
+
set.add(sub);
|
|
293
|
+
},
|
|
294
|
+
cancel() {
|
|
295
|
+
if (sub) self.removeSubscriber(traceId, sub);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Refresh the rolling TTL for a subscriber after an event lands. Replaces
|
|
301
|
+
* the existing timer with a fresh 60s one.
|
|
302
|
+
*/
|
|
303
|
+
refreshTtl(traceId, sub) {
|
|
304
|
+
clearTimeout(sub.ttlTimer);
|
|
305
|
+
sub.ttlTimer = setTimeout(() => {
|
|
306
|
+
this.closeSubscriber(traceId, sub);
|
|
307
|
+
}, GLASSBOX_STREAM_TTL_MS);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Close the subscriber's stream cleanly and remove from the fan-out set.
|
|
311
|
+
* Idempotent — safe to call multiple times.
|
|
312
|
+
*/
|
|
313
|
+
closeSubscriber(traceId, sub) {
|
|
314
|
+
if (sub.closed) return;
|
|
315
|
+
sub.closed = true;
|
|
316
|
+
clearTimeout(sub.ttlTimer);
|
|
317
|
+
try {
|
|
318
|
+
sub.controller.close();
|
|
319
|
+
} catch {
|
|
320
|
+
}
|
|
321
|
+
this.removeSubscriber(traceId, sub);
|
|
322
|
+
}
|
|
323
|
+
removeSubscriber(traceId, sub) {
|
|
324
|
+
clearTimeout(sub.ttlTimer);
|
|
325
|
+
const set = this.subscribers.get(traceId);
|
|
326
|
+
if (!set) return;
|
|
327
|
+
set.delete(sub);
|
|
328
|
+
if (set.size === 0) this.subscribers.delete(traceId);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Test-only reset. Tears down all subscribers, clears all state. Calling
|
|
332
|
+
* outside of tests is harmless but cancels every active stream.
|
|
333
|
+
*/
|
|
334
|
+
_reset() {
|
|
335
|
+
for (const [, set] of this.subscribers) {
|
|
336
|
+
for (const sub of set) {
|
|
337
|
+
this.closeSubscriber("", sub);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
this.subscribers.clear();
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// src/glassbox/pubsub-upstash.ts
|
|
345
|
+
var UpstashPubSub = class {
|
|
346
|
+
url;
|
|
347
|
+
token;
|
|
348
|
+
fetchImpl;
|
|
349
|
+
blockMs;
|
|
350
|
+
maxLen;
|
|
351
|
+
constructor(cfg) {
|
|
352
|
+
this.url = cfg.url.replace(/\/$/, "");
|
|
353
|
+
this.token = cfg.token;
|
|
354
|
+
this.fetchImpl = cfg.fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
355
|
+
this.blockMs = cfg.blockMs ?? 100;
|
|
356
|
+
this.maxLen = cfg.maxLen ?? 100;
|
|
357
|
+
}
|
|
358
|
+
async publish(traceId, event) {
|
|
359
|
+
const key = streamKey(traceId);
|
|
360
|
+
const payload = JSON.stringify(event);
|
|
361
|
+
await this.cmd([
|
|
362
|
+
"XADD",
|
|
363
|
+
key,
|
|
364
|
+
"MAXLEN",
|
|
365
|
+
"~",
|
|
366
|
+
String(this.maxLen),
|
|
367
|
+
"*",
|
|
368
|
+
"event",
|
|
369
|
+
payload
|
|
370
|
+
]);
|
|
371
|
+
await this.cmd(["EXPIRE", key, String(Math.ceil(GLASSBOX_STREAM_TTL_MS / 1e3))]);
|
|
372
|
+
}
|
|
373
|
+
subscribe(traceId) {
|
|
374
|
+
const key = streamKey(traceId);
|
|
375
|
+
const self = this;
|
|
376
|
+
let cursor = "$";
|
|
377
|
+
let cancelled = false;
|
|
378
|
+
let ttlDeadline = Date.now() + GLASSBOX_STREAM_TTL_MS;
|
|
379
|
+
return new ReadableStream({
|
|
380
|
+
async start(controller) {
|
|
381
|
+
try {
|
|
382
|
+
while (!cancelled && Date.now() < ttlDeadline) {
|
|
383
|
+
const resp = await self.cmd([
|
|
384
|
+
"XREAD",
|
|
385
|
+
"BLOCK",
|
|
386
|
+
String(self.blockMs),
|
|
387
|
+
"STREAMS",
|
|
388
|
+
key,
|
|
389
|
+
cursor
|
|
390
|
+
]);
|
|
391
|
+
if (cancelled) break;
|
|
392
|
+
const parsed = parseXReadResult(resp.result);
|
|
393
|
+
if (parsed.entries.length === 0) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
for (const entry of parsed.entries) {
|
|
397
|
+
const evt = decodeEvent(entry.fields);
|
|
398
|
+
if (evt) {
|
|
399
|
+
try {
|
|
400
|
+
controller.enqueue(evt);
|
|
401
|
+
} catch {
|
|
402
|
+
cancelled = true;
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
cursor = entry.id;
|
|
407
|
+
}
|
|
408
|
+
ttlDeadline = Date.now() + GLASSBOX_STREAM_TTL_MS;
|
|
409
|
+
}
|
|
410
|
+
} catch (err) {
|
|
411
|
+
if (!cancelled) {
|
|
412
|
+
try {
|
|
413
|
+
controller.error(err);
|
|
414
|
+
} catch {
|
|
415
|
+
}
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
try {
|
|
420
|
+
controller.close();
|
|
421
|
+
} catch {
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
cancel() {
|
|
425
|
+
cancelled = true;
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
async cmd(args) {
|
|
430
|
+
const res = await this.fetchImpl(this.url, {
|
|
431
|
+
method: "POST",
|
|
432
|
+
headers: {
|
|
433
|
+
Authorization: `Bearer ${this.token}`,
|
|
434
|
+
"Content-Type": "application/json"
|
|
435
|
+
},
|
|
436
|
+
body: JSON.stringify(args)
|
|
437
|
+
});
|
|
438
|
+
if (!res.ok) {
|
|
439
|
+
throw new Error(`Upstash ${args[0]} failed: HTTP ${res.status}`);
|
|
440
|
+
}
|
|
441
|
+
const json = await res.json();
|
|
442
|
+
if (json.error) {
|
|
443
|
+
throw new Error(`Upstash ${args[0]} failed: ${json.error}`);
|
|
444
|
+
}
|
|
445
|
+
return json;
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
function streamKey(traceId) {
|
|
449
|
+
return `glassbox:trace:${traceId}`;
|
|
450
|
+
}
|
|
451
|
+
function decodeEvent(fields) {
|
|
452
|
+
const raw = fields["event"];
|
|
453
|
+
if (!raw) return void 0;
|
|
454
|
+
try {
|
|
455
|
+
const parsed = JSON.parse(raw);
|
|
456
|
+
if (typeof parsed.kind === "string" && typeof parsed.at === "number") {
|
|
457
|
+
return parsed;
|
|
458
|
+
}
|
|
459
|
+
return void 0;
|
|
460
|
+
} catch {
|
|
461
|
+
return void 0;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
function parseXReadResult(raw) {
|
|
465
|
+
if (!Array.isArray(raw)) return { entries: [] };
|
|
466
|
+
const entries = [];
|
|
467
|
+
for (const stream of raw) {
|
|
468
|
+
if (!Array.isArray(stream) || stream.length < 2) continue;
|
|
469
|
+
const streamEntries = stream[1];
|
|
470
|
+
if (!Array.isArray(streamEntries)) continue;
|
|
471
|
+
for (const entry of streamEntries) {
|
|
472
|
+
if (!Array.isArray(entry) || entry.length < 2) continue;
|
|
473
|
+
const id = String(entry[0]);
|
|
474
|
+
const flat = entry[1];
|
|
475
|
+
if (!Array.isArray(flat)) continue;
|
|
476
|
+
const fields = {};
|
|
477
|
+
for (let i = 0; i < flat.length; i += 2) {
|
|
478
|
+
const k = flat[i];
|
|
479
|
+
const v = flat[i + 1];
|
|
480
|
+
if (typeof k === "string") fields[k] = String(v ?? "");
|
|
481
|
+
}
|
|
482
|
+
entries.push({ id, fields });
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return { entries };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// src/glassbox/emit.ts
|
|
489
|
+
var activePubSub;
|
|
490
|
+
function getPubSub() {
|
|
491
|
+
if (activePubSub) return activePubSub;
|
|
492
|
+
const url = readEnv("UPSTASH_REDIS_URL");
|
|
493
|
+
const token = readEnv("UPSTASH_REDIS_TOKEN");
|
|
494
|
+
if (url && token) {
|
|
495
|
+
activePubSub = new UpstashPubSub({ url, token });
|
|
496
|
+
} else {
|
|
497
|
+
activePubSub = new MemoryPubSub();
|
|
498
|
+
}
|
|
499
|
+
return activePubSub;
|
|
500
|
+
}
|
|
501
|
+
function readEnv(key) {
|
|
502
|
+
try {
|
|
503
|
+
if (typeof process !== "undefined" && process.env) {
|
|
504
|
+
const v = process.env[key];
|
|
505
|
+
return v && v.trim() !== "" ? v : void 0;
|
|
506
|
+
}
|
|
507
|
+
} catch {
|
|
508
|
+
}
|
|
509
|
+
return void 0;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// src/glassbox/subscribe.ts
|
|
513
|
+
function subscribe(traceId) {
|
|
514
|
+
if (!traceId) {
|
|
515
|
+
return new ReadableStream({
|
|
516
|
+
start(controller) {
|
|
517
|
+
controller.close();
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
return getPubSub().subscribe(traceId);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// src/glassbox-routes/index.ts
|
|
525
|
+
function requireString(name, value) {
|
|
526
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
527
|
+
throw new Error(`createGlassboxRoutes: ${name} is required`);
|
|
528
|
+
}
|
|
529
|
+
return value;
|
|
530
|
+
}
|
|
531
|
+
function createGlassboxRoutes(config) {
|
|
532
|
+
const installToken = requireString("installToken", config.installToken);
|
|
533
|
+
const extensionId = requireString("extensionId", config.extensionId);
|
|
534
|
+
const brainEndpoint = requireString("brainEndpoint", config.brainEndpoint);
|
|
535
|
+
const brainJwt = requireString("brainJwt", config.brainJwt);
|
|
536
|
+
const appId = requireString("appId", config.appId);
|
|
537
|
+
const proxy = createProxyHandler({
|
|
538
|
+
installToken,
|
|
539
|
+
extensionId,
|
|
540
|
+
brainEndpoint,
|
|
541
|
+
brainJwt,
|
|
542
|
+
appId,
|
|
543
|
+
scrub: config.scrub,
|
|
544
|
+
fetch: config.fetch
|
|
545
|
+
});
|
|
546
|
+
const stream = createStreamHandler(
|
|
547
|
+
{
|
|
548
|
+
installToken,
|
|
549
|
+
extensionId,
|
|
550
|
+
scrub: config.scrub
|
|
551
|
+
},
|
|
552
|
+
config.subscribe ?? subscribe
|
|
553
|
+
);
|
|
554
|
+
return { proxy, stream };
|
|
555
|
+
}
|
|
556
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
557
|
+
0 && (module.exports = {
|
|
558
|
+
createGlassboxRoutes
|
|
559
|
+
});
|