brumate 0.2.0 → 0.4.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.
Files changed (138) hide show
  1. package/README.md +2 -2
  2. package/check.d.ts +49 -0
  3. package/dist/{boot-D-IdPy7B.d.ts → boot-DLlRmD9z.d.ts} +11 -10
  4. package/dist/cli.js +275 -15
  5. package/dist/cli.js.map +1 -1
  6. package/dist/client-BvDsC6Qr.js +3463 -0
  7. package/dist/client-BvDsC6Qr.js.map +1 -0
  8. package/dist/client-DBwL2wKQ.d.ts +669 -0
  9. package/dist/{client-routes-JTbGtukf.js → client-routes-BtGpYCef.js} +60 -30
  10. package/dist/client-routes-BtGpYCef.js.map +1 -0
  11. package/dist/{cookies-Av5WFmcf.js → cookies-Cnbf4w-S.js} +15 -7
  12. package/dist/cookies-Cnbf4w-S.js.map +1 -0
  13. package/dist/{defer-D0bo1Gqx.js → defer-CKWZLv2K.js} +8 -4
  14. package/dist/defer-CKWZLv2K.js.map +1 -0
  15. package/dist/devtools-4uTPwR_v.js +597 -0
  16. package/dist/devtools-4uTPwR_v.js.map +1 -0
  17. package/dist/devtools-panel-Be9pxYgv.js +1994 -0
  18. package/dist/devtools-panel-Be9pxYgv.js.map +1 -0
  19. package/dist/events-LgLdp1R7.js +114 -0
  20. package/dist/events-LgLdp1R7.js.map +1 -0
  21. package/dist/features-CHY5Ekku.js +30 -0
  22. package/dist/features-CHY5Ekku.js.map +1 -0
  23. package/dist/files-WW4RlajL.js +506 -0
  24. package/dist/files-WW4RlajL.js.map +1 -0
  25. package/dist/forms-j1xPAR14.d.ts +281 -0
  26. package/dist/forms-nXK7-Lv_.js +440 -0
  27. package/dist/forms-nXK7-Lv_.js.map +1 -0
  28. package/dist/highlight.d.ts +17 -0
  29. package/dist/highlight.js +278 -0
  30. package/dist/highlight.js.map +1 -0
  31. package/dist/html-BtKRyi7g.js +305 -0
  32. package/dist/html-BtKRyi7g.js.map +1 -0
  33. package/dist/html-Bw9Qwu6F.d.ts +107 -0
  34. package/dist/index.d.ts +142 -11
  35. package/dist/index.js +641 -106
  36. package/dist/index.js.map +1 -1
  37. package/dist/{inspect-BJtACnQp.js → inspect-Y6y4uNZp.js} +4333 -681
  38. package/dist/inspect-Y6y4uNZp.js.map +1 -0
  39. package/dist/{loader-B18i3_-T.js → loader-BjKGJzLN.js} +21 -14
  40. package/dist/loader-BjKGJzLN.js.map +1 -0
  41. package/dist/logo-DQ_OYhBi.js +10 -0
  42. package/dist/logo-DQ_OYhBi.js.map +1 -0
  43. package/dist/{navigate-BwuZveDF.js → navigate-CfOr-TRF.js} +130 -24
  44. package/dist/navigate-CfOr-TRF.js.map +1 -0
  45. package/dist/nesting-DA5vJw9X.js +265 -0
  46. package/dist/nesting-DA5vJw9X.js.map +1 -0
  47. package/dist/produce-B7X9ori9.js +402 -0
  48. package/dist/produce-B7X9ori9.js.map +1 -0
  49. package/dist/produce-DIgHBVwL.d.ts +148 -0
  50. package/dist/{rpc-B-Zkn6Ch.js → rpc-Q-XkLwFh.js} +28 -13
  51. package/dist/rpc-Q-XkLwFh.js.map +1 -0
  52. package/dist/runtime/batch.js +9 -7
  53. package/dist/runtime/batch.js.map +1 -1
  54. package/dist/runtime/client.d.ts +4 -4
  55. package/dist/runtime/client.js +4 -4
  56. package/dist/runtime/devtools.d.ts +172 -5
  57. package/dist/runtime/devtools.js +3 -1
  58. package/dist/runtime/env.js +14 -2
  59. package/dist/runtime/env.js.map +1 -1
  60. package/dist/runtime/events.d.ts +38 -1
  61. package/dist/runtime/events.js +1 -1
  62. package/dist/runtime/forms.d.ts +2 -0
  63. package/dist/runtime/forms.js +1 -0
  64. package/dist/runtime/gather.d.ts +25 -0
  65. package/dist/runtime/gather.js +33 -0
  66. package/dist/runtime/gather.js.map +1 -0
  67. package/dist/runtime/hot.d.ts +1 -1
  68. package/dist/runtime/hot.js +5 -2
  69. package/dist/runtime/hot.js.map +1 -1
  70. package/dist/runtime/html.d.ts +2 -2
  71. package/dist/runtime/html.js +1 -1
  72. package/dist/runtime/loader.d.ts +2 -2
  73. package/dist/runtime/loader.js +2 -2
  74. package/dist/runtime/routes.js +4 -1
  75. package/dist/runtime/routes.js.map +1 -1
  76. package/dist/runtime/server.d.ts +5 -4
  77. package/dist/runtime/server.js +5 -5
  78. package/dist/runtime/signals.d.ts +75 -2
  79. package/dist/runtime/signals.js +44 -2
  80. package/dist/runtime/signals.js.map +1 -1
  81. package/dist/runtime/static-loader.d.ts +2 -2
  82. package/dist/runtime/static-loader.js +1 -1
  83. package/dist/runtime/testing.d.ts +18 -3
  84. package/dist/runtime/testing.js +150 -33
  85. package/dist/runtime/testing.js.map +1 -1
  86. package/dist/runtime/values.d.ts +2 -0
  87. package/dist/runtime/values.js +24 -0
  88. package/dist/runtime/values.js.map +1 -0
  89. package/dist/{server-BA9SJ2Vm.d.ts → server-BvPIg9pT.d.ts} +363 -32
  90. package/dist/{server-BqfM95r1.js → server-DjyOVJtJ.js} +1309 -220
  91. package/dist/server-DjyOVJtJ.js.map +1 -0
  92. package/dist/{static-loader-BzE9SRaj.d.ts → static-loader-BGgdp_Y5.d.ts} +2 -2
  93. package/dist/static-loader-BZR17hSw.js +312 -0
  94. package/dist/static-loader-BZR17hSw.js.map +1 -0
  95. package/dist/{stream-JaMHMdBD.js → stream-BP5AVmg5.js} +182 -17
  96. package/dist/stream-BP5AVmg5.js.map +1 -0
  97. package/dist/values-B8C7CLvG.d.ts +63 -0
  98. package/dist/vite.d.ts +20 -20
  99. package/dist/vite.js +2031 -539
  100. package/dist/vite.js.map +1 -1
  101. package/elements.d.ts +951 -0
  102. package/forms/forms.brm.ts +141 -0
  103. package/package.json +41 -3
  104. package/skills/brumate/SKILL.md +58 -0
  105. package/skills/brumate/references/errors.md +2078 -0
  106. package/skills/brumate/references/events.md +117 -0
  107. package/skills/brumate/references/signals.md +61 -0
  108. package/skills/brumate/references/state.md +235 -0
  109. package/skills/brumate/references/syntax.md +250 -0
  110. package/types.d.ts +4 -4
  111. package/dist/client-C0UCNL3J.js +0 -1643
  112. package/dist/client-C0UCNL3J.js.map +0 -1
  113. package/dist/client-MD0ZmcFR.d.ts +0 -354
  114. package/dist/client-routes-JTbGtukf.js.map +0 -1
  115. package/dist/cookies-Av5WFmcf.js.map +0 -1
  116. package/dist/defer-D0bo1Gqx.js.map +0 -1
  117. package/dist/devtools-C50VH8gZ.js +0 -113
  118. package/dist/devtools-C50VH8gZ.js.map +0 -1
  119. package/dist/devtools-panel-AYjfwPhj.js +0 -435
  120. package/dist/devtools-panel-AYjfwPhj.js.map +0 -1
  121. package/dist/events-BKWRqtMg.js +0 -52
  122. package/dist/events-BKWRqtMg.js.map +0 -1
  123. package/dist/files-Bnn1JqHx.js +0 -192
  124. package/dist/files-Bnn1JqHx.js.map +0 -1
  125. package/dist/html-bHZbkK-h.js +0 -170
  126. package/dist/html-bHZbkK-h.js.map +0 -1
  127. package/dist/html-y1q7-t7D.d.ts +0 -60
  128. package/dist/inspect-BJtACnQp.js.map +0 -1
  129. package/dist/loader-B18i3_-T.js.map +0 -1
  130. package/dist/navigate-BwuZveDF.js.map +0 -1
  131. package/dist/produce-CWfJ4byV.js +0 -167
  132. package/dist/produce-CWfJ4byV.js.map +0 -1
  133. package/dist/produce-D4K7qpaL.d.ts +0 -72
  134. package/dist/rpc-B-Zkn6Ch.js.map +0 -1
  135. package/dist/server-BqfM95r1.js.map +0 -1
  136. package/dist/static-loader-DOygOFj5.js +0 -216
  137. package/dist/static-loader-DOygOFj5.js.map +0 -1
  138. package/dist/stream-JaMHMdBD.js.map +0 -1
