@quad-education/sdk 0.2.0 → 0.4.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.
- package/README.md +4 -2
- package/dist/contract-tests/apps.d.ts +3 -112
- package/dist/contract-tests/apps.js +6 -183
- package/dist/contract-tests/harness.d.ts +2 -2
- package/dist/contract-tests/index.d.ts +2 -3
- package/dist/contract-tests/index.js +1 -2
- package/dist/contract-tests/suite.d.ts +3 -10
- package/dist/contract-tests/suite.js +49 -502
- package/dist/src/client/connect.d.ts +3 -13
- package/dist/src/client/connect.js +3 -21
- package/dist/src/client/create-client.js +0 -3
- package/dist/src/client/handshake-client.d.ts +0 -2
- package/dist/src/client/handshake-client.js +0 -2
- package/dist/src/contract/errors.d.ts +1 -1
- package/dist/src/contract/recipients.d.ts +2 -8
- package/dist/src/contract/types.d.ts +7 -44
- package/dist/src/contract/types.js +1 -1
- package/dist/src/engine/engine.d.ts +10 -46
- package/dist/src/engine/engine.js +42 -222
- package/dist/src/engine/host.d.ts +2 -2
- package/dist/src/engine/in-process.d.ts +2 -2
- package/dist/src/engine/runner.js +2 -4
- package/dist/src/engine/state.d.ts +6 -17
- package/dist/src/engine/state.js +3 -31
- package/dist/src/engine/tasks.js +3 -10
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/jobs/define.d.ts +2 -2
- package/dist/src/platform-types.d.ts +2 -2
- package/dist/src/protocol.d.ts +1 -1
- package/dist/src/protocol.js +0 -1
- package/package.json +4 -4
- package/dist/contract-tests/standalone-apps.d.ts +0 -183
- package/dist/contract-tests/standalone-apps.js +0 -178
- package/dist/contract-tests/standalone.d.ts +0 -2
- package/dist/contract-tests/standalone.js +0 -709
- package/dist/src/client/http-transport.d.ts +0 -18
- package/dist/src/client/http-transport.js +0 -76
- package/dist/src/client/live.d.ts +0 -39
- package/dist/src/client/live.js +0 -122
- package/dist/src/contract/principals.d.ts +0 -30
- package/dist/src/contract/principals.js +0 -12
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { QuadSdk } from '../contract/types.ts';
|
|
2
|
-
import { type SessionInfo } from '../protocol.ts';
|
|
3
|
-
import { type Transport } from './create-client.ts';
|
|
4
|
-
export type HttpOptions = {
|
|
5
|
-
/** The data API's address, e.g. https://data.us-east-1.quadapps.net */
|
|
6
|
-
baseUrl: string;
|
|
7
|
-
/** The current token. Called for every request, so the provider can keep it fresh. */
|
|
8
|
-
token: () => string | Promise<string>;
|
|
9
|
-
/** For tests and other runtimes; defaults to the global `fetch`. */
|
|
10
|
-
fetch?: typeof fetch;
|
|
11
|
-
};
|
|
12
|
-
/** Moves RPC calls to the data API. Also holds the session the first call to `/session` returned. */
|
|
13
|
-
export type HttpTransport = Transport & {
|
|
14
|
-
session: SessionInfo;
|
|
15
|
-
};
|
|
16
|
-
export declare function createHttpTransport(options: HttpOptions): Promise<HttpTransport>;
|
|
17
|
-
/** Connect to a live app's data: the SDK over HTTP, as the person the token names. */
|
|
18
|
-
export declare function connectHttp(options: HttpOptions): Promise<QuadSdk>;
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { QuadError } from "../contract/errors.js";
|
|
2
|
-
import { deserializeError, } from "../protocol.js";
|
|
3
|
-
import { createSdkClient } from "./create-client.js";
|
|
4
|
-
async function readEnvelope(response) {
|
|
5
|
-
try {
|
|
6
|
-
return (await response.json());
|
|
7
|
-
}
|
|
8
|
-
catch {
|
|
9
|
-
throw new QuadError('TRANSPORT', `the data API answered ${response.status} with no usable body`);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export async function createHttpTransport(options) {
|
|
13
|
-
const doFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
14
|
-
const base = options.baseUrl.replace(/\/+$/, '');
|
|
15
|
-
let clock = { kind: 'offset', offsetMs: 0 };
|
|
16
|
-
let known = Date.now();
|
|
17
|
-
let sentAt = Date.now();
|
|
18
|
-
const learn = (env) => {
|
|
19
|
-
if (env.clock)
|
|
20
|
-
clock = env.clock;
|
|
21
|
-
if (typeof env.now === 'number') {
|
|
22
|
-
known = env.now;
|
|
23
|
-
sentAt = Date.now();
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
async function request(path, init) {
|
|
27
|
-
// A token that can't be had is its own error (a session that ended), not a network failure.
|
|
28
|
-
const token = await options.token();
|
|
29
|
-
let response;
|
|
30
|
-
try {
|
|
31
|
-
response = await doFetch(`${base}${path}`, {
|
|
32
|
-
method: init.method,
|
|
33
|
-
headers: {
|
|
34
|
-
authorization: `Bearer ${token}`,
|
|
35
|
-
...(init.body !== undefined ? { 'content-type': 'application/json' } : {}),
|
|
36
|
-
},
|
|
37
|
-
...(init.body !== undefined ? { body: JSON.stringify(init.body) } : {}),
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
throw new QuadError('TRANSPORT', `couldn't reach the data API: ${e.message}`);
|
|
42
|
-
}
|
|
43
|
-
const env = await readEnvelope(response);
|
|
44
|
-
learn(env);
|
|
45
|
-
if (!env.ok) {
|
|
46
|
-
throw env.error
|
|
47
|
-
? deserializeError(env.error)
|
|
48
|
-
: new QuadError('TRANSPORT', `the data API answered ${response.status}`);
|
|
49
|
-
}
|
|
50
|
-
return env;
|
|
51
|
-
}
|
|
52
|
-
const hello = (await request('/session', { method: 'GET' }));
|
|
53
|
-
const session = {
|
|
54
|
-
appId: hello.result.appId,
|
|
55
|
-
timezone: hello.result.timezone,
|
|
56
|
-
clock,
|
|
57
|
-
};
|
|
58
|
-
return {
|
|
59
|
-
session,
|
|
60
|
-
async call(method, params, context) {
|
|
61
|
-
const env = await request('/rpc', {
|
|
62
|
-
method: 'POST',
|
|
63
|
-
body: { method, params, ...(context?.runId !== undefined ? { runId: context.runId } : {}) },
|
|
64
|
-
});
|
|
65
|
-
return env.result;
|
|
66
|
-
},
|
|
67
|
-
nowMs() {
|
|
68
|
-
return clock.kind === 'fixed' ? clock.at : known + (Date.now() - sentAt);
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/** Connect to a live app's data: the SDK over HTTP, as the person the token names. */
|
|
73
|
-
export async function connectHttp(options) {
|
|
74
|
-
const transport = await createHttpTransport(options);
|
|
75
|
-
return createSdkClient(transport, transport.session);
|
|
76
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { QuadError } from '../contract/errors.ts';
|
|
2
|
-
import type { QuadSdk } from '../contract/types.ts';
|
|
3
|
-
export type LiveOptions = {
|
|
4
|
-
/** For tests and other runtimes; defaults to the global `fetch`. */
|
|
5
|
-
fetch?: typeof fetch;
|
|
6
|
-
/** Where the page is and how to leave it; defaults to `window.location`. */
|
|
7
|
-
location?: {
|
|
8
|
-
pathname: string;
|
|
9
|
-
search: string;
|
|
10
|
-
hash: string;
|
|
11
|
-
assign(url: string): void;
|
|
12
|
-
};
|
|
13
|
-
/** Where the guest device id is kept; defaults to `localStorage`, or the page's memory if that is blocked. */
|
|
14
|
-
storage?: Pick<Storage, 'getItem' | 'setItem'>;
|
|
15
|
-
/** The time in ms; defaults to `Date.now`. */
|
|
16
|
-
now?: () => number;
|
|
17
|
-
};
|
|
18
|
-
export type LiveStatus = {
|
|
19
|
-
signedIn: true;
|
|
20
|
-
dataApi: string;
|
|
21
|
-
} | {
|
|
22
|
-
signedIn: false;
|
|
23
|
-
guests: boolean;
|
|
24
|
-
};
|
|
25
|
-
/** Thrown while nobody is signed in. Offer {@link signIn}, and {@link continueAsGuest} when `guests` is true. */
|
|
26
|
-
export declare class SignInRequired extends QuadError {
|
|
27
|
-
readonly guests: boolean;
|
|
28
|
-
constructor(guests: boolean);
|
|
29
|
-
}
|
|
30
|
-
/** Who is signed in at this address, and whether guests are welcome if no one is. */
|
|
31
|
-
export declare function getLiveStatus(options?: LiveOptions): Promise<LiveStatus>;
|
|
32
|
-
/** Leaves for Quad's sign-in, which returns the person to this page. */
|
|
33
|
-
export declare function signIn(options?: LiveOptions): void;
|
|
34
|
-
/** Starts a guest session. The device id stays on this origin; Quad keeps only a salted hash of it (ADR 0010). */
|
|
35
|
-
export declare function continueAsGuest(options?: LiveOptions): Promise<void>;
|
|
36
|
-
/** Connects as the signed-in person, or throws {@link SignInRequired}. */
|
|
37
|
-
export declare function connectLive(options?: LiveOptions): Promise<QuadSdk>;
|
|
38
|
-
/** Connects from a status already fetched; throws {@link SignInRequired} if it says nobody is signed in. */
|
|
39
|
-
export declare function connectSignedIn(status: LiveStatus, options: LiveOptions): Promise<QuadSdk>;
|
package/dist/src/client/live.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { QuadError } from "../contract/errors.js";
|
|
2
|
-
import { connectHttp } from "./http-transport.js";
|
|
3
|
-
// A live app's connection (V2c C3a, ADR 0008). The app runs at its own address, with no shell around it, so it
|
|
4
|
-
// asks that same origin (`/__quad/live/*`, which the edge forwards to the control plane) who is signed in, gets a
|
|
5
|
-
// short-lived token for them, and sends every call to the data API with it. The app never sees the session
|
|
6
|
-
// cookie (it is HttpOnly) or any key.
|
|
7
|
-
const LIVE = '/__quad/live';
|
|
8
|
-
/** Ask for a new token when this little time is left, so a call never goes out with one about to lapse. */
|
|
9
|
-
const REFRESH_MARGIN_MS = 60_000;
|
|
10
|
-
const DEVICE_KEY = 'quad:device';
|
|
11
|
-
/** Thrown while nobody is signed in. Offer {@link signIn}, and {@link continueAsGuest} when `guests` is true. */
|
|
12
|
-
export class SignInRequired extends QuadError {
|
|
13
|
-
guests;
|
|
14
|
-
constructor(guests) {
|
|
15
|
-
super('FORBIDDEN', 'not signed in');
|
|
16
|
-
this.name = 'SignInRequired';
|
|
17
|
-
this.guests = guests;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
const doFetch = (options) => options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
21
|
-
const clock = (options) => options.now ?? Date.now;
|
|
22
|
-
const here = (options) => options.location ?? globalThis.location;
|
|
23
|
-
/** A plain request to the live origin: same-origin, cookies included, and marked so a stray page can't forge it. */
|
|
24
|
-
async function ask(options, path, init) {
|
|
25
|
-
try {
|
|
26
|
-
return await doFetch(options)(`${LIVE}${path}`, {
|
|
27
|
-
method: init.method,
|
|
28
|
-
credentials: 'same-origin',
|
|
29
|
-
headers: {
|
|
30
|
-
'x-quad-live': '1',
|
|
31
|
-
...(init.body !== undefined ? { 'content-type': 'application/json' } : {}),
|
|
32
|
-
},
|
|
33
|
-
...(init.body !== undefined ? { body: JSON.stringify(init.body) } : {}),
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
throw new QuadError('TRANSPORT', `couldn't reach Quad: ${e.message}`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/** Who is signed in at this address, and whether guests are welcome if no one is. */
|
|
41
|
-
export async function getLiveStatus(options = {}) {
|
|
42
|
-
const response = await ask(options, '/session', { method: 'GET' });
|
|
43
|
-
if (response.status === 404)
|
|
44
|
-
throw new QuadError('TRANSPORT', 'This app runs inside the Quad demo shell, or at its live address. Open it from there.');
|
|
45
|
-
const body = (await response.json().catch(() => null));
|
|
46
|
-
if (!response.ok || !body)
|
|
47
|
-
throw new QuadError('TRANSPORT', `Quad answered ${response.status} with no usable session`);
|
|
48
|
-
if (body.signedIn && typeof body.dataApi === 'string')
|
|
49
|
-
return { signedIn: true, dataApi: body.dataApi };
|
|
50
|
-
return { signedIn: false, guests: body.guests === true };
|
|
51
|
-
}
|
|
52
|
-
/** Leaves for Quad's sign-in, which returns the person to this page. */
|
|
53
|
-
export function signIn(options = {}) {
|
|
54
|
-
const page = here(options);
|
|
55
|
-
const next = `${page.pathname}${page.search}${page.hash}`;
|
|
56
|
-
page.assign(`${LIVE}/signin?next=${encodeURIComponent(next)}`);
|
|
57
|
-
}
|
|
58
|
-
function device(options) {
|
|
59
|
-
const storage = options.storage ?? safeStorage();
|
|
60
|
-
const known = storage?.getItem(DEVICE_KEY);
|
|
61
|
-
if (known)
|
|
62
|
-
return known;
|
|
63
|
-
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
64
|
-
const fresh = btoa(String.fromCharCode(...bytes))
|
|
65
|
-
.replace(/\+/g, '-')
|
|
66
|
-
.replace(/\//g, '_')
|
|
67
|
-
.replace(/=+$/, '');
|
|
68
|
-
storage?.setItem(DEVICE_KEY, fresh);
|
|
69
|
-
return fresh;
|
|
70
|
-
}
|
|
71
|
-
let memory = null;
|
|
72
|
-
function safeStorage() {
|
|
73
|
-
try {
|
|
74
|
-
return globalThis.localStorage;
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
// Blocked storage: the id lasts as long as the page, which is a new guest after a reload.
|
|
78
|
-
return { getItem: () => memory, setItem: (_k, v) => void (memory = v) };
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/** Starts a guest session. The device id stays on this origin; Quad keeps only a salted hash of it (ADR 0010). */
|
|
82
|
-
export async function continueAsGuest(options = {}) {
|
|
83
|
-
const response = await ask(options, '/guest', {
|
|
84
|
-
method: 'POST',
|
|
85
|
-
body: { device: device(options) },
|
|
86
|
-
});
|
|
87
|
-
if (!response.ok)
|
|
88
|
-
throw new QuadError('FORBIDDEN', 'this app does not let guests in');
|
|
89
|
-
}
|
|
90
|
-
/** Connects as the signed-in person, or throws {@link SignInRequired}. */
|
|
91
|
-
export async function connectLive(options = {}) {
|
|
92
|
-
return connectSignedIn(await getLiveStatus(options), options);
|
|
93
|
-
}
|
|
94
|
-
/** Connects from a status already fetched; throws {@link SignInRequired} if it says nobody is signed in. */
|
|
95
|
-
export async function connectSignedIn(status, options) {
|
|
96
|
-
if (!status.signedIn)
|
|
97
|
-
throw new SignInRequired(status.guests);
|
|
98
|
-
let current;
|
|
99
|
-
let pending;
|
|
100
|
-
const mint = async () => {
|
|
101
|
-
const response = await ask(options, '/token', { method: 'POST' });
|
|
102
|
-
if (response.status === 401 || response.status === 403)
|
|
103
|
-
throw new QuadError('FORBIDDEN', 'your session has ended; sign in again');
|
|
104
|
-
const body = (await response.json().catch(() => null));
|
|
105
|
-
if (!response.ok || !body?.token || typeof body.expiresAt !== 'number')
|
|
106
|
-
throw new QuadError('TRANSPORT', `Quad answered ${response.status} with no usable token`);
|
|
107
|
-
current = { token: body.token, expiresAtMs: body.expiresAt * 1000 };
|
|
108
|
-
return body.token;
|
|
109
|
-
};
|
|
110
|
-
const token = () => {
|
|
111
|
-
if (current && current.expiresAtMs - clock(options)() > REFRESH_MARGIN_MS)
|
|
112
|
-
return current.token;
|
|
113
|
-
// Calls made together share one request.
|
|
114
|
-
pending ??= mint().finally(() => (pending = undefined));
|
|
115
|
-
return pending;
|
|
116
|
-
};
|
|
117
|
-
return connectHttp({
|
|
118
|
-
baseUrl: status.dataApi,
|
|
119
|
-
token,
|
|
120
|
-
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
121
|
-
});
|
|
122
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { Principal } from '@quad-education/canonical';
|
|
2
|
-
/** A Quad account using a live or Sandbox app, with the app-defined roles the owner assigned it (V2c, D100). */
|
|
3
|
-
export type AccountPrincipal = {
|
|
4
|
-
readonly kind: 'account';
|
|
5
|
-
readonly accountId: string;
|
|
6
|
-
readonly roles: readonly string[];
|
|
7
|
-
/** Carried by a live token, so a call doesn't need the directory to say who this is. */
|
|
8
|
-
readonly displayName?: string;
|
|
9
|
-
};
|
|
10
|
-
/** Someone not signed in, identified only by a device id (V2c, D100). */
|
|
11
|
-
export type GuestPrincipal = {
|
|
12
|
-
readonly kind: 'guest';
|
|
13
|
-
readonly guestId: string;
|
|
14
|
-
};
|
|
15
|
-
/** One account in the directory an engine is given: who exists and the app roles they hold (V2c, D100). */
|
|
16
|
-
export type AccountRecord = {
|
|
17
|
-
readonly id: string;
|
|
18
|
-
readonly displayName: string;
|
|
19
|
-
readonly roles: readonly string[];
|
|
20
|
-
};
|
|
21
|
-
/** Who the Quad accounts are: live, the control plane; in the shell and tests, a fixed list. */
|
|
22
|
-
export interface AccountDirectory {
|
|
23
|
-
get(id: string): Promise<AccountRecord | undefined>;
|
|
24
|
-
/** Every account holding an app role, matched ignoring case. */
|
|
25
|
-
withRole(role: string): Promise<readonly AccountRecord[]>;
|
|
26
|
-
}
|
|
27
|
-
/** A directory over a fixed list of accounts. */
|
|
28
|
-
export declare function staticDirectory(accounts: readonly AccountRecord[]): AccountDirectory;
|
|
29
|
-
/** Who a call is made as: a school user or the system (D2, D8), a Quad account or a guest. */
|
|
30
|
-
export type ActingAs = Principal | AccountPrincipal | GuestPrincipal;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/** A directory over a fixed list of accounts. */
|
|
2
|
-
export function staticDirectory(accounts) {
|
|
3
|
-
const byId = new Map(accounts.map((a) => [a.id, a]));
|
|
4
|
-
return {
|
|
5
|
-
async get(id) {
|
|
6
|
-
return byId.get(id);
|
|
7
|
-
},
|
|
8
|
-
async withRole(role) {
|
|
9
|
-
return accounts.filter((a) => a.roles.some((r) => r.toLowerCase() === role.toLowerCase()));
|
|
10
|
-
},
|
|
11
|
-
};
|
|
12
|
-
}
|