@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/dist/index.cjs CHANGED
@@ -26,8 +26,12 @@ __export(src_exports, {
26
26
  DEFAULT_SETTINGS_TIMEOUT_MS: () => DEFAULT_SETTINGS_TIMEOUT_MS,
27
27
  DEFAULT_SETTINGS_TTL_MS: () => DEFAULT_SETTINGS_TTL_MS,
28
28
  HttpError: () => HttpError,
29
+ MAX_MONTHS_BACK: () => MAX_MONTHS_BACK,
29
30
  adapterKinds: () => adapterKinds,
30
31
  applyDefaults: () => applyDefaults,
32
+ backdated: () => backdated,
33
+ backdatingDb: () => backdatingDb,
34
+ clock: () => clock,
31
35
  createAdapter: () => createAdapter,
32
36
  createClient: () => createClient,
33
37
  createMockAdapter: () => createMockAdapter,
@@ -36,9 +40,12 @@ __export(src_exports, {
36
40
  createSettings: () => createSettings,
37
41
  isHttpError: () => isHttpError,
38
42
  matches: () => matches,
43
+ now: () => now,
39
44
  opsFromAccess: () => opsFromAccess,
45
+ parseBackdate: () => parseBackdate,
40
46
  refusalOf: () => refusalOf,
41
47
  registerAdapter: () => registerAdapter,
48
+ runAt: () => runAt,
42
49
  toParams: () => toParams
43
50
  });
44
51
  module.exports = __toCommonJS(src_exports);
@@ -397,12 +404,14 @@ function createRemoteAdapter(config) {
397
404
  const base = String(config.endpoint ?? "").replace(/\/$/, "");
398
405
  if (!base) throw new Error("remote adapter: endpoint is required");
399
406
  const audit = auditEnvelope(config.audit);
407
+ const note = auditNote(config.auditNote);
400
408
  const headers = (extra) => {
401
409
  const h = { "Content-Type": "application/json", Accept: "application/json", ...extra };
402
410
  if (config.token) h.Authorization = `Bearer ${config.token}`;
403
411
  if (config.tenant) h["X-Revenexx-Tenant"] = config.tenant;
404
412
  if (config.market) h["X-Revenexx-Market"] = config.market;
405
413
  if (audit) h["X-Revenexx-Audit"] = audit;
414
+ if (note) h["X-Revenexx-Audit-Note"] = note;
406
415
  return h;
407
416
  };
408
417
  async function send(method, path, opts = {}) {
@@ -544,6 +553,9 @@ function createRemoteAdapter(config) {
544
553
  function auditEnvelope(value) {
545
554
  return typeof value === "string" && /^[\x21-\x7e]{1,4096}$/.test(value) ? value : "";
546
555
  }
556
+ function auditNote(value) {
557
+ return typeof value === "string" && /^[\x20-\x7e]{1,8192}$/.test(value) ? value : "";
558
+ }
547
559
 
548
560
  // src/adapters/runtime.ts
549
561
  var DEFAULT_DATA_ENDPOINT = "https://apps.api.revenexx.com";
@@ -564,7 +576,11 @@ function createRuntimeAdapter(config = {}) {
564
576
  // The record-history envelope of THIS request, passed through unchanged.
565
577
  // Bound to the request's token and short-lived, so there is no env
566
578
  // fallback: an App without one is attributed to the token's subject.
567
- audit: get("x-revenexx-audit")
579
+ audit: get("x-revenexx-audit"),
580
+ // What the change is and why (the Cockpit sends it on a rollback). Like the
581
+ // envelope it belongs to THIS request, so there is no env fallback; unlike
582
+ // it, it names nobody, so an App may describe its own change instead.
583
+ auditNote: config.auditNote ?? get("x-revenexx-audit-note")
568
584
  });
569
585
  return { ...remote, kind: "runtime" };
570
586
  }
@@ -823,11 +839,11 @@ function createSettings(options = {}) {
823
839
  if (!app) return fallback("", tenant, market, "no app name (pass `app` or set REVENEXX_APP_NAME)");
824
840
  if (!tenant) return fallback(app, "", market, "no tenant on the request");
825
841
  const key = `${tenant}\0${app}\0${market ?? ""}`;
826
- const now = Date.now();
842
+ const now2 = Date.now();
827
843
  const hit = cache.get(key);
828
- if (hit && now - hit.at < hit.ttl) return hit.promise;
844
+ if (hit && now2 - hit.at < hit.ttl) return hit.promise;
829
845
  const token = (source?.contextToken ?? "") || headerOf(source, "x-revenexx-context");
830
- const entry = { at: now, ttl: ttlMs, promise: void 0 };
846
+ const entry = { at: now2, ttl: ttlMs, promise: void 0 };
831
847
  entry.promise = request(app, tenant, token, market).then((snapshot) => {
832
848
  lastGood.set(key, snapshot);
833
849
  return snapshot;
@@ -854,6 +870,78 @@ function createSettings(options = {}) {
854
870
  }
855
871
  };
856
872
  }
873
+
874
+ // src/backdating.ts
875
+ var import_node_async_hooks = require("async_hooks");
876
+ var MAX_MONTHS_BACK = 24;
877
+ var BACKDATING_INVALID = "backdating_invalid";
878
+ var STORE_KEY = /* @__PURE__ */ Symbol.for("@revenexx/app-sdk/backdating-clock");
879
+ function clockStorage() {
880
+ const g = globalThis;
881
+ let storage = g[STORE_KEY];
882
+ if (!storage) {
883
+ storage = new import_node_async_hooks.AsyncLocalStorage();
884
+ g[STORE_KEY] = storage;
885
+ }
886
+ return storage;
887
+ }
888
+ var CLOCK = clockStorage();
889
+ var now = () => CLOCK.getStore()?.at ?? (/* @__PURE__ */ new Date()).toISOString();
890
+ var clock = () => new Date(now());
891
+ var backdated = () => Boolean(CLOCK.getStore()?.at);
892
+ function runAt(at, fn) {
893
+ return at ? CLOCK.run({ at }, fn) : fn();
894
+ }
895
+ 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})$/;
896
+ var invalid = (field, message) => new HttpError(400, `'${field}' ${message}`, { field }, { code: BACKDATING_INVALID });
897
+ function parseBackdate(field, value, reference = /* @__PURE__ */ new Date()) {
898
+ const text = typeof value === "string" ? value.trim() : "";
899
+ if (!ISO_DATE_TIME.test(text)) throw invalid(field, "must be an ISO 8601 date-time with a time zone");
900
+ const at = new Date(text);
901
+ if (Number.isNaN(at.getTime())) throw invalid(field, "must be an ISO 8601 date-time with a time zone");
902
+ if (at.getTime() > reference.getTime()) throw invalid(field, "must not be in the future");
903
+ const earliest = new Date(reference.getTime());
904
+ earliest.setUTCMonth(earliest.getUTCMonth() - MAX_MONTHS_BACK);
905
+ if (at.getTime() < earliest.getTime()) {
906
+ throw invalid(field, `must not be more than ${MAX_MONTHS_BACK} months back`);
907
+ }
908
+ return at.toISOString();
909
+ }
910
+ function backdatingDb(db, entities) {
911
+ const wrapped = /* @__PURE__ */ new Map();
912
+ return new Proxy(db, {
913
+ get(target, prop, receiver) {
914
+ const value = Reflect.get(target, prop, receiver);
915
+ if (typeof prop !== "string") return value;
916
+ const columns = entities?.[prop]?.columns;
917
+ if (!columns || !("created_at" in columns) || !value || typeof value !== "object") return value;
918
+ const repo = value;
919
+ if (typeof repo.create !== "function") return value;
920
+ if (!wrapped.has(prop)) {
921
+ wrapped.set(
922
+ prop,
923
+ new Proxy(repo, {
924
+ get(r, key, rr) {
925
+ const member = Reflect.get(r, key, rr);
926
+ if (key !== "create" || typeof member !== "function") return member;
927
+ return (data, ...rest) => {
928
+ if (!backdated() || !data || typeof data !== "object" || Array.isArray(data)) {
929
+ return member.call(r, data, ...rest);
930
+ }
931
+ const at = now();
932
+ const row = { ...data };
933
+ if (row.created_at === void 0) row.created_at = at;
934
+ if ("updated_at" in columns && row.updated_at === void 0) row.updated_at = at;
935
+ return member.call(r, row, ...rest);
936
+ };
937
+ }
938
+ })
939
+ );
940
+ }
941
+ return wrapped.get(prop);
942
+ }
943
+ });
944
+ }
857
945
  // Annotate the CommonJS export names for ESM import in node:
858
946
  0 && (module.exports = {
859
947
  DEFAULT_DATA_ENDPOINT,
@@ -862,8 +950,12 @@ function createSettings(options = {}) {
862
950
  DEFAULT_SETTINGS_TIMEOUT_MS,
863
951
  DEFAULT_SETTINGS_TTL_MS,
864
952
  HttpError,
953
+ MAX_MONTHS_BACK,
865
954
  adapterKinds,
866
955
  applyDefaults,
956
+ backdated,
957
+ backdatingDb,
958
+ clock,
867
959
  createAdapter,
868
960
  createClient,
869
961
  createMockAdapter,
@@ -872,9 +964,12 @@ function createSettings(options = {}) {
872
964
  createSettings,
873
965
  isHttpError,
874
966
  matches,
967
+ now,
875
968
  opsFromAccess,
969
+ parseBackdate,
876
970
  refusalOf,
877
971
  registerAdapter,
972
+ runAt,
878
973
  toParams
879
974
  });
880
975
  //# sourceMappingURL=index.cjs.map