@kayjs/kay 2.0.0-beta.0 → 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.
Files changed (55) hide show
  1. package/package.json +1 -1
  2. package/src/attrs.ts +35 -0
  3. package/src/build/bundle.ts +615 -0
  4. package/src/build/compile.ts +32 -19
  5. package/src/build/files.ts +174 -0
  6. package/src/build/graph.ts +223 -0
  7. package/src/build/index.ts +255 -1461
  8. package/src/build/islands.ts +177 -0
  9. package/src/build/manifest.ts +227 -0
  10. package/src/build/plugin-host.ts +186 -0
  11. package/src/build/prerender.ts +94 -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/static.ts +73 -0
  16. package/src/cli/check.ts +300 -129
  17. package/src/cli/index.ts +297 -94
  18. package/src/cli/new.ts +58 -38
  19. package/src/compiler/analyze.ts +723 -93
  20. package/src/compiler/dom.ts +848 -762
  21. package/src/compiler/mistakes.ts +354 -0
  22. package/src/compiler/patch.ts +1 -1
  23. package/src/compiler/server-only.ts +172 -23
  24. package/src/compiler/shared.ts +485 -0
  25. package/src/compiler/ssr.ts +331 -342
  26. package/src/context.ts +120 -0
  27. package/src/dev/index.ts +358 -76
  28. package/src/dom/action.ts +114 -14
  29. package/src/dom/extras.ts +121 -83
  30. package/src/dom/index.ts +122 -80
  31. package/src/dom/island.ts +71 -36
  32. package/src/dom/loop.ts +28 -20
  33. package/src/index.ts +5 -1
  34. package/src/jsx-runtime.ts +432 -93
  35. package/src/plugins/context.ts +83 -0
  36. package/src/plugins/index.ts +285 -23
  37. package/src/reactivity/index.ts +237 -213
  38. package/src/reactivity/selector.ts +11 -9
  39. package/src/reactivity/store.ts +36 -11
  40. package/src/server/action.ts +157 -79
  41. package/src/server/compress.ts +197 -104
  42. package/src/server/cookies.ts +46 -16
  43. package/src/server/env.ts +18 -13
  44. package/src/server/http.ts +189 -77
  45. package/src/server/index.ts +1024 -878
  46. package/src/server/loader.ts +138 -0
  47. package/src/server/router.ts +69 -26
  48. package/src/server/security.ts +50 -58
  49. package/src/server/standard-schema.ts +42 -11
  50. package/src/server/static-dir.ts +144 -0
  51. package/src/ssr/context.ts +95 -10
  52. package/src/ssr/index.ts +167 -66
  53. package/src/style.ts +33 -0
  54. package/src/test/index.ts +5 -15
  55. package/src/unrenderable.ts +35 -0
package/src/style.ts ADDED
@@ -0,0 +1,33 @@
1
+ // The CSS text of a style object, shared by the server and the browser so both render the same declarations.
2
+
3
+ // Properties that take a bare number; any other number gets `px`.
4
+ const UNITLESS = new Set([
5
+ 'animation-iteration-count', 'aspect-ratio', 'border-image-outset', 'border-image-slice', 'border-image-width',
6
+ 'column-count', 'columns', 'fill-opacity', 'flex', 'flex-grow', 'flex-shrink', 'flood-opacity', 'font-weight',
7
+ 'grid-area', 'grid-column', 'grid-column-end', 'grid-column-start', 'grid-row', 'grid-row-end', 'grid-row-start',
8
+ 'line-clamp', '-webkit-line-clamp', 'line-height', 'opacity', 'order', 'orphans', 'scale', 'stop-opacity',
9
+ 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'tab-size',
10
+ 'widows', 'z-index', 'zoom',
11
+ ])
12
+ const UPPER = /[A-Z]/g
13
+ const MS_PREFIX = /^ms[A-Z]/
14
+
15
+ // `backgroundColor` becomes `background-color` and `msTransform` `-ms-transform`; a custom property keeps its case.
16
+ function cssName(key: string): string {
17
+ if (key.startsWith('--'))
18
+ return key
19
+ const name = key.replace(UPPER, c => `-${c.toLowerCase()}`)
20
+ return MS_PREFIX.test(key) ? `-${name}` : name
21
+ }
22
+
23
+ export function styleText(style: Record<string, unknown>): string {
24
+ let out = ''
25
+ for (const [key, value] of Object.entries(style)) {
26
+ if (value == null || value === '' || value === false)
27
+ continue
28
+ const name = cssName(key)
29
+ const px = typeof value === 'number' && !name.startsWith('--') && !UNITLESS.has(name)
30
+ out += `${name}:${value}${px ? 'px' : ''};`
31
+ }
32
+ return out
33
+ }
package/src/test/index.ts CHANGED
@@ -1,17 +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, ServerOptions } from '../server/index'
4
+ import type { KayApp, ServerRuntimeConfig } from '../server/index'
5
5
  import { build } from '../build/index'
