@protobase/server 0.123.7 → 0.124.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/better-auth/auth-settings.d.ts +37 -6
  2. package/dist/better-auth/create-auth.d.ts +49 -31
  3. package/dist/better-auth/create-auth.js +14 -5
  4. package/dist/better-auth/last-admin.js +1 -1
  5. package/dist/better-auth/mcp/mcp-auth-plugins.d.ts +2 -2
  6. package/dist/better-auth/names/modules/index.d.ts +51 -1
  7. package/dist/better-auth/names/modules/mail-senders.js +36 -2
  8. package/dist/better-auth/names/resolve-names.d.ts +1 -1
  9. package/dist/better-auth/openid-provider/openid-provider-plugins.d.ts +1 -1
  10. package/dist/better-auth/organizations/accept-invitation.js +1 -1
  11. package/dist/better-auth/organizations/add-every-user.js +1 -1
  12. package/dist/better-auth/organizations/host.js +1 -1
  13. package/dist/better-auth/organizations/options.d.ts +1 -1
  14. package/dist/better-auth/staff-checks.js +2 -1
  15. package/dist/better-auth/status-route.js +3 -2
  16. package/dist/better-auth/store/steps/index.js +6 -1
  17. package/dist/better-auth/store/steps/legacy-versions.js +4 -0
  18. package/dist/better-auth/store/steps/mail-senders/003-sender-address-evidence.js +81 -0
  19. package/dist/better-auth/store/store-sql.d.ts +3 -0
  20. package/dist/better-auth/users.js +1 -1
  21. package/dist/functions/define-function.d.ts +7 -5
  22. package/dist/functions/own-mailbox.js +2 -1
  23. package/dist/functions/routes.js +2 -1
  24. package/dist/index.d.ts +7 -3
  25. package/dist/index.js +3 -1
  26. package/dist/package.js +1 -1
  27. package/dist/personal-mail/alias-checks.js +2 -2
  28. package/dist/personal-mail/gmail-errors.js +22 -0
  29. package/dist/personal-mail/gmail-sender-addresses.d.ts +4 -0
  30. package/dist/personal-mail/gmail-sender-addresses.js +74 -0
  31. package/dist/personal-mail/gmail.js +9 -22
  32. package/dist/personal-mail/listed-mail-senders.d.ts +31 -0
  33. package/dist/personal-mail/listed-mail-senders.js +30 -0
  34. package/dist/personal-mail/mail-sender-book.js +28 -0
  35. package/dist/personal-mail/mail-sender.d.ts +9 -3
  36. package/dist/personal-mail/mail-sender.js +43 -32
  37. package/dist/personal-mail/mail-senders-plugin.js +92 -78
  38. package/dist/personal-mail/mailbox-connections.d.ts +1 -0
  39. package/dist/personal-mail/mailbox-connections.js +11 -0
  40. package/dist/personal-mail/personal-mail-provider.d.ts +32 -0
  41. package/dist/personal-mail/personal-mail-provider.js +7 -0
  42. package/dist/personal-mail/personal-mail-providers.d.ts +9 -0
  43. package/dist/personal-mail/personal-mail-providers.js +12 -5
  44. package/dist/personal-mail/personal-mailer.d.ts +18 -9
  45. package/dist/personal-mail/personal-mailer.js +62 -20
  46. package/dist/personal-mail/sender-address-error.d.ts +21 -0
  47. package/dist/personal-mail/sender-address-error.js +27 -0
  48. package/dist/personal-mail/sender-address-input.js +15 -0
  49. package/dist/personal-mail/sender-address-state.d.ts +12 -0
  50. package/dist/personal-mail/sender-address-state.js +20 -0
  51. package/dist/personal-mail/sender-listing.d.ts +22 -0
  52. package/dist/personal-mail/sender-listing.js +66 -0
  53. package/dist/personal-mail/sender-store.d.ts +1 -1
  54. package/dist/personal-mail/sender-store.js +86 -55
  55. package/dist/webhooks/table/owner-roles.js +1 -1
  56. package/package.json +6 -6
  57. package/dist/personal-mail/mail-senders-plugin.d.ts +0 -14
