ag-common 0.0.720 → 0.0.722

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.
@@ -55,8 +55,8 @@ const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, fun
55
55
  try {
56
56
  yield (0, async_1.asyncForEach)(chunked, (items) => __awaiter(void 0, void 0, void 0, function* () {
57
57
  let retryCount = 0;
58
- let retryMax = 3;
59
- let params = new lib_dynamodb_1.BatchWriteCommand({
58
+ const retryMax = 3;
59
+ const params = new lib_dynamodb_1.BatchWriteCommand({
60
60
  RequestItems: {
61
61
  [`${tableName}`]: items.map((Item) => ({
62
62
  PutRequest: { Item },
@@ -64,28 +64,32 @@ const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, fun
64
64
  },
65
65
  });
66
66
  (0, log_1.debug)(`running dynamo batchWrite=${JSON.stringify(params, null, 2)}`);
67
- try {
68
- yield exports.dynamoDb.send(params);
69
- return {};
70
- }
71
- catch (e) {
72
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
- let es = e.toString();
74
- let msg = es;
75
- (0, log_1.warn)('dynamo write error', msg);
76
- if (es.indexOf('429') !== -1 ||
77
- es.indexOf('ProvisionedThroughputExceeded') !== -1) {
78
- retryCount += 1;
79
- msg = `batch write throttled. retry ${retryCount}/${retryMax}`;
80
- }
81
- else {
82
- throw e;
67
+ // eslint-disable-next-line
68
+ while (true) {
69
+ try {
70
+ yield exports.dynamoDb.send(params);
71
+ return {};
83
72
  }
84
- if (retryCount >= retryMax) {
73
+ catch (e) {
74
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
+ const es = e.toString();
76
+ let msg = es;
77
+ (0, log_1.warn)('dynamo write error', msg);
78
+ if (es.indexOf('429') !== -1 ||
79
+ es.indexOf('ProvisionedThroughputExceeded') !== -1) {
80
+ retryCount += 1;
81
+ msg = `batch write throttled. retry ${retryCount}/${retryMax}`;
82
+ (0, log_1.warn)(msg);
83
+ if (retryCount >= retryMax) {
84
+ throw new Error(`Max retries (${retryMax}) exceeded: ${es}`);
85
+ }
86
+ yield (0, sleep_1.sleep)(2000);
87
+ // Continue the while loop to retry
88
+ continue;
89
+ }
90
+ // For non-throttling errors, throw immediately
85
91
  throw e;
86
92
  }
87
- (0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
88
- yield (0, sleep_1.sleep)(2000);
89
93
  }
90
94
  }));
91
95
  return {};
@@ -97,13 +101,13 @@ const batchWrite = (tableName, itemsIn) => __awaiter(void 0, void 0, void 0, fun
97
101
  });
98
102
  exports.batchWrite = batchWrite;
99
103
  const batchDelete = (_a) => __awaiter(void 0, [_a], void 0, function* ({ tableName, keys, pkName, }) {
100
- //batch up to 20, so we can retry.
104
+ // batch up to 20, so we can retry
101
105
  let chunked = (0, array_1.chunk)(keys, 20);
102
106
  try {
103
107
  yield (0, async_1.asyncForEach)(chunked, (items) => __awaiter(void 0, void 0, void 0, function* () {
104
108
  let retryCount = 0;
105
- let retryMax = 3;
106
- let params = new lib_dynamodb_1.BatchWriteCommand({
109
+ const retryMax = 3;
110
+ const params = new lib_dynamodb_1.BatchWriteCommand({
107
111
  RequestItems: {
108
112
  [`${tableName}`]: items.map((key) => ({
109
113
  DeleteRequest: { Key: { [`${pkName}`]: key } },
@@ -111,28 +115,36 @@ const batchDelete = (_a) => __awaiter(void 0, [_a], void 0, function* ({ tableNa
111
115
  },
112
116
  });
113
117
  (0, log_1.debug)(`running dynamo batch delete=${JSON.stringify(params, null, 2)}`);
114
- try {
115
- yield exports.dynamoDb.send(params);
116
- return {};
117
- }
118
- catch (e) {
119
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
- let es = e.toString();
121
- let msg = es;
122
- (0, log_1.warn)('dynamo write error', msg);
123
- if (es.indexOf('429') !== -1 ||
124
- es.indexOf('ProvisionedThroughputExceeded') !== -1) {
125
- retryCount += 1;
126
- msg = `batch delete write throttled. retry ${retryCount}/${retryMax}`;
127
- }
128
- else {
129
- throw e;
118
+ while (retryCount < retryMax) {
119
+ try {
120
+ yield exports.dynamoDb.send(params);
121
+ return {};
130
122
  }
131
- if (retryCount >= retryMax) {
132
- throw e;
123
+ catch (e) {
124
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
125
+ const es = e.toString();
126
+ let shouldRetry = false;
127
+ if (es.indexOf('429') !== -1 ||
128
+ es.indexOf('ProvisionedThroughputExceeded') !== -1) {
129
+ shouldRetry = true;
130
+ retryCount += 1;
131
+ const msg = `batch delete write throttled. retry ${retryCount}/${retryMax}`;
132
+ (0, log_1.warn)('dynamo write error', msg);
133
+ }
134
+ else {
135
+ // Non-retryable error
136
+ throw e;
137
+ }
138
+ if (shouldRetry) {
139
+ if (retryCount >= retryMax) {
140
+ (0, log_1.warn)(`Max retries (${retryMax}) reached, giving up`);
141
+ throw e;
142
+ }
143
+ (0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
144
+ yield (0, sleep_1.sleep)(2000 * Math.pow(2, retryCount - 1)); // Exponential backoff
145
+ continue;
146
+ }
133
147
  }
134
- (0, log_1.warn)(`dynamo retry ${retryCount}/${retryMax}`);
135
- yield (0, sleep_1.sleep)(2000);
136
148
  }
137
149
  }));
138
150
  return {};
@@ -27,6 +27,7 @@ const client_s3_1 = require("@aws-sdk/client-s3");
27
27
  const s3_presigned_post_1 = require("@aws-sdk/s3-presigned-post");
28
28
  const array_1 = require("../../common/helpers/array");
29
29
  const log_1 = require("../../common/helpers/log");
30
+ const object_1 = require("../../common/helpers/object");
30
31
  const setS3 = (region) => {
31
32
  const raw = new client_s3_1.S3Client({ region });
32
33
  return raw;
@@ -191,7 +192,7 @@ function getPresignedPostURL(_a) {
191
192
  ['starts-with', '$Content-Type', 'image/'],
192
193
  ],
193
194
  });
194
- const fields = JSON.parse(JSON.stringify(ps.fields));
195
+ const fields = (0, object_1.copy)(ps.fields);
195
196
  return { data: { fields, url: ps.url } };
196
197
  }
197
198
  catch (e) {
@@ -40,7 +40,7 @@ const getOperation = ({ path, method, resource, schema, }) => {
40
40
  const re = new RegExp(resourcePath
41
41
  .replace(/\//gim, `\\/`)
42
42
  .replace(/\{(.+?)\}/gim, '(?<$1>[^\\\\]+)'), 'i').exec(path);
43
- const pathParams = (re === null || re === void 0 ? void 0 : re.groups) && JSON.parse(JSON.stringify(re.groups));
43
+ const pathParams = (re === null || re === void 0 ? void 0 : re.groups) && (0, object_1.copy)(re.groups);
44
44
  return { operation, pathParams };
45
45
  };
46
46
  function validateOpenApi(_a) {
@@ -65,3 +65,4 @@ export declare const removeUndefValuesFromObjectAdditional: <T>(orig: Record<str
65
65
  */
66
66
  export declare const castStringlyObject: (orig: Record<string, string | string[] | undefined>) => Record<string, string>;
67
67
  export declare const isObject: (o: any) => any;
68
+ export declare const copy: <T>(v: T) => T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isObject = exports.castStringlyObject = exports.removeUndefValuesFromObjectAdditional = exports.removeUndefValuesFromObject = exports.filterObject = exports.castObject = exports.objectToString = exports.paramsToObject = exports.objectAlphaSort = exports.objectToArray = exports.getObjectKeysAsNumber = exports.objectKeysToLowerCase = exports.isJson = exports.tryJsonParse = void 0;
3
+ exports.copy = exports.isObject = exports.castStringlyObject = exports.removeUndefValuesFromObjectAdditional = exports.removeUndefValuesFromObject = exports.filterObject = exports.castObject = exports.objectToString = exports.paramsToObject = exports.objectAlphaSort = exports.objectToArray = exports.getObjectKeysAsNumber = exports.objectKeysToLowerCase = exports.isJson = exports.tryJsonParse = void 0;
4
4
  const tryJsonParse = (str, defaultValue) => {
5
5
  if (!str) {
6
6
  return null;
@@ -193,3 +193,5 @@ exports.castStringlyObject = castStringlyObject;
193
193
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
194
194
  const isObject = (o) => o && typeof o === 'object' && !Array.isArray(o);
195
195
  exports.isObject = isObject;
196
+ const copy = (v) => JSON.parse(JSON.stringify(v));
197
+ exports.copy = copy;
@@ -3,11 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLegendItems = void 0;
4
4
  const array_1 = require("../../../common/helpers/array");
5
5
  const math_1 = require("../../../common/helpers/math");
6
+ const object_1 = require("../../../common/helpers/object");
6
7
  const getLegendItems = ({ data, selectedKey, }) => {
7
8
  const min = data.total * 0.1;
8
9
  const shownResults = 4;
9
10
  const part = (0, array_1.take)(data.values.filter((r) => r.value > min), shownResults).part;
10
- const rest = JSON.parse(JSON.stringify(data.values)).filter((r) => !part.find((p) => p.name === r.name));
11
+ const rest = (0, object_1.copy)(data.values).filter((r) => !part.find((p) => p.name === r.name));
11
12
  //if we want to ensure this value exists in the returned results
12
13
  if (selectedKey) {
13
14
  const pi = part.findIndex((r) => r.name === selectedKey);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLegendItems = void 0;
4
4
  const array_1 = require("../../../common/helpers/array");
5
5
  const math_1 = require("../../../common/helpers/math");
6
+ const object_1 = require("../../../common/helpers/object");
6
7
  const shownResults = 4;
7
8
  const getTopItems = ({ data, colours, }) => {
8
9
  const val = {};
@@ -41,7 +42,7 @@ const getLegendItems = (p) => {
41
42
  let rest = [];
42
43
  if (part.length > shownResults) {
43
44
  part = (0, array_1.take)(part.filter((r) => r.y > min), shownResults).part;
44
- rest = JSON.parse(JSON.stringify(values)).filter((r) => !part.find((p) => p.name === r.name));
45
+ rest = (0, object_1.copy)(values).filter((r) => !part.find((p) => p.name === r.name));
45
46
  }
46
47
  const restTotal = (0, math_1.sumArray)(rest.map((s) => s.y));
47
48
  return { part, rest, restTotal, total };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.720",
2
+ "version": "0.0.722",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",