@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/index.cjs CHANGED
@@ -215,10 +215,13 @@ var import_node_crypto = require("crypto");
215
215
  var DEV_TENANT_ID = "dev-tenant";
216
216
  var BLOB_DEFINITION_VERSION = "v0-draft";
217
217
  var EXECUTION_MODES = ["async_only", "sync_only", "caller_decides"];
218
- var STORE_SCHEMA_VERSION = 2;
218
+ var STORE_SCHEMA_VERSION = 3;
219
219
  function now() {
220
220
  return (/* @__PURE__ */ new Date()).toISOString();
221
221
  }
222
+ function stateKey(role, namespace, key) {
223
+ return `${role}\0${namespace}\0${key}`;
224
+ }
222
225
  var DevStore = class {
223
226
  /**
224
227
  * @param onChange fired after any mutation so a caller (the CLI) can persist
@@ -232,6 +235,8 @@ var DevStore = class {
232
235
  secrets = /* @__PURE__ */ new Map();
233
236
  workflows = /* @__PURE__ */ new Map();
234
237
  triggers = /* @__PURE__ */ new Map();
238
+ /** Keyed by `${role}\u0000${namespace}\u0000${key}` — a NUL occurs in no part. */
239
+ stateEntries = /* @__PURE__ */ new Map();
235
240
  nextWorkflowId = 1;
236
241
  /** (Re)wire the post-mutation callback — e.g. attach it only after seeding. */
237
242
  setOnMutation(onChange) {
@@ -430,6 +435,21 @@ var DevStore = class {
430
435
  return deleted;
431
436
  }
432
437
  // ------------------------------------------------------------- persistence
438
+ // ------------------------------------------------------------------ state
439
+ /** The stored entry for a role/namespace/key, or undefined when unknown. */
440
+ getStateEntry(role, namespace, key) {
441
+ return this.stateEntries.get(stateKey(role, namespace, key));
442
+ }
443
+ /** Every entry a namespace holds in one role — how a reverse mapping lookup is answered. */
444
+ listStateEntries(role, namespace) {
445
+ return [...this.stateEntries.values()].filter((e) => e.role === role && e.namespace === namespace);
446
+ }
447
+ putStateEntry(entry) {
448
+ const record = { ...entry, updatedAt: now() };
449
+ this.stateEntries.set(stateKey(entry.role, entry.namespace, entry.key), record);
450
+ this.onChange?.();
451
+ return record;
452
+ }
433
453
  toSnapshot() {
434
454
  return {
435
455
  schemaVersion: STORE_SCHEMA_VERSION,
@@ -437,6 +457,7 @@ var DevStore = class {
437
457
  secrets: [...this.secrets.values()],
438
458
  workflows: [...this.workflows.values()],
439
459
  triggers: [...this.triggers.values()],
460
+ stateEntries: [...this.stateEntries.values()],
440
461
  nextWorkflowId: this.nextWorkflowId
441
462
  };
442
463
  }
@@ -445,6 +466,7 @@ var DevStore = class {
445
466
  this.secrets = new Map(snapshot.secrets.map((s) => [s.key, s]));
446
467
  this.workflows = new Map(snapshot.workflows.map((w) => [w.id, w]));
447
468
  this.triggers = new Map(snapshot.triggers.map((t) => [t.id, t]));
469
+ this.stateEntries = new Map((snapshot.stateEntries ?? []).map((e) => [stateKey(e.role, e.namespace, e.key), e]));
448
470
  this.nextWorkflowId = snapshot.nextWorkflowId ?? Math.max(0, ...snapshot.workflows.map((w) => w.id)) + 1;
449
471
  }
450
472
  /**
@@ -466,6 +488,9 @@ var DevStore = class {
466
488
  for (const t of snapshot.triggers) {
467
489
  this.triggers.set(t.id, t);
468
490
  }
491
+ for (const e of snapshot.stateEntries ?? []) {
492
+ this.stateEntries.set(stateKey(e.role, e.namespace, e.key), e);
493
+ }
469
494
  this.nextWorkflowId = Math.max(this.nextWorkflowId, snapshot.nextWorkflowId ?? 0, ...snapshot.workflows.map((w) => w.id + 1));
470
495
  }
471
496
  };
@@ -555,7 +580,33 @@ async function loadSeedsFromDir(dir) {
555
580
  }
556
581
 
557
582
  // src/resolve.ts
583
+ var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
584
+
585
+ // src/state-rules.ts
558
586
  var import_integrations_node_sdk2 = require("@revenexx/integrations-node-sdk");
587
+ var DEFAULT_CLAIM_TTL_SECONDS = 604800;
588
+ var MIN_CLAIM_TTL_SECONDS = 1;
589
+ var MAX_CLAIM_TTL_SECONDS = 31536e3;
590
+ function assertTtlInRange(ttlSeconds) {
591
+ if (ttlSeconds === void 0) {
592
+ return;
593
+ }
594
+ if (!Number.isFinite(ttlSeconds) || ttlSeconds < MIN_CLAIM_TTL_SECONDS || ttlSeconds > MAX_CLAIM_TTL_SECONDS) {
595
+ 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)}.`);
596
+ }
597
+ }
598
+ function assertNotRepointed(namespace, pairs, left, right) {
599
+ for (const [existingLeft, existingRight] of pairs) {
600
+ if (existingLeft === left && existingRight !== right) {
601
+ throw new import_integrations_node_sdk2.NodeError("MAPPING_CONFLICT", `'${left}' is already correlated with '${existingRight}' in namespace '${namespace}'.`);
602
+ }
603
+ if (existingRight === right && existingLeft !== left) {
604
+ throw new import_integrations_node_sdk2.NodeError("MAPPING_CONFLICT", `'${right}' is already correlated with '${existingLeft}' in namespace '${namespace}'.`);
605
+ }
606
+ }
607
+ }
608
+
609
+ // src/resolve.ts
559
610
  var noopLogger = {
560
611
  info() {
561
612
  },
@@ -620,6 +671,68 @@ async function resolveCredentialInstance(loaded, store, credentialId) {
620
671
  const ctx = credentialContext(store, credentialId);
621
672
  return cred.resolve(ctx, instance.config, instance.durableCreds);
622
673
  }
674
+ function createRunState(store) {
675
+ const staged = [];
676
+ const stage = (entry) => {
677
+ staged.push(entry);
678
+ };
679
+ const state = {
680
+ mapping: {
681
+ async get(namespace, key, side = "left") {
682
+ if (side === "left") {
683
+ const entry = store.getStateEntry("mapping", namespace, key);
684
+ return entry ? String(entry.value) : null;
685
+ }
686
+ const match = store.listStateEntries("mapping", namespace).find((e) => String(e.value) === key);
687
+ return match ? match.key : null;
688
+ },
689
+ async put(namespace, left, right) {
690
+ assertNotRepointed(
691
+ namespace,
692
+ store.listStateEntries("mapping", namespace).map((e) => [e.key, String(e.value)]),
693
+ left,
694
+ right
695
+ );
696
+ store.putStateEntry({ namespace, role: "mapping", key: left, value: right });
697
+ }
698
+ },
699
+ cursor: {
700
+ async get(namespace, partitionKey = "") {
701
+ return store.getStateEntry("cursor", namespace, partitionKey)?.value;
702
+ },
703
+ async set(namespace, value, partitionKey = "") {
704
+ stage({ namespace, role: "cursor", key: partitionKey, value });
705
+ }
706
+ },
707
+ async claim(namespace, key, opts) {
708
+ assertTtlInRange(opts?.ttlSeconds);
709
+ const held = store.getStateEntry("dedupe", namespace, key);
710
+ if (held && Number(held.value) > Date.now()) {
711
+ return false;
712
+ }
713
+ const ttlSeconds = opts?.ttlSeconds ?? DEFAULT_CLAIM_TTL_SECONDS;
714
+ store.putStateEntry({ namespace, role: "dedupe", key, value: Date.now() + ttlSeconds * 1e3 });
715
+ return true;
716
+ },
717
+ digest: {
718
+ async unchanged(namespace, entityKey, digest) {
719
+ return store.getStateEntry("digest", namespace, entityKey)?.value === digest;
720
+ },
721
+ async set(namespace, entityKey, digest) {
722
+ stage({ namespace, role: "digest", key: entityKey, value: digest });
723
+ }
724
+ }
725
+ };
726
+ return {
727
+ state,
728
+ commit() {
729
+ for (const entry of staged) {
730
+ store.putStateEntry(entry);
731
+ }
732
+ staged.length = 0;
733
+ }
734
+ };
735
+ }
623
736
  function authorContext(loaded, store, config, locale) {
624
737
  return {
625
738
  signal: new AbortController().signal,
@@ -710,6 +823,10 @@ function validateField(field, value, errors) {
710
823
  switch (field.type) {
711
824
  case "string":
712
825
  case "secret-ref":
826
+ // A `state-ref` carries the chosen namespace NAME, so a string is all there
827
+ // is to check here: which names exist is the workflow's declaration, and
828
+ // nothing declares one locally (see the README's fidelity caveats).
829
+ case "state-ref":
713
830
  case "expression":
714
831
  if (typeof value !== "string") {
715
832
  push("Expected a string.");
@@ -793,9 +910,12 @@ async function validateNodeConfig(loaded, store, input) {
793
910
  if (field.type === "dynamic-schema") {
794
911
  continue;
795
912
  }
913
+ if (!(0, import_integrations_node_sdk3.settingApplies)(field, config)) {
914
+ continue;
915
+ }
796
916
  validateField(field, config[field.key], errors);
797
917
  }
798
- if (fields.some((f) => f.type === "dynamic-schema") && node.resolveConfigSchema) {
918
+ if (fields.some((f) => f.type === "dynamic-schema" && (0, import_integrations_node_sdk3.settingApplies)(f, config)) && node.resolveConfigSchema) {
799
919
  const ctx = authorContext(loaded, store, config, input.locale);
800
920
  let children;
801
921
  try {
@@ -807,6 +927,9 @@ async function validateNodeConfig(loaded, store, input) {
807
927
  throw new DevApiError(502, `Node resolve failed: ${err.message}`);
808
928
  }
809
929
  for (const child of children) {
930
+ if (!(0, import_integrations_node_sdk3.settingApplies)(child, config)) {
931
+ continue;
932
+ }
810
933
  validateField(child, config[child.key], errors);
811
934
  }
812
935
  }
@@ -823,6 +946,7 @@ async function executeNodeTest(loaded, store, input) {
823
946
  logs.push(meta === void 0 ? { level, message } : { level, message, meta });
824
947
  };
825
948
  const timeoutMs = Math.max(MIN_TIMEOUT_MS, input.timeoutMs ?? DEFAULT_TIMEOUT_MS);
949
+ const run = createRunState(store);
826
950
  const controller = new AbortController();
827
951
  let timer;
828
952
  const expired = new Promise((_resolve, reject) => {
@@ -847,10 +971,12 @@ async function executeNodeTest(loaded, store, input) {
847
971
  async get(id) {
848
972
  return (await resolveCredentialInstance(loaded, store, id)).credentials;
849
973
  }
850
- }
974
+ },
975
+ state: run.state
851
976
  };
852
977
  try {
853
978
  const result = await Promise.race([node.execute(ctx, { ...input.config, ...input.inputs }), expired]);
979
+ run.commit();
854
980
  return { outputs: result.outputs ?? {}, branch: result.branch ?? null, logs };
855
981
  } catch (err) {
856
982
  if (err instanceof DevApiError) {
@@ -880,7 +1006,7 @@ async function buildOAuthAuthorizeUrl(loaded, store, credentialId, params) {
880
1006
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
881
1007
  }
882
1008
  const cred = findCredential(loaded, instance.credentialTypeSlug);
883
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1009
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
884
1010
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
885
1011
  }
886
1012
  return cred.buildAuthorizeUrl(credentialContext(store, credentialId), instance.config, params);
@@ -891,7 +1017,7 @@ async function exchangeOAuthCode(loaded, store, credentialId, params) {
891
1017
  throw new DevApiError(404, `Credential instance '${credentialId}' not found.`);
892
1018
  }
893
1019
  const cred = findCredential(loaded, instance.credentialTypeSlug);
894
- if (!(0, import_integrations_node_sdk2.isOAuthAuthorizeCredential)(cred)) {
1020
+ if (!(0, import_integrations_node_sdk3.isOAuthAuthorizeCredential)(cred)) {
895
1021
  throw new DevApiError(422, `Credential type '${instance.credentialTypeSlug}' is not a 3-legged OAuth type.`);
896
1022
  }
897
1023
  const out = await cred.exchangeCode(credentialContext(store, credentialId), instance.config, params);
@@ -946,7 +1072,7 @@ async function readJson(req) {
946
1072
  }
947
1073
 
948
1074
  // src/projections.ts
949
- var import_integrations_node_sdk3 = require("@revenexx/integrations-node-sdk");
1075
+ var import_integrations_node_sdk4 = require("@revenexx/integrations-node-sdk");
950
1076
  function hasDynamicOptions(desc) {
951
1077
  return (desc.config ?? []).some((f) => f.dynamic === true);
952
1078
  }
@@ -967,7 +1093,7 @@ function nodeToApi(desc, ctx) {
967
1093
  // come from the developer's working tree, so the package identity is read from
968
1094
  // their package.json and the timestamps are when the mock loaded the entry.
969
1095
  package: ctx?.packageInfo ?? null,
970
- manifest_version: import_integrations_node_sdk3.MANIFEST_VERSION,
1096
+ manifest_version: import_integrations_node_sdk4.MANIFEST_VERSION,
971
1097
  manifest: desc,
972
1098
  has_dynamic_options: hasDynamicOptions(desc),
973
1099
  has_dynamic_schema: hasDynamicSchema(desc),