@@ -0,0 +1,21 @@
1
+ import { HttpProblem } from "../problem.js";
2
+ //#region src/personal-mail/sender-address-error.d.ts
3
+ /**
4
+ * Why a message names a From address it cannot go out from, sending nothing: `failed`, the mailbox's provider says it
5
+ * cannot send as it; `pending`, its owner has not confirmed it with the provider yet; `detached`, it is on no mailbox
6
+ * connection its owner can send through; `ambiguous`, an address or the mailbox's own stands for more than one of them.
7
+ */
8
+ export type SenderAddressErrorCode = 'sender_address_failed' | 'sender_address_pending' | 'sender_address_detached' | 'sender_address_ambiguous';
9
+ /** Thrown when a send names a From address it cannot go out from; it answers as a 409 problem whose `code` says why. */
10
+ export declare class SenderAddressError extends HttpProblem {
11
+ readonly code: SenderAddressErrorCode;
12
+ constructor(code: SenderAddressErrorCode);
13
+ }
14
+ /**
15
+ * Thrown when a send names a From address id that is not one of the sender's: unknown, removed or someone else's, in
16
+ * the same words, so the answer does not tell which. It answers as a 404 problem.
17
+ */
18
+ export declare class SenderAddressNotFoundError extends HttpProblem {
19
+ constructor();
20
+ }
21
+ //#endregion
@@ -0,0 +1,27 @@
1
+ import { HttpProblem } from "../problem.js";
2
+ //#region src/personal-mail/sender-address-error.ts
3
+ const details = {
4
+ sender_address_failed: "Your mailbox cannot send as this address: set it up with your mail provider, or choose another",
5
+ sender_address_pending: "This address still waits for you to confirm it with your mail provider",
6
+ sender_address_detached: "This address is on no mailbox you have connected: add it again under Account → Mailbox",
7
+ sender_address_ambiguous: "This address is on more than one of your mailboxes: choose the From address by its id"
8
+ };
9
+ /** Thrown when a send names a From address it cannot go out from; it answers as a 409 problem whose `code` says why. */
10
+ var SenderAddressError = class extends HttpProblem {
11
+ code;
12
+ constructor(code) {
13
+ super(409, code.replaceAll("_", "-"), "Conflict", details[code], { code });
14
+ this.code = code;
15
+ }
16
+ };
17
+ /**
18
+ * Thrown when a send names a From address id that is not one of the sender's: unknown, removed or someone else's, in
19
+ * the same words, so the answer does not tell which. It answers as a 404 problem.
20
+ */
21
+ var SenderAddressNotFoundError = class extends HttpProblem {
22
+ constructor() {
23
+ super(404, "sender-address-not-found", "Not Found", "This From address is not one of yours");
24
+ }
25
+ };
26
+ //#endregion
27
+ export { SenderAddressError, SenderAddressNotFoundError };
@@ -0,0 +1,15 @@
1
+ import * as z$1 from "zod";
2
+ //#region src/personal-mail/sender-address-input.ts
3
+ /**
4
+ * A From address as it may go into a From header, wherever it comes from: what a person types and what a provider
5
+ * lists alike. A line break or other control character would end the header, so it is refused, as is anything that is
6
+ * not an address; a valid one is trimmed and lowercased, the spelling the store keeps.
7
+ */
8
+ const senderAddressInput = z$1.string().trim().max(254).regex(/^[^\p{Cc}]*$/u).pipe(z$1.email()).transform((address) => address.toLowerCase());
9
+ /** `value` as a From address (`senderAddressInput`), or undefined when it is none. */
10
+ const senderAddressOf = (value) => {
11
+ const parsed = senderAddressInput.safeParse(value);
12
+ return parsed.success ? parsed.data : void 0;
13
+ };
14
+ //#endregion
15
+ export { senderAddressInput, senderAddressOf };
@@ -0,0 +1,12 @@
1
+ import "./sender-store.js";
2
+ //#region src/personal-mail/sender-address-state.d.ts
3
+ /**
4
+ * Where a From address stands, derived from what vouches for it each time it is read: `verified` when the provider's
5
+ * last listing says the mailbox may send as it; `pending` while its owner still has to confirm it with the provider;
6
+ * `failed` when a complete listing since it was known did not hold it, so the provider would send from another
7
+ * address; `unverified` when nothing says either way, as for a typed address on a mailbox that cannot list. A removed
8
+ * address is `removed`, and one on no usable connection `detached`: neither is sent from.
9
+ */
10
+ export type SenderAddressState = 'unverified' | 'pending' | 'verified' | 'failed';
11
+ export type SenderRowState = SenderAddressState | 'removed' | 'detached';
12
+ //#endregion
@@ -0,0 +1,20 @@
1
+ //#region src/personal-mail/sender-address-state.ts
2
+ /** Whether the person removed the address and has not typed it again since. */
3
+ const isRemoved = ({ removedAt, typedAt }) => removedAt !== null && (typedAt === null || removedAt >= typedAt);
4
+ /** Whether a listing read the address after its owner last removed it: an older listing no longer counts. */
5
+ const hasListing = ({ providerStateReadAt, removedAt }) => providerStateReadAt !== null && (removedAt === null || providerStateReadAt > removedAt);
6
+ /**
7
+ * The state of `row`, with `connected` whether its connection is still one of its owner's that can send. Only a
8
+ * provider state of `verified` or `pending` makes a claim: any other value, as an unknown one, makes none.
9
+ */
10
+ const senderRowState = (row, connected) => {
11
+ if (isRemoved(row)) return "removed";
12
+ if (!connected) return "detached";
13
+ if (!hasListing(row)) return "unverified";
14
+ if (row.listedAt === null) return "failed";
15
+ if (row.providerState === "verified") return "verified";
16
+ if (row.providerState === "pending") return "pending";
17
+ return "unverified";
18
+ };
19
+ //#endregion
20
+ export { hasListing, isRemoved, senderRowState };
@@ -0,0 +1,22 @@
1
+ import "./sender-store.js";
2
+ import "./linked-accounts.js";
3
+ import "./mailbox-connections.js";
4
+ //#region src/personal-mail/sender-listing.d.ts
5
+ /**
6
+ * How a listing of one connection went: `listed`, its addresses are recorded; `not_permitted`, the link lacks
7
+ * `missing_scopes`, so reconnecting with them lets it list; `error`, with `code` `link_expired` when the provider no
8
+ * longer honours the link, or `listing_failed`. Only `listed` records anything.
9
+ */
10
+ export type ConnectionListing = {
11
+ connection: string;
12
+ provider: string;
13
+ } & ({
14
+ status: 'listed';
15
+ } | {
16
+ status: 'not_permitted';
17
+ missing_scopes: string[];
18
+ } | {
19
+ status: 'error';
20
+ code: 'link_expired' | 'listing_failed';
21
+ });
22
+ //#endregion
@@ -0,0 +1,66 @@
1
+ import { PersonalMailError } from "./personal-mail-error.js";
2
+ import { SenderListingError } from "./personal-mail-provider.js";
3
+ //#region src/personal-mail/sender-listing.ts
4
+ /**
5
+ * Lists the send-as addresses of one connection whose provider can, and records them (`recordListing`), as of the
6
+ * moment it asked: an address typed while it listed keeps what was typed. Rejects as its provider and the token
7
+ * refresh do, recording nothing.
8
+ */
9
+ const listConnection = async ({ accounts, senders, userId, now }, { account, provider }) => {
10
+ const list = provider.list_sender_addresses;
11
+ if (!list) throw new Error(`${provider.name} cannot list send-as addresses`);
12
+ const readAt = now();
13
+ const listing = await list({
14
+ accessToken: await accounts.accessToken(userId, account, provider.id),
15
+ scopes: account.scopes
16
+ });
17
+ const named = {
18
+ connection: account.id,
19
+ provider: provider.id
20
+ };
21
+ if (listing.kind === "not_permitted") return {
22
+ ...named,
23
+ status: "not_permitted",
24
+ missing_scopes: listing.missing_scopes
25
+ };
26
+ await senders.recordListing(userId, account.id, listing.addresses, readAt);
27
+ return {
28
+ ...named,
29
+ status: "listed"
30
+ };
31
+ };
32
+ /**
33
+ * Lists every connection of `connections` whose provider can, one after another, each on its own: a connection whose
34
+ * token or listing fails answers its `error` and changes nothing, and the others still list. Errors of another kind
35
+ * are thrown once every connection had its turn.
36
+ */
37
+ const listConnections = async (lister, connections) => {
38
+ const results = [];
39
+ const unexpected = [];
40
+ for (const connection of connections.filter(({ provider }) => provider.list_sender_addresses)) {
41
+ const named = {
42
+ connection: connection.account.id,
43
+ provider: connection.provider.id
44
+ };
45
+ try {
46
+ results.push(await listConnection(lister, connection));
47
+ } catch (error) {
48
+ if (error instanceof PersonalMailError) results.push({
49
+ ...named,
50
+ status: "error",
51
+ code: "link_expired"
52
+ });
53
+ else if (error instanceof SenderListingError) results.push({
54
+ ...named,
55
+ status: "error",
56
+ code: "listing_failed"
57
+ });
58
+ else unexpected.push(error);
59
+ }
60
+ }
61
+ if (unexpected.length === 1) throw unexpected[0];
62
+ if (unexpected.length > 1) throw new AggregateError(unexpected, "Listing the send-as addresses failed");
63
+ return results;
64
+ };
65
+ //#endregion
66
+ export { listConnection, listConnections };
@@ -1 +1 @@
1
- import "better-auth";
1
+ import "../better-auth/store/store-sql.js";
@@ -1,60 +1,91 @@
1
- import { everyRow } from "../better-auth/organizations/store.js";
1
+ import { sql } from "kysely";
2
2
  //#region src/personal-mail/sender-store.ts
