@protobase/server 0.133.1 → 0.133.3
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/api-cors.js +4 -1
- package/dist/api-keys/caller-settings.js +10 -5
- package/dist/batch/run-batch.js +2 -2
- package/dist/changes/actor.js +7 -6
- package/dist/changes/capture-schema-sql.d.ts +2 -1
- package/dist/changes/capture-schema-sql.js +2 -1
- package/dist/changes/events-options.d.ts +5 -0
- package/dist/changes/logical-message.js +17 -0
- package/dist/changes/stream/schema-messages-sql.js +42 -0
- package/dist/changes/stream/stream-module.js +48 -0
- package/dist/comments/post-comment.js +2 -1
- package/dist/create-admin.js +23 -8
- package/dist/deps.d.ts +3 -0
- package/dist/functions/define-function.d.ts +5 -0
- package/dist/functions/routes.js +4 -1
- package/dist/functions/supabase-routes.js +6 -3
- package/dist/list-service.js +15 -5
- package/dist/live/live-route.js +20 -5
- package/dist/live/refresh/live-refresh.d.ts +13 -0
- package/dist/live/refresh/live-refresh.js +8 -0
- package/dist/live/refresh/refresh-notices.js +21 -0
- package/dist/live/token/key-filter.js +31 -0
- package/dist/live/token/live-headers.js +9 -0
- package/dist/live/token/live-key.js +16 -0
- package/dist/live/token/live-preference.js +11 -0
- package/dist/live/token/live-subject.js +26 -0
- package/dist/live/token/live-token.d.ts +14 -0
- package/dist/live/token/live-token.js +24 -0
- package/dist/live/token/live-tokens.d.ts +20 -0
- package/dist/live/token/live-tokens.js +28 -0
- package/dist/live/visible-notices.js +5 -1
- package/dist/package.js +1 -1
- package/dist/read-service.js +27 -6
- package/dist/records.d.ts +1 -1
- package/dist/request-access.d.ts +1 -1
- package/dist/rest/rest-context.js +4 -2
- package/dist/routes/collection.js +17 -6
- package/dist/routes/record.js +8 -8
- package/dist/schema-modules/modules.d.ts +2 -1
- package/dist/schema-modules/modules.js +2 -0
- package/dist/schema-modules/needed-modules.js +1 -0
- package/dist/sql-parts.d.ts +1 -1
- package/dist/storage/storage-db.js +6 -7
- package/dist/transactions.js +16 -6
- package/dist/webhooks/attempt.js +1 -0
- package/dist/webhooks/webhooks.js +3 -1
- package/dist/write-pipeline.js +2 -2
- package/dist/write-shared.d.ts +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/live/token/key-filter.ts
|
|
2
|
+
const span = {
|
|
3
|
+
start: 0,
|
|
4
|
+
end: 0
|
|
5
|
+
};
|
|
6
|
+
const literalOf = (value) => typeof value === "number" ? {
|
|
7
|
+
kind: "number",
|
|
8
|
+
value,
|
|
9
|
+
raw: String(value),
|
|
10
|
+
span
|
|
11
|
+
} : {
|
|
12
|
+
kind: "string",
|
|
13
|
+
value,
|
|
14
|
+
span
|
|
15
|
+
};
|
|
16
|
+
/** The record with this key, as a filter: each part of the primary key among its one value, as `=` reads a `*` in text as a wildcard. */
|
|
17
|
+
const keyFilter = (model, key) => {
|
|
18
|
+
const parts = model.primaryKey.map((name, i) => ({
|
|
19
|
+
kind: "in",
|
|
20
|
+
field: model.fields[name],
|
|
21
|
+
values: [literalOf(key[i])],
|
|
22
|
+
span
|
|
23
|
+
}));
|
|
24
|
+
return parts.length === 1 ? parts[0] : {
|
|
25
|
+
kind: "and",
|
|
26
|
+
args: parts,
|
|
27
|
+
span
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { keyFilter };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/live/token/live-headers.ts
|
|
2
|
+
/** The headers of a list or record read: it varies with `Prefer`, and carries the live token when one was asked for and made. */
|
|
3
|
+
const liveHeaders = (token) => token ? {
|
|
4
|
+
Vary: "Prefer",
|
|
5
|
+
"Preference-Applied": "live",
|
|
6
|
+
"Protobase-Live": token
|
|
7
|
+
} : { Vary: "Prefer" };
|
|
8
|
+
//#endregion
|
|
9
|
+
export { liveHeaders };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/live/token/live-key.ts
|
|
2
|
+
/** What the live token key is derived for, so the same secret signs relay tickets and seals live tokens apart. */
|
|
3
|
+
const info = "protobase live token";
|
|
4
|
+
/** The 256-bit key live tokens are sealed with: HKDF-SHA-256 of the relay's ticket key, which the relay holds too. */
|
|
5
|
+
const liveTokenKey = async (ticketKey) => {
|
|
6
|
+
const secret = await crypto.subtle.importKey("raw", new TextEncoder().encode(ticketKey), "HKDF", false, ["deriveBits"]);
|
|
7
|
+
const bits = await crypto.subtle.deriveBits({
|
|
8
|
+
name: "HKDF",
|
|
9
|
+
hash: "SHA-256",
|
|
10
|
+
salt: /* @__PURE__ */ new Uint8Array(),
|
|
11
|
+
info: new TextEncoder().encode(info)
|
|
12
|
+
}, secret, 256);
|
|
13
|
+
return new Uint8Array(bits);
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { liveTokenKey };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/live/token/live-preference.ts
|
|
2
|
+
/**
|
|
3
|
+
* Whether the request asks for a live token: RFC 7240 `Prefer: live`, alone or among other preferences, in one header
|
|
4
|
+
* or several, or `?live=1` where a page cannot set headers.
|
|
5
|
+
*/
|
|
6
|
+
const asksLive = (c) => {
|
|
7
|
+
if (c.req.query("live") === "1") return true;
|
|
8
|
+
return (c.req.header("prefer") ?? "").split(",").some((preference) => preference.split(/[=;]/, 1)[0].trim().toLowerCase() === "live");
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { asksLive };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/live/token/live-subject.ts
|
|
2
|
+
/** How long a token lasts at most: a visitor's always, longer than the API lets anyone cache a response (it lets none). */
|
|
3
|
+
const liveTokenSeconds = 3600;
|
|
4
|
+
/**
|
|
5
|
+
* Whom a live token is for: a visitor, unbound, for `liveTokenSeconds`; a person signed in with a session of the app,
|
|
6
|
+
* bound to it until it ends, at most `liveTokenSeconds`. Undefined for any other caller, such as an API key or an
|
|
7
|
+
* outside app, and for a session that has ended: they get no token.
|
|
8
|
+
*/
|
|
9
|
+
const liveSubject = async (session, sessionEnd, now = /* @__PURE__ */ new Date()) => {
|
|
10
|
+
const longest = new Date(now.getTime() + liveTokenSeconds * 1e3);
|
|
11
|
+
if (session.anonymous) return {
|
|
12
|
+
anonymous: true,
|
|
13
|
+
expiresAt: longest
|
|
14
|
+
};
|
|
15
|
+
const id = session.signIn?.session;
|
|
16
|
+
if (session.client || typeof id !== "string" || id === "") return;
|
|
17
|
+
const end = await sessionEnd(id);
|
|
18
|
+
if (!end || end.getTime() <= now.getTime()) return;
|
|
19
|
+
return {
|
|
20
|
+
user: String(session.user.id),
|
|
21
|
+
session: id,
|
|
22
|
+
expiresAt: end < longest ? end : longest
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { liveSubject, liveTokenSeconds };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/live/token/live-token.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Who a live token is for: a signed-in person in their sign-in session, which ends it, or a visitor, bound to nothing.
|
|
4
|
+
* `expiresAt` is when it stops being taken.
|
|
5
|
+
*/
|
|
6
|
+
export type LiveSubject = {
|
|
7
|
+
user: string;
|
|
8
|
+
session: string;
|
|
9
|
+
expiresAt: Date;
|
|
10
|
+
} | {
|
|
11
|
+
anonymous: true;
|
|
12
|
+
expiresAt: Date;
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { EncryptJWT } from "jose";
|
|
2
|
+
//#region src/live/token/live-token.ts
|
|
3
|
+
/** The largest token sent: the response's whole header block stays under a proxy's default 4 KB buffer. */
|
|
4
|
+
const maxLiveTokenBytes = 3e3;
|
|
5
|
+
/**
|
|
6
|
+
* The read sealed as a JWE (`dir`, A256GCM) with the live token key, so its caller can neither read nor change the
|
|
7
|
+
* conditions, which may hold keys of records they cannot see. Undefined when it is longer than `maxLiveTokenBytes`.
|
|
8
|
+
*/
|
|
9
|
+
const sealLiveToken = async (key, app, read, subject, now = /* @__PURE__ */ new Date()) => {
|
|
10
|
+
const claims = {
|
|
11
|
+
...read,
|
|
12
|
+
..."session" in subject ? { sid: subject.session } : { anonymous: true }
|
|
13
|
+
};
|
|
14
|
+
const jwt = new EncryptJWT(claims).setProtectedHeader({
|
|
15
|
+
alg: "dir",
|
|
16
|
+
enc: "A256GCM",
|
|
17
|
+
typ: "JWT"
|
|
18
|
+
}).setIssuer(app).setIssuedAt(Math.floor(now.getTime() / 1e3)).setExpirationTime(Math.floor(subject.expiresAt.getTime() / 1e3));
|
|
19
|
+
if ("user" in subject) jwt.setSubject(subject.user);
|
|
20
|
+
const token = await jwt.encrypt(key);
|
|
21
|
+
return token.length > 3e3 ? void 0 : token;
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
export { maxLiveTokenBytes, sealLiveToken };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Session } from "../../types.js";
|
|
2
|
+
import { LiveSubject } from "./live-token.js";
|
|
3
|
+
import { CheckedFilter, ResourceModel } from "@protobase/schema";
|
|
4
|
+
import { Db, Scope } from "@protobase/query";
|
|
5
|
+
//#region src/live/token/live-tokens.d.ts
|
|
6
|
+
/** What a read gave a live token for: the query's model and scope, the caller's filter and the fields it returned. */
|
|
7
|
+
export type LiveTokenRead = {
|
|
8
|
+
resource: string;
|
|
9
|
+
model: ResourceModel;
|
|
10
|
+
scope: Scope;
|
|
11
|
+
filter?: CheckedFilter;
|
|
12
|
+
fields: string[];
|
|
13
|
+
};
|
|
14
|
+
export type LiveTokens = {
|
|
15
|
+
/** Whom a token would be for, before the read's transaction opens; undefined when the caller gets none. */
|
|
16
|
+
subject: (session: Session) => Promise<LiveSubject | undefined>;
|
|
17
|
+
/** The token for the read, in its REPEATABLE READ transaction; undefined when it cannot be made or is too long. */
|
|
18
|
+
seal: (trx: Db, subject: LiveSubject, read: LiveTokenRead) => Promise<string | undefined>;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { liveTokenKey } from "./live-key.js";
|
|
2
|
+
import { liveSubject } from "./live-subject.js";
|
|
3
|
+
import { sealLiveToken } from "./live-token.js";
|
|
4
|
+
import { printFilter } from "@protobase/schema";
|
|
5
|
+
import { sameRowFilter } from "@protobase/query";
|
|
6
|
+
import { sql } from "kysely";
|
|
7
|
+
//#region src/live/token/live-tokens.ts
|
|
8
|
+
/** Live tokens sealed with the key derived from the relay's ticket key, for the app the relay knows as `app`. */
|
|
9
|
+
const liveTokens = (options) => {
|
|
10
|
+
const key = liveTokenKey(options.ticketKey);
|
|
11
|
+
return {
|
|
12
|
+
subject: (session) => liveSubject(session, options.sessionEnd),
|
|
13
|
+
seal: async (trx, subject, read) => {
|
|
14
|
+
const where = await sameRowFilter(trx, read.model, read.scope, read.filter);
|
|
15
|
+
if (where === false) return;
|
|
16
|
+
const { rows } = await sql`select pg_current_snapshot()::text as snapshot`.execute(trx);
|
|
17
|
+
const claims = {
|
|
18
|
+
resource: read.resource,
|
|
19
|
+
...where && { where: printFilter(where) },
|
|
20
|
+
fields: read.fields,
|
|
21
|
+
snapshot: rows[0].snapshot
|
|
22
|
+
};
|
|
23
|
+
return sealLiveToken(await key, options.app, claims, subject);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { liveTokens };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { requestAccess } from "../request-access.js";
|
|
2
|
+
import "./refresh/refresh-notices.js";
|
|
2
3
|
import { ringNotice } from "@protobase/schema/internal";
|
|
3
4
|
//#region src/live/visible-notices.ts
|
|
4
5
|
/**
|
|
5
6
|
* Which notices a caller hears of: those of the live resources they may list or read, in their tenant, and, with
|
|
6
|
-
* calls, those of the calls that ring them (`ringNotice`, keyed by the user's id)
|
|
7
|
+
* calls, those of the calls that ring them (`ringNotice`, keyed by the user's id), and those that refresh their
|
|
8
|
+
* access (`refreshNotice`, keyed likewise). A notice names a record, never its
|
|
7
9
|
* values, and the page reads the record again as the caller, so no row filter applies here. A notice of `anyRecord`
|
|
8
10
|
* without a tenant, of a view's source without tenants, reaches every tenant's readers of the view.
|
|
9
11
|
*/
|
|
@@ -16,8 +18,10 @@ const visibleNotices = async (deps, session) => {
|
|
|
16
18
|
readable.set(entry.name, entry.model.tenant ? String(session.tenant) : void 0);
|
|
17
19
|
}
|
|
18
20
|
const rung = deps.calls && !session.anonymous && !session.client ? String(session.user.id) : void 0;
|
|
21
|
+
const own = session.anonymous ? void 0 : String(session.user.id);
|
|
19
22
|
return (notice) => {
|
|
20
23
|
if (notice.resource === ringNotice) return rung !== void 0 && notice.key === rung;
|
|
24
|
+
if (notice.resource === "$live") return own !== void 0 && notice.key === own;
|
|
21
25
|
if (!readable.has(notice.resource)) return false;
|
|
22
26
|
const tenant = readable.get(notice.resource);
|
|
23
27
|
return tenant === void 0 || tenant === notice.tenant || notice.key === "" && notice.tenant === null;
|
package/dist/package.js
CHANGED
package/dist/read-service.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { notFound } from "./problem.js";
|
|
2
|
-
import { requestAccess, requireOperation, visibleFields, writableFields } from "./request-access.js";
|
|
2
|
+
import { ownScope, requestAccess, requireOperation, visibleFields, writableFields } from "./request-access.js";
|
|
3
3
|
import { pickFields } from "./rows.js";
|
|
4
|
-
import { readTransaction } from "./transactions.js";
|
|
5
|
-
import { deletedRows } from "./params.js";
|
|
4
|
+
import { readTransaction, snapshotReadTransaction } from "./transactions.js";
|
|
5
|
+
import { deletedRows, selectedFields } from "./params.js";
|
|
6
6
|
import { fetchReadable } from "./fetch-readable.js";
|
|
7
7
|
import { presentFiles } from "./files/present.js";
|
|
8
8
|
import { rowPermissions } from "./row-permissions.js";
|
|
9
|
+
import { keyFilter } from "./live/token/key-filter.js";
|
|
9
10
|
//#region src/read-service.ts
|
|
10
11
|
/**
|
|
11
12
|
* `edit` for fields the caller may change on this record, `read` for the rest (sensitive ones included); hidden fields
|
|
@@ -21,13 +22,19 @@ const fieldModes = (access, canUpdate) => {
|
|
|
21
22
|
* The record with this key as the caller may see it: readable fields only, inside the caller's tenant and row filter.
|
|
22
23
|
* A key outside the tenant, outside the row filter, or failing a record-level read rule is a plain 404. `showDeleted`
|
|
23
24
|
* also finds a soft-deleted record, and is refused on a resource without a soft delete once the caller may read it.
|
|
25
|
+
* `live` asks for a live token of the read (`token`), for a live resource when the app has one to give and no
|
|
26
|
+
* record-level read rule decides it, which reads no filter: the read then runs in one snapshot. `fields`, the
|
|
27
|
+
* selection of `?fields=`, is checked against the caller's readable fields and is what the token names (`columns`).
|
|
24
28
|
*/
|
|
25
|
-
const readRecord = async (deps, entry, session, key, showDeleted = false) => {
|
|
29
|
+
const readRecord = async (deps, entry, session, key, showDeleted = false, live = false, fields) => {
|
|
26
30
|
const access = await requestAccess(deps, entry, session);
|
|
27
31
|
requireOperation(access, "read");
|
|
28
32
|
const deleted = deletedRows(access.full, showDeleted);
|
|
29
33
|
const names = Object.keys(access.model.fields);
|
|
30
|
-
const
|
|
34
|
+
const subject = live && entry.model.live && deps.liveTokens && !access.resolved.recordChecks.includes("read") ? await deps.liveTokens.subject(session) : void 0;
|
|
35
|
+
const transaction = subject ? snapshotReadTransaction : readTransaction;
|
|
36
|
+
const sealed = subject && selectedFields(access.model, fields);
|
|
37
|
+
const found = await transaction(deps.db, deps.statementTimeoutMs, async (trx) => {
|
|
31
38
|
const current = await fetchReadable(trx, access, key, deleted, {
|
|
32
39
|
etagFields: names,
|
|
33
40
|
fields: visibleFields(access)
|
|
@@ -35,18 +42,32 @@ const readRecord = async (deps, entry, session, key, showDeleted = false) => {
|
|
|
35
42
|
if (!current) return void 0;
|
|
36
43
|
const record = pickFields(current.record, names);
|
|
37
44
|
const [row] = await rowPermissions(trx, access, [record]);
|
|
45
|
+
const full = deleted === "show" ? {
|
|
46
|
+
...access.full,
|
|
47
|
+
softDelete: void 0
|
|
48
|
+
} : access.full;
|
|
49
|
+
const read = {
|
|
50
|
+
resource: entry.name,
|
|
51
|
+
model: full,
|
|
52
|
+
scope: ownScope(access, full),
|
|
53
|
+
filter: keyFilter(full, key),
|
|
54
|
+
fields: sealed || names
|
|
55
|
+
};
|
|
56
|
+
const token = subject && await deps.liveTokens.seal(trx, subject, read);
|
|
38
57
|
return {
|
|
39
58
|
record,
|
|
40
59
|
etag: current.etag,
|
|
41
60
|
permissions: {
|
|
42
61
|
...row,
|
|
43
62
|
fields: fieldModes(access, row.update)
|
|
44
|
-
}
|
|
63
|
+
},
|
|
64
|
+
token
|
|
45
65
|
};
|
|
46
66
|
});
|
|
47
67
|
if (!found) throw notFound(`No ${entry.name} with this key`);
|
|
48
68
|
return {
|
|
49
69
|
...found,
|
|
70
|
+
columns: selectedFields(access.model, fields),
|
|
50
71
|
record: (await presentFiles(access, [found.record]))[0]
|
|
51
72
|
};
|
|
52
73
|
};
|
package/dist/records.d.ts
CHANGED
package/dist/request-access.d.ts
CHANGED
|
@@ -2,15 +2,17 @@ import { readTransaction, writeTransaction } from "../transactions.js";
|
|
|
2
2
|
import { wakeDispatch } from "../changes/actor.js";
|
|
3
3
|
import { callerSettings } from "./caller-settings.js";
|
|
4
4
|
import { restError } from "./rest-error.js";
|
|
5
|
+
import { callerActor } from "../api-keys/caller-settings.js";
|
|
5
6
|
//#region src/rest/rest-context.ts
|
|
6
7
|
/**
|
|
7
8
|
* Runs `work` in one transaction as the request's caller (see `callerSettings`), under the statement timeout;
|
|
8
9
|
* read-only for a read, so a function that writes fails as on PostgREST.
|
|
9
10
|
*/
|
|
10
11
|
const asCaller = async (deps, c, readOnly, work) => {
|
|
11
|
-
const
|
|
12
|
+
const caller = c.get("caller");
|
|
13
|
+
const settings = callerSettings(caller, deps.actorSetting);
|
|
12
14
|
if (readOnly) return readTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
13
|
-
const result = await writeTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
15
|
+
const result = await writeTransaction(deps.db, deps.statementTimeoutMs, work, settings, callerActor(caller));
|
|
14
16
|
wakeDispatch(deps);
|
|
15
17
|
return result;
|
|
16
18
|
};
|
|
@@ -10,6 +10,8 @@ import { batchAggregates } from "../aggregate-batch.js";
|
|
|
10
10
|
import { mapRecords } from "../map-service.js";
|
|
11
11
|
import { uploadFile } from "../files/upload.js";
|
|
12
12
|
import { returnPreference } from "./preferences.js";
|
|
13
|
+
import { asksLive } from "../live/token/live-preference.js";
|
|
14
|
+
import { liveHeaders } from "../live/token/live-headers.js";
|
|
13
15
|
import { resolveTarget, unknownAction } from "./target.js";
|
|
14
16
|
import { Hono } from "hono";
|
|
15
17
|
import { encodeKey } from "@protobase/schema/internal";
|
|
@@ -20,8 +22,11 @@ const warningHeader = (warning) => warning ? { "X-Protobase-Warning": warning }
|
|
|
20
22
|
const listMethod = async (c, deps, entry, action, query, within) => {
|
|
21
23
|
const session = c.get("session");
|
|
22
24
|
if (action === "") {
|
|
23
|
-
const { body, warning } = await listRecords(deps, entry, session, query, void 0, false, within);
|
|
24
|
-
return c.json(body, 200,
|
|
25
|
+
const { body, warning, token } = await listRecords(deps, entry, session, query, void 0, false, within, asksLive(c));
|
|
26
|
+
return c.json(body, 200, {
|
|
27
|
+
...warningHeader(warning),
|
|
28
|
+
...liveHeaders(token)
|
|
29
|
+
});
|
|
25
30
|
}
|
|
26
31
|
if (action === "facets") {
|
|
27
32
|
const { body, warning } = await facets(deps, entry, session, query, within);
|
|
@@ -74,8 +79,11 @@ const collectionRoutes = (deps) => {
|
|
|
74
79
|
if (action !== "search") return unknownAction(action);
|
|
75
80
|
const body = parseParams(listParams, await c.req.json());
|
|
76
81
|
const within = await belongingFilter(deps, entry, c.get("session"), parent, key);
|
|
77
|
-
const { body: page, warning } = await listRecords(deps, entry, c.get("session"), body, searchFilterLength, false, within);
|
|
78
|
-
return c.json(page, 200,
|
|
82
|
+
const { body: page, warning, token } = await listRecords(deps, entry, c.get("session"), body, searchFilterLength, false, within, asksLive(c));
|
|
83
|
+
return c.json(page, 200, {
|
|
84
|
+
...warningHeader(warning),
|
|
85
|
+
...liveHeaders(token)
|
|
86
|
+
});
|
|
79
87
|
});
|
|
80
88
|
app.post("/:target", async (c) => {
|
|
81
89
|
const { entry, action } = resolveTarget(deps, c.req.param("target"));
|
|
@@ -83,8 +91,11 @@ const collectionRoutes = (deps) => {
|
|
|
83
91
|
if (action === "upload") return c.json(await uploadFile(deps, entry, session, c.req.raw), 201);
|
|
84
92
|
const body = await c.req.json();
|
|
85
93
|
if (action === "search") {
|
|
86
|
-
const { body: page, warning } = await listRecords(deps, entry, session, body, searchFilterLength);
|
|
87
|
-
return c.json(page, 200,
|
|
94
|
+
const { body: page, warning, token } = await listRecords(deps, entry, session, body, searchFilterLength, false, void 0, asksLive(c));
|
|
95
|
+
return c.json(page, 200, {
|
|
96
|
+
...warningHeader(warning),
|
|
97
|
+
...liveHeaders(token)
|
|
98
|
+
});
|
|
88
99
|
}
|
|
89
100
|
if (action === "batchAggregates") {
|
|
90
101
|
const { body: read, warning } = await batchAggregates(deps, entry, session, body);
|
package/dist/routes/record.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { notFound } from "../problem.js";
|
|
2
2
|
import { pickFields } from "../rows.js";
|
|
3
|
-
import { selectedFields } from "../params.js";
|
|
4
3
|
import { keyFromPath } from "../key.js";
|
|
5
4
|
import { runWrite } from "../write-pipeline.js";
|
|
6
5
|
import { readRecord } from "../read-service.js";
|
|
@@ -8,6 +7,8 @@ import { postComment } from "../comments/post-comment.js";
|
|
|
8
7
|
import { readComments } from "../comments/read-comments.js";
|
|
9
8
|
import { auditOrigin } from "../audit/origin.js";
|
|
10
9
|
import { returnPreference } from "./preferences.js";
|
|
10
|
+
import { asksLive } from "../live/token/live-preference.js";
|
|
11
|
+
import { liveHeaders } from "../live/token/live-headers.js";
|
|
11
12
|
import { resolveTarget } from "./target.js";
|
|
12
13
|
import { revealField } from "../reveal-service.js";
|
|
13
14
|
import { readHistory } from "../history/read-history.js";
|
|
@@ -28,16 +29,15 @@ const recordRoutes = (deps) => {
|
|
|
28
29
|
return c.json(await readRecordCalls(deps, entry, c.get("session"), key));
|
|
29
30
|
}
|
|
30
31
|
if (c.req.param("key").endsWith(":mentionable")) return c.json(await mentionable(deps, entry, c.get("session"), keyFromPath(c, entry, ":mentionable"), c.req.query()));
|
|
31
|
-
const found = await readRecord(deps, entry, c.get("session"), keyFromPath(c, entry), c.req.query("show_deleted") === "true");
|
|
32
|
-
const columns = selectedFields({
|
|
33
|
-
...entry.model,
|
|
34
|
-
fields: Object.fromEntries(Object.keys(found.record).map((name) => [name, entry.model.fields[name]]))
|
|
35
|
-
}, c.req.query("fields"));
|
|
32
|
+
const found = await readRecord(deps, entry, c.get("session"), keyFromPath(c, entry), c.req.query("show_deleted") === "true", asksLive(c), c.req.query("fields"));
|
|
36
33
|
return c.json({
|
|
37
|
-
...pickFields(found.record, columns),
|
|
34
|
+
...pickFields(found.record, found.columns),
|
|
38
35
|
etag: found.etag,
|
|
39
36
|
permissions: found.permissions
|
|
40
|
-
}, 200, {
|
|
37
|
+
}, 200, {
|
|
38
|
+
ETag: found.etag,
|
|
39
|
+
...liveHeaders(found.token)
|
|
40
|
+
});
|
|
41
41
|
});
|
|
42
42
|
const reveal = async (c, entry) => {
|
|
43
43
|
const body = await c.req.json();
|
|
@@ -5,6 +5,7 @@ export declare const schemaModules: {
|
|
|
5
5
|
mail_feedback: SchemaModule;
|
|
6
6
|
computed: SchemaModule;
|
|
7
7
|
changes: SchemaModule;
|
|
8
|
+
change_stream: SchemaModule;
|
|
8
9
|
history: SchemaModule;
|
|
9
10
|
derived: SchemaModule;
|
|
10
11
|
notices: SchemaModule;
|
|
@@ -31,7 +32,7 @@ export type ModulesModel = Partial<Record<SchemaModuleId, ModuleModel>>;
|
|
|
31
32
|
/** The schema modules an app needs, each with what it gives the module (`neededModules`), in their order. */
|
|
32
33
|
export type ModuleTargets = Partial<Record<SchemaModuleId, ModuleInput>>;
|
|
33
34
|
/** The modules of `targets`, in their order. */
|
|
34
|
-
export declare const targetIds: (targets: ModuleTargets) => ("status" | "query" | "user_tables" | "history" | "comments" | "derived" | "jobs" | "changes" | "mail_feedback" | "calls" | "webhooks" | "webhook_delivery_log" | "workflows" | "workflow_custom_steps" | "computed" | "notices" | "workflow_storage" | "notification_devices" | "notification_history" | "relay" | "realtime_relay" | "channel_sends" | "unsubscribes")[];
|
|
35
|
+
export declare const targetIds: (targets: ModuleTargets) => ("status" | "query" | "user_tables" | "history" | "comments" | "derived" | "jobs" | "changes" | "mail_feedback" | "calls" | "webhooks" | "webhook_delivery_log" | "workflows" | "workflow_custom_steps" | "computed" | "change_stream" | "notices" | "workflow_storage" | "notification_devices" | "notification_history" | "relay" | "realtime_relay" | "channel_sends" | "unsubscribes")[];
|
|
35
36
|
/** The model of each module of `targets` this version of Protobase reads. */
|
|
36
37
|
export declare const latestModules: (targets: ModuleTargets) => ModulesModel;
|
|
37
38
|
//#endregion
|
|
@@ -3,6 +3,7 @@ import { channelSendsModule } from "../channels/sends-module.js";
|
|
|
3
3
|
import { computedModule } from "../computed/computed-module.js";
|
|
4
4
|
import { derivedModule } from "../derived/derived-module.js";
|
|
5
5
|
import { changesModule } from "../changes/changes-module.js";
|
|
6
|
+
import { streamModule } from "../changes/stream/stream-module.js";
|
|
6
7
|
import { commentsModule } from "../comments/comments-module.js";
|
|
7
8
|
import { historyModule } from "../history/history-module.js";
|
|
8
9
|
import { latestModel } from "./module-difference.js";
|
|
@@ -28,6 +29,7 @@ const schemaModules = {
|
|
|
28
29
|
mail_feedback: mailFeedbackModule,
|
|
29
30
|
computed: computedModule,
|
|
30
31
|
changes: changesModule,
|
|
32
|
+
change_stream: streamModule,
|
|
31
33
|
history: historyModule,
|
|
32
34
|
derived: derivedModule,
|
|
33
35
|
notices: noticesModule,
|
|
@@ -40,6 +40,7 @@ const neededModules = (app) => {
|
|
|
40
40
|
...app.mailFeedback && { mail_feedback: noInput },
|
|
41
41
|
...app.models.some(computedOn) && { computed: computedInput(app.models) },
|
|
42
42
|
...changes && { changes: changesInput(captured, app.events) },
|
|
43
|
+
...app.events?.stream && { change_stream: events },
|
|
43
44
|
...app.models.some((model) => model.history) && { history: events },
|
|
44
45
|
...derivedNames(app.models).length > 0 && { derived: derivedInput(app.models) },
|
|
45
46
|
...(app.models.some((model) => model.live) || Boolean(app.calls)) && { notices: events },
|
package/dist/sql-parts.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { readTransaction, writeTransaction } from "../transactions.js";
|
|
2
|
-
import { callerSettings } from "../api-keys/caller-settings.js";
|
|
3
|
-
import { sql } from "kysely";
|
|
1
|
+
import { applySettings, readTransaction, writeTransaction } from "../transactions.js";
|
|
2
|
+
import { callerActor, callerSettings } from "../api-keys/caller-settings.js";
|
|
4
3
|
//#region src/storage/storage-db.ts
|
|
5
4
|
/**
|
|
6
5
|
* Makes the rest of the transaction run as `caller`: its role and claims, as `/api/sb/rest/v1` sets them (see
|
|
7
6
|
* `callerSettings`), so the restored `storage.objects` and `storage.buckets` policies decide what it reads and writes.
|
|
8
|
-
* What runs before it runs as the app's own role, which owns the tables.
|
|
7
|
+
* What runs before it runs as the app's own role, which owns the tables. A signed-in caller's `protobase_actor`
|
|
8
|
+
* message goes with it, in the same statement.
|
|
9
9
|
*/
|
|
10
10
|
const becomeCaller = async (trx, deps, caller) => {
|
|
11
|
-
|
|
12
|
-
await sql`select ${sql.join(settings.map(([name, value]) => sql`set_config(${name}, ${value}, true)`))}`.execute(trx);
|
|
11
|
+
await applySettings(trx, callerSettings(caller, deps.actorSetting), callerActor(caller));
|
|
13
12
|
};
|
|
14
13
|
/** Runs `work` in one transaction as `caller`, read-only when `readOnly`. */
|
|
15
14
|
const asCaller = (deps, caller, readOnly, work) => {
|
|
16
15
|
const settings = callerSettings(caller, deps.actorSetting);
|
|
17
16
|
if (readOnly) return readTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
18
|
-
return writeTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
17
|
+
return writeTransaction(deps.db, deps.statementTimeoutMs, work, settings, callerActor(caller));
|
|
19
18
|
};
|
|
20
19
|
/** Runs `work` in one transaction as the app's own role, for what no caller decides: public objects, signed URLs. */
|
|
21
20
|
const asApp = (deps, work) => writeTransaction(deps.db, deps.statementTimeoutMs, work);
|
package/dist/transactions.js
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
|
+
import { actorPrefix, emitMessage } from "./changes/logical-message.js";
|
|
1
2
|
import { sql } from "kysely";
|
|
2
3
|
//#region src/transactions.ts
|
|
3
4
|
/**
|
|
5
|
+
* Sets `settings` for the rest of the open transaction and, given an `actor`, emits its `protobase_actor` message, so
|
|
6
|
+
* the change stream knows who wrote the rows that follow: one statement, one round trip.
|
|
7
|
+
*/
|
|
8
|
+
const applySettings = async (trx, settings, actor) => {
|
|
9
|
+
const parts = Object.entries(settings).map(([name, value]) => sql`set_config(${name}, ${value}, true)`);
|
|
10
|
+
if (actor) parts.push(sql`${emitMessage(actorPrefix, JSON.stringify(actor))} is not null`);
|
|
11
|
+
if (parts.length === 0) return;
|
|
12
|
+
await sql`select ${sql.join(parts)}`.execute(trx);
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
4
15
|
* One transaction whose statements Postgres cancels after `timeoutMs` (SET LOCAL statement_timeout), with `settings`
|
|
5
|
-
* set for it as well, all in one statement.
|
|
16
|
+
* set for it as well and `actor`'s message emitted (see `applySettings`), all in one statement.
|
|
6
17
|
*/
|
|
7
|
-
const writeTransaction = (db, timeoutMs, run, settings = {}) => db.transaction().execute(async (trx) => {
|
|
8
|
-
|
|
18
|
+
const writeTransaction = (db, timeoutMs, run, settings = {}, actor) => db.transaction().execute(async (trx) => {
|
|
19
|
+
await applySettings(trx, {
|
|
9
20
|
statement_timeout: String(timeoutMs),
|
|
10
21
|
...settings
|
|
11
|
-
});
|
|
12
|
-
await sql`select ${sql.join(all.map(([name, value]) => sql`set_config(${name}, ${value}, true)`))}`.execute(trx);
|
|
22
|
+
}, actor);
|
|
13
23
|
return run(trx);
|
|
14
24
|
});
|
|
15
25
|
/** Same, and Postgres refuses any write inside it. */
|
|
@@ -23,4 +33,4 @@ const snapshotReadTransaction = (db, timeoutMs, run) => db.transaction().setIsol
|
|
|
23
33
|
return run(trx);
|
|
24
34
|
});
|
|
25
35
|
//#endregion
|
|
26
|
-
export { readTransaction, snapshotReadTransaction, writeTransaction };
|
|
36
|
+
export { applySettings, readTransaction, snapshotReadTransaction, writeTransaction };
|
package/dist/webhooks/attempt.js
CHANGED
|
@@ -14,6 +14,7 @@ import { maxTries, nextRetryDelay } from "./retry-schedule.js";
|
|
|
14
14
|
import { eventChange, subscriberEvents } from "./subscriber-view.js";
|
|
15
15
|
import { attemptDelivery } from "./attempt.js";
|
|
16
16
|
import { subscriptionProblems, subscriptionRouter } from "./route-events.js";
|
|
17
|
+
import { liveRefresh } from "../live/refresh/live-refresh.js";
|
|
17
18
|
import { tableSubscriptionName } from "./table/options.js";
|
|
18
19
|
import { sendTestJob } from "./table/schema-sql.js";
|
|
19
20
|
import { testEvent } from "./test-event.js";
|
|
@@ -49,7 +50,7 @@ const deliveryGroup = ({ subscription, change }) => `${subscription.name}:${chan
|
|
|
49
50
|
*
|
|
50
51
|
* No jobs without a resource with events or history; the subscriptions must name declared events.
|
|
51
52
|
*/
|
|
52
|
-
const createWebhooks = ({ deps, subscriptions, functions, channels, schema, log, table, onChanges }) => {
|
|
53
|
+
const createWebhooks = ({ deps, subscriptions, functions, channels, live = liveRefresh(void 0), schema, log, table, onChanges }) => {
|
|
53
54
|
const entries = deps.registry.entries;
|
|
54
55
|
const models = entries.map((entry) => entry.model);
|
|
55
56
|
const problems = subscriptionProblems(subscriptions, models, Object.keys(functions));
|
|
@@ -72,6 +73,7 @@ const createWebhooks = ({ deps, subscriptions, functions, channels, schema, log,
|
|
|
72
73
|
functions,
|
|
73
74
|
records,
|
|
74
75
|
channels,
|
|
76
|
+
live,
|
|
75
77
|
attempt: tried,
|
|
76
78
|
signal,
|
|
77
79
|
allowPrivateAddresses
|
package/dist/write-pipeline.js
CHANGED
|
@@ -4,7 +4,7 @@ import { requestAccess, requireOperation, targetFilter, writableFields } from ".
|
|
|
4
4
|
import { writeTransaction } from "./transactions.js";
|
|
5
5
|
import { bindingOf, pendingBinding } from "./encryption/binding.js";
|
|
6
6
|
import { deleteRecord, fetchRecord, restoreRecord, updateRecord } from "./records.js";
|
|
7
|
-
import { actorSettings, wakeDispatch } from "./changes/actor.js";
|
|
7
|
+
import { actorSettings, sessionActor, wakeDispatch } from "./changes/actor.js";
|
|
8
8
|
import { isDatabaseError } from "./error-handler.js";
|
|
9
9
|
import { attachFiles, scheduleRemovedFiles } from "./files/attach.js";
|
|
10
10
|
import { assertWritten, checkParents, checkPrecondition, decide, denied, firstKeyField, keyOf, missing, notifier, shown } from "./write-shared.js";
|
|
@@ -150,7 +150,7 @@ const runWrite = async (deps, entry, session, request, form) => {
|
|
|
150
150
|
const result = await withDeclaredChecks(deps.registry, () => writeTransaction(deps.db, deps.statementTimeoutMs, async (trx) => {
|
|
151
151
|
await lockOrders(trx, locks);
|
|
152
152
|
return writeIn(trx, deps, access, request, removed);
|
|
153
|
-
}, actorSettings(deps, session)));
|
|
153
|
+
}, actorSettings(deps, session), sessionActor(session)));
|
|
154
154
|
await scheduleRemovedFiles(deps, removed);
|
|
155
155
|
wakeDispatch(deps);
|
|
156
156
|
return result;
|
package/dist/write-shared.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Row } from "./types.js";
|
|
|
3
3
|
import "./deps.js";
|
|
4
4
|
import "./request-access.js";
|
|
5
5
|
import "./records.js";
|
|
6
|
-
import "@protobase/schema";
|
|
6
|
+
import { CheckedFilter } from "@protobase/schema";
|
|
7
7
|
import { Db } from "@protobase/query";
|
|
8
8
|
//#region src/write-shared.d.ts
|
|
9
9
|
export type WriteResult = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.133.
|
|
3
|
+
"version": "0.133.3",
|
|
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.133.
|
|
48
|
-
"@protobase/query": "0.133.
|
|
49
|
-
"@protobase/schema": "0.133.
|
|
50
|
-
"@protobase/workflow-plugins": "0.133.
|
|
51
|
-
"@protobase/workflows": "0.133.
|
|
47
|
+
"@protobase/layout": "0.133.3",
|
|
48
|
+
"@protobase/query": "0.133.3",
|
|
49
|
+
"@protobase/schema": "0.133.3",
|
|
50
|
+
"@protobase/workflow-plugins": "0.133.3",
|
|
51
|
+
"@protobase/workflows": "0.133.3",
|
|
52
52
|
"@react-email/render": "^2.1.0",
|
|
53
53
|
"@react-pdf/primitives": "^4.4.0",
|
|
54
54
|
"@react-pdf/renderer": "^4.9.1",
|