@aldus-runtime/file-store 0.1.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.
- package/LICENSE +201 -0
- package/NOTICE +21 -0
- package/dist/atomic.d.ts +65 -0
- package/dist/atomic.d.ts.map +1 -0
- package/dist/atomic.js +160 -0
- package/dist/atomic.js.map +1 -0
- package/dist/collections.d.ts +27 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +58 -0
- package/dist/collections.js.map +1 -0
- package/dist/document.d.ts +66 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +109 -0
- package/dist/document.js.map +1 -0
- package/dist/errors.d.ts +60 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +56 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonl.d.ts +62 -0
- package/dist/jsonl.d.ts.map +1 -0
- package/dist/jsonl.js +99 -0
- package/dist/jsonl.js.map +1 -0
- package/dist/layout.d.ts +54 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +86 -0
- package/dist/layout.js.map +1 -0
- package/dist/lock.d.ts +80 -0
- package/dist/lock.d.ts.map +1 -0
- package/dist/lock.js +257 -0
- package/dist/lock.js.map +1 -0
- package/dist/ports.d.ts +104 -0
- package/dist/ports.d.ts.map +1 -0
- package/dist/ports.js +18 -0
- package/dist/ports.js.map +1 -0
- package/dist/stores.d.ts +56 -0
- package/dist/stores.d.ts.map +1 -0
- package/dist/stores.js +210 -0
- package/dist/stores.js.map +1 -0
- package/dist/workspace.d.ts +37 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +48 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +48 -0
- package/src/atomic.ts +185 -0
- package/src/collections.ts +94 -0
- package/src/document.ts +146 -0
- package/src/errors.ts +65 -0
- package/src/index.ts +96 -0
- package/src/jsonl.ts +149 -0
- package/src/layout.ts +105 -0
- package/src/lock.ts +359 -0
- package/src/ports.ts +126 -0
- package/src/stores.ts +295 -0
- package/src/workspace.ts +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Aldus
|
|
2
|
+
Copyright 2026 Jam Chen
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
5
|
+
this software except in compliance with the License. You may obtain a copy of
|
|
6
|
+
the License in the LICENSE file distributed with this software, or at:
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Aldus is currently owned as personal work and has not been assigned to a
|
|
11
|
+
separate legal entity. If ownership is transferred, this file and the related
|
|
12
|
+
copyright statements will be updated at that time.
|
|
13
|
+
|
|
14
|
+
No third-party code is bundled, inlined, or vendored into any published Aldus
|
|
15
|
+
package. Dependencies are resolved at install time and reach the consumer under
|
|
16
|
+
their own licenses, from their own publishers. The dependency audit is recorded
|
|
17
|
+
in THIRD-PARTY-NOTICES.md.
|
|
18
|
+
|
|
19
|
+
This NOTICE file exists by the project's choice rather than by obligation:
|
|
20
|
+
Apache-2.0 section 4(d) governs the preservation of a NOTICE file when the work
|
|
21
|
+
already carries one, and does not itself require that one be created.
|
package/dist/atomic.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crash-safe file primitives.
|
|
3
|
+
*
|
|
4
|
+
* Architecture contract §19.1 requires "recovery from partial success" and "recovery from
|
|
5
|
+
* interrupted writes" (§22 WP-02). The failure this module exists to prevent is concrete: an
|
|
6
|
+
* operator's machine sleeps, a process is killed, or a container is evicted midway through
|
|
7
|
+
* rewriting `run.json`, and the next read finds a half-written file where the Run's state used
|
|
8
|
+
* to be. Contract §3.4 makes files authoritative, so a truncated manifest is not an
|
|
9
|
+
* inconvenience — it is the loss of the only authoritative record.
|
|
10
|
+
*
|
|
11
|
+
* The durability sequence is write-to-temp, fsync the file, rename, fsync the directory. Each
|
|
12
|
+
* step is load-bearing:
|
|
13
|
+
*
|
|
14
|
+
* - **Temp file in the same directory.** `rename` is only atomic within a filesystem. A temp
|
|
15
|
+
* file in `os.tmpdir()` may be on a different device, which silently degrades the rename into
|
|
16
|
+
* a copy — exactly the non-atomic write this is meant to avoid.
|
|
17
|
+
* - **fsync the file** before renaming, or the rename can be durable while the contents are not,
|
|
18
|
+
* leaving a correctly named empty file after a power loss.
|
|
19
|
+
* - **fsync the directory** after renaming, or the rename itself may not survive a crash.
|
|
20
|
+
*/
|
|
21
|
+
/** Injection point for the durability sequence, so tests can interrupt it at a chosen step. */
|
|
22
|
+
export interface AtomicWriteHooks {
|
|
23
|
+
/** Invoked after the temp file is written and synced, before the rename. */
|
|
24
|
+
beforeRename?: (temporaryPath: string) => void | Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/** Options for {@link writeFileAtomic}. */
|
|
27
|
+
export interface AtomicWriteOptions {
|
|
28
|
+
/** Test seam; unused in production paths. */
|
|
29
|
+
hooks?: AtomicWriteHooks;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Write `contents` to `path` so that a reader sees either the previous bytes or the new bytes,
|
|
33
|
+
* never a mixture.
|
|
34
|
+
*
|
|
35
|
+
* Parent directories are created as needed. The temp file is removed if any step fails, so an
|
|
36
|
+
* interrupted write leaves no debris for the next reader to mistake for real state.
|
|
37
|
+
*/
|
|
38
|
+
export declare function writeFileAtomic(path: string, contents: string, options?: AtomicWriteOptions): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Append a single line to a file, creating it if absent, and sync before returning.
|
|
41
|
+
*
|
|
42
|
+
* Used for the append-only event log (contract §6.4). `O_APPEND` makes each write land at the
|
|
43
|
+
* current end of file even with concurrent writers, so an interleaved append can never overwrite
|
|
44
|
+
* another writer's bytes — it can only ever be interleaved *between* lines, or torn at the tail
|
|
45
|
+
* if the process dies mid-write. {@link readJsonLines} handles the torn tail.
|
|
46
|
+
*/
|
|
47
|
+
export declare function appendLineSynced(path: string, line: string): Promise<void>;
|
|
48
|
+
/** Read a file, returning `undefined` rather than throwing when it does not exist. */
|
|
49
|
+
export declare function readFileOrUndefined(path: string): Promise<string | undefined>;
|
|
50
|
+
/** True if `error` is a Node `ENOENT`. */
|
|
51
|
+
export declare function isNotFound(error: unknown): boolean;
|
|
52
|
+
/** True if `error` is a Node `EEXIST`, i.e. an `O_EXCL` create lost the race. */
|
|
53
|
+
export declare function isAlreadyExists(error: unknown): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Create a file only if it does not already exist, returning `false` if it did.
|
|
56
|
+
*
|
|
57
|
+
* `O_CREAT | O_EXCL` is the primitive the lock is built on: the check and the create are one
|
|
58
|
+
* syscall, so two processes racing cannot both believe they created it.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createExclusive(path: string, contents: string): Promise<boolean>;
|
|
61
|
+
/** Overwrite a file in place without the rename dance. Used for lock heartbeats only. */
|
|
62
|
+
export declare function overwrite(path: string, contents: string): Promise<void>;
|
|
63
|
+
/** Remove a file, tolerating its absence. */
|
|
64
|
+
export declare function removeIfPresent(path: string): Promise<void>;
|
|
65
|
+
//# sourceMappingURL=atomic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atomic.d.ts","sourceRoot":"","sources":["../src/atomic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmB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ives.
|
|
3
|
+
*
|
|
4
|
+
* Architecture contract §19.1 requires "recovery from partial success" and "recovery from
|
|
5
|
+
* interrupted writes" (§22 WP-02). The failure this module exists to prevent is concrete: an
|
|
6
|
+
* operator's machine sleeps, a process is killed, or a container is evicted midway through
|
|
7
|
+
* rewriting `run.json`, and the next read finds a half-written file where the Run's state used
|
|
8
|
+
* to be. Contract §3.4 makes files authoritative, so a truncated manifest is not an
|
|
9
|
+
* inconvenience — it is the loss of the only authoritative record.
|
|
10
|
+
*
|
|
11
|
+
* The durability sequence is write-to-temp, fsync the file, rename, fsync the directory. Each
|
|
12
|
+
* step is load-bearing:
|
|
13
|
+
*
|
|
14
|
+
* - **Temp file in the same directory.** `rename` is only atomic within a filesystem. A temp
|
|
15
|
+
* file in `os.tmpdir()` may be on a different device, which silently degrades the rename into
|
|
16
|
+
* a copy — exactly the non-atomic write this is meant to avoid.
|
|
17
|
+
* - **fsync the file** before renaming, or the rename can be durable while the contents are not,
|
|
18
|
+
* leaving a correctly named empty file after a power loss.
|
|
19
|
+
* - **fsync the directory** after renaming, or the rename itself may not survive a crash.
|
|
20
|
+
*/
|
|
21
|
+
import { constants as fsConstants } from "node:fs";
|
|
22
|
+
import { mkdir, open, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
23
|
+
import { dirname, join } from "node:path";
|
|
24
|
+
let temporaryCounter = 0;
|
|
25
|
+
/**
|
|
26
|
+
* Write `contents` to `path` so that a reader sees either the previous bytes or the new bytes,
|
|
27
|
+
* never a mixture.
|
|
28
|
+
*
|
|
29
|
+
* Parent directories are created as needed. The temp file is removed if any step fails, so an
|
|
30
|
+
* interrupted write leaves no debris for the next reader to mistake for real state.
|
|
31
|
+
*/
|
|
32
|
+
export async function writeFileAtomic(path, contents, options = {}) {
|
|
33
|
+
const directory = dirname(path);
|
|
34
|
+
await mkdir(directory, { recursive: true });
|
|
35
|
+
// Same directory, therefore same filesystem, therefore a genuinely atomic rename. The counter
|
|
36
|
+
// plus pid keeps two writers in one process from colliding on the temp name.
|
|
37
|
+
temporaryCounter += 1;
|
|
38
|
+
const temporaryPath = join(directory, `.${basenameOf(path)}.${process.pid}.${temporaryCounter}.tmp`);
|
|
39
|
+
try {
|
|
40
|
+
const handle = await open(temporaryPath, "w");
|
|
41
|
+
try {
|
|
42
|
+
await handle.writeFile(contents, "utf8");
|
|
43
|
+
await handle.sync();
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
await handle.close();
|
|
47
|
+
}
|
|
48
|
+
await options.hooks?.beforeRename?.(temporaryPath);
|
|
49
|
+
await rename(temporaryPath, path);
|
|
50
|
+
await syncDirectory(directory);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
await unlink(temporaryPath).catch(() => undefined);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Append a single line to a file, creating it if absent, and sync before returning.
|
|
59
|
+
*
|
|
60
|
+
* Used for the append-only event log (contract §6.4). `O_APPEND` makes each write land at the
|
|
61
|
+
* current end of file even with concurrent writers, so an interleaved append can never overwrite
|
|
62
|
+
* another writer's bytes — it can only ever be interleaved *between* lines, or torn at the tail
|
|
63
|
+
* if the process dies mid-write. {@link readJsonLines} handles the torn tail.
|
|
64
|
+
*/
|
|
65
|
+
export async function appendLineSynced(path, line) {
|
|
66
|
+
await mkdir(dirname(path), { recursive: true });
|
|
67
|
+
const handle = await open(path, "a");
|
|
68
|
+
try {
|
|
69
|
+
await handle.writeFile(`${line}\n`, "utf8");
|
|
70
|
+
await handle.sync();
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await handle.close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Read a file, returning `undefined` rather than throwing when it does not exist. */
|
|
77
|
+
export async function readFileOrUndefined(path) {
|
|
78
|
+
try {
|
|
79
|
+
return await readFile(path, "utf8");
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (isNotFound(error))
|
|
83
|
+
return undefined;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** True if `error` is a Node `ENOENT`. */
|
|
88
|
+
export function isNotFound(error) {
|
|
89
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
90
|
+
}
|
|
91
|
+
/** True if `error` is a Node `EEXIST`, i.e. an `O_EXCL` create lost the race. */
|
|
92
|
+
export function isAlreadyExists(error) {
|
|
93
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "EEXIST";
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create a file only if it does not already exist, returning `false` if it did.
|
|
97
|
+
*
|
|
98
|
+
* `O_CREAT | O_EXCL` is the primitive the lock is built on: the check and the create are one
|
|
99
|
+
* syscall, so two processes racing cannot both believe they created it.
|
|
100
|
+
*/
|
|
101
|
+
export async function createExclusive(path, contents) {
|
|
102
|
+
await mkdir(dirname(path), { recursive: true });
|
|
103
|
+
try {
|
|
104
|
+
const handle = await open(path, fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY);
|
|
105
|
+
try {
|
|
106
|
+
await handle.writeFile(contents, "utf8");
|
|
107
|
+
await handle.sync();
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
await handle.close();
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (isAlreadyExists(error))
|
|
116
|
+
return false;
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** Overwrite a file in place without the rename dance. Used for lock heartbeats only. */
|
|
121
|
+
export async function overwrite(path, contents) {
|
|
122
|
+
await writeFile(path, contents, "utf8");
|
|
123
|
+
}
|
|
124
|
+
/** Remove a file, tolerating its absence. */
|
|
125
|
+
export async function removeIfPresent(path) {
|
|
126
|
+
try {
|
|
127
|
+
await unlink(path);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (!isNotFound(error))
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* fsync a directory so that a rename within it survives a crash.
|
|
136
|
+
*
|
|
137
|
+
* Not portable: some platforms refuse to open a directory for the purpose. A failure here means
|
|
138
|
+
* the rename may not be durable across a power loss, which is strictly weaker than the
|
|
139
|
+
* within-process atomicity the rename already guarantees — so it is tolerated rather than
|
|
140
|
+
* escalated into a failed write.
|
|
141
|
+
*/
|
|
142
|
+
async function syncDirectory(directory) {
|
|
143
|
+
try {
|
|
144
|
+
const handle = await open(directory, "r");
|
|
145
|
+
try {
|
|
146
|
+
await handle.sync();
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
await handle.close();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// Best effort by design; see the doc comment.
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function basenameOf(path) {
|
|
157
|
+
const index = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
158
|
+
return index === -1 ? path : path.slice(index + 1);
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=atomic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atomic.js","sourceRoot":"","sources":["../src/atomic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,SAAS,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAc1C,IAAI,gBAAgB,GAAG,CAAC,CAAC;AAEzB;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IA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CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;IACtE,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-run collection files (contract §7: `artifacts.json`, `approvals.json`, `costs.json`,
|
|
3
|
+
* `release.json`).
|
|
4
|
+
*
|
|
5
|
+
* Each holds a JSON array of records that each carry their own `schemaVersion`, so the array
|
|
6
|
+
* itself needs no envelope. Appending writes back the **raw** parsed array with one element
|
|
7
|
+
* added, which preserves unknown properties on every existing element for free — there is no
|
|
8
|
+
* merge to get wrong, because nothing already stored is ever re-serialised from a validated
|
|
9
|
+
* value (ADR-0004 decision 3).
|
|
10
|
+
*/
|
|
11
|
+
import { type SchemaTypeFor, type VersionedSchemaName } from "@aldus-runtime/core";
|
|
12
|
+
import { type AtomicWriteOptions } from "./atomic.js";
|
|
13
|
+
/** A collection read: validated records plus the raw array they were parsed from. */
|
|
14
|
+
export interface StoredCollection<T> {
|
|
15
|
+
values: T[];
|
|
16
|
+
raw: unknown[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Read and validate a collection file.
|
|
20
|
+
*
|
|
21
|
+
* An absent file reads as an empty collection: a Run that has produced no artifacts yet is an
|
|
22
|
+
* ordinary state (contract §6.2 `created`), not a missing record.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readCollection<N extends VersionedSchemaName>(path: string, schema: N): Promise<StoredCollection<SchemaTypeFor<N>>>;
|
|
25
|
+
/** Append one record to a collection file, preserving every existing element byte for byte. */
|
|
26
|
+
export declare function appendToCollection<N extends VersionedSchemaName>(path: string, schema: N, record: SchemaTypeFor<N>, options?: AtomicWriteOptions): Promise<void>;
|
|
27
|
+
//# sourceMappingURL=collections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.d.ts","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAuB,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAI3E,qFAAqF;AACrF,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,MAAM,EAAE,CAAC,EAAE,CAAC;IACZ,GAAG,EAAE,OAAO,EAAE,CAAC;CAChB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,CAAC,SAAS,mBAAmB,EAChE,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,GACR,OAAO,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CA6C7C;AAED,+FAA+F;AAC/F,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,mBAAmB,EACpE,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,EACxB,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,IAAI,CAAC,CAGf"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-run collection files (contract §7: `artifacts.json`, `approvals.json`, `costs.json`,
|
|
3
|
+
* `release.json`).
|
|
4
|
+
*
|
|
5
|
+
* Each holds a JSON array of records that each carry their own `schemaVersion`, so the array
|
|
6
|
+
* itself needs no envelope. Appending writes back the **raw** parsed array with one element
|
|
7
|
+
* added, which preserves unknown properties on every existing element for free — there is no
|
|
8
|
+
* merge to get wrong, because nothing already stored is ever re-serialised from a validated
|
|
9
|
+
* value (ADR-0004 decision 3).
|
|
10
|
+
*/
|
|
11
|
+
import { validateRecord, fromStructuredError, } from "@aldus-runtime/core";
|
|
12
|
+
import { readFileOrUndefined } from "./atomic.js";
|
|
13
|
+
import { writeDocument } from "./document.js";
|
|
14
|
+
import { FileStoreErrorCodes, fileStoreError } from "./errors.js";
|
|
15
|
+
/**
|
|
16
|
+
* Read and validate a collection file.
|
|
17
|
+
*
|
|
18
|
+
* An absent file reads as an empty collection: a Run that has produced no artifacts yet is an
|
|
19
|
+
* ordinary state (contract §6.2 `created`), not a missing record.
|
|
20
|
+
*/
|
|
21
|
+
export async function readCollection(path, schema) {
|
|
22
|
+
const contents = await readFileOrUndefined(path);
|
|
23
|
+
if (contents === undefined || contents.trim().length === 0)
|
|
24
|
+
return { values: [], raw: [] };
|
|
25
|
+
let parsed;
|
|
26
|
+
try {
|
|
27
|
+
parsed = JSON.parse(contents);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
throw fileStoreError(FileStoreErrorCodes.RECORD_MALFORMED, `The stored ${schema} collection is not valid JSON. Atomic writes make a torn file ` +
|
|
31
|
+
"impossible, so this means the file was edited or replaced by something other than the store.", { category: "io", retryable: false, details: { path, schema, byteLength: contents.length } });
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(parsed)) {
|
|
34
|
+
throw fileStoreError(FileStoreErrorCodes.RECORD_MALFORMED, `The stored ${schema} collection is valid JSON but not an array.`, { category: "io", retryable: false, details: { path, schema } });
|
|
35
|
+
}
|
|
36
|
+
const values = [];
|
|
37
|
+
for (let index = 0; index < parsed.length; index += 1) {
|
|
38
|
+
const result = validateRecord(schema, parsed[index]);
|
|
39
|
+
if (!result.ok) {
|
|
40
|
+
const error = fromStructuredError(result.error);
|
|
41
|
+
// Name the offending index. Without it, a 200-element artifacts file reports "one of these
|
|
42
|
+
// is wrong" and leaves an operator to bisect by hand.
|
|
43
|
+
throw fileStoreError(FileStoreErrorCodes.RECORD_MALFORMED, `Element ${index} of the stored ${schema} collection is not a valid ${schema}: ${error.message}`, {
|
|
44
|
+
category: "io",
|
|
45
|
+
retryable: false,
|
|
46
|
+
details: { path, schema, index },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
values.push(result.value);
|
|
50
|
+
}
|
|
51
|
+
return { values, raw: parsed };
|
|
52
|
+
}
|
|
53
|
+
/** Append one record to a collection file, preserving every existing element byte for byte. */
|
|
54
|
+
export async function appendToCollection(path, schema, record, options = {}) {
|
|
55
|
+
const existing = await readCollection(path, schema);
|
|
56
|
+
await writeDocument(path, [...existing.raw, record], options);
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=collections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EACL,cAAc,EACd,mBAAmB,GAGpB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,mBAAmB,EAA2B,MAAM,aAAa,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAQlE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAY,EACZ,MAAS;IAET,MAAM,QAAQ,GAAG,MAAM,mBAAmB,CAAC,IAAI,CAAC,CAAC;IACjD,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;IAE3F,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAChC,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,cAAc,CAClB,mBAAmB,CAAC,gBAAgB,EACpC,cAAc,MAAM,gEAAgE;YAClF,8FAA8F,EAChG,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EA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BAAkB,CACtC,IAAY,EACZ,MAAS,EACT,MAAwB,EACxB,OAAO,GAAuB,EAAE;IAEhC,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACpD,MAAM,aAAa,CAAC,IAAI,EAAE,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validated JSON documents with forward-compatible round-tripping.
|
|
3
|
+
*
|
|
4
|
+
* ADR-0004 decision 3 is a MUST on this package: a store MUST preserve unknown properties across
|
|
5
|
+
* a read-modify-write. The hazard it addresses is quiet. Zod strips unknown properties on parse
|
|
6
|
+
* (ADR-0002 decision 7), so an older build that reads a manifest written by a newer minor
|
|
7
|
+
* version, changes one field, and writes it back would delete the newer version's data without
|
|
8
|
+
* any error, any warning, or any way to notice afterwards.
|
|
9
|
+
*
|
|
10
|
+
* A Git-tracked `.aldus/` shared between machines on different builds makes that an ordinary
|
|
11
|
+
* situation, not an edge case (contract §5.1, §7). So every read keeps the raw parsed JSON
|
|
12
|
+
* beside the validated value, and every update merges the caller's changes back over the raw.
|
|
13
|
+
*/
|
|
14
|
+
import { type SchemaCompatibility, type SchemaTypeFor, type VersionedSchemaName } from "@aldus-runtime/core";
|
|
15
|
+
import { type AtomicWriteOptions } from "./atomic.js";
|
|
16
|
+
/** A record as it exists on disk: validated, plus the bytes it was validated from. */
|
|
17
|
+
export interface StoredDocument<T> {
|
|
18
|
+
/** The validated record. Unknown properties are absent, as Zod strips them. */
|
|
19
|
+
value: T;
|
|
20
|
+
/** The parsed JSON exactly as stored, including properties this build does not know about. */
|
|
21
|
+
raw: Record<string, unknown>;
|
|
22
|
+
/** How the stored `schemaVersion` relates to this build (ADR-0003). */
|
|
23
|
+
compatibility: SchemaCompatibility;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Read and validate a versioned record.
|
|
27
|
+
*
|
|
28
|
+
* Returns `undefined` when the file does not exist — an absent record is an ordinary state, not
|
|
29
|
+
* a failure, and a store that threw here would make "does this run exist?" an exception-handling
|
|
30
|
+
* exercise.
|
|
31
|
+
*
|
|
32
|
+
* @throws {AldusError} `ALDUS_RECORD_MALFORMED` if the bytes are not JSON,
|
|
33
|
+
* `ALDUS_SCHEMA_VERSION_UNSUPPORTED` if the major version is unreadable, or
|
|
34
|
+
* `ALDUS_SCHEMA_VALIDATION_FAILED` if the record does not satisfy its schema.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readDocument<N extends VersionedSchemaName>(path: string, schema: N): Promise<StoredDocument<SchemaTypeFor<N>> | undefined>;
|
|
37
|
+
/** Write a record, replacing any previous contents atomically. */
|
|
38
|
+
export declare function writeDocument(path: string, value: unknown, options?: AtomicWriteOptions): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Merge a validated update back over the bytes it came from, keeping properties this build does
|
|
41
|
+
* not know about (ADR-0004 decision 3).
|
|
42
|
+
*
|
|
43
|
+
* The three arguments are what make this precise rather than a guess:
|
|
44
|
+
*
|
|
45
|
+
* - `raw` — everything that was on disk.
|
|
46
|
+
* - `original` — the same record after validation, so its keys are exactly the ones this build
|
|
47
|
+
* understands.
|
|
48
|
+
* - `next` — the caller's updated record.
|
|
49
|
+
*
|
|
50
|
+
* A key present in `raw` but absent from `original` is unknown to this build, so it is preserved.
|
|
51
|
+
* A key present in both but absent from `next` was deliberately removed by the caller, so it is
|
|
52
|
+
* dropped. Without that distinction, preservation would resurrect fields a caller had just
|
|
53
|
+
* deleted.
|
|
54
|
+
*
|
|
55
|
+
* Arrays are merged element-wise **only when the lengths match**, which is the case where index
|
|
56
|
+
* identity is sound. If the caller added or removed elements, indices no longer denote the same
|
|
57
|
+
* records and `next` is taken wholesale — losing unknown properties inside those elements rather
|
|
58
|
+
* than attaching them to the wrong element. That trade is deliberate: silently misattributing a
|
|
59
|
+
* field is worse than dropping it.
|
|
60
|
+
*/
|
|
61
|
+
export declare function preserveUnknown(raw: unknown, original: unknown, next: unknown): unknown;
|
|
62
|
+
/** Apply {@link preserveUnknown} against a stored document. */
|
|
63
|
+
export declare function mergeForWrite<T>(document: StoredDocument<T>, next: T): unknown;
|
|
64
|
+
/** True for a JSON object, excluding arrays and null. */
|
|
65
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
66
|
+
//# sourceMappingURL=document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAGL,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAwC,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAG5F,sFAAsF;AACtF,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B,+EAA+E;IAC/E,KAAK,EAAE,CAAC,CAAC;IACT,8FAA8F;IAC9F,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7B,uEAAuE;IACvE,aAAa,EAAE,mBAAmB,CAAC;CACpC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAAC,CAAC,SAAS,mBAAmB,EAC9D,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,GACR,OAAO,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAkCvD;AAED,kEAAkE;AAClE,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,QAA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