ag-common 0.0.96 → 0.0.102

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.
@@ -1,4 +1,4 @@
1
- export declare const enforceDynamoProvisionCap: ({ tables, readsMax, writesMax, }: {
1
+ export declare const enforceDynamoProvisionCap: ({ tables, readsMax, writesMax, mustEqual, }: {
2
2
  tables: any[];
3
3
  /**
4
4
  * default 25
@@ -8,4 +8,8 @@ export declare const enforceDynamoProvisionCap: ({ tables, readsMax, writesMax,
8
8
  * default 25
9
9
  */
10
10
  writesMax?: number | undefined;
11
+ /**
12
+ * default false. if true, will throw if cap isnt met
13
+ */
14
+ mustEqual?: boolean | undefined;
11
15
  }) => void;
@@ -9,7 +9,7 @@ const extractSum = ({ str, regex }) => {
9
9
  return (0, math_1.sumArray)(((_a = str
10
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
11
  };
12
- const enforceDynamoProvisionCap = ({ tables, readsMax = 25, writesMax = 25, }) => {
12
+ const enforceDynamoProvisionCap = ({ tables, readsMax = 25, writesMax = 25, mustEqual = false, }) => {
13
13
  if (!tables || tables.length === 0) {
14
14
  (0, log_1.warn)('error in dynamo FT enforce');
15
15
  return;
@@ -19,14 +19,13 @@ const enforceDynamoProvisionCap = ({ tables, readsMax = 25, writesMax = 25, }) =
19
19
  const s = (0, string_1.safeStringify)(t.node._children.Resource.node.scope);
20
20
  const reads = extractSum({ str: s, regex: /readCapacityUnits.*/gim });
21
21
  const writes = extractSum({ str: s, regex: /writeCapacityUnits.*/gim });
22
- if (reads > readsMax) {
23
- (0, log_1.warn)(`dynamo table provisioned reads:${reads} greater than max:${readsMax}`);
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) {
24
25
  throw new Error('exceeded dynamo provision cap');
25
26
  }
26
- if (writes > writesMax) {
27
- (0, log_1.warn)(`dynamo table provisioned writes:${writes} greater than max:${writesMax}`);
28
- throw new Error('exceeded dynamo provision cap');
27
+ if (mustEqual && (reads !== readsMax || writes !== writesMax)) {
28
+ throw new Error(`dynamo provision cap not met`);
29
29
  }
30
- (0, log_1.warn)(`dynamo provisioned total: R=${reads} W=${writes}`);
31
30
  };
32
31
  exports.enforceDynamoProvisionCap = enforceDynamoProvisionCap;
@@ -1,14 +1,13 @@
1
1
  export declare const toBase64: (str: string) => string;
2
2
  export declare const fromBase64: (str: string) => string;
3
+ export declare const setNodeCookieDocument: (s: string) => void;
3
4
  export declare function setCookieWrapper<T>(cname: string, raw: T, exdays?: number): void;
4
5
  export declare function wipeCookies(cname: string): void;
5
- export declare function getCookieWrapper<T>({ cname, cookieDocument, defaultValue, }: {
6
+ export declare function getCookieWrapper<T>({ cname, defaultValue, }: {
6
7
  cname: string;
7
- cookieDocument?: string;
8
8
  defaultValue?: string;
9
9
  }): T;
10
- export declare function useCookie<T>({ key, defaultValue, cookieDocument, }: {
10
+ export declare function useCookie<T>({ key, defaultValue, }: {
11
11
  key: string;
12
12
  defaultValue?: string;
13
- cookieDocument?: string;
14
13
  }): [T, (v: T) => any];
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useCookie = exports.getCookieWrapper = exports.wipeCookies = exports.setCookieWrapper = exports.fromBase64 = exports.toBase64 = void 0;
3
+ exports.useCookie = exports.getCookieWrapper = exports.wipeCookies = exports.setCookieWrapper = exports.setNodeCookieDocument = exports.fromBase64 = exports.toBase64 = void 0;
4
4
  /* eslint-disable guard-for-in */
5
5
  /* eslint-disable no-restricted-syntax */
6
6
  const react_1 = require("react");
7
+ const log_1 = require("../../common/helpers/log");
7
8
  const expireDate = 'Thu, 01 Jan 1970 00:00:00 UTC';
8
9
  const maxCookieLen = 4000;
9
10
  const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
@@ -11,15 +12,32 @@ const toBase64 = (str) => Buffer.from(str).toString('base64');
11
12
  exports.toBase64 = toBase64;
12
13
  const fromBase64 = (str) => Buffer.from(decodeURIComponent(str), 'base64').toString();
13
14
  exports.fromBase64 = fromBase64;
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ if (!process.nodeLocalCookie === undefined) {
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ process.nodeLocalCookie = '';
19
+ }
20
+ const setNodeCookieDocument = (s) => {
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ process.nodeLocalCookie = s;
23
+ };
24
+ exports.setNodeCookieDocument = setNodeCookieDocument;
14
25
  function setCookie(cname, raw, exdays = 1) {
15
26
  const d = new Date();
16
27
  d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
17
28
  const expires = `expires=${!raw || exdays < 0 ? expireDate : d.toUTCString()}`;
29
+ if (typeof window === undefined) {
30
+ (0, log_1.warn)('cant set cookie with no window object');
31
+ return;
32
+ }
18
33
  document.cookie = `${cname}=${!raw ? '' : raw};${expires};path=/`;
19
34
  }
20
- function getCookie({ cname, cookieDocument, }) {
35
+ function getCookie({ cname }) {
21
36
  const name = `${cname}=`;
22
- const ca1 = cookieDocument || (typeof window !== 'undefined' && document.cookie);
37
+ const ca1 =
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ process.nodeLocalCookie ||
40
+ (typeof window !== 'undefined' && document.cookie);
23
41
  if (!ca1 || !(ca1 === null || ca1 === void 0 ? void 0 : ca1.trim())) {
24
42
  return undefined;
25
43
  }
@@ -65,14 +83,13 @@ function wipeCookies(cname) {
65
83
  }
66
84
  }
67
85
  exports.wipeCookies = wipeCookies;
68
- function getCookieWrapper({ cname, cookieDocument, defaultValue, }) {
86
+ function getCookieWrapper({ cname, defaultValue, }) {
69
87
  let raw = '';
70
88
  let currentCount = 0;
71
89
  // eslint-disable-next-line no-constant-condition
72
90
  while (true) {
73
91
  const newv = getCookie({
74
92
  cname: cname + currentCount,
75
- cookieDocument,
76
93
  });
77
94
  if (!newv) {
78
95
  if (currentCount === 0) {
@@ -97,8 +114,8 @@ function getCookieWrapper({ cname, cookieDocument, defaultValue, }) {
97
114
  }
98
115
  exports.getCookieWrapper = getCookieWrapper;
99
116
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
- function useCookie({ key, defaultValue, cookieDocument, }) {
101
- const [cookie, setC] = (0, react_1.useState)(getCookieWrapper({ cname: key, defaultValue, cookieDocument }));
117
+ function useCookie({ key, defaultValue, }) {
118
+ const [cookie, setC] = (0, react_1.useState)(getCookieWrapper({ cname: key, defaultValue }));
102
119
  return [
103
120
  cookie,
104
121
  (v) => {
@@ -5,8 +5,16 @@ 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
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ if (!process.nodeLocalStorage) {
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ process.nodeLocalStorage = {};
13
+ }
8
14
  const clearLocalStorageItem = (key) => {
9
15
  if (typeof window === 'undefined') {
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ delete process.nodeLocalStorage[key];
10
18
  return;
11
19
  }
12
20
  try {
@@ -20,6 +28,8 @@ exports.clearLocalStorageItem = clearLocalStorageItem;
20
28
  const clearAllLocalStorage = (except) => {
21
29
  try {
22
30
  if (typeof window === 'undefined') {
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ process.nodeLocalStorage = {};
23
33
  return;
24
34
  }
25
35
  for (let i = 0; i < localStorage.length; i += 1) {
@@ -38,6 +48,8 @@ exports.clearAllLocalStorage = clearAllLocalStorage;
38
48
  const setLocalStorageItem = (key, value, ttl) => {
39
49
  try {
40
50
  if (typeof window === 'undefined') {
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
+ process.nodeLocalStorage[key] = value;
41
53
  return;
42
54
  }
43
55
  const set = {
@@ -54,7 +66,9 @@ const setLocalStorageItem = (key, value, ttl) => {
54
66
  exports.setLocalStorageItem = setLocalStorageItem;
55
67
  const getLocalStorageItem = (key, initialValue, ttl) => {
56
68
  if (typeof window === 'undefined') {
57
- return initialValue;
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ const value = process.nodeLocalStorage[key] || initialValue;
71
+ return value;
58
72
  }
59
73
  const itemraw = window.localStorage.getItem(key);
60
74
  const item = (0, object_1.tryJsonParse)(itemraw, undefined);
@@ -70,7 +84,6 @@ const getLocalStorageItem = (key, initialValue, ttl) => {
70
84
  return itemv;
71
85
  };
72
86
  exports.getLocalStorageItem = getLocalStorageItem;
73
- // Hook
74
87
  function UseLocalStorage(key, initialValue, ttl) {
75
88
  const storedValue = (0, exports.getLocalStorageItem)(key, initialValue, ttl);
76
89
  //bump use of stored value
@@ -78,9 +91,6 @@ function UseLocalStorage(key, initialValue, ttl) {
78
91
  // Return a wrapped version of useState's setter function that ...
79
92
  // ... persists the new value to localStorage.
80
93
  const setValue = (value) => {
81
- if (typeof window === 'undefined') {
82
- return;
83
- }
84
94
  const valueToStore = (value instanceof Function ? value(storedValue) : value);
85
95
  (0, exports.setLocalStorageItem)(key, valueToStore, ttl);
86
96
  setT(new Date().getTime());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.96",
3
+ "version": "0.0.102",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",