@kayjs/kay 2.0.0-alpha.6 → 2.0.0-alpha.8
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 +13 -7
- package/src/build/index.ts +129 -32
- package/src/cli/check.ts +41 -37
- package/src/cli/index.ts +5 -3
- package/src/cli/new.ts +10 -5
- package/src/compiler/dom.ts +163 -54
- package/src/compiler/server-only.ts +62 -0
- package/src/compiler/ssr.ts +16 -1
- package/src/dom/extras.ts +8 -1
- package/src/dom/index.ts +23 -14
- package/src/plugins/index.ts +10 -0
- package/src/reactivity/index.ts +61 -29
- package/src/reactivity/store.ts +4 -0
- package/src/server/http.ts +4 -2
- package/src/server/index.ts +130 -89
package/src/reactivity/index.ts
CHANGED
|
@@ -38,7 +38,7 @@ interface SubNode {
|
|
|
38
38
|
c: Link | undefined // cursor
|
|
39
39
|
no: SubNode | undefined // nextOwned
|
|
40
40
|
nq: SubNode | undefined // nextQueued
|
|
41
|
-
|
|
41
|
+
k: (() => void) | undefined // cleanup returned by the effect
|
|
42
42
|
o: Owner | undefined // owner
|
|
43
43
|
r(): void // run
|
|
44
44
|
}
|
|
@@ -211,10 +211,12 @@ function propagate(dep: DepNode, flag: number): void {
|
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
+
// One effect throwing must not strand the rest of the queue: every node still runs, then a lone error is rethrown as is and several as one AggregateError.
|
|
214
215
|
function flush(): void {
|
|
215
216
|
if (flushing || batchDepth > 0)
|
|
216
217
|
return
|
|
217
218
|
flushing = true
|
|
219
|
+
const errors: unknown[] = []
|
|
218
220
|
try {
|
|
219
221
|
let rounds = 0
|
|
220
222
|
while (qHead !== undefined) {
|
|
@@ -226,16 +228,32 @@ function flush(): void {
|
|
|
226
228
|
const next: SubNode | undefined = node.nq
|
|
227
229
|
node.nq = undefined
|
|
228
230
|
node.f &= ~QUEUED
|
|
229
|
-
|
|
230
|
-
node.
|
|
231
|
+
try {
|
|
232
|
+
if (!(node.f & DISPOSED) && shouldRun(node))
|
|
233
|
+
node.r()
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
errors.push(err)
|
|
237
|
+
}
|
|
231
238
|
node = next
|
|
232
239
|
}
|
|
233
240
|
}
|
|
234
241
|
}
|
|
235
242
|
finally {
|
|
236
243
|
flushing = false
|
|
244
|
+
let left = qHead
|
|
237
245
|
qHead = qTail = undefined
|
|
246
|
+
while (left !== undefined) {
|
|
247
|
+
const next: SubNode | undefined = left.nq
|
|
248
|
+
left.nq = undefined
|
|
249
|
+
left.f &= ~QUEUED
|
|
250
|
+
left = next
|
|
251
|
+
}
|
|
238
252
|
}
|
|
253
|
+
if (errors.length === 1)
|
|
254
|
+
throw errors[0]
|
|
255
|
+
if (errors.length > 1)
|
|
256
|
+
throw new AggregateError(errors, `kay: ${errors.length} effects threw`)
|
|
239
257
|
}
|
|
240
258
|
|
|
241
259
|
function shouldRun(sub: SubNode): boolean {
|
|
@@ -309,7 +327,7 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
|
|
|
309
327
|
c: Link | undefined = undefined
|
|
310
328
|
no: SubNode | undefined = undefined
|
|
311
329
|
nq: SubNode | undefined = undefined
|
|
312
|
-
|
|
330
|
+
k: (() => void) | undefined = undefined
|
|
313
331
|
o: Owner | undefined = undefined
|
|
314
332
|
_f: () => T
|
|
315
333
|
_v!: T
|
|
@@ -360,7 +378,8 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
|
|
|
360
378
|
}
|
|
361
379
|
}
|
|
362
380
|
|
|
363
|
-
|
|
381
|
+
// An effect is also the owner of what its run creates (effects, onCleanup), released before the next run.
|
|
382
|
+
class EffectNode implements SubNode, Owner {
|
|
364
383
|
f = EFFECT | DIRTY
|
|
365
384
|
e = -1
|
|
366
385
|
d: Link | undefined = undefined
|
|
@@ -368,13 +387,19 @@ class EffectNode implements SubNode {
|
|
|
368
387
|
c: Link | undefined = undefined
|
|
369
388
|
no: SubNode | undefined = undefined
|
|
370
389
|
nq: SubNode | undefined = undefined
|
|
371
|
-
|
|
390
|
+
k: (() => void) | undefined = undefined
|
|
372
391
|
o: Owner | undefined
|
|
392
|
+
p: Owner | undefined
|
|
393
|
+
fc: Owner | undefined = undefined
|
|
394
|
+
ns: Owner | undefined = undefined
|
|
395
|
+
fs: SubNode | undefined = undefined
|
|
396
|
+
cl: (() => void)[] | undefined = undefined
|
|
397
|
+
x = false
|
|
373
398
|
_f: () => void | (() => void)
|
|
374
399
|
|
|
375
400
|
constructor(fn: () => void | (() => void), owner: Owner | undefined) {
|
|
376
401
|
this._f = fn
|
|
377
|
-
this.o = owner
|
|
402
|
+
this.o = this.p = owner
|
|
378
403
|
if (owner !== undefined) {
|
|
379
404
|
this.no = owner.fs
|
|
380
405
|
owner.fs = this
|
|
@@ -384,9 +409,10 @@ class EffectNode implements SubNode {
|
|
|
384
409
|
r(): void {
|
|
385
410
|
if (this.f & DISPOSED)
|
|
386
411
|
return
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
412
|
+
releaseOwned(this)
|
|
413
|
+
if (this.k !== undefined) {
|
|
414
|
+
const fn = this.k
|
|
415
|
+
this.k = undefined
|
|
390
416
|
fn()
|
|
391
417
|
}
|
|
392
418
|
const prevSub = currentSub
|
|
@@ -395,11 +421,15 @@ class EffectNode implements SubNode {
|
|
|
395
421
|
this.e = ++globalEpoch
|
|
396
422
|
this.c = this.d
|
|
397
423
|
currentSub = this
|
|
398
|
-
currentOwner = this
|
|
424
|
+
currentOwner = this
|
|
399
425
|
try {
|
|
400
426
|
const ret = (this._f)()
|
|
401
|
-
if (typeof ret === 'function')
|
|
402
|
-
this.
|
|
427
|
+
if (typeof ret === 'function') {
|
|
428
|
+
if (this.f & DISPOSED)
|
|
429
|
+
ret()
|
|
430
|
+
else
|
|
431
|
+
this.k = ret
|
|
432
|
+
}
|
|
403
433
|
}
|
|
404
434
|
finally {
|
|
405
435
|
unlinkRemaining(this)
|
|
@@ -430,7 +460,22 @@ export function disposeOwner(owner: Owner): void {
|
|
|
430
460
|
if (owner.x)
|
|
431
461
|
return
|
|
432
462
|
owner.x = true
|
|
463
|
+
releaseOwned(owner)
|
|
464
|
+
}
|
|
433
465
|
|
|
466
|
+
function disposeSub(sub: SubNode): void {
|
|
467
|
+
sub.f |= DISPOSED
|
|
468
|
+
unlinkAll(sub)
|
|
469
|
+
if (sub.f & EFFECT)
|
|
470
|
+
disposeOwner(sub as EffectNode)
|
|
471
|
+
if (sub.k !== undefined) {
|
|
472
|
+
const fn = sub.k
|
|
473
|
+
sub.k = undefined
|
|
474
|
+
fn()
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function releaseOwned(owner: Owner): void {
|
|
434
479
|
let child = owner.fc
|
|
435
480
|
owner.fc = undefined
|
|
436
481
|
while (child !== undefined) {
|
|
@@ -445,13 +490,7 @@ export function disposeOwner(owner: Owner): void {
|
|
|
445
490
|
while (sub !== undefined) {
|
|
446
491
|
const next = sub.no
|
|
447
492
|
sub.no = undefined
|
|
448
|
-
sub
|
|
449
|
-
unlinkAll(sub)
|
|
450
|
-
if (sub.cl !== undefined) {
|
|
451
|
-
const fn = sub.cl
|
|
452
|
-
sub.cl = undefined
|
|
453
|
-
fn()
|
|
454
|
-
}
|
|
493
|
+
disposeSub(sub)
|
|
455
494
|
sub = next
|
|
456
495
|
}
|
|
457
496
|
|
|
@@ -498,15 +537,8 @@ export function effect(fn: () => void | (() => void)): () => void {
|
|
|
498
537
|
const node = new EffectNode(fn, currentOwner)
|
|
499
538
|
node.r()
|
|
500
539
|
return () => {
|
|
501
|
-
if (node.f & DISPOSED)
|
|
502
|
-
|
|
503
|
-
node.f |= DISPOSED
|
|
504
|
-
unlinkAll(node)
|
|
505
|
-
if (node.cl !== undefined) {
|
|
506
|
-
const cb = node.cl
|
|
507
|
-
node.cl = undefined
|
|
508
|
-
cb()
|
|
509
|
-
}
|
|
540
|
+
if (!(node.f & DISPOSED))
|
|
541
|
+
disposeSub(node)
|
|
510
542
|
}
|
|
511
543
|
}
|
|
512
544
|
|
package/src/reactivity/store.ts
CHANGED
|
@@ -57,12 +57,16 @@ function wrap<T extends object>(target: T): T {
|
|
|
57
57
|
: value
|
|
58
58
|
const had = Object.prototype.hasOwnProperty.call(obj, key)
|
|
59
59
|
const prev = Reflect.get(obj, key, receiver)
|
|
60
|
+
const prevLength = Array.isArray(obj) ? obj.length : 0
|
|
60
61
|
const ok = Reflect.set(obj, key, rawValue, receiver)
|
|
61
62
|
if (!had || !Object.is(prev, rawValue)) {
|
|
62
63
|
notifyKey(obj, key)
|
|
63
64
|
if (!had || Array.isArray(obj))
|
|
64
65
|
notifyKey(obj, OWN_KEYS)
|
|
65
66
|
}
|
|
67
|
+
// Writing past the end grows the array without a later `length` write that differs, so readers of length are told here.
|
|
68
|
+
if (Array.isArray(obj) && key !== 'length' && obj.length !== prevLength)
|
|
69
|
+
notifyKey(obj, 'length')
|
|
66
70
|
return ok
|
|
67
71
|
},
|
|
68
72
|
deleteProperty(obj, key) {
|
package/src/server/http.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// HTTP helpers, MIME table, RequestContext factory, and island tag scanner for Kay v2 server.
|
|
2
2
|
|
|
3
3
|
import type { BuildManifest } from '../build/index'
|
|
4
|
-
import type { KayPlugin } from '../plugins/index'
|
|
4
|
+
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'
|
|
@@ -16,6 +16,7 @@ export interface ServerOptions {
|
|
|
16
16
|
security?: SecurityHeadersOptions | false | undefined
|
|
17
17
|
trustedOrigins?: string[] | undefined
|
|
18
18
|
compress?: boolean | undefined
|
|
19
|
+
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
19
20
|
plugins?: KayPlugin[] | undefined
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -35,7 +36,7 @@ export type Middleware = (ctx: RequestContext, next: () => Promise<Response>) =>
|
|
|
35
36
|
|
|
36
37
|
export interface KayApp {
|
|
37
38
|
handle: (request: Request) => Promise<Response>
|
|
38
|
-
serve: (port?: number) => ReturnType<typeof Bun.serve>
|
|
39
|
+
serve: (port?: number, hostname?: string) => ReturnType<typeof Bun.serve>
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
export type EmbeddedBody = Uint8Array<ArrayBuffer> | string
|
|
@@ -53,6 +54,7 @@ export interface EmbeddedServerConfig {
|
|
|
53
54
|
security?: SecurityHeadersOptions | false | undefined
|
|
54
55
|
trustedOrigins?: string[] | undefined
|
|
55
56
|
compress?: boolean | undefined
|
|
57
|
+
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
56
58
|
plugins?: KayPlugin[] | undefined
|
|
57
59
|
}
|
|
58
60
|
|
package/src/server/index.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import type { BuildManifest, RouteManifestEntry } from '../build/index'
|
|
9
9
|
import type { Html, HydrateStrategy } from '../jsx-runtime'
|
|
10
|
-
import type { KayPlugin, ServerPluginAPI } from '../plugins/index'
|
|
10
|
+
import type { KayPlugin, ServerErrorInfo, ServerPluginAPI } from '../plugins/index'
|
|
11
11
|
import type { ActionContext, ServerAction } from './action'
|
|
12
12
|
import type { CookieStore } from './cookies'
|
|
13
13
|
import type { EmbeddedBody, EmbeddedServerConfig, KayApp, Middleware, RequestContext, ServerOptions } from './http'
|
|
@@ -24,7 +24,7 @@ import { Router } from './router'
|
|
|
24
24
|
import { buildSecurityHeaderEntries, KAY_ISLAND_STYLE_HASH, KAY_STREAM_SCRIPT_HASH, KAY_SUSPENSE_STYLE_HASH, withCspHashes } from './security'
|
|
25
25
|
|
|
26
26
|
export { defineConfig, definePlugin, loadConfig } from '../plugins/index'
|
|
27
|
-
export type { BuildPluginContext, CheckPluginContext, KayConfig, KayPlugin, ServerPluginAPI } from '../plugins/index'
|
|
27
|
+
export type { BuildPluginContext, CheckPluginContext, KayConfig, KayPlugin, ServerErrorInfo, ServerPluginAPI } from '../plugins/index'
|
|
28
28
|
export { action, getActionError } from './action'
|
|
29
29
|
export type { ActionContext, ActionErrorFlash, ActionResult, ServerAction } from './action'
|
|
30
30
|
export { CompressionCache, isCompressibleType, pickEncoding } from './compress'
|
|
@@ -174,6 +174,21 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
const toResponseBody = (body: EmbeddedBody) => typeof body === 'string' ? Bun.file(body) : body
|
|
177
|
+
const errorHooks: ((info: ServerErrorInfo) => void)[] = []
|
|
178
|
+
if (config.onError !== undefined)
|
|
179
|
+
errorHooks.push(config.onError)
|
|
180
|
+
for (const p of plugins) {
|
|
181
|
+
if (p.onError !== undefined)
|
|
182
|
+
errorHooks.push(p.onError)
|
|
183
|
+
}
|
|
184
|
+
const notifyError = (error: unknown, request: Request, source: ServerErrorInfo['source']) => {
|
|
185
|
+
for (let i = 0; i < errorHooks.length; i++) {
|
|
186
|
+
try {
|
|
187
|
+
errorHooks[i]!({ error, request, source })
|
|
188
|
+
}
|
|
189
|
+
catch {}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
177
192
|
|
|
178
193
|
async function serveAsset(request: Request, pathname: string, asset: AssetEntry): Promise<Response> {
|
|
179
194
|
if (request.method === 'HEAD')
|
|
@@ -276,13 +291,14 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
276
291
|
}
|
|
277
292
|
|
|
278
293
|
// Register server actions and assign their live URLs
|
|
279
|
-
const actionHandlers = new Map<string, ServerAction<unknown, unknown
|
|
294
|
+
const actionHandlers = new Map<string, { act: ServerAction<unknown, unknown>, middlewares: Middleware[] }>()
|
|
280
295
|
for (const entry of manifest.actions) {
|
|
281
296
|
const mod = modules[entry.file] as Record<string, unknown> | undefined
|
|
282
297
|
const act = mod?.[entry.exportName]
|
|
283
298
|
if (isServerAction(act)) {
|
|
284
299
|
;(act as { url: string }).url = entry.url
|
|
285
|
-
|
|
300
|
+
const middlewares = entry.middlewares.map(m => (modules[m] as LoadedMiddlewareModule).default)
|
|
301
|
+
actionHandlers.set(entry.url, { act, middlewares: [...globalMiddlewares, ...middlewares] })
|
|
286
302
|
}
|
|
287
303
|
}
|
|
288
304
|
|
|
@@ -367,70 +383,80 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
367
383
|
const fastHeaders = selectHtmlHeaders(hasIslands, false)
|
|
368
384
|
|
|
369
385
|
fastHandler = (request: Request): Response | Promise<Response> => {
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
const wrapLayouts = (inner: string): Response | Promise<Response> => {
|
|
375
|
-
if (layouts.length === 0)
|
|
376
|
-
return respondHtml(inner)
|
|
377
|
-
if (singleLayout !== undefined) {
|
|
378
|
-
const w = singleLayout.default({ children: raw(inner), url: reqUrl, params: {}, locals: {} })
|
|
379
|
-
if (w instanceof Promise)
|
|
380
|
-
return w.then(x => respondHtml(ssrChild(x as Html)))
|
|
381
|
-
return respondHtml(ssrChild(w as Html))
|
|
382
|
-
}
|
|
383
|
-
return (async () => {
|
|
384
|
-
let cur = inner
|
|
385
|
-
for (let i = layouts.length - 1; i >= 0; i--)
|
|
386
|
-
cur = ssrChild(await layouts[i]!.default({ children: raw(cur), url: reqUrl, params: {}, locals: {} }) as Html)
|
|
387
|
-
return respondHtml(cur)
|
|
388
|
-
})()
|
|
386
|
+
const handleErr = (error: unknown): Promise<Response> => {
|
|
387
|
+
notifyError(error, request, 'route')
|
|
388
|
+
return renderError(error, makeRequestContext(request, {}).cookieStore, new URL(request.url))
|
|
389
389
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
(
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
390
|
+
try {
|
|
391
|
+
const reqUrl = new URL(request.url)
|
|
392
|
+
const respondHtml = (body: string): Response =>
|
|
393
|
+
compressedHtmlResponse(docPrefix + body + docSuffix, 200, fastHeaders, request, compressCache)
|
|
394
|
+
|
|
395
|
+
const wrapLayouts = (inner: string): Response | Promise<Response> => {
|
|
396
|
+
if (layouts.length === 0)
|
|
397
|
+
return respondHtml(inner)
|
|
398
|
+
if (singleLayout !== undefined) {
|
|
399
|
+
const w = singleLayout.default({ children: raw(inner), url: reqUrl, params: {}, locals: {} })
|
|
400
|
+
if (w instanceof Promise)
|
|
401
|
+
return w.then(x => respondHtml(ssrChild(x as Html)), handleErr)
|
|
402
|
+
return respondHtml(ssrChild(w as Html))
|
|
403
|
+
}
|
|
404
|
+
return (async () => {
|
|
405
|
+
let cur = inner
|
|
406
|
+
for (let i = layouts.length - 1; i >= 0; i--)
|
|
407
|
+
cur = ssrChild(await layouts[i]!.default({ children: raw(cur), url: reqUrl, params: {}, locals: {} }) as Html)
|
|
408
|
+
return respondHtml(cur)
|
|
409
|
+
})().catch(handleErr)
|
|
407
410
|
}
|
|
408
|
-
const rendered = mod.default!(pageProps)
|
|
409
|
-
if (rendered instanceof Promise)
|
|
410
|
-
return rendered.then(r => wrapLayouts(ssrChild(r as Html)))
|
|
411
|
-
return wrapLayouts(ssrChild(rendered as Html))
|
|
412
|
-
}
|
|
413
411
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
412
|
+
const renderPage = (data: unknown): Response | Promise<Response> => {
|
|
413
|
+
const pageProps: Record<string, unknown> = {
|
|
414
|
+
params: {},
|
|
415
|
+
locals: {},
|
|
416
|
+
url: reqUrl,
|
|
417
|
+
...(data !== undefined ? { data } : {}),
|
|
418
|
+
}
|
|
419
|
+
if (entry.isIsland && entry.islandChunk !== undefined) {
|
|
420
|
+
const inner = ssrIsland(
|
|
421
|
+
entry.path,
|
|
422
|
+
entry.islandChunk,
|
|
423
|
+
() => ssrChild(mod.default!(pageProps) as Html),
|
|
424
|
+
data !== undefined ? { data } : {},
|
|
425
|
+
entry.islandHydrate as HydrateStrategy | undefined,
|
|
426
|
+
)
|
|
427
|
+
return wrapLayouts(inner)
|
|
428
|
+
}
|
|
429
|
+
const rendered = mod.default!(pageProps)
|
|
430
|
+
if (rendered instanceof Promise)
|
|
431
|
+
return rendered.then(r => wrapLayouts(ssrChild(r as Html)), handleErr)
|
|
432
|
+
return wrapLayouts(ssrChild(rendered as Html))
|
|
422
433
|
}
|
|
423
|
-
|
|
424
|
-
if (
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
434
|
+
|
|
435
|
+
if (entry.hasLoad && typeof mod.load === 'function') {
|
|
436
|
+
const { ctx, cookieStore } = makeRequestContext(request, {})
|
|
437
|
+
const finish = (res: Response | Promise<Response>): Response | Promise<Response> => {
|
|
438
|
+
if (!cookieStore.hasChanges)
|
|
439
|
+
return res
|
|
440
|
+
if (res instanceof Promise)
|
|
441
|
+
return res.then(r => finalizeResponse(r, cookieStore, securityEntries), handleErr)
|
|
442
|
+
return finalizeResponse(res, cookieStore, securityEntries)
|
|
443
|
+
}
|
|
444
|
+
const loaded = mod.load(ctx)
|
|
445
|
+
if (loaded instanceof Promise) {
|
|
446
|
+
return loaded.then(
|
|
447
|
+
data => finish(data instanceof Response ? data : renderPage(data)),
|
|
448
|
+
handleErr,
|
|
449
|
+
)
|
|
450
|
+
}
|
|
451
|
+
if (loaded instanceof Response)
|
|
452
|
+
return finish(loaded)
|
|
453
|
+
return finish(renderPage(loaded))
|
|
428
454
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
455
|
+
return renderPage(undefined)
|
|
456
|
+
}
|
|
457
|
+
catch (error) {
|
|
458
|
+
return handleErr(error)
|
|
432
459
|
}
|
|
433
|
-
return renderPage(undefined)
|
|
434
460
|
}
|
|
435
461
|
fastStaticPages.set(entry.path, fastHandler)
|
|
436
462
|
}
|
|
@@ -604,10 +630,11 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
604
630
|
return servePublic(request, pathname, pub)
|
|
605
631
|
}
|
|
606
632
|
|
|
607
|
-
// 2. Server actions (POST /_kay/action/<id>)
|
|
633
|
+
// 2. Server actions (POST /_kay/action/<id>): global plugin middlewares, then the _middleware.ts chain of the action's folder
|
|
608
634
|
if (request.method === 'POST') {
|
|
609
|
-
const
|
|
610
|
-
if (
|
|
635
|
+
const handler = actionHandlers.get(pathname)
|
|
636
|
+
if (handler !== undefined) {
|
|
637
|
+
const { act, middlewares } = handler
|
|
611
638
|
const { ctx, cookieStore } = makeRequestContext(request, {})
|
|
612
639
|
const runAction = async (): Promise<Response> => {
|
|
613
640
|
const actionCtx: ActionContext = {
|
|
@@ -621,21 +648,23 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
621
648
|
}
|
|
622
649
|
return executeAction(act, request, actionCtx, trustedOrigins)
|
|
623
650
|
}
|
|
624
|
-
if (globalMiddlewares.length === 0) {
|
|
625
|
-
const res = await runAction()
|
|
626
|
-
return finalizeResponse(res, cookieStore, securityEntries)
|
|
627
|
-
}
|
|
628
651
|
let index = -1
|
|
629
652
|
const dispatch = async (i: number): Promise<Response> => {
|
|
630
653
|
if (i <= index)
|
|
631
654
|
throw new Error('kay: middleware called next() multiple times')
|
|
632
655
|
index = i
|
|
633
|
-
if (i <
|
|
634
|
-
return
|
|
656
|
+
if (i < middlewares.length)
|
|
657
|
+
return middlewares[i]!(ctx, () => dispatch(i + 1))
|
|
635
658
|
return runAction()
|
|
636
659
|
}
|
|
637
|
-
|
|
638
|
-
|
|
660
|
+
try {
|
|
661
|
+
const res = await dispatch(0)
|
|
662
|
+
return finalizeResponse(res, cookieStore, securityEntries)
|
|
663
|
+
}
|
|
664
|
+
catch (error) {
|
|
665
|
+
notifyError(error, request, 'action')
|
|
666
|
+
return renderError(error, cookieStore, ctx.url)
|
|
667
|
+
}
|
|
639
668
|
}
|
|
640
669
|
}
|
|
641
670
|
|
|
@@ -686,22 +715,28 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
686
715
|
if (methodHandler !== undefined) {
|
|
687
716
|
ctx.params = pluginMatch.params
|
|
688
717
|
let res: Response
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
718
|
+
try {
|
|
719
|
+
if (globalMiddlewares.length === 0) {
|
|
720
|
+
res = await methodHandler(ctx)
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
let index = -1
|
|
724
|
+
const dispatch = async (i: number): Promise<Response> => {
|
|
725
|
+
if (i <= index)
|
|
726
|
+
throw new Error('kay: middleware called next() multiple times')
|
|
727
|
+
index = i
|
|
728
|
+
if (i < globalMiddlewares.length)
|
|
729
|
+
return globalMiddlewares[i]!(ctx, () => dispatch(i + 1))
|
|
730
|
+
return methodHandler(ctx)
|
|
731
|
+
}
|
|
732
|
+
res = await dispatch(0)
|
|
701
733
|
}
|
|
702
|
-
|
|
734
|
+
return finalizeResponse(await maybeCompressResponse(res, request, compressCache), cookieStore, securityEntries)
|
|
735
|
+
}
|
|
736
|
+
catch (error) {
|
|
737
|
+
notifyError(error, request, 'plugin')
|
|
738
|
+
return renderError(error, cookieStore, ctx.url)
|
|
703
739
|
}
|
|
704
|
-
return finalizeResponse(await maybeCompressResponse(res, request, compressCache), cookieStore, securityEntries)
|
|
705
740
|
}
|
|
706
741
|
}
|
|
707
742
|
|
|
@@ -729,11 +764,12 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
729
764
|
return finalizeResponse(res, cookieStore, securityEntries)
|
|
730
765
|
}
|
|
731
766
|
catch (error) {
|
|
767
|
+
notifyError(error, request, 'route')
|
|
732
768
|
return renderError(error, cookieStore, ctx.url)
|
|
733
769
|
}
|
|
734
770
|
}
|
|
735
771
|
|
|
736
|
-
function serve(port = 3000) {
|
|
772
|
+
function serve(port = 3000, hostname?: string) {
|
|
737
773
|
const routes: Record<string, (req: Request) => Response | Promise<Response>> = {}
|
|
738
774
|
|
|
739
775
|
for (const [path, asset] of assetMap)
|
|
@@ -762,6 +798,7 @@ export async function createServerFromEmbedded(config: EmbeddedServerConfig): Pr
|
|
|
762
798
|
|
|
763
799
|
return Bun.serve({
|
|
764
800
|
port,
|
|
801
|
+
...(hostname ? { hostname } : {}),
|
|
765
802
|
idleTimeout: 0,
|
|
766
803
|
routes,
|
|
767
804
|
fetch: handle,
|
|
@@ -919,7 +956,10 @@ export async function createServer(options: ServerOptions): Promise<KayApp> {
|
|
|
919
956
|
}
|
|
920
957
|
if (manifest.errorFile) await loadMod(manifest.errorFile)
|
|
921
958
|
if (manifest.notFoundFile) await loadMod(manifest.notFoundFile)
|
|
922
|
-
for (const a of manifest.actions)
|
|
959
|
+
for (const a of manifest.actions) {
|
|
960
|
+
await loadMod(a.file)
|
|
961
|
+
for (const m of a.middlewares) await loadMod(m)
|
|
962
|
+
}
|
|
923
963
|
|
|
924
964
|
return createServerFromEmbedded({
|
|
925
965
|
manifest,
|
|
@@ -934,6 +974,7 @@ export async function createServer(options: ServerOptions): Promise<KayApp> {
|
|
|
934
974
|
security: options.security ?? fileConfig.security,
|
|
935
975
|
trustedOrigins: options.trustedOrigins ?? fileConfig.trustedOrigins,
|
|
936
976
|
compress: options.compress ?? fileConfig.compress,
|
|
977
|
+
onError: options.onError ?? fileConfig.onError,
|
|
937
978
|
plugins,
|
|
938
979
|
})
|
|
939
980
|
}
|