@revenexx/app-sdk 1.4.0 → 1.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/dist/index.js CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  refusalOf,
21
21
  runAt,
22
22
  toParams
23
- } from "./chunk-D6QST6NV.js";
23
+ } from "./chunk-FMHCS3ZH.js";
24
24
 
25
25
  // src/adapters/mock.ts
26
26
  import { randomUUID } from "crypto";
@@ -118,26 +118,42 @@ var ACTOR_HEADERS = {
118
118
  trust: "x-revenexx-actor-trust",
119
119
  contact: "x-revenexx-contact",
120
120
  organization: "x-revenexx-organization",
121
- permissions: "x-revenexx-permissions"
121
+ permissions: "x-revenexx-permissions",
122
+ /** `contact` or `rep` — the kind of person the gateway resolved. */
123
+ caller: "x-revenexx-caller",
124
+ /** The rep's code, beside `caller: rep`, percent-encoded as UTF-8 (RFC 3986). */
125
+ rep: "x-revenexx-rep"
122
126
  };
123
127
  var nullIfEmpty = (v) => v.trim() === "" ? null : v.trim();
128
+ var decodeRep = (v) => {
129
+ if (v === null) return null;
130
+ try {
131
+ return nullIfEmpty(decodeURIComponent(v));
132
+ } catch {
133
+ return null;
134
+ }
135
+ };
124
136
  function actorFrom(header) {
125
137
  const read = (name) => {
126
138
  const v = header(name);
127
139
  return typeof v === "string" ? v : "";
128
140
  };
129
141
  const user = nullIfEmpty(read(ACTOR_HEADERS.user));
130
- const contact = nullIfEmpty(read(ACTOR_HEADERS.contact));
131
- const permissions = read(ACTOR_HEADERS.permissions).split(",").map((p) => p.trim()).filter(Boolean);
142
+ const isRepCall = read(ACTOR_HEADERS.caller).trim().toLowerCase() === "rep";
143
+ const rep = isRepCall ? decodeRep(nullIfEmpty(read(ACTOR_HEADERS.rep))) : null;
144
+ const contact = isRepCall ? null : nullIfEmpty(read(ACTOR_HEADERS.contact));
145
+ const organization = isRepCall ? null : nullIfEmpty(read(ACTOR_HEADERS.organization));
146
+ const permissions = isRepCall && rep === null ? [] : read(ACTOR_HEADERS.permissions).split(",").map((p) => p.trim()).filter(Boolean);
132
147
  const raw = read(ACTOR_HEADERS.trust).trim().toLowerCase();
133
- const trust = user === null && contact === null ? "none" : user !== null && raw === "authenticated" ? "authenticated" : "asserted";
134
- const plane = contact !== null ? "contact" : user !== null ? "operator" : "system";
148
+ const trust = user === null && contact === null && rep === null ? "none" : !isRepCall && user !== null && raw === "authenticated" ? "authenticated" : "asserted";
149
+ const plane = rep !== null ? "rep" : contact !== null ? "contact" : user !== null ? "operator" : "system";
135
150
  const held = new Set(permissions);
136
151
  return {
137
152
  user,
138
153
  email: nullIfEmpty(read(ACTOR_HEADERS.email)),
139
154
  contact,
140
- organization: nullIfEmpty(read(ACTOR_HEADERS.organization)),
155
+ organization,
156
+ rep,
141
157
  permissions,
142
158
  trust,
143
159
  plane,
@@ -164,6 +180,9 @@ var DEFAULT_GATEWAY_ENDPOINT = "https://api.revenexx.com";
164
180
  var DEFAULT_SETTINGS_TTL_MS = 6e4;
165
181
  var DEFAULT_SETTINGS_ERROR_TTL_MS = 5e3;
166
182
  var DEFAULT_SETTINGS_TIMEOUT_MS = 2e3;
183
+ function stringList(value) {
184
+ return Array.isArray(value) ? value.filter((k) => typeof k === "string") : [];
185
+ }
167
186
  function headerOf(source, name) {
168
187
  if (!source) return "";
169
188
  if (typeof source.header === "function") {
@@ -217,6 +236,7 @@ function createSettings(options = {}) {
217
236
  market,
218
237
  settings: mergeSettings(defaults, void 0),
219
238
  masked: [],
239
+ stored: [],
220
240
  resolved: false,
221
241
  error
222
242
  });
@@ -250,10 +270,81 @@ function createSettings(options = {}) {
250
270
  tenant,
251
271
  market,
252
272
  settings: mergeSettings(defaults, body.settings),
253
- masked: Array.isArray(body.masked) ? body.masked.filter((k) => typeof k === "string") : [],
273
+ masked: stringList(body.masked),
274
+ stored: stringList(body.stored),
254
275
  resolved: true
255
276
  };
256
277
  }
278
+ const forgetTenant = (tenant, app) => {
279
+ const prefix = `${tenant}\0${app}\0`;
280
+ for (const key of [...cache.keys()]) if (key.startsWith(prefix)) cache.delete(key);
281
+ for (const key of [...lastGood.keys()]) if (key.startsWith(prefix)) lastGood.delete(key);
282
+ };
283
+ async function set(source, values, writeOptions = {}) {
284
+ const app = appName();
285
+ const tenant = (source?.tenant ?? "") || headerOf(source, "x-revenexx-tenant");
286
+ const refused = (status, error) => ({
287
+ ok: false,
288
+ status,
289
+ written: [],
290
+ kept: [],
291
+ ignored: [],
292
+ error
293
+ });
294
+ if (!app) return refused(0, "no app name (pass `app` or set REVENEXX_APP_NAME)");
295
+ if (!tenant) return refused(0, "no tenant on the request");
296
+ const appContext = headerOf(source, "x-revenexx-app-context");
297
+ if (!appContext) {
298
+ return refused(0, "no app context on the request \u2014 settings are written from an HTTP route, not an event or schedule run");
299
+ }
300
+ if (!values || typeof values !== "object" || Array.isArray(values)) return refused(0, "values must be an object");
301
+ const f = options.fetchImpl ?? globalThis.fetch;
302
+ if (typeof f !== "function") return refused(0, "settings: no fetch available (Node >=18 or pass fetchImpl)");
303
+ const token = (source?.contextToken ?? "") || headerOf(source, "x-revenexx-context");
304
+ const headers = {
305
+ Accept: "application/json",
306
+ "Content-Type": "application/json",
307
+ "X-Revenexx-Tenant": tenant,
308
+ "X-Revenexx-App-Context": appContext
309
+ };
310
+ if (token) headers.Authorization = `Bearer ${token}`;
311
+ try {
312
+ const signal = timeoutSignal(timeoutMs);
313
+ const res = await f(`${endpoint}/v1/settings/apps/${encodeURIComponent(app)}`, {
314
+ method: "PATCH",
315
+ headers,
316
+ body: JSON.stringify({ data: values, if_absent: writeOptions.ifAbsent === true }),
317
+ ...signal ? { signal } : {}
318
+ });
319
+ const text = await res.text();
320
+ let body = {};
321
+ try {
322
+ body = text ? JSON.parse(text) : {};
323
+ } catch {
324
+ body = {};
325
+ }
326
+ if (!res.ok) {
327
+ const reason = typeof body.error === "string" ? body.error : typeof body.message === "string" ? body.message : "";
328
+ const err = new Error(`settings: write ${res.status} for ${app}${reason ? ` \u2014 ${reason}` : ""}`);
329
+ err.status = res.status;
330
+ report(err, { app, tenant, market: null });
331
+ return refused(res.status, err.message);
332
+ }
333
+ const result = {
334
+ ok: true,
335
+ status: res.status,
336
+ written: stringList(body.written),
337
+ kept: stringList(body.kept),
338
+ ignored: stringList(body.ignored)
339
+ };
340
+ if (result.written.length > 0) forgetTenant(tenant, app);
341
+ return result;
342
+ } catch (err) {
343
+ const error = err instanceof Error ? err : new Error(String(err));
344
+ report(error, { app, tenant, market: null });
345
+ return refused(0, error.message);
346
+ }
347
+ }
257
348
  async function load(source, readOptions = {}) {
258
349
  const app = appName();
259
350
  const tenant = (source?.tenant ?? "") || headerOf(source, "x-revenexx-tenant");
@@ -283,6 +374,7 @@ function createSettings(options = {}) {
283
374
  }
284
375
  return {
285
376
  load,
377
+ set,
286
378
  async get(source, readOptions) {
287
379
  return (await load(source, readOptions)).settings;
288
380
  },
@@ -668,6 +760,7 @@ function createApp(options = {}) {
668
760
  // resolves for the market this call is for — no argument needed.
669
761
  settings: (readOptions) => settings.get(c, readOptions),
670
762
  settingsSnapshot: (readOptions) => settings.load(c, readOptions),
763
+ setSettings: (values, writeOptions) => settings.set(c, values, writeOptions),
671
764
  now,
672
765
  clock
673
766
  };