@kayjs/kay 2.0.0-beta.0 → 2.0.0-beta.3

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.
Files changed (69) hide show
  1. package/package.json +1 -1
  2. package/src/attrs.ts +35 -0
  3. package/src/build/bundle.ts +617 -0
  4. package/src/build/compile.ts +27 -51
  5. package/src/build/files.ts +174 -0
  6. package/src/build/graph.ts +223 -0
  7. package/src/build/index.ts +272 -1466
  8. package/src/build/islands.ts +177 -0
  9. package/src/build/manifest.ts +229 -0
  10. package/src/build/plugin-host.ts +186 -0
  11. package/src/build/prerender.ts +102 -0
  12. package/src/build/report.ts +157 -0
  13. package/src/build/resolve.ts +51 -0
  14. package/src/build/routes.ts +163 -0
  15. package/src/build/server-entry.ts +38 -0
  16. package/src/build/static.ts +73 -0
  17. package/src/cli/check.ts +300 -129
  18. package/src/cli/index.ts +297 -94
  19. package/src/cli/new.ts +58 -38
  20. package/src/compiler/analyze.ts +723 -93
  21. package/src/compiler/dom.ts +848 -762
  22. package/src/compiler/mistakes.ts +354 -0
  23. package/src/compiler/patch.ts +1 -1
  24. package/src/compiler/server-only.ts +172 -23
  25. package/src/compiler/shared.ts +485 -0
  26. package/src/compiler/ssr.ts +331 -342
  27. package/src/context.ts +120 -0
  28. package/src/dev/index.ts +339 -76
  29. package/src/dom/action.ts +114 -14
  30. package/src/dom/extras.ts +121 -83
  31. package/src/dom/index.ts +122 -80
  32. package/src/dom/island.ts +71 -36
  33. package/src/dom/loop.ts +28 -20
  34. package/src/index.ts +5 -1
  35. package/src/jsx-runtime.ts +432 -93
  36. package/src/plugins/config.ts +49 -0
  37. package/src/plugins/context.ts +61 -0
  38. package/src/plugins/index.ts +10 -123
  39. package/src/plugins/load.ts +38 -0
  40. package/src/plugins/order.ts +107 -0
  41. package/src/plugins/services.ts +23 -0
  42. package/src/plugins/types.ts +197 -0
  43. package/src/reactivity/index.ts +237 -213
  44. package/src/reactivity/selector.ts +11 -9
  45. package/src/reactivity/store.ts +36 -11
  46. package/src/server/action-registry.ts +72 -0
  47. package/src/server/action.ts +157 -79
  48. package/src/server/app-entry.ts +23 -0
  49. package/src/server/assets.ts +182 -0
  50. package/src/server/compress.ts +197 -104
  51. package/src/server/cookies.ts +46 -16
  52. package/src/server/document.ts +192 -0
  53. package/src/server/env.ts +18 -13
  54. package/src/server/errors.ts +123 -0
  55. package/src/server/http.ts +203 -77
  56. package/src/server/index.ts +420 -973
  57. package/src/server/loader.ts +113 -0
  58. package/src/server/page.ts +107 -0
  59. package/src/server/plugin-host.ts +167 -0
  60. package/src/server/router.ts +69 -26
  61. package/src/server/security.ts +90 -59
  62. package/src/server/standard-schema.ts +42 -11
  63. package/src/server/static-dir.ts +163 -0
  64. package/src/server/stream.ts +94 -0
  65. package/src/ssr/context.ts +95 -10
  66. package/src/ssr/index.ts +167 -66
  67. package/src/style.ts +33 -0
  68. package/src/test/index.ts +24 -18
  69. package/src/unrenderable.ts +35 -0
