akanjs 3.0.0-alpha.71 → 3.0.0-alpha.73

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 (188) hide show
  1. package/client/cookie.ts +21 -34
  2. package/client/csrTypes.ts +0 -12
  3. package/client/frameConfig.ts +13 -0
  4. package/common/Logger.ts +13 -6
  5. package/common/TrustedProxy.ts +134 -0
  6. package/common/clamp.ts +2 -0
  7. package/common/dynamicRecord.ts +12 -0
  8. package/common/index.ts +5 -1
  9. package/common/isValidDate.ts +1 -1
  10. package/common/requestOrigin.ts +32 -0
  11. package/common/{httpClient.ts → restClient.ts} +16 -9
  12. package/constant/types.ts +22 -0
  13. package/dictionary/base.dictionary.ts +4 -0
  14. package/dictionary/trans.ts +24 -5
  15. package/document/dataLoader.ts +0 -4
  16. package/document/filterMeta.ts +2 -2
  17. package/document/loaderInfo.ts +4 -1
  18. package/fetch/client/fetchClient.ts +65 -88
  19. package/fetch/client/fetchHandle.ts +57 -0
  20. package/fetch/client/httpClient.ts +42 -14
  21. package/fetch/client/sliceInitHandle.ts +101 -0
  22. package/fetch/fetchType/appliedReturn.type.ts +64 -1
  23. package/fetch/fetchType/sliceFetch.type.ts +5 -5
  24. package/fetch/requestStorage.ts +19 -5
  25. package/index.ts +16 -2
  26. package/local/apps/serverLifecycle/serverLifecycle-local.db-shm +0 -0
  27. package/local/apps/serverLifecycle/serverLifecycle-local_solid.db-shm +0 -0
  28. package/package.json +1 -1
  29. package/server/akanApp.ts +3 -19
  30. package/server/akanAppHeaders.ts +10 -4
  31. package/server/akanOption.ts +14 -1
  32. package/server/akanServer.ts +3 -0
  33. package/server/cachePolicy.ts +2 -11
  34. package/server/decorators.ts +12 -3
  35. package/server/devtools/constantSerializer.ts +8 -1
  36. package/server/di/diLifecycle.ts +21 -1
  37. package/server/di/resolveHierarchy.ts +3 -3
  38. package/server/imageOptimizer.ts +2 -17
  39. package/server/mcp/McpDispatcher.ts +8 -2
  40. package/server/resolver/database.resolver.ts +13 -8
  41. package/server/resolver/signal.resolver.ts +3 -3
  42. package/server/routeTreeBuilder.ts +1 -15
  43. package/server/routing/apiRouter.ts +4 -5
  44. package/server/routing/appWsData.ts +9 -6
  45. package/server/staticPath.ts +34 -0
  46. package/server/webRouter.ts +35 -41
  47. package/service/adapt.ts +1 -5
  48. package/service/predefinedAdaptor/cache.adaptor.ts +8 -4
  49. package/service/predefinedAdaptor/database.adaptor.ts +37 -1766
  50. package/service/predefinedAdaptor/sql/PendingStoreEnsures.ts +22 -0
  51. package/service/predefinedAdaptor/sql/QueryCompiler.ts +225 -0
  52. package/service/predefinedAdaptor/sql/SqlDocumentStore.ts +812 -0
  53. package/service/predefinedAdaptor/sql/UpdateCompiler.ts +67 -0
  54. package/service/predefinedAdaptor/sql/dialect/postgres.ts +151 -0
  55. package/service/predefinedAdaptor/sql/dialect/sqlite.ts +134 -0
  56. package/service/predefinedAdaptor/sql/driver/bunSqlite.ts +29 -0
  57. package/service/predefinedAdaptor/sql/driver/libsql.ts +37 -0
  58. package/service/predefinedAdaptor/sql/driver/postgres.ts +42 -0
  59. package/service/predefinedAdaptor/sql/types.ts +251 -0
  60. package/service/predefinedAdaptor/sql/values.ts +91 -0
  61. package/signal/CrossSiteGuard.ts +71 -0
  62. package/signal/SignalFailure.ts +51 -0
  63. package/signal/endpoint.ts +2 -2
  64. package/signal/exception.ts +43 -6
  65. package/signal/index.ts +2 -0
  66. package/signal/intercept.ts +5 -2
  67. package/signal/internal.ts +10 -2
  68. package/signal/serializer/fetch.serializer.ts +11 -1
  69. package/signal/signalContext.ts +25 -28
  70. package/signal/slice.ts +1 -1
  71. package/store/SliceRequest.ts +23 -0
  72. package/store/action.ts +177 -114
  73. package/store/agent/AgentBridge.ts +3 -1
  74. package/store/agentic/StExposeBuilder.ts +7 -4
  75. package/store/agentic/StStateBuilder.ts +9 -4
  76. package/store/agentic/StToolBuilder.ts +21 -7
  77. package/store/store.ts +1 -1
  78. package/store/storeInstance.ts +3 -3
  79. package/store/storeRegistry.ts +1 -1
  80. package/types/client/cookie.d.ts +13 -2
  81. package/types/client/csrTypes.d.ts +0 -1
  82. package/types/client/frameConfig.d.ts +1 -0
  83. package/types/common/TrustedProxy.d.ts +24 -0
  84. package/types/common/clamp.d.ts +2 -0
  85. package/types/common/dynamicRecord.d.ts +14 -0
  86. package/types/common/index.d.ts +5 -1
  87. package/types/common/requestOrigin.d.ts +14 -0
  88. package/types/common/restClient.d.ts +27 -0
  89. package/types/constant/types.d.ts +13 -0
  90. package/types/dictionary/base.dictionary.d.ts +1 -1
  91. package/types/dictionary/dictionary.d.ts +8 -8
  92. package/types/document/dataLoader.d.ts +0 -3
  93. package/types/document/filterMeta.d.ts +2 -2
  94. package/types/document/loaderInfo.d.ts +2 -2
  95. package/types/fetch/client/fetchHandle.d.ts +24 -0
  96. package/types/fetch/client/httpClient.d.ts +2 -0
  97. package/types/fetch/client/sliceInitHandle.d.ts +29 -0
  98. package/types/fetch/fetchType/appliedReturn.type.d.ts +38 -1
  99. package/types/fetch/fetchType/sliceFetch.type.d.ts +5 -5
  100. package/types/fetch/requestStorage.d.ts +14 -0
  101. package/types/index.d.ts +17 -2
  102. package/types/server/akanOption.d.ts +8 -1
  103. package/types/server/decorators.d.ts +12 -2
  104. package/types/server/di/diLifecycle.d.ts +9 -1
  105. package/types/server/routeTreeBuilder.d.ts +1 -2
  106. package/types/server/routing/appWsData.d.ts +7 -3
  107. package/types/server/staticPath.d.ts +17 -0
  108. package/types/service/adapt.d.ts +1 -1
  109. package/types/service/predefinedAdaptor/database.adaptor.d.ts +15 -407
  110. package/types/service/predefinedAdaptor/sql/PendingStoreEnsures.d.ts +6 -0
  111. package/types/service/predefinedAdaptor/sql/QueryCompiler.d.ts +28 -0
  112. package/types/service/predefinedAdaptor/sql/SqlDocumentStore.d.ts +127 -0
  113. package/types/service/predefinedAdaptor/sql/UpdateCompiler.d.ts +17 -0
  114. package/types/service/predefinedAdaptor/sql/dialect/postgres.d.ts +27 -0
  115. package/types/service/predefinedAdaptor/sql/dialect/sqlite.d.ts +27 -0
  116. package/types/service/predefinedAdaptor/sql/driver/bunSqlite.d.ts +16 -0
  117. package/types/service/predefinedAdaptor/sql/driver/libsql.d.ts +17 -0
  118. package/types/service/predefinedAdaptor/sql/driver/postgres.d.ts +17 -0
  119. package/types/service/predefinedAdaptor/sql/types.d.ts +226 -0
  120. package/types/service/predefinedAdaptor/sql/values.d.ts +13 -0
  121. package/types/signal/CrossSiteGuard.d.ts +44 -0
  122. package/types/signal/SignalFailure.d.ts +28 -0
  123. package/types/signal/exception.d.ts +38 -54774
  124. package/types/signal/index.d.ts +2 -0
  125. package/types/signal/intercept.d.ts +3 -3
  126. package/types/signal/internal.d.ts +5 -1
  127. package/types/signal/serializer/fetch.serializer.d.ts +7 -1
  128. package/types/store/SliceRequest.d.ts +16 -0
  129. package/types/store/agentic/StToolBuilder.d.ts +10 -1
  130. package/types/ui/Agent/Menu.d.ts +1 -1
  131. package/types/ui/Field/Choice.d.ts +52 -0
  132. package/types/ui/Field/Collection.d.ts +51 -0
  133. package/types/ui/Field/DateField.d.ts +32 -0
  134. package/types/ui/Field/Label.d.ts +9 -0
  135. package/types/ui/Field/NumberField.d.ts +43 -0
  136. package/types/ui/Field/Relation.d.ts +80 -0
  137. package/types/ui/Field/Text.d.ts +101 -0
  138. package/types/ui/Field/index.d.ts +50 -0
  139. package/types/ui/Field.d.ts +1 -412
  140. package/types/ui/Load/Edit_Client.d.ts +3 -1
  141. package/types/ui/Load/Stream.d.ts +22 -0
  142. package/types/ui/Load/index.d.ts +2 -0
  143. package/types/ui/System/Root.d.ts +4 -0
  144. package/types/ui/index.d.ts +1 -1
  145. package/types/ui/recipe/badgeRecipe.d.ts +2 -2
  146. package/types/ui/recipe/buttonRecipe.d.ts +2 -2
  147. package/types/webkit/useCamera.d.ts +10 -1
  148. package/types/webkit/usePurchase.d.ts +4 -0
  149. package/ui/Agent/Menu.tsx +2 -2
  150. package/ui/Data/TableList.tsx +1 -1
  151. package/ui/DraggableList.tsx +1 -2
  152. package/ui/Field/Choice.tsx +172 -0
  153. package/ui/Field/Collection.tsx +293 -0
  154. package/ui/Field/DateField.tsx +138 -0
  155. package/ui/Field/Label.tsx +31 -0
  156. package/ui/Field/NumberField.tsx +169 -0
  157. package/ui/Field/Relation.tsx +387 -0
  158. package/ui/Field/Text.tsx +360 -0
  159. package/ui/Field/index.tsx +70 -0
  160. package/ui/Input.tsx +3 -3
  161. package/ui/Load/Edit.tsx +2 -2
  162. package/ui/Load/Edit_Client.tsx +11 -3
  163. package/ui/Load/Pagination.tsx +25 -25
  164. package/ui/Load/Stream.tsx +38 -0
  165. package/ui/Load/Units.tsx +50 -37
  166. package/ui/Load/View.tsx +25 -17
  167. package/ui/Load/index.ts +2 -0
  168. package/ui/Model/EditModal.tsx +7 -7
  169. package/ui/Signal/Message.tsx +7 -7
  170. package/ui/Signal/PubSub.tsx +4 -1
  171. package/ui/System/Client.tsx +0 -2
  172. package/ui/System/Gtag.tsx +1 -1
  173. package/ui/System/Root.tsx +4 -0
  174. package/ui/index.ts +1 -1
  175. package/ui/overlayPosition.ts +1 -2
  176. package/webkit/useCamera.tsx +22 -6
  177. package/webkit/useCodepush.tsx +2 -2
  178. package/webkit/useCsrValues.ts +1 -3
  179. package/webkit/useFetch.ts +4 -3
  180. package/webkit/useFrameRuntime.ts +14 -7
  181. package/webkit/usePurchase.tsx +4 -1
  182. package/fetch/serializer/fetch.serializer.ts +0 -142
  183. package/fetch/serializer/index.ts +0 -1
  184. package/types/common/httpClient.d.ts +0 -20
  185. package/types/fetch/serializer/fetch.serializer.d.ts +0 -14
  186. package/types/fetch/serializer/index.d.ts +0 -1
  187. package/types/fetch/serializer.d.ts +0 -1
  188. package/ui/Field.tsx +0 -1639
