@vite-hub/runtime 0.4.1 → 0.5.1

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.
Files changed (57) hide show
  1. package/dist/core-Crv8CV_r.js +1103 -0
  2. package/dist/core-auTkK4iZ.d.ts +298 -0
  3. package/dist/durability.d.ts +3 -5
  4. package/dist/durability.js +5 -3
  5. package/dist/{error-diagnostics-BM9W4pBg.js → error-diagnostics-Dj98U7ch.js} +1 -2
  6. package/dist/{error-diagnostics-0sYgKTT8.d.ts → error-diagnostics-DuusexaK.d.ts} +0 -2
  7. package/dist/{errors-BQ0ofIL4.js → errors-D2ixvqv3.js} +2 -3
  8. package/dist/{execution-budget-BPXbISEt.d.ts → execution-budget-Bv7h0-5V.d.ts} +0 -2
  9. package/dist/hosted-console-DTh9zwNz.d.ts +28 -0
  10. package/dist/index-hFKl7B_y.d.ts +345 -0
  11. package/dist/index.d.ts +7 -5
  12. package/dist/index.js +6 -5
  13. package/dist/internal/console.d.ts +5 -0
  14. package/dist/internal/console.js +1 -0
  15. package/dist/internal/context.d.ts +2 -0
  16. package/dist/internal/context.js +1 -0
  17. package/dist/internal/drain.d.ts +1 -3
  18. package/dist/internal/drain.js +3 -5
  19. package/dist/internal/error-diagnostics.d.ts +2 -0
  20. package/dist/internal/error-diagnostics.js +1 -0
  21. package/dist/internal/execution-budget.d.ts +2 -3
  22. package/dist/internal/execution-budget.js +226 -3
  23. package/dist/internal/grant.d.ts +1 -2
  24. package/dist/internal/grant.js +3 -4
  25. package/dist/internal/h3.d.ts +2 -4
  26. package/dist/internal/h3.js +4 -12
  27. package/dist/internal/node.d.ts +2 -3
  28. package/dist/internal/node.js +2 -3
  29. package/dist/internal/public-url.d.ts +2 -2
  30. package/dist/internal/public-url.js +1 -2
  31. package/dist/internal/response.d.ts +2 -0
  32. package/dist/internal/response.js +1 -0
  33. package/dist/internal/run-failed.d.ts +1 -3
  34. package/dist/internal/run-failed.js +1 -3
  35. package/dist/internal/run-scope.d.ts +1 -3
  36. package/dist/internal/run-scope.js +1 -2
  37. package/dist/internal/runtime-type.d.ts +1 -3
  38. package/dist/internal/runtime-type.js +2 -2
  39. package/dist/internal/shutdown.d.ts +1 -3
  40. package/dist/internal/shutdown.js +18 -21
  41. package/dist/internal/test-mode-setup.d.ts +1 -3
  42. package/dist/internal/test-mode-setup.js +1 -6
  43. package/dist/internal/test-mode.d.ts +1 -3
  44. package/dist/internal/test-mode.js +1 -2
  45. package/dist/internal/trace.d.ts +4 -0
  46. package/dist/internal/trace.js +1 -0
  47. package/dist/operations.d.ts +7 -4
  48. package/dist/operations.js +14 -11
  49. package/dist/{public-url-1WeOw54Z.d.ts → public-url-B4p8EOqr.d.ts} +6 -3
  50. package/dist/{public-url-ByAmAjbP.js → public-url-DDh_BHz3.js} +27 -22
  51. package/dist/request-context-Ci5FjuUY.js +680 -0
  52. package/dist/response-ClQ9ZSS_.d.ts +31 -0
  53. package/dist/response-DEyMvEBF.js +430 -0
  54. package/dist/{runtime-type-BftydMAf.js → runtime-type-EgaBGoVU.js} +2 -11
  55. package/package.json +29 -4
  56. package/dist/index-C1DoYfCf.d.ts +0 -733
  57. package/dist/request-context-uRQpodgl.js +0 -2433
