@pairz/directory 0.0.1 → 1.0.0-rc.82
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/LICENSE +21 -0
- package/README.md +70 -1
- package/WORDS.md +198 -0
- package/actions/code.d.ts +4 -0
- package/actions/code.js +135 -0
- package/actions/host.d.ts +3 -0
- package/actions/host.js +90 -0
- package/actions/invite.d.ts +5 -0
- package/actions/invite.js +129 -0
- package/actions/org.d.ts +10 -0
- package/actions/org.js +273 -0
- package/collections/credit-code.d.ts +107 -0
- package/collections/credit-code.js +28 -0
- package/collections/invite.d.ts +74 -0
- package/collections/invite.js +20 -0
- package/collections/membership.d.ts +65 -0
- package/collections/membership.js +22 -0
- package/collections/org-host.d.ts +72 -0
- package/collections/org-host.js +45 -0
- package/collections/org.d.ts +134 -0
- package/collections/org.js +58 -0
- package/collections/staff.d.ts +35 -0
- package/collections/staff.js +12 -0
- package/contract.d.ts +8 -0
- package/contract.js +8 -0
- package/mod.d.ts +69 -0
- package/mod.js +126 -0
- package/package.json +63 -3
- package/routes/console.d.ts +26 -0
- package/routes/console.js +129 -0
- package/routes/enter.d.ts +18 -0
- package/routes/enter.js +47 -0
- package/routes/forward.d.ts +35 -0
- package/routes/forward.js +120 -0
- package/routes/orgs.d.ts +25 -0
- package/routes/orgs.js +113 -0
- package/routes/signout.d.ts +11 -0
- package/routes/signout.js +36 -0
- package/shared/enter.d.ts +36 -0
- package/shared/enter.js +82 -0
- package/shared/http.d.ts +44 -0
- package/shared/http.js +79 -0
- package/shared/member.d.ts +21 -0
- package/shared/member.js +93 -0
- package/shared/settings.d.ts +53 -0
- package/shared/settings.js +16 -0
- package/shared/state.d.ts +75 -0
- package/shared/state.js +107 -0
- package/views/web/codes.d.ts +27 -0
- package/views/web/codes.js +50 -0
- package/views/web/console-codes.d.ts +37 -0
- package/views/web/console-codes.js +166 -0
- package/views/web/console.d.ts +37 -0
- package/views/web/console.js +224 -0
- package/views/web/lists.d.ts +48 -0
- package/views/web/lists.js +42 -0
- package/views/web/org.d.ts +12 -0
- package/views/web/org.js +62 -0
- package/views/web/orgs.d.ts +148 -0
- package/views/web/orgs.js +253 -0
- package/views/web/style.d.ts +6 -0
- package/views/web/style.js +197 -0
- package/views/web/switcher.d.ts +81 -0
- package/views/web/switcher.js +367 -0
- package/views/web/words.d.ts +10 -0
- package/views/web/words.js +206 -0
- package/web.d.ts +36 -0
- package/web.js +57 -0
package/shared/http.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The directory's HTTP, in words its routes share: its cookies' names, how
|
|
3
|
+
* it answers, and requests it makes of an org on someone's behalf.
|
|
4
|
+
*
|
|
5
|
+
* - **Cookies**, by the names the directory's runtime gives them
|
|
6
|
+
* (`__Host-` wherever they're Secure): its session (identity's `session`,
|
|
7
|
+
* which the directory runtime names `dir`), the org a browser is in
|
|
8
|
+
* (`org`), and each org's session (`s_<org>`, the name that org's runtime
|
|
9
|
+
* gives identity's `session`, see member.ts). Today's names, so nobody
|
|
10
|
+
* signs in again.
|
|
11
|
+
*/
|
|
12
|
+
import { type Runtime, type Runtimes } from "pairz";
|
|
13
|
+
/** The directory's session: identity's, which the directory's runtime names `dir`. */
|
|
14
|
+
export declare const SESSION = "session";
|
|
15
|
+
/** Which org a browser is in. */
|
|
16
|
+
export declare const CURRENT = "org";
|
|
17
|
+
/** An org's session, as its runtime names identity's (`s_<org>`). */
|
|
18
|
+
export declare const sessionOf: (org: string) => string;
|
|
19
|
+
/** An org's session cookie's name, when its id can be in one (a cookie's name may hold little). */
|
|
20
|
+
export declare const orgCookie: (org: string) => string | undefined;
|
|
21
|
+
/** The orgs a request holds a session for, by its cookies' names (`s_<org>`, `__Host-` or not). */
|
|
22
|
+
export declare const heldOrgs: (request: Request) => string[];
|
|
23
|
+
/** An org's address: `/o/<org>`, where its page, its socket and its webhooks are. */
|
|
24
|
+
export declare const baseOf: (org: string) => string;
|
|
25
|
+
/** Where the front asks whose a host is. */
|
|
26
|
+
export declare const RESOLVE = "/__pairz/hosts/resolve";
|
|
27
|
+
/** Where it asked before 1.0: answered too through 1.x, so a front moves once the app runs 1.0. */
|
|
28
|
+
export declare const RESOLVE_BEFORE = "/__copilotz/hosts/resolve";
|
|
29
|
+
/** What the directory answers in JSON is someone's: never kept. */
|
|
30
|
+
export declare const json: (value: unknown, status?: number) => Response;
|
|
31
|
+
/**
|
|
32
|
+
* Whether a GET is a browser navigating (`Sec-Fetch-Mode: navigate`), or,
|
|
33
|
+
* from one that doesn't say, one asking for a page (`Accept` with HTML).
|
|
34
|
+
*/
|
|
35
|
+
export declare function navigating(request: Request): boolean;
|
|
36
|
+
/** The orgs' runtimes the app gave the directory. */
|
|
37
|
+
export declare function orgsOf(runtime: Pick<Runtime, "describe">): Runtimes;
|
|
38
|
+
/**
|
|
39
|
+
* A request the directory makes of an org for someone (entering it): on the
|
|
40
|
+
* same site, with their cookies and the front's headers, from the address
|
|
41
|
+
* they came from; never with where their browser said it came from (the
|
|
42
|
+
* directory's own, not another site's).
|
|
43
|
+
*/
|
|
44
|
+
export declare function onBehalf(request: Request, path: string): Request;
|
package/shared/http.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The directory's HTTP, in words its routes share: its cookies' names, how
|
|
3
|
+
* it answers, and requests it makes of an org on someone's behalf.
|
|
4
|
+
*
|
|
5
|
+
* - **Cookies**, by the names the directory's runtime gives them
|
|
6
|
+
* (`__Host-` wherever they're Secure): its session (identity's `session`,
|
|
7
|
+
* which the directory runtime names `dir`), the org a browser is in
|
|
8
|
+
* (`org`), and each org's session (`s_<org>`, the name that org's runtime
|
|
9
|
+
* gives identity's `session`, see member.ts). Today's names, so nobody
|
|
10
|
+
* signs in again.
|
|
11
|
+
*/
|
|
12
|
+
import { clientAddress, setClientAddress } from "pairz";
|
|
13
|
+
import { settingsOf } from "./settings.js";
|
|
14
|
+
/** The directory's session: identity's, which the directory's runtime names `dir`. */
|
|
15
|
+
export const SESSION = "session";
|
|
16
|
+
/** Which org a browser is in. */
|
|
17
|
+
export const CURRENT = "org";
|
|
18
|
+
/** An org's session, as its runtime names identity's (`s_<org>`). */
|
|
19
|
+
export const sessionOf = (org) => `s_${org}`;
|
|
20
|
+
/** An org's session cookie's name, when its id can be in one (a cookie's name may hold little). */
|
|
21
|
+
export const orgCookie = (org) => /^[A-Za-z0-9_-]{1,62}$/.test(org) ? sessionOf(org) : undefined;
|
|
22
|
+
/** The orgs a request holds a session for, by its cookies' names (`s_<org>`, `__Host-` or not). */
|
|
23
|
+
export const heldOrgs = (request) => [
|
|
24
|
+
...new Set((request.headers.get("cookie") ?? "").split(/;\s*/).flatMap((pair) => {
|
|
25
|
+
const found = /^(?:__Host-)?s_([A-Za-z0-9_-]{1,62})=/.exec(pair);
|
|
26
|
+
return found ? [found[1]] : [];
|
|
27
|
+
})),
|
|
28
|
+
];
|
|
29
|
+
/** An org's address: `/o/<org>`, where its page, its socket and its webhooks are. */
|
|
30
|
+
export const baseOf = (org) => `/o/${encodeURIComponent(org)}`;
|
|
31
|
+
/** Where the front asks whose a host is. */
|
|
32
|
+
export const RESOLVE = "/__pairz/hosts/resolve";
|
|
33
|
+
/** Where it asked before 1.0: answered too through 1.x, so a front moves once the app runs 1.0. */
|
|
34
|
+
export const RESOLVE_BEFORE = "/__copilotz/hosts/resolve"; // legacy name: the route before 1.0
|
|
35
|
+
/** What the directory answers in JSON is someone's: never kept. */
|
|
36
|
+
export const json = (value, status = 200) => new Response(JSON.stringify(value), {
|
|
37
|
+
status,
|
|
38
|
+
headers: { "content-type": "application/json", "cache-control": "no-store" },
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Whether a GET is a browser navigating (`Sec-Fetch-Mode: navigate`), or,
|
|
42
|
+
* from one that doesn't say, one asking for a page (`Accept` with HTML).
|
|
43
|
+
*/
|
|
44
|
+
export function navigating(request) {
|
|
45
|
+
if (request.method !== "GET")
|
|
46
|
+
return false;
|
|
47
|
+
const mode = request.headers.get("sec-fetch-mode");
|
|
48
|
+
if (mode !== null)
|
|
49
|
+
return mode === "navigate";
|
|
50
|
+
return /text\/html/.test(request.headers.get("accept") ?? "");
|
|
51
|
+
}
|
|
52
|
+
/** The orgs' runtimes the app gave the directory. */
|
|
53
|
+
export function orgsOf(runtime) {
|
|
54
|
+
const orgs = settingsOf({ resources: runtime.describe().resources }).orgs;
|
|
55
|
+
if (!orgs) {
|
|
56
|
+
throw new Error("directory: give it the orgs' runtimes (directory.orgs: runtimes(open))");
|
|
57
|
+
}
|
|
58
|
+
return orgs;
|
|
59
|
+
}
|
|
60
|
+
/** Headers a browser sets that say where a request comes from: not the directory's to pass on. */
|
|
61
|
+
const ITS_OWN = /^(?:origin|referer|sec-fetch-.+|content-type|content-length)$/;
|
|
62
|
+
/**
|
|
63
|
+
* A request the directory makes of an org for someone (entering it): on the
|
|
64
|
+
* same site, with their cookies and the front's headers, from the address
|
|
65
|
+
* they came from; never with where their browser said it came from (the
|
|
66
|
+
* directory's own, not another site's).
|
|
67
|
+
*/
|
|
68
|
+
export function onBehalf(request, path) {
|
|
69
|
+
const headers = new Headers();
|
|
70
|
+
for (const [name, value] of request.headers) {
|
|
71
|
+
if (!ITS_OWN.test(name))
|
|
72
|
+
headers.set(name, value);
|
|
73
|
+
}
|
|
74
|
+
const made = new Request(new URL(path, request.url), { method: "POST", headers });
|
|
75
|
+
const address = clientAddress(request);
|
|
76
|
+
if (address)
|
|
77
|
+
setClientAddress(made, address);
|
|
78
|
+
return made;
|
|
79
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `member(dir, org)`: the plugin each org's runtime gets from the directory
|
|
3
|
+
* (`dir`, its runtime) that serves it, beside the app's own plugins:
|
|
4
|
+
*
|
|
5
|
+
* - **Which org it is** (`resources.org`: its id, name, kind and address,
|
|
6
|
+
* `/o/<org>`), and **where it's served** (`page.base`: its page, its
|
|
7
|
+
* `/connect` and its socket at its address).
|
|
8
|
+
* - **Its cookies' names** (`resources.cookies`): identity's session goes by
|
|
9
|
+
* `s_<org>`, any other cookie by `<name>_<org>` (`visitor_<org>`), so one
|
|
10
|
+
* org's never reads another's. Today's names.
|
|
11
|
+
* - **The app's own hosts** (`resources.hosts`, the directory's): a host an
|
|
12
|
+
* org claimed reaches only its host routes.
|
|
13
|
+
* - **How it reaches the directory** (`resources.directory`): invitations,
|
|
14
|
+
* who leaves, its name, the hosts and names it claims, and gift codes.
|
|
15
|
+
* - **Its `/enter`**, which only the directory's handoff opens (see
|
|
16
|
+
* routes/enter.ts).
|
|
17
|
+
*/
|
|
18
|
+
import { type Plugin, type Runtime } from "pairz";
|
|
19
|
+
import type { OrgInfo } from "./settings.js";
|
|
20
|
+
/** What an org's runtime is given of the directory: `dir`, the directory's runtime, and the org. */
|
|
21
|
+
export declare function member(dir: Pick<Runtime, "call" | "describe">, org: Pick<OrgInfo, "id" | "name" | "kind">): Plugin;
|
package/shared/member.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `member(dir, org)`: the plugin each org's runtime gets from the directory
|
|
3
|
+
* (`dir`, its runtime) that serves it, beside the app's own plugins:
|
|
4
|
+
*
|
|
5
|
+
* - **Which org it is** (`resources.org`: its id, name, kind and address,
|
|
6
|
+
* `/o/<org>`), and **where it's served** (`page.base`: its page, its
|
|
7
|
+
* `/connect` and its socket at its address).
|
|
8
|
+
* - **Its cookies' names** (`resources.cookies`): identity's session goes by
|
|
9
|
+
* `s_<org>`, any other cookie by `<name>_<org>` (`visitor_<org>`), so one
|
|
10
|
+
* org's never reads another's. Today's names.
|
|
11
|
+
* - **The app's own hosts** (`resources.hosts`, the directory's): a host an
|
|
12
|
+
* org claimed reaches only its host routes.
|
|
13
|
+
* - **How it reaches the directory** (`resources.directory`): invitations,
|
|
14
|
+
* who leaves, its name, the hosts and names it claims, and gift codes.
|
|
15
|
+
* - **Its `/enter`**, which only the directory's handoff opens (see
|
|
16
|
+
* routes/enter.ts).
|
|
17
|
+
*/
|
|
18
|
+
import { definePlugin, forbidden, ownHost, } from "pairz";
|
|
19
|
+
import { enterRoute } from "../routes/enter.js";
|
|
20
|
+
import { baseOf, sessionOf } from "./http.js";
|
|
21
|
+
import { forget, HANDOFF, plainHost } from "./state.js";
|
|
22
|
+
/** What an org's runtime is given of the directory: `dir`, the directory's runtime, and the org. */
|
|
23
|
+
export function member(dir, org) {
|
|
24
|
+
const resources = dir.describe().resources;
|
|
25
|
+
const hosts = resources.hosts;
|
|
26
|
+
const base = baseOf(org.id);
|
|
27
|
+
const ask = (action, input = {}) => dir.call(action, { org: org.id, ...input });
|
|
28
|
+
return definePlugin({
|
|
29
|
+
name: "directory-member",
|
|
30
|
+
// Its `/enter` starts identity's session, and sets identity's cookie.
|
|
31
|
+
requires: ["identity"],
|
|
32
|
+
routes: { [HANDOFF]: enterRoute(resources, org) },
|
|
33
|
+
resources: {
|
|
34
|
+
org: { id: org.id, name: org.name, kind: org.kind, base },
|
|
35
|
+
page: { base },
|
|
36
|
+
cookies: { names: { session: sessionOf(org.id) }, suffix: `_${org.id}` },
|
|
37
|
+
...(hosts ? { hosts } : {}),
|
|
38
|
+
directory: {
|
|
39
|
+
invite: (invite) => ask("invite.send", invite).then(() => { }),
|
|
40
|
+
invites: () => ask("invite.org").then((r) => r.invites),
|
|
41
|
+
cancel: (email) => ask("invite.cancel", { email }).then(() => { }),
|
|
42
|
+
leave: (person) => ask("org.leave", { person }).then(() => { }),
|
|
43
|
+
rename: (name) => ask("org.rename", { name }).then(() => forget(resources, { org: org.id })),
|
|
44
|
+
/**
|
|
45
|
+
* Makes a host the org's: its requests are its routes' by host (a
|
|
46
|
+
* preview's), or, with a `target`, the front sends them there (an
|
|
47
|
+
* app's service). Refused for the app's own hosts, and another org's.
|
|
48
|
+
*/
|
|
49
|
+
claimHost: async (host, claim = {}) => {
|
|
50
|
+
const name = plainHost(host);
|
|
51
|
+
if (ownHost(hosts, name))
|
|
52
|
+
throw forbidden(`${name} is the app's own`);
|
|
53
|
+
await ask("host.claim", {
|
|
54
|
+
host: name,
|
|
55
|
+
...(claim.target ? { target: claim.target } : {}),
|
|
56
|
+
});
|
|
57
|
+
forget(resources, { host: name });
|
|
58
|
+
},
|
|
59
|
+
/** The host is no longer the org's. */
|
|
60
|
+
releaseHost: async (host) => {
|
|
61
|
+
const name = plainHost(host);
|
|
62
|
+
await ask("host.release", { host: name });
|
|
63
|
+
forget(resources, { host: name });
|
|
64
|
+
},
|
|
65
|
+
/**
|
|
66
|
+
* Makes a name the org's: a host's first label, on every domain
|
|
67
|
+
* (`shop` for `shop.pairz.app` and `shop.copilotz.app`), its requests
|
|
68
|
+
* its routes' by host; one across the whole app. Refused when it's
|
|
69
|
+
* another org's.
|
|
70
|
+
*/
|
|
71
|
+
claimName: async (name, claim = {}) => {
|
|
72
|
+
await ask("host.claim", {
|
|
73
|
+
host: `${name}.*`,
|
|
74
|
+
...(claim.preview ? { preview: claim.preview } : {}),
|
|
75
|
+
});
|
|
76
|
+
forget(resources, { name });
|
|
77
|
+
},
|
|
78
|
+
/** The name is no longer the org's. */
|
|
79
|
+
releaseName: async (name) => {
|
|
80
|
+
await ask("host.release", { host: `${name}.*` });
|
|
81
|
+
forget(resources, { name });
|
|
82
|
+
},
|
|
83
|
+
/** Whether the org may claim a name: nobody's, or its own already. */
|
|
84
|
+
nameFree: (name) => ask("host.free", { host: `${name}.*` }).then((r) => r.free),
|
|
85
|
+
/** A gift code's credits, for this org. */
|
|
86
|
+
claimCode: (code, person) => ask("code.claim", {
|
|
87
|
+
code,
|
|
88
|
+
person,
|
|
89
|
+
}),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The directory's settings, its `directory` resource:
|
|
3
|
+
*
|
|
4
|
+
* - `pool`: the schema new orgs share ("orgs" by default; a big org is
|
|
5
|
+
* placed apart by changing its placement);
|
|
6
|
+
* - `staff`: the emails of platform staff to start with;
|
|
7
|
+
* - `orgs`: the orgs' runtimes (`runtimes(open)`, the runtime's pool), the
|
|
8
|
+
* app's: each opened with the org's own store and plugins, and
|
|
9
|
+
* `member(dir, org)`. The directory's routes forward to them;
|
|
10
|
+
* - `lobby`: what answers someone signed in nowhere (the sign-in page): a
|
|
11
|
+
* runtime with the page (`webPage`), or any fetch handler. Without one,
|
|
12
|
+
* they're told to sign in (401);
|
|
13
|
+
* - `operations`: what staff may do to an org's rows (its stores know where
|
|
14
|
+
* they live): move them to another placement, export them, drop them.
|
|
15
|
+
* The directory closes the org first where it must.
|
|
16
|
+
*/
|
|
17
|
+
import type { ReadContext, Runtimes } from "pairz";
|
|
18
|
+
/** An org as the directory opens it: its name, its kind, where its rows live. */
|
|
19
|
+
export type OrgInfo = {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
kind: "personal" | "team";
|
|
23
|
+
placement: {
|
|
24
|
+
schema: string;
|
|
25
|
+
};
|
|
26
|
+
suspended?: true;
|
|
27
|
+
};
|
|
28
|
+
/** What staff may do to an org's rows: the app's, as its stores know where they live. */
|
|
29
|
+
export type Operations = {
|
|
30
|
+
move?(org: OrgInfo, to: OrgInfo["placement"]): Promise<void>;
|
|
31
|
+
export?(org: OrgInfo): ReadableStream<Uint8Array> | Promise<ReadableStream<Uint8Array>>;
|
|
32
|
+
drop?(org: OrgInfo): Promise<void>;
|
|
33
|
+
};
|
|
34
|
+
export type DirectorySettings = {
|
|
35
|
+
pool?: string;
|
|
36
|
+
staff?: string[];
|
|
37
|
+
orgs?: Runtimes;
|
|
38
|
+
lobby?: {
|
|
39
|
+
fetch(request: Request): Response | Promise<Response>;
|
|
40
|
+
};
|
|
41
|
+
operations?: Operations;
|
|
42
|
+
};
|
|
43
|
+
export declare const settingsOf: (ctx: {
|
|
44
|
+
resources: ReadContext["resources"];
|
|
45
|
+
}) => DirectorySettings;
|
|
46
|
+
/** Where a new org's rows go: the shared schema, as its own tenant. */
|
|
47
|
+
export declare const placementOf: (ctx: {
|
|
48
|
+
resources: ReadContext["resources"];
|
|
49
|
+
}, _org: string) => {
|
|
50
|
+
schema: string;
|
|
51
|
+
};
|
|
52
|
+
/** Whether someone is platform staff: by their sign-in, or the email they verified. */
|
|
53
|
+
export declare function isStaff(ctx: ReadContext, person: string, email?: string): Promise<boolean>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const settingsOf = (ctx) => (ctx.resources.directory ?? {});
|
|
2
|
+
/** Where a new org's rows go: the shared schema, as its own tenant. */
|
|
3
|
+
export const placementOf = (ctx, _org) => ({
|
|
4
|
+
schema: settingsOf(ctx).pool ?? "orgs",
|
|
5
|
+
});
|
|
6
|
+
/** Whether someone is platform staff: by their sign-in, or the email they verified. */
|
|
7
|
+
export async function isStaff(ctx, person, email) {
|
|
8
|
+
const listed = (settingsOf(ctx).staff ?? []).map((e) => e.toLowerCase());
|
|
9
|
+
const known = email ? email.toLowerCase() : undefined;
|
|
10
|
+
if (known && listed.includes(known))
|
|
11
|
+
return true;
|
|
12
|
+
if (listed.includes(person.toLowerCase()))
|
|
13
|
+
return true;
|
|
14
|
+
const kept = ctx.db.collection("staff");
|
|
15
|
+
return !!(await kept.findOne({ _id: person }) || (known && await kept.findOne({ _id: known })));
|
|
16
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a directory keeps in memory, beside its store, shared by its routes
|
|
3
|
+
* and the orgs it opened (`member(dir, org)`): one per directory runtime,
|
|
4
|
+
* by its resources (the same object for its routes and for the app's `dir`).
|
|
5
|
+
*
|
|
6
|
+
* - **The handoff's key**: entering an org, the directory signs who goes in
|
|
7
|
+
* (`/enter`, see member.ts) with a key of its own, made at start; the org
|
|
8
|
+
* checks it. A handoff lives a minute, and never leaves the process.
|
|
9
|
+
* - **Orgs as the directory has them** (`org.info`), kept 10 seconds, and
|
|
10
|
+
* **hosts orgs claimed** (`orgHost.resolve`), a minute: every request asks.
|
|
11
|
+
* What changes them here (a rename, a suspension, a claim) forgets them
|
|
12
|
+
* at once.
|
|
13
|
+
* - **When each org last told how it is** (`org.report`): at most once a
|
|
14
|
+
* minute while it's used.
|
|
15
|
+
*/
|
|
16
|
+
import { type LinksSettings, type Resources } from "pairz";
|
|
17
|
+
import type { OrgInfo } from "./settings.js";
|
|
18
|
+
/** Whose a claimed host is, and where the front sends it. */
|
|
19
|
+
export type Claim = {
|
|
20
|
+
org: string;
|
|
21
|
+
target?: string;
|
|
22
|
+
};
|
|
23
|
+
/** A query, as a route's runtime and the app's `dir` both ask it. */
|
|
24
|
+
type Asks = {
|
|
25
|
+
query<R = unknown>(name: string, params?: unknown): Promise<R>;
|
|
26
|
+
};
|
|
27
|
+
type Kept<T> = Map<string, {
|
|
28
|
+
value: Promise<T>;
|
|
29
|
+
until: number;
|
|
30
|
+
}>;
|
|
31
|
+
type State = {
|
|
32
|
+
handoff: LinksSettings;
|
|
33
|
+
info: Kept<OrgInfo | null>;
|
|
34
|
+
claims: Kept<Claim | null>;
|
|
35
|
+
reported: Map<string, number>;
|
|
36
|
+
};
|
|
37
|
+
/** How long a handoff is good for. */
|
|
38
|
+
export declare const HANDOFF_MS = 60000;
|
|
39
|
+
/** What a handoff signs: the org's own route, under its address. */
|
|
40
|
+
export declare const HANDOFF = "/enter";
|
|
41
|
+
/** The directory's state, by its resources. */
|
|
42
|
+
export declare function stateOf(resources: Resources): State;
|
|
43
|
+
/** An org as the directory has it, kept a little while. */
|
|
44
|
+
export declare const infoOf: (dir: Asks, resources: Resources, org: string) => Promise<OrgInfo | null>;
|
|
45
|
+
/** A host as claims keep it: lowercase, no trailing dot. */
|
|
46
|
+
export declare const plainHost: (host: string) => string;
|
|
47
|
+
/** Whose a host is, kept a minute. */
|
|
48
|
+
export declare const claimOf: (dir: Asks, resources: Resources, host: string) => Promise<Claim | null>;
|
|
49
|
+
/** Forgets what's kept of an org, a host, or a name (every host it's the first label of) here: it just changed. */
|
|
50
|
+
export declare function forget(resources: Resources, what: {
|
|
51
|
+
org?: string;
|
|
52
|
+
host?: string;
|
|
53
|
+
name?: string;
|
|
54
|
+
}): void;
|
|
55
|
+
/** Who goes into an org: what the directory signs, and the org's `/enter` reads. */
|
|
56
|
+
export type Handoff = {
|
|
57
|
+
org: string;
|
|
58
|
+
person: string;
|
|
59
|
+
email?: string;
|
|
60
|
+
name?: string;
|
|
61
|
+
picture?: string;
|
|
62
|
+
/** The role they join with, the first time. */
|
|
63
|
+
role?: string;
|
|
64
|
+
/** As platform staff. */
|
|
65
|
+
staff?: boolean;
|
|
66
|
+
/** Where the browser goes then (a path on the app): a redirect; none, `{ org }`. */
|
|
67
|
+
to?: string;
|
|
68
|
+
};
|
|
69
|
+
/** A handoff, signed: the query of the org's `/enter`. */
|
|
70
|
+
export declare function signHandoff(resources: Resources, handoff: Handoff): Promise<string>;
|
|
71
|
+
/** A handoff the directory signed, from an `/enter` request's query; undefined for any other. */
|
|
72
|
+
export declare function readHandoff(resources: Resources, query: URLSearchParams): Promise<Handoff | undefined>;
|
|
73
|
+
/** Whether an org's report is due (at most once a minute), counting this one. */
|
|
74
|
+
export declare function reportDue(resources: Resources, org: string): boolean;
|
|
75
|
+
export {};
|
package/shared/state.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a directory keeps in memory, beside its store, shared by its routes
|
|
3
|
+
* and the orgs it opened (`member(dir, org)`): one per directory runtime,
|
|
4
|
+
* by its resources (the same object for its routes and for the app's `dir`).
|
|
5
|
+
*
|
|
6
|
+
* - **The handoff's key**: entering an org, the directory signs who goes in
|
|
7
|
+
* (`/enter`, see member.ts) with a key of its own, made at start; the org
|
|
8
|
+
* checks it. A handoff lives a minute, and never leaves the process.
|
|
9
|
+
* - **Orgs as the directory has them** (`org.info`), kept 10 seconds, and
|
|
10
|
+
* **hosts orgs claimed** (`orgHost.resolve`), a minute: every request asks.
|
|
11
|
+
* What changes them here (a rename, a suspension, a claim) forgets them
|
|
12
|
+
* at once.
|
|
13
|
+
* - **When each org last told how it is** (`org.report`): at most once a
|
|
14
|
+
* minute while it's used.
|
|
15
|
+
*/
|
|
16
|
+
import { signLink, verifyLink } from "pairz";
|
|
17
|
+
const INFO_MS = 10_000;
|
|
18
|
+
const CLAIMS_MS = 60_000;
|
|
19
|
+
/** How long a handoff is good for. */
|
|
20
|
+
export const HANDOFF_MS = 60_000;
|
|
21
|
+
/** What a handoff signs: the org's own route, under its address. */
|
|
22
|
+
export const HANDOFF = "/enter";
|
|
23
|
+
const states = new WeakMap();
|
|
24
|
+
/** The directory's state, by its resources. */
|
|
25
|
+
export function stateOf(resources) {
|
|
26
|
+
let state = states.get(resources);
|
|
27
|
+
if (!state) {
|
|
28
|
+
const bytes = crypto.getRandomValues(new Uint8Array(32));
|
|
29
|
+
state = {
|
|
30
|
+
handoff: {
|
|
31
|
+
origin: "https://directory.invalid",
|
|
32
|
+
secret: Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""),
|
|
33
|
+
},
|
|
34
|
+
info: new Map(),
|
|
35
|
+
claims: new Map(),
|
|
36
|
+
reported: new Map(),
|
|
37
|
+
};
|
|
38
|
+
states.set(resources, state);
|
|
39
|
+
}
|
|
40
|
+
return state;
|
|
41
|
+
}
|
|
42
|
+
/** A value kept a while, asked again once it's past (or failed). */
|
|
43
|
+
function kept(map, key, ms, ask) {
|
|
44
|
+
const found = map.get(key);
|
|
45
|
+
if (found && found.until > Date.now())
|
|
46
|
+
return found.value;
|
|
47
|
+
const value = ask();
|
|
48
|
+
map.set(key, { value, until: Date.now() + ms });
|
|
49
|
+
value.catch(() => map.delete(key));
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
/** An org as the directory has it, kept a little while. */
|
|
53
|
+
export const infoOf = (dir, resources, org) => kept(stateOf(resources).info, org, INFO_MS, () => dir.query("org.info", { org }));
|
|
54
|
+
/** A host as claims keep it: lowercase, no trailing dot. */
|
|
55
|
+
export const plainHost = (host) => String(host ?? "").trim().toLowerCase().replace(/\.$/, "");
|
|
56
|
+
/** Whose a host is, kept a minute. */
|
|
57
|
+
export const claimOf = (dir, resources, host) => kept(stateOf(resources).claims, plainHost(host), CLAIMS_MS, () => dir.query("orgHost.resolve", { host: plainHost(host) }));
|
|
58
|
+
/** Forgets what's kept of an org, a host, or a name (every host it's the first label of) here: it just changed. */
|
|
59
|
+
export function forget(resources, what) {
|
|
60
|
+
const state = stateOf(resources);
|
|
61
|
+
if (what.org !== undefined)
|
|
62
|
+
state.info.delete(what.org);
|
|
63
|
+
if (what.host !== undefined)
|
|
64
|
+
state.claims.delete(plainHost(what.host));
|
|
65
|
+
if (what.name !== undefined) {
|
|
66
|
+
for (const host of state.claims.keys()) {
|
|
67
|
+
if (host.split(".")[0] === what.name)
|
|
68
|
+
state.claims.delete(host);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** A handoff, signed: the query of the org's `/enter`. */
|
|
73
|
+
export async function signHandoff(resources, handoff) {
|
|
74
|
+
const params = {};
|
|
75
|
+
for (const [name, value] of Object.entries(handoff)) {
|
|
76
|
+
if (value === undefined || value === false || value === "")
|
|
77
|
+
continue;
|
|
78
|
+
params[name] = value === true ? "1" : String(value);
|
|
79
|
+
}
|
|
80
|
+
const link = await signLink(stateOf(resources).handoff, HANDOFF, params, HANDOFF_MS);
|
|
81
|
+
return new URL(link).search;
|
|
82
|
+
}
|
|
83
|
+
/** A handoff the directory signed, from an `/enter` request's query; undefined for any other. */
|
|
84
|
+
export async function readHandoff(resources, query) {
|
|
85
|
+
const params = await verifyLink(stateOf(resources).handoff, HANDOFF, Object.fromEntries(query));
|
|
86
|
+
if (!params?.org || !params.person)
|
|
87
|
+
return undefined;
|
|
88
|
+
return {
|
|
89
|
+
org: params.org,
|
|
90
|
+
person: params.person,
|
|
91
|
+
...(params.email ? { email: params.email } : {}),
|
|
92
|
+
...(params.name ? { name: params.name } : {}),
|
|
93
|
+
...(params.picture ? { picture: params.picture } : {}),
|
|
94
|
+
...(params.role ? { role: params.role } : {}),
|
|
95
|
+
...(params.staff === "1" ? { staff: true } : {}),
|
|
96
|
+
...(params.to ? { to: params.to } : {}),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** Whether an org's report is due (at most once a minute), counting this one. */
|
|
100
|
+
export function reportDue(resources, org) {
|
|
101
|
+
const reported = stateOf(resources).reported;
|
|
102
|
+
const now = Date.now();
|
|
103
|
+
if (now - (reported.get(org) ?? 0) < 60_000)
|
|
104
|
+
return false;
|
|
105
|
+
reported.set(org, now);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { GiftCode } from "./orgs.js";
|
|
2
|
+
export type CodeStatus = "waiting" | "claimed" | "revoked" | "expired";
|
|
3
|
+
/** Where a code stands: claimed, taken back, past its last moment, or still waiting. */
|
|
4
|
+
export declare function codeStatus(code: GiftCode, now?: string): CodeStatus;
|
|
5
|
+
/** The status as a label (said now, in the app's words), with the toolkit's tone for it. */
|
|
6
|
+
export declare const statusOf: (status: CodeStatus) => {
|
|
7
|
+
label: string;
|
|
8
|
+
tone: "success" | "neutral" | "primary" | "warning";
|
|
9
|
+
};
|
|
10
|
+
/** What the form asks for, as `POST /console/codes` takes it; or why it can't be. */
|
|
11
|
+
export declare function newCodeOf(form: {
|
|
12
|
+
amount: string;
|
|
13
|
+
expires: string;
|
|
14
|
+
until: string;
|
|
15
|
+
note: string;
|
|
16
|
+
}): {
|
|
17
|
+
amount: number;
|
|
18
|
+
expires?: string;
|
|
19
|
+
until?: string;
|
|
20
|
+
note?: string;
|
|
21
|
+
} | {
|
|
22
|
+
error: string;
|
|
23
|
+
};
|
|
24
|
+
/** A day (`YYYY-MM-DD`, or a moment's), as the calendar has it, wherever you are. */
|
|
25
|
+
export declare const dayOf: (at: string) => string;
|
|
26
|
+
/** US dollars, cents only when there are some. */
|
|
27
|
+
export declare const dollarsOf: (n: number) => string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gift codes in staff's console, as words: where each stands, and what a person
|
|
3
|
+
* types turned into what's asked for. Pure.
|
|
4
|
+
*/
|
|
5
|
+
import { formatDate, formatNumber } from "pairz/web/words";
|
|
6
|
+
import { say } from "./words.js";
|
|
7
|
+
/** Where a code stands: claimed, taken back, past its last moment, or still waiting. */
|
|
8
|
+
export function codeStatus(code, now = new Date().toISOString()) {
|
|
9
|
+
if (code.org)
|
|
10
|
+
return "claimed";
|
|
11
|
+
if (code.revoked)
|
|
12
|
+
return "revoked";
|
|
13
|
+
if (code.until && code.until <= now)
|
|
14
|
+
return "expired";
|
|
15
|
+
return "waiting";
|
|
16
|
+
}
|
|
17
|
+
/** The status as a label (said now, in the app's words), with the toolkit's tone for it. */
|
|
18
|
+
export const statusOf = (status) => ({
|
|
19
|
+
waiting: { label: say("codeWaiting"), tone: "primary" },
|
|
20
|
+
claimed: { label: say("codeClaimed"), tone: "success" },
|
|
21
|
+
revoked: { label: say("codeRevoked"), tone: "neutral" },
|
|
22
|
+
expired: { label: say("codeExpired"), tone: "neutral" },
|
|
23
|
+
})[status];
|
|
24
|
+
/** What the form asks for, as `POST /console/codes` takes it; or why it can't be. */
|
|
25
|
+
export function newCodeOf(form) {
|
|
26
|
+
const amount = Number(form.amount.trim().replace(/^\$\s*/, "").replace(/,/g, ""));
|
|
27
|
+
if (!Number.isFinite(amount) || amount <= 0)
|
|
28
|
+
return { error: say("sayDollars") };
|
|
29
|
+
return {
|
|
30
|
+
amount: Math.round(amount * 100) / 100,
|
|
31
|
+
...(form.expires ? { expires: form.expires } : {}),
|
|
32
|
+
// A code can be claimed through the whole of its last day.
|
|
33
|
+
...(form.until ? { until: `${form.until}T23:59:59.999Z` } : {}),
|
|
34
|
+
...(form.note.trim() ? { note: form.note.trim() } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** A day (`YYYY-MM-DD`, or a moment's), as the calendar has it, wherever you are. */
|
|
38
|
+
export const dayOf = (at) => formatDate(new Date(`${at.slice(0, 10)}T00:00:00Z`), {
|
|
39
|
+
timeZone: "UTC",
|
|
40
|
+
month: "short",
|
|
41
|
+
day: "numeric",
|
|
42
|
+
year: "numeric",
|
|
43
|
+
});
|
|
44
|
+
/** US dollars, cents only when there are some. */
|
|
45
|
+
export const dollarsOf = (n) => formatNumber(n, {
|
|
46
|
+
style: "currency",
|
|
47
|
+
currency: "USD",
|
|
48
|
+
minimumFractionDigits: Math.round(n * 100) % 100 ? 2 : 0,
|
|
49
|
+
maximumFractionDigits: 2,
|
|
50
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gift codes in staff's console (at `/console?tab=codes`): making one (how many
|
|
3
|
+
* dollars of credits, when they lapse, the last day it can be claimed, what
|
|
4
|
+
* it's for), which shows the code just this once, with a way to copy it and a
|
|
5
|
+
* clear word that it won't be shown again, since only its hash is kept. Then
|
|
6
|
+
* every code, newest first: how much, for what, where it stands, which org
|
|
7
|
+
* claimed it; and taking back one nobody has claimed.
|
|
8
|
+
*/
|
|
9
|
+
import { type ReactNode } from "react";
|
|
10
|
+
import { type ConsoleOrg, type GiftCode } from "./orgs.js";
|
|
11
|
+
export declare function GiftCodes({ orgs, ready }: {
|
|
12
|
+
orgs: ConsoleOrg[];
|
|
13
|
+
ready: boolean;
|
|
14
|
+
}): ReactNode;
|
|
15
|
+
export declare function Row({ code, orgName, onRevoke }: {
|
|
16
|
+
code: GiftCode;
|
|
17
|
+
orgName(id: string): string;
|
|
18
|
+
onRevoke(): void;
|
|
19
|
+
}): ReactNode;
|
|
20
|
+
export declare function NewCode({ open, close, onMade }: {
|
|
21
|
+
open: boolean;
|
|
22
|
+
close(): void;
|
|
23
|
+
onMade(code: string, amount: number): void;
|
|
24
|
+
}): ReactNode;
|
|
25
|
+
/** The code, just this once. */
|
|
26
|
+
export declare function ShownOnce({ made, close }: {
|
|
27
|
+
made?: {
|
|
28
|
+
code: string;
|
|
29
|
+
amount: number;
|
|
30
|
+
};
|
|
31
|
+
close(): void;
|
|
32
|
+
}): ReactNode;
|
|
33
|
+
export declare function Revoke({ code, close, onDone }: {
|
|
34
|
+
code?: GiftCode;
|
|
35
|
+
close(): void;
|
|
36
|
+
onDone(): void;
|
|
37
|
+
}): ReactNode;
|