@boon4681/giri 0.0.2-alpha-4 → 0.0.2-alpha-6

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.
@@ -201,6 +201,64 @@ async function composeMiddleware(middleware, handle, context) {
201
201
  return result;
202
202
  }
203
203
 
204
+ // src/logger.ts
205
+ var noColor = !process.stdout.isTTY || process.env.NO_COLOR !== void 0 || process.env.TERM === "dumb" || process.env.FORCE_COLOR === "0";
206
+ function paint(open, close) {
207
+ return (text) => noColor ? text : `\x1B[${open}m${text}\x1B[${close}m`;
208
+ }
209
+ var color = {
210
+ dim: paint(2, 22),
211
+ bold: paint(1, 22),
212
+ red: paint(31, 39),
213
+ green: paint(32, 39),
214
+ yellow: paint(33, 39),
215
+ blue: paint(34, 39),
216
+ magenta: paint(35, 39),
217
+ cyan: paint(36, 39),
218
+ gray: paint(90, 39)
219
+ };
220
+ var highlight = (text) => color.green(text);
221
+ function timestamp() {
222
+ const now = /* @__PURE__ */ new Date();
223
+ const pad = (n) => String(n).padStart(2, "0");
224
+ return `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
225
+ }
226
+ var TAG = "giri";
227
+ function line(tag2, message, scope) {
228
+ const parts = [color.gray(timestamp()), tag2];
229
+ if (scope) {
230
+ parts.push(color.dim(`(${scope})`));
231
+ }
232
+ parts.push(message);
233
+ return parts.join(" ");
234
+ }
235
+ var tag = {
236
+ info: color.bold(color.cyan(`[${TAG}]`)),
237
+ warn: color.bold(color.yellow(`[${TAG}]`)),
238
+ error: color.bold(color.red(`[${TAG}]`))
239
+ };
240
+ var log = {
241
+ info(message, scope) {
242
+ console.log(line(tag.info, message, scope));
243
+ },
244
+ success(message, scope) {
245
+ console.log(line(tag.info, color.green(message), scope));
246
+ },
247
+ warn(message, scope) {
248
+ console.warn(line(tag.warn, color.yellow(message), scope));
249
+ },
250
+ error(message, scope) {
251
+ console.error(line(tag.error, color.red(message), scope));
252
+ },
253
+ ready(url) {
254
+ console.log(line(tag.info, `${color.green("ready")} on ${color.cyan(url)}`));
255
+ },
256
+ change(verb, path, count) {
257
+ const suffix = count && count > 1 ? ` ${color.dim(`(x${count})`)}` : "";
258
+ console.log(line(tag.info, `${color.green(verb)} ${highlight(path)}${suffix}`, "watch"));
259
+ }
260
+ };
261
+
204
262
  // src/validation.ts
205
263
  function isGiriInputSchema(value) {
206
264
  return Boolean(
@@ -359,8 +417,15 @@ async function routeHandler(honoContext, route) {
359
417
  cookieSecret: route.cookieSecret,
360
418
  cookies: honoCookieJar
361
419
  });
362
- const result = await composeMiddleware(route.middleware, route.handle, context);
363
- return toResponse(result, context);
420
+ try {
421
+ const result = await composeMiddleware(route.middleware, route.handle, context);
422
+ return toResponse(result, context);
423
+ } catch (error) {
424
+ const err = error instanceof Error ? error : new Error(String(error));
425
+ log.error(`${route.method} ${route.path} - ${err.message}`, "request");
426
+ console.error(err.stack ?? err);
427
+ return new Response("Internal Server Error", { status: 500 });
428
+ }
364
429
  }
365
430
  function syncHonoVars(honoContext, giriContext) {
366
431
  const vars = honoContext.var;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/adapters/hono.ts","../../src/types.ts","../../src/context.ts","../../src/validation.ts"],"sourcesContent":["import { serve as serveNode } from '@hono/node-server';\nimport { Hono } from 'hono';\nimport type { Context as HonoContext, ContextVariableMap, MiddlewareHandler } from 'hono';\nimport { parse, parseSigned, serialize, serializeSigned } from 'hono/utils/cookie';\nimport {\n composeMiddleware,\n createContext,\n isTypedResponse,\n toResponse,\n typedResponseToResponse,\n} from '../context';\nimport { nativeContextBrand } from '../types';\nimport type {\n Context as GiriContext,\n CookieJarFactory,\n GiriAdapter,\n GiriRouteRegistration,\n Middleware,\n ValidatedInput,\n} from '../types';\nimport { prepareRequestInput } from '../validation';\n\nconst honoCookieJar: CookieJarFactory = ({ request, append, secret }) => {\n const header = request.headers.get('cookie') ?? '';\n const requireSecret = (): string => {\n if (!secret) {\n throw new Error('Signed cookies require `cookieSecret` in giri.config.');\n }\n return secret;\n };\n\n return {\n get: (name) => parse(header, name)[name],\n all: () => parse(header),\n set: (name, value, options) => append(serialize(name, value, options)),\n delete: (name, options) =>\n append(serialize(name, '', { ...options, maxAge: 0, expires: new Date(0) })),\n getSigned: async (name) => (await parseSigned(header, requireSecret(), name))[name],\n setSigned: async (name, value, options) =>\n append(await serializeSigned(name, value, requireSecret(), options)),\n };\n};\n\nexport type HonoGiriApp = Hono;\nexport type HonoContextVars = { [K in keyof ContextVariableMap]: ContextVariableMap[K] };\n\nasync function routeHandler(honoContext: HonoContext, route: GiriRouteRegistration): Promise<Response> {\n const prepared = await prepareRequestInput(honoContext.req.raw, route.input);\n if (!prepared.ok) {\n return toResponse(prepared.response);\n }\n\n const context = createContext({\n request: honoContext.req.raw,\n params: honoContext.req.param() as Record<string, string>,\n validated: prepared.validated,\n app: route.services,\n native: honoContext,\n cookieSecret: route.cookieSecret,\n cookies: honoCookieJar,\n });\n const result = await composeMiddleware(route.middleware, route.handle, context);\n return toResponse(result, context);\n}\n\nfunction syncHonoVars(honoContext: HonoContext, giriContext: GiriContext): void {\n const vars = honoContext.var as Record<string, unknown> | undefined;\n if (!vars) {\n return;\n }\n for (const key of Object.keys(vars)) {\n giriContext.set(key, vars[key]);\n }\n}\n\n/**\n * Wrap one or more native Hono middleware as a single giri `Middleware`, so the existing Hono\n * ecosystem (`@hono/oauth-providers`, CORS, etc.) runs unchanged on a giri route:\n *\n * ```ts\n * // routes/auth/google/+shared.ts\n * import { fromHono } from \"@boon4681/giri/adapters/hono\";\n * import { googleAuth } from \"@hono/oauth-providers/google\";\n *\n * export const middleware = stack(\n * fromHono(googleAuth({ client_id: …, client_secret: …, scope: [\"openid\", \"email\"] })),\n * );\n * // downstream handler: const user = c.get(\"user-google\");\n * ```\n *\n * It runs the Hono middleware against the real Hono context (cookies, `c.redirect`, `c.req.query`\n * all work), then mirrors any vars it set onto giri's `c` for downstream `c.get`. Only valid on the\n * Hono adapter - throws on any other backend.\n */\nexport function fromHono<Vars extends Record<string, unknown> = HonoContextVars>(\n ...handlers: MiddlewareHandler[]\n): Middleware<Record<string, string>, ValidatedInput, Vars> {\n if (handlers.length === 0) {\n throw new Error('fromHono() requires at least one Hono middleware.');\n }\n\n return async (c, giriNext) => {\n const honoContext = (c as unknown as Record<symbol, unknown>)[nativeContextBrand] as\n | HonoContext\n | undefined;\n if (!honoContext) {\n throw new Error(\n 'fromHono() can only run on the Hono adapter - no native Hono context found on the giri context.',\n );\n }\n\n const tail = async (): Promise<void> => {\n syncHonoVars(honoContext, c);\n const result = await giriNext();\n if (result instanceof Response) {\n honoContext.res = result;\n } else if (isTypedResponse(result)) {\n honoContext.res = typedResponseToResponse(result);\n }\n };\n\n const dispatch = (index: number): Promise<unknown> => {\n const handler = handlers[index];\n if (!handler) {\n return tail();\n }\n return Promise.resolve(handler(honoContext, () => dispatch(index + 1) as Promise<void>));\n };\n\n const returned = await dispatch(0);\n syncHonoVars(honoContext, c);\n return returned instanceof Response ? returned : honoContext.res;\n };\n}\n\nfunction registerHonoRoute(app: Hono, route: GiriRouteRegistration): void {\n type HonoHandler = (c: HonoContext) => Promise<Response>;\n\n const handler: HonoHandler = (c) => routeHandler(c, route);\n const method = route.method.toLowerCase();\n const appMethods = app as never as Record<string, (path: string, handler: HonoHandler) => void>;\n\n if (method in app && typeof appMethods[method] === 'function') {\n appMethods[method](route.path, handler);\n return;\n }\n\n throw new Error(`Hono adapter does not support ${route.method}.`);\n}\n\nexport function hono(): GiriAdapter<HonoGiriApp> {\n return {\n name: 'hono',\n createApp: () => new Hono({ strict: false }),\n register: registerHonoRoute,\n fetch: async (app, req) => app.fetch(req),\n serve: (handler, options, onListen) => {\n const server = serveNode(\n {\n fetch: handler,\n port: options.port,\n hostname: options.hostname,\n },\n onListen ? (info) => onListen({ address: info.address, port: info.port }) : undefined,\n );\n\n return {\n close: () => {\n return new Promise<void>((resolve, reject) => {\n server.close((error) => (error ? reject(error) : resolve()));\n })\n }\n };\n },\n };\n}\n","export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';\n\nexport type StatusCode = number;\n\nexport type ResponseFormat = 'json' | 'text' | 'html';\n\nexport const typedResponseBrand: unique symbol = Symbol.for('giri.typed-response') as never;\nexport const nativeContextBrand: unique symbol = Symbol.for('giri.native-context') as never;\n\nexport interface TypedResponse<\n T,\n S extends StatusCode = StatusCode,\n F extends ResponseFormat = ResponseFormat,\n> {\n readonly [typedResponseBrand]: {\n data: T;\n status: S;\n format: F;\n };\n readonly data: T;\n readonly status: S;\n readonly format: F;\n readonly headers?: HeadersInit;\n}\n\nexport type HandlerResponse = Response | TypedResponse<unknown, StatusCode, ResponseFormat>;\n\nexport interface ValidatedInput {\n /**\n * The validated request body. For a single declared content-type it's that schema's\n * output; for several it's a discriminated union `{ type; data }` (see `ValidBody`).\n */\n body?: unknown;\n query?: unknown;\n}\n\n/** Attributes for a `Set-Cookie` header. `path` defaults to `/`. */\nexport interface CookieOptions {\n domain?: string;\n path?: string;\n /** Lifetime in seconds. */\n maxAge?: number;\n expires?: Date;\n httpOnly?: boolean;\n secure?: boolean;\n sameSite?: 'Strict' | 'Lax' | 'None' | 'strict' | 'lax' | 'none';\n partitioned?: boolean;\n priority?: 'Low' | 'Medium' | 'High' | 'low' | 'medium' | 'high';\n}\n\n/**\n * Cookie read/write, implemented per adapter with its runtime's native helpers. giri core\n * supplies the {@link CookieSink} (where to read from / write to); the adapter owns encoding.\n */\nexport interface CookieJar {\n get(name: string): string | undefined;\n all(): Record<string, string>;\n set(name: string, value: string, options?: CookieOptions): void;\n delete(name: string, options?: CookieOptions): void;\n getSigned(name: string): Promise<string | false | undefined>;\n setSigned(name: string, value: string, options?: CookieOptions): Promise<void>;\n}\n\n/** What core hands an adapter's cookie jar: the request to read from, the response sink to write to. */\nexport interface CookieSink {\n /** The incoming request, for reading the `Cookie` header. */\n request: Request;\n /** Append one already-serialized `Set-Cookie` header value to the response. */\n append(setCookieHeader: string): void;\n /** The configured `cookieSecret`, if any (for signed cookies). */\n secret?: string;\n}\n\n/** Builds a {@link CookieJar} bound to one request's {@link CookieSink}. Each adapter provides one. */\nexport type CookieJarFactory = (sink: CookieSink) => CookieJar;\n\nexport interface GiriRequest<Input extends ValidatedInput = ValidatedInput> {\n raw: Request;\n url: URL;\n method: string;\n header(name: string): string | null;\n json<T = unknown>(): Promise<T>;\n text(): Promise<string>;\n arrayBuffer(): Promise<ArrayBuffer>;\n formData(): Promise<FormData>;\n valid<K extends keyof Input & ('body' | 'query')>(key: K): Input[K];\n /** Read a request cookie by name, or `undefined` if absent. */\n cookie(name: string): string | undefined;\n /** All request cookies as a name→value map. */\n cookies(): Record<string, string>;\n /**\n * Read and verify a signed cookie. Resolves to the original value, `false` if the\n * signature was tampered with, or `undefined` if the cookie is absent. Requires\n * `cookieSecret` in `giri.config`.\n */\n signedCookie(name: string): Promise<string | false | undefined>;\n}\n\ndeclare global {\n /**\n * Global registration surface for app-wide types. `giri sync` augments\n * `Giri.Register[\"app\"]` from `src/main.ts` `init()` return type so `c.app` is\n * typed without per-route generics (the registration pattern).\n */\n namespace Giri {\n interface Register {}\n }\n}\n\n/**\n * The app-wide services container, the type of `c.app`. `giri sync` infers it from\n * `src/main.ts`'s `init()` return type (via the global `Giri.Register` augmentation);\n * until then it falls back to an open record. Leave `init` unannotated (its return is\n * the source of truth) and annotate `teardown`'s parameter with this:\n *\n * ```ts\n * export const init = () => ({ db }); // inferred\n * export const teardown = (services: Services) => services.db.close();\n * ```\n */\nexport type Services = Giri.Register extends { app: infer A }\n ? A\n : Record<string, unknown>;\n\nexport interface Context<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n Vars extends Record<string, unknown> = {},\n> {\n params: Params;\n /** App-wide services from `src/main.ts`'s `init()`, seeded into every request. */\n app: Services;\n req: GiriRequest<Input>;\n // Context vars (`c.set`/`c.get`). Keys declared by middleware (`Vars`) are typed;\n // any other key stays open (`unknown`) so untracked keys still work.\n set<K extends keyof Vars & string>(key: K, value: Vars[K]): void;\n set<K extends string>(key: K, value: unknown): void;\n get<K extends keyof Vars & string>(key: K): Vars[K];\n get<V = unknown>(key: string): V;\n json<T, S extends StatusCode = 200>(\n data: T,\n status?: S,\n headers?: HeadersInit,\n ): TypedResponse<T, S, 'json'>;\n text<S extends StatusCode = 200>(\n text: string,\n status?: S,\n headers?: HeadersInit,\n ): TypedResponse<string, S, 'text'>;\n /** An HTML response (`text/html`). Like `text`, the body is a string. */\n html<S extends StatusCode = 200>(\n html: string,\n status?: S,\n headers?: HeadersInit,\n ): TypedResponse<string, S, 'html'>;\n /** A raw-body response - string, stream, buffer, FormData, … (not documented in OpenAPI). */\n body(data: BodyInit | null, status?: StatusCode, headers?: HeadersInit): Response;\n /** Alias of `body`, mirroring Hono's `c.newResponse`. */\n newResponse(data: BodyInit | null, status?: StatusCode, headers?: HeadersInit): Response;\n /** A redirect (defaults to 302) with the `Location` header set. */\n redirect(location: string, status?: StatusCode): Response;\n /** A 404 Not Found response. */\n notFound(): Response;\n /**\n * Set a response header applied to whatever this handler returns. Pass `{ append: true }` to add\n * another value (e.g. `Set-Cookie`); omit `value` to delete. Mirrors Hono's `c.header`.\n */\n header(name: string, value?: string, options?: { append?: boolean }): void;\n /** Default status for `body`/`redirect`, and for `json`/`text`/`html` when no status arg is given. */\n status(code: StatusCode): void;\n /**\n * Set a response cookie via `Set-Cookie`. Pass `value: null` to delete it (send the\n * same `path`/`domain` you set it with). Stacks with other cookies set this request.\n */\n cookie(name: string, value: string | null, options?: CookieOptions): void;\n /** Set an HMAC-signed cookie. Requires `cookieSecret` in `giri.config`. */\n signedCookie(name: string, value: string, options?: CookieOptions): Promise<void>;\n}\n\nexport type Handle<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n Vars extends Record<string, unknown> = {},\n> = (c: Context<Params, Input, Vars>) => HandlerResponse | Promise<HandlerResponse>;\n\nexport type Next = () => Promise<HandlerResponse | void>;\n\n/** An OpenAPI security requirement, e.g. `{ bearerAuth: [] }`. */\nexport type SecurityRequirement = Record<string, string[]>;\n\nexport interface MiddlewareOpenApi {\n /** Security requirements this middleware enforces */\n security?: SecurityRequirement[];\n /** Optional scheme definitions, merged into `components.securitySchemes` so the doc is self-contained. */\n securitySchemes?: Record<string, unknown>;\n [key: string]: unknown;\n}\n\nexport interface MiddlewareOptions {\n openapi?: MiddlewareOpenApi;\n}\n\nexport interface Middleware<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n Vars extends Record<string, unknown> = {},\n> {\n (c: Context<Params, Input, Vars>, next: Next): HandlerResponse | void | Promise<HandlerResponse | void>;\n openapi?: MiddlewareOpenApi;\n}\n\n/** The context vars a middleware injects (its `Vars` type parameter). */\nexport type VarsOf<M> = M extends Middleware<Record<string, string>, ValidatedInput, infer V>\n ? V\n : {};\n\n/** Intersect the injected vars of a tuple of middleware (built with `stack(...)`). */\nexport type MergeStack<T> = T extends readonly [infer Head, ...infer Rest]\n ? VarsOf<Head> & MergeStack<Rest>\n : {};\n\n/**\n * Merge the injected vars of a `middleware` export. A `stack(...)` tuple is merged element-wise;\n * a single bare middleware (`export const middleware = fromHono(...)`) contributes its own vars; a\n * plain `Middleware[]` (not a `stack(...)` tuple) contributes nothing - its element types are lost.\n */\nexport type InferStackVars<T> = T extends readonly [unknown, ...unknown[]]\n ? MergeStack<T>\n : T extends Middleware<Record<string, string>, ValidatedInput, any>\n ? VarsOf<T>\n : {};\n\n/**\n * The vars injected by a module own `middleware` export (a `stack(...)`). Used by the\n * generated per-method handle so a verb file's own `export const middleware` types\n * `c.get`/`c.set`, on top of the folder's `+shared.ts` chain.\n */\nexport type MiddlewareVarsOf<M> = M extends { middleware: infer Stack }\n ? InferStackVars<Stack>\n : {};\n\n/** A JSON Schema object (JSON Schema 2020-12 / OpenAPI 3.1 dialect). */\nexport type JsonSchema = Record<string, unknown>;\n\nexport const inputSchemaBrand: unique symbol = Symbol.for('giri.input-schema') as never;\n\nexport type InputValidationResult<Output = unknown> =\n | { ok: true; value: Output }\n | { ok: false; issues: unknown };\n\n/**\n * A input schema every wrapper form (`body`/`query`) export takes. A vendor\n * adapter (`@boon4681/giri/validators/zod`, `@boon4681/giri/validators/valibot`, …) returns one; build a\n * custom one with `defineInputSchema`. giri core depends only on this interface, never\n * on a validator library. `validate` is the runtime check; `toJsonSchema` feeds OpenAPI.\n */\nexport interface GiriInputSchema<Output = unknown> {\n readonly [inputSchemaBrand]: true;\n validate(value: unknown): InputValidationResult<Output> | Promise<InputValidationResult<Output>>;\n toJsonSchema(): JsonSchema;\n}\n\n/** Extract the validated output type of a giri input schema: `Infer<typeof body>`. */\nexport type Infer<T> = T extends GiriInputSchema<infer Output> ? Output : never;\n\nexport type BodyContentType = 'json' | 'form' | 'urlencoded' | 'text';\n\nexport const bodySchemaBrand: unique symbol = Symbol.for('giri.body-schema') as never;\n\n/**\n * A request body declared as a set of accepted content-types wrapped form `body`\n * takes (`zod.body({ json, form })`). One key means that encoding only; several mean the\n * endpoint accepts any of them, dispatched at runtime on the request `Content-Type`.\n * Each entry is a plain `GiriInputSchema`, so `validate`/`toJsonSchema` work per content-type.\n */\nexport interface GiriBodySchema<\n Outputs extends Partial<Record<BodyContentType, unknown>> = Partial<Record<BodyContentType, unknown>>,\n> {\n readonly [bodySchemaBrand]: true;\n readonly contents: { [K in keyof Outputs & BodyContentType]: GiriInputSchema<Outputs[K]> };\n}\n\n/** True when `T` is a union of more than one member. */\ntype IsUnion<T, U = T> = T extends unknown ? ([U] extends [T] ? false : true) : never;\n\n/**\n * The validated body a handler receives. A single declared content-type yields that\n * schema's output directly; several yield a discriminated union keyed by content-type.\n */\nexport type ValidBody<B> = B extends GiriBodySchema<infer Outputs>\n ? IsUnion<keyof Outputs> extends true\n ? { [K in keyof Outputs]: { type: K; data: Outputs[K] } }[keyof Outputs]\n : Outputs[keyof Outputs]\n : never;\n\n/** The validated query a handler receives. */\nexport type ValidQuery<Q> = Q extends GiriInputSchema<infer Output> ? Output : never;\n\n/** Drop keys whose value resolved to `never` (an input the route didn't declare). */\ntype PruneNever<T> = { [K in keyof T as [T[K]] extends [never] ? never : K]: T[K] };\n\n/**\n * Derive a route's `ValidatedInput` from a module's `body`/`query` exports. The generated\n * per-method `$types` handle (`POST`, `GET`, …) uses this so handlers infer `c.req.valid`\n * with no manual generic.\n */\nexport type RouteInputOf<M> = PruneNever<{\n body: M extends { body: infer B } ? ValidBody<B> : never;\n query: M extends { query: infer Q } ? ValidQuery<Q> : never;\n}>;\n\nexport interface RouteInput {\n body?: GiriBodySchema;\n query?: GiriInputSchema;\n}\n\nexport interface RouteOpenApi {\n /** Omit this route from the generated `openapi.json` (it still serves normally). */\n hidden?: boolean;\n // Room to grow: summary, description, tags, deprecated, operationId, …\n}\n\nexport type RouteOpenApiConfig = RouteOpenApi | boolean;\n\nexport interface GiriRouteRegistration {\n method: HttpMethod;\n path: string;\n handle: Handle;\n middleware: Middleware[];\n input?: RouteInput;\n /** App-wide services to seed onto `c.app` (same instance for every route). */\n services?: Services;\n /** Secret for signing/verifying cookies (`c.signedCookie`), from `config.cookieSecret`. */\n cookieSecret?: string;\n}\n\nexport type GiriFetchHandler = (req: Request) => Response | Promise<Response>;\n\nexport interface GiriServeOptions {\n port: number;\n hostname?: string;\n}\n\nexport interface GiriServerInfo {\n address: string;\n port: number;\n}\n\nexport interface GiriServer {\n close(): void | Promise<void>;\n}\n\nexport interface GiriAdapter<App> {\n name?: string;\n createApp(): App;\n register(app: App, route: GiriRouteRegistration): void;\n fetch(app: App, req: Request): Promise<Response>;\n /**\n * Bind the configured backend's runtime to a port and start serving.\n * giri core stays runtime-agnostic: it hands the adapter a request handler\n * (so hot-reload keeps working) and the adapter owns the actual server.\n */\n serve(\n handler: GiriFetchHandler,\n options: GiriServeOptions,\n onListen?: (info: GiriServerInfo) => void,\n ): GiriServer;\n}\n\nexport interface GiriConfig<App = unknown> {\n adapter: GiriAdapter<App>;\n alias?: Record<string, string | string[]>;\n outDir?: string;\n server?: {\n port?: number;\n hostname?: string;\n };\n errorSchema?: unknown;\n /** Secret used to sign/verify cookies via `c.signedCookie` / `c.req.signedCookie`. */\n cookieSecret?: string;\n}\n\nexport interface GiriPaths {\n cwd: string;\n routesDir: string;\n outDir: string;\n}\n","import {\n type CookieJar,\n type CookieJarFactory,\n type Services,\n type Context,\n type HandlerResponse,\n type Middleware,\n type MiddlewareOptions,\n type ResponseFormat,\n type StatusCode,\n type TypedResponse,\n type ValidatedInput,\n nativeContextBrand,\n typedResponseBrand,\n} from './types';\n\nconst BODYLESS_STATUS = new Set([101, 103, 204, 205, 304]);\n\n/** Imperative response state set via `c.header()` / `c.status()`, merged in `toResponse`. */\ninterface PendingResponse {\n headers: Headers;\n status?: StatusCode;\n}\n\nconst pendingResponseBrand: unique symbol = Symbol('giri.pending-response');\n\nfunction getPending(context: Context): PendingResponse | undefined {\n return (context as unknown as Record<symbol, PendingResponse | undefined>)[pendingResponseBrand];\n}\n\n/** Used when the active adapter provides no cookie jar; every cookie call throws. */\nconst unsupportedCookieJar: CookieJar = {\n get: cookiesUnsupported,\n all: cookiesUnsupported,\n set: cookiesUnsupported,\n delete: cookiesUnsupported,\n getSigned: cookiesUnsupported,\n setSigned: cookiesUnsupported,\n};\n\nfunction cookiesUnsupported(): never {\n throw new Error('The active adapter does not support cookies.');\n}\n\nexport interface CreateContextOptions<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n> {\n request: Request;\n params?: Params;\n validated?: Input;\n app?: Services;\n /** The adapter's native per-request context, stashed for backend-specific bridges. */\n native?: unknown;\n /** Secret for `c.signedCookie` / `c.req.signedCookie` (from `config.cookieSecret`). */\n cookieSecret?: string;\n /** The adapter's cookie jar, built from its runtime's native helpers. */\n cookies?: CookieJarFactory;\n}\n\nexport function createTypedResponse<\n T,\n S extends StatusCode,\n F extends ResponseFormat,\n>(data: T, status: S, format: F, headers?: HeadersInit): TypedResponse<T, S, F> {\n return {\n [typedResponseBrand]: { data, status, format },\n data,\n status,\n format,\n headers,\n };\n}\n\nexport function isTypedResponse(value: unknown): value is TypedResponse<unknown> {\n return Boolean(value && typeof value === 'object' && typedResponseBrand in value);\n}\n\nexport function createContext<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n>(options: CreateContextOptions<Params, Input>): Context<Params, Input> {\n const url = new URL(options.request.url);\n const store = new Map<string, unknown>();\n const validated = options.validated ?? ({} as Input);\n\n // Headers/status set imperatively via c.header()/c.status(); merged into the final response.\n const pending: PendingResponse = { headers: new Headers() };\n const defaultStatus = (): StatusCode => pending.status ?? 200;\n\n // Cookies are a runtime concern: the adapter owns reading/encoding/signing via its native\n // helpers. Core only hands it the sink (request in, Set-Cookie onto the pending response).\n // No adapter jar => cookies aren't supported, and using them throws.\n const cookies: CookieJar = options.cookies\n ? options.cookies({\n request: options.request,\n append: (header) => pending.headers.append('set-cookie', header),\n secret: options.cookieSecret,\n })\n : unsupportedCookieJar;\n\n const context: Context<Params, Input> = {\n params: options.params ?? ({} as Params),\n app: options.app ?? ({} as Services),\n req: {\n raw: options.request,\n url,\n method: options.request.method,\n header: (name) => options.request.headers.get(name),\n json: <T = unknown>() => options.request.json() as Promise<T>,\n text: () => options.request.text(),\n arrayBuffer: () => options.request.arrayBuffer(),\n formData: () => options.request.formData(),\n valid: (key) => {\n if (!(key in validated)) {\n throw new Error(`No validated ${String(key)} data is available for this route.`);\n }\n return validated[key];\n },\n cookie: (name) => cookies.get(name),\n cookies: () => cookies.all(),\n signedCookie: (name) => cookies.getSigned(name),\n },\n set: (key: string, value: unknown) => {\n store.set(key, value);\n },\n get: (key: string) => store.get(key) as never,\n json: (data, status, headers) =>\n createTypedResponse(data, (status ?? defaultStatus()) as never, 'json', headers),\n text: (text, status, headers) =>\n createTypedResponse(text, (status ?? defaultStatus()) as never, 'text', headers),\n html: (html, status, headers) =>\n createTypedResponse(html, (status ?? defaultStatus()) as never, 'html', headers),\n body: (data, status, headers) =>\n new Response(data, { status: status ?? defaultStatus(), headers }),\n newResponse: (data, status, headers) =>\n new Response(data, { status: status ?? defaultStatus(), headers }),\n redirect: (location, status) =>\n new Response(null, { status: status ?? 302, headers: { Location: location } }),\n notFound: () => new Response('404 Not Found', { status: 404 }),\n header: (name, value, options) => {\n if (value === undefined) {\n pending.headers.delete(name);\n } else if (options?.append) {\n pending.headers.append(name, value);\n } else {\n pending.headers.set(name, value);\n }\n },\n status: (code) => {\n pending.status = code;\n },\n cookie: (name, value, options) => {\n if (value === null) {\n cookies.delete(name, options);\n } else {\n cookies.set(name, value, options);\n }\n },\n signedCookie: (name, value, options) => cookies.setSigned(name, value, options),\n };\n\n (context as unknown as Record<symbol, unknown>)[nativeContextBrand] = options.native;\n (context as unknown as Record<symbol, unknown>)[pendingResponseBrand] = pending;\n\n return context;\n}\n\nexport function typedResponseToResponse(response: TypedResponse<unknown>): Response {\n const headers = new Headers(response.headers);\n\n if (response.format === 'json' && !headers.has('content-type')) {\n headers.set('content-type', 'application/json; charset=utf-8');\n }\n\n if (response.format === 'text' && !headers.has('content-type')) {\n headers.set('content-type', 'text/plain; charset=utf-8');\n }\n\n if (response.format === 'html' && !headers.has('content-type')) {\n headers.set('content-type', 'text/html; charset=utf-8');\n }\n\n const body = BODYLESS_STATUS.has(response.status)\n ? null\n : response.format === 'json'\n ? JSON.stringify(response.data)\n : String(response.data);\n\n return new Response(body, {\n status: response.status,\n headers,\n });\n}\n\n/**\n * Convert a handler's return value to a real `Response`, then merge any headers set imperatively via\n * `c.header()` (the response's own headers win; pending ones fill the gaps). Pass the `context` so\n * those imperative headers are applied; without it the response is returned unchanged.\n */\nexport function toResponse(response: HandlerResponse, context?: Context): Response {\n const base = isTypedResponse(response) ? typedResponseToResponse(response) : response;\n const pending = context ? getPending(context) : undefined;\n if (!pending) {\n return base;\n }\n\n let hasPending = false;\n pending.headers.forEach(() => {\n hasPending = true;\n });\n if (!hasPending) {\n return base;\n }\n\n const headers = new Headers(base.headers);\n pending.headers.forEach((value, key) => {\n // Set-Cookie is multi-valued; forEach coalesces it into one comma-joined string, so\n // handle it separately below to keep each cookie its own header.\n if (key === 'set-cookie') {\n return;\n }\n if (!headers.has(key)) {\n headers.set(key, value);\n }\n });\n for (const cookie of pending.headers.getSetCookie?.() ?? []) {\n headers.append('set-cookie', cookie);\n }\n return new Response(base.body, { status: base.status, statusText: base.statusText, headers });\n}\n\nexport async function composeMiddleware(\n middleware: Middleware[],\n handle: (c: Context) => HandlerResponse | Promise<HandlerResponse>,\n context: Context,\n): Promise<HandlerResponse> {\n let index = -1;\n let result: HandlerResponse | undefined;\n\n const dispatch = async (i: number): Promise<HandlerResponse | void> => {\n if (i <= index) {\n throw new Error('next() called multiple times in giri middleware.');\n }\n index = i;\n\n if (i === middleware.length) {\n result = await handle(context);\n return result;\n }\n\n const returned = await middleware[i](context, () => dispatch(i + 1));\n if (returned !== undefined) {\n result = returned;\n return returned;\n }\n return result;\n };\n\n await dispatch(0);\n\n if (result === undefined) {\n throw new Error('Route completed without returning a response.');\n }\n\n return result;\n}\n\ntype AnyMiddleware<Vars extends Record<string, unknown>> = Middleware<\n Record<string, string>,\n ValidatedInput,\n Vars\n>;\n\nexport function defineMiddleware<Vars extends Record<string, unknown> = {}>(\n middleware: AnyMiddleware<Vars>,\n): AnyMiddleware<Vars>;\nexport function defineMiddleware<Vars extends Record<string, unknown> = {}>(\n options: MiddlewareOptions,\n middleware: AnyMiddleware<Vars>,\n): AnyMiddleware<Vars>;\nexport function defineMiddleware(\n optionsOrMiddleware: MiddlewareOptions | Middleware,\n maybeMiddleware?: Middleware,\n): Middleware {\n if (typeof optionsOrMiddleware === 'function') {\n return optionsOrMiddleware;\n }\n\n if (!maybeMiddleware) {\n throw new Error('defineMiddleware(options, middleware) requires a middleware function.');\n }\n\n maybeMiddleware.openapi = optionsOrMiddleware.openapi;\n return maybeMiddleware;\n}\n\n/**\n * Group middleware into an ordered stack, preserving each element's type as a tuple so\n * the injected context vars (`defineMiddleware<Vars>` / `Middleware<…, Vars>`) propagate\n * to downstream handlers. Use it for `+shared.ts` and verb `middleware` exports:\n * `export const middleware = stack(auth, requireAdmin)`.\n */\n// `Vars` is contravariant (it sits in the `c` parameter), so the constraint must leave it\n// open (`any`) otherwise a middleware that injects vars isn't assignable to the element type.\nexport function stack<T extends Middleware<Record<string, string>, ValidatedInput, any>[]>(...middleware: T): T {\n return middleware;\n}\n","import {\n type BodyContentType,\n type GiriBodySchema,\n type GiriInputSchema,\n type InputValidationResult,\n type RouteInput,\n type TypedResponse,\n type ValidatedInput,\n bodySchemaBrand,\n inputSchemaBrand,\n} from './types';\nimport { createTypedResponse } from './context';\n\ninterface PreparedInput {\n ok: true;\n validated: ValidatedInput;\n}\n\ninterface FailedInput {\n ok: false;\n response: TypedResponse<{ message: string; issues: unknown }, 400 | 415, 'json'>;\n}\n\nexport type PreparedRequestInput = PreparedInput | FailedInput;\n\n/**\n * Build a giri input schema from a `validate` + `toJsonSchema` pair. Vendor adapters use\n * this; you can call it directly to make a custom validator. The brand is a global symbol,\n * so a hand-rolled `{ [Symbol.for(\"giri.input-schema\")]: true, validate, toJsonSchema }` works too.\n */\nexport function defineInputSchema<Output>(\n schema: Omit<GiriInputSchema<Output>, typeof inputSchemaBrand>,\n): GiriInputSchema<Output> {\n return { [inputSchemaBrand]: true, ...schema };\n}\n\nexport function isGiriInputSchema(value: unknown): value is GiriInputSchema {\n return Boolean(\n value &&\n typeof value === 'object' &&\n (value as Record<symbol, unknown>)[inputSchemaBrand] === true,\n );\n}\n\n/**\n * Build a giri body schema from per-content-type input schemas. Validator adapters use this `zod.body({ json, form })`\n */\nexport function defineBodySchema<Outputs extends Partial<Record<BodyContentType, unknown>>>(\n contents: GiriBodySchema<Outputs>['contents'],\n): GiriBodySchema<Outputs> {\n return { [bodySchemaBrand]: true, contents };\n}\n\nexport function isGiriBodySchema(value: unknown): value is GiriBodySchema {\n return Boolean(\n value &&\n typeof value === 'object' &&\n (value as Record<symbol, unknown>)[bodySchemaBrand] === true,\n );\n}\n\nconst MIME_TO_CONTENT_TYPE: Record<string, BodyContentType> = {\n 'application/json': 'json',\n 'multipart/form-data': 'form',\n 'application/x-www-form-urlencoded': 'urlencoded',\n 'text/plain': 'text',\n};\n\nfunction contentTypeFromHeader(header: string | null): BodyContentType | undefined {\n if (!header) {\n return undefined;\n }\n const mime = header.split(';', 1)[0].trim().toLowerCase();\n return MIME_TO_CONTENT_TYPE[mime];\n}\n\n/** Flatten a `FormData` into a plain object, collapsing repeated fields into arrays. */\nfunction formDataObject(form: FormData): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n form.forEach((value, key) => {\n const current = result[key];\n if (current === undefined) {\n result[key] = value;\n } else if (Array.isArray(current)) {\n current.push(value);\n } else {\n result[key] = [current, value];\n }\n });\n return result;\n}\n\nasync function readRawBody(request: Request, contentType: BodyContentType): Promise<unknown> {\n const cloned = request.clone();\n if (contentType === 'json') {\n return cloned.json();\n }\n if (contentType === 'text') {\n return cloned.text();\n }\n return formDataObject(await cloned.formData());\n}\n\nfunction queryObject(url: URL): Record<string, string | string[]> {\n const result: Record<string, string | string[]> = {};\n for (const [key, value] of url.searchParams) {\n const current = result[key];\n if (current === undefined) {\n result[key] = value;\n } else if (Array.isArray(current)) {\n current.push(value);\n } else {\n result[key] = [current, value];\n }\n }\n return result;\n}\n\nasync function runValidation(\n schema: GiriInputSchema,\n value: unknown,\n label: string,\n): Promise<InputValidationResult> {\n if (!isGiriInputSchema(schema)) {\n throw new Error(\n `giri: ${label} schema must be wrapped with a validator, e.g. \\`export const ${label} = zod(...)\\` from @boon4681/giri/validators/zod.`,\n );\n }\n return schema.validate(value);\n}\n\nexport async function prepareRequestInput(request: Request, input?: RouteInput): Promise<PreparedRequestInput> {\n const validated: ValidatedInput = {};\n\n if (input?.query) {\n const query = queryObject(new URL(request.url));\n const result = await runValidation(input.query, query, 'query');\n if (!result.ok) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Invalid query parameters.', issues: result.issues },\n 400,\n 'json',\n ),\n };\n }\n validated.query = result.value;\n }\n\n if (input?.body) {\n const contents = input.body.contents as Record<BodyContentType, GiriInputSchema>;\n const declared = Object.keys(contents) as BodyContentType[];\n const requested = contentTypeFromHeader(request.headers.get('content-type'));\n // Pick the schema matching the request's content-type; fall back to JSON when the\n // header is missing/unrecognized but JSON is on offer (so header-less posts still work).\n const chosen: BodyContentType | undefined =\n requested && contents[requested] ? requested : contents.json ? 'json' : undefined;\n\n if (!chosen) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Unsupported media type.', issues: { accepted: declared } },\n 415,\n 'json',\n ),\n };\n }\n\n let rawBody: unknown;\n try {\n rawBody = await readRawBody(request, chosen);\n } catch (error) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Invalid request body.', issues: error },\n 400,\n 'json',\n ),\n };\n }\n\n const result = await runValidation(contents[chosen], rawBody, 'body');\n if (!result.ok) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Invalid request body.', issues: result.issues },\n 400,\n 'json',\n ),\n };\n }\n\n validated.body = declared.length > 1 ? { type: chosen, data: result.value } : result.value;\n }\n\n return { ok: true, validated };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAmC;AACnC,kBAAqB;AAErB,oBAA+D;;;ACGxD,IAAM,qBAAoC,uBAAO,IAAI,qBAAqB;AAC1E,IAAM,qBAAoC,uBAAO,IAAI,qBAAqB;AA6O1E,IAAM,mBAAkC,uBAAO,IAAI,mBAAmB;;;ACpO7E,IAAM,kBAAkB,oBAAI,IAAI,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG,CAAC;AAQzD,IAAM,uBAAsC,uBAAO,uBAAuB;AAE1E,SAAS,WAAW,SAA+C;AAC/D,SAAQ,QAAmE,oBAAoB;AACnG;AAGA,IAAM,uBAAkC;AAAA,EACpC,KAAK;AAAA,EACL,KAAK;A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AC/D,CAAC;AAAA,QACL;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;","names":["options","serveNode"]}
1
+ {"version":3,"sources":["../../src/adapters/hono.ts","../../src/types.ts","../../src/context.ts","../../src/logger.ts","../../src/validation.ts"],"sourcesContent":["import { serve as serveNode } from '@hono/node-server';\nimport { Hono } from 'hono';\nimport type { Context as HonoContext, ContextVariableMap, MiddlewareHandler } from 'hono';\nimport { parse, parseSigned, serialize, serializeSigned } from 'hono/utils/cookie';\nimport {\n composeMiddleware,\n createContext,\n isTypedResponse,\n toResponse,\n typedResponseToResponse,\n} from '../context';\nimport { log } from '../logger';\nimport { nativeContextBrand } from '../types';\nimport type {\n Context as GiriContext,\n CookieJarFactory,\n GiriAdapter,\n GiriRouteRegistration,\n Middleware,\n ValidatedInput,\n} from '../types';\nimport { prepareRequestInput } from '../validation';\n\nconst honoCookieJar: CookieJarFactory = ({ request, append, secret }) => {\n const header = request.headers.get('cookie') ?? '';\n const requireSecret = (): string => {\n if (!secret) {\n throw new Error('Signed cookies require `cookieSecret` in giri.config.');\n }\n return secret;\n };\n\n return {\n get: (name) => parse(header, name)[name],\n all: () => parse(header),\n set: (name, value, options) => append(serialize(name, value, options)),\n delete: (name, options) =>\n append(serialize(name, '', { ...options, maxAge: 0, expires: new Date(0) })),\n getSigned: async (name) => (await parseSigned(header, requireSecret(), name))[name],\n setSigned: async (name, value, options) =>\n append(await serializeSigned(name, value, requireSecret(), options)),\n };\n};\n\nexport type HonoGiriApp = Hono;\nexport type HonoContextVars = { [K in keyof ContextVariableMap]: ContextVariableMap[K] };\n\nasync function routeHandler(honoContext: HonoContext, route: GiriRouteRegistration): Promise<Response> {\n const prepared = await prepareRequestInput(honoContext.req.raw, route.input);\n if (!prepared.ok) {\n return toResponse(prepared.response);\n }\n\n const context = createContext({\n request: honoContext.req.raw,\n params: honoContext.req.param() as Record<string, string>,\n validated: prepared.validated,\n app: route.services,\n native: honoContext,\n cookieSecret: route.cookieSecret,\n cookies: honoCookieJar,\n });\n try {\n const result = await composeMiddleware(route.middleware, route.handle, context);\n return toResponse(result, context);\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n log.error(`${route.method} ${route.path} - ${err.message}`, 'request');\n console.error(err.stack ?? err);\n return new Response('Internal Server Error', { status: 500 });\n }\n}\n\nfunction syncHonoVars(honoContext: HonoContext, giriContext: GiriContext): void {\n const vars = honoContext.var as Record<string, unknown> | undefined;\n if (!vars) {\n return;\n }\n for (const key of Object.keys(vars)) {\n giriContext.set(key, vars[key]);\n }\n}\n\n/**\n * Wrap one or more native Hono middleware as a single giri `Middleware`, so the existing Hono\n * ecosystem (`@hono/oauth-providers`, CORS, etc.) runs unchanged on a giri route:\n *\n * ```ts\n * // routes/auth/google/+shared.ts\n * import { fromHono } from \"@boon4681/giri/adapters/hono\";\n * import { googleAuth } from \"@hono/oauth-providers/google\";\n *\n * export const middleware = stack(\n * fromHono(googleAuth({ client_id: …, client_secret: …, scope: [\"openid\", \"email\"] })),\n * );\n * // downstream handler: const user = c.get(\"user-google\");\n * ```\n *\n * It runs the Hono middleware against the real Hono context (cookies, `c.redirect`, `c.req.query`\n * all work), then mirrors any vars it set onto giri's `c` for downstream `c.get`. Only valid on the\n * Hono adapter - throws on any other backend.\n */\nexport function fromHono<Vars extends Record<string, unknown> = HonoContextVars>(\n ...handlers: MiddlewareHandler[]\n): Middleware<Record<string, string>, ValidatedInput, Vars> {\n if (handlers.length === 0) {\n throw new Error('fromHono() requires at least one Hono middleware.');\n }\n\n return async (c, giriNext) => {\n const honoContext = (c as unknown as Record<symbol, unknown>)[nativeContextBrand] as\n | HonoContext\n | undefined;\n if (!honoContext) {\n throw new Error(\n 'fromHono() can only run on the Hono adapter - no native Hono context found on the giri context.',\n );\n }\n\n const tail = async (): Promise<void> => {\n syncHonoVars(honoContext, c);\n const result = await giriNext();\n if (result instanceof Response) {\n honoContext.res = result;\n } else if (isTypedResponse(result)) {\n honoContext.res = typedResponseToResponse(result);\n }\n };\n\n const dispatch = (index: number): Promise<unknown> => {\n const handler = handlers[index];\n if (!handler) {\n return tail();\n }\n return Promise.resolve(handler(honoContext, () => dispatch(index + 1) as Promise<void>));\n };\n\n const returned = await dispatch(0);\n syncHonoVars(honoContext, c);\n return returned instanceof Response ? returned : honoContext.res;\n };\n}\n\nfunction registerHonoRoute(app: Hono, route: GiriRouteRegistration): void {\n type HonoHandler = (c: HonoContext) => Promise<Response>;\n\n const handler: HonoHandler = (c) => routeHandler(c, route);\n const method = route.method.toLowerCase();\n const appMethods = app as never as Record<string, (path: string, handler: HonoHandler) => void>;\n\n if (method in app && typeof appMethods[method] === 'function') {\n appMethods[method](route.path, handler);\n return;\n }\n\n throw new Error(`Hono adapter does not support ${route.method}.`);\n}\n\nexport function hono(): GiriAdapter<HonoGiriApp> {\n return {\n name: 'hono',\n createApp: () => new Hono({ strict: false }),\n register: registerHonoRoute,\n fetch: async (app, req) => app.fetch(req),\n serve: (handler, options, onListen) => {\n const server = serveNode(\n {\n fetch: handler,\n port: options.port,\n hostname: options.hostname,\n },\n onListen ? (info) => onListen({ address: info.address, port: info.port }) : undefined,\n );\n\n return {\n close: () => {\n return new Promise<void>((resolve, reject) => {\n server.close((error) => (error ? reject(error) : resolve()));\n })\n }\n };\n },\n };\n}\n","export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';\n\nexport type StatusCode = number;\n\nexport type ResponseFormat = 'json' | 'text' | 'html';\n\nexport const typedResponseBrand: unique symbol = Symbol.for('giri.typed-response') as never;\nexport const nativeContextBrand: unique symbol = Symbol.for('giri.native-context') as never;\n\nexport interface TypedResponse<\n T,\n S extends StatusCode = StatusCode,\n F extends ResponseFormat = ResponseFormat,\n> {\n readonly [typedResponseBrand]: {\n data: T;\n status: S;\n format: F;\n };\n readonly data: T;\n readonly status: S;\n readonly format: F;\n readonly headers?: HeadersInit;\n}\n\nexport type HandlerResponse = Response | TypedResponse<unknown, StatusCode, ResponseFormat>;\n\nexport interface ValidatedInput {\n /**\n * The validated request body. For a single declared content-type it's that schema's\n * output; for several it's a discriminated union `{ type; data }` (see `ValidBody`).\n */\n body?: unknown;\n query?: unknown;\n}\n\n/** Attributes for a `Set-Cookie` header. `path` defaults to `/`. */\nexport interface CookieOptions {\n domain?: string;\n path?: string;\n /** Lifetime in seconds. */\n maxAge?: number;\n expires?: Date;\n httpOnly?: boolean;\n secure?: boolean;\n sameSite?: 'Strict' | 'Lax' | 'None' | 'strict' | 'lax' | 'none';\n partitioned?: boolean;\n priority?: 'Low' | 'Medium' | 'High' | 'low' | 'medium' | 'high';\n}\n\n/**\n * Cookie read/write, implemented per adapter with its runtime's native helpers. giri core\n * supplies the {@link CookieSink} (where to read from / write to); the adapter owns encoding.\n */\nexport interface CookieJar {\n get(name: string): string | undefined;\n all(): Record<string, string>;\n set(name: string, value: string, options?: CookieOptions): void;\n delete(name: string, options?: CookieOptions): void;\n getSigned(name: string): Promise<string | false | undefined>;\n setSigned(name: string, value: string, options?: CookieOptions): Promise<void>;\n}\n\n/** What core hands an adapter's cookie jar: the request to read from, the response sink to write to. */\nexport interface CookieSink {\n /** The incoming request, for reading the `Cookie` header. */\n request: Request;\n /** Append one already-serialized `Set-Cookie` header value to the response. */\n append(setCookieHeader: string): void;\n /** The configured `cookieSecret`, if any (for signed cookies). */\n secret?: string;\n}\n\n/** Builds a {@link CookieJar} bound to one request's {@link CookieSink}. Each adapter provides one. */\nexport type CookieJarFactory = (sink: CookieSink) => CookieJar;\n\nexport interface GiriRequest<Input extends ValidatedInput = ValidatedInput> {\n raw: Request;\n url: URL;\n method: string;\n header(name: string): string | null;\n json<T = unknown>(): Promise<T>;\n text(): Promise<string>;\n arrayBuffer(): Promise<ArrayBuffer>;\n formData(): Promise<FormData>;\n valid<K extends keyof Input & ('body' | 'query')>(key: K): Input[K];\n /** Read a request cookie by name, or `undefined` if absent. */\n cookie(name: string): string | undefined;\n /** All request cookies as a name→value map. */\n cookies(): Record<string, string>;\n /**\n * Read and verify a signed cookie. Resolves to the original value, `false` if the\n * signature was tampered with, or `undefined` if the cookie is absent. Requires\n * `cookieSecret` in `giri.config`.\n */\n signedCookie(name: string): Promise<string | false | undefined>;\n}\n\ndeclare global {\n /**\n * Global registration surface for app-wide types. `giri sync` augments\n * `Giri.Register[\"app\"]` from `src/main.ts` `init()` return type so `c.app` is\n * typed without per-route generics (the registration pattern).\n */\n namespace Giri {\n interface Register {}\n }\n}\n\n/**\n * The app-wide services container, the type of `c.app`. `giri sync` infers it from\n * `src/main.ts`'s `init()` return type (via the global `Giri.Register` augmentation);\n * until then it falls back to an open record. Leave `init` unannotated (its return is\n * the source of truth) and annotate `teardown`'s parameter with this:\n *\n * ```ts\n * export const init = () => ({ db }); // inferred\n * export const teardown = (services: Services) => services.db.close();\n * ```\n */\nexport type Services = Giri.Register extends { app: infer A }\n ? A\n : Record<string, unknown>;\n\nexport interface Context<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n Vars extends Record<string, unknown> = {},\n> {\n params: Params;\n /** App-wide services from `src/main.ts`'s `init()`, seeded into every request. */\n app: Services;\n req: GiriRequest<Input>;\n // Context vars (`c.set`/`c.get`). Keys declared by middleware (`Vars`) are typed;\n // any other key stays open (`unknown`) so untracked keys still work.\n set<K extends keyof Vars & string>(key: K, value: Vars[K]): void;\n set<K extends string>(key: K, value: unknown): void;\n get<K extends keyof Vars & string>(key: K): Vars[K];\n get<V = unknown>(key: string): V;\n json<T, S extends StatusCode = 200>(\n data: T,\n status?: S,\n headers?: HeadersInit,\n ): TypedResponse<T, S, 'json'>;\n text<S extends StatusCode = 200>(\n text: string,\n status?: S,\n headers?: HeadersInit,\n ): TypedResponse<string, S, 'text'>;\n /** An HTML response (`text/html`). Like `text`, the body is a string. */\n html<S extends StatusCode = 200>(\n html: string,\n status?: S,\n headers?: HeadersInit,\n ): TypedResponse<string, S, 'html'>;\n /** A raw-body response - string, stream, buffer, FormData, … (not documented in OpenAPI). */\n body(data: BodyInit | null, status?: StatusCode, headers?: HeadersInit): Response;\n /** Alias of `body`, mirroring Hono's `c.newResponse`. */\n newResponse(data: BodyInit | null, status?: StatusCode, headers?: HeadersInit): Response;\n /** A redirect (defaults to 302) with the `Location` header set. */\n redirect(location: string, status?: StatusCode): Response;\n /** A 404 Not Found response. */\n notFound(): Response;\n /**\n * Set a response header applied to whatever this handler returns. Pass `{ append: true }` to add\n * another value (e.g. `Set-Cookie`); omit `value` to delete. Mirrors Hono's `c.header`.\n */\n header(name: string, value?: string, options?: { append?: boolean }): void;\n /** Default status for `body`/`redirect`, and for `json`/`text`/`html` when no status arg is given. */\n status(code: StatusCode): void;\n /**\n * Set a response cookie via `Set-Cookie`. Pass `value: null` to delete it (send the\n * same `path`/`domain` you set it with). Stacks with other cookies set this request.\n */\n cookie(name: string, value: string | null, options?: CookieOptions): void;\n /** Set an HMAC-signed cookie. Requires `cookieSecret` in `giri.config`. */\n signedCookie(name: string, value: string, options?: CookieOptions): Promise<void>;\n}\n\nexport type Handle<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n Vars extends Record<string, unknown> = {},\n> = (c: Context<Params, Input, Vars>) => HandlerResponse | Promise<HandlerResponse>;\n\nexport type Next = () => Promise<HandlerResponse | void>;\n\n/** An OpenAPI security requirement, e.g. `{ bearerAuth: [] }`. */\nexport type SecurityRequirement = Record<string, string[]>;\n\nexport interface MiddlewareOpenApi {\n /** Security requirements this middleware enforces */\n security?: SecurityRequirement[];\n /** Optional scheme definitions, merged into `components.securitySchemes` so the doc is self-contained. */\n securitySchemes?: Record<string, unknown>;\n [key: string]: unknown;\n}\n\nexport interface MiddlewareOptions {\n openapi?: MiddlewareOpenApi;\n}\n\nexport interface Middleware<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n Vars extends Record<string, unknown> = {},\n> {\n (c: Context<Params, Input, Vars>, next: Next): HandlerResponse | void | Promise<HandlerResponse | void>;\n openapi?: MiddlewareOpenApi;\n}\n\n/** The context vars a middleware injects (its `Vars` type parameter). */\nexport type VarsOf<M> = M extends Middleware<Record<string, string>, ValidatedInput, infer V>\n ? V\n : {};\n\n/** Intersect the injected vars of a tuple of middleware (built with `stack(...)`). */\nexport type MergeStack<T> = T extends readonly [infer Head, ...infer Rest]\n ? VarsOf<Head> & MergeStack<Rest>\n : {};\n\n/**\n * Merge the injected vars of a `middleware` export. A `stack(...)` tuple is merged element-wise;\n * a single bare middleware (`export const middleware = fromHono(...)`) contributes its own vars; a\n * plain `Middleware[]` (not a `stack(...)` tuple) contributes nothing - its element types are lost.\n */\nexport type InferStackVars<T> = T extends readonly [unknown, ...unknown[]]\n ? MergeStack<T>\n : T extends Middleware<Record<string, string>, ValidatedInput, any>\n ? VarsOf<T>\n : {};\n\n/**\n * The vars injected by a module own `middleware` export (a `stack(...)`). Used by the\n * generated per-method handle so a verb file's own `export const middleware` types\n * `c.get`/`c.set`, on top of the folder's `+shared.ts` chain.\n */\nexport type MiddlewareVarsOf<M> = M extends { middleware: infer Stack }\n ? InferStackVars<Stack>\n : {};\n\n/** A JSON Schema object (JSON Schema 2020-12 / OpenAPI 3.1 dialect). */\nexport type JsonSchema = Record<string, unknown>;\n\nexport const inputSchemaBrand: unique symbol = Symbol.for('giri.input-schema') as never;\n\nexport type InputValidationResult<Output = unknown> =\n | { ok: true; value: Output }\n | { ok: false; issues: unknown };\n\n/**\n * A input schema every wrapper form (`body`/`query`) export takes. A vendor\n * adapter (`@boon4681/giri/validators/zod`, `@boon4681/giri/validators/valibot`, …) returns one; build a\n * custom one with `defineInputSchema`. giri core depends only on this interface, never\n * on a validator library. `validate` is the runtime check; `toJsonSchema` feeds OpenAPI.\n */\nexport interface GiriInputSchema<Output = unknown> {\n readonly [inputSchemaBrand]: true;\n validate(value: unknown): InputValidationResult<Output> | Promise<InputValidationResult<Output>>;\n toJsonSchema(): JsonSchema;\n}\n\n/** Extract the validated output type of a giri input schema: `Infer<typeof body>`. */\nexport type Infer<T> = T extends GiriInputSchema<infer Output> ? Output : never;\n\nexport type BodyContentType = 'json' | 'form' | 'urlencoded' | 'text';\n\nexport const bodySchemaBrand: unique symbol = Symbol.for('giri.body-schema') as never;\n\n/**\n * A request body declared as a set of accepted content-types wrapped form `body`\n * takes (`zod.body({ json, form })`). One key means that encoding only; several mean the\n * endpoint accepts any of them, dispatched at runtime on the request `Content-Type`.\n * Each entry is a plain `GiriInputSchema`, so `validate`/`toJsonSchema` work per content-type.\n */\nexport interface GiriBodySchema<\n Outputs extends Partial<Record<BodyContentType, unknown>> = Partial<Record<BodyContentType, unknown>>,\n> {\n readonly [bodySchemaBrand]: true;\n readonly contents: { [K in keyof Outputs & BodyContentType]: GiriInputSchema<Outputs[K]> };\n}\n\n/** True when `T` is a union of more than one member. */\ntype IsUnion<T, U = T> = T extends unknown ? ([U] extends [T] ? false : true) : never;\n\n/**\n * The validated body a handler receives. A single declared content-type yields that\n * schema's output directly; several yield a discriminated union keyed by content-type.\n */\nexport type ValidBody<B> = B extends GiriBodySchema<infer Outputs>\n ? IsUnion<keyof Outputs> extends true\n ? { [K in keyof Outputs]: { type: K; data: Outputs[K] } }[keyof Outputs]\n : Outputs[keyof Outputs]\n : never;\n\n/** The validated query a handler receives. */\nexport type ValidQuery<Q> = Q extends GiriInputSchema<infer Output> ? Output : never;\n\n/** Drop keys whose value resolved to `never` (an input the route didn't declare). */\ntype PruneNever<T> = { [K in keyof T as [T[K]] extends [never] ? never : K]: T[K] };\n\n/**\n * Derive a route's `ValidatedInput` from a module's `body`/`query` exports. The generated\n * per-method `$types` handle (`POST`, `GET`, …) uses this so handlers infer `c.req.valid`\n * with no manual generic.\n */\nexport type RouteInputOf<M> = PruneNever<{\n body: M extends { body: infer B } ? ValidBody<B> : never;\n query: M extends { query: infer Q } ? ValidQuery<Q> : never;\n}>;\n\nexport interface RouteInput {\n body?: GiriBodySchema;\n query?: GiriInputSchema;\n}\n\nexport interface RouteOpenApi {\n /** Omit this route from the generated `openapi.json` (it still serves normally). */\n hidden?: boolean;\n // Room to grow: summary, description, tags, deprecated, operationId, …\n}\n\nexport type RouteOpenApiConfig = RouteOpenApi | boolean;\n\nexport interface GiriRouteRegistration {\n method: HttpMethod;\n path: string;\n handle: Handle;\n middleware: Middleware[];\n input?: RouteInput;\n /** App-wide services to seed onto `c.app` (same instance for every route). */\n services?: Services;\n /** Secret for signing/verifying cookies (`c.signedCookie`), from `config.cookieSecret`. */\n cookieSecret?: string;\n}\n\nexport type GiriFetchHandler = (req: Request) => Response | Promise<Response>;\n\nexport interface GiriServeOptions {\n port: number;\n hostname?: string;\n}\n\nexport interface GiriServerInfo {\n address: string;\n port: number;\n}\n\nexport interface GiriServer {\n close(): void | Promise<void>;\n}\n\nexport interface GiriAdapter<App> {\n name?: string;\n createApp(): App;\n register(app: App, route: GiriRouteRegistration): void;\n fetch(app: App, req: Request): Promise<Response>;\n /**\n * Bind the configured backend's runtime to a port and start serving.\n * giri core stays runtime-agnostic: it hands the adapter a request handler\n * (so hot-reload keeps working) and the adapter owns the actual server.\n */\n serve(\n handler: GiriFetchHandler,\n options: GiriServeOptions,\n onListen?: (info: GiriServerInfo) => void,\n ): GiriServer;\n}\n\nexport interface GiriConfig<App = unknown> {\n adapter: GiriAdapter<App>;\n alias?: Record<string, string | string[]>;\n outDir?: string;\n server?: {\n port?: number;\n hostname?: string;\n };\n errorSchema?: unknown;\n /** Secret used to sign/verify cookies via `c.signedCookie` / `c.req.signedCookie`. */\n cookieSecret?: string;\n}\n\nexport interface GiriPaths {\n cwd: string;\n routesDir: string;\n outDir: string;\n}\n","import {\n type CookieJar,\n type CookieJarFactory,\n type Services,\n type Context,\n type HandlerResponse,\n type Middleware,\n type MiddlewareOptions,\n type ResponseFormat,\n type StatusCode,\n type TypedResponse,\n type ValidatedInput,\n nativeContextBrand,\n typedResponseBrand,\n} from './types';\n\nconst BODYLESS_STATUS = new Set([101, 103, 204, 205, 304]);\n\n/** Imperative response state set via `c.header()` / `c.status()`, merged in `toResponse`. */\ninterface PendingResponse {\n headers: Headers;\n status?: StatusCode;\n}\n\nconst pendingResponseBrand: unique symbol = Symbol('giri.pending-response');\n\nfunction getPending(context: Context): PendingResponse | undefined {\n return (context as unknown as Record<symbol, PendingResponse | undefined>)[pendingResponseBrand];\n}\n\n/** Used when the active adapter provides no cookie jar; every cookie call throws. */\nconst unsupportedCookieJar: CookieJar = {\n get: cookiesUnsupported,\n all: cookiesUnsupported,\n set: cookiesUnsupported,\n delete: cookiesUnsupported,\n getSigned: cookiesUnsupported,\n setSigned: cookiesUnsupported,\n};\n\nfunction cookiesUnsupported(): never {\n throw new Error('The active adapter does not support cookies.');\n}\n\nexport interface CreateContextOptions<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n> {\n request: Request;\n params?: Params;\n validated?: Input;\n app?: Services;\n /** The adapter's native per-request context, stashed for backend-specific bridges. */\n native?: unknown;\n /** Secret for `c.signedCookie` / `c.req.signedCookie` (from `config.cookieSecret`). */\n cookieSecret?: string;\n /** The adapter's cookie jar, built from its runtime's native helpers. */\n cookies?: CookieJarFactory;\n}\n\nexport function createTypedResponse<\n T,\n S extends StatusCode,\n F extends ResponseFormat,\n>(data: T, status: S, format: F, headers?: HeadersInit): TypedResponse<T, S, F> {\n return {\n [typedResponseBrand]: { data, status, format },\n data,\n status,\n format,\n headers,\n };\n}\n\nexport function isTypedResponse(value: unknown): value is TypedResponse<unknown> {\n return Boolean(value && typeof value === 'object' && typedResponseBrand in value);\n}\n\nexport function createContext<\n Params extends Record<string, string> = Record<string, string>,\n Input extends ValidatedInput = ValidatedInput,\n>(options: CreateContextOptions<Params, Input>): Context<Params, Input> {\n const url = new URL(options.request.url);\n const store = new Map<string, unknown>();\n const validated = options.validated ?? ({} as Input);\n\n // Headers/status set imperatively via c.header()/c.status(); merged into the final response.\n const pending: PendingResponse = { headers: new Headers() };\n const defaultStatus = (): StatusCode => pending.status ?? 200;\n\n // Cookies are a runtime concern: the adapter owns reading/encoding/signing via its native\n // helpers. Core only hands it the sink (request in, Set-Cookie onto the pending response).\n // No adapter jar => cookies aren't supported, and using them throws.\n const cookies: CookieJar = options.cookies\n ? options.cookies({\n request: options.request,\n append: (header) => pending.headers.append('set-cookie', header),\n secret: options.cookieSecret,\n })\n : unsupportedCookieJar;\n\n const context: Context<Params, Input> = {\n params: options.params ?? ({} as Params),\n app: options.app ?? ({} as Services),\n req: {\n raw: options.request,\n url,\n method: options.request.method,\n header: (name) => options.request.headers.get(name),\n json: <T = unknown>() => options.request.json() as Promise<T>,\n text: () => options.request.text(),\n arrayBuffer: () => options.request.arrayBuffer(),\n formData: () => options.request.formData(),\n valid: (key) => {\n if (!(key in validated)) {\n throw new Error(`No validated ${String(key)} data is available for this route.`);\n }\n return validated[key];\n },\n cookie: (name) => cookies.get(name),\n cookies: () => cookies.all(),\n signedCookie: (name) => cookies.getSigned(name),\n },\n set: (key: string, value: unknown) => {\n store.set(key, value);\n },\n get: (key: string) => store.get(key) as never,\n json: (data, status, headers) =>\n createTypedResponse(data, (status ?? defaultStatus()) as never, 'json', headers),\n text: (text, status, headers) =>\n createTypedResponse(text, (status ?? defaultStatus()) as never, 'text', headers),\n html: (html, status, headers) =>\n createTypedResponse(html, (status ?? defaultStatus()) as never, 'html', headers),\n body: (data, status, headers) =>\n new Response(data, { status: status ?? defaultStatus(), headers }),\n newResponse: (data, status, headers) =>\n new Response(data, { status: status ?? defaultStatus(), headers }),\n redirect: (location, status) =>\n new Response(null, { status: status ?? 302, headers: { Location: location } }),\n notFound: () => new Response('404 Not Found', { status: 404 }),\n header: (name, value, options) => {\n if (value === undefined) {\n pending.headers.delete(name);\n } else if (options?.append) {\n pending.headers.append(name, value);\n } else {\n pending.headers.set(name, value);\n }\n },\n status: (code) => {\n pending.status = code;\n },\n cookie: (name, value, options) => {\n if (value === null) {\n cookies.delete(name, options);\n } else {\n cookies.set(name, value, options);\n }\n },\n signedCookie: (name, value, options) => cookies.setSigned(name, value, options),\n };\n\n (context as unknown as Record<symbol, unknown>)[nativeContextBrand] = options.native;\n (context as unknown as Record<symbol, unknown>)[pendingResponseBrand] = pending;\n\n return context;\n}\n\nexport function typedResponseToResponse(response: TypedResponse<unknown>): Response {\n const headers = new Headers(response.headers);\n\n if (response.format === 'json' && !headers.has('content-type')) {\n headers.set('content-type', 'application/json; charset=utf-8');\n }\n\n if (response.format === 'text' && !headers.has('content-type')) {\n headers.set('content-type', 'text/plain; charset=utf-8');\n }\n\n if (response.format === 'html' && !headers.has('content-type')) {\n headers.set('content-type', 'text/html; charset=utf-8');\n }\n\n const body = BODYLESS_STATUS.has(response.status)\n ? null\n : response.format === 'json'\n ? JSON.stringify(response.data)\n : String(response.data);\n\n return new Response(body, {\n status: response.status,\n headers,\n });\n}\n\n/**\n * Convert a handler's return value to a real `Response`, then merge any headers set imperatively via\n * `c.header()` (the response's own headers win; pending ones fill the gaps). Pass the `context` so\n * those imperative headers are applied; without it the response is returned unchanged.\n */\nexport function toResponse(response: HandlerResponse, context?: Context): Response {\n const base = isTypedResponse(response) ? typedResponseToResponse(response) : response;\n const pending = context ? getPending(context) : undefined;\n if (!pending) {\n return base;\n }\n\n let hasPending = false;\n pending.headers.forEach(() => {\n hasPending = true;\n });\n if (!hasPending) {\n return base;\n }\n\n const headers = new Headers(base.headers);\n pending.headers.forEach((value, key) => {\n // Set-Cookie is multi-valued; forEach coalesces it into one comma-joined string, so\n // handle it separately below to keep each cookie its own header.\n if (key === 'set-cookie') {\n return;\n }\n if (!headers.has(key)) {\n headers.set(key, value);\n }\n });\n for (const cookie of pending.headers.getSetCookie?.() ?? []) {\n headers.append('set-cookie', cookie);\n }\n return new Response(base.body, { status: base.status, statusText: base.statusText, headers });\n}\n\nexport async function composeMiddleware(\n middleware: Middleware[],\n handle: (c: Context) => HandlerResponse | Promise<HandlerResponse>,\n context: Context,\n): Promise<HandlerResponse> {\n let index = -1;\n let result: HandlerResponse | undefined;\n\n const dispatch = async (i: number): Promise<HandlerResponse | void> => {\n if (i <= index) {\n throw new Error('next() called multiple times in giri middleware.');\n }\n index = i;\n\n if (i === middleware.length) {\n result = await handle(context);\n return result;\n }\n\n const returned = await middleware[i](context, () => dispatch(i + 1));\n if (returned !== undefined) {\n result = returned;\n return returned;\n }\n return result;\n };\n\n await dispatch(0);\n\n if (result === undefined) {\n throw new Error('Route completed without returning a response.');\n }\n\n return result;\n}\n\ntype AnyMiddleware<Vars extends Record<string, unknown>> = Middleware<\n Record<string, string>,\n ValidatedInput,\n Vars\n>;\n\nexport function defineMiddleware<Vars extends Record<string, unknown> = {}>(\n middleware: AnyMiddleware<Vars>,\n): AnyMiddleware<Vars>;\nexport function defineMiddleware<Vars extends Record<string, unknown> = {}>(\n options: MiddlewareOptions,\n middleware: AnyMiddleware<Vars>,\n): AnyMiddleware<Vars>;\nexport function defineMiddleware(\n optionsOrMiddleware: MiddlewareOptions | Middleware,\n maybeMiddleware?: Middleware,\n): Middleware {\n if (typeof optionsOrMiddleware === 'function') {\n return optionsOrMiddleware;\n }\n\n if (!maybeMiddleware) {\n throw new Error('defineMiddleware(options, middleware) requires a middleware function.');\n }\n\n maybeMiddleware.openapi = optionsOrMiddleware.openapi;\n return maybeMiddleware;\n}\n\n/**\n * Group middleware into an ordered stack, preserving each element's type as a tuple so\n * the injected context vars (`defineMiddleware<Vars>` / `Middleware<…, Vars>`) propagate\n * to downstream handlers. Use it for `+shared.ts` and verb `middleware` exports:\n * `export const middleware = stack(auth, requireAdmin)`.\n */\n// `Vars` is contravariant (it sits in the `c` parameter), so the constraint must leave it\n// open (`any`) otherwise a middleware that injects vars isn't assignable to the element type.\nexport function stack<T extends Middleware<Record<string, string>, ValidatedInput, any>[]>(...middleware: T): T {\n return middleware;\n}\n","/**\n * A tiny, dependency-free logger styled after Vite's dev output:\n * `HH:MM:SS [giri] (scope) message`. Colors auto-disable for non-TTY output, `NO_COLOR`,\n * or `TERM=dumb`, so piped/CI logs stay clean.\n */\n\nconst noColor =\n !process.stdout.isTTY ||\n process.env.NO_COLOR !== undefined ||\n process.env.TERM === 'dumb' ||\n process.env.FORCE_COLOR === '0';\n\nfunction paint(open: number, close: number): (text: string) => string {\n return (text) => (noColor ? text : `\\x1b[${open}m${text}\\x1b[${close}m`);\n}\n\nexport const color = {\n dim: paint(2, 22),\n bold: paint(1, 22),\n red: paint(31, 39),\n green: paint(32, 39),\n yellow: paint(33, 39),\n blue: paint(34, 39),\n magenta: paint(35, 39),\n cyan: paint(36, 39),\n gray: paint(90, 39),\n};\n\n/** Green for paths/values, like Vite highlights updated files. */\nexport const highlight = (text: string): string => color.green(text);\n/** Dim for secondary details (counts, durations). */\nexport const muted = (text: string): string => color.dim(text);\n\nfunction timestamp(): string {\n const now = new Date();\n const pad = (n: number): string => String(n).padStart(2, '0');\n return `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;\n}\n\nconst TAG = 'giri';\n\nfunction line(tag: string, message: string, scope?: string): string {\n const parts = [color.gray(timestamp()), tag];\n if (scope) {\n parts.push(color.dim(`(${scope})`));\n }\n parts.push(message);\n return parts.join(' ');\n}\n\nconst tag = {\n info: color.bold(color.cyan(`[${TAG}]`)),\n warn: color.bold(color.yellow(`[${TAG}]`)),\n error: color.bold(color.red(`[${TAG}]`)),\n};\n\nexport const log = {\n info(message: string, scope?: string): void {\n console.log(line(tag.info, message, scope));\n },\n success(message: string, scope?: string): void {\n console.log(line(tag.info, color.green(message), scope));\n },\n warn(message: string, scope?: string): void {\n console.warn(line(tag.warn, color.yellow(message), scope));\n },\n error(message: string, scope?: string): void {\n console.error(line(tag.error, color.red(message), scope));\n },\n ready(url: string): void {\n console.log(line(tag.info, `${color.green('ready')} on ${color.cyan(url)}`));\n },\n change(verb: string, path: string, count?: number): void {\n const suffix = count && count > 1 ? ` ${color.dim(`(x${count})`)}` : '';\n console.log(line(tag.info, `${color.green(verb)} ${highlight(path)}${suffix}`, 'watch'));\n },\n};\n","import {\n type BodyContentType,\n type GiriBodySchema,\n type GiriInputSchema,\n type InputValidationResult,\n type RouteInput,\n type TypedResponse,\n type ValidatedInput,\n bodySchemaBrand,\n inputSchemaBrand,\n} from './types';\nimport { createTypedResponse } from './context';\n\ninterface PreparedInput {\n ok: true;\n validated: ValidatedInput;\n}\n\ninterface FailedInput {\n ok: false;\n response: TypedResponse<{ message: string; issues: unknown }, 400 | 415, 'json'>;\n}\n\nexport type PreparedRequestInput = PreparedInput | FailedInput;\n\n/**\n * Build a giri input schema from a `validate` + `toJsonSchema` pair. Vendor adapters use\n * this; you can call it directly to make a custom validator. The brand is a global symbol,\n * so a hand-rolled `{ [Symbol.for(\"giri.input-schema\")]: true, validate, toJsonSchema }` works too.\n */\nexport function defineInputSchema<Output>(\n schema: Omit<GiriInputSchema<Output>, typeof inputSchemaBrand>,\n): GiriInputSchema<Output> {\n return { [inputSchemaBrand]: true, ...schema };\n}\n\nexport function isGiriInputSchema(value: unknown): value is GiriInputSchema {\n return Boolean(\n value &&\n typeof value === 'object' &&\n (value as Record<symbol, unknown>)[inputSchemaBrand] === true,\n );\n}\n\n/**\n * Build a giri body schema from per-content-type input schemas. Validator adapters use this `zod.body({ json, form })`\n */\nexport function defineBodySchema<Outputs extends Partial<Record<BodyContentType, unknown>>>(\n contents: GiriBodySchema<Outputs>['contents'],\n): GiriBodySchema<Outputs> {\n return { [bodySchemaBrand]: true, contents };\n}\n\nexport function isGiriBodySchema(value: unknown): value is GiriBodySchema {\n return Boolean(\n value &&\n typeof value === 'object' &&\n (value as Record<symbol, unknown>)[bodySchemaBrand] === true,\n );\n}\n\nconst MIME_TO_CONTENT_TYPE: Record<string, BodyContentType> = {\n 'application/json': 'json',\n 'multipart/form-data': 'form',\n 'application/x-www-form-urlencoded': 'urlencoded',\n 'text/plain': 'text',\n};\n\nfunction contentTypeFromHeader(header: string | null): BodyContentType | undefined {\n if (!header) {\n return undefined;\n }\n const mime = header.split(';', 1)[0].trim().toLowerCase();\n return MIME_TO_CONTENT_TYPE[mime];\n}\n\n/** Flatten a `FormData` into a plain object, collapsing repeated fields into arrays. */\nfunction formDataObject(form: FormData): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n form.forEach((value, key) => {\n const current = result[key];\n if (current === undefined) {\n result[key] = value;\n } else if (Array.isArray(current)) {\n current.push(value);\n } else {\n result[key] = [current, value];\n }\n });\n return result;\n}\n\nasync function readRawBody(request: Request, contentType: BodyContentType): Promise<unknown> {\n const cloned = request.clone();\n if (contentType === 'json') {\n return cloned.json();\n }\n if (contentType === 'text') {\n return cloned.text();\n }\n return formDataObject(await cloned.formData());\n}\n\nfunction queryObject(url: URL): Record<string, string | string[]> {\n const result: Record<string, string | string[]> = {};\n for (const [key, value] of url.searchParams) {\n const current = result[key];\n if (current === undefined) {\n result[key] = value;\n } else if (Array.isArray(current)) {\n current.push(value);\n } else {\n result[key] = [current, value];\n }\n }\n return result;\n}\n\nasync function runValidation(\n schema: GiriInputSchema,\n value: unknown,\n label: string,\n): Promise<InputValidationResult> {\n if (!isGiriInputSchema(schema)) {\n throw new Error(\n `giri: ${label} schema must be wrapped with a validator, e.g. \\`export const ${label} = zod(...)\\` from @boon4681/giri/validators/zod.`,\n );\n }\n return schema.validate(value);\n}\n\nexport async function prepareRequestInput(request: Request, input?: RouteInput): Promise<PreparedRequestInput> {\n const validated: ValidatedInput = {};\n\n if (input?.query) {\n const query = queryObject(new URL(request.url));\n const result = await runValidation(input.query, query, 'query');\n if (!result.ok) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Invalid query parameters.', issues: result.issues },\n 400,\n 'json',\n ),\n };\n }\n validated.query = result.value;\n }\n\n if (input?.body) {\n const contents = input.body.contents as Record<BodyContentType, GiriInputSchema>;\n const declared = Object.keys(contents) as BodyContentType[];\n const requested = contentTypeFromHeader(request.headers.get('content-type'));\n // Pick the schema matching the request's content-type; fall back to JSON when the\n // header is missing/unrecognized but JSON is on offer (so header-less posts still work).\n const chosen: BodyContentType | undefined =\n requested && contents[requested] ? requested : contents.json ? 'json' : undefined;\n\n if (!chosen) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Unsupported media type.', issues: { accepted: declared } },\n 415,\n 'json',\n ),\n };\n }\n\n let rawBody: unknown;\n try {\n rawBody = await readRawBody(request, chosen);\n } catch (error) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Invalid request body.', issues: error },\n 400,\n 'json',\n ),\n };\n }\n\n const result = await runValidation(contents[chosen], rawBody, 'body');\n if (!result.ok) {\n return {\n ok: false,\n response: createTypedResponse(\n { message: 'Invalid request body.', issues: result.issues },\n 400,\n 'json',\n ),\n };\n }\n\n validated.body = declared.length > 1 ? { type: chosen, data: result.value } : result.value;\n }\n\n return { ok: true, validated };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAmC;AACnC,kBAAqB;AAErB,oBAA+D;;;ACGxD,IAAM,qBAAoC,uBAAO,IAAI,qBAAqB;AAC1E,IAAM,qBAAoC,uBAAO,IAAI,qBAAqB;AA6O1E,IAAM,mBAAkC,uBAAO,IAAI,mBAAmB;;;ACpO7E,IAAM,kBAAkB,oBAAI,IAAI,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG,CAAC;AAQzD,IAAM,uBAAsC,uBAAO,uBAAuB;AAE1E,SAAS,WAAW,SAA+C;AAC/D,SAAQ,QAAmE,oBAAoB;AACnG;AAGA,IAAM,uBAAkC;AAAA,EACpC,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,WAAW;AAAA,EACX,WAAW;AACf;AAEA,SAAS,qBAA4B;AACjC,QAAM,IAAI,MAAM,8CAA8C;AAClE;AAkBO,SAAS,oBAId,MAAS,QAAW,QAAW,SAA+C;AAC5E,SAAO;AAAA,IACH,CAAC,kBAAkB,GAAG,EAAE,MAAM,QAAQ,OAAO;AAAA,IAC7C;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACJ;AACJ;AAEO,SAAS,gBAAgB,OA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Aa,aAAa,CAAC;AAC3B,WAAO,oBAAoB,WAAW,WAAW,YAAY;AAAA,EACjE;AACJ;AAEA,SAAS,kBAAkB,KAAW,OAAoC;AAGtE,QAAM,UAAuB,CAAC,MAAM,aAAa,GAAG,KAAK;AACzD,QAAM,SAAS,MAAM,OAAO,YAAY;AACxC,QAAM,aAAa;AAEnB,MAAI,UAAU,OAAO,OAAO,WAAW,MAAM,MAAM,YAAY;AAC3D,eAAW,MAAM,EAAE,MAAM,MAAM,OAAO;AACtC;AAAA,EACJ;AAEA,QAAM,IAAI,MAAM,iCAAiC,MAAM,MAAM,GAAG;AACpE;AAEO,SAAS,OAAiC;AAC7C,SAAO;AAAA,IACH,MAAM;AAAA,IACN,WAAW,MAAM,IAAI,iBAAK,EAAE,QAAQ,MAAM,CAAC;AAAA,IAC3C,UAAU;AAAA,IACV,OAAO,OAAO,KAAK,QAAQ,IAAI,MAAM,GAAG;AAAA,IACxC,OAAO,CAAC,SAAS,SAAS,aAAa;AACnC,YAAM,aAAS,mBAAAC;AAAA,QACX;AAAA,UACI,OAAO;AAAA,UACP,MAAM,QAAQ;AAAA,UACd,UAAU,QAAQ;AAAA,QACtB;AAAA,QACA,WAAW,CAAC,SAAS,SAAS,EAAE,SAAS,KAAK,SAAS,MAAM,KAAK,KAAK,CAAC,IAAI;AAAA,MAChF;AAEA,aAAO;AAAA,QACH,OAAO,MAAM;AACT,iBAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC1C,mBAAO,MAAM,CAAC,UAAW,QAAQ,OAAO,KAAK,IAAI,QAAQ,CAAE;AAAA,UAC/D,CAAC;AAAA,QACL;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;","names":["options","tag","serveNode"]}
package/dist/cli.js CHANGED
@@ -729,20 +729,13 @@ function resolveAliasTarget(cwd, target, capture = "") {
729
729
  }
730
730
  function matchAlias(request, key) {
731
731
  if (key.includes("*")) {
732
- const [prefix2, suffix = ""] = key.split("*");
733
- if (request.startsWith(prefix2) && request.endsWith(suffix)) {
734
- return request.slice(prefix2.length, request.length - suffix.length);
732
+ const [prefix, suffix = ""] = key.split("*");
733
+ if (request.startsWith(prefix) && request.endsWith(suffix)) {
734
+ return request.slice(prefix.length, request.length - suffix.length);
735
735
  }
736
736
  return void 0;
737
737
  }
738
- if (request === key) {
739
- return "";
740
- }
741
- const prefix = `${key}/`;
742
- if (request.startsWith(prefix)) {
743
- return request.slice(prefix.length);
744
- }
745
- return void 0;
738
+ return request === key ? "" : void 0;
746
739
  }
747
740
  function resolveAliasRequest(request, alias, cwd) {
748
741
  for (const [key, value] of Object.entries(alias ?? {})) {
@@ -1639,6 +1632,9 @@ function createWatchUpdater(config, initial) {
1639
1632
  if (!(0, import_node_fs7.existsSync)(abs)) {
1640
1633
  return fullResync();
1641
1634
  }
1635
+ if ((0, import_node_fs7.statSync)(abs).isDirectory()) {
1636
+ return "skip";
1637
+ }
1642
1638
  const graph = buildModuleGraph(paths.cwd);
1643
1639
  const isRoute = routes.some((candidate) => slash(candidate.file) === file);
1644
1640
  if (!graph.nodes.has(file) && !isRoute) {
@@ -2044,6 +2040,7 @@ function displayHost(address) {
2044
2040
  return address.includes(":") ? `[${address}]` : address;
2045
2041
  }
2046
2042
  async function serveProject(config, flags) {
2043
+ Error.stackTraceLimit = 30;
2047
2044
  const { watch } = await import("fs");
2048
2045
  let stop;
2049
2046
  const boot = async (cfg) => {
@@ -2081,12 +2078,22 @@ async function serveProject(config, flags) {
2081
2078
  while (changed.size > 0) {
2082
2079
  const batch = [...changed];
2083
2080
  changed.clear();
2081
+ let dirty = false;
2084
2082
  for (const name of batch) {
2085
2083
  const outcome = await updater.apply(name || null);
2084
+ if (outcome === "skip") {
2085
+ continue;
2086
+ }
2087
+ dirty = true;
2086
2088
  const rel = name ? `src/${name.replace(/\\/g, "/")}` : "src";
2087
2089
  log2.change(outcome === "full" ? "sync" : "update", rel, bump(rel));
2090
+ if (outcome === "full") {
2091
+ break;
2092
+ }
2093
+ }
2094
+ if (dirty) {
2095
+ current = await buildGiriApp(cfg, { services });
2088
2096
  }
2089
- current = await buildGiriApp(cfg, { services });
2090
2097
  }
2091
2098
  } catch (error) {
2092
2099
  log2.error(error instanceof Error ? error.message : String(error), "watch");