@@ -0,0 +1,141 @@
1
+ // `createForm`, the component side of progressive forms. It is a module
2
+ // function, so the compiler copies it into the component that calls it, and
3
+ // its state resumes with the page.
4
+
5
+ import { prevent } from 'brumate/events';
6
+ import { $action, $effect, $handler, $id, $memo, $signal } from 'brumate/signals';
7
+ import {
8
+ DEFAULT_SUBMIT,
9
+ applyLocalIntent,
10
+ checkField,
11
+ checkedSince,
12
+ getFormErrors,
13
+ formFields,
14
+ validateValues,
15
+ } from 'brumate/forms/core';
16
+ import type { CheckedErrors, Intent, LocalChanges, SubmissionResult } from 'brumate/forms/core';
17
+ import type { StandardSchemaV1 } from 'brumate/server';
18
+
19
+ export type {
20
+ Field,
21
+ FieldAttributes,
22
+ FieldState,
23
+ Fields,
24
+ IntentAttributes,
25
+ ListItem,
26
+ SubmissionResult,
27
+ } from 'brumate/forms/core';
28
+
29
+ /**
30
+ * The values of a form: the input of its schema, or the type of the default
31
+ * value when the schema does not type its input.
32
+ */
33
+ export type FormValue<S extends StandardSchemaV1, D> =
34
+ unknown extends StandardSchemaV1.InferInput<S> ? D : StandardSchemaV1.InferInput<S>;
35
+
36
+ /** The options of `createForm`. */
37
+ export interface FormOptions<T> {
38
+ /**
39
+ * The values that the fields show before a submit. It names every field
40
+ * of the form, and a list of objects has one item, which new items take
41
+ * the fields of.
42
+ */
43
+ defaultValue?: T;
44
+ }
45
+
46
+ /**
47
+ * The state of a form that posts to a server function, which answers with
48
+ * `parseSubmission(…).reply()`. It gives the attributes of the form, of its
49
+ * fields and of the buttons of its lists, with the posted values and the
50
+ * errors of the last submit. The form works without scripts, and with
51
+ * scripts it posts without loading the page.
52
+ *
53
+ * ```brm
54
+ * <script>
55
+ * import { createForm } from 'brumate/forms';
56
+ *
57
+ * const { attrs, fields, errors, defaultSubmit } = createForm(save, Order, {
58
+ * defaultValue: { email: '' },
59
+ * });
60
+ * </script>
61
+ *
62
+ * <form action=save {...attrs}>
63
+ * <button {...defaultSubmit}></button>
64
+ * <input {...fields.email.attrs} type="email">
65
+ * </form>
66
+ * ```
67
+ */
68
+ export function createForm<S extends StandardSchemaV1, D = StandardSchemaV1.InferInput<S>>(
69
+ action: (form: FormData) => Promise<SubmissionResult>,
70
+ schema: S,
71
+ options: FormOptions<D> = {},
72
+ ) {
73
+ const result = $action(action);
74
+ const id = $id();
75
+ // The errors that the browser found since the last reply.
76
+ let checked = $signal<CheckedErrors | undefined>(undefined);
77
+ // The changes of lists that the browser made since the last reply.
78
+ let local = $signal<LocalChanges | undefined>(undefined);
79
+
80
+ // Applies the intent of a button of a list at once. Its `prevent` stops the post of the form,
81
+ // even before this code loads.
82
+ async function change(intent: Intent) {
83
+ const next = applyLocalIntent(
84
+ document.getElementById(id) as HTMLFormElement | null,
85
+ intent,
86
+ local,
87
+ result,
88
+ );
89
+ if (!next) {
90
+ return;
91
+ }
92
+ local = next;
93
+ // The errors that show follow the items to where they are now.
94
+ const current = checkedSince(checked, result);
95
+ if (current) {
96
+ checked = { ...current, ...(await validateValues(schema, next.value)) };
97
+ }
98
+ }
99
+ const press = (intent: Intent) => $handler(prevent(change), intent);
100
+
101
+ const fields = $memo(
102
+ formFields(id, options.defaultValue as FormValue<S, D> | undefined, result, {
103
+ checked,
104
+ local,
105
+ press,
106
+ }),
107
+ );
108
+ const errors = $memo(getFormErrors(result, checked));
109
+
110
+ // Validates with the schema when the user leaves a field, and as the user types in a field
111
+ // that shows its errors. The code of the schema loads with the first of these events.
112
+ async function check(event: Event) {
113
+ const next = await checkField(
114
+ schema,
115
+ document.getElementById(id) as HTMLFormElement | null,
116
+ (event.target as HTMLInputElement).name,
117
+ event.type === 'focusout',
118
+ checked,
119
+ result,
120
+ );
121
+ if (next) {
122
+ checked = next;
123
+ }
124
+ }
125
+
126
+ // A reply that resets the form sets the fields back to their default values,
127
+ // which the browser keeps in their attributes.
128
+ $effect(() => {
129
+ if (result?.reset) {
130
+ (document.getElementById(id) as HTMLFormElement | null)?.reset();
131
+ }
132
+ });
133
+
134
+ return {
135
+ result,
136
+ fields,
137
+ errors,
138
+ attrs: { id, onFocusOut: $handler(check), onInput: $handler(check) },
139
+ defaultSubmit: DEFAULT_SUBMIT,
140
+ };
141
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brumate",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "A component language and compiler for resumable web apps",
5
5
  "homepage": "https://github.com/lxsmnsyc/brumate",
6
6
  "bugs": "https://github.com/lxsmnsyc/brumate/issues",
@@ -16,7 +16,11 @@
16
16
  },
