@happyvertical/smrt-app-runtime 0.53.2 → 0.53.4

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/AGENTS.md +40 -0
  2. package/README.md +101 -0
  3. package/dist/application-identity.d.ts +43 -0
  4. package/dist/application-identity.d.ts.map +1 -0
  5. package/dist/chunks/src-DZGOVGsU.js +2258 -0
  6. package/dist/chunks/src-DZGOVGsU.js.map +1 -0
  7. package/dist/deployed-runtime.d.ts +11 -0
  8. package/dist/deployed-runtime.d.ts.map +1 -1
  9. package/dist/error-code.d.ts +3 -0
  10. package/dist/error-code.d.ts.map +1 -0
  11. package/dist/index.d.ts +6 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +2 -1831
  14. package/dist/manifest.json +1 -1
  15. package/dist/operation-lock.d.ts +28 -0
  16. package/dist/operation-lock.d.ts.map +1 -0
  17. package/dist/provider-readiness.d.ts +48 -0
  18. package/dist/provider-readiness.d.ts.map +1 -0
  19. package/dist/smrt-knowledge.json +9 -6
  20. package/dist/stale-reclaim.d.ts +33 -0
  21. package/dist/stale-reclaim.d.ts.map +1 -0
  22. package/dist/state-root.d.ts +42 -0
  23. package/dist/state-root.d.ts.map +1 -0
  24. package/dist/sveltekit/handlers.d.ts +83 -0
  25. package/dist/sveltekit/handlers.d.ts.map +1 -0
  26. package/dist/sveltekit/index.d.ts +20 -0
  27. package/dist/sveltekit/index.d.ts.map +1 -0
  28. package/dist/sveltekit/owner-setup.d.ts +102 -0
  29. package/dist/sveltekit/owner-setup.d.ts.map +1 -0
  30. package/dist/sveltekit/runtime.d.ts +277 -0
  31. package/dist/sveltekit/runtime.d.ts.map +1 -0
  32. package/dist/sveltekit/tenancy.d.ts +59 -0
  33. package/dist/sveltekit/tenancy.d.ts.map +1 -0
  34. package/dist/sveltekit.d.ts +2 -0
  35. package/dist/sveltekit.d.ts.map +1 -0
  36. package/dist/sveltekit.js +704 -0
  37. package/dist/sveltekit.js.map +1 -0
  38. package/dist/writer-lease.d.ts +36 -0
  39. package/dist/writer-lease.d.ts.map +1 -0
  40. package/package.json +22 -8
  41. package/dist/index.js.map +0 -1
