@authhero/proxy 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/README.md +252 -0
  2. package/dist/proxy.cjs +1 -0
  3. package/dist/proxy.d.ts +236 -0
  4. package/dist/proxy.mjs +3082 -0
  5. package/dist/tsconfig.types.tsbuildinfo +1 -0
  6. package/dist/types/adapter.d.ts +20 -0
  7. package/dist/types/app.d.ts +12 -0
  8. package/dist/types/data-plane/cache-api-cache.d.ts +20 -0
  9. package/dist/types/data-plane/cache.d.ts +13 -0
  10. package/dist/types/data-plane/compile.d.ts +9 -0
  11. package/dist/types/data-plane/handlers/basic-auth.d.ts +5 -0
  12. package/dist/types/data-plane/handlers/cache.d.ts +3 -0
  13. package/dist/types/data-plane/handlers/cors.d.ts +8 -0
  14. package/dist/types/data-plane/handlers/forwarded-headers.d.ts +5 -0
  15. package/dist/types/data-plane/handlers/headers.d.ts +6 -0
  16. package/dist/types/data-plane/handlers/http.d.ts +17 -0
  17. package/dist/types/data-plane/handlers/index.d.ts +13 -0
  18. package/dist/types/data-plane/handlers/redirect.d.ts +6 -0
  19. package/dist/types/data-plane/handlers/rewrite-cookies.d.ts +3 -0
  20. package/dist/types/data-plane/handlers/rewrite-location.d.ts +3 -0
  21. package/dist/types/data-plane/handlers/service-binding.d.ts +5 -0
  22. package/dist/types/data-plane/handlers/static.d.ts +6 -0
  23. package/dist/types/data-plane/handlers/util.d.ts +4 -0
  24. package/dist/types/data-plane/matcher.d.ts +15 -0
  25. package/dist/types/data-plane/registry.d.ts +19 -0
  26. package/dist/types/data-plane/router.d.ts +15 -0
  27. package/dist/types/http-adapter/index.d.ts +22 -0
  28. package/dist/types/index.d.ts +19 -0
  29. package/dist/types/static/index.d.ts +30 -0
  30. package/dist/types/types.d.ts +2 -0
  31. package/package.json +3 -6
