@copilotz/directory 1.0.0-rc.80 → 1.0.0-rc.81
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 +1 -1
- package/README.md +9 -68
- package/contract.d.ts +1 -8
- package/contract.js +1 -8
- package/mod.d.ts +2 -67
- package/mod.js +2 -122
- package/package.json +9 -35
- package/web.d.ts +2 -36
- package/web.js +2 -57
- package/WORDS.md +0 -198
- package/actions/code.d.ts +0 -4
- package/actions/code.js +0 -135
- package/actions/host.d.ts +0 -2
- package/actions/host.js +0 -55
- package/actions/invite.d.ts +0 -5
- package/actions/invite.js +0 -129
- package/actions/org.d.ts +0 -10
- package/actions/org.js +0 -273
- package/collections/credit-code.d.ts +0 -107
- package/collections/credit-code.js +0 -28
- package/collections/invite.d.ts +0 -74
- package/collections/invite.js +0 -20
- package/collections/membership.d.ts +0 -65
- package/collections/membership.js +0 -22
- package/collections/org-host.d.ts +0 -55
- package/collections/org-host.js +0 -30
- package/collections/org.d.ts +0 -134
- package/collections/org.js +0 -58
- package/collections/staff.d.ts +0 -35
- package/collections/staff.js +0 -12
- package/routes/console.d.ts +0 -26
- package/routes/console.js +0 -129
- package/routes/enter.d.ts +0 -18
- package/routes/enter.js +0 -47
- package/routes/forward.d.ts +0 -35
- package/routes/forward.js +0 -119
- package/routes/orgs.d.ts +0 -25
- package/routes/orgs.js +0 -113
- package/routes/signout.d.ts +0 -11
- package/routes/signout.js +0 -36
- package/shared/enter.d.ts +0 -36
- package/shared/enter.js +0 -82
- package/shared/http.d.ts +0 -42
- package/shared/http.js +0 -77
- package/shared/member.d.ts +0 -21
- package/shared/member.js +0 -73
- package/shared/settings.d.ts +0 -53
- package/shared/settings.js +0 -16
- package/shared/state.d.ts +0 -74
- package/shared/state.js +0 -101
- package/views/web/codes.d.ts +0 -27
- package/views/web/codes.js +0 -50
- package/views/web/console-codes.d.ts +0 -37
- package/views/web/console-codes.js +0 -166
- package/views/web/console.d.ts +0 -37
- package/views/web/console.js +0 -224
- package/views/web/lists.d.ts +0 -48
- package/views/web/lists.js +0 -42
- package/views/web/org.d.ts +0 -12
- package/views/web/org.js +0 -62
- package/views/web/orgs.d.ts +0 -148
- package/views/web/orgs.js +0 -253
- package/views/web/style.d.ts +0 -6
- package/views/web/style.js +0 -197
- package/views/web/switcher.d.ts +0 -81
- package/views/web/switcher.js +0 -367
- package/views/web/words.d.ts +0 -10
- package/views/web/words.js +0 -206
package/routes/orgs.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The orgs of whoever the directory's session is (each a 401 for anyone
|
|
3
|
-
* else; what goes wrong answers its code's status, its words only when
|
|
4
|
-
* they're for anyone):
|
|
5
|
-
*
|
|
6
|
-
* - `GET /orgs`: who's signed in, the org they're in (`current`), the orgs
|
|
7
|
-
* they belong to (the last entered first), the invitations waiting for
|
|
8
|
-
* their email, whether they're platform staff, and whether the session
|
|
9
|
-
* they hold in the org they're in is staff's (`asStaff`);
|
|
10
|
-
* - `POST /orgs` `{ name }`: makes a team org, its maker its owner, and
|
|
11
|
-
* goes into it;
|
|
12
|
-
* - `POST /orgs/enter` `{ org, staff? }`: goes into another (staff into any,
|
|
13
|
-
* as staff when they ask: `staff: true`);
|
|
14
|
-
* - `POST /orgs/invites/answer` `{ invite, accept }`: joins its org (and
|
|
15
|
-
* goes into it) or declines (`{ joined: false }`).
|
|
16
|
-
*
|
|
17
|
-
* Going into an org answers `{ org }`, with its cookies (see shared/enter.ts).
|
|
18
|
-
*/
|
|
19
|
-
import { jsonOf } from "copilotz";
|
|
20
|
-
import { enter, personIn, profileOf } from "../shared/enter.js";
|
|
21
|
-
import { CURRENT, json, orgCookie, orgsOf } from "../shared/http.js";
|
|
22
|
-
import { infoOf } from "../shared/state.js";
|
|
23
|
-
const NOT_JSON = "Send a JSON object, of 16 KiB at most";
|
|
24
|
-
/** Whether the session a request holds in an org is staff's. */
|
|
25
|
-
async function heldAsStaff(ctx, org) {
|
|
26
|
-
const name = orgCookie(org);
|
|
27
|
-
const secret = name ? ctx.cookies.get(name) : undefined;
|
|
28
|
-
if (!secret)
|
|
29
|
-
return false;
|
|
30
|
-
const info = await infoOf(ctx.runtime, ctx.runtime.describe().resources, org);
|
|
31
|
-
if (!info || info.suspended)
|
|
32
|
-
return false;
|
|
33
|
-
const runtime = await orgsOf(ctx.runtime).get(info.id);
|
|
34
|
-
const actor = await runtime?.query("credential.actor", { secret })
|
|
35
|
-
.catch(() => null);
|
|
36
|
-
return actor?.staff === true;
|
|
37
|
-
}
|
|
38
|
-
/** `GET /orgs` and `POST /orgs`. */
|
|
39
|
-
export const orgsRoute = async (request, ctx) => {
|
|
40
|
-
const { runtime } = ctx;
|
|
41
|
-
const person = personIn(ctx);
|
|
42
|
-
if (request.method === "GET") {
|
|
43
|
-
const email = (await profileOf(runtime, person))?.email ?? null;
|
|
44
|
-
const [{ orgs, staff }, { invites }] = await Promise.all([
|
|
45
|
-
runtime.call("org.mine", {
|
|
46
|
-
person,
|
|
47
|
-
...(email ? { email } : {}),
|
|
48
|
-
}),
|
|
49
|
-
email
|
|
50
|
-
? runtime.call("invite.mine", { email })
|
|
51
|
-
: Promise.resolve({ invites: [] }),
|
|
52
|
-
]);
|
|
53
|
-
const current = ctx.cookies.get(CURRENT) ?? null;
|
|
54
|
-
return json({
|
|
55
|
-
person,
|
|
56
|
-
email,
|
|
57
|
-
current,
|
|
58
|
-
orgs,
|
|
59
|
-
invites,
|
|
60
|
-
staff,
|
|
61
|
-
asStaff: staff && !!current && await heldAsStaff(ctx, current),
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
if (request.method !== "POST")
|
|
65
|
-
return json({ error: "Only GET and POST" }, 405);
|
|
66
|
-
const asked = await jsonOf(request);
|
|
67
|
-
if (!asked)
|
|
68
|
-
return json({ error: NOT_JSON }, 400);
|
|
69
|
-
const name = typeof asked.name === "string" ? asked.name.trim() : "";
|
|
70
|
-
if (!name)
|
|
71
|
-
return json({ error: "A name" }, 400);
|
|
72
|
-
const { org } = await runtime.call("org.create", {
|
|
73
|
-
name,
|
|
74
|
-
kind: "team",
|
|
75
|
-
by: person,
|
|
76
|
-
});
|
|
77
|
-
return await enter(request, ctx, org);
|
|
78
|
-
};
|
|
79
|
-
/** `POST /orgs/enter` `{ org, staff? }`. */
|
|
80
|
-
export const enterOrgRoute = async (request, ctx) => {
|
|
81
|
-
if (request.method !== "POST")
|
|
82
|
-
return json({ error: "Only POST" }, 405);
|
|
83
|
-
personIn(ctx);
|
|
84
|
-
const asked = await jsonOf(request);
|
|
85
|
-
if (!asked)
|
|
86
|
-
return json({ error: NOT_JSON }, 400);
|
|
87
|
-
if (typeof asked.org !== "string" || !asked.org)
|
|
88
|
-
return json({ error: "Which org" }, 400);
|
|
89
|
-
return await enter(request, ctx, asked.org, { staff: asked.staff === true });
|
|
90
|
-
};
|
|
91
|
-
/** `POST /orgs/invites/answer` `{ invite, accept }`. */
|
|
92
|
-
export const answerInviteRoute = async (request, ctx) => {
|
|
93
|
-
if (request.method !== "POST")
|
|
94
|
-
return json({ error: "Only POST" }, 405);
|
|
95
|
-
const person = personIn(ctx);
|
|
96
|
-
const email = (await profileOf(ctx.runtime, person))?.email;
|
|
97
|
-
if (!email)
|
|
98
|
-
return json({ error: "Not signed in with an email" }, 401);
|
|
99
|
-
const asked = await jsonOf(request);
|
|
100
|
-
if (!asked)
|
|
101
|
-
return json({ error: NOT_JSON }, 400);
|
|
102
|
-
if (typeof asked.invite !== "string")
|
|
103
|
-
return json({ error: "Which invitation" }, 400);
|
|
104
|
-
const answered = await ctx.runtime.call("invite.answer", {
|
|
105
|
-
invite: asked.invite,
|
|
106
|
-
person,
|
|
107
|
-
email,
|
|
108
|
-
accept: asked.accept === true,
|
|
109
|
-
});
|
|
110
|
-
if (!answered.joined)
|
|
111
|
-
return json({ joined: false });
|
|
112
|
-
return await enter(request, ctx, answered.org);
|
|
113
|
-
};
|
package/routes/signout.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `POST /signout`, the directory's (in place of identity's): out of the
|
|
3
|
-
* directory and every org the browser holds a session for. The directory's
|
|
4
|
-
* session ends; each org's ends in its org when that org is open (signing
|
|
5
|
-
* out never opens one: any request may name orgs in its cookies), and
|
|
6
|
-
* whoever it was is disconnected there; the person's browsers in every org
|
|
7
|
-
* open now go too. Every one of those cookies is cleared, and which org the
|
|
8
|
-
* browser is in.
|
|
9
|
-
*/
|
|
10
|
-
import type { Route } from "copilotz";
|
|
11
|
-
export declare const signOutRoute: Route;
|
package/routes/signout.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { CURRENT, heldOrgs, json, SESSION, sessionOf } from "../shared/http.js";
|
|
2
|
-
import { settingsOf } from "../shared/settings.js";
|
|
3
|
-
export const signOutRoute = async (request, { runtime, actor, cookies }) => {
|
|
4
|
-
if (request.method !== "POST")
|
|
5
|
-
return json({ error: "Only POST" }, 405);
|
|
6
|
-
const secret = cookies.get(SESSION);
|
|
7
|
-
if (secret && actor.kind === "person") {
|
|
8
|
-
await runtime.call("session.end", { secret }, { actor }).catch(() => { });
|
|
9
|
-
runtime.disconnect(actor);
|
|
10
|
-
}
|
|
11
|
-
const orgs = settingsOf({ resources: runtime.describe().resources }).orgs;
|
|
12
|
-
await Promise.all(heldOrgs(request).map(async (org) => {
|
|
13
|
-
const held = cookies.get(sessionOf(org));
|
|
14
|
-
if (held === undefined)
|
|
15
|
-
return;
|
|
16
|
-
cookies.clear(sessionOf(org));
|
|
17
|
-
const open = orgs?.peek(org);
|
|
18
|
-
if (!open)
|
|
19
|
-
return;
|
|
20
|
-
const who = await open.query("credential.actor", { secret: held })
|
|
21
|
-
.catch(() => null);
|
|
22
|
-
if (!who)
|
|
23
|
-
return;
|
|
24
|
-
await open.call("session.end", { secret: held }, { actor: who }).catch(() => { });
|
|
25
|
-
open.disconnect(who);
|
|
26
|
-
}));
|
|
27
|
-
// Their browsers in every org open now go: one comes back only with a cookie that's someone.
|
|
28
|
-
if (actor.kind === "person") {
|
|
29
|
-
for (const [, open] of orgs?.entries() ?? []) {
|
|
30
|
-
open.disconnect({ kind: "person", id: actor.id });
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
cookies.clear(SESSION);
|
|
34
|
-
cookies.clear(CURRENT);
|
|
35
|
-
return json({});
|
|
36
|
-
};
|
package/shared/enter.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Going into an org, from the directory's session: the directory lets the
|
|
3
|
-
* person in (`org.enter`: a member, or staff), signs who goes in (the
|
|
4
|
-
* handoff), and hands it to the org's own `/enter` (see member.ts), which
|
|
5
|
-
* knows them there, starts its session and sets its cookie (`s_<org>`), as
|
|
6
|
-
* a sign-in would: sessions stay the org's. The directory sets which org the
|
|
7
|
-
* browser is in (`org`) on the org's answer, and passes it on: `{ org }`, or
|
|
8
|
-
* a redirect for a page asked for (`to`).
|
|
9
|
-
*/
|
|
10
|
-
import { type RouteContext, type Runtime } from "copilotz";
|
|
11
|
-
type Profile = {
|
|
12
|
-
name?: string;
|
|
13
|
-
email?: string;
|
|
14
|
-
picture?: string;
|
|
15
|
-
};
|
|
16
|
-
/** What a sign-in verified about someone, at the directory. */
|
|
17
|
-
export declare const profileOf: (runtime: Pick<Runtime, "query">, person: string) => Promise<Profile | null>;
|
|
18
|
-
/** Who's signed in at the directory (its session), or a 401. */
|
|
19
|
-
export declare function personIn(ctx: Pick<RouteContext, "actor">): string;
|
|
20
|
-
/**
|
|
21
|
-
* Tells the directory how an org is (its people and spaces, and that it was
|
|
22
|
-
* used now), for staff's console: at most once a minute while it's used,
|
|
23
|
-
* unless `now`. Nothing waits for it, and nothing fails for it.
|
|
24
|
-
*/
|
|
25
|
-
export declare function report(dir: Pick<Runtime, "call" | "describe">, org: string, runtime: Pick<Runtime, "call">, now?: boolean): void;
|
|
26
|
-
/**
|
|
27
|
-
* Goes into `org` as the person the directory's session is: the org's answer
|
|
28
|
-
* (its cookie set), with which org the browser is in set too.
|
|
29
|
-
*/
|
|
30
|
-
export declare function enter(request: Request, ctx: Pick<RouteContext, "runtime" | "actor" | "cookies">, org: string, options?: {
|
|
31
|
-
staff?: boolean;
|
|
32
|
-
to?: string;
|
|
33
|
-
}): Promise<Response>;
|
|
34
|
-
/** The org someone goes back to: the one they were in last (not `not`), or their own, made now. */
|
|
35
|
-
export declare function landing(runtime: Pick<Runtime, "call">, person: string, name: string, not?: string): Promise<string>;
|
|
36
|
-
export {};
|
package/shared/enter.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Going into an org, from the directory's session: the directory lets the
|
|
3
|
-
* person in (`org.enter`: a member, or staff), signs who goes in (the
|
|
4
|
-
* handoff), and hands it to the org's own `/enter` (see member.ts), which
|
|
5
|
-
* knows them there, starts its session and sets its cookie (`s_<org>`), as
|
|
6
|
-
* a sign-in would: sessions stay the org's. The directory sets which org the
|
|
7
|
-
* browser is in (`org`) on the org's answer, and passes it on: `{ org }`, or
|
|
8
|
-
* a redirect for a page asked for (`to`).
|
|
9
|
-
*/
|
|
10
|
-
import { failure } from "copilotz";
|
|
11
|
-
import { baseOf, CURRENT, onBehalf, orgsOf } from "./http.js";
|
|
12
|
-
import { HANDOFF, reportDue, signHandoff } from "./state.js";
|
|
13
|
-
/** What a sign-in verified about someone, at the directory. */
|
|
14
|
-
export const profileOf = (runtime, person) => runtime.query("person.profile", { person }).catch(() => null);
|
|
15
|
-
/** Who's signed in at the directory (its session), or a 401. */
|
|
16
|
-
export function personIn(ctx) {
|
|
17
|
-
if (ctx.actor.kind !== "person") {
|
|
18
|
-
throw failure("unauthorized", "Not signed in", { public: true });
|
|
19
|
-
}
|
|
20
|
-
return ctx.actor.id;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Tells the directory how an org is (its people and spaces, and that it was
|
|
24
|
-
* used now), for staff's console: at most once a minute while it's used,
|
|
25
|
-
* unless `now`. Nothing waits for it, and nothing fails for it.
|
|
26
|
-
*/
|
|
27
|
-
export function report(dir, org, runtime, now = false) {
|
|
28
|
-
if (!reportDue(dir.describe().resources, org) && !now)
|
|
29
|
-
return;
|
|
30
|
-
void runtime.call("org.summary", {})
|
|
31
|
-
.then((summary) => dir.call("org.report", { org, ...summary, active: new Date().toISOString() }))
|
|
32
|
-
.catch(() => { });
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Goes into `org` as the person the directory's session is: the org's answer
|
|
36
|
-
* (its cookie set), with which org the browser is in set too.
|
|
37
|
-
*/
|
|
38
|
-
export async function enter(request, ctx, org, options = {}) {
|
|
39
|
-
const { runtime } = ctx;
|
|
40
|
-
const person = personIn(ctx);
|
|
41
|
-
const profile = await profileOf(runtime, person);
|
|
42
|
-
const entered = await runtime.call("org.enter", {
|
|
43
|
-
org,
|
|
44
|
-
person,
|
|
45
|
-
...(profile?.email ? { email: profile.email } : {}),
|
|
46
|
-
...(options.staff ? { staff: true } : {}),
|
|
47
|
-
});
|
|
48
|
-
const id = entered.org.id;
|
|
49
|
-
const target = await orgsOf(runtime).get(id);
|
|
50
|
-
if (!target) {
|
|
51
|
-
throw failure("not_found", "No such org, or not one you belong to.", { public: true });
|
|
52
|
-
}
|
|
53
|
-
const query = await signHandoff(runtime.describe().resources, {
|
|
54
|
-
org: id,
|
|
55
|
-
person,
|
|
56
|
-
...(profile?.email ? { email: profile.email } : {}),
|
|
57
|
-
...(profile?.name ? { name: profile.name } : {}),
|
|
58
|
-
...(profile?.picture ? { picture: profile.picture } : {}),
|
|
59
|
-
...(entered.role ? { role: entered.role } : {}),
|
|
60
|
-
...(entered.staff ? { staff: true } : {}),
|
|
61
|
-
...(options.to ? { to: options.to } : {}),
|
|
62
|
-
});
|
|
63
|
-
const answered = await target.fetch(onBehalf(request, `${baseOf(id)}${HANDOFF}${query}`));
|
|
64
|
-
if (answered.status < 400) {
|
|
65
|
-
ctx.cookies.set(CURRENT, id);
|
|
66
|
-
report(runtime, id, target, true);
|
|
67
|
-
}
|
|
68
|
-
return answered;
|
|
69
|
-
}
|
|
70
|
-
/** The org someone goes back to: the one they were in last (not `not`), or their own, made now. */
|
|
71
|
-
export async function landing(runtime, person, name, not) {
|
|
72
|
-
const { orgs } = await runtime.call("org.mine", { person });
|
|
73
|
-
const open = orgs.filter((o) => o.id !== not && !o.suspended);
|
|
74
|
-
if (open.length)
|
|
75
|
-
return open[0].id;
|
|
76
|
-
const { org } = await runtime.call("org.create", {
|
|
77
|
-
name,
|
|
78
|
-
kind: "personal",
|
|
79
|
-
by: person,
|
|
80
|
-
});
|
|
81
|
-
return org;
|
|
82
|
-
}
|
package/shared/http.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
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 "copilotz";
|
|
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 = "/__copilotz/hosts/resolve";
|
|
27
|
-
/** What the directory answers in JSON is someone's: never kept. */
|
|
28
|
-
export declare const json: (value: unknown, status?: number) => Response;
|
|
29
|
-
/**
|
|
30
|
-
* Whether a GET is a browser navigating (`Sec-Fetch-Mode: navigate`), or,
|
|
31
|
-
* from one that doesn't say, one asking for a page (`Accept` with HTML).
|
|
32
|
-
*/
|
|
33
|
-
export declare function navigating(request: Request): boolean;
|
|
34
|
-
/** The orgs' runtimes the app gave the directory. */
|
|
35
|
-
export declare function orgsOf(runtime: Pick<Runtime, "describe">): Runtimes;
|
|
36
|
-
/**
|
|
37
|
-
* A request the directory makes of an org for someone (entering it): on the
|
|
38
|
-
* same site, with their cookies and the front's headers, from the address
|
|
39
|
-
* they came from; never with where their browser said it came from (the
|
|
40
|
-
* directory's own, not another site's).
|
|
41
|
-
*/
|
|
42
|
-
export declare function onBehalf(request: Request, path: string): Request;
|
package/shared/http.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
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 "copilotz";
|
|
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 = "/__copilotz/hosts/resolve";
|
|
33
|
-
/** What the directory answers in JSON is someone's: never kept. */
|
|
34
|
-
export const json = (value, status = 200) => new Response(JSON.stringify(value), {
|
|
35
|
-
status,
|
|
36
|
-
headers: { "content-type": "application/json", "cache-control": "no-store" },
|
|
37
|
-
});
|
|
38
|
-
/**
|
|
39
|
-
* Whether a GET is a browser navigating (`Sec-Fetch-Mode: navigate`), or,
|
|
40
|
-
* from one that doesn't say, one asking for a page (`Accept` with HTML).
|
|
41
|
-
*/
|
|
42
|
-
export function navigating(request) {
|
|
43
|
-
if (request.method !== "GET")
|
|
44
|
-
return false;
|
|
45
|
-
const mode = request.headers.get("sec-fetch-mode");
|
|
46
|
-
if (mode !== null)
|
|
47
|
-
return mode === "navigate";
|
|
48
|
-
return /text\/html/.test(request.headers.get("accept") ?? "");
|
|
49
|
-
}
|
|
50
|
-
/** The orgs' runtimes the app gave the directory. */
|
|
51
|
-
export function orgsOf(runtime) {
|
|
52
|
-
const orgs = settingsOf({ resources: runtime.describe().resources }).orgs;
|
|
53
|
-
if (!orgs) {
|
|
54
|
-
throw new Error("directory: give it the orgs' runtimes (directory.orgs: runtimes(open))");
|
|
55
|
-
}
|
|
56
|
-
return orgs;
|
|
57
|
-
}
|
|
58
|
-
/** Headers a browser sets that say where a request comes from: not the directory's to pass on. */
|
|
59
|
-
const ITS_OWN = /^(?:origin|referer|sec-fetch-.+|content-type|content-length)$/;
|
|
60
|
-
/**
|
|
61
|
-
* A request the directory makes of an org for someone (entering it): on the
|
|
62
|
-
* same site, with their cookies and the front's headers, from the address
|
|
63
|
-
* they came from; never with where their browser said it came from (the
|
|
64
|
-
* directory's own, not another site's).
|
|
65
|
-
*/
|
|
66
|
-
export function onBehalf(request, path) {
|
|
67
|
-
const headers = new Headers();
|
|
68
|
-
for (const [name, value] of request.headers) {
|
|
69
|
-
if (!ITS_OWN.test(name))
|
|
70
|
-
headers.set(name, value);
|
|
71
|
-
}
|
|
72
|
-
const made = new Request(new URL(path, request.url), { method: "POST", headers });
|
|
73
|
-
const address = clientAddress(request);
|
|
74
|
-
if (address)
|
|
75
|
-
setClientAddress(made, address);
|
|
76
|
-
return made;
|
|
77
|
-
}
|
package/shared/member.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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 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 "copilotz";
|
|
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
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
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 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 "copilotz";
|
|
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
|
-
/** A gift code's credits, for this org. */
|
|
66
|
-
claimCode: (code, person) => ask("code.claim", {
|
|
67
|
-
code,
|
|
68
|
-
person,
|
|
69
|
-
}),
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
}
|
package/shared/settings.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
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 "copilotz";
|
|
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>;
|
package/shared/settings.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
}
|
package/shared/state.d.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
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 "copilotz";
|
|
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, or of a host, here: it just changed. */
|
|
50
|
-
export declare function forget(resources: Resources, what: {
|
|
51
|
-
org?: string;
|
|
52
|
-
host?: string;
|
|
53
|
-
}): void;
|
|
54
|
-
/** Who goes into an org: what the directory signs, and the org's `/enter` reads. */
|
|
55
|
-
export type Handoff = {
|
|
56
|
-
org: string;
|
|
57
|
-
person: string;
|
|
58
|
-
email?: string;
|
|
59
|
-
name?: string;
|
|
60
|
-
picture?: string;
|
|
61
|
-
/** The role they join with, the first time. */
|
|
62
|
-
role?: string;
|
|
63
|
-
/** As platform staff. */
|
|
64
|
-
staff?: boolean;
|
|
65
|
-
/** Where the browser goes then (a path on the app): a redirect; none, `{ org }`. */
|
|
66
|
-
to?: string;
|
|
67
|
-
};
|
|
68
|
-
/** A handoff, signed: the query of the org's `/enter`. */
|
|
69
|
-
export declare function signHandoff(resources: Resources, handoff: Handoff): Promise<string>;
|
|
70
|
-
/** A handoff the directory signed, from an `/enter` request's query; undefined for any other. */
|
|
71
|
-
export declare function readHandoff(resources: Resources, query: URLSearchParams): Promise<Handoff | undefined>;
|
|
72
|
-
/** Whether an org's report is due (at most once a minute), counting this one. */
|
|
73
|
-
export declare function reportDue(resources: Resources, org: string): boolean;
|
|
74
|
-
export {};
|