@@ -0,0 +1,277 @@
1
+ /**
2
+ * SvelteKit server composition for the validated runtime profiles.
3
+ *
4
+ * Request foundation, in order:
5
+ *
6
+ * 0. Wait for the fail-closed runtime startup gate (`init`).
7
+ * 1. Resolve a URL tenant candidate into `locals.selectedTenant*`. This is
8
+ * selection only and never enters AsyncLocalStorage tenant context.
9
+ * 2. Load the signed session. Its tenant is accepted only for a verified
10
+ * active direct or legitimately inherited membership
11
+ * ({@link isSessionTenantAuthorized}, the deployed runtime's rule);
12
+ * otherwise the request is unauthenticated. An accepted session enters the
13
+ * tenant context and permission set for downstream code, inside the
14
+ * request RLS transaction when the profile selects `database-rls`.
15
+ * 3. Publish the authorized context on `locals.tenantContext` only when it
16
+ * matches the verified session tenant.
17
+ *
18
+ * This ordering prevents a spoofed header or hostname from becoming query
19
+ * authority. Membership-gated tenant switching belongs in an explicit action
20
+ * using `switchSessionTenant()` from `@happyvertical/smrt-users/sveltekit`.
21
+ */
22
+ import { type ResolvedApplicationRuntime } from '@happyvertical/smrt-config';
23
+ import type { SmrtClassOptions } from '@happyvertical/smrt-core';
24
+ import { type MinimalTenantContext } from '@happyvertical/smrt-tenancy';
25
+ import type { SessionHandlerOptions, SessionLocals } from '@happyvertical/smrt-users/sveltekit';
26
+ import { type DatabaseInterface } from '@happyvertical/sql';
27
+ import type { Handle, ServerInit } from '@sveltejs/kit';
28
+ import { type DeployedApplicationRuntime, initializeDeployedApplicationRuntime } from '../deployed-runtime.js';
29
+ import { initializeLocalApplicationRuntime, type LocalApplicationRuntime, type LocalOwnerBootstrapInvitation } from '../index.js';
30
+ import { type RuntimeDiagnostics } from '../runtime-diagnostics.js';
31
+ import type { WriterLease } from '../writer-lease.js';
32
+ import { type TenantSelector } from './tenancy.js';
33
+ /** `App.Locals` fields populated by the SMRT runtime `handle`. */
34
+ export interface SmrtRuntimeLocals extends SessionLocals {
35
+ /**
36
+ * URL-selected tenant candidate. Never use this as authorization; the
37
+ * session-authorized tenant remains `tenantId` from {@link SessionLocals}.
38
+ */
39
+ selectedTenantId: string | null;
40
+ selectedTenantSlug: string | null;
41
+ /** Full context for the authorized session tenant, when present. */
42
+ tenantContext?: MinimalTenantContext;
43
+ }
44
+ /** Provider component whose readiness a deployed web process must prove. */
45
+ export type ProviderReadinessComponent = 'authentication' | 'assets' | 'secrets';
46
+ /**
47
+ * Build a provider-owned readiness probe. The probe must check the real
48
+ * backing service and must never return or log credentials.
49
+ */
50
+ export type ProviderReadinessFactory = (component: ProviderReadinessComponent, context: {
51
+ readonly profile: string;
52
+ readonly provider: string;
53
+ }) => () => Promise<void>;
54
+ /** Context handed to an application-owned writer lease. */
55
+ export interface WriterLeaseContext {
56
+ readonly appId: string;
57
+ readonly dataDirectory: string | undefined;
58
+ readonly sourceRoot: string;
59
+ }
60
+ /**
61
+ * Held for the life of the local web process; released on failed startup.
62
+ * The root entry's `acquireWriterLease()` returns one.
63
+ */
64
+ export type { WriterLease } from '../writer-lease.js';
65
+ /** Inputs to the public diagnostics projection that only the app can supply. */
66
+ export interface ApplicationRuntimeDiagnosticsOptions {
67
+ readonly toolNames: readonly string[];
68
+ readonly observedAt: Date;
69
+ /** Application-owned schema verification seam; runtime liveness is not proof. */
70
+ readonly schemaStatus?: 'not-ready' | 'ready' | 'unknown';
71
+ /** Application-owned migration tracker seam; database liveness is not proof. */
72
+ readonly migrationStatus?: 'current' | 'failed' | 'pending' | 'unknown';
73
+ /** Bounded worker seam supplied by the deployment's lease/heartbeat adapter. */
74
+ readonly workerHeartbeatAt?: Date | string | null;
75
+ /** Stable code/timestamp pairs only; raw errors never cross this seam. */
76
+ readonly recentErrors?: readonly {
77
+ readonly code?: unknown;
78
+ readonly at?: unknown;
79
+ }[];
80
+ }
81
+ /** Liveness body served by the mounted health route. */
82
+ export interface RuntimeHealthBody {
83
+ readonly schemaVersion: 1;
84
+ readonly status: 'ready';
85
+ readonly profile: ResolvedApplicationRuntime['profile'];
86
+ /** Local profile only: canonical application ID. */
87
+ readonly application?: string;
88
+ /** Local profile only: managed process instance (`SMRT_PROCESS_INSTANCE`). */
89
+ readonly instance?: string | null;
90
+ /** Local profile only: {@link runtimeConfigurationFingerprint}. */
91
+ readonly configuration?: string;
92
+ }
93
+ /** Session-handler options the application may tune. */
94
+ export type SmrtRuntimeSessionOptions = Omit<SessionHandlerOptions, 'enterTenantContext'>;
95
+ /** App-specific options for {@link createSmrtSvelteKitRuntime}. */
96
+ export interface SmrtSvelteKitRuntimeOptions {
97
+ /**
98
+ * Explicit application ID. Defaults to `SMRT_APP_ID` (strictly validated)
99
+ * or the encoded `package.json` name under `sourceRoot`.
100
+ */
101
+ readonly appId?: string;
102
+ /** Application source checkout. Defaults to `process.cwd()`. */
103
+ readonly sourceRoot?: string;
104
+ /** Local data root. Defaults to `SMRT_DATA_DIR`, then the OS data directory. */
105
+ readonly dataDirectory?: string;
106
+ /** Environment to read. Defaults to `process.env`. */
107
+ readonly env?: NodeJS.ProcessEnv;
108
+ /**
109
+ * Resolved runtime profile. Defaults to `smrt.config` `runtime` when
110
+ * present, otherwise the `local` profile.
111
+ */
112
+ readonly runtime?: ResolvedApplicationRuntime | (() => ResolvedApplicationRuntime | Promise<ResolvedApplicationRuntime>);
113
+ /**
114
+ * Explicit, idempotent application migration hook, run during `init`.
115
+ * The runtime never synthesizes application tables itself.
116
+ */
117
+ readonly prepareDatabase?: (db: DatabaseInterface) => Promise<void>;
118
+ /**
119
+ * Local HTTP bind host. Defaults to `HOST`, or `127.0.0.1` when
120
+ * `NODE_ENV=development`. Local production startup without one fails.
121
+ */
122
+ readonly bindHost?: string;
123
+ /** Opt in to persisted local background jobs. Default `SMRT_BACKGROUND_JOBS === 'true'`. */
124
+ readonly backgroundJobs?: boolean;
125
+ /** Opt in to application-defined paid capabilities. Default false. */
126
+ readonly paidCapabilities?: boolean;
127
+ /** Owner-bootstrap token lifetime (1..900 seconds). */
128
+ readonly bootstrapTtlSeconds?: number;
129
+ /** Session lifetime for the owner-bootstrap session cookie. Default 7 days. */
130
+ readonly sessionTtlSeconds?: number;
131
+ /**
132
+ * Local single-writer lease held for the web process (for example the CLI's
133
+ * state-root writer lease). Released when local startup fails.
134
+ */
135
+ readonly acquireWriterLease?: (context: WriterLeaseContext) => WriterLease;
136
+ /**
137
+ * Deployed provider readiness probes. Required by `self-hosted` and `cloud`;
138
+ * deployed startup fails closed without them.
139
+ */
140
+ readonly providerReadiness?: ProviderReadinessFactory;
141
+ /** Replace the default subdomain tenant selector. Selection only. */
142
+ readonly selectTenant?: TenantSelector;
143
+ /** Base domain for the default selector. Defaults to `TENANT_BASE_DOMAIN`. */
144
+ readonly tenantBaseDomain?: string;
145
+ /** Session handler tuning. `enterTenantContext` is always `true`. */
146
+ readonly session?: SmrtRuntimeSessionOptions;
147
+ /** Per-class overrides layered over the runtime database config. */
148
+ readonly classOverrides?: Readonly<Record<string, Partial<SmrtClassOptions>>>;
149
+ /** Call `enableTenancy()` if it is not already enabled. Default true. */
150
+ readonly enableTenancy?: boolean;
151
+ /**
152
+ * Receives an owner-bootstrap invitation that this process's startup newly
153
+ * issued (the plaintext is returned exactly once; only its HMAC is stored).
154
+ * Use it to show a loopback `/setup?token=` URL. Errors are ignored so a
155
+ * failing presenter never blocks startup. Default: not presented.
156
+ */
157
+ readonly onBootstrapInvitation?: (invitation: LocalOwnerBootstrapInvitation, context: {
158
+ readonly bindHost: string;
159
+ }) => void;
160
+ /** Testable clock for the local runtime. */
161
+ readonly now?: () => Date;
162
+ }
163
+ /** Cookie attributes for a session established outside the session handler. */
164
+ export interface SmrtRuntimeSessionCookie {
165
+ readonly name: string;
166
+ readonly path: string;
167
+ readonly domain?: string;
168
+ readonly sameSite: 'strict' | 'lax' | 'none';
169
+ /** `undefined` means "secure when the request is HTTPS". */
170
+ readonly secure?: boolean;
171
+ readonly maxAgeSeconds: number;
172
+ }
173
+ /** The composed runtime; `handle` and `init` are bound and destructurable. */
174
+ /** A principal a route authenticated itself, for {@link SmrtSvelteKitRuntime.runAsPrincipal}. */
175
+ export interface SmrtRuntimeBoundPrincipal {
176
+ readonly id: string;
177
+ readonly tenantId?: string | null;
178
+ /** Granted scopes that cap the live permission set (e.g. token scopes). */
179
+ readonly scopes?: readonly string[];
180
+ }
181
+ export interface SmrtSvelteKitRuntime {
182
+ /** `hooks.server.ts` `handle`. */
183
+ readonly handle: Handle;
184
+ /** `hooks.server.ts` `init`: the fail-closed startup gate. */
185
+ readonly init: ServerInit;
186
+ /** Session cookie attributes shared with owner setup. */
187
+ readonly sessionCookie: SmrtRuntimeSessionCookie;
188
+ /** Resolve the runtime profile (memoized; retried after failure). */
189
+ resolvedRuntime(): Promise<ResolvedApplicationRuntime>;
190
+ /** Await profile-specific startup (same promise as `init`). */
191
+ ready(): Promise<void>;
192
+ /** Canonical application ID. */
193
+ applicationId(): string;
194
+ /** Secret-free configuration fingerprint. Requires a resolved runtime. */
195
+ configurationFingerprint(): string;
196
+ /**
197
+ * Database for SMRT collections. Requires a resolved runtime. Inside a
198
+ * request running in the RLS transaction (`database-rls` isolation, or
199
+ * `session.postgresRls`) this is the transaction-bound request database;
200
+ * otherwise the base configuration. Call it per request and never retain
201
+ * the result (or collections built from it) beyond that request.
202
+ */
203
+ databaseConfig(): SmrtClassOptions['db'];
204
+ /**
205
+ * Collection options for a class: {@link databaseConfig} plus
206
+ * `classOverrides`. A class whose override sets `db` keeps that database.
207
+ * Same per-request rule as {@link databaseConfig}.
208
+ */
209
+ classOptions(className: string): SmrtClassOptions;
210
+ /**
211
+ * Run `fn` as a principal the route verified itself (for example a bearer
212
+ * token mapped by an MCP route), replacing the cookie session's permission
213
+ * context for that call. The principal's permissions are resolved live from
214
+ * its membership in `tenantId` and, when `scopes` is given, capped to it;
215
+ * under the RLS transaction rule of {@link databaseConfig} a fresh
216
+ * transaction publishes that user, tenant and permission set, and
217
+ * `databaseConfig()`/`classOptions()` return it inside `fn`. Rejects (before
218
+ * `fn` runs) without a user id, a tenant, or an authorized membership.
219
+ *
220
+ * `fn` receives the principal with `scopes` replaced by that effective
221
+ * permission set (live permissions, capped by the given scopes). Callers
222
+ * that authorize in code (rather than through database policy, as under
223
+ * `application` isolation) must authorize with these effective scopes so a
224
+ * revoked permission takes effect even while a token still carries it.
225
+ */
226
+ runAsPrincipal<T, P extends SmrtRuntimeBoundPrincipal = SmrtRuntimeBoundPrincipal>(principal: P, fn: (bound: P & {
227
+ scopes: string[];
228
+ }) => Promise<T>): Promise<T>;
229
+ /** The local runtime. Rejects outside the `local` profile. */
230
+ localRuntime(): Promise<LocalApplicationRuntime>;
231
+ /** The deployed runtime. Rejects in the `local` profile. */
232
+ deployedRuntime(): Promise<DeployedApplicationRuntime>;
233
+ /** Liveness body for the health route (awaits startup). */
234
+ health(): Promise<RuntimeHealthBody>;
235
+ /**
236
+ * Read private runtime state and project it onto the public allowlist.
237
+ * Call only after the route has authenticated and authorized its caller.
238
+ */
239
+ readDiagnostics(options: ApplicationRuntimeDiagnosticsOptions): Promise<RuntimeDiagnostics>;
240
+ }
241
+ /** @internal Test and composition seams; not part of the public entry. */
242
+ export interface SmrtSvelteKitRuntimeSeams {
243
+ readonly initializeLocal?: typeof initializeLocalApplicationRuntime;
244
+ readonly initializeDeployed?: typeof initializeDeployedApplicationRuntime;
245
+ readonly loadRuntime?: () => Promise<ResolvedApplicationRuntime>;
246
+ }
247
+ /**
248
+ * Compose runtime startup, tenant selection, the signed session, and
249
+ * authorized-tenant locals into a SvelteKit `{ handle, init }` pair.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * // src/hooks.server.ts
254
+ * import { createSmrtSvelteKitRuntime } from '@happyvertical/smrt-app-runtime/sveltekit';
255
+ *
256
+ * export const runtime = createSmrtSvelteKitRuntime();
257
+ * export const { handle, init } = runtime;
258
+ * ```
259
+ */
260
+ export declare function createSmrtSvelteKitRuntime(options?: SmrtSvelteKitRuntimeOptions): SmrtSvelteKitRuntime;
261
+ /** @internal */
262
+ export declare function composeSmrtSvelteKitRuntime(options: SmrtSvelteKitRuntimeOptions, seams: SmrtSvelteKitRuntimeSeams): SmrtSvelteKitRuntime;
263
+ /**
264
+ * Step 3: publish the authorized tenant context only when this runtime's
265
+ * session step verified the session tenant and it matches the active
266
+ * AsyncLocalStorage context. Locals populated by any other session layer are
267
+ * never published.
268
+ */
269
+ export declare const authorizedTenantLocalsHandle: Handle;
270
+ /**
271
+ * Run handles in order. Unlike `sequence()` from `@sveltejs/kit/hooks`, this
272
+ * needs no SvelteKit request store, so the composed handle is usable (and
273
+ * testable) outside the Kit server runtime. The runtime's own handles never
274
+ * pass resolve options; any options a handle passes are forwarded unchanged.
275
+ */
276
+ export declare function composeHandles(...handles: readonly Handle[]): Handle;
277
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/sveltekit/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAEL,KAAK,0BAA0B,EAGhC,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAIL,KAAK,oBAAoB,EAC1B,MAAM,6BAA6B,CAAC;AAUrC,OAAO,KAAK,EACV,qBAAqB,EACrB,aAAa,EACd,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,KAAK,iBAAiB,EAAe,MAAM,oBAAoB,CAAC;AACzE,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAKxD,OAAO,EACL,KAAK,0BAA0B,EAE/B,oCAAoC,EAGrC,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,iCAAiC,EACjC,KAAK,uBAAuB,EAC5B,KAAK,6BAA6B,EAGnC,MAAM,aAAa,CAAC;AACrB,OAAO,EAEL,KA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deliberately separate:
5
+ *
6
+ * - a selector maps a trusted URL shape (by default the leading subdomain) to
7
+ * an active tenant record and publishes it as `locals.selectedTenant*`;
8
+ * - the signed session (`createSessionHandler({ enterTenantContext: true })`)
9
+ * authorizes the signed-in user's membership and establishes the request
10
+ * tenant context.
11
+ *
12
+ * A selected tenant is useful for login and membership-gated switch flows, but
13
+ * it never becomes ambient query authority by itself. The default selector
14
+ * ignores client-provided tenant headers.
15
+ */
16
+ import type { SmrtClassOptions } from '@happyvertical/smrt-core';
17
+ /** The request fields a tenant selector may read. */
18
+ export interface TenantSelectionEvent {
19
+ readonly url: URL;
20
+ readonly request: {
21
+ readonly headers: Headers;
22
+ };
23
+ }
24
+ /** URL-derived tenant candidate. Never authorization. */
25
+ export interface TenantSelection {
26
+ /** Database ID for an active tenant, or null when none resolves. */
27
+ readonly tenantId: string | null;
28
+ /** URL-derived slug. Informational until membership authorizes a switch. */
29
+ readonly tenantSlug: string | null;
30
+ }
31
+ /**
32
+ * Replaceable selection extension point. Implementations must preserve the
33
+ * rule that selection alone never establishes authorization context.
34
+ */
35
+ export type TenantSelector = (event: TenantSelectionEvent) => TenantSelection | Promise<TenantSelection>;
36
+ /**
37
+ * Pure URL parser used by the default selector.
38
+ *
39
+ * Set `TENANT_BASE_DOMAIN` (or pass `baseDomain`) in production so
40
+ * multi-label public suffixes are never guessed. Without it, the fallback is
41
+ * intended only for local domains such as `acme.demo.local`.
42
+ */
43
+ export declare function selectTenantSlug(url: URL, baseDomain?: string | undefined): string | null;
44
+ export interface SubdomainTenantSelectorOptions {
45
+ /** Explicit public base domain. Defaults to `TENANT_BASE_DOMAIN`. */
46
+ readonly baseDomain?: string;
47
+ /** Collection options for the `Tenant` lookup. */
48
+ readonly classOptions: () => SmrtClassOptions;
49
+ }
50
+ /**
51
+ * Default selector: the leading subdomain names an ACTIVE tenant.
52
+ *
53
+ * A missing `tenants` table (a fresh project before its first migration)
54
+ * yields no selection; the runtime never creates that table.
55
+ */
56
+ export declare function createSubdomainTenantSelector(options: SubdomainTenantSelectorOptions): TenantSelector;
57
+ /** Coerce an application selector's result into the published shape. */
58
+ export declare function normalizeTenantSelection(selection: TenantSelection | null | undefined): TenantSelection;
59
+ //# sourceMappingURL=tenancy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenancy.d.ts","sourceRoot":"","sources":["../../src/sveltekit/tenancy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAGjE,qDAAqD;AACrD,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,QAAQ,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;CACjD;AAED,yDAAyD;AACzD,MAAM,WAAW,eAAe;IAC9B,oEAAoE;IACpE,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,4EAA4E;IAC5E,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CACpC;AAED;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,CAC3B,KAAK,EAAE,oBAAoB,KACxB,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;AAShD;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,GAAG,EACR,UAAU,GAAE,MAAM,GAAG,SAA0C,GAC9D,MAAM,GAAG,IAAI,CA4Bf;AAYD,MAAM,WAAW,8BAA8B;IAC7C,qEAAqE;IACrE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,kDAAkD;IAClD,QAAQ,CAAC,YAAY,EAAE,MAAM,gBAAgB,CAAC;CAC/C;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,8BAA8B,GACtC,cAAc,CAgBhB;AAED,wEAAwE;AACxE,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,eAAe,GAAG,IAAI,GAAG,SAAS,GAC5C,eAAe,CAUjB"}
@@ -0,0 +1,2 @@
1
+ export * from './sveltekit/index.js';
2
+ //# sourceMappingURL=sveltekit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sveltekit.d.ts","sourceRoot":"","sources":["../src/sveltekit.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}