3
- /** Better Auth's model for the From addresses people enter: one row per person and address. */
3
+ /** Better Auth's model for the From addresses people send as: one row per mailbox connection and address. */
4
4
  const mailSenderModel = "mailSender";
5
- /** The user's addresses, oldest first. */
6
- const sendersOf = (adapter, userId) => adapter.findMany({
7
- model: mailSenderModel,
8
- where: [{
9
- field: "userId",
10
- value: userId
11
- }],
12
- sortBy: {
13
- field: "createdAt",
14
- direction: "asc"
5
+ const columns = (store) => {
6
+ const table = store.table("mail_sender");
7
+ const c = table.columns;
8
+ return {
9
+ table,
10
+ c,
11
+ selected: sql.join([
12
+ sql`id`,
13
+ sql`${c.user_id} as "userId"`,
14
+ sql`${c.connection_id} as "connectionId"`,
15
+ sql`${c.address} as address`,
16
+ sql`${c.name} as name`,
17
+ sql`${c.shared} as shared`,
18
+ sql`${c.signature} as signature`,
19
+ sql`${c.is_default} as "isDefault"`,
20
+ sql`${c.created_at} as "createdAt"`,
21
+ sql`${c.typed_at} as "typedAt"`,
22
+ sql`${c.listed_at} as "listedAt"`,
23
+ sql`${c.provider_state} as "providerState"`,
24
+ sql`${c.provider_state_read_at} as "providerStateReadAt"`,
25
+ sql`${c.removed_at} as "removedAt"`
26
+ ])
27
+ };
28
+ };
29
+ /**
30
+ * The store in SQL, on the auth store: each statement is one query, which the person's rows' key or the connection
31
+ * and address index serves. Typing and listing write disjoint columns of one row with `on conflict`, so a typed
32
+ * address and a listing at the same moment both keep what they wrote.
33
+ */
34
+ const sqlSenderStore = (store) => ({
35
+ ofUser: async (userId) => {
36
+ const s = await store();
37
+ const { table, c, selected } = columns(s);
38
+ const { rows } = await sql`select ${selected} from ${table} where ${c.user_id} = ${userId} order by ${c.created_at}, ${c.address_key}, id`.execute(s.db);
39
+ return rows;
15
40
  },
16
- limit: everyRow
17
- });
18
- /** The store as sending reads it, through Better Auth's adapter. */
19
- const adapterSenderStore = (adapter) => ({ ofUser: async (userId) => sendersOf(await adapter(), userId) });
20
- const storeSender = (adapter, data) => adapter.create({
21
- model: mailSenderModel,
22
- data
23
- });
24
- /** Changes the user's address `id`; one that is not theirs is left as it is, and answers null. */
25
- const changeSender = (adapter, userId, id, update) => adapter.update({
26
- model: mailSenderModel,
27
- where: [{
28
- field: "id",
29
- value: id
30
- }, {
31
- field: "userId",
32
- value: userId
33
- }],
34
- update
35
- });
36
- /** Makes none of the user's addresses their default, so their mailbox's own address is. */
37
- const clearDefault = (adapter, userId) => adapter.updateMany({
38
- model: mailSenderModel,
39
- where: [{
40
- field: "userId",
41
- value: userId
42
- }, {
43
- field: "isDefault",
44
- value: true
45
- }],
46
- update: { isDefault: false }
47
- });
48
- /** Deletes the user's address `id`; one that is not theirs, or gone already, is left as it is. */
49
- const deleteSender = (adapter, userId, id) => adapter.deleteMany({
50
- model: mailSenderModel,
51
- where: [{
52
- field: "id",
53
- value: id
54
- }, {
55
- field: "userId",
56
- value: userId
57
- }]
41
+ type: async (sender, at) => {
42
+ const s = await store();
43
+ const { table, c, selected } = columns(s);
44
+ const { rows } = await sql`insert into ${table} (id, ${c.user_id}, ${c.connection_id}, ${c.address}, ${c.name}, ${c.shared}, ${c.signature}, ${c.is_default}, ${c.created_at}, ${c.typed_at})
45
+ values (${s.newId}, ${sender.userId}, ${sender.connectionId}, ${sender.address}, ${sender.name}, false, ${sender.signature}, false, ${at}, ${at})
46
+ on conflict (${c.connection_id}, ${c.address_key}) do update set ${c.typed_at} = greatest(excluded.${c.typed_at}, ${table}.${c.removed_at} + interval '1 millisecond'), ${c.name} = excluded.${c.name}, ${c.signature} = excluded.${c.signature}
47
+ returning ${selected}`.execute(s.db);
48
+ return rows[0];
49
+ },
50
+ recordListing: async (userId, connectionId, listed, readAt) => {
51
+ const s = await store();
52
+ const { table, c } = columns(s);
53
+ const addresses = listed.map(({ address }) => address);
54
+ const states = listed.map(({ provider_state }) => provider_state ?? null);
55
+ await sql`with listed (address, state) as (select * from unnest(${addresses}::text[], ${states}::text[])),
56
+ upserted as (
57
+ insert into ${table} (id, ${c.user_id}, ${c.connection_id}, ${c.address}, ${c.shared}, ${c.is_default}, ${c.created_at}, ${c.listed_at}, ${c.provider_state}, ${c.provider_state_read_at})
58
+ select ${s.newId}, ${userId}, ${connectionId}, listed.address, false, false, ${readAt}, ${readAt}, listed.state, ${readAt} from listed
59
+ on conflict (${c.connection_id}, ${c.address_key}) do update set ${c.listed_at} = excluded.${c.listed_at}, ${c.provider_state} = excluded.${c.provider_state}, ${c.provider_state_read_at} = excluded.${c.provider_state_read_at}
60
+ where ${table}.${c.provider_state_read_at} is null or ${table}.${c.provider_state_read_at} <= excluded.${c.provider_state_read_at}
61
+ returning id
62
+ )
63
+ update ${table} set ${c.listed_at} = null, ${c.provider_state} = null, ${c.provider_state_read_at} = ${readAt}
64
+ where ${c.connection_id} = ${connectionId} and ${c.user_id} = ${userId} and ${c.address_key} <> all(${addresses}::text[]) and coalesce(${c.typed_at}, ${c.created_at}) <= ${readAt}
65
+ and (${c.provider_state_read_at} is null or ${c.provider_state_read_at} <= ${readAt})`.execute(s.db);
66
+ },
67
+ change: async (userId, id, change) => {
68
+ const s = await store();
69
+ const { table, c, selected } = columns(s);
70
+ const set = [
71
+ ...change.name !== void 0 ? [sql`${c.name} = ${change.name}`] : [],
72
+ ...change.signature !== void 0 ? [sql`${c.signature} = ${change.signature}`] : [],
73
+ ...change.isDefault !== void 0 ? [sql`${c.is_default} = ${change.isDefault}`] : []
74
+ ];
75
+ if (set.length === 0) return (await sqlSenderStore(store).ofUser(userId)).find((row) => row.id === id);
76
+ const { rows } = await sql`update ${table} set ${sql.join(set)} where id = ${id} and ${c.user_id} = ${userId} returning ${selected}`.execute(s.db);
77
+ return rows[0];
78
+ },
79
+ clearDefault: async (userId) => {
80
+ const s = await store();
81
+ const { table, c } = columns(s);
82
+ await sql`update ${table} set ${c.is_default} = false where ${c.user_id} = ${userId} and ${c.is_default}`.execute(s.db);
83
+ },
84
+ remove: async (userId, id, at) => {
85
+ const s = await store();
86
+ const { table, c } = columns(s);
87
+ await sql`update ${table} set ${c.removed_at} = ${at}, ${c.is_default} = false where id = ${id} and ${c.user_id} = ${userId}`.execute(s.db);
88
+ }
58
89
  });
59
90
  //#endregion
60
- export { adapterSenderStore, changeSender, clearDefault, deleteSender, mailSenderModel, sendersOf, storeSender };
91
+ export { mailSenderModel, sqlSenderStore };
@@ -1,8 +1,8 @@
1
1
  import { parseRoles } from "../../better-auth/parse-roles.js";
2
2
  import { effectiveRoles, globalRoles, parseAppRoles } from "../../better-auth/organizations/app-roles.js";
3
3
  import { resolveActiveRole } from "../../better-auth/roles/active-role.js";
4
- import { mayBeId, storedId } from "../../better-auth/store/store-reads.js";
5
4
  import { storeSqlOf } from "../../better-auth/store/store-sql.js";
5
+ import { mayBeId, storedId } from "../../better-auth/store/store-reads.js";
6
6
  import { bannedNow } from "../../better-auth/token-standing.js";
7
7
  import { sql } from "kysely";
8
8
  //#region src/webhooks/table/owner-roles.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/server",
3
- "version": "0.123.7",
3
+ "version": "0.124.1",
4
4
  "description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -44,11 +44,11 @@
44
44
  "@better-auth/sso": "^1.7.12",
45
45
  "@livekit/protocol": "1.53.1",
46
46
  "@modelcontextprotocol/server": "^2.3.1",
47
- "@protobase/layout": "0.123.7",
48
- "@protobase/query": "0.123.7",
49
- "@protobase/schema": "0.123.7",
50
- "@protobase/workflow-plugins": "0.123.7",
51
- "@protobase/workflows": "0.123.7",
47
+ "@protobase/layout": "0.124.1",
48
+ "@protobase/query": "0.124.1",
49
+ "@protobase/schema": "0.124.1",
50
+ "@protobase/workflow-plugins": "0.124.1",
51
+ "@protobase/workflows": "0.124.1",
52
52
  "@react-email/render": "^2.1.0",
53
53
  "@react-pdf/primitives": "^4.4.0",
54
54
  "@react-pdf/renderer": "^4.9.0",
@@ -1,14 +0,0 @@
1
- import "./alias-checks.js";
2
- import "../better-auth/names/better-auth-names.js";
3
- import "./mail-sender-use-check.js";
4
- import "zod";
5
- //#region src/personal-mail/mail-senders-plugin.d.ts
6
- /** An address as one entered it, as the account page lists it. */
7
- export type ListedMailSender = {
8
- id: string;
9
- address: string;
10
- name?: string;
11
- signature?: string;
12
- isDefault: boolean;
13
- };
14
- //#endregion