@quad-education/sdk 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +5 -2
  2. package/dist/contract-tests/apps.d.ts +617 -0
  3. package/dist/contract-tests/apps.js +215 -1
  4. package/dist/contract-tests/harness.d.ts +1 -1
  5. package/dist/contract-tests/harness.js +7 -0
  6. package/dist/contract-tests/lists-and-files.d.ts +19 -0
  7. package/dist/contract-tests/lists-and-files.js +279 -0
  8. package/dist/contract-tests/live.d.ts +3 -0
  9. package/dist/contract-tests/live.js +156 -0
  10. package/dist/contract-tests/notifications.d.ts +3 -0
  11. package/dist/contract-tests/notifications.js +218 -0
  12. package/dist/contract-tests/search-and-totals.d.ts +13 -0
  13. package/dist/contract-tests/search-and-totals.js +226 -0
  14. package/dist/contract-tests/suite.d.ts +7 -0
  15. package/dist/contract-tests/suite.js +180 -2
  16. package/dist/contract-tests/writes.d.ts +3 -0
  17. package/dist/contract-tests/writes.js +265 -0
  18. package/dist/src/base64.d.ts +3 -0
  19. package/dist/src/base64.js +52 -0
  20. package/dist/src/client/connect.d.ts +1 -0
  21. package/dist/src/client/connect.js +1 -0
  22. package/dist/src/client/create-client.js +39 -5
  23. package/dist/src/client/save-file.d.ts +16 -0
  24. package/dist/src/client/save-file.js +28 -0
  25. package/dist/src/client/watch.d.ts +20 -0
  26. package/dist/src/client/watch.js +175 -0
  27. package/dist/src/contract/errors.d.ts +1 -1
  28. package/dist/src/contract/types.d.ts +220 -10
  29. package/dist/src/contract/types.js +4 -1
  30. package/dist/src/engine/engine.d.ts +91 -2
  31. package/dist/src/engine/engine.js +604 -67
  32. package/dist/src/engine/files.d.ts +25 -0
  33. package/dist/src/engine/files.js +88 -0
  34. package/dist/src/engine/index.d.ts +1 -1
  35. package/dist/src/engine/memory-store.d.ts +33 -3
  36. package/dist/src/engine/memory-store.js +116 -8
  37. package/dist/src/engine/notify-content.d.ts +9 -0
  38. package/dist/src/engine/notify-content.js +88 -0
  39. package/dist/src/engine/query.d.ts +14 -2
  40. package/dist/src/engine/query.js +45 -13
  41. package/dist/src/engine/search.d.ts +8 -0
  42. package/dist/src/engine/search.js +32 -0
  43. package/dist/src/engine/state.d.ts +5 -0
  44. package/dist/src/engine/storage.d.ts +46 -1
  45. package/dist/src/engine/storage.js +46 -3
  46. package/dist/src/engine/store.d.ts +39 -2
  47. package/dist/src/engine/summary.d.ts +25 -0
  48. package/dist/src/engine/summary.js +107 -0
  49. package/dist/src/engine/validate-input.d.ts +7 -1
  50. package/dist/src/engine/validate-input.js +28 -1
  51. package/dist/src/engine/working-copy.d.ts +30 -0
  52. package/dist/src/engine/working-copy.js +91 -0
  53. package/dist/src/platform-types.d.ts +11 -1
  54. package/dist/src/protocol.d.ts +1 -1
  55. package/dist/src/protocol.js +9 -0
  56. package/dist/src/testing/index.d.ts +9 -0
  57. package/dist/src/testing/index.js +16 -1
  58. package/package.json +4 -4
@@ -1,9 +1,11 @@
1
1
  import { type Dataset, type Principal } from '@quad-education/canonical';
2
2
  import { type AppSchema, type Manifest } from '@quad-education/schema';
3
+ import { type FileInfo } from '../contract/types.ts';
3
4
  import type { OutboxEntry } from '../platform-types.ts';
4
5
  import { type ClockState, type Method, type SessionInfo } from '../protocol.ts';
5
6
  import { type RunRequest, type RunResult } from './runner.ts';
