@saplings/remote-config 0.0.0-stage → 0.1.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/browser.d.ts +11 -0
- package/dist/browser.js +57 -0
- package/dist/browser.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +160 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +40 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +7 -0
- package/dist/validation.js +40 -0
- package/dist/validation.js.map +1 -0
- package/package.json +29 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @saplings/remote-config
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 98f90ee: Harden required MFA, trusted proxy identity, SDK authorization, SSE backpressure, and native email validation, login attempt limits, concurrent config reads, and memoization of failing requests.
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 9e13a24: Add Remote Config revision approval and Miner analytics, flags, and experiments. Provide a PostgreSQL index and bounded event query for Miner.
|
|
14
|
+
|
|
15
|
+
Initial workspace implementation.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sapling Unified SDK contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @saplings/remote-config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Versioned application JSON with review, publication, and tenant-scoped reads.
|
|
4
|
+
|
|
5
|
+
## Server setup
|
|
6
|
+
|
|
7
|
+
Install `@saplings/remote-config` in the application API. Construct the service once with a transactional store, trusted scopes, and registered document validators. The Saplings SDK's InMemoryStore supports local testing. DrizzleStore uses PostgreSQL row-level security and tenant transactions for durable operation.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createRemoteConfig } from '@saplings/remote-config';
|
|
11
|
+
|
|
12
|
+
const config = createRemoteConfig({
|
|
13
|
+
store,
|
|
14
|
+
publicTenantId: 'project-wide',
|
|
15
|
+
definitions: {
|
|
16
|
+
welcome: {
|
|
17
|
+
audience: 'public',
|
|
18
|
+
validate: (value) =>
|
|
19
|
+
Object.keys(value).length === 1 && typeof value.message === 'string',
|
|
20
|
+
},
|
|
21
|
+
workspace: {
|
|
22
|
+
audience: 'signed-in',
|
|
23
|
+
validate: (value) =>
|
|
24
|
+
Object.keys(value).length === 1 && typeof value.message === 'string',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Resolve `projectId`, `tenantId`, `environment`, `actorId`, and permissions on the application backend. Provide `config:edit` to authors, `config:review` to reviewers, and `config:read` to operators viewing history. Grant project-wide public document access separately in the HTTP adapter. The application verifies signed-in reads and chooses the public partition for anonymous reads.
|
|
31
|
+
|
|
32
|
+
## Draft and publish
|
|
33
|
+
|
|
34
|
+
Call `saveDraft(actor, key, value, expectedLatest)` with revision 0 for a new document. Submit the returned revision and approve it to publish atomically. `reject` records a decision. `restore` copies a previously published revision into a draft, with the current revision as its conflict check. Use `history` for the editor and review queue. Workflow identities can receive edit permission and submit drafts for human review.
|
|
35
|
+
|
|
36
|
+
The default review policy permits self-approval in development and requires a different actor in staging and production. Set `reviewPolicy` to `operator`, `distinct-reviewer`, or an environment map in application configuration. Keep the operator policy and validators outside editable documents. Browser-facing config should contain client-visible settings. Store credentials in the application's secret provider.
|
|
37
|
+
|
|
38
|
+
## Consume published values
|
|
39
|
+
|
|
40
|
+
Return the SDK's published value, revision, and ETag from the application HTTP adapter. For React, create a reader per session and workspace, and reset it on logout or scope changes.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createConfigReader } from '@saplings/remote-config/browser';
|
|
44
|
+
|
|
45
|
+
const config = createConfigReader('/api/config/workspace', { message: 'Welcome' });
|
|
46
|
+
const current = await config.read();
|
|
47
|
+
console.log(current.message);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Revalidate every 30 seconds by default. The reader uses a scoped cached value for a transient failure and the app default until the first publication. A private read needs an authenticated application session.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Document } from './types.js';
|
|
2
|
+
/** One reader per authenticated scope. Call reset on logout or scope change. */
|
|
3
|
+
export declare function createConfigReader<T extends Document>(url: string, fallback: T, settings?: {
|
|
4
|
+
refreshMs?: number;
|
|
5
|
+
fetch?: typeof fetch;
|
|
6
|
+
}): {
|
|
7
|
+
reset(): void;
|
|
8
|
+
read(time?: number, { force }?: {
|
|
9
|
+
force?: boolean | undefined;
|
|
10
|
+
}): Promise<T>;
|
|
11
|
+
};
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** One reader per authenticated scope. Call reset on logout or scope change. */
|
|
2
|
+
export function createConfigReader(url, fallback, settings = {}) {
|
|
3
|
+
const request = settings.fetch ?? fetch;
|
|
4
|
+
let cached;
|
|
5
|
+
let nextRead = 0;
|
|
6
|
+
let generation = 0;
|
|
7
|
+
let inFlight;
|
|
8
|
+
return {
|
|
9
|
+
reset() {
|
|
10
|
+
generation++;
|
|
11
|
+
inFlight = undefined;
|
|
12
|
+
cached = undefined;
|
|
13
|
+
nextRead = 0;
|
|
14
|
+
},
|
|
15
|
+
async read(time = Date.now(), { force = false } = {}) {
|
|
16
|
+
if (!force && time < nextRead)
|
|
17
|
+
return cached?.value ?? fallback;
|
|
18
|
+
if (inFlight)
|
|
19
|
+
return inFlight;
|
|
20
|
+
const task = (async () => {
|
|
21
|
+
const started = ++generation;
|
|
22
|
+
try {
|
|
23
|
+
const response = await request(url, {
|
|
24
|
+
credentials: 'same-origin',
|
|
25
|
+
cache: 'no-store',
|
|
26
|
+
headers: cached?.etag ? { 'If-None-Match': cached.etag } : {},
|
|
27
|
+
});
|
|
28
|
+
if (started !== generation)
|
|
29
|
+
return cached?.value ?? fallback;
|
|
30
|
+
if ([401, 403, 404].includes(response.status))
|
|
31
|
+
cached = undefined;
|
|
32
|
+
else if (response.ok) {
|
|
33
|
+
const payload = (await response.json());
|
|
34
|
+
if (started !== generation)
|
|
35
|
+
return cached?.value ?? fallback;
|
|
36
|
+
cached = { value: payload.value, etag: response.headers.get('etag') };
|
|
37
|
+
}
|
|
38
|
+
nextRead = time + (settings.refreshMs ?? 30_000);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
if (started === generation)
|
|
42
|
+
nextRead = time + (settings.refreshMs ?? 30_000);
|
|
43
|
+
}
|
|
44
|
+
return cached?.value ?? fallback;
|
|
45
|
+
})();
|
|
46
|
+
inFlight = task;
|
|
47
|
+
try {
|
|
48
|
+
return await task;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
if (inFlight === task)
|
|
52
|
+
inFlight = undefined;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAEA,gFAAgF;AAChF,MAAM,UAAU,kBAAkB,CAChC,GAAW,EACX,QAAW,EACX,WAAyD,EAAE;IAE3D,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,IAAI,KAAK,CAAC;IACxC,IAAI,MAAqD,CAAC;IAC1D,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,IAAI,QAAgC,CAAC;IACrC,OAAO;QACL,KAAK;YACH,UAAU,EAAE,CAAC;YACb,QAAQ,GAAG,SAAS,CAAC;YACrB,MAAM,GAAG,SAAS,CAAC;YACnB,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,GAAG,KAAK,EAAE,GAAG,EAAE;YAClD,IAAI,CAAC,KAAK,IAAI,IAAI,GAAG,QAAQ;gBAAE,OAAO,MAAM,EAAE,KAAK,IAAI,QAAQ,CAAC;YAChE,IAAI,QAAQ;gBAAE,OAAO,QAAQ,CAAC;YAC9B,MAAM,IAAI,GAAG,CAAC,KAAK,IAAI,EAAE;gBACvB,MAAM,OAAO,GAAG,EAAE,UAAU,CAAC;gBAC7B,IAAI,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AAS,CAAC;gBACT,IAAI,QAAQ,KAAK,IAAI;oBAAE,QAAQ,GAAG,SAAS,CAAC;YAC9C,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Actor, ConfigStore, Definition, Revision, ReviewPolicy, Scope } from './types.js';
|
|
2
|
+
import { ConfigError } from './validation.js';
|
|
3
|
+
export { ConfigError };
|
|
4
|
+
export type * from './types.js';
|
|
5
|
+
export declare function createRemoteConfig(options: {
|
|
6
|
+
store: ConfigStore;
|
|
7
|
+
definitions: Readonly<Record<string, Definition>>;
|
|
8
|
+
publicTenantId: string;
|
|
9
|
+
reviewPolicy?: ReviewPolicy;
|
|
10
|
+
}): {
|
|
11
|
+
saveDraft: (actor: Actor, key: string, input: unknown, expected: number) => Promise<Revision>;
|
|
12
|
+
submit: (actor: Actor, key: string, number: number) => Promise<Revision>;
|
|
13
|
+
approve: (actor: Actor, key: string, number: number) => Promise<Revision>;
|
|
14
|
+
reject: (actor: Actor, key: string, number: number) => Promise<Revision>;
|
|
15
|
+
restore(actor: Actor, key: string, from: number, expected: number): Promise<Revision>;
|
|
16
|
+
history(actor: Actor, key: string, before?: number): Promise<{
|
|
17
|
+
latest: number;
|
|
18
|
+
published: number | null;
|
|
19
|
+
publishedRevision: Revision | undefined;
|
|
20
|
+
versions: Revision[];
|
|
21
|
+
next: number | null;
|
|
22
|
+
}>;
|
|
23
|
+
read(scope: Scope, key: string, audience: Definition["audience"]): Promise<{
|
|
24
|
+
revision: number;
|
|
25
|
+
value: import("./types.js").Document;
|
|
26
|
+
etag: string;
|
|
27
|
+
} | undefined>;
|
|
28
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { ConfigError, document, scopedKey } from './validation.js';
|
|
3
|
+
export { ConfigError };
|
|
4
|
+
const heads = 'remote-config-heads';
|
|
5
|
+
const revisions = 'remote-config-revisions';
|
|
6
|
+
const initial = (id) => ({ id, latest: 0, published: null });
|
|
7
|
+
const revisionId = (id, number) => JSON.stringify([id, number]);
|
|
8
|
+
const defaultPolicy = {
|
|
9
|
+
development: 'operator',
|
|
10
|
+
staging: 'distinct-reviewer',
|
|
11
|
+
production: 'distinct-reviewer',
|
|
12
|
+
};
|
|
13
|
+
export function createRemoteConfig(options) {
|
|
14
|
+
const { store, publicTenantId, definitions, reviewPolicy = defaultPolicy } = options;
|
|
15
|
+
const definition = (key) => {
|
|
16
|
+
if (!Object.hasOwn(definitions, key))
|
|
17
|
+
throw new ConfigError('Unknown config document.', 404);
|
|
18
|
+
return definitions[key];
|
|
19
|
+
};
|
|
20
|
+
function authorize(actor, key, permission) {
|
|
21
|
+
if (!actor.actorId || !actor.permissions.includes(permission))
|
|
22
|
+
throw new ConfigError('Config permission denied.', 403);
|
|
23
|
+
if (definition(key).audience === 'public' && actor.tenantId !== publicTenantId)
|
|
24
|
+
throw new ConfigError('Public config requires project-level access.', 403);
|
|
25
|
+
return scopedKey(actor, key);
|
|
26
|
+
}
|
|
27
|
+
async function load(tx, tenant, id, number) {
|
|
28
|
+
const stored = await tx.get(tenant, revisions, revisionId(id, number));
|
|
29
|
+
if (!stored)
|
|
30
|
+
throw new ConfigError('Config revision missing.', 404);
|
|
31
|
+
return stored;
|
|
32
|
+
}
|
|
33
|
+
async function audit(tx, actor, action, key, number, restoredFrom) {
|
|
34
|
+
await tx.put(actor.tenantId, 'audit', {
|
|
35
|
+
id: randomUUID(),
|
|
36
|
+
at: new Date().toISOString(),
|
|
37
|
+
tenantId: actor.tenantId,
|
|
38
|
+
actorId: actor.actorId,
|
|
39
|
+
resourceType: 'remote-config',
|
|
40
|
+
resourceId: key,
|
|
41
|
+
action: 'config.' + action,
|
|
42
|
+
detail: {
|
|
43
|
+
projectId: actor.projectId,
|
|
44
|
+
environment: actor.environment,
|
|
45
|
+
revision: number,
|
|
46
|
+
restoredFrom,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async function draft(actor, key, input, expected, restoredFrom) {
|
|
51
|
+
const id = authorize(actor, key, 'config:edit');
|
|
52
|
+
const value = document(input);
|
|
53
|
+
if (!definition(key).validate(value))
|
|
54
|
+
throw new ConfigError('Config does not match its registered schema.');
|
|
55
|
+
return store.transaction(actor.tenantId, async (tx) => {
|
|
56
|
+
const head = (await tx.get(actor.tenantId, heads, id)) ?? initial(id);
|
|
57
|
+
if (expected !== head.latest)
|
|
58
|
+
throw new ConfigError('Config changed. Reload the revision.', 409);
|
|
59
|
+
if (head.latest && (await load(tx, actor.tenantId, id, head.latest)).state === 'submitted')
|
|
60
|
+
throw new ConfigError('Resolve the submitted revision before creating another draft.', 409);
|
|
61
|
+
const number = head.latest + 1;
|
|
62
|
+
const draft = {
|
|
63
|
+
id: revisionId(id, number),
|
|
64
|
+
number,
|
|
65
|
+
base: head.published,
|
|
66
|
+
state: 'draft',
|
|
67
|
+
value,
|
|
68
|
+
author: actor.actorId,
|
|
69
|
+
at: new Date().toISOString(),
|
|
70
|
+
};
|
|
71
|
+
await tx.put(actor.tenantId, revisions, draft);
|
|
72
|
+
await tx.put(actor.tenantId, heads, { ...head, latest: number });
|
|
73
|
+
await audit(tx, actor, restoredFrom === undefined ? 'drafted' : 'restored', key, number, restoredFrom);
|
|
74
|
+
return draft;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async function transition(actor, key, number, action) {
|
|
78
|
+
const id = authorize(actor, key, action === 'submit' ? 'config:edit' : 'config:review');
|
|
79
|
+
return store.transaction(actor.tenantId, async (tx) => {
|
|
80
|
+
const head = await tx.get(actor.tenantId, heads, id);
|
|
81
|
+
if (!head || head.latest !== number)
|
|
82
|
+
throw new ConfigError('Config changed. Reload the revision.', 409);
|
|
83
|
+
const current = await load(tx, actor.tenantId, id, number);
|
|
84
|
+
if (current.base !== head.published ||
|
|
85
|
+
current.state !== (action === 'submit' ? 'draft' : 'submitted'))
|
|
86
|
+
throw new ConfigError('Revision is no longer eligible for this transition.', 409);
|
|
87
|
+
const policy = typeof reviewPolicy === 'string' ? reviewPolicy : reviewPolicy[actor.environment];
|
|
88
|
+
if (policy !== 'operator' && policy !== 'distinct-reviewer')
|
|
89
|
+
throw new ConfigError('Config review policy is invalid.', 500);
|
|
90
|
+
if (action === 'approve' &&
|
|
91
|
+
policy === 'distinct-reviewer' &&
|
|
92
|
+
current.author === actor.actorId)
|
|
93
|
+
throw new ConfigError('A reviewer distinct from the author is required.', 403);
|
|
94
|
+
if (action === 'approve' && !definition(key).validate(current.value))
|
|
95
|
+
throw new ConfigError('Config no longer matches its registered schema.', 409);
|
|
96
|
+
const next = {
|
|
97
|
+
...current,
|
|
98
|
+
state: action === 'submit' ? 'submitted' : action === 'approve' ? 'published' : 'rejected',
|
|
99
|
+
...(action !== 'submit' && { reviewer: actor.actorId }),
|
|
100
|
+
};
|
|
101
|
+
await tx.put(actor.tenantId, revisions, next);
|
|
102
|
+
if (action === 'approve')
|
|
103
|
+
await tx.put(actor.tenantId, heads, { ...head, published: number });
|
|
104
|
+
await audit(tx, actor, action, key, number);
|
|
105
|
+
return next;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
saveDraft: (actor, key, input, expected) => draft(actor, key, input, expected),
|
|
110
|
+
submit: (actor, key, number) => transition(actor, key, number, 'submit'),
|
|
111
|
+
approve: (actor, key, number) => transition(actor, key, number, 'approve'),
|
|
112
|
+
reject: (actor, key, number) => transition(actor, key, number, 'reject'),
|
|
113
|
+
async restore(actor, key, from, expected) {
|
|
114
|
+
const id = authorize(actor, key, 'config:edit');
|
|
115
|
+
const old = await load(store, actor.tenantId, id, from);
|
|
116
|
+
if (old.state !== 'published')
|
|
117
|
+
throw new ConfigError('Restore requires a previously published revision.', 409);
|
|
118
|
+
return draft(actor, key, old.value, expected, from);
|
|
119
|
+
},
|
|
120
|
+
async history(actor, key, before) {
|
|
121
|
+
const id = authorize(actor, key, 'config:read');
|
|
122
|
+
if (before !== undefined && (!Number.isSafeInteger(before) || before < 1))
|
|
123
|
+
throw new ConfigError('Invalid revision cursor.');
|
|
124
|
+
return store.transaction(actor.tenantId, async (tx) => {
|
|
125
|
+
const head = (await tx.get(actor.tenantId, heads, id)) ?? initial(id);
|
|
126
|
+
const start = Math.min(head.latest, (before ?? head.latest + 1) - 1);
|
|
127
|
+
const versions = [];
|
|
128
|
+
for (let n = start; n > Math.max(0, start - 25); n--)
|
|
129
|
+
versions.push(await load(tx, actor.tenantId, id, n));
|
|
130
|
+
return {
|
|
131
|
+
latest: head.latest,
|
|
132
|
+
published: head.published,
|
|
133
|
+
publishedRevision: head.published
|
|
134
|
+
? await load(tx, actor.tenantId, id, head.published)
|
|
135
|
+
: undefined,
|
|
136
|
+
versions,
|
|
137
|
+
next: start > 25 ? start - 24 : null,
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
async read(scope, key, audience) {
|
|
142
|
+
if (definition(key).audience !== audience)
|
|
143
|
+
throw new ConfigError('Unknown config document.', 404);
|
|
144
|
+
if (audience === 'public' && scope.tenantId !== publicTenantId)
|
|
145
|
+
throw new ConfigError('Unknown config document.', 404);
|
|
146
|
+
const id = scopedKey(scope, key);
|
|
147
|
+
const head = await store.get(scope.tenantId, heads, id);
|
|
148
|
+
if (!head?.published)
|
|
149
|
+
return;
|
|
150
|
+
const version = await load(store, scope.tenantId, id, head.published);
|
|
151
|
+
const etag = '"' +
|
|
152
|
+
createHash('sha256')
|
|
153
|
+
.update(JSON.stringify([scope.tenantId, id, version.number, version.value]))
|
|
154
|
+
.digest('hex') +
|
|
155
|
+
'"';
|
|
156
|
+
return { revision: version.number, value: version.value, etag };
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":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onValue = null | boolean | number | string | JsonValue[] | {
|
|
2
|
+
[key: string]: JsonValue;
|
|
3
|
+
};
|
|
4
|
+
export type Document = {
|
|
5
|
+
[key: string]: JsonValue;
|
|
6
|
+
};
|
|
7
|
+
export type Environment = 'development' | 'staging' | 'production';
|
|
8
|
+
export type ReviewPolicy = 'operator' | 'distinct-reviewer' | Record<Environment, 'operator' | 'distinct-reviewer'>;
|
|
9
|
+
export type Scope = {
|
|
10
|
+
projectId: string;
|
|
11
|
+
environment: Environment;
|
|
12
|
+
tenantId: string;
|
|
13
|
+
};
|
|
14
|
+
export type Actor = Scope & {
|
|
15
|
+
actorId: string;
|
|
16
|
+
permissions: readonly string[];
|
|
17
|
+
};
|
|
18
|
+
export type Definition = {
|
|
19
|
+
audience: 'public' | 'signed-in';
|
|
20
|
+
validate: (value: Document) => boolean;
|
|
21
|
+
};
|
|
22
|
+
export type Revision = {
|
|
23
|
+
id: string;
|
|
24
|
+
number: number;
|
|
25
|
+
base: number | null;
|
|
26
|
+
state: 'draft' | 'submitted' | 'published' | 'rejected';
|
|
27
|
+
value: Document;
|
|
28
|
+
author: string;
|
|
29
|
+
reviewer?: string;
|
|
30
|
+
at: string;
|
|
31
|
+
};
|
|
32
|
+
export interface Records {
|
|
33
|
+
get<T>(tenant: string, collection: string, id: string): Promise<T | undefined>;
|
|
34
|
+
put<T extends {
|
|
35
|
+
id: string;
|
|
36
|
+
}>(tenant: string, collection: string, record: T): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
export interface ConfigStore extends Records {
|
|
39
|
+
transaction<T>(tenant: string, run: (tx: Records) => Promise<T>): Promise<T>;
|
|
40
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Document, Scope } from './types.js';
|
|
2
|
+
export declare class ConfigError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
constructor(message: string, status?: number);
|
|
5
|
+
}
|
|
6
|
+
export declare function scopedKey(scope: Scope, key: string): string;
|
|
7
|
+
export declare function document(value: unknown): Document;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class ConfigError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
constructor(message, status = 400) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function scopedKey(scope, key) {
|
|
9
|
+
const id = /^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,99}$/;
|
|
10
|
+
if (!id.test(scope.projectId) ||
|
|
11
|
+
!id.test(scope.tenantId) ||
|
|
12
|
+
!id.test(key) ||
|
|
13
|
+
!['development', 'staging', 'production'].includes(scope.environment))
|
|
14
|
+
throw new ConfigError('Invalid config scope or key.');
|
|
15
|
+
return JSON.stringify([scope.projectId, scope.environment, key]);
|
|
16
|
+
}
|
|
17
|
+
export function document(value) {
|
|
18
|
+
function validate(entry, depth) {
|
|
19
|
+
if (depth > 16)
|
|
20
|
+
return false;
|
|
21
|
+
if (entry === null || typeof entry === 'boolean' || typeof entry === 'string')
|
|
22
|
+
return true;
|
|
23
|
+
if (typeof entry === 'number')
|
|
24
|
+
return Number.isFinite(entry);
|
|
25
|
+
if (Array.isArray(entry))
|
|
26
|
+
return entry.every((item) => validate(item, depth + 1));
|
|
27
|
+
if (!entry ||
|
|
28
|
+
typeof entry !== 'object' ||
|
|
29
|
+
![Object.prototype, null].includes(Object.getPrototypeOf(entry)))
|
|
30
|
+
return false;
|
|
31
|
+
return Object.entries(entry).every(([key, item]) => !['__proto__', 'constructor', 'prototype'].includes(key) && validate(item, depth + 1));
|
|
32
|
+
}
|
|
33
|
+
if (!value || Array.isArray(value) || typeof value !== 'object' || !validate(value, 0))
|
|
34
|
+
throw new ConfigError('Expected a plain JSON object with at most 16 nested levels.');
|
|
35
|
+
const serialized = JSON.stringify(value);
|
|
36
|
+
if (Buffer.byteLength(serialized) > 64 * 1024)
|
|
37
|
+
throw new ConfigError('Config exceeds 64 KiB.', 413);
|
|
38
|
+
return JSON.parse(serialized);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAY,SAAQ,KAAK;IAGzB;IAFX,YACE,OAAe,EACN,SAAS,GAAG;QAErB,KAAK,CAAC,OAAO,CAAC,CAAC;QAFN,WAAM,GAAN,MAAM,CAAM;IAGvB,CAAC;CACF;AAED,MAAM,UAAU,SAAS,CAAC,KAAY,EAAE,GAAW;IACjD,MAAM,EAAE,GAAG,mCAAmC,CAAC;IAC/C,IACE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;QACzB,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QACxB,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC;QACb,CAAC,CAAC,aAAa,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC;QAErE,MAAM,IAAI,WAAW,CAAC,8BAA8B,CAAC,CAAC;IACxD,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC;AACnE,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,SAAS,QAAQ,CAAC,KAAc,EAAE,KAAa;QAC7C,IAAI,KAAK,GAAG,EAAE;YAAE,OAAO,KAAK,CAAC;QAC7B,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,SAAS,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QAC3F,IAAI,OAAO,KAAK,KAAK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{
|
|
2
2
|
"name": "@saplings/remote-config",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./browser": {
|
|
13
|
+
"types": "./dist/browser.d.ts",
|
|
14
|
+
"default": "./dist/browser.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md",
|
|
20
|
+
"CHANGELOG.md"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/snrakshith/saplings.git",
|
|
26
|
+
"directory": "packages/remote-config"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"registry": "https://registry.npmjs.org/"
|
|
31
|
+
}
|
|
6
32
|
}
|