@kayjs/kay 2.0.0-alpha.8 → 2.0.0-beta.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/package.json +1 -1
- package/src/build/compile.ts +11 -2
- package/src/build/index.ts +233 -42
- package/src/cli/index.ts +27 -11
- package/src/cli/new.ts +15 -4
- package/src/compiler/dom.ts +141 -13
- package/src/compiler/ssr.ts +101 -22
- package/src/dev/index.ts +26 -7
- package/src/dom/extras.ts +79 -44
- package/src/dom/index.ts +90 -6
- package/src/dom/island.ts +67 -24
- package/src/dom/loop.ts +6 -1
- package/src/plugins/index.ts +8 -1
- package/src/reactivity/index.ts +80 -11
- package/src/reactivity/store.ts +1 -1
- package/src/server/action.ts +98 -26
- package/src/server/compress.ts +87 -8
- package/src/server/cookies.ts +15 -8
- package/src/server/http.ts +121 -6
- package/src/server/index.ts +446 -236
- package/src/server/router.ts +6 -1
- package/src/server/security.ts +76 -18
- package/src/ssr/context.ts +2 -1
- package/src/ssr/index.ts +88 -20
- package/src/test/index.ts +4 -0
package/src/server/http.ts
CHANGED
|
@@ -5,7 +5,9 @@ import type { KayPlugin, ServerErrorInfo } from '../plugins/index'
|
|
|
5
5
|
import type { Cookies } from './cookies'
|
|
6
6
|
import type { SecurityHeadersOptions } from './security'
|
|
7
7
|
import { CookieStore } from './cookies'
|
|
8
|
-
import { buildSecurityHeaderEntries } from './security'
|
|
8
|
+
import { buildSecurityHeaderEntries, getClientIp, isHttpsRequest } from './security'
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_MAX_REQUEST_BODY_SIZE = 1024 * 1024
|
|
9
11
|
|
|
10
12
|
export interface ServerOptions {
|
|
11
13
|
root: string
|
|
@@ -15,6 +17,8 @@ export interface ServerOptions {
|
|
|
15
17
|
lang?: string | undefined
|
|
16
18
|
security?: SecurityHeadersOptions | false | undefined
|
|
17
19
|
trustedOrigins?: string[] | undefined
|
|
20
|
+
trustProxy?: boolean | undefined
|
|
21
|
+
maxRequestBodySize?: number | undefined
|
|
18
22
|
compress?: boolean | undefined
|
|
19
23
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
20
24
|
plugins?: KayPlugin[] | undefined
|
|
@@ -26,6 +30,7 @@ export interface RequestContext {
|
|
|
26
30
|
params: Record<string, string>
|
|
27
31
|
cookies: Cookies
|
|
28
32
|
locals: Record<string, unknown>
|
|
33
|
+
ip?: string | undefined
|
|
29
34
|
json: (data: unknown, init?: ResponseInit) => Response
|
|
30
35
|
text: (body: string, init?: ResponseInit) => Response
|
|
31
36
|
redirect: (location: string, status?: 301 | 302 | 303 | 307 | 308) => Response
|
|
@@ -35,7 +40,7 @@ export interface RequestContext {
|
|
|
35
40
|
export type Middleware = (ctx: RequestContext, next: () => Promise<Response>) => Response | Promise<Response>
|
|
36
41
|
|
|
37
42
|
export interface KayApp {
|
|
38
|
-
handle: (request: Request) => Promise<Response>
|
|
43
|
+
handle: (request: Request, remoteIp?: string) => Promise<Response>
|
|
39
44
|
serve: (port?: number, hostname?: string) => ReturnType<typeof Bun.serve>
|
|
40
45
|
}
|
|
41
46
|
|
|
@@ -53,6 +58,8 @@ export interface EmbeddedServerConfig {
|
|
|
53
58
|
lang?: string | undefined
|
|
54
59
|
security?: SecurityHeadersOptions | false | undefined
|
|
55
60
|
trustedOrigins?: string[] | undefined
|
|
61
|
+
trustProxy?: boolean | undefined
|
|
62
|
+
maxRequestBodySize?: number | undefined
|
|
56
63
|
compress?: boolean | undefined
|
|
57
64
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
58
65
|
plugins?: KayPlugin[] | undefined
|
|
@@ -102,23 +109,131 @@ export function extractRenderedIslands(html: string): { hasIslands: boolean, eag
|
|
|
102
109
|
return { hasIslands, eagerChunks: [...eager] }
|
|
103
110
|
}
|
|
104
111
|
|
|
105
|
-
export
|
|
112
|
+
export interface RequestContextOptions {
|
|
113
|
+
trustProxy?: boolean | undefined
|
|
114
|
+
remoteIp?: string | undefined
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function makeRequestContext(
|
|
118
|
+
request: Request,
|
|
119
|
+
params: Record<string, string>,
|
|
120
|
+
opts?: RequestContextOptions,
|
|
121
|
+
): { ctx: RequestContext, cookieStore: CookieStore } {
|
|
106
122
|
const url = new URL(request.url)
|
|
107
|
-
const
|
|
123
|
+
const trustProxy = opts?.trustProxy === true
|
|
124
|
+
const isHttps = isHttpsRequest(request, trustProxy)
|
|
125
|
+
const cookieStore = new CookieStore(request.headers.get('cookie'), isHttps)
|
|
126
|
+
const ip = getClientIp(request, trustProxy, opts?.remoteIp)
|
|
108
127
|
const ctx: RequestContext = {
|
|
109
128
|
request,
|
|
110
129
|
url,
|
|
111
130
|
params,
|
|
112
131
|
cookies: cookieStore,
|
|
113
132
|
locals: {},
|
|
114
|
-
|
|
115
|
-
|
|
133
|
+
ip,
|
|
134
|
+
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
|
+
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
|
+
},
|
|
116
151
|
redirect: (location, status = 302) => new Response(null, { status, headers: { location } }),
|
|
117
152
|
deny: (status, message, fieldErrors) => Response.json({ ok: false, status, error: message, fieldErrors }, { status }),
|
|
118
153
|
}
|
|
119
154
|
return { ctx, cookieStore }
|
|
120
155
|
}
|
|
121
156
|
|
|
157
|
+
/** Enforces `maxBytes` on incoming request bodies (both Content-Length and streaming body reads). */
|
|
158
|
+
export async function enforceRequestBodyLimit(
|
|
159
|
+
request: Request,
|
|
160
|
+
maxBytes: number,
|
|
161
|
+
): Promise<{ ok: true, request: Request } | { ok: false }> {
|
|
162
|
+
if (request.method === 'GET' || request.method === 'HEAD' || !Number.isFinite(maxBytes) || maxBytes <= 0)
|
|
163
|
+
return { ok: true, request }
|
|
164
|
+
|
|
165
|
+
const clHeader = request.headers.get('content-length')
|
|
166
|
+
if (clHeader !== null) {
|
|
167
|
+
const cl = Number(clHeader)
|
|
168
|
+
if (Number.isFinite(cl) && cl > maxBytes)
|
|
169
|
+
return { ok: false }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Capture headers BEFORE accessing request.body so Bun preserves FormData multipart boundaries.
|
|
173
|
+
const headers = new Headers(request.headers)
|
|
174
|
+
const body = request.body
|
|
175
|
+
if (body === null)
|
|
176
|
+
return { ok: true, request }
|
|
177
|
+
|
|
178
|
+
const reader = body.getReader()
|
|
179
|
+
const chunks: Uint8Array[] = []
|
|
180
|
+
let total = 0
|
|
181
|
+
try {
|
|
182
|
+
while (true) {
|
|
183
|
+
const { done, value } = await reader.read()
|
|
184
|
+
if (done)
|
|
185
|
+
break
|
|
186
|
+
total += value.byteLength
|
|
187
|
+
if (total > maxBytes) {
|
|
188
|
+
await reader.cancel().catch(() => {})
|
|
189
|
+
return { ok: false }
|
|
190
|
+
}
|
|
191
|
+
chunks.push(value)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
reader.releaseLock()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const combined = new Uint8Array(total)
|
|
199
|
+
let offset = 0
|
|
200
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
201
|
+
const chunk = chunks[i]!
|
|
202
|
+
combined.set(chunk, offset)
|
|
203
|
+
offset += chunk.byteLength
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
ok: true,
|
|
208
|
+
request: new Request(request.url, {
|
|
209
|
+
method: request.method,
|
|
210
|
+
headers,
|
|
211
|
+
body: combined,
|
|
212
|
+
}),
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function applyHstsHeader(
|
|
217
|
+
res: Response,
|
|
218
|
+
request: Request,
|
|
219
|
+
hstsValue: string | undefined,
|
|
220
|
+
trustProxy: boolean,
|
|
221
|
+
): Response {
|
|
222
|
+
if (hstsValue === undefined || process.env.NODE_ENV !== 'production' || !isHttpsRequest(request, trustProxy))
|
|
223
|
+
return res
|
|
224
|
+
if (res.headers.has('strict-transport-security'))
|
|
225
|
+
return res
|
|
226
|
+
try {
|
|
227
|
+
res.headers.set('strict-transport-security', hstsValue)
|
|
228
|
+
return res
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
const headers = new Headers(res.headers)
|
|
232
|
+
headers.set('strict-transport-security', hstsValue)
|
|
233
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
122
237
|
export function finalizeResponse(
|
|
123
238
|
res: Response,
|
|
124
239
|
cookieStore: CookieStore,
|