@shushed/helpers 0.0.270 → 0.0.271

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.
@@ -156,6 +156,26 @@ class PubSubHelper extends runtime_1.default {
156
156
  if (topicName.indexOf('rn.') !== 0 && /^\d/.test(topicName)) {
157
157
  topicName = `rn.${topicName}`;
158
158
  }
159
+ const topicAttributesNorm = {};
160
+ if (opts.topicAttributes && typeof opts.topicAttributes === 'object') {
161
+ for (const key in opts.topicAttributes) {
162
+ const v = normalizeCore(`${opts.topicAttributes[key]}`);
163
+ const k = normalizeCore(key);
164
+ if (v) {
165
+ topicAttributesNorm[k] = v;
166
+ }
167
+ }
168
+ }
169
+ const subscriptionAttributesNorm = {};
170
+ if (opts.subscriptionAttributes && typeof opts.subscriptionAttributes === 'object') {
171
+ for (const key in opts.subscriptionAttributes) {
172
+ const v = normalizeCore(`${opts.subscriptionAttributes[key]}`);
173
+ const k = normalizeCore(key);
174
+ if (v) {
175
+ subscriptionAttributesNorm[k] = v;
176
+ }
177
+ }
178
+ }
159
179
  const mainSubscriptionName = opts.subscriptionName || topicName;
160
180
  const tableSubscriptionName = `${mainSubscriptionName}.bq`;
161
181
  const mainDlqTopicName = `${mainSubscriptionName}.dlq`;
@@ -181,7 +201,7 @@ class PubSubHelper extends runtime_1.default {
181
201
  let mainDlqSubscription = mainDlqTopicSubscriptions?.find(x => this.getNameFromFullyQualifiedName(x.name) === mainDlqSubscriptionName) || null;
182
202
  let tableSubscription = mainTopicSubscriptions?.find(x => this.getNameFromFullyQualifiedName(x.name) === tableSubscriptionName) || null;
183
203
  let tableDlqSubscription = tableDlqTopicSubscriptions?.find(x => this.getNameFromFullyQualifiedName(x.name) === tableDlqSubscriptionName) || null;
184
- const topic = await this.ensureTopicExists(opts.topicName);
204
+ const topic = await this.ensureTopicExists(opts.topicName, topicAttributesNorm);
185
205
  if (!opts.table) {
186
206
  if (tableSubscription) {
187
207
  await tableSubscription.delete();
@@ -194,13 +214,15 @@ class PubSubHelper extends runtime_1.default {
194
214
  }
195
215
  else {
196
216
  if (opts.createDeadLetterQueues) {
197
- tableDlqTopic = await this.ensureTopicExists(tableDlqTopicName);
217
+ tableDlqTopic = await this.ensureTopicExists(tableDlqTopicName, topicAttributesNorm);
198
218
  tableDlqSubscription = await this.ensureSubscribtionExists(tableDlqSubscriptionName, {
219
+ attributes: subscriptionAttributesNorm,
199
220
  topic: tableDlqTopic,
200
221
  dlqTopic: null,
201
222
  });
202
223
  }
203
224
  tableSubscription = await this.ensureSubscribtionExists(tableSubscriptionName, {
225
+ attributes: subscriptionAttributesNorm,
204
226
  topic: topic,
205
227
  tableId: opts.table.tableId,
206
228
  ackDeadline: opts.table.ackDeadline,
@@ -223,13 +245,15 @@ class PubSubHelper extends runtime_1.default {
223
245
  }
224
246
  else {
225
247
  if (opts.createDeadLetterQueues) {
226
- mainDlqTopic = await this.ensureTopicExists(mainDlqTopicName);
248
+ mainDlqTopic = await this.ensureTopicExists(mainDlqTopicName, topicAttributesNorm);
227
249
  mainDlqSubscription = await this.ensureSubscribtionExists(mainDlqSubscriptionName, {
250
+ attributes: subscriptionAttributesNorm,
228
251
  topic: mainDlqTopic,
229
252
  dlqTopic: null,
230
253
  });
231
254
  }
232
255
  mainSubscription = await this.ensureSubscribtionExists(mainSubscriptionName, {
256
+ attributes: subscriptionAttributesNorm,
233
257
  topic,
234
258
  dlqTopic: mainDlqTopic || null,
235
259
  exactlyOnceDelivery: true,
@@ -269,7 +293,7 @@ class PubSubHelper extends runtime_1.default {
269
293
  const [topics] = await this.pubSub.getTopics();
270
294
  return topics.find(topic => this.getNameFromFullyQualifiedName(topic.name) === topicName);
271
295
  }
272
- async ensureTopicExists(topicName) {
296
+ async ensureTopicExists(topicName, attributes) {
273
297
  let topic = await this.findTopic(topicName);
274
298
  let topicMetadata;
275
299
  try {
@@ -280,16 +304,17 @@ class PubSubHelper extends runtime_1.default {
280
304
  }
281
305
  const metadata = {
282
306
  labels: {
283
- 'bs-envname': this.systemEnvName.toLowerCase(),
307
+ 'dlq': topicName.indexOf('.dlq') > -1 ? '1' : '0',
284
308
  'env': this.envName.toLowerCase(),
285
- 'bs-is-deadletter': topicName.indexOf('.dlq') > -1 ? '1' : '0',
286
- 'respectful-nudge': '1'
309
+ 'rn': '1',
310
+ ...(Object.assign({}, attributes || {}), {
311
+ 'bq': topicName.indexOf('.bq') > -1 ? '1' : '0',
312
+ }),
287
313
  }
288
314
  };
289
315
  if (topic && !process.env.PUBSUB_EMULATOR_HOST) {
290
316
  const simMetadata = Object.assign({}, topicMetadata?.labels || {}, metadata.labels);
291
317
  if (!(0, lodash_isequal_1.default)(topicMetadata?.labels, simMetadata)) {
292
- simMetadata['last-modified-at'] = new Date().getTime().toString();
293
318
  try {
294
319
  await topic.setMetadata({
295
320
  labels: simMetadata
@@ -374,12 +399,12 @@ class PubSubHelper extends runtime_1.default {
374
399
  }
375
400
  const proposedLabels = {
376
401
  'env': this.envName.toLowerCase(),
377
- 'bs-workflowid': this.workflowId.toLowerCase(),
378
- 'bs-envname': this.systemEnvName.toLowerCase(),
379
- 'bs-is-deadletter': subscriptionName.indexOf('.dlq') > -1 ? '1' : '0',
380
- 'bs-bigquery-tableid': isSubscriptionBigQuery(opts) && opts.tableId ? opts.tableId.replace(/\./g, '-').toLowerCase() : '',
381
- 'bs-is-bigquery': isSubscriptionBigQuery(opts) && opts.tableId ? '1' : '0',
382
- 'respectful-nudge': '1'
402
+ 'dlq': subscriptionName.indexOf('.dlq') > -1 ? '1' : '0',
403
+ 'bq': isSubscriptionBigQuery(opts) && opts.tableId ? '1' : '0',
404
+ 'table': (isSubscriptionBigQuery(opts) && opts.tableId) ? normalizeCore(opts.tableId.split('.').slice(-1)[0]) : '',
405
+ 'int': subscriptionName.indexOf('.int') > -1 ? '1' : '0',
406
+ 'topic': normalizeCore(opts.topic.name.split('/').slice(-1)[0].split('---').slice(-1)[0].split('.')[0]) + (subscriptionName.indexOf('.int') > -1 ? '.int' : ''),
407
+ ...(Object.assign({}, inputOpts.attributes || {})),
383
408
  };
384
409
  const simLabels = Object.assign({}, subscriptionMetaData?.labels || {}, proposedLabels);
385
410
  if (!(0, lodash_isequal_1.default)(simLabels, subscriptionMetaData?.labels)) {
@@ -487,4 +512,13 @@ class PubSubHelper extends runtime_1.default {
487
512
  return subscription;
488
513
  }
489
514
  }
515
+ function normalizeCore(raw) {
516
+ return raw
517
+ .normalize("NFKD")
518
+ .toLowerCase()
519
+ .replace(/[\s./:;+|]+/g, "_")
520
+ .replace(/[^a-z0-9_-]/g, "")
521
+ .replace(/[_-]{2,}/g, "_")
522
+ .replace(/^[_-]+|[_-]+$/g, "");
523
+ }
490
524
  exports.default = PubSubHelper;
@@ -23,6 +23,7 @@ type MessageBody = Record<string, any>;
23
23
  type SubscriptionOptionsPull = {
24
24
  topic: Topic;
25
25
  exactlyOnceDelivery?: boolean;
26
+ attributes?: Record<string, string>;
26
27
  retryMinDelay?: number;
27
28
  retryMaxDelay?: number;
28
29
  retryLimit?: number;
@@ -32,6 +33,7 @@ type SubscriptionOptionsPull = {
32
33
  };
33
34
  type SubscriptionOptionsBigQuery = {
34
35
  topic: Topic;
36
+ attributes?: Record<string, string>;
35
37
  tableId: string;
36
38
  dlqTopic: Topic | null;
37
39
  retryMinDelay?: number;
@@ -68,6 +70,9 @@ declare class PubSubHelper extends Runtime {
68
70
  subscriptionName?: string;
69
71
  createDeadLetterQueues: boolean;
70
72
  subscriptionFilter?: string | null;
73
+ subscriptionLabels?: Record<string, string>;
74
+ subscriptionAttributes?: Record<string, string>;
75
+ topicAttributes?: Record<string, string>;
71
76
  pull?: null | {
72
77
  retryLimit: number;
73
78
  ackDeadline: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shushed/helpers",
3
- "version": "0.0.270",
3
+ "version": "0.0.271",
4
4
  "author": "",
5
5
  "license": "UNLICENSED",
6
6
  "description": "",