@revenexx/app-sdk 1.2.0 → 1.4.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 +88 -0
- package/dist/{chunk-P6ZEOMKS.js → chunk-D6QST6NV.js} +138 -5
- package/dist/chunk-D6QST6NV.js.map +1 -0
- package/dist/{errors-CtrtCOoG.d.cts → errors-DERK5sWS.d.cts} +109 -1
- package/dist/{errors-CtrtCOoG.d.ts → errors-DERK5sWS.d.ts} +109 -1
- package/dist/index.cjs +99 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -4
- package/dist/index.d.ts +19 -4
- package/dist/index.js +25 -2
- package/dist/index.js.map +1 -1
- package/dist/router/index.cjs +175 -15
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +21 -3
- package/dist/router/index.d.ts +21 -3
- package/dist/router/index.js +52 -12
- package/dist/router/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-P6ZEOMKS.js.map +0 -1
package/dist/router/index.cjs
CHANGED
|
@@ -32,6 +32,9 @@ var router_exports = {};
|
|
|
32
32
|
__export(router_exports, {
|
|
33
33
|
ACTOR_HEADERS: () => ACTOR_HEADERS,
|
|
34
34
|
AUDIT_SETTINGS_PATH: () => AUDIT_SETTINGS_PATH,
|
|
35
|
+
BACKDATING_FIELD: () => BACKDATING_FIELD,
|
|
36
|
+
BACKDATING_INVALID: () => BACKDATING_INVALID,
|
|
37
|
+
BACKDATING_SETTING: () => BACKDATING_SETTING,
|
|
35
38
|
DEFAULT_GATEWAY_ENDPOINT: () => DEFAULT_GATEWAY_ENDPOINT,
|
|
36
39
|
DEFAULT_SETTINGS_ERROR_TTL_MS: () => DEFAULT_SETTINGS_ERROR_TTL_MS,
|
|
37
40
|
DEFAULT_SETTINGS_TIMEOUT_MS: () => DEFAULT_SETTINGS_TIMEOUT_MS,
|
|
@@ -39,12 +42,17 @@ __export(router_exports, {
|
|
|
39
42
|
DEFAULT_TRASH_RETENTION_DAYS: () => DEFAULT_TRASH_RETENTION_DAYS,
|
|
40
43
|
HttpError: () => HttpError,
|
|
41
44
|
MARKET_HEADERS: () => MARKET_HEADERS,
|
|
45
|
+
MAX_MONTHS_BACK: () => MAX_MONTHS_BACK,
|
|
42
46
|
PURGE_REASON_COLUMN: () => PURGE_REASON_COLUMN,
|
|
43
47
|
RESERVED_LIST_QUERY_KEYS: () => RESERVED_LIST_QUERY_KEYS,
|
|
44
48
|
TRASH_COLUMN: () => TRASH_COLUMN,
|
|
45
49
|
actorFrom: () => actorFrom,
|
|
46
50
|
auditSettingsUrl: () => auditSettingsUrl,
|
|
51
|
+
backdated: () => backdated,
|
|
52
|
+
backdatingAllowed: () => backdatingAllowed,
|
|
53
|
+
backdatingDb: () => backdatingDb,
|
|
47
54
|
badRequest: () => badRequest,
|
|
55
|
+
clock: () => clock,
|
|
48
56
|
conflict: () => conflict,
|
|
49
57
|
createApp: () => createApp,
|
|
50
58
|
createGatewayCounter: () => createGatewayCounter,
|
|
@@ -55,8 +63,11 @@ __export(router_exports, {
|
|
|
55
63
|
mountCrud: () => mountCrud,
|
|
56
64
|
mountTrash: () => mountTrash,
|
|
57
65
|
notFound: () => notFound,
|
|
66
|
+
now: () => now,
|
|
67
|
+
parseBackdate: () => parseBackdate,
|
|
58
68
|
readTrashRetention: () => readTrashRetention,
|
|
59
|
-
refusalOf: () => refusalOf
|
|
69
|
+
refusalOf: () => refusalOf,
|
|
70
|
+
runAt: () => runAt
|
|
60
71
|
});
|
|
61
72
|
module.exports = __toCommonJS(router_exports);
|
|
62
73
|
|
|
@@ -250,11 +261,11 @@ function createSettings(options = {}) {
|
|
|
250
261
|
if (!app) return fallback("", tenant, market, "no app name (pass `app` or set REVENEXX_APP_NAME)");
|
|
251
262
|
if (!tenant) return fallback(app, "", market, "no tenant on the request");
|
|
252
263
|
const key = `${tenant}\0${app}\0${market ?? ""}`;
|
|
253
|
-
const
|
|
264
|
+
const now2 = Date.now();
|
|
254
265
|
const hit = cache.get(key);
|
|
255
|
-
if (hit &&
|
|
266
|
+
if (hit && now2 - hit.at < hit.ttl) return hit.promise;
|
|
256
267
|
const token = (source?.contextToken ?? "") || headerOf(source, "x-revenexx-context");
|
|
257
|
-
const entry = { at:
|
|
268
|
+
const entry = { at: now2, ttl: ttlMs, promise: void 0 };
|
|
258
269
|
entry.promise = request(app, tenant, token, market).then((snapshot) => {
|
|
259
270
|
lastGood.set(key, snapshot);
|
|
260
271
|
return snapshot;
|
|
@@ -282,6 +293,9 @@ function createSettings(options = {}) {
|
|
|
282
293
|
};
|
|
283
294
|
}
|
|
284
295
|
|
|
296
|
+
// src/backdating.ts
|
|
297
|
+
var import_node_async_hooks = require("async_hooks");
|
|
298
|
+
|
|
285
299
|
// src/errors.ts
|
|
286
300
|
var HttpError = class extends Error {
|
|
287
301
|
status;
|
|
@@ -345,6 +359,125 @@ function refusalOf(err) {
|
|
|
345
359
|
};
|
|
346
360
|
}
|
|
347
361
|
|
|
362
|
+
// src/backdating.ts
|
|
363
|
+
var MAX_MONTHS_BACK = 24;
|
|
364
|
+
var BACKDATING_SETTING = "allow_backdating";
|
|
365
|
+
var BACKDATING_FIELD = "at";
|
|
366
|
+
var BACKDATING_INVALID = "backdating_invalid";
|
|
367
|
+
var BACKDATING_METHODS = ["POST", "PUT", "PATCH", "DELETE"];
|
|
368
|
+
var STORE_KEY = /* @__PURE__ */ Symbol.for("@revenexx/app-sdk/backdating-clock");
|
|
369
|
+
function clockStorage() {
|
|
370
|
+
const g = globalThis;
|
|
371
|
+
let storage = g[STORE_KEY];
|
|
372
|
+
if (!storage) {
|
|
373
|
+
storage = new import_node_async_hooks.AsyncLocalStorage();
|
|
374
|
+
g[STORE_KEY] = storage;
|
|
375
|
+
}
|
|
376
|
+
return storage;
|
|
377
|
+
}
|
|
378
|
+
var CLOCK = clockStorage();
|
|
379
|
+
var now = () => CLOCK.getStore()?.at ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
380
|
+
var clock = () => new Date(now());
|
|
381
|
+
var backdated = () => Boolean(CLOCK.getStore()?.at);
|
|
382
|
+
function runAt(at, fn) {
|
|
383
|
+
return at ? CLOCK.run({ at }, fn) : fn();
|
|
384
|
+
}
|
|
385
|
+
var ISO_DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
386
|
+
var invalid = (field, message) => new HttpError(400, `'${field}' ${message}`, { field }, { code: BACKDATING_INVALID });
|
|
387
|
+
function parseBackdate(field, value, reference = /* @__PURE__ */ new Date()) {
|
|
388
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
389
|
+
if (!ISO_DATE_TIME.test(text)) throw invalid(field, "must be an ISO 8601 date-time with a time zone");
|
|
390
|
+
const at = new Date(text);
|
|
391
|
+
if (Number.isNaN(at.getTime())) throw invalid(field, "must be an ISO 8601 date-time with a time zone");
|
|
392
|
+
if (at.getTime() > reference.getTime()) throw invalid(field, "must not be in the future");
|
|
393
|
+
const earliest = new Date(reference.getTime());
|
|
394
|
+
earliest.setUTCMonth(earliest.getUTCMonth() - MAX_MONTHS_BACK);
|
|
395
|
+
if (at.getTime() < earliest.getTime()) {
|
|
396
|
+
throw invalid(field, `must not be more than ${MAX_MONTHS_BACK} months back`);
|
|
397
|
+
}
|
|
398
|
+
return at.toISOString();
|
|
399
|
+
}
|
|
400
|
+
function tokenTenant(token) {
|
|
401
|
+
const parts = typeof token === "string" ? token.split(".") : [];
|
|
402
|
+
if (parts.length < 2) return "";
|
|
403
|
+
try {
|
|
404
|
+
const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
405
|
+
const claims = JSON.parse(Buffer.from(payload, "base64").toString("utf8"));
|
|
406
|
+
return typeof claims?.tenant_id === "string" ? claims.tenant_id.trim() : "";
|
|
407
|
+
} catch {
|
|
408
|
+
return "";
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function settingAllowsBackdating(value) {
|
|
412
|
+
return value === true || typeof value === "string" && value.trim().toLowerCase() === "true";
|
|
413
|
+
}
|
|
414
|
+
async function backdatingAllowed(c) {
|
|
415
|
+
const read = (name) => typeof c?.header === "function" ? String(c.header(name) ?? "").trim() : "";
|
|
416
|
+
const header = read("x-revenexx-tenant");
|
|
417
|
+
const claimed = tokenTenant(c?.contextToken || read("x-revenexx-context"));
|
|
418
|
+
if (!header || header !== claimed) return false;
|
|
419
|
+
if (typeof c?.settings !== "function") return false;
|
|
420
|
+
try {
|
|
421
|
+
const settings = await c.settings();
|
|
422
|
+
return settingAllowsBackdating(settings?.[BACKDATING_SETTING]);
|
|
423
|
+
} catch {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
var present = (value) => value !== void 0 && value !== null && value !== "";
|
|
428
|
+
async function requestedMoment(c, fields) {
|
|
429
|
+
const body = c?.body;
|
|
430
|
+
if (!body || typeof body !== "object") return null;
|
|
431
|
+
const field = fields.find((f) => present(body[f]));
|
|
432
|
+
if (field === void 0) return null;
|
|
433
|
+
if (!await backdatingAllowed(c)) return null;
|
|
434
|
+
return parseBackdate(field, body[field]);
|
|
435
|
+
}
|
|
436
|
+
function backdatingFields(option, method, template) {
|
|
437
|
+
if (!option) return null;
|
|
438
|
+
const opts = option === true ? {} : option;
|
|
439
|
+
const key = `${method.toUpperCase()} ${template}`;
|
|
440
|
+
if (opts.exclude?.includes(key)) return null;
|
|
441
|
+
const named = opts.routes?.[key];
|
|
442
|
+
if (named) return named.includes(BACKDATING_FIELD) ? [...named] : [...named, BACKDATING_FIELD];
|
|
443
|
+
return BACKDATING_METHODS.includes(method.toUpperCase()) ? [BACKDATING_FIELD] : null;
|
|
444
|
+
}
|
|
445
|
+
function backdatingDb(db, entities) {
|
|
446
|
+
const wrapped = /* @__PURE__ */ new Map();
|
|
447
|
+
return new Proxy(db, {
|
|
448
|
+
get(target, prop, receiver) {
|
|
449
|
+
const value = Reflect.get(target, prop, receiver);
|
|
450
|
+
if (typeof prop !== "string") return value;
|
|
451
|
+
const columns = entities?.[prop]?.columns;
|
|
452
|
+
if (!columns || !("created_at" in columns) || !value || typeof value !== "object") return value;
|
|
453
|
+
const repo = value;
|
|
454
|
+
if (typeof repo.create !== "function") return value;
|
|
455
|
+
if (!wrapped.has(prop)) {
|
|
456
|
+
wrapped.set(
|
|
457
|
+
prop,
|
|
458
|
+
new Proxy(repo, {
|
|
459
|
+
get(r, key, rr) {
|
|
460
|
+
const member = Reflect.get(r, key, rr);
|
|
461
|
+
if (key !== "create" || typeof member !== "function") return member;
|
|
462
|
+
return (data, ...rest) => {
|
|
463
|
+
if (!backdated() || !data || typeof data !== "object" || Array.isArray(data)) {
|
|
464
|
+
return member.call(r, data, ...rest);
|
|
465
|
+
}
|
|
466
|
+
const at = now();
|
|
467
|
+
const row = { ...data };
|
|
468
|
+
if (row.created_at === void 0) row.created_at = at;
|
|
469
|
+
if ("updated_at" in columns && row.updated_at === void 0) row.updated_at = at;
|
|
470
|
+
return member.call(r, row, ...rest);
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
})
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
return wrapped.get(prop);
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
348
481
|
// src/router/router.ts
|
|
349
482
|
var splitPath = (path) => path.split("/").filter(Boolean);
|
|
350
483
|
function compileTemplate(template) {
|
|
@@ -429,6 +562,7 @@ var headerGetter = {
|
|
|
429
562
|
function createApp(options = {}) {
|
|
430
563
|
const routes = [];
|
|
431
564
|
const health = options.health !== false;
|
|
565
|
+
const strip = typeof options.backdating === "object" && options.backdating.strip === true;
|
|
432
566
|
const settings = options.settings && isSettingsReader(options.settings) ? options.settings : createSettings({ app: options.name, ...options.settings ?? {} });
|
|
433
567
|
const app = {
|
|
434
568
|
settings,
|
|
@@ -444,7 +578,8 @@ function createApp(options = {}) {
|
|
|
444
578
|
template,
|
|
445
579
|
segments,
|
|
446
580
|
literals: segments.filter((s) => s.lit !== void 0).length,
|
|
447
|
-
handler
|
|
581
|
+
handler,
|
|
582
|
+
backdating: backdatingFields(options.backdating, method, template)
|
|
448
583
|
});
|
|
449
584
|
routes.sort((a, b) => b.literals - a.literals);
|
|
450
585
|
return app;
|
|
@@ -488,12 +623,12 @@ function createApp(options = {}) {
|
|
|
488
623
|
const reqSegs = splitPath(path);
|
|
489
624
|
let matched = null;
|
|
490
625
|
let pathExists = false;
|
|
491
|
-
for (const
|
|
492
|
-
const params = matchSegments(
|
|
626
|
+
for (const route2 of routes) {
|
|
627
|
+
const params = matchSegments(route2.segments, reqSegs);
|
|
493
628
|
if (!params) continue;
|
|
494
629
|
pathExists = true;
|
|
495
|
-
if (
|
|
496
|
-
matched = { route, params };
|
|
630
|
+
if (route2.method === method) {
|
|
631
|
+
matched = { route: route2, params };
|
|
497
632
|
break;
|
|
498
633
|
}
|
|
499
634
|
}
|
|
@@ -532,9 +667,23 @@ function createApp(options = {}) {
|
|
|
532
667
|
// The market rides on the request, so a market-scoped setting
|
|
533
668
|
// resolves for the market this call is for — no argument needed.
|
|
534
669
|
settings: (readOptions) => settings.get(c, readOptions),
|
|
535
|
-
settingsSnapshot: (readOptions) => settings.load(c, readOptions)
|
|
670
|
+
settingsSnapshot: (readOptions) => settings.load(c, readOptions),
|
|
671
|
+
now,
|
|
672
|
+
clock
|
|
536
673
|
};
|
|
537
|
-
|
|
674
|
+
const fields = matched.route.backdating;
|
|
675
|
+
if (!fields) return await matched.route.handler(c);
|
|
676
|
+
const at = await requestedMoment(c, fields);
|
|
677
|
+
if (at) {
|
|
678
|
+
span.setAttribute("revenexx.backdated_at", at);
|
|
679
|
+
if (strip) {
|
|
680
|
+
const body = { ...c.body };
|
|
681
|
+
for (const field of fields) delete body[field];
|
|
682
|
+
c.body = body;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
const route = matched.route;
|
|
686
|
+
return await runAt(at, () => route.handler(c));
|
|
538
687
|
} catch (err) {
|
|
539
688
|
if (isHttpError(err)) {
|
|
540
689
|
span.setAttribute("http.response.status_code", err.status);
|
|
@@ -780,7 +929,7 @@ function mountTrash(app, repo, opts) {
|
|
|
780
929
|
const base = opts.path.replace(/\/+$/, "");
|
|
781
930
|
const idParam = opts.idParam ?? "id";
|
|
782
931
|
const children = opts.children ?? [];
|
|
783
|
-
const
|
|
932
|
+
const now2 = opts.now ?? (() => /* @__PURE__ */ new Date());
|
|
784
933
|
const reasonColumn = opts.purgeReasonColumn === false ? null : opts.purgeReasonColumn ?? PURGE_REASON_COLUMN;
|
|
785
934
|
const load = async (id) => {
|
|
786
935
|
if (!id) throw refuse(400, `missing path parameter '${idParam}'`, "missing_parameter");
|
|
@@ -801,7 +950,7 @@ function mountTrash(app, repo, opts) {
|
|
|
801
950
|
}
|
|
802
951
|
const batch = (0, import_node_crypto.randomUUID)();
|
|
803
952
|
const fields = {
|
|
804
|
-
[TRASH_COLUMN]:
|
|
953
|
+
[TRASH_COLUMN]: now2().toISOString(),
|
|
805
954
|
deleted_by: caller.actor?.email ?? caller.actor?.user ?? null,
|
|
806
955
|
deleted_by_id: caller.actor?.user ?? caller.actor?.contact ?? null,
|
|
807
956
|
trash_batch: batch
|
|
@@ -874,7 +1023,7 @@ function mountTrash(app, repo, opts) {
|
|
|
874
1023
|
} catch (err) {
|
|
875
1024
|
return { skipped: true, reason: err instanceof Error ? err.message : String(err), purged: [], blocked: [] };
|
|
876
1025
|
}
|
|
877
|
-
const cutoff = new Date(
|
|
1026
|
+
const cutoff = new Date(now2().getTime() - retentionDays * 864e5).toISOString();
|
|
878
1027
|
const max = options.max ?? 500;
|
|
879
1028
|
const due = (await listAll(repo, { [TRASH_COLUMN]: { op: "lt", value: cutoff } })).slice(0, max);
|
|
880
1029
|
const result = { skipped: false, retentionDays, cutoff, purged: [], blocked: [] };
|
|
@@ -959,6 +1108,9 @@ function createGatewayCounter(options = {}) {
|
|
|
959
1108
|
0 && (module.exports = {
|
|
960
1109
|
ACTOR_HEADERS,
|
|
961
1110
|
AUDIT_SETTINGS_PATH,
|
|
1111
|
+
BACKDATING_FIELD,
|
|
1112
|
+
BACKDATING_INVALID,
|
|
1113
|
+
BACKDATING_SETTING,
|
|
962
1114
|
DEFAULT_GATEWAY_ENDPOINT,
|
|
963
1115
|
DEFAULT_SETTINGS_ERROR_TTL_MS,
|
|
964
1116
|
DEFAULT_SETTINGS_TIMEOUT_MS,
|
|
@@ -966,12 +1118,17 @@ function createGatewayCounter(options = {}) {
|
|
|
966
1118
|
DEFAULT_TRASH_RETENTION_DAYS,
|
|
967
1119
|
HttpError,
|
|
968
1120
|
MARKET_HEADERS,
|
|
1121
|
+
MAX_MONTHS_BACK,
|
|
969
1122
|
PURGE_REASON_COLUMN,
|
|
970
1123
|
RESERVED_LIST_QUERY_KEYS,
|
|
971
1124
|
TRASH_COLUMN,
|
|
972
1125
|
actorFrom,
|
|
973
1126
|
auditSettingsUrl,
|
|
1127
|
+
backdated,
|
|
1128
|
+
backdatingAllowed,
|
|
1129
|
+
backdatingDb,
|
|
974
1130
|
badRequest,
|
|
1131
|
+
clock,
|
|
975
1132
|
conflict,
|
|
976
1133
|
createApp,
|
|
977
1134
|
createGatewayCounter,
|
|
@@ -982,7 +1139,10 @@ function createGatewayCounter(options = {}) {
|
|
|
982
1139
|
mountCrud,
|
|
983
1140
|
mountTrash,
|
|
984
1141
|
notFound,
|
|
1142
|
+
now,
|
|
1143
|
+
parseBackdate,
|
|
985
1144
|
readTrashRetention,
|
|
986
|
-
refusalOf
|
|
1145
|
+
refusalOf,
|
|
1146
|
+
runAt
|
|
987
1147
|
});
|
|
988
1148
|
//# sourceMappingURL=index.cjs.map
|