@kayjs/kay 2.0.0-beta.0 → 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 +10 -10
- package/src/build/files.ts +136 -0
- package/src/build/graph.ts +158 -0
- package/src/build/index.ts +204 -1460
- 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 +297 -94
- package/src/cli/new.ts +53 -32
- package/src/compiler/analyze.ts +666 -91
- package/src/compiler/dom.ts +808 -743
- 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 +329 -342
- package/src/context.ts +120 -0
- package/src/dev/index.ts +271 -75
- package/src/dom/action.ts +23 -13
- package/src/dom/extras.ts +121 -83
- package/src/dom/index.ts +114 -77
- package/src/dom/island.ts +71 -36
- package/src/dom/loop.ts +28 -20
- 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 +114 -18
- package/src/reactivity/index.ts +237 -213
- package/src/reactivity/selector.ts +11 -9
- package/src/reactivity/store.ts +36 -11
- package/src/server/action.ts +147 -76
- package/src/server/compress.ts +116 -96
- package/src/server/cookies.ts +46 -16
- package/src/server/env.ts +18 -13
- package/src/server/http.ts +107 -54
- package/src/server/index.ts +887 -870
- package/src/server/loader.ts +120 -0
- package/src/server/router.ts +22 -8
- package/src/server/security.ts +48 -58
- package/src/server/standard-schema.ts +42 -11
- package/src/server/static-dir.ts +144 -0
- package/src/ssr/context.ts +95 -10
- package/src/ssr/index.ts +167 -66
- package/src/style.ts +33 -0
- package/src/test/index.ts +2 -1
- package/src/unrenderable.ts +35 -0
package/src/server/http.ts
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
import type { BuildManifest } from '../build/index'
|
|
4
4
|
import type { KayPlugin, ServerErrorInfo } from '../plugins/index'
|
|
5
5
|
import type { Cookies } from './cookies'
|
|
6
|
-
import type { SecurityHeadersOptions } from './security'
|
|
6
|
+
import type { SecurityHeadersOptions, TrustProxy } from './security'
|
|
7
7
|
import { CookieStore } from './cookies'
|
|
8
|
-
import {
|
|
8
|
+
import { nodeEnv } from './env'
|
|
9
|
+
import { getClientIp, isHttpsRequest } from './security'
|
|
9
10
|
|
|
10
11
|
export const DEFAULT_MAX_REQUEST_BODY_SIZE = 1024 * 1024
|
|
11
12
|
|
|
@@ -17,11 +18,16 @@ export interface ServerOptions {
|
|
|
17
18
|
lang?: string | undefined
|
|
18
19
|
security?: SecurityHeadersOptions | false | undefined
|
|
19
20
|
trustedOrigins?: string[] | undefined
|
|
20
|
-
trustProxy?:
|
|
21
|
+
trustProxy?: TrustProxy | undefined
|
|
21
22
|
maxRequestBodySize?: number | undefined
|
|
23
|
+
/** Milliseconds a streamed `<Suspense>` boundary may take before it renders its error and the response ends (default 30000). */
|
|
24
|
+
streamTimeout?: number | undefined
|
|
22
25
|
compress?: boolean | undefined
|
|
26
|
+
dev?: boolean | undefined
|
|
23
27
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
24
28
|
plugins?: KayPlugin[] | undefined
|
|
29
|
+
/** @internal Imports a built server module by absolute path; `kay dev` passes one that keeps unchanged modules loaded. */
|
|
30
|
+
importModule?: ((path: string) => Promise<Record<string, unknown>>) | undefined
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
export interface RequestContext {
|
|
@@ -42,6 +48,8 @@ export type Middleware = (ctx: RequestContext, next: () => Promise<Response>) =>
|
|
|
42
48
|
export interface KayApp {
|
|
43
49
|
handle: (request: Request, remoteIp?: string) => Promise<Response>
|
|
44
50
|
serve: (port?: number, hostname?: string) => ReturnType<typeof Bun.serve>
|
|
51
|
+
/** Runs plugin shutdown hooks and releases timers/resources (safe to call multiple times). */
|
|
52
|
+
dispose?: () => Promise<void>
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
export type EmbeddedBody = Uint8Array<ArrayBuffer> | string
|
|
@@ -54,13 +62,19 @@ export interface EmbeddedServerConfig {
|
|
|
54
62
|
publicEtags?: Record<string, string> | undefined
|
|
55
63
|
prerendered: Record<string, string>
|
|
56
64
|
prerenderedFiles?: Record<string, string> | undefined
|
|
65
|
+
root?: string | undefined
|
|
66
|
+
outDir?: string | undefined
|
|
67
|
+
publicDir?: string | undefined
|
|
57
68
|
assetPrefix?: string | undefined
|
|
58
69
|
lang?: string | undefined
|
|
59
70
|
security?: SecurityHeadersOptions | false | undefined
|
|
60
71
|
trustedOrigins?: string[] | undefined
|
|
61
|
-
trustProxy?:
|
|
72
|
+
trustProxy?: TrustProxy | undefined
|
|
62
73
|
maxRequestBodySize?: number | undefined
|
|
74
|
+
/** Milliseconds a streamed `<Suspense>` boundary may take before it renders its error and the response ends (default 30000). */
|
|
75
|
+
streamTimeout?: number | undefined
|
|
63
76
|
compress?: boolean | undefined
|
|
77
|
+
dev?: boolean | undefined
|
|
64
78
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
65
79
|
plugins?: KayPlugin[] | undefined
|
|
66
80
|
}
|
|
@@ -110,8 +124,47 @@ export function extractRenderedIslands(html: string): { hasIslands: boolean, eag
|
|
|
110
124
|
}
|
|
111
125
|
|
|
112
126
|
export interface RequestContextOptions {
|
|
113
|
-
trustProxy?:
|
|
127
|
+
trustProxy?: TrustProxy | undefined
|
|
114
128
|
remoteIp?: string | undefined
|
|
129
|
+
/** The parsed `request.url`, when the caller already has it. */
|
|
130
|
+
url?: URL | undefined
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// None of these read the request, so every context shares them.
|
|
134
|
+
const json: RequestContext['json'] = (data, init) => {
|
|
135
|
+
const payload = JSON.stringify(data)
|
|
136
|
+
const headers = new Headers(init?.headers)
|
|
137
|
+
if (!headers.has('content-type'))
|
|
138
|
+
headers.set('content-type', 'application/json; charset=utf-8')
|
|
139
|
+
if (!headers.has('content-length'))
|
|
140
|
+
headers.set('content-length', String(Buffer.byteLength(payload, 'utf8')))
|
|
141
|
+
return new Response(payload, { ...init, headers })
|
|
142
|
+
}
|
|
143
|
+
const text: RequestContext['text'] = (body, init) => {
|
|
144
|
+
const headers = new Headers(init?.headers)
|
|
145
|
+
if (!headers.has('content-type'))
|
|
146
|
+
headers.set('content-type', 'text/plain; charset=utf-8')
|
|
147
|
+
if (!headers.has('content-length'))
|
|
148
|
+
headers.set('content-length', String(Buffer.byteLength(body, 'utf8')))
|
|
149
|
+
return new Response(body, { ...init, headers })
|
|
150
|
+
}
|
|
151
|
+
const redirect: RequestContext['redirect'] = (location, status = 302) => new Response(null, { status, headers: { location } })
|
|
152
|
+
const deny: RequestContext['deny'] = (status, message, fieldErrors) => Response.json({ ok: false, status, error: message, fieldErrors }, { status })
|
|
153
|
+
|
|
154
|
+
/** RFC 9110 weak ETag comparison supporting `*`, comma-separated lists, and `W/` prefixes. */
|
|
155
|
+
export function etagMatches(ifNoneMatch: string | null, etag: string): boolean {
|
|
156
|
+
if (!ifNoneMatch)
|
|
157
|
+
return false
|
|
158
|
+
const trimmed = ifNoneMatch.trim()
|
|
159
|
+
if (trimmed === '*')
|
|
160
|
+
return true
|
|
161
|
+
const stripWeak = (v: string) => (v.startsWith('W/') || v.startsWith('w/') ? v.slice(2) : v)
|
|
162
|
+
const target = stripWeak(etag)
|
|
163
|
+
for (const part of trimmed.split(',')) {
|
|
164
|
+
if (stripWeak(part.trim()) === target)
|
|
165
|
+
return true
|
|
166
|
+
}
|
|
167
|
+
return false
|
|
115
168
|
}
|
|
116
169
|
|
|
117
170
|
export function makeRequestContext(
|
|
@@ -119,41 +172,42 @@ export function makeRequestContext(
|
|
|
119
172
|
params: Record<string, string>,
|
|
120
173
|
opts?: RequestContextOptions,
|
|
121
174
|
): { ctx: RequestContext, cookieStore: CookieStore } {
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
175
|
+
const trustProxy = opts?.trustProxy ?? false
|
|
176
|
+
const remoteIp = opts?.remoteIp
|
|
177
|
+
const cookieStore = new CookieStore(request.headers.get('cookie'), isHttpsRequest(request, trustProxy))
|
|
178
|
+
let cachedIp: string | undefined
|
|
179
|
+
let ipComputed = false
|
|
127
180
|
const ctx: RequestContext = {
|
|
128
181
|
request,
|
|
129
|
-
url,
|
|
182
|
+
url: opts?.url ?? new URL(request.url),
|
|
130
183
|
params,
|
|
131
184
|
cookies: cookieStore,
|
|
132
185
|
locals: {},
|
|
133
|
-
ip
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (!headers.has('content-length'))
|
|
140
|
-
headers.set('content-length', String(Buffer.byteLength(payload, 'utf8')))
|
|
141
|
-
return new Response(payload, { ...init, headers })
|
|
186
|
+
get ip() {
|
|
187
|
+
if (!ipComputed) {
|
|
188
|
+
ipComputed = true
|
|
189
|
+
cachedIp = getClientIp(request, trustProxy, remoteIp)
|
|
190
|
+
}
|
|
191
|
+
return cachedIp
|
|
142
192
|
},
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
headers.set('content-type', 'text/plain; charset=utf-8')
|
|
147
|
-
if (!headers.has('content-length'))
|
|
148
|
-
headers.set('content-length', String(Buffer.byteLength(body, 'utf8')))
|
|
149
|
-
return new Response(body, { ...init, headers })
|
|
193
|
+
set ip(val: string | undefined) {
|
|
194
|
+
ipComputed = true
|
|
195
|
+
cachedIp = val
|
|
150
196
|
},
|
|
151
|
-
|
|
152
|
-
|
|
197
|
+
json,
|
|
198
|
+
text,
|
|
199
|
+
redirect,
|
|
200
|
+
deny,
|
|
153
201
|
}
|
|
154
202
|
return { ctx, cookieStore }
|
|
155
203
|
}
|
|
156
204
|
|
|
205
|
+
/** True when the client sent or asked for JSON: such requests get JSON errors, not pages. */
|
|
206
|
+
export function wantsJson(request: Request): boolean {
|
|
207
|
+
return (request.headers.get('accept') ?? '').includes('application/json')
|
|
208
|
+
|| (request.headers.get('content-type') ?? '').includes('application/json')
|
|
209
|
+
}
|
|
210
|
+
|
|
157
211
|
/** Enforces `maxBytes` on incoming request bodies (both Content-Length and streaming body reads). */
|
|
158
212
|
export async function enforceRequestBodyLimit(
|
|
159
213
|
request: Request,
|
|
@@ -185,7 +239,10 @@ export async function enforceRequestBodyLimit(
|
|
|
185
239
|
break
|
|
186
240
|
total += value.byteLength
|
|
187
241
|
if (total > maxBytes) {
|
|
188
|
-
|
|
242
|
+
try {
|
|
243
|
+
await reader.cancel()
|
|
244
|
+
}
|
|
245
|
+
catch {}
|
|
189
246
|
return { ok: false }
|
|
190
247
|
}
|
|
191
248
|
chunks.push(value)
|
|
@@ -203,12 +260,14 @@ export async function enforceRequestBodyLimit(
|
|
|
203
260
|
offset += chunk.byteLength
|
|
204
261
|
}
|
|
205
262
|
|
|
263
|
+
headers.delete('transfer-encoding')
|
|
206
264
|
return {
|
|
207
265
|
ok: true,
|
|
208
266
|
request: new Request(request.url, {
|
|
209
267
|
method: request.method,
|
|
210
268
|
headers,
|
|
211
269
|
body: combined,
|
|
270
|
+
signal: request.signal,
|
|
212
271
|
}),
|
|
213
272
|
}
|
|
214
273
|
}
|
|
@@ -217,9 +276,11 @@ export function applyHstsHeader(
|
|
|
217
276
|
res: Response,
|
|
218
277
|
request: Request,
|
|
219
278
|
hstsValue: string | undefined,
|
|
220
|
-
trustProxy:
|
|
279
|
+
trustProxy: TrustProxy,
|
|
280
|
+
isDev = false,
|
|
221
281
|
): Response {
|
|
222
|
-
|
|
282
|
+
const env = nodeEnv()
|
|
283
|
+
if (hstsValue === undefined || isDev || env === 'development' || env === 'test' || !isHttpsRequest(request, trustProxy))
|
|
223
284
|
return res
|
|
224
285
|
if (res.headers.has('strict-transport-security'))
|
|
225
286
|
return res
|
|
@@ -234,40 +295,32 @@ export function applyHstsHeader(
|
|
|
234
295
|
}
|
|
235
296
|
}
|
|
236
297
|
|
|
298
|
+
/** Adds the pending cookies (when any) and the security headers the response does not set itself. */
|
|
237
299
|
export function finalizeResponse(
|
|
238
300
|
res: Response,
|
|
239
|
-
cookieStore: CookieStore,
|
|
240
|
-
|
|
301
|
+
cookieStore: CookieStore | undefined,
|
|
302
|
+
entries: readonly [string, string][],
|
|
241
303
|
): Response {
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
: buildSecurityHeaderEntries(securityEntries as SecurityHeadersOptions | false | undefined)
|
|
245
|
-
|
|
246
|
-
if (!cookieStore.hasChanges && entries.length === 0)
|
|
304
|
+
const hasCookies = cookieStore?.hasChanges === true
|
|
305
|
+
if (!hasCookies && entries.length === 0)
|
|
247
306
|
return res
|
|
248
307
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
cookieStore.applyTo(h)
|
|
254
|
-
for (let i = 0; i < entries.length; i++) {
|
|
255
|
-
const [k, v] = entries[i]!
|
|
308
|
+
const apply = (h: Headers): void => {
|
|
309
|
+
if (hasCookies)
|
|
310
|
+
cookieStore!.applyTo(h)
|
|
311
|
+
for (const [k, v] of entries) {
|
|
256
312
|
if (!h.has(k))
|
|
257
313
|
h.set(k, v)
|
|
258
314
|
}
|
|
315
|
+
}
|
|
316
|
+
// Mutate in place (no copy in Bun); a response with immutable headers (e.g. a proxied upstream fetch) is rebuilt.
|
|
317
|
+
try {
|
|
318
|
+
apply(res.headers)
|
|
259
319
|
return res
|
|
260
320
|
}
|
|
261
321
|
catch {
|
|
262
|
-
// Fallback for immutable headers (e.g. proxied upstream fetch Response)
|
|
263
322
|
const headers = new Headers(res.headers)
|
|
264
|
-
|
|
265
|
-
cookieStore.applyTo(headers)
|
|
266
|
-
for (let i = 0; i < entries.length; i++) {
|
|
267
|
-
const [k, v] = entries[i]!
|
|
268
|
-
if (!headers.has(k))
|
|
269
|
-
headers.set(k, v)
|
|
270
|
-
}
|
|
323
|
+
apply(headers)
|
|
271
324
|
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
|
|
272
325
|
}
|
|
273
326
|
}
|