@@ -0,0 +1,31 @@
1
+ /** A JSON-safe representation of a web Response for durable boundaries. */
2
+ interface SerializedResponse {
3
+ readonly body: {
4
+ readonly data: string;
5
+ readonly encoding: "base64";
6
+ readonly mediaType: string;
7
+ readonly isNull?: boolean;
8
+ };
9
+ /** Header entries are kept as pairs so repeated headers are preserved. */
10
+ readonly headers: readonly (readonly [string, string])[];
11
+ readonly status: number;
12
+ readonly statusText: string;
13
+ readonly url?: string;
14
+ readonly type?: "error" | "opaque" | "opaqueredirect";
15
+ }
16
+ /**
17
+ * Normalize a primitive value into the native Web Response contract.
18
+ * An error answers with `{ code, why, fix }` and never with its `stack`, `cause`, or `data`:
19
+ * - A ResultError or other Nostics diagnostic answers its `status`, or 500, so a route can `return toResponse(error)`.
20
+ * - A `ViteHubError` answers 500 with its `code` and its message as `why`.
21
+ * - Any other `Error` answers 500 with `RUNTIME_HIDDEN_ERROR`, because its message can hold server details.
22
+ */
23
+ declare function toResponse(value: unknown): Response;
24
+ /** Recognize Responses created by another realm from their standard tag and surface. */
25
+ declare function isResponse(value: unknown): value is Response;
26
+ /** Convert a native Response into a JSON-safe record. The body is fully buffered. */
27
+ declare function serializeResponse(response: Response): Promise<SerializedResponse>;
28
+ /** Reconstruct a native Response from a durable response record. */
29
+ declare function deserializeResponse(value: SerializedResponse): Response;
30
+ declare function isSerializedResponse(value: unknown): value is SerializedResponse;
31
+ export { serializeResponse as a, isSerializedResponse as i, deserializeResponse as n, toResponse as o, isResponse as r, SerializedResponse as t };
@@ -0,0 +1,430 @@
1
+ import { r as isRuntimeObject } from "./runtime-type-EgaBGoVU.js";
2
+ import { n as getViteHubErrorShape } from "./errors-D2ixvqv3.js";
3
+ import { t as runtimeErrorDiagnostics } from "./error-diagnostics-Dj98U7ch.js";
4
+ import { Diagnostic, formatDiagnostic } from "nostics";
5
+ // src/diagnostics.ts
6
+ /** Recognizes an `Error` from any realm, without reading its properties. */
7
+ function isNativeError(value) {
8
+ if (!isRuntimeObject(value)) return false;
9
+ try {
10
+ const nativeIsError = Reflect.get(Error, "isError");
11
+ return typeof nativeIsError === "function" ? nativeIsError(value) === true : value instanceof Error || !(Symbol.toStringTag in value) && Object.prototype.toString.call(value) === "[object Error]";
12
+ } catch {
13
+ return false;
14
+ }
15
+ }
16
+ /**
17
+ * Recognizes a live Nostics error across bundled copies. This does not make its fields safe for public responses.
18
+ * nostics' own check matches only its own copy (https://github.com/vercel-labs/nostics/pull/40).
19
+ */
20
+ function isRuntimeDiagnostic(error) {
21
+ if (!isNativeError(error)) return false;
22
+ try {
23
+ const message = Reflect.get(error, "message");
24
+ const code = Reflect.get(error, "code");
25
+ const name = Reflect.get(error, "name");
26
+ const why = Reflect.get(error, "why");
27
+ const fix = Reflect.get(error, "fix");
28
+ const docs = Reflect.get(error, "docs");
29
+ const sources = Reflect.get(error, "sources");
30
+ return typeof code === "string" && code.length > 0 && typeof name === "string" && typeof message === "string" && why === message && (fix === void 0 || typeof fix === "string") && (docs === void 0 || typeof docs === "string") && (sources === void 0 || Array.isArray(sources) && sources.every((source) => typeof source === "string")) && typeof Reflect.get(error, "toJSON") === "function";
31
+ } catch {
32
+ return false;
33
+ }
34
+ }
35
+ function boundedString(value, state) {
36
+ if (typeof value !== "string") return;
37
+ const maximum = Math.min(state.maxStringLength, state.remainingStringLength);
38
+ if (maximum <= 0) return;
39
+ if (value.length <= maximum) {
40
+ state.remainingStringLength -= value.length;
41
+ return value;
42
+ }
43
+ const suffix = `… [${value.length - maximum} chars omitted]`;
44
+ const bounded = maximum > suffix.length ? `${value.slice(0, maximum - suffix.length)}${suffix}` : value.slice(0, maximum);
45
+ state.remainingStringLength -= bounded.length;
46
+ return bounded;
47
+ }
48
+ function safeString(value) {
49
+ try {
50
+ return String(value);
51
+ } catch {
52
+ return "Unknown error";
53
+ }
54
+ }
55
+ function readProperty(value, key) {
56
+ if (!isRuntimeObject(value)) return void 0;
57
+ try {
58
+ return value[key];
59
+ } catch {
60
+ return;
61
+ }
62
+ }
63
+ function scalarProperty(value, key, state) {
64
+ const property = readProperty(value, key);
65
+ if (typeof property === "string") return boundedString(property, state);
66
+ return typeof property === "number" && Number.isFinite(property) ? property : void 0;
67
+ }
68
+ function errorChildren(value, maximum) {
69
+ try {
70
+ if (!Array.isArray(value)) return;
71
+ const length = Math.min(value.length, maximum);
72
+ return Array.from({ length }, (_item, index) => readProperty(value, index));
73
+ } catch {
74
+ return [];
75
+ }
76
+ }
77
+ function normalizeDetailValue(value, state, depth) {
78
+ if (state.remainingNodes <= 0) return;
79
+ state.remainingNodes -= 1;
80
+ if (value === null || typeof value === "boolean") return value;
81
+ if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
82
+ if (typeof value === "string") return boundedString(value, state);
83
+ if (depth > state.maxDepth) return boundedString("[Detail depth exceeded]", state);
84
+ if (Array.isArray(value)) return value.flatMap((item) => {
85
+ const normalized = normalizeDetailValue(item, state, depth + 1);
86
+ return normalized === void 0 ? [] : [normalized];
87
+ });
88
+ if (!isRuntimeObject(value)) return;
89
+ const normalized = {};
90
+ let keys;
91
+ try {
92
+ keys = Object.keys(value);
93
+ } catch {
94
+ return normalized;
95
+ }
96
+ for (const key of keys) {
97
+ const normalizedKey = boundedString(key, state);
98
+ if (!normalizedKey) break;
99
+ const child = normalizeDetailValue(readProperty(value, key), state, depth + 1);
100
+ if (child !== void 0) normalized[normalizedKey] = child;
101
+ }
102
+ return normalized;
103
+ }
104
+ function normalizeDetails(details, state, depth) {
105
+ const normalized = normalizeDetailValue(details, state, depth);
106
+ if (!isRuntimeObject(normalized) || Array.isArray(normalized)) return;
107
+ return normalized;
108
+ }
109
+ function normalizeError(value, state, depth) {
110
+ if (state.remainingNodes <= 0) return;
111
+ state.remainingNodes -= 1;
112
+ if (typeof value === "string") return { message: boundedString(value, state) || "Error" };
113
+ if (!value || typeof value !== "object") return { message: boundedString(safeString(value), state) || "Error" };
114
+ if (state.ancestors.has(value)) return { message: boundedString("[Circular error cause]", state) || "Error" };
115
+ if (depth > state.maxDepth) return { message: boundedString("[Error cause depth exceeded]", state) || "Error" };
116
+ state.ancestors.add(value);
117
+ try {
118
+ const publicError = getViteHubErrorShape(value);
119
+ const why = readProperty(value, "why");
120
+ const message = boundedString(readProperty(value, "message"), state) || boundedString(why, state) || boundedString(safeString(value), state) || "Error";
121
+ const name = boundedString(readProperty(value, "name"), state) || boundedString(readProperty(readProperty(value, "constructor") || {}, "name"), state);
122
+ const result = {
123
+ message,
124
+ ...name ? { name } : {}
125
+ };
126
+ const code = publicError?.code ? boundedString(publicError.code, state) : scalarProperty(value, "code", state) ?? (typeof why === "string" ? boundedString(name, state) : void 0);
127
+ const fix = boundedString(readProperty(value, "fix"), state);
128
+ const docs = boundedString(readProperty(value, "docs"), state);
129
+ const requestId = publicError?.requestId ? boundedString(publicError.requestId, state) : boundedString(readProperty(value, "requestId"), state);
130
+ const status = scalarProperty(value, "status", state);
131
+ const statusCode = scalarProperty(value, "statusCode", state);
132
+ const stack = state.includeStack ? boundedString(readProperty(value, "stack"), state) : void 0;
133
+ if (code !== void 0) result.code = code;
134
+ if (fix) result.fix = fix;
135
+ if (docs) result.docs = docs;
136
+ if (requestId) result.requestId = requestId;
137
+ if (stack) result.stack = stack;
138
+ if (status !== void 0) result.status = status;
139
+ if (statusCode !== void 0) result.statusCode = statusCode;
140
+ const cause = readProperty(value, "cause");
141
+ const normalizedCause = cause === void 0 ? void 0 : normalizeError(cause, state, depth + 1);
142
+ if (normalizedCause) result.cause = normalizedCause;
143
+ const normalizedErrors = errorChildren(readProperty(value, "errors"), state.remainingNodes)?.flatMap((error) => {
144
+ const normalized = normalizeError(error, state, depth + 1);
145
+ return normalized ? [normalized] : [];
146
+ });
147
+ if (normalizedErrors?.length) result.errors = normalizedErrors;
148
+ const details = publicError?.details ? normalizeDetails(publicError.details, state, depth + 1) : void 0;
149
+ if (details) result.details = details;
150
+ const sources = errorChildren(readProperty(value, "sources"), state.remainingNodes)?.flatMap((source) => {
151
+ state.remainingNodes -= 1;
152
+ const normalized = boundedString(source, state);
153
+ return normalized ? [normalized] : [];
154
+ });
155
+ if (sources?.length) result.sources = sources;
156
+ return result;
157
+ } finally {
158
+ state.ancestors.delete(value);
159
+ }
160
+ }
161
+ /** Formats bounded developer diagnostics without exposing causes or stacks. */
162
+ function formatRuntimeDiagnosticError(error) {
163
+ const normalized = normalizeRuntimeDiagnosticError(error);
164
+ if (normalized.code === void 0 && !normalized.fix && !normalized.docs && !normalized.sources?.length) return normalized.message;
165
+ return formatDiagnostic(new Diagnostic({
166
+ code: normalized.code === void 0 ? normalized.name || "Error" : String(normalized.code),
167
+ docs: normalized.docs,
168
+ fix: normalized.fix,
169
+ sources: normalized.sources ? [...normalized.sources] : void 0,
170
+ why: normalized.message
171
+ }));
172
+ }
173
+ function normalizeRuntimeDiagnosticError(error, options = {}) {
174
+ const maxDepth = options.maxDepth ?? 4;
175
+ const maxErrors = options.maxErrors ?? 8;
176
+ const maxStringLength = options.maxStringLength ?? 4096;
177
+ if (!Number.isInteger(maxDepth) || maxDepth < 0) throw runtimeErrorDiagnostics.RUNTIME_R0001({ message: "[vitehub:runtime] Diagnostic error maxDepth must be a non-negative integer." });
178
+ if (!Number.isInteger(maxErrors) || maxErrors < 1) throw runtimeErrorDiagnostics.RUNTIME_R0002({ message: "[vitehub:runtime] Diagnostic error maxErrors must be a positive integer." });
179
+ if (!Number.isInteger(maxStringLength) || maxStringLength < 64) throw runtimeErrorDiagnostics.RUNTIME_R0003({ message: "[vitehub:runtime] Diagnostic error maxStringLength must be an integer of at least 64." });
180
+ return normalizeError(error, {
181
+ ancestors: /* @__PURE__ */ new Set(),
182
+ includeStack: options.includeStack === true,
183
+ maxDepth,
184
+ maxStringLength,
185
+ remainingNodes: maxErrors + 1,
186
+ remainingStringLength: maxStringLength * (maxErrors + 1)
187
+ }, 0);
188
+ }
189
+ // src/response.ts
190
+ function prototypeGetter(constructor, key) {
191
+ return Object.getOwnPropertyDescriptor(constructor.prototype, key)?.get;
192
+ }
193
+ function prototypeMethod(constructor, key) {
194
+ const value = Object.getOwnPropertyDescriptor(constructor.prototype, key)?.value;
195
+ return typeof value === "function" ? value : void 0;
196
+ }
197
+ const blobTypeGetter = /* @__PURE__ */ prototypeGetter(Blob, "type");
198
+ const blobStreamMethod = /* @__PURE__ */ prototypeMethod(Blob, "stream");
199
+ function streamBody(read) {
200
+ let reader;
201
+ const cleanup = async (reason, cancel = false) => {
202
+ const current = reader;
203
+ reader = void 0;
204
+ if (!current) return;
205
+ try {
206
+ if (cancel) await current.cancel(reason);
207
+ } finally {
208
+ current.releaseLock();
209
+ }
210
+ };
211
+ return new ReadableStream({
212
+ async pull(controller) {
213
+ reader ??= read().getReader();
214
+ try {
215
+ const chunk = await reader.read();
216
+ if (chunk.done) {
217
+ await cleanup();
218
+ controller.close();
219
+ } else controller.enqueue(chunk.value);
220
+ } catch (error) {
221
+ await cleanup(error, true);
222
+ throw error;
223
+ }
224
+ },
225
+ async cancel(reason) {
226
+ await cleanup(reason, true);
227
+ }
228
+ });
229
+ }
230
+ function getBlobMethods(value) {
231
+ if (value === null || typeof value !== "object" && typeof value !== "function") return void 0;
232
+ try {
233
+ if (typeof blobTypeGetter !== "function" || typeof blobStreamMethod !== "function") return void 0;
234
+ const type = String(Reflect.apply(blobTypeGetter, value, []));
235
+ const body = () => streamBody(() => Reflect.apply(blobStreamMethod, value, []));
236
+ return {
237
+ type,
238
+ body
239
+ };
240
+ } catch {
241
+ return;
242
+ }
243
+ }
244
+ const arrayBufferByteLengthGetter = /* @__PURE__ */ prototypeGetter(ArrayBuffer, "byteLength");
245
+ function isArrayBufferValue(value) {
246
+ if (!isRuntimeObject(value)) return false;
247
+ try {
248
+ if (typeof arrayBufferByteLengthGetter !== "function") return false;
249
+ arrayBufferByteLengthGetter.call(value);
250
+ return true;
251
+ } catch {
252
+ return false;
253
+ }
254
+ }
255
+ const jsonHeaders = { "content-type": "application/json; charset=utf-8" };
256
+ /**
257
+ * Normalize a primitive value into the native Web Response contract.
258
+ * An error answers with `{ code, why, fix }` and never with its `stack`, `cause`, or `data`:
259
+ * - A ResultError or other Nostics diagnostic answers its `status`, or 500, so a route can `return toResponse(error)`.
260
+ * - A `ViteHubError` answers 500 with its `code` and its message as `why`.
261
+ * - Any other `Error` answers 500 with `RUNTIME_HIDDEN_ERROR`, because its message can hold server details.
262
+ */
263
+ function toResponse(value) {
264
+ if (isResponse(value)) return value;
265
+ if (isNativeError(value)) return errorResponse(value);
266
+ if (value === void 0) return new Response(null, { status: 204 });
267
+ if (typeof value === "string" || value instanceof Blob || isArrayBufferValue(value) || ArrayBuffer.isView(value)) return new Response(value);
268
+ const blobMethods = getBlobMethods(value);
269
+ if (blobMethods) return new Response(blobMethods.body(), blobMethods.type ? { headers: { "content-type": blobMethods.type } } : void 0);
270
+ return new Response(JSON.stringify(value), { headers: jsonHeaders });
271
+ }
272
+ function errorResponse(error) {
273
+ let body;
274
+ let status;
275
+ if (isRuntimeDiagnostic(error)) try {
276
+ body = {
277
+ code: error.code,
278
+ why: error.why,
279
+ fix: error.fix
280
+ };
281
+ status = Reflect.get(error, "status");
282
+ } catch {
283
+ body = void 0;
284
+ }
285
+ else {
286
+ const shape = getViteHubErrorShape(error);
287
+ if (shape) body = {
288
+ code: shape.code,
289
+ why: shape.message,
290
+ fix: void 0
291
+ };
292
+ }
293
+ if (!body || typeof body.code !== "string" || typeof body.why !== "string" || body.fix !== void 0 && typeof body.fix !== "string") {
294
+ const hidden = runtimeErrorDiagnostics.RUNTIME_HIDDEN_ERROR();
295
+ body = {
296
+ code: hidden.code,
297
+ why: hidden.why,
298
+ fix: hidden.fix
299
+ };
300
+ status = 500;
301
+ }
302
+ return new Response(JSON.stringify(body), {
303
+ headers: jsonHeaders,
304
+ status: typeof status === "number" && Number.isInteger(status) && status >= 400 && status <= 599 ? status : 500
305
+ });
306
+ }
307
+ /** Recognize Responses created by another realm from their standard tag and surface. */
308
+ function isResponse(value) {
309
+ if (value instanceof Response) return true;
310
+ if (!isRuntimeObject(value)) return false;
311
+ try {
312
+ if (Object.prototype.toString.call(value) !== "[object Response]") return false;
313
+ const headers = Object.getOwnPropertyDescriptor(Response.prototype, "headers")?.get?.call(value);
314
+ if (!headers) return false;
315
+ Array.from(Headers.prototype.entries.call(headers));
316
+ return true;
317
+ } catch {
318
+ return false;
319
+ }
320
+ }
321
+ /** Convert a native Response into a JSON-safe record. The body is fully buffered. */
322
+ async function serializeResponse(response) {
323
+ const isNull = response.body === null;
324
+ return {
325
+ body: {
326
+ data: bytesToBase64(new Uint8Array(await response.arrayBuffer())),
327
+ isNull,
328
+ encoding: "base64",
329
+ mediaType: response.headers.get("content-type") || "application/octet-stream"
330
+ },
331
+ headers: Array.from(response.headers, ([name, value]) => [name, value]),
332
+ status: response.status,
333
+ statusText: response.statusText,
334
+ ...response.type === "opaqueredirect" ? { url: response.url } : {},
335
+ ...response.type === "error" || response.type === "opaque" || response.type === "opaqueredirect" ? { type: response.type } : {}
336
+ };
337
+ }
338
+ /** Reconstruct a native Response from a durable response record. */
339
+ function deserializeResponse(value) {
340
+ if (!isSerializedResponse(value)) throw runtimeErrorDiagnostics.RUNTIME_SERIALIZED_RESPONSE_INVALID();
341
+ if (value.type !== void 0) return createStatusZeroResponse(value.type, value.url);
342
+ const bytes = base64ToBytes(value.body.data);
343
+ const body = value.body.isNull === true || [
344
+ 204,
345
+ 205,
346
+ 304
347
+ ].includes(value.status) && bytes.length === 0 ? null : bytes;
348
+ return new Response(body, {
349
+ headers: value.headers.map(([name, headerValue]) => [name, headerValue]),
350
+ status: value.status,
351
+ statusText: value.statusText
352
+ });
353
+ }
354
+ function isSerializedResponse(value) {
355
+ if (!isRuntimeObject(value)) return false;
356
+ const record = value;
357
+ if (!isRuntimeObject(record.body)) return false;
358
+ const body = record.body;
359
+ if (typeof body.data !== "string" || body.encoding !== "base64" || typeof body.mediaType !== "string") return false;
360
+ if (body.isNull !== void 0 && typeof body.isNull !== "boolean") return false;
361
+ if (body.isNull === true && body.data !== "") return false;
362
+ if (record.url !== void 0 && typeof record.url !== "string") return false;
363
+ if (record.type !== void 0 && record.type !== "error" && record.type !== "opaque" && record.type !== "opaqueredirect") return false;
364
+ if (record.type !== void 0) {
365
+ if (record.type !== "opaqueredirect" && record.url !== void 0) return false;
366
+ return record.status === 0 && record.statusText === "" && Array.isArray(record.headers) && record.headers.length === 0 && body.isNull === true && body.data === "";
367
+ }
368
+ if (record.url !== void 0) return false;
369
+ if (typeof record.status !== "number" || !Number.isInteger(record.status) || record.status < 200 || record.status > 599) return false;
370
+ if (typeof record.statusText !== "string" || !isSerializedHeaderEntries(record.headers)) return false;
371
+ if ([
372
+ 204,
373
+ 205,
374
+ 304
375
+ ].includes(record.status) && (/[^\t\n\f\r ]/.test(body.data) || body.isNull === false)) return false;
376
+ const headers = record.headers;
377
+ if (!isBase64(body.data)) return false;
378
+ try {
379
+ new Response(null, {
380
+ headers: headers.map(([name, headerValue]) => [name, headerValue]),
381
+ status: record.status,
382
+ statusText: record.statusText
383
+ });
384
+ } catch {
385
+ return false;
386
+ }
387
+ return true;
388
+ }
389
+ function isSerializedHeaderEntry(value) {
390
+ return Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "string";
391
+ }
392
+ function isSerializedHeaderEntries(value) {
393
+ if (!Array.isArray(value)) return false;
394
+ for (let index = 0; index < value.length; index++) if (!isSerializedHeaderEntry(value[index])) return false;
395
+ return true;
396
+ }
397
+ function isBase64(value) {
398
+ const compact = value.replace(/[\t\n\f\r ]/g, "");
399
+ const unpadded = compact.length % 4 === 0 ? compact.replace(/={1,2}$/, "") : compact;
400
+ return unpadded.length % 4 !== 1 && !/[^A-Za-z0-9+/]/.test(unpadded);
401
+ }
402
+ function bytesToBase64(bytes) {
403
+ if (typeof Buffer !== "undefined") return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64");
404
+ let binary = "";
405
+ for (let offset = 0; offset < bytes.length; offset += 32768) binary += String.fromCharCode(...bytes.subarray(offset, offset + 32768));
406
+ return btoa(binary);
407
+ }
408
+ function base64ToBytes(value) {
409
+ if (typeof Buffer !== "undefined") return Buffer.from(value, "base64");
410
+ const binary = atob(value);
411
+ const bytes = new Uint8Array(binary.length);
412
+ for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
413
+ return bytes;
414
+ }
415
+ function createStatusZeroResponse(type, url = "") {
416
+ const response = Response.error();
417
+ if (type !== "error") Object.defineProperties(response, {
418
+ type: {
419
+ value: type,
420
+ enumerable: true
421
+ },
422
+ url: {
423
+ value: type === "opaqueredirect" ? url : "",
424
+ enumerable: true
425
+ },
426
+ clone: { value: () => createStatusZeroResponse(type, url) }
427
+ });
428
+ return response;
429
+ }
430
+ export { toResponse as a, normalizeRuntimeDiagnosticError as c, serializeResponse as i, isResponse as n, formatRuntimeDiagnosticError as o, isSerializedResponse as r, isRuntimeDiagnostic as s, deserializeResponse as t };
@@ -1,5 +1,5 @@
1
1
  import { defineDiagnostics } from "nostics";
