@shushed/helpers 0.0.57 → 0.0.59

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.d.ts CHANGED
@@ -28763,22 +28763,37 @@ declare function validate<T>(payload: T, schema: JSONSchemaType<T>, options: {
28763
28763
  declare const sanitizeToString: DeepRedact;
28764
28764
  declare const sanitize: DeepRedact;
28765
28765
 
28766
+ interface ILogging {
28767
+ log(...args: any[]): void;
28768
+ error(...args: any[]): void;
28769
+ }
28766
28770
  type Opts = {
28767
28771
  workflowId: string;
28768
28772
  triggerId: string;
28769
28773
  url: string;
28770
28774
  envName: string;
28775
+ logging?: ILogging;
28771
28776
  } | {
28772
28777
  workflowId: string;
28773
28778
  triggerId: string;
28774
28779
  url: string;
28775
- };
28780
+ logging?: ILogging;
28781
+ } | Runtime;
28782
+ declare class Logging implements ILogging {
28783
+ private workflowId;
28784
+ private triggerId;
28785
+ private systemEnvName;
28786
+ constructor(workflowId: string, triggerId: string, systemEnvName: string);
28787
+ log(...args: any[]): void;
28788
+ error(...args: any[]): void;
28789
+ }
28776
28790
  declare class Runtime {
28777
28791
  readonly workflowId: string;
28778
28792
  readonly triggerId: string;
28779
28793
  readonly envName: string;
28780
28794
  readonly systemEnvName: string;
28781
28795
  readonly runtimeUrl: string;
28796
+ readonly logging: ILogging;
28782
28797
  constructor(opts: Opts);
28783
28798
  }
28784
28799
 
@@ -28879,7 +28894,6 @@ declare class PubSubHelper extends Runtime {
28879
28894
  private getNameFromFullyQualifiedName;
28880
28895
  private updateSubscription;
28881
28896
  private ensureSubscribtionExists;
28882
- private ensureDLQSubscriptionExists;
28883
28897
  deleteSubscriptions(topicName: string): Promise<{
28884
28898
  mainTopic: Topic | undefined;
28885
28899
  mainDlqTopic: Topic | undefined;
@@ -29229,8 +29243,11 @@ type index_BigQueryHelper = BigQueryHelper;
29229
29243
  declare const index_BigQueryHelper: typeof BigQueryHelper;
29230
29244
  type index_EnvEngine = EnvEngine;
29231
29245
  declare const index_EnvEngine: typeof EnvEngine;
29246
+ type index_ILogging = ILogging;
29232
29247
  type index_JWKSHelper = JWKSHelper;
29233
29248
  declare const index_JWKSHelper: typeof JWKSHelper;
29249
+ type index_Logging = Logging;
29250
+ declare const index_Logging: typeof Logging;
29234
29251
  type index_Message = Message;
29235
29252
  type index_NodeOptions = NodeOptions;
29236
29253
  type index_PubSubHelper = PubSubHelper;
@@ -29253,7 +29270,7 @@ declare const index_shortHash: typeof shortHash;
29253
29270
  declare const index_validate: typeof validate;
29254
29271
  declare const index_validateGoogleAuth: typeof validateGoogleAuth;
29255
29272
  declare namespace index {
29256
- export { index_BigQueryHelper as BigQueryHelper, index_EnvEngine as EnvEngine, index_JWKSHelper as JWKSHelper, type index_Message as Message, type index_NodeOptions as NodeOptions, index_PubSubHelper as PubSubHelper, index_Runtime as Runtime, index_SchedulerHelper as SchedulerHelper, index_Secrets as Secrets, type index_TriggerOnCreateOptions as TriggerOnCreateOptions, type index_TriggerOnExecuteOptions as TriggerOnExecuteOptions, index_getEventTime as getEventTime, index_isCronMessage as isCronMessage, index_isPubSubRequest as isPubSubRequest, index_parseDateOrDefault as parseDateOrDefault, index_sanitize as sanitize, index_sanitizeToString as sanitizeToString, index_shortHash as shortHash, index_validate as validate, index_validateGoogleAuth as validateGoogleAuth };
29273
+ export { index_BigQueryHelper as BigQueryHelper, index_EnvEngine as EnvEngine, type index_ILogging as ILogging, index_JWKSHelper as JWKSHelper, index_Logging as Logging, type index_Message as Message, type index_NodeOptions as NodeOptions, index_PubSubHelper as PubSubHelper, index_Runtime as Runtime, index_SchedulerHelper as SchedulerHelper, index_Secrets as Secrets, type index_TriggerOnCreateOptions as TriggerOnCreateOptions, type index_TriggerOnExecuteOptions as TriggerOnExecuteOptions, index_getEventTime as getEventTime, index_isCronMessage as isCronMessage, index_isPubSubRequest as isPubSubRequest, index_parseDateOrDefault as parseDateOrDefault, index_sanitize as sanitize, index_sanitizeToString as sanitizeToString, index_shortHash as shortHash, index_validate as validate, index_validateGoogleAuth as validateGoogleAuth };
29257
29274
  }
29258
29275
 
29259
29276
  export { index as lib, index$9 as schema, index$1 as types };
package/dist/index.js CHANGED
@@ -288,6 +288,7 @@ __export(src_public_exports, {
288
288
  BigQueryHelper: () => bigquery_default,
289
289
  EnvEngine: () => env_default,
290
290
  JWKSHelper: () => jwks_default,
291
+ Logging: () => Logging,
291
292
  PubSubHelper: () => pubsub_default,
292
293
  Runtime: () => Runtime,
293
294
  SchedulerHelper: () => scheduler_default,
@@ -363,25 +364,63 @@ var import_jose = require("jose");
363
364
 
364
365
  // src-public/runtime.ts
365
366
  var DEFAULT_BRANCH = "migratedprod";
366
- var Runtime = class {
367
+ var Logging = class {
368
+ constructor(workflowId, triggerId, systemEnvName) {
369
+ this.workflowId = workflowId;
370
+ this.triggerId = triggerId;
371
+ this.systemEnvName = systemEnvName;
372
+ }
373
+ log(...args) {
374
+ console.log({
375
+ payload: args,
376
+ workflowId: this.workflowId,
377
+ wId: this.workflowId,
378
+ triggerId: this.triggerId,
379
+ tId: this.triggerId,
380
+ env: this.systemEnvName
381
+ });
382
+ }
383
+ error(...args) {
384
+ console.error({
385
+ payload: args,
386
+ workflowId: this.workflowId,
387
+ wId: this.workflowId,
388
+ triggerId: this.triggerId,
389
+ tId: this.triggerId,
390
+ env: this.systemEnvName
391
+ });
392
+ }
393
+ };
394
+ var Runtime = class _Runtime {
367
395
  workflowId;
368
396
  triggerId;
369
397
  envName;
370
398
  systemEnvName;
371
399
  runtimeUrl;
400
+ logging;
372
401
  constructor(opts) {
373
- this.workflowId = opts.workflowId;
374
- this.triggerId = opts.triggerId;
375
- this.systemEnvName = typeof opts.envName !== "undefined" ? opts.envName : (() => {
376
- const hostId = opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0].split(".")[0].toLowerCase();
377
- let branch = DEFAULT_BRANCH;
378
- if (hostId.includes("-")) {
379
- branch = hostId.split("-")[0];
380
- }
381
- return branch;
382
- })();
383
- this.runtimeUrl = "https://" + new URL("https://" + opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0]).host;
384
- this.envName = this.systemEnvName === DEFAULT_BRANCH ? "prod" : this.systemEnvName;
402
+ if (opts instanceof _Runtime) {
403
+ this.workflowId = opts.workflowId;
404
+ this.triggerId = opts.triggerId;
405
+ this.systemEnvName = opts.systemEnvName;
406
+ this.runtimeUrl = opts.runtimeUrl;
407
+ this.envName = opts.envName;
408
+ this.logging = opts.logging || new Logging(this.workflowId, this.triggerId, this.systemEnvName);
409
+ } else {
410
+ this.workflowId = opts.workflowId;
411
+ this.triggerId = opts.triggerId;
412
+ this.systemEnvName = typeof opts.envName !== "undefined" ? opts.envName : (() => {
413
+ const hostId = opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0].split(".")[0].toLowerCase();
414
+ let branch = DEFAULT_BRANCH;
415
+ if (hostId.includes("-")) {
416
+ branch = hostId.split("-")[0];
417
+ }
418
+ return branch;
419
+ })();
420
+ this.runtimeUrl = "https://" + new URL("https://" + opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0]).host;
421
+ this.envName = this.systemEnvName === DEFAULT_BRANCH ? "prod" : this.systemEnvName;
422
+ this.logging = opts.logging || new Logging(this.workflowId, this.triggerId, this.systemEnvName);
423
+ }
385
424
  }
386
425
  };
387
426
 
@@ -567,13 +606,6 @@ var EnvEngine = class extends Runtime {
567
606
  };
568
607
  }
569
608
  async set(envs, level = "trigger", encryptWithValue) {
570
- const timeout = 300;
571
- await Promise.race([this.initializeIfNeeded(level), new Promise((resolve, reject) => setTimeout(() => {
572
- reject(new Error(`Waitied ${timeout}ms. Terminating firestore initialize during the set function`));
573
- }, timeout))]).catch((err) => {
574
- console.warn("Invariant: While initializing the env engine during set - " + err.message + ` - ${this.envName} - ${this.workflowId} - ${this.triggerId}.
575
- Failing quietly as the set might proceed, but this is not a good thing.`);
576
- });
577
609
  let aes = null;
578
610
  if (encryptWithValue) {
579
611
  aes = new AES256GCM(Buffer.from(encryptWithValue, "utf8"));
@@ -598,7 +630,6 @@ var EnvEngine = class extends Runtime {
598
630
  throw new Error(`Failed to set the ${envs.map((x) => x.name).join(", ")}. Error: ${err.message}`);
599
631
  }
600
632
  }
601
- Object.assign(this.store[level], obj);
602
633
  return void 0;
603
634
  }
604
635
  async initializeIfNeeded(level = "trigger") {
@@ -791,16 +822,19 @@ var PubSubHelper = class extends Runtime {
791
822
  return Promise.all(promises);
792
823
  }
793
824
  async createOrUpdate(opts) {
825
+ this.logging.log("createOrUpdate called with", opts);
794
826
  const [topics] = await this.pubSub.getTopics();
795
- const mainDlqTopicName = `${opts.topicName}-${shortHash(this.triggerId)}-dlq`;
796
- const tableDlqTopicName = `${opts.topicName}-${shortHash(this.triggerId)}-bq-dlq`;
827
+ this.logging.log("topics discovered", topics);
828
+ const infraPrefix = opts.topicName.indexOf("---") !== -1 ? opts.topicName : `${this.envName}---${opts.topicName}`;
829
+ const mainDlqTopicName = `${infraPrefix}-${shortHash(this.triggerId)}-dlq`;
830
+ const tableDlqTopicName = `${infraPrefix}-${shortHash(this.triggerId)}-bq-dlq`;
797
831
  let mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === opts.topicName);
798
832
  let mainDlqTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainDlqTopicName);
799
833
  let tableDlqTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === tableDlqTopicName);
