@revenexx/integrations-node-devkit 0.6.0 → 0.7.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
  },
@@ -757,6 +808,68 @@ async function resolveCredentialInstance(loaded, store, credentialId) {
757
808
  const ctx = credentialContext(store, credentialId);
758
809
  return cred.resolve(ctx, instance.config, instance.durableCreds);
759
810
  }
811
+ function createRunState(store) {
812
+ const staged = [];
813
+ const stage = (entry) => {
814
+ staged.push(entry);
815
+ };
816
+ const state = {
817
+ mapping: {
818
+ async get(namespace, key, side = "left") {
819
+ if (side === "left") {
820
+ const entry = store.getStateEntry("mapping", namespace, key);
821
+ return entry ? String(entry.value) : null;
822
+ }
823
+ const match = store.listStateEntries("mapping", namespace).find((e) => String(e.value) === key);
824
+ return match ? match.key : null;
825
+ },
826
+ async put(namespace, left, right) {
827
+ assertNotRepointed(
828
+ namespace,
829
+ store.listStateEntries("mapping", namespace).map((e) => [e.key, String(e.value)]),
830
+ left,
831
+ right
832
+ );
833
+ store.putStateEntry({ namespace, role: "mapping", key: left, value: right });
834
+ }
835
+ },
836
+ cursor: {
837
+ async get(namespace, partitionKey = "") {
838
+ return store.getStateEntry("cursor", namespace, partitionKey)?.value;
839
+ },
840
+ async set(namespace, value, partitionKey = "") {
841
+ stage({ namespace, role: "cursor", key: partitionKey, value });
842
+ }
843
+ },
844
+ async claim(namespace, key, opts) {
845
+ assertTtlInRange(opts?.ttlSeconds);
846
+ const held = store.getStateEntry("dedupe", namespace, key);
847
+ if (held && Number(held.value) > Date.now()) {
848
+ return false;
849
+ }
850
+ const ttlSeconds = opts?.ttlSeconds ?? DEFAULT_CLAIM_TTL_SECONDS;
851
+ store.putStateEntry({ namespace, role: "dedupe", key, value: Date.now() + ttlSeconds * 1e3 });
852
+ return true;
853
+ },
854
+ digest: {
855
+ async unchanged(namespace, entityKey, digest) {
856
+ return store.getStateEntry("digest", namespace, entityKey)?.value === digest;
857
+ },
858
+ async set(namespace, entityKey, digest) {
859
+ stage({ namespace, role: "digest", key: entityKey, value: digest });
860
+ }
861
+ }
862
+ };
863
+ return {
864
+ state,
865
+ commit() {
866
+ for (const entry of staged) {
867
+ store.putStateEntry(entry);
868
+ }
869
+ staged.length = 0;
870
+ }
871
+ };
872
+ }
760
873
  function authorContext(loaded, store, config, locale) {
761
874
  return {
762
875
  signal: new AbortController().signal,
@@ -847,6 +960,10 @@ function validateField(field, value, errors) {
847
960
  switch (field.type) {
848
961
  case "string":
849
962
  case "secret-ref":
963
+ // A `state-ref` carries the chosen namespace NAME, so a string is all there
964
+ // is to check here: which names exist is the workflow's declaration, and
965
+ // nothing declares one locally (see the README's fidelity caveats).
966
+ case "state-ref":
850
967
  case "expression":
851
968
  if (typeof value !== "string") {
852
969
  push("Expected a string.");
@@ -930,9 +1047,12 @@ async function validateNodeConfig(loaded, store, input) {
930
1047
  if (field.type === "dynamic-schema") {
931
1048
  continue;
932
1049
  }
1050
+ if (!(0, import_integrations_node_sdk3.settingApplies)(field, config)) {
1051
+ continue;
1052
+ }
933
1053
  validateField(field, config[field.key], errors);
934
1054
  }
935
- if (fields.some((f) => f.type === "dynamic-schema") && node.resolveConfigSchema) {
1055
+ if (fields.some((f) => f.type === "dynamic-schema" && (0, import_integrations_node_sdk3.settingApplies)(f, config)) && node.resolveConfigSchema) {
936
1056
  const ctx = authorContext(loaded, store, config, input.locale);
937
1057
  let children;
938
1058
  try {
@@ -944,6 +1064,9 @@ async function validateNodeConfig(loaded, store, input) {
944
1064
  throw new DevApiError(502, `Node resolve failed: ${err.message}`);
945
1065
  }
946
1066
  for (const child of children) {
1067
+ if (!(0, import_integrations_node_sdk3.settingApplies)(child, config)) {
1068
+ continue;
1069
+ }
947
1070
  validateField(child, config[child.key], errors);
948
1071
  }
949
1072
  }
@@ -960,6 +1083,7 @@ async function executeNodeTest(loaded, store, input) {
960
1083
  logs.push(meta === void 0 ? { level, message } : { level, message, meta });
961
1084
  };
962
1085
  const timeoutMs = Math.max(MIN_TIMEOUT_MS, input.timeoutMs ?? DEFAULT_TIMEOUT_MS);
1086
+ const run2 = createRunState(store);
963
1087
  const controller = new AbortController();
964
1088
  let timer;
965
1089
  const expired = new Promise((_resolve, reject) => {
@@ -984,10 +1108,12 @@ async function executeNodeTest(loaded, store, input) {
984
1108
  async get(id) {
985
1109
  return (await resolveCredentialInstance(loaded, store, id)).credentials;
986
1110
  }
987
- }
1111
+ },
1112
+ state: run2.state
988
1113
  };
989
1114
  try {
990
1115
  const result = await Promise.race([node.execute(ctx, { ...input.config, ...input.inputs }), expired]);
1116
+ run2.commit();
991
1117
  return { outputs: result.outputs ?? {}, branch: result.branch ?? null, logs };
992
1118
  } catch (err) {
993
1119
  if (err instanceof DevApiError) {
@@ -1017,7 +1143,7 @@ async function buildOAuthAuthorizeUrl(loaded, store, credentialId, params) {
1017
1143
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
1018
1144
  }
1019
1145
  const cred = findCredential(loaded, instance.credentialTypeSlug);
1020
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1146
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
1021
1147
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
1022
1148
  }
1023
1149
  return cred.buildAuthorizeUrl(credentialContext(store, credentialId), instance.config, params);
@@ -1028,7 +1154,7 @@ async function exchangeOAuthCode(loaded, store, credentialId, params) {
1028
1154
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
1029
1155
  }
1030
1156
  const cred = findCredential(loaded, instance.credentialTypeSlug);
1031
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1157
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
1032
1158
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
1033
1159
  }
1034
1160
  const out = await cred.exchangeCode(credentialContext(store, credentialId), instance.config, params);
@@ -1083,7 +1209,7 @@ async function readJson(req) {
1083
1209
  }
1084
1210
 
1085
1211
  // src/projections.ts
1086
- var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
1212
+ var import_integrations_node_sdk4 = require("@revenexx/integrations-node-sdk");
1087
1213
  function hasDynamicOptions(desc) {
1088
1214
  return (desc.config ?? []).some((f) => f.dynamic === true);
1089
1215
  }
@@ -1104,7 +1230,7 @@ function nodeToApi(desc, ctx) {
1104
1230
  // come from the developer's working tree, so the package identity is read from
1105
1231
  // their package.json and the timestamps are when the mock loaded the entry.
1106
1232
  package: ctx?.packageInfo ?? null,
1107
- manifest_version: import_integrations_node_sdk3.MANIFEST_VERSION,
1233
+ manifest_version: import_integrations_node_sdk4.MANIFEST_VERSION,
1108
1234
  manifest: desc,
1109
1235
  has_dynamic_options: hasDynamicOptions(desc),
1110
1236
  has_dynamic_schema: hasDynamicSchema(desc),