@kindgi/env-inmemory 0.0.0-bootstrap.0 → 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/README.md +75 -2
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +202 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -4
- package/src/index.ts +274 -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 describing the origin of the Work and
|
|
141
|
+
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 2026 Kindgi Inc.
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@kindgi/env-inmemory`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reference in-memory implementation of the `EnvBinding` contract from [`@kindgi/api`](../api/): the store for non-sensitive, per-environment configuration values keyed by `(scope, envName, name)`. Use it in tests and local development. It refuses to start under `NODE_ENV=production`; deployed environments use a durable `EnvBinding` implemented by the Kindgi runtime.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Give every consumer of `EnvBinding` a store that needs no database but keeps the contract's semantics: values are partitioned by `envName` (a `staging` write is invisible to `production` reads), every write bumps a revision counter that supports optimistic concurrency, and `resolve` walks from the most specific scope to the tenant. With an `AuditEventBinding` attached it also emits the `env-*` audit events, so evidence trails can be exercised end to end in tests.
|
|
8
|
+
|
|
9
|
+
## Exports
|
|
10
|
+
|
|
11
|
+
- **`createInMemoryEnvBinding(options?)`** — returns a fresh, isolated `EnvBinding` backed by process memory. Throws at construction when `process.env.NODE_ENV === 'production'` and `productionSafe` is not `true`. The returned binding:
|
|
12
|
+
- `resolve` — checks the requested scope, then the tenant scope (`project → tenant`, `org → tenant`). Returns `{ kind: 'ok', value: { name, value, revision } }` or an `env-not-found` error.
|
|
13
|
+
- `get` / `list` — exact scope only, no walk. `list` filters by `namePrefix` and `tagFilter` (every pair must match; untagged entries never match), sorts by name and returns at most `limit` entries. `cursor` is ignored and `nextCursor` is never set.
|
|
14
|
+
- `set` — creates or overwrites; `revision` starts at `1` and increments on every write. A mismatched `ifRevision` returns `{ kind: 'revision-conflict', currentRevision }` (`0` for a name that does not exist). `enqueueTuples` is required by the contract but not called.
|
|
15
|
+
- `delete` — removes the entry and returns `{ deleted }`; a later `resolve` at that scope falls through to the tenant value.
|
|
16
|
+
- **`InMemoryEnvBindingOptions`**:
|
|
17
|
+
- `productionSafe?: boolean` — default `false`. Allows construction under `NODE_ENV=production`, for example in a parity-test container.
|
|
18
|
+
- `now?: () => number` — clock used for `createdAt` / `updatedAt`; defaults to `Date.now`.
|
|
19
|
+
- `auditEvents?: AuditEventBinding`, `tenantId?: TenantId`, `projectId?: ProjectId` — audit emission is enabled only when all three are set. `set` emits `env-set` (outcome `failed` with `env-write-conflict` on a revision conflict), `resolve` emits `env-resolved`, and a `delete` that removed something emits `env-deleted`. Every event is recorded under the configured `tenantId` / `projectId`, and payloads never contain the value. Emission is awaited, but a failing audit sink is logged with `console.warn` and does not fail the operation.
|
|
20
|
+
|
|
21
|
+
## Example
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createInMemoryAuditEventBinding } from '@kindgi/audit-events-inmemory';
|
|
25
|
+
import { createInMemoryEnvBinding } from '@kindgi/env-inmemory';
|
|
26
|
+
import type { Scope } from '@kindgi/platform';
|
|
27
|
+
import { makeEnvName, type ProjectId, type TenantId } from '@kindgi/types';
|
|
28
|
+
|
|
29
|
+
const tenantId = 'acme' as TenantId;
|
|
30
|
+
const projectId = 'acme-support' as ProjectId;
|
|
31
|
+
const staging = makeEnvName('staging');
|
|
32
|
+
if (staging === null) throw new Error('invalid env name');
|
|
33
|
+
|
|
34
|
+
const tenant: Scope = { kind: 'tenant', tenantId };
|
|
35
|
+
const project: Scope = { kind: 'project', tenantId, projectId };
|
|
36
|
+
|
|
37
|
+
const audit = createInMemoryAuditEventBinding();
|
|
38
|
+
const env = createInMemoryEnvBinding({ auditEvents: audit, tenantId, projectId });
|
|
39
|
+
|
|
40
|
+
const kbUrl = { envName: staging, name: 'KB_URL' } as const;
|
|
41
|
+
await env.set({ scope: tenant, ...kbUrl, value: 'https://kb.acme.test', enqueueTuples: () => [] });
|
|
42
|
+
|
|
43
|
+
// The project has no value of its own, so resolution falls back to the tenant.
|
|
44
|
+
const resolved = await env.resolve({
|
|
45
|
+
scope: project,
|
|
46
|
+
...kbUrl,
|
|
47
|
+
resolveContext: { caller: 'dispatch', runId: 'run-1' },
|
|
48
|
+
});
|
|
49
|
+
if (resolved.kind === 'ok') console.log(resolved.value.value, resolved.value.revision); // https://kb.acme.test 1
|
|
50
|
+
|
|
51
|
+
// Optimistic concurrency: this write applies only while the tenant value is at revision 1.
|
|
52
|
+
const updated = await env.set({
|
|
53
|
+
scope: tenant,
|
|
54
|
+
...kbUrl,
|
|
55
|
+
value: 'https://kb-v2.acme.test',
|
|
56
|
+
ifRevision: 1,
|
|
57
|
+
enqueueTuples: () => [],
|
|
58
|
+
});
|
|
59
|
+
if (updated.kind === 'revision-conflict') console.warn('stale write', updated.currentRevision);
|
|
60
|
+
|
|
61
|
+
const events = await audit.query({ tenantId }); // env-set, env-resolved, env-set
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Non-goals
|
|
65
|
+
|
|
66
|
+
- **Durability or cross-process sharing.** State lives in one process and is lost on exit.
|
|
67
|
+
- **Org-level resolution for project scopes.** The in-memory store has no project → org relation, so a project scope falls back straight to the tenant. Durable implementations walk `project → org → tenant`.
|
|
68
|
+
- **Secrets.** Values are stored and returned in plaintext; sensitive values belong in a `SecretBinding`.
|
|
69
|
+
- **Soft delete.** `delete` removes the entry; the contract defines env as a plain overwrite-shaped store.
|
|
70
|
+
|
|
71
|
+
## Related
|
|
72
|
+
|
|
73
|
+
- [`@kindgi/api`](../api/) — the `EnvBinding` contract and `ResolveContext`.
|
|
74
|
+
- [`@kindgi/platform`](../platform/) — the `Scope` union.
|
|
75
|
+
- [`@kindgi/types`](../types/) — `EnvName` and the `makeEnvName` validator.
|
|
76
|
+
- [`@kindgi/audit-events-inmemory`](../audit-events-inmemory/) — in-memory `AuditEventBinding` for the emitted events.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@kindgi/env-inmemory` — reference in-memory `EnvBinding` for
|
|
3
|
+
* dev + tests. Refuses to boot in production.
|
|
4
|
+
*
|
|
5
|
+
* Storage: `Map<scopeKey(scope), Map<EnvName, Map<name, EnvRecord>>>`.
|
|
6
|
+
* Resolution walks most-specific-wins: `project → tenant` and
|
|
7
|
+
* `org → tenant`. A project scope skips the `org` middle step because
|
|
8
|
+
* this adapter has no project → org relation; the full walk described
|
|
9
|
+
* on `Scope` in `@kindgi/platform` is `project → org → tenant`.
|
|
10
|
+
*/
|
|
11
|
+
import type { EnvBinding } from '@kindgi/api';
|
|
12
|
+
import type { AuditEventBinding } from '@kindgi/audit-events';
|
|
13
|
+
import type { ProjectId, TenantId } from '@kindgi/types';
|
|
14
|
+
/**
|
|
15
|
+
* Options for the in-memory env binding.
|
|
16
|
+
*/
|
|
17
|
+
export interface InMemoryEnvBindingOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Set `true` to allow booting under `NODE_ENV === 'production'`.
|
|
20
|
+
* Default `false` — `createInMemoryEnvBinding` throws when the process
|
|
21
|
+
* is in production without this flag. Never set in production unless
|
|
22
|
+
* the process is truly a dev-shape (e.g. an isolated test container
|
|
23
|
+
* running with `NODE_ENV=production` for parity testing).
|
|
24
|
+
*/
|
|
25
|
+
readonly productionSafe?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Deterministic clock for tests. Defaults to `Date.now`.
|
|
28
|
+
*/
|
|
29
|
+
readonly now?: () => number;
|
|
30
|
+
/**
|
|
31
|
+
* Optional unified audit binding — when supplied, `set`, `resolve`,
|
|
32
|
+
* and a `delete` that removed an entry each emit an `env-*`
|
|
33
|
+
* `AuditEvent`. Emission is awaited before the op returns; emission
|
|
34
|
+
* failures are logged (`console.warn`) but never fail the caller's op.
|
|
35
|
+
*
|
|
36
|
+
* `tenantId` + `projectId` must be provided alongside — emission is
|
|
37
|
+
* enabled only when all three are set, and every emitted record is
|
|
38
|
+
* attributed to this tenant + project.
|
|
39
|
+
*/
|
|
40
|
+
readonly auditEvents?: AuditEventBinding;
|
|
41
|
+
readonly tenantId?: TenantId;
|
|
42
|
+
readonly projectId?: ProjectId;
|
|
43
|
+
}
|
|
44
|
+
export declare function createInMemoryEnvBinding(options?: InMemoryEnvBindingOptions): EnvBinding;
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EACV,UAAU,EAYX,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAK9D,OAAO,KAAK,EAAW,SAAS,EAAU,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE1E;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;;;;OAMG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAClC;;OAEG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IAC5B;;;;;;;;;OASG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;CAChC;AAED,wBAAgB,wBAAwB,CAAC,OAAO,GAAE,yBAA8B,GAAG,UAAU,CAsM5F"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
import { emitLifecycleEvent, emitResolveEvent } from '@kindgi/compliance';
|
|
4
|
+
import { scopeKey } from '@kindgi/platform';
|
|
5
|
+
export function createInMemoryEnvBinding(options = {}) {
|
|
6
|
+
if (!options.productionSafe && process.env.NODE_ENV === 'production') {
|
|
7
|
+
throw new Error('@kindgi/env-inmemory refuses to boot with NODE_ENV=production; supply { productionSafe: true } if this is a parity test.');
|
|
8
|
+
}
|
|
9
|
+
const now = options.now ?? (() => Date.now());
|
|
10
|
+
const auditEvents = options.auditEvents;
|
|
11
|
+
const evidenceTenant = options.tenantId;
|
|
12
|
+
const evidenceProject = options.projectId;
|
|
13
|
+
const canEmit = auditEvents !== undefined && evidenceTenant !== undefined && evidenceProject !== undefined;
|
|
14
|
+
// scopeKey → envName → name → record.
|
|
15
|
+
const store = new Map();
|
|
16
|
+
const bucket = (scope, envName) => {
|
|
17
|
+
const sk = scopeKey(scope);
|
|
18
|
+
let byEnv = store.get(sk);
|
|
19
|
+
if (byEnv === undefined) {
|
|
20
|
+
byEnv = new Map();
|
|
21
|
+
store.set(sk, byEnv);
|
|
22
|
+
}
|
|
23
|
+
let byName = byEnv.get(envName);
|
|
24
|
+
if (byName === undefined) {
|
|
25
|
+
byName = new Map();
|
|
26
|
+
byEnv.set(envName, byName);
|
|
27
|
+
}
|
|
28
|
+
return byName;
|
|
29
|
+
};
|
|
30
|
+
const parents = (scope) => {
|
|
31
|
+
// Most-specific first; caller iterates and returns first hit.
|
|
32
|
+
const chain = [scope];
|
|
33
|
+
if (scope.kind === 'project') {
|
|
34
|
+
chain.push({ kind: 'tenant', tenantId: scope.tenantId });
|
|
35
|
+
}
|
|
36
|
+
else if (scope.kind === 'org') {
|
|
37
|
+
chain.push({ kind: 'tenant', tenantId: scope.tenantId });
|
|
38
|
+
}
|
|
39
|
+
return chain;
|
|
40
|
+
};
|
|
41
|
+
const seqRef = { seq: 0 };
|
|
42
|
+
const nextTimestamp = () => {
|
|
43
|
+
seqRef.seq += 1;
|
|
44
|
+
return new Date(now() + seqRef.seq).toISOString();
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
async list(input) {
|
|
48
|
+
const byName = bucket(input.scope, input.envName);
|
|
49
|
+
const all = [];
|
|
50
|
+
for (const rec of byName.values()) {
|
|
51
|
+
if (input.namePrefix !== undefined &&
|
|
52
|
+
input.namePrefix.length > 0 &&
|
|
53
|
+
!rec.name.startsWith(input.namePrefix)) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (input.tagFilter !== undefined) {
|
|
57
|
+
if (!matchesTagFilter(rec, input.tagFilter))
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
all.push(rec);
|
|
61
|
+
}
|
|
62
|
+
all.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
63
|
+
return { data: all.slice(0, input.limit) };
|
|
64
|
+
},
|
|
65
|
+
async get(input) {
|
|
66
|
+
const byName = bucket(input.scope, input.envName);
|
|
67
|
+
return byName.get(input.name) ?? null;
|
|
68
|
+
},
|
|
69
|
+
async resolve(input) {
|
|
70
|
+
for (const s of parents(input.scope)) {
|
|
71
|
+
const byName = bucket(s, input.envName);
|
|
72
|
+
const rec = byName.get(input.name);
|
|
73
|
+
if (rec !== undefined) {
|
|
74
|
+
if (canEmit) {
|
|
75
|
+
await emitResolveEvent({
|
|
76
|
+
kind: 'env',
|
|
77
|
+
scope: input.scope,
|
|
78
|
+
envName: input.envName,
|
|
79
|
+
name: input.name,
|
|
80
|
+
outcome: 'succeeded',
|
|
81
|
+
version: rec.revision,
|
|
82
|
+
resolveContext: input.resolveContext,
|
|
83
|
+
auditEvents: auditEvents,
|
|
84
|
+
tenantId: evidenceTenant,
|
|
85
|
+
projectId: evidenceProject,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
kind: 'ok',
|
|
90
|
+
value: { name: rec.name, value: rec.value, revision: rec.revision },
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (canEmit) {
|
|
95
|
+
await emitResolveEvent({
|
|
96
|
+
kind: 'env',
|
|
97
|
+
scope: input.scope,
|
|
98
|
+
envName: input.envName,
|
|
99
|
+
name: input.name,
|
|
100
|
+
outcome: 'failed',
|
|
101
|
+
errorCode: 'env-not-found',
|
|
102
|
+
resolveContext: input.resolveContext,
|
|
103
|
+
auditEvents: auditEvents,
|
|
104
|
+
tenantId: evidenceTenant,
|
|
105
|
+
projectId: evidenceProject,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
kind: 'err',
|
|
110
|
+
error: {
|
|
111
|
+
code: 'env-not-found',
|
|
112
|
+
message: `No env value at scope + envName for name "${input.name}"`,
|
|
113
|
+
name: input.name,
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
async set(input) {
|
|
118
|
+
const byName = bucket(input.scope, input.envName);
|
|
119
|
+
const existing = byName.get(input.name);
|
|
120
|
+
if (input.ifRevision !== undefined) {
|
|
121
|
+
const current = existing?.revision ?? 0;
|
|
122
|
+
if (current !== input.ifRevision) {
|
|
123
|
+
if (canEmit) {
|
|
124
|
+
await emitLifecycleEvent({
|
|
125
|
+
evidenceKind: 'env-set',
|
|
126
|
+
bindingKind: 'env',
|
|
127
|
+
scope: input.scope,
|
|
128
|
+
envName: input.envName,
|
|
129
|
+
name: input.name,
|
|
130
|
+
outcome: 'failed',
|
|
131
|
+
errorCode: 'env-write-conflict',
|
|
132
|
+
version: current,
|
|
133
|
+
auditEvents: auditEvents,
|
|
134
|
+
tenantId: evidenceTenant,
|
|
135
|
+
projectId: evidenceProject,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return { kind: 'revision-conflict', currentRevision: current };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const createdAt = existing?.createdAt ?? nextTimestamp();
|
|
142
|
+
const updatedAt = nextTimestamp();
|
|
143
|
+
const previousRevision = existing?.revision;
|
|
144
|
+
const record = {
|
|
145
|
+
scope: input.scope,
|
|
146
|
+
envName: input.envName,
|
|
147
|
+
name: input.name,
|
|
148
|
+
value: input.value,
|
|
149
|
+
revision: (existing?.revision ?? 0) + 1,
|
|
150
|
+
createdAt,
|
|
151
|
+
updatedAt,
|
|
152
|
+
...(input.tags !== undefined && { tags: input.tags }),
|
|
153
|
+
};
|
|
154
|
+
byName.set(input.name, record);
|
|
155
|
+
if (canEmit) {
|
|
156
|
+
await emitLifecycleEvent({
|
|
157
|
+
evidenceKind: 'env-set',
|
|
158
|
+
bindingKind: 'env',
|
|
159
|
+
scope: input.scope,
|
|
160
|
+
envName: input.envName,
|
|
161
|
+
name: input.name,
|
|
162
|
+
outcome: 'succeeded',
|
|
163
|
+
version: record.revision,
|
|
164
|
+
...(previousRevision !== undefined && { previousVersion: previousRevision }),
|
|
165
|
+
auditEvents: auditEvents,
|
|
166
|
+
tenantId: evidenceTenant,
|
|
167
|
+
projectId: evidenceProject,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return { kind: 'ok', record };
|
|
171
|
+
},
|
|
172
|
+
async delete(input) {
|
|
173
|
+
const byName = bucket(input.scope, input.envName);
|
|
174
|
+
const had = byName.delete(input.name);
|
|
175
|
+
if (had && canEmit) {
|
|
176
|
+
await emitLifecycleEvent({
|
|
177
|
+
evidenceKind: 'env-deleted',
|
|
178
|
+
bindingKind: 'env',
|
|
179
|
+
scope: input.scope,
|
|
180
|
+
envName: input.envName,
|
|
181
|
+
name: input.name,
|
|
182
|
+
outcome: 'succeeded',
|
|
183
|
+
auditEvents: auditEvents,
|
|
184
|
+
tenantId: evidenceTenant,
|
|
185
|
+
projectId: evidenceProject,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return { deleted: had };
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function matchesTagFilter(rec, filter) {
|
|
193
|
+
const tags = rec.tags;
|
|
194
|
+
if (tags === undefined)
|
|
195
|
+
return false;
|
|
196
|
+
for (const [k, v] of Object.entries(filter)) {
|
|
197
|
+
if (tags[k] !== v)
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AA6BjC,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE1E,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAkC5C,MAAM,UAAU,wBAAwB,CAAC,UAAqC,EAAE;IAC9E,IAAI,CAAC,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE,CAAC;QACrE,MAAM,IAAI,KAAK,CACb,0HAA0H,CAC3H,CAAC;IACJ,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAC9C,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACxC,MAAM,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IACxC,MAAM,eAAe,GAAG,OAAO,CAAC,SAAS,CAAC;IAC1C,MAAM,OAAO,GACX,WAAW,KAAK,SAAS,IAAI,cAAc,KAAK,SAAS,IAAI,eAAe,KAAK,SAAS,CAAC;IAE7F,sCAAsC;IACtC,M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": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Reference in-memory `EnvBinding` adapter — dev + tests only; refuses to boot in production.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"repository": {
|
|
7
|
-
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kindgi/kindgi-sdk.git",
|
|
9
|
+
"directory": "packages/env-inmemory"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/kindgi/kindgi-sdk/tree/main/packages/env-inmemory#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kindgi/kindgi-sdk/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@kindgi/api": "0.1.0",
|
|
31
|
+
"@kindgi/audit-events": "0.1.0",
|
|
32
|
+
"@kindgi/compliance": "0.1.0",
|
|
33
|
+
"@kindgi/platform": "0.1.0",
|
|
34
|
+
"@kindgi/types": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@kindgi/audit-events-inmemory": "0.1.0",
|
|
38
|
+
"@types/node": "^22.10.5",
|
|
39
|
+
"typescript": "^5.7.3",
|
|
40
|
+
"vitest": "^2.1.8"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22.0.0"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"provenance": true
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -p tsconfig.build.json",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@kindgi/env-inmemory` — reference in-memory `EnvBinding` for
|
|
6
|
+
* dev + tests. Refuses to boot in production.
|
|
7
|
+
*
|
|
8
|
+
* Storage: `Map<scopeKey(scope), Map<EnvName, Map<name, EnvRecord>>>`.
|
|
9
|
+
* Resolution walks most-specific-wins: `project → tenant` and
|
|
10
|
+
* `org → tenant`. A project scope skips the `org` middle step because
|
|
11
|
+
* this adapter has no project → org relation; the full walk described
|
|
12
|
+
* on `Scope` in `@kindgi/platform` is `project → org → tenant`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
EnvBinding,
|
|
17
|
+
EnvDeleteInput,
|
|
18
|
+
EnvDeleteOutcome,
|
|
19
|
+
EnvError,
|
|
20
|
+
EnvGetInput,
|
|
21
|
+
EnvListInput,
|
|
22
|
+
EnvListPage,
|
|
23
|
+
EnvRecord,
|
|
24
|
+
EnvResolveInput,
|
|
25
|
+
EnvResolveOutcome,
|
|
26
|
+
EnvSetInput,
|
|
27
|
+
EnvSetOutcome,
|
|
28
|
+
} from '@kindgi/api';
|
|
29
|
+
import type { AuditEventBinding } from '@kindgi/audit-events';
|
|
30
|
+
import type { ResolveContext as ComplianceResolveContext } from '@kindgi/compliance';
|
|
31
|
+
import { emitLifecycleEvent, emitResolveEvent } from '@kindgi/compliance';
|
|
32
|
+
import type { Scope } from '@kindgi/platform';
|
|
33
|
+
import { scopeKey } from '@kindgi/platform';
|
|
34
|
+
import type { EnvName, ProjectId, Result, TenantId } from '@kindgi/types';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Options for the in-memory env binding.
|
|
38
|
+
*/
|
|
39
|
+
export interface InMemoryEnvBindingOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Set `true` to allow booting under `NODE_ENV === 'production'`.
|
|
42
|
+
* Default `false` — `createInMemoryEnvBinding` throws when the process
|
|
43
|
+
* is in production without this flag. Never set in production unless
|
|
44
|
+
* the process is truly a dev-shape (e.g. an isolated test container
|
|
45
|
+
* running with `NODE_ENV=production` for parity testing).
|
|
46
|
+
*/
|
|
47
|
+
readonly productionSafe?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Deterministic clock for tests. Defaults to `Date.now`.
|
|
50
|
+
*/
|
|
51
|
+
readonly now?: () => number;
|
|
52
|
+
/**
|
|
53
|
+
* Optional unified audit binding — when supplied, `set`, `resolve`,
|
|
54
|
+
* and a `delete` that removed an entry each emit an `env-*`
|
|
55
|
+
* `AuditEvent`. Emission is awaited before the op returns; emission
|
|
56
|
+
* failures are logged (`console.warn`) but never fail the caller's op.
|
|
57
|
+
*
|
|
58
|
+
* `tenantId` + `projectId` must be provided alongside — emission is
|
|
59
|
+
* enabled only when all three are set, and every emitted record is
|
|
60
|
+
* attributed to this tenant + project.
|
|
61
|
+
*/
|
|
62
|
+
readonly auditEvents?: AuditEventBinding;
|
|
63
|
+
readonly tenantId?: TenantId;
|
|
64
|
+
readonly projectId?: ProjectId;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function createInMemoryEnvBinding(options: InMemoryEnvBindingOptions = {}): EnvBinding {
|
|
68
|
+
if (!options.productionSafe && process.env.NODE_ENV === 'production') {
|
|
69
|
+
throw new Error(
|
|
70
|
+
'@kindgi/env-inmemory refuses to boot with NODE_ENV=production; supply { productionSafe: true } if this is a parity test.',
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
const now = options.now ?? (() => Date.now());
|
|
74
|
+
const auditEvents = options.auditEvents;
|
|
75
|
+
const evidenceTenant = options.tenantId;
|
|
76
|
+
const evidenceProject = options.projectId;
|
|
77
|
+
const canEmit =
|
|
78
|
+
auditEvents !== undefined && evidenceTenant !== undefined && evidenceProject !== undefined;
|
|
79
|
+
|
|
80
|
+
// scopeKey → envName → name → record.
|
|
81
|
+
const store = new Map<string, Map<EnvName, Map<string, EnvRecord>>>();
|
|
82
|
+
|
|
83
|
+
const bucket = (scope: Scope, envName: EnvName): Map<string, EnvRecord> => {
|
|
84
|
+
const sk = scopeKey(scope);
|
|
85
|
+
let byEnv = store.get(sk);
|
|
86
|
+
if (byEnv === undefined) {
|
|
87
|
+
byEnv = new Map();
|
|
88
|
+
store.set(sk, byEnv);
|
|
89
|
+
}
|
|
90
|
+
let byName = byEnv.get(envName);
|
|
91
|
+
if (byName === undefined) {
|
|
92
|
+
byName = new Map();
|
|
93
|
+
byEnv.set(envName, byName);
|
|
94
|
+
}
|
|
95
|
+
return byName;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const parents = (scope: Scope): Scope[] => {
|
|
99
|
+
// Most-specific first; caller iterates and returns first hit.
|
|
100
|
+
const chain: Scope[] = [scope];
|
|
101
|
+
if (scope.kind === 'project') {
|
|
102
|
+
chain.push({ kind: 'tenant', tenantId: scope.tenantId as TenantId });
|
|
103
|
+
} else if (scope.kind === 'org') {
|
|
104
|
+
chain.push({ kind: 'tenant', tenantId: scope.tenantId as TenantId });
|
|
105
|
+
}
|
|
106
|
+
return chain;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const seqRef = { seq: 0 };
|
|
110
|
+
const nextTimestamp = (): string => {
|
|
111
|
+
seqRef.seq += 1;
|
|
112
|
+
return new Date(now() + seqRef.seq).toISOString();
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
async list(input: EnvListInput): Promise<EnvListPage> {
|
|
117
|
+
const byName = bucket(input.scope, input.envName);
|
|
118
|
+
const all: EnvRecord[] = [];
|
|
119
|
+
for (const rec of byName.values()) {
|
|
120
|
+
if (
|
|
121
|
+
input.namePrefix !== undefined &&
|
|
122
|
+
input.namePrefix.length > 0 &&
|
|
123
|
+
!rec.name.startsWith(input.namePrefix)
|
|
124
|
+
) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (input.tagFilter !== undefined) {
|
|
128
|
+
if (!matchesTagFilter(rec, input.tagFilter)) continue;
|
|
129
|
+
}
|
|
130
|
+
all.push(rec);
|
|
131
|
+
}
|
|
132
|
+
all.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
133
|
+
return { data: all.slice(0, input.limit) };
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
async get(input: EnvGetInput): Promise<EnvRecord | null> {
|
|
137
|
+
const byName = bucket(input.scope, input.envName);
|
|
138
|
+
return byName.get(input.name) ?? null;
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
async resolve(input: EnvResolveInput): Promise<Result<EnvResolveOutcome, EnvError>> {
|
|
142
|
+
for (const s of parents(input.scope)) {
|
|
143
|
+
const byName = bucket(s, input.envName);
|
|
144
|
+
const rec = byName.get(input.name);
|
|
145
|
+
if (rec !== undefined) {
|
|
146
|
+
if (canEmit) {
|
|
147
|
+
await emitResolveEvent({
|
|
148
|
+
kind: 'env',
|
|
149
|
+
scope: input.scope,
|
|
150
|
+
envName: input.envName,
|
|
151
|
+
name: input.name,
|
|
152
|
+
outcome: 'succeeded',
|
|
153
|
+
version: rec.revision,
|
|
154
|
+
resolveContext: input.resolveContext as ComplianceResolveContext,
|
|
155
|
+
auditEvents: auditEvents!,
|
|
156
|
+
tenantId: evidenceTenant,
|
|
157
|
+
projectId: evidenceProject,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
kind: 'ok',
|
|
162
|
+
value: { name: rec.name, value: rec.value, revision: rec.revision },
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (canEmit) {
|
|
167
|
+
await emitResolveEvent({
|
|
168
|
+
kind: 'env',
|
|
169
|
+
scope: input.scope,
|
|
170
|
+
envName: input.envName,
|
|
171
|
+
name: input.name,
|
|
172
|
+
outcome: 'failed',
|
|
173
|
+
errorCode: 'env-not-found',
|
|
174
|
+
resolveContext: input.resolveContext as ComplianceResolveContext,
|
|
175
|
+
auditEvents: auditEvents!,
|
|
176
|
+
tenantId: evidenceTenant,
|
|
177
|
+
projectId: evidenceProject,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
kind: 'err',
|
|
182
|
+
error: {
|
|
183
|
+
code: 'env-not-found',
|
|
184
|
+
message: `No env value at scope + envName for name "${input.name}"`,
|
|
185
|
+
name: input.name,
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
async set(input: EnvSetInput): Promise<EnvSetOutcome> {
|
|
191
|
+
const byName = bucket(input.scope, input.envName);
|
|
192
|
+
const existing = byName.get(input.name);
|
|
193
|
+
if (input.ifRevision !== undefined) {
|
|
194
|
+
const current = existing?.revision ?? 0;
|
|
195
|
+
if (current !== input.ifRevision) {
|
|
196
|
+
if (canEmit) {
|
|
197
|
+
await emitLifecycleEvent({
|
|
198
|
+
evidenceKind: 'env-set',
|
|
199
|
+
bindingKind: 'env',
|
|
200
|
+
scope: input.scope,
|
|
201
|
+
envName: input.envName,
|
|
202
|
+
name: input.name,
|
|
203
|
+
outcome: 'failed',
|
|
204
|
+
errorCode: 'env-write-conflict',
|
|
205
|
+
version: current,
|
|
206
|
+
auditEvents: auditEvents!,
|
|
207
|
+
tenantId: evidenceTenant,
|
|
208
|
+
projectId: evidenceProject,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return { kind: 'revision-conflict', currentRevision: current };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const createdAt = existing?.createdAt ?? nextTimestamp();
|
|
215
|
+
const updatedAt = nextTimestamp();
|
|
216
|
+
const previousRevision = existing?.revision;
|
|
217
|
+
const record: EnvRecord = {
|
|
218
|
+
scope: input.scope,
|
|
219
|
+
envName: input.envName,
|
|
220
|
+
name: input.name,
|
|
221
|
+
value: input.value,
|
|
222
|
+
revision: (existing?.revision ?? 0) + 1,
|
|
223
|
+
createdAt,
|
|
224
|
+
updatedAt,
|
|
225
|
+
...(input.tags !== undefined && { tags: input.tags }),
|
|
226
|
+
};
|
|
227
|
+
byName.set(input.name, record);
|
|
228
|
+
if (canEmit) {
|
|
229
|
+
await emitLifecycleEvent({
|
|
230
|
+
evidenceKind: 'env-set',
|
|
231
|
+
bindingKind: 'env',
|
|
232
|
+
scope: input.scope,
|
|
233
|
+
envName: input.envName,
|
|
234
|
+
name: input.name,
|
|
235
|
+
outcome: 'succeeded',
|
|
236
|
+
version: record.revision,
|
|
237
|
+
...(previousRevision !== undefined && { previousVersion: previousRevision }),
|
|
238
|
+
auditEvents: auditEvents!,
|
|
239
|
+
tenantId: evidenceTenant,
|
|
240
|
+
projectId: evidenceProject,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return { kind: 'ok', record };
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
async delete(input: EnvDeleteInput): Promise<EnvDeleteOutcome> {
|
|
247
|
+
const byName = bucket(input.scope, input.envName);
|
|
248
|
+
const had = byName.delete(input.name);
|
|
249
|
+
if (had && canEmit) {
|
|
250
|
+
await emitLifecycleEvent({
|
|
251
|
+
evidenceKind: 'env-deleted',
|
|
252
|
+
bindingKind: 'env',
|
|
253
|
+
scope: input.scope,
|
|
254
|
+
envName: input.envName,
|
|
255
|
+
name: input.name,
|
|
256
|
+
outcome: 'succeeded',
|
|
257
|
+
auditEvents: auditEvents!,
|
|
258
|
+
tenantId: evidenceTenant,
|
|
259
|
+
projectId: evidenceProject,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
return { deleted: had };
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function matchesTagFilter(rec: EnvRecord, filter: Readonly<Record<string, string>>): boolean {
|
|
268
|
+
const tags = rec.tags;
|
|
269
|
+
if (tags === undefined) return false;
|
|
270
|
+
for (const [k, v] of Object.entries(filter)) {
|
|
271
|
+
if (tags[k] !== v) return false;
|
|
272
|
+
}
|
|
273
|
+
return true;
|
|
274
|
+
}
|