17
17
  "files": [
18
18
  "dist",
19
- "types.d.ts"
19
+ "types.d.ts",
20
+ "elements.d.ts",
21
+ "check.d.ts",
22
+ "forms",
23
+ "skills"
20
24
  ],
21
25
  "type": "module",
22
26
  "exports": {
@@ -71,6 +75,26 @@
71
75
  "types": "./dist/runtime/events.d.ts",
72
76
  "default": "./dist/runtime/events.js"
73
77
  },
78
+ "./stream": {
79
+ "types": "./dist/runtime/gather.d.ts",
80
+ "default": "./dist/runtime/gather.js"
81
+ },
82
+ "./serial": {
83
+ "types": "./dist/runtime/values.d.ts",
84
+ "default": "./dist/runtime/values.js"
85
+ },
86
+ "./highlight": {
87
+ "types": "./dist/highlight.d.ts",
88
+ "default": "./dist/highlight.js"
89
+ },
90
+ "./forms": {
91
+ "types": "./forms/forms.brm.ts",
92
+ "default": "./forms/forms.brm.ts"
93
+ },
94
+ "./forms/core": {
95
+ "types": "./dist/runtime/forms.d.ts",
96
+ "default": "./dist/runtime/forms.js"
97
+ },
74
98
  "./devtools": {
75
99
  "types": "./dist/runtime/devtools.d.ts",
76
100
  "default": "./dist/runtime/devtools.js"
@@ -87,12 +111,22 @@
87
111
  "types": "./dist/runtime/env.d.ts",
88
112
  "default": "./dist/runtime/env.js"
89
113
  },
