ag-common 0.0.94 → 0.0.98

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.
@@ -0,0 +1,15 @@
1
+ export declare const enforceDynamoProvisionCap: ({ tables, readsMax, writesMax, mustEqual, }: {
2
+ tables: any[];
3
+ /**
4
+ * default 25
5
+ */
6
+ readsMax?: number | undefined;
7
+ /**
8
+ * default 25
9
+ */
10
+ writesMax?: number | undefined;
11
+ /**
12
+ * default false. if true, will throw if cap isnt met
13
+ */
14
+ mustEqual?: boolean | undefined;
15
+ }) => void;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.enforceDynamoProvisionCap = void 0;
4
+ const log_1 = require("../../common/helpers/log");
5
+ const math_1 = require("../../common/helpers/math");
6
+ const string_1 = require("../../common/helpers/string");
7
+ const extractSum = ({ str, regex }) => {
8
+ var _a;
9
+ return (0, math_1.sumArray)(((_a = str
10
+ .match(regex)) === null || _a === void 0 ? void 0 : _a.map((s2) => (0, string_1.trim)(s2.substring(s2.indexOf(':') + 1), ':', ',', ' ')).filter((r) => r && Number(r)).map((r) => Number(r))) || []);
11
+ };
12
+ const enforceDynamoProvisionCap = ({ tables, readsMax = 25, writesMax = 25, mustEqual = false, }) => {
13
+ if (!tables || tables.length === 0) {
14
+ (0, log_1.warn)('error in dynamo FT enforce');
15
+ return;
16
+ }
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ const t = tables[0];
19
+ const s = (0, string_1.safeStringify)(t.node._children.Resource.node.scope);
20
+ const reads = extractSum({ str: s, regex: /readCapacityUnits.*/gim });
21
+ const writes = extractSum({ str: s, regex: /writeCapacityUnits.*/gim });
22
+ (0, log_1.warn)(`dynamo table provisioned reads:${reads}/${readsMax}`);
23
+ (0, log_1.warn)(`dynamo table provisioned writes:${writes}/${writesMax}`);
24
+ if (reads > readsMax || writes > writesMax) {
25
+ throw new Error('exceeded dynamo provision cap');
26
+ }
27
+ if (mustEqual && (reads !== readsMax || writes !== writesMax)) {
28
+ throw new Error(`dynamo provision cap not met`);
29
+ }
30
+ };
31
+ exports.enforceDynamoProvisionCap = enforceDynamoProvisionCap;
@@ -1,6 +1,7 @@
1
1
  export * from './api';
2
2
  export * from './aws';
3
3
  export * from './dynamo';
4
+ export * from './enforceDynamoProvisionCap';
4
5
  export * from './dynamoInfra';
5
6
  export * from './openApiHelpers';
6
7
  export * from './s3';
@@ -13,6 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./api"), exports);
14
14
  __exportStar(require("./aws"), exports);
15
15
  __exportStar(require("./dynamo"), exports);
16
+ __exportStar(require("./enforceDynamoProvisionCap"), exports);
16
17
  __exportStar(require("./dynamoInfra"), exports);
17
18
  __exportStar(require("./openApiHelpers"), exports);
18
19
  __exportStar(require("./s3"), exports);
@@ -24,3 +24,10 @@ export declare function toTitleCase(str: string): string;
24
24
  */
25
25
  export declare function replaceRemove(str: string, ...params: string[]): string;
26
26
  export declare function containsInsensitive(str: string, ...args: string[]): boolean;
27
+ /**
28
+ * safely handles circular references
29
+ * @param obj
30
+ * @param indent
31
+ * @returns
32
+ */
33
+ export declare const safeStringify: (obj: any, indent?: number) => string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.containsInsensitive = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
3
+ exports.safeStringify = exports.containsInsensitive = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
4
4
  const csvJSON = (csv) => {
5
5
  const lines = csv.split('\n');
6
6
  const result = [];
@@ -117,3 +117,22 @@ function containsInsensitive(str, ...args) {
117
117
  return !!args.find((a) => l.includes(a));
118
118
  }
119
119
  exports.containsInsensitive = containsInsensitive;
120
+ /**
121
+ * safely handles circular references
122
+ * @param obj
123
+ * @param indent
124
+ * @returns
125
+ */
126
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
+ const safeStringify = (obj, indent = 2) => {
128
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
129
+ let cache = [];
130
+ const retVal = JSON.stringify(obj, (_key, value) => typeof value === 'object' && value !== null
131
+ ? cache.includes(value)
132
+ ? undefined // Duplicate reference found, discard key
133
+ : cache.push(value) && value // Store value in our collection
134
+ : value, indent);
135
+ cache = null;
136
+ return retVal;
137
+ };
138
+ exports.safeStringify = safeStringify;
@@ -5,8 +5,11 @@ const react_1 = require("react");
5
5
  const log_1 = require("../../common/helpers/log");
6
6
  const object_1 = require("../../common/helpers/object");
7
7
  const getTimeSeconds = () => Math.ceil(new Date().getTime() / 1000);
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ let nodeLocalStorage = {};
8
10
  const clearLocalStorageItem = (key) => {
9
11
  if (typeof window === 'undefined') {
12
+ delete nodeLocalStorage[key];
10
13
  return;
11
14
  }
12
15
  try {
@@ -20,6 +23,7 @@ exports.clearLocalStorageItem = clearLocalStorageItem;
20
23
  const clearAllLocalStorage = (except) => {
21
24
  try {
22
25
  if (typeof window === 'undefined') {
26
+ nodeLocalStorage = {};
23
27
  return;
24
28
  }
25
29
  for (let i = 0; i < localStorage.length; i += 1) {
@@ -38,6 +42,7 @@ exports.clearAllLocalStorage = clearAllLocalStorage;
38
42
  const setLocalStorageItem = (key, value, ttl) => {
39
43
  try {
40
44
  if (typeof window === 'undefined') {
45
+ nodeLocalStorage[key] = value;
41
46
  return;
42
47
  }
43
48
  const set = {
@@ -54,7 +59,7 @@ const setLocalStorageItem = (key, value, ttl) => {
54
59
  exports.setLocalStorageItem = setLocalStorageItem;
55
60
  const getLocalStorageItem = (key, initialValue, ttl) => {
56
61
  if (typeof window === 'undefined') {
57
- return initialValue;
62
+ return nodeLocalStorage[key] || initialValue;
58
63
  }
59
64
  const itemraw = window.localStorage.getItem(key);
60
65
  const item = (0, object_1.tryJsonParse)(itemraw, undefined);
@@ -78,9 +83,6 @@ function UseLocalStorage(key, initialValue, ttl) {
78
83
  // Return a wrapped version of useState's setter function that ...
79
84
  // ... persists the new value to localStorage.
80
85
  const setValue = (value) => {
81
- if (typeof window === 'undefined') {
82
- return;
83
- }
84
86
  const valueToStore = (value instanceof Function ? value(storedValue) : value);
85
87
  (0, exports.setLocalStorageItem)(key, valueToStore, ttl);
86
88
  setT(new Date().getTime());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.94",
3
+ "version": "0.0.98",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",