@composable-os/sdk 0.1.0-beta.7 → 0.1.0-beta.9
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/dist/allowance.d.ts +192 -0
- package/dist/allowance.js +365 -0
- package/dist/capacity.d.ts +26 -0
- package/dist/capacity.js +59 -0
- package/dist/contract.d.ts +92 -1
- package/dist/document.d.ts +52 -6
- package/dist/document.js +101 -11
- package/dist/field-types.d.ts +1 -1
- package/dist/field-types.js +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/manifest.d.ts +356 -17
- package/dist/manifest.js +179 -5
- package/dist/media.d.ts +32 -0
- package/dist/media.js +47 -0
- package/dist/recurring.d.ts +114 -0
- package/dist/recurring.js +166 -0
- package/dist/rich-text.d.ts +16 -2
- package/dist/rich-text.js +50 -4
- package/dist/sellable.d.ts +62 -0
- package/dist/sellable.js +70 -0
- package/dist/states.d.ts +94 -2
- package/dist/states.js +109 -1
- package/dist/unique.d.ts +20 -0
- package/dist/unique.js +58 -0
- package/package.json +1 -1
- package/src/allowance.ts +508 -0
- package/src/capacity.ts +89 -0
- package/src/contract.ts +97 -1
- package/src/document.ts +161 -14
- package/src/field-types.ts +5 -0
- package/src/index.ts +3 -0
- package/src/manifest.ts +206 -5
- package/src/media.ts +62 -0
- package/src/recurring.ts +314 -0
- package/src/rich-text.ts +52 -4
- package/src/sellable.ts +120 -0
- package/src/states.ts +162 -1
- package/src/unique.ts +81 -0
package/dist/manifest.js
CHANGED
|
@@ -3,17 +3,20 @@
|
|
|
3
3
|
// diffs and reconciles the manifest. `validateManifest` is the contract check `extension.check`
|
|
4
4
|
// runs; it returns structured issues a model can act on.
|
|
5
5
|
import { z } from "zod";
|
|
6
|
+
import { allowanceManifestSchema, checkObjectAllowance } from "./allowance.js";
|
|
6
7
|
import { automationManifestSchema } from "./automation.js";
|
|
7
8
|
import { checkAutomation } from "./automation-check.js";
|
|
8
9
|
import { checkObjectBehaviour } from "./behaviour.js";
|
|
9
|
-
import { capacityManifestSchema, checkObjectCapacity } from "./capacity.js";
|
|
10
|
+
import { capacityManifestSchema, checkObjectCapacity, checkObjectClaims, claimsManifestSchema, } from "./capacity.js";
|
|
10
11
|
import { HOST_MODULES, STAGE_TYPES, SUPPORTED_CONTRACT_VERSIONS, TOOL_AUDIENCES, TOOL_RISKS, } from "./contract.js";
|
|
11
12
|
import { checkDocumentSchema, DOCUMENT_MAX_BYTES, DOCUMENT_MAX_PATHS, documentPathNode, } from "./document.js";
|
|
12
13
|
import { FIELD_TYPES, formulaTypeOf } from "./field-types.js";
|
|
13
14
|
import { checkFormula } from "./formula/compile.js";
|
|
15
|
+
import { checkObjectRecurring, recurringManifestSchema } from "./recurring.js";
|
|
14
16
|
import { renamedMessage } from "./renamed.js";
|
|
15
17
|
import { checkObjectSellable, sellableManifestSchema } from "./sellable.js";
|
|
16
18
|
import { checkObjectStates, statesManifestSchema } from "./states.js";
|
|
19
|
+
import { checkObjectUnique, uniqueManifestSchema } from "./unique.js";
|
|
17
20
|
export const EXTENSION_ID_RE = /^[a-z][a-z0-9]{1,29}$/;
|
|
18
21
|
export const OBJECT_NAME_RE = /^[a-z][a-z0-9]{0,29}$/;
|
|
19
22
|
export const FIELD_NAME_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
@@ -164,13 +167,18 @@ export const objectManifestSchema = z.object({
|
|
|
164
167
|
stages: stagesManifestSchema.optional(),
|
|
165
168
|
capacity: capacityManifestSchema.optional(),
|
|
166
169
|
sellable: sellableManifestSchema.optional(),
|
|
170
|
+
allowance: allowanceManifestSchema.optional(),
|
|
167
171
|
states: statesManifestSchema.optional(),
|
|
172
|
+
claims: claimsManifestSchema.optional(),
|
|
173
|
+
unique: uniqueManifestSchema.optional(),
|
|
174
|
+
recurring: recurringManifestSchema.optional(),
|
|
168
175
|
owner: z.string().max(40).optional(),
|
|
169
176
|
customer: z
|
|
170
177
|
.object({
|
|
171
178
|
field: z.string().max(40),
|
|
172
179
|
read: z.array(z.string().max(40)).max(100),
|
|
173
180
|
write: z.array(z.string().max(40)).max(100),
|
|
181
|
+
requireAccess: z.array(z.string().max(40)).max(10).optional(),
|
|
174
182
|
})
|
|
175
183
|
.strict()
|
|
176
184
|
.optional(),
|
|
@@ -184,6 +192,38 @@ export const objectManifestSchema = z.object({
|
|
|
184
192
|
})
|
|
185
193
|
.strict()
|
|
186
194
|
.optional(),
|
|
195
|
+
access: z
|
|
196
|
+
.object({
|
|
197
|
+
sources: z
|
|
198
|
+
.array(z.union([
|
|
199
|
+
z
|
|
200
|
+
.object({
|
|
201
|
+
via: z.string().max(81),
|
|
202
|
+
field: z.string().max(40),
|
|
203
|
+
through: z.string().max(40).optional(),
|
|
204
|
+
states: z.array(z.string().max(60)).min(1).max(20).optional(),
|
|
205
|
+
until: z.string().max(40).optional(),
|
|
206
|
+
})
|
|
207
|
+
.strict(),
|
|
208
|
+
z
|
|
209
|
+
.object({
|
|
210
|
+
kind: z.string().regex(/^[a-z][a-zA-Z0-9]{0,39}$/),
|
|
211
|
+
options: z
|
|
212
|
+
.record(z.string().max(40), z.union([
|
|
213
|
+
z.string().max(200),
|
|
214
|
+
z.number(),
|
|
215
|
+
z.boolean(),
|
|
216
|
+
z.array(z.string().max(200)).max(20),
|
|
217
|
+
]))
|
|
218
|
+
.optional(),
|
|
219
|
+
})
|
|
220
|
+
.strict(),
|
|
221
|
+
]))
|
|
222
|
+
.min(1)
|
|
223
|
+
.max(8),
|
|
224
|
+
})
|
|
225
|
+
.strict()
|
|
226
|
+
.optional(),
|
|
187
227
|
publishable: z
|
|
188
228
|
.object({
|
|
189
229
|
content: z.array(z.string().max(40)).min(1).max(40),
|
|
@@ -331,6 +371,10 @@ export const manifestSchema = z.object({
|
|
|
331
371
|
contractVersion: z.number().int(),
|
|
332
372
|
label: z.string().max(100).optional(),
|
|
333
373
|
description: z.string().max(2000).optional(),
|
|
374
|
+
requires: z
|
|
375
|
+
.array(z.string().regex(/^[a-z][a-z0-9]*$/))
|
|
376
|
+
.max(10)
|
|
377
|
+
.optional(),
|
|
334
378
|
permissions: permissionsManifestSchema.default({
|
|
335
379
|
read: [],
|
|
336
380
|
write: [],
|
|
@@ -356,14 +400,14 @@ export const manifestSchema = z.object({
|
|
|
356
400
|
* may not repeat.
|
|
357
401
|
*/
|
|
358
402
|
function checkDocumentField(path, name, field, fields, known, contract) {
|
|
359
|
-
if (field.type === "image") {
|
|
403
|
+
if (field.type === "image" || field.type === "video") {
|
|
360
404
|
for (const key of ["schema", "maxBytes", "paths"]) {
|
|
361
405
|
if (field[key] !== undefined) {
|
|
362
|
-
contract(`${path}.${key}`, `An
|
|
406
|
+
contract(`${path}.${key}`, `An ${field.type} field has the platform's schema and no ${key}`);
|
|
363
407
|
}
|
|
364
408
|
}
|
|
365
409
|
if (field.track)
|
|
366
|
-
contract(`${path}.track`,
|
|
410
|
+
contract(`${path}.track`, `An ${field.type} is not tracked on the timeline`);
|
|
367
411
|
return;
|
|
368
412
|
}
|
|
369
413
|
if (field.type === "richText") {
|
|
@@ -395,7 +439,8 @@ function checkDocumentField(path, name, field, fields, known, contract) {
|
|
|
395
439
|
contract(`${path}.schema`, "A document field declares its schema");
|
|
396
440
|
return;
|
|
397
441
|
}
|
|
398
|
-
|
|
442
|
+
// A document's refs may also name the media library (stage 42, ADR 0475): a lesson's video.
|
|
443
|
+
const problems = checkDocumentSchema(field.schema, new Set([...known, "media"]));
|
|
399
444
|
for (const [at, message] of problems)
|
|
400
445
|
contract(`${path}.schema${at ? `.${at}` : ""}`, message);
|
|
401
446
|
if (field.maxBytes !== undefined &&
|
|
@@ -508,6 +553,7 @@ export function toManifest(extension) {
|
|
|
508
553
|
},
|
|
509
554
|
])),
|
|
510
555
|
...(extension.grantable ? { grantable: [...extension.grantable] } : {}),
|
|
556
|
+
...(extension.requires?.length ? { requires: [...extension.requires] } : {}),
|
|
511
557
|
roles: Object.fromEntries(Object.entries(extension.roles ?? {}).map(([name, role]) => [
|
|
512
558
|
name,
|
|
513
559
|
{
|
|
@@ -770,7 +816,11 @@ export function validateManifest(input, options = {}) {
|
|
|
770
816
|
checkObjectBehaviour(m, name, object, contract);
|
|
771
817
|
checkObjectCapacity(m, name, object, contract);
|
|
772
818
|
checkObjectSellable(m, name, object, contract);
|
|
819
|
+
checkObjectAllowance(m, name, object, contract);
|
|
773
820
|
checkObjectStates(m, name, object, contract);
|
|
821
|
+
checkObjectClaims(m, name, object, contract);
|
|
822
|
+
checkObjectUnique(m, name, object, contract);
|
|
823
|
+
checkObjectRecurring(m, name, object, contract);
|
|
774
824
|
object.requireOneOf?.forEach((group, i) => {
|
|
775
825
|
for (const field of group) {
|
|
776
826
|
if (!object.fields[field]) {
|
|
@@ -1003,6 +1053,130 @@ export function validateManifest(input, options = {}) {
|
|
|
1003
1053
|
contract(`${path}.field`, `customerVia.field "${v.field}" must be one ${local}'s customer may read (customer.read)`);
|
|
1004
1054
|
}
|
|
1005
1055
|
}
|
|
1056
|
+
// The extensions this one needs installed first (stage 42): never itself, each once.
|
|
1057
|
+
for (const [i, required] of (m.requires ?? []).entries()) {
|
|
1058
|
+
if (required === m.id)
|
|
1059
|
+
contract(`requires.${i}`, "An extension does not require itself");
|
|
1060
|
+
if ((m.requires ?? []).indexOf(required) !== i) {
|
|
1061
|
+
contract(`requires.${i}`, `"${required}" is listed twice`);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
// A customer's write that needs access to what it points to (stage 42, ADR 0478): relations of
|
|
1065
|
+
// the object to one record of another of this extension's objects that declares `access`.
|
|
1066
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1067
|
+
for (const fieldName of object.customer?.requireAccess ?? []) {
|
|
1068
|
+
const path = `objects.${name}.customer.requireAccess`;
|
|
1069
|
+
const field = object.fields[fieldName];
|
|
1070
|
+
const local = field?.to?.startsWith(`${m.id}.`) ? field.to.slice(m.id.length + 1) : null;
|
|
1071
|
+
const target = local ? m.objects[local] : undefined;
|
|
1072
|
+
if (field?.type !== "relation" || field.many || !target) {
|
|
1073
|
+
contract(path, `"${fieldName}" must be a relation of ${name} to one of this extension's objects`);
|
|
1074
|
+
}
|
|
1075
|
+
else if (!target.access) {
|
|
1076
|
+
contract(path, `"${fieldName}" points to ${local}, which declares no access`);
|
|
1077
|
+
}
|
|
1078
|
+
else if (!object.customer?.read.includes(fieldName)) {
|
|
1079
|
+
contract(path, `"${fieldName}" must be one the customer reads (customer.read)`);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
// Who holds access to a record (ADR 0477): sources of this extension's own records. A kind the
|
|
1084
|
+
// host provides is checked by the host, which knows its kinds; the subscription kind's options
|
|
1085
|
+
// (stage 32a, ADR 0488) are checked here too, as they name this extension's own plan object.
|
|
1086
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1087
|
+
const a = object.access;
|
|
1088
|
+
if (!a)
|
|
1089
|
+
continue;
|
|
1090
|
+
a.sources.forEach((given, i) => {
|
|
1091
|
+
const path = `objects.${name}.access.sources.${i}`;
|
|
1092
|
+
if ("kind" in given && given.kind === "subscription") {
|
|
1093
|
+
const options = (given.options ?? {});
|
|
1094
|
+
for (const key of Object.keys(options)) {
|
|
1095
|
+
if (key !== "plans" && key !== "covers") {
|
|
1096
|
+
contract(`${path}.options.${key}`, `a subscription source has no option "${key}"`);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
if (options.covers !== "all") {
|
|
1100
|
+
contract(`${path}.options.covers`, 'covers must be "all": every live record');
|
|
1101
|
+
}
|
|
1102
|
+
const plans = typeof options.plans === "string" ? options.plans : "";
|
|
1103
|
+
const planLocal = plans.startsWith(`${m.id}.`) ? plans.slice(m.id.length + 1) : null;
|
|
1104
|
+
const plan = planLocal && planLocal !== name ? m.objects[planLocal] : undefined;
|
|
1105
|
+
if (!plan) {
|
|
1106
|
+
contract(`${path}.options.plans`, `plans must name another of this extension's objects, as ${m.id}.<object>`);
|
|
1107
|
+
}
|
|
1108
|
+
else if (!plan.sellable?.recurring) {
|
|
1109
|
+
contract(`${path}.options.plans`, "plans must name a plan: its sellable declares recurring");
|
|
1110
|
+
}
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
if (!("via" in given))
|
|
1114
|
+
return;
|
|
1115
|
+
const source = given;
|
|
1116
|
+
const local = source.via.startsWith(`${m.id}.`) ? source.via.slice(m.id.length + 1) : null;
|
|
1117
|
+
const via = local ? m.objects[local] : undefined;
|
|
1118
|
+
if (!via || local === name) {
|
|
1119
|
+
contract(`${path}.via`, `via must name another of this extension's objects, as ${m.id}.<object>`);
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
if (!via.customer) {
|
|
1123
|
+
contract(`${path}.via`, "via must name an object that declares a customer");
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
const pointer = via.fields[source.field];
|
|
1127
|
+
if (pointer?.type !== "relation" || pointer.many || !pointer.to) {
|
|
1128
|
+
contract(`${path}.field`, `field must name a relation of ${local} to one record`);
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
if (source.through === undefined) {
|
|
1132
|
+
if (pointer.to !== `${m.id}.${name}`) {
|
|
1133
|
+
contract(`${path}.field`, `field must be a relation of ${local} to one ${name}`);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
else {
|
|
1137
|
+
const targetLocal = pointer.to.startsWith(`${m.id}.`)
|
|
1138
|
+
? pointer.to.slice(m.id.length + 1)
|
|
1139
|
+
: null;
|
|
1140
|
+
const target = targetLocal ? m.objects[targetLocal] : undefined;
|
|
1141
|
+
const list = target?.fields[source.through];
|
|
1142
|
+
const lists = (list?.type === "relation" && list.many && list.to === `${m.id}.${name}`) ||
|
|
1143
|
+
list?.type === "document";
|
|
1144
|
+
if (!target || !lists) {
|
|
1145
|
+
contract(`${path}.through`, `through must name a many relation to ${name}, or a document whose refs name it, on the object field points to`);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
if (via.states) {
|
|
1149
|
+
const stateField = via.fields[via.states.field];
|
|
1150
|
+
const known = new Set((stateField?.options ?? []).map((o) => o.value));
|
|
1151
|
+
// What a refund moves a grant of `via` to: never a state that gives access.
|
|
1152
|
+
const voids = new Set(Object.values(m.objects).flatMap((o) => {
|
|
1153
|
+
const g = o.sellable?.grants;
|
|
1154
|
+
return g && (g.object === local || g.object === source.via) ? [g.void] : [];
|
|
1155
|
+
}));
|
|
1156
|
+
if (!source.states?.length) {
|
|
1157
|
+
contract(`${path}.states`, `${local} has states: name the ones that give access`);
|
|
1158
|
+
}
|
|
1159
|
+
else {
|
|
1160
|
+
for (const st of source.states) {
|
|
1161
|
+
if (!known.has(st))
|
|
1162
|
+
contract(`${path}.states`, `"${st}" is not a state of ${local}`);
|
|
1163
|
+
if (voids.has(st)) {
|
|
1164
|
+
contract(`${path}.states`, `"${st}" is where a refund moves ${local}: it gives no access`);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
else if (source.states !== undefined) {
|
|
1170
|
+
contract(`${path}.states`, `${local} has no states`);
|
|
1171
|
+
}
|
|
1172
|
+
if (source.until !== undefined) {
|
|
1173
|
+
const until = via.fields[source.until];
|
|
1174
|
+
if (until?.type !== "date" && until?.type !== "datetime") {
|
|
1175
|
+
contract(`${path}.until`, `until must name a date or datetime field of ${local}`);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1006
1180
|
for (const [target, fields] of Object.entries(m.fields)) {
|
|
1007
1181
|
const path = `fields.${target}`;
|
|
1008
1182
|
if (!known.has(target)) {
|
package/dist/media.d.ts
CHANGED
|
@@ -7,3 +7,35 @@ export declare const MEDIA_MAX_BYTES: number;
|
|
|
7
7
|
export declare function sniffMediaType(bytes: Uint8Array): MediaType | null;
|
|
8
8
|
/** Why `bytes` cannot be uploaded, or null when they can. */
|
|
9
9
|
export declare function mediaProblem(bytes: Uint8Array): string | null;
|
|
10
|
+
/** The kinds of item the media library holds. */
|
|
11
|
+
export declare const MEDIA_KINDS: readonly ["image", "video", "file"];
|
|
12
|
+
export type MediaKind = (typeof MEDIA_KINDS)[number];
|
|
13
|
+
/** The types a `file` item may be. */
|
|
14
|
+
export declare const MEDIA_FILE_TYPES: readonly ["application/pdf"];
|
|
15
|
+
export type MediaFileType = (typeof MEDIA_FILE_TYPES)[number];
|
|
16
|
+
/** The `type` the library records for a video (its bytes are Stream's, never the host's). */
|
|
17
|
+
export declare const MEDIA_VIDEO_TYPE: "video/stream";
|
|
18
|
+
/** The most a downloadable file may be, in bytes. */
|
|
19
|
+
export declare const MEDIA_FILE_MAX_BYTES: number;
|
|
20
|
+
/** The most a video may be, in bytes: under Stream's 30 GB. */
|
|
21
|
+
export declare const MEDIA_VIDEO_MAX_BYTES: number;
|
|
22
|
+
/** The longest a video may be, in seconds (Stream refuses an upload longer than it was told). */
|
|
23
|
+
export declare const MEDIA_VIDEO_MAX_SECONDS: number;
|
|
24
|
+
/** The most a caption file may be (Stream's limit): WebVTT only. */
|
|
25
|
+
export declare const MEDIA_CAPTION_MAX_BYTES: number;
|
|
26
|
+
/** The type a downloadable file's bytes are, or null when the library does not take it. */
|
|
27
|
+
export declare function sniffFileType(bytes: Uint8Array): MediaFileType | null;
|
|
28
|
+
/** The kind and type an uploaded file is, from its bytes: a picture or a download. */
|
|
29
|
+
export declare function sniffUpload(bytes: Uint8Array): {
|
|
30
|
+
kind: "image";
|
|
31
|
+
type: MediaType;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "file";
|
|
34
|
+
type: MediaFileType;
|
|
35
|
+
} | null;
|
|
36
|
+
/** The most an upload of this kind may be. */
|
|
37
|
+
export declare function maxBytesOf(kind: "image" | "file"): number;
|
|
38
|
+
/** Whether `bytes` are a WebVTT file (its header, after an optional byte order mark). */
|
|
39
|
+
export declare function isWebVtt(bytes: Uint8Array): boolean;
|
|
40
|
+
/** A caption's language: a BCP 47 tag as Stream takes it (`en`, `pt-BR`). */
|
|
41
|
+
export declare const CAPTION_LANGUAGE_RE: RegExp;
|
package/dist/media.js
CHANGED
|
@@ -41,3 +41,50 @@ export function mediaProblem(bytes) {
|
|
|
41
41
|
}
|
|
42
42
|
return null;
|
|
43
43
|
}
|
|
44
|
+
// --- Files and videos (stage 42, ADR 0475) ------------------------------------------------------
|
|
45
|
+
//
|
|
46
|
+
// A `file` item is a download (a PDF only, at first), kept in R2 like a picture and served by the
|
|
47
|
+
// same rule. Its type is read from its bytes too. A `video` item is held in Cloudflare Stream on
|
|
48
|
+
// the deployment's own account: the file goes from the staff browser straight to Stream, so the
|
|
49
|
+
// host only checks the size a person says it is, and Stream refuses what is not a video.
|
|
50
|
+
/** The kinds of item the media library holds. */
|
|
51
|
+
export const MEDIA_KINDS = ["image", "video", "file"];
|
|
52
|
+
/** The types a `file` item may be. */
|
|
53
|
+
export const MEDIA_FILE_TYPES = ["application/pdf"];
|
|
54
|
+
/** The `type` the library records for a video (its bytes are Stream's, never the host's). */
|
|
55
|
+
export const MEDIA_VIDEO_TYPE = "video/stream";
|
|
56
|
+
/** The most a downloadable file may be, in bytes. */
|
|
57
|
+
export const MEDIA_FILE_MAX_BYTES = 50 * 1024 * 1024;
|
|
58
|
+
/** The most a video may be, in bytes: under Stream's 30 GB. */
|
|
59
|
+
export const MEDIA_VIDEO_MAX_BYTES = 30 * 1000 * 1000 * 1000 - 1;
|
|
60
|
+
/** The longest a video may be, in seconds (Stream refuses an upload longer than it was told). */
|
|
61
|
+
export const MEDIA_VIDEO_MAX_SECONDS = 6 * 60 * 60;
|
|
62
|
+
/** The most a caption file may be (Stream's limit): WebVTT only. */
|
|
63
|
+
export const MEDIA_CAPTION_MAX_BYTES = 10 * 1024 * 1024;
|
|
64
|
+
/** The type a downloadable file's bytes are, or null when the library does not take it. */
|
|
65
|
+
export function sniffFileType(bytes) {
|
|
66
|
+
return ascii(bytes, 0, "%PDF-") ? "application/pdf" : null;
|
|
67
|
+
}
|
|
68
|
+
/** The kind and type an uploaded file is, from its bytes: a picture or a download. */
|
|
69
|
+
export function sniffUpload(bytes) {
|
|
70
|
+
const image = sniffMediaType(bytes);
|
|
71
|
+
if (image)
|
|
72
|
+
return { kind: "image", type: image };
|
|
73
|
+
const file = sniffFileType(bytes);
|
|
74
|
+
return file ? { kind: "file", type: file } : null;
|
|
75
|
+
}
|
|
76
|
+
/** The most an upload of this kind may be. */
|
|
77
|
+
export function maxBytesOf(kind) {
|
|
78
|
+
return kind === "file" ? MEDIA_FILE_MAX_BYTES : MEDIA_MAX_BYTES;
|
|
79
|
+
}
|
|
80
|
+
/** Whether `bytes` are a WebVTT file (its header, after an optional byte order mark). */
|
|
81
|
+
export function isWebVtt(bytes) {
|
|
82
|
+
const at = bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf ? 3 : 0;
|
|
83
|
+
if (!ascii(bytes, at, "WEBVTT"))
|
|
84
|
+
return false;
|
|
85
|
+
const next = bytes[at + 6];
|
|
86
|
+
// "WEBVTT" alone on its line, or followed by a space or a tab and a header.
|
|
87
|
+
return next === undefined || next === 0x0a || next === 0x0d || next === 0x20 || next === 0x09;
|
|
88
|
+
}
|
|
89
|
+
/** A caption's language: a BCP 47 tag as Stream takes it (`en`, `pt-BR`). */
|
|
90
|
+
export const CAPTION_LANGUAGE_RE = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2}$/;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
3
|
+
/** The weekday values a rule's `weekdays` field must use (ISO order, Monday first). */
|
|
4
|
+
export declare const WEEKDAYS: readonly ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
5
|
+
export type Weekday = (typeof WEEKDAYS)[number];
|
|
6
|
+
/** How far ahead the host makes records when a series does not say (days). */
|
|
7
|
+
export declare const RECURRING_HORIZON_DAYS = 56;
|
|
8
|
+
/** The most days ahead a series may ask for. */
|
|
9
|
+
export declare const RECURRING_HORIZON_MAX = 366;
|
|
10
|
+
/**
|
|
11
|
+
* A weekly schedule that generates dated records (ADR 0500). The rule is kept in local
|
|
12
|
+
* wall-clock time in the series' zone: 06:00 is 06:00 on both sides of a clock change. A local
|
|
13
|
+
* time that does not exist on a change day (02:30 when clocks go forward) moves forward by the
|
|
14
|
+
* gap; one that happens twice is the first of the two.
|
|
15
|
+
*
|
|
16
|
+
* - `generates` is another of this extension's objects, and `link` its relation field (one
|
|
17
|
+
* record, read-only) back to this object: the host sets it.
|
|
18
|
+
* - `rule` names this object's fields: `weekdays` (a multiSelect whose option values are
|
|
19
|
+
* `mon`…`sun`), `time` (a text field, `HH:MM`), `length` (a number field, minutes), `from` and
|
|
20
|
+
* `until` (date fields; `until` empty for ongoing) and `zone` (a timezone field; empty is the
|
|
21
|
+
* organisation's zone).
|
|
22
|
+
* - `into` names the generated object's fields the host fills: `starts` (datetime), `ends`
|
|
23
|
+
* (datetime, starts plus the length), `zone` (timezone) and `changed` (a read-only boolean the
|
|
24
|
+
* host sets when someone other than the host edits the record).
|
|
25
|
+
* - `copy` maps generated fields to this object's fields copied onto each record (`{ coach:
|
|
26
|
+
* "coach" }`).
|
|
27
|
+
* - `cancel` is the generated object's state the host moves a record to when a closure covers it
|
|
28
|
+
* or the series ends while places are claimed on it. The generated object declares `states`
|
|
29
|
+
* with a `host` move into it.
|
|
30
|
+
* - `ended` (optional) is this object's select field and the values that mean the series has
|
|
31
|
+
* ended: nothing more is made, and its future records go.
|
|
32
|
+
* - `closures` (optional) is another of this extension's objects whose records are date ranges
|
|
33
|
+
* (`from`, `until`, both date fields, inclusive) in which nothing is made and records already
|
|
34
|
+
* made are cancelled.
|
|
35
|
+
* - `horizonDays` (default 56): how far ahead records are made.
|
|
36
|
+
* - `horizonAtLeast` (optional): another of this extension's objects and a number field of it in
|
|
37
|
+
* days (a class type's booking window): the horizon is never less than the largest value any
|
|
38
|
+
* live record of it holds, so a class can be booked as soon as its window opens.
|
|
39
|
+
*
|
|
40
|
+
* Made records are never made twice (the host keys each on the series and its local date). A
|
|
41
|
+
* record edited by staff (its time, copied fields or state) is never overwritten or made again;
|
|
42
|
+
* a change of the series applies to the future records nobody edited.
|
|
43
|
+
*/
|
|
44
|
+
export interface RecurringScheduleSpec {
|
|
45
|
+
generates: string;
|
|
46
|
+
link: string;
|
|
47
|
+
rule: {
|
|
48
|
+
weekdays: string;
|
|
49
|
+
time: string;
|
|
50
|
+
length?: string;
|
|
51
|
+
from: string;
|
|
52
|
+
until?: string;
|
|
53
|
+
zone?: string;
|
|
54
|
+
};
|
|
55
|
+
into: {
|
|
56
|
+
starts: string;
|
|
57
|
+
ends?: string;
|
|
58
|
+
zone?: string;
|
|
59
|
+
changed?: string;
|
|
60
|
+
};
|
|
61
|
+
copy?: Readonly<Record<string, string>>;
|
|
62
|
+
cancel: string;
|
|
63
|
+
ended?: {
|
|
64
|
+
field: string;
|
|
65
|
+
in: readonly string[];
|
|
66
|
+
};
|
|
67
|
+
closures?: {
|
|
68
|
+
object: string;
|
|
69
|
+
from: string;
|
|
70
|
+
until: string;
|
|
71
|
+
};
|
|
72
|
+
horizonDays?: number;
|
|
73
|
+
horizonAtLeast?: {
|
|
74
|
+
object: string;
|
|
75
|
+
field: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export declare const recurringManifestSchema: z.ZodObject<{
|
|
79
|
+
generates: z.ZodString;
|
|
80
|
+
link: z.ZodString;
|
|
81
|
+
rule: z.ZodObject<{
|
|
82
|
+
weekdays: z.ZodString;
|
|
83
|
+
time: z.ZodString;
|
|
84
|
+
length: z.ZodOptional<z.ZodString>;
|
|
85
|
+
from: z.ZodString;
|
|
86
|
+
until: z.ZodOptional<z.ZodString>;
|
|
87
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
88
|
+
}, z.core.$strict>;
|
|
89
|
+
into: z.ZodObject<{
|
|
90
|
+
starts: z.ZodString;
|
|
91
|
+
ends: z.ZodOptional<z.ZodString>;
|
|
92
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
93
|
+
changed: z.ZodOptional<z.ZodString>;
|
|
94
|
+
}, z.core.$strict>;
|
|
95
|
+
copy: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
96
|
+
cancel: z.ZodString;
|
|
97
|
+
ended: z.ZodOptional<z.ZodObject<{
|
|
98
|
+
field: z.ZodString;
|
|
99
|
+
in: z.ZodArray<z.ZodString>;
|
|
100
|
+
}, z.core.$strict>>;
|
|
101
|
+
closures: z.ZodOptional<z.ZodObject<{
|
|
102
|
+
object: z.ZodString;
|
|
103
|
+
from: z.ZodString;
|
|
104
|
+
until: z.ZodString;
|
|
105
|
+
}, z.core.$strict>>;
|
|
106
|
+
horizonDays: z.ZodOptional<z.ZodNumber>;
|
|
107
|
+
horizonAtLeast: z.ZodOptional<z.ZodObject<{
|
|
108
|
+
object: z.ZodString;
|
|
109
|
+
field: z.ZodString;
|
|
110
|
+
}, z.core.$strict>>;
|
|
111
|
+
}, z.core.$strict>;
|
|
112
|
+
export type RecurringScheduleManifest = z.infer<typeof recurringManifestSchema>;
|
|
113
|
+
/** Reports problems with `objects.<name>.recurring` through `contract`. */
|
|
114
|
+
export declare function checkObjectRecurring(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// The `recurring` behaviour (stage 31, ADR 0500): a record (a series: one line of a timetable)
|
|
2
|
+
// declares a weekly rule in local wall-clock time, and the host makes the dated records it
|
|
3
|
+
// generates (its classes) ahead of time. Its declaration, its manifest schema and its contract
|
|
4
|
+
// checks live here; the host's side is packages/core/src/recurring/.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
/** The weekday values a rule's `weekdays` field must use (ISO order, Monday first). */
|
|
7
|
+
export const WEEKDAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
8
|
+
/** How far ahead the host makes records when a series does not say (days). */
|
|
9
|
+
export const RECURRING_HORIZON_DAYS = 56;
|
|
10
|
+
/** The most days ahead a series may ask for. */
|
|
11
|
+
export const RECURRING_HORIZON_MAX = 366;
|
|
12
|
+
const fieldName = z.string().max(40);
|
|
13
|
+
export const recurringManifestSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
generates: fieldName,
|
|
16
|
+
link: fieldName,
|
|
17
|
+
rule: z
|
|
18
|
+
.object({
|
|
19
|
+
weekdays: fieldName,
|
|
20
|
+
time: fieldName,
|
|
21
|
+
length: fieldName.optional(),
|
|
22
|
+
from: fieldName,
|
|
23
|
+
until: fieldName.optional(),
|
|
24
|
+
zone: fieldName.optional(),
|
|
25
|
+
})
|
|
26
|
+
.strict(),
|
|
27
|
+
into: z
|
|
28
|
+
.object({
|
|
29
|
+
starts: fieldName,
|
|
30
|
+
ends: fieldName.optional(),
|
|
31
|
+
zone: fieldName.optional(),
|
|
32
|
+
changed: fieldName.optional(),
|
|
33
|
+
})
|
|
34
|
+
.strict(),
|
|
35
|
+
copy: z
|
|
36
|
+
.record(fieldName, fieldName)
|
|
37
|
+
.refine((copy) => Object.keys(copy).length <= 20, "At most 20 copied fields")
|
|
38
|
+
.optional(),
|
|
39
|
+
cancel: z.string().min(1).max(100),
|
|
40
|
+
ended: z
|
|
41
|
+
.object({ field: fieldName, in: z.array(z.string().min(1).max(100)).min(1).max(20) })
|
|
42
|
+
.strict()
|
|
43
|
+
.optional(),
|
|
44
|
+
closures: z
|
|
45
|
+
.object({ object: fieldName, from: fieldName, until: fieldName })
|
|
46
|
+
.strict()
|
|
47
|
+
.optional(),
|
|
48
|
+
horizonDays: z.number().int().min(1).max(RECURRING_HORIZON_MAX).optional(),
|
|
49
|
+
horizonAtLeast: z.object({ object: fieldName, field: fieldName }).strict().optional(),
|
|
50
|
+
})
|
|
51
|
+
.strict();
|
|
52
|
+
/** Reports problems with `objects.<name>.recurring` through `contract`. */
|
|
53
|
+
export function checkObjectRecurring(m, name, object, contract) {
|
|
54
|
+
const recurring = object.recurring;
|
|
55
|
+
if (!recurring)
|
|
56
|
+
return;
|
|
57
|
+
const path = `objects.${name}.recurring`;
|
|
58
|
+
const own = (o, local) => Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
59
|
+
const expect = (o, oName, at, local, types, what) => {
|
|
60
|
+
if (local === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
const field = own(o, local);
|
|
63
|
+
if (!field || !types.includes(field.type) || field.many) {
|
|
64
|
+
contract(at, `"${local}" must be ${what} of ${oName}`);
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return field;
|
|
68
|
+
};
|
|
69
|
+
const rule = recurring.rule;
|
|
70
|
+
const weekdays = expect(object, name, `${path}.rule.weekdays`, rule.weekdays, ["multiSelect"], "a multiSelect field");
|
|
71
|
+
if (weekdays) {
|
|
72
|
+
const values = (weekdays.options ?? []).map((o) => o.value);
|
|
73
|
+
const wrong = values.filter((v) => !WEEKDAYS.includes(v));
|
|
74
|
+
if (wrong.length || !values.length) {
|
|
75
|
+
contract(`${path}.rule.weekdays`, `"${rule.weekdays}"'s options must be weekdays (${WEEKDAYS.join(", ")})`);
|
|
76
|
+
}
|
|
77
|
+
if (weekdays.editableOptions) {
|
|
78
|
+
contract(`${path}.rule.weekdays`, "The weekdays field cannot have editableOptions");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
expect(object, name, `${path}.rule.time`, rule.time, ["text"], "a text field (HH:MM)");
|
|
82
|
+
expect(object, name, `${path}.rule.length`, rule.length, ["number"], "a number field (minutes)");
|
|
83
|
+
expect(object, name, `${path}.rule.from`, rule.from, ["date"], "a date field");
|
|
84
|
+
expect(object, name, `${path}.rule.until`, rule.until, ["date"], "a date field");
|
|
85
|
+
expect(object, name, `${path}.rule.zone`, rule.zone, ["timezone"], "a timezone field");
|
|
86
|
+
if (recurring.ended) {
|
|
87
|
+
const field = expect(object, name, `${path}.ended.field`, recurring.ended.field, ["select"], "a select field");
|
|
88
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
89
|
+
for (const value of recurring.ended.in) {
|
|
90
|
+
if (field && !values.has(value)) {
|
|
91
|
+
contract(`${path}.ended.in`, `"${value}" is not an option of ${recurring.ended.field}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const generatedName = recurring.generates;
|
|
96
|
+
const generated = Object.hasOwn(m.objects, generatedName) ? m.objects[generatedName] : undefined;
|
|
97
|
+
if (!generated || generatedName === name) {
|
|
98
|
+
contract(`${path}.generates`, `"${generatedName}" must be another of this extension's objects`);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const link = own(generated, recurring.link);
|
|
102
|
+
if (link?.type !== "relation" || link.many || link.to !== `${m.id}.${name}`) {
|
|
103
|
+
contract(`${path}.link`, `"${recurring.link}" must be a relation field of ${generatedName} to ${m.id}.${name}`);
|
|
104
|
+
}
|
|
105
|
+
else if (!link.readOnly) {
|
|
106
|
+
contract(`${path}.link`, `"${recurring.link}" must be read-only: the host sets it`);
|
|
107
|
+
}
|
|
108
|
+
expect(generated, generatedName, `${path}.into.starts`, recurring.into.starts, ["datetime"], "a datetime field");
|
|
109
|
+
expect(generated, generatedName, `${path}.into.ends`, recurring.into.ends, ["datetime"], "a datetime field");
|
|
110
|
+
expect(generated, generatedName, `${path}.into.zone`, recurring.into.zone, ["timezone"], "a timezone field");
|
|
111
|
+
const changed = expect(generated, generatedName, `${path}.into.changed`, recurring.into.changed, ["boolean"], "a boolean field");
|
|
112
|
+
if (changed && !changed.readOnly) {
|
|
113
|
+
contract(`${path}.into.changed`, `"${recurring.into.changed}" must be read-only: the host sets it`);
|
|
114
|
+
}
|
|
115
|
+
const into = new Set(Object.values(recurring.into));
|
|
116
|
+
// The state is the host's to move and staff's to change; a series never copies one in.
|
|
117
|
+
if (generated.states)
|
|
118
|
+
into.add(generated.states.field);
|
|
119
|
+
for (const [target, source] of Object.entries(recurring.copy ?? {})) {
|
|
120
|
+
const to = own(generated, target);
|
|
121
|
+
const from = own(object, source);
|
|
122
|
+
if (!to || to.type === "formula" || into.has(target) || target === recurring.link) {
|
|
123
|
+
contract(`${path}.copy.${target}`, `"${target}" must be a field of ${generatedName} the host does not already fill`);
|
|
124
|
+
}
|
|
125
|
+
else if (!from ||
|
|
126
|
+
from.type !== to.type ||
|
|
127
|
+
Boolean(from.many) !== Boolean(to.many) ||
|
|
128
|
+
from.to !== to.to) {
|
|
129
|
+
contract(`${path}.copy.${target}`, `"${source}" must be a field of ${name} of the same type as ${generatedName}.${target}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const states = generated.states;
|
|
133
|
+
if (!states) {
|
|
134
|
+
contract(`${path}.cancel`, `${generatedName} must declare states, with a host move into "${recurring.cancel}"`);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
const field = own(generated, states.field);
|
|
138
|
+
if (!(field?.options ?? []).some((o) => o.value === recurring.cancel)) {
|
|
139
|
+
contract(`${path}.cancel`, `"${recurring.cancel}" is not a state of ${generatedName}`);
|
|
140
|
+
}
|
|
141
|
+
else if (!states.moves.some((move) => move.to === recurring.cancel && move.by.includes("host"))) {
|
|
142
|
+
contract(`${path}.cancel`, `${generatedName} needs a move into "${recurring.cancel}" that the host may make`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (recurring.horizonAtLeast) {
|
|
146
|
+
const h = recurring.horizonAtLeast;
|
|
147
|
+
const target = Object.hasOwn(m.objects, h.object) ? m.objects[h.object] : undefined;
|
|
148
|
+
if (!target) {
|
|
149
|
+
contract(`${path}.horizonAtLeast.object`, `"${h.object}" must be one of this extension's objects`);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
expect(target, h.object, `${path}.horizonAtLeast.field`, h.field, ["number"], "a number field (days)");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (recurring.closures) {
|
|
156
|
+
const c = recurring.closures;
|
|
157
|
+
const closure = Object.hasOwn(m.objects, c.object) ? m.objects[c.object] : undefined;
|
|
158
|
+
if (!closure || c.object === name || c.object === generatedName) {
|
|
159
|
+
contract(`${path}.closures.object`, `"${c.object}" must be another of this extension's objects`);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
expect(closure, c.object, `${path}.closures.from`, c.from, ["date"], "a date field");
|
|
163
|
+
expect(closure, c.object, `${path}.closures.until`, c.until, ["date"], "a date field");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
package/dist/rich-text.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DocumentSchema } from "./document.ts";
|
|
1
|
+
import type { DocumentNode, DocumentObjectNode, DocumentSchema } from "./document.ts";
|
|
2
2
|
/** The block types, in the order a renderer lists them. */
|
|
3
3
|
export declare const RICH_TEXT_BLOCKS: readonly ["paragraph", "heading", "list", "quote", "image", "divider", "embed", "callout", "button"];
|
|
4
4
|
export type RichTextBlockType = (typeof RICH_TEXT_BLOCKS)[number];
|
|
@@ -17,7 +17,21 @@ export declare const RICH_TEXT_SCHEMA: DocumentSchema;
|
|
|
17
17
|
export declare const IMAGE_SCHEMA: DocumentSchema;
|
|
18
18
|
/** The most an image field's value may be, in bytes of its JSON. */
|
|
19
19
|
export declare const IMAGE_MAX_BYTES = 2048;
|
|
20
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* A video from the media library (stage 42, ADR 0475), as a node a declared document may use: the
|
|
22
|
+
* video (a ready one, checked on write) and, optionally, a picture to show before it plays.
|
|
23
|
+
*/
|
|
24
|
+
export declare const VIDEO_NODE: DocumentObjectNode;
|
|
25
|
+
/** A downloadable file from the media library (a PDF), as a node a declared document may use. */
|
|
26
|
+
export declare const FILE_NODE: DocumentNode;
|
|
27
|
+
/** The `video` field type (stage 42, ADR 0475): `{ media, poster? }`. */
|
|
28
|
+
export declare const VIDEO_SCHEMA: DocumentSchema;
|
|
29
|
+
/** The most a video field's value may be, in bytes of its JSON. */
|
|
30
|
+
export declare const VIDEO_MAX_BYTES = 1024;
|
|
31
|
+
/**
|
|
32
|
+
* The field types whose value is a document (ADR 0445): `document`, `richText`, `image` and
|
|
33
|
+
* `video`.
|
|
34
|
+
*/
|
|
21
35
|
export declare function isDocumentType(type: string | undefined): boolean;
|
|
22
36
|
/**
|
|
23
37
|
* The schema a document-valued field is checked against: its declared one, or richText's. Not a
|