@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/index.cjs CHANGED
@@ -47,6 +47,7 @@ __export(index_exports, {
47
47
  findNode: () => findNode,
48
48
  importFresh: () => importFresh,
49
49
  interpolateEnv: () => interpolateEnv,
50
+ listNodesNewestFirst: () => listNodesNewestFirst,
50
51
  loadEnvFile: () => loadEnvFile,
51
52
  loadPackageFromEntry: () => loadPackageFromEntry,
52
53
  loadSeedsFromDir: () => loadSeedsFromDir,
@@ -215,10 +216,13 @@ var import_node_crypto = require("crypto");
215
216
  var DEV_TENANT_ID = "dev-tenant";
216
217
  var BLOB_DEFINITION_VERSION = "v0-draft";
217
218
  var EXECUTION_MODES = ["async_only", "sync_only", "caller_decides"];
218
- var STORE_SCHEMA_VERSION = 2;
219
+ var STORE_SCHEMA_VERSION = 3;
219
220
  function now() {
220
221
  return (/* @__PURE__ */ new Date()).toISOString();
221
222
  }
223
+ function stateKey(role, namespace, key) {
224
+ return `${role}\0${namespace}\0${key}`;
225
+ }
222
226
  var DevStore = class {
223
227
  /**
224
228
  * @param onChange fired after any mutation so a caller (the CLI) can persist
@@ -232,6 +236,8 @@ var DevStore = class {
232
236
  secrets = /* @__PURE__ */ new Map();
233
237
  workflows = /* @__PURE__ */ new Map();
234
238
  triggers = /* @__PURE__ */ new Map();
239
+ /** Keyed by `${role}\u0000${namespace}\u0000${key}` — a NUL occurs in no part. */
240
+ stateEntries = /* @__PURE__ */ new Map();
235
241
  nextWorkflowId = 1;
236
242
  /** (Re)wire the post-mutation callback — e.g. attach it only after seeding. */
237
243
  setOnMutation(onChange) {
@@ -430,6 +436,21 @@ var DevStore = class {
430
436
  return deleted;
431
437
  }
432
438
  // ------------------------------------------------------------- persistence
439
+ // ------------------------------------------------------------------ state
440
+ /** The stored entry for a role/namespace/key, or undefined when unknown. */
441
+ getStateEntry(role, namespace, key) {
442
+ return this.stateEntries.get(stateKey(role, namespace, key));
443
+ }
444
+ /** Every entry a namespace holds in one role — how a reverse mapping lookup is answered. */
445
+ listStateEntries(role, namespace) {
446
+ return [...this.stateEntries.values()].filter((e) => e.role === role && e.namespace === namespace);
447
+ }
448
+ putStateEntry(entry) {
449
+ const record = { ...entry, updatedAt: now() };
450
+ this.stateEntries.set(stateKey(entry.role, entry.namespace, entry.key), record);
451
+ this.onChange?.();
452
+ return record;
453
+ }
433
454
  toSnapshot() {
434
455
  return {
435
456
  schemaVersion: STORE_SCHEMA_VERSION,
@@ -437,6 +458,7 @@ var DevStore = class {
437
458
  secrets: [...this.secrets.values()],
438
459
  workflows: [...this.workflows.values()],
439
460
  triggers: [...this.triggers.values()],
461
+ stateEntries: [...this.stateEntries.values()],
440
462
  nextWorkflowId: this.nextWorkflowId
441
463
  };
442
464
  }
@@ -445,6 +467,7 @@ var DevStore = class {
445
467
  this.secrets = new Map(snapshot.secrets.map((s) => [s.key, s]));
446
468
  this.workflows = new Map(snapshot.workflows.map((w) => [w.id, w]));
447
469
  this.triggers = new Map(snapshot.triggers.map((t) => [t.id, t]));
470
+ this.stateEntries = new Map((snapshot.stateEntries ?? []).map((e) => [stateKey(e.role, e.namespace, e.key), e]));
448
471
  this.nextWorkflowId = snapshot.nextWorkflowId ?? Math.max(0, ...snapshot.workflows.map((w) => w.id)) + 1;
449
472
  }
450
473
  /**
@@ -466,6 +489,9 @@ var DevStore = class {
466
489
  for (const t of snapshot.triggers) {
467
490
  this.triggers.set(t.id, t);
468
491
  }
492
+ for (const e of snapshot.stateEntries ?? []) {
493
+ this.stateEntries.set(stateKey(e.role, e.namespace, e.key), e);
494
+ }
469
495
  this.nextWorkflowId = Math.max(this.nextWorkflowId, snapshot.nextWorkflowId ?? 0, ...snapshot.workflows.map((w) => w.id + 1));
470
496
  }
471
497
  };
@@ -555,7 +581,33 @@ async function loadSeedsFromDir(dir) {
555
581
  }
556
582
 
557
583
  // src/resolve.ts
584
+ var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
585
+
586
+ // src/state-rules.ts
558
587
  var import_integrations_node_sdk2 = require("@revenexx/integrations-node-sdk");
588
+ var DEFAULT_CLAIM_TTL_SECONDS = 604800;
589
+ var MIN_CLAIM_TTL_SECONDS = 1;
590
+ var MAX_CLAIM_TTL_SECONDS = 31536e3;
591
+ function assertTtlInRange(ttlSeconds) {
592
+ if (ttlSeconds === void 0) {
593
+ return;
594
+ }
595
+ if (!Number.isFinite(ttlSeconds) || ttlSeconds < MIN_CLAIM_TTL_SECONDS || ttlSeconds > MAX_CLAIM_TTL_SECONDS) {
596
+ 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)}.`);
597
+ }
598
+ }
599
+ function assertNotRepointed(namespace, pairs, left, right) {
600
+ for (const [existingLeft, existingRight] of pairs) {
601
+ if (existingLeft === left && existingRight !== right) {
602
+ throw new import_integrations_node_sdk2.NodeError("MAPPING_CONFLICT", `'${left}' is already correlated with '${existingRight}' in namespace '${namespace}'.`);
603
+ }
604
+ if (existingRight === right && existingLeft !== left) {
605
+ throw new import_integrations_node_sdk2.NodeError("MAPPING_CONFLICT", `'${right}' is already correlated with '${existingLeft}' in namespace '${namespace}'.`);
606
+ }
607
+ }
608
+ }
609
+
610
+ // src/resolve.ts
559
611
  var noopLogger = {
560
612
  info() {
561
613
  },
@@ -564,20 +616,66 @@ var noopLogger = {
564
616
  error() {
565
617
  }
566
618
  };
619
+ function parseSemver(version) {
620
+ const [withoutBuild] = version.split("+");
621
+ const dash = withoutBuild.indexOf("-");
622
+ const core = (dash === -1 ? withoutBuild : withoutBuild.slice(0, dash)).split(".").map((n) => Number.parseInt(n, 10) || 0);
623
+ const pre = dash === -1 ? [] : withoutBuild.slice(dash + 1).split(".");
624
+ return { core, pre };
625
+ }
626
+ function comparePrerelease(a, b) {
627
+ if (a.length === 0 || b.length === 0) {
628
+ return (a.length === 0 ? 1 : 0) - (b.length === 0 ? 1 : 0);
629
+ }
630
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
631
+ const ia = a[i];
632
+ const ib = b[i];
633
+ if (ia === void 0 || ib === void 0) {
634
+ return ia === void 0 ? -1 : 1;
635
+ }
636
+ const na = /^\d+$/.test(ia) ? Number(ia) : null;
637
+ const nb = /^\d+$/.test(ib) ? Number(ib) : null;
638
+ if (na !== null && nb !== null) {
639
+ if (na !== nb) {
640
+ return na - nb;
641
+ }
642
+ continue;
643
+ }
644
+ if (na !== null || nb !== null) {
645
+ return na !== null ? -1 : 1;
646
+ }
647
+ if (ia !== ib) {
648
+ return ia < ib ? -1 : 1;
649
+ }
650
+ }
651
+ return 0;
652
+ }
567
653
  function compareSemver(a, b) {
568
- const pa = a.split(".").map((n) => Number.parseInt(n, 10) || 0);
569
- const pb = b.split(".").map((n) => Number.parseInt(n, 10) || 0);
654
+ const pa = parseSemver(a);
655
+ const pb = parseSemver(b);
570
656
  for (let i = 0; i < 3; i++) {
571
- const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
657
+ const diff = (pa.core[i] ?? 0) - (pb.core[i] ?? 0);
572
658
  if (diff !== 0) {
573
659
  return diff;
574
660
  }
575
661
  }
576
- return 0;
662
+ return comparePrerelease(pa.pre, pb.pre);
577
663
  }
578
664
  function nodeVersions(loaded, slug) {
579
665
  return loaded.nodes.filter((n) => n.description.slug === slug).map((n) => n.description.version).sort((a, b) => compareSemver(b, a));
580
666
  }
667
+ function listNodesNewestFirst(nodes) {
668
+ const bySlug = /* @__PURE__ */ new Map();
669
+ for (const node of nodes) {
670
+ const group = bySlug.get(node.slug);
671
+ if (group) {
672
+ group.push(node);
673
+ } else {
674
+ bySlug.set(node.slug, [node]);
675
+ }
676
+ }
677
+ return [...bySlug.values()].flatMap((group) => [...group].sort((a, b) => compareSemver(b.version, a.version)));
678
+ }
581
679
  function findNode(loaded, slug, version) {
582
680
  const matches = loaded.nodes.filter((n) => n.description.slug === slug);
583
681
  if (matches.length === 0) {
@@ -620,6 +718,68 @@ async function resolveCredentialInstance(loaded, store, credentialId) {
620
718
  const ctx = credentialContext(store, credentialId);
621
719
  return cred.resolve(ctx, instance.config, instance.durableCreds);
622
720
  }
721
+ function createRunState(store) {
722
+ const staged = [];
723
+ const stage = (entry) => {
724
+ staged.push(entry);
725
+ };
726
+ const state = {
727
+ mapping: {
728
+ async get(namespace, key, side = "left") {
729
+ if (side === "left") {
730
+ const entry = store.getStateEntry("mapping", namespace, key);
731
+ return entry ? String(entry.value) : null;
732
+ }
733
+ const match = store.listStateEntries("mapping", namespace).find((e) => String(e.value) === key);
734
+ return match ? match.key : null;
735
+ },
736
+ async put(namespace, left, right) {
737
+ assertNotRepointed(
738
+ namespace,
739
+ store.listStateEntries("mapping", namespace).map((e) => [e.key, String(e.value)]),
740
+ left,
741
+ right
742
+ );
743
+ store.putStateEntry({ namespace, role: "mapping", key: left, value: right });
744
+ }
745
+ },
746
+ cursor: {
747
+ async get(namespace, partitionKey = "") {
748
+ return store.getStateEntry("cursor", namespace, partitionKey)?.value;
749
+ },
750
+ async set(namespace, value, partitionKey = "") {
751
+ stage({ namespace, role: "cursor", key: partitionKey, value });
752
+ }
753
+ },
754
+ async claim(namespace, key, opts) {
755
+ assertTtlInRange(opts?.ttlSeconds);
756
+ const held = store.getStateEntry("dedupe", namespace, key);
757
+ if (held && Number(held.value) > Date.now()) {
758
+ return false;
759
+ }
760
+ const ttlSeconds = opts?.ttlSeconds ?? DEFAULT_CLAIM_TTL_SECONDS;
761
+ store.putStateEntry({ namespace, role: "dedupe", key, value: Date.now() + ttlSeconds * 1e3 });
762
+ return true;
763
+ },
764
+ digest: {
765
+ async unchanged(namespace, entityKey, digest) {
766
+ return store.getStateEntry("digest", namespace, entityKey)?.value === digest;
767
+ },
768
+ async set(namespace, entityKey, digest) {
769
+ stage({ namespace, role: "digest", key: entityKey, value: digest });
770
+ }
771
+ }
772
+ };
773
+ return {
774
+ state,
775
+ commit() {
776
+ for (const entry of staged) {
777
+ store.putStateEntry(entry);
778
+ }
779
+ staged.length = 0;
780
+ }
781
+ };
782
+ }
623
783
  function authorContext(loaded, store, config, locale) {
624
784
  return {
625
785
  signal: new AbortController().signal,
@@ -710,6 +870,10 @@ function validateField(field, value, errors) {
710
870
  switch (field.type) {
711
871
  case "string":
712
872
  case "secret-ref":
873
+ // A `state-ref` carries the chosen namespace NAME, so a string is all there
874
+ // is to check here: which names exist is the workflow's declaration, and
875
+ // nothing declares one locally (see the README's fidelity caveats).
876
+ case "state-ref":
713
877
  case "expression":
714
878
  if (typeof value !== "string") {
715
879
  push("Expected a string.");
@@ -793,9 +957,12 @@ async function validateNodeConfig(loaded, store, input) {
793
957
  if (field.type === "dynamic-schema") {
794
958
  continue;
795
959
  }
960
+ if (!(0, import_integrations_node_sdk3.settingApplies)(field, config)) {
961
+ continue;
962
+ }
796
963
  validateField(field, config[field.key], errors);
797
964
  }
798
- if (fields.some((f) => f.type === "dynamic-schema") && node.resolveConfigSchema) {
965
+ if (fields.some((f) => f.type === "dynamic-schema" && (0, import_integrations_node_sdk3.settingApplies)(f, config)) && node.resolveConfigSchema) {
799
966
  const ctx = authorContext(loaded, store, config, input.locale);
800
967
  let children;
801
968
  try {
@@ -807,6 +974,9 @@ async function validateNodeConfig(loaded, store, input) {
807
974
  throw new DevApiError(502, `Node resolve failed: ${err.message}`);
808
975
  }
809
976
  for (const child of children) {
977
+ if (!(0, import_integrations_node_sdk3.settingApplies)(child, config)) {
978
+ continue;
979
+ }
810
980
  validateField(child, config[child.key], errors);
811
981
  }
812
982
  }
@@ -823,6 +993,7 @@ async function executeNodeTest(loaded, store, input) {
823
993
  logs.push(meta === void 0 ? { level, message } : { level, message, meta });
824
994
  };
825
995
  const timeoutMs = Math.max(MIN_TIMEOUT_MS, input.timeoutMs ?? DEFAULT_TIMEOUT_MS);
996
+ const run = createRunState(store);
826
997
  const controller = new AbortController();
827
998
  let timer;
828
999
  const expired = new Promise((_resolve, reject) => {
@@ -847,10 +1018,12 @@ async function executeNodeTest(loaded, store, input) {
847
1018
  async get(id) {
848
1019
  return (await resolveCredentialInstance(loaded, store, id)).credentials;
849
1020
  }
850
- }
1021
+ },
1022
+ state: run.state
851
1023
  };
852
1024
  try {
853
1025
  const result = await Promise.race([node.execute(ctx, { ...input.config, ...input.inputs }), expired]);
1026
+ run.commit();
854
1027
  return { outputs: result.outputs ?? {}, branch: result.branch ?? null, logs };
855
1028
  } catch (err) {
856
1029
  if (err instanceof DevApiError) {
@@ -880,7 +1053,7 @@ async function buildOAuthAuthorizeUrl(loaded, store, credentialId, params) {
880
1053
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
881
1054
  }
882
1055
  const cred = findCredential(loaded, instance.credentialTypeSlug);
883
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1056
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
884
1057
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
885
1058
  }
886
1059
  return cred.buildAuthorizeUrl(credentialContext(store, credentialId), instance.config, params);
@@ -891,7 +1064,7 @@ async function exchangeOAuthCode(loaded, store, credentialId, params) {
891
1064
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
892
1065
  }
893
1066
  const cred = findCredential(loaded, instance.credentialTypeSlug);
894
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1067
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
895
1068
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
896
1069
  }
897
1070
  const out = await cred.exchangeCode(credentialContext(store, credentialId), instance.config, params);
@@ -946,7 +1119,7 @@ async function readJson(req) {
946
1119
  }
947
1120
 
948
1121
  // src/projections.ts
949
- var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
1122
+ var import_integrations_node_sdk4 = require("@revenexx/integrations-node-sdk");
950
1123
  function hasDynamicOptions(desc) {
951
1124
  return (desc.config ?? []).some((f) => f.dynamic === true);
952
1125
  }
@@ -967,7 +1140,7 @@ function nodeToApi(desc, ctx) {
967
1140
  // come from the developer's working tree, so the package identity is read from
968
1141
  // their package.json and the timestamps are when the mock loaded the entry.
969
1142
  package: ctx?.packageInfo ?? null,
970
- manifest_version: import_integrations_node_sdk3.MANIFEST_VERSION,
1143
+ manifest_version: import_integrations_node_sdk4.MANIFEST_VERSION,
971
1144
  manifest: desc,
972
1145
  has_dynamic_options: hasDynamicOptions(desc),
973
1146
  has_dynamic_schema: hasDynamicSchema(desc),
@@ -1086,13 +1259,24 @@ function createRequestListener(deps) {
1086
1259
  function createDevServer(deps) {
1087
1260
  return import_node_http.default.createServer(createRequestListener(deps));
1088
1261
  }
1262
+ function requestOrigin(req) {
1263
+ const host = req.headers.host;
1264
+ if (!host) {
1265
+ return "http://localhost";
1266
+ }
1267
+ try {
1268
+ return new URL(`http://${host}`).origin;
1269
+ } catch {
1270
+ return "http://localhost";
1271
+ }
1272
+ }
1089
1273
  async function handle(req, res, deps) {
1090
1274
  const method = (req.method ?? "GET").toUpperCase();
1091
1275
  if (method === "OPTIONS") {
1092
1276
  sendJson(res, 204, {});
1093
1277
  return;
1094
1278
  }
1095
- const url = new URL(req.url ?? "/", "http://localhost");
1279
+ const url = new URL(req.url ?? "/", requestOrigin(req));
1096
1280
  let pathname = url.pathname;
1097
1281
  if (pathname.startsWith("/api/v1")) {
1098
1282
  pathname = pathname.slice("/api/v1".length);
@@ -1153,7 +1337,7 @@ function handleSchemas(res, deps, seg) {
1153
1337
  }
1154
1338
  async function handleNodes(req, res, method, loaded, store, seg) {
1155
1339
  if (seg.length === 1 && method === "GET") {
1156
- return void sendJson(res, 200, { data: loaded.manifest.nodes.map((n) => nodeToApi(n, nodeApiContext(loaded))) });
1340
+ return void sendJson(res, 200, { data: listNodesNewestFirst(loaded.manifest.nodes).map((n) => nodeToApi(n, nodeApiContext(loaded))) });
1157
1341
  }
1158
1342
  const slug = seg[1];
1159
1343
  if (!slug) {
@@ -1218,7 +1402,8 @@ function nodeApiContext(loaded) {
1218
1402
  return { packageInfo: loaded.packageInfo, loadedAt: loaded.loadedAt };
1219
1403
  }
1220
1404
  function findManifestNode(loaded, slug, version) {
1221
- const node = loaded.manifest.nodes.find((n) => n.slug === slug && (version === "latest" || n.version === version));
1405
+ const matches = loaded.manifest.nodes.filter((n) => n.slug === slug);
1406
+ const node = version === "latest" ? listNodesNewestFirst(matches)[0] : matches.find((n) => n.version === version);
1222
1407
  if (!node) {
1223
1408
  throw new DevApiError(404, `Node '${slug}@${version}' not found.`);
1224
1409
  }
@@ -1296,7 +1481,7 @@ async function handleCredentials(req, res, method, loaded, store, seg, url) {
1296
1481
  });
1297
1482
  }
1298
1483
  if (seg[2] === "oauth" && seg[3] === "authorize-url" && method === "POST") {
1299
- const redirectUri = "http://localhost/api/v1/credentials/oauth/callback";
1484
+ const redirectUri = `${url.origin}/api/v1/credentials/oauth/callback`;
1300
1485
  const result = await buildOAuthAuthorizeUrl(loaded, store, id, { redirectUri, state: id });
1301
1486
  return void sendJson(res, 200, { authorize_url: result.authorizeUrl });
1302
1487
  }
@@ -1503,6 +1688,7 @@ var DEVKIT_VERSION = JSON.parse((0, import_node_fs4.readFileSync)(manifestUrl, "
1503
1688
  findNode,
1504
1689
  importFresh,
1505
1690
  interpolateEnv,
1691
+ listNodesNewestFirst,
1506
1692
  loadEnvFile,
1507
1693
  loadPackageFromEntry,
1508
1694
  loadSeedsFromDir,