800
- const mainSubscriptionName = `${opts.topicName}--${shortHash(this.triggerId)}`;
801
- const mainDlqSubscriptionName = `${opts.topicName}--${shortHash(this.triggerId)}-dlq`;
802
- const tableSubscriptionName = `${opts.topicName}--${shortHash(this.triggerId)}-bq`;
803
- const tableDlqSubscriptionName = `${opts.topicName}--${shortHash(this.triggerId)}-bq-dlq`;
834
+ const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
835
+ const mainDlqSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}-dlq`;
836
+ const tableSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}-bq`;
837
+ const tableDlqSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}-bq-dlq`;
804
838
  let mainTopicSubscriptions = null;
805
839
  let mainDlqTopicSubscriptions = null;
806
840
  let tableDlqTopicSubscriptions = null;
@@ -817,11 +851,28 @@ var PubSubHelper = class extends Runtime {
817
851
  let mainDlqSubscription = mainDlqTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainDlqSubscriptionName) || null;
818
852
  let tableSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === tableSubscriptionName) || null;
819
853
  let tableDlqSubscription = tableDlqTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === tableDlqSubscriptionName) || null;
854
+ this.logging.log("environment discovery", {
855
+ mainTopicName: opts.topicName,
856
+ mainDlqTopicName,
857
+ tableDlqTopicName,
858
+ mainSubscriptionName,
859
+ mainDlqSubscriptionName,
860
+ tableSubscriptionName,
861
+ tableDlqSubscriptionName,
862
+ mainTopicExists: mainTopic,
863
+ mainDlqTopicExists: mainDlqTopic,
864
+ tableDlqTopicExists: tableDlqTopic,
865
+ mainSubscriptionExists: mainSubscription,
866
+ mainDlqSubscriptionExists: mainDlqSubscription,
867
+ tableSubscriptionExists: tableSubscription,
868
+ tableDlqSubscriptionExists: tableDlqSubscription
869
+ });
820
870
  const topic = await this.ensureTopicExists(opts.topicName);
821
871
  if (opts.table || opts.pull || opts.push) {
822
872
  mainTopic = await this.ensureTopicExists(opts.topicName);
823
873
  }
824
874
  if (!opts.table) {
875
+ this.logging.log("createOrUpdate deleting table subscriptions");
825
876
  if (tableSubscription) {
826
877
  await tableSubscription.delete();
827
878
  tableSubscription = null;
@@ -831,12 +882,17 @@ var PubSubHelper = class extends Runtime {
831
882
  tableDlqSubscription = null;
832
883
  }
833
884
  } else {
885
+ this.logging.log("createOrUpdate creating table subscription");
834
886
  if (opts.createDeadLetterQueues) {
887
+ this.logging.log("createOrUpdate ensuring dlqs exist for table subscription");
835
888
  tableDlqTopic = await this.ensureTopicExists(tableDlqTopicName);
836
- tableDlqSubscription = await this.ensureDLQSubscriptionExists(tableDlqSubscriptionName, {
837
- topic: tableDlqTopic
889
+ tableDlqSubscription = await this.ensureSubscribtionExists(tableDlqSubscriptionName, {
890
+ topic: tableDlqTopic,
891
+ endpointUrl: null,
892
+ dlqTopic: null
838
893
  });
839
894
  }
895
+ this.logging.log("createOrUpdate ensuring the table subscription exists");
840
896
  tableSubscription = await this.ensureSubscribtionExists(tableSubscriptionName, {
841
897
  topic,
842
898
  tableId: opts.table.tableId,
@@ -849,6 +905,7 @@ var PubSubHelper = class extends Runtime {
849
905
  }
850
906
  const pullPushSubscription = opts.pull || opts.push;
851
907
  if (!pullPushSubscription) {
908
+ this.logging.log("createOrUpdate deleting main subscriptions");
852
909
  if (mainSubscription) {
853
910
  await mainSubscription.delete();
854
911
  mainSubscription = null;
@@ -859,22 +916,26 @@ var PubSubHelper = class extends Runtime {
859
916
  }
860
917
  } else {
861
918
  if (opts.createDeadLetterQueues) {
919
+ this.logging.log("createOrUpdate creating dlqs for main subscription");
862
920
  mainDlqTopic = await this.ensureTopicExists(mainDlqTopicName);
863
- mainDlqSubscription = await this.ensureDLQSubscriptionExists(mainDlqSubscriptionName, {
864
- topic: mainDlqTopic
865
- });
866
- mainSubscription = await this.ensureSubscribtionExists(mainSubscriptionName, {
867
- topic,
868
- dlqTopic: mainDlqTopic,
869
- endpointUrl: opts.push ? this.runtimeUrl + `/executeWorkflow/${this.workflowId}/${this.triggerId}?topic_name=${opts.topicName}` : null,
870
- retryMinDelay: pullPushSubscription.retryMinDelay,
871
- retryMaxDelay: pullPushSubscription.retryMaxDelay,
872
- retryLimit: pullPushSubscription.retryLimit,
873
- ackDeadline: pullPushSubscription.ackDeadline
921
+ mainDlqSubscription = await this.ensureSubscribtionExists(mainDlqSubscriptionName, {
922
+ topic: mainDlqTopic,
923
+ dlqTopic: null,
924
+ endpointUrl: null
874
925
  });
875
926
  }
927
+ mainSubscription = await this.ensureSubscribtionExists(mainSubscriptionName, {
928
+ topic,
929
+ dlqTopic: mainDlqTopic || null,
930
+ endpointUrl: opts.push ? this.runtimeUrl + `/executeWorkflow/${this.workflowId}/${this.triggerId}?topic_name=${opts.topicName}` : null,
931
+ retryMinDelay: pullPushSubscription.retryMinDelay,
932
+ retryMaxDelay: pullPushSubscription.retryMaxDelay,
933
+ retryLimit: pullPushSubscription.retryLimit,
934
+ ackDeadline: pullPushSubscription.ackDeadline
935
+ });
876
936
  }
877
937
  if (!opts.createDeadLetterQueues) {
938
+ this.logging.log("createOrUpdate deleting all dlqs");
878
939
  if (mainDlqSubscription) {
879
940
  await mainDlqSubscription.delete();
880
941
  mainDlqSubscription = null;
@@ -904,11 +965,20 @@ var PubSubHelper = class extends Runtime {
904
965
  return topics.find((topic) => this.getNameFromFullyQualifiedName(topic.name) === topicName);
905
966
  }
906
967
  async ensureTopicExists(topicName) {
968
+ this.logging.log("ensureTopicExists called with", topicName);
907
969
  let topic = await this.findTopic(topicName);
970
+ this.logging.log(`ensureTopicExists topic ${!topic ? "does not " : ""}exists`, topic);
971
+ let topicMetadata;
972
+ try {
973
+ [topicMetadata] = await topic?.getMetadata() || [null];
974
+ } catch (err) {
975
+ throw new Error("Invariant: While getting topic metadata - " + err.message);
976
+ }
977
+ this.logging.log(`ensureTopicExists retrieved topic metadata`, topicMetadata);
908
978
  const metadata = {
909
979
  labels: {
910
980
  "created-at": (/* @__PURE__ */ new Date()).getTime().toString(),
911
- "last-modified-at": (/* @__PURE__ */ new Date()).getTime().toString(),
981
+ "last-modified-at": topicMetadata?.labels?.["last-modified-at"] || (/* @__PURE__ */ new Date()).getTime().toString(),
912
982
  "created-by-bs-triggerid": this.triggerId.toLowerCase(),
913
983
  "created-by-bs-workflowid": this.workflowId.toLowerCase(),
914
984
  "bs-envname": this.systemEnvName.toLowerCase(),
@@ -916,21 +986,25 @@ var PubSubHelper = class extends Runtime {
916
986
  [`w-${this.workflowId}-${this.triggerId}`.toLowerCase()]: "true"
917
987
  }
918
988
  };
919
- let topicMetadata;
920
- try {
921
- [topicMetadata] = await topic?.getMetadata() || [null];
922
- } catch (err) {
923
- throw new Error("Invariant: While getting topic metadata - " + err.message);
924
- }
925
989
  if (topic) {
926
990
  const simMetadata = Object.assign({}, topicMetadata?.labels || {}, metadata.labels);
927
991
  if (!(0, import_lodash.default)(topicMetadata?.labels, simMetadata)) {
928
992
  simMetadata["last-modified-at"] = (/* @__PURE__ */ new Date()).getTime().toString();
929
- await topic.setMetadata({
930
- labels: simMetadata
931
- });
993
+ this.logging.log(`ensureTopicExists topic metadata has changed updating with`, simMetadata);
994
+ try {
995
+ await topic.setMetadata({
996
+ labels: simMetadata
997
+ });
998
+ } catch (err) {
999
+ this.logging.error("ensureTopicExists received error while setting metadata", err.message, err.stack);
1000
+ throw err;
1001
+ }
932
1002
  }
933
1003
  } else {
1004
+ this.logging.log(`ensureTopicExists creating new topic`, {
1005
+ name: topicName,
1006
+ labels: metadata.labels
1007
+ });
934
1008
  await this.pubSub.createTopic({
935
1009
  name: topicName,
936
1010
  labels: metadata.labels
@@ -953,11 +1027,20 @@ var PubSubHelper = class extends Runtime {
953
1027
  retryMaxDelay: 360,
954
1028
  ackDeadline: 90
955
1029
  };
1030
+ this.logging.log("updateSubscription called with", subscriptionName, subscription, subscriptionMetaData, inputOpts);
956
1031
  const opts = Object.assign({}, defaultOptions, inputOpts);
1032
+ this.logging.log("updateSubscription final opts", opts);
957
1033
  let nextMetadata = null;
958
1034
  if (isSubscriptionOptionsPush(opts)) {
959
1035
  if (opts.endpointUrl !== subscriptionMetaData?.pushConfig?.pushEndpoint || this.serviceAccountEmail !== subscriptionMetaData?.pushConfig.oidcToken?.serviceAccountEmail || process.env.GCLOUD_PROJECT + "/" + this.triggerId !== subscriptionMetaData.pushConfig.oidcToken.audience) {
960
1036
  if (subscriptionMetaData?.pushConfig?.pushEndpoint && subscription) {
1037
+ this.logging.log("updateSubscription pushConfig changed, push configuration already exists, modifying", {
1038
+ pushEndpoint: opts.endpointUrl,
1039
+ oidcToken: {
1040
+ serviceAccountEmail: this.serviceAccountEmail,
1041
+ audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1042
+ }
1043
+ });
961
1044
  try {
962
1045
  await subscription.modifyPushConfig({
963
1046
  pushEndpoint: opts.endpointUrl,
@@ -1047,10 +1130,11 @@ var PubSubHelper = class extends Runtime {
1047
1130
  nextMetadata.bigqueryConfig = null;
1048
1131
  }
1049
1132
  if (subscription && nextMetadata) {
1133
+ this.logging.log("updateSubscription updating subscription", nextMetadata);
1050
1134
  try {
1051
1135
  await subscription.setMetadata(nextMetadata);
1052
1136
  } catch (err) {
1053
- console.error("Invariant: While updating subscription - " + err.message + ` - ${this.pubSub.projectId} - ${subscriptionName} - ${JSON.stringify(nextMetadata)}`);
1137
+ this.logging.error("updateSubscription received error while setting metadata", err.message, err.stack);
1054
1138
  throw new Error("Invariant: While updating subscription - " + err.message);
1055
1139
  }
1056
1140
  }
@@ -1059,14 +1143,17 @@ var PubSubHelper = class extends Runtime {
1059
1143
  async ensureSubscribtionExists(subscriptionName, opts) {
1060
1144
  let [subscriptions] = await opts.topic.getSubscriptions();
1061
1145
  let subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName) || null;
1146
+ this.logging.log("ensureSubscribtionExists called with", subscriptionName, opts);
1062
1147
  let subscriptionMetaData;
1063
1148
  try {
1064
1149
  [subscriptionMetaData] = await subscription?.getMetadata() || [null];
1065
1150
  } catch (err) {
1066
1151
  throw new Error("Invariant: While getting subscription metadata - " + err.message);
1067
1152
  }
1153
+ this.logging.log("ensureSubscribtionExists retrieved subscriptionMetaData", subscriptionMetaData);
1068
1154
  const createPayload = await this.updateSubscription(subscriptionName, subscription, subscriptionMetaData, opts);
1069
- if (createPayload) {
1155
+ if (createPayload && !subscription) {
1156
+ this.logging.log(`ensureSubscribtionExists creating subscription: ${subscriptionName} on topic: ${opts.topic.name} with`, createPayload);
1070
1157
  await opts.topic.createSubscription(subscriptionName, createPayload);
1071
1158
  [subscriptions] = await opts.topic.getSubscriptions();
1072
1159
  subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName) || null;
@@ -1076,20 +1163,8 @@ var PubSubHelper = class extends Runtime {
1076
1163
  }
1077
1164
  return subscription;
1078
1165
  }
1079
- async ensureDLQSubscriptionExists(subscriptionName, opts) {
1080
- let [subscriptions] = await opts.topic.getSubscriptions();
1081
- let subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName);
1082
- if (!subscription) {
1083
- await opts.topic.createSubscription(subscriptionName);
1084
- [subscriptions] = await opts.topic.getSubscriptions();
1085
- subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName);
1086
- }
1087
- if (!subscription) {
1088
- throw new Error(`Invariant: While creating subscription - subscription is not found`);
1089
- }
1090
- return subscription;
1091
- }
1092
1166
  async deleteSubscriptions(topicName) {
1167
+ this.logging.log("deleteSubscriptions called with", topicName);
1093
1168
  return this.createOrUpdate({
1094
1169
  topicName,
1095
1170
  createDeadLetterQueues: false,
@@ -1101,13 +1176,18 @@ var PubSubHelper = class extends Runtime {
1101
1176
  async isRunning(topicName) {
1102
1177
  const [topics] = await this.pubSub.getTopics();
1103
1178
  const mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === topicName);
1179
+ this.logging.log("isRunning called with", topicName);
1180
+ const infraPrefix = topicName.indexOf("---") !== -1 ? topicName : `${this.envName}---${topicName}`;
1104
1181
  let mainTopicSubscriptions = null;
1105
1182
  if (mainTopic) {
1106
1183
  [mainTopicSubscriptions] = await mainTopic.getSubscriptions();
1107
1184
  }
1108
- const mainSubscriptionName = `${topicName}--${shortHash(this.triggerId)}`;
1185
+ const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
1186
+ this.logging.log("isRunning checking the", mainSubscriptionName);
1109
1187
  const mainSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainSubscriptionName) || null;
1188
+ this.logging.log("isRunning found the", mainSubscription);
1110
1189
  const [metadata] = await mainSubscription?.getMetadata() || [null];
1190
+ this.logging.log("isRunning received metadata", metadata);
1111
1191
  return {
1112
1192
  subscriptionId: mainSubscription?.name,
1113
1193
  isRunning: metadata?.pushConfig?.pushEndpoint !== null
@@ -1116,16 +1196,25 @@ var PubSubHelper = class extends Runtime {
1116
1196
  async pause(topicName) {
1117
1197
  const [topics] = await this.pubSub.getTopics();
1118
1198
  const mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === topicName);
1199
+ this.logging.log("paused called with", topicName);
1200
+ const infraPrefix = topicName.indexOf("---") !== -1 ? topicName : `${this.envName}---${topicName}`;
1119
1201
  let mainTopicSubscriptions = null;
1120
1202
  if (mainTopic) {
1121
1203
  [mainTopicSubscriptions] = await mainTopic.getSubscriptions();
1122
1204
  }
1123
- const mainSubscriptionName = `${topicName}--${shortHash(this.triggerId)}`;
1205
+ const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
1206
+ this.logging.log("isRunning checking the", mainSubscriptionName);
1124
1207
  const mainSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainSubscriptionName) || null;
1208
+ this.logging.log("pause found the", mainSubscription);
1125
1209
  if (mainSubscription) {
1126
- await mainSubscription.setMetadata({
1127
- pushConfig: null
1128
- });
1210
+ try {
1211
+ await mainSubscription.setMetadata({
1212
+ pushConfig: null
1213
+ });
1214
+ } catch (err) {
1215
+ this.logging.error("pause received error while setting metadata", err.message, err.stack);
1216
+ throw err;
1217
+ }
1129
1218
  return {
1130
1219
  subscriptionId: mainSubscription.name
1131
1220
  };
@@ -1137,22 +1226,40 @@ var PubSubHelper = class extends Runtime {
1137
1226
  async resume(topicName) {
1138
1227
  const [topics] = await this.pubSub.getTopics();
1139
1228
  const mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === topicName);
1229
+ this.logging.log("resume called with", topicName);
1230
+ const infraPrefix = topicName.indexOf("---") !== -1 ? topicName : `${this.envName}---${topicName}`;
1140
1231
  let mainTopicSubscriptions = null;
1141
1232
  if (mainTopic) {
1142
1233
  [mainTopicSubscriptions] = await mainTopic.getSubscriptions();
1143
1234
  }
1144
- const mainSubscriptionName = `${topicName}--${shortHash(this.triggerId)}`;
1235
+ const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
1236
+ this.logging.log("resume checking the", mainSubscriptionName);
1145
1237
  const mainSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainSubscriptionName) || null;
1238
+ this.logging.log("resume found the", mainSubscription);
1146
1239
  if (mainSubscription) {
1147
- await mainSubscription.setMetadata({
1148
- pushConfig: {
1149
- pushEndpoint: this.runtimeUrl + "/executeWorkflow/" + this.workflowId + "/" + this.triggerId + `?topic_name=${topicName}`,
1150
- oidcToken: {
1151
- serviceAccountEmail: this.serviceAccountEmail,
1152
- audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1240
+ try {
1241
+ this.logging.log("resume setting metadata", {
1242
+ pushConfig: {
1243
+ pushEndpoint: this.runtimeUrl + "/executeWorkflow/" + this.workflowId + "/" + this.triggerId + `?topic_name=${topicName}`,
1244
+ oidcToken: {
1245
+ serviceAccountEmail: this.serviceAccountEmail,
1246
+ audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1247
+ }
1153
1248
  }
1154
- }
1155
- });
1249
+ });
1250
+ await mainSubscription.setMetadata({
1251
+ pushConfig: {
1252
+ pushEndpoint: this.runtimeUrl + "/executeWorkflow/" + this.workflowId + "/" + this.triggerId + `?topic_name=${topicName}`,
1253
+ oidcToken: {
1254
+ serviceAccountEmail: this.serviceAccountEmail,
1255
+ audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1256
+ }
1257
+ }
1258
+ });
1259
+ } catch (err) {
1260
+ this.logging.error("resume received error while setting metadata", err.message, err.stack);
1261
+ throw err;
1262
+ }
1156
1263
  return {
1157
1264
  subscriptionId: mainSubscription.name
1158
1265
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shushed/helpers",
3
- "version": "0.0.57",
3
+ "version": "0.0.59",
4
4
  "author": "",
5
5
  "license": "UNLICENSED",
6
6
  "description": "",