@revenexx/integrations-node-devkit 0.6.0 → 0.8.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/cli.cjs CHANGED
@@ -352,10 +352,13 @@ var import_node_crypto = require("crypto");
352
352
  var DEV_TENANT_ID = "dev-tenant";
353
353
  var BLOB_DEFINITION_VERSION = "v0-draft";
354
354
  var EXECUTION_MODES = ["async_only", "sync_only", "caller_decides"];
355
- var STORE_SCHEMA_VERSION = 2;
355
+ var STORE_SCHEMA_VERSION = 3;
356
356
  function now() {
357
357
  return (/* @__PURE__ */ new Date()).toISOString();
358
358
  }
359
+ function stateKey(role, namespace, key) {
360
+ return `${role}\0${namespace}\0${key}`;
361
+ }
359
362
  var DevStore = class {
360
363
  /**
361
364
  * @param onChange fired after any mutation so a caller (the CLI) can persist
@@ -369,6 +372,8 @@ var DevStore = class {
369
372
  secrets = /* @__PURE__ */ new Map();
370
373
  workflows = /* @__PURE__ */ new Map();
371
374
  triggers = /* @__PURE__ */ new Map();
375
+ /** Keyed by `${role}\u0000${namespace}\u0000${key}` — a NUL occurs in no part. */
376
+ stateEntries = /* @__PURE__ */ new Map();
372
377
  nextWorkflowId = 1;
373
378
  /** (Re)wire the post-mutation callback — e.g. attach it only after seeding. */
374
379
  setOnMutation(onChange) {
@@ -567,6 +572,21 @@ var DevStore = class {
567
572
  return deleted;
568
573
  }
569
574
  // ------------------------------------------------------------- persistence
575
+ // ------------------------------------------------------------------ state
576
+ /** The stored entry for a role/namespace/key, or undefined when unknown. */
577
+ getStateEntry(role, namespace, key) {
578
+ return this.stateEntries.get(stateKey(role, namespace, key));
579
+ }
580
+ /** Every entry a namespace holds in one role — how a reverse mapping lookup is answered. */
581
+ listStateEntries(role, namespace) {
582
+ return [...this.stateEntries.values()].filter((e) => e.role === role && e.namespace === namespace);
583
+ }
584
+ putStateEntry(entry) {
585
+ const record = { ...entry, updatedAt: now() };
586
+ this.stateEntries.set(stateKey(entry.role, entry.namespace, entry.key), record);
587
+ this.onChange?.();
588
+ return record;
589
+ }
570
590
  toSnapshot() {
571
591
  return {
572
592
  schemaVersion: STORE_SCHEMA_VERSION,
@@ -574,6 +594,7 @@ var DevStore = class {
574
594
  secrets: [...this.secrets.values()],
575
595
  workflows: [...this.workflows.values()],
576
596
  triggers: [...this.triggers.values()],
597
+ stateEntries: [...this.stateEntries.values()],
577
598
  nextWorkflowId: this.nextWorkflowId
578
599
  };
579
600
  }
@@ -582,6 +603,7 @@ var DevStore = class {
582
603
  this.secrets = new Map(snapshot.secrets.map((s) => [s.key, s]));
583
604
  this.workflows = new Map(snapshot.workflows.map((w) => [w.id, w]));
584
605
  this.triggers = new Map(snapshot.triggers.map((t) => [t.id, t]));
606
+ this.stateEntries = new Map((snapshot.stateEntries ?? []).map((e) => [stateKey(e.role, e.namespace, e.key), e]));
585
607
  this.nextWorkflowId = snapshot.nextWorkflowId ?? Math.max(0, ...snapshot.workflows.map((w) => w.id)) + 1;
586
608
  }
587
609
  /**
@@ -603,6 +625,9 @@ var DevStore = class {
603
625
  for (const t of snapshot.triggers) {
604
626
  this.triggers.set(t.id, t);
605
627
  }
628
+ for (const e of snapshot.stateEntries ?? []) {
629
+ this.stateEntries.set(stateKey(e.role, e.namespace, e.key), e);
630
+ }
606
631
  this.nextWorkflowId = Math.max(this.nextWorkflowId, snapshot.nextWorkflowId ?? 0, ...snapshot.workflows.map((w) => w.id + 1));
607
632
  }
608
633
  };
@@ -692,7 +717,33 @@ async function loadSeedsFromDir(dir) {
692
717
  }
693
718
 
694
719
  // src/resolve.ts
720
+ var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
721
+
722
+ // src/state-rules.ts
695
723
  var import_integrations_node_sdk2 = require("@revenexx/integrations-node-sdk");
724
+ var DEFAULT_CLAIM_TTL_SECONDS = 604800;
725
+ var MIN_CLAIM_TTL_SECONDS = 1;
726
+ var MAX_CLAIM_TTL_SECONDS = 31536e3;
727
+ function assertTtlInRange(ttlSeconds) {
728
+ if (ttlSeconds === void 0) {
729
+ return;
730
+ }
731
+ if (!Number.isFinite(ttlSeconds) || ttlSeconds < MIN_CLAIM_TTL_SECONDS || ttlSeconds > MAX_CLAIM_TTL_SECONDS) {
732
+ throw new import_integrations_node_sdk2.NodeError("INVALID_TTL", `ttlSeconds must be between ${MIN_CLAIM_TTL_SECONDS} and ${MAX_CLAIM_TTL_SECONDS}, got ${String(ttlSeconds)}.`);
733
+ }
734
+ }
735
+ function assertNotRepointed(namespace, pairs, left, right) {
736
+ for (const [existingLeft, existingRight] of pairs) {
737
+ if (existingLeft === left && existingRight !== right) {
738
+ throw new import_integrations_node_sdk2.NodeError("MAPPING_CONFLICT", `'${left}' is already correlated with '${existingRight}' in namespace '${namespace}'.`);
739
+ }
740
+ if (existingRight === right && existingLeft !== left) {
741
+ throw new import_integrations_node_sdk2.NodeError("MAPPING_CONFLICT", `'${right}' is already correlated with '${existingLeft}' in namespace '${namespace}'.`);
742
+ }
743
+ }
744
+ }
745
+
746
+ // src/resolve.ts
696
747
  var noopLogger = {
697
748
  info() {
698
749
  },
@@ -701,20 +752,66 @@ var noopLogger = {
701
752
  error() {
702
753
  }
703
754
  };
755
+ function parseSemver(version) {
756
+ const [withoutBuild] = version.split("+");
757
+ const dash = withoutBuild.indexOf("-");
758
+ const core = (dash === -1 ? withoutBuild : withoutBuild.slice(0, dash)).split(".").map((n) => Number.parseInt(n, 10) || 0);
759
+ const pre = dash === -1 ? [] : withoutBuild.slice(dash + 1).split(".");
760
+ return { core, pre };
761
+ }
762
+ function comparePrerelease(a, b) {
763
+ if (a.length === 0 || b.length === 0) {
764
+ return (a.length === 0 ? 1 : 0) - (b.length === 0 ? 1 : 0);
765
+ }
766
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
767
+ const ia = a[i];
768
+ const ib = b[i];
769
+ if (ia === void 0 || ib === void 0) {
770
+ return ia === void 0 ? -1 : 1;
771
+ }
772
+ const na = /^\d+$/.test(ia) ? Number(ia) : null;
773
+ const nb = /^\d+$/.test(ib) ? Number(ib) : null;
774
+ if (na !== null && nb !== null) {
775
+ if (na !== nb) {
776
+ return na - nb;
777
+ }
778
+ continue;
779
+ }
780
+ if (na !== null || nb !== null) {
781
+ return na !== null ? -1 : 1;
782
+ }
783
+ if (ia !== ib) {
784
+ return ia < ib ? -1 : 1;
785
+ }
786
+ }
787
+ return 0;
788
+ }
704
789
  function compareSemver(a, b) {
705
- const pa = a.split(".").map((n) => Number.parseInt(n, 10) || 0);
706
- const pb = b.split(".").map((n) => Number.parseInt(n, 10) || 0);
790
+ const pa = parseSemver(a);
791
+ const pb = parseSemver(b);
707
792
  for (let i = 0; i < 3; i++) {
708
- const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
793
+ const diff = (pa.core[i] ?? 0) - (pb.core[i] ?? 0);
709
794
  if (diff !== 0) {
710
795
  return diff;
711
796
  }
712
797
  }
713
- return 0;
798
+ return comparePrerelease(pa.pre, pb.pre);
714
799
  }
715
800
  function nodeVersions(loaded, slug) {
716
801
  return loaded.nodes.filter((n) => n.description.slug === slug).map((n) => n.description.version).sort((a, b) => compareSemver(b, a));
717
802
  }
803
+ function listNodesNewestFirst(nodes) {
804
+ const bySlug = /* @__PURE__ */ new Map();
805
+ for (const node of nodes) {
806
+ const group = bySlug.get(node.slug);
807
+ if (group) {
808
+ group.push(node);
809
+ } else {
810
+ bySlug.set(node.slug, [node]);
811
+ }
812
+ }
813
+ return [...bySlug.values()].flatMap((group) => [...group].sort((a, b) => compareSemver(b.version, a.version)));
814
+ }
718
815
  function findNode(loaded, slug, version) {
719
816
  const matches = loaded.nodes.filter((n) => n.description.slug === slug);
720
817
  if (matches.length === 0) {
@@ -757,6 +854,68 @@ async function resolveCredentialInstance(loaded, store, credentialId) {
757
854
  const ctx = credentialContext(store, credentialId);
758
855
  return cred.resolve(ctx, instance.config, instance.durableCreds);
759
856
  }
857
+ function createRunState(store) {
858
+ const staged = [];
859
+ const stage = (entry) => {
860
+ staged.push(entry);
861
+ };
862
+ const state = {
863
+ mapping: {
864
+ async get(namespace, key, side = "left") {
865
+ if (side === "left") {
866
+ const entry = store.getStateEntry("mapping", namespace, key);
867
+ return entry ? String(entry.value) : null;
868
+ }
869
+ const match = store.listStateEntries("mapping", namespace).find((e) => String(e.value) === key);
870
+ return match ? match.key : null;
871
+ },
872
+ async put(namespace, left, right) {
873
+ assertNotRepointed(
874
+ namespace,
875
+ store.listStateEntries("mapping", namespace).map((e) => [e.key, String(e.value)]),
876
+ left,
877
+ right
878
+ );
879
+ store.putStateEntry({ namespace, role: "mapping", key: left, value: right });
880
+ }
881
+ },
882
+ cursor: {
883
+ async get(namespace, partitionKey = "") {
884
+ return store.getStateEntry("cursor", namespace, partitionKey)?.value;
885
+ },
886
+ async set(namespace, value, partitionKey = "") {
887
+ stage({ namespace, role: "cursor", key: partitionKey, value });
888
+ }
889
+ },
890
+ async claim(namespace, key, opts) {
891
+ assertTtlInRange(opts?.ttlSeconds);
892
+ const held = store.getStateEntry("dedupe", namespace, key);
893
+ if (held && Number(held.value) > Date.now()) {
894
+ return false;
895
+ }
896
+ const ttlSeconds = opts?.ttlSeconds ?? DEFAULT_CLAIM_TTL_SECONDS;
897
+ store.putStateEntry({ namespace, role: "dedupe", key, value: Date.now() + ttlSeconds * 1e3 });
898
+ return true;
899
+ },
900
+ digest: {
901
+ async unchanged(namespace, entityKey, digest) {
902
+ return store.getStateEntry("digest", namespace, entityKey)?.value === digest;
903
+ },
904
+ async set(namespace, entityKey, digest) {
905
+ stage({ namespace, role: "digest", key: entityKey, value: digest });
906
+ }
907
+ }
908
+ };
909
+ return {
910
+ state,
911
+ commit() {
912
+ for (const entry of staged) {
913
+ store.putStateEntry(entry);
914
+ }
915
+ staged.length = 0;
916
+ }
917
+ };
918
+ }
760
919
  function authorContext(loaded, store, config, locale) {
761
920
  return {
762
921
  signal: new AbortController().signal,
@@ -847,6 +1006,10 @@ function validateField(field, value, errors) {
847
1006
  switch (field.type) {
848
1007
  case "string":
849
1008
  case "secret-ref":
1009
+ // A `state-ref` carries the chosen namespace NAME, so a string is all there
1010
+ // is to check here: which names exist is the workflow's declaration, and
1011
+ // nothing declares one locally (see the README's fidelity caveats).
1012
+ case "state-ref":
850
1013
  case "expression":
851
1014
  if (typeof value !== "string") {
852
1015
  push("Expected a string.");
@@ -930,9 +1093,12 @@ async function validateNodeConfig(loaded, store, input) {
930
1093
  if (field.type === "dynamic-schema") {
931
1094
  continue;
932
1095
  }
1096
+ if (!(0, import_integrations_node_sdk3.settingApplies)(field, config)) {
1097
+ continue;
1098
+ }
933
1099
  validateField(field, config[field.key], errors);
934
1100
  }
935
- if (fields.some((f) => f.type === "dynamic-schema") && node.resolveConfigSchema) {
1101
+ if (fields.some((f) => f.type === "dynamic-schema" && (0, import_integrations_node_sdk3.settingApplies)(f, config)) && node.resolveConfigSchema) {
936
1102
  const ctx = authorContext(loaded, store, config, input.locale);
937
1103
  let children;
938
1104
  try {
@@ -944,6 +1110,9 @@ async function validateNodeConfig(loaded, store, input) {
944
1110
  throw new DevApiError(502, `Node resolve failed: ${err.message}`);
945
1111
  }
946
1112
  for (const child of children) {
1113
+ if (!(0, import_integrations_node_sdk3.settingApplies)(child, config)) {
1114
+ continue;
1115
+ }
947
1116
  validateField(child, config[child.key], errors);
948
1117
  }
949
1118
  }
@@ -960,6 +1129,7 @@ async function executeNodeTest(loaded, store, input) {
960
1129
  logs.push(meta === void 0 ? { level, message } : { level, message, meta });
961
1130
  };
962
1131
  const timeoutMs = Math.max(MIN_TIMEOUT_MS, input.timeoutMs ?? DEFAULT_TIMEOUT_MS);
1132
+ const run2 = createRunState(store);
963
1133
  const controller = new AbortController();
964
1134
  let timer;
965
1135
  const expired = new Promise((_resolve, reject) => {
@@ -984,10 +1154,12 @@ async function executeNodeTest(loaded, store, input) {
984
1154
  async get(id) {
985
1155
  return (await resolveCredentialInstance(loaded, store, id)).credentials;
986
1156
  }
987
- }
1157
+ },
1158
+ state: run2.state
988
1159
  };
989
1160
  try {
990
1161
  const result = await Promise.race([node.execute(ctx, { ...input.config, ...input.inputs }), expired]);
1162
+ run2.commit();
991
1163
  return { outputs: result.outputs ?? {}, branch: result.branch ?? null, logs };
992
1164
  } catch (err) {
993
1165
  if (err instanceof DevApiError) {
@@ -1017,7 +1189,7 @@ async function buildOAuthAuthorizeUrl(loaded, store, credentialId, params) {
1017
1189
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
1018
1190
  }
1019
1191
  const cred = findCredential(loaded, instance.credentialTypeSlug);
1020
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1192
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
1021
1193
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
1022
1194
  }
1023
1195
  return cred.buildAuthorizeUrl(credentialContext(store, credentialId), instance.config, params);
@@ -1028,7 +1200,7 @@ async function exchangeOAuthCode(loaded, store, credentialId, params) {
1028
1200
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
1029
1201
  }
1030
1202
  const cred = findCredential(loaded, instance.credentialTypeSlug);
1031
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1203
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
1032
1204
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
1033
1205
  }
1034
1206
  const out = await cred.exchangeCode(credentialContext(store, credentialId), instance.config, params);
@@ -1083,7 +1255,7 @@ async function readJson(req) {
1083
1255
  }
1084
1256
 
1085
1257
  // src/projections.ts
1086
- var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
1258
+ var import_integrations_node_sdk4 = require("@revenexx/integrations-node-sdk");
1087
1259
  function hasDynamicOptions(desc) {
1088
1260
  return (desc.config ?? []).some((f) => f.dynamic === true);
1089
1261
  }
@@ -1104,7 +1276,7 @@ function nodeToApi(desc, ctx) {
1104
1276
  // come from the developer's working tree, so the package identity is read from
1105
1277
  // their package.json and the timestamps are when the mock loaded the entry.
1106
1278
  package: ctx?.packageInfo ?? null,
1107
- manifest_version: import_integrations_node_sdk3.MANIFEST_VERSION,
1279
+ manifest_version: import_integrations_node_sdk4.MANIFEST_VERSION,
1108
1280
  manifest: desc,
1109
1281
  has_dynamic_options: hasDynamicOptions(desc),
1110
1282
  has_dynamic_schema: hasDynamicSchema(desc),
@@ -1220,13 +1392,24 @@ function createRequestListener(deps) {
1220
1392
  handle(req, res, deps).catch((err) => sendError(res, err));
1221
1393
  };
1222
1394
  }
1395
+ function requestOrigin(req) {
1396
+ const host = req.headers.host;
1397
+ if (!host) {
1398
+ return "http://localhost";
1399
+ }
1400
+ try {
1401
+ return new URL(`http://${host}`).origin;
1402
+ } catch {
1403
+ return "http://localhost";
1404
+ }
1405
+ }
1223
1406
  async function handle(req, res, deps) {
1224
1407
  const method = (req.method ?? "GET").toUpperCase();
1225
1408
  if (method === "OPTIONS") {
1226
1409
  sendJson(res, 204, {});
1227
1410
  return;
1228
1411
  }
1229
- const url = new URL(req.url ?? "/", "http://localhost");
1412
+ const url = new URL(req.url ?? "/", requestOrigin(req));
1230
1413
  let pathname = url.pathname;
1231
1414
  if (pathname.startsWith("/api/v1")) {
1232
1415
  pathname = pathname.slice("/api/v1".length);
@@ -1287,7 +1470,7 @@ function handleSchemas(res, deps, seg) {
1287
1470
  }
1288
1471
  async function handleNodes(req, res, method, loaded, store, seg) {
1289
1472
  if (seg.length === 1 && method === "GET") {
1290
- return void sendJson(res, 200, { data: loaded.manifest.nodes.map((n) => nodeToApi(n, nodeApiContext(loaded))) });
1473
+ return void sendJson(res, 200, { data: listNodesNewestFirst(loaded.manifest.nodes).map((n) => nodeToApi(n, nodeApiContext(loaded))) });
1291
1474
  }
1292
1475
  const slug = seg[1];
1293
1476
  if (!slug) {
@@ -1352,7 +1535,8 @@ function nodeApiContext(loaded) {
1352
1535
  return { packageInfo: loaded.packageInfo, loadedAt: loaded.loadedAt };
1353
1536
  }
1354
1537
  function findManifestNode(loaded, slug, version) {
1355
- const node = loaded.manifest.nodes.find((n) => n.slug === slug && (version === "latest" || n.version === version));
1538
+ const matches = loaded.manifest.nodes.filter((n) => n.slug === slug);
1539
+ const node = version === "latest" ? listNodesNewestFirst(matches)[0] : matches.find((n) => n.version === version);
1356
1540
  if (!node) {
1357
1541
  throw new DevApiError(404, `Node '${slug}@${version}' not found.`);
1358
1542
  }
@@ -1430,7 +1614,7 @@ async function handleCredentials(req, res, method, loaded, store, seg, url) {
1430
1614
  });
1431
1615
  }
1432
1616
  if (seg[2] === "oauth" && seg[3] === "authorize-url" && method === "POST") {
1433
- const redirectUri = "http://localhost/api/v1/credentials/oauth/callback";
1617
+ const redirectUri = `${url.origin}/api/v1/credentials/oauth/callback`;
1434
1618
  const result = await buildOAuthAuthorizeUrl(loaded, store, id, { redirectUri, state: id });
1435
1619
  return void sendJson(res, 200, { authorize_url: result.authorizeUrl });
1436
1620
  }