6
7
  import { type App, type Ctx, type Kernel, type ResolvedRecipients, type StoredRun } from './state.ts';
8
+ import { type StoreChannel } from './memory-store.ts';
7
9
  import type { AppStore, PlatformKind } from './store.ts';
8
10
  export type EngineOptions = {
9
11
  dataset: Dataset;
@@ -13,6 +15,11 @@ export type EngineOptions = {
13
15
  dbName?: string;
14
16
  /** Keep state here instead of in memory and IndexedDB (a school instance's data API, Phase 4). */
15
17
  store?: AppStore;
18
+ /**
19
+ * How windows of one browser share what they store (D158), with `indexedDB`: a `BroadcastChannel`. Without
20
+ * one, each window keeps its own copy until it is reloaded.
21
+ */
22
+ channel?: StoreChannel;
16
23
  };
17
24
  export declare class Engine implements Kernel {
18
25
  private readonly apps;
@@ -20,6 +27,7 @@ export declare class Engine implements Kernel {
20
27
  private byId;
21
28
  /** Each app's admins, as person ids (D133). The platform sets them, as a school does; apps only read them. */
22
29
  private readonly admins;
30
+ private readonly roles;
23
31
  private school;
24
32
  readonly store: AppStore;
25
33
  private constructor();
@@ -40,12 +48,25 @@ export declare class Engine implements Kernel {
40
48
  schema: AppSchema;
41
49
  }): Promise<void>;
42
50
  manifest(appId: string): Manifest;
51
+ /** The installed app's own tables (D144): what the Sandbox's School data view lists beside the school's. */
52
+ schema(appId: string): AppSchema;
43
53
  /**
44
54
  * Who the school has put in charge of an app (D133): staff person ids, replacing any before. Platform only (the
45
55
  * Sandbox's School data view, a test platform); no SDK method writes it. Ids that aren't staff in the school served at
46
56
  * the time of a read are ignored, so someone who leaves drops out.
47
57
  */
48
58
  setAppAdmins(appId: string, personIds: readonly string[]): void;
59
+ /**
60
+ * The app's own roles each person holds (D143), as a school assigns them: person id to role names, replacing any
61
+ * before. Platform only (the Sandbox's School data view, a test platform); no SDK method writes it. A name the
62
+ * manifest's `roles` doesn't have is ignored, and so is a person who isn't in the school served at the time of a read.
63
+ */
64
+ setAppRoles(appId: string, assignments: Readonly<Record<string, readonly string[]>>): void;
65
+ /**
66
+ * The app roles a person holds, in the manifest's spelling and order: the ones assigned to them, and any named like
67
+ * their school role, which everyone with that school role previews (V2s D84).
68
+ */
69
+ private heldAppRoles;
49
70
  /** The app's admins who are staff in the school served now, in name order. */
50
71
  private appAdminRecords;
51
72
  /** Clear an app's data and outbox (the shell's reset button). */
@@ -80,6 +101,7 @@ export declare class Engine implements Kernel {
80
101
  };
81
102
  audit: number;
82
103
  records: Record<string, Record<string, string>>;
104
+ files: string[];
83
105
  }>;
84
106
  /** Runs for the job log, newest first. */
85
107
  runs(appId: string): Promise<StoredRun[]>;
@@ -139,8 +161,16 @@ export declare class Engine implements Kernel {
139
161
  private parentChildren;
140
162
  private dbEntity;
141
163
  nowIso(): string;
142
- /** The rules for an operation: 'system' (full access), or the role's rules (empty = denied). */
164
+ /**
165
+ * The rules for an operation: 'system' (full access), or every rule of the person's school role and of the app
166
+ * roles they hold (D143); empty means denied.
167
+ */
143
168
  private rules;
169
+ /**
170
+ * Which `appAdmin` and `listed:` rules the principal passes (D142). A listing is a row of the named table whose
171
+ * person field holds this person; the lookup ignores that table's own read rules, so tables may name each other.
172
+ */
173
+ private resolveGrants;
144
174
  /** Whether the current user may see a student, under the school's visibility policy (D4). */
145
175
  private seesStudent;
146
176
  private matches;
@@ -156,13 +186,72 @@ export declare class Engine implements Kernel {
156
186
  private checkWrite;
157
187
  remember(ctx: Ctx, key: string, result: unknown): Promise<void>;
158
188
  idempotencyKey(options: unknown, scope: string): string | undefined;
189
+ private readonly writeQueues;
190
+ /**
191
+ * Writes to one app run one after another, so a check and the write that follows it are never interleaved with
192
+ * another call's: of two bookings for the last slot started together, one sees the other.
193
+ */
194
+ private queued;
195
+ /** One create, update or delete: a working copy of its own, committed when it has passed every check. */
196
+ private single;
197
+ /** Up to 25 writes, all stored or none (D150). Each is checked as it is on its own. */
198
+ private dbBatch;
199
+ /** D149: no other record of the entity holds the same values for any of its unique rules. */
200
+ private checkUnique;
201
+ /** D150: the write goes ahead only when the record's fields hold the values given; null means empty. */
202
+ private checkIf;
159
203
  private dbCreate;
160
204
  private dbUpdate;
161
205
  private dbGet;
206
+ /**
207
+ * The records of an entity the caller may read, with what a query over them may use: the fields they can see,
208
+ * which of those are lists, and where a search looks (D153). Access comes first: filters, pages, counts and
209
+ * totals only ever cover readable records.
210
+ */
211
+ private dbReadable;
212
+ /**
213
+ * D157: a stamp for each entity, different after any write to it, for a caller who may read the entity. It
214
+ * reads no records. An entity's stamp also moves with every table its rules look people up in (`listed:`),
215
+ * because a change there changes who may read what.
216
+ */
217
+ private dbChanges;
218
+ /** D154: how many records match, of those the caller may read. */
219
+ private dbCount;
220
+ /** D154: the count, and totals of number fields, of the records the caller may read; by group when asked. */
221
+ private dbSummarise;
162
222
  private dbList;
163
223
  private resolveRef;
164
224
  private dbDelete;
225
+ /** A file nobody attached within a day of its upload is gone, by the engine's clock. */
226
+ private fileExpired;
227
+ private filesUpload;
228
+ /**
229
+ * A file the caller may read, or NOT_FOUND whether it is missing or just not theirs. Unattached, it is its
230
+ * uploader's alone; attached, it follows the field of the record that holds it.
231
+ */
232
+ private readableFile;
233
+ private filesRead;
234
+ /**
235
+ * What a write does to files: which to attach and which to remove. A file is attached once, by whoever uploaded
236
+ * it, to one field of one record; an id the record's field already holds stays as it is.
237
+ */
238
+ private planFiles;
239
+ private applyFiles;
165
240
  private notify;
241
+ /** D152: the files a notification names, each one the sender can read, within the limits. */
242
+ private attachmentsFor;
243
+ /**
244
+ * D152: a file never goes to a role its field is hidden from (`visibleTo`), and one at a staff-only level goes
245
+ * only to staff. The app declared both; a notification does not get round them.
246
+ */
247
+ private checkAttachmentRoles;
248
+ /** D152: the copies a message carries, made as it is sent, so they outlast the records they came from. */
249
+ private copyAttachments;
250
+ /** A file a sent notification carries, for the shell's outbox and tests. Never part of the SDK. */
251
+ sentFile(appId: string, id: string): Promise<{
252
+ info: FileInfo;
253
+ bytes: Uint8Array;
254
+ } | undefined>;
166
255
  private roleOf;
167
256
  /**
168
257
  * Resolves recipients to people, applying the app's roles and D17. Parents are only ever
@@ -175,4 +264,4 @@ export declare class Engine implements Kernel {
175
264
  /** D19: one CIO-visible entry per call that returns pastoral or medical fields. */
176
265
  private auditSensitiveRead;
177
266
  }
178
- export declare const ENGINE_SDK_VERSION = "0.6.0";
267
+ export declare const ENGINE_SDK_VERSION = "0.9.0";