2
- //#region ../internal/dist/invariant.js
2
+ // ../internal/dist/invariant.js
3
3
  const invariantDiagnostics = /*#__PURE__*/ defineDiagnostics({
4
4
  docsBase: "https://vitehub.dev/docs/development/diagnostics",
5
5
  codes: { INTERNAL_INVARIANT_FAILED: {
@@ -14,8 +14,7 @@ const invariantDiagnostics = /*#__PURE__*/ defineDiagnostics({
14
14
  function invariantError(what) {
15
15
  return invariantDiagnostics.INTERNAL_INVARIANT_FAILED({ what });
16
16
  }
17
- //#endregion
18
- //#region src/internal/runtime-type.ts
17
+ // src/internal/runtime-type.ts
19
18
  /** Narrows opaque inputs without reading their properties. Kept for `@vite-hub/presets`; other packages use `typeof`. */
20
19
  function hasRuntimeType(value, expected) {
21
20
  switch (expected) {
@@ -40,12 +39,10 @@ function asUnknownBoundary(value) {
40
39
  function isRuntimeObject(value) {
41
40
  return value !== null && Object(value) === value;
42
41
  }
43
- /** Whether a value is an object literal or a null-prototype record. A built-in object whose prototype code replaced reads as one. */
44
42
  function isPlainRecord(value) {
45
43
  const prototype = Object.getPrototypeOf(value);
46
44
  return prototype === Object.prototype || prototype === null;
47
45
  }
48
- /** The native Response class and its `status` getter, and the proxy check of Node.js. Read once, at import. */
49
46
  function captureResponseChecks() {
50
47
  const response = globalThis.Response;
51
48
  return {
@@ -55,11 +52,6 @@ function captureResponseChecks() {
55
52
  };
56
53
  }
57
54
  const nativeResponseChecks = /* @__PURE__ */ captureResponseChecks();
58
- /**
59
- * The `status` getter of the global `Response` when a server replaced the native class. srvx, which Nitro uses on Node.js,
60
- * installs its own `Response` class, so `Response.json()` in app code and in ViteHub returns an instance of that class.
61
- * Its getters read private fields, so they check the brand like the native getter does.
62
- */
63
55
  function replacedResponseStatus() {
64
56
  const current = globalThis.Response;
65
57
  if (typeof current !== "function" || current === nativeResponseChecks.response) return void 0;
@@ -84,5 +76,4 @@ function isRuntimeResponse(value) {
84
76
  }
85
77
  return false;
86
78
  }
87
- //#endregion
88
79
  export { isRuntimeResponse as i, hasRuntimeType as n, isRuntimeObject as r, asUnknownBoundary as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vite-hub/runtime",
3
- "version": "0.4.1",
3
+ "version": "0.5.1",
4
4
  "description": "Shared runtime context, capability, policy, approval, and tracing primitives for ViteHub.",
5
5
  "keywords": [
6
6
  "approvals",
@@ -37,11 +37,26 @@
37
37
  "import": "./dist/durability.js",
38
38
  "default": "./dist/durability.js"
39
39
  },
40
+ "./internal/console": {
41
+ "types": "./dist/internal/console.d.ts",
42
+ "import": "./dist/internal/console.js",
43
+ "default": "./dist/internal/console.js"
44
+ },
45
+ "./internal/context": {
46
+ "types": "./dist/internal/context.d.ts",
47
+ "import": "./dist/internal/context.js",
48
+ "default": "./dist/internal/context.js"
49
+ },
40
50
  "./internal/drain": {
41
51
  "types": "./dist/internal/drain.d.ts",
42
52
  "import": "./dist/internal/drain.js",
43
53
  "default": "./dist/internal/drain.js"
44
54
  },
55
+ "./internal/error-diagnostics": {
56
+ "types": "./dist/internal/error-diagnostics.d.ts",
57
+ "import": "./dist/internal/error-diagnostics.js",
58
+ "default": "./dist/internal/error-diagnostics.js"
59
+ },
45
60
  "./internal/execution-budget": {
46
61
  "types": "./dist/internal/execution-budget.d.ts",
47
62
  "import": "./dist/internal/execution-budget.js",
@@ -67,6 +82,11 @@
67
82
  "import": "./dist/internal/public-url.js",
68
83
  "default": "./dist/internal/public-url.js"
69
84
  },
85
+ "./internal/response": {
86
+ "types": "./dist/internal/response.d.ts",
87
+ "import": "./dist/internal/response.js",
88
+ "default": "./dist/internal/response.js"
89
+ },
70
90
  "./internal/run-failed": {
71
91
  "types": "./dist/internal/run-failed.d.ts",
72
92
  "import": "./dist/internal/run-failed.js",
@@ -97,6 +117,11 @@
97
117
  "import": "./dist/internal/test-mode-setup.js",
98
118
  "default": "./dist/internal/test-mode-setup.js"
99
119
  },
120
+ "./internal/trace": {
121
+ "types": "./dist/internal/trace.d.ts",
122
+ "import": "./dist/internal/trace.js",
123
+ "default": "./dist/internal/trace.js"
124
+ },
100
125
  "./operations": {
101
126
  "types": "./dist/operations.d.ts",
102
127
  "import": "./dist/operations.js",
@@ -114,13 +139,13 @@
114
139
  },
115
140
  "devDependencies": {
116
141
  "@libsql/client": "^0.18.0",
117
- "@types/node": "^24.19.1",
142
+ "@types/node": "^24.19.2",
118
143
  "@vite-hub/internal": "0.0.1",
119
144
  "postgres": "^3.4.9",
120
145
  "publint": "^0.3.25",
121
146
  "typescript": "^6.0.3",
122
- "vite-plus": "^0.3.3",
123
- "vitest": "^4.1.11"
147
+ "vite-plus": "^1.7.0",
148
+ "vitest": "5.1.0"
124
149
  },
125
150
  "engines": {
126
151
  "node": ">=24.15.0"