@serve.zone/interfaces 32.92.0 → 32.93.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/changelog.md +6 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/data/gatewayarearows.d.ts +379 -0
- package/dist_ts/data/gatewayarearows.js +347 -0
- package/dist_ts/data/gatewayareas.d.ts +24 -0
- package/dist_ts/data/gatewayareas.js +41 -1
- package/dist_ts/data/gatewayareas.validation.d.ts +11 -3
- package/dist_ts/data/gatewayareas.validation.js +41 -58
- package/dist_ts/data/gatewayintent.d.ts +6 -5
- package/dist_ts/data/gatewayintent.js +18 -5
- package/dist_ts/data/index.d.ts +1 -0
- package/dist_ts/data/index.js +2 -1
- package/dist_ts/requests/gatewayconnector.d.ts +70 -0
- package/package.json +1 -1
- package/readme.md +43 -3
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/data/gatewayarearows.ts +688 -0
- package/ts/data/gatewayareas.ts +55 -0
- package/ts/data/gatewayareas.validation.ts +43 -59
- package/ts/data/gatewayintent.ts +17 -4
- package/ts/data/index.ts +1 -0
- package/ts/requests/gatewayconnector.ts +83 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* An administrator's row-level read and write of the area tables a controller holds for a gateway it
|
|
3
|
+
* took over (Linus rulings 117 §4 criterion 4 and §6, 121 §6 item 7, 123 D1, 166 point 4, 171): the
|
|
4
|
+
* one way an operator changes a gateway table row once the gateway refuses local writes. The requests
|
|
5
|
+
* are `requests.gatewayconnector.IReq_Admin_Cloudly_GetGatewayAreaRows` and
|
|
6
|
+
* `IReq_Admin_Cloudly_WriteGatewayAreaRow`.
|
|
7
|
+
*
|
|
8
|
+
* A row is addressed by area, member and id: a list member's row by its `id`, a singleton member
|
|
9
|
+
* (`gatewayAreaSingletonMembers`) by `id: null`. Its write form (`IGatewayAreaRowWrites`) is its
|
|
10
|
+
* table type (`gatewayareas.ts`) except that every secret-class member (`gatewayAreaSecretPaths`) is
|
|
11
|
+
* a keep-or-set choice: a read answers every secret as `keep` (a sealed member with its version, a
|
|
12
|
+
* verifier without anything), so a row read and sent back unchanged writes nothing, and a write
|
|
13
|
+
* states a new secret only as `set`. A sealed value's version is minted by the controller that
|
|
14
|
+
* resolves the write (Linus ruling 121 Q9: by every write that sets a secret), never by the request.
|
|
15
|
+
* Nothing here carries a stored secret value: reads and answers name versions, never values.
|
|
16
|
+
*
|
|
17
|
+
* Both requests are for a human administrator who signed in within the last five minutes, with
|
|
18
|
+
* neither a delegated admin temp token nor a machine identity (Linus rulings 166 point 2 and 171
|
|
19
|
+
* D6); no delegated admin scope names either. What else the controller judges stays the
|
|
20
|
+
* controller's: the table and document validation, the lifeline, the rows its own records compile.
|
|
21
|
+
* This file holds the shapes and the pure helpers that own where a row's secrets are
|
|
22
|
+
* (`projectGatewayAreaRow`, `resolveGatewayAreaRowWrite`, `computeGatewayAreaRowDigest`) and the one
|
|
23
|
+
* check of a write's envelope (`validateGatewayAreaRowWrite`), so neither the controller nor a
|
|
24
|
+
* client keeps a copy of them.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { canonicalizeStrictJson, compareWireTokens, createCanonicalJsonSha256Hex } from '../private/canonicaljson.js';
|
|
28
|
+
import { isPlainRecord, readDataProperty, readExactDataRecord } from '../private/dataproperties.js';
|
|
29
|
+
import type {
|
|
30
|
+
IGatewayAcmeSettings,
|
|
31
|
+
IGatewayAreaKeyRow,
|
|
32
|
+
IGatewayAreaSecretPath,
|
|
33
|
+
IGatewayApiTokenRow,
|
|
34
|
+
IGatewayBlockRuleRow,
|
|
35
|
+
IGatewayCoreMailCredentialVerifier,
|
|
36
|
+
IGatewayCoreMailPeerRow,
|
|
37
|
+
IGatewayDnsClientHostnameRow,
|
|
38
|
+
IGatewayDnsDomainRow,
|
|
39
|
+
IGatewayDnsProtectedHostnameRow,
|
|
40
|
+
IGatewayDnsProviderRow,
|
|
41
|
+
IGatewayDnsRecordRow,
|
|
42
|
+
IGatewayDnsServerSettings,
|
|
43
|
+
IGatewayDnsStaticRecordRow,
|
|
44
|
+
IGatewayEdgeHubSettings,
|
|
45
|
+
IGatewayEdgeRow,
|
|
46
|
+
IGatewayHttp3Settings,
|
|
47
|
+
IGatewayMailDomainRow,
|
|
48
|
+
IGatewayMailIdentityRow,
|
|
49
|
+
IGatewayMailServerSettings,
|
|
50
|
+
IGatewayNetworkTargetRow,
|
|
51
|
+
IGatewayRouteRow,
|
|
52
|
+
IGatewayScramVerifier,
|
|
53
|
+
IGatewaySmtpAccountRow,
|
|
54
|
+
IGatewaySourceProfileRow,
|
|
55
|
+
IGatewayTargetProfileRow,
|
|
56
|
+
IGatewayTokenHash,
|
|
57
|
+
TGatewayAreaSecretClass,
|
|
58
|
+
TGatewayEngineConfig,
|
|
59
|
+
} from './gatewayareas.js';
|
|
60
|
+
import { gatewayAreaMembers, gatewayAreaSecretPaths } from './gatewayareas.js';
|
|
61
|
+
import { isGatewayAreaRowId } from './gatewayareas.validation.js';
|
|
62
|
+
import { isGatewayConfigArea, type TGatewayConfigArea } from './gatewayconnector.js';
|
|
63
|
+
import type { TGatewayIntentRefusal } from './gatewayintent.js';
|
|
64
|
+
import { isSha256Digest, type TSha256Digest } from './immutableimage.js';
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------------------------
|
|
67
|
+
// Secrets in a row write
|
|
68
|
+
// ---------------------------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
/** A sealed member (`IGatewaySealedValue`) in a row's write form. */
|
|
71
|
+
export type TGatewaySealedValueWrite =
|
|
72
|
+
/**
|
|
73
|
+
* Keep the value the stored row holds at this path under this version: the form a read answers.
|
|
74
|
+
* A version the stored row does not hold there is refused `kept-secret-unknown`: the written row
|
|
75
|
+
* keeps a secret this row does not hold (a new row, a member stored `null`, another row's
|
|
76
|
+
* version), so the row is fixed, not read again; a row another write moved answers
|
|
77
|
+
* `intent-row-stale` first.
|
|
78
|
+
*/
|
|
79
|
+
| { kind: 'keep'; version: string }
|
|
80
|
+
/** A new value; the controller mints its version. */
|
|
81
|
+
| { kind: 'set'; value: string };
|
|
82
|
+
|
|
83
|
+
/** A verifier member in a row's write form; the secret it verifies never travels. */
|
|
84
|
+
export type TGatewayVerifierWrite<TVerifier> =
|
|
85
|
+
/**
|
|
86
|
+
* Keep the verifier the stored row holds at this path: the form a read answers. A row that holds
|
|
87
|
+
* none there (a new row, a member stored `null`) is refused `kept-secret-unknown`.
|
|
88
|
+
*/
|
|
89
|
+
| { kind: 'keep' }
|
|
90
|
+
/** A verifier the operator derived (`deriveGatewayScramVerifier`, a token's SHA-256). */
|
|
91
|
+
| { kind: 'set'; verifier: TVerifier };
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------------------------
|
|
94
|
+
// Write forms
|
|
95
|
+
// ---------------------------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
/** `IGatewayApiTokenRow` with its verifier as a keep-or-set choice. */
|
|
98
|
+
export interface IGatewayApiTokenRowWrite extends Omit<IGatewayApiTokenRow, 'credential'> {
|
|
99
|
+
credential: TGatewayVerifierWrite<IGatewayTokenHash>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** `IGatewayDnsProviderRow` with its API token as a keep-or-set choice. */
|
|
103
|
+
export interface IGatewayDnsProviderRowWrite extends Omit<IGatewayDnsProviderRow, 'credentials'> {
|
|
104
|
+
credentials: { type: 'cloudflare'; apiToken: TGatewaySealedValueWrite };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** `IGatewayRouteSecrets` with every set secret as a keep-or-set choice. */
|
|
108
|
+
export interface IGatewayRouteSecretsWrite {
|
|
109
|
+
tlsKey: TGatewaySealedValueWrite | null;
|
|
110
|
+
basicAuthUsers: TGatewaySealedValueWrite | null;
|
|
111
|
+
jwtSecret: TGatewaySealedValueWrite | null;
|
|
112
|
+
requestHeaders: TGatewaySealedValueWrite | null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `IGatewayRouteRow` with its secrets as keep-or-set choices. */
|
|
116
|
+
export interface IGatewayRouteRowWrite extends Omit<IGatewayRouteRow, 'secrets'> {
|
|
117
|
+
secrets: IGatewayRouteSecretsWrite;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** `IGatewaySourceProfileSecrets` with every set secret as a keep-or-set choice. */
|
|
121
|
+
export interface IGatewaySourceProfileSecretsWrite {
|
|
122
|
+
basicAuthUsers: TGatewaySealedValueWrite | null;
|
|
123
|
+
jwtSecret: TGatewaySealedValueWrite | null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** `IGatewaySourceProfileRow` with its secrets as keep-or-set choices. */
|
|
127
|
+
export interface IGatewaySourceProfileRowWrite extends Omit<IGatewaySourceProfileRow, 'secrets'> {
|
|
128
|
+
secrets: IGatewaySourceProfileSecretsWrite;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** `IGatewayMailServerSettings` with its TLS key as a keep-or-set choice. */
|
|
132
|
+
export interface IGatewayMailServerSettingsWrite extends Omit<IGatewayMailServerSettings, 'tlsKey'> {
|
|
133
|
+
tlsKey: TGatewaySealedValueWrite | null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** `IGatewaySmtpAccountRow` with its verifier as a keep-or-set choice. */
|
|
137
|
+
export interface IGatewaySmtpAccountRowWrite extends Omit<IGatewaySmtpAccountRow, 'credential'> {
|
|
138
|
+
credential: TGatewayVerifierWrite<IGatewayScramVerifier>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** `IGatewayMailIdentityRow` with its verifier as a keep-or-set choice. */
|
|
142
|
+
export interface IGatewayMailIdentityRowWrite extends Omit<IGatewayMailIdentityRow, 'credential'> {
|
|
143
|
+
credential: TGatewayVerifierWrite<IGatewayScramVerifier>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** `IGatewayCoreMailPeerRow` with its verifier list as one keep-or-set choice. */
|
|
147
|
+
export interface IGatewayCoreMailPeerRowWrite extends Omit<IGatewayCoreMailPeerRow, 'credentials'> {
|
|
148
|
+
credentials: TGatewayVerifierWrite<IGatewayCoreMailCredentialVerifier[]>;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** `IGatewayEdgeRow` with its tunnel secret as a keep-or-set choice. */
|
|
152
|
+
export interface IGatewayEdgeRowWrite extends Omit<IGatewayEdgeRow, 'secret'> {
|
|
153
|
+
secret: TGatewaySealedValueWrite;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The write form of one row of every member, by area and member: a list member's row, or a
|
|
158
|
+
* singleton's value. A member without secrets writes its table type unchanged.
|
|
159
|
+
*/
|
|
160
|
+
export interface IGatewayAreaRowWrites {
|
|
161
|
+
security: {
|
|
162
|
+
blockRules: IGatewayBlockRuleRow;
|
|
163
|
+
apiTokens: IGatewayApiTokenRowWrite;
|
|
164
|
+
proxyPolicy: TGatewayEngineConfig | null;
|
|
165
|
+
};
|
|
166
|
+
dns: {
|
|
167
|
+
server: IGatewayDnsServerSettings | null;
|
|
168
|
+
nameservers: IGatewayAreaKeyRow;
|
|
169
|
+
acme: IGatewayAcmeSettings | null;
|
|
170
|
+
providers: IGatewayDnsProviderRowWrite;
|
|
171
|
+
domains: IGatewayDnsDomainRow;
|
|
172
|
+
records: IGatewayDnsRecordRow;
|
|
173
|
+
staticRecords: IGatewayDnsStaticRecordRow;
|
|
174
|
+
protectedHostnames: IGatewayDnsProtectedHostnameRow;
|
|
175
|
+
authorityZones: IGatewayAreaKeyRow;
|
|
176
|
+
clientHostnames: IGatewayDnsClientHostnameRow;
|
|
177
|
+
};
|
|
178
|
+
routes: {
|
|
179
|
+
routes: IGatewayRouteRowWrite;
|
|
180
|
+
sourceProfiles: IGatewaySourceProfileRowWrite;
|
|
181
|
+
networkTargets: IGatewayNetworkTargetRow;
|
|
182
|
+
targetProfiles: IGatewayTargetProfileRow;
|
|
183
|
+
disabledSystemRoutes: IGatewayAreaKeyRow;
|
|
184
|
+
http3: IGatewayHttp3Settings | null;
|
|
185
|
+
};
|
|
186
|
+
mail: {
|
|
187
|
+
server: IGatewayMailServerSettingsWrite;
|
|
188
|
+
domains: IGatewayMailDomainRow;
|
|
189
|
+
smtpAccounts: IGatewaySmtpAccountRowWrite;
|
|
190
|
+
identities: IGatewayMailIdentityRowWrite;
|
|
191
|
+
coreMailPeers: IGatewayCoreMailPeerRowWrite;
|
|
192
|
+
};
|
|
193
|
+
edges: {
|
|
194
|
+
hub: IGatewayEdgeHubSettings;
|
|
195
|
+
edges: IGatewayEdgeRowWrite;
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The member names of an area (those of `IGatewayAreaContents`, which `test.gatewayarearows` pins). */
|
|
200
|
+
export type TGatewayAreaMember<TArea extends TGatewayConfigArea = TGatewayConfigArea> =
|
|
201
|
+
TArea extends TGatewayConfigArea ? keyof IGatewayAreaRowWrites[TArea] & string : never;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The members of each area that hold one value rather than rows (an object or `null`, never a list),
|
|
205
|
+
* addressed with `id: null`: the singletons of `gatewayAreaMembers`.
|
|
206
|
+
*/
|
|
207
|
+
export const gatewayAreaSingletonMembers = Object.freeze({
|
|
208
|
+
security: Object.freeze(['proxyPolicy'] as const),
|
|
209
|
+
dns: Object.freeze(['server', 'acme'] as const),
|
|
210
|
+
routes: Object.freeze(['http3'] as const),
|
|
211
|
+
mail: Object.freeze(['server'] as const),
|
|
212
|
+
edges: Object.freeze(['hub'] as const),
|
|
213
|
+
} as const);
|
|
214
|
+
|
|
215
|
+
/** A singleton member of an area. */
|
|
216
|
+
export type TGatewayAreaSingletonMember<TArea extends TGatewayConfigArea = TGatewayConfigArea> =
|
|
217
|
+
TArea extends TGatewayConfigArea ? typeof gatewayAreaSingletonMembers[TArea][number] : never;
|
|
218
|
+
|
|
219
|
+
/** A list member of an area: its rows carry an `id`. */
|
|
220
|
+
export type TGatewayAreaListMember<TArea extends TGatewayConfigArea = TGatewayConfigArea> =
|
|
221
|
+
TArea extends TGatewayConfigArea ? Exclude<TGatewayAreaMember<TArea>, TGatewayAreaSingletonMember<TArea>> : never;
|
|
222
|
+
|
|
223
|
+
/** Whether `memberArg` is a member of the area (`gatewayAreaMembers`). */
|
|
224
|
+
export const isGatewayAreaMember = <TArea extends TGatewayConfigArea>(
|
|
225
|
+
areaArg: TArea, memberArg: unknown,
|
|
226
|
+
): memberArg is TGatewayAreaMember<TArea> => (
|
|
227
|
+
typeof memberArg === 'string' && (gatewayAreaMembers[areaArg] as readonly string[]).includes(memberArg)
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
/** Whether a member of the area is a singleton, addressed with `id: null`. */
|
|
231
|
+
export const isGatewayAreaSingletonMember = (areaArg: TGatewayConfigArea, memberArg: string): boolean => (
|
|
232
|
+
(gatewayAreaSingletonMembers[areaArg] as readonly string[]).includes(memberArg)
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
// ---------------------------------------------------------------------------------------------
|
|
236
|
+
// Refusals
|
|
237
|
+
// ---------------------------------------------------------------------------------------------
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The gateway intent refusals a row read answers in its report: the controller records no such
|
|
241
|
+
* gateway, or did not take it over. A caller who is not a fresh human administrator is refused
|
|
242
|
+
* before this (a `TypedResponseError` naming the requirement), and an area or member the contract
|
|
243
|
+
* does not name is the thrown connector refusal `request-invalid`; neither is a report.
|
|
244
|
+
*/
|
|
245
|
+
export const gatewayAreaRowsRefusals = Object.freeze([
|
|
246
|
+
'gateway-not-found', 'gateway-not-taken-over',
|
|
247
|
+
] as const satisfies readonly TGatewayIntentRefusal[]);
|
|
248
|
+
|
|
249
|
+
/** A refusal a row read answers. */
|
|
250
|
+
export type TGatewayAreaRowsRefusal = typeof gatewayAreaRowsRefusals[number];
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The gateway intent refusals a row write answers in its report, in the order the controller judges
|
|
254
|
+
* them: the gateway (`gateway-not-found`, `gateway-not-taken-over`); `row-compile-owned` (the stored
|
|
255
|
+
* row is a claim of the controller's records, or the put row names the controller's own gateway
|
|
256
|
+
* client, whose rows only the compile writes); then a write whose outcome the table already states
|
|
257
|
+
* answers `ok` with `revision: null`; `intent-row-stale` (the row is not the one `expectedDigest`
|
|
258
|
+
* names); `kept-secret-unknown`; `area-table-invalid` (the resolved table, the whole document or the
|
|
259
|
+
* change rule `validateGatewayAreaContentChange`); `controller-origin-unserved` and
|
|
260
|
+
* `lifeline-unjudged` (a routes write); `gateway-vocabulary-newer`; `intent-revision-stale` (the
|
|
261
|
+
* tables kept moving through the controller's attempts). A caller who is not a fresh human
|
|
262
|
+
* administrator and a write `validateGatewayAreaRowWrite` refuses are refused before any of them,
|
|
263
|
+
* and neither is a report.
|
|
264
|
+
*/
|
|
265
|
+
export const gatewayAreaRowWriteRefusals = Object.freeze([
|
|
266
|
+
'gateway-not-found', 'gateway-not-taken-over', 'row-compile-owned', 'intent-row-stale', 'kept-secret-unknown',
|
|
267
|
+
'area-table-invalid', 'controller-origin-unserved', 'lifeline-unjudged', 'gateway-vocabulary-newer',
|
|
268
|
+
'intent-revision-stale',
|
|
269
|
+
] as const satisfies readonly TGatewayIntentRefusal[]);
|
|
270
|
+
|
|
271
|
+
/** A refusal a row write answers. */
|
|
272
|
+
export type TGatewayAreaRowWriteRefusal = typeof gatewayAreaRowWriteRefusals[number];
|
|
273
|
+
|
|
274
|
+
// ---------------------------------------------------------------------------------------------
|
|
275
|
+
// Row token
|
|
276
|
+
// ---------------------------------------------------------------------------------------------
|
|
277
|
+
|
|
278
|
+
/** The constants of the row read and write. */
|
|
279
|
+
export const gatewayAreaRowContract = Object.freeze({
|
|
280
|
+
/** The digest domain of `computeGatewayAreaRowDigest`. */
|
|
281
|
+
rowDigestDomain: 'serve.zone/gateway-area-row',
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const splitPath = (pathArg: string): string[] => pathArg.split('.');
|
|
285
|
+
|
|
286
|
+
const secretPathsOf = (areaArg: TGatewayConfigArea, memberArg: string): readonly IGatewayAreaSecretPath[] => {
|
|
287
|
+
const members = gatewayAreaSecretPaths[areaArg] as Readonly<Record<string, readonly IGatewayAreaSecretPath[] | undefined>>;
|
|
288
|
+
return Object.hasOwn(members, memberArg) ? members[memberArg] ?? [] : [];
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
/** An inert deep copy of strict JSON (`{ value }`), or `null` for a value that is not strict JSON. */
|
|
292
|
+
const copyStrictJson = (valueArg: unknown): { value: unknown } | null => {
|
|
293
|
+
try {
|
|
294
|
+
return {
|
|
295
|
+
value: JSON.parse(canonicalizeStrictJson(valueArg, (reasonArg) => {
|
|
296
|
+
throw new Error(reasonArg);
|
|
297
|
+
}, 'gateway area row')),
|
|
298
|
+
};
|
|
299
|
+
} catch {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/** The record holding the last key of `keysArg` inside a copied row, and that key; `null` when a parent or the key is missing. */
|
|
305
|
+
const parentOf = (rowArg: unknown, keysArg: readonly string[]): { parent: Record<string, unknown>; key: string } | null => {
|
|
306
|
+
let current: unknown = rowArg;
|
|
307
|
+
for (const key of keysArg.slice(0, -1)) {
|
|
308
|
+
if (!isPlainRecord(current) || !Object.hasOwn(current, key)) return null;
|
|
309
|
+
current = current[key];
|
|
310
|
+
}
|
|
311
|
+
const key = keysArg[keysArg.length - 1];
|
|
312
|
+
return isPlainRecord(current) && Object.hasOwn(current, key) ? { parent: current, key } : null;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const failRowDigest = (reasonArg: string): never => {
|
|
316
|
+
throw new Error(`Gateway area row digest input rejected: ${reasonArg}.`);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* The canonical JSON a row's compare token hashes (Linus ruling 171 D2): the digest domain, the
|
|
321
|
+
* row's address and the stored row (or singleton value) with every sealed member as its version
|
|
322
|
+
* only (`{ version }`), never its value; a verifier enters as stored and a `null` member as `null`.
|
|
323
|
+
* A version names exactly one value (a writer mints a new one for every new value), so the token
|
|
324
|
+
* moves with every change of the row, a secret's included, while a reader who holds every other
|
|
325
|
+
* input (the read answers the row and the versions) cannot test a guess at a sealed value against
|
|
326
|
+
* it. Throws, naming no value, for an unknown area or member, an `id` that is not `null` exactly
|
|
327
|
+
* for a singleton, and a row that is not strict JSON or lacks a sealed member or its version, which
|
|
328
|
+
* a valid table never holds.
|
|
329
|
+
*/
|
|
330
|
+
export const createGatewayAreaRowDigestInput = (inputArg: {
|
|
331
|
+
area: TGatewayConfigArea;
|
|
332
|
+
member: string;
|
|
333
|
+
id: string | null;
|
|
334
|
+
row: unknown;
|
|
335
|
+
}): string => {
|
|
336
|
+
const { area, member, id } = inputArg;
|
|
337
|
+
if (!isGatewayConfigArea(area) || !isGatewayAreaMember(area, member)) failRowDigest('unknown area or member');
|
|
338
|
+
if ((id === null) !== isGatewayAreaSingletonMember(area, member)) failRowDigest(`${area} ${member} takes ${id === null ? 'a row id' : 'id null'}`);
|
|
339
|
+
const row = (copyStrictJson(inputArg.row) ?? failRowDigest(`the ${area} ${member} row is not strict JSON`)).value;
|
|
340
|
+
for (const secret of secretPathsOf(area, member)) {
|
|
341
|
+
if (secret.secretClass !== 'sealed') continue;
|
|
342
|
+
const place = parentOf(row, splitPath(secret.path)) ?? failRowDigest(`the ${area} ${member} row has no member ${secret.path}`);
|
|
343
|
+
const value = place.parent[place.key];
|
|
344
|
+
if (value === null) continue;
|
|
345
|
+
const version = isPlainRecord(value) ? value.version : undefined;
|
|
346
|
+
if (typeof version !== 'string') failRowDigest(`the ${area} ${member} row's ${secret.path} has no version`);
|
|
347
|
+
place.parent[place.key] = { version };
|
|
348
|
+
}
|
|
349
|
+
return canonicalizeStrictJson({ domain: gatewayAreaRowContract.rowDigestDomain, area, member, id, row }, failRowDigest,
|
|
350
|
+
'gateway area row');
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* A row's compare token: `sha256:<hex>` of `createGatewayAreaRowDigestInput`, through Web Crypto.
|
|
355
|
+
* The controller answers it for every row a read answers and for the row a write leaves, and judges
|
|
356
|
+
* a write's `expectedDigest` against it. A reader compares it for equality only.
|
|
357
|
+
*/
|
|
358
|
+
export const computeGatewayAreaRowDigest = async (inputArg: {
|
|
359
|
+
area: TGatewayConfigArea;
|
|
360
|
+
member: string;
|
|
361
|
+
id: string | null;
|
|
362
|
+
row: unknown;
|
|
363
|
+
}): Promise<TSha256Digest> => `sha256:${await createCanonicalJsonSha256Hex(
|
|
364
|
+
createGatewayAreaRowDigestInput(inputArg), failRowDigest,
|
|
365
|
+
)}`;
|
|
366
|
+
|
|
367
|
+
// ---------------------------------------------------------------------------------------------
|
|
368
|
+
// Read
|
|
369
|
+
// ---------------------------------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Who a row is the author of: `compile` for a row the controller's own records compile (its claim;
|
|
373
|
+
* an operator write refuses it `row-compile-owned`), `operator` for every other row.
|
|
374
|
+
*/
|
|
375
|
+
export type TGatewayAreaRowOwner = 'operator' | 'compile';
|
|
376
|
+
|
|
377
|
+
/** One row of a table as an administrator reads it: its address, compare token, author and write form. */
|
|
378
|
+
export type TGatewayAreaRowRead<TArea extends TGatewayConfigArea = TGatewayConfigArea> =
|
|
379
|
+
TArea extends TGatewayConfigArea ? {
|
|
380
|
+
[TMember in TGatewayAreaMember<TArea>]: {
|
|
381
|
+
member: TMember;
|
|
382
|
+
/** The row's `id`; `null` for a singleton. */
|
|
383
|
+
id: TMember extends TGatewayAreaSingletonMember<TArea> ? null : string;
|
|
384
|
+
/**
|
|
385
|
+
* The compare token a write of this row names (`expectedDigest`): `computeGatewayAreaRowDigest`
|
|
386
|
+
* of the stored row, which takes every sealed member by its version only. A reader compares it
|
|
387
|
+
* for equality only.
|
|
388
|
+
*/
|
|
389
|
+
digest: TSha256Digest;
|
|
390
|
+
owner: TGatewayAreaRowOwner;
|
|
391
|
+
/** The write form, every secret as `keep`: a sealed member with its version, never its value. */
|
|
392
|
+
row: IGatewayAreaRowWrites[TArea][TMember];
|
|
393
|
+
};
|
|
394
|
+
}[TGatewayAreaMember<TArea>] : never;
|
|
395
|
+
|
|
396
|
+
/** One area table of a taken-over gateway as an administrator reads it: revision, digest and rows. */
|
|
397
|
+
export interface IGatewayAreaRowsRead {
|
|
398
|
+
gatewayId: string;
|
|
399
|
+
area: TGatewayConfigArea;
|
|
400
|
+
/** The controller revision that wrote the table last. */
|
|
401
|
+
revision: number;
|
|
402
|
+
/** `computeGatewayAreaTableDigest` of the table: what the gateway reports applied once it applied it. */
|
|
403
|
+
tableDigest: TSha256Digest;
|
|
404
|
+
/**
|
|
405
|
+
* The rows of the member asked for (every member when none was): members ascending by
|
|
406
|
+
* `compareWireTokens`, each member's rows in the table's order (ascending by id).
|
|
407
|
+
*/
|
|
408
|
+
rows: TGatewayAreaRowRead[];
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** A table read's answer: the table, or why there is none. No secret value. */
|
|
412
|
+
export type TGatewayAreaRowsReport =
|
|
413
|
+
| { ok: true; table: IGatewayAreaRowsRead }
|
|
414
|
+
| { ok: false; code: TGatewayAreaRowsRefusal };
|
|
415
|
+
|
|
416
|
+
// ---------------------------------------------------------------------------------------------
|
|
417
|
+
// Write
|
|
418
|
+
// ---------------------------------------------------------------------------------------------
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Put one row: create or replace a list row (its `row.id` is `id`), or replace a singleton's value
|
|
422
|
+
* (`id: null`; `row: null` for a nullable singleton). `expectedDigest` is the row's `digest` as the
|
|
423
|
+
* administrator read it, or `null` for a list row that must be absent.
|
|
424
|
+
*/
|
|
425
|
+
export type TGatewayAreaRowPut = {
|
|
426
|
+
[TArea in TGatewayConfigArea]: {
|
|
427
|
+
[TMember in TGatewayAreaMember<TArea>]: {
|
|
428
|
+
operation: 'put';
|
|
429
|
+
area: TArea;
|
|
430
|
+
member: TMember;
|
|
431
|
+
id: TMember extends TGatewayAreaSingletonMember<TArea> ? null : string;
|
|
432
|
+
expectedDigest: TSha256Digest | null;
|
|
433
|
+
row: IGatewayAreaRowWrites[TArea][TMember];
|
|
434
|
+
};
|
|
435
|
+
}[TGatewayAreaMember<TArea>];
|
|
436
|
+
}[TGatewayConfigArea];
|
|
437
|
+
|
|
438
|
+
/** Delete one list row; `expectedDigest` is its `digest` as the administrator read it. A singleton is never deleted. */
|
|
439
|
+
export type TGatewayAreaRowDelete = {
|
|
440
|
+
[TArea in TGatewayConfigArea]: {
|
|
441
|
+
operation: 'delete';
|
|
442
|
+
area: TArea;
|
|
443
|
+
member: TGatewayAreaListMember<TArea>;
|
|
444
|
+
id: string;
|
|
445
|
+
expectedDigest: TSha256Digest;
|
|
446
|
+
};
|
|
447
|
+
}[TGatewayConfigArea];
|
|
448
|
+
|
|
449
|
+
/** One row-level compare-and-swap write. */
|
|
450
|
+
export type TGatewayAreaRowWrite = TGatewayAreaRowPut | TGatewayAreaRowDelete;
|
|
451
|
+
|
|
452
|
+
/** A sealed version a put minted, by the secret's path inside the row. */
|
|
453
|
+
export interface IGatewayAreaRowMintedVersion {
|
|
454
|
+
path: string;
|
|
455
|
+
version: string;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** What a row write changed. */
|
|
459
|
+
export interface IGatewayAreaRowWritten {
|
|
460
|
+
gatewayId: string;
|
|
461
|
+
area: TGatewayConfigArea;
|
|
462
|
+
member: string;
|
|
463
|
+
id: string | null;
|
|
464
|
+
operation: 'put' | 'delete';
|
|
465
|
+
/**
|
|
466
|
+
* The controller revision the write appended; `null` when the table already stated what the
|
|
467
|
+
* write asked (the put row equals the stored one, or the deleted row is absent) and nothing was
|
|
468
|
+
* written.
|
|
469
|
+
*/
|
|
470
|
+
revision: number | null;
|
|
471
|
+
/** The row's compare token now; `null` after a delete. */
|
|
472
|
+
digest: TSha256Digest | null;
|
|
473
|
+
/** The table's digest now: what the gateway reports applied once it applied the write. */
|
|
474
|
+
tableDigest: TSha256Digest;
|
|
475
|
+
/** Every sealed version this put minted, ascending by path; never a value. */
|
|
476
|
+
mintedVersions: IGatewayAreaRowMintedVersion[];
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* A row write's answer: what it changed, or the refusal that left every table as it was, with the
|
|
481
|
+
* validator lines that name why (area, member, row id, path and code; never a value).
|
|
482
|
+
*/
|
|
483
|
+
export type TGatewayAreaRowWriteReport =
|
|
484
|
+
| { ok: true; written: IGatewayAreaRowWritten }
|
|
485
|
+
| { ok: false; code: TGatewayAreaRowWriteRefusal; problems: string[] };
|
|
486
|
+
|
|
487
|
+
/** One secret member of a write form as written: `null`, a keep or a set; `null` itself for a malformed form. */
|
|
488
|
+
type TSecretWriteForm =
|
|
489
|
+
| { form: 'null' }
|
|
490
|
+
| { form: 'keep'; version: string | null }
|
|
491
|
+
| { form: 'set'; value: unknown };
|
|
492
|
+
|
|
493
|
+
const readSecretWriteForm = (valueArg: unknown, secretClassArg: TGatewayAreaSecretClass): TSecretWriteForm | null => {
|
|
494
|
+
if (valueArg === null) return { form: 'null' };
|
|
495
|
+
if (secretClassArg === 'verifier') {
|
|
496
|
+
if (readExactDataRecord(valueArg, ['kind'])?.kind === 'keep') return { form: 'keep', version: null };
|
|
497
|
+
const set = readExactDataRecord(valueArg, ['kind', 'verifier']);
|
|
498
|
+
return set?.kind === 'set' ? { form: 'set', value: set.verifier } : null;
|
|
499
|
+
}
|
|
500
|
+
const keep = readExactDataRecord(valueArg, ['kind', 'version']);
|
|
501
|
+
if (keep?.kind === 'keep' && typeof keep.version === 'string') return { form: 'keep', version: keep.version };
|
|
502
|
+
const set = readExactDataRecord(valueArg, ['kind', 'value']);
|
|
503
|
+
return set?.kind === 'set' && typeof set.value === 'string' ? { form: 'set', value: set.value } : null;
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
const rowWriteKeys = Object.freeze({
|
|
507
|
+
put: Object.freeze(['operation', 'area', 'member', 'id', 'expectedDigest', 'row'] as const),
|
|
508
|
+
delete: Object.freeze(['operation', 'area', 'member', 'id', 'expectedDigest'] as const),
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* The one check of a row write's envelope (Linus ruling 171 section 4), which the controller runs
|
|
513
|
+
* before it reads anything and a client runs before it signs in: exact keys per operation, a
|
|
514
|
+
* configuration area and one of its members, `id` `null` exactly for a singleton (which is never
|
|
515
|
+
* deleted) and a row id (`isGatewayAreaRowId`) for a list member, a `sha256:` `expectedDigest`
|
|
516
|
+
* (`null` only for a put), and for a put a strict JSON row whose `id` is the address's (a list
|
|
517
|
+
* member) and whose every secret member (`gatewayAreaSecretPaths`) is `null` or one of its
|
|
518
|
+
* keep-or-set forms. The controller answers any problem as the thrown connector refusal
|
|
519
|
+
* `request-invalid`. The row's other members are the area validator's, judged with its table once
|
|
520
|
+
* the write is resolved. Problems name keys and paths, never a value.
|
|
521
|
+
*/
|
|
522
|
+
export const validateGatewayAreaRowWrite = (valueArg: unknown, pathArg = 'write'): string[] => {
|
|
523
|
+
const operation = readDataProperty(valueArg, 'operation')?.value;
|
|
524
|
+
if (operation !== 'put' && operation !== 'delete') return [`${pathArg} must be an object whose operation is put or delete`];
|
|
525
|
+
const keys = rowWriteKeys[operation];
|
|
526
|
+
const record = readExactDataRecord(valueArg, keys);
|
|
527
|
+
if (record === null) return [`${pathArg} must be exactly { ${keys.join(', ')} } for a ${operation}`];
|
|
528
|
+
const area = record.area;
|
|
529
|
+
if (!isGatewayConfigArea(area)) return [`${pathArg} area must be a gateway configuration area`];
|
|
530
|
+
const member = record.member;
|
|
531
|
+
if (!isGatewayAreaMember(area, member)) return [`${pathArg} member must be a member of the ${area} area`];
|
|
532
|
+
const problems: string[] = [];
|
|
533
|
+
const singleton = isGatewayAreaSingletonMember(area, member);
|
|
534
|
+
if (singleton) {
|
|
535
|
+
if (record.id !== null) problems.push(`${pathArg} id must be null for the singleton ${area} ${member}`);
|
|
536
|
+
if (operation === 'delete') problems.push(`${pathArg} deletes the singleton ${area} ${member}; a singleton is put, never deleted`);
|
|
537
|
+
} else if (!isGatewayAreaRowId(record.id)) {
|
|
538
|
+
problems.push(`${pathArg} id must be a ${area} ${member} row id`);
|
|
539
|
+
}
|
|
540
|
+
const expected = record.expectedDigest;
|
|
541
|
+
if (!(expected === null && operation === 'put') && !(typeof expected === 'string' && isSha256Digest(expected))) {
|
|
542
|
+
problems.push(`${pathArg} expectedDigest must be a sha256: digest${operation === 'put' ? ' or null' : ''}`);
|
|
543
|
+
}
|
|
544
|
+
if (operation === 'delete') return problems;
|
|
545
|
+
const copied = copyStrictJson(record.row);
|
|
546
|
+
if (copied === null) return [...problems, `${pathArg} row must be strict JSON`];
|
|
547
|
+
const row = copied.value;
|
|
548
|
+
if (!singleton && !(isPlainRecord(row) && row.id === record.id)) problems.push(`${pathArg} row must be a row whose id is ${pathArg} id`);
|
|
549
|
+
for (const secret of secretPathsOf(area, member)) {
|
|
550
|
+
const place = parentOf(row, splitPath(secret.path));
|
|
551
|
+
if (place === null || readSecretWriteForm(place.parent[place.key], secret.secretClass) === null) {
|
|
552
|
+
problems.push(`${pathArg} row ${secret.path} must be null or a ${secret.secretClass} keep-or-set form`);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return problems;
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
// ---------------------------------------------------------------------------------------------
|
|
559
|
+
// Projection and resolution
|
|
560
|
+
// ---------------------------------------------------------------------------------------------
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* A stored row (or singleton value) of a valid table in its write form, every secret as `keep`: a
|
|
564
|
+
* sealed member as its version, a verifier as `{ kind: 'keep' }`, a `null` member as `null`. Throws
|
|
565
|
+
* for a row that is not strict JSON or lacks a secret member's parent, which a valid table never
|
|
566
|
+
* holds.
|
|
567
|
+
*/
|
|
568
|
+
export const projectGatewayAreaRow = <TArea extends TGatewayConfigArea, TMember extends TGatewayAreaMember<TArea>>(
|
|
569
|
+
areaArg: TArea, memberArg: TMember, storedArg: unknown,
|
|
570
|
+
): IGatewayAreaRowWrites[TArea][TMember] => {
|
|
571
|
+
const copied = copyStrictJson(storedArg);
|
|
572
|
+
if (copied === null) throw new Error(`The stored ${areaArg} ${memberArg} row is not strict JSON.`);
|
|
573
|
+
const row = copied.value;
|
|
574
|
+
for (const secret of secretPathsOf(areaArg, memberArg)) {
|
|
575
|
+
const place = parentOf(row, splitPath(secret.path));
|
|
576
|
+
if (place === null) throw new Error(`The stored ${areaArg} ${memberArg} row has no member ${secret.path}.`);
|
|
577
|
+
const value = place.parent[place.key];
|
|
578
|
+
if (value === null) continue;
|
|
579
|
+
if (secret.secretClass === 'verifier') {
|
|
580
|
+
place.parent[place.key] = { kind: 'keep' };
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
const version = isPlainRecord(value) ? value.version : undefined;
|
|
584
|
+
if (typeof version !== 'string') throw new Error(`The stored ${areaArg} ${memberArg} row's ${secret.path} has no version.`);
|
|
585
|
+
place.parent[place.key] = { kind: 'keep', version };
|
|
586
|
+
}
|
|
587
|
+
return row as IGatewayAreaRowWrites[TArea][TMember];
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Why a row's write form cannot be resolved, by the secret's path: `invalid-write-form` for a
|
|
592
|
+
* secret member that is not one of its keep-or-set forms (a write `validateGatewayAreaRowWrite`
|
|
593
|
+
* refuses), or `kept-secret-unknown` for a `keep` the stored row does not hold there. Never a value.
|
|
594
|
+
*/
|
|
595
|
+
export interface IGatewayAreaRowWriteFormRefusal {
|
|
596
|
+
/** The secret's dotted path inside the row; `''` for the row as a whole. */
|
|
597
|
+
path: string;
|
|
598
|
+
code: 'invalid-write-form' | 'kept-secret-unknown';
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** A resolved write: the row in its stored form, and every version minted for it. */
|
|
602
|
+
export type TGatewayAreaRowResolution =
|
|
603
|
+
| { ok: true; row: unknown; mintedVersions: IGatewayAreaRowMintedVersion[] }
|
|
604
|
+
| { ok: false; refusals: IGatewayAreaRowWriteFormRefusal[] };
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Resolves a row's write form against the row stored at the same address (`null` when there is
|
|
608
|
+
* none) into its stored form: a sealed `keep` becomes the stored `{ version, value }` at that path
|
|
609
|
+
* when the stored row holds that version there, a verifier `keep` the stored verifier when the row
|
|
610
|
+
* holds one; a sealed `set` becomes `{ version: mintVersionArg(), value }`, a verifier `set` the
|
|
611
|
+
* stated verifier; `null` stays `null`. Every other member is copied as written; the area validator
|
|
612
|
+
* judges the resolved row as part of its table. Judges the forms with the rule
|
|
613
|
+
* `validateGatewayAreaRowWrite` applies and never reads a value into a refusal. The minted
|
|
614
|
+
* versions are answered ascending by path. `mintVersionArg` is the writer's
|
|
615
|
+
* (`createGatewaySealedVersion`).
|
|
616
|
+
*/
|
|
617
|
+
export const resolveGatewayAreaRowWrite = (inputArg: {
|
|
618
|
+
area: TGatewayConfigArea;
|
|
619
|
+
member: string;
|
|
620
|
+
written: unknown;
|
|
621
|
+
stored: unknown | null;
|
|
622
|
+
mintVersion: () => string;
|
|
623
|
+
}): TGatewayAreaRowResolution => {
|
|
624
|
+
const copied = copyStrictJson(inputArg.written);
|
|
625
|
+
if (copied === null) return { ok: false, refusals: [{ path: '', code: 'invalid-write-form' }] };
|
|
626
|
+
const row = copied.value;
|
|
627
|
+
const refusals: IGatewayAreaRowWriteFormRefusal[] = [];
|
|
628
|
+
const minted: IGatewayAreaRowMintedVersion[] = [];
|
|
629
|
+
for (const secret of secretPathsOf(inputArg.area, inputArg.member)) {
|
|
630
|
+
const keys = splitPath(secret.path);
|
|
631
|
+
const place = parentOf(row, keys);
|
|
632
|
+
const form = place === null ? null : readSecretWriteForm(place.parent[place.key], secret.secretClass);
|
|
633
|
+
if (place === null || form === null) {
|
|
634
|
+
refusals.push({ path: secret.path, code: 'invalid-write-form' });
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
if (form.form === 'null') continue;
|
|
638
|
+
if (form.form === 'set') {
|
|
639
|
+
if (secret.secretClass === 'verifier') {
|
|
640
|
+
place.parent[place.key] = form.value;
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
const version = inputArg.mintVersion();
|
|
644
|
+
place.parent[place.key] = { version, value: form.value };
|
|
645
|
+
minted.push({ path: secret.path, version });
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const storedPlace = inputArg.stored === null ? null : parentOf(inputArg.stored, keys);
|
|
649
|
+
const storedValue = storedPlace === null ? null : storedPlace.parent[storedPlace.key] ?? null;
|
|
650
|
+
if (secret.secretClass === 'verifier') {
|
|
651
|
+
if (storedValue === null) refusals.push({ path: secret.path, code: 'kept-secret-unknown' });
|
|
652
|
+
else place.parent[place.key] = copyStrictJson(storedValue)?.value ?? null;
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
const stored = readExactDataRecord(storedValue, ['version', 'value']);
|
|
656
|
+
if (stored === null || stored.version !== form.version) refusals.push({ path: secret.path, code: 'kept-secret-unknown' });
|
|
657
|
+
else place.parent[place.key] = { version: stored.version, value: stored.value };
|
|
658
|
+
}
|
|
659
|
+
if (refusals.length > 0) return { ok: false, refusals };
|
|
660
|
+
minted.sort((leftArg, rightArg) => compareWireTokens(leftArg.path, rightArg.path));
|
|
661
|
+
return { ok: true, row, mintedVersions: minted };
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* A new sealed version: 22 base64url characters of 128 random bits (`IGatewaySealedValue.version`),
|
|
666
|
+
* drawn with Web Crypto. The writer that sets a secret mints one for every new value (Linus ruling
|
|
667
|
+
* 121 Q9); a version is never reused for another value.
|
|
668
|
+
*/
|
|
669
|
+
export const createGatewaySealedVersion = (): string => {
|
|
670
|
+
const crypto = globalThis.crypto;
|
|
671
|
+
if (!crypto?.getRandomValues) throw new Error('Web Crypto random values are unavailable in this runtime.');
|
|
672
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
673
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
674
|
+
let text = '';
|
|
675
|
+
let buffer = 0;
|
|
676
|
+
let bits = 0;
|
|
677
|
+
for (const byte of bytes) {
|
|
678
|
+
buffer = (buffer << 8) | byte;
|
|
679
|
+
bits += 8;
|
|
680
|
+
while (bits >= 6) {
|
|
681
|
+
bits -= 6;
|
|
682
|
+
text += alphabet[(buffer >> bits) & 63];
|
|
683
|
+
}
|
|
684
|
+
buffer &= (1 << bits) - 1;
|
|
685
|
+
}
|
|
686
|
+
if (bits > 0) text += alphabet[(buffer << (6 - bits)) & 63];
|
|
687
|
+
return text;
|
|
688
|
+
};
|