@kelpie/server 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app.d.ts +11 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +27 -0
- package/dist/app.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/config.d.ts +8 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +5 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/errors.d.ts +1 -0
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/errors.js +3 -0
- package/dist/lib/errors.js.map +1 -1
- package/dist/lib/ids.d.ts +5 -0
- package/dist/lib/ids.d.ts.map +1 -1
- package/dist/lib/ids.js +5 -0
- package/dist/lib/ids.js.map +1 -1
- package/dist/lib/rateLimit.d.ts +26 -0
- package/dist/lib/rateLimit.d.ts.map +1 -0
- package/dist/lib/rateLimit.js +26 -0
- package/dist/lib/rateLimit.js.map +1 -0
- package/dist/lib/securityHeaders.d.ts +3 -0
- package/dist/lib/securityHeaders.d.ts.map +1 -0
- package/dist/lib/securityHeaders.js +41 -0
- package/dist/lib/securityHeaders.js.map +1 -0
- package/dist/modules/rate-limit/middleware.d.ts +42 -0
- package/dist/modules/rate-limit/middleware.d.ts.map +1 -0
- package/dist/modules/rate-limit/middleware.js +127 -0
- package/dist/modules/rate-limit/middleware.js.map +1 -0
- package/dist/modules/rate-limit/repository.d.ts +32 -0
- package/dist/modules/rate-limit/repository.d.ts.map +1 -0
- package/dist/modules/rate-limit/repository.js +48 -0
- package/dist/modules/rate-limit/repository.js.map +1 -0
- package/dist/modules/rate-limit/schema.d.ts +125 -0
- package/dist/modules/rate-limit/schema.d.ts.map +1 -0
- package/dist/modules/rate-limit/schema.js +29 -0
- package/dist/modules/rate-limit/schema.js.map +1 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +1 -0
- package/dist/schema/index.js.map +1 -1
- package/dist/testing/app.d.ts +5 -0
- package/dist/testing/app.d.ts.map +1 -1
- package/dist/testing/app.js +14 -0
- package/dist/testing/app.js.map +1 -1
- package/dist/webBundle.d.ts +37 -0
- package/dist/webBundle.d.ts.map +1 -0
- package/dist/webBundle.js +75 -0
- package/dist/webBundle.js.map +1 -0
- package/migrations/0016_misty_mentor.sql +11 -0
- package/migrations/meta/0016_snapshot.json +5154 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +3 -2
- package/src/app.ts +46 -0
- package/src/index.ts +4 -1
- package/src/lib/config.ts +13 -0
- package/src/lib/errors.ts +4 -0
- package/src/lib/ids.ts +5 -0
- package/src/lib/rateLimit.ts +45 -0
- package/src/lib/securityHeaders.ts +44 -0
- package/src/modules/rate-limit/middleware.ts +177 -0
- package/src/modules/rate-limit/repository.ts +64 -0
- package/src/modules/rate-limit/schema.ts +34 -0
- package/src/schema/index.ts +1 -0
- package/src/testing/app.ts +21 -0
- package/src/webBundle.ts +110 -0
package/src/webBundle.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { join, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { serveStatic } from '@hono/node-server/serve-static'
|
|
5
|
+
import type { Hono, MiddlewareHandler } from 'hono'
|
|
6
|
+
|
|
7
|
+
import type { AppBindings } from './app.ts'
|
|
8
|
+
import { MCP_ROUTE_PREFIX } from './modules/mcp/index.ts'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Serves a built web bundle from the same origin as the API.
|
|
12
|
+
*
|
|
13
|
+
* `createApp` answers data and nothing else. In development the Vite dev server
|
|
14
|
+
* builds the pages and proxies `/v1` through to the API, so one address serves
|
|
15
|
+
* both. That proxy is doing two jobs: rebuilding on edit, which is development
|
|
16
|
+
* only, and putting the pages and the API on one origin, which is permanent.
|
|
17
|
+
* Nothing did the second job in production, so a deployed assembly answered API
|
|
18
|
+
* calls and served no pages at all.
|
|
19
|
+
*
|
|
20
|
+
* An assembly opts in by calling this after `createApp`, which is where the
|
|
21
|
+
* three assemblies (`apps/kelpie`, a scaffolded project, and `kelpie-cloud`)
|
|
22
|
+
* would otherwise each grow their own copy of the fallback rule below.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export interface WebBundleOptions {
|
|
26
|
+
/** Directory holding the built `index.html` and its assets. */
|
|
27
|
+
readonly directory: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The bundle directory does not hold a build. Thrown at boot, never per request. */
|
|
31
|
+
export class WebBundleError extends Error {
|
|
32
|
+
constructor(message: string) {
|
|
33
|
+
super(message)
|
|
34
|
+
this.name = 'WebBundleError'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The prefixes `createApp` answers on.
|
|
40
|
+
*
|
|
41
|
+
* `/v1/public` needs no entry of its own: it sits under `/v1`. `MCP_ROUTE_PREFIX`
|
|
42
|
+
* is imported rather than written out, so moving the endpoint moves this with it.
|
|
43
|
+
*/
|
|
44
|
+
const API_PREFIXES: readonly string[] = ['/v1', MCP_ROUTE_PREFIX, '/healthz']
|
|
45
|
+
|
|
46
|
+
function isApiRequest(path: string): boolean {
|
|
47
|
+
return API_PREFIXES.some((prefix) => path === prefix || path.startsWith(`${prefix}/`))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Restricts a handler to requests for the web app.
|
|
52
|
+
*
|
|
53
|
+
* The API exclusion is the point of the whole file. `app.notFound` renders
|
|
54
|
+
* `api.md`'s JSON 404, and it only fires when no route matched, so a bare
|
|
55
|
+
* catch-all registered after `createApp` would answer `GET /v1/typo` with the
|
|
56
|
+
* app shell and a 200. A client asking for data would get a web page and no
|
|
57
|
+
* indication it had misspelled anything.
|
|
58
|
+
*
|
|
59
|
+
* Methods are filtered for the same reason: `POST /v1/typo` is a wrong endpoint,
|
|
60
|
+
* not a page request, and only a `GET` or a `HEAD` can sensibly be answered with
|
|
61
|
+
* a document.
|
|
62
|
+
*/
|
|
63
|
+
function webRequestsOnly(handler: MiddlewareHandler): MiddlewareHandler {
|
|
64
|
+
return async (context, next) => {
|
|
65
|
+
const isDocumentRequest = context.req.method === 'GET' || context.req.method === 'HEAD'
|
|
66
|
+
|
|
67
|
+
if (!isDocumentRequest || isApiRequest(context.req.path)) {
|
|
68
|
+
return next()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return handler(context, next)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Mounts the bundle on an app built by `createApp`.
|
|
77
|
+
*
|
|
78
|
+
* Call it after `createApp` and before serving. Hono composes a request's
|
|
79
|
+
* handlers in registration order and stops at the first one that answers, so
|
|
80
|
+
* mounting last leaves every API route matching ahead of these two.
|
|
81
|
+
*
|
|
82
|
+
* @throws WebBundleError if the directory holds no `index.html`. A deployment
|
|
83
|
+
* whose build did not run should stop at boot rather than serve an API with
|
|
84
|
+
* invisible pages, which is the failure this function exists to remove.
|
|
85
|
+
*/
|
|
86
|
+
export function serveWebBundle(app: Hono<AppBindings>, options: WebBundleOptions): void {
|
|
87
|
+
const directory = resolve(options.directory)
|
|
88
|
+
const indexHtml = join(directory, 'index.html')
|
|
89
|
+
|
|
90
|
+
if (!existsSync(indexHtml)) {
|
|
91
|
+
throw new WebBundleError(
|
|
92
|
+
`No index.html in ${directory}. Point WEB_BUNDLE_DIR at a built web bundle, or run the web build first.`,
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Two registrations rather than one: `serveStatic` calls `next()` when it
|
|
97
|
+
// finds no file, which is exactly the signal the fallback needs. A request
|
|
98
|
+
// for a real asset is answered by the first and never reaches the second.
|
|
99
|
+
app.use('*', webRequestsOnly(serveStatic({ root: directory })))
|
|
100
|
+
|
|
101
|
+
// The single-page fallback. The app decides what to draw from the address, so
|
|
102
|
+
// a deep link to `/people/per_01J…` has to return the same `index.html` even
|
|
103
|
+
// though no file sits at that path.
|
|
104
|
+
//
|
|
105
|
+
// A missing asset gets the shell too, which is what every static SPA server
|
|
106
|
+
// does by default. Narrowing this by `Accept` would turn a stale asset
|
|
107
|
+
// reference into a clean 404, and would also turn `curl /people/per_01J…`
|
|
108
|
+
// into one, so it is left alone.
|
|
109
|
+
app.use('*', webRequestsOnly(serveStatic({ path: indexHtml })))
|
|
110
|
+
}
|