@@ -0,0 +1,22 @@
1
+ export class PendingStoreEnsures {
2
+ readonly #pending = new Set<Promise<void>>();
3
+ #closed = false;
4
+
5
+ track(ensure: Promise<void>): void {
6
+ const tracked = ensure
7
+ .catch((error: unknown) => {
8
+ if (this.#closed) return;
9
+ throw error;
10
+ })
11
+ .finally(() => {
12
+ this.#pending.delete(tracked);
13
+ });
14
+ this.#pending.add(tracked);
15
+ }
16
+
17
+ /** Let them finish against a live connection. Call before closing it. */
18
+ async settle(): Promise<void> {
19
+ this.#closed = true;
20
+ await Promise.allSettled([...this.#pending]);
21
+ }
22
+ }
@@ -0,0 +1,225 @@
1
+ import { type DocumentQuery, type DocumentQueryNode, searchColumns } from "akanjs/document";
2
+ import { DEFAULT_SEARCH_WEIGHTS, toMatchExpression } from "../searchIndex";
3
+ import { quoteIdent } from "../sqlDescriptor";
4
+ import {
5
+ BASE_COLUMNS,
6
+ type CompileContext,
7
+ type CompiledQuery,
8
+ type FieldMap,
9
+ QUERY_OPERATOR_KEYS,
10
+ type QueryLeafOps,
11
+ type QueryOperatorName,
12
+ type SearchJoin,
13
+ type SqlDialect,
14
+ } from "./types";
15
+ import { BASE_COLUMN_LEAF } from "./values";
16
+
17
+ export class QueryCompiler {
18
+ constructor(
19
+ private readonly fields: FieldMap,
20
+ private readonly dialect: SqlDialect,
21
+ private readonly ref: string,
22
+ private readonly searchEnabled: () => boolean,
23
+ ) {}
24
+
25
+ #leaf(path: string): QueryLeafOps {
26
+ return BASE_COLUMNS.has(path) ? BASE_COLUMN_LEAF : this.dialect;
27
+ }
28
+
29
+ compile(query?: DocumentQuery): CompiledQuery {
30
+ const joins: SearchJoin[] = [];
31
+ if (!query || (typeof query === "object" && !Array.isArray(query) && Object.keys(query).length === 0)) {
32
+ return { where: "1 = 1", params: [], joins };
33
+ }
34
+ const compiled = this.compileNode(query, { joins, conjunctive: true });
35
+ return { where: compiled.sql || "1 = 1", params: compiled.params, joins };
36
+ }
37
+
38
+ orderBy(sort: Record<string, 1 | -1> = { createdAt: -1 }) {
39
+ return Object.entries(sort)
40
+ .map(([path, direction]) => `${this.fieldExpr(path)} ${direction === 1 ? "ASC" : "DESC"}`)
41
+ .join(", ");
42
+ }
43
+
44
+ fieldExpr(path: string) {
45
+ this.assertPath(path);
46
+ return BASE_COLUMNS.has(path) ? quoteIdent(path) : this.dialect.extract(path);
47
+ }
48
+
49
+ projectExpr(path: string) {
50
+ this.assertPath(path);
51
+ return BASE_COLUMNS.has(path) ? quoteIdent(path) : this.dialect.projectExpr(path);
52
+ }
53
+
54
+ private compileNode(query: DocumentQuery, ctx: CompileContext): { sql: string; params: unknown[] } {
55
+ if (this.isQueryNode(query)) {
56
+ if (query.kind === "search") {
57
+ if (!ctx.conjunctive)
58
+ throw new Error(
59
+ `Text search on "${this.ref}" must sit at an AND position; q.search() cannot be nested under q.any() or q.not().`,
60
+ );
61
+ const join = this.#searchJoin(query, ctx.joins.length);
62
+ if (join) ctx.joins.push(join);
63
+ return { sql: join ? "" : "0 = 1", params: [] };
64
+ }
65
+ if (query.kind === "all" || query.kind === "any") {
66
+ const subCtx = query.kind === "all" ? ctx : { ...ctx, conjunctive: false };
67
+ const parts = query.queries.map((sub) => this.compileNode(sub, subCtx)).filter((part) => part.sql);
68
+ if (!parts.length) return { sql: "1 = 1", params: [] };
69
+ const joiner = query.kind === "all" ? " AND " : " OR ";
70
+ return {
71
+ sql: `(${parts.map((part) => part.sql).join(joiner)})`,
72
+ params: parts.flatMap((part) => part.params),
73
+ };
74
+ }
75
+ if (query.kind === "not") {
76
+ const part = this.compileNode(query.query, { ...ctx, conjunctive: false });
77
+ return { sql: `NOT (${part.sql})`, params: part.params };
78
+ }
79
+ if (query.kind === "raw") {
80
+ if (/[;]/.test(query.sql)) throw new Error("Raw SQL query fragments must be a single statement fragment");
81
+ return { sql: `(${query.sql})`, params: query.params };
82
+ }
83
+ throw new Error("Operator nodes must be attached to a document path");
84
+ }
85
+ const parts = Object.entries(query).flatMap(([path, value]) => {
86
+ if (value === undefined) throw new Error(`Undefined query value is not allowed: ${path}`);
87
+ return [this.compileField(path, value, ctx)];
88
+ });
89
+ if (!parts.length) return { sql: "1 = 1", params: [] };
90
+ return {
91
+ sql: `(${parts.map((part) => part.sql).join(" AND ")})`,
92
+ params: parts.flatMap((part) => part.params),
93
+ };
94
+ }
95
+
96
+ #searchJoin(node: Extract<DocumentQueryNode, { kind: "search" }>, index: number): SearchJoin | null {
97
+ if (!this.searchEnabled())
98
+ throw new Error(
99
+ `Text search on "${this.ref}" is unavailable because the search index is switched off (AKAN_SEARCH_ENABLED).`,
100
+ );
101
+ const columns = node.columns?.filter((column) => searchColumns.includes(column));
102
+ if (node.columns?.length && !columns?.length)
103
+ throw new Error(`Unknown search column on "${this.ref}": ${node.columns.join(", ")}`);
104
+ const match = toMatchExpression(node.text, { prefix: node.prefix, columns });
105
+
106
+ if (!match) return null;
107
+ const weights = node.weights ?? DEFAULT_SEARCH_WEIGHTS;
108
+
109
+ if (weights.length !== searchColumns.length || weights.some((weight) => !Number.isFinite(weight)))
110
+ throw new Error(
111
+ `Search weights on "${this.ref}" must be ${searchColumns.length} finite numbers: ${JSON.stringify(node.weights)}`,
112
+ );
113
+ const alias = `__s${index}`;
114
+ return { alias, ...this.dialect.searchJoin({ alias, ref: this.ref, match, weights }) };
115
+ }
116
+
117
+ private compileField(path: string, value: unknown, ctx: CompileContext): { sql: string; params: unknown[] } {
118
+ this.assertPath(path);
119
+ const field = this.fields[path]?.getProps?.() ?? this.fields[path];
120
+ const leaf = this.#leaf(path);
121
+ if (this.isQueryNode(value)) {
122
+ if (value.kind !== "op") return this.compileNode({ [path]: value } as DocumentQuery, ctx);
123
+ switch (value.op) {
124
+ case "eq":
125
+ return leaf.eq(path, value.value);
126
+ case "ne":
127
+ return leaf.ne(path, value.value);
128
+ case "oneOf": {
129
+ const values = (value.value as unknown[]) ?? [];
130
+ if (!values.length) return { sql: "0 = 1", params: [] };
131
+ if (field?.isArray) {
132
+ const parts = values.map((item) => leaf.arrayHas(path, item));
133
+ return {
134
+ sql: `(${parts.map((part) => part.sql).join(" OR ")})`,
135
+ params: parts.flatMap((part) => part.params),
136
+ };
137
+ }
138
+ return leaf.inList(path, values);
139
+ }
140
+ case "notOneOf": {
141
+ const values = (value.value as unknown[]) ?? [];
142
+ if (!values.length) return { sql: "1 = 1", params: [] };
143
+ if (field?.isArray) {
144
+ const parts = values.map((item) => leaf.arrayHas(path, item));
145
+ return {
146
+ sql: `NOT (${parts.map((part) => part.sql).join(" OR ")})`,
147
+ params: parts.flatMap((part) => part.params),
148
+ };
149
+ }
150
+ return leaf.notInList(path, values);
151
+ }
152
+ case "gt":
153
+ case "gte":
154
+ case "lt":
155
+ case "lte":
156
+ return leaf.compare(path, value.op, value.value);
157
+ case "between": {
158
+ const [from, to] = value.value as [unknown, unknown];
159
+ return leaf.between(path, from, to);
160
+ }
161
+ case "exists":
162
+ return leaf.exists(path);
163
+ case "missing":
164
+ return leaf.missing(path);
165
+ case "empty":
166
+ return leaf.empty(path);
167
+ case "has":
168
+ return leaf.arrayHas(path, value.value);
169
+ case "contains":
170
+ return leaf.contains(path, value.value);
171
+ }
172
+ }
173
+ if (value && typeof value === "object" && !Array.isArray(value)) {
174
+ const operators = value as Record<string, unknown>;
175
+ const keys = Object.keys(operators);
176
+ if (keys.some((key) => QUERY_OPERATOR_KEYS.has(key))) {
177
+ const parts = keys.flatMap((key) => {
178
+ if (!QUERY_OPERATOR_KEYS.has(key)) return [];
179
+ if (key === "exists")
180
+ return [this.compileField(path, { kind: "op", op: operators.exists ? "exists" : "missing" }, ctx)];
181
+ if (key === "missing")
182
+ return [this.compileField(path, { kind: "op", op: operators.missing ? "missing" : "exists" }, ctx)];
183
+ if (key === "empty")
184
+ return [this.compileField(path, { kind: "op", op: operators.empty ? "empty" : "exists" }, ctx)];
185
+ return [this.compileField(path, { kind: "op", op: key as QueryOperatorName, value: operators[key] }, ctx)];
186
+ });
187
+ return {
188
+ sql: `(${parts.map((part) => part.sql).join(" AND ")})`,
189
+ params: parts.flatMap((part) => part.params),
190
+ };
191
+ }
192
+ }
193
+ if (field?.isArray && !Array.isArray(value)) return leaf.arrayHas(path, value);
194
+ return leaf.eq(path, value);
195
+ }
196
+
197
+ /**
198
+ * The root segment only, deliberately.
199
+ *
200
+ * Everything past it reaches the escaped JSON-path literal and nothing else, so a deep typo is a query that
201
+ * matches nothing rather than a query that does something else. Validating it would mean walking the field
202
+ * graph through array indices (`payments.3.name`), array-of-object leaves (`works.tags`) and `Map` keys, which
203
+ * have no fixed path to check against — a validator that guessed there would refuse working queries, which is
204
+ * worse than the silence. Deep paths are written by the model's own filter code, where the first test catches
205
+ * a typo; the root is the one segment a caller can reach.
206
+ */
207
+ private assertPath(path: string) {
208
+ const root = path.split(".")[0];
209
+ if (BASE_COLUMNS.has(root)) return;
210
+ if (!this.fields[root]) {
211
+
212
+ if (/^\d+$/.test(root))
213
+ throw new Error(
214
+ `Query received an array instead of a query object (field path "${path}"). ` +
215
+ `A query must be a descriptor object; a slice exec must return queryBy...(...), not an executed list.`,
216
+ );
217
+ throw new Error(`Unknown document field path: ${path}`);
218
+ }
219
+ }
220
+
221
+ private isQueryNode(value: unknown): value is DocumentQueryNode {
222
+ return !!value && typeof value === "object" && "kind" in value;
223
+ }
224
+ }
225
+