@getaster/data 0.0.0-stage → 0.1.0-preview.1
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/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +6 -2
- package/dist/authentication.d.ts +42 -0
- package/dist/authentication.js +121 -0
- package/dist/changes/changeset.d.ts +47 -0
- package/dist/changes/changeset.js +75 -0
- package/dist/collection/aggregate.d.ts +22 -0
- package/dist/collection/aggregate.js +94 -0
- package/dist/collection/collection.d.ts +96 -0
- package/dist/collection/collection.js +588 -0
- package/dist/collection/grouping.d.ts +75 -0
- package/dist/collection/grouping.js +190 -0
- package/dist/collection/groups.d.ts +31 -0
- package/dist/collection/groups.js +117 -0
- package/dist/collection/summary.d.ts +17 -0
- package/dist/collection/summary.js +103 -0
- package/dist/composition/activity.d.ts +36 -0
- package/dist/composition/activity.js +60 -0
- package/dist/composition/command.d.ts +22 -0
- package/dist/composition/command.js +57 -0
- package/dist/composition/lifetime.d.ts +11 -0
- package/dist/composition/lifetime.js +33 -0
- package/dist/composition/sessions.d.ts +64 -0
- package/dist/composition/sessions.js +209 -0
- package/dist/context.d.ts +76 -0
- package/dist/context.js +211 -0
- package/dist/criteria/ast.d.ts +79 -0
- package/dist/criteria/ast.js +1 -0
- package/dist/criteria/compile.d.ts +37 -0
- package/dist/criteria/compile.js +233 -0
- package/dist/criteria/evaluate.d.ts +20 -0
- package/dist/criteria/evaluate.js +171 -0
- package/dist/criteria/filter-draft.d.ts +79 -0
- package/dist/criteria/filter-draft.js +320 -0
- package/dist/criteria/relative.d.ts +13 -0
- package/dist/criteria/relative.js +82 -0
- package/dist/criteria/values.d.ts +20 -0
- package/dist/criteria/values.js +283 -0
- package/dist/edit/edit-model.d.ts +152 -0
- package/dist/edit/edit-model.js +620 -0
- package/dist/edit/list-edit.d.ts +116 -0
- package/dist/edit/list-edit.js +332 -0
- package/dist/edit/record-edit.d.ts +93 -0
- package/dist/edit/record-edit.js +317 -0
- package/dist/export/csv.d.ts +25 -0
- package/dist/export/csv.js +75 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +35 -0
- package/dist/odata/errors.d.ts +25 -0
- package/dist/odata/errors.js +56 -0
- package/dist/odata/source.d.ts +65 -0
- package/dist/odata/source.js +193 -0
- package/dist/progress/status.d.ts +29 -0
- package/dist/progress/status.js +105 -0
- package/dist/schema/csdl.d.ts +13 -0
- package/dist/schema/csdl.js +531 -0
- package/dist/schema/model.d.ts +132 -0
- package/dist/schema/model.js +1 -0
- package/dist/scope/filters.d.ts +130 -0
- package/dist/scope/filters.js +349 -0
- package/dist/scope/publications.d.ts +27 -0
- package/dist/scope/publications.js +74 -0
- package/dist/scope/related-aggregate.d.ts +9 -0
- package/dist/scope/related-aggregate.js +67 -0
- package/dist/scope/scope.d.ts +181 -0
- package/dist/scope/scope.js +574 -0
- package/dist/selection/selection.d.ts +56 -0
- package/dist/selection/selection.js +137 -0
- package/dist/store/keys.d.ts +10 -0
- package/dist/store/keys.js +21 -0
- package/dist/store/store.d.ts +72 -0
- package/dist/store/store.js +143 -0
- package/dist/template/template.d.ts +19 -0
- package/dist/template/template.js +186 -0
- package/dist/transport.d.ts +28 -0
- package/dist/transport.js +20 -0
- package/dist/tree/tree.d.ts +117 -0
- package/dist/tree/tree.js +435 -0
- package/dist/validate/criteria-rule.d.ts +28 -0
- package/dist/validate/criteria-rule.js +22 -0
- package/dist/validate/validate.d.ts +13 -0
- package/dist/validate/validate.js +123 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Aster UI
|
|
2
|
+
Copyright 2026 Aster UI contributors
|
|
3
|
+
|
|
4
|
+
Aster UI source and the separately supplied OData mock WebAssembly binary are
|
|
5
|
+
licensed under the Apache License, Version 2.0. Third-party components retain
|
|
6
|
+
their respective licenses. The binary's attribution inventory and license texts
|
|
7
|
+
are supplied in packages/mock/wasm/; packages include those notices when shipped.
|
|
8
|
+
|
|
9
|
+
Geist and IBM Plex Mono fonts retain their SIL Open Font License notices.
|
|
10
|
+
ECharts is an optional separately loaded third-party runtime.
|
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @getaster/data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Aster UI data.
|
|
4
|
+
|
|
5
|
+
Documentation: https://ui.getaster.now/docs/guide/library.html
|
|
6
|
+
|
|
7
|
+
Licensed under Apache-2.0. See LICENSE and NOTICE.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type ReadonlySignal } from "@preact/signals-core";
|
|
2
|
+
import type { ODataRequest, ODataResponse, Transport } from "./transport.ts";
|
|
3
|
+
export type AuthenticationFailure = "cancelled" | "timeout" | "failed" | "identity-changed" | "retry-limit" | "replay-blocked" | "disposed";
|
|
4
|
+
/** Recovery failed without settling the original operation successfully. */
|
|
5
|
+
export declare class AuthenticationError extends Error {
|
|
6
|
+
readonly reason: AuthenticationFailure;
|
|
7
|
+
constructor(reason: AuthenticationFailure, options?: ErrorOptions);
|
|
8
|
+
}
|
|
9
|
+
export interface AuthenticationChallenge {
|
|
10
|
+
readonly request: ODataRequest;
|
|
11
|
+
readonly response: ODataResponse;
|
|
12
|
+
}
|
|
13
|
+
export interface AuthenticationRecovery extends AuthenticationChallenge {
|
|
14
|
+
/** Abort UI/SDK work when recovery times out, loses all waiters or is disposed. */
|
|
15
|
+
readonly signal: AbortSignal;
|
|
16
|
+
/** Stable account and tenant identity before the challenged operation. */
|
|
17
|
+
readonly identity: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
export interface AuthenticationOptions {
|
|
20
|
+
/** Stable account/tenant key; undefined is allowed before initial login. */
|
|
21
|
+
readonly identity: () => string | undefined;
|
|
22
|
+
/** Resolves only after credentials are ready. Throw on cancellation or failure. */
|
|
23
|
+
readonly recover: (context: AuthenticationRecovery) => Promise<void>;
|
|
24
|
+
/** Defaults to HTTP 401. Restrict custom detection to trusted API responses. */
|
|
25
|
+
readonly challenge?: (context: AuthenticationChallenge) => boolean;
|
|
26
|
+
/** GET only by default. Opt in writes only with a server non-application guarantee or idempotency. */
|
|
27
|
+
readonly replay?: (context: AuthenticationChallenge) => boolean;
|
|
28
|
+
/** Rebuild credential headers on every send, including retries. */
|
|
29
|
+
readonly credentials?: () => Readonly<Record<string, string>> | Promise<Readonly<Record<string, string>>>;
|
|
30
|
+
/** Total recovery deadline, including interaction. Defaults to five minutes. */
|
|
31
|
+
readonly timeoutMs?: number;
|
|
32
|
+
}
|
|
33
|
+
/** One authentication context shared by explicitly wrapped service transports. */
|
|
34
|
+
export declare class Authentication {
|
|
35
|
+
#private;
|
|
36
|
+
readonly recovering: ReadonlySignal<boolean>;
|
|
37
|
+
constructor(options: AuthenticationOptions);
|
|
38
|
+
/** Wrap only transports that belong to this identity/credential context. */
|
|
39
|
+
wrap(transport: Transport): Transport;
|
|
40
|
+
/** Cancels waiting and in-flight wrapped requests; late recovery cannot resume them. */
|
|
41
|
+
dispose(): void;
|
|
42
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { signal } from "@preact/signals-core";
|
|
2
|
+
/** Recovery failed without settling the original operation successfully. */
|
|
3
|
+
export class AuthenticationError extends Error {
|
|
4
|
+
reason;
|
|
5
|
+
constructor(reason, options) {
|
|
6
|
+
super(`Authentication recovery: ${reason}`, options);
|
|
7
|
+
this.name = "AuthenticationError";
|
|
8
|
+
this.reason = reason;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** One authentication context shared by explicitly wrapped service transports. */
|
|
12
|
+
export class Authentication {
|
|
13
|
+
#options;
|
|
14
|
+
#timeout;
|
|
15
|
+
#lifetime = new AbortController();
|
|
16
|
+
#recovering = signal(false);
|
|
17
|
+
recovering = this.#recovering;
|
|
18
|
+
#recovery;
|
|
19
|
+
#generation = 0;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
const timeout = options.timeoutMs ?? 300_000;
|
|
22
|
+
if (!Number.isSafeInteger(timeout) || timeout <= 0 || timeout > 2_147_483_647)
|
|
23
|
+
throw new RangeError("Authentication timeoutMs must be a positive timer integer");
|
|
24
|
+
this.#options = Object.freeze({ ...options });
|
|
25
|
+
this.#timeout = timeout;
|
|
26
|
+
}
|
|
27
|
+
/** Wrap only transports that belong to this identity/credential context. */
|
|
28
|
+
wrap(transport) {
|
|
29
|
+
return async (original) => {
|
|
30
|
+
const signal = original.signal ? AbortSignal.any([original.signal, this.#lifetime.signal]) : this.#lifetime.signal;
|
|
31
|
+
const request = Object.freeze({ ...original, ...(original.headers ? { headers: Object.freeze({ ...original.headers }) } : {}), signal });
|
|
32
|
+
const identity = this.#options.identity();
|
|
33
|
+
let retried = false;
|
|
34
|
+
for (;;) {
|
|
35
|
+
signal.throwIfAborted();
|
|
36
|
+
if (this.#recovery)
|
|
37
|
+
await this.#join(this.#recovery, signal);
|
|
38
|
+
this.#checkIdentity(identity);
|
|
39
|
+
const generation = this.#generation;
|
|
40
|
+
const credentials = this.#options.credentials ? await interrupted(Promise.resolve().then(() => { signal.throwIfAborted(); return this.#options.credentials(); }), signal) : {};
|
|
41
|
+
signal.throwIfAborted();
|
|
42
|
+
this.#checkIdentity(identity);
|
|
43
|
+
// Credential lookup can yield while another request starts recovery.
|
|
44
|
+
if (this.#recovery || generation !== this.#generation)
|
|
45
|
+
continue;
|
|
46
|
+
const headers = new Headers(request.headers);
|
|
47
|
+
for (const [key, value] of Object.entries(credentials))
|
|
48
|
+
headers.set(key, value);
|
|
49
|
+
const response = await interrupted(Promise.resolve().then(() => { signal.throwIfAborted(); return transport({ ...request, headers: Object.fromEntries(headers) }); }), signal);
|
|
50
|
+
const challenge = { request, response };
|
|
51
|
+
if (!(this.#options.challenge?.(challenge) ?? response.status === 401))
|
|
52
|
+
return response;
|
|
53
|
+
this.#checkIdentity(identity);
|
|
54
|
+
if (retried)
|
|
55
|
+
throw new AuthenticationError("retry-limit");
|
|
56
|
+
if (!(this.#options.replay?.(challenge) ?? request.method === "GET"))
|
|
57
|
+
throw new AuthenticationError("replay-blocked");
|
|
58
|
+
retried = true;
|
|
59
|
+
// A late response using old credentials shares the already completed recovery.
|
|
60
|
+
if (generation === this.#generation)
|
|
61
|
+
await this.#join(this.#recovery ?? this.#start(challenge, identity), signal);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
#checkIdentity(expected) {
|
|
66
|
+
if (expected !== undefined && this.#options.identity() !== expected)
|
|
67
|
+
throw new AuthenticationError("identity-changed");
|
|
68
|
+
}
|
|
69
|
+
#start(challenge, identity) {
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
const timer = setTimeout(() => controller.abort(new AuthenticationError("timeout")), this.#timeout);
|
|
72
|
+
const recovery = {
|
|
73
|
+
controller, waiters: 0,
|
|
74
|
+
promise: Promise.resolve().then(async () => {
|
|
75
|
+
controller.signal.throwIfAborted();
|
|
76
|
+
await interrupted(Promise.resolve().then(() => this.#options.recover({ ...challenge, signal: controller.signal, identity })), controller.signal);
|
|
77
|
+
this.#checkIdentity(identity);
|
|
78
|
+
this.#generation++;
|
|
79
|
+
}).catch(error => {
|
|
80
|
+
throw error instanceof AuthenticationError ? error : new AuthenticationError("failed", { cause: error });
|
|
81
|
+
}).finally(() => {
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
if (this.#recovery === recovery) {
|
|
84
|
+
this.#recovery = undefined;
|
|
85
|
+
this.#recovering.value = false;
|
|
86
|
+
}
|
|
87
|
+
}),
|
|
88
|
+
};
|
|
89
|
+
this.#recovery = recovery;
|
|
90
|
+
this.#recovering.value = true;
|
|
91
|
+
return recovery;
|
|
92
|
+
}
|
|
93
|
+
async #join(recovery, signal) {
|
|
94
|
+
recovery.waiters++;
|
|
95
|
+
try {
|
|
96
|
+
await interrupted(recovery.promise, signal);
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
if (--recovery.waiters === 0 && this.#recovery === recovery) {
|
|
100
|
+
this.#recovery = undefined;
|
|
101
|
+
recovery.controller.abort(new AuthenticationError("cancelled"));
|
|
102
|
+
this.#recovering.value = false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Cancels waiting and in-flight wrapped requests; late recovery cannot resume them. */
|
|
107
|
+
dispose() {
|
|
108
|
+
this.#lifetime.abort(new AuthenticationError("disposed"));
|
|
109
|
+
this.#recovery?.controller.abort(new AuthenticationError("disposed"));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function interrupted(promise, signal) {
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
const abort = () => { signal.removeEventListener("abort", abort); reject(signal.reason); };
|
|
115
|
+
promise.then(value => { signal.removeEventListener("abort", abort); resolve(value); }, error => { signal.removeEventListener("abort", abort); reject(error); });
|
|
116
|
+
if (signal.aborted)
|
|
117
|
+
abort();
|
|
118
|
+
else
|
|
119
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
120
|
+
});
|
|
121
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Entity, EntityType, Value } from "../schema/model.ts";
|
|
2
|
+
import type { KeyValues } from "../store/keys.ts";
|
|
3
|
+
export interface FieldChange {
|
|
4
|
+
readonly from: Value;
|
|
5
|
+
readonly to: Value;
|
|
6
|
+
}
|
|
7
|
+
/** Changes to a record's owned collections, by navigation name: one per child record added, updated or removed. */
|
|
8
|
+
export type OwnedChanges = Readonly<Record<string, readonly ChangeSet[]>>;
|
|
9
|
+
/**
|
|
10
|
+
* What a save changed, as data: it can be stored, sent, inverted for undo and
|
|
11
|
+
* replayed over its old values. An update can carry its owned collections'
|
|
12
|
+
* changes, so one record's edit across nested editors is one change set.
|
|
13
|
+
*/
|
|
14
|
+
export type ChangeSet = {
|
|
15
|
+
readonly kind: "create";
|
|
16
|
+
readonly set: string;
|
|
17
|
+
readonly values: Entity;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: "update";
|
|
20
|
+
readonly set: string;
|
|
21
|
+
readonly key: KeyValues;
|
|
22
|
+
readonly changes: Readonly<Record<string, FieldChange>>;
|
|
23
|
+
readonly owned?: OwnedChanges;
|
|
24
|
+
} | {
|
|
25
|
+
readonly kind: "delete";
|
|
26
|
+
readonly set: string;
|
|
27
|
+
readonly key: KeyValues;
|
|
28
|
+
readonly values: Entity;
|
|
29
|
+
};
|
|
30
|
+
/** Field changes from `baseline` to `current`; only fields present in `current` count. */
|
|
31
|
+
export declare function diff(baseline: Entity, current: Entity): Record<string, FieldChange>;
|
|
32
|
+
export declare function isEmpty(changes: Readonly<Record<string, FieldChange>>): boolean;
|
|
33
|
+
export declare function apply(entity: Entity, changes: Readonly<Record<string, FieldChange>>): Entity;
|
|
34
|
+
/**
|
|
35
|
+
* The change that undoes `change`. Undoing a create needs the created record's
|
|
36
|
+
* key; undoing a delete recreates it without its server-computed properties.
|
|
37
|
+
*/
|
|
38
|
+
export declare function invert(change: ChangeSet, type: EntityType, created?: KeyValues): ChangeSet;
|
|
39
|
+
/** Two updates of one record as one: first `from`, last `to`; fields that end where they began drop out. */
|
|
40
|
+
export declare function merge(a: Readonly<Record<string, FieldChange>>, b: Readonly<Record<string, FieldChange>>): Record<string, FieldChange>;
|
|
41
|
+
/** The properties a client may send: computed ones are the server's. */
|
|
42
|
+
export declare function writable(type: EntityType, values: Entity): Entity;
|
|
43
|
+
/**
|
|
44
|
+
* What a create sends: the writable values that differ from what the server
|
|
45
|
+
* would store without them (the field's `$DefaultValue`, else null).
|
|
46
|
+
*/
|
|
47
|
+
export declare function creatable(type: EntityType, values: Entity): Entity;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** Field changes from `baseline` to `current`; only fields present in `current` count. */
|
|
2
|
+
export function diff(baseline, current) {
|
|
3
|
+
const out = {};
|
|
4
|
+
for (const [name, to] of Object.entries(current)) {
|
|
5
|
+
const from = baseline[name] ?? null;
|
|
6
|
+
if (!Object.is(from, to))
|
|
7
|
+
out[name] = { from, to };
|
|
8
|
+
}
|
|
9
|
+
return out;
|
|
10
|
+
}
|
|
11
|
+
export function isEmpty(changes) {
|
|
12
|
+
return Object.keys(changes).length === 0;
|
|
13
|
+
}
|
|
14
|
+
export function apply(entity, changes) {
|
|
15
|
+
const out = { ...entity };
|
|
16
|
+
for (const [name, c] of Object.entries(changes))
|
|
17
|
+
out[name] = c.to;
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The change that undoes `change`. Undoing a create needs the created record's
|
|
22
|
+
* key; undoing a delete recreates it without its server-computed properties.
|
|
23
|
+
*/
|
|
24
|
+
export function invert(change, type, created) {
|
|
25
|
+
switch (change.kind) {
|
|
26
|
+
case "create":
|
|
27
|
+
if (!created)
|
|
28
|
+
throw new Error("Undoing a create needs the created record's key");
|
|
29
|
+
return { kind: "delete", set: change.set, key: created, values: change.values };
|
|
30
|
+
case "update": {
|
|
31
|
+
const changes = {};
|
|
32
|
+
for (const [name, c] of Object.entries(change.changes))
|
|
33
|
+
changes[name] = { from: c.to, to: c.from };
|
|
34
|
+
if (change.owned)
|
|
35
|
+
throw new Error("Undoing owned changes needs each child's type and keys; undo them one by one");
|
|
36
|
+
return { kind: "update", set: change.set, key: change.key, changes };
|
|
37
|
+
}
|
|
38
|
+
case "delete":
|
|
39
|
+
return { kind: "create", set: change.set, values: writable(type, change.values) };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Two updates of one record as one: first `from`, last `to`; fields that end where they began drop out. */
|
|
43
|
+
export function merge(a, b) {
|
|
44
|
+
const out = { ...a };
|
|
45
|
+
for (const [name, c] of Object.entries(b)) {
|
|
46
|
+
const first = out[name];
|
|
47
|
+
out[name] = first ? { from: first.from, to: c.to } : c;
|
|
48
|
+
}
|
|
49
|
+
for (const [name, c] of Object.entries(out))
|
|
50
|
+
if (Object.is(c.from, c.to))
|
|
51
|
+
delete out[name];
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
/** The properties a client may send: computed ones are the server's. */
|
|
55
|
+
export function writable(type, values) {
|
|
56
|
+
const out = {};
|
|
57
|
+
for (const [name, v] of Object.entries(values)) {
|
|
58
|
+
const field = type.field(name);
|
|
59
|
+
if (field && !field.computed)
|
|
60
|
+
out[name] = v;
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* What a create sends: the writable values that differ from what the server
|
|
66
|
+
* would store without them (the field's `$DefaultValue`, else null).
|
|
67
|
+
*/
|
|
68
|
+
export function creatable(type, values) {
|
|
69
|
+
const out = {};
|
|
70
|
+
for (const [name, v] of Object.entries(writable(type, values))) {
|
|
71
|
+
if (!Object.is(v, type.field(name).defaultValue ?? null))
|
|
72
|
+
out[name] = v;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ReadonlySignal } from "@preact/signals-core";
|
|
2
|
+
import type { Criteria } from "../criteria/ast.ts";
|
|
3
|
+
import type { Entity, Field, Value } from "../schema/model.ts";
|
|
4
|
+
import type { Collection } from "./collection.ts";
|
|
5
|
+
/**
|
|
6
|
+
* A value computed over every record of a collection. It is `undefined`
|
|
7
|
+
* until the collection is fully loaded, so a figure never shows a partial
|
|
8
|
+
* answer as if it were the whole one.
|
|
9
|
+
*/
|
|
10
|
+
export declare function aggregate<T>(collection: Collection, reduce: (items: readonly Entity[]) => T): ReadonlySignal<T | undefined>;
|
|
11
|
+
/** Records matching `criteria`, evaluated locally with OData semantics. */
|
|
12
|
+
export declare function countWhere(collection: Collection, criteria: Criteria): ReadonlySignal<number | undefined>;
|
|
13
|
+
/** Records per value of `field`; null values are counted under `null`. */
|
|
14
|
+
export declare function countBy(collection: Collection, field: string): ReadonlySignal<ReadonlyMap<Value, number> | undefined>;
|
|
15
|
+
/**
|
|
16
|
+
* The sum of a numeric field, ignoring nulls. Decimal and Int64 sums are
|
|
17
|
+
* exact and returned as text, like the values themselves.
|
|
18
|
+
*/
|
|
19
|
+
export declare function sum(collection: Collection, field: string): ReadonlySignal<Value | undefined>;
|
|
20
|
+
export declare function sumValues(values: readonly Value[], field: Field): Value;
|
|
21
|
+
/** `a - b` for exact numeric text, keeping the larger scale. */
|
|
22
|
+
export declare function subtractExact(a: Value, b: Value): string;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { computed } from "@preact/signals-core";
|
|
2
|
+
import { matches } from "../criteria/evaluate.js";
|
|
3
|
+
/**
|
|
4
|
+
* A value computed over every record of a collection. It is `undefined`
|
|
5
|
+
* until the collection is fully loaded, so a figure never shows a partial
|
|
6
|
+
* answer as if it were the whole one.
|
|
7
|
+
*/
|
|
8
|
+
export function aggregate(collection, reduce) {
|
|
9
|
+
return computed(() => {
|
|
10
|
+
if (!collection.complete.value)
|
|
11
|
+
return undefined;
|
|
12
|
+
return reduce(collection.items.value);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** Records matching `criteria`, evaluated locally with OData semantics. */
|
|
16
|
+
export function countWhere(collection, criteria) {
|
|
17
|
+
const type = collection.type;
|
|
18
|
+
return aggregate(collection, (items) => items.filter((e) => matches(criteria, e, type)).length);
|
|
19
|
+
}
|
|
20
|
+
/** Records per value of `field`; null values are counted under `null`. */
|
|
21
|
+
export function countBy(collection, field) {
|
|
22
|
+
requireField(collection, field);
|
|
23
|
+
return aggregate(collection, (items) => {
|
|
24
|
+
const out = new Map();
|
|
25
|
+
for (const e of items) {
|
|
26
|
+
const v = e[field] ?? null;
|
|
27
|
+
out.set(v, (out.get(v) ?? 0) + 1);
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The sum of a numeric field, ignoring nulls. Decimal and Int64 sums are
|
|
34
|
+
* exact and returned as text, like the values themselves.
|
|
35
|
+
*/
|
|
36
|
+
export function sum(collection, field) {
|
|
37
|
+
const f = requireField(collection, field);
|
|
38
|
+
return aggregate(collection, (items) => sumValues(items.map((e) => e[field] ?? null), f));
|
|
39
|
+
}
|
|
40
|
+
export function sumValues(values, field) {
|
|
41
|
+
if (field.type === "Edm.Decimal" || field.type === "Edm.Int64") {
|
|
42
|
+
let total = 0n;
|
|
43
|
+
let scale = 0;
|
|
44
|
+
for (const v of values) {
|
|
45
|
+
if (v === null)
|
|
46
|
+
continue;
|
|
47
|
+
const d = parseExact(v);
|
|
48
|
+
[total, scale] = addScaled(total, scale, d.units, d.scale);
|
|
49
|
+
}
|
|
50
|
+
return formatExact(total, scale);
|
|
51
|
+
}
|
|
52
|
+
let total = 0;
|
|
53
|
+
for (const v of values)
|
|
54
|
+
if (typeof v === "number")
|
|
55
|
+
total += v;
|
|
56
|
+
return total;
|
|
57
|
+
}
|
|
58
|
+
/** `a - b` for exact numeric text, keeping the larger scale. */
|
|
59
|
+
export function subtractExact(a, b) {
|
|
60
|
+
const x = parseExact(a);
|
|
61
|
+
const y = parseExact(b);
|
|
62
|
+
const [total, scale] = addScaled(x.units, x.scale, -y.units, y.scale);
|
|
63
|
+
return formatExact(total, scale);
|
|
64
|
+
}
|
|
65
|
+
function requireField(collection, name) {
|
|
66
|
+
const f = collection.type.field(name);
|
|
67
|
+
if (!f)
|
|
68
|
+
throw new Error(`${collection.type.name} has no field ${name}`);
|
|
69
|
+
return f;
|
|
70
|
+
}
|
|
71
|
+
function parseExact(v) {
|
|
72
|
+
const text = String(v);
|
|
73
|
+
const m = /^(-?)(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/.exec(text);
|
|
74
|
+
if (!m)
|
|
75
|
+
throw new Error(`${text} isn't an exact number`);
|
|
76
|
+
const frac = m[3] ?? "";
|
|
77
|
+
let units = BigInt(m[2] + frac);
|
|
78
|
+
let scale = frac.length - Number(m[4] ?? 0);
|
|
79
|
+
if (scale < 0) {
|
|
80
|
+
units *= 10n ** BigInt(-scale);
|
|
81
|
+
scale = 0;
|
|
82
|
+
}
|
|
83
|
+
return { units: m[1] ? -units : units, scale };
|
|
84
|
+
}
|
|
85
|
+
function addScaled(a, as, b, bs) {
|
|
86
|
+
const scale = Math.max(as, bs);
|
|
87
|
+
return [a * 10n ** BigInt(scale - as) + b * 10n ** BigInt(scale - bs), scale];
|
|
88
|
+
}
|
|
89
|
+
function formatExact(units, scale) {
|
|
90
|
+
const negative = units < 0n;
|
|
91
|
+
const digits = (negative ? -units : units).toString().padStart(scale + 1, "0");
|
|
92
|
+
const text = scale === 0 ? digits : `${digits.slice(0, -scale)}.${digits.slice(-scale)}`;
|
|
93
|
+
return negative ? `-${text}` : text;
|
|
94
|
+
}
|