@protobase/server 0.123.2 → 0.123.4
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/package.js +1 -1
- package/dist/relay/change-commands.js +16 -5
- package/dist/relay/relay-runtime.js +1 -1
- package/dist/relay/relay-schema.d.ts +5 -2
- package/dist/relay/relay-schema.js +5 -2
- package/dist/relay/sessions-sql.js +14 -10
- package/dist/relay/ticket-route.js +1 -1
- package/package.json +6 -6
package/dist/package.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { peopleChannel } from "./presence-commands.js";
|
|
1
2
|
//#region src/relay/change-commands.ts
|
|
2
3
|
/** The event every notice of a change is published as. */
|
|
3
4
|
const changeEvent = "change";
|
|
5
|
+
/** The event that tells a person on `people` of a channel they were added to, so their open pages join it. */
|
|
6
|
+
const joinedEvent = "joined";
|
|
4
7
|
/** A channel by name: `<resource>/<key>`, as the record channels name it too. */
|
|
5
8
|
const channelName = (resource, key) => `${resource}/${String(key)}`;
|
|
6
9
|
const present = (value) => value !== null && value !== void 0;
|
|
@@ -60,22 +63,30 @@ const notice = (models, change, channel, members, moved) => {
|
|
|
60
63
|
}
|
|
61
64
|
};
|
|
62
65
|
};
|
|
66
|
+
const joinedCommand = (sequence, add) => ({
|
|
67
|
+
kind: "publish",
|
|
68
|
+
id: `${sequence}:${add.channel}:${joinedEvent}`,
|
|
69
|
+
channel: peopleChannel,
|
|
70
|
+
users: add.users,
|
|
71
|
+
event: joinedEvent,
|
|
72
|
+
payload: { channel: add.channel }
|
|
73
|
+
});
|
|
63
74
|
const reaches = (command, open) => command.users === void 0 ? open.has(command.channel) : command.users.length > 0;
|
|
64
75
|
/**
|
|
65
76
|
* The relay's commands for a dispatched batch, in commit order: for each change, a person who joins a channel joins it
|
|
66
|
-
* first
|
|
67
|
-
* members hears it only through its members as the app's database has them
|
|
68
|
-
* left; an open one through everyone joined to it, and only while its record is public (`open`), so sockets that joined
|
|
77
|
+
* first and hears `joined` of it on `people`, then each channel the record was or is on hears one notice, then a person
|
|
78
|
+
* who left it leaves. A channel with members hears it only through its members as the app's database has them
|
|
79
|
+
* (`members`), and the person who joined or left; an open one through everyone joined to it, and only while its record is public (`open`), so sockets that joined
|
|
69
80
|
* it before hear nothing once it is not. A notice of a channel with no one to reach is left out.
|
|
70
81
|
*/
|
|
71
82
|
const changeCommands = (models, changes, members, open) => changes.flatMap((change) => {
|
|
72
83
|
const moved = membersCommands(models, change);
|
|
73
84
|
const notices = noticedChannels(models, change).map((channel) => notice(models, change, channel, members, moved)).filter((command) => reaches(command, open));
|
|
74
85
|
return [
|
|
75
|
-
...moved.filter((command) => command.kind === "add"),
|
|
86
|
+
...moved.filter((command) => command.kind === "add").flatMap((add) => [add, joinedCommand(change.sequence, add)]),
|
|
76
87
|
...notices,
|
|
77
88
|
...moved.filter((command) => command.kind === "remove")
|
|
78
89
|
];
|
|
79
90
|
});
|
|
80
91
|
//#endregion
|
|
81
|
-
export { audienceChannels, changeCommands, changeEvent, channelName, noticedChannels, openChannels };
|
|
92
|
+
export { audienceChannels, changeCommands, changeEvent, channelName, joinedEvent, noticedChannels, openChannels };
|
|
@@ -2,9 +2,9 @@ import { runningJobs } from "../jobs/current.js";
|
|
|
2
2
|
import { dispatchJobName } from "../changes/capture-sql.js";
|
|
3
3
|
import { anonymousSession } from "../anonymous.js";
|
|
4
4
|
import { readableKeys } from "../channels/records-readable.js";
|
|
5
|
+
import { presenceCommands, presencePeople } from "./presence-commands.js";
|
|
5
6
|
import { audienceChannels, changeCommands, openChannels } from "./change-commands.js";
|
|
6
7
|
import { channelMembers, coMembers } from "./members-sql.js";
|
|
7
|
-
import { presenceCommands, presencePeople } from "./presence-commands.js";
|
|
8
8
|
import { presenceRoutes } from "./presence-route.js";
|
|
9
9
|
import { presenceWriter } from "./presence-sql.js";
|
|
10
10
|
import { relayClient } from "./relay-commands.js";
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { AdminAuth } from "../better-auth/create-auth.js";
|
|
2
2
|
import { Db } from "@protobase/query";
|
|
3
3
|
//#region src/relay/relay-schema.d.ts
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* The SQL the relay needs of the app's database: the trigger that closes a deleted session's sockets; `undefined` when
|
|
6
|
+
* it is current. `authWritten`: the same migration writes the auth store's steps before it.
|
|
7
|
+
*/
|
|
8
|
+
export declare const relayMigration: (db: Db, auth: AdminAuth | undefined, authWritten: boolean) => Promise<string | undefined>;
|
|
6
9
|
/** Refuses to start while the relay's trigger is missing or out of date. */
|
|
7
10
|
export declare const checkRelaySchema: (db: Db, auth: AdminAuth | undefined) => Promise<void>;
|
|
8
11
|
//#endregion
|
|
@@ -5,8 +5,11 @@ const needsAuth = (auth) => {
|
|
|
5
5
|
if (!auth) throw new Error("relay needs the app's own sign-in (auth), whose sessions its tickets name and end");
|
|
6
6
|
return auth;
|
|
7
7
|
};
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The SQL the relay needs of the app's database: the trigger that closes a deleted session's sockets; `undefined` when
|
|
10
|
+
* it is current. `authWritten`: the same migration writes the auth store's steps before it.
|
|
11
|
+
*/
|
|
12
|
+
const relayMigration = (db, auth, authWritten) => relaySessionsMigration(db, sessionsTable(needsAuth(auth)), authWritten);
|
|
10
13
|
/** Refuses to start while the relay's trigger is missing or out of date. */
|
|
11
14
|
const checkRelaySchema = (db, auth) => checkRelaySessions(db, sessionsTable(needsAuth(auth)));
|
|
12
15
|
//#endregion
|
|
@@ -32,29 +32,33 @@ $$`,
|
|
|
32
32
|
`create trigger ${triggerName} after delete on ${sessions} referencing old table as ended for each statement execute function ${relaySchema}.session_ended()`
|
|
33
33
|
];
|
|
34
34
|
/**
|
|
35
|
-
* Refuses a sessions table the app's database does not have
|
|
36
|
-
* the
|
|
35
|
+
* Refuses a sessions table the app's database does not have, nor gets from the migration that writes the trigger
|
|
36
|
+
* after the auth store's steps (`written`): the trigger queues its job in the app's jobs queue, so the relay needs the
|
|
37
|
+
* auth store in the app's own database, as the `auth` config puts it.
|
|
37
38
|
*/
|
|
38
|
-
const refuseSessionsElsewhere = (table, sessions) => {
|
|
39
|
-
if (table) return;
|
|
39
|
+
const refuseSessionsElsewhere = (table, written, sessions) => {
|
|
40
|
+
if (table || written) return;
|
|
40
41
|
throw new Error(`The relay needs the sign-in sessions table ${sessions} in the app's own database, whose jobs queue closes ended sessions' sockets, but the app's database has no such table: keep the auth store in the app's database to use the relay.`);
|
|
41
42
|
};
|
|
42
|
-
const current = async (db, sessions) => {
|
|
43
|
+
const current = async (db, sessions, written) => {
|
|
43
44
|
const { rows } = await sql`select to_regclass(${sessions}) is not null as here, exists (
|
|
44
45
|
select from pg_trigger t join pg_proc p on p.oid = t.tgfoid
|
|
45
46
|
where t.tgrelid = to_regclass(${sessions}) and t.tgname = ${triggerName} and obj_description(p.oid, 'pg_proc') = ${sessionsComment}
|
|
46
47
|
) as found`.execute(db);
|
|
47
|
-
refuseSessionsElsewhere(rows[0].here, sessions);
|
|
48
|
+
refuseSessionsElsewhere(rows[0].here, written, sessions);
|
|
48
49
|
return rows[0].found;
|
|
49
50
|
};
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
/**
|
|
52
|
+
* The SQL that ends deleted sessions' sockets on the relay (`sessionsSql`); `undefined` when it is current. `written`:
|
|
53
|
+
* the same migration writes the auth store's steps before it, which create the sessions table the database lacks yet.
|
|
54
|
+
*/
|
|
55
|
+
const relaySessionsMigration = async (db, sessions, written) => {
|
|
56
|
+
if (await current(db, sessions, written)) return;
|
|
53
57
|
return `${sessionsSql(sessions).join(";\n\n")};`;
|
|
54
58
|
};
|
|
55
59
|
/** Refuses to start while deleted sessions would keep their sockets on the relay. */
|
|
56
60
|
const checkRelaySessions = async (db, sessions) => {
|
|
57
|
-
if (await current(db, sessions)) return;
|
|
61
|
+
if (await current(db, sessions, false)) return;
|
|
58
62
|
throw new Error(`The trigger that closes a deleted session's sockets on the relay is missing or out of date on ${sessions}. Write it as a migration with \`${migrationCommand}\`, apply it with the project's migrations, then start again.`);
|
|
59
63
|
};
|
|
60
64
|
//#endregion
|
|
@@ -3,8 +3,8 @@ import { channelRecord } from "../channels/channel-name.js";
|
|
|
3
3
|
import { recordReadable } from "../channels/record-readable.js";
|
|
4
4
|
import { anonymousSession } from "../anonymous.js";
|
|
5
5
|
import { jsonBody, jsonOnly } from "../notifications/history-request.js";
|
|
6
|
-
import { memberships } from "./members-sql.js";
|
|
7
6
|
import { peopleChannel } from "./presence-commands.js";
|
|
7
|
+
import { memberships } from "./members-sql.js";
|
|
8
8
|
import { signTicket } from "./ticket.js";
|
|
9
9
|
import { Hono } from "hono";
|
|
10
10
|
import { bodyLimit } from "hono/body-limit";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.123.
|
|
3
|
+
"version": "0.123.4",
|
|
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.
|
|
48
|
-
"@protobase/query": "0.123.
|
|
49
|
-
"@protobase/schema": "0.123.
|
|
50
|
-
"@protobase/workflow-plugins": "0.123.
|
|
51
|
-
"@protobase/workflows": "0.123.
|
|
47
|
+
"@protobase/layout": "0.123.4",
|
|
48
|
+
"@protobase/query": "0.123.4",
|
|
49
|
+
"@protobase/schema": "0.123.4",
|
|
50
|
+
"@protobase/workflow-plugins": "0.123.4",
|
|
51
|
+
"@protobase/workflows": "0.123.4",
|
|
52
52
|
"@react-email/render": "^2.1.0",
|
|
53
53
|
"@react-pdf/primitives": "^4.4.0",
|
|
54
54
|
"@react-pdf/renderer": "^4.9.0",
|