@protobase/server 0.131.0 → 0.132.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/assistant/query-runner/query-module.js +4 -4
- package/dist/assistant/query-runner/query-runner.d.ts +1 -1
- package/dist/assistant/query-runner/query-runner.js +3 -3
- package/dist/jobs/functions-sql.js +37 -12
- package/dist/jobs/jobs-module.js +5 -4
- package/dist/jobs/runtime.js +9 -5
- package/dist/package.js +1 -1
- package/dist/schema-modules/feature-schemas-sql.d.ts +1 -1
- package/dist/schema-modules/feature-schemas-sql.js +1 -1
- package/dist/schema-modules/needed-modules.d.ts +1 -1
- package/package.json +6 -6
|
@@ -17,12 +17,12 @@ const grants = {
|
|
|
17
17
|
keptWhileOthers: false
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
|
-
* The role
|
|
21
|
-
* tables (`queryInput`), and `query.run_read_only`, the runner every query goes through as that role.
|
|
20
|
+
* The role read-only queries run as, `<database>_query`, of the built-in assistant and the MCP endpoint's `sql`, which
|
|
21
|
+
* reads only the resources' tables (`queryInput`), and `query.run_read_only`, the runner every query goes through as that role.
|
|
22
22
|
*/
|
|
23
23
|
const queryModule = {
|
|
24
|
-
label: "the query role of
|
|
25
|
-
title: "The role
|
|
24
|
+
label: "the query role of read-only queries",
|
|
25
|
+
title: "The role read-only queries run as, which reads only the resources' tables",
|
|
26
26
|
steps: [{
|
|
27
27
|
step: 1,
|
|
28
28
|
name: "role",
|
|
@@ -11,7 +11,7 @@ export type GuardedTables = {
|
|
|
11
11
|
schemas: readonly string[];
|
|
12
12
|
};
|
|
13
13
|
/**
|
|
14
|
-
* Refuses to go on while the runner of
|
|
14
|
+
* Refuses to go on while the runner of read-only queries, which the record of migrations says is there,
|
|
15
15
|
* does not run as a role weaker than the app's: it is no security definer, or runs as the app's role itself or one it
|
|
16
16
|
* is a member of, a superuser, a login role, or one that can read an auth table, or while that role may call a
|
|
17
17
|
* security-definer function that runs as a role able to read one. A migration cannot promise these: they are the live
|
|
@@ -5,7 +5,7 @@ import { sql } from "kysely";
|
|
|
5
5
|
const rowsOf = async (db, query) => (await query.execute(db)).rows;
|
|
6
6
|
const guardedRelations = ({ tables, schemas }) => sql`select c.oid, c.relnamespace from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
|
7
7
|
where c.oid in (select to_regclass(t.name) from unnest(${tables.map(regclassText)}::text[]) as t(name)) or n.nspname = any(${[...schemas]}::text[])`;
|
|
8
|
-
const refusal = (state) => /* @__PURE__ */ new Error(`The query role of
|
|
8
|
+
const refusal = (state) => /* @__PURE__ */ new Error(`The query role of read-only queries ${state}. Take back by hand what it was given, then start again.`);
|
|
9
9
|
const leakingFunctions = (db, authTables) => rowsOf(db, sql`with runner as (select proowner as role from pg_proc where oid = to_regprocedure(${queryRunnerSignature}))
|
|
10
10
|
select format('%I.%I(%s)', n.nspname, p.proname, pg_get_function_identity_arguments(p.oid)) as name
|
|
11
11
|
from pg_proc p join pg_namespace n on n.oid = p.pronamespace, runner
|
|
@@ -15,7 +15,7 @@ const leakingFunctions = (db, authTables) => rowsOf(db, sql`with runner as (sele
|
|
|
15
15
|
where has_schema_privilege(p.proowner, c.relnamespace, 'USAGE') and has_any_column_privilege(p.proowner, c.oid, 'SELECT'))
|
|
16
16
|
order by 1`);
|
|
17
17
|
/**
|
|
18
|
-
* Refuses to go on while the runner of
|
|
18
|
+
* Refuses to go on while the runner of read-only queries, which the record of migrations says is there,
|
|
19
19
|
* does not run as a role weaker than the app's: it is no security definer, or runs as the app's role itself or one it
|
|
20
20
|
* is a member of, a superuser, a login role, or one that can read an auth table, or while that role may call a
|
|
21
21
|
* security-definer function that runs as a role able to read one. A migration cannot promise these: they are the live
|
|
@@ -35,7 +35,7 @@ const checkQueryRoleSafety = async (db, authTables) => {
|
|
|
35
35
|
if (runner.reads_auth) throw refusal("can read the auth schema");
|
|
36
36
|
const leaking = await leakingFunctions(db, authTables);
|
|
37
37
|
if (leaking.length === 0) return;
|
|
38
|
-
throw new Error(`The query role of
|
|
38
|
+
throw new Error(`The query role of read-only queries can read the auth schema through the security-definer functions ${leaking.map((found) => found.name).join(", ")}. Revoke execute on them from PUBLIC, or move them to a schema without a resource, then start again.`);
|
|
39
39
|
};
|
|
40
40
|
//#endregion
|
|
41
41
|
export { checkQueryRoleSafety };
|
|
@@ -1,31 +1,44 @@
|
|
|
1
1
|
import { organizationKey } from "./job-data.js";
|
|
2
2
|
/** The comment each Protobase function in the jobs schema carries, which the startup check reads. */
|
|
3
|
-
const functionsComment = `Protobase jobs
|
|
3
|
+
const functionsComment = `Protobase jobs 5`;
|
|
4
4
|
/**
|
|
5
5
|
* `jobs.enqueue(name, payload, run_at, key, group_key, priority)`: a job from SQL, such as a trigger, in the caller's
|
|
6
|
-
* transaction. It inserts what pg-boss's `send()` does, with the queue's retry settings; it
|
|
7
|
-
* worker has created yet. A `key` keeps one waiting job, as `mode: 'dedupe'` does, and an
|
|
8
|
-
* as `group_key`, and an id from `uuidv7()`, which rises within a database session: pg-boss
|
|
9
|
-
* transaction's start, then by id, so the jobs one transaction enqueues run in that order.
|
|
10
|
-
*
|
|
6
|
+
* transaction, working in no organization. It inserts what pg-boss's `send()` does, with the queue's retry settings; it
|
|
7
|
+
* creates a queue that no worker has created yet. A `key` keeps one waiting job, as `mode: 'dedupe'` does, and an
|
|
8
|
+
* ordered job takes its group as `group_key`, and an id from `uuidv7()`, which rises within a database session: pg-boss
|
|
9
|
+
* runs a group's jobs by their transaction's start, then by id, so the jobs one transaction enqueues run in that order.
|
|
10
|
+
* The runtime's keyed enqueue passes an `organization` too, a JSON string or number, which only that overload takes, so
|
|
11
|
+
* a payload never names one: its data is built as `jobData` builds it. Tied to the pg-boss schema version, which the
|
|
12
|
+
* startup check pins.
|
|
11
13
|
*/
|
|
12
14
|
const enqueue = (schema) => `create or replace function ${schema}.enqueue(
|
|
13
15
|
job_name text,
|
|
14
|
-
payload jsonb
|
|
15
|
-
run_at timestamptz
|
|
16
|
-
key text
|
|
17
|
-
group_key text
|
|
18
|
-
priority integer
|
|
16
|
+
payload jsonb,
|
|
17
|
+
run_at timestamptz,
|
|
18
|
+
key text,
|
|
19
|
+
group_key text,
|
|
20
|
+
priority integer,
|
|
21
|
+
organization jsonb
|
|
19
22
|
) returns uuid
|
|
20
23
|
language plpgsql as $$
|
|
21
24
|
declare
|
|
22
25
|
queue ${schema}.queue;
|
|
26
|
+
data jsonb := payload;
|
|
23
27
|
start timestamptz := coalesce(run_at, ${schema}.job_now());
|
|
24
28
|
job_id uuid;
|
|
25
29
|
begin
|
|
26
30
|
if key is not null and group_key is not null then
|
|
27
31
|
raise exception 'Job "%": an ordered job takes a group_key and no key', job_name;
|
|
28
32
|
end if;
|
|
33
|
+
if organization = 'null'::jsonb then
|
|
34
|
+
organization := null;
|
|
35
|
+
end if;
|
|
36
|
+
if organization is not null and jsonb_typeof(organization) not in ('string', 'number') then
|
|
37
|
+
raise exception 'Job "%": an organization is a string or a number', job_name;
|
|
38
|
+
end if;
|
|
39
|
+
if organization is not null or (jsonb_typeof(payload) = 'object' and payload ? '${organizationKey}') then
|
|
40
|
+
data := jsonb_build_object('${organizationKey}', coalesce(organization, 'null'::jsonb), 'payload', coalesce(payload, 'null'::jsonb));
|
|
41
|
+
end if;
|
|
29
42
|
perform ${schema}.create_queue(job_name, jsonb_build_object('policy', case when group_key is null then 'standard' else 'key_strict_fifo' end));
|
|
30
43
|
select * into queue from ${schema}.queue where name = job_name;
|
|
31
44
|
if (queue.policy = 'key_strict_fifo') <> (group_key is not null) then
|
|
@@ -46,7 +59,7 @@ begin
|
|
|
46
59
|
on conflict do nothing
|
|
47
60
|
returning id',
|
|
48
61
|
queue.table_name)
|
|
49
|
-
using job_name,
|
|
62
|
+
using job_name, data, priority, start, coalesce(key, group_key), queue.retry_limit, queue.retry_delay,
|
|
50
63
|
queue.retry_backoff, queue.retry_delay_max, queue.expire_seconds, queue.deletion_seconds, queue.retention_seconds,
|
|
51
64
|
queue.policy, queue.dead_letter, queue.heartbeat_seconds, key is not null,
|
|
52
65
|
case when group_key is null then gen_random_uuid() else uuidv7() end
|
|
@@ -60,6 +73,18 @@ begin
|
|
|
60
73
|
return job_id;
|
|
61
74
|
end
|
|
62
75
|
$$;
|
|
76
|
+
comment on function ${schema}.enqueue(text, jsonb, timestamptz, text, text, integer, jsonb) is '${functionsComment}';
|
|
77
|
+
create or replace function ${schema}.enqueue(
|
|
78
|
+
job_name text,
|
|
79
|
+
payload jsonb default null,
|
|
80
|
+
run_at timestamptz default null,
|
|
81
|
+
key text default null,
|
|
82
|
+
group_key text default null,
|
|
83
|
+
priority integer default 0
|
|
84
|
+
) returns uuid
|
|
85
|
+
language sql as $$
|
|
86
|
+
select ${schema}.enqueue(job_name, payload, run_at, key, group_key, priority, null)
|
|
87
|
+
$$;
|
|
63
88
|
comment on function ${schema}.enqueue(text, jsonb, timestamptz, text, text, integer) is '${functionsComment}';`;
|
|
64
89
|
/**
|
|
65
90
|
* `jobs.next_due()`: when the next job is due, or `null` when none waits: what a host that sleeps reads to know when to
|
package/dist/jobs/jobs-module.js
CHANGED
|
@@ -14,6 +14,7 @@ const commentVersion = (comment) => {
|
|
|
14
14
|
};
|
|
15
15
|
/** Protobase's functions in the jobs schema by the signature `functionsSql` makes them with, so an overload of one is not it. */
|
|
16
16
|
const functionSignatures = [
|
|
17
|
+
"enqueue(text, jsonb, timestamptz, text, text, integer, jsonb)",
|
|
17
18
|
"enqueue(text, jsonb, timestamptz, text, text, integer)",
|
|
18
19
|
"next_due()",
|
|
19
20
|
"failed_retry()",
|
|
@@ -28,10 +29,10 @@ const functionSignatures = [
|
|
|
28
29
|
const functionsModel = (versions) => {
|
|
29
30
|
const known = versions.filter((version) => version !== void 0);
|
|
30
31
|
const newest = Math.max(...known);
|
|
31
|
-
if (newest >
|
|
32
|
+
if (newest > 5) throw new Error(`Protobase's functions of the jobs schema are at version ${newest}, newer than the 5 this version of Protobase writes: upgrade Protobase`);
|
|
32
33
|
if (known.length === 0) return {};
|
|
33
34
|
const oldest = Math.min(...known);
|
|
34
|
-
if (known.length < versions.length && oldest ===
|
|
35
|
+
if (known.length < versions.length && oldest === 5) return {};
|
|
35
36
|
return { functions: oldest };
|
|
36
37
|
};
|
|
37
38
|
/**
|
|
@@ -71,8 +72,8 @@ const jobsModule = {
|
|
|
71
72
|
replaceables: [{
|
|
72
73
|
name: "functions",
|
|
73
74
|
title: "Protobase's functions of the jobs schema",
|
|
74
|
-
version:
|
|
75
|
-
compatibleFrom:
|
|
75
|
+
version: 5,
|
|
76
|
+
compatibleFrom: 5,
|
|
76
77
|
sql: () => functionsSql(jobsSchema)
|
|
77
78
|
}],
|
|
78
79
|
resourceReplaceables: [],
|
package/dist/jobs/runtime.js
CHANGED
|
@@ -19,6 +19,8 @@ import { sql } from "kysely";
|
|
|
19
19
|
import { randomUUID } from "node:crypto";
|
|
20
20
|
import { PgBoss } from "pg-boss";
|
|
21
21
|
//#region src/jobs/runtime.ts
|
|
22
|
+
/** A `jsonb` parameter of `jobs.enqueue`: the value as JSON, or SQL null for none. */
|
|
23
|
+
const asJson = (value) => value === void 0 || value === null ? null : JSON.stringify(value);
|
|
22
24
|
const createBoss = (db, work) => new PgBoss({
|
|
23
25
|
db,
|
|
24
26
|
schema: jobsSchema,
|
|
@@ -97,7 +99,8 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
97
99
|
if (!definitions.has(name)) throw new Error(`Job "${name}" is not among the app's jobs; export it from the module protobase.config.ts passes as \`jobs\``);
|
|
98
100
|
const checked = await validatePayload(definition, payload);
|
|
99
101
|
if ("invalid" in checked) throw new Error(checked.invalid);
|
|
100
|
-
const
|
|
102
|
+
const organization = organizationOf(target);
|
|
103
|
+
const data = jobData(checked.value, organization);
|
|
101
104
|
const orderedId = definition.options.ordered ? (await sql`select uuidv7() as id`.execute(target)).rows[0].id : void 0;
|
|
102
105
|
const send = {
|
|
103
106
|
...sendOptions(definition.options, options),
|
|
@@ -107,7 +110,7 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
107
110
|
const keyed = async (key) => {
|
|
108
111
|
if (options.mode === "replace") return (await boss.upsert(name, data, send)).jobs[0];
|
|
109
112
|
const runAt = send.startAfter === void 0 ? sql`null` : send.startAfter instanceof Date ? sql`${send.startAfter}::timestamptz` : sql`${sql.id(jobsSchema, "job_now")}() + ${send.startAfter} * interval '1 second'`;
|
|
110
|
-
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, ${
|
|
113
|
+
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, ${asJson(checked.value)}::jsonb, ${runAt}, ${key}, null, ${send.priority ?? 0}, ${asJson(organization)}::jsonb) as id`.execute(target);
|
|
111
114
|
return rows[0].id;
|
|
112
115
|
};
|
|
113
116
|
const id = options.key === void 0 ? await boss.send(name, data, send) : await keyed(options.key);
|
|
@@ -124,6 +127,7 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
124
127
|
const checked = await validatePayload(definition, payload);
|
|
125
128
|
if ("invalid" in checked) throw new Error(checked.invalid);
|
|
126
129
|
return {
|
|
130
|
+
payload: checked.value,
|
|
127
131
|
data: jobData(checked.value, organization),
|
|
128
132
|
send: sendOptions(definition.options, options),
|
|
129
133
|
key: options.key
|
|
@@ -149,10 +153,10 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
149
153
|
}]);
|
|
150
154
|
if (keyed.length > 0) {
|
|
151
155
|
const at = (job) => job.send.startAfter;
|
|
152
|
-
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, j.
|
|
153
|
-
from unnest(${keyed.map((job) =>
|
|
156
|
+
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, j.payload, coalesce(j.run_at, ${sql.id(jobsSchema, "job_now")}() + j.delay * interval '1 second'), j.key, null, j.priority, ${asJson(organization)}::jsonb) as id
|
|
157
|
+
from unnest(${keyed.map((job) => asJson(job.payload))}::jsonb[], ${keyed.map((job) => at(job) instanceof Date ? at(job) : null)}::timestamptz[],
|
|
154
158
|
${keyed.map((job) => typeof at(job) === "number" ? at(job) : null)}::float8[], ${keyed.map((job) => job.key)}::text[], ${keyed.map((job) => job.send.priority ?? 0)}::integer[])
|
|
155
|
-
with ordinality as j(
|
|
159
|
+
with ordinality as j(payload, run_at, delay, key, priority, place)
|
|
156
160
|
order by j.place`.execute(target);
|
|
157
161
|
rows.forEach((row, n) => {
|
|
158
162
|
if (!row.id) throw new Error(`Job "${name}" was not enqueued`);
|
package/dist/package.js
CHANGED
|
@@ -35,6 +35,6 @@ export declare const commentsSchemaSql: () => string;
|
|
|
35
35
|
export declare const mailFeedbackSchemaSql: () => string;
|
|
36
36
|
/** The table of who unsubscribed from which topic of the bulk mail. */
|
|
37
37
|
export declare const unsubscribesSchemaSql: () => string;
|
|
38
|
-
/** The role of
|
|
38
|
+
/** The role of read-only queries and its runner, with select on the tables of `models` but those of `auth`'s store. */
|
|
39
39
|
export declare const querySchemaSql: (models: readonly ResourceModel[], auth?: StoreNamesSource) => string;
|
|
40
40
|
//#endregion
|
|
@@ -45,7 +45,7 @@ const commentsSchemaSql = () => modulesSql({ comments: noInput });
|
|
|
45
45
|
const mailFeedbackSchemaSql = () => modulesSql({ mail_feedback: noInput });
|
|
46
46
|
/** The table of who unsubscribed from which topic of the bulk mail. */
|
|
47
47
|
const unsubscribesSchemaSql = () => modulesSql({ unsubscribes: noInput });
|
|
48
|
-
/** The role of
|
|
48
|
+
/** The role of read-only queries and its runner, with select on the tables of `models` but those of `auth`'s store. */
|
|
49
49
|
const querySchemaSql = (models, auth) => modulesSql({ query: queryInput(models, auth && storeNames(auth)) });
|
|
50
50
|
//#endregion
|
|
51
51
|
export { callsSchemaSql, channelSendsSchemaSql, commentsSchemaSql, computedSchemaSql, derivedSchemaSql, mailFeedbackSchemaSql, notificationsSchemaSql, querySchemaSql, realtimeRelaySchemaSql, relaySchemaSql, statusSchemaSql, unsubscribesSchemaSql, userTablesSchemaSql, webhooksSchemaSql, workflowsSchemaSql };
|
|
@@ -30,7 +30,7 @@ export type ModuleApp = {
|
|
|
30
30
|
events?: EventsOptions;
|
|
31
31
|
/** Whether the tables users add serve (`userTables`). */
|
|
32
32
|
userTables?: boolean;
|
|
33
|
-
/** Whether the built-in assistant's
|
|
33
|
+
/** Whether read-only queries run, of the built-in assistant or the MCP endpoint's `sql`, as a role of their own that must not read the auth store. */
|
|
34
34
|
readOnlyQueries?: boolean;
|
|
35
35
|
};
|
|
36
36
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.132.0",
|
|
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.
|
|
48
|
-
"@protobase/query": "0.
|
|
49
|
-
"@protobase/schema": "0.
|
|
50
|
-
"@protobase/workflow-plugins": "0.
|
|
51
|
-
"@protobase/workflows": "0.
|
|
47
|
+
"@protobase/layout": "0.132.0",
|
|
48
|
+
"@protobase/query": "0.132.0",
|
|
49
|
+
"@protobase/schema": "0.132.0",
|
|
50
|
+
"@protobase/workflow-plugins": "0.132.0",
|
|
51
|
+
"@protobase/workflows": "0.132.0",
|
|
52
52
|
"@react-email/render": "^2.1.0",
|
|
53
53
|
"@react-pdf/primitives": "^4.4.0",
|
|
54
54
|
"@react-pdf/renderer": "^4.9.1",
|