@kayjs/kay 1.106.0 → 1.108.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/README.md +2 -2
- package/dist/eslint.js +477 -204
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-from-islands.md +1 -1
- package/llms/actions-refused-fields.md +22 -0
- package/llms/actions.md +2 -22
- package/llms/browser-only.md +1 -1
- package/llms/compiler-api.md +1 -1
- package/llms/component-libraries.md +1 -1
- package/llms/components.md +1 -1
- package/llms/deprecated-lints.md +2 -4
- package/llms/island-and-form-lints.md +1 -0
- package/llms/islands.md +11 -2
- package/llms/project-new-and-add.md +1 -1
- package/llms/project.md +2 -2
- package/llms/schemas.md +6 -4
- package/llms/upgrading.md +25 -1
- package/llms/whats-new-archive.md +36 -1
- package/llms/whats-new.md +18 -35
- package/llms.txt +1 -1
- package/package.json +15 -6
- package/src/build/client.ts +23 -26
- package/src/build/entry-files.ts +19 -0
- package/src/build/icon-slots.ts +140 -0
- package/src/build/lazy-components.ts +130 -0
- package/src/build/lazy-manifest.ts +56 -0
- package/src/build/lazy-stubs.ts +31 -0
- package/src/build/libraries.ts +24 -2
- package/src/build/plugin.ts +13 -1
- package/src/build/runtime-chunk.ts +3 -3
- package/src/build/source-files.ts +8 -3
- package/src/build/transform.ts +17 -7
- package/src/build/variants.ts +97 -5
- package/src/check/action-options.ts +56 -0
- package/src/check/project.ts +4 -4
- package/src/check/renamed-packages.ts +3 -63
- package/src/check/schema-json.ts +226 -0
- package/src/check/shadow-types.ts +15 -27
- package/src/compiler/codegen.ts +7 -17
- package/src/compiler/dead-code.ts +22 -0
- package/src/compiler/deprecated-lints-1-105.ts +41 -0
- package/src/compiler/deprecated-lints-1-106.ts +1 -1
- package/src/compiler/deprecated-lints-1-107.ts +14 -0
- package/src/compiler/deprecated-lints.ts +8 -65
- package/src/compiler/dom-property.ts +23 -0
- package/src/compiler/dom.ts +15 -15
- package/src/compiler/import-edits.ts +15 -0
- package/src/compiler/index.ts +8 -3
- package/src/compiler/island-declaration.ts +15 -0
- package/src/compiler/kay-exports.ts +1 -0
- package/src/compiler/lazy-imports.ts +357 -0
- package/src/compiler/parser.ts +2 -2
- package/src/compiler/route-lints.ts +2 -0
- package/src/compiler/schema-lints.ts +13 -0
- package/src/compiler/script-server.ts +6 -3
- package/src/content/validate.ts +1 -1
- package/src/new.ts +6 -2
- package/src/package-names.ts +1 -1
- package/src/runtime/action-call.ts +39 -327
- package/src/runtime/action-run.ts +357 -0
- package/src/runtime/action.ts +47 -4
- package/src/runtime/client.ts +10 -0
- package/src/runtime/dom-lazy.ts +276 -0
- package/src/runtime/field-errors.ts +58 -0
- package/src/runtime/field-refusal.ts +179 -0
- package/src/runtime/form.ts +3 -2
- package/src/runtime/html.ts +6 -1
- package/src/runtime/index.ts +2 -0
- package/src/runtime/island.server.ts +36 -2
- package/src/runtime/schema-record.ts +2 -1
- package/src/runtime/schema.client.ts +104 -14
- package/src/runtime/schema.ts +9 -2
- package/src/runtime/standard-schema.ts +32 -0
- package/src/runtime/validate-run.client.ts +179 -0
- package/src/runtime/validate.client.ts +11 -0
- package/src/runtime/validate.ts +4 -137
- package/src/server/actions.ts +48 -43
- package/src/server/answer.ts +102 -0
- package/src/server/api-input.ts +166 -0
- package/src/server/api.ts +12 -44
- package/src/server/app-stages.ts +15 -8
- package/src/server/app.ts +5 -2
- package/src/server/client.ts +2 -0
- package/src/server/dev.ts +7 -9
- package/src/server/file-limit.ts +7 -1
- package/src/server/forms.ts +1 -1
- package/src/server/host-allowlist.ts +7 -7
- package/src/server/index.ts +2 -0
- package/src/server/kay-routes.ts +221 -0
- package/src/server/kay-server.ts +228 -0
- package/src/server/kept-exports.ts +1 -0
- package/src/server/library.ts +0 -2
- package/src/server/mcp.ts +1 -1
- package/src/server/openapi.ts +12 -3
- package/src/server/phases.ts +249 -0
- package/src/server/protection.ts +6 -6
- package/src/server/proxy-key.ts +8 -8
- package/src/server/query.ts +4 -25
- package/src/server/receive.ts +43 -0
- package/src/server/render-document.ts +13 -3
- package/src/server/request-context.ts +137 -0
- package/src/server/schema-check.ts +1 -1
- package/src/server/socket.ts +19 -47
- package/src/server/stream.ts +1 -1
- package/src/server/validate.ts +346 -0
- package/src/test/mount.ts +6 -2
- package/types/build/client.d.ts +3 -2
- package/types/build/client.d.ts.map +1 -1
- package/types/build/entry-files.d.ts +4 -0
- package/types/build/entry-files.d.ts.map +1 -0
- package/types/build/icon-slots.d.ts +7 -0
- package/types/build/icon-slots.d.ts.map +1 -0
- package/types/build/lazy-components.d.ts +6 -0
- package/types/build/lazy-components.d.ts.map +1 -0
- package/types/build/lazy-manifest.d.ts +19 -0
- package/types/build/lazy-manifest.d.ts.map +1 -0
- package/types/build/lazy-stubs.d.ts +5 -0
- package/types/build/lazy-stubs.d.ts.map +1 -0
- package/types/build/libraries.d.ts +2 -0
- package/types/build/libraries.d.ts.map +1 -1
- package/types/build/plugin.d.ts +2 -0
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/source-files.d.ts.map +1 -1
- package/types/build/transform.d.ts +3 -1
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/variants.d.ts.map +1 -1
- package/types/check/action-options.d.ts +3 -0
- package/types/check/action-options.d.ts.map +1 -0
- package/types/check/renamed-packages.d.ts +0 -3
- package/types/check/renamed-packages.d.ts.map +1 -1
- package/types/check/schema-json.d.ts +3 -0
- package/types/check/schema-json.d.ts.map +1 -0
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +2 -0
- package/types/compiler/codegen.d.ts.map +1 -1
- package/types/compiler/dead-code.d.ts +9 -0
- package/types/compiler/dead-code.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
- package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-106.d.ts +1 -1
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -1
- package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
- package/types/compiler/deprecated-lints.d.ts +36 -53
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/dom-property.d.ts +9 -0
- package/types/compiler/dom-property.d.ts.map +1 -0
- package/types/compiler/dom.d.ts +1 -0
- package/types/compiler/dom.d.ts.map +1 -1
- package/types/compiler/import-edits.d.ts +1 -0
- package/types/compiler/import-edits.d.ts.map +1 -1
- package/types/compiler/index.d.ts +2 -0
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/island-declaration.d.ts +5 -0
- package/types/compiler/island-declaration.d.ts.map +1 -1
- package/types/compiler/kay-exports.d.ts.map +1 -1
- package/types/compiler/lazy-imports.d.ts +33 -0
- package/types/compiler/lazy-imports.d.ts.map +1 -0
- package/types/compiler/lints.d.ts +44 -53
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/route-lints.d.ts +8 -0
- package/types/compiler/route-lints.d.ts.map +1 -1
- package/types/compiler/schema-lints.d.ts +11 -0
- package/types/compiler/schema-lints.d.ts.map +1 -0
- package/types/compiler/script-server.d.ts.map +1 -1
- package/types/new.d.ts +1 -0
- package/types/new.d.ts.map +1 -1
- package/types/runtime/action-call.d.ts.map +1 -1
- package/types/runtime/action-run.d.ts +23 -0
- package/types/runtime/action-run.d.ts.map +1 -0
- package/types/runtime/action.d.ts +26 -1
- package/types/runtime/action.d.ts.map +1 -1
- package/types/runtime/client.d.ts.map +1 -1
- package/types/runtime/dom-lazy.d.ts +6 -0
- package/types/runtime/dom-lazy.d.ts.map +1 -0
- package/types/runtime/field-errors.d.ts +4 -0
- package/types/runtime/field-errors.d.ts.map +1 -0
- package/types/runtime/field-refusal.d.ts +37 -0
- package/types/runtime/field-refusal.d.ts.map +1 -0
- package/types/runtime/form.d.ts +1 -1
- package/types/runtime/form.d.ts.map +1 -1
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/index.d.ts +2 -0
- package/types/runtime/index.d.ts.map +1 -1
- package/types/runtime/island.server.d.ts +2 -0
- package/types/runtime/island.server.d.ts.map +1 -1
- package/types/runtime/schema-record.d.ts.map +1 -1
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +6 -1
- package/types/runtime/schema.d.ts.map +1 -1
- package/types/runtime/standard-schema.d.ts +37 -0
- package/types/runtime/standard-schema.d.ts.map +1 -0
- package/types/runtime/validate-run.client.d.ts +4 -0
- package/types/runtime/validate-run.client.d.ts.map +1 -0
- package/types/runtime/validate.client.d.ts +4 -0
- package/types/runtime/validate.client.d.ts.map +1 -0
- package/types/runtime/validate.d.ts +1 -21
- package/types/runtime/validate.d.ts.map +1 -1
- package/types/server/actions.d.ts.map +1 -1
- package/types/server/answer.d.ts +6 -0
- package/types/server/answer.d.ts.map +1 -0
- package/types/server/api-input.d.ts +19 -0
- package/types/server/api-input.d.ts.map +1 -0
- package/types/server/api.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +4 -1
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/app.d.ts +2 -0
- package/types/server/app.d.ts.map +1 -1
- package/types/server/client.d.ts +1 -0
- package/types/server/client.d.ts.map +1 -1
- package/types/server/dev.d.ts +1 -1
- package/types/server/dev.d.ts.map +1 -1
- package/types/server/file-limit.d.ts.map +1 -1
- package/types/server/host-allowlist.d.ts +3 -3
- package/types/server/host-allowlist.d.ts.map +1 -1
- package/types/server/index.d.ts +2 -0
- package/types/server/index.d.ts.map +1 -1
- package/types/server/kay-routes.d.ts +47 -0
- package/types/server/kay-routes.d.ts.map +1 -0
- package/types/server/kay-server.d.ts +25 -0
- package/types/server/kay-server.d.ts.map +1 -0
- package/types/server/kept-exports.d.ts.map +1 -1
- package/types/server/library.d.ts +0 -1
- package/types/server/library.d.ts.map +1 -1
- package/types/server/openapi.d.ts.map +1 -1
- package/types/server/phases.d.ts +36 -0
- package/types/server/phases.d.ts.map +1 -0
- package/types/server/protection.d.ts +3 -2
- package/types/server/protection.d.ts.map +1 -1
- package/types/server/proxy-key.d.ts +3 -3
- package/types/server/proxy-key.d.ts.map +1 -1
- package/types/server/query.d.ts.map +1 -1
- package/types/server/receive.d.ts +9 -0
- package/types/server/receive.d.ts.map +1 -0
- package/types/server/render-document.d.ts.map +1 -1
- package/types/server/request-context.d.ts +29 -0
- package/types/server/request-context.d.ts.map +1 -0
- package/types/server/schema-check.d.ts +1 -0
- package/types/server/schema-check.d.ts.map +1 -1
- package/types/server/socket.d.ts +3 -5
- package/types/server/socket.d.ts.map +1 -1
- package/types/server/validate.d.ts +26 -0
- package/types/server/validate.d.ts.map +1 -0
- package/types/test/mount.d.ts.map +1 -1
- package/src/check/schema-type.ts +0 -9
- package/src/compiler/deprecated-script-fence.ts +0 -26
- package/src/server/field-errors.ts +0 -136
- package/types/check/schema-type.d.ts +0 -3
- package/types/check/schema-type.d.ts.map +0 -1
- package/types/compiler/deprecated-script-fence.d.ts +0 -3
- package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
- package/types/server/field-errors.d.ts +0 -6
- package/types/server/field-errors.d.ts.map +0 -1
package/src/server/app.ts
CHANGED
|
@@ -25,6 +25,7 @@ import { faviconSource, MANIFEST, manifestJson } from './favicon'
|
|
|
25
25
|
import { guardRequirement, requiredBy } from './guard-requirement'
|
|
26
26
|
import { loadGuards } from './guards'
|
|
27
27
|
import { refuseLocaleClashes } from './i18n'
|
|
28
|
+
import { KayServer, serveThrough } from './kay-server'
|
|
28
29
|
import { dropExitListenerOnStop } from './lifecycle'
|
|
29
30
|
import { mcpKeySetting, resolveMcpKey } from './mcp-key'
|
|
30
31
|
import { checkPageLimits } from './page-limits'
|
|
@@ -185,10 +186,12 @@ export async function pagesApp(project: Project<ServedOptions>, loadClient: () =
|
|
|
185
186
|
const required = guardRequirement(requiredBy(options), setup.routes, setup.endpoints, await setup.api)
|
|
186
187
|
const routes = await servedRoutes(setup.routes, setup.claimed.routes, required)
|
|
187
188
|
const app = new Elysia({ name: 'kay/pages', seed: root })
|
|
189
|
+
const kay = new KayServer({ env: { base: setup.base, trailingSlash: options.trailingSlash ?? 'never' }, dev: running.dev })
|
|
188
190
|
|
|
189
191
|
dropExitListenerOnStop(app)
|
|
192
|
+
serveThrough(app, kay)
|
|
190
193
|
|
|
191
|
-
const boot = bootOf({ app, env: { ...running, base: setup.base, trailingSlash: options.trailingSlash }, project, setup, served, routes, table, late, mcpAt, mcpKey, required })
|
|
194
|
+
const boot = bootOf({ app, kay, env: { ...running, base: setup.base, trailingSlash: options.trailingSlash }, project, setup, served, routes, table, late, mcpAt, mcpKey, required })
|
|
192
195
|
const earlier = knowPages(boot)
|
|
193
196
|
const writes = keptPageWrites(boot)
|
|
194
197
|
|
|
@@ -226,5 +229,5 @@ export async function pagesApp(project: Project<ServedOptions>, loadClient: () =
|
|
|
226
229
|
|
|
227
230
|
await pageRoutes(boot, { redirects, publicServed, redirector, prerendered, writes })
|
|
228
231
|
|
|
229
|
-
return { app, served, locale: setup.locales.settings.defaultLocale, securityOptions: boot.securityOptions, routes }
|
|
232
|
+
return { app, kay, served, locale: setup.locales.settings.defaultLocale, securityOptions: boot.securityOptions, routes }
|
|
230
233
|
}
|
package/src/server/client.ts
CHANGED
|
@@ -33,6 +33,8 @@ export interface ClientManifest {
|
|
|
33
33
|
hydrateWhen?: string[]
|
|
34
34
|
// Each island's chunk and every chunk it imports statically, by island id.
|
|
35
35
|
islands: Record<string, string[]>
|
|
36
|
+
// Each component an island loads when it first draws it, by its key (its path from the root): its chunk and every chunk that imports statically.
|
|
37
|
+
lazies?: Record<string, string[]>
|
|
36
38
|
// The islands whose code holds `installPrompt`, which a page rendering one carries the small script keeping the browser's install offer from the first moment.
|
|
37
39
|
installs?: string[]
|
|
38
40
|
// The stylesheet of each page, layout and special page that imports any, by path relative to the root.
|
package/src/server/dev.ts
CHANGED
|
@@ -33,6 +33,7 @@ import { hostName, isLocalHost } from './host-allowlist'
|
|
|
33
33
|
import { localeCopies } from './i18n'
|
|
34
34
|
import { compileErrorPage } from './overlay'
|
|
35
35
|
import { requestId } from './protection'
|
|
36
|
+
import { bridgeReceive, receiving } from './receive'
|
|
36
37
|
import { apiRoutePath, endpointPath, ROUTE_METHODS } from './routes'
|
|
37
38
|
|
|
38
39
|
export const DEV_EVENTS = '/_kay/dev/events'
|
|
@@ -572,9 +573,9 @@ function broadcast(event: string): void {
|
|
|
572
573
|
|
|
573
574
|
// Under `kay dev`, a request naming a host other than localhost, an IP address or the site is refused, as a page of another site renaming itself to this machine's address (DNS rebinding) would send it.
|
|
574
575
|
// `KAY_DEV_HOSTS` lists other names the server answers to, comma-separated, such as a tunnel's; `kay dev --host` adds this machine's own names, as a phone on the network reaches it by.
|
|
575
|
-
export function refuseForeignHosts(
|
|
576
|
+
export function refuseForeignHosts(site: string | undefined, security?: Record<string, string>): ((request: Request) => Response | undefined) | undefined {
|
|
576
577
|
if (!underKayDev())
|
|
577
|
-
return
|
|
578
|
+
return undefined
|
|
578
579
|
|
|
579
580
|
const allowed = new Set((process.env.KAY_DEV_HOSTS ?? '').split(',').map(name => name.trim().toLowerCase()).filter(Boolean))
|
|
580
581
|
const siteName = site === undefined ? undefined : URL.parse(site)?.hostname
|
|
@@ -586,15 +587,12 @@ export function refuseForeignHosts(app: Elysia, site: string | undefined, securi
|
|
|
586
587
|
if (listening !== 'localhost')
|
|
587
588
|
allowed.add(listening.toLowerCase()).add(hostname().toLowerCase()).add(`${hostname().toLowerCase()}.local`)
|
|
588
589
|
|
|
589
|
-
|
|
590
|
+
return (request) => {
|
|
590
591
|
const host = request.headers.get('host')
|
|
591
592
|
const name = host === null ? undefined : hostName(host)
|
|
592
593
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
return new Response(`kay dev answers to localhost, an IP address or the site, and ${host} is none of them; list it in KAY_DEV_HOSTS to reach the server by that name\n`, { status: 421, headers: { ...security, 'content-type': 'text/plain; charset=utf-8' } })
|
|
597
|
-
})
|
|
594
|
+
return host === null || (name !== undefined && (isLocalHost(name) || allowed.has(name))) ? undefined : new Response(`kay dev answers to localhost, an IP address or the site, and ${host} is none of them; list it in KAY_DEV_HOSTS to reach the server by that name\n`, { status: 421, headers: { ...security, 'content-type': 'text/plain; charset=utf-8' } })
|
|
595
|
+
}
|
|
598
596
|
}
|
|
599
597
|
|
|
600
598
|
const DEV_PREFIX = '/_kay/dev/'
|
|
@@ -632,7 +630,7 @@ export function bootFailure(error: unknown, root: string, tools = true): Elysia
|
|
|
632
630
|
const app = new Elysia({ name: 'kay/pages', seed: root })
|
|
633
631
|
|
|
634
632
|
rememberBoot(true)
|
|
635
|
-
|
|
633
|
+
bridgeReceive(app, receiving([refuseForeignHosts(siteFromEnv())]))
|
|
636
634
|
|
|
637
635
|
if (tools) {
|
|
638
636
|
refuseRemoteDevTools(app)
|
package/src/server/file-limit.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { TSchema } from 'elysia'
|
|
2
2
|
import { byteSize } from '../runtime/byte-size'
|
|
3
|
+
import { jsonSchemaOf } from '../runtime/standard-schema'
|
|
3
4
|
|
|
4
5
|
const ELYSIA_SIZE = /^(\d+(?:\.\d+)?)([km])$/
|
|
5
6
|
|
|
@@ -56,7 +57,12 @@ function weigh(node: Node | undefined): number {
|
|
|
56
57
|
|
|
57
58
|
// The body an action taking sized files needs at least, so a `maxSize` written on the field raises the action's limit when the action sets none; undefined when its files have no size.
|
|
58
59
|
export function fileBodyLimit(schema: TSchema): number | undefined {
|
|
59
|
-
const
|
|
60
|
+
const described = jsonSchemaOf(schema)
|
|
61
|
+
|
|
62
|
+
if (described === undefined)
|
|
63
|
+
return undefined
|
|
64
|
+
|
|
65
|
+
const files = weigh(described)
|
|
60
66
|
|
|
61
67
|
return files > 0 ? Math.ceil(files) + FRAMING : undefined
|
|
62
68
|
}
|
package/src/server/forms.ts
CHANGED
|
@@ -2,8 +2,8 @@ import type { TSchema } from 'elysia'
|
|
|
2
2
|
import type { Action, ServerAction } from '../runtime/action'
|
|
3
3
|
import type { FieldRules, FormRules } from '../runtime/validate'
|
|
4
4
|
import { isAction } from '../runtime/action'
|
|
5
|
+
import { errorText } from '../runtime/field-errors'
|
|
5
6
|
import { localeState } from '../runtime/locale-state'
|
|
6
|
-
import { errorText } from './field-errors'
|
|
7
7
|
|
|
8
8
|
// The keywords the browser checks a field against, with the `error` message the server shows for it.
|
|
9
9
|
const CHECKED = new Set(['type', 'format', 'pattern', 'minLength', 'maxLength', 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'minItems', 'maxItems', 'const', 'items', 'anyOf', 'error'])
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ReceiveStep } from './receive'
|
|
2
2
|
import { KayError } from '../compiler/errors'
|
|
3
3
|
import { kayLog } from '../runtime/log'
|
|
4
4
|
import { nodeNet } from './builtins'
|
|
@@ -26,7 +26,7 @@ export function isLocalHost(name: string): boolean {
|
|
|
26
26
|
// Names other than a site's own are refused in production when `security.hosts` is set, as a page of another site renaming itself to this server's address (DNS rebinding) would reach it by its own name.
|
|
27
27
|
// Localhost and IP addresses are always let in, as a health check or a collector on the private network reaches the server by them and a rebinding page cannot make a browser send one.
|
|
28
28
|
// A `*.example.com` entry answers any one label under the domain, never the bare domain.
|
|
29
|
-
export function refuseUnknownHosts(
|
|
29
|
+
export function refuseUnknownHosts(options: { site: string | undefined, hosts: string[] | true, localeHosts: string[], security: Record<string, string> | undefined, health?: string[] | undefined }): ReceiveStep {
|
|
30
30
|
const listed = [...options.hosts === true ? [] : options.hosts, ...options.localeHosts]
|
|
31
31
|
const wildcards = listed.filter(name => name.startsWith('*.')).map(name => name.slice(1).toLowerCase())
|
|
32
32
|
const allowed = new Set(listed.filter(name => !name.startsWith('*.')).map(name => hostName(name) ?? name.toLowerCase()))
|
|
@@ -38,20 +38,20 @@ export function refuseUnknownHosts(app: Elysia, options: { site: string | undefi
|
|
|
38
38
|
if (allowed.size === 0 && wildcards.length === 0)
|
|
39
39
|
throw new KayError('config-invalid', 'kay: security.hosts answers to no host name, so every named host would get a 421: set `site` in kay.config.ts (or SITE_URL), or list the names in `security.hosts`')
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
return (request) => {
|
|
42
42
|
const host = request.headers.get('host')
|
|
43
43
|
|
|
44
44
|
if (host === null)
|
|
45
|
-
return
|
|
45
|
+
return undefined
|
|
46
46
|
|
|
47
47
|
const name = hostName(host)
|
|
48
48
|
|
|
49
49
|
if (name !== undefined && (allowed.has(name) || isLocalHost(name) || wildcards.some(suffix => name.endsWith(suffix) && !name.slice(0, -suffix.length).includes('.') && name.length > suffix.length)))
|
|
50
|
-
return
|
|
50
|
+
return undefined
|
|
51
51
|
|
|
52
52
|
// The container's own probe reaches the server by its service name.
|
|
53
53
|
if (options.health?.includes(requestPath(request)))
|
|
54
|
-
return
|
|
54
|
+
return undefined
|
|
55
55
|
|
|
56
56
|
if (!warned.has(host) && warned.size < 100) {
|
|
57
57
|
warned.add(host)
|
|
@@ -59,5 +59,5 @@ export function refuseUnknownHosts(app: Elysia, options: { site: string | undefi
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
return new Response(`Misdirected request: this host is not one the site answers to\n${FIX}\n`, { status: 421, headers: { ...options.security, 'content-type': 'text/plain; charset=utf-8' } })
|
|
62
|
-
}
|
|
62
|
+
}
|
|
63
63
|
}
|
package/src/server/index.ts
CHANGED
|
@@ -89,6 +89,8 @@ export type { FileHooks, FileStore, PutOptions, StoredFile, UrlOptions } from '.
|
|
|
89
89
|
export { diskFiles, files, memoryFiles, s3Files } from './storage'
|
|
90
90
|
export type { RenderToStringOptions } from './string'
|
|
91
91
|
export { renderLocale, renderToString } from './string'
|
|
92
|
+
export type { Issue, Refusal, Source, Validated, ValidateOptions } from './validate'
|
|
93
|
+
export { validate } from './validate'
|
|
92
94
|
export { CspViolationError } from './violations'
|
|
93
95
|
export type { BunWebSocketConstructor } from './web-socket'
|
|
94
96
|
export { WebSocket } from './web-socket'
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type { TrailingSlash } from '../config'
|
|
2
|
+
import type { RequestContext } from './request-context'
|
|
3
|
+
import { decodePath } from './routes'
|
|
4
|
+
|
|
5
|
+
export type RouteKind = 'page' | 'action' | 'endpoint' | 'api' | 'socket' | 'file' | 'kay' | 'mounted'
|
|
6
|
+
|
|
7
|
+
// What a route reads of the request, each part a schema the `input` phase validates against.
|
|
8
|
+
export interface RouteInput {
|
|
9
|
+
params?: unknown
|
|
10
|
+
query?: unknown
|
|
11
|
+
body?: unknown
|
|
12
|
+
headers?: unknown
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// One route kay serves, whatever it is: the table holds nothing else. `path` is Bun's syntax (`/blog/:slug`, `/docs/*`) as the router sees it, before the mount point and the slash the app asks for.
|
|
16
|
+
export interface RouteRecord {
|
|
17
|
+
method: string
|
|
18
|
+
path: string
|
|
19
|
+
kind: RouteKind
|
|
20
|
+
input?: RouteInput | undefined
|
|
21
|
+
output?: unknown
|
|
22
|
+
// Settings read by the phases (`maxSize`, `timeout`, `rateLimit`, `cors`...) and by plugins; resolved nearest first, which the slices moving routes onto the table fill in.
|
|
23
|
+
options: Record<string, unknown>
|
|
24
|
+
// The app, a library's name, or `kay`.
|
|
25
|
+
owner: string
|
|
26
|
+
// The locale a prefixed or translated path serves in.
|
|
27
|
+
locale?: string | undefined
|
|
28
|
+
handle?: ((context: RequestContext) => unknown) | undefined
|
|
29
|
+
// A Response known at boot, which `Bun.serve` answers with no JS while no phase has anything to apply.
|
|
30
|
+
static?: Response | undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface RouteEnv {
|
|
34
|
+
base: string
|
|
35
|
+
trailingSlash: TrailingSlash
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A record under one spelling of its path.
|
|
39
|
+
export interface Compiled {
|
|
40
|
+
record: RouteRecord
|
|
41
|
+
// The path as registered, the mount point and the slash included.
|
|
42
|
+
path: string
|
|
43
|
+
segments: string[]
|
|
44
|
+
// Set for the other spelling of a page's path, which answers with a redirect to the served one.
|
|
45
|
+
redirect: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type Params = Record<string, string>
|
|
49
|
+
|
|
50
|
+
const EDGE_SLASHES = /^\/+/
|
|
51
|
+
|
|
52
|
+
// Every spelling a record is served under: a proxy may strip the mount point or not, so both are served; a page follows `trailingSlash`, the other spelling redirecting to it, and every other kind is served as written.
|
|
53
|
+
export function compileRecord(record: RouteRecord, env: RouteEnv): Compiled[] {
|
|
54
|
+
const bases = env.base === '' ? [''] : ['', env.base]
|
|
55
|
+
const catchAll = record.path.endsWith('/*')
|
|
56
|
+
const policy = record.kind === 'page' && !catchAll ? env.trailingSlash : 'exact'
|
|
57
|
+
const compiled: Compiled[] = []
|
|
58
|
+
|
|
59
|
+
for (const base of bases) {
|
|
60
|
+
const joined = record.path === '/' ? base : `${base}${record.path}`
|
|
61
|
+
const bare = joined === '' ? '/' : joined
|
|
62
|
+
const slashed = joined === '' ? '/' : `${joined}/`
|
|
63
|
+
const spelling = (path: string, redirect: boolean) => compiled.push({ record, path, segments: path.split('/'), redirect })
|
|
64
|
+
|
|
65
|
+
if (policy === 'exact' || (bare === '/' && slashed === '/')) {
|
|
66
|
+
spelling(bare, false)
|
|
67
|
+
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (policy === 'ignore') {
|
|
72
|
+
spelling(bare, false)
|
|
73
|
+
spelling(slashed, false)
|
|
74
|
+
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
spelling(policy === 'always' ? slashed : bare, false)
|
|
79
|
+
spelling(policy === 'always' ? bare : slashed, true)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return compiled
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Whether a path has params, which only a request for it needs read from the path.
|
|
86
|
+
export function isDynamic(path: string): boolean {
|
|
87
|
+
return path.includes(':') || path.includes('*')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The params a path gives the route, each decoded once as the path writes it: `%2F` is a slash in the value and never a segment. `null` when the path is not the route's, undefined when a value holds an escape that does not decode.
|
|
91
|
+
export function extractParams(segments: readonly string[], pathname: string): Params | null | undefined {
|
|
92
|
+
const given = pathname.split('/')
|
|
93
|
+
const params: Params = {}
|
|
94
|
+
|
|
95
|
+
for (const [index, segment] of segments.entries()) {
|
|
96
|
+
if (segment === '*') {
|
|
97
|
+
// A catch-all's own directory (`/docs` for `/docs/*`) is not one of its paths.
|
|
98
|
+
if (given.length <= index)
|
|
99
|
+
return null
|
|
100
|
+
|
|
101
|
+
const value = decodePath(given.slice(index).join('/'))
|
|
102
|
+
|
|
103
|
+
if (value === undefined)
|
|
104
|
+
return undefined
|
|
105
|
+
|
|
106
|
+
params['*'] = value
|
|
107
|
+
|
|
108
|
+
return params
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const value = given[index]
|
|
112
|
+
|
|
113
|
+
if (value === undefined)
|
|
114
|
+
return null
|
|
115
|
+
|
|
116
|
+
if (segment.startsWith(':')) {
|
|
117
|
+
if (value === '')
|
|
118
|
+
return null
|
|
119
|
+
|
|
120
|
+
const decoded = decodePath(value)
|
|
121
|
+
|
|
122
|
+
if (decoded === undefined)
|
|
123
|
+
return undefined
|
|
124
|
+
|
|
125
|
+
params[segment.slice(1)] = decoded
|
|
126
|
+
|
|
127
|
+
continue
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (value !== segment)
|
|
131
|
+
return null
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return segments.length === given.length ? params : null
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// How early a spelling is tried among the ones with params: a static segment before a param, a param before a catch-all.
|
|
138
|
+
function rank(segment: string | undefined): number {
|
|
139
|
+
if (segment === undefined)
|
|
140
|
+
return 3
|
|
141
|
+
|
|
142
|
+
if (segment === '*')
|
|
143
|
+
return 2
|
|
144
|
+
|
|
145
|
+
return segment.startsWith(':') ? 1 : 0
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function bySpecificity(a: Compiled, b: Compiled): number {
|
|
149
|
+
for (let index = 0; index < Math.max(a.segments.length, b.segments.length); index++) {
|
|
150
|
+
const difference = rank(a.segments[index]) - rank(b.segments[index])
|
|
151
|
+
|
|
152
|
+
if (difference !== 0)
|
|
153
|
+
return difference
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return 0
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
type Methods = Map<string, Compiled>
|
|
160
|
+
|
|
161
|
+
export interface Match {
|
|
162
|
+
compiled: Compiled
|
|
163
|
+
// Undefined for a path whose param held an escape that does not decode.
|
|
164
|
+
params: Params | undefined
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// The records by method and path, for a request `Bun.serve`'s router did not match for kay: a rewritten one, or one that came through `fetch` alone.
|
|
168
|
+
export class RouteIndex {
|
|
169
|
+
readonly #exact = new Map<string, Methods>()
|
|
170
|
+
readonly #dynamic = new Map<string, Methods>()
|
|
171
|
+
|
|
172
|
+
constructor(compiled: readonly Compiled[]) {
|
|
173
|
+
// Sorted so the dynamic paths are tried most specific first, a Map keeping their order.
|
|
174
|
+
for (const entry of [...compiled].sort(bySpecificity)) {
|
|
175
|
+
const table = isDynamic(entry.path) ? this.#dynamic : this.#exact
|
|
176
|
+
const methods = table.get(entry.path) ?? new Map<string, Compiled>()
|
|
177
|
+
|
|
178
|
+
table.set(entry.path, methods)
|
|
179
|
+
|
|
180
|
+
// The first record of a method keeps it, as the router tries them in order.
|
|
181
|
+
if (!methods.has(entry.record.method))
|
|
182
|
+
methods.set(entry.record.method, entry)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
match(method: string, pathname: string): Match | undefined {
|
|
187
|
+
const exact = this.#exact.get(pathname)
|
|
188
|
+
|
|
189
|
+
if (exact !== undefined) {
|
|
190
|
+
const compiled = pick(exact, method)
|
|
191
|
+
|
|
192
|
+
return compiled && { compiled, params: {} }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
for (const [path, methods] of this.#dynamic) {
|
|
196
|
+
const params = extractParams(path.split('/'), pathname)
|
|
197
|
+
|
|
198
|
+
if (params === null)
|
|
199
|
+
continue
|
|
200
|
+
|
|
201
|
+
const compiled = pick(methods, method)
|
|
202
|
+
|
|
203
|
+
if (compiled !== undefined)
|
|
204
|
+
return { compiled, params }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return undefined
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// The record for a method: its own, else `ALL`, else a GET one for a HEAD.
|
|
212
|
+
function pick(methods: Methods, method: string): Compiled | undefined {
|
|
213
|
+
return methods.get(method) ?? methods.get('ALL') ?? (method === 'HEAD' ? methods.get('GET') : undefined)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The path a redirect from the other slash spelling goes to, from the one it was asked at: one slash more or less, and no leading `//`, which a browser reads as another host.
|
|
217
|
+
export function slashed(pathname: string, search: string): string {
|
|
218
|
+
const path = `/${pathname.replace(EDGE_SLASHES, '')}`
|
|
219
|
+
|
|
220
|
+
return `${path.endsWith('/') ? path.slice(0, -1) : `${path}/`}${search}`
|
|
221
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import type { Server } from 'bun'
|
|
2
|
+
import type { Elysia } from 'elysia'
|
|
3
|
+
import type { Compiled, RouteEnv, RouteRecord } from './kay-routes'
|
|
4
|
+
import type { PipelineOptions } from './phases'
|
|
5
|
+
import type { ReceiveServer } from './receive'
|
|
6
|
+
import { compileRecord, extractParams, isDynamic, RouteIndex, slashed } from './kay-routes'
|
|
7
|
+
import { emptyPhases, Pipeline } from './phases'
|
|
8
|
+
import { requestUrl } from './request-url'
|
|
9
|
+
|
|
10
|
+
// What answers a request no record of the table serves: the Elysia app kay still routes the rest through.
|
|
11
|
+
export type Fallback = (request: Request, server: ReceiveServer) => Response | Promise<Response>
|
|
12
|
+
|
|
13
|
+
type Handler = (request: Request, server: ReceiveServer) => Promise<Response>
|
|
14
|
+
type BunRoutes = Record<string, Record<string, Handler | Response>>
|
|
15
|
+
|
|
16
|
+
export interface KayServerOptions extends PipelineOptions {
|
|
17
|
+
env: RouteEnv
|
|
18
|
+
fallback?: Fallback | undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']
|
|
22
|
+
|
|
23
|
+
// The methods Bun is handed a record for: a GET record answers HEAD too, an ALL one every method.
|
|
24
|
+
function askedBy(method: string): string[] {
|
|
25
|
+
if (method === 'ALL')
|
|
26
|
+
return METHODS
|
|
27
|
+
|
|
28
|
+
return method === 'GET' ? ['GET', 'HEAD'] : [method]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// kay's own server: the route table compiled into the `routes` `Bun.serve` matches, and `fetch` for what that router leaves, with the fixed phases around both and the fallback app answering what no record serves.
|
|
32
|
+
export class KayServer {
|
|
33
|
+
readonly pipeline: Pipeline
|
|
34
|
+
readonly records: RouteRecord[] = []
|
|
35
|
+
fallback: Fallback | undefined
|
|
36
|
+
readonly #env: RouteEnv
|
|
37
|
+
#built: { routes: BunRoutes, index: RouteIndex } | undefined
|
|
38
|
+
|
|
39
|
+
constructor(options: KayServerOptions) {
|
|
40
|
+
this.pipeline = new Pipeline(emptyPhases(), options)
|
|
41
|
+
this.#env = options.env
|
|
42
|
+
this.fallback = options.fallback
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Adds a record to the table; the routes compile again from the next read.
|
|
46
|
+
add(...records: RouteRecord[]): void {
|
|
47
|
+
this.records.push(...records)
|
|
48
|
+
this.#built = undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// What to hand `Bun.serve({ routes })`.
|
|
52
|
+
get routes(): BunRoutes {
|
|
53
|
+
return this.#build().routes
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Answers a request the router did not send to a record: a rewritten one, a request with no server, or one whose path is the fallback's.
|
|
57
|
+
async fetch(request: Request, server?: ReceiveServer): Promise<Response> {
|
|
58
|
+
const received = await this.#receive(request, server)
|
|
59
|
+
|
|
60
|
+
if (received instanceof Response)
|
|
61
|
+
return received
|
|
62
|
+
|
|
63
|
+
const hit = this.#build().index.match(received.method, requestUrl(received).pathname)
|
|
64
|
+
|
|
65
|
+
return hit === undefined ? this.#fallback(received, server) : this.#answer(hit.compiled, hit.params, received)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#build(): { routes: BunRoutes, index: RouteIndex } {
|
|
69
|
+
if (this.#built !== undefined)
|
|
70
|
+
return this.#built
|
|
71
|
+
|
|
72
|
+
const compiled = this.records.flatMap(record => compileRecord(record, this.#env))
|
|
73
|
+
const routes: BunRoutes = {}
|
|
74
|
+
|
|
75
|
+
for (const entry of compiled) {
|
|
76
|
+
const methods = routes[entry.path] ??= {}
|
|
77
|
+
const asked = askedBy(entry.record.method)
|
|
78
|
+
const staticAnswer = !entry.redirect && this.pipeline.empty ? entry.record.static : undefined
|
|
79
|
+
|
|
80
|
+
for (const method of asked) {
|
|
81
|
+
// The first record of a path and method keeps it.
|
|
82
|
+
if (methods[method] === undefined)
|
|
83
|
+
methods[method] = staticAnswer !== undefined && method === 'GET' ? staticAnswer : async (request, server) => this.#serve(entry, request, server)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return this.#built = { routes, index: new RouteIndex(compiled) }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// `receive`, its answer or the failure of a step taken through the phases that follow it, as any failure is.
|
|
91
|
+
async #receive(request: Request, server: ReceiveServer): Promise<Request | Response> {
|
|
92
|
+
try {
|
|
93
|
+
const received = this.pipeline.receive(request, server)
|
|
94
|
+
|
|
95
|
+
return received instanceof Response ? await this.pipeline.settle(request, received) : received
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
return this.pipeline.failed(request, error)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A request Bun's router sent to a record: received first, and handed on to the table's own match when `receive` rewrote it.
|
|
103
|
+
async #serve(entry: Compiled, request: Request, server: ReceiveServer): Promise<Response> {
|
|
104
|
+
const received = await this.#receive(request, server)
|
|
105
|
+
|
|
106
|
+
if (received instanceof Response)
|
|
107
|
+
return received
|
|
108
|
+
|
|
109
|
+
if (received !== request)
|
|
110
|
+
return this.fetch(received, server)
|
|
111
|
+
|
|
112
|
+
const params = isDynamic(entry.path) ? extractParams(entry.segments, requestUrl(received).pathname) : {}
|
|
113
|
+
|
|
114
|
+
// A path the router took for the record, which its own reading of the params refuses, is the fallback's.
|
|
115
|
+
return params === null ? this.#fallback(received, server) : this.#answer(entry, params, received)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async #answer(entry: Compiled, params: Record<string, string> | undefined, request: Request): Promise<Response> {
|
|
119
|
+
if (entry.redirect) {
|
|
120
|
+
const { pathname, search } = requestUrl(request)
|
|
121
|
+
|
|
122
|
+
return this.pipeline.settle(request, new Response(null, { status: 308, headers: { location: slashed(pathname, search) } }))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return this.pipeline.serve(entry.record.static === undefined || entry.record.handle !== undefined ? entry.record : { ...entry.record, handle: () => entry.record.static!.clone() }, params, request)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async #fallback(request: Request, server: ReceiveServer): Promise<Response> {
|
|
129
|
+
if (this.fallback === undefined)
|
|
130
|
+
return this.pipeline.settle(request, new Response('Not Found\n', { status: 404, headers: { 'content-type': 'text/plain; charset=utf-8' } }))
|
|
131
|
+
|
|
132
|
+
return this.pipeline.settle(request, await this.fallback(request, server))
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The routes Elysia handed Bun with the table's laid over them, each method of a path the table serves taken from it and the others left to Elysia.
|
|
137
|
+
function withTable(elysia: Record<string, unknown> | undefined, table: BunRoutes): Record<string, unknown> {
|
|
138
|
+
const merged: Record<string, unknown> = { ...elysia }
|
|
139
|
+
|
|
140
|
+
for (const [path, methods] of Object.entries(table)) {
|
|
141
|
+
const held = merged[path]
|
|
142
|
+
|
|
143
|
+
merged[path] = typeof held === 'object' && held !== null && Object.getPrototypeOf(held) === Object.prototype ? { ...held, ...methods } : methods
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return merged
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const INSTALLED = Symbol.for('kay.server.installed')
|
|
150
|
+
|
|
151
|
+
type Reload = Server<unknown>['reload']
|
|
152
|
+
|
|
153
|
+
// What a server holds once kay is in front of it: the table it serves now, which a later run of the app (`kay dev` restarts it on the same server) takes over.
|
|
154
|
+
interface Front {
|
|
155
|
+
kay: KayServer
|
|
156
|
+
// What the app last handed the server, laid over again when a later run's table takes over.
|
|
157
|
+
options?: Parameters<Reload>[0]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
interface Installable extends Server<unknown> {
|
|
161
|
+
[INSTALLED]?: Front
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The `reload` of the server is its prototype's, which the patch shadows: it is lifted for the call, then put back.
|
|
165
|
+
function lifted(server: Installable, options: Parameters<Reload>[0]): void {
|
|
166
|
+
delete (server as { reload?: Reload }).reload
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
server.reload(options)
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
server.reload = patched as Reload
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Hands Bun what the app gave, with the table's routes laid over its own and a `fetch` that reads the table from the server at each request, so a reload of an earlier run never leaves a request to a table that has no fallback.
|
|
177
|
+
function lay(server: Installable, front: Front, options: Parameters<Reload>[0]): void {
|
|
178
|
+
const { fetch: elysia } = options as { fetch?: ((request: Request, server: ReceiveServer) => Response | Promise<Response>) | undefined }
|
|
179
|
+
|
|
180
|
+
front.kay.fallback = elysia && (async request => elysia(request, server))
|
|
181
|
+
lifted(server, { ...options, fetch: async (request: Request, bun: ReceiveServer) => server[INSTALLED]!.kay.fetch(request, bun ?? server), routes: withTable(options.routes, front.kay.routes) as never })
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// What the server's `reload` is once kay is in front: the app's `fetch` becomes the fallback of the table's, and its routes are laid under the table's. It is a function of the module, not of one run of the app, so it keeps no run's app or table alive; a wrapper of the server (`kay dev` hands each run one) calls it on the server itself, so `this` is the server whatever object the app holds.
|
|
185
|
+
function patched(this: Installable, options: Parameters<Reload>[0]): void {
|
|
186
|
+
// Elysia also reloads a server with the server itself, which holds what it already serves.
|
|
187
|
+
if (typeof (options as { stop?: unknown }).stop === 'function')
|
|
188
|
+
return lifted(this, options)
|
|
189
|
+
|
|
190
|
+
const front = this[INSTALLED]!
|
|
191
|
+
|
|
192
|
+
front.options = options
|
|
193
|
+
lay(this, front, options)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Puts kay's server in front of the Elysia app once it listens: Bun is handed `kay.fetch`, with the app's own `fetch` as its fallback, and the routes the table compiled to, beside the ones Elysia hands it.
|
|
197
|
+
// Elysia hands Bun its `fetch` and routes in a `reload` when it has loaded its modules, a microtask after it starts listening, and recompiles `app.fetch` by then, so the `reload` of the running server is where both are taken over.
|
|
198
|
+
// Elysia also hands Bun a handler for each of its routes, which Bun calls without `fetch`, so the answer would skip `respond` and `after`: once a step of either is set, Elysia's own router serves them through `fetch` instead. With none set, which is the case until the slices moving the response work onto the phases land, the app is served exactly as before.
|
|
199
|
+
export function serveThrough(app: Elysia, kay: KayServer): void {
|
|
200
|
+
app.onStart((root) => {
|
|
201
|
+
const server = root.server as Installable | null | undefined
|
|
202
|
+
|
|
203
|
+
if (server === null || server === undefined)
|
|
204
|
+
return
|
|
205
|
+
|
|
206
|
+
const { respond, after } = kay.pipeline.phases
|
|
207
|
+
|
|
208
|
+
if (respond.length > 0 || after.length > 0)
|
|
209
|
+
root.config.systemRouter = false
|
|
210
|
+
|
|
211
|
+
const held = server[INSTALLED]
|
|
212
|
+
|
|
213
|
+
if (held !== undefined) {
|
|
214
|
+
// The table of the new run takes the server over, its routes laid under Bun again, and no table of an earlier run keeps the app of a later one.
|
|
215
|
+
kay.fallback = held.kay.fallback
|
|
216
|
+
held.kay.fallback = undefined
|
|
217
|
+
held.kay = kay
|
|
218
|
+
|
|
219
|
+
if (held.options !== undefined)
|
|
220
|
+
lay(server, held, held.options)
|
|
221
|
+
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
server[INSTALLED] = { kay }
|
|
226
|
+
server.reload = patched as Reload
|
|
227
|
+
})
|
|
228
|
+
}
|
package/src/server/library.ts
CHANGED
|
@@ -107,8 +107,6 @@ export { sha256 } from './sha256'
|
|
|
107
107
|
export { stoppers } from './signals'
|
|
108
108
|
export { appSecrets, keyedDigest } from './stamp'
|
|
109
109
|
export type { MissingTranslations } from './translations'
|
|
110
|
-
// Deprecated since 1.103: `Schema`, kay's own name for it.
|
|
111
|
-
export type { TSchema } from 'elysia'
|
|
112
110
|
|
|
113
111
|
type Translations = typeof import('./translations')
|
|
114
112
|
type Drafts = typeof import('./message-drafts')
|
package/src/server/mcp.ts
CHANGED
|
@@ -176,7 +176,7 @@ function toolOf(path: string, method: string, operation: Operation, id: string,
|
|
|
176
176
|
const init: RequestInit = { method: method.toUpperCase(), headers: { accept: 'application/json' } }
|
|
177
177
|
|
|
178
178
|
if (action) {
|
|
179
|
-
init.headers = { 'accept': 'application/json', 'content-type': 'application/json', 'x-kay-action': '1' }
|
|
179
|
+
init.headers = { 'accept': 'application/json', 'content-type': 'application/json', 'x-kay-action': '1', 'x-kay-tool': '1' }
|
|
180
180
|
init.body = encodeValue(args)
|
|
181
181
|
}
|
|
182
182
|
else if (!NO_BODY.has(method) && operation.requestBody) {
|