@vite-hub/runtime 0.4.1 → 0.5.0

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 (41) hide show
  1. package/dist/core-BfUEli_0.d.ts +304 -0
  2. package/dist/core-DpLK9dQt.js +1105 -0
  3. package/dist/durability.d.ts +2 -2
  4. package/dist/durability.js +4 -1
  5. package/dist/hosted-console-ZfMo5MW4.d.ts +30 -0
  6. package/dist/index-DsisbCNa.d.ts +364 -0
  7. package/dist/index.d.ts +7 -5
  8. package/dist/index.js +5 -4
  9. package/dist/internal/console.d.ts +7 -0
  10. package/dist/internal/console.js +2 -0
  11. package/dist/internal/context.d.ts +2 -0
  12. package/dist/internal/context.js +2 -0
  13. package/dist/internal/error-diagnostics.d.ts +2 -0
  14. package/dist/internal/error-diagnostics.js +2 -0
  15. package/dist/internal/execution-budget.d.ts +3 -2
  16. package/dist/internal/execution-budget.js +226 -1
  17. package/dist/internal/grant.js +1 -1
  18. package/dist/internal/h3.d.ts +2 -2
  19. package/dist/internal/h3.js +1 -7
  20. package/dist/internal/node.d.ts +1 -1
  21. package/dist/internal/public-url.d.ts +2 -2
  22. package/dist/internal/public-url.js +2 -2
  23. package/dist/internal/response.d.ts +2 -0
  24. package/dist/internal/response.js +2 -0
  25. package/dist/internal/run-failed.js +0 -1
  26. package/dist/internal/runtime-type.js +1 -1
  27. package/dist/internal/shutdown.js +17 -19
  28. package/dist/internal/test-mode-setup.js +0 -4
  29. package/dist/internal/trace.d.ts +6 -0
  30. package/dist/internal/trace.js +2 -0
  31. package/dist/operations.d.ts +8 -2
  32. package/dist/operations.js +6 -2
  33. package/dist/{public-url-1WeOw54Z.d.ts → public-url-BCFhTiTM.d.ts} +6 -1
  34. package/dist/{public-url-ByAmAjbP.js → public-url-DizC8CFs.js} +25 -17
  35. package/dist/request-context-Dmsliqan.js +687 -0
  36. package/dist/response-Be5M4WIn.js +432 -0
  37. package/dist/response-iONbTI0E.d.ts +33 -0
  38. package/dist/{runtime-type-BftydMAf.js → runtime-type-CHYQYwDx.js} +0 -7
  39. package/package.json +26 -1
  40. package/dist/index-C1DoYfCf.d.ts +0 -733
  41. package/dist/request-context-uRQpodgl.js +0 -2433
