@adminiumjs/manifest 0.3.67 → 0.3.69

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.
@@ -110,6 +110,39 @@ export const plainTextRef = (entry) => (typeof entry === 'string' ? entry : entr
110
110
  /** The longest a plain-text entry takes. */
111
111
  export const plainTextMax = (entry) => (typeof entry === 'string' ? PLAIN_TEXT_MAX : (entry.max ?? PLAIN_TEXT_MAX));
112
112
  const keyNameSchema = z.string().regex(/^[a-z][a-z0-9-]{0,31}$/, 'a key name is kebab-case');
113
+ /**
114
+ * What a shopper may ask of a column of a list anyone reads: equal to, one
115
+ * of (at most {@link SHOPPER_IN_MOST} values), and at least, at most, more or
116
+ * less than. Never a pattern of their own (`like`), never "or": a shopper's
117
+ * filter only narrows what the entry shows, one condition on top of another.
118
+ */
119
+ export const SHOPPER_OPS = ['eq', 'in', 'gte', 'lte', 'gt', 'lt'];
120
+ /** The operators a column takes when the entry names none. */
121
+ export const SHOPPER_DEFAULT_OPS = ['eq', 'in', 'gte', 'lte'];
122
+ /** The most values one `in` of a shopper's filter carries. */
123
+ export const SHOPPER_IN_MOST = 50;
124
+ /** The most columns a list anyone reads may be filtered, searched and ordered by. */
125
+ export const SHOPPER_FILTERABLE_MOST = 8;
126
+ export const SHOPPER_SEARCHABLE_MOST = 4;
127
+ export const SHOPPER_ORDERABLE_MOST = 5;
128
+ /** The fewest characters a search asks for, and how many searches a minute one visitor makes. */
129
+ export const SHOPPER_SEARCH_MIN = 2;
130
+ export const SHOPPER_SEARCHES_A_MINUTE = 30;
131
+ /** How far a list ordered by the shopper pages by offset: past it, by cursor. */
132
+ export const SHOPPER_OFFSET_MOST = 1_000;
133
+ /** How far a list anyone reads counts its rows before it says "1,000 or more". */
134
+ export const SHOPPER_COUNT_MOST = 1_000;
135
+ /** The longest page an entry of a file not made on an Adminium serves. */
136
+ export const SHOPPER_PAGE_MOST = 100;
137
+ /** A column a shopper may filter by: its ref (the default operators), or its ref with its own. */
138
+ const shopperFilterSchema = z.union([
139
+ refSchema,
140
+ z.object({ column: refSchema, ops: z.array(z.enum(SHOPPER_OPS)).min(1).max(SHOPPER_OPS.length) }).strict(),
141
+ ]);
142
+ /** The column a shopper's filter names. */
143
+ export const shopperFilterColumn = (filter) => (typeof filter === 'string' ? filter : filter.column);
144
+ /** The operators a shopper's filter takes on its column. */
145
+ export const shopperFilterOps = (filter) => (typeof filter === 'string' ? SHOPPER_DEFAULT_OPS : filter.ops);
113
146
  const valueFilterSchema = z
114
147
  .object({
115
148
  column: refSchema,
@@ -524,6 +557,31 @@ export const publicAccessSchema = z
524
557
  select: z.array(refSchema).optional(),
525
558
  writable: z.array(refSchema).optional(),
526
559
  filters: z.array(z.union([valueFilterSchema, relativeFilterSchema])).optional(),
560
+ /**
561
+ * A list anyone reads (a catalogue): the columns a shopper may filter it
562
+ * by, each with the operators it takes (`eq`, `in`, `gte`, `lte` when
563
+ * none are named). Every filter is ANDed after the entry's own `filters`,
564
+ * so a shopper's only narrows what the entry shows.
565
+ */
566
+ filterable: z.array(shopperFilterSchema).min(1).max(SHOPPER_FILTERABLE_MOST).optional(),
567
+ /** Text columns a shopper's search looks in: a part of the text, any case, from two characters. */
568
+ searchable: z.array(refSchema).min(1).max(SHOPPER_SEARCHABLE_MOST).optional(),
569
+ /** Columns a shopper may order the list by, each never empty; the key always breaks a tie. */
570
+ orderable: z.array(refSchema).min(1).max(SHOPPER_ORDERABLE_MOST).optional(),
571
+ /**
572
+ * Filters through another list anyone reads on the same key, whose rows
573
+ * point at this one (a product's variants): `entry` is that list's table,
574
+ * `on` its link to this table, `columns` what may be asked of its rows.
575
+ * Every condition sent through it must hold on ONE of those rows, as that
576
+ * list shows them (its own filters included): a product is found by
577
+ * "Slate and M" only when one variant it has is both.
578
+ */
579
+ via: z
580
+ .object({ entry: refSchema, on: refSchema, columns: z.array(shopperFilterSchema).min(1).max(4) })
581
+ .strict()
582
+ .optional(),
583
+ /** The first page of a list anyone reads says how many rows it holds, counted to a thousand. */
584
+ count: z.literal(true).optional(),
527
585
  /** Values the server writes, whatever the browser sends (`status: confirmed`). */
528
586
  defaults: z.record(refSchema, z.union([z.string(), z.number(), z.boolean()])).optional(),
529
587
  /** How a person proves who they are on this key (see {@link claimSchema}). */
@@ -1023,12 +1081,187 @@ function appColumnIssues(entries, index) {
1023
1081
  });
1024
1082
  return out;
1025
1083
  }
1084
+ /** Whether an entry declares any of a shopper's lists: filters, search, order, a filter through another list, a count. */
1085
+ export function shopperLists(entry) {
1086
+ return entry.filterable !== undefined || entry.searchable !== undefined || entry.orderable !== undefined || entry.via !== undefined || entry.count !== undefined;
1087
+ }
1088
+ /** The columns an entry shows: its `select`, else every column but a code Adminium makes and a secret (as the server reads it). */
1089
+ function shownColumns(entry, index) {
1090
+ if (entry.select !== undefined)
1091
+ return entry.select;
1092
+ return (index.table(entry.table)?.columns ?? [])
1093
+ .filter((column) => column.rules?.['code'] === undefined && column.rules?.['secret'] !== true)
1094
+ .map((column) => column.ref);
1095
+ }
1096
+ /**
1097
+ * A SHOPPER'S LISTS: which entries may say them, and of which columns.
1098
+ *
1099
+ * "Does a row match" is a read of the column it asks about, one question at a
1100
+ * time. So the lists are said only of a list anyone reads — GET alone, no
1101
+ * sign-in, no code that opens it, nothing withheld from some readers — of a
1102
+ * table that is no person's, and only of columns the list shows that are no
1103
+ * code, secret, personal data, file or JSON, nor the app an add-on's row is
1104
+ * for. A search reads text; an order is by a column that is never empty, so
1105
+ * the three engines put the rows in one order and a cursor never stops at a
1106
+ * gap. A filter through another list is the same rule twice, and its link
1107
+ * points at this table.
1108
+ */
1109
+ function shopperListIssues(entries, ctx) {
1110
+ const out = [];
1111
+ const { index } = ctx;
1112
+ // A person's table: one a key signs people in on, finds them by, or claims rows for.
1113
+ const people = new Set();
1114
+ for (const entry of entries) {
1115
+ if (entry.claim !== undefined && !('by' in entry.claim))
1116
+ people.add(entry.table);
1117
+ if (entry.identity !== undefined)
1118
+ people.add(entry.identity.table);
1119
+ if (entry.claimedBy !== undefined)
1120
+ people.add(entry.claimedBy.table);
1121
+ }
1122
+ const anyoneReads = (entry) => {
1123
+ if (!readByAnyone(entry))
1124
+ return 'a list anyone reads, with no sign-in';
1125
+ if (entry.methods.length !== 1)
1126
+ return 'a list that only reads (GET alone)';
1127
+ if (entry.unlockBy !== undefined)
1128
+ return 'a list no code opens';
1129
+ if (entry.withhold !== undefined)
1130
+ return 'a list that shows every reader the same columns';
1131
+ if (people.has(entry.table))
1132
+ return `a list of no person's rows, and "${entry.table}" holds people`;
1133
+ return null;
1134
+ };
1135
+ const why = (entry, column) => {
1136
+ const found = index.column(entry.table, column);
1137
+ if (found === undefined)
1138
+ return `"${entry.table}" has no column "${column}"`;
1139
+ if (!shownColumns(entry, index).includes(column))
1140
+ return `"${column}" is not shown by the entry (its select), so nothing may be asked of it`;
1141
+ if (found.rules?.['code'] !== undefined || shareCodeColumns(entries, entry.table).includes(column))
1142
+ return `"${column}" is a code: a shopper's list may not ask about it`;
1143
+ if (found.rules?.['secret'] === true)
1144
+ return `"${column}" is a secret`;
1145
+ if (found.type === 'file' || found.type === 'json')
1146
+ return `"${column}" holds ${found.type === 'file' ? 'a file' : 'JSON'}, which no list asks about`;
1147
+ const table = index.table(entry.table);
1148
+ if (table !== undefined && personalColumn(table, column) !== null)
1149
+ return `"${column}" is personal data: a shopper's list may not ask about it`;
1150
+ if (entry.appColumn === column)
1151
+ return `"${column}" holds the app the row is for, which Adminium alone fills`;
1152
+ return null;
1153
+ };
1154
+ entries.forEach((entry, i) => {
1155
+ if (!shopperLists(entry))
1156
+ return;
1157
+ const at = (...rest) => ['publicAccess', i, ...rest];
1158
+ const notAnyone = anyoneReads(entry);
1159
+ if (notAnyone !== null) {
1160
+ for (const key of ['filterable', 'searchable', 'orderable', 'via', 'count']) {
1161
+ if (entry[key] !== undefined)
1162
+ out.push({ path: at(key), message: `a shopper's ${key} is said only of ${notAnyone}` });
1163
+ }
1164
+ return;
1165
+ }
1166
+ const seen = new Set();
1167
+ (entry.filterable ?? []).forEach((filter, f) => {
1168
+ const column = shopperFilterColumn(filter);
1169
+ if (seen.has(column))
1170
+ out.push({ path: at('filterable', f), message: `"${column}" is listed twice` });
1171
+ seen.add(column);
1172
+ const wrong = why(entry, column);
1173
+ if (wrong !== null)
1174
+ out.push({ path: at('filterable', f), message: wrong });
1175
+ const ops = shopperFilterOps(filter);
1176
+ if (new Set(ops).size !== ops.length)
1177
+ out.push({ path: at('filterable', f, 'ops'), message: 'an operator is listed once' });
1178
+ });
1179
+ (entry.searchable ?? []).forEach((column, c) => {
1180
+ const wrong = why(entry, column);
1181
+ if (wrong !== null)
1182
+ out.push({ path: at('searchable', c), message: wrong });
1183
+ else if (index.column(entry.table, column)?.type !== 'text')
1184
+ out.push({ path: at('searchable', c), message: `"${column}" is searched as text: a text column` });
1185
+ if ((entry.searchable ?? []).indexOf(column) !== c)
1186
+ out.push({ path: at('searchable', c), message: `"${column}" is listed twice` });
1187
+ });
1188
+ (entry.orderable ?? []).forEach((column, c) => {
1189
+ const wrong = why(entry, column);
1190
+ const found = index.column(entry.table, column);
1191
+ if (wrong !== null)
1192
+ out.push({ path: at('orderable', c), message: wrong });
1193
+ else if (found?.nullable === true && found.role !== 'pk') {
1194
+ out.push({ path: at('orderable', c), message: `"${column}" may be empty, and an empty value sorts first on one engine and last on another: order by a column that is never empty` });
1195
+ }
1196
+ if ((entry.orderable ?? []).indexOf(column) !== c)
1197
+ out.push({ path: at('orderable', c), message: `"${column}" is listed twice` });
1198
+ });
1199
+ const via = entry.via;
1200
+ if (via !== undefined) {
1201
+ const key = entry.key ?? CUSTOMER_KEY;
1202
+ const lists = entries.filter((other) => other.table === via.entry && (other.key ?? CUSTOMER_KEY) === key && other.methods.includes('GET'));
1203
+ const through = lists[0];
1204
+ if (via.entry === entry.table)
1205
+ out.push({ path: at('via', 'entry'), message: 'a filter through another list names another table' });
1206
+ else if (lists.length !== 1 || through === undefined) {
1207
+ out.push({ path: at('via', 'entry'), message: `a filter through "${via.entry}" names the one entry that reads it on the "${key}" key, and there ${lists.length === 0 ? 'is none' : `are ${String(lists.length)}`}` });
1208
+ }
1209
+ else {
1210
+ const notThrough = anyoneReads(through);
1211
+ if (notThrough !== null)
1212
+ out.push({ path: at('via', 'entry'), message: `a filter through "${via.entry}" reads rows as that list shows them, so it is ${notThrough}` });
1213
+ else if (shopperLists(through) && through.via !== undefined)
1214
+ out.push({ path: at('via', 'entry'), message: `"${via.entry}" filters through another list itself: one step only` });
1215
+ const link = index.column(via.entry, via.on);
1216
+ if (link === undefined)
1217
+ out.push({ path: at('via', 'on'), message: `"${via.entry}" has no column "${via.on}"` });
1218
+ else if (link.type !== 'fk' || link.references !== entry.table)
1219
+ out.push({ path: at('via', 'on'), message: `"${via.entry}.${via.on}" is not a link to "${entry.table}"` });
1220
+ else if (!shownColumns(through, index).includes(via.on))
1221
+ out.push({ path: at('via', 'on'), message: `"${via.on}" joins "${via.entry}" to this list: the "${via.entry}" list shows it, or a filter through it would say what it hides` });
1222
+ const named = new Set();
1223
+ via.columns.forEach((filter, f) => {
1224
+ const column = shopperFilterColumn(filter);
1225
+ if (named.has(column))
1226
+ out.push({ path: at('via', 'columns', f), message: `"${column}" is listed twice` });
1227
+ named.add(column);
1228
+ const wrong = why(through, column);
1229
+ if (wrong !== null)
1230
+ out.push({ path: at('via', 'columns', f), message: wrong });
1231
+ });
1232
+ }
1233
+ }
1234
+ });
1235
+ // An entry's own pages, from a file not made on an Adminium too: a page of at most a hundred, ordered by a column never empty.
1236
+ if (ctx.local !== true) {
1237
+ entries.forEach((entry, i) => {
1238
+ const pages = entry.pagination;
1239
+ if (pages === undefined)
1240
+ return;
1241
+ const at = (...rest) => ['publicAccess', i, 'pagination', ...rest];
1242
+ if (pages.maxLimit > SHOPPER_PAGE_MOST)
1243
+ out.push({ path: at('maxLimit'), message: `a page holds at most ${String(SHOPPER_PAGE_MOST)} rows` });
1244
+ const ordered = pages.order;
1245
+ if (ordered === undefined)
1246
+ return;
1247
+ const column = ordered.slice(0, ordered.lastIndexOf('.'));
1248
+ const found = index.column(entry.table, column);
1249
+ if (found !== undefined && found.nullable === true && found.role !== 'pk') {
1250
+ out.push({ path: at('order'), message: `"${column}" may be empty, and an empty value sorts first on one engine and last on another: order by a column that is never empty` });
1251
+ }
1252
+ if (found !== undefined && !shownColumns(entry, index).includes(column))
1253
+ out.push({ path: at('order'), message: `"${column}" is not shown by the entry, and an order says what a column holds` });
1254
+ });
1255
+ }
1256
+ return out;
1257
+ }
1026
1258
  /** Everything in `publicAccess` and `publicKeys` that names something undeclared, or breaks a rule. */
1027
1259
  export function publicAccessIssues(entries, ctx) {
1028
1260
  const out = [];
1029
1261
  const { index } = ctx;
1030
1262
  const has = index.has;
1031
1263
  out.push(...appColumnIssues(entries, index));
1264
+ out.push(...shopperListIssues(entries, ctx));
1032
1265
  const doorKeys = ctx.chatReserved === false ? [FORMS_KEY] : DOOR_KEYS;
1033
1266
  if (doorKeys.includes(CHAT_KEY) && ctx.publicKeys?.[CHAT_KEY] !== undefined) {
1034
1267
  out.push({ path: ['publicKeys', CHAT_KEY], message: '"chat" is the name of the entries a chat\'s left questions write through, and no browser key: name the key otherwise' });
@@ -1234,7 +1467,8 @@ export function publicAccessIssues(entries, ctx) {
1234
1467
  else if (!claimed)
1235
1468
  out.push({ path: at('methods'), message: `${method} is allowed only with a claim` });
1236
1469
  }
1237
- for (const field of ['path', 'pagination', 'rateLimit', 'response']) {
1470
+ // Pages are any file's to say from 0.3.68 on (a page of at most a hundred, checked with the shopper's lists).
1471
+ for (const field of ['path', 'rateLimit', 'response']) {
1238
1472
  if (entry[field] !== undefined && ctx.local !== true)
1239
1473
  out.push({ path: at(field), message: `"${field}" is read only from a file made on an Adminium (publisher "local")` });
1240
1474
  }