package/src/context.ts ADDED
@@ -0,0 +1,120 @@
1
+ // Universal component context (createContext / useContext) and hydration-safe useId().
2
+ // Browser-pure: zero Node imports. SSR wires its per-request AsyncLocalStorage bridge via registerServerContextBridge().
3
+
4
+ import type { KayChild } from './jsx-runtime'
5
+ import type { IdScope, Owner } from './reactivity/index'
6
+ import { createOwner, getOwner, runWithOwner } from './reactivity/index'
7
+
8
+ export interface ServerContextBridge {
9
+ getContext(key: symbol): { found: true, value: unknown } | undefined
10
+ runWithContext<T>(key: symbol, value: unknown, fn: () => T): T
11
+ nextId(): string | undefined
12
+ head(props: { children?: KayChild }): KayChild
13
+ suspense(props: { fallback?: KayChild, error?: KayChild | ((error: unknown) => KayChild), children?: KayChild | Promise<KayChild> }): KayChild
14
+ }
15
+
16
+ const BRIDGE_KEY = Symbol.for('kay.server.context.bridge')
17
+
18
+ function getServerBridge(): ServerContextBridge | undefined {
19
+ return (globalThis as Record<symbol, ServerContextBridge | undefined>)[BRIDGE_KEY]
20
+ }
21
+
22
+ /** Internal: registered by `kay/ssr` on the server so `Head`, `Suspense`, `useContext`, and `useId` stay request-scoped without pulling `node:async_hooks` into client bundles. */
23
+ export function registerServerContextBridge(bridge: ServerContextBridge): void {
24
+ ;(globalThis as Record<symbol, ServerContextBridge | undefined>)[BRIDGE_KEY] = bridge
25
+ }
26
+
27
+ /** Collects document `<head>` tags during SSR; no-op in the browser. */
28
+ export function Head(props: { children?: KayChild }): KayChild {
29
+ const bridge = getServerBridge()
30
+ return bridge !== undefined ? bridge.head(props) : null
31
+ }
32
+
33
+ /** Streaming SSR boundary: flushes `fallback` immediately and streams resolved async children. */
34
+ export function Suspense(props: { fallback?: KayChild, error?: KayChild | ((error: unknown) => KayChild), children?: KayChild | Promise<KayChild> }): KayChild {
35
+ const bridge = getServerBridge()
36
+ if (bridge !== undefined)
37
+ return bridge.suspense(props)
38
+ return props.children instanceof Promise ? (props.fallback ?? null) : (props.children ?? null)
39
+ }
40
+
41
+ export interface Context<T> {
42
+ readonly id: symbol
43
+ readonly defaultValue: T
44
+ Provider: (props: { value: T, children?: KayChild | (() => KayChild) }) => KayChild
45
+ }
46
+
47
+ function findOwnerContext(owner: Owner | undefined, key: symbol): { found: true, value: unknown } | undefined {
48
+ for (let o = owner; o !== undefined; o = o.o) {
49
+ if (o.cx !== undefined && o.cx.has(key))
50
+ return { found: true, value: o.cx.get(key) }
51
+ }
52
+ return undefined
53
+ }
54
+
55
+ function findOwnerIdScope(owner: Owner | undefined): IdScope | undefined {
56
+ for (let o = owner; o !== undefined; o = o.o) {
57
+ if (o.id !== undefined)
58
+ return o.id
59
+ }
60
+ return undefined
61
+ }
62
+
63
+ /** Run `fn` with `ctx` bound to `value` on both server (SSR) and browser (reactive owner tree). */
64
+ export function provideContext<T, R>(ctx: Context<T>, value: T, fn: () => R): R {
65
+ const owner = getOwner()
66
+ if (owner !== undefined) {
67
+ const scope = createOwner(owner)
68
+ ;(scope.cx ??= new Map()).set(ctx.id, value)
69
+ return runWithOwner(scope, fn)
70
+ }
71
+ const bridge = getServerBridge()
72
+ if (bridge !== undefined)
73
+ return bridge.runWithContext(ctx.id, value, fn)
74
+ return fn()
75
+ }
76
+
77
+ export function createContext<T>(defaultValue: T): Context<T> {
78
+ const id = Symbol('kay.context')
79
+ const ctx: Context<T> = {
80
+ id,
81
+ defaultValue,
82
+ Provider(props) {
83
+ return provideContext(ctx, props.value, () => {
84
+ const c = props.children
85
+ return typeof c === 'function' ? (c as () => KayChild)() : c
86
+ })
87
+ },
88
+ }
89
+ return ctx
90
+ }
91
+
92
+ export function useContext<T>(ctx: Context<T>): T {
93
+ const owner = getOwner()
94
+ if (owner !== undefined) {
95
+ const hit = findOwnerContext(owner, ctx.id)
96
+ if (hit !== undefined)
97
+ return hit.value as T
98
+ }
99
+ const bridge = getServerBridge()
100
+ if (bridge !== undefined) {
101
+ const hit = bridge.getContext(ctx.id)
102
+ if (hit !== undefined)
103
+ return hit.value as T
104
+ }
105
+ return ctx.defaultValue
106
+ }
107
+
108
+ let fallbackIdSeq = 0
109
+
110
+ /** Generates a deterministic, hydration-safe unique ID scoped to the current island or SSR request. */
111
+ export function useId(prefix = 'k'): string {
112
+ const scope = findOwnerIdScope(getOwner())
113
+ if (scope !== undefined)
114
+ return `${prefix}-${scope.p}${scope.n++}`
115
+ const bridge = getServerBridge()
116
+ const serverId = bridge?.nextId()
117
+ if (serverId !== undefined)
118
+ return `${prefix}-${serverId}`
119
+ return `${prefix}-g${fallbackIdSeq++}`
120
+ }