@grovemotorco/manifold 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +50 -2
- package/dist/api.d.ts +6515 -0
- package/dist/api.js +643 -0
- package/dist/client.d.ts +2603 -0
- package/dist/client.js +42 -0
- package/dist/domain-Dye3ZdC_.js +922 -0
- package/dist/filter-DQnhobby.js +240 -0
- package/dist/ids-Ced7gYBp.d.ts +1704 -0
- package/dist/index.d.ts +640 -0
- package/dist/index.js +1339 -0
- package/dist/listen.d.ts +530 -0
- package/dist/listen.js +566 -0
- package/package.json +49 -3
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import * as Context from "effect/Context";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Exit from "effect/Exit";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
import * as Schema from "effect/Schema";
|
|
6
|
+
import * as Base64 from "effect/encoding/Base64";
|
|
7
|
+
import * as Base64Url from "effect/encoding/Base64Url";
|
|
8
|
+
import * as Hex from "effect/encoding/Hex";
|
|
9
|
+
import * as Result from "effect/Result";
|
|
10
|
+
// src/core/crypto.ts
|
|
11
|
+
/**
|
|
12
|
+
* WebCrypto primitives: digests, HMAC, constant-time comparison, fingerprints,
|
|
13
|
+
* and the AES-256-GCM secret box used to store secrets at rest (§5.5).
|
|
14
|
+
*/
|
|
15
|
+
const textEncoder = new TextEncoder();
|
|
16
|
+
const textDecoder = new TextDecoder();
|
|
17
|
+
const utf8Encode = (text) => textEncoder.encode(text);
|
|
18
|
+
const utf8Decode = (bytes) => textDecoder.decode(bytes);
|
|
19
|
+
const toBase64 = (bytes) => Base64.encode(bytes);
|
|
20
|
+
const toBase64Url = (bytes) => Base64Url.encode(bytes);
|
|
21
|
+
const toHex = (bytes) => Hex.encode(bytes);
|
|
22
|
+
const fromBase64 = (text) => Result.getOrUndefined(Base64.decode(text));
|
|
23
|
+
const fromBase64Url = (text) => Result.getOrUndefined(Base64Url.decode(text));
|
|
24
|
+
const fromHex = (text) => text.length % 2 === 0 ? Result.getOrUndefined(Hex.decode(text.toLowerCase())) : void 0;
|
|
25
|
+
const concatBytes = (...parts) => {
|
|
26
|
+
let length = 0;
|
|
27
|
+
for (const part of parts) length += part.length;
|
|
28
|
+
const out = new Uint8Array(length);
|
|
29
|
+
let offset = 0;
|
|
30
|
+
for (const part of parts) {
|
|
31
|
+
out.set(part, offset);
|
|
32
|
+
offset += part.length;
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
};
|
|
36
|
+
/** Copies into a fresh ArrayBuffer-backed array, which WebCrypto's typings require. */
|
|
37
|
+
const toBufferSource = (bytes) => {
|
|
38
|
+
const out = new Uint8Array(new ArrayBuffer(bytes.length));
|
|
39
|
+
out.set(bytes);
|
|
40
|
+
return out;
|
|
41
|
+
};
|
|
42
|
+
const subtleHash = {
|
|
43
|
+
sha1: "SHA-1",
|
|
44
|
+
sha256: "SHA-256",
|
|
45
|
+
sha512: "SHA-512"
|
|
46
|
+
};
|
|
47
|
+
const randomBytes = (length) => crypto.getRandomValues(new Uint8Array(length));
|
|
48
|
+
const sha256 = async (data) => new Uint8Array(await crypto.subtle.digest("SHA-256", toBufferSource(data instanceof Uint8Array ? data : utf8Encode(data))));
|
|
49
|
+
const sha256Hex = async (data) => toHex(await sha256(data));
|
|
50
|
+
/** First 8 hex characters of the secret's SHA-256: safe to show, enough to tell secrets apart. */
|
|
51
|
+
const fingerprint = async (secret) => (await sha256Hex(secret)).slice(0, 8);
|
|
52
|
+
const importHmacKey = (algorithm, key) => crypto.subtle.importKey("raw", toBufferSource(key), {
|
|
53
|
+
name: "HMAC",
|
|
54
|
+
hash: subtleHash[algorithm]
|
|
55
|
+
}, false, ["sign"]);
|
|
56
|
+
const hmac = async (algorithm, key, data) => {
|
|
57
|
+
const cryptoKey = await importHmacKey(algorithm, key);
|
|
58
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", cryptoKey, toBufferSource(data)));
|
|
59
|
+
};
|
|
60
|
+
/** Compares two byte arrays in time that depends only on their lengths. */
|
|
61
|
+
const timingSafeEqual = (a, b) => {
|
|
62
|
+
const length = Math.max(a.length, b.length);
|
|
63
|
+
let diff = a.length ^ b.length;
|
|
64
|
+
for (let i = 0; i < length; i++) diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
65
|
+
return diff === 0;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Compares two secrets by their SHA-256, so neither the content nor the length
|
|
69
|
+
* of the expected value leaks through timing. Used for tokens (§7.1).
|
|
70
|
+
*/
|
|
71
|
+
const secretsEqual = async (provided, expected) => {
|
|
72
|
+
const [a, b] = await Promise.all([sha256(provided), sha256(expected)]);
|
|
73
|
+
return timingSafeEqual(a, b);
|
|
74
|
+
};
|
|
75
|
+
/** Raised for a malformed `MANIFOLD_ENCRYPTION_KEYS`. Never carries key material. */
|
|
76
|
+
var KeyringError = class extends Schema.TaggedError()("KeyringError", { message: Schema.String }) {};
|
|
77
|
+
/** Raised when a sealed secret cannot be opened. Never carries the ciphertext or plaintext. */
|
|
78
|
+
var SecretBoxError = class extends Schema.TaggedError()("SecretBoxError", {
|
|
79
|
+
reason: Schema.Literals([
|
|
80
|
+
"malformed",
|
|
81
|
+
"unknown_key",
|
|
82
|
+
"decrypt_failed"
|
|
83
|
+
]),
|
|
84
|
+
keyId: Schema.optionalKey(Schema.String)
|
|
85
|
+
}) {
|
|
86
|
+
get message() {
|
|
87
|
+
return `secret box: ${this.reason}${this.keyId === void 0 ? "" : ` (key ${this.keyId})`}`;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const KEY_ID = /^[A-Za-z0-9_-]{1,32}$/;
|
|
91
|
+
const SEALED_PREFIX = "v1";
|
|
92
|
+
/**
|
|
93
|
+
* Parses `"k2:<base64>,k1:<base64>"`. The first key is the current one. Each
|
|
94
|
+
* key must decode to exactly 32 bytes.
|
|
95
|
+
*/
|
|
96
|
+
const parseKeyring = async (spec) => {
|
|
97
|
+
const entries = spec.split(",").map((part) => part.trim()).filter((part) => part.length > 0);
|
|
98
|
+
if (entries.length === 0) throw new KeyringError({ message: "no encryption keys configured" });
|
|
99
|
+
const keys = /* @__PURE__ */ new Map();
|
|
100
|
+
let current;
|
|
101
|
+
for (const [index, entry] of entries.entries()) {
|
|
102
|
+
const separator = entry.indexOf(":");
|
|
103
|
+
const id = separator === -1 ? "" : entry.slice(0, separator);
|
|
104
|
+
if (!KEY_ID.test(id)) throw new KeyringError({ message: `encryption key #${index + 1}: expected "<id>:<base64>" with a [A-Za-z0-9_-] id` });
|
|
105
|
+
if (keys.has(id)) throw new KeyringError({ message: `encryption key id "${id}" is listed twice` });
|
|
106
|
+
const raw = fromBase64(entry.slice(separator + 1).trim());
|
|
107
|
+
if (raw === void 0 || raw.length !== 32) throw new KeyringError({ message: `encryption key "${id}" must be 32 bytes of base64` });
|
|
108
|
+
const entryKey = {
|
|
109
|
+
id,
|
|
110
|
+
key: await crypto.subtle.importKey("raw", toBufferSource(raw), { name: "AES-GCM" }, false, ["encrypt", "decrypt"])
|
|
111
|
+
};
|
|
112
|
+
keys.set(id, entryKey);
|
|
113
|
+
current ??= entryKey;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
current,
|
|
117
|
+
keys
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
/** The associated data bound into each ciphertext: `"{kind}:{id}:{field}"`. */
|
|
121
|
+
const secretAad = (kind, id, field) => `${kind}:${id}:${field}`;
|
|
122
|
+
/** Seals `plaintext` as `v1.<keyId>.<nonce b64url>.<ciphertext b64url>`. */
|
|
123
|
+
const sealSecret = async (keyring, plaintext, aad) => {
|
|
124
|
+
const nonce = randomBytes(12);
|
|
125
|
+
const ciphertext = await crypto.subtle.encrypt({
|
|
126
|
+
name: "AES-GCM",
|
|
127
|
+
iv: toBufferSource(nonce),
|
|
128
|
+
additionalData: toBufferSource(utf8Encode(aad))
|
|
129
|
+
}, keyring.current.key, toBufferSource(utf8Encode(plaintext)));
|
|
130
|
+
return [
|
|
131
|
+
SEALED_PREFIX,
|
|
132
|
+
keyring.current.id,
|
|
133
|
+
toBase64Url(nonce),
|
|
134
|
+
toBase64Url(new Uint8Array(ciphertext))
|
|
135
|
+
].join(".");
|
|
136
|
+
};
|
|
137
|
+
/** The key id a sealed secret was encrypted with, or `undefined` if malformed. */
|
|
138
|
+
const sealedKeyId = (sealed) => {
|
|
139
|
+
const parts = sealed.split(".");
|
|
140
|
+
return parts.length === 4 && parts[0] === SEALED_PREFIX ? parts[1] : void 0;
|
|
141
|
+
};
|
|
142
|
+
/** True when the secret was sealed with a key other than the current one. */
|
|
143
|
+
const needsReseal = (keyring, sealed) => sealedKeyId(sealed) !== keyring.current.id;
|
|
144
|
+
const openSecret = async (keyring, sealed, aad) => {
|
|
145
|
+
const parts = sealed.split(".");
|
|
146
|
+
const [prefix, keyId, nonceText, ciphertextText] = parts;
|
|
147
|
+
if (parts.length !== 4 || prefix !== SEALED_PREFIX || keyId === void 0 || nonceText === void 0 || ciphertextText === void 0) throw new SecretBoxError({ reason: "malformed" });
|
|
148
|
+
const entry = keyring.keys.get(keyId);
|
|
149
|
+
if (entry === void 0) throw new SecretBoxError({
|
|
150
|
+
reason: "unknown_key",
|
|
151
|
+
keyId
|
|
152
|
+
});
|
|
153
|
+
const nonce = fromBase64Url(nonceText);
|
|
154
|
+
const ciphertext = fromBase64Url(ciphertextText);
|
|
155
|
+
if (nonce === void 0 || nonce.length !== 12 || ciphertext === void 0) throw new SecretBoxError({
|
|
156
|
+
reason: "malformed",
|
|
157
|
+
keyId
|
|
158
|
+
});
|
|
159
|
+
try {
|
|
160
|
+
const plaintext = await crypto.subtle.decrypt({
|
|
161
|
+
name: "AES-GCM",
|
|
162
|
+
iv: toBufferSource(nonce),
|
|
163
|
+
additionalData: toBufferSource(utf8Encode(aad))
|
|
164
|
+
}, entry.key, toBufferSource(ciphertext));
|
|
165
|
+
return utf8Decode(new Uint8Array(plaintext));
|
|
166
|
+
} catch {
|
|
167
|
+
throw new SecretBoxError({
|
|
168
|
+
reason: "decrypt_failed",
|
|
169
|
+
keyId
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
/** Re-encrypts with the current key; returns the input unchanged when it already uses it. */
|
|
174
|
+
const resealSecret = async (keyring, sealed, aad) => needsReseal(keyring, sealed) ? sealSecret(keyring, await openSecret(keyring, sealed, aad), aad) : sealed;
|
|
175
|
+
/** Seals a JSON value (for example a list of secrets) as one ciphertext. */
|
|
176
|
+
const sealJson = (keyring, value, aad) => sealSecret(keyring, JSON.stringify(value), aad);
|
|
177
|
+
/** Opens a sealed JSON value and decodes it with `schema`. */
|
|
178
|
+
const openJson = async (keyring, sealed, aad, schema) => {
|
|
179
|
+
const text = await openSecret(keyring, sealed, aad);
|
|
180
|
+
let parsed;
|
|
181
|
+
try {
|
|
182
|
+
parsed = JSON.parse(text);
|
|
183
|
+
} catch {
|
|
184
|
+
throw new SecretBoxError({ reason: "malformed" });
|
|
185
|
+
}
|
|
186
|
+
const result = Schema.decodeUnknownExit(schema)(parsed);
|
|
187
|
+
if (Exit.isFailure(result)) throw new SecretBoxError({ reason: "malformed" });
|
|
188
|
+
return result.value;
|
|
189
|
+
};
|
|
190
|
+
/** Effect service over a keyring, for runtimes that compose with Layers. */
|
|
191
|
+
var SecretBox = class SecretBox extends Context.Service()("manifold/core/SecretBox") {
|
|
192
|
+
static fromKeyring = (keyring) => {
|
|
193
|
+
const catchBox = (cause) => cause instanceof SecretBoxError ? cause : new SecretBoxError({ reason: "decrypt_failed" });
|
|
194
|
+
return SecretBox.of({
|
|
195
|
+
keyring,
|
|
196
|
+
seal: (plaintext, aad) => Effect.promise(() => sealSecret(keyring, plaintext, aad)),
|
|
197
|
+
open: (sealed, aad) => Effect.tryPromise({
|
|
198
|
+
try: () => openSecret(keyring, sealed, aad),
|
|
199
|
+
catch: catchBox
|
|
200
|
+
}),
|
|
201
|
+
reseal: (sealed, aad) => Effect.tryPromise({
|
|
202
|
+
try: () => resealSecret(keyring, sealed, aad),
|
|
203
|
+
catch: catchBox
|
|
204
|
+
}),
|
|
205
|
+
needsReseal: (sealed) => needsReseal(keyring, sealed)
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
/** Builds the service from a `MANIFOLD_ENCRYPTION_KEYS` value. */
|
|
209
|
+
static layer = (spec) => Layer.effect(SecretBox, Effect.tryPromise({
|
|
210
|
+
try: () => parseKeyring(spec),
|
|
211
|
+
catch: (cause) => cause instanceof KeyringError ? cause : new KeyringError({ message: "invalid encryption keys" })
|
|
212
|
+
}).pipe(Effect.map(SecretBox.fromKeyring)));
|
|
213
|
+
};
|
|
214
|
+
// src/core/filter.ts
|
|
215
|
+
/**
|
|
216
|
+
* Event-type globs for route filters. `*` matches any run of characters
|
|
217
|
+
* (including `.`); everything else matches literally and case-sensitively.
|
|
218
|
+
* A request without an event type matches only the pattern `*`.
|
|
219
|
+
*/
|
|
220
|
+
const cache = /* @__PURE__ */ new Map();
|
|
221
|
+
const toRegExp = (pattern) => {
|
|
222
|
+
let regex = cache.get(pattern);
|
|
223
|
+
if (regex === void 0) {
|
|
224
|
+
const body = pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*");
|
|
225
|
+
regex = new RegExp(`^${body}$`, "s");
|
|
226
|
+
if (cache.size > 1e3) cache.clear();
|
|
227
|
+
cache.set(pattern, regex);
|
|
228
|
+
}
|
|
229
|
+
return regex;
|
|
230
|
+
};
|
|
231
|
+
const matchesGlob = (pattern, eventType) => {
|
|
232
|
+
if (pattern === "*") return true;
|
|
233
|
+
if (eventType === null || eventType === void 0) return false;
|
|
234
|
+
return toRegExp(pattern).test(eventType);
|
|
235
|
+
};
|
|
236
|
+
/** True when any of the route's patterns matches. */
|
|
237
|
+
const matchesEventTypes = (patterns, eventType) => patterns.some((pattern) => matchesGlob(pattern, eventType));
|
|
238
|
+
/** The enabled routes whose filter matches; paused routes are included (their deliveries wait). */
|
|
239
|
+
const matchingRoutes = (routes, eventType) => routes.filter((route) => route.enabled && matchesEventTypes(route.eventTypes, eventType));
|
|
240
|
+
export { toBufferSource as A, secretAad as C, timingSafeEqual as D, sha256Hex as E, utf8Decode as M, utf8Encode as N, toBase64 as O, sealedKeyId as S, sha256 as T, parseKeyring as _, SecretBox as a, sealJson as b, fingerprint as c, fromHex as d, hmac as f, openSecret as g, openJson as h, KeyringError as i, toHex as j, toBase64Url as k, fromBase64 as l, needsReseal as m, matchesGlob as n, SecretBoxError as o, importHmacKey as p, matchingRoutes as r, concatBytes as s, matchesEventTypes as t, fromBase64Url as u, randomBytes as v, secretsEqual as w, sealSecret as x, resealSecret as y };
|