@@ -0,0 +1,304 @@
1
+ import { StandardContext } from "standard-context";
2
+ //#region ../internal/dist/runtime/cloudflare-env.d.ts
3
+ interface CloudflareWorkerExecutionContext {
4
+ waitUntil?: (promise: Promise<unknown>) => void;
5
+ }
6
+ //#endregion
7
+ //#region src/standard-context.d.ts
8
+ /** The host data that ViteHub puts in `runtime` of a Standard Context. */
9
+ interface ViteHubRuntime {
10
+ /** The Cloudflare bindings and execution context of the request. A host can pass either one without the other. */
11
+ cloudflare?: {
12
+ context?: CloudflareWorkerExecutionContext;
13
+ env?: Record<string, unknown>;
14
+ };
15
+ /**
16
+ * The client IP address that the host reports. ViteHub does not read it from forwarding headers.
17
+ * Standard Context has no client address field yet (https://github.com/standard-servers/proposals/issues/4).
18
+ */
19
+ clientAddress?: string;
20
+ }
21
+ /** The `~standard` properties of a request event in a ViteHub app. */
22
+ type ViteHubStandardProps = StandardContext.Props<ViteHubRuntime, void>;
23
+ /** A request event in a ViteHub app: any server context with Standard Context properties. */
24
+ type ViteHubContext<C = {}> = StandardContext<C, ViteHubRuntime, void>;
25
+ //#endregion
26
+ //#region src/core.d.ts
27
+ type MaybePromise<T> = T | Promise<T>;
28
+ declare global {
29
+ namespace ViteHub {
30
+ interface AuthRegistry {}
31
+ }
32
+ }
33
+ type AccessUser<TRegistry> = TRegistry extends {
34
+ sessionResult: {
35
+ user: infer TUser extends {
36
+ id: string;
37
+ };
38
+ };
39
+ } ? TUser : Record<string, unknown> & {
40
+ id: string;
41
+ };
42
+ type AccessSession<TRegistry> = TRegistry extends {
43
+ sessionResult: {
44
+ session: infer TSession extends object;
45
+ };
46
+ } ? TSession : Record<string, unknown>;
47
+ interface AccessContext<TEvent, TSession, TUser> {
48
+ event: TEvent;
49
+ request: Pick<Request, "body" | "headers" | "method" | "signal" | "url">;
50
+ session: TSession;
51
+ user: TUser;
52
+ }
53
+ /** `TEvent` is the guard input. `TRegistered: false` avoids reading the app registry while inferring its Auth Definition. */
54
+ type AccessAuthorizationContext<TEvent = ViteHubContext, TRegistered extends boolean = true> = AccessContext<TEvent, AccessSession<TRegistered extends true ? ViteHub.AuthRegistry : unknown>, AccessUser<TRegistered extends true ? ViteHub.AuthRegistry : unknown>>;
55
+ type AccessAuthorizeResult = boolean | Response;
56
+ type Authorize<TContext> = (context: TContext) => MaybePromise<AccessAuthorizeResult>;
57
+ /** Decides whether a signed-in user can access one request. Return `false` for `403` or a custom `Response`. */
58
+ type AccessAuthorize<TEvent = ViteHubContext, TRegistered extends boolean = true> = TRegistered extends true ? Authorize<AccessAuthorizationContext<TEvent>> : Authorize<AccessAuthorizationContext<TEvent, false>>;
59
+ /** `true` requires a signed-in session. A callback also decides each request after sign-in. */
60
+ type AccessAuthorizeOption<TEvent = ViteHubContext, TRegistered extends boolean = true> = true | AccessAuthorize<TEvent, TRegistered>;
61
+ /** A signed-out caller. `session` and `user` are `null`. */
62
+ interface AnonymousAccessAuthorizationContext<TEvent = ViteHubContext> {
63
+ event: TEvent;
64
+ request: AccessAuthorizationContext["request"];
65
+ session: null;
66
+ user: null;
67
+ }
68
+ /**
69
+ * Decides whether a signed-in or signed-out caller can access one request.
70
+ * Check `user` before you read it. Return `false` for `403` (`401` when signed out) or a custom `Response`.
71
+ */
72
+ type AnonymousAccessAuthorize<TEvent = ViteHubContext, TRegistered extends boolean = true> = TRegistered extends true ? Authorize<AccessAuthorizationContext<TEvent> | AnonymousAccessAuthorizationContext<TEvent>> : Authorize<AccessAuthorizationContext<TEvent, false> | AnonymousAccessAuthorizationContext<TEvent>>;
73
+ interface ExecutionAuthority {
74
+ readonly credentials: "ambient" | "none" | "provisioned" | "unknown";
75
+ readonly environment: "ambient" | "none" | "selected" | "unknown";
76
+ readonly filesystem: {
77
+ readonly access: "none" | "read-only" | "read-write" | "unknown";
78
+ readonly scope: "host" | "none" | "sandbox" | "unknown" | "workspace";
79
+ };
80
+ readonly isolation: "container" | "microvm" | "none" | "process" | "unknown";
81
+ readonly network: "none" | "restricted" | "unrestricted" | "unknown";
82
+ readonly processes: "arbitrary" | "none" | "restricted" | "unknown";
83
+ }
84
+ declare const noExecutionAuthority: ExecutionAuthority;
85
+ declare const unknownExecutionAuthority: ExecutionAuthority;
86
+ declare function isExecutionAuthority(value: unknown): value is ExecutionAuthority;
87
+ declare function normalizeExecutionAuthority(value: unknown): ExecutionAuthority;
88
+ type RuntimeWaitUntil = (task: Promise<unknown>) => void;
89
+ interface RuntimeWaitUntilController {
90
+ /**
91
+ * Drain registered work, including tasks added during the flush.
92
+ * Call from the operation owner. A registered task must not await its own
93
+ * controller's flush, because the flush waits for that task to finish.
94
+ */
95
+ flushWaitUntil(): Promise<void>;
96
+ waitUntil: RuntimeWaitUntil;
97
+ }
98
+ interface TraceContext {
99
+ id: string;
100
+ parentId?: string;
101
+ sampled?: boolean;
102
+ }
103
+ type TraceActivityOwner = "agent" | "vitehub";
104
+ type TraceActivityPhase = "delivery" | "execution" | "setup" | "teardown";
105
+ interface TraceActivityContext {
106
+ owner: TraceActivityOwner;
107
+ phase: TraceActivityPhase;
108
+ }
109
+ type TracePayloadVisibility = "private" | "public" | "redacted" | "summary";
110
+ type TraceEventPayload = {
111
+ value: unknown;
112
+ visibility: "public";
113
+ } | {
114
+ summary: string;
115
+ visibility: "summary";
116
+ } | {
117
+ visibility: "redacted";
118
+ } | {
119
+ visibility: "private";
120
+ };
121
+ type TraceEventContentPolicy = "content" | "metadata";
122
+ type TraceRunStatus = "cancelled" | "completed" | "failed" | "running";
123
+ type TraceStepStatus = "completed" | "failed" | "running";
124
+ interface TraceEvent {
125
+ activity?: TraceActivityContext;
126
+ attributes?: Record<string, unknown>;
127
+ name: string;
128
+ payload?: TraceEventPayload;
129
+ timestamp?: Date | string;
130
+ trace?: TraceContext;
131
+ type: "approval" | "capability" | "error" | "lifecycle" | "policy" | "run";
132
+ }
133
+ interface TraceEventLogEntry extends TraceEvent {
134
+ sequence: number;
135
+ timestamp: string;
136
+ }
137
+ interface TraceEventLog {
138
+ append(event: TraceEvent): MaybePromise<TraceEventLogEntry>;
139
+ entries(): readonly TraceEventLogEntry[];
140
+ }
141
+ interface TraceEventLogOptions {
142
+ content?: TraceEventContentPolicy;
143
+ onEntry?: (entry: TraceEventLogEntry) => MaybePromise<void>;
144
+ }
145
+ interface TraceStepView {
146
+ attributes?: Record<string, unknown>;
147
+ durationMs?: number;
148
+ endTime?: string;
149
+ events: TraceEventLogEntry[];
150
+ id: string;
151
+ name: string;
152
+ startTime: string;
153
+ status: TraceStepStatus;
154
+ type: TraceEvent["type"];
155
+ }
156
+ interface TraceRunView {
157
+ durationMs?: number;
158
+ endTime?: string;
159
+ events: TraceEventLogEntry[];
160
+ id: string;
161
+ startTime: string;
162
+ status: TraceRunStatus;
163
+ steps: TraceStepView[];
164
+ }
165
+ interface OpenTelemetrySpanView {
166
+ attributes?: Record<string, unknown>;
167
+ endTime?: string;
168
+ events?: OpenTelemetrySpanEventView[];
169
+ name: string;
170
+ parentSpanId?: string;
171
+ spanId: string;
172
+ startTime: string;
173
+ status: {
174
+ code: "ERROR" | "OK" | "UNSET";
175
+ message?: string;
176
+ };
177
+ traceId: string;
178
+ }
179
+ interface OpenTelemetrySpanEventView {
180
+ attributes?: Record<string, unknown>;
181
+ name: string;
182
+ time: string;
183
+ }
184
+ interface OpenTelemetryLogRecordView {
185
+ attributes?: Record<string, unknown>;
186
+ eventName: string;
187
+ severityNumber?: number;
188
+ severityText?: string;
189
+ spanId: string;
190
+ time: string;
191
+ traceId: string;
192
+ }
193
+ interface OpenTelemetrySpanViewOptions {
194
+ content?: TraceEventContentPolicy;
195
+ }
196
+ interface RuntimeHostContext<TRuntimeConfig = Record<string, unknown>> {
197
+ capabilities?: RuntimeCapabilities;
198
+ cloudflare?: {
199
+ context?: unknown;
200
+ durableObjectStateName?: string;
201
+ env?: Record<string, unknown>;
202
+ };
203
+ event?: unknown;
204
+ memo<T>(key: string, create: () => T): T;
205
+ platform?: string;
206
+ request?: Request;
207
+ runtime: string;
208
+ runtimeConfig?: TRuntimeConfig;
209
+ trace?: TraceContext;
210
+ traceLog?: TraceEventLog;
211
+ vercel?: {
212
+ waitUntil?: RuntimeWaitUntil;
213
+ };
214
+ /** Drain from the operation owner, never from work registered with this context. */
215
+ flushWaitUntil?: () => Promise<void>;
216
+ waitUntil: RuntimeWaitUntil;
217
+ }
218
+ type ExecutionContext<TRuntimeConfig = Record<string, unknown>> = RuntimeHostContext<TRuntimeConfig> & {
219
+ capabilities: RuntimeCapabilities;
220
+ runtimeConfig: TRuntimeConfig;
221
+ };
222
+ interface CapabilityHandle<TKind extends string = string, TValue = unknown> {
223
+ kind: TKind;
224
+ name?: string;
225
+ value: TValue;
226
+ }
227
+ type RuntimeCapabilityHandle<TKind extends string = string, TValue = unknown> = CapabilityHandle<TKind, TValue>;
228
+ type RuntimeCapabilities = Record<string, CapabilityHandle | unknown>;
229
+ interface CapabilityDefinition<TKind extends string = string, TValue = unknown> {
230
+ handle: CapabilityHandle<TKind, TValue>;
231
+ resolve(context: ExecutionContext): MaybePromise<CapabilityHandle<TKind, TValue>>;
232
+ }
233
+ interface Resolvable<T, TContext extends RuntimeHostContext<any> = RuntimeHostContext> {
234
+ resolve(context: TContext): MaybePromise<T>;
235
+ }
236
+ type MaybeResolvable<T, TContext extends RuntimeHostContext<any> = RuntimeHostContext> = T | Resolvable<T, TContext> | ((context: TContext) => MaybePromise<T>);
237
+ interface PolicyContext {
238
+ capability?: string;
239
+ input?: unknown;
240
+ operation?: string;
241
+ runtime?: string;
242
+ subject?: unknown;
243
+ trace?: TraceContext;
244
+ }
245
+ type PolicyDecision = "allow" | "deny" | "require-approval" | "retryable-failure";
246
+ type CapabilityPolicy = PolicyDecision | ((context: PolicyContext) => MaybePromise<PolicyDecision>);
247
+ type ApprovalState = "proposed" | "awaiting-approval" | "approved" | "denied" | "executing" | "completed" | "failed";
248
+ interface ApprovalRequest<TInput = unknown> {
249
+ id: string;
250
+ capability?: string;
251
+ input?: TInput;
252
+ reason?: string;
253
+ state: Extract<ApprovalState, "awaiting-approval" | "proposed">;
254
+ trace?: TraceContext;
255
+ }
256
+ interface ApprovalDecision {
257
+ approved: boolean;
258
+ decidedAt?: Date | string;
259
+ reason?: string;
260
+ requestId: string;
261
+ state: Extract<ApprovalState, "approved" | "denied">;
262
+ }
263
+ interface RunLifecycleHooks<TContext extends RuntimeHostContext<any> = RuntimeHostContext> {
264
+ approval?: (request: ApprovalRequest, context: TContext) => MaybePromise<void>;
265
+ error?: (error: unknown, context: TContext) => MaybePromise<void>;
266
+ finish?: (context: TContext) => MaybePromise<void>;
267
+ request?: (context: TContext) => MaybePromise<void>;
268
+ trace?: (event: TraceEvent, context: TContext) => MaybePromise<void>;
269
+ }
270
+ declare function emitTraceEvent<TContext extends RuntimeHostContext<any>>(context: TContext, event: TraceEvent): Promise<TraceEventLogEntry | undefined>;
271
+ declare function createTraceEventLog(options?: TraceEventLogOptions): TraceEventLog;
272
+ declare function deriveTraceRuns(events: Iterable<TraceEventLogEntry>): TraceRunView[];
273
+ declare function traceEventsToOpenTelemetryLogRecords(events: Iterable<TraceEventLogEntry>, options?: OpenTelemetrySpanViewOptions): OpenTelemetryLogRecordView[];
274
+ declare function traceEventsToOpenTelemetrySpans(events: Iterable<TraceEventLogEntry>, options?: OpenTelemetrySpanViewOptions): OpenTelemetrySpanView[];
275
+ type NormalizedRuntimeConfig<TConfig> = Exclude<TConfig, null | undefined> extends Record<string, unknown> ? Record<string, unknown> : Exclude<TConfig, null | undefined> | Record<string, unknown>;
276
+ type RuntimeConfigOf<TContext extends RuntimeHostContext<any>> = "runtimeConfig" extends keyof TContext ? undefined extends TContext["runtimeConfig"] ? NormalizedRuntimeConfig<TContext["runtimeConfig"]> : null extends TContext["runtimeConfig"] ? NormalizedRuntimeConfig<TContext["runtimeConfig"]> : TContext["runtimeConfig"] : Record<string, unknown>;
277
+ type RuntimeCapabilitiesOf<TContext extends RuntimeHostContext<any>> = "capabilities" extends keyof TContext ? undefined extends TContext["capabilities"] ? RuntimeCapabilities : TContext["capabilities"] : RuntimeCapabilities;
278
+ type CreatedExecutionContext<TContext extends RuntimeHostContext<any>> = TContext extends unknown ? Omit<TContext, "capabilities" | "runtimeConfig"> & {
279
+ capabilities: RuntimeCapabilitiesOf<TContext>;
280
+ runtimeConfig: RuntimeConfigOf<TContext>;
281
+ } : never;
282
+ declare function createExecutionContext<TContext extends RuntimeHostContext<any>>(context: TContext): CreatedExecutionContext<TContext>;
283
+ type RuntimeContextInput = Omit<RuntimeHostContext<unknown>, "flushWaitUntil" | "memo" | "waitUntil"> & {
284
+ memo?: RuntimeHostContext["memo"];
285
+ waitUntil?: RuntimeWaitUntil;
286
+ };
287
+ type CreatedRuntimeContext<TContext extends RuntimeContextInput> = CreatedExecutionContext<TContext & RuntimeWaitUntilController & {
288
+ memo: RuntimeHostContext["memo"];
289
+ }>;
290
+ /** Create one operation's context. Without a host waitUntil, drain work before returning. */
291
+ declare function createRuntimeContext<const TContext extends RuntimeContextInput>(input: TContext): CreatedRuntimeContext<TContext>;
292
+ declare function defineCapability<TKind extends string, TValue>(kind: TKind, value: TValue, options?: {
293
+ name?: string;
294
+ }): CapabilityHandle<TKind, TValue>;
295
+ declare function createRuntimeWaitUntilController(options?: {
296
+ forward?: RuntimeWaitUntil;
297
+ }): RuntimeWaitUntilController;
298
+ declare function hasCapability(context: RuntimeHostContext, name: string): boolean;
299
+ declare function getCapability(context: RuntimeHostContext<any>, name: string): CapabilityHandle;
300
+ declare function isResolvable<T, TContext extends RuntimeHostContext<any>>(value: MaybeResolvable<T, TContext>): value is Resolvable<T, TContext>;
301
+ declare function resolveRuntimeValue<T, TContext extends RuntimeHostContext<any>>(value: MaybeResolvable<T, TContext>, context: TContext): Promise<T>;
302
+ declare function resolveCapabilityPolicy(policy: CapabilityPolicy | undefined, context: PolicyContext): Promise<PolicyDecision>;
303
+ //#endregion
304
+ export { emitTraceEvent as $, RuntimeWaitUntil as A, TraceEventLogOptions as B, PolicyDecision as C, RuntimeCapabilityHandle as D, RuntimeCapabilities as E, TraceContext as F, TraceStepStatus as G, TracePayloadVisibility as H, TraceEvent as I, createRuntimeContext as J, TraceStepView as K, TraceEventContentPolicy as L, TraceActivityContext as M, TraceActivityOwner as N, RuntimeContextInput as O, TraceActivityPhase as P, deriveTraceRuns as Q, TraceEventLog as R, PolicyContext as S, RunLifecycleHooks as T, TraceRunStatus as U, TraceEventPayload as V, TraceRunView as W, createTraceEventLog as X, createRuntimeWaitUntilController as Y, defineCapability as Z, MaybeResolvable as _, AnonymousAccessAuthorizationContext as a, normalizeExecutionAuthority as at, OpenTelemetrySpanView as b, ApprovalRequest as c, traceEventsToOpenTelemetryLogRecords as ct, CapabilityHandle as d, ViteHubContext as dt, getCapability as et, CapabilityPolicy as f, ViteHubRuntime as ft, MaybePromise as g, ExecutionContext as h, AccessAuthorizeResult as i, noExecutionAuthority as it, RuntimeWaitUntilController as j, RuntimeHostContext as k, ApprovalState as l, traceEventsToOpenTelemetrySpans as lt, ExecutionAuthority as m, AccessAuthorize as n, isExecutionAuthority as nt, AnonymousAccessAuthorize as o, resolveCapabilityPolicy as ot, CreatedRuntimeContext as p, ViteHubStandardProps as pt, createExecutionContext as q, AccessAuthorizeOption as r, isResolvable as rt, ApprovalDecision as s, resolveRuntimeValue as st, AccessAuthorizationContext as t, hasCapability as tt, CapabilityDefinition as u, unknownExecutionAuthority as ut, OpenTelemetryLogRecordView as v, Resolvable as w, OpenTelemetrySpanViewOptions as x, OpenTelemetrySpanEventView as y, TraceEventLogEntry as z };