@inferencebureau/apps-sdk 0.4.1 → 0.6.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/README.md +4 -2
- package/dist/index.d.ts +56 -3
- package/dist/index.js +4 -1
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.js +3 -2
- package/dist/test.d.ts +56 -7
- package/dist/test.js +1107 -77
- package/package.json +1 -1
package/dist/test.js
CHANGED
|
@@ -8,9 +8,12 @@
|
|
|
8
8
|
* The records follow the records engine's rules (packages/apps/src/records.ts): field types and conversions,
|
|
9
9
|
* defaults, validations, uniqueness, belongsTo, filters, sorting and pages, and the errors the API answers with, as
|
|
10
10
|
* ApiError. An action's run goes through the same runner as a deployment (runtime.ts): its params and record arrive
|
|
11
|
-
* as JSON, its writes are applied when `run` returns, then `onSuccess` runs with the saved record
|
|
12
|
-
*
|
|
13
|
-
* and
|
|
11
|
+
* as JSON, its writes are applied when `run` returns, then `onSuccess` runs with the saved record, each within its
|
|
12
|
+
* `timeoutMS`. Calls run as the system unless a test says who they're for (`runAs`, `api.asRole`, `api.forShop`), and
|
|
13
|
+
* are then held to accessControl/permissions.ib.ts and the store as the API holds them (packages/apps/src/access.ts).
|
|
14
|
+
* Webhook entries' `filter`s are applied as Shopify applies them, and `queueSchedules` queues what settings.ib.ts's
|
|
15
|
+
* schedules owe in a window of time. Computed fields are worked out as records.ts's SQL works them out. Not covered:
|
|
16
|
+
* relationship fields other than belongsTo, except as the hasMany a computed field aggregates over.
|
|
14
17
|
*
|
|
15
18
|
* File fields keep their files in memory: `base64` reads back as a `data:` URL, `copyURL` as the URL itself (not
|
|
16
19
|
* fetched, size 0), and `upload`/`createUpload` tokens as the upload (a `createUpload` needs no PUT here).
|
|
@@ -190,6 +193,309 @@ function contains(a, b) {
|
|
|
190
193
|
const sameJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
191
194
|
/** `data->>key`: text, or null. */
|
|
192
195
|
const asText = (v) => (v === null || v === undefined ? null : typeof v === "string" ? v : JSON.stringify(v));
|
|
196
|
+
const AGGREGATES = ["count", "sum", "avg", "min", "max"];
|
|
197
|
+
const FUNCTIONS = {
|
|
198
|
+
concat: [1, 20], coalesce: [2, 20], if: [3, 3], min: [2, 20], max: [2, 20],
|
|
199
|
+
round: [1, 2], floor: [1, 1], ceil: [1, 1], abs: [1, 1], lower: [1, 1], upper: [1, 1], length: [1, 1],
|
|
200
|
+
};
|
|
201
|
+
const CTX = { seen: [], depth: 0 };
|
|
202
|
+
function computationOf(def) {
|
|
203
|
+
const hasExpr = def.expression !== undefined, hasAgg = def.aggregate !== undefined;
|
|
204
|
+
if (hasExpr === hasAgg)
|
|
205
|
+
throw new Error('needs an expression ("price * quantity") or an aggregate ({ count: "reviews" })');
|
|
206
|
+
if (hasExpr) {
|
|
207
|
+
if (typeof def.expression !== "string" || !def.expression.trim())
|
|
208
|
+
throw new Error("expression must be text");
|
|
209
|
+
return { kind: "expression", expr: parseExpression(def.expression) };
|
|
210
|
+
}
|
|
211
|
+
const a = def.aggregate;
|
|
212
|
+
if (!isObj(a))
|
|
213
|
+
throw new Error('aggregate must be an object like { count: "reviews" }');
|
|
214
|
+
const fns = AGGREGATES.filter((f) => a[f] !== undefined);
|
|
215
|
+
const extra = Object.keys(a).filter((k) => k !== "filter" && !AGGREGATES.includes(k));
|
|
216
|
+
if (extra.length)
|
|
217
|
+
throw new Error(`aggregate doesn't take ${extra[0]}; use one of ${AGGREGATES.join(", ")}, and filter`);
|
|
218
|
+
if (fns.length !== 1)
|
|
219
|
+
throw new Error(`aggregate takes one of ${AGGREGATES.join(", ")}`);
|
|
220
|
+
const fn = fns[0];
|
|
221
|
+
const path = a[fn];
|
|
222
|
+
if (typeof path !== "string" || !/^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)?$/.test(path))
|
|
223
|
+
throw new Error(`aggregate.${fn} must name a hasMany field, like "reviews"${fn === "count" ? "" : ' or "reviews.rating"'}`);
|
|
224
|
+
const [relation, field = null] = path.split(".");
|
|
225
|
+
if (fn !== "count" && !field)
|
|
226
|
+
throw new Error(`aggregate.${fn} needs the field to ${fn}, like "${relation}.rating"`);
|
|
227
|
+
if (a.filter !== undefined && !isObj(a.filter))
|
|
228
|
+
throw new Error("aggregate.filter must be a filter object, like { status: { equals: \"approved\" } }");
|
|
229
|
+
return { kind: "aggregate", fn, relation, field, filter: a.filter };
|
|
230
|
+
}
|
|
231
|
+
function exprTokens(s) {
|
|
232
|
+
const out = [];
|
|
233
|
+
let i = 0;
|
|
234
|
+
while (i < s.length) {
|
|
235
|
+
const c = s[i];
|
|
236
|
+
if (/\s/.test(c)) {
|
|
237
|
+
i++;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const num = /^\d+(\.\d+)?([eE][+-]?\d+)?/.exec(s.slice(i));
|
|
241
|
+
if (num) {
|
|
242
|
+
out.push({ t: "num", v: Number(num[0]) });
|
|
243
|
+
i += num[0].length;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const id = /^[A-Za-z_$][\w$]*/.exec(s.slice(i));
|
|
247
|
+
if (id) {
|
|
248
|
+
out.push({ t: "id", v: id[0] });
|
|
249
|
+
i += id[0].length;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (c === '"' || c === "'") {
|
|
253
|
+
let v = "", j = i + 1;
|
|
254
|
+
for (; j < s.length && s[j] !== c; j++)
|
|
255
|
+
v += s[j] === "\\" && j + 1 < s.length ? s[++j] : s[j];
|
|
256
|
+
if (j >= s.length)
|
|
257
|
+
throw new Error("expression has an unclosed string");
|
|
258
|
+
out.push({ t: "str", v });
|
|
259
|
+
i = j + 1;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const sym = ["==", "!=", "<=", ">=", "&&", "||", "+", "-", "*", "/", "<", ">", "!", "(", ")", ","].find((x) => s.startsWith(x, i));
|
|
263
|
+
if (!sym)
|
|
264
|
+
throw new Error(`expression can't use ${JSON.stringify(c)}`);
|
|
265
|
+
out.push({ t: "sym", v: sym });
|
|
266
|
+
i += sym.length;
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
function parseExpression(text) {
|
|
271
|
+
const ts = exprTokens(text);
|
|
272
|
+
let i = 0;
|
|
273
|
+
const isSym = (v) => { const t = ts[i]; return !!t && t.t === "sym" && t.v === v; };
|
|
274
|
+
const isWord = (v) => { const t = ts[i]; return !!t && t.t === "id" && t.v === v; };
|
|
275
|
+
const expect = (v) => { if (!isSym(v))
|
|
276
|
+
throw new Error(`expression expected ${v}${ts[i] ? ` before ${String(ts[i].v)}` : " at the end"}`); i++; };
|
|
277
|
+
const or = () => { let a = and(); while (isSym("||") || isWord("or")) {
|
|
278
|
+
i++;
|
|
279
|
+
a = { k: "op", op: "or", a, b: and() };
|
|
280
|
+
} return a; };
|
|
281
|
+
const and = () => { let a = not(); while (isSym("&&") || isWord("and")) {
|
|
282
|
+
i++;
|
|
283
|
+
a = { k: "op", op: "and", a, b: not() };
|
|
284
|
+
} return a; };
|
|
285
|
+
const not = () => { if (isSym("!") || isWord("not")) {
|
|
286
|
+
i++;
|
|
287
|
+
return { k: "not", a: not() };
|
|
288
|
+
} return cmp(); };
|
|
289
|
+
const cmp = () => {
|
|
290
|
+
const a = add();
|
|
291
|
+
const t = ts[i];
|
|
292
|
+
if (t?.t === "sym" && ["==", "!=", "<", "<=", ">", ">="].includes(t.v)) {
|
|
293
|
+
i++;
|
|
294
|
+
return { k: "op", op: t.v, a, b: add() };
|
|
295
|
+
}
|
|
296
|
+
return a;
|
|
297
|
+
};
|
|
298
|
+
const add = () => { let a = mul(); while (isSym("+") || isSym("-")) {
|
|
299
|
+
const op = ts[i++].v;
|
|
300
|
+
a = { k: "op", op, a, b: mul() };
|
|
301
|
+
} return a; };
|
|
302
|
+
const mul = () => { let a = unary(); while (isSym("*") || isSym("/")) {
|
|
303
|
+
const op = ts[i++].v;
|
|
304
|
+
a = { k: "op", op, a, b: unary() };
|
|
305
|
+
} return a; };
|
|
306
|
+
const unary = () => { if (isSym("-")) {
|
|
307
|
+
i++;
|
|
308
|
+
return { k: "neg", a: unary() };
|
|
309
|
+
} return primary(); };
|
|
310
|
+
const primary = () => {
|
|
311
|
+
const t = ts[i++];
|
|
312
|
+
if (!t)
|
|
313
|
+
throw new Error("expression ends too soon");
|
|
314
|
+
if (t.t === "num" || t.t === "str")
|
|
315
|
+
return { k: "lit", v: t.v };
|
|
316
|
+
if (t.t === "sym") {
|
|
317
|
+
if (t.v !== "(")
|
|
318
|
+
throw new Error(`expression has ${t.v} where a value should be`);
|
|
319
|
+
const e = or();
|
|
320
|
+
expect(")");
|
|
321
|
+
return e;
|
|
322
|
+
}
|
|
323
|
+
if (t.v === "true" || t.v === "false")
|
|
324
|
+
return { k: "lit", v: t.v === "true" };
|
|
325
|
+
if (t.v === "null")
|
|
326
|
+
return { k: "lit", v: null };
|
|
327
|
+
if (!isSym("("))
|
|
328
|
+
return { k: "field", name: t.v };
|
|
329
|
+
const arity = FUNCTIONS[t.v];
|
|
330
|
+
if (!arity)
|
|
331
|
+
throw new Error(`expression calls ${t.v}(), which isn't one of ${Object.keys(FUNCTIONS).join(", ")}`);
|
|
332
|
+
i++;
|
|
333
|
+
const args = [];
|
|
334
|
+
if (!isSym(")")) {
|
|
335
|
+
args.push(or());
|
|
336
|
+
while (isSym(",")) {
|
|
337
|
+
i++;
|
|
338
|
+
args.push(or());
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
expect(")");
|
|
342
|
+
if (args.length < arity[0] || args.length > arity[1])
|
|
343
|
+
throw new Error(`${t.v}() takes ${arity[0] === arity[1] ? arity[0] : `${arity[0]} to ${arity[1]}`} argument${arity[1] === 1 ? "" : "s"}`);
|
|
344
|
+
return { k: "call", fn: t.v, args };
|
|
345
|
+
};
|
|
346
|
+
const e = or();
|
|
347
|
+
if (i < ts.length)
|
|
348
|
+
throw new Error(`expression has more after it should end: ${String(ts[i].v)}`);
|
|
349
|
+
return e;
|
|
350
|
+
}
|
|
351
|
+
function unify(a, b, what) {
|
|
352
|
+
if (a === "null")
|
|
353
|
+
return b;
|
|
354
|
+
if (b === "null" || a === b)
|
|
355
|
+
return a;
|
|
356
|
+
throw new Error(`${what} mixes ${a} and ${b}`);
|
|
357
|
+
}
|
|
358
|
+
const need = (t, want, what) => { if (t !== "null" && !want.includes(t))
|
|
359
|
+
throw new Error(`${what} needs ${want.join(" or ")}, not ${t}`); };
|
|
360
|
+
function typeOf(e, ref) {
|
|
361
|
+
const ty = (x) => typeOf(x, ref);
|
|
362
|
+
switch (e.k) {
|
|
363
|
+
case "lit": return e.v === null ? "null" : typeof e.v === "number" ? "number" : typeof e.v === "string" ? "string" : "boolean";
|
|
364
|
+
case "field": return ref(e.name);
|
|
365
|
+
case "neg":
|
|
366
|
+
need(ty(e.a), ["number"], "-");
|
|
367
|
+
return "number";
|
|
368
|
+
case "not":
|
|
369
|
+
need(ty(e.a), ["boolean"], "not");
|
|
370
|
+
return "boolean";
|
|
371
|
+
case "op": {
|
|
372
|
+
const a = ty(e.a), b = ty(e.b);
|
|
373
|
+
if (["+", "-", "*", "/"].includes(e.op)) {
|
|
374
|
+
if (e.op === "+" && (a === "string" || b === "string"))
|
|
375
|
+
throw new Error("+ adds numbers; join text with concat()");
|
|
376
|
+
need(a, ["number"], e.op);
|
|
377
|
+
need(b, ["number"], e.op);
|
|
378
|
+
return "number";
|
|
379
|
+
}
|
|
380
|
+
if (e.op === "and" || e.op === "or") {
|
|
381
|
+
need(a, ["boolean"], e.op);
|
|
382
|
+
need(b, ["boolean"], e.op);
|
|
383
|
+
return "boolean";
|
|
384
|
+
}
|
|
385
|
+
const t = unify(a, b, e.op);
|
|
386
|
+
if (e.op !== "==" && e.op !== "!=")
|
|
387
|
+
need(t, ["number", "string", "dateTime"], e.op);
|
|
388
|
+
return "boolean";
|
|
389
|
+
}
|
|
390
|
+
case "call": {
|
|
391
|
+
const ts = e.args.map(ty);
|
|
392
|
+
switch (e.fn) {
|
|
393
|
+
case "concat": return "string";
|
|
394
|
+
case "coalesce": return ts.reduce((a, b) => unify(a, b, "coalesce()"));
|
|
395
|
+
case "if":
|
|
396
|
+
need(ts[0], ["boolean"], "if()'s condition");
|
|
397
|
+
return unify(ts[1], ts[2], "if()");
|
|
398
|
+
case "min":
|
|
399
|
+
case "max": {
|
|
400
|
+
const t = ts.reduce((a, b) => unify(a, b, `${e.fn}()`));
|
|
401
|
+
need(t, ["number", "string", "dateTime"], `${e.fn}()`);
|
|
402
|
+
return t;
|
|
403
|
+
}
|
|
404
|
+
case "round": {
|
|
405
|
+
need(ts[0], ["number"], "round()");
|
|
406
|
+
const d = e.args[1];
|
|
407
|
+
if (d && !(d.k === "lit" && typeof d.v === "number" && Number.isInteger(d.v) && d.v >= 0 && d.v <= 10))
|
|
408
|
+
throw new Error("round()'s digits must be a whole number from 0 to 10");
|
|
409
|
+
return "number";
|
|
410
|
+
}
|
|
411
|
+
case "floor":
|
|
412
|
+
case "ceil":
|
|
413
|
+
case "abs":
|
|
414
|
+
need(ts[0], ["number"], `${e.fn}()`);
|
|
415
|
+
return "number";
|
|
416
|
+
case "lower":
|
|
417
|
+
case "upper":
|
|
418
|
+
need(ts[0], ["string"], `${e.fn}()`);
|
|
419
|
+
return "string";
|
|
420
|
+
case "length":
|
|
421
|
+
need(ts[0], ["string"], "length()");
|
|
422
|
+
return "number";
|
|
423
|
+
}
|
|
424
|
+
throw new Error(`expression calls ${e.fn}(), which isn't a function`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/** Postgres's ordering for the values a computed field compares: numbers, booleans (false first) and text. */
|
|
429
|
+
const order = (a, b) => { const x = typeof a === "boolean" ? Number(a) : a, y = typeof b === "boolean" ? Number(b) : b; return x < y ? -1 : x > y ? 1 : 0; };
|
|
430
|
+
/** An expression's value, as records.ts's SQL works it out: null in, null out; `/` by zero is null; `and`/`or` are three-valued. */
|
|
431
|
+
function evalExpr(e, ref) {
|
|
432
|
+
const x = (a) => evalExpr(a, ref);
|
|
433
|
+
switch (e.k) {
|
|
434
|
+
case "lit": return e.v;
|
|
435
|
+
case "field": return ref(e.name);
|
|
436
|
+
case "neg": {
|
|
437
|
+
const a = x(e.a);
|
|
438
|
+
return a === null ? null : -a;
|
|
439
|
+
}
|
|
440
|
+
case "not": {
|
|
441
|
+
const a = x(e.a);
|
|
442
|
+
return a === null ? null : !a;
|
|
443
|
+
}
|
|
444
|
+
case "op": {
|
|
445
|
+
const a = x(e.a), b = x(e.b);
|
|
446
|
+
if (e.op === "==")
|
|
447
|
+
return a === b;
|
|
448
|
+
if (e.op === "!=")
|
|
449
|
+
return a !== b;
|
|
450
|
+
if (e.op === "and")
|
|
451
|
+
return a === false || b === false ? false : a === null || b === null ? null : true;
|
|
452
|
+
if (e.op === "or")
|
|
453
|
+
return a === true || b === true ? true : a === null || b === null ? null : false;
|
|
454
|
+
if (a === null || b === null)
|
|
455
|
+
return null;
|
|
456
|
+
switch (e.op) {
|
|
457
|
+
case "+": return a + b;
|
|
458
|
+
case "-": return a - b;
|
|
459
|
+
case "*": return a * b;
|
|
460
|
+
case "/": return b === 0 ? null : a / b;
|
|
461
|
+
case "<": return order(a, b) < 0;
|
|
462
|
+
case "<=": return order(a, b) <= 0;
|
|
463
|
+
case ">": return order(a, b) > 0;
|
|
464
|
+
default: return order(a, b) >= 0;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
case "call": {
|
|
468
|
+
const args = e.args.map(x);
|
|
469
|
+
const [a] = args;
|
|
470
|
+
switch (e.fn) {
|
|
471
|
+
case "concat": return args.filter((v) => v !== null).map(String).join("");
|
|
472
|
+
case "coalesce": return args.find((v) => v !== null) ?? null;
|
|
473
|
+
case "if": return a === true ? args[1] : args[2];
|
|
474
|
+
// least() and greatest() skip nulls.
|
|
475
|
+
case "min":
|
|
476
|
+
case "max": {
|
|
477
|
+
const vs = args.filter((v) => v !== null);
|
|
478
|
+
return vs.length ? vs.reduce((p, v) => ((e.fn === "min" ? order(v, p) < 0 : order(v, p) > 0) ? v : p)) : null;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (a === null)
|
|
482
|
+
return null;
|
|
483
|
+
switch (e.fn) {
|
|
484
|
+
// numeric's round: half away from zero, on the value to 15 digits.
|
|
485
|
+
case "round": {
|
|
486
|
+
const f = 10 ** (e.args[1]?.k === "lit" ? Number(e.args[1].v) : 0);
|
|
487
|
+
return (Math.sign(a) * Math.round(Number((Math.abs(a) * f).toPrecision(15)))) / f;
|
|
488
|
+
}
|
|
489
|
+
case "floor": return Math.floor(a);
|
|
490
|
+
case "ceil": return Math.ceil(a);
|
|
491
|
+
case "abs": return Math.abs(a);
|
|
492
|
+
case "lower": return a.toLowerCase();
|
|
493
|
+
case "upper": return a.toUpperCase();
|
|
494
|
+
default: return [...a].length;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
193
499
|
const DEFAULT_EMBEDDING_MODEL = "openai/text-embedding-3-small";
|
|
194
500
|
class Records {
|
|
195
501
|
models;
|
|
@@ -226,6 +532,16 @@ class Records {
|
|
|
226
532
|
out[f.name] = [...v];
|
|
227
533
|
continue;
|
|
228
534
|
}
|
|
535
|
+
// One that can't be worked out reads null, as the API's does.
|
|
536
|
+
if (f.type === "computed") {
|
|
537
|
+
try {
|
|
538
|
+
out[f.name] = wire(this.computed(m, f, CTX).at(row)) ?? null;
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
out[f.name] = null;
|
|
542
|
+
}
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
229
545
|
if (!STORED.has(f.type))
|
|
230
546
|
continue;
|
|
231
547
|
const c = convert(f, Object.prototype.hasOwnProperty.call(row.data, f.key) ? row.data[f.key] : defaultOf(f), "read");
|
|
@@ -243,13 +559,112 @@ class Records {
|
|
|
243
559
|
const m = this.model(model);
|
|
244
560
|
return [...this.table(m).values()].map((r) => this.present(m, r, true));
|
|
245
561
|
}
|
|
562
|
+
// ---- computed fields (records.ts: computedSql) ----
|
|
563
|
+
/** A computed field compiled against the rows: its type, and its value on a row. Throws an Error when it can't be worked out. */
|
|
564
|
+
computed(m, f, ctx) {
|
|
565
|
+
const at = `${m.name}.${f.name}`;
|
|
566
|
+
if (ctx.seen.includes(at))
|
|
567
|
+
throw new Error(`${[...ctx.seen, at].join(" → ")} goes round in a circle`);
|
|
568
|
+
const inner = { ...ctx, seen: [...ctx.seen, at] };
|
|
569
|
+
const c = computationOf(f.def);
|
|
570
|
+
if (c.kind === "expression") {
|
|
571
|
+
const refs = new Map();
|
|
572
|
+
const ref = (n) => { if (!refs.has(n))
|
|
573
|
+
refs.set(n, this.ref(m, n, inner)); return refs.get(n); };
|
|
574
|
+
const type = typeOf(c.expr, (n) => ref(n).type);
|
|
575
|
+
return { type, at: (row) => evalExpr(c.expr, (n) => ref(n).at(row)) };
|
|
576
|
+
}
|
|
577
|
+
const rel = m.byName.get(c.relation);
|
|
578
|
+
if (!rel || rel.type !== "hasMany")
|
|
579
|
+
throw new Error(`${m.name} has no hasMany field named ${c.relation}`);
|
|
580
|
+
const child = this.childrenOf(m, rel);
|
|
581
|
+
if (ctx.depth >= 3)
|
|
582
|
+
throw new Error("aggregates can reach at most 3 models deep");
|
|
583
|
+
const cc = { seen: inner.seen, depth: ctx.depth + 1 };
|
|
584
|
+
const v = c.field ? this.ref(child.model, c.field, cc) : null;
|
|
585
|
+
if (v) {
|
|
586
|
+
const want = c.fn === "sum" || c.fn === "avg" ? ["number"] : ["number", "string", "dateTime"];
|
|
587
|
+
if (!want.includes(v.type))
|
|
588
|
+
throw new Error(`${c.fn} needs ${want.join(" or ")}, and ${c.relation}.${c.field} is ${v.type}`);
|
|
589
|
+
}
|
|
590
|
+
// The filter is checked now, as the API's SQL is, not only when there are records to try it on.
|
|
591
|
+
const probe = { id: "", createdAt: "", updatedAt: "", data: {}, vectors: new Map() };
|
|
592
|
+
try {
|
|
593
|
+
this.matches(child.model, probe, c.filter, cc);
|
|
594
|
+
}
|
|
595
|
+
catch (e) {
|
|
596
|
+
throw new Error(`aggregate.filter: ${e.message}`);
|
|
597
|
+
}
|
|
598
|
+
return {
|
|
599
|
+
type: v ? v.type : "number",
|
|
600
|
+
at: (row) => {
|
|
601
|
+
const kids = [...this.table(child.model).values()].filter((r) => r.data[child.link.key] === row.id && this.matches(child.model, r, c.filter, cc));
|
|
602
|
+
if (c.fn === "count")
|
|
603
|
+
return kids.length;
|
|
604
|
+
const vals = kids.map((r) => v.at(r)).filter((x) => x !== null);
|
|
605
|
+
if (c.fn === "sum")
|
|
606
|
+
return vals.reduce((a, b) => a + b, 0);
|
|
607
|
+
if (!vals.length)
|
|
608
|
+
return null;
|
|
609
|
+
if (c.fn === "avg")
|
|
610
|
+
return vals.reduce((a, b) => a + b, 0) / vals.length;
|
|
611
|
+
return vals.reduce((p, x) => ((c.fn === "min" ? order(x, p) < 0 : order(x, p) > 0) ? x : p));
|
|
612
|
+
},
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
childrenOf(m, rel) {
|
|
616
|
+
const to = isObj(rel.def.children) ? rel.def.children : {};
|
|
617
|
+
const cm = typeof to.model === "string" ? this.models.get(to.model) : undefined;
|
|
618
|
+
if (!cm)
|
|
619
|
+
throw new Error(`${rel.name} points at ${typeof to.model === "string" ? to.model : "no model"}, which doesn't exist`);
|
|
620
|
+
const parentOf = (f) => (isObj(f.def.parent) ? f.def.parent.model : null);
|
|
621
|
+
const links = cm.fields.filter((f) => f.type === "belongsTo" && parentOf(f) === m.name);
|
|
622
|
+
const named = typeof to.belongsToField === "string" && to.belongsToField ? cm.byName.get(to.belongsToField) : undefined;
|
|
623
|
+
const link = named ?? (links.length === 1 ? links[0] : undefined);
|
|
624
|
+
if (!link || link.type !== "belongsTo") {
|
|
625
|
+
throw new Error(typeof to.belongsToField === "string" && to.belongsToField ? `${cm.name} has no belongsTo field named ${to.belongsToField}` : `${rel.name} needs children.belongsToField: the field on ${cm.name} that points back`);
|
|
626
|
+
}
|
|
627
|
+
return { model: cm, link };
|
|
628
|
+
}
|
|
629
|
+
/** A field a computed field reads, as its type and value: stored fields as they read (default included), system fields, computed fields. */
|
|
630
|
+
ref(m, name, ctx) {
|
|
631
|
+
if (name === "id" || name === "createdAt" || name === "updatedAt")
|
|
632
|
+
return { type: name === "id" ? "string" : "dateTime", at: (row) => row[name] };
|
|
633
|
+
const f = m.byName.get(name);
|
|
634
|
+
if (!f)
|
|
635
|
+
throw new Error(`${m.name} has no field named ${name}`);
|
|
636
|
+
if (f.type === "computed")
|
|
637
|
+
return this.computed(m, f, ctx);
|
|
638
|
+
const type = f.type === "number" ? "number" : f.type === "boolean" ? "boolean" : f.type === "dateTime" ? "dateTime"
|
|
639
|
+
: ["string", "richText", "email", "url", "color", "belongsTo"].includes(f.type) || (f.type === "enum" && f.def.allowMultiple !== true) ? "string" : null;
|
|
640
|
+
if (!type)
|
|
641
|
+
throw new Error(`${name} is a ${f.type} field, which a computed field can't read`);
|
|
642
|
+
const def = defaultOf(f);
|
|
643
|
+
return {
|
|
644
|
+
type,
|
|
645
|
+
at: (row) => {
|
|
646
|
+
if (!Object.prototype.hasOwnProperty.call(row.data, f.key))
|
|
647
|
+
return def;
|
|
648
|
+
const v = row.data[f.key];
|
|
649
|
+
return type === "number" ? (typeof v === "number" ? v : null) : type === "boolean" ? (typeof v === "boolean" ? v : null) : asText(v);
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
}
|
|
246
653
|
// ---- filters and sorting ----
|
|
247
|
-
target(m, name) {
|
|
654
|
+
target(m, name, ctx = CTX) {
|
|
248
655
|
if (name === "id" || name === "createdAt" || name === "updatedAt")
|
|
249
656
|
return { column: name };
|
|
250
657
|
const f = m.byName.get(name);
|
|
251
658
|
if (!f)
|
|
252
659
|
throw invalid([{ field: name, message: `${m.name} has no field named ${name}` }]);
|
|
660
|
+
if (f.type === "computed") {
|
|
661
|
+
try {
|
|
662
|
+
return { computed: this.computed(m, f, ctx) };
|
|
663
|
+
}
|
|
664
|
+
catch (e) {
|
|
665
|
+
throw invalid([{ field: name, message: e.message }]);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
253
668
|
if (!STORED.has(f.type) || f.type === "password" || f.type === "encryptedString" || f.type === "file")
|
|
254
669
|
throw invalid([{ field: name, message: `${f.type} fields can't be filtered or sorted on` }]);
|
|
255
670
|
return { f };
|
|
@@ -258,6 +673,8 @@ class Records {
|
|
|
258
673
|
comparable(row, t) {
|
|
259
674
|
if ("column" in t)
|
|
260
675
|
return row[t.column];
|
|
676
|
+
if ("computed" in t)
|
|
677
|
+
return t.computed.at(row);
|
|
261
678
|
const v = row.data[t.f.key];
|
|
262
679
|
if (t.f.type === "number")
|
|
263
680
|
return typeof v === "number" ? v : null;
|
|
@@ -265,8 +682,8 @@ class Records {
|
|
|
265
682
|
return typeof v === "boolean" ? v : null;
|
|
266
683
|
return asText(v);
|
|
267
684
|
}
|
|
268
|
-
test(m, row, name, op, arg) {
|
|
269
|
-
const t = this.target(m, name);
|
|
685
|
+
test(m, row, name, op, arg, ctx) {
|
|
686
|
+
const t = this.target(m, name, ctx);
|
|
270
687
|
const wrong = (message) => invalid([{ field: name, message }]);
|
|
271
688
|
const cmp = (a, b) => {
|
|
272
689
|
if (a === null || a === undefined || b === null || b === undefined)
|
|
@@ -296,9 +713,62 @@ class Records {
|
|
|
296
713
|
}
|
|
297
714
|
case "isSet": return arg !== false;
|
|
298
715
|
case "startsWith": return col.startsWith(String(v(arg)));
|
|
716
|
+
case "contains":
|
|
717
|
+
if (t.column !== "id")
|
|
718
|
+
throw wrong("contains needs a text field");
|
|
719
|
+
return col.toLowerCase().includes(String(v(arg)).toLowerCase());
|
|
299
720
|
default: return compare(col, v(arg));
|
|
300
721
|
}
|
|
301
722
|
}
|
|
723
|
+
if ("computed" in t) {
|
|
724
|
+
const { type } = t.computed;
|
|
725
|
+
const got = t.computed.at(row);
|
|
726
|
+
const val = (x) => {
|
|
727
|
+
if (x === null)
|
|
728
|
+
return null;
|
|
729
|
+
if (type === "number") {
|
|
730
|
+
if (typeof x !== "number" || !Number.isFinite(x))
|
|
731
|
+
throw wrong("must be a number");
|
|
732
|
+
return x;
|
|
733
|
+
}
|
|
734
|
+
if (type === "boolean") {
|
|
735
|
+
if (typeof x !== "boolean")
|
|
736
|
+
throw wrong("must be true or false");
|
|
737
|
+
return x;
|
|
738
|
+
}
|
|
739
|
+
if (typeof x !== "string")
|
|
740
|
+
throw wrong("must be text");
|
|
741
|
+
if (type !== "dateTime")
|
|
742
|
+
return x;
|
|
743
|
+
const d = new Date(x);
|
|
744
|
+
if (Number.isNaN(d.getTime()))
|
|
745
|
+
throw wrong("must be an ISO 8601 date and time");
|
|
746
|
+
return d.toISOString();
|
|
747
|
+
};
|
|
748
|
+
switch (op) {
|
|
749
|
+
case "equals": return got === val(arg);
|
|
750
|
+
case "notEquals": return got !== val(arg);
|
|
751
|
+
case "in":
|
|
752
|
+
case "notIn": {
|
|
753
|
+
if (!Array.isArray(arg))
|
|
754
|
+
throw wrong(`${op} needs a list`);
|
|
755
|
+
const hit = arg.map(val).includes(got);
|
|
756
|
+
return op === "in" ? hit : !hit;
|
|
757
|
+
}
|
|
758
|
+
case "isSet": return arg === false ? got === null : got !== null;
|
|
759
|
+
case "startsWith":
|
|
760
|
+
case "contains": {
|
|
761
|
+
if (type !== "string")
|
|
762
|
+
throw wrong(`${op} needs a text field`);
|
|
763
|
+
if (typeof arg !== "string")
|
|
764
|
+
throw wrong(`${op} needs text`);
|
|
765
|
+
if (got === null)
|
|
766
|
+
return false;
|
|
767
|
+
return op === "startsWith" ? got.startsWith(arg) : got.toLowerCase().includes(arg.toLowerCase());
|
|
768
|
+
}
|
|
769
|
+
default: return compare(got, val(arg));
|
|
770
|
+
}
|
|
771
|
+
}
|
|
302
772
|
const f = t.f;
|
|
303
773
|
const def = defaultOf(f);
|
|
304
774
|
const has = Object.prototype.hasOwnProperty.call(row.data, f.key);
|
|
@@ -330,13 +800,20 @@ class Records {
|
|
|
330
800
|
throw wrong("startsWith needs text");
|
|
331
801
|
return asText(row.data[f.key])?.startsWith(arg) ?? false;
|
|
332
802
|
}
|
|
803
|
+
case "contains": {
|
|
804
|
+
if (!TEXT_SEARCHED.has(f.type))
|
|
805
|
+
throw wrong("contains needs a text field");
|
|
806
|
+
if (typeof arg !== "string")
|
|
807
|
+
throw wrong("contains needs text");
|
|
808
|
+
return asText(row.data[f.key])?.toLowerCase().includes(arg.toLowerCase()) ?? false;
|
|
809
|
+
}
|
|
333
810
|
default: {
|
|
334
811
|
const value = val(arg);
|
|
335
812
|
return compare(this.comparable(row, t), f.type === "number" || f.type === "boolean" ? value : asText(value));
|
|
336
813
|
}
|
|
337
814
|
}
|
|
338
815
|
}
|
|
339
|
-
matches(m, row, filter) {
|
|
816
|
+
matches(m, row, filter, ctx = CTX) {
|
|
340
817
|
if (filter === undefined || filter === null)
|
|
341
818
|
return true;
|
|
342
819
|
if (!isObj(filter))
|
|
@@ -346,7 +823,7 @@ class Records {
|
|
|
346
823
|
const list = (Array.isArray(v) ? v : [v]).filter((x) => x !== undefined && x !== null);
|
|
347
824
|
if (!list.length)
|
|
348
825
|
continue;
|
|
349
|
-
const each = list.map((x) => this.matches(m, row, x));
|
|
826
|
+
const each = list.map((x) => this.matches(m, row, x, ctx));
|
|
350
827
|
if (k === "AND" && !each.every(Boolean))
|
|
351
828
|
return false;
|
|
352
829
|
if (k === "OR" && !each.some(Boolean))
|
|
@@ -360,7 +837,7 @@ class Records {
|
|
|
360
837
|
for (const [op, arg] of Object.entries(v)) {
|
|
361
838
|
if (!OPS.includes(op))
|
|
362
839
|
throw invalid([{ field: k, message: `${op} isn't a filter; use ${OPS.join(", ")}` }]);
|
|
363
|
-
if (!this.test(m, row, k, op, arg))
|
|
840
|
+
if (!this.test(m, row, k, op, arg, ctx))
|
|
364
841
|
return false;
|
|
365
842
|
}
|
|
366
843
|
}
|
|
@@ -392,13 +869,33 @@ class Records {
|
|
|
392
869
|
return 0;
|
|
393
870
|
});
|
|
394
871
|
}
|
|
872
|
+
/** As the API's text search: every word in one of the text fields, and the rows holding the whole phrase. */
|
|
873
|
+
textSearch(m, rows, text) {
|
|
874
|
+
const words = searchWords(text);
|
|
875
|
+
if (!words.length)
|
|
876
|
+
return null;
|
|
877
|
+
const fields = m.fields.filter((f) => TEXT_SEARCHED.has(f.type));
|
|
878
|
+
if (!fields.length)
|
|
879
|
+
throw invalid([{ field: "search", message: `${m.name} has no text fields to search` }]);
|
|
880
|
+
const docOf = (r) => fields.map((f) => asText(r.data[f.key])).filter((x) => x !== null).join(" ").toLowerCase();
|
|
881
|
+
const found = rows.filter((r) => { const doc = docOf(r); return words.every((w) => doc.includes(w)); });
|
|
882
|
+
const whole = text.trim().toLowerCase();
|
|
883
|
+
return { rows: found, phrase: new Set(found.filter((r) => docOf(r).includes(whole)).map((r) => r.id)) };
|
|
884
|
+
}
|
|
395
885
|
async findMany(model, o = {}) {
|
|
396
886
|
const m = this.model(model);
|
|
397
887
|
const first = Math.min(Math.max(1, Math.floor(o.first ?? 50)), MAX_PAGE);
|
|
398
888
|
const offset = offsetOf(o.after);
|
|
399
889
|
let rows = [...this.table(m).values()].filter((r) => this.matches(m, r, o.filter));
|
|
400
890
|
let distances = null;
|
|
401
|
-
|
|
891
|
+
const text = typeof o.search === "string" ? this.textSearch(m, rows, o.search) : null;
|
|
892
|
+
if (text) {
|
|
893
|
+
rows = this.sorted(m, text.rows, o.sort);
|
|
894
|
+
// Unsorted, the rows holding the whole phrase come first (a stable sort keeps the default order within each).
|
|
895
|
+
if (!o.sort)
|
|
896
|
+
rows.sort((a, b) => Number(text.phrase.has(b.id)) - Number(text.phrase.has(a.id)));
|
|
897
|
+
}
|
|
898
|
+
else if (o.search && typeof o.search === "object") {
|
|
402
899
|
const f = m.byName.get(o.search.field);
|
|
403
900
|
if (!f || f.type !== "vector")
|
|
404
901
|
throw invalid([{ field: o.search.field, message: `${m.name} has no vector field named ${o.search.field}` }]);
|
|
@@ -597,7 +1094,9 @@ class Records {
|
|
|
597
1094
|
return typeof stored === "string" && stored === password;
|
|
598
1095
|
}
|
|
599
1096
|
}
|
|
600
|
-
const OPS = ["equals", "notEquals", "in", "notIn", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "isSet", "startsWith"];
|
|
1097
|
+
const OPS = ["equals", "notEquals", "in", "notIn", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "isSet", "startsWith", "contains"];
|
|
1098
|
+
const TEXT_SEARCHED = new Set(["string", "richText", "email", "url", "enum"]);
|
|
1099
|
+
const searchWords = (text) => text.toLowerCase().match(/[\p{L}\p{N}]+(?:['’.@-][\p{L}\p{N}]+)*/gu) ?? [];
|
|
601
1100
|
const embeddingModelOf = (f) => (typeof f.def.embeddingModel === "string" && f.def.embeddingModel ? f.def.embeddingModel : DEFAULT_EMBEDDING_MODEL);
|
|
602
1101
|
function sourceText(m, f, data) {
|
|
603
1102
|
const names = Array.isArray(f.def.source) ? f.def.source.map(String) : [];
|
|
@@ -736,6 +1235,454 @@ function syncItem(model, o, shop) {
|
|
|
736
1235
|
const fields = Array.isArray(o.fields) ? Object.fromEntries(o.fields.map((x) => [x.key, x.value])) : isObj(o.fields) ? o.fields : {};
|
|
737
1236
|
return { model, shopifyId: id, updatedAt, values: { sh_id: id, sh_shop: shop, sh_type: str(o.type), sh_handle: str(o.handle), sh_display_name: str(o.display_name), sh_fields: fields, sh_updated_at: updatedAt } };
|
|
738
1237
|
}
|
|
1238
|
+
const FULL_ACCESS = { shop: null, role: null, session: { userId: null, customerId: null, customerRecordId: null } };
|
|
1239
|
+
const isScoped = (s) => !!s.shop || !!s.role;
|
|
1240
|
+
const CUSTOMER_ROLE = "storefront-customers";
|
|
1241
|
+
const PERMISSIONS_PATH = "accessControl/permissions.ib.ts";
|
|
1242
|
+
const ROLE = /^[a-z][a-z0-9-]{0,63}$/;
|
|
1243
|
+
const sessionOf = (s = {}) => ({ userId: s.userId ?? null, customerId: s.customerId ?? null, customerRecordId: s.customerRecordId ?? null });
|
|
1244
|
+
const denied = (message) => new ApiError(message, 403, "policy_denied");
|
|
1245
|
+
/** The field holding a record's store, or null when the model isn't store-owned. */
|
|
1246
|
+
function storeField(m) {
|
|
1247
|
+
if (m.name === "shopifyShop" && m.byName.has("myshopifyDomain"))
|
|
1248
|
+
return "myshopifyDomain";
|
|
1249
|
+
const synced = m.fields.find((f) => f.key === "sh_shop");
|
|
1250
|
+
if (synced)
|
|
1251
|
+
return synced.name;
|
|
1252
|
+
const own = m.byName.get("shop");
|
|
1253
|
+
return own && ["string", "email", "enum"].includes(own.type) ? "shop" : null;
|
|
1254
|
+
}
|
|
1255
|
+
const relatedModel = (f) => {
|
|
1256
|
+
for (const k of ["parent", "child", "children", "through"]) {
|
|
1257
|
+
const v = f.def[k];
|
|
1258
|
+
if (isObj(v) && typeof v.model === "string")
|
|
1259
|
+
return v.model;
|
|
1260
|
+
}
|
|
1261
|
+
return null;
|
|
1262
|
+
};
|
|
1263
|
+
/** How a model's records belong to a storefront customer: by their record id or their Shopify gid; null when they don't. */
|
|
1264
|
+
function customerField(m) {
|
|
1265
|
+
if (m.name === "shopifyCustomer")
|
|
1266
|
+
return { field: "id", by: "record" };
|
|
1267
|
+
const link = m.fields.find((f) => f.type === "belongsTo" && relatedModel(f) === "shopifyCustomer");
|
|
1268
|
+
if (link)
|
|
1269
|
+
return { field: link.name, by: "record" };
|
|
1270
|
+
const synced = m.fields.find((f) => f.key === "sh_customer_id");
|
|
1271
|
+
return synced ? { field: synced.name, by: "gid" } : null;
|
|
1272
|
+
}
|
|
1273
|
+
const customerGid = (id) => (id.startsWith("gid://") ? id : `gid://shopify/Customer/${id}`);
|
|
1274
|
+
/** A condition no record meets. */
|
|
1275
|
+
const NOTHING = { id: { in: [] } };
|
|
1276
|
+
function customerFilter(m, scope) {
|
|
1277
|
+
if (scope.role !== CUSTOMER_ROLE)
|
|
1278
|
+
return undefined;
|
|
1279
|
+
const f = customerField(m);
|
|
1280
|
+
if (!f)
|
|
1281
|
+
return undefined;
|
|
1282
|
+
const value = f.by === "record" ? scope.session.customerRecordId : scope.session.customerId ? customerGid(scope.session.customerId) : null;
|
|
1283
|
+
return value ? { [f.field]: { equals: value } } : NOTHING;
|
|
1284
|
+
}
|
|
1285
|
+
function grantOf(v, where, problems) {
|
|
1286
|
+
if (v === undefined)
|
|
1287
|
+
return undefined;
|
|
1288
|
+
if (typeof v === "boolean")
|
|
1289
|
+
return v;
|
|
1290
|
+
if (isObj(v) && isObj(v.filter))
|
|
1291
|
+
return { filter: v.filter };
|
|
1292
|
+
if (isObj(v) && typeof v.filter === "string")
|
|
1293
|
+
problems.push(`${where}: filter files aren't read; write the filter inline, e.g. { filter: { id: { equals: { $session: "userId" } } } }`);
|
|
1294
|
+
else
|
|
1295
|
+
problems.push(`${where}: must be true, false or { filter: { … } }`);
|
|
1296
|
+
return false;
|
|
1297
|
+
}
|
|
1298
|
+
/** The roles permissions.ib.ts grants. What doesn't check out grants nothing and is named in `problem`, as the API does. */
|
|
1299
|
+
function readPermissions(p) {
|
|
1300
|
+
const roles = new Map();
|
|
1301
|
+
if (p === undefined)
|
|
1302
|
+
return { roles, problem: null };
|
|
1303
|
+
if (!isObj(p) || !isObj(p.roles))
|
|
1304
|
+
return { roles, problem: `${PERMISSIONS_PATH} needs \`export const permissions = { roles: { … } }\`.` };
|
|
1305
|
+
const problems = [];
|
|
1306
|
+
for (const [name, r] of Object.entries(p.roles)) {
|
|
1307
|
+
if (!isObj(r)) {
|
|
1308
|
+
problems.push(`roles.${name}: must be an object`);
|
|
1309
|
+
continue;
|
|
1310
|
+
}
|
|
1311
|
+
const d = isObj(r.default) ? r.default : {};
|
|
1312
|
+
const models = {};
|
|
1313
|
+
for (const [m, g] of Object.entries(isObj(r.models) ? r.models : {})) {
|
|
1314
|
+
if (!isObj(g)) {
|
|
1315
|
+
problems.push(`roles.${name}.models.${m}: must be an object`);
|
|
1316
|
+
continue;
|
|
1317
|
+
}
|
|
1318
|
+
const actions = {};
|
|
1319
|
+
for (const [a, ag] of Object.entries(isObj(g.actions) ? g.actions : {}))
|
|
1320
|
+
actions[a] = grantOf(ag, `roles.${name}.models.${m}.actions.${a}`, problems) ?? false;
|
|
1321
|
+
models[m] = { read: grantOf(g.read, `roles.${name}.models.${m}.read`, problems), actions };
|
|
1322
|
+
}
|
|
1323
|
+
const globalActions = Object.fromEntries(Object.entries(isObj(r.globalActions) ? r.globalActions : {}).map(([a, v]) => [a, v === true]));
|
|
1324
|
+
roles.set(name, { default: { read: grantOf(d.read, `roles.${name}.default.read`, problems), action: grantOf(d.action, `roles.${name}.default.action`, problems) }, models, globalActions });
|
|
1325
|
+
}
|
|
1326
|
+
return { roles, problem: problems.length ? `${PERMISSIONS_PATH}: ${problems.join("; ")}` : null };
|
|
1327
|
+
}
|
|
1328
|
+
/** A value with `{ $session: name }` filled in; `missing()` is told when the caller has no such value. */
|
|
1329
|
+
function fillValue(v, scope, missing) {
|
|
1330
|
+
if (Array.isArray(v))
|
|
1331
|
+
return v.map((x) => fillValue(x, scope, missing));
|
|
1332
|
+
if (!isObj(v))
|
|
1333
|
+
return v;
|
|
1334
|
+
const keys = Object.keys(v);
|
|
1335
|
+
if (keys.length === 1 && keys[0] === "$session") {
|
|
1336
|
+
const name = v.$session;
|
|
1337
|
+
const value = name === "shop" ? scope.shop : name === "userId" || name === "customerId" || name === "customerRecordId" ? scope.session[name] : null;
|
|
1338
|
+
if (value === null || value === undefined)
|
|
1339
|
+
missing();
|
|
1340
|
+
return value ?? null;
|
|
1341
|
+
}
|
|
1342
|
+
return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, fillValue(x, scope, missing)]));
|
|
1343
|
+
}
|
|
1344
|
+
/** A grant's filter for the caller: a condition on a value the caller doesn't have matches nothing, not everything. */
|
|
1345
|
+
function fill(f, scope) {
|
|
1346
|
+
const parts = [];
|
|
1347
|
+
for (const [k, v] of Object.entries(f)) {
|
|
1348
|
+
if (k === "AND" || k === "OR" || k === "NOT") {
|
|
1349
|
+
parts.push({ [k]: (Array.isArray(v) ? v : [v]).map((x) => fill(x, scope)) });
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
let gone = false;
|
|
1353
|
+
const filled = fillValue(v, scope, () => { gone = true; });
|
|
1354
|
+
parts.push(gone ? NOTHING : { [k]: filled });
|
|
1355
|
+
}
|
|
1356
|
+
return parts.length === 1 ? parts[0] : { AND: parts };
|
|
1357
|
+
}
|
|
1358
|
+
/** The role's grant, as a filter to apply ({} for everything), or a refusal. */
|
|
1359
|
+
function roleFilter(perms, scope, model, op, what) {
|
|
1360
|
+
if (!scope.role)
|
|
1361
|
+
return {};
|
|
1362
|
+
const r = perms.roles.get(scope.role);
|
|
1363
|
+
let g;
|
|
1364
|
+
if (r) {
|
|
1365
|
+
if (model === null)
|
|
1366
|
+
g = "action" in op && r.globalActions[op.action] === true;
|
|
1367
|
+
else {
|
|
1368
|
+
const m = r.models[model];
|
|
1369
|
+
g = "read" in op ? (m?.read ?? r.default.read) : (m?.actions[op.action] ?? r.default.action);
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
if (!g)
|
|
1373
|
+
throw denied(`The ${scope.role} role can't ${what}.${perms.problem ? ` (${perms.problem})` : ` Grant it in ${PERMISSIONS_PATH}.`}`);
|
|
1374
|
+
return g === true ? {} : fill(g.filter, scope);
|
|
1375
|
+
}
|
|
1376
|
+
const combine = (...fs) => {
|
|
1377
|
+
const list = fs.filter((f) => !!f && Object.keys(f).length > 0);
|
|
1378
|
+
return list.length === 0 ? undefined : list.length === 1 ? list[0] : { AND: list };
|
|
1379
|
+
};
|
|
1380
|
+
const storeFilter = (m, scope) => {
|
|
1381
|
+
const f = scope.shop ? storeField(m) : null;
|
|
1382
|
+
return f ? { [f]: { equals: scope.shop } } : undefined;
|
|
1383
|
+
};
|
|
1384
|
+
/** What a read may see of a model: the caller's filter narrowed to the store and the role's grant. */
|
|
1385
|
+
function readFilter(perms, scope, m, filter) {
|
|
1386
|
+
if (!isScoped(scope))
|
|
1387
|
+
return filter;
|
|
1388
|
+
return combine(filter, storeFilter(m, scope), customerFilter(m, scope), roleFilter(perms, scope, m.name, { read: true }, `read ${m.name}`));
|
|
1389
|
+
}
|
|
1390
|
+
/** The records an action may run on: the store's, through the role's grant. */
|
|
1391
|
+
function actionFilter(perms, scope, m, action) {
|
|
1392
|
+
if (!isScoped(scope))
|
|
1393
|
+
return undefined;
|
|
1394
|
+
return combine(m ? storeFilter(m, scope) : undefined, m ? customerFilter(m, scope) : undefined, roleFilter(perms, scope, m?.name ?? null, { action }, m ? `run ${m.name}.${action}` : `run ${action}`));
|
|
1395
|
+
}
|
|
1396
|
+
/** A new record of a store-owned model is the store's, a storefront customer's is theirs, and no write hands one to another. */
|
|
1397
|
+
function scopedValues(scope, m, values) {
|
|
1398
|
+
let out = values;
|
|
1399
|
+
const f = scope.shop ? storeField(m) : null;
|
|
1400
|
+
if (f && scope.shop) {
|
|
1401
|
+
if (f in values && values[f] !== scope.shop)
|
|
1402
|
+
throw denied(`This call is for ${scope.shop}; it can't write ${m.name} records of another store.`);
|
|
1403
|
+
out = { ...out, [f]: scope.shop };
|
|
1404
|
+
}
|
|
1405
|
+
const c = scope.role === CUSTOMER_ROLE ? customerField(m) : null;
|
|
1406
|
+
if (c && c.field !== "id") {
|
|
1407
|
+
const mine = c.by === "record" ? scope.session.customerRecordId : scope.session.customerId ? customerGid(scope.session.customerId) : null;
|
|
1408
|
+
if (!mine)
|
|
1409
|
+
throw denied(`This customer has no ${c.by === "record" ? "shopifyCustomer record" : "Shopify id"}, so they can't write ${m.name} records.`);
|
|
1410
|
+
if (c.field in values && values[c.field] !== mine)
|
|
1411
|
+
throw denied(`A customer can't write ${m.name} records of another customer.`);
|
|
1412
|
+
out = { ...out, [c.field]: mine };
|
|
1413
|
+
}
|
|
1414
|
+
return out;
|
|
1415
|
+
}
|
|
1416
|
+
/** An update's values: the store field may stay as it is, never become another store, and a customer's record stays theirs. */
|
|
1417
|
+
function scopedUpdate(scope, m, values) {
|
|
1418
|
+
const f = scope.shop ? storeField(m) : null;
|
|
1419
|
+
const c = scope.role === CUSTOMER_ROLE ? customerField(m)?.field : null;
|
|
1420
|
+
if (!(f && f in values) && !(c && c !== "id" && c in values))
|
|
1421
|
+
return values;
|
|
1422
|
+
return scopedValues(scope, m, values);
|
|
1423
|
+
}
|
|
1424
|
+
/** The store an action runs for: the call's own, which a different `shop` can't replace. */
|
|
1425
|
+
function scopedShop(scope, shop) {
|
|
1426
|
+
if (!scope.shop)
|
|
1427
|
+
return shop;
|
|
1428
|
+
if (shop && shop !== scope.shop)
|
|
1429
|
+
throw denied(`This call is for ${scope.shop}; it can't act for ${shop}.`);
|
|
1430
|
+
return scope.shop;
|
|
1431
|
+
}
|
|
1432
|
+
// ---- schedules (settings.ts's cron) ----
|
|
1433
|
+
const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
|
|
1434
|
+
const DAYS = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
|
|
1435
|
+
const CRON_FIELDS = [
|
|
1436
|
+
{ name: "minute", min: 0, max: 59 }, { name: "hour", min: 0, max: 23 }, { name: "day of month", min: 1, max: 31 },
|
|
1437
|
+
{ name: "month", min: 1, max: 12, names: MONTHS }, { name: "day of week", min: 0, max: 7, names: DAYS },
|
|
1438
|
+
];
|
|
1439
|
+
function cronField(text, f) {
|
|
1440
|
+
const out = new Set();
|
|
1441
|
+
const num = (s) => {
|
|
1442
|
+
const i = f.names?.indexOf(s.toLowerCase()) ?? -1;
|
|
1443
|
+
const n = i >= 0 ? i + (f.name === "month" ? 1 : 0) : /^\d+$/.test(s) ? Number(s) : NaN;
|
|
1444
|
+
if (!Number.isInteger(n) || n < f.min || n > f.max)
|
|
1445
|
+
throw new Error(`${s} isn't a ${f.name} (${f.min}–${f.max})`);
|
|
1446
|
+
return n;
|
|
1447
|
+
};
|
|
1448
|
+
for (const part of text.split(",")) {
|
|
1449
|
+
const [range, stepText] = part.split("/");
|
|
1450
|
+
const step = stepText === undefined ? 1 : Number(stepText);
|
|
1451
|
+
if (!Number.isInteger(step) || step < 1)
|
|
1452
|
+
throw new Error(`${part} has a bad step`);
|
|
1453
|
+
let lo, hi;
|
|
1454
|
+
if (range === "*") {
|
|
1455
|
+
lo = f.min;
|
|
1456
|
+
hi = f.max;
|
|
1457
|
+
}
|
|
1458
|
+
else if (range.includes("-")) {
|
|
1459
|
+
const [a, b] = range.split("-");
|
|
1460
|
+
lo = num(a ?? "");
|
|
1461
|
+
hi = num(b ?? "");
|
|
1462
|
+
if (lo > hi)
|
|
1463
|
+
throw new Error(`${range} runs backwards`);
|
|
1464
|
+
}
|
|
1465
|
+
else {
|
|
1466
|
+
lo = num(range);
|
|
1467
|
+
hi = stepText === undefined ? lo : f.max;
|
|
1468
|
+
}
|
|
1469
|
+
for (let v = lo; v <= hi; v += step)
|
|
1470
|
+
out.add(f.name === "day of week" && v === 7 ? 0 : v);
|
|
1471
|
+
}
|
|
1472
|
+
return out;
|
|
1473
|
+
}
|
|
1474
|
+
function parseCron(cron) {
|
|
1475
|
+
const parts = cron.trim().split(/\s+/);
|
|
1476
|
+
try {
|
|
1477
|
+
if (parts.length !== 5)
|
|
1478
|
+
throw new Error("A cron has five fields: minute hour day-of-month month day-of-week, e.g. 0 3 * * *");
|
|
1479
|
+
const [minute, hour, dom, month, dow] = parts.map((p, i) => cronField(p, CRON_FIELDS[i]));
|
|
1480
|
+
return { minute, hour, dom, month, dow, domAny: parts[2] === "*", dowAny: parts[4] === "*" };
|
|
1481
|
+
}
|
|
1482
|
+
catch (e) {
|
|
1483
|
+
throw new Error(`cron "${cron}": ${e.message}`);
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
const cronFormats = new Map();
|
|
1487
|
+
function localParts(t, tz) {
|
|
1488
|
+
let f = cronFormats.get(tz);
|
|
1489
|
+
if (!f) {
|
|
1490
|
+
try {
|
|
1491
|
+
f = new Intl.DateTimeFormat("en-US", { timeZone: tz, hourCycle: "h23", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", weekday: "short" });
|
|
1492
|
+
}
|
|
1493
|
+
catch {
|
|
1494
|
+
throw new Error(`${tz} isn't a timezone. Use an IANA name such as America/New_York.`);
|
|
1495
|
+
}
|
|
1496
|
+
cronFormats.set(tz, f);
|
|
1497
|
+
}
|
|
1498
|
+
const p = Object.fromEntries(f.formatToParts(t).map((x) => [x.type, x.value]));
|
|
1499
|
+
return { month: Number(p.month), day: Number(p.day), hour: Number(p.hour) % 24, minute: Number(p.minute), dow: DAYS.indexOf(String(p.weekday).toLowerCase().slice(0, 3)) };
|
|
1500
|
+
}
|
|
1501
|
+
const MINUTE = 60_000;
|
|
1502
|
+
/** The first time after `after` the cron matches in `tz`, or null within a year and a day (Vixie cron's day rule). */
|
|
1503
|
+
function nextRun(c, tz, after) {
|
|
1504
|
+
let t = Math.floor(after / MINUTE) * MINUTE + MINUTE;
|
|
1505
|
+
const until = after + 367 * 24 * 60 * MINUTE;
|
|
1506
|
+
while (t <= until) {
|
|
1507
|
+
const p = localParts(t, tz);
|
|
1508
|
+
const dayOk = c.domAny && c.dowAny ? true : c.domAny ? c.dow.has(p.dow) : c.dowAny ? c.dom.has(p.day) : c.dom.has(p.day) || c.dow.has(p.dow);
|
|
1509
|
+
if (!c.month.has(p.month) || !dayOk) {
|
|
1510
|
+
t += ((23 - p.hour) * 60 + (60 - p.minute)) * MINUTE;
|
|
1511
|
+
continue;
|
|
1512
|
+
}
|
|
1513
|
+
if (!c.hour.has(p.hour)) {
|
|
1514
|
+
t += (60 - p.minute) * MINUTE;
|
|
1515
|
+
continue;
|
|
1516
|
+
}
|
|
1517
|
+
if (!c.minute.has(p.minute)) {
|
|
1518
|
+
t += MINUTE;
|
|
1519
|
+
continue;
|
|
1520
|
+
}
|
|
1521
|
+
return t;
|
|
1522
|
+
}
|
|
1523
|
+
return null;
|
|
1524
|
+
}
|
|
1525
|
+
const msOf = (w, what) => {
|
|
1526
|
+
const t = w instanceof Date ? w.getTime() : typeof w === "number" ? w : Date.parse(w);
|
|
1527
|
+
if (!Number.isFinite(t))
|
|
1528
|
+
throw new Error(`${what} isn't a date: ${String(w)}`);
|
|
1529
|
+
return t;
|
|
1530
|
+
};
|
|
1531
|
+
/** A cron's times in (from, to], oldest first. */
|
|
1532
|
+
function runsBetween(cron, tz, from, to, limit) {
|
|
1533
|
+
const c = parseCron(cron);
|
|
1534
|
+
const out = [];
|
|
1535
|
+
for (let t = nextRun(c, tz, from); t !== null && t <= to; t = nextRun(c, tz, t)) {
|
|
1536
|
+
if (out.length === limit)
|
|
1537
|
+
throw new Error(`${cron} runs more than ${limit} times in that window; ask for a shorter one.`);
|
|
1538
|
+
out.push(t);
|
|
1539
|
+
}
|
|
1540
|
+
return out;
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* When a schedule (settings.ib.ts's entry, or a cron) runs next after `after` (now unless named), as ISO times, as
|
|
1544
|
+
* Inference Bureau works them out: in its timezone (UTC unless named), minutes a DST change skips not occurring.
|
|
1545
|
+
*/
|
|
1546
|
+
export function nextRuns(schedule, o = {}) {
|
|
1547
|
+
const cron = typeof schedule === "string" ? schedule : schedule.cron;
|
|
1548
|
+
const tz = o.timezone ?? (typeof schedule === "string" ? undefined : schedule.timezone) ?? "UTC";
|
|
1549
|
+
const c = parseCron(cron);
|
|
1550
|
+
const out = [];
|
|
1551
|
+
for (let t = nextRun(c, tz, o.after === undefined ? Date.now() : msOf(o.after, "after")); t !== null && out.length < (o.count ?? 3); t = nextRun(c, tz, t))
|
|
1552
|
+
out.push(new Date(t).toISOString());
|
|
1553
|
+
return out;
|
|
1554
|
+
}
|
|
1555
|
+
function parseWebhookFilter(text) {
|
|
1556
|
+
const tokens = [];
|
|
1557
|
+
for (let i = 0; i < text.length;) {
|
|
1558
|
+
const ch = text[i];
|
|
1559
|
+
if (/\s/.test(ch)) {
|
|
1560
|
+
i++;
|
|
1561
|
+
continue;
|
|
1562
|
+
}
|
|
1563
|
+
if (ch === "(" || ch === ")") {
|
|
1564
|
+
tokens.push(ch);
|
|
1565
|
+
i++;
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
let word = "";
|
|
1569
|
+
while (i < text.length && !/[\s()]/.test(text[i])) {
|
|
1570
|
+
if (text[i] === "\"") {
|
|
1571
|
+
const end = text.indexOf("\"", i + 1);
|
|
1572
|
+
if (end < 0)
|
|
1573
|
+
throw new Error(`filter "${text}": a quote isn't closed`);
|
|
1574
|
+
word += text.slice(i, end + 1);
|
|
1575
|
+
i = end + 1;
|
|
1576
|
+
}
|
|
1577
|
+
else
|
|
1578
|
+
word += text[i++];
|
|
1579
|
+
}
|
|
1580
|
+
tokens.push(word);
|
|
1581
|
+
}
|
|
1582
|
+
let at = 0;
|
|
1583
|
+
const fail = (why) => { throw new Error(`filter "${text}": ${why}`); };
|
|
1584
|
+
const term = (word) => {
|
|
1585
|
+
const colon = word.indexOf(":");
|
|
1586
|
+
if (colon <= 0)
|
|
1587
|
+
fail(`${word} isn't field:value`);
|
|
1588
|
+
const rest = word.slice(colon + 1);
|
|
1589
|
+
const op = (/^(>=|<=|>|<)/.exec(rest)?.[1] ?? ":");
|
|
1590
|
+
const raw = op === ":" ? rest : rest.slice(op.length);
|
|
1591
|
+
const value = /^".*"$/.test(raw) ? raw.slice(1, -1) : raw;
|
|
1592
|
+
if (!value)
|
|
1593
|
+
fail(`${word} has no value`);
|
|
1594
|
+
return { field: word.slice(0, colon), op, value };
|
|
1595
|
+
};
|
|
1596
|
+
const unary = () => {
|
|
1597
|
+
const t = tokens[at++];
|
|
1598
|
+
if (t === undefined)
|
|
1599
|
+
return fail("it ends where a condition should be");
|
|
1600
|
+
if (t === "NOT")
|
|
1601
|
+
return { not: unary() };
|
|
1602
|
+
if (t === "(") {
|
|
1603
|
+
const inner = either();
|
|
1604
|
+
if (tokens[at++] !== ")")
|
|
1605
|
+
fail("a parenthesis isn't closed");
|
|
1606
|
+
return inner;
|
|
1607
|
+
}
|
|
1608
|
+
if (t === ")" || t === "AND" || t === "OR")
|
|
1609
|
+
return fail(`${t} where a condition should be`);
|
|
1610
|
+
return t.startsWith("-") ? { not: term(t.slice(1)) } : term(t);
|
|
1611
|
+
};
|
|
1612
|
+
// Conditions side by side must all hold, as with AND.
|
|
1613
|
+
const both = () => {
|
|
1614
|
+
const list = [unary()];
|
|
1615
|
+
while (at < tokens.length && tokens[at] !== ")" && tokens[at] !== "OR") {
|
|
1616
|
+
if (tokens[at] === "AND")
|
|
1617
|
+
at++;
|
|
1618
|
+
list.push(unary());
|
|
1619
|
+
}
|
|
1620
|
+
return list.length === 1 ? list[0] : { all: list };
|
|
1621
|
+
};
|
|
1622
|
+
const either = () => {
|
|
1623
|
+
const list = [both()];
|
|
1624
|
+
while (tokens[at] === "OR") {
|
|
1625
|
+
at++;
|
|
1626
|
+
list.push(both());
|
|
1627
|
+
}
|
|
1628
|
+
return list.length === 1 ? list[0] : { any: list };
|
|
1629
|
+
};
|
|
1630
|
+
const tree = either();
|
|
1631
|
+
if (at < tokens.length)
|
|
1632
|
+
fail(`${tokens[at]} is out of place`);
|
|
1633
|
+
return tree;
|
|
1634
|
+
}
|
|
1635
|
+
/** A field's values in a payload by dot path; through a list, every element's. */
|
|
1636
|
+
function valuesAt(v, path) {
|
|
1637
|
+
if (Array.isArray(v))
|
|
1638
|
+
return v.flatMap((x) => valuesAt(x, path));
|
|
1639
|
+
if (!path.length)
|
|
1640
|
+
return [v];
|
|
1641
|
+
return isObj(v) && path[0] in v ? valuesAt(v[path[0]], path.slice(1)) : [];
|
|
1642
|
+
}
|
|
1643
|
+
const NUMERIC = /^-?\d+(\.\d+)?$/;
|
|
1644
|
+
function holds(node, payload) {
|
|
1645
|
+
if ("all" in node)
|
|
1646
|
+
return node.all.every((n) => holds(n, payload));
|
|
1647
|
+
if ("any" in node)
|
|
1648
|
+
return node.any.some((n) => holds(n, payload));
|
|
1649
|
+
if ("not" in node)
|
|
1650
|
+
return !holds(node.not, payload);
|
|
1651
|
+
const path = node.field.split(".");
|
|
1652
|
+
// Tags arrive as one comma-separated string; each tag is matched on its own.
|
|
1653
|
+
const found = valuesAt(payload, path).flatMap((x) => (path[path.length - 1] === "tags" && typeof x === "string" ? x.split(",").map((t) => t.trim()) : [x]))
|
|
1654
|
+
.filter((x) => x !== null && x !== undefined);
|
|
1655
|
+
const { op, value } = node;
|
|
1656
|
+
if (op === ":" && value === "*")
|
|
1657
|
+
return found.length > 0;
|
|
1658
|
+
return found.some((x) => {
|
|
1659
|
+
const text = typeof x === "object" ? JSON.stringify(x) : String(x);
|
|
1660
|
+
if (op === ":") {
|
|
1661
|
+
if (value.endsWith("*"))
|
|
1662
|
+
return text.startsWith(value.slice(0, -1));
|
|
1663
|
+
return NUMERIC.test(text) && NUMERIC.test(value) ? Number(text) === Number(value) : text === value;
|
|
1664
|
+
}
|
|
1665
|
+
const dates = /^\d{4}-\d{2}-\d{2}/;
|
|
1666
|
+
const c = NUMERIC.test(text) && NUMERIC.test(value) ? Number(text) - Number(value)
|
|
1667
|
+
: dates.test(text) && dates.test(value) && !Number.isNaN(Date.parse(text)) && !Number.isNaN(Date.parse(value)) ? Date.parse(text) - Date.parse(value)
|
|
1668
|
+
: text < value ? -1 : text > value ? 1 : 0;
|
|
1669
|
+
return op === ">" ? c > 0 : op === ">=" ? c >= 0 : op === "<" ? c < 0 : c <= 0;
|
|
1670
|
+
});
|
|
1671
|
+
}
|
|
1672
|
+
const filterFields = (node) => "all" in node ? node.all.flatMap(filterFields) : "any" in node ? node.any.flatMap(filterFields) : "not" in node ? filterFields(node.not) : [node.field];
|
|
1673
|
+
/**
|
|
1674
|
+
* Whether Shopify delivers a payload to a webhook entry with this `filter`, in Shopify's search syntax:
|
|
1675
|
+
* `field:value` (case-sensitive), `field:>=10`, `field:*` (has a value), `field:Prefix*`, dot paths into nested fields
|
|
1676
|
+
* (any element of a list), AND (or just a space), OR, NOT or a leading `-`, and parentheses. Throws on a filter
|
|
1677
|
+
* Shopify would refuse to subscribe with.
|
|
1678
|
+
*/
|
|
1679
|
+
export function matchesWebhookFilter(filter, payload) {
|
|
1680
|
+
return holds(parseWebhookFilter(filter), payload);
|
|
1681
|
+
}
|
|
1682
|
+
/** The calls an action may take: a minute at most (30 seconds unless it says), five minutes as a background job. */
|
|
1683
|
+
const CALL_TIMEOUT_MS = 60_000;
|
|
1684
|
+
const DEFAULT_CALL_TIMEOUT_MS = 30_000;
|
|
1685
|
+
const JOB_TIMEOUT_MS = 300_000;
|
|
739
1686
|
/** An action's `run` threw: nothing it asked for was applied. The API answers the same 422. */
|
|
740
1687
|
export class ActionFailedError extends ApiError {
|
|
741
1688
|
logs;
|
|
@@ -756,12 +1703,19 @@ export function createTestApp(o = {}) {
|
|
|
756
1703
|
const shopify = shopifyMock();
|
|
757
1704
|
const jobs = [];
|
|
758
1705
|
const logs = [];
|
|
1706
|
+
const perms = readPermissions(o.permissions);
|
|
1707
|
+
/** Schedule times already queued, so each is queued once. */
|
|
1708
|
+
const scheduled = new Set();
|
|
759
1709
|
let jobSeq = 0;
|
|
760
1710
|
async function actionOf(model, action) {
|
|
761
1711
|
const load = registry[model ? `${model}/${action}` : `$global/${action}`];
|
|
762
1712
|
return load ? load() : null;
|
|
763
1713
|
}
|
|
764
|
-
|
|
1714
|
+
/**
|
|
1715
|
+
* One run of an action: `caller` when a call held to a role asked for it, `background` for a job's (a webhook's or a
|
|
1716
|
+
* schedule's), which may take longer.
|
|
1717
|
+
*/
|
|
1718
|
+
async function run(model, action, input = {}, how = {}) {
|
|
765
1719
|
const name = model ? `${model}.${action}` : action;
|
|
766
1720
|
const mod = await actionOf(model, action);
|
|
767
1721
|
if (!mod)
|
|
@@ -782,7 +1736,23 @@ export function createTestApp(o = {}) {
|
|
|
782
1736
|
}
|
|
783
1737
|
const params = wire(input.params ?? {});
|
|
784
1738
|
const shop = input.shop ?? null;
|
|
785
|
-
const
|
|
1739
|
+
const max = how.background ? JOB_TIMEOUT_MS : CALL_TIMEOUT_MS;
|
|
1740
|
+
const given = mod.options?.timeoutMS;
|
|
1741
|
+
const timeout = Math.min(max, Math.max(1000, typeof given === "number" ? given : how.background ? JOB_TIMEOUT_MS : DEFAULT_CALL_TIMEOUT_MS));
|
|
1742
|
+
// A phase that runs out of time is answered as the API answers it; the run itself is left to finish unheard.
|
|
1743
|
+
const phase = async (p, rec, isNew) => {
|
|
1744
|
+
let timer;
|
|
1745
|
+
const late = new Promise((_, reject) => {
|
|
1746
|
+
timer = setTimeout(() => reject(new ApiError(`The action took longer than ${Math.round(timeout / 1000)} seconds.`, 503, "dependency_unavailable")), timeout);
|
|
1747
|
+
});
|
|
1748
|
+
const req = { phase: p, model, action, params, record: rec, isNew, environment, shop, caller: how.caller ?? null, ...(how.background ? { background: true } : {}) };
|
|
1749
|
+
try {
|
|
1750
|
+
return await Promise.race([runPhase(wire(req), registry, { api, shopify: shopify.client, echo: o.echo ?? false }), late]);
|
|
1751
|
+
}
|
|
1752
|
+
finally {
|
|
1753
|
+
clearTimeout(timer);
|
|
1754
|
+
}
|
|
1755
|
+
};
|
|
786
1756
|
const keep = (lines) => { for (const l of lines)
|
|
787
1757
|
logs.push({ ...l, action: name }); };
|
|
788
1758
|
const reply = await phase("run", record, type === "create");
|
|
@@ -799,7 +1769,7 @@ export function createTestApp(o = {}) {
|
|
|
799
1769
|
else
|
|
800
1770
|
saved = type === "create" ? await store.create(model, reply.write.values) : await store.update(model, input.id, reply.write.values);
|
|
801
1771
|
}
|
|
802
|
-
const after = await phase("onSuccess", saved ?? record, false);
|
|
1772
|
+
const after = await phase("onSuccess", saved ?? record, false).catch((e) => ({ ok: false, error: { message: e.message }, logs: [] }));
|
|
803
1773
|
keep(after.logs);
|
|
804
1774
|
return {
|
|
805
1775
|
action: name, record: saved, ...("result" in reply && reply.result !== undefined ? { result: wire(reply.result) } : {}),
|
|
@@ -810,60 +1780,79 @@ export function createTestApp(o = {}) {
|
|
|
810
1780
|
const dot = action.indexOf(".");
|
|
811
1781
|
return dot < 0 ? { model: null, action } : { model: action.slice(0, dot), action: action.slice(dot + 1) };
|
|
812
1782
|
};
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
if (
|
|
819
|
-
return
|
|
820
|
-
const
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
const
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
1783
|
+
const otherStore = (shop) => denied(`This call is for ${shop}; it can't reach another store's records.`);
|
|
1784
|
+
/** The record by id when the filter lets the call see it (undefined: no narrowing), else null. */
|
|
1785
|
+
const findVisible = async (model, id, filter) => (filter ? (await store.findMany(model, { filter: { AND: [{ id: { equals: id } }, filter] }, first: 1 })).records[0] ?? null : store.findOne(model, id));
|
|
1786
|
+
/** Refuses a run the scope doesn't allow; a record its store or grant doesn't reach is answered as missing. */
|
|
1787
|
+
async function canRun(scope, model, action, id) {
|
|
1788
|
+
if (!isScoped(scope))
|
|
1789
|
+
return;
|
|
1790
|
+
const m = model ? store.model(model) : null;
|
|
1791
|
+
const filter = actionFilter(perms, scope, m, action);
|
|
1792
|
+
if (m && id && !(await findVisible(m.name, id, filter)))
|
|
1793
|
+
throw notFound(`There's no ${m.name} with id ${id}.`);
|
|
1794
|
+
}
|
|
1795
|
+
/** A run through the API for the scope: what it may run, on records it reaches, for its own store, as its caller. */
|
|
1796
|
+
async function runFor(scope, model, action, q) {
|
|
1797
|
+
await canRun(scope, model, action, q.id ?? null);
|
|
1798
|
+
const shop = scopedShop(scope, q.shop ?? null);
|
|
1799
|
+
const { shop: _given, ...rest } = q;
|
|
1800
|
+
return run(model, action, { ...rest, ...(shop ? { shop } : {}) }, { caller: scope.role ? { role: scope.role, ...scope.session } : null });
|
|
1801
|
+
}
|
|
1802
|
+
// As through the API, a scope narrows what a call reaches: a store to its records of store-owned models (api.forShop,
|
|
1803
|
+
// an action's api), a role to what permissions.ib.ts grants it (api.asRole).
|
|
1804
|
+
const modelClient = (name, scope) => {
|
|
1805
|
+
const scoped = isScoped(scope);
|
|
1806
|
+
const readOf = (filter) => (scoped ? readFilter(perms, scope, store.model(name), filter) : filter);
|
|
1807
|
+
const narrow = (q) => { const filter = readOf(q.filter); return filter ? { ...q, filter } : q; };
|
|
1808
|
+
// A file goes to a record the role may make or change.
|
|
1809
|
+
const uploadable = () => {
|
|
1810
|
+
if (!scoped)
|
|
1811
|
+
return;
|
|
1812
|
+
try {
|
|
1813
|
+
actionFilter(perms, scope, store.model(name), "create");
|
|
1814
|
+
}
|
|
1815
|
+
catch {
|
|
1816
|
+
actionFilter(perms, scope, store.model(name), "update");
|
|
1817
|
+
}
|
|
845
1818
|
};
|
|
846
1819
|
return {
|
|
847
|
-
findOne: async (id) =>
|
|
1820
|
+
findOne: async (id) => findVisible(name, id, readOf()),
|
|
848
1821
|
findMany: async (q = {}) => store.findMany(name, narrow(wire(q))),
|
|
849
1822
|
findFirst: async (q = {}) => (await store.findMany(name, { ...narrow(wire(q)), first: 1 })).records[0] ?? null,
|
|
850
1823
|
search: async (field, near, q = {}) => (await store.findMany(name, { ...narrow(wire(q)), search: { field, near } })),
|
|
851
1824
|
// As through the API: the model's create, update or delete action when it has one, else a direct write.
|
|
852
|
-
create: async (values) => {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
store.
|
|
858
|
-
|
|
1825
|
+
create: async (values) => {
|
|
1826
|
+
const v = scoped ? scopedValues(scope, store.model(name), wire(values)) : wire(values);
|
|
1827
|
+
if (registry[`${name}/create`])
|
|
1828
|
+
return (await runFor(scope, name, "create", { params: v })).record;
|
|
1829
|
+
await canRun(scope, name, "create", null);
|
|
1830
|
+
return store.create(name, v);
|
|
1831
|
+
},
|
|
1832
|
+
update: async (id, values) => {
|
|
1833
|
+
const v = scoped ? scopedUpdate(scope, store.model(name), wire(values)) : wire(values);
|
|
1834
|
+
if (registry[`${name}/update`])
|
|
1835
|
+
return (await runFor(scope, name, "update", { id, params: v })).record;
|
|
1836
|
+
await canRun(scope, name, "update", id);
|
|
1837
|
+
return store.update(name, id, v);
|
|
1838
|
+
},
|
|
1839
|
+
delete: async (id) => {
|
|
1840
|
+
if (registry[`${name}/delete`]) {
|
|
1841
|
+
await runFor(scope, name, "delete", { id });
|
|
1842
|
+
return;
|
|
1843
|
+
}
|
|
1844
|
+
await canRun(scope, name, "delete", id);
|
|
1845
|
+
store.delete(name, id);
|
|
1846
|
+
},
|
|
1847
|
+
// A record the call can't read answers false, as a missing one does.
|
|
1848
|
+
checkPassword: async (id, password, field) => (scoped && !(await findVisible(name, id, readOf())) ? false : store.checkPassword(name, id, password, typeof field === "string" ? field : field?.field)),
|
|
859
1849
|
run: async (action, q = {}) => {
|
|
860
|
-
|
|
861
|
-
reach(q.id);
|
|
862
|
-
const r = await run(name, action, runFor(q));
|
|
1850
|
+
const r = await runFor(scope, name, action, q);
|
|
863
1851
|
return { record: r.record, ...(r.result !== undefined ? { result: r.result } : {}) };
|
|
864
1852
|
},
|
|
865
1853
|
createUpload: async (field, q = {}) => {
|
|
866
1854
|
fileFieldOf(store.model(name), field);
|
|
1855
|
+
uploadable();
|
|
867
1856
|
const token = `upload_${String(++uploadSeq).padStart(6, "0")}`, fileName = baseName(q.fileName);
|
|
868
1857
|
const mimeType = q.mimeType === undefined ? null : mimeOf(q.mimeType);
|
|
869
1858
|
UPLOADS.set(token, { url: `https://uploads.test/${token}/${encodeURIComponent(fileName)}`, fileName, mimeType: mimeType ?? "application/octet-stream", size: q.size ?? 0 });
|
|
@@ -871,6 +1860,7 @@ export function createTestApp(o = {}) {
|
|
|
871
1860
|
},
|
|
872
1861
|
upload: async (field, body, q = {}) => {
|
|
873
1862
|
fileFieldOf(store.model(name), field);
|
|
1863
|
+
uploadable();
|
|
874
1864
|
const blob = body instanceof Blob ? body : new Blob([body]);
|
|
875
1865
|
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
876
1866
|
let bin = "";
|
|
@@ -893,10 +1883,13 @@ export function createTestApp(o = {}) {
|
|
|
893
1883
|
return wire(job);
|
|
894
1884
|
};
|
|
895
1885
|
// Runs succeed at once and write nothing: a test sees what was asked for in `syncRuns`.
|
|
896
|
-
const shopifySync = (shop) => {
|
|
897
|
-
const
|
|
1886
|
+
const shopifySync = ({ shop, role }) => {
|
|
1887
|
+
const allowed = () => { if (role)
|
|
1888
|
+
throw denied(`The ${role} role can't run Shopify syncs.`); };
|
|
1889
|
+
const find = (id) => { allowed(); return syncRuns.find((r) => r.id === id) ?? null; };
|
|
898
1890
|
return {
|
|
899
1891
|
run: async ({ wait: _wait, since, ...q }) => {
|
|
1892
|
+
allowed();
|
|
900
1893
|
const target = q.shop ?? shop;
|
|
901
1894
|
if (!target)
|
|
902
1895
|
throw new ApiError("Name the store: shop.", 400, "invalid_request");
|
|
@@ -911,26 +1904,33 @@ export function createTestApp(o = {}) {
|
|
|
911
1904
|
return r;
|
|
912
1905
|
},
|
|
913
1906
|
get: async (id) => find(id),
|
|
914
|
-
list: async (q = {}) => syncRuns.filter((r) => !(q.shop ?? shop) || r.shop === (q.shop ?? shop)).reverse().slice(0, q.limit ?? 50),
|
|
1907
|
+
list: async (q = {}) => { allowed(); return syncRuns.filter((r) => !(q.shop ?? shop) || r.shop === (q.shop ?? shop)).reverse().slice(0, q.limit ?? 50); },
|
|
915
1908
|
abort: async (id) => { const r = find(id); if (!r)
|
|
916
1909
|
throw notFound(`There's no sync run ${id}.`); return r; },
|
|
917
1910
|
};
|
|
918
1911
|
};
|
|
919
1912
|
const syncRuns = [];
|
|
920
1913
|
const apis = new Map();
|
|
921
|
-
/**
|
|
922
|
-
const
|
|
923
|
-
|
|
1914
|
+
/** A role's name as the API takes it; a malformed one is refused, never ignored (ignoring it would widen the call). */
|
|
1915
|
+
const roleOf = (role) => {
|
|
1916
|
+
if (!ROLE.test(role))
|
|
1917
|
+
throw new ApiError("x-ib-role must be a role's name.", 400, "invalid_request");
|
|
1918
|
+
return role;
|
|
1919
|
+
};
|
|
1920
|
+
/** The API for a scope: every store or one (`forShop`), as the environment or held to a role (`asRole`). */
|
|
1921
|
+
const apiFor = (scope) => {
|
|
1922
|
+
const key = JSON.stringify(scope);
|
|
924
1923
|
if (apis.has(key))
|
|
925
1924
|
return apis.get(key);
|
|
926
1925
|
const clients = new Map();
|
|
927
1926
|
const $global = async (action, params = {}) => {
|
|
928
|
-
const r = await
|
|
1927
|
+
const r = await runFor(scope, null, action, { params });
|
|
929
1928
|
return r.result !== undefined ? { result: r.result } : {};
|
|
930
1929
|
};
|
|
931
|
-
const queue = (action, params = {}, q = {}) => {
|
|
932
|
-
|
|
933
|
-
|
|
1930
|
+
const queue = async (action, params = {}, q = {}) => {
|
|
1931
|
+
const n = parseName(action);
|
|
1932
|
+
await canRun(scope, n.model, n.action, q.id ?? null);
|
|
1933
|
+
const shop = scopedShop(scope, q.shop ?? null);
|
|
934
1934
|
return enqueue(action, params, shop ? { ...q, shop } : q);
|
|
935
1935
|
};
|
|
936
1936
|
const made = new Proxy({}, {
|
|
@@ -940,22 +1940,22 @@ export function createTestApp(o = {}) {
|
|
|
940
1940
|
if (prop === "enqueue")
|
|
941
1941
|
return queue;
|
|
942
1942
|
if (prop === "forShop")
|
|
943
|
-
return (s) => apiFor(s);
|
|
1943
|
+
return (s) => apiFor({ ...scope, shop: s });
|
|
944
1944
|
if (prop === "asRole")
|
|
945
|
-
return () =>
|
|
1945
|
+
return (role, session) => apiFor({ ...scope, role: roleOf(role), session: session ? sessionOf(session) : scope.session });
|
|
946
1946
|
if (prop === "shopifySync")
|
|
947
|
-
return shopifySync(
|
|
1947
|
+
return shopifySync(scope);
|
|
948
1948
|
if (typeof prop !== "string" || prop === "then")
|
|
949
1949
|
return undefined;
|
|
950
1950
|
if (!clients.has(prop))
|
|
951
|
-
clients.set(prop, modelClient(prop,
|
|
1951
|
+
clients.set(prop, modelClient(prop, scope));
|
|
952
1952
|
return clients.get(prop);
|
|
953
1953
|
},
|
|
954
1954
|
});
|
|
955
1955
|
apis.set(key, made);
|
|
956
1956
|
return made;
|
|
957
1957
|
};
|
|
958
|
-
const api = apiFor(
|
|
1958
|
+
const api = apiFor(FULL_ACCESS);
|
|
959
1959
|
// ---- Shopify's side ----
|
|
960
1960
|
const shopModel = () => models.get("shopifyShop");
|
|
961
1961
|
const shopRecord = async (shop) => (shopModel()?.byName.has("myshopifyDomain") ? (await store.findMany("shopifyShop", { filter: { myshopifyDomain: { equals: shop } }, first: 1 })).records[0] ?? null : null);
|
|
@@ -1069,8 +2069,18 @@ export function createTestApp(o = {}) {
|
|
|
1069
2069
|
if (s.topic !== topic)
|
|
1070
2070
|
continue;
|
|
1071
2071
|
handled = true;
|
|
2072
|
+
// Shopify delivers only what the filter matches, and needs every field it names in includeFields.
|
|
2073
|
+
if (s.filter) {
|
|
2074
|
+
const tree = parseWebhookFilter(s.filter);
|
|
2075
|
+
const left = s.includeFields?.length ? filterFields(tree).filter((f) => !s.includeFields.includes(f.split(".")[0])) : [];
|
|
2076
|
+
if (left.length)
|
|
2077
|
+
throw new Error(`The ${topic} webhook for ${s.action} filters on ${left.join(", ")}, which its includeFields leave out; Shopify needs every field the filter names in includeFields.`);
|
|
2078
|
+
if (!holds(tree, wire(payload)))
|
|
2079
|
+
continue;
|
|
2080
|
+
}
|
|
1072
2081
|
const params = s.includeFields?.length ? Object.fromEntries(s.includeFields.filter((k) => k in payload).map((k) => [k, payload[k]])) : payload;
|
|
1073
|
-
|
|
2082
|
+
// Queued as a job on Inference Bureau, so it has a job's time.
|
|
2083
|
+
runs.push(await run(null, s.action, { params, shop }, { background: true }));
|
|
1074
2084
|
}
|
|
1075
2085
|
if (!handled)
|
|
1076
2086
|
throw new Error(`Nothing handles ${topic}: no synced Shopify model takes it and no webhooks entry in settings.ib.ts names it.`);
|
|
@@ -1089,7 +2099,7 @@ export function createTestApp(o = {}) {
|
|
|
1089
2099
|
next.attempts++;
|
|
1090
2100
|
const { model, action } = parseName(next.action);
|
|
1091
2101
|
try {
|
|
1092
|
-
await run(model, action, { params: next.params, ...(next.record_id ? { id: next.record_id } : {}), ...(next.shop ? { shop: next.shop } : {}) });
|
|
2102
|
+
await run(model, action, { params: next.params, ...(next.record_id ? { id: next.record_id } : {}), ...(next.shop ? { shop: next.shop } : {}) }, { background: true });
|
|
1093
2103
|
next.state = "succeeded";
|
|
1094
2104
|
next.last_error = null;
|
|
1095
2105
|
}
|
|
@@ -1103,6 +2113,10 @@ export function createTestApp(o = {}) {
|
|
|
1103
2113
|
return {
|
|
1104
2114
|
api,
|
|
1105
2115
|
run: (action, q) => { const n = parseName(action); return run(n.model, n.action, q); },
|
|
2116
|
+
async runAs({ role, shop, ...session }, action, q = {}) {
|
|
2117
|
+
const n = parseName(action);
|
|
2118
|
+
return runFor({ shop: shop ?? null, role: role === undefined ? null : roleOf(role), session: sessionOf(session) }, n.model, n.action, q);
|
|
2119
|
+
},
|
|
1106
2120
|
seed: (model, values) => store.create(model, wire(values)),
|
|
1107
2121
|
records: (model) => store.all(model),
|
|
1108
2122
|
install: (shop = TEST_SHOP, q = {}) => shopHook(shop, "install", { myshopifyDomain: shop, state: "installed", installedAt: iso((o.now ?? Date.now)()), uninstalledAt: null, grantedScopes: q.scopes ?? "" }),
|
|
@@ -1111,9 +2125,24 @@ export function createTestApp(o = {}) {
|
|
|
1111
2125
|
jobs,
|
|
1112
2126
|
syncRuns,
|
|
1113
2127
|
runJobs,
|
|
2128
|
+
async queueSchedules(w) {
|
|
2129
|
+
const from = msOf(w.from, "from"), to = w.to === undefined ? (o.now ?? Date.now)() : msOf(w.to, "to");
|
|
2130
|
+
const due = (o.settings?.schedules ?? []).filter((s) => (s.environments ?? ["production"]).includes(environment))
|
|
2131
|
+
.flatMap((s) => runsBetween(s.cron, s.timezone ?? "UTC", from, to, 1000).map((at) => ({ s, at })))
|
|
2132
|
+
.sort((a, b) => a.at - b.at);
|
|
2133
|
+
const queued = [];
|
|
2134
|
+
for (const { s, at } of due) {
|
|
2135
|
+
const key = `${s.action}:${s.cron}:${s.timezone ?? "UTC"}:${at}`;
|
|
2136
|
+
if (scheduled.has(key))
|
|
2137
|
+
continue;
|
|
2138
|
+
scheduled.add(key);
|
|
2139
|
+
queued.push(await enqueue(s.action, { scheduledAt: new Date(at).toISOString() }));
|
|
2140
|
+
}
|
|
2141
|
+
return queued;
|
|
2142
|
+
},
|
|
1114
2143
|
shopify,
|
|
1115
2144
|
logs,
|
|
1116
|
-
reset() { store.clear(); jobs.length = 0; syncRuns.length = 0; logs.length = 0; shopify.calls.length = 0; },
|
|
2145
|
+
reset() { store.clear(); jobs.length = 0; scheduled.clear(); syncRuns.length = 0; logs.length = 0; shopify.calls.length = 0; },
|
|
1117
2146
|
};
|
|
1118
2147
|
}
|
|
1119
2148
|
// ---- loading an app's folder ----
|
|
@@ -1124,7 +2153,7 @@ const GLOBAL_ACTION = /^api\/actions\/([^/]+)\.(ts|tsx|js|mjs)$/;
|
|
|
1124
2153
|
const nodeImport = (name) => import(/* @vite-ignore */ name);
|
|
1125
2154
|
/**
|
|
1126
2155
|
* A TestApp from an app's folder (the working directory unless named): every model's schema.ib.ts, every action
|
|
1127
|
-
* file (loaded when first run, as a deployment does) and
|
|
2156
|
+
* file (loaded when first run, as a deployment does), settings.ib.ts and accessControl/permissions.ib.ts. Runs under Vitest (`ib-apps test`), with
|
|
1128
2157
|
* this package inlined (`server.deps.inline`, as new apps' vitest.config.ts has it) so it can load TypeScript.
|
|
1129
2158
|
*/
|
|
1130
2159
|
export async function loadApp(dir, o = {}) {
|
|
@@ -1174,5 +2203,6 @@ export async function loadApp(dir, o = {}) {
|
|
|
1174
2203
|
actions[`$global/${g[1]}`] = () => load(f);
|
|
1175
2204
|
}
|
|
1176
2205
|
const settings = fs.existsSync(path.join(root, "settings.ib.ts")) ? (await load("settings.ib.ts")).settings : undefined;
|
|
1177
|
-
|
|
2206
|
+
const permissions = fs.existsSync(path.join(root, PERMISSIONS_PATH)) ? (await load(PERMISSIONS_PATH)).permissions : undefined;
|
|
2207
|
+
return createTestApp({ ...o, models, actions, ...(settings ? { settings } : {}), ...(permissions ? { permissions } : {}) });
|
|
1178
2208
|
}
|