@kayjs/kay 2.0.0-beta.1 → 2.0.0-beta.2
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/bundle.ts +206 -33
- package/src/build/compile.ts +25 -12
- package/src/build/files.ts +38 -0
- package/src/build/graph.ts +67 -2
- package/src/build/index.ts +57 -7
- package/src/build/manifest.ts +3 -0
- package/src/build/plugin-host.ts +2 -0
- package/src/build/prerender.ts +2 -1
- package/src/build/resolve.ts +11 -18
- package/src/build/routes.ts +33 -5
- package/src/cli/check.ts +102 -45
- package/src/cli/new.ts +6 -7
- package/src/compiler/analyze.ts +65 -10
- package/src/compiler/dom.ts +143 -122
- package/src/compiler/server-only.ts +14 -8
- package/src/compiler/shared.ts +57 -14
- package/src/compiler/ssr.ts +14 -12
- package/src/dev/index.ts +94 -8
- package/src/dom/action.ts +91 -1
- package/src/dom/index.ts +8 -3
- package/src/index.ts +3 -0
- package/src/jsx-runtime.ts +421 -93
- package/src/plugins/context.ts +26 -0
- package/src/plugins/index.ts +174 -8
- package/src/server/action.ts +15 -8
- package/src/server/compress.ts +87 -14
- package/src/server/http.ts +89 -30
- package/src/server/index.ts +191 -62
- package/src/server/loader.ts +53 -35
- package/src/server/router.ts +49 -20
- package/src/server/security.ts +4 -2
- package/src/test/index.ts +5 -16
package/src/server/router.ts
CHANGED
|
@@ -2,9 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
const ENCODED_SLASH_RE = /%2f/i
|
|
4
4
|
|
|
5
|
+
const enum SegmentKind {
|
|
6
|
+
Static = 0,
|
|
7
|
+
Param = 1,
|
|
8
|
+
CatchAll = 2,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface CompiledSegment {
|
|
12
|
+
kind: SegmentKind
|
|
13
|
+
/** Exact literal for Static, parameter name for Param/CatchAll. */
|
|
14
|
+
value: string
|
|
15
|
+
}
|
|
16
|
+
|
|
5
17
|
interface RoutePattern<T> {
|
|
6
18
|
path: string
|
|
7
|
-
segments:
|
|
19
|
+
segments: CompiledSegment[]
|
|
8
20
|
isStatic: boolean
|
|
9
21
|
hasCatchAll: boolean
|
|
10
22
|
score: number
|
|
@@ -23,24 +35,28 @@ function normalizePath(path: string): string {
|
|
|
23
35
|
function compileRoutePattern<T>(path: string, data: T): RoutePattern<T> {
|
|
24
36
|
const norm = normalizePath(path)
|
|
25
37
|
const clean = norm === '/' ? '' : norm.replace(/^\/|\/$/g, '')
|
|
26
|
-
const
|
|
38
|
+
const rawSegments = clean === '' ? [] : clean.split('/')
|
|
39
|
+
const segments: CompiledSegment[] = []
|
|
27
40
|
let isStatic = true
|
|
28
41
|
let hasCatchAll = false
|
|
29
42
|
let score = 0
|
|
30
43
|
|
|
31
|
-
for (let i = 0; i <
|
|
32
|
-
const seg =
|
|
44
|
+
for (let i = 0; i < rawSegments.length; i++) {
|
|
45
|
+
const seg = rawSegments[i]!
|
|
33
46
|
if (seg.startsWith('*')) {
|
|
34
47
|
isStatic = false
|
|
35
48
|
hasCatchAll = true
|
|
36
49
|
score += 1
|
|
50
|
+
segments.push({ kind: SegmentKind.CatchAll, value: seg.slice(1) || 'rest' })
|
|
37
51
|
}
|
|
38
52
|
else if (seg.startsWith(':')) {
|
|
39
53
|
isStatic = false
|
|
40
54
|
score += 10
|
|
55
|
+
segments.push({ kind: SegmentKind.Param, value: seg.slice(1) })
|
|
41
56
|
}
|
|
42
57
|
else {
|
|
43
58
|
score += 100
|
|
59
|
+
segments.push({ kind: SegmentKind.Static, value: seg })
|
|
44
60
|
}
|
|
45
61
|
}
|
|
46
62
|
|
|
@@ -50,6 +66,7 @@ function compileRoutePattern<T>(path: string, data: T): RoutePattern<T> {
|
|
|
50
66
|
export class Router<T> {
|
|
51
67
|
readonly #staticRoutes = new Map<string, T>()
|
|
52
68
|
readonly #dynamicRoutes: RoutePattern<T>[] = []
|
|
69
|
+
#sorted = true
|
|
53
70
|
|
|
54
71
|
add(path: string, data: T): void {
|
|
55
72
|
const compiled = compileRoutePattern(path, data)
|
|
@@ -62,7 +79,7 @@ export class Router<T> {
|
|
|
62
79
|
this.#dynamicRoutes[existing] = compiled
|
|
63
80
|
else
|
|
64
81
|
this.#dynamicRoutes.push(compiled)
|
|
65
|
-
this.#
|
|
82
|
+
this.#sorted = false
|
|
66
83
|
}
|
|
67
84
|
}
|
|
68
85
|
|
|
@@ -73,12 +90,18 @@ export class Router<T> {
|
|
|
73
90
|
if (exact !== undefined)
|
|
74
91
|
return { data: exact, params: {} }
|
|
75
92
|
|
|
76
|
-
|
|
93
|
+
const dynLen = this.#dynamicRoutes.length
|
|
94
|
+
if (dynLen === 0)
|
|
77
95
|
return undefined
|
|
78
96
|
|
|
97
|
+
if (!this.#sorted) {
|
|
98
|
+
this.#dynamicRoutes.sort((a, b) => b.score - a.score || b.segments.length - a.segments.length)
|
|
99
|
+
this.#sorted = true
|
|
100
|
+
}
|
|
101
|
+
|
|
79
102
|
const parts = norm === '/' ? [] : norm.slice(1).split('/')
|
|
80
103
|
|
|
81
|
-
for (let i = 0; i <
|
|
104
|
+
for (let i = 0; i < dynLen; i++) {
|
|
82
105
|
const route = this.#dynamicRoutes[i]!
|
|
83
106
|
const segs = route.segments
|
|
84
107
|
|
|
@@ -87,32 +110,38 @@ export class Router<T> {
|
|
|
87
110
|
if (route.hasCatchAll && parts.length < segs.length - 1)
|
|
88
111
|
continue
|
|
89
112
|
|
|
90
|
-
const params: Record<string, string> = {}
|
|
91
113
|
let matched = true
|
|
92
|
-
|
|
93
114
|
for (let j = 0; j < segs.length; j++) {
|
|
94
115
|
const seg = segs[j]!
|
|
95
|
-
if (seg.
|
|
96
|
-
const name = seg.slice(1) || 'rest'
|
|
97
|
-
params[name] = parts.slice(j).map(decodeSegment).join('/')
|
|
116
|
+
if (seg.kind === SegmentKind.CatchAll)
|
|
98
117
|
break
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const rawSeg = parts[j]!
|
|
102
|
-
if (ENCODED_SLASH_RE.test(rawSeg)) {
|
|
118
|
+
if (seg.kind === SegmentKind.Param) {
|
|
119
|
+
if (ENCODED_SLASH_RE.test(parts[j]!)) {
|
|
103
120
|
matched = false
|
|
104
121
|
break
|
|
105
122
|
}
|
|
106
|
-
params[seg.slice(1)] = decodeSegment(rawSeg)
|
|
107
123
|
}
|
|
108
|
-
else if (seg !== parts[j]) {
|
|
124
|
+
else if (seg.value !== parts[j]) {
|
|
109
125
|
matched = false
|
|
110
126
|
break
|
|
111
127
|
}
|
|
112
128
|
}
|
|
113
129
|
|
|
114
|
-
if (matched)
|
|
115
|
-
|
|
130
|
+
if (!matched)
|
|
131
|
+
continue
|
|
132
|
+
|
|
133
|
+
// Allocate and decode params only for the winning route.
|
|
134
|
+
const params: Record<string, string> = {}
|
|
135
|
+
for (let j = 0; j < segs.length; j++) {
|
|
136
|
+
const seg = segs[j]!
|
|
137
|
+
if (seg.kind === SegmentKind.CatchAll) {
|
|
138
|
+
params[seg.value] = parts.slice(j).map(decodeSegment).join('/')
|
|
139
|
+
break
|
|
140
|
+
}
|
|
141
|
+
if (seg.kind === SegmentKind.Param)
|
|
142
|
+
params[seg.value] = decodeSegment(parts[j]!)
|
|
143
|
+
}
|
|
144
|
+
return { data: route.data, params }
|
|
116
145
|
}
|
|
117
146
|
|
|
118
147
|
return undefined
|
package/src/server/security.ts
CHANGED
|
@@ -137,8 +137,9 @@ export function isTrustedOrigin(
|
|
|
137
137
|
request: Request,
|
|
138
138
|
trustedOrigins?: ReadonlySet<string>,
|
|
139
139
|
trustProxy: TrustProxy = false,
|
|
140
|
+
requestUrl?: URL,
|
|
140
141
|
): boolean {
|
|
141
|
-
return origin === new URL(request.url).origin
|
|
142
|
+
return origin === (requestUrl?.origin ?? new URL(request.url).origin)
|
|
142
143
|
|| origin === forwardedOrigin(request, trustProxy)
|
|
143
144
|
|| trustedOrigins?.has(origin) === true
|
|
144
145
|
}
|
|
@@ -148,13 +149,14 @@ export function verifyOrigin(
|
|
|
148
149
|
request: Request,
|
|
149
150
|
trustedOrigins?: ReadonlySet<string>,
|
|
150
151
|
trustProxy: TrustProxy = false,
|
|
152
|
+
requestUrl?: URL,
|
|
151
153
|
): { ok: true } | { ok: false, reason: string } {
|
|
152
154
|
if (!UNSAFE_METHODS.has(request.method.toUpperCase()))
|
|
153
155
|
return { ok: true }
|
|
154
156
|
|
|
155
157
|
const origin = request.headers.get('origin')
|
|
156
158
|
if (origin !== null) {
|
|
157
|
-
if (isTrustedOrigin(origin, request, trustedOrigins, trustProxy))
|
|
159
|
+
if (isTrustedOrigin(origin, request, trustedOrigins, trustProxy, requestUrl))
|
|
158
160
|
return { ok: true }
|
|
159
161
|
return { ok: false, reason: `Cross-site request blocked: untrusted origin ${origin}` }
|
|
160
162
|
}
|
package/src/test/index.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
// kay/test: in-process testing for pages, middleware, cookies, and actions — zero port binding.
|
|
2
2
|
|
|
3
3
|
import type { BuildManifest, BuildOptions } from '../build/index'
|
|
4
|
-
import type { KayApp,
|
|
5
|
-
import type { TrustProxy } from '../server/security'
|
|
4
|
+
import type { KayApp, ServerRuntimeConfig } from '../server/index'
|
|
6
5
|
import { build } from '../build/index'
|
|
7
|
-
import { createServer } from '../server/index'
|
|
6
|
+
import { createServer, resolveServerConfig } from '../server/index'
|
|
8
7
|
|
|
9
|
-
export interface TestAppOptions extends Omit<BuildOptions, 'compile' | 'static'> {
|
|
8
|
+
export interface TestAppOptions extends Omit<BuildOptions, 'compile' | 'static'>, Partial<ServerRuntimeConfig> {
|
|
10
9
|
prebuilt?: boolean | undefined
|
|
11
|
-
lang?: string | undefined
|
|
12
|
-
security?: ServerOptions['security']
|
|
13
|
-
trustedOrigins?: string[] | undefined
|
|
14
|
-
trustProxy?: TrustProxy | undefined
|
|
15
|
-
maxRequestBodySize?: number | undefined
|
|
16
10
|
}
|
|
17
11
|
|
|
18
12
|
export interface TestApp {
|
|
@@ -33,19 +27,14 @@ export async function testApp(options: TestAppOptions): Promise<TestApp> {
|
|
|
33
27
|
outDir: options.outDir,
|
|
34
28
|
minify: options.minify ?? false,
|
|
35
29
|
assetPrefix: options.assetPrefix,
|
|
30
|
+
site: options.site,
|
|
36
31
|
plugins: options.plugins,
|
|
37
32
|
})
|
|
38
33
|
|
|
39
34
|
const app = await createServer({
|
|
40
35
|
root: options.root,
|
|
41
36
|
outDir: options.outDir,
|
|
42
|
-
|
|
43
|
-
lang: options.lang,
|
|
44
|
-
security: options.security,
|
|
45
|
-
trustedOrigins: options.trustedOrigins,
|
|
46
|
-
trustProxy: options.trustProxy,
|
|
47
|
-
maxRequestBodySize: options.maxRequestBodySize,
|
|
48
|
-
plugins: options.plugins,
|
|
37
|
+
...resolveServerConfig(undefined, options),
|
|
49
38
|
})
|
|
50
39
|
|
|
51
40
|
const cookies = new Map<string, string>()
|