@protobase/server 0.133.7 → 0.133.9

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.
@@ -303,7 +303,8 @@ const createAdmin = ({ resources: appResources, views: appViews = [], userTables
303
303
  const tokens = relaySettings && auth && liveTokens({
304
304
  app: relaySettings.app,
305
305
  ticketKey: relaySettings.ticketKey,
306
- sessionEnd: sessionEnd(auth)
306
+ sessionEnd: sessionEnd(auth),
307
+ keyTypesOf: (name) => registry.find(name)?.keyTypes
307
308
  });
308
309
  const deps = {
309
310
  db,
@@ -114,7 +114,6 @@ const listRecords = async (deps, entry, session, input, maxFilterLength, jump =
114
114
  ...params.count === "exact" && { counted: await countUpTo(trx, queryModel, 10001, filter, scope) },
115
115
  ...params.labels !== void 0 && { labels: await relatedLabels(trx, labels, rows) },
116
116
  token: subject && await deps.liveTokens.seal(trx, subject, {
117
- resource: entry.name,
118
117
  model: queryModel,
119
118
  scope,
120
119
  ...filter && { filter },
@@ -0,0 +1,2 @@
1
+ import "./change-constant.js";
2
+ import { CheckedFilter, KeyType } from "@protobase/schema";
@@ -0,0 +1,54 @@
1
+ import { changeConstant } from "./change-constant.js";
2
+ //#region src/live/token/change-condition.ts
3
+ const operators = {
4
+ "=": "eq",
5
+ "!=": "neq",
6
+ "<": "lt",
7
+ "<=": "lte",
8
+ ">": "gt",
9
+ ">=": "gte"
10
+ };
11
+ const isPattern = (field, op, value) => field.type === "text" && (op === "=" || op === "!=") && value.kind === "string" && value.value.includes("*");
12
+ const comparedType = (field, keyTypesOf) => {
13
+ if (field.type !== "relation") return field.type;
14
+ const types = field.relation ? keyTypesOf(field.relation.resource) : void 0;
15
+ return types?.length === 1 && field.relation.columns.length <= 1 ? types[0] : void 0;
16
+ };
17
+ /**
18
+ * A checked filter on the row alone (`sameRowFilter`) as a condition of the change stream, which then lets in the rows
19
+ * Postgres would. Undefined when a part has no such form: a search, `similar`, `regex`, a JSON field's `has` or
20
+ * `present`, a text pattern, a lookup field, a relation to a key of several parts, or a value `changeConstant` leaves out.
21
+ * `keyTypesOf` gives the key types of the resource a relation points at.
22
+ */
23
+ const changeCondition = (filter, keyTypesOf) => {
24
+ if (filter.kind === "and" || filter.kind === "or") {
25
+ const args = filter.args.map((arg) => changeCondition(arg, keyTypesOf));
26
+ if (args.some((arg) => arg === void 0)) return;
27
+ return filter.kind === "and" ? { and: args } : { or: args };
28
+ }
29
+ if (filter.kind === "not") {
30
+ const arg = changeCondition(filter.arg, keyTypesOf);
31
+ return arg && { not: arg };
32
+ }
33
+ if (filter.kind === "search" || filter.field.lookup) return;
34
+ const column = filter.field.column;
35
+ if (filter.kind === "isNull") return { is_null: column };
36
+ if (filter.kind === "present") return filter.field.type === "json" ? void 0 : { not: { is_null: column } };
37
+ if (filter.kind === "compare") {
38
+ if (isPattern(filter.field, filter.op, filter.value)) return;
39
+ const type = comparedType(filter.field, keyTypesOf);
40
+ const name = operators[filter.op];
41
+ const value = type && changeConstant(type, filter.value, name !== "eq" && name !== "neq");
42
+ if (value === void 0) return;
43
+ return { [name]: [column, value] };
44
+ }
45
+ if (filter.kind === "in") {
46
+ const type = comparedType(filter.field, keyTypesOf);
47
+ if (!type) return;
48
+ const values = filter.values.map((value) => changeConstant(type, value, false));
49
+ if (values.length === 0 || values.some((value) => value === void 0)) return;
50
+ return { in: [column, values] };
51
+ }
52
+ };
53
+ //#endregion
54
+ export { changeCondition };
@@ -0,0 +1 @@
1
+ import "@protobase/schema";
@@ -0,0 +1,45 @@
1
+ //#region src/live/token/change-constant.ts
2
+ const textTypes = /* @__PURE__ */ new Set([
3
+ "text",
4
+ "enum",
5
+ "currency",
6
+ "country",
7
+ "mail_sender"
8
+ ]);
9
+ const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
10
+ const exactNumber = (text) => {
11
+ if (!/^-?\d+(\.\d+)?$/.test(text)) return;
12
+ const plain = text.replace(/^(-?)0+(?=\d)/, "$1").replace(/(\.\d*?)0+$/, "$1").replace(/\.$/, "");
13
+ const number = Number(plain);
14
+ return String(number) === plain ? number : void 0;
15
+ };
16
+ const integerOf = (text, ordered) => {
17
+ if (!/^-?\d+$/.test(text)) return;
18
+ const canonical = BigInt(text).toString();
19
+ if (Number.isSafeInteger(Number(canonical))) return Number(canonical);
20
+ return ordered ? void 0 : canonical;
21
+ };
22
+ const numericText = (value) => value.kind === "number" ? value.raw : value.kind === "string" ? value.value : void 0;
23
+ /**
24
+ * The value as the stream compares it with a column of `type` as Postgres does, for an ordering comparison when
25
+ * `ordered`; undefined where it would compare differently: a date, a timestamp or `now()`, an order on text or booleans,
26
+ * a number a JS number cannot hold exactly. A relation's column compares as the type of the key it points at.
27
+ */
28
+ const changeConstant = (type, value, ordered) => {
29
+ if (type === "boolean") return value.kind === "boolean" && !ordered ? value.value : void 0;
30
+ if (type === "integer" || type === "bigint") {
31
+ const text = numericText(value);
32
+ return text === void 0 ? void 0 : integerOf(text, ordered);
33
+ }
34
+ if (type === "decimal") {
35
+ const text = numericText(value);
36
+ return text === void 0 ? void 0 : exactNumber(text);
37
+ }
38
+ if (type === "uuid") {
39
+ const text = value.kind === "string" ? value.value.toLowerCase() : void 0;
40
+ return text !== void 0 && uuidPattern.test(text) ? text : void 0;
41
+ }
42
+ if (textTypes.has(type)) return value.kind === "string" && !ordered ? value.value : void 0;
43
+ };
44
+ //#endregion
45
+ export { changeConstant };
@@ -1,3 +1,4 @@
1
+ import "./change-condition.js";
1
2
  //#region src/live/token/live-token.d.ts
2
3
  /**
3
4
  * Who a live token is for: a signed-in person in their sign-in session, which ends it, or a visitor, bound to nothing.
@@ -1,11 +1,11 @@
1
1
  import { Session } from "../../types.js";
2
+ import "./change-condition.js";
2
3
  import { LiveSubject } from "./live-token.js";
3
4
  import { CheckedFilter, ResourceModel } from "@protobase/schema";
4
5
  import { Db, Scope } from "@protobase/query";
5
6
  //#region src/live/token/live-tokens.d.ts
6
7
  /** What a read gave a live token for: the query's model and scope, the caller's filter and the fields it returned. */
7
8
  export type LiveTokenRead = {
8
- resource: string;
9
9
  model: ResourceModel;
10
10
  scope: Scope;
11
11
  filter?: CheckedFilter;
@@ -1,22 +1,35 @@
1
+ import { changeCondition } from "./change-condition.js";
1
2
  import { liveTokenKey } from "./live-key.js";
2
3
  import { liveSubject } from "./live-subject.js";
3
4
  import { sealLiveToken } from "./live-token.js";
4
- import { printFilter } from "@protobase/schema";
5
5
  import { sameRowFilter } from "@protobase/query";
6
6
  import { sql } from "kysely";
7
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`. */
8
+ const changeResource = ({ table }) => {
9
+ if (table.name.includes(".") || table.schema?.includes(".")) return;
10
+ return table.schema && table.schema !== "public" ? `${table.schema}.${table.name}` : table.name;
11
+ };
12
+ /**
13
+ * Live tokens sealed with the key derived from the relay's ticket key, for the app the relay knows as `app`. `keyTypesOf`
14
+ * gives the key types of a resource a relation points at, which its conditions compare by.
15
+ */
9
16
  const liveTokens = (options) => {
10
17
  const key = liveTokenKey(options.ticketKey);
11
18
  return {
12
19
  subject: (session) => liveSubject(session, options.sessionEnd),
13
20
  seal: async (trx, subject, read) => {
14
- const where = await sameRowFilter(trx, read.model, read.scope, read.filter);
15
- if (where === false) return;
21
+ const resource = changeResource(read.model);
22
+ if (!resource) return;
23
+ const filter = await sameRowFilter(trx, read.model, read.scope, read.filter);
24
+ if (filter === false) return;
25
+ const where = filter && changeCondition(filter, options.keyTypesOf);
26
+ if (filter && !where) return;
16
27
  const { rows } = await sql`select pg_current_snapshot()::text as snapshot`.execute(trx);
17
28
  const claims = {
18
- resource: read.resource,
19
- ...where && { where: printFilter(where) },
29
+ subs: [{
30
+ resource,
31
+ ...where && { where }
32
+ }],
20
33
  fields: read.fields,
21
34
  snapshot: rows[0].snapshot
22
35
  };
package/dist/package.js CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.133.7";
2
+ var version = "0.133.9";
3
3
  //#endregion
4
4
  export { version };
@@ -47,7 +47,6 @@ const readRecord = async (deps, entry, session, key, showDeleted = false, live =
47
47
  softDelete: void 0
48
48
  } : access.full;
49
49
  const read = {
50
- resource: entry.name,
51
50
  model: full,
52
51
  scope: ownScope(access, full),
53
52
  filter: keyFilter(full, key),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/server",
3
- "version": "0.133.7",
3
+ "version": "0.133.9",
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.7",
48
- "@protobase/query": "0.133.7",
49
- "@protobase/schema": "0.133.7",
50
- "@protobase/workflow-plugins": "0.133.7",
51
- "@protobase/workflows": "0.133.7",
47
+ "@protobase/layout": "0.133.9",
48
+ "@protobase/query": "0.133.9",
49
+ "@protobase/schema": "0.133.9",
50
+ "@protobase/workflow-plugins": "0.133.9",
51
+ "@protobase/workflows": "0.133.9",
52
52
  "@react-email/render": "^2.1.0",
53
53
  "@react-pdf/primitives": "^4.4.0",
54
54
  "@react-pdf/renderer": "^4.9.1",