@@ -0,0 +1,236 @@
1
+ import { ProxyRoute, ProxyRoutesAdapter, RouteMatch, HandlerConfig } from '@authhero/adapter-interfaces';
2
+ export { HandlerConfig, ListProxyRoutesParams, ListProxyRoutesResult, ProxyRoute, ProxyRouteInsert, ProxyRouteUpdate, ProxyRoutesAdapter, RouteMatch, handlerConfigSchema, matchSchema, proxyRouteInsertSchema, proxyRouteSchema, proxyRouteUpdateSchema } from '@authhero/adapter-interfaces';
3
+ import { MiddlewareHandler, Hono, Context } from 'hono';
4
+ import { z } from '@hono/zod-openapi';
5
+
6
+ interface ResolvedHost {
7
+ tenant_id: string;
8
+ custom_domain_id: string;
9
+ domain: string;
10
+ routes: ProxyRoute[];
11
+ }
12
+ /**
13
+ * Data adapter consumed by the proxy data plane.
14
+ *
15
+ * `proxyRoutes` mirrors the tenant-scoped CRUD adapter that AuthHero uses for
16
+ * its management API. `resolveHost` is the cross-tenant lookup the proxy data
17
+ * plane needs on every incoming request — it maps a request `Host` header to
18
+ * the owning tenant, custom domain, and route set.
19
+ */
20
+ interface ProxyDataAdapter {
21
+ proxyRoutes: ProxyRoutesAdapter;
22
+ resolveHost(host: string): Promise<ResolvedHost | null>;
23
+ }
24
+
25
+ interface HostResolverCache {
26
+ resolveHost(host: string): Promise<ResolvedHost | null>;
27
+ }
28
+ interface HostCacheOptions {
29
+ freshTtlMs: number;
30
+ staleTtlMs?: number;
31
+ negativeTtlMs?: number;
32
+ maxEntries?: number;
33
+ waitUntil?: (promise: Promise<unknown>) => void;
34
+ }
35
+ declare function createInMemoryHostCache(data: ProxyDataAdapter, ttlMs: number, maxEntries?: number): HostResolverCache;
36
+ declare function createInMemoryHostCache(data: ProxyDataAdapter, options: HostCacheOptions): HostResolverCache;
37
+
38
+ interface HandlerBuildContext {
39
+ bindings: Record<string, unknown>;
40
+ }
41
+ interface HandlerDefinition<TOptions = unknown> {
42
+ type: string;
43
+ optionsSchema: z.ZodType<TOptions>;
44
+ build(options: TOptions, ctx: HandlerBuildContext): MiddlewareHandler;
45
+ }
46
+ declare class HandlerRegistry {
47
+ private definitions;
48
+ private bindings;
49
+ constructor(bindings?: Record<string, unknown>);
50
+ add<TOptions>(def: HandlerDefinition<TOptions>): this;
51
+ has(type: string): boolean;
52
+ build(type: string, rawOptions: unknown): MiddlewareHandler;
53
+ }
54
+ declare function defineHandler<TOptions>(def: HandlerDefinition<TOptions>): HandlerDefinition<TOptions>;
55
+
56
+ interface ProxyAppOptions {
57
+ data: ProxyDataAdapter;
58
+ cacheTtlMs?: number;
59
+ cache?: HostCacheOptions;
60
+ registry?: HandlerRegistry;
61
+ bindings?: Record<string, unknown>;
62
+ }
63
+ declare function createProxyApp(options: ProxyAppOptions): Hono;
64
+
65
+ interface ProxyDataPlaneOptions {
66
+ data: ProxyDataAdapter;
67
+ cacheTtlMs?: number;
68
+ cache?: HostCacheOptions;
69
+ resolver?: HostResolverCache;
70
+ registry?: HandlerRegistry;
71
+ bindings?: Record<string, unknown>;
72
+ }
73
+ declare function createProxyDataPlaneHandler(options: ProxyDataPlaneOptions): (c: Context) => Promise<Response>;
74
+ declare function createProxyDataPlaneRouter(options: ProxyDataPlaneOptions): Hono;
75
+
76
+ interface CacheApiHostCacheOptions {
77
+ upstream: HostResolverCache;
78
+ cacheTtlSeconds?: number;
79
+ negativeCacheTtlSeconds?: number;
80
+ namespace?: string;
81
+ cache?: Cache;
82
+ waitUntil?: (promise: Promise<unknown>) => void;
83
+ }
84
+ /**
85
+ * Wraps an upstream `HostResolverCache` with a Cloudflare Cache-API layer.
86
+ * On cache miss, the upstream is queried and the result written back into
87
+ * the Cache API for subsequent reads in the same colo.
88
+ *
89
+ * Invalidation is the caller's responsibility — typically the control plane
90
+ * issues `cache.delete(syntheticKey(...))` on every proxy_route mutation,
91
+ * and each colo's cache expires on its own TTL otherwise.
92
+ */
93
+ declare function createCacheApiHostCache(options: CacheApiHostCacheOptions): HostResolverCache;
94
+ declare function buildCacheApiKey(namespace: string, host: string): Request;
95
+
96
+ declare const corsHandler: HandlerDefinition<{
97
+ origins?: string[] | undefined;
98
+ allow_credentials?: boolean | undefined;
99
+ allow_headers?: string[] | undefined;
100
+ allow_methods?: string[] | undefined;
101
+ expose_headers?: string[] | undefined;
102
+ max_age?: number | undefined;
103
+ }>;
104
+
105
+ declare const basicAuthHandler: HandlerDefinition<{
106
+ username: string;
107
+ password: string;
108
+ realm?: string | undefined;
109
+ }>;
110
+
111
+ declare const headersHandler: HandlerDefinition<{
112
+ request?: Record<string, string> | undefined;
113
+ response?: Record<string, string> | undefined;
114
+ remove_request?: string[] | undefined;
115
+ remove_response?: string[] | undefined;
116
+ }>;
117
+
118
+ declare const cacheHandler: HandlerDefinition<{
119
+ ttl_seconds: number;
120
+ }>;
121
+
122
+ declare const forwardedHeadersHandler: HandlerDefinition<{
123
+ client_ip_header: string;
124
+ set_x_real_ip: boolean;
125
+ set_x_original_url: boolean;
126
+ }>;
127
+
128
+ declare const rewriteCookiesHandler: HandlerDefinition<{
129
+ upstream_host?: string | undefined;
130
+ }>;
131
+
132
+ declare const rewriteLocationHandler: HandlerDefinition<{
133
+ upstream_origin?: string | undefined;
134
+ }>;
135
+
136
+ declare const httpHandler: HandlerDefinition<{
137
+ upstream_url: string;
138
+ preserve_host: boolean;
139
+ timeout_ms?: number | undefined;
140
+ }>;
141
+
142
+ declare const serviceBindingHandler: HandlerDefinition<{
143
+ binding: string;
144
+ preserve_host: boolean;
145
+ upstream_url?: string | undefined;
146
+ }>;
147
+
148
+ declare const redirectHandler: HandlerDefinition<{
149
+ upstream_url: string;
150
+ status: 301 | 302 | 307 | 308;
151
+ preserve_path: boolean;
152
+ preserve_query: boolean;
153
+ }>;
154
+
155
+ declare const staticHandler: HandlerDefinition<{
156
+ status: number;
157
+ headers?: Record<string, string> | undefined;
158
+ body?: string | undefined;
159
+ json?: unknown;
160
+ }>;
161
+
162
+ declare function registerBuiltinHandlers(registry: HandlerRegistry): void;
163
+
164
+ /**
165
+ * Build a Hono app whose route table mirrors the configured proxy routes for
166
+ * a single host. Compiled once per host (and cached alongside the resolved
167
+ * host) — see `cache.ts`.
168
+ */
169
+ declare function compileHostApp(routes: ProxyRoute[], registry: HandlerRegistry): Hono;
170
+
171
+ declare function sortRoutes(routes: ProxyRoute[]): ProxyRoute[];
172
+ /**
173
+ * Match a configured host pattern against a request's `Host`. Supports
174
+ * exact match and `*.example.com` wildcard prefix.
175
+ */
176
+ declare function matchesHost(pattern: string, host: string): boolean;
177
+ declare function matchesAnyHost(patterns: string[] | undefined, host: string): boolean;
178
+ /**
179
+ * Filter Hono middleware that 404s when the route's `match.headers` /
180
+ * `match.query` / `match.hosts` predicates don't hold. Path and method
181
+ * matching is done by Hono itself via `app.on(methods, path, ...)`.
182
+ */
183
+ declare function buildMatchFilter(match: RouteMatch): (c: Context, next: () => Promise<void>) => Promise<Response | undefined>;
184
+
185
+ interface StaticRouteInput {
186
+ id?: string;
187
+ priority?: number;
188
+ match: RouteMatch;
189
+ handlers: HandlerConfig[];
190
+ created_at?: string;
191
+ updated_at?: string;
192
+ }
193
+ interface StaticHostConfig {
194
+ tenant_id?: string;
195
+ custom_domain_id?: string;
196
+ routes: StaticRouteInput[];
197
+ }
198
+ interface StaticProxyAdapterOptions {
199
+ hosts: Record<string, StaticHostConfig | StaticRouteInput[]>;
200
+ defaultTenantId?: string;
201
+ }
202
+ declare function createStaticProxyAdapter(options: StaticProxyAdapterOptions): ProxyDataAdapter;
203
+ /**
204
+ * Sugar for building a single HTTP-forwarding route in static configs.
205
+ * Equivalent to `{ match: { path: "/*" }, handlers: [{ type: "http", options: { upstream_url, preserve_host } }] }`.
206
+ */
207
+ declare function httpRoute(upstream_url: string, options?: {
208
+ path?: string;
209
+ preserve_host?: boolean;
210
+ priority?: number;
211
+ }): StaticRouteInput;
212
+
213
+ interface HttpProxyAdapterOptions {
214
+ baseUrl: string;
215
+ clientId: string;
216
+ clientSecret: string;
217
+ audience?: string;
218
+ resolveHostPath?: string;
219
+ tokenRefreshSkewSeconds?: number;
220
+ timeoutMs?: number;
221
+ fetch?: typeof fetch;
222
+ }
223
+ /**
224
+ * Build a `ProxyDataAdapter` that reads from the AuthHero control plane over
225
+ * HTTP. Intended for a proxy deployment that does not share a database with
226
+ * the control plane.
227
+ *
228
+ * Authentication is a single `client_credentials` grant against the control
229
+ * plane. The token is cached in-memory and refreshed before expiry. The
230
+ * privileged `resolveHost` endpoint must be served by the control plane and
231
+ * requires the `proxy:resolve_host` scope.
232
+ */
233
+ declare function createHttpProxyAdapter(options: HttpProxyAdapterOptions): ProxyDataAdapter;
234
+
235
+ export { HandlerRegistry, basicAuthHandler, buildCacheApiKey, buildMatchFilter, cacheHandler, compileHostApp, corsHandler, createCacheApiHostCache, createHttpProxyAdapter, createInMemoryHostCache, createProxyApp, createProxyDataPlaneHandler, createProxyDataPlaneRouter, createStaticProxyAdapter, defineHandler, forwardedHeadersHandler, headersHandler, httpHandler, httpRoute, matchesAnyHost, matchesHost, redirectHandler, registerBuiltinHandlers, rewriteCookiesHandler, rewriteLocationHandler, serviceBindingHandler, sortRoutes, staticHandler };
236
+ export type { CacheApiHostCacheOptions, HandlerBuildContext, HandlerDefinition, HostCacheOptions, HostResolverCache, HttpProxyAdapterOptions, ProxyAppOptions, ProxyDataAdapter, ProxyDataPlaneOptions, ResolvedHost, StaticHostConfig, StaticProxyAdapterOptions, StaticRouteInput };