@teable/app 6.1.9 → 7.0.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/dist/build/cli.js +2 -2
- package/dist/build/index.js +2 -2
- package/dist/chunks/{GMRE3ZBW.js → CFKHMHDT.js} +7 -1
- package/dist/chunks/{KOPAQ575.js → DPGYFUG5.js} +22 -4
- package/dist/chunks/{QPDT7M6Y.js → MV7KRBHD.js} +1 -1
- package/dist/chunks/{K5LVABM3.js → O4WQUPBV.js} +3 -3
- package/dist/chunks/{ERQJZOEM.js → SY3BAXHH.js} +6 -2
- package/dist/client.d.ts +38 -5
- package/dist/client.js +2 -2
- package/dist/dev/host.d.ts +13 -2
- package/dist/dev/host.js +120 -10
- package/dist/index.js +3 -3
- package/dist/protocol.d.ts +92 -8
- package/dist/protocol.js +3 -1
- package/dist/schema.js +2 -2
- package/dist/testing.js +4 -4
- package/dist/worker.js +2 -2
- package/package.json +1 -1
package/dist/build/cli.js
CHANGED
package/dist/build/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
FILE_NAME_HEADER,
|
|
5
5
|
SESSION_PATH,
|
|
6
6
|
fileKeyPath
|
|
7
|
-
} from "./
|
|
7
|
+
} from "./SY3BAXHH.js";
|
|
8
8
|
|
|
9
9
|
// src/client.ts
|
|
10
10
|
var MethodError = class extends Error {
|
|
@@ -95,6 +95,8 @@ function createClient(options) {
|
|
|
95
95
|
manifestKey: ["teable-manifest", options.appId],
|
|
96
96
|
call: ((method, input) => request(`/methods/${method}`, json(input))),
|
|
97
97
|
manifest: async () => await request("/manifest"),
|
|
98
|
+
members: async () => (await request("/members")).members,
|
|
99
|
+
accounts: async () => (await request("/accounts")).accounts,
|
|
98
100
|
queryKey: (method, input) => [...dataKey, method, input ?? {}],
|
|
99
101
|
subscribe,
|
|
100
102
|
upload: async (file, name) => await request("/files", {
|
|
@@ -117,6 +119,10 @@ function createClient(options) {
|
|
|
117
119
|
const back = returnTo ?? (page ? `${page.pathname}${page.search}${page.hash}` : "/");
|
|
118
120
|
page?.assign(`${base()}${SESSION_PATH}/sign-in?return=${encodeURIComponent(back)}`);
|
|
119
121
|
},
|
|
122
|
+
sendSignInCode: async (email) => {
|
|
123
|
+
await request("/sign-in/email", json({ email }), SESSION_PATH);
|
|
124
|
+
},
|
|
125
|
+
signInWithCode: async (email, code) => await request("/sign-in/code", json({ email, code }), SESSION_PATH),
|
|
120
126
|
signOut: async () => {
|
|
121
127
|
await request("/sign-out", { method: "POST" }, SESSION_PATH);
|
|
122
128
|
}
|
|
@@ -7,9 +7,10 @@ import {
|
|
|
7
7
|
PAGES_APP_META,
|
|
8
8
|
SESSION_PATH,
|
|
9
9
|
checkAccess,
|
|
10
|
+
isMember,
|
|
10
11
|
pagesBridge,
|
|
11
12
|
spaceRoleOf
|
|
12
|
-
} from "./
|
|
13
|
+
} from "./SY3BAXHH.js";
|
|
13
14
|
|
|
14
15
|
// src/build/index.ts
|
|
15
16
|
import { existsSync, readFileSync } from "node:fs";
|
|
@@ -319,13 +320,24 @@ function localRuntime(server, appId, workerd) {
|
|
|
319
320
|
}
|
|
320
321
|
function remoteMiddleware(remote, appId) {
|
|
321
322
|
const runtime = remote.url.replace(/\/$/, "");
|
|
322
|
-
const
|
|
323
|
+
const get = (req, path) => {
|
|
323
324
|
const token = signedOut(req) ? void 0 : remote.token;
|
|
324
|
-
|
|
325
|
+
return fetch(`${runtime}${APPS_PATH}/${appId}/${path}`, {
|
|
325
326
|
headers: token ? { Authorization: `Bearer ${token}` } : {}
|
|
326
327
|
});
|
|
328
|
+
};
|
|
329
|
+
const manifest = async (req) => {
|
|
330
|
+
const response = await get(req, "manifest");
|
|
327
331
|
return response.ok ? await response.json() : null;
|
|
328
332
|
};
|
|
333
|
+
const memberOf = async (req, caller) => {
|
|
334
|
+
if (caller.kind !== "user" || !isMember(caller)) return null;
|
|
335
|
+
const response = await get(req, "members");
|
|
336
|
+
if (!response.ok) return null;
|
|
337
|
+
const { members } = await response.json();
|
|
338
|
+
const member = members.find(({ userId }) => userId === caller.userId);
|
|
339
|
+
return member && caller.role ? { ...member, role: caller.role } : null;
|
|
340
|
+
};
|
|
329
341
|
const cookie = (who) => `${DEV_SESSION}=${who}; Path=/; HttpOnly; SameSite=Lax`;
|
|
330
342
|
const onHost = (path) => path?.startsWith("/") && !path.startsWith("//") ? path : "/";
|
|
331
343
|
return (req, res, next) => {
|
|
@@ -360,8 +372,14 @@ function remoteMiddleware(remote, appId) {
|
|
|
360
372
|
}
|
|
361
373
|
case `GET ${SESSION_PATH}/session`: {
|
|
362
374
|
const caller = (await manifest(req))?.caller ?? { kind: "anonymous", via: "web" };
|
|
375
|
+
const session = {
|
|
376
|
+
caller,
|
|
377
|
+
expiresAt: null,
|
|
378
|
+
member: await memberOf(req, caller),
|
|
379
|
+
account: null
|
|
380
|
+
};
|
|
363
381
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
364
|
-
return res.end(JSON.stringify(
|
|
382
|
+
return res.end(JSON.stringify(session));
|
|
365
383
|
}
|
|
366
384
|
case `POST ${SESSION_PATH}/sign-out`:
|
|
367
385
|
res.writeHead(204, { "Set-Cookie": cookie("anonymous") });
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
describeTables
|
|
3
|
-
} from "./
|
|
3
|
+
} from "./MV7KRBHD.js";
|
|
4
4
|
import {
|
|
5
5
|
CAPABILITIES,
|
|
6
6
|
PROTOCOL_VERSION,
|
|
7
7
|
admit,
|
|
8
8
|
isPageAccess
|
|
9
|
-
} from "./
|
|
9
|
+
} from "./SY3BAXHH.js";
|
|
10
10
|
|
|
11
11
|
// src/server/errors.ts
|
|
12
12
|
import { z } from "zod";
|
|
@@ -852,7 +852,7 @@ function methodName(method) {
|
|
|
852
852
|
function defineApp(schema, migrations, methods, { capabilities, pages }) {
|
|
853
853
|
if (!isPageAccess(pages.access)) {
|
|
854
854
|
throw new Error(
|
|
855
|
-
`Pages access "${pages.access}" must be a space role (owner, creator, editor, commenter, viewer) or "public"`
|
|
855
|
+
`Pages access "${pages.access}" must be a space role (owner, creator, editor, commenter, viewer), "account" or "public"`
|
|
856
856
|
);
|
|
857
857
|
}
|
|
858
858
|
for (const capability of capabilities) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/protocol.ts
|
|
2
|
-
var PROTOCOL_VERSION =
|
|
2
|
+
var PROTOCOL_VERSION = 7;
|
|
3
3
|
var APP_COMPATIBILITY_DATE = "2026-12-01";
|
|
4
4
|
var SPACE_ROLES = ["owner", "creator", "editor", "commenter", "viewer"];
|
|
5
5
|
var EVERY_CAPABILITY = { files: true, http: true };
|
|
@@ -60,7 +60,7 @@ var ERROR_STATUS = {
|
|
|
60
60
|
INTERNAL: 500
|
|
61
61
|
};
|
|
62
62
|
function isPageAccess(access) {
|
|
63
|
-
return access === "public" || SPACE_ROLES.includes(access);
|
|
63
|
+
return access === "public" || access === "account" || SPACE_ROLES.includes(access);
|
|
64
64
|
}
|
|
65
65
|
var DEV_PATHS = {
|
|
66
66
|
deploy: "/__teable/deploy",
|
|
@@ -69,6 +69,7 @@ var DEV_PATHS = {
|
|
|
69
69
|
};
|
|
70
70
|
var ASSETS_HEADER = "X-Teable-Assets";
|
|
71
71
|
var SESSION_PATH = "/.teable";
|
|
72
|
+
var SIGN_IN_ACCOUNTS = ["members", "anyone", "domains", "invited"];
|
|
72
73
|
var BRIDGE_PATH = `${SESSION_PATH}/bridge.js`;
|
|
73
74
|
function pagesBridge(origins) {
|
|
74
75
|
return `(${BRIDGE})(window, ${JSON.stringify(origins)});
|
|
@@ -283,7 +284,9 @@ function checkAccess(caller, access) {
|
|
|
283
284
|
if (access === "internal") return "FORBIDDEN";
|
|
284
285
|
if (access === "public") return null;
|
|
285
286
|
if (caller.kind === "anonymous") return "UNAUTHENTICATED";
|
|
287
|
+
if (caller.kind === "account") return access === "account" ? null : "FORBIDDEN";
|
|
286
288
|
if (caller.role === null) return "FORBIDDEN";
|
|
289
|
+
if (access === "account") return null;
|
|
287
290
|
return SPACE_ROLES.indexOf(caller.role) <= SPACE_ROLES.indexOf(access) ? null : "FORBIDDEN";
|
|
288
291
|
}
|
|
289
292
|
function admit(manifest, name, caller) {
|
|
@@ -332,6 +335,7 @@ export {
|
|
|
332
335
|
DEV_PATHS,
|
|
333
336
|
ASSETS_HEADER,
|
|
334
337
|
SESSION_PATH,
|
|
338
|
+
SIGN_IN_ACCOUNTS,
|
|
335
339
|
BRIDGE_PATH,
|
|
336
340
|
pagesBridge,
|
|
337
341
|
APPS_PATH,
|
package/dist/client.d.ts
CHANGED
|
@@ -14,10 +14,14 @@
|
|
|
14
14
|
*
|
|
15
15
|
* Pages are served at the app's own host, signed in with a session the
|
|
16
16
|
* platform keeps there (ADR 0014): `session()` says who is signed in,
|
|
17
|
-
* `signIn()` and `signOut()` change it.
|
|
17
|
+
* `signIn()` and `signOut()` change it. `members()` lists the people of the
|
|
18
|
+
* app's space (ADR 0019), by the ids methods know callers by. People outside
|
|
19
|
+
* the space sign in with an account of the app (ADR 0021): the runtime's
|
|
20
|
+
* sign-in page asks for their email and a code, or the pages do, with
|
|
21
|
+
* `sendSignInCode()` and `signInWithCode()`; `accounts()` lists them.
|
|
18
22
|
*/
|
|
19
23
|
import type { z } from "zod";
|
|
20
|
-
import { type AppErrorShape, type Attachment, type Jsonify, type JsonValue, type ManifestResponse, type PublicLink, type RealtimeMessage, type SessionResponse } from "./protocol.js";
|
|
24
|
+
import { type AppAccount, type AppErrorShape, type AppMember, type Attachment, type Jsonify, type JsonValue, type ManifestResponse, type PublicLink, type RealtimeMessage, type SessionResponse } from "./protocol.js";
|
|
21
25
|
/** An app's server module as its pages see it: `App` is the class `define()` returned. */
|
|
22
26
|
export type ServerModule = {
|
|
23
27
|
App: {
|
|
@@ -91,6 +95,18 @@ export interface AppClient<S> {
|
|
|
91
95
|
call<K extends MethodName<S>>(method: K, input?: MethodInput<S, K>): Promise<MethodOutput<S, K>>;
|
|
92
96
|
/** The app's tables and methods as far as the caller may see them, its live version, and the caller. */
|
|
93
97
|
manifest(): Promise<ManifestResponse>;
|
|
98
|
+
/**
|
|
99
|
+
* The members of the app's space (ADR 0019): each person's id, as methods
|
|
100
|
+
* know callers (`ctx.caller.userId`), name, avatar and role. For its
|
|
101
|
+
* members; empty until Teable has said who they are.
|
|
102
|
+
*/
|
|
103
|
+
members(): Promise<AppMember[]>;
|
|
104
|
+
/**
|
|
105
|
+
* The app's own accounts (ADR 0021): people outside its space who sign in
|
|
106
|
+
* by email, each with the id methods know them by (`ctx.caller.accountId`).
|
|
107
|
+
* For its members.
|
|
108
|
+
*/
|
|
109
|
+
accounts(): Promise<AppAccount[]>;
|
|
94
110
|
/** For TanStack Query: a key that includes the app, the method and the input. */
|
|
95
111
|
queryKey<K extends MethodName<S>>(method: K, input?: MethodInput<S, K>): readonly unknown[];
|
|
96
112
|
/** The prefix of every `queryKey`: what a change to the app's data makes stale (`subscribe`). */
|
|
@@ -107,13 +123,30 @@ export interface AppClient<S> {
|
|
|
107
123
|
share(file: Pick<Attachment, "key">, options?: {
|
|
108
124
|
expiresInSeconds?: number;
|
|
109
125
|
}): Promise<PublicLink>;
|
|
110
|
-
/**
|
|
126
|
+
/**
|
|
127
|
+
* Who is signed in to the app's pages, on its own host, and until when; and
|
|
128
|
+
* `member`, the person as `members()` lists them, for a member, or
|
|
129
|
+
* `account`, for an account of the app.
|
|
130
|
+
*/
|
|
111
131
|
session(): Promise<SessionResponse>;
|
|
112
132
|
/**
|
|
113
|
-
* Sign in to the app's pages
|
|
114
|
-
*
|
|
133
|
+
* Sign in to the app's pages, on its own host: the browser goes to its
|
|
134
|
+
* sign-in page (an email and a code, for an app that lets people sign in
|
|
135
|
+
* with an account; Teable for members) and comes back to `returnTo` (a
|
|
136
|
+
* path; this page by default).
|
|
115
137
|
*/
|
|
116
138
|
signIn(returnTo?: string): void;
|
|
139
|
+
/**
|
|
140
|
+
* Send a sign-in code to `email`, for a sign-in page of the app's own. It
|
|
141
|
+
* resolves the same whether the app lets that email sign in or not, so
|
|
142
|
+
* that nobody learns who may; RATE_LIMITED when it was asked too often.
|
|
143
|
+
*/
|
|
144
|
+
sendSignInCode(email: string): Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Sign in with the code `sendSignInCode` sent: the session, signed in as
|
|
147
|
+
* the account. A wrong or expired code is UNAUTHENTICATED.
|
|
148
|
+
*/
|
|
149
|
+
signInWithCode(email: string, code: string): Promise<SessionResponse>;
|
|
117
150
|
/** Sign out of the app's pages, on its own host. Signing out of Teable is Teable's. */
|
|
118
151
|
signOut(): Promise<void>;
|
|
119
152
|
}
|
package/dist/client.js
CHANGED
package/dist/dev/host.d.ts
CHANGED
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
* the developer's own host. `teable()`, the Vite plugin in
|
|
4
4
|
* `@teable/app/build`, runs this Worker in workerd and sends it what the
|
|
5
5
|
* runtime answers on an app's host: signing in (faked: the developer picks
|
|
6
|
-
* who to be
|
|
6
|
+
* who to be, a member of any role or an account of the app by email, ADR
|
|
7
|
+
* 0021), the app's API, and the app's own Worker, with the props it
|
|
7
8
|
* gets on the platform. The app's server code runs as a facet of a host
|
|
8
9
|
* Durable Object on a local database, as on the platform: calls are
|
|
9
10
|
* admitted with the protocol's `admit`, tasks run from the host's alarm,
|
|
10
11
|
* and realtime tells open pages what changed.
|
|
11
12
|
*/
|
|
12
13
|
import { DurableObject, WorkerEntrypoint } from "cloudflare:workers";
|
|
13
|
-
import { type AppManifest, type Attachment, type Caller, type FilesCapability, type JsonValue, type MethodValue, type PublicLink, type Result } from "../protocol.js";
|
|
14
|
+
import { type AppAccount, type AppManifest, type Attachment, type Caller, type FilesCapability, type JsonValue, type MethodValue, type PublicLink, type Result } from "../protocol.js";
|
|
14
15
|
interface DevEnv {
|
|
15
16
|
HOST: DurableObjectNamespace;
|
|
16
17
|
LOADER: WorkerLoader;
|
|
@@ -35,6 +36,16 @@ export declare class DevHost extends DurableObject<DevEnv> {
|
|
|
35
36
|
worker: string | null;
|
|
36
37
|
} | null>;
|
|
37
38
|
call(caller: Caller, method: string, input: JsonValue | undefined, readOnly?: boolean): Promise<Result<MethodValue>>;
|
|
39
|
+
/** The accounts the developer signed in as, the last signed in first. */
|
|
40
|
+
accounts(): Promise<AppAccount[]>;
|
|
41
|
+
/** An account, by its id; null for none. */
|
|
42
|
+
account(accountId: string): Promise<AppAccount | null>;
|
|
43
|
+
/** Sign in as the account with this email, made now if there is none: development needs no code. */
|
|
44
|
+
signIn(email: string): Promise<AppAccount>;
|
|
45
|
+
/** A code for the email: development prints it, sending no email. */
|
|
46
|
+
sendCode(email: string): Promise<void>;
|
|
47
|
+
/** Sign in with the code printed for the email, once; null for another code. */
|
|
48
|
+
redeem(email: string, code: string): Promise<AppAccount | null>;
|
|
38
49
|
alarm(): Promise<void>;
|
|
39
50
|
fetch(): Promise<Response>;
|
|
40
51
|
}
|
package/dist/dev/host.js
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
callableMethods,
|
|
11
11
|
checkAccess,
|
|
12
12
|
isMember
|
|
13
|
-
} from "../chunks/
|
|
13
|
+
} from "../chunks/SY3BAXHH.js";
|
|
14
14
|
|
|
15
15
|
// src/dev/host.ts
|
|
16
16
|
import { DurableObject, WorkerEntrypoint } from "cloudflare:workers";
|
|
@@ -24,9 +24,19 @@ var PICKS = [
|
|
|
24
24
|
{ who: "outsider", label: "Signed in, not a member of the app's space" },
|
|
25
25
|
{ who: "anonymous", label: "Signed out" }
|
|
26
26
|
];
|
|
27
|
+
var MEMBERS = PICKS.flatMap(({ who, label }) => {
|
|
28
|
+
const role = SPACE_ROLES.find((name) => name === who);
|
|
29
|
+
return role ? [{ userId: `dev-${who}`, name: label, avatar: null, role }] : [];
|
|
30
|
+
});
|
|
31
|
+
var accountSession = (account) => `account.${account.accountId}.${encodeURIComponent(account.email)}`;
|
|
27
32
|
function callerOf(request) {
|
|
28
33
|
const cookie = request.headers.get("Cookie") ?? "";
|
|
29
34
|
const who = cookie.match(new RegExp(`(?:^|;\\s*)${DEV_SESSION}=([^;]+)`))?.[1];
|
|
35
|
+
const account = who?.match(/^account\.(acc[0-9A-Za-z]+)\.(.+)$/);
|
|
36
|
+
if (account) {
|
|
37
|
+
const [, accountId = "", email = ""] = account;
|
|
38
|
+
return { kind: "account", accountId, email: decodeURIComponent(email), via: "web" };
|
|
39
|
+
}
|
|
30
40
|
if (!PICKS.some((pick) => pick.who === who) || who === "anonymous") {
|
|
31
41
|
return { kind: "anonymous", via: "web" };
|
|
32
42
|
}
|
|
@@ -51,8 +61,13 @@ var KV = {
|
|
|
51
61
|
codeId: "codeId",
|
|
52
62
|
worker: "worker",
|
|
53
63
|
manifest: "manifest",
|
|
54
|
-
version: "version"
|
|
64
|
+
version: "version",
|
|
65
|
+
accounts: "accounts"
|
|
55
66
|
};
|
|
67
|
+
function emailOf(value) {
|
|
68
|
+
const email = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
69
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) ? email : null;
|
|
70
|
+
}
|
|
56
71
|
var DevHost = class extends DurableObject {
|
|
57
72
|
/**
|
|
58
73
|
* Run new code on the local database at once, its migrations with it (no
|
|
@@ -106,6 +121,47 @@ var DevHost = class extends DurableObject {
|
|
|
106
121
|
await this.#apply(result.effects);
|
|
107
122
|
return result.ok ? { ok: true, value: result.value } : { ok: false, error: result.error };
|
|
108
123
|
}
|
|
124
|
+
/** The accounts the developer signed in as, the last signed in first. */
|
|
125
|
+
async accounts() {
|
|
126
|
+
const accounts = Object.values(this.#kv(KV.accounts) ?? {});
|
|
127
|
+
return accounts.sort((a, b) => (b.lastSignInAt ?? 0) - (a.lastSignInAt ?? 0));
|
|
128
|
+
}
|
|
129
|
+
/** An account, by its id; null for none. */
|
|
130
|
+
async account(accountId) {
|
|
131
|
+
return (await this.accounts()).find((account) => account.accountId === accountId) ?? null;
|
|
132
|
+
}
|
|
133
|
+
/** Sign in as the account with this email, made now if there is none: development needs no code. */
|
|
134
|
+
async signIn(email) {
|
|
135
|
+
const accounts = this.#kv(KV.accounts) ?? {};
|
|
136
|
+
const now = Date.now();
|
|
137
|
+
const random = crypto.getRandomValues(new Uint8Array(16));
|
|
138
|
+
const account = {
|
|
139
|
+
...accounts[email] ?? {
|
|
140
|
+
accountId: `acc${[...random].map((byte) => (byte % 36).toString(36)).join("")}`,
|
|
141
|
+
email,
|
|
142
|
+
name: email.slice(0, email.indexOf("@")),
|
|
143
|
+
createdAt: now
|
|
144
|
+
},
|
|
145
|
+
lastSignInAt: now
|
|
146
|
+
};
|
|
147
|
+
this.ctx.storage.kv.put(KV.accounts, { ...accounts, [email]: account });
|
|
148
|
+
return account;
|
|
149
|
+
}
|
|
150
|
+
/** A code for the email: development prints it, sending no email. */
|
|
151
|
+
async sendCode(email) {
|
|
152
|
+
const code = String(crypto.getRandomValues(new Uint32Array(1))[0] ?? 0).slice(-6).padStart(6, "0");
|
|
153
|
+
this.#codes.set(email, code);
|
|
154
|
+
console.log(
|
|
155
|
+
`teable: the sign-in code for ${email} is ${code} (in development, no email is sent)`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
/** Sign in with the code printed for the email, once; null for another code. */
|
|
159
|
+
async redeem(email, code) {
|
|
160
|
+
if (this.#codes.get(email) !== code) return null;
|
|
161
|
+
this.#codes.delete(email);
|
|
162
|
+
return this.signIn(email);
|
|
163
|
+
}
|
|
164
|
+
#codes = /* @__PURE__ */ new Map();
|
|
109
165
|
async alarm() {
|
|
110
166
|
if (this.#kv(KV.code) === void 0) return;
|
|
111
167
|
const { failed, effects } = await this.#facet().wake(Date.now());
|
|
@@ -225,15 +281,17 @@ function html(body, status = 200) {
|
|
|
225
281
|
return new Response(page, { status, headers: { "Content-Type": "text/html; charset=utf-8" } });
|
|
226
282
|
}
|
|
227
283
|
function picker(returnTo, current) {
|
|
228
|
-
const now = current.kind === "user" ? current.role ?? "outsider" :
|
|
284
|
+
const now = current.kind === "user" ? current.role ?? "outsider" : current.kind;
|
|
229
285
|
const buttons = PICKS.map(
|
|
230
286
|
({ who, label }) => `<button name="who" value="${who}"${who === now ? " aria-current=true" : ""}>${label}</button>`
|
|
231
287
|
).join("");
|
|
288
|
+
const back = `<input type="hidden" name="return" value="${escapeHtml(returnTo)}">`;
|
|
289
|
+
const email = current.kind === "account" ? escapeHtml(current.email) : "";
|
|
232
290
|
return html(
|
|
233
|
-
`<h1>Sign in to the app</h1><p>Development: pick who to be. On Teable,
|
|
291
|
+
`<h1>Sign in to the app</h1><p>Development: pick who to be. On Teable, the members of the app's space sign in with Teable, with the role they have there.</p><form method="post">${back}${buttons}</form><h2>An account of the app</h2><p>People outside its space sign in with an email code, where its sign-in setting lets them. Here, an email is enough.</p><form method="post">${back}<input type="hidden" name="who" value="account"><input name="email" type="email" required placeholder="someone@example.com" value="${email}"><button${now === "account" ? " aria-current=true" : ""}>Sign in as this account</button></form>`
|
|
234
292
|
);
|
|
235
293
|
}
|
|
236
|
-
async function session(request, url) {
|
|
294
|
+
async function session(request, env, url) {
|
|
237
295
|
const route = `${request.method} ${url.pathname.slice(SESSION_PATH.length)}`;
|
|
238
296
|
const cookie = (who) => `${DEV_SESSION}=${who}; Path=/; HttpOnly; SameSite=Lax`;
|
|
239
297
|
switch (route) {
|
|
@@ -249,22 +307,67 @@ async function session(request, url) {
|
|
|
249
307
|
});
|
|
250
308
|
}
|
|
251
309
|
const caller = callerOf(request);
|
|
252
|
-
if (caller.kind
|
|
310
|
+
if (caller.kind !== "anonymous" && !url.searchParams.has("choose")) {
|
|
253
311
|
return new Response(null, { status: 302, headers: { Location: returnTo } });
|
|
254
312
|
}
|
|
255
313
|
return picker(returnTo, caller);
|
|
256
314
|
}
|
|
257
315
|
case "POST /sign-in": {
|
|
258
316
|
const form = await request.formData();
|
|
259
|
-
const pick = PICKS.find((p) => p.who === form.get("who"))?.who ?? "anonymous";
|
|
260
317
|
const to = pathOnHost(String(form.get("return") ?? "/"));
|
|
318
|
+
const email = form.get("who") === "account" ? emailOf(form.get("email")) : null;
|
|
319
|
+
const pick = email ? accountSession(await host(env).signIn(email)) : PICKS.find((p) => p.who === form.get("who"))?.who ?? "anonymous";
|
|
261
320
|
return new Response(null, {
|
|
262
321
|
status: 303,
|
|
263
322
|
headers: { Location: to, "Set-Cookie": cookie(pick) }
|
|
264
323
|
});
|
|
265
324
|
}
|
|
266
|
-
|
|
267
|
-
|
|
325
|
+
// A page's own sign-in for accounts (client.sendSignInCode, client.signInWithCode).
|
|
326
|
+
case "POST /sign-in/email":
|
|
327
|
+
case "POST /sign-in/code": {
|
|
328
|
+
const body = await request.json().catch(() => ({}));
|
|
329
|
+
const email = emailOf(body.email);
|
|
330
|
+
if (email === null) return errorBody("BAD_INPUT", "email must be an email address");
|
|
331
|
+
if (route === "POST /sign-in/email") {
|
|
332
|
+
await host(env).sendCode(email);
|
|
333
|
+
return new Response(null, { status: 204 });
|
|
334
|
+
}
|
|
335
|
+
const account = await host(env).redeem(email, String(body.code ?? "").trim());
|
|
336
|
+
if (account === null) {
|
|
337
|
+
return errorBody(
|
|
338
|
+
"UNAUTHENTICATED",
|
|
339
|
+
"That code is wrong, used or expired: ask for a new one"
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
const caller = { kind: "account", accountId: account.accountId, email, via: "web" };
|
|
343
|
+
return Response.json(
|
|
344
|
+
{ caller, expiresAt: null, member: null, account },
|
|
345
|
+
{
|
|
346
|
+
headers: { "Set-Cookie": cookie(accountSession(account)) }
|
|
347
|
+
}
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
case "GET /session": {
|
|
351
|
+
const caller = callerOf(request);
|
|
352
|
+
if (caller.kind === "account") {
|
|
353
|
+
const account = await host(env).account(caller.accountId);
|
|
354
|
+
const signedIn = { caller, expiresAt: null, member: null, account };
|
|
355
|
+
const signedOut = {
|
|
356
|
+
caller: { kind: "anonymous", via: "web" },
|
|
357
|
+
expiresAt: null,
|
|
358
|
+
member: null,
|
|
359
|
+
account: null
|
|
360
|
+
};
|
|
361
|
+
return Response.json(account ? signedIn : signedOut);
|
|
362
|
+
}
|
|
363
|
+
const member = caller.kind === "user" ? MEMBERS.find((m) => m.userId === caller.userId) ?? null : null;
|
|
364
|
+
return Response.json({
|
|
365
|
+
caller,
|
|
366
|
+
expiresAt: null,
|
|
367
|
+
member,
|
|
368
|
+
account: null
|
|
369
|
+
});
|
|
370
|
+
}
|
|
268
371
|
case "POST /sign-out":
|
|
269
372
|
return new Response(null, { status: 204, headers: { "Set-Cookie": cookie("anonymous") } });
|
|
270
373
|
}
|
|
@@ -293,6 +396,13 @@ async function api(request, env, url) {
|
|
|
293
396
|
pagesUrl: env.PAGES
|
|
294
397
|
});
|
|
295
398
|
}
|
|
399
|
+
if (path === "members" && request.method === "GET") {
|
|
400
|
+
return needs("viewer") ?? Response.json({ members: MEMBERS });
|
|
401
|
+
}
|
|
402
|
+
if (path === "accounts" && request.method === "GET") {
|
|
403
|
+
const accounts = await app.accounts();
|
|
404
|
+
return needs("viewer") ?? Response.json({ accounts });
|
|
405
|
+
}
|
|
296
406
|
const method = path.match(/^methods\/([A-Za-z][A-Za-z0-9_]*)$/)?.[1];
|
|
297
407
|
if (method) {
|
|
298
408
|
const raw = request.method === "GET" ? url.searchParams.get("input") : await request.text();
|
|
@@ -382,7 +492,7 @@ var host_default = {
|
|
|
382
492
|
if (url.pathname === DEV_PATHS.worker || url.pathname.startsWith(`${DEV_PATHS.worker}/`)) {
|
|
383
493
|
return toWorker(request, env, ctx, url);
|
|
384
494
|
}
|
|
385
|
-
if (url.pathname.startsWith(`${SESSION_PATH}/`)) return session(request, url);
|
|
495
|
+
if (url.pathname.startsWith(`${SESSION_PATH}/`)) return session(request, env, url);
|
|
386
496
|
if (url.pathname.startsWith(`${APPS_PATH}/`)) return api(request, env, url);
|
|
387
497
|
return errorBody("NOT_FOUND", "Not found");
|
|
388
498
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createApp,
|
|
3
3
|
fail
|
|
4
|
-
} from "./chunks/
|
|
5
|
-
import "./chunks/
|
|
6
|
-
import "./chunks/
|
|
4
|
+
} from "./chunks/O4WQUPBV.js";
|
|
5
|
+
import "./chunks/MV7KRBHD.js";
|
|
6
|
+
import "./chunks/SY3BAXHH.js";
|
|
7
7
|
|
|
8
8
|
// src/index.ts
|
|
9
9
|
import { z } from "zod";
|
package/dist/protocol.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* and starts only apps built for its own version, so a change to the contract
|
|
14
14
|
* comes with a new version, and every app is deployed again.
|
|
15
15
|
*/
|
|
16
|
-
export declare const PROTOCOL_VERSION =
|
|
16
|
+
export declare const PROTOCOL_VERSION = 7;
|
|
17
17
|
/**
|
|
18
18
|
* The workerd compatibility date every app runs with: the host loads app code
|
|
19
19
|
* with it, and the SDK's and templates' tests run with it. Bump deliberately,
|
|
@@ -25,14 +25,22 @@ export declare const SPACE_ROLES: readonly ["owner", "creator", "editor", "comme
|
|
|
25
25
|
export type SpaceRole = (typeof SPACE_ROLES)[number];
|
|
26
26
|
/**
|
|
27
27
|
* Who is calling a method. Identity is verified by the platform, never by the
|
|
28
|
-
* app.
|
|
29
|
-
*
|
|
28
|
+
* app. A user is a Teable user, with their role in the app's space (null
|
|
29
|
+
* outside it); an account is one of the app's own (ADR 0021), someone its
|
|
30
|
+
* pages let sign in by email, never with a role. The app itself calls as
|
|
31
|
+
* `system`: from a task, or from its own Worker acting as the app (`asApp`,
|
|
32
|
+
* ADR 0014).
|
|
30
33
|
*/
|
|
31
34
|
export type Caller = {
|
|
32
35
|
kind: "user";
|
|
33
36
|
userId: string;
|
|
34
37
|
role: SpaceRole | null;
|
|
35
38
|
via: ExternalChannel;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "account";
|
|
41
|
+
accountId: string;
|
|
42
|
+
email: string;
|
|
43
|
+
via: ExternalChannel;
|
|
36
44
|
} | {
|
|
37
45
|
kind: "anonymous";
|
|
38
46
|
via: ExternalChannel;
|
|
@@ -48,10 +56,11 @@ export type ExternalChannel = "web" | "api" | "mcp";
|
|
|
48
56
|
/**
|
|
49
57
|
* Who may call a method from outside the app.
|
|
50
58
|
* - a role: members of the app's space with at least that role
|
|
59
|
+
* - "account": anyone signed in to the app: its accounts, and members of its space
|
|
51
60
|
* - "public": anyone, including anonymous visitors
|
|
52
61
|
* - "internal": only the app itself (tasks); never exposed
|
|
53
62
|
*/
|
|
54
|
-
export type Access = SpaceRole | "public" | "internal";
|
|
63
|
+
export type Access = SpaceRole | "account" | "public" | "internal";
|
|
55
64
|
export type MethodKind = "read" | "write" | "task";
|
|
56
65
|
export interface MethodInfo {
|
|
57
66
|
name: string;
|
|
@@ -143,11 +152,12 @@ export interface AppManifest {
|
|
|
143
152
|
}
|
|
144
153
|
/**
|
|
145
154
|
* Who may open an app's pages: members of its space with at least that role,
|
|
146
|
-
*
|
|
155
|
+
* anyone signed in to the app (`account`: its accounts and its members), or
|
|
156
|
+
* anyone (`public`). A browser's navigation by anyone below it is sent to
|
|
147
157
|
* sign in, or told what it lacks, before the app's Worker runs; data still
|
|
148
158
|
* comes only from methods, under their own access.
|
|
149
159
|
*/
|
|
150
|
-
export type PageAccess = SpaceRole | "public";
|
|
160
|
+
export type PageAccess = SpaceRole | "account" | "public";
|
|
151
161
|
/**
|
|
152
162
|
* What the app's Worker gets with every request, as `ctx.props` (ADR 0014):
|
|
153
163
|
* who sent the request, as methods see callers (`via: "web"`); the app's
|
|
@@ -374,7 +384,7 @@ export interface ManifestResponse extends AppManifest {
|
|
|
374
384
|
caller: Caller;
|
|
375
385
|
pagesUrl: string | null;
|
|
376
386
|
}
|
|
377
|
-
/** Whether a manifest's pages access is one there is: a space role, or `public`. */
|
|
387
|
+
/** Whether a manifest's pages access is one there is: a space role, `account` or `public`. */
|
|
378
388
|
export declare function isPageAccess(access: unknown): access is PageAccess;
|
|
379
389
|
/**
|
|
380
390
|
* How `teable()`, the Vite plugin in `@teable/app/build`, and the runtime's
|
|
@@ -396,7 +406,9 @@ export declare const ASSETS_HEADER = "X-Teable-Assets";
|
|
|
396
406
|
/**
|
|
397
407
|
* Where the runtime answers on an app's own host before its Worker (ADR
|
|
398
408
|
* 0014): `sign-in?return=<path>` (to Teable and back), `callback`,
|
|
399
|
-
* `session` (`SessionResponse`) and `sign-out` (POST)
|
|
409
|
+
* `session` (`SessionResponse`) and `sign-out` (POST); for an app's accounts
|
|
410
|
+
* (ADR 0021), `sign-in/email` and `sign-in/code` (POST, `SignInCodeRequest`).
|
|
411
|
+
* With `${APPS_PATH}/`,
|
|
400
412
|
* the app's own API there, the app's Worker never sees these paths.
|
|
401
413
|
*/
|
|
402
414
|
export declare const SESSION_PATH = "/.teable";
|
|
@@ -408,6 +420,78 @@ export declare const SESSION_PATH = "/.teable";
|
|
|
408
420
|
export interface SessionResponse {
|
|
409
421
|
caller: Caller;
|
|
410
422
|
expiresAt: number | null;
|
|
423
|
+
/**
|
|
424
|
+
* The person signed in, as the app's members list them (ADR 0019): their
|
|
425
|
+
* name and avatar. Null for anyone else, and until the old platform has
|
|
426
|
+
* said who the members are.
|
|
427
|
+
*/
|
|
428
|
+
member: AppMember | null;
|
|
429
|
+
/** The account signed in (ADR 0021); null for anyone else. */
|
|
430
|
+
account: AppAccount | null;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* A member of an app's space (ADR 0019), as the old platform says: someone
|
|
434
|
+
* who may sign in to its pages, with their role in the space (the highest of
|
|
435
|
+
* their own and their departments'). Methods know a caller by `userId`;
|
|
436
|
+
* pages show the person by these.
|
|
437
|
+
*/
|
|
438
|
+
export interface AppMember {
|
|
439
|
+
userId: string;
|
|
440
|
+
name: string;
|
|
441
|
+
/** Their avatar's public URL; null without one. */
|
|
442
|
+
avatar: string | null;
|
|
443
|
+
role: SpaceRole;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* `GET {APPS_PATH}/:appId/members`: the members of the app's space, for its
|
|
447
|
+
* members (pages ask with their session), in the order the old platform
|
|
448
|
+
* sent them. Empty until it has said who they are.
|
|
449
|
+
*/
|
|
450
|
+
export interface MembersResponse {
|
|
451
|
+
members: AppMember[];
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* One of an app's own accounts (ADR 0021): someone its pages let sign in by
|
|
455
|
+
* email, outside its space. Times are epoch milliseconds; `lastSignInAt` is
|
|
456
|
+
* null for one added and not yet signed in.
|
|
457
|
+
*/
|
|
458
|
+
export interface AppAccount {
|
|
459
|
+
accountId: string;
|
|
460
|
+
email: string;
|
|
461
|
+
name: string;
|
|
462
|
+
createdAt: number;
|
|
463
|
+
lastSignInAt: number | null;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Who may sign in to an app's pages with an account of the app (ADR 0021):
|
|
467
|
+
* nobody but the members of its space (`members`), anyone with an email
|
|
468
|
+
* address (`anyone`), emails at `domains` (`domains`), or only accounts that
|
|
469
|
+
* exist (`invited`: those its creators added, and any made before).
|
|
470
|
+
*/
|
|
471
|
+
export type SignInSetting = {
|
|
472
|
+
accounts: "members" | "anyone" | "invited";
|
|
473
|
+
} | {
|
|
474
|
+
accounts: "domains";
|
|
475
|
+
domains: string[];
|
|
476
|
+
};
|
|
477
|
+
/** What a sign-in setting's `accounts` may be. */
|
|
478
|
+
export declare const SIGN_IN_ACCOUNTS: readonly ["members", "anyone", "domains", "invited"];
|
|
479
|
+
/**
|
|
480
|
+
* `GET {APPS_PATH}/:appId/accounts`: the app's accounts, the last signed in
|
|
481
|
+
* first, for the members of its space. `POST` (`{"email", "name"?}`, its
|
|
482
|
+
* creators) adds one; `DELETE …/accounts/:accountId` removes one.
|
|
483
|
+
*/
|
|
484
|
+
export interface AccountsResponse {
|
|
485
|
+
accounts: AppAccount[];
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* `POST ${SESSION_PATH}/sign-in/email`, `{"email"}`, sends a code to the
|
|
489
|
+
* email when the app lets it sign in; `POST ${SESSION_PATH}/sign-in/code`,
|
|
490
|
+
* `{"email", "code"}`, signs it in with that code (`SessionResponse`).
|
|
491
|
+
*/
|
|
492
|
+
export interface SignInCodeRequest {
|
|
493
|
+
email: string;
|
|
494
|
+
code?: string;
|
|
411
495
|
}
|
|
412
496
|
/**
|
|
413
497
|
* The bridge between the builder's Preview and an app's pages, in its frame:
|
package/dist/protocol.js
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
SECRET_NAME,
|
|
17
17
|
SECRET_PLACEHOLDER,
|
|
18
18
|
SESSION_PATH,
|
|
19
|
+
SIGN_IN_ACCOUNTS,
|
|
19
20
|
SPACE_ROLES,
|
|
20
21
|
admit,
|
|
21
22
|
callableMethods,
|
|
@@ -31,7 +32,7 @@ import {
|
|
|
31
32
|
secretPlaceholder,
|
|
32
33
|
signedInAsAsked,
|
|
33
34
|
spaceRoleOf
|
|
34
|
-
} from "./chunks/
|
|
35
|
+
} from "./chunks/SY3BAXHH.js";
|
|
35
36
|
export {
|
|
36
37
|
APPS_PATH,
|
|
37
38
|
APP_COMPATIBILITY_DATE,
|
|
@@ -50,6 +51,7 @@ export {
|
|
|
50
51
|
SECRET_NAME,
|
|
51
52
|
SECRET_PLACEHOLDER,
|
|
52
53
|
SESSION_PATH,
|
|
54
|
+
SIGN_IN_ACCOUNTS,
|
|
53
55
|
SPACE_ROLES,
|
|
54
56
|
admit,
|
|
55
57
|
callableMethods,
|
package/dist/schema.js
CHANGED
package/dist/testing.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
methodName
|
|
3
|
-
} from "./chunks/
|
|
4
|
-
import "./chunks/
|
|
3
|
+
} from "./chunks/O4WQUPBV.js";
|
|
4
|
+
import "./chunks/MV7KRBHD.js";
|
|
5
5
|
import {
|
|
6
6
|
MethodError
|
|
7
|
-
} from "./chunks/
|
|
7
|
+
} from "./chunks/CFKHMHDT.js";
|
|
8
8
|
import {
|
|
9
9
|
ERROR_STATUS
|
|
10
|
-
} from "./chunks/
|
|
10
|
+
} from "./chunks/SY3BAXHH.js";
|
|
11
11
|
|
|
12
12
|
// src/testing.ts
|
|
13
13
|
var OWNER = { kind: "user", userId: "test-owner", role: "owner", via: "api" };
|
package/dist/worker.js
CHANGED