@kayjs/kay 2.0.0-alpha.8 → 2.0.0-beta.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.
- package/package.json +1 -1
- package/src/attrs.ts +35 -0
- package/src/build/bundle.ts +442 -0
- package/src/build/compile.ts +20 -11
- package/src/build/files.ts +136 -0
- package/src/build/graph.ts +158 -0
- package/src/build/index.ts +231 -1296
- package/src/build/islands.ts +177 -0
- package/src/build/manifest.ts +224 -0
- package/src/build/plugin-host.ts +184 -0
- package/src/build/prerender.ts +93 -0
- package/src/build/report.ts +157 -0
- package/src/build/resolve.ts +58 -0
- package/src/build/routes.ts +135 -0
- package/src/build/static.ts +73 -0
- package/src/cli/check.ts +228 -114
- package/src/cli/index.ts +298 -79
- package/src/cli/new.ts +67 -35
- package/src/compiler/analyze.ts +666 -91
- package/src/compiler/dom.ts +822 -629
- package/src/compiler/mistakes.ts +354 -0
- package/src/compiler/patch.ts +1 -1
- package/src/compiler/server-only.ts +164 -21
- package/src/compiler/shared.ts +442 -0
- package/src/compiler/ssr.ts +390 -324
- package/src/context.ts +120 -0
- package/src/dev/index.ts +291 -76
- package/src/dom/action.ts +23 -13
- package/src/dom/extras.ts +157 -84
- package/src/dom/index.ts +154 -33
- package/src/dom/island.ts +117 -39
- package/src/dom/loop.ts +34 -21
- package/src/index.ts +2 -1
- package/src/jsx-runtime.ts +11 -0
- package/src/plugins/context.ts +57 -0
- package/src/plugins/index.ts +120 -17
- package/src/reactivity/index.ts +250 -157
- package/src/reactivity/selector.ts +11 -9
- package/src/reactivity/store.ts +37 -12
- package/src/server/action.ts +217 -74
- package/src/server/compress.ts +155 -56
- package/src/server/cookies.ts +59 -22
- package/src/server/env.ts +18 -13
- package/src/server/http.ts +201 -33
- package/src/server/index.ts +964 -737
- package/src/server/loader.ts +120 -0
- package/src/server/router.ts +27 -8
- package/src/server/security.ts +87 -39
- package/src/server/standard-schema.ts +42 -11
- package/src/server/static-dir.ts +144 -0
- package/src/ssr/context.ts +96 -10
- package/src/ssr/index.ts +225 -56
- package/src/style.ts +33 -0
- package/src/test/index.ts +5 -0
- package/src/unrenderable.ts +35 -0
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { createContext, Head, provideContext, Suspense, useContext, useId } from './context'
|
|
2
|
+
export type { Context } from './context'
|
|
1
3
|
export {
|
|
2
4
|
batch,
|
|
3
5
|
computed,
|
|
@@ -13,7 +15,6 @@ export type { Owner, ReadonlySignal, Signal } from './reactivity/index'
|
|
|
13
15
|
export { selector } from './reactivity/selector'
|
|
14
16
|
export type { Selector } from './reactivity/selector'
|
|
15
17
|
export { snapshot, store } from './reactivity/store'
|
|
16
|
-
export { Head, Suspense } from './ssr/context'
|
|
17
18
|
export type { Component, Html, HydrateStrategy, KayChild } from './jsx-runtime'
|
|
18
19
|
|
|
19
20
|
import type { Component, HydrateStrategy } from './jsx-runtime'
|
package/src/jsx-runtime.ts
CHANGED
|
@@ -20,8 +20,12 @@ export type KayChild =
|
|
|
20
20
|
| Node
|
|
21
21
|
| KayChild[]
|
|
22
22
|
|
|
23
|
+
declare const htmlBrand: unique symbol
|
|
24
|
+
|
|
25
|
+
/** HTML kay injects unescaped; only `raw()` makes one, so a plain `{ __html }` object is not an Html. */
|
|
23
26
|
export interface Html {
|
|
24
27
|
readonly __html: string
|
|
28
|
+
readonly [htmlBrand]: true
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
export type Component<P = Record<string, unknown>> = (props: P) => KayChild | Promise<KayChild>
|
|
@@ -58,6 +62,13 @@ interface CommonAttributes {
|
|
|
58
62
|
key?: string | number | undefined
|
|
59
63
|
ref?: ((el: Element) => void) | undefined
|
|
60
64
|
$hydrate?: HydrateStrategy | undefined
|
|
65
|
+
innerHTML?: string | Html | null | undefined
|
|
66
|
+
// Spellings from React and Solid that kay refuses at compile time, refused here first.
|
|
67
|
+
dangerouslySetInnerHTML?: never
|
|
68
|
+
classList?: never
|
|
69
|
+
defaultValue?: never
|
|
70
|
+
defaultChecked?: never
|
|
71
|
+
onDoubleClick?: never
|
|
61
72
|
// Events
|
|
62
73
|
onClick?: ((e: MouseEvent) => void) | undefined
|
|
63
74
|
onDblClick?: ((e: MouseEvent) => void) | undefined
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Per-build / per-server scoped runtime context (AsyncLocalStorage-backed, zero mutable process globals).
|
|
2
|
+
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
4
|
+
|
|
5
|
+
export interface AppRuntimeContext {
|
|
6
|
+
readonly root: string
|
|
7
|
+
readonly outDir: string
|
|
8
|
+
readonly publicDir: string
|
|
9
|
+
readonly assetPrefix: string
|
|
10
|
+
readonly dev: boolean
|
|
11
|
+
provide<T>(key: string, value: T): void
|
|
12
|
+
inject<T>(key: string): T | undefined
|
|
13
|
+
registerPublicFile(relPath: string): void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CreateAppContextOptions {
|
|
17
|
+
root: string
|
|
18
|
+
outDir: string
|
|
19
|
+
publicDir: string
|
|
20
|
+
assetPrefix: string
|
|
21
|
+
dev: boolean
|
|
22
|
+
onRegisterPublicFile?: ((relPath: string) => void) | undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const APP_STORAGE_KEY = Symbol.for('kay.app.storage')
|
|
26
|
+
const appStorage: AsyncLocalStorage<AppRuntimeContext> = (
|
|
27
|
+
(globalThis as Record<symbol, AsyncLocalStorage<AppRuntimeContext> | undefined>)[APP_STORAGE_KEY]
|
|
28
|
+
??= new AsyncLocalStorage<AppRuntimeContext>()
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
export function createAppContext(options: CreateAppContextOptions): AppRuntimeContext {
|
|
32
|
+
const services = new Map<string, unknown>()
|
|
33
|
+
return {
|
|
34
|
+
root: options.root,
|
|
35
|
+
outDir: options.outDir,
|
|
36
|
+
publicDir: options.publicDir,
|
|
37
|
+
assetPrefix: options.assetPrefix,
|
|
38
|
+
dev: options.dev,
|
|
39
|
+
provide<T>(key: string, value: T): void {
|
|
40
|
+
services.set(key, value)
|
|
41
|
+
},
|
|
42
|
+
inject<T>(key: string): T | undefined {
|
|
43
|
+
return services.get(key) as T | undefined
|
|
44
|
+
},
|
|
45
|
+
registerPublicFile(relPath: string): void {
|
|
46
|
+
options.onRegisterPublicFile?.(relPath)
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function runWithAppContext<T>(ctx: AppRuntimeContext, fn: () => T): T {
|
|
52
|
+
return appStorage.run(ctx, fn)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function getAppContext(): AppRuntimeContext | undefined {
|
|
56
|
+
return appStorage.getStore()
|
|
57
|
+
}
|
package/src/plugins/index.ts
CHANGED
|
@@ -4,16 +4,26 @@
|
|
|
4
4
|
|
|
5
5
|
import type { BunPlugin } from 'bun'
|
|
6
6
|
import type { RouteManifestEntry } from '../build/index'
|
|
7
|
+
import type { ActionResult, ServerAction } from '../server/action'
|
|
7
8
|
import type { Middleware, RequestContext } from '../server/index'
|
|
8
|
-
import type { SecurityHeadersOptions } from '../server/security'
|
|
9
|
-
import {
|
|
9
|
+
import type { SecurityHeadersOptions, TrustProxy } from '../server/security'
|
|
10
|
+
import type { AppRuntimeContext } from './context'
|
|
11
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
10
12
|
import { join, resolve } from 'node:path'
|
|
13
|
+
import { createAppContext, getAppContext, runWithAppContext } from './context'
|
|
14
|
+
|
|
15
|
+
export { createAppContext, getAppContext, runWithAppContext } from './context'
|
|
16
|
+
export type { AppRuntimeContext, CreateAppContextOptions } from './context'
|
|
17
|
+
|
|
18
|
+
export type BuildPluginTarget = 'client' | 'server' | 'compile'
|
|
11
19
|
|
|
12
20
|
export interface BuildPluginContext {
|
|
13
21
|
readonly root: string
|
|
14
22
|
readonly outDir: string
|
|
15
23
|
readonly assetPrefix: string
|
|
16
24
|
readonly minify: boolean
|
|
25
|
+
readonly isStatic: boolean
|
|
26
|
+
readonly dev: boolean
|
|
17
27
|
readonly routes: readonly RouteManifestEntry[]
|
|
18
28
|
/** Emit an immutable build asset into dist/client/<relPath>; returns its public URL (`/_kay/<relPath>`). */
|
|
19
29
|
emitAsset(relPath: string, content: string | Uint8Array): string
|
|
@@ -27,6 +37,35 @@ export interface BuildPluginContext {
|
|
|
27
37
|
addHead(html: string): void
|
|
28
38
|
/** Add raw HTML to <head> on a specific route path only. */
|
|
29
39
|
addRouteHead(routePath: string, html: string): void
|
|
40
|
+
/** Register an explicit build-scoped service for other plugins or render-time helpers. */
|
|
41
|
+
provide<T>(key: string, value: T): void
|
|
42
|
+
/** Look up a build-scoped service registered by another plugin. */
|
|
43
|
+
inject<T>(key: string): T | undefined
|
|
44
|
+
/** Persist JSON-serializable build data into `manifest.pluginData[plugin.name]` for cold-start servers and `--compile`. */
|
|
45
|
+
setData(data: Record<string, unknown>): void
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface FinalizeBuildContext {
|
|
49
|
+
readonly root: string
|
|
50
|
+
readonly outDir: string
|
|
51
|
+
readonly assetPrefix: string
|
|
52
|
+
readonly minify: boolean
|
|
53
|
+
readonly isStatic: boolean
|
|
54
|
+
readonly dev: boolean
|
|
55
|
+
/** Route manifest entries with `prerendered` maps populated after Step 3 prerendering. */
|
|
56
|
+
readonly routes: readonly RouteManifestEntry[]
|
|
57
|
+
/** Relative paths of all public files known so far (including files registered during prerender). */
|
|
58
|
+
readonly publicFiles: readonly string[]
|
|
59
|
+
/** Emit an immutable build asset into dist/client/<relPath>; returns its public URL (`/_kay/<relPath>`). */
|
|
60
|
+
emitAsset(relPath: string, content: string | Uint8Array): string
|
|
61
|
+
/** Emit a static file into dist/public/<relPath> served at `/<relPath>` (included in `--static` and `--compile`). */
|
|
62
|
+
emitPublic(relPath: string, content: string | Uint8Array): string
|
|
63
|
+
/** Register a build-scoped service. */
|
|
64
|
+
provide<T>(key: string, value: T): void
|
|
65
|
+
/** Look up a build-scoped service. */
|
|
66
|
+
inject<T>(key: string): T | undefined
|
|
67
|
+
/** Merge/persist JSON-serializable build data into `manifest.pluginData[plugin.name]`. */
|
|
68
|
+
setData(data: Record<string, unknown>): void
|
|
30
69
|
}
|
|
31
70
|
|
|
32
71
|
export interface CheckPluginContext {
|
|
@@ -36,13 +75,46 @@ export interface CheckPluginContext {
|
|
|
36
75
|
error(message: string): void
|
|
37
76
|
}
|
|
38
77
|
|
|
78
|
+
export interface PluginRouteOptions {
|
|
79
|
+
/** Enable CSRF Origin/Sec-Fetch-Site verification on unsafe methods (default: true). Set `false` to opt out (e.g. webhooks, bearer-token APIs). */
|
|
80
|
+
csrf?: boolean | undefined
|
|
81
|
+
/** Additional trusted origins merged with `KayConfig.trustedOrigins` for this route's CSRF check. */
|
|
82
|
+
trustedOrigins?: readonly string[] | undefined
|
|
83
|
+
/** Override `KayConfig.trustProxy` for this route's CSRF check. */
|
|
84
|
+
trustProxy?: TrustProxy | undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface MiddlewareOptions {
|
|
88
|
+
/** `'all'` (default) runs on every route; `'dynamic'` skips static/prerendered fast paths; a string prefix scopes to matching pathnames. */
|
|
89
|
+
scope?: 'all' | 'dynamic' | string | undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
39
92
|
export interface ServerPluginAPI {
|
|
40
|
-
|
|
41
|
-
|
|
93
|
+
readonly root: string
|
|
94
|
+
readonly outDir: string
|
|
95
|
+
readonly assetPrefix: string
|
|
96
|
+
readonly dev: boolean
|
|
97
|
+
/** Readonly server security configuration so plugins inherit `trustProxy` / `trustedOrigins` without overriding them. */
|
|
98
|
+
readonly trustProxy: TrustProxy
|
|
99
|
+
readonly trustedOrigins: readonly string[]
|
|
100
|
+
/** Readonly runtime config forwarded from `kay.config.ts` (minus build-only keys). */
|
|
101
|
+
readonly config?: Readonly<Record<string, unknown>> | undefined
|
|
102
|
+
/** Register global (or scoped) middleware that runs before route handlers. */
|
|
103
|
+
use(middleware: Middleware, options?: MiddlewareOptions): void
|
|
42
104
|
/** Register a custom HTTP route (e.g. GET /sitemap.xml, POST /auth/callback). */
|
|
43
|
-
route(method: string, path: string, handler: (ctx: RequestContext) => Response | Promise<Response
|
|
105
|
+
route(method: string, path: string, handler: (ctx: RequestContext) => Response | Promise<Response>, options?: PluginRouteOptions): void
|
|
44
106
|
/** Rewrite an incoming pathname before route matching (e.g. strip /fr locale prefix). */
|
|
45
107
|
rewrite(fn: (pathname: string, ctx: RequestContext) => string | undefined): void
|
|
108
|
+
/** Register a server-scoped service for other plugins or render-time helpers (isolated per server instance). */
|
|
109
|
+
provide<T>(key: string, value: T): void
|
|
110
|
+
/** Look up a server-scoped service registered by another plugin. */
|
|
111
|
+
inject<T>(key: string): T | undefined
|
|
112
|
+
/** Read persisted build data from `manifest.pluginData` (defaults to the current plugin's name). */
|
|
113
|
+
getBuildData<T = Record<string, unknown>>(pluginName?: string): T | undefined
|
|
114
|
+
/** Register a shutdown cleanup callback run when `app.dispose()` is called (dev reloads, tests, graceful exit). */
|
|
115
|
+
onShutdown(fn: () => void | Promise<void>): void
|
|
116
|
+
/** Invoke a ServerAction with Standard Schema validation and directory-scoped `_middleware.ts` checks. */
|
|
117
|
+
invokeAction<I, O>(action: ServerAction<I, O>, input: I, ctx: RequestContext): Promise<ActionResult<O>>
|
|
46
118
|
}
|
|
47
119
|
|
|
48
120
|
export interface ServerErrorInfo {
|
|
@@ -53,19 +125,23 @@ export interface ServerErrorInfo {
|
|
|
53
125
|
|
|
54
126
|
export interface KayPlugin {
|
|
55
127
|
name: string
|
|
56
|
-
/** Register server routes, global middleware, or
|
|
128
|
+
/** Register server routes, global middleware, URL rewrites, or runtime services. */
|
|
57
129
|
server?: (api: ServerPluginAPI) => void | Promise<void>
|
|
130
|
+
/** Optional cleanup hook called when a server instance is disposed (dev reloads, tests, graceful shutdown). */
|
|
131
|
+
dispose?: () => void | Promise<void>
|
|
58
132
|
/** Error hook called whenever a route, action, or plugin handler throws an unhandled error. */
|
|
59
133
|
onError?: (info: ServerErrorInfo) => void
|
|
60
134
|
/** Build hook run after route compilation, before prerender / --static / --compile. */
|
|
61
135
|
build?: (ctx: BuildPluginContext) => void | Promise<void>
|
|
62
|
-
/**
|
|
136
|
+
/** Post-prerender build hook run after `route.prerendered` is populated, before `--static` / `--compile` / final manifest write. */
|
|
137
|
+
afterPrerender?: (ctx: FinalizeBuildContext) => void | Promise<void>
|
|
138
|
+
/** Code transform run during client & server compilation (`.ts` and `.tsx`). */
|
|
63
139
|
transform?: (code: string, id: string, target: 'server' | 'client') => string | undefined | void
|
|
64
|
-
/** Native Bun.build plugins (e.g. bun-plugin-tailwind, images, icons). */
|
|
65
|
-
bunPlugins?: BunPlugin[] | ((info: { root: string, assetPrefix: string, minify: boolean }) => BunPlugin[]) | undefined
|
|
140
|
+
/** Native Bun.build plugins (e.g. bun-plugin-tailwind, images, icons). Factory is invoked per build target. */
|
|
141
|
+
bunPlugins?: BunPlugin[] | ((info: { root: string, assetPrefix: string, minify: boolean, target: BuildPluginTarget }) => BunPlugin[]) | undefined
|
|
66
142
|
/** Global CSS files to bundle and inject as <link rel="stylesheet"> in <head>. */
|
|
67
143
|
css?: string[] | undefined
|
|
68
|
-
/** Post-process rendered HTML before sending. */
|
|
144
|
+
/** Post-process rendered HTML before sending (applies to pages, `<Suspense>` shells, `_error.tsx`, and `_404.tsx`). */
|
|
69
145
|
transformHtml?: (html: string, ctx: RequestContext) => string | Promise<string>
|
|
70
146
|
/** Expand or filter prerender URLs for a route (e.g. @kayjs/i18n expanding `/posts/a` -> `['/posts/a', '/fr/posts/a']`). */
|
|
71
147
|
prerenderPaths?: (paths: string[], route: RouteManifestEntry) => string[] | Promise<string[]>
|
|
@@ -88,6 +164,10 @@ export interface KayConfig {
|
|
|
88
164
|
assetPrefix?: string | undefined
|
|
89
165
|
security?: SecurityHeadersOptions | false | undefined
|
|
90
166
|
trustedOrigins?: string[] | undefined
|
|
167
|
+
trustProxy?: TrustProxy | undefined
|
|
168
|
+
maxRequestBodySize?: number | undefined
|
|
169
|
+
/** Milliseconds a streamed `<Suspense>` boundary may take before it renders its error and the response ends (default 30000). */
|
|
170
|
+
streamTimeout?: number | undefined
|
|
91
171
|
compress?: boolean | undefined
|
|
92
172
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
93
173
|
plugins?: KayPlugin[] | undefined
|
|
@@ -97,22 +177,45 @@ export function defineConfig(config: KayConfig): KayConfig {
|
|
|
97
177
|
return config
|
|
98
178
|
}
|
|
99
179
|
|
|
180
|
+
/** Forwards every runtime key from `kay.config.ts`, stripping build/CLI-only keys (`routesDir`, `outDir`, `publicDir`, `port`). */
|
|
181
|
+
export function pickRuntimeConfig(config: KayConfig | undefined): Omit<KayConfig, 'routesDir' | 'outDir' | 'publicDir' | 'port'> {
|
|
182
|
+
if (config === undefined)
|
|
183
|
+
return {}
|
|
184
|
+
const { routesDir: _r, outDir: _o, publicDir: _p, port: _pt, ...runtime } = config
|
|
185
|
+
return runtime
|
|
186
|
+
}
|
|
187
|
+
|
|
100
188
|
export function definePlugin(plugin: KayPlugin): KayPlugin {
|
|
101
189
|
return plugin
|
|
102
190
|
}
|
|
103
191
|
|
|
104
|
-
const KAY_ROOT_KEY = Symbol.for('kay.root')
|
|
105
192
|
let configReloadSeq = 0
|
|
106
193
|
|
|
107
|
-
|
|
194
|
+
// In dev the query follows the file content: a save elsewhere reuses the loaded config and its plugin instances instead of re-running every factory.
|
|
195
|
+
function configSpecifier(configPath: string, fresh: boolean): string {
|
|
196
|
+
if (fresh)
|
|
197
|
+
return `${configPath}?v=fresh-${++configReloadSeq}`
|
|
198
|
+
if (process.env.KAY_DEV === '1')
|
|
199
|
+
return `${configPath}?v=${Bun.hash(readFileSync(configPath)).toString(36)}`
|
|
200
|
+
return configPath
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function loadConfig(root: string, options?: { fresh?: boolean, appContext?: AppRuntimeContext }): Promise<KayConfig> {
|
|
108
204
|
const absRoot = resolve(root)
|
|
109
|
-
;(globalThis as Record<symbol, unknown>)[KAY_ROOT_KEY] = absRoot
|
|
110
205
|
const configPath = join(absRoot, 'kay.config.ts')
|
|
111
206
|
if (!existsSync(configPath))
|
|
112
207
|
return {}
|
|
113
|
-
const spec = options?.fresh
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
208
|
+
const spec = configSpecifier(configPath, options?.fresh === true)
|
|
209
|
+
const active = options?.appContext ?? getAppContext()
|
|
210
|
+
const ctx = active !== undefined && active.root === absRoot
|
|
211
|
+
? active
|
|
212
|
+
: createAppContext({
|
|
213
|
+
root: absRoot,
|
|
214
|
+
outDir: join(absRoot, 'dist'),
|
|
215
|
+
publicDir: join(absRoot, 'dist', 'public'),
|
|
216
|
+
assetPrefix: '/_kay',
|
|
217
|
+
dev: process.env.KAY_DEV === '1',
|
|
218
|
+
})
|
|
219
|
+
const mod = await runWithAppContext(ctx, () => import(spec)) as { default?: KayConfig }
|
|
117
220
|
return mod.default ?? {}
|
|
118
221
|
}
|