114
+ "./elements": {
115
+ "types": "./elements.d.ts"
116
+ },
117
+ "./check": {
118
+ "types": "./check.d.ts"
119
+ },
90
120
  "./package.json": "./package.json"
91
121
  },
92
122
  "publishConfig": {
93
123
  "access": "public"
94
124
  },
95
125
  "dependencies": {
126
+ "@twinkleplop/core": "0.3.1",
127
+ "@twinkleplop/css": "0.1.7",
128
+ "@twinkleplop/theme-github": "0.2.4",
129
+ "@twinkleplop/tsx": "0.1.7",
96
130
  "oxc-parser": "^0.154.0",
97
131
  "postcss": "^8.5.31",
98
132
  "postcss-selector-parser": "^7.1.6",
@@ -130,9 +164,13 @@
130
164
  "engines": {
131
165
  "node": ">=22"
132
166
  },
167
+ "brumate": {
168
+ "version": 1,
169
+ "dir": "forms"
170
+ },
133
171
  "scripts": {
134
172
  "build": "tsdown",
135
173
  "test": "vitest run",
136
- "typecheck": "tsc --noEmit"
174
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.elements.json"
137
175
  }
138
176
  }
@@ -0,0 +1,58 @@
1
+ ---
2
+ name: brumate
3
+ description: Writes and fixes brumate components, the `.brm` files, with their state, events, control flow and server functions. Use it when a task creates or edits a `.brm` file, or reads an error of the brumate compiler.
4
+ ---
5
+
6
+ # Writing .brm files
7
+
8
+ A `.brm` file is a component: scripts for its state, an optional `<style>`, and markup. The server renders it, and the browser gets only the code that an event, an effect or a changing value reaches. Write it as below, because the compiler reads the form of the code, and it reports the rest as an error.
9
+
10
+ ```brm
11
+ <script>
12
+ import { $memo, $signal } from 'brumate/signals';
13
+
14
+ let count = $signal(0);
15
+ const double = $memo(count * 2);
16
+ </script>
17
+
18
+ <button onClick=(() => count++)>Clicked {count} times</button>
19
+ <p>Double: {double}</p>
20
+ ```
21
+
22
+ ## Rules that agents get wrong
23
+
24
+ - Compile-time functions come from `brumate/signals`: `$signal`, `$memo`, `$props`, `$effect`, `$client`, `$id`, `$bind` and the others. Import each one that the file calls.
25
+ - A signal is `let name = $signal(value)`. A memo is `const name = $memo(expression)`. Read and write them as plain variables: `count++`, `name = 'Ada'`. There is no `.value`.
26
+ - Never write a signal in the setup code of the script. Only handlers, effects, `$client` callbacks and functions that they call write. Give the start value in the call: `$signal(5)`.
27
+ - Keep only data in signals and memos: strings, numbers, booleans, `null`, plain objects and arrays, `Date`, `Map`, `Set`, `URL`, typed arrays and the like. Never put a function, a class instance or a regular expression in one, because the server cannot send it to the browser. Use a plain function in the script, or a function prop, and send a regular expression as its source. A class that the app sends needs a plugin in the serial entry (`src/serial.ts`). See `website/docs/reference-serialization.md`.
28
+ - Never change a value in place. `todos.push(todo)` updates nothing. Assign a new value, or use `$produce(todos, (draft) => { … })`.
29
+ - An attribute value is a quoted string, a plain name, a property path or a call, or an expression in parentheses. A value with an operator, such as `?:`, `+`, `>` or `&&`, or an arrow function, needs parentheses: `disabled=(count > 3)`. A bare `title=!closed` and `data-a=-1` work.
30
+ - Use the shorthands: `<a {href}>`, `<button {onClick}>`, `<input {...rest}>`.
31
+ - Write a branch on the line of the closing brace, as the style: `} #else if (…) {` and `} #else {`. The compiler also accepts `#else` on the next line.
32
+ - A `#for` has a key: `#for (item, index of items by item.id) { … }`. Without `by`, each item is its own key.
33
+ - `$effect` never runs on mount. Its first run is after the first change of a value it reads. Code that must run when the page loads or comes into view is `$client(callback, trigger)`. The default trigger is `'visible'`.
34
+ - Reach for a handler first, then `$memo`, and `$effect` last. An effect is only for work outside the page's state, such as an element method, a cookie, `document.title`, a timer or a library. Never write an effect that only sets a signal from other signals: that value is a `$memo`, or a write in the handler.
35
+ - An element that browser code needs is `ref=$bind(name)`, where `name` is a signal. A bare `ref=name` is an error.
36
+ - To let a child write a value, destructure its prop with `let` and type it `Bindable<T>`. The parent passes `$bind(signal)`.
37
+ - A component never receives a signal. A function prop writes the parent's value: `onChange=((next) => (count = next))`.
38
+ - Markup is never a value in code or in an attribute. Use `#template name(parameters) { … }` and call it as `{name(arguments)}`.
39
+ - Browser wrappers of a handler come from `brumate/events`: `prevent`, `stop`, `once`, `capture`, `transition`. Write them in the attribute, with keys as string literals. `prevent(handler, 'Enter')` prevents only Enter with Ctrl, Meta and Alt up. `'Mod+ArrowDown'` names Ctrl or Meta. Calling `event.preventDefault()` in a handler is too late.
40
+ - A server function is an `async` function with `'use server'` on its first line. It reads no variable of the instance script. Pass values as arguments.
41
+ - Do not use `$ref`, `$watch`, `$settled`, `$cleanup`, `$store`, `$get`, `$set`, or modifiers such as `onSubmit:prevent`.
42
+
43
+ ## References
44
+
45
+ Read the file for the part of the work that you do.
46
+
47
+ - [references/syntax.md](references/syntax.md): the parts of a file, attributes, props, children, templates and control flow.
48
+ - [references/signals.md](references/signals.md): every function of `brumate/signals`, with what it returns and where it goes.
49
+ - [references/state.md](references/state.md): signals, memos, async data, effects, `$client` and refs.
50
+ - [references/events.md](references/events.md): handlers, the wrappers of `brumate/events`, and `$handler`.
51
+ - [references/errors.md](references/errors.md): every compile error, its cause, and the form to write instead.
52
+
53
+ ## Checking your work
54
+
55
+ - `npx brumate-tsc --noEmit` type-checks the project, `.brm` files included.
56
+ - `npx brumate check --json` compiles every `.brm` file and runs the type check. It prints one JSON object per problem, with `file`, `range`, `message`, `code` and `link`, and exits with 1 on an error.
57
+ - A compile error names the file, the line and the column, and ends with a code such as `[BRM012]`. Look its message or its code up in [references/errors.md](references/errors.md).
58
+ - The docs of brumate, in `website/docs` of its repository, cover each topic in full.