zitejs 0.10.1 → 0.10.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/dist/cjs/start/build/entries.js +21 -4
- package/dist/cjs/start/describe/schema.d.ts +1 -0
- package/dist/cjs/start/describe/schema.js +2 -0
- package/dist/cjs/start/frames.d.ts +63 -0
- package/dist/cjs/start/frames.js +297 -0
- package/dist/cjs/start/protocol.d.ts +3 -3
- package/dist/cjs/start/protocol.js +10 -6
- package/dist/cjs/start/serve/provider.js +3 -1
- package/dist/cjs/start/types.d.ts +31 -8
- package/dist/esm/start/build/entries.js +22 -5
- package/dist/esm/start/describe/describe.d.ts +2 -0
- package/dist/esm/start/describe/describe.js +2 -0
- package/dist/esm/start/describe/schema.d.ts +1 -0
- package/dist/esm/start/describe/schema.js +2 -0
- package/dist/esm/start/frames.d.ts +63 -0
- package/dist/esm/start/frames.js +290 -0
- package/dist/esm/start/platform/runLog.d.ts +4 -2
- package/dist/esm/start/platform/runLog.js +17 -4
- package/dist/esm/start/platform/telemetry.d.ts +6 -7
- package/dist/esm/start/platform/telemetry.js +11 -15
- package/dist/esm/start/protocol.d.ts +3 -3
- package/dist/esm/start/protocol.js +4 -5
- package/dist/esm/start/serve/console.d.ts +5 -0
- package/dist/esm/start/serve/console.js +60 -0
- package/dist/esm/start/serve/fetch.js +4 -1
- package/dist/esm/start/serve/provider.js +3 -1
- package/dist/esm/start/serve/server.d.ts +4 -3
- package/dist/esm/start/serve/server.js +46 -25
- package/dist/esm/start/types.d.ts +31 -8
- package/dist/esm/start/version.d.ts +1 -1
- package/dist/esm/start/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { RunReport } from './types.js';
|
|
2
|
+
export declare const FRAME: {
|
|
3
|
+
readonly head: 1;
|
|
4
|
+
readonly chunk: 2;
|
|
5
|
+
readonly end: 3;
|
|
6
|
+
readonly report: 4;
|
|
7
|
+
readonly fail: 5;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* What the host decides an answer by, in place of reading its headers. The status travels here because a 204 or a 304
|
|
11
|
+
* can't carry the frames as its body.
|
|
12
|
+
*/
|
|
13
|
+
export type AnswerHead = {
|
|
14
|
+
status: number;
|
|
15
|
+
contentType: string | null;
|
|
16
|
+
/** The body's length as the app declared it, for the host to declare again; the frames are longer. */
|
|
17
|
+
contentLength: number | null;
|
|
18
|
+
/** What the edge's page cache decides by: the host writes these over the app's own, so no one reads those. */
|
|
19
|
+
cacheControl: string | null;
|
|
20
|
+
vary: string | null;
|
|
21
|
+
setCookies: string[];
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The app's answer as the host reads it: its headers as they are, and its body in frames. `report` is asked once the
|
|
25
|
+
* body has ended cleanly; a body that fails or is cancelled stops the frames short of `end`.
|
|
26
|
+
*/
|
|
27
|
+
export declare const framedAnswer: (response: Response,
|
|
28
|
+
/** Given what failed the body, when it failed. */
|
|
29
|
+
report: (failure?: unknown) => Promise<RunReport | null>) => Response;
|
|
30
|
+
/** An answer that breaks the protocol, which only a broken or hostile build sends. */
|
|
31
|
+
export declare class MalformedAnswer extends Error {
|
|
32
|
+
}
|
|
33
|
+
/** The app's own body failed partway, with the app's message for why. */
|
|
34
|
+
export declare class AnswerFailed extends Error {
|
|
35
|
+
}
|
|
36
|
+
/** How a decoded body stopped: at `end`, cancelled by its reader, or with what stopped it. */
|
|
37
|
+
export type BodyEnd = {
|
|
38
|
+
outcome: 'ok' | 'canceled';
|
|
39
|
+
} | {
|
|
40
|
+
outcome: 'error';
|
|
41
|
+
error: unknown;
|
|
42
|
+
};
|
|
43
|
+
export type DecodedAnswer = {
|
|
44
|
+
head: AnswerHead;
|
|
45
|
+
/** The body's chunks as they arrive. It closes at `end` and errors on anything else. */
|
|
46
|
+
body: ReadableStream<Uint8Array>;
|
|
47
|
+
ended: Promise<BodyEnd>;
|
|
48
|
+
/**
|
|
49
|
+
* The report's JSON once the body has ended, cleanly or with the app's `AnswerFailed`: null without one, past its
|
|
50
|
+
* cap, or when the answer broke off. A frame that breaks the protocol rejects it.
|
|
51
|
+
*/
|
|
52
|
+
report: Promise<string | null>;
|
|
53
|
+
};
|
|
54
|
+
/** The most of a `head` and a `report` payload `decodeAnswer` holds, in bytes. */
|
|
55
|
+
export type AnswerCaps = {
|
|
56
|
+
head: number;
|
|
57
|
+
report: number;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Reads an answer's head, then hands on its body's chunks as they arrive, never holding one, and reads its report once
|
|
61
|
+
* the body has ended. A missing, oversized or malformed head rejects.
|
|
62
|
+
*/
|
|
63
|
+
export declare const decodeAnswer: (stream: ReadableStream<Uint8Array> | null, caps: AnswerCaps) => Promise<DecodedAnswer>;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
export const FRAME = { head: 1, chunk: 2, end: 3, report: 4, fail: 5 };
|
|
2
|
+
// A failure's message, cut to this on the app's side and refused past four bytes a character on the host's.
|
|
3
|
+
const FAILURE_CHARS = 1_024;
|
|
4
|
+
const FAILURE_BYTES = FAILURE_CHARS * 4;
|
|
5
|
+
const declaredLength = (header) => {
|
|
6
|
+
const length = header === null ? NaN : Number(header);
|
|
7
|
+
return Number.isSafeInteger(length) && length >= 0 ? length : null;
|
|
8
|
+
};
|
|
9
|
+
const HEADER_BYTES = 5;
|
|
10
|
+
const NOTHING = new Uint8Array(0);
|
|
11
|
+
const encoder = new TextEncoder();
|
|
12
|
+
const decoder = new TextDecoder();
|
|
13
|
+
const frameHeader = (type, length) => {
|
|
14
|
+
const header = new Uint8Array(HEADER_BYTES);
|
|
15
|
+
header[0] = type;
|
|
16
|
+
new DataView(header.buffer).setUint32(1, length);
|
|
17
|
+
return header;
|
|
18
|
+
};
|
|
19
|
+
const frame = (type, payload = NOTHING) => {
|
|
20
|
+
const whole = new Uint8Array(HEADER_BYTES + payload.byteLength);
|
|
21
|
+
whole.set(frameHeader(type, payload.byteLength));
|
|
22
|
+
whole.set(payload, HEADER_BYTES);
|
|
23
|
+
return whole;
|
|
24
|
+
};
|
|
25
|
+
const jsonFrame = (type, value) => frame(type, encoder.encode(JSON.stringify(value)));
|
|
26
|
+
/**
|
|
27
|
+
* The app's answer as the host reads it: its headers as they are, and its body in frames. `report` is asked once the
|
|
28
|
+
* body has ended cleanly; a body that fails or is cancelled stops the frames short of `end`.
|
|
29
|
+
*/
|
|
30
|
+
export const framedAnswer = (response,
|
|
31
|
+
/** Given what failed the body, when it failed. */
|
|
32
|
+
report) => {
|
|
33
|
+
const head = {
|
|
34
|
+
status: response.status,
|
|
35
|
+
contentType: response.headers.get('content-type'),
|
|
36
|
+
contentLength: declaredLength(response.headers.get('content-length')),
|
|
37
|
+
cacheControl: response.headers.get('cache-control'),
|
|
38
|
+
vary: response.headers.get('vary'),
|
|
39
|
+
setCookies: response.headers.getSetCookie(),
|
|
40
|
+
};
|
|
41
|
+
const reader = response.body?.getReader();
|
|
42
|
+
const body = new ReadableStream({
|
|
43
|
+
start: controller => controller.enqueue(jsonFrame(FRAME.head, head)),
|
|
44
|
+
async pull(controller) {
|
|
45
|
+
let read;
|
|
46
|
+
try {
|
|
47
|
+
read = reader ? await reader.read() : { done: true, value: undefined };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// The run log keeps a failed answer's input, console and stack, as it does a clean one's.
|
|
51
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
52
|
+
controller.enqueue(frame(FRAME.fail, encoder.encode(message.slice(0, FAILURE_CHARS))));
|
|
53
|
+
controller.enqueue(jsonFrame(FRAME.report, await report(error)));
|
|
54
|
+
controller.close();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (!read.done) {
|
|
58
|
+
controller.enqueue(frameHeader(FRAME.chunk, read.value.byteLength));
|
|
59
|
+
controller.enqueue(read.value);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
controller.enqueue(frame(FRAME.end));
|
|
63
|
+
controller.enqueue(jsonFrame(FRAME.report, await report()));
|
|
64
|
+
controller.close();
|
|
65
|
+
},
|
|
66
|
+
cancel: reason => reader?.cancel(reason),
|
|
67
|
+
});
|
|
68
|
+
const headers = new Headers(response.headers);
|
|
69
|
+
// The frames are longer than the body the app measured.
|
|
70
|
+
headers.delete('content-length');
|
|
71
|
+
return new Response(body, { headers });
|
|
72
|
+
};
|
|
73
|
+
/** An answer that breaks the protocol, which only a broken or hostile build sends. */
|
|
74
|
+
export class MalformedAnswer extends Error {
|
|
75
|
+
}
|
|
76
|
+
/** The app's own body failed partway, with the app's message for why. */
|
|
77
|
+
export class AnswerFailed extends Error {
|
|
78
|
+
}
|
|
79
|
+
// The runtime's own errors are short; one an app raises can carry tens of megabytes, which kept would fill the host.
|
|
80
|
+
const KEPT_MESSAGE_CHARS = 256;
|
|
81
|
+
/** An error from the app's side of the transport, as the host may keep it: short, or only that the answer broke off. */
|
|
82
|
+
const kept = (error) => {
|
|
83
|
+
if (error instanceof MalformedAnswer || error instanceof AnswerFailed)
|
|
84
|
+
return error;
|
|
85
|
+
const message = error?.message;
|
|
86
|
+
return new Error(typeof message === 'string' && message.length <= KEPT_MESSAGE_CHARS
|
|
87
|
+
? message
|
|
88
|
+
: "The app's answer broke off");
|
|
89
|
+
};
|
|
90
|
+
const framesOf = (stream) => {
|
|
91
|
+
const reader = stream.getReader();
|
|
92
|
+
let held = NOTHING;
|
|
93
|
+
/** Up to `max` of the next bytes, as they arrived; none at the stream's end. */
|
|
94
|
+
const some = async (max) => {
|
|
95
|
+
while (!held.byteLength) {
|
|
96
|
+
const read = await reader.read();
|
|
97
|
+
if (read.done)
|
|
98
|
+
return NOTHING;
|
|
99
|
+
held = read.value;
|
|
100
|
+
}
|
|
101
|
+
if (held.byteLength <= max) {
|
|
102
|
+
const all = held;
|
|
103
|
+
held = NOTHING;
|
|
104
|
+
return all;
|
|
105
|
+
}
|
|
106
|
+
// A copy: what stays held shares the read's buffer, which whoever takes this may detach.
|
|
107
|
+
const taken = held.slice(0, max);
|
|
108
|
+
held = held.subarray(max);
|
|
109
|
+
return taken;
|
|
110
|
+
};
|
|
111
|
+
const exactly = async (length, what) => {
|
|
112
|
+
const whole = new Uint8Array(length);
|
|
113
|
+
for (let filled = 0; filled < length;) {
|
|
114
|
+
const piece = await some(length - filled);
|
|
115
|
+
if (!piece.byteLength)
|
|
116
|
+
throw new MalformedAnswer(`The answer ended inside ${what}`);
|
|
117
|
+
whole.set(piece, filled);
|
|
118
|
+
filled += piece.byteLength;
|
|
119
|
+
}
|
|
120
|
+
return whole;
|
|
121
|
+
};
|
|
122
|
+
/** The next frame's type and length; undefined at the stream's end between frames. */
|
|
123
|
+
const next = async () => {
|
|
124
|
+
const start = await some(HEADER_BYTES);
|
|
125
|
+
if (!start.byteLength)
|
|
126
|
+
return undefined;
|
|
127
|
+
const header = new Uint8Array(HEADER_BYTES);
|
|
128
|
+
header.set(start);
|
|
129
|
+
if (start.byteLength < HEADER_BYTES)
|
|
130
|
+
header.set(await exactly(HEADER_BYTES - start.byteLength, 'a frame header'), start.byteLength);
|
|
131
|
+
return {
|
|
132
|
+
type: header[0],
|
|
133
|
+
length: new DataView(header.buffer).getUint32(1),
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
// A stream that already failed has nothing left to stop.
|
|
137
|
+
const cancel = (reason) => reader.cancel(reason).catch(() => { });
|
|
138
|
+
return { some, exactly, next, cancel };
|
|
139
|
+
};
|
|
140
|
+
const isHead = (value) => {
|
|
141
|
+
const head = value;
|
|
142
|
+
return (typeof head?.status === 'number' &&
|
|
143
|
+
Number.isInteger(head.status) &&
|
|
144
|
+
head.status >= 200 &&
|
|
145
|
+
head.status <= 599 &&
|
|
146
|
+
(head.contentType === null || typeof head.contentType === 'string') &&
|
|
147
|
+
(head.contentLength === null ||
|
|
148
|
+
(Number.isSafeInteger(head.contentLength) &&
|
|
149
|
+
head.contentLength >= 0)) &&
|
|
150
|
+
(head.cacheControl === null || typeof head.cacheControl === 'string') &&
|
|
151
|
+
(head.vary === null || typeof head.vary === 'string') &&
|
|
152
|
+
Array.isArray(head.setCookies) &&
|
|
153
|
+
head.setCookies.every(cookie => typeof cookie === 'string'));
|
|
154
|
+
};
|
|
155
|
+
const readHead = async (frames, cap) => {
|
|
156
|
+
const first = await frames.next();
|
|
157
|
+
if (first?.type !== FRAME.head)
|
|
158
|
+
throw new MalformedAnswer("The answer doesn't start with its head");
|
|
159
|
+
if (first.length > cap)
|
|
160
|
+
throw new MalformedAnswer("The answer's head is larger than the host reads");
|
|
161
|
+
const text = decoder.decode(await frames.exactly(first.length, 'its head'));
|
|
162
|
+
let head;
|
|
163
|
+
try {
|
|
164
|
+
head = JSON.parse(text);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
throw new MalformedAnswer("The answer's head isn't JSON");
|
|
168
|
+
}
|
|
169
|
+
if (!isHead(head))
|
|
170
|
+
throw new MalformedAnswer("The answer's head isn't one");
|
|
171
|
+
// Only these: anything else an app padded the head with is let go now, not held as long as its body streams.
|
|
172
|
+
return {
|
|
173
|
+
status: head.status,
|
|
174
|
+
contentType: head.contentType,
|
|
175
|
+
contentLength: head.contentLength,
|
|
176
|
+
cacheControl: head.cacheControl,
|
|
177
|
+
vary: head.vary,
|
|
178
|
+
setCookies: head.setCookies,
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
const readReport = async (frames, cap) => {
|
|
182
|
+
try {
|
|
183
|
+
// Cut off inside its frame's header is missing too, as inside its payload.
|
|
184
|
+
const report = await frames.next().catch(() => undefined);
|
|
185
|
+
if (!report)
|
|
186
|
+
return null;
|
|
187
|
+
if (report.type !== FRAME.report)
|
|
188
|
+
throw new MalformedAnswer(`The answer sent a frame of type ${report.type} after its end`);
|
|
189
|
+
if (report.length > cap) {
|
|
190
|
+
await frames.cancel();
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
// Cut off inside it, as a build stopped mid-report is, the report is missing, not malformed.
|
|
194
|
+
const bytes = await frames
|
|
195
|
+
.exactly(report.length, 'its report')
|
|
196
|
+
.catch(() => null);
|
|
197
|
+
if (!bytes)
|
|
198
|
+
return null;
|
|
199
|
+
const text = decoder.decode(bytes);
|
|
200
|
+
if (await frames.next())
|
|
201
|
+
throw new MalformedAnswer('The answer sent more after its report');
|
|
202
|
+
return text;
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
// A build stopped after `end` sends no report, which leaves the host's word.
|
|
206
|
+
if (error instanceof MalformedAnswer)
|
|
207
|
+
throw error;
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Reads an answer's head, then hands on its body's chunks as they arrive, never holding one, and reads its report once
|
|
213
|
+
* the body has ended. A missing, oversized or malformed head rejects.
|
|
214
|
+
*/
|
|
215
|
+
export const decodeAnswer = async (stream, caps) => {
|
|
216
|
+
if (!stream)
|
|
217
|
+
throw new MalformedAnswer("The answer doesn't start with its head");
|
|
218
|
+
const frames = framesOf(stream);
|
|
219
|
+
let head;
|
|
220
|
+
try {
|
|
221
|
+
head = await readHead(frames, caps.head);
|
|
222
|
+
}
|
|
223
|
+
catch (raw) {
|
|
224
|
+
const error = kept(raw);
|
|
225
|
+
await frames.cancel(error);
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
let settleEnded;
|
|
229
|
+
const ended = new Promise(resolve => (settleEnded = resolve));
|
|
230
|
+
let settleReport;
|
|
231
|
+
const report = new Promise(resolve => (settleReport = resolve));
|
|
232
|
+
// Read for a run's row only; a page's goes unread.
|
|
233
|
+
report.catch(() => { });
|
|
234
|
+
const stopped = (end) => {
|
|
235
|
+
settleEnded(end);
|
|
236
|
+
settleReport(Promise.resolve(null));
|
|
237
|
+
};
|
|
238
|
+
// Of the chunk now passing through.
|
|
239
|
+
let left = 0;
|
|
240
|
+
const body = new ReadableStream({
|
|
241
|
+
async pull(controller) {
|
|
242
|
+
try {
|
|
243
|
+
for (;;) {
|
|
244
|
+
if (left > 0) {
|
|
245
|
+
const piece = await frames.some(left);
|
|
246
|
+
if (!piece.byteLength)
|
|
247
|
+
throw new MalformedAnswer('The answer ended inside a chunk');
|
|
248
|
+
left -= piece.byteLength;
|
|
249
|
+
controller.enqueue(piece);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const next = await frames.next();
|
|
253
|
+
if (next?.type === FRAME.chunk) {
|
|
254
|
+
left = next.length;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (next?.type === FRAME.end && next.length === 0) {
|
|
258
|
+
controller.close();
|
|
259
|
+
settleEnded({ outcome: 'ok' });
|
|
260
|
+
settleReport(readReport(frames, caps.report));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (next?.type === FRAME.fail) {
|
|
264
|
+
if (next.length > FAILURE_BYTES)
|
|
265
|
+
throw new MalformedAnswer("The answer's failure is longer than the host reads");
|
|
266
|
+
const error = new AnswerFailed(decoder.decode(await frames.exactly(next.length, 'its failure')));
|
|
267
|
+
controller.error(error);
|
|
268
|
+
settleEnded({ outcome: 'error', error });
|
|
269
|
+
settleReport(readReport(frames, caps.report));
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
throw new MalformedAnswer(next
|
|
273
|
+
? `The answer sent a frame of type ${next.type} in its body`
|
|
274
|
+
: 'The answer ended without its end');
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch (raw) {
|
|
278
|
+
const error = kept(raw);
|
|
279
|
+
controller.error(error);
|
|
280
|
+
stopped({ outcome: 'error', error });
|
|
281
|
+
await frames.cancel(error);
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
cancel(reason) {
|
|
285
|
+
stopped({ outcome: 'canceled' });
|
|
286
|
+
return frames.cancel(reason);
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
return { head, body, ended, report };
|
|
290
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CallRecord, HostRequestState, PlatformCallInput, PlatformCallResult, RunRecord } from '../types.js';
|
|
1
|
+
import type { CallRecord, HostRequestState, PlatformCallInput, PlatformCallResult, RunRecord, RunReport } from '../types.js';
|
|
2
2
|
export declare const preview: (value: unknown) => string | undefined;
|
|
3
3
|
export declare const errorOf: (error: unknown) => NonNullable<RunRecord["error"]>;
|
|
4
4
|
/** Which call: what the run log names it by. */
|
|
@@ -31,10 +31,12 @@ export declare const ranTo: (outcome: {
|
|
|
31
31
|
} | {
|
|
32
32
|
error: unknown;
|
|
33
33
|
}) => Pick<RunRecord, "output" | "error">;
|
|
34
|
+
/** The request's report: its top-level run, the one that failed if any did, and its console; null when it has neither. */
|
|
35
|
+
export declare const runReportOf: (host: HostRequestState) => RunReport | null;
|
|
34
36
|
/** One run of app code for the request's run log, timed from `startedAt` to now. */
|
|
35
37
|
export declare const recordRun: (host: HostRequestState | undefined, run: Omit<RunRecord, "durationMs">, endedAt?: number) => void;
|
|
36
38
|
/**
|
|
37
39
|
* A server route's run, which run capture doesn't see: its body in, and its status and body out. The bodies are
|
|
38
|
-
* read from copies after the fact, so the response never waits; the
|
|
40
|
+
* read from copies after the fact, so the response never waits; the request's report waits for them.
|
|
39
41
|
*/
|
|
40
42
|
export declare function recordRoute<T>(host: HostRequestState | undefined, request: Request, run: () => Promise<T>): Promise<T>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asZiteError } from '../errors.js';
|
|
2
2
|
import { textUpTo } from './caps.js';
|
|
3
|
-
import {
|
|
3
|
+
import { sendEvents } from './telemetry.js';
|
|
4
4
|
// Classic's per-field cap. The host caps again; this keeps a large value from crossing at all.
|
|
5
5
|
const RUN_FIELD_LIMIT = 50 * 1024;
|
|
6
6
|
const capped = (text) => text.length > RUN_FIELD_LIMIT ? `${text.slice(0, RUN_FIELD_LIMIT)}…` : text;
|
|
@@ -21,7 +21,7 @@ export const preview = (value) => {
|
|
|
21
21
|
};
|
|
22
22
|
export const errorOf = (error) => ({
|
|
23
23
|
code: asZiteError(error)?.code ?? 'INTERNAL_ERROR',
|
|
24
|
-
message: error instanceof Error ? error.message : String(error),
|
|
24
|
+
message: capped(error instanceof Error ? error.message : String(error)),
|
|
25
25
|
...(error instanceof Error && error.stack
|
|
26
26
|
? { stack: capped(error.stack) }
|
|
27
27
|
: {}),
|
|
@@ -76,7 +76,7 @@ const report = (host, record) => {
|
|
|
76
76
|
(host.calls ??= []).push(record);
|
|
77
77
|
return;
|
|
78
78
|
}
|
|
79
|
-
const sent =
|
|
79
|
+
const sent = sendEvents(host, { calls: [record] }).catch((error) => console.error("[zite] couldn't record a platform call", error));
|
|
80
80
|
host.waitUntil?.(sent);
|
|
81
81
|
};
|
|
82
82
|
/** A call that left the app for a service, failures included, reported for its run as it ends. */
|
|
@@ -90,6 +90,19 @@ export const reportedCall = async (host, input, run) => {
|
|
|
90
90
|
export const ranTo = (outcome) => 'error' in outcome
|
|
91
91
|
? { error: errorOf(outcome.error) }
|
|
92
92
|
: { output: preview(outcome.output) };
|
|
93
|
+
/** The request's report: its top-level run, the one that failed if any did, and its console; null when it has neither. */
|
|
94
|
+
export const runReportOf = (host) => {
|
|
95
|
+
const top = host.runs.filter(run => !run.nested);
|
|
96
|
+
const run = top.find(({ error }) => error) ?? top[0];
|
|
97
|
+
if (!run && !host.console?.length)
|
|
98
|
+
return null;
|
|
99
|
+
return {
|
|
100
|
+
...(run?.input !== undefined && { input: run.input }),
|
|
101
|
+
...(run?.output !== undefined && { output: run.output }),
|
|
102
|
+
...(run?.error && { error: run.error }),
|
|
103
|
+
...(host.console?.length && { console: host.console }),
|
|
104
|
+
};
|
|
105
|
+
};
|
|
93
106
|
/** One run of app code for the request's run log, timed from `startedAt` to now. */
|
|
94
107
|
export const recordRun = (host, run, endedAt = Date.now()) => {
|
|
95
108
|
host?.runs.push({ ...run, durationMs: endedAt - run.startedAt });
|
|
@@ -107,7 +120,7 @@ const bodyText = async (message) => {
|
|
|
107
120
|
};
|
|
108
121
|
/**
|
|
109
122
|
* A server route's run, which run capture doesn't see: its body in, and its status and body out. The bodies are
|
|
110
|
-
* read from copies after the fact, so the response never waits; the
|
|
123
|
+
* read from copies after the fact, so the response never waits; the request's report waits for them.
|
|
111
124
|
*/
|
|
112
125
|
export async function recordRoute(host, request, run) {
|
|
113
126
|
if (!host)
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import type { CallRecord, FetchRecord, HostRequestState
|
|
2
|
-
export type
|
|
3
|
-
runs: RunRecord[];
|
|
1
|
+
import type { CallRecord, FetchRecord, HostRequestState } from '../types.js';
|
|
2
|
+
export type EventReport = {
|
|
4
3
|
calls?: CallRecord[];
|
|
5
4
|
fetches?: FetchRecord[];
|
|
6
5
|
};
|
|
7
6
|
/** The report in as few sends as fit under the runs service's limit, each record whole. */
|
|
8
|
-
export declare const
|
|
7
|
+
export declare const eventSends: (report: EventReport) => EventReport[];
|
|
9
8
|
/**
|
|
10
|
-
* One report of the request's
|
|
11
|
-
* dispatched without a runs URL or a token has nowhere to send it, which the throw says.
|
|
9
|
+
* One report of the request's calls and fetches; the caller keeps it going past the response with `waitUntil`. A
|
|
10
|
+
* request the host dispatched without a runs URL or a token has nowhere to send it, which the throw says.
|
|
12
11
|
*/
|
|
13
|
-
export declare const
|
|
12
|
+
export declare const sendEvents: (host: HostRequestState, report: EventReport) => Promise<void>;
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
// What a request reports
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// request of its own.
|
|
1
|
+
// What a request reports beside its run: the platform calls it made and its outbound fetches with what its own AI keys
|
|
2
|
+
// used. On the app token to the platform's runs service, which stamps the org, app and request from the token, never
|
|
3
|
+
// from the report. `local` keeps none: a dev token names no request of its own.
|
|
5
4
|
import { sendOnToken } from './transport.js';
|
|
6
5
|
// Half of what the runs service reads in one send; each record is capped far below it.
|
|
7
6
|
const MAX_SEND_BYTES = 512 * 1024;
|
|
8
7
|
const RUNS_TIMEOUT_MS = 10_000;
|
|
9
8
|
const encoder = new TextEncoder();
|
|
10
|
-
const emptySend = () => ({
|
|
11
|
-
const asReport = ({
|
|
12
|
-
runs,
|
|
9
|
+
const emptySend = () => ({ calls: [], fetches: [] });
|
|
10
|
+
const asReport = ({ calls, fetches }) => ({
|
|
13
11
|
...(calls.length > 0 && { calls }),
|
|
14
12
|
...(fetches.length > 0 && { fetches }),
|
|
15
13
|
});
|
|
16
14
|
/** The report in as few sends as fit under the runs service's limit, each record whole. */
|
|
17
|
-
export const
|
|
15
|
+
export const eventSends = (report) => {
|
|
18
16
|
const sends = [];
|
|
19
17
|
let send = emptySend();
|
|
20
18
|
let bytes = 0;
|
|
@@ -28,8 +26,6 @@ export const reportSends = (report) => {
|
|
|
28
26
|
add(send);
|
|
29
27
|
bytes += size;
|
|
30
28
|
};
|
|
31
|
-
for (const run of report.runs)
|
|
32
|
-
place(run, into => into.runs.push(run));
|
|
33
29
|
for (const call of report.calls ?? [])
|
|
34
30
|
place(call, into => into.calls.push(call));
|
|
35
31
|
for (const fetched of report.fetches ?? [])
|
|
@@ -39,7 +35,7 @@ export const reportSends = (report) => {
|
|
|
39
35
|
return sends;
|
|
40
36
|
};
|
|
41
37
|
const toRunsService = async (host, runs, token, report) => {
|
|
42
|
-
const answers = await Promise.all(
|
|
38
|
+
const answers = await Promise.all(eventSends(report).map(send => sendOnToken(token, `${runs}/ingest`, {
|
|
43
39
|
method: 'POST',
|
|
44
40
|
body: JSON.stringify({
|
|
45
41
|
kind: 'events',
|
|
@@ -56,16 +52,16 @@ const toRunsService = async (host, runs, token, report) => {
|
|
|
56
52
|
throw new Error(`the runs service answered ${refused.status}`);
|
|
57
53
|
};
|
|
58
54
|
/**
|
|
59
|
-
* One report of the request's
|
|
60
|
-
* dispatched without a runs URL or a token has nowhere to send it, which the throw says.
|
|
55
|
+
* One report of the request's calls and fetches; the caller keeps it going past the response with `waitUntil`. A
|
|
56
|
+
* request the host dispatched without a runs URL or a token has nowhere to send it, which the throw says.
|
|
61
57
|
*/
|
|
62
|
-
export const
|
|
58
|
+
export const sendEvents = async (host, report) => {
|
|
63
59
|
const { provider } = host;
|
|
64
60
|
if (!provider || provider.kind === 'local')
|
|
65
61
|
return;
|
|
66
62
|
const runs = provider.urls?.runs;
|
|
67
63
|
const token = runs ? await provider.token() : undefined;
|
|
68
64
|
if (!runs || !token)
|
|
69
|
-
throw new Error(
|
|
65
|
+
throw new Error('This request has no runs URL or app token to send its calls and fetches with');
|
|
70
66
|
return toRunsService(host, runs, token, report);
|
|
71
67
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { isReservedIntegrationId, RESERVED_INTEGRATION_IDS, } from '../internal/reservedIds.js';
|
|
2
2
|
export { authHookNameSchema, describedAccessSchema, describedFunctionSchema, describedPageSchema, describedRouteSchema, describedScheduleSchema, describedTriggerSchema, describedWorkflowSchema, describeOutputSchema, } from './describe/schema.js';
|
|
3
|
-
export
|
|
3
|
+
export { AnswerFailed, decodeAnswer, FRAME, framedAnswer, MalformedAnswer, } from './frames.js';
|
|
4
|
+
export type { AnswerCaps, AnswerHead, BodyEnd, DecodedAnswer, } from './frames.js';
|
|
5
|
+
export type { AppContext, ConsoleRecord, ExpiringToken, HostConnection, HostMode, HostPage, HostProps, PlatformBinding, RunReport, SiteHead, WebManifest, } from './types.js';
|
|
4
6
|
/** What the host dispatches a request to do beside serving it; without props, only `warm` and `describe`. */
|
|
5
7
|
export declare const INVOKE_KINDS: readonly ["webhook", "describe", "workflow", "register", "warm", "authHook", "function"];
|
|
6
8
|
export type InvokeKind = (typeof INVOKE_KINDS)[number];
|
|
@@ -9,8 +11,6 @@ export declare const isInvokeKind: (kind: unknown) => kind is InvokeKind;
|
|
|
9
11
|
export declare const INVOKE_HEADER = "x-zite-invoke";
|
|
10
12
|
/** Where the host POSTs a workflow, registration, auth hook or test run, with `{ input }`. */
|
|
11
13
|
export declare const INVOKE_PATH = "/_zite/invoke";
|
|
12
|
-
/** On describe's answer: the zitejs the build's server code was bundled from. */
|
|
13
|
-
export declare const RUNTIME_VERSION_HEADER = "x-zitejs-version";
|
|
14
14
|
/** An editor preview's access, on every same-origin fetch its pages make. */
|
|
15
15
|
export declare const PREVIEW_CLAIM_HEADER = "x-zite-preview";
|
|
16
16
|
/** At the gateway, the connection a call on Zite's AI key bills to. */
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// What a Start build and the platform's host say to each other on the wire. Import-free but for the reserved ids
|
|
2
|
-
// describe's output schema, which imports only zod and the auth hook names, so the host takes these
|
|
3
|
-
// anything of zitejs.
|
|
1
|
+
// What a Start build and the platform's host say to each other on the wire. Import-free but for the reserved ids, the
|
|
2
|
+
// answer's frames and describe's output schema, which imports only zod and the auth hook names, so the host takes these
|
|
3
|
+
// without loading anything of zitejs.
|
|
4
4
|
export { isReservedIntegrationId, RESERVED_INTEGRATION_IDS, } from '../internal/reservedIds.js';
|
|
5
5
|
export { authHookNameSchema, describedAccessSchema, describedFunctionSchema, describedPageSchema, describedRouteSchema, describedScheduleSchema, describedTriggerSchema, describedWorkflowSchema, describeOutputSchema, } from './describe/schema.js';
|
|
6
|
+
export { AnswerFailed, decodeAnswer, FRAME, framedAnswer, MalformedAnswer, } from './frames.js';
|
|
6
7
|
/** What the host dispatches a request to do beside serving it; without props, only `warm` and `describe`. */
|
|
7
8
|
export const INVOKE_KINDS = [
|
|
8
9
|
'webhook',
|
|
@@ -18,8 +19,6 @@ export const isInvokeKind = (kind) => INVOKE_KINDS.includes(kind);
|
|
|
18
19
|
export const INVOKE_HEADER = 'x-zite-invoke';
|
|
19
20
|
/** Where the host POSTs a workflow, registration, auth hook or test run, with `{ input }`. */
|
|
20
21
|
export const INVOKE_PATH = '/_zite/invoke';
|
|
21
|
-
/** On describe's answer: the zitejs the build's server code was bundled from. */
|
|
22
|
-
export const RUNTIME_VERSION_HEADER = 'x-zitejs-version';
|
|
23
22
|
/** An editor preview's access, on every same-origin fetch its pages make. */
|
|
24
23
|
export const PREVIEW_CLAIM_HEADER = 'x-zite-preview';
|
|
25
24
|
/** At the gateway, the connection a call on Zite's AI key bills to. */
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps console once per isolate, before any request, so each request's console goes in its own report. A
|
|
3
|
+
* registration's `withoutConsole` wraps on first use, outside this, so what it drops never arrives here.
|
|
4
|
+
*/
|
|
5
|
+
export declare const installConsoleCapture: () => void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Server-only. A request's console, for its report, kept as classic's run log keeps one: each message up to 50 KB, and
|
|
2
|
+
// 100 KB of messages in all, past which one line says the rest went unrecorded. Outside a request nothing is kept.
|
|
3
|
+
import { getHost } from './scope.js';
|
|
4
|
+
const LEVELS = ['log', 'info', 'warn', 'error'];
|
|
5
|
+
const MESSAGE_LIMIT = 50 * 1024;
|
|
6
|
+
const CONSOLE_LIMIT = 100 * 1024;
|
|
7
|
+
const textOf = (arg) => {
|
|
8
|
+
if (typeof arg === 'string')
|
|
9
|
+
return arg;
|
|
10
|
+
// An Error has no enumerable fields, so JSON would print `{}`.
|
|
11
|
+
if (arg instanceof Error)
|
|
12
|
+
return `${arg.name}: ${arg.message}`;
|
|
13
|
+
try {
|
|
14
|
+
return JSON.stringify(arg) ?? String(arg);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return '[non-serializable]';
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const keep = (host, level, args) => {
|
|
21
|
+
const used = host.consoleChars ?? 0;
|
|
22
|
+
if (used >= CONSOLE_LIMIT)
|
|
23
|
+
return;
|
|
24
|
+
const whole = args.map(textOf).join(' ');
|
|
25
|
+
const message = whole.length > MESSAGE_LIMIT
|
|
26
|
+
? `${whole.slice(0, MESSAGE_LIMIT)}...[truncated]`
|
|
27
|
+
: whole;
|
|
28
|
+
const records = (host.console ??= []);
|
|
29
|
+
records.push({ level, message, at: Date.now() });
|
|
30
|
+
host.consoleChars = used + message.length;
|
|
31
|
+
if (host.consoleChars >= CONSOLE_LIMIT) {
|
|
32
|
+
records.push({
|
|
33
|
+
level: 'warn',
|
|
34
|
+
message: `[zite] Console output reached the ${CONSOLE_LIMIT / 1024}KB log limit; later console output is not recorded.`,
|
|
35
|
+
at: Date.now(),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// Keyed on globalThis so two copies of zitejs in one bundle wrap console once.
|
|
40
|
+
const CAPTURE_KEY = Symbol.for('zitejs:console-capture');
|
|
41
|
+
/**
|
|
42
|
+
* Wraps console once per isolate, before any request, so each request's console goes in its own report. A
|
|
43
|
+
* registration's `withoutConsole` wraps on first use, outside this, so what it drops never arrives here.
|
|
44
|
+
*/
|
|
45
|
+
export const installConsoleCapture = () => {
|
|
46
|
+
const stores = globalThis;
|
|
47
|
+
if (stores[CAPTURE_KEY])
|
|
48
|
+
return;
|
|
49
|
+
stores[CAPTURE_KEY] = true;
|
|
50
|
+
const methods = console;
|
|
51
|
+
for (const level of LEVELS) {
|
|
52
|
+
const original = methods[level];
|
|
53
|
+
methods[level] = (...args) => {
|
|
54
|
+
original.apply(console, args);
|
|
55
|
+
const host = getHost();
|
|
56
|
+
if (host)
|
|
57
|
+
keep(host, level, args);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// The generated server entry's first import: an SDK client an app builds at load (`new OpenAI()` at a module's top
|
|
2
|
-
// level) keeps the fetch it found then, so the wrapper must already be in place, and so must process.env's view
|
|
2
|
+
// level) keeps the fetch it found then, so the wrapper must already be in place, and so must process.env's view and
|
|
3
|
+
// the console a logger binds at load.
|
|
3
4
|
import { watchOutboundFetches } from '../platform/outbound.js';
|
|
5
|
+
import { installConsoleCapture } from './console.js';
|
|
4
6
|
import { installRequestEnv } from './requestEnv.js';
|
|
5
7
|
watchOutboundFetches();
|
|
6
8
|
installRequestEnv();
|
|
9
|
+
installConsoleCapture();
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// A streamed answer can outlive the request's deadline, which the token's expiry follows.
|
|
2
2
|
const RENEW_WITHIN_MS = 30_000;
|
|
3
|
+
// The platform binding answers its one question whatever it's asked.
|
|
4
|
+
const APP_TOKEN_URL = 'https://platform/app-token';
|
|
3
5
|
const expiresWithin = (expiresAt, ms) => expiresAt !== undefined && expiresAt - Date.now() < ms;
|
|
4
6
|
export const isExpiringToken = (value) => {
|
|
5
7
|
const given = value;
|
|
@@ -61,7 +63,7 @@ export const ziteProvider = (props) => {
|
|
|
61
63
|
held: () => current,
|
|
62
64
|
keep: fresh => (current = fresh),
|
|
63
65
|
renew: async () => {
|
|
64
|
-
const fresh = await props.platform.
|
|
66
|
+
const fresh = await (await props.platform.fetch(APP_TOKEN_URL)).json();
|
|
65
67
|
return isExpiringToken(fresh) ? fresh : null;
|
|
66
68
|
},
|
|
67
69
|
withinMs: RENEW_WITHIN_MS,
|