@kayjs/kay 2.0.0-alpha.7 → 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 +15 -3
- package/src/build/index.ts +301 -49
- package/src/cli/check.ts +41 -37
- package/src/cli/index.ts +27 -11
- package/src/cli/new.ts +24 -8
- package/src/compiler/dom.ts +153 -30
- package/src/compiler/server-only.ts +62 -0
- package/src/compiler/ssr.ts +101 -22
- package/src/dev/index.ts +26 -7
- package/src/dom/extras.ts +85 -43
- package/src/dom/index.ts +110 -17
- 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 +141 -40
- package/src/reactivity/store.ts +5 -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 +453 -242
- 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/reactivity/index.ts
CHANGED
|
@@ -36,9 +36,10 @@ interface SubNode {
|
|
|
36
36
|
d: Link | undefined // deps head
|
|
37
37
|
dt: Link | undefined // deps tail
|
|
38
38
|
c: Link | undefined // cursor
|
|
39
|
+
po: SubNode | undefined // prevOwned
|
|
39
40
|
no: SubNode | undefined // nextOwned
|
|
40
41
|
nq: SubNode | undefined // nextQueued
|
|
41
|
-
|
|
42
|
+
k: (() => void) | undefined // cleanup returned by the effect
|
|
42
43
|
o: Owner | undefined // owner
|
|
43
44
|
r(): void // run
|
|
44
45
|
}
|
|
@@ -46,6 +47,7 @@ interface SubNode {
|
|
|
46
47
|
export interface Owner {
|
|
47
48
|
p: Owner | undefined // parent
|
|
48
49
|
fc: Owner | undefined // firstChild
|
|
50
|
+
ps: Owner | undefined // prevSibling
|
|
49
51
|
ns: Owner | undefined // nextSibling
|
|
50
52
|
fs: SubNode | undefined // firstSub
|
|
51
53
|
cl: (() => void)[] | undefined // cleanups
|
|
@@ -125,6 +127,8 @@ function linkSub(dep: DepNode, sub: SubNode): void {
|
|
|
125
127
|
|
|
126
128
|
function unlink(l: Link): void {
|
|
127
129
|
const { d, s, pd, nd, ps, ns } = l
|
|
130
|
+
if (d.ls === s)
|
|
131
|
+
d.ls = undefined
|
|
128
132
|
if (pd !== undefined)
|
|
129
133
|
pd.nd = nd
|
|
130
134
|
else
|
|
@@ -161,6 +165,8 @@ function unlinkAll(sub: SubNode): void {
|
|
|
161
165
|
while (l !== undefined) {
|
|
162
166
|
const next = l.nd
|
|
163
167
|
const { d, ps, ns } = l
|
|
168
|
+
if (d.ls === sub)
|
|
169
|
+
d.ls = undefined
|
|
164
170
|
if (ps !== undefined)
|
|
165
171
|
ps.ns = ns
|
|
166
172
|
else
|
|
@@ -211,10 +217,12 @@ function propagate(dep: DepNode, flag: number): void {
|
|
|
211
217
|
}
|
|
212
218
|
}
|
|
213
219
|
|
|
220
|
+
// 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
221
|
function flush(): void {
|
|
215
222
|
if (flushing || batchDepth > 0)
|
|
216
223
|
return
|
|
217
224
|
flushing = true
|
|
225
|
+
const errors: unknown[] = []
|
|
218
226
|
try {
|
|
219
227
|
let rounds = 0
|
|
220
228
|
while (qHead !== undefined) {
|
|
@@ -226,20 +234,38 @@ function flush(): void {
|
|
|
226
234
|
const next: SubNode | undefined = node.nq
|
|
227
235
|
node.nq = undefined
|
|
228
236
|
node.f &= ~QUEUED
|
|
229
|
-
|
|
230
|
-
node.
|
|
237
|
+
try {
|
|
238
|
+
if (!(node.f & DISPOSED) && shouldRun(node))
|
|
239
|
+
node.r()
|
|
240
|
+
}
|
|
241
|
+
catch (err) {
|
|
242
|
+
errors.push(err)
|
|
243
|
+
}
|
|
231
244
|
node = next
|
|
232
245
|
}
|
|
233
246
|
}
|
|
234
247
|
}
|
|
235
248
|
finally {
|
|
236
249
|
flushing = false
|
|
250
|
+
let left = qHead
|
|
237
251
|
qHead = qTail = undefined
|
|
252
|
+
while (left !== undefined) {
|
|
253
|
+
const next: SubNode | undefined = left.nq
|
|
254
|
+
left.nq = undefined
|
|
255
|
+
left.f &= ~QUEUED
|
|
256
|
+
left = next
|
|
257
|
+
}
|
|
238
258
|
}
|
|
259
|
+
if (errors.length === 1)
|
|
260
|
+
throw errors[0]
|
|
261
|
+
if (errors.length > 1)
|
|
262
|
+
throw new AggregateError(errors, `kay: ${errors.length} effects threw`)
|
|
239
263
|
}
|
|
240
264
|
|
|
241
265
|
function shouldRun(sub: SubNode): boolean {
|
|
242
266
|
const f = sub.f
|
|
267
|
+
if (f & DISPOSED)
|
|
268
|
+
return false
|
|
243
269
|
if (f & DIRTY)
|
|
244
270
|
return true
|
|
245
271
|
if (!(f & CHECK))
|
|
@@ -307,19 +333,35 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
|
|
|
307
333
|
d: Link | undefined = undefined
|
|
308
334
|
dt: Link | undefined = undefined
|
|
309
335
|
c: Link | undefined = undefined
|
|
336
|
+
po: SubNode | undefined = undefined
|
|
310
337
|
no: SubNode | undefined = undefined
|
|
311
338
|
nq: SubNode | undefined = undefined
|
|
312
|
-
|
|
339
|
+
k: (() => void) | undefined = undefined
|
|
313
340
|
o: Owner | undefined = undefined
|
|
314
341
|
_f: () => T
|
|
315
342
|
_v!: T
|
|
316
343
|
_i = false
|
|
317
344
|
|
|
318
|
-
constructor(fn: () => T) {
|
|
345
|
+
constructor(fn: () => T, owner: Owner | undefined) {
|
|
319
346
|
this._f = fn
|
|
347
|
+
if (owner !== undefined) {
|
|
348
|
+
if (owner.x) {
|
|
349
|
+
this.f |= DISPOSED
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
this.o = owner
|
|
353
|
+
const first = owner.fs
|
|
354
|
+
this.no = first
|
|
355
|
+
if (first !== undefined)
|
|
356
|
+
first.po = this
|
|
357
|
+
owner.fs = this
|
|
358
|
+
}
|
|
359
|
+
}
|
|
320
360
|
}
|
|
321
361
|
|
|
322
362
|
r(): void {
|
|
363
|
+
if (this.f & DISPOSED)
|
|
364
|
+
return
|
|
323
365
|
if (this.f & RUNNING)
|
|
324
366
|
throw new Error('kay: cyclic computed')
|
|
325
367
|
const prevSub = currentSub
|
|
@@ -346,37 +388,51 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
|
|
|
346
388
|
}
|
|
347
389
|
|
|
348
390
|
get value(): T {
|
|
349
|
-
if (
|
|
350
|
-
this
|
|
351
|
-
|
|
352
|
-
|
|
391
|
+
if (!(this.f & DISPOSED)) {
|
|
392
|
+
if (shouldRun(this))
|
|
393
|
+
this.r()
|
|
394
|
+
if (currentSub !== undefined)
|
|
395
|
+
linkSub(this, currentSub)
|
|
396
|
+
}
|
|
353
397
|
return this._v
|
|
354
398
|
}
|
|
355
399
|
|
|
356
400
|
peek(): T {
|
|
357
|
-
if (shouldRun(this))
|
|
401
|
+
if (!(this.f & DISPOSED) && shouldRun(this))
|
|
358
402
|
this.r()
|
|
359
403
|
return this._v
|
|
360
404
|
}
|
|
361
405
|
}
|
|
362
406
|
|
|
363
|
-
|
|
407
|
+
// An effect is also the owner of what its run creates (effects, onCleanup), released before the next run.
|
|
408
|
+
class EffectNode implements SubNode, Owner {
|
|
364
409
|
f = EFFECT | DIRTY
|
|
365
410
|
e = -1
|
|
366
411
|
d: Link | undefined = undefined
|
|
367
412
|
dt: Link | undefined = undefined
|
|
368
413
|
c: Link | undefined = undefined
|
|
414
|
+
po: SubNode | undefined = undefined
|
|
369
415
|
no: SubNode | undefined = undefined
|
|
370
416
|
nq: SubNode | undefined = undefined
|
|
371
|
-
|
|
417
|
+
k: (() => void) | undefined = undefined
|
|
372
418
|
o: Owner | undefined
|
|
419
|
+
p: Owner | undefined
|
|
420
|
+
fc: Owner | undefined = undefined
|
|
421
|
+
ps: Owner | undefined = undefined
|
|
422
|
+
ns: Owner | undefined = undefined
|
|
423
|
+
fs: SubNode | undefined = undefined
|
|
424
|
+
cl: (() => void)[] | undefined = undefined
|
|
425
|
+
x = false
|
|
373
426
|
_f: () => void | (() => void)
|
|
374
427
|
|
|
375
428
|
constructor(fn: () => void | (() => void), owner: Owner | undefined) {
|
|
376
429
|
this._f = fn
|
|
377
|
-
this.o = owner
|
|
430
|
+
this.o = this.p = owner
|
|
378
431
|
if (owner !== undefined) {
|
|
379
|
-
|
|
432
|
+
const first = owner.fs
|
|
433
|
+
this.no = first
|
|
434
|
+
if (first !== undefined)
|
|
435
|
+
first.po = this
|
|
380
436
|
owner.fs = this
|
|
381
437
|
}
|
|
382
438
|
}
|
|
@@ -384,9 +440,10 @@ class EffectNode implements SubNode {
|
|
|
384
440
|
r(): void {
|
|
385
441
|
if (this.f & DISPOSED)
|
|
386
442
|
return
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
443
|
+
releaseOwned(this)
|
|
444
|
+
if (this.k !== undefined) {
|
|
445
|
+
const fn = this.k
|
|
446
|
+
this.k = undefined
|
|
390
447
|
fn()
|
|
391
448
|
}
|
|
392
449
|
const prevSub = currentSub
|
|
@@ -395,11 +452,15 @@ class EffectNode implements SubNode {
|
|
|
395
452
|
this.e = ++globalEpoch
|
|
396
453
|
this.c = this.d
|
|
397
454
|
currentSub = this
|
|
398
|
-
currentOwner = this
|
|
455
|
+
currentOwner = this
|
|
399
456
|
try {
|
|
400
457
|
const ret = (this._f)()
|
|
401
|
-
if (typeof ret === 'function')
|
|
402
|
-
this.
|
|
458
|
+
if (typeof ret === 'function') {
|
|
459
|
+
if (this.f & DISPOSED)
|
|
460
|
+
ret()
|
|
461
|
+
else
|
|
462
|
+
this.k = ret
|
|
463
|
+
}
|
|
403
464
|
}
|
|
404
465
|
finally {
|
|
405
466
|
unlinkRemaining(this)
|
|
@@ -414,28 +475,81 @@ export function createOwner(parent: Owner | undefined = currentOwner): Owner {
|
|
|
414
475
|
const owner: Owner = {
|
|
415
476
|
p: parent,
|
|
416
477
|
fc: undefined,
|
|
478
|
+
ps: undefined,
|
|
417
479
|
ns: undefined,
|
|
418
480
|
fs: undefined,
|
|
419
481
|
cl: undefined,
|
|
420
482
|
x: false,
|
|
421
483
|
}
|
|
422
484
|
if (parent !== undefined) {
|
|
423
|
-
|
|
485
|
+
const first = parent.fc
|
|
486
|
+
owner.ns = first
|
|
487
|
+
if (first !== undefined)
|
|
488
|
+
first.ps = owner
|
|
424
489
|
parent.fc = owner
|
|
425
490
|
}
|
|
426
491
|
return owner
|
|
427
492
|
}
|
|
428
493
|
|
|
494
|
+
function unlinkOwner(owner: Owner): void {
|
|
495
|
+
const parent = owner.p
|
|
496
|
+
if (parent === undefined)
|
|
497
|
+
return
|
|
498
|
+
const prev = owner.ps
|
|
499
|
+
const next = owner.ns
|
|
500
|
+
if (prev !== undefined)
|
|
501
|
+
prev.ns = next
|
|
502
|
+
else if (parent.fc === owner)
|
|
503
|
+
parent.fc = next
|
|
504
|
+
if (next !== undefined)
|
|
505
|
+
next.ps = prev
|
|
506
|
+
owner.p = owner.ps = owner.ns = undefined
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function unlinkOwnedSub(sub: SubNode): void {
|
|
510
|
+
const owner = sub.o
|
|
511
|
+
if (owner === undefined)
|
|
512
|
+
return
|
|
513
|
+
const prev = sub.po
|
|
514
|
+
const next = sub.no
|
|
515
|
+
if (prev !== undefined)
|
|
516
|
+
prev.no = next
|
|
517
|
+
else if (owner.fs === sub)
|
|
518
|
+
owner.fs = next
|
|
519
|
+
if (next !== undefined)
|
|
520
|
+
next.po = prev
|
|
521
|
+
sub.o = sub.po = sub.no = undefined
|
|
522
|
+
}
|
|
523
|
+
|
|
429
524
|
export function disposeOwner(owner: Owner): void {
|
|
430
525
|
if (owner.x)
|
|
431
526
|
return
|
|
432
527
|
owner.x = true
|
|
528
|
+
unlinkOwner(owner)
|
|
529
|
+
releaseOwned(owner)
|
|
530
|
+
}
|
|
433
531
|
|
|
532
|
+
function disposeSub(sub: SubNode): void {
|
|
533
|
+
if (sub.f & DISPOSED)
|
|
534
|
+
return
|
|
535
|
+
sub.f |= DISPOSED
|
|
536
|
+
unlinkOwnedSub(sub)
|
|
537
|
+
unlinkAll(sub)
|
|
538
|
+
if (sub.f & EFFECT)
|
|
539
|
+
disposeOwner(sub as EffectNode)
|
|
540
|
+
if (sub.k !== undefined) {
|
|
541
|
+
const fn = sub.k
|
|
542
|
+
sub.k = undefined
|
|
543
|
+
fn()
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function releaseOwned(owner: Owner): void {
|
|
434
548
|
let child = owner.fc
|
|
435
549
|
owner.fc = undefined
|
|
436
550
|
while (child !== undefined) {
|
|
437
551
|
const next = child.ns
|
|
438
|
-
child.ns = undefined
|
|
552
|
+
child.p = child.ps = child.ns = undefined
|
|
439
553
|
disposeOwner(child)
|
|
440
554
|
child = next
|
|
441
555
|
}
|
|
@@ -444,14 +558,8 @@ export function disposeOwner(owner: Owner): void {
|
|
|
444
558
|
owner.fs = undefined
|
|
445
559
|
while (sub !== undefined) {
|
|
446
560
|
const next = sub.no
|
|
447
|
-
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
|
-
}
|
|
561
|
+
sub.o = sub.po = sub.no = undefined
|
|
562
|
+
disposeSub(sub)
|
|
455
563
|
sub = next
|
|
456
564
|
}
|
|
457
565
|
|
|
@@ -491,22 +599,15 @@ export function signal<T>(initial: T): Signal<T> {
|
|
|
491
599
|
}
|
|
492
600
|
|
|
493
601
|
export function computed<T>(fn: () => T): ReadonlySignal<T> {
|
|
494
|
-
return new ComputedNode(fn)
|
|
602
|
+
return new ComputedNode(fn, currentOwner)
|
|
495
603
|
}
|
|
496
604
|
|
|
497
605
|
export function effect(fn: () => void | (() => void)): () => void {
|
|
498
606
|
const node = new EffectNode(fn, currentOwner)
|
|
499
607
|
node.r()
|
|
500
608
|
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
|
-
}
|
|
609
|
+
if (!(node.f & DISPOSED))
|
|
610
|
+
disposeSub(node)
|
|
510
611
|
}
|
|
511
612
|
}
|
|
512
613
|
|
package/src/reactivity/store.ts
CHANGED
|
@@ -35,7 +35,7 @@ function getDep(target: object, key: PropertyKey): Signal<number> {
|
|
|
35
35
|
function notifyKey(target: object, key: PropertyKey): void {
|
|
36
36
|
const dep = depsCache.get(target)?.get(key)
|
|
37
37
|
if (dep !== undefined)
|
|
38
|
-
dep.value
|
|
38
|
+
dep.value = dep.peek() + 1
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function wrap<T extends object>(target: T): T {
|
|
@@ -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/action.ts
CHANGED
|
@@ -2,11 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
import type { Cookies } from './cookies'
|
|
4
4
|
import type { InferInput, InferOutput, StandardSchemaIssue, StandardSchemaV1 } from './standard-schema'
|
|
5
|
+
import { DEFAULT_MAX_REQUEST_BODY_SIZE, enforceRequestBodyLimit } from './http'
|
|
5
6
|
import { isTrustedOrigin, verifyOrigin } from './security'
|
|
6
7
|
import { validateSchema } from './standard-schema'
|
|
7
8
|
|
|
8
9
|
export const ACTION_ERROR_COOKIE = '__kay_action_err'
|
|
9
10
|
|
|
11
|
+
const MAX_FLASH_COOKIE_BYTES = 3500
|
|
12
|
+
const MAX_FLASH_FIELD_CHARS = 512
|
|
13
|
+
const SENSITIVE_FIELD_RE = /password|passwd|pwd|secret|token|pin|cvv|cvc|iban|ssn|card|credit|auth|key|otp|code|routing|account|bank|tax/i
|
|
14
|
+
|
|
15
|
+
export interface ActionOptions {
|
|
16
|
+
/** Explicit allowlist of form field names permitted in the progressive-form validation error flash cookie. */
|
|
17
|
+
flashFields?: readonly string[] | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
10
20
|
export interface ActionErrorFlash {
|
|
11
21
|
action: string
|
|
12
22
|
error: string
|
|
@@ -24,6 +34,7 @@ export interface ActionContext {
|
|
|
24
34
|
params: Record<string, string>
|
|
25
35
|
cookies: Cookies
|
|
26
36
|
locals: Record<string, unknown>
|
|
37
|
+
ip?: string | undefined
|
|
27
38
|
redirect: (location: string, status?: 301 | 302 | 303 | 307 | 308) => Response
|
|
28
39
|
deny: (status: number, message: string, fieldErrors?: Record<string, string>) => Response
|
|
29
40
|
}
|
|
@@ -37,6 +48,7 @@ export interface ServerAction<Input, Output> {
|
|
|
37
48
|
readonly __kayAction: true
|
|
38
49
|
readonly url: string
|
|
39
50
|
readonly schema: StandardSchemaV1 | undefined
|
|
51
|
+
readonly flashFields: readonly string[] | undefined
|
|
40
52
|
readonly handler: (input: never, ctx: ActionContext) => Output | Response | Promise<Output | Response>
|
|
41
53
|
toString(): string
|
|
42
54
|
}
|
|
@@ -49,19 +61,23 @@ export function actionHash(relFile: string, exportName: string): string {
|
|
|
49
61
|
export function action<S extends StandardSchemaV1, Output>(
|
|
50
62
|
schema: S,
|
|
51
63
|
handler: (input: InferOutput<S>, ctx: ActionContext) => Output | Response | Promise<Output | Response>,
|
|
64
|
+
options?: ActionOptions,
|
|
52
65
|
): ServerAction<InferInput<S>, Exclude<Output, Response>>
|
|
53
66
|
export function action<Output>(
|
|
54
67
|
handler: (ctx: ActionContext) => Output | Response | Promise<Output | Response>,
|
|
68
|
+
options?: ActionOptions,
|
|
55
69
|
): ServerAction<void, Exclude<Output, Response>>
|
|
56
70
|
export function action(
|
|
57
71
|
schemaOrHandler: StandardSchemaV1 | ((ctx: ActionContext) => unknown),
|
|
58
|
-
|
|
72
|
+
maybeHandlerOrOpts?: ((input: never, ctx: ActionContext) => unknown) | ActionOptions,
|
|
73
|
+
maybeOpts?: ActionOptions,
|
|
59
74
|
): ServerAction<unknown, unknown> {
|
|
60
75
|
const hasSchema = typeof schemaOrHandler !== 'function'
|
|
61
76
|
const schema = hasSchema ? schemaOrHandler : undefined
|
|
62
77
|
const handler = hasSchema
|
|
63
|
-
?
|
|
78
|
+
? (maybeHandlerOrOpts as (input: never, ctx: ActionContext) => unknown)
|
|
64
79
|
: ((_input: never, ctx: ActionContext) => (schemaOrHandler as (c: ActionContext) => unknown)(ctx))
|
|
80
|
+
const options = hasSchema ? maybeOpts : (maybeHandlerOrOpts as ActionOptions | undefined)
|
|
65
81
|
|
|
66
82
|
const fn = (async () => {
|
|
67
83
|
throw new Error('kay: server action cannot be called directly without a request context; invoke it from an island or via HTTP POST')
|
|
@@ -70,6 +86,7 @@ export function action(
|
|
|
70
86
|
Object.defineProperties(fn, {
|
|
71
87
|
__kayAction: { value: true, enumerable: false },
|
|
72
88
|
schema: { value: schema, enumerable: false },
|
|
89
|
+
flashFields: { value: options?.flashFields, enumerable: false },
|
|
73
90
|
handler: { value: handler, enumerable: false },
|
|
74
91
|
url: { value: '', writable: true, enumerable: true },
|
|
75
92
|
toString: {
|
|
@@ -87,13 +104,24 @@ export function isServerAction(value: unknown): value is ServerAction<unknown, u
|
|
|
87
104
|
return typeof value === 'function' && (value as { __kayAction?: boolean }).__kayAction === true
|
|
88
105
|
}
|
|
89
106
|
|
|
90
|
-
|
|
91
|
-
|
|
107
|
+
class PayloadTooLargeError extends Error {
|
|
108
|
+
constructor() {
|
|
109
|
+
super('Payload Too Large')
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function parseActionBody(request: Request, maxRequestBodySize: number): Promise<unknown> {
|
|
114
|
+
const checked = await enforceRequestBodyLimit(request, maxRequestBodySize)
|
|
115
|
+
if (!checked.ok)
|
|
116
|
+
throw new PayloadTooLargeError()
|
|
117
|
+
const boundedReq = checked.request
|
|
118
|
+
|
|
119
|
+
const contentType = boundedReq.headers.get('content-type') ?? ''
|
|
92
120
|
if (contentType.includes('application/json'))
|
|
93
|
-
return
|
|
121
|
+
return boundedReq.json()
|
|
94
122
|
|
|
95
123
|
if (contentType.includes('multipart/form-data') || contentType.includes('application/x-www-form-urlencoded')) {
|
|
96
|
-
const fd = await
|
|
124
|
+
const fd = await boundedReq.formData()
|
|
97
125
|
const out: Record<string, unknown> = {}
|
|
98
126
|
for (const [key, val] of fd.entries()) {
|
|
99
127
|
if (key in out) {
|
|
@@ -110,13 +138,67 @@ async function parseActionBody(request: Request): Promise<unknown> {
|
|
|
110
138
|
return undefined
|
|
111
139
|
}
|
|
112
140
|
|
|
141
|
+
function encodedFlashSize(flash: ActionErrorFlash): number {
|
|
142
|
+
return Buffer.byteLength(encodeURIComponent(JSON.stringify(flash)), 'utf8')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function buildActionErrorFlash(
|
|
146
|
+
actionUrl: string,
|
|
147
|
+
fieldErrors: Record<string, string>,
|
|
148
|
+
rawBody: unknown,
|
|
149
|
+
flashFields?: readonly string[],
|
|
150
|
+
): string {
|
|
151
|
+
const safeErrors: Record<string, string> = {}
|
|
152
|
+
const base: ActionErrorFlash = {
|
|
153
|
+
action: actionUrl,
|
|
154
|
+
error: 'Validation failed',
|
|
155
|
+
fieldErrors: safeErrors,
|
|
156
|
+
}
|
|
157
|
+
for (const [k, msg] of Object.entries(fieldErrors)) {
|
|
158
|
+
const trimmed = msg.length > 256 ? msg.slice(0, 256) : msg
|
|
159
|
+
safeErrors[k] = trimmed
|
|
160
|
+
if (encodedFlashSize(base) > MAX_FLASH_COOKIE_BYTES) {
|
|
161
|
+
delete safeErrors[k]
|
|
162
|
+
break
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const allowSet = flashFields !== undefined ? new Set(flashFields) : undefined
|
|
167
|
+
const values: Record<string, string> = {}
|
|
168
|
+
if (rawBody !== null && typeof rawBody === 'object') {
|
|
169
|
+
for (const [k, v] of Object.entries(rawBody as Record<string, unknown>)) {
|
|
170
|
+
if (typeof v !== 'string' || v.length > MAX_FLASH_FIELD_CHARS)
|
|
171
|
+
continue
|
|
172
|
+
if (allowSet !== undefined) {
|
|
173
|
+
if (!allowSet.has(k))
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
else if (SENSITIVE_FIELD_RE.test(k)) {
|
|
177
|
+
continue
|
|
178
|
+
}
|
|
179
|
+
values[k] = v
|
|
180
|
+
const candidate: ActionErrorFlash = { ...base, values }
|
|
181
|
+
if (encodedFlashSize(candidate) > MAX_FLASH_COOKIE_BYTES)
|
|
182
|
+
delete values[k]
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const finalFlash: ActionErrorFlash = {
|
|
187
|
+
...base,
|
|
188
|
+
...(Object.keys(values).length > 0 ? { values } : {}),
|
|
189
|
+
}
|
|
190
|
+
return JSON.stringify(finalFlash)
|
|
191
|
+
}
|
|
192
|
+
|
|
113
193
|
export async function executeAction(
|
|
114
194
|
act: ServerAction<unknown, unknown>,
|
|
115
195
|
request: Request,
|
|
116
196
|
ctx: ActionContext,
|
|
117
197
|
trustedOrigins?: ReadonlySet<string>,
|
|
198
|
+
trustProxy = false,
|
|
199
|
+
maxRequestBodySize = DEFAULT_MAX_REQUEST_BODY_SIZE,
|
|
118
200
|
): Promise<Response> {
|
|
119
|
-
const originCheck = verifyOrigin(request, trustedOrigins)
|
|
201
|
+
const originCheck = verifyOrigin(request, trustedOrigins, trustProxy)
|
|
120
202
|
if (!originCheck.ok) {
|
|
121
203
|
return Response.json(
|
|
122
204
|
{ ok: false, status: 403, error: originCheck.reason },
|
|
@@ -131,15 +213,17 @@ export async function executeAction(
|
|
|
131
213
|
if (act.schema !== undefined) {
|
|
132
214
|
let rawBody: unknown
|
|
133
215
|
try {
|
|
134
|
-
rawBody = await parseActionBody(request)
|
|
216
|
+
rawBody = await parseActionBody(request, maxRequestBodySize)
|
|
135
217
|
}
|
|
136
|
-
catch {
|
|
218
|
+
catch (err) {
|
|
219
|
+
if (err instanceof PayloadTooLargeError)
|
|
220
|
+
return Response.json({ ok: false, status: 413, error: 'Payload Too Large' }, { status: 413 })
|
|
137
221
|
return Response.json({ ok: false, status: 400, error: 'Invalid request body' }, { status: 400 })
|
|
138
222
|
}
|
|
139
223
|
|
|
140
224
|
const checked = await validateSchema(act.schema, rawBody)
|
|
141
225
|
if (!checked.ok) {
|
|
142
|
-
// Zero-JS progressive HTML <form> submit: flash fieldErrors + submitted
|
|
226
|
+
// Zero-JS progressive HTML <form> submit: flash fieldErrors + safe submitted values (<4KB) and redirect 303 back to trusted Referer
|
|
143
227
|
if (!wantsJson) {
|
|
144
228
|
const referer = request.headers.get('referer')
|
|
145
229
|
if (referer) {
|
|
@@ -150,21 +234,9 @@ export async function executeAction(
|
|
|
150
234
|
catch {
|
|
151
235
|
refUrl = undefined
|
|
152
236
|
}
|
|
153
|
-
if (refUrl !== undefined && isTrustedOrigin(refUrl.origin, request, trustedOrigins)) {
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
for (const [k, v] of Object.entries(rawBody as Record<string, unknown>)) {
|
|
157
|
-
if (typeof v === 'string' && !/password|secret|token/i.test(k) && v.length <= 512)
|
|
158
|
-
values[k] = v
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
const flash: ActionErrorFlash = {
|
|
162
|
-
action: act.url,
|
|
163
|
-
error: 'Validation failed',
|
|
164
|
-
fieldErrors: checked.fieldErrors,
|
|
165
|
-
...(Object.keys(values).length > 0 ? { values } : {}),
|
|
166
|
-
}
|
|
167
|
-
ctx.cookies.set(ACTION_ERROR_COOKIE, JSON.stringify(flash), {
|
|
237
|
+
if (refUrl !== undefined && isTrustedOrigin(refUrl.origin, request, trustedOrigins, trustProxy)) {
|
|
238
|
+
const serializedFlash = buildActionErrorFlash(act.url, checked.fieldErrors, rawBody, act.flashFields)
|
|
239
|
+
ctx.cookies.set(ACTION_ERROR_COOKIE, serializedFlash, {
|
|
168
240
|
httpOnly: true,
|
|
169
241
|
sameSite: 'lax',
|
|
170
242
|
maxAge: 60,
|
|
@@ -195,7 +267,7 @@ export async function executeAction(
|
|
|
195
267
|
catch {
|
|
196
268
|
refUrl = undefined
|
|
197
269
|
}
|
|
198
|
-
if (refUrl !== undefined && isTrustedOrigin(refUrl.origin, request, trustedOrigins))
|
|
270
|
+
if (refUrl !== undefined && isTrustedOrigin(refUrl.origin, request, trustedOrigins, trustProxy))
|
|
199
271
|
return Response.redirect(refUrl.href, 303)
|
|
200
272
|
}
|
|
201
273
|
}
|