@cogover/sdk 0.11.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 +65 -21
- package/dist/crypto.js +89 -46
- package/dist/data.js +4 -1
- package/dist/email.d.ts +77 -0
- package/dist/email.js +192 -0
- package/dist/index.d.ts +2 -0
- 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/job-runtime.js +4 -0
- package/dist/internal/messaging.js +75 -0
- package/dist/internal/trigger-runtime.js +5 -0
- package/dist/internal/utf8.js +18 -17
- package/dist/jobs.d.ts +4 -0
- package/dist/locks.js +13 -3
- package/dist/logger.js +47 -1
- package/dist/notifications.d.ts +56 -0
- package/dist/notifications.js +92 -0
- package/dist/org.js +14 -14
- package/dist/records.d.ts +13 -0
- package/dist/response.js +21 -1
- package/dist/router.js +2 -0
- package/dist/schema.d.ts +1 -0
- package/dist/script.d.ts +4 -0
- package/dist/script.js +11 -3
- package/dist/trigger.d.ts +6 -0
- 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 +218 -19
- package/package.json +1 -1
- package/docs/en/api-reference.md +0 -2619
package/README.md
CHANGED
|
@@ -45,10 +45,10 @@ export default defineScript<Input, Output>(async ({ input, data, log }) => {
|
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
`defineScript` parses the input JSON, provides invocation metadata and scoped
|
|
48
|
-
data, schema, logging, state, lock, push, job, secret, and
|
|
49
|
-
the async handler, and serializes its result. Cogover
|
|
50
|
-
not exposed to project code, and Cogover enforces
|
|
51
|
-
limits on capability calls.
|
|
48
|
+
data, schema, logging, state, lock, push, notification, email, job, secret, and
|
|
49
|
+
crypto APIs, waits for the async handler, and serializes its result. Cogover
|
|
50
|
+
authentication credentials are not exposed to project code, and Cogover enforces
|
|
51
|
+
the configured permissions and limits on capability calls.
|
|
52
52
|
|
|
53
53
|
## Current identity and workspace
|
|
54
54
|
|
|
@@ -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
|
|
|
@@ -157,15 +158,15 @@ the trigger runs.
|
|
|
157
158
|
|
|
158
159
|
A handler receives up to 200 records per call, so read related data once with
|
|
159
160
|
`records.getMany` instead of once per record. Before-change triggers are read-only:
|
|
160
|
-
record writes, `fetch`, locks, state writes,
|
|
161
|
-
`PermissionDeniedError` (`details.reason === "TRIGGER_READ_ONLY"`).
|
|
162
|
-
acts as the user who made the change. A handler that throws or exceeds its
|
|
161
|
+
record writes, `fetch`, locks, state writes, push messages, notifications, and email
|
|
162
|
+
are refused with `PermissionDeniedError` (`details.reason === "TRIGGER_READ_ONLY"`).
|
|
163
|
+
`data.object()` acts as the user who made the change. A handler that throws or exceeds its
|
|
163
164
|
`timeoutMs` (default 2,000 ms) rejects the write.
|
|
164
165
|
|
|
165
166
|
An after-change trigger (`timing: "afterChange"`) is for follow-up work. It cannot
|
|
166
167
|
reject or modify the saved change (`addError` is only logged), but it may write
|
|
167
|
-
records
|
|
168
|
-
ID.
|
|
168
|
+
records, call `fetch`, and send notifications and email. `record.new` holds the
|
|
169
|
+
saved values, including the record ID.
|
|
169
170
|
|
|
170
171
|
```typescript
|
|
171
172
|
import { defineTrigger, fetch, RetryableError } from "@cogover/sdk";
|
|
@@ -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
|
|
|
@@ -244,9 +245,10 @@ export const jobs = [
|
|
|
244
245
|
|
|
245
246
|
A job handler receives `job` (run ID, key, attempt, source), the `payload` passed
|
|
246
247
|
to `enqueue` or `null`, and the same `data`, `schema`, `log`, `state`, `locks`,
|
|
247
|
-
`jobs`, `secrets`, `crypto`, and `org` APIs as a script.
|
|
248
|
-
and retried with growing delays when the handler
|
|
249
|
-
out, up to `maxAttempts` (default 5) attempts;
|
|
248
|
+
`notifications`, `email`, `jobs`, `secrets`, `crypto`, and `org` APIs as a script.
|
|
249
|
+
Runs are delivered at least once and retried with growing delays when the handler
|
|
250
|
+
throws `RetryableError` or times out, up to `maxAttempts` (default 5) attempts;
|
|
251
|
+
other errors fail the run. A run
|
|
250
252
|
enqueued from a route acts as that route's user, while a scheduled run uses the
|
|
251
253
|
system identity. `jobs.enqueue(key, payload?, { delayMs?, runAt?, idempotencyKey? })`
|
|
252
254
|
returns `{ runId, duplicate }`.
|
|
@@ -279,8 +281,9 @@ export default defineScript<Input>(async ({ input }) => {
|
|
|
279
281
|
});
|
|
280
282
|
```
|
|
281
283
|
|
|
282
|
-
V1 supports public HTTPS URLs on port 443
|
|
283
|
-
|
|
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,
|
|
284
287
|
cookie jars, streaming, WebSocket, URL credentials, IP-literal hosts, custom proxy,
|
|
285
288
|
dispatcher, agent, DNS, and TLS settings are not supported. A failed mutating request
|
|
286
289
|
may already have reached the remote API, so use the remote API's idempotency mechanism
|
|
@@ -469,7 +472,8 @@ export default defineScript(async ({ state }) => {
|
|
|
469
472
|
```
|
|
470
473
|
|
|
471
474
|
Distributed locks provide project-scoped token leases for short critical sections.
|
|
472
|
-
`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
|
|
473
477
|
fencing token. A lease does not auto-renew and locks do not replace idempotency or
|
|
474
478
|
provide exactly-once execution.
|
|
475
479
|
|
|
@@ -509,6 +513,46 @@ Delivery is best-effort, each call counts as one capability call, and before-cha
|
|
|
509
513
|
triggers cannot push. Invalid input throws `ValidationError`; an unknown object or
|
|
510
514
|
personnel throws `NotFoundError`.
|
|
511
515
|
|
|
516
|
+
## Notifications and email
|
|
517
|
+
|
|
518
|
+
`notifications` saves a notification in people's notification list and delivers it
|
|
519
|
+
in the web app, by web and mobile push, and as an email copy, as the chosen
|
|
520
|
+
notification channel of the workspace decides. `email` sends email from a shared
|
|
521
|
+
workspace mailbox or from the personal mailbox of the user who started the
|
|
522
|
+
execution, and can log it on a record's timeline.
|
|
523
|
+
|
|
524
|
+
```typescript
|
|
525
|
+
router.post("/quotes/:id/send", async ({ request, notifications, email }) => {
|
|
526
|
+
const quoteId = request.params.id;
|
|
527
|
+
const sent = await email.send({
|
|
528
|
+
from: { mailbox: "EMWXXXXXXXXXXXX" },
|
|
529
|
+
subject: "Your quote",
|
|
530
|
+
html: "<p>Please find our quote attached.</p>",
|
|
531
|
+
record: { object: "quote", recordId: quoteId },
|
|
532
|
+
recordEmailFields: ["contact_email"],
|
|
533
|
+
attachments: [{ object: "quote", recordId: quoteId, field: "quote_pdf" }],
|
|
534
|
+
idempotencyKey: `quote-email:${quoteId}`,
|
|
535
|
+
});
|
|
536
|
+
await notifications.send({
|
|
537
|
+
to: ["PERXXXXXXXXXXXX"],
|
|
538
|
+
title: "Quote sent to the customer",
|
|
539
|
+
content: "The quote was emailed and logged on its timeline.",
|
|
540
|
+
link: { url: `/app/quote/${quoteId}` },
|
|
541
|
+
idempotencyKey: `quote-sent:${quoteId}`,
|
|
542
|
+
});
|
|
543
|
+
return { requestId: sent.requestId };
|
|
544
|
+
});
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
Both are available in scripts, routes, after-change triggers, and jobs, and both are
|
|
548
|
+
asynchronous and best-effort: a successful call means Cogover accepted the message.
|
|
549
|
+
`idempotencyKey` makes a retry within 7 days return the first result with
|
|
550
|
+
`duplicate: true` instead of sending again. Notifications need no approval; email
|
|
551
|
+
may only be sent from the mailboxes allowed by the `email` section of the Project's
|
|
552
|
+
approved identity policy, otherwise `PermissionDeniedError` is thrown with
|
|
553
|
+
`details.reason === "EMAIL_SENDER_NOT_GRANTED"`. `email.senders()` lists the
|
|
554
|
+
mailboxes the current execution may use.
|
|
555
|
+
|
|
512
556
|
## Organization structure
|
|
513
557
|
|
|
514
558
|
`org` reads the departments, positions, and personnel of the workspace, and the
|
|
@@ -572,8 +616,8 @@ Recommended `tsconfig.json` options:
|
|
|
572
616
|
## Additional documentation
|
|
573
617
|
|
|
574
618
|
The installed package includes an English usage guide at
|
|
575
|
-
`docs/en/usage-guide.md` and the complete API reference
|
|
576
|
-
`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`.
|
|
577
621
|
|
|
578
622
|
## License
|
|
579
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.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { EffectiveWorkspaceObjects, StringKeyOf } from "./workspace.js";
|
|
2
|
+
/**
|
|
3
|
+
* Mailbox an email is sent from: a shared workspace mailbox by ID, or `"actor"` for the default personal mailbox
|
|
4
|
+
* of the user whose action started this execution. The Project policy must allow the mailbox.
|
|
5
|
+
*/
|
|
6
|
+
export type EmailSender = {
|
|
7
|
+
readonly mailbox: string;
|
|
8
|
+
} | "actor";
|
|
9
|
+
/** An address, an address with a display name, or a personnel whose work email is used. */
|
|
10
|
+
export type EmailRecipient = string | {
|
|
11
|
+
readonly email: string;
|
|
12
|
+
readonly name?: string;
|
|
13
|
+
} | {
|
|
14
|
+
readonly personnelId: string;
|
|
15
|
+
};
|
|
16
|
+
/** Record on whose timeline the email is logged as an activity. */
|
|
17
|
+
export interface EmailRecordLink<TObject extends string = string> {
|
|
18
|
+
readonly object: TObject;
|
|
19
|
+
readonly recordId: string;
|
|
20
|
+
}
|
|
21
|
+
/** Every file stored in a file field of a record. */
|
|
22
|
+
export interface EmailAttachment<TObject extends string = string> {
|
|
23
|
+
readonly object: TObject;
|
|
24
|
+
readonly recordId: string;
|
|
25
|
+
readonly field: string;
|
|
26
|
+
}
|
|
27
|
+
interface EmailMessageFields<TObject extends string> {
|
|
28
|
+
readonly from: EmailSender;
|
|
29
|
+
readonly to?: readonly EmailRecipient[];
|
|
30
|
+
readonly cc?: readonly EmailRecipient[];
|
|
31
|
+
readonly bcc?: readonly EmailRecipient[];
|
|
32
|
+
/** A single line of at most 255 characters. */
|
|
33
|
+
readonly subject: string;
|
|
34
|
+
/** Logs the email on this record's timeline and enables the options below. */
|
|
35
|
+
readonly record?: EmailRecordLink<TObject>;
|
|
36
|
+
/** Email fields of `record` whose addresses are added to `to`. */
|
|
37
|
+
readonly recordEmailFields?: readonly string[];
|
|
38
|
+
/** Files to attach, at most 10 files and 20 MB in total; requires `record`. */
|
|
39
|
+
readonly attachments?: readonly EmailAttachment<TObject>[];
|
|
40
|
+
/** Appends the sender's default email signature; requires `record` and a personal mailbox. */
|
|
41
|
+
readonly appendSignature?: boolean;
|
|
42
|
+
/** A retry with the same key returns the first result instead of sending again. */
|
|
43
|
+
readonly idempotencyKey?: string;
|
|
44
|
+
}
|
|
45
|
+
/** An email with either an HTML or a plain-text body of at most 192 KB. */
|
|
46
|
+
export type EmailMessage<TObject extends string = string> = EmailMessageFields<TObject> & ({
|
|
47
|
+
readonly html: string;
|
|
48
|
+
readonly text?: never;
|
|
49
|
+
} | {
|
|
50
|
+
readonly text: string;
|
|
51
|
+
readonly html?: never;
|
|
52
|
+
});
|
|
53
|
+
/** `"activity"` when the email was logged on a record, `"direct"` otherwise. */
|
|
54
|
+
export type EmailDelivery = "activity" | "direct";
|
|
55
|
+
export interface EmailSendResult {
|
|
56
|
+
readonly requestId: string;
|
|
57
|
+
readonly delivery: EmailDelivery;
|
|
58
|
+
/** Addresses in to, cc and bcc after duplicates were removed; record email fields are resolved later. */
|
|
59
|
+
readonly recipients: number;
|
|
60
|
+
/** `true` when an earlier call with the same `idempotencyKey` already sent it. */
|
|
61
|
+
readonly duplicate: boolean;
|
|
62
|
+
}
|
|
63
|
+
/** `"workspace"` senders are used as `{ mailbox: id }`; the `"actor"` sender is the mailbox `from: "actor"` uses. */
|
|
64
|
+
export type EmailSenderKind = "workspace" | "actor";
|
|
65
|
+
export interface EmailSenderInfo {
|
|
66
|
+
readonly id: string;
|
|
67
|
+
readonly kind: EmailSenderKind;
|
|
68
|
+
readonly email: string;
|
|
69
|
+
readonly displayName: string;
|
|
70
|
+
}
|
|
71
|
+
/** Sends email from workspace mailboxes. Sending is asynchronous: success means the email was accepted. */
|
|
72
|
+
export interface EmailApi<TSchema extends object = EffectiveWorkspaceObjects> {
|
|
73
|
+
send(message: EmailMessage<StringKeyOf<TSchema>>): Promise<EmailSendResult>;
|
|
74
|
+
/** Active mailboxes this execution may send from. */
|
|
75
|
+
senders(): Promise<readonly EmailSenderInfo[]>;
|
|
76
|
+
}
|
|
77
|
+
export {};
|