@cogover/sdk 0.12.0 → 0.12.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/README.md +12 -9
- package/dist/crypto.js +89 -46
- package/dist/data.js +4 -1
- package/dist/email.js +5 -3
- package/dist/internal/base64.js +76 -27
- package/dist/internal/bridge.js +41 -3
- package/dist/internal/job-config.js +7 -1
- package/dist/internal/utf8.js +18 -17
- package/dist/locks.js +13 -3
- package/dist/logger.js +47 -1
- package/dist/org.js +14 -14
- package/dist/records.d.ts +13 -0
- package/dist/response.js +21 -1
- package/dist/schema.d.ts +1 -0
- package/dist/script.js +7 -3
- package/docs/en/api-reference/background-jobs.md +288 -0
- package/docs/en/api-reference/cryptography.md +317 -0
- package/docs/en/api-reference/fetch-and-secrets.md +184 -0
- package/docs/en/api-reference/inbound-webhooks.md +65 -0
- package/docs/en/api-reference/notifications-and-email.md +317 -0
- package/docs/en/api-reference/organization-structure.md +304 -0
- package/docs/en/api-reference/overview.md +198 -0
- package/docs/en/api-reference/push-messages.md +115 -0
- package/docs/en/api-reference/record-triggers.md +469 -0
- package/docs/en/api-reference/records.md +447 -0
- package/docs/en/api-reference/scripts-and-routes.md +354 -0
- package/docs/en/api-reference/state-and-locks.md +162 -0
- package/docs/en/usage-guide.md +17 -14
- package/package.json +1 -1
- package/docs/en/api-reference.md +0 -2944
package/README.md
CHANGED
|
@@ -110,9 +110,10 @@ Routes support GET, POST, PUT, PATCH, and DELETE. `request` exposes `method`, `p
|
|
|
110
110
|
decoded `params`, repeated-value-aware `query`, safe lower-case `headers`, and JSON
|
|
111
111
|
`body`. Authentication headers are never exposed. Plain return values are JSON with
|
|
112
112
|
HTTP 200. `response.json`, `text`, `bytes`, `empty`, and `redirect` provide explicit
|
|
113
|
-
status, safe response headers, and text/binary bodies. Missing routes
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
status, safe response headers, and text/binary bodies. Missing routes, including a
|
|
114
|
+
path registered only for other methods, return 404; methods other than these five
|
|
115
|
+
return 405. Existing `defineScript()` projects continue at `/`.
|
|
116
|
+
The complete router contract is included in `docs/en/api-reference/scripts-and-routes.md`.
|
|
116
117
|
|
|
117
118
|
## Record triggers
|
|
118
119
|
|
|
@@ -199,7 +200,7 @@ before-change trigger. Throw `RetryableError` for a temporary failure and Cogove
|
|
|
199
200
|
runs the handler again later, a limited number of times; other errors are logged and
|
|
200
201
|
not retried. Writes made by a trigger run triggers again, and Cogover ends such
|
|
201
202
|
chains after a bounded number of automated writes. The complete contract is included
|
|
202
|
-
in `docs/en/api-reference.md`.
|
|
203
|
+
in `docs/en/api-reference/record-triggers.md`.
|
|
203
204
|
|
|
204
205
|
## Background jobs
|
|
205
206
|
|
|
@@ -280,8 +281,9 @@ export default defineScript<Input>(async ({ input }) => {
|
|
|
280
281
|
});
|
|
281
282
|
```
|
|
282
283
|
|
|
283
|
-
V1 supports public HTTPS URLs on port 443
|
|
284
|
-
|
|
284
|
+
V1 supports public HTTPS URLs on port 443, or on another port once a Workspace
|
|
285
|
+
administrator approves that exact origin for the project, and the methods GET, HEAD,
|
|
286
|
+
POST, PUT, PATCH, and DELETE. Requests and buffered responses have platform limits. Redirects,
|
|
285
287
|
cookie jars, streaming, WebSocket, URL credentials, IP-literal hosts, custom proxy,
|
|
286
288
|
dispatcher, agent, DNS, and TLS settings are not supported. A failed mutating request
|
|
287
289
|
may already have reached the remote API, so use the remote API's idempotency mechanism
|
|
@@ -470,7 +472,8 @@ export default defineScript(async ({ state }) => {
|
|
|
470
472
|
```
|
|
471
473
|
|
|
472
474
|
Distributed locks provide project-scoped token leases for short critical sections.
|
|
473
|
-
`withLock` releases
|
|
475
|
+
`withLock` releases the lease after the callback and reports the callback's own error
|
|
476
|
+
if both fail; manual leases support renew/release and expose a
|
|
474
477
|
fencing token. A lease does not auto-renew and locks do not replace idempotency or
|
|
475
478
|
provide exactly-once execution.
|
|
476
479
|
|
|
@@ -613,8 +616,8 @@ Recommended `tsconfig.json` options:
|
|
|
613
616
|
## Additional documentation
|
|
614
617
|
|
|
615
618
|
The installed package includes an English usage guide at
|
|
616
|
-
`docs/en/usage-guide.md` and the complete API reference
|
|
617
|
-
`docs/en/api-reference.md`.
|
|
619
|
+
`docs/en/usage-guide.md` and the complete API reference in
|
|
620
|
+
`docs/en/api-reference/`, starting at `docs/en/api-reference/overview.md`.
|
|
618
621
|
|
|
619
622
|
## License
|
|
620
623
|
|
package/dist/crypto.js
CHANGED
|
@@ -28,21 +28,38 @@ const AES_DECRYPT_OPTIONS = new Set(["mode", "iv", "tag", "aad", "encoding", "ou
|
|
|
28
28
|
const RSA_ENCRYPT_OPTIONS = new Set(["padding", "encoding"]);
|
|
29
29
|
const RSA_DECRYPT_OPTIONS = new Set(["padding", "encoding", "output"]);
|
|
30
30
|
const SIGNATURE_OPTIONS = new Set(["encoding", "signatureFormat"]);
|
|
31
|
+
/**
|
|
32
|
+
* Bytes that cross the bridge as base64. They are encoded only when the request is serialized, after the size
|
|
33
|
+
* check: base64 runs as guest code, so encoding data that is then refused would spend the invocation's CPU time
|
|
34
|
+
* and statement budget for nothing.
|
|
35
|
+
*/
|
|
36
|
+
class Base64Bytes {
|
|
37
|
+
bytes;
|
|
38
|
+
constructor(bytes) {
|
|
39
|
+
this.bytes = bytes;
|
|
40
|
+
}
|
|
41
|
+
toJSON() {
|
|
42
|
+
return encodeBase64(this.bytes);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
31
45
|
async function aesEncrypt(key, data, options) {
|
|
32
46
|
const label = "aesEncrypt()";
|
|
33
47
|
const opts = optionsObject(options, AES_ENCRYPT_OPTIONS, label);
|
|
34
48
|
const mode = aesMode(opts.mode, label);
|
|
35
49
|
const encoding = binaryEncoding(opts.encoding, label);
|
|
50
|
+
const keyPayload = aesKey(key, label);
|
|
51
|
+
const plain = bridgeData(data, `${label} data`);
|
|
36
52
|
const payload = {
|
|
37
53
|
algorithm: `AES-${mode}`,
|
|
38
|
-
key:
|
|
39
|
-
...namedData(
|
|
54
|
+
key: keyPayload,
|
|
55
|
+
...namedData(plain, "data"),
|
|
40
56
|
encoding,
|
|
41
57
|
};
|
|
58
|
+
let minimumBytes = plain.jsonBytes;
|
|
42
59
|
if (opts.iv !== undefined)
|
|
43
60
|
addBinary(payload, "iv", binaryInput(opts.iv, encoding, `${label} iv`, MAX_ENCODED_PARAMETER_CHARS));
|
|
44
61
|
if (opts.aad !== undefined)
|
|
45
|
-
addAad(payload, opts.aad, mode, label);
|
|
62
|
+
minimumBytes += addAad(payload, opts.aad, mode, label);
|
|
46
63
|
if (opts.separateTag !== undefined) {
|
|
47
64
|
if (typeof opts.separateTag !== "boolean")
|
|
48
65
|
throw new ValidationError(`${label} separateTag must be a boolean`);
|
|
@@ -51,7 +68,7 @@ async function aesEncrypt(key, data, options) {
|
|
|
51
68
|
if (opts.separateTag)
|
|
52
69
|
payload.separateTag = true;
|
|
53
70
|
}
|
|
54
|
-
const result = await callBridge("crypto.encrypt", payload);
|
|
71
|
+
const result = await callBridge("crypto.encrypt", payload, minimumBytes);
|
|
55
72
|
if (!isRecord(result) || typeof result.ciphertext !== "string" || typeof result.iv !== "string"
|
|
56
73
|
|| result.iv.length === 0 || (payload.separateTag === true) !== (typeof result.tag === "string")) {
|
|
57
74
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Encryption result is invalid");
|
|
@@ -70,31 +87,37 @@ async function aesDecrypt(key, ciphertext, options) {
|
|
|
70
87
|
const output = decryptOutput(opts.output, label);
|
|
71
88
|
if (opts.iv === undefined)
|
|
72
89
|
throw new ValidationError(`${label} options.iv is required`);
|
|
90
|
+
const keyPayload = aesKey(key, label);
|
|
91
|
+
const sealed = binaryInput(ciphertext, encoding, `${label} ciphertext`, MAX_ENCODED_CHARS);
|
|
73
92
|
const payload = {
|
|
74
93
|
algorithm: `AES-${mode}`,
|
|
75
|
-
key:
|
|
76
|
-
...namedBinary(
|
|
94
|
+
key: keyPayload,
|
|
95
|
+
...namedBinary(sealed, "data"),
|
|
77
96
|
...namedBinary(binaryInput(opts.iv, encoding, `${label} iv`, MAX_ENCODED_PARAMETER_CHARS), "iv"),
|
|
78
97
|
output: output === "bytes" ? "base64" : output,
|
|
79
98
|
};
|
|
99
|
+
let minimumBytes = sealed.jsonBytes;
|
|
80
100
|
if (opts.tag !== undefined) {
|
|
81
101
|
if (mode !== "GCM")
|
|
82
102
|
throw new ValidationError(`${label} tag requires mode "GCM"`);
|
|
83
103
|
addBinary(payload, "tag", binaryInput(opts.tag, encoding, `${label} tag`, MAX_ENCODED_PARAMETER_CHARS));
|
|
84
104
|
}
|
|
85
105
|
if (opts.aad !== undefined)
|
|
86
|
-
addAad(payload, opts.aad, mode, label);
|
|
87
|
-
return plaintext(await callBridge("crypto.decrypt", payload), output);
|
|
106
|
+
minimumBytes += addAad(payload, opts.aad, mode, label);
|
|
107
|
+
return plaintext(await callBridge("crypto.decrypt", payload, minimumBytes), output);
|
|
88
108
|
}
|
|
89
109
|
async function rsaEncrypt(publicKey, data, options) {
|
|
90
110
|
const label = "rsaEncrypt()";
|
|
91
111
|
const opts = optionsObject(options, RSA_ENCRYPT_OPTIONS, label);
|
|
112
|
+
const algorithm = `RSA-${rsaPadding(opts.padding, label)}`;
|
|
113
|
+
const key = asymmetricKey(publicKey, `${label} public key`);
|
|
114
|
+
const plain = bridgeData(data, `${label} data`);
|
|
92
115
|
const result = await callBridge("crypto.encrypt", {
|
|
93
|
-
algorithm
|
|
94
|
-
key
|
|
95
|
-
...namedData(
|
|
116
|
+
algorithm,
|
|
117
|
+
key,
|
|
118
|
+
...namedData(plain, "data"),
|
|
96
119
|
encoding: binaryEncoding(opts.encoding, label),
|
|
97
|
-
});
|
|
120
|
+
}, plain.jsonBytes);
|
|
98
121
|
if (!isRecord(result) || typeof result.ciphertext !== "string" || result.ciphertext.length === 0) {
|
|
99
122
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Encryption result is invalid");
|
|
100
123
|
}
|
|
@@ -105,25 +128,30 @@ async function rsaDecrypt(privateKey, ciphertext, options) {
|
|
|
105
128
|
const opts = optionsObject(options, RSA_DECRYPT_OPTIONS, label);
|
|
106
129
|
const encoding = binaryEncoding(opts.encoding, label);
|
|
107
130
|
const output = decryptOutput(opts.output, label);
|
|
131
|
+
const algorithm = `RSA-${rsaPadding(opts.padding, label)}`;
|
|
132
|
+
const key = asymmetricKey(privateKey, `${label} private key`);
|
|
133
|
+
const sealed = binaryInput(ciphertext, encoding, `${label} ciphertext`, MAX_ENCODED_CHARS);
|
|
108
134
|
return plaintext(await callBridge("crypto.decrypt", {
|
|
109
|
-
algorithm
|
|
110
|
-
key
|
|
111
|
-
...namedBinary(
|
|
135
|
+
algorithm,
|
|
136
|
+
key,
|
|
137
|
+
...namedBinary(sealed, "data"),
|
|
112
138
|
output: output === "bytes" ? "base64" : output,
|
|
113
|
-
}), output);
|
|
139
|
+
}, sealed.jsonBytes), output);
|
|
114
140
|
}
|
|
115
141
|
async function sign(algorithm, privateKey, data, options) {
|
|
116
142
|
const label = "sign()";
|
|
117
143
|
const signatureAlgorithm = requireSignatureAlgorithm(algorithm, label);
|
|
118
144
|
const opts = optionsObject(options, SIGNATURE_OPTIONS, label);
|
|
145
|
+
const key = asymmetricKey(privateKey, `${label} private key`);
|
|
146
|
+
const signed = bridgeData(data, `${label} data`);
|
|
119
147
|
const payload = {
|
|
120
148
|
algorithm: signatureAlgorithm,
|
|
121
|
-
key
|
|
122
|
-
...namedData(
|
|
149
|
+
key,
|
|
150
|
+
...namedData(signed, "data"),
|
|
123
151
|
encoding: binaryEncoding(opts.encoding, label),
|
|
124
152
|
};
|
|
125
153
|
addSignatureFormat(payload, opts.signatureFormat, signatureAlgorithm, label);
|
|
126
|
-
const result = await callBridge("crypto.sign", payload);
|
|
154
|
+
const result = await callBridge("crypto.sign", payload, signed.jsonBytes);
|
|
127
155
|
if (!isRecord(result) || typeof result.signature !== "string" || result.signature.length === 0) {
|
|
128
156
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Signature is invalid");
|
|
129
157
|
}
|
|
@@ -139,47 +167,46 @@ async function verify(algorithm, publicKey, data, signature, options) {
|
|
|
139
167
|
if (typeof signature !== "string" && !(signature instanceof Uint8Array)) {
|
|
140
168
|
throw new ValidationError(`${label} signature must be a string or a Uint8Array`);
|
|
141
169
|
}
|
|
142
|
-
// A signature usually comes from the party being verified, so one that is too long is simply invalid.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
170
|
+
// A signature usually comes from the party being verified, so one that is too long is simply invalid. It is
|
|
171
|
+
// replaced by an empty one, which Cogover reports as invalid only after it has checked the key and algorithm.
|
|
172
|
+
const oversized = typeof signature === "string"
|
|
173
|
+
? signature.length > MAX_ENCODED_PARAMETER_CHARS * 2
|
|
174
|
+
: signature.length > 1024;
|
|
147
175
|
const payload = {
|
|
148
176
|
algorithm: signatureAlgorithm,
|
|
149
177
|
key,
|
|
150
178
|
...namedData(signed, "data"),
|
|
151
|
-
...namedBinary(
|
|
152
|
-
?
|
|
153
|
-
: { value: encodeBase64(signature), encoding: "base64" }, "signature"),
|
|
179
|
+
...namedBinary(oversized ? textInput("", "base64")
|
|
180
|
+
: typeof signature === "string" ? textInput(signature, encoding) : bytesInput(signature), "signature"),
|
|
154
181
|
};
|
|
155
182
|
addSignatureFormat(payload, opts.signatureFormat, signatureAlgorithm, label);
|
|
156
|
-
const result = await callBridge("crypto.verify", payload);
|
|
183
|
+
const result = await callBridge("crypto.verify", payload, signed.jsonBytes);
|
|
157
184
|
if (!isRecord(result) || typeof result.valid !== "boolean") {
|
|
158
185
|
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "Verification result is invalid");
|
|
159
186
|
}
|
|
160
|
-
return result.valid;
|
|
187
|
+
return result.valid && !oversized;
|
|
161
188
|
}
|
|
162
189
|
/** Cryptographic helpers backed by Cogover's managed implementation. */
|
|
163
190
|
export const crypto = Object.freeze({
|
|
164
191
|
async sha256(data, encoding = "hex") {
|
|
165
|
-
const { data: text, dataEncoding } = bridgeData(data, "sha256() data");
|
|
192
|
+
const { data: text, dataEncoding, jsonBytes } = bridgeData(data, "sha256() data");
|
|
166
193
|
return digest(await callBridge("crypto.hash", {
|
|
167
194
|
algorithm: "SHA-256",
|
|
168
195
|
data: text,
|
|
169
196
|
dataEncoding,
|
|
170
197
|
encoding: digestEncoding(encoding, "sha256()"),
|
|
171
|
-
}));
|
|
198
|
+
}, jsonBytes));
|
|
172
199
|
},
|
|
173
200
|
async hmacSha256(key, data, encoding = "hex") {
|
|
174
|
-
const keyPayload = hmacKey(key);
|
|
175
|
-
const { data: text, dataEncoding } = bridgeData(data, "hmacSha256() data");
|
|
201
|
+
const { key: keyPayload, jsonBytes: keyBytes } = hmacKey(key);
|
|
202
|
+
const { data: text, dataEncoding, jsonBytes } = bridgeData(data, "hmacSha256() data");
|
|
176
203
|
return digest(await callBridge("crypto.hmac", {
|
|
177
204
|
algorithm: "SHA-256",
|
|
178
205
|
key: keyPayload,
|
|
179
206
|
data: text,
|
|
180
207
|
dataEncoding,
|
|
181
208
|
encoding: digestEncoding(encoding, "hmacSha256()"),
|
|
182
|
-
}));
|
|
209
|
+
}, keyBytes + jsonBytes));
|
|
183
210
|
},
|
|
184
211
|
async randomBytes(length) {
|
|
185
212
|
if (!Number.isInteger(length) || length < 1 || length > MAX_RANDOM_BYTES) {
|
|
@@ -218,16 +245,21 @@ export const crypto = Object.freeze({
|
|
|
218
245
|
sign,
|
|
219
246
|
verify,
|
|
220
247
|
});
|
|
248
|
+
/** Characters of the padded base64 text of `byteLength` bytes. */
|
|
249
|
+
function base64Length(byteLength) {
|
|
250
|
+
return 4 * Math.ceil(byteLength / 3);
|
|
251
|
+
}
|
|
221
252
|
function bridgeData(data, label) {
|
|
222
253
|
if (typeof data === "string") {
|
|
223
|
-
|
|
254
|
+
const byteLength = utf8ByteLength(data);
|
|
255
|
+
if (byteLength > MAX_DATA_BYTES)
|
|
224
256
|
throw tooLarge(label);
|
|
225
|
-
return { data, dataEncoding: "utf8" };
|
|
257
|
+
return { data, dataEncoding: "utf8", jsonBytes: byteLength + 2 };
|
|
226
258
|
}
|
|
227
259
|
if (data instanceof Uint8Array) {
|
|
228
260
|
if (data.length > MAX_DATA_BYTES)
|
|
229
261
|
throw tooLarge(label);
|
|
230
|
-
return { data:
|
|
262
|
+
return { data: new Base64Bytes(data), dataEncoding: "base64", jsonBytes: base64Length(data.length) + 2 };
|
|
231
263
|
}
|
|
232
264
|
throw new ValidationError(`${label} must be a string or a Uint8Array`);
|
|
233
265
|
}
|
|
@@ -240,25 +272,34 @@ function namedBinary(input, name) {
|
|
|
240
272
|
function addBinary(payload, name, input) {
|
|
241
273
|
Object.assign(payload, namedBinary(input, name));
|
|
242
274
|
}
|
|
275
|
+
/** Text already in `encoding`; each UTF-16 code unit takes at least one byte. */
|
|
276
|
+
function textInput(value, encoding) {
|
|
277
|
+
return { value, encoding, jsonBytes: value.length + 2 };
|
|
278
|
+
}
|
|
279
|
+
function bytesInput(bytes) {
|
|
280
|
+
return { value: new Base64Bytes(bytes), encoding: "base64", jsonBytes: base64Length(bytes.length) + 2 };
|
|
281
|
+
}
|
|
243
282
|
/** A binary value given as text in `encoding`, or as bytes that cross the bridge as base64. */
|
|
244
283
|
function binaryInput(value, encoding, label, maxChars) {
|
|
245
284
|
if (typeof value === "string") {
|
|
246
285
|
if (value.length > maxChars)
|
|
247
286
|
throw new ValidationError(`${label} must not exceed ${maxChars} characters`);
|
|
248
|
-
return
|
|
287
|
+
return textInput(value, encoding);
|
|
249
288
|
}
|
|
250
289
|
if (value instanceof Uint8Array) {
|
|
251
|
-
|
|
252
|
-
if (text.length > maxChars)
|
|
290
|
+
if (base64Length(value.length) > maxChars)
|
|
253
291
|
throw new ValidationError(`${label} is too large`);
|
|
254
|
-
return
|
|
292
|
+
return bytesInput(value);
|
|
255
293
|
}
|
|
256
294
|
throw new ValidationError(`${label} must be a string or a Uint8Array`);
|
|
257
295
|
}
|
|
296
|
+
/** Adds `aad` and returns the bytes it takes in the JSON request at least. */
|
|
258
297
|
function addAad(payload, aad, mode, label) {
|
|
259
298
|
if (mode !== "GCM")
|
|
260
299
|
throw new ValidationError(`${label} aad requires mode "GCM"`);
|
|
261
|
-
|
|
300
|
+
const data = bridgeData(aad, `${label} aad`);
|
|
301
|
+
Object.assign(payload, namedData(data, "aad"));
|
|
302
|
+
return data.jsonBytes;
|
|
262
303
|
}
|
|
263
304
|
function optionsObject(options, allowed, label) {
|
|
264
305
|
if (options === undefined)
|
|
@@ -368,23 +409,25 @@ function plaintext(payload, output) {
|
|
|
368
409
|
function aesKeyLength(label) {
|
|
369
410
|
return new ValidationError(`${label} key must be 16, 24 or 32 bytes`);
|
|
370
411
|
}
|
|
412
|
+
/** The HMAC key payload and the bytes its value takes in the JSON request at least. */
|
|
371
413
|
function hmacKey(key) {
|
|
372
414
|
if (typeof key === "string") {
|
|
373
415
|
if (key.length === 0)
|
|
374
416
|
throw new ValidationError("hmacSha256() key must not be empty");
|
|
375
|
-
|
|
417
|
+
const byteLength = utf8ByteLength(key);
|
|
418
|
+
if (byteLength > MAX_DATA_BYTES)
|
|
376
419
|
throw tooLarge("hmacSha256() key");
|
|
377
|
-
return { value: key, encoding: "utf8" };
|
|
420
|
+
return { key: { value: key, encoding: "utf8" }, jsonBytes: byteLength + 2 };
|
|
378
421
|
}
|
|
379
422
|
if (key instanceof Uint8Array) {
|
|
380
423
|
if (key.length === 0)
|
|
381
424
|
throw new ValidationError("hmacSha256() key must not be empty");
|
|
382
425
|
if (key.length > MAX_DATA_BYTES)
|
|
383
426
|
throw tooLarge("hmacSha256() key");
|
|
384
|
-
return { value:
|
|
427
|
+
return { key: { value: new Base64Bytes(key), encoding: "base64" }, jsonBytes: base64Length(key.length) + 2 };
|
|
385
428
|
}
|
|
386
429
|
if (isRecord(key) && Object.keys(key).length === 1 && "secret" in key) {
|
|
387
|
-
return { secret: requireSecretName(key.secret, "hmacSha256() key.secret") };
|
|
430
|
+
return { key: { secret: requireSecretName(key.secret, "hmacSha256() key.secret") }, jsonBytes: 0 };
|
|
388
431
|
}
|
|
389
432
|
throw new ValidationError("hmacSha256() key must be a string, a Uint8Array or { secret: name }");
|
|
390
433
|
}
|
package/dist/data.js
CHANGED
|
@@ -109,7 +109,7 @@ function createObjectClient(slug, identity) {
|
|
|
109
109
|
}
|
|
110
110
|
try {
|
|
111
111
|
return {
|
|
112
|
-
id: requireText(item.id,
|
|
112
|
+
id: requireText(item.id, "record id"),
|
|
113
113
|
fields: requireFields(item.fields),
|
|
114
114
|
};
|
|
115
115
|
}
|
|
@@ -144,6 +144,9 @@ function createObjectClient(slug, identity) {
|
|
|
144
144
|
if (!Array.isArray(ids) || ids.length === 0) {
|
|
145
145
|
throw new ValidationError("deleteMany() requires at least one record id");
|
|
146
146
|
}
|
|
147
|
+
if (ids.length > MAX_BATCH_RECORDS) {
|
|
148
|
+
throw new ValidationError(`deleteMany() supports at most ${MAX_BATCH_RECORDS} record ids`);
|
|
149
|
+
}
|
|
147
150
|
return callBridge("records.deleteMany", {
|
|
148
151
|
identity,
|
|
149
152
|
objectSlug: slug,
|
package/dist/email.js
CHANGED
|
@@ -8,7 +8,8 @@ const MAX_RECIPIENTS = 50;
|
|
|
8
8
|
const MAX_SUBJECT_CHARS = 255;
|
|
9
9
|
const MAX_NAME_CHARS = 200;
|
|
10
10
|
const MAX_EMAIL_CHARS = 254;
|
|
11
|
-
// The
|
|
11
|
+
// The body is measured as it travels, JSON-encoded, so that with the rest of the message it fits one capability
|
|
12
|
+
// request (MAX_BRIDGE_REQUEST_BYTES).
|
|
12
13
|
const MAX_BODY_BYTES = 192 * 1024;
|
|
13
14
|
const MAX_ATTACHMENTS = 10;
|
|
14
15
|
const MAX_RECORD_EMAIL_FIELDS = 20;
|
|
@@ -80,8 +81,9 @@ function emailPayload(message) {
|
|
|
80
81
|
if ((html === undefined) === (text === undefined))
|
|
81
82
|
throw new ValidationError("Provide exactly one of html and text");
|
|
82
83
|
const body = (html ?? text);
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
// JSON.stringify adds two quotes; each quote, backslash or control character inside counts as its escape.
|
|
85
|
+
if (utf8ByteLength(JSON.stringify(body)) - 2 > MAX_BODY_BYTES) {
|
|
86
|
+
throw new ValidationError(`The email body must not exceed ${MAX_BODY_BYTES} bytes once JSON-encoded`);
|
|
85
87
|
}
|
|
86
88
|
if (html !== undefined)
|
|
87
89
|
payload.html = html;
|
package/dist/internal/base64.js
CHANGED
|
@@ -1,41 +1,90 @@
|
|
|
1
|
-
const
|
|
1
|
+
const A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
2
2
|
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
3
3
|
// Sandboxed code has neither Buffer nor atob/btoa, so binary data crosses the bridge through these helpers.
|
|
4
|
+
// The sandbox caps the statements one execution may run and every loop turn costs about three of them, so each
|
|
5
|
+
// turn handles 24 bytes in a single expression: 256 KiB costs about 33,000 statements instead of several hundred
|
|
6
|
+
// thousand.
|
|
7
|
+
let digits;
|
|
8
|
+
/** 6-bit value of each base64 character code; "=" reads as 0 once the text has been validated. */
|
|
9
|
+
function digitTable() {
|
|
10
|
+
if (digits === undefined) {
|
|
11
|
+
const table = new Int32Array(128);
|
|
12
|
+
for (let index = 0; index < A.length; index++)
|
|
13
|
+
table[A.charCodeAt(index)] = index;
|
|
14
|
+
digits = table;
|
|
15
|
+
}
|
|
16
|
+
return digits;
|
|
17
|
+
}
|
|
4
18
|
/** Standard base64 with padding (RFC 4648 section 4). */
|
|
5
19
|
export function encodeBase64(bytes) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
+
|
|
14
|
-
+ (index +
|
|
15
|
-
+ (index +
|
|
20
|
+
const length = bytes.length;
|
|
21
|
+
const whole = length - (length % 3);
|
|
22
|
+
const parts = [];
|
|
23
|
+
let index = 0;
|
|
24
|
+
for (; index + 24 <= whole; index += 24) {
|
|
25
|
+
parts[parts.length] = A.charAt(bytes[index] >>> 2) + A.charAt(((bytes[index] & 3) << 4) | (bytes[index + 1] >>> 4))
|
|
26
|
+
+ A.charAt(((bytes[index + 1] & 15) << 2) | (bytes[index + 2] >>> 6)) + A.charAt(bytes[index + 2] & 63)
|
|
27
|
+
+ A.charAt(bytes[index + 3] >>> 2) + A.charAt(((bytes[index + 3] & 3) << 4) | (bytes[index + 4] >>> 4))
|
|
28
|
+
+ A.charAt(((bytes[index + 4] & 15) << 2) | (bytes[index + 5] >>> 6)) + A.charAt(bytes[index + 5] & 63)
|
|
29
|
+
+ A.charAt(bytes[index + 6] >>> 2) + A.charAt(((bytes[index + 6] & 3) << 4) | (bytes[index + 7] >>> 4))
|
|
30
|
+
+ A.charAt(((bytes[index + 7] & 15) << 2) | (bytes[index + 8] >>> 6)) + A.charAt(bytes[index + 8] & 63)
|
|
31
|
+
+ A.charAt(bytes[index + 9] >>> 2) + A.charAt(((bytes[index + 9] & 3) << 4) | (bytes[index + 10] >>> 4))
|
|
32
|
+
+ A.charAt(((bytes[index + 10] & 15) << 2) | (bytes[index + 11] >>> 6)) + A.charAt(bytes[index + 11] & 63)
|
|
33
|
+
+ A.charAt(bytes[index + 12] >>> 2) + A.charAt(((bytes[index + 12] & 3) << 4) | (bytes[index + 13] >>> 4))
|
|
34
|
+
+ A.charAt(((bytes[index + 13] & 15) << 2) | (bytes[index + 14] >>> 6)) + A.charAt(bytes[index + 14] & 63)
|
|
35
|
+
+ A.charAt(bytes[index + 15] >>> 2) + A.charAt(((bytes[index + 15] & 3) << 4) | (bytes[index + 16] >>> 4))
|
|
36
|
+
+ A.charAt(((bytes[index + 16] & 15) << 2) | (bytes[index + 17] >>> 6)) + A.charAt(bytes[index + 17] & 63)
|
|
37
|
+
+ A.charAt(bytes[index + 18] >>> 2) + A.charAt(((bytes[index + 18] & 3) << 4) | (bytes[index + 19] >>> 4))
|
|
38
|
+
+ A.charAt(((bytes[index + 19] & 15) << 2) | (bytes[index + 20] >>> 6)) + A.charAt(bytes[index + 20] & 63)
|
|
39
|
+
+ A.charAt(bytes[index + 21] >>> 2) + A.charAt(((bytes[index + 21] & 3) << 4) | (bytes[index + 22] >>> 4))
|
|
40
|
+
+ A.charAt(((bytes[index + 22] & 15) << 2) | (bytes[index + 23] >>> 6)) + A.charAt(bytes[index + 23] & 63);
|
|
41
|
+
}
|
|
42
|
+
for (; index < whole; index += 3) {
|
|
43
|
+
parts[parts.length] = A.charAt(bytes[index] >>> 2) + A.charAt(((bytes[index] & 3) << 4) | (bytes[index + 1] >>> 4))
|
|
44
|
+
+ A.charAt(((bytes[index + 1] & 15) << 2) | (bytes[index + 2] >>> 6)) + A.charAt(bytes[index + 2] & 63);
|
|
45
|
+
}
|
|
46
|
+
if (length - whole === 1) {
|
|
47
|
+
parts[parts.length] = `${A.charAt(bytes[whole] >>> 2)}${A.charAt((bytes[whole] & 3) << 4)}==`;
|
|
16
48
|
}
|
|
17
|
-
|
|
49
|
+
else if (length - whole === 2) {
|
|
50
|
+
parts[parts.length] = `${A.charAt(bytes[whole] >>> 2)}${A.charAt(((bytes[whole] & 3) << 4) | (bytes[whole + 1] >>> 4))}`
|
|
51
|
+
+ `${A.charAt((bytes[whole + 1] & 15) << 2)}=`;
|
|
52
|
+
}
|
|
53
|
+
return parts.join("");
|
|
18
54
|
}
|
|
19
55
|
/** Decodes padded standard base64; returns null for text that is not canonical base64. */
|
|
20
56
|
export function decodeBase64(text) {
|
|
21
57
|
if (typeof text !== "string" || !BASE64.test(text))
|
|
22
58
|
return null;
|
|
59
|
+
const table = digitTable();
|
|
23
60
|
const padding = text.endsWith("==") ? 2 : text.endsWith("=") ? 1 : 0;
|
|
24
|
-
const
|
|
61
|
+
const groups = text.length / 4;
|
|
62
|
+
// One spare byte: each group is written as a 32-bit word whose last byte the next group overwrites.
|
|
63
|
+
const buffer = new Uint8Array(groups * 3 + 1);
|
|
64
|
+
const view = new DataView(buffer.buffer);
|
|
65
|
+
let index = 0;
|
|
25
66
|
let position = 0;
|
|
26
|
-
for (
|
|
27
|
-
|
|
28
|
-
| (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
67
|
+
for (; index + 32 <= text.length; index += 32, position += 24) {
|
|
68
|
+
view.setUint32(position, (table[text.charCodeAt(index)] << 26) | (table[text.charCodeAt(index + 1)] << 20)
|
|
69
|
+
| (table[text.charCodeAt(index + 2)] << 14) | (table[text.charCodeAt(index + 3)] << 8)),
|
|
70
|
+
view.setUint32(position + 3, (table[text.charCodeAt(index + 4)] << 26) | (table[text.charCodeAt(index + 5)] << 20)
|
|
71
|
+
| (table[text.charCodeAt(index + 6)] << 14) | (table[text.charCodeAt(index + 7)] << 8)),
|
|
72
|
+
view.setUint32(position + 6, (table[text.charCodeAt(index + 8)] << 26) | (table[text.charCodeAt(index + 9)] << 20)
|
|
73
|
+
| (table[text.charCodeAt(index + 10)] << 14) | (table[text.charCodeAt(index + 11)] << 8)),
|
|
74
|
+
view.setUint32(position + 9, (table[text.charCodeAt(index + 12)] << 26) | (table[text.charCodeAt(index + 13)] << 20)
|
|
75
|
+
| (table[text.charCodeAt(index + 14)] << 14) | (table[text.charCodeAt(index + 15)] << 8)),
|
|
76
|
+
view.setUint32(position + 12, (table[text.charCodeAt(index + 16)] << 26) | (table[text.charCodeAt(index + 17)] << 20)
|
|
77
|
+
| (table[text.charCodeAt(index + 18)] << 14) | (table[text.charCodeAt(index + 19)] << 8)),
|
|
78
|
+
view.setUint32(position + 15, (table[text.charCodeAt(index + 20)] << 26) | (table[text.charCodeAt(index + 21)] << 20)
|
|
79
|
+
| (table[text.charCodeAt(index + 22)] << 14) | (table[text.charCodeAt(index + 23)] << 8)),
|
|
80
|
+
view.setUint32(position + 18, (table[text.charCodeAt(index + 24)] << 26) | (table[text.charCodeAt(index + 25)] << 20)
|
|
81
|
+
| (table[text.charCodeAt(index + 26)] << 14) | (table[text.charCodeAt(index + 27)] << 8)),
|
|
82
|
+
view.setUint32(position + 21, (table[text.charCodeAt(index + 28)] << 26) | (table[text.charCodeAt(index + 29)] << 20)
|
|
83
|
+
| (table[text.charCodeAt(index + 30)] << 14) | (table[text.charCodeAt(index + 31)] << 8));
|
|
35
84
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return
|
|
85
|
+
for (; index < text.length; index += 4, position += 3) {
|
|
86
|
+
view.setUint32(position, (table[text.charCodeAt(index)] << 26) | (table[text.charCodeAt(index + 1)] << 20)
|
|
87
|
+
| (table[text.charCodeAt(index + 2)] << 14) | (table[text.charCodeAt(index + 3)] << 8));
|
|
88
|
+
}
|
|
89
|
+
return buffer.slice(0, groups * 3 - padding);
|
|
41
90
|
}
|
package/dist/internal/bridge.js
CHANGED
|
@@ -1,13 +1,40 @@
|
|
|
1
|
-
import { CogoverApiError, NotFoundError, PermissionDeniedError, RateLimitError, StateConflictError, LockLostError, ValidationError, } from "../errors.js";
|
|
1
|
+
import { CogoverApiError, NotFoundError, PermissionDeniedError, RateLimitError, RetryableError, StateConflictError, LockLostError, ValidationError, } from "../errors.js";
|
|
2
|
+
import { utf8ByteLength } from "./utf8.js";
|
|
2
3
|
import { errorMessage, isRecord } from "./values.js";
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Largest capability request, envelope included, that Cogover accepts. A larger one would end the whole execution
|
|
6
|
+
* with a platform failure that a script cannot catch, so it is refused here as a ValidationError instead.
|
|
7
|
+
*/
|
|
8
|
+
export const MAX_BRIDGE_REQUEST_BYTES = 256 * 1024;
|
|
9
|
+
/** Bytes of the request envelope around an empty operation name and an empty payload. */
|
|
10
|
+
const ENVELOPE_BYTES = JSON.stringify({ version: 1, operation: "", payload: {} }).length;
|
|
11
|
+
/**
|
|
12
|
+
* Sends one capability request. `minimumPayloadBytes`, when given, must not exceed the UTF-8 size of the
|
|
13
|
+
* JSON-encoded payload: a request that cannot fit is then refused before it is serialized, so values that encode
|
|
14
|
+
* themselves while serializing (large bytes as base64) cost no CPU time or statements from the invocation's limits.
|
|
15
|
+
*/
|
|
16
|
+
export async function callBridge(operation, payload, minimumPayloadBytes = 0) {
|
|
4
17
|
const bridge = globalThis.__cogoverBridgeCall;
|
|
5
18
|
if (typeof bridge !== "function") {
|
|
6
19
|
throw new CogoverApiError("CAPABILITY_UNAVAILABLE", "Cogover data capabilities are only available inside the Cogover runtime");
|
|
7
20
|
}
|
|
21
|
+
if (ENVELOPE_BYTES + operation.length + minimumPayloadBytes > MAX_BRIDGE_REQUEST_BYTES) {
|
|
22
|
+
throw requestTooLarge(operation);
|
|
23
|
+
}
|
|
24
|
+
let requestJson;
|
|
25
|
+
try {
|
|
26
|
+
requestJson = JSON.stringify({ version: 1, operation, payload });
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new CogoverApiError("BRIDGE_ERROR", errorMessage(error));
|
|
30
|
+
}
|
|
31
|
+
// A UTF-16 code unit never takes more than three UTF-8 bytes, so a short request needs no count.
|
|
32
|
+
if (requestJson.length * 3 > MAX_BRIDGE_REQUEST_BYTES && utf8ByteLength(requestJson) > MAX_BRIDGE_REQUEST_BYTES) {
|
|
33
|
+
throw requestTooLarge(operation);
|
|
34
|
+
}
|
|
8
35
|
let responseJson;
|
|
9
36
|
try {
|
|
10
|
-
responseJson = await bridge(
|
|
37
|
+
responseJson = await bridge(requestJson);
|
|
11
38
|
}
|
|
12
39
|
catch (error) {
|
|
13
40
|
throw new CogoverApiError("BRIDGE_ERROR", errorMessage(error));
|
|
@@ -24,6 +51,15 @@ export async function callBridge(operation, payload) {
|
|
|
24
51
|
}
|
|
25
52
|
throw publicError(response.error);
|
|
26
53
|
}
|
|
54
|
+
/** The same error Cogover reports for a request it refuses before dispatch. */
|
|
55
|
+
function requestTooLarge(operation) {
|
|
56
|
+
return publicError({
|
|
57
|
+
code: operation === "http.fetch" ? "FETCH_REQUEST_TOO_LARGE" : "VALIDATION_ERROR",
|
|
58
|
+
message: `The ${operation} request must not exceed ${MAX_BRIDGE_REQUEST_BYTES} bytes once JSON-encoded; `
|
|
59
|
+
+ "send less data in one call",
|
|
60
|
+
details: { limit: MAX_BRIDGE_REQUEST_BYTES },
|
|
61
|
+
});
|
|
62
|
+
}
|
|
27
63
|
function publicError(error) {
|
|
28
64
|
switch (error.code) {
|
|
29
65
|
case "NOT_FOUND": {
|
|
@@ -38,6 +74,8 @@ function publicError(error) {
|
|
|
38
74
|
return new PermissionDeniedError(error.message, error.details);
|
|
39
75
|
case "RATE_LIMITED":
|
|
40
76
|
return new RateLimitError(error.message, error.details);
|
|
77
|
+
case "RETRYABLE":
|
|
78
|
+
return new RetryableError(error.message, error.details);
|
|
41
79
|
case "STATE_CONFLICT":
|
|
42
80
|
return new StateConflictError(error.message, error.details);
|
|
43
81
|
case "LOCK_LOST":
|
|
@@ -12,6 +12,8 @@ const MIN_ATTEMPTS = 1;
|
|
|
12
12
|
const MAX_ATTEMPTS = 10;
|
|
13
13
|
const DEFAULT_ATTEMPTS = 5;
|
|
14
14
|
const MAX_TIMEZONE_LENGTH = 64;
|
|
15
|
+
// Cogover refuses a longer expression when the version is published.
|
|
16
|
+
const MAX_CRON_LENGTH = 128;
|
|
15
17
|
const DEFAULT_TIMEZONE = "UTC";
|
|
16
18
|
// The shape of an IANA zone ID; Cogover checks that the zone exists when the version is published.
|
|
17
19
|
const TIMEZONE = /^[A-Za-z][A-Za-z0-9_+-]*(?:\/[A-Za-z0-9_+-]+)*$/;
|
|
@@ -93,8 +95,12 @@ function normalizeCron(expression, fail) {
|
|
|
93
95
|
if (fields.length !== 5) {
|
|
94
96
|
throw fail("schedule", `schedule.cron must contain exactly five fields, found ${fields.length}`);
|
|
95
97
|
}
|
|
98
|
+
const normalized = fields.join(" ");
|
|
99
|
+
if (normalized.length > MAX_CRON_LENGTH) {
|
|
100
|
+
throw fail("schedule", `schedule.cron must be a non-empty string of at most ${MAX_CRON_LENGTH} characters`);
|
|
101
|
+
}
|
|
96
102
|
fields.forEach((field, index) => validateCronField(field, CRON_FIELDS[index], fail));
|
|
97
|
-
return
|
|
103
|
+
return normalized;
|
|
98
104
|
}
|
|
99
105
|
function validateCronField(field, spec, fail) {
|
|
100
106
|
const unsupported = (item) => fail("schedule", `schedule.cron ${spec.name} field contains the unsupported value '${item}'; `
|