6
- import { createServer } from '../server/index'
6
+ import { createServer, resolveServerConfig } from '../server/index'
7
7
 
8
- export interface TestAppOptions extends Omit<BuildOptions, 'compile' | 'static'> {
8
+ export interface TestAppOptions extends Omit<BuildOptions, 'compile' | 'static'>, Partial<ServerRuntimeConfig> {
9
9
  prebuilt?: boolean | undefined
10
- lang?: string | undefined
11
- security?: ServerOptions['security']
12
- trustedOrigins?: string[] | undefined
13
- trustProxy?: boolean | undefined
14
- maxRequestBodySize?: number | undefined
15
10
  }
16
11
 
17
12
  export interface TestApp {
@@ -32,19 +27,14 @@ export async function testApp(options: TestAppOptions): Promise<TestApp> {
32
27
  outDir: options.outDir,
33
28
  minify: options.minify ?? false,
34
29
  assetPrefix: options.assetPrefix,
30
+ site: options.site,
35
31
  plugins: options.plugins,
36
32
  })
37
33
 
38
34
  const app = await createServer({
39
35
  root: options.root,
40
36
  outDir: options.outDir,
41
- assetPrefix: options.assetPrefix,
42
- lang: options.lang,
43
- security: options.security,
44
- trustedOrigins: options.trustedOrigins,
45
- trustProxy: options.trustProxy,
46
- maxRequestBodySize: options.maxRequestBodySize,
47
- plugins: options.plugins,
37
+ ...resolveServerConfig(undefined, options),
48
38
  })
49
39
 
50
40
  const cookies = new Map<string, string>()
@@ -0,0 +1,35 @@
1
+ // Values whose String() is never what the author meant, shared by the SSR and DOM renderers so both refuse them alike.
2
+
3
+ // A value with its own toString, such as a server action printing its URL, renders as that string on purpose.
4
+ function hasOwnToString(value: object): boolean {
5
+ const { toString } = value
6
+ return typeof toString === 'function' && toString !== Function.prototype.toString && toString !== Object.prototype.toString && toString !== Array.prototype.toString
7
+ }
8
+
9
+ function describe(value: unknown, asClass: boolean): [string, string] | undefined {
10
+ if (typeof value === 'function')
11
+ return hasOwnToString(value) ? undefined : ['a function', 'call it, or pass it to an event prop']
12
+ if (typeof value !== 'object' || value === null)
13
+ return undefined
14
+ if (typeof (value as { then?: unknown }).then === 'function')
15
+ return ['a Promise', 'await it in an async component, or make that component the only child of <Suspense>']
16
+ if ('peek' in value && 'value' in value)
17
+ return ['a signal', 'read its .value']
18
+ if ('__html' in value)
19
+ return ['a { __html } object', 'pass the string to innerHTML, or wrap trusted HTML in raw()']
20
+ if (asClass && Array.isArray(value))
21
+ return ['an array', 'join the names into one string']
22
+ if (hasOwnToString(value))
23
+ return undefined
24
+ const proto = Object.getPrototypeOf(value)
25
+ if (proto === Object.prototype || proto === null)
26
+ return ['a plain object', 'render one of its fields']
27
+ return undefined
28
+ }
29
+
30
+ /** Throws when `value` would print as `[object Object]`, `[object Promise]` or a function's source; `place` is `text` or an attribute name. */
31
+ export function assertRenderable(value: unknown, place: string): void {
32
+ const found = describe(value, place === 'class')
33
+ if (found !== undefined)
34
+ throw new TypeError(`kay: cannot render ${found[0]} as ${place === 'text' ? 'text' : `the ${place} attribute`}; ${found[1]}.`)
35
+ }