@carbonorm/carbonnode 3.11.0 → 4.0.1

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,46 +1,87 @@
1
- import {AxiosPromise} from "axios";
1
+ import { AxiosPromise } from "axios";
2
2
  import { iCacheAPI } from "../types/ormInterfaces";
3
3
 
4
- // do not remove entries from this array. It is used to track the progress of API requests.
5
- // position in array is important. Do not sort. To not add to begging.
6
- export let apiRequestCache: iCacheAPI[] = [];
7
-
8
- export let userCustomClearCache: (() => void)[] = [];
9
-
10
- interface iClearCache {
11
- ignoreWarning: boolean
12
- }
13
-
14
- export function clearCache(props?: iClearCache) {
15
-
16
- if (false === props?.ignoreWarning) {
17
-
18
- console.warn('The rest api clearCache should only be used with extreme care! Avoid using this in favor of using `cacheResults : boolean`.')
19
-
4
+ // -----------------------------------------------------------------------------
5
+ // Cache Storage
6
+ // -----------------------------------------------------------------------------
7
+ export const apiRequestCache = new Map<string, iCacheAPI>();
8
+ export const userCustomClearCache: (() => void)[] = [];
9
+
10
+ // -----------------------------------------------------------------------------
11
+ // Cache Key Generator (safe, fixed-size ~40 chars)
12
+ // -----------------------------------------------------------------------------
13
+ // -----------------------------------------------------------------------------
14
+ // Browser-safe deterministic hash (FNV-1a)
15
+ // -----------------------------------------------------------------------------
16
+ function fnv1a(str: string): string {
17
+ let h = 0x811c9dc5;
18
+ for (let i = 0; i < str.length; i++) {
19
+ h ^= str.charCodeAt(i);
20
+ h = (h * 0x01000193) >>> 0;
20
21
  }
21
-
22
- userCustomClearCache.map((f) => 'function' === typeof f && f());
23
-
24
- userCustomClearCache = apiRequestCache = []
25
-
22
+ return h.toString(16);
26
23
  }
27
24
 
28
- export function checkCache<ResponseDataType = any, RestShortTableNames = string>(cacheResult: iCacheAPI<ResponseDataType>, requestMethod: string, tableName: RestShortTableNames | RestShortTableNames[], request: any): false | AxiosPromise<ResponseDataType> {
29
-
30
- if (undefined === cacheResult) {
31
-
32
- return false;
25
+ function makeCacheKey(
26
+ method: string,
27
+ tableName: string | string[],
28
+ requestData: unknown,
29
+ ): string {
30
+ const raw = JSON.stringify([method, tableName, requestData]);
31
+ return fnv1a(raw);
32
+ }
33
33
 
34
+ // -----------------------------------------------------------------------------
35
+ // Clear Cache (no shared-array bugs)
36
+ // -----------------------------------------------------------------------------
37
+ export function clearCache(props?: { ignoreWarning?: boolean }): void {
38
+ if (!props?.ignoreWarning) {
39
+ console.warn(
40
+ "The REST API clearCache should only be used with extreme care!",
41
+ );
34
42
  }
35
43
 
36
- console.groupCollapsed('%c API: The request on (' + tableName + ') is in cache. Returning the request Promise!', 'color: #0c0')
37
-
38
- console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
39
-
40
- console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
44
+ for (const fn of userCustomClearCache) {
45
+ try {
46
+ fn();
47
+ } catch {}
48
+ }
41
49
 
42
- console.groupEnd()
50
+ apiRequestCache.clear();
51
+ }
43
52
 
44
- return cacheResult.request;
53
+ // -----------------------------------------------------------------------------
54
+ // Check Cache (dedupe via hashed key)
55
+ // -----------------------------------------------------------------------------
56
+ export function checkCache<ResponseDataType = any>(
57
+ method: string,
58
+ tableName: string | string[],
59
+ requestData: any,
60
+ ): AxiosPromise<ResponseDataType> | false {
61
+ const key = makeCacheKey(method, tableName, requestData);
62
+ const cached = apiRequestCache.get(key);
63
+
64
+ if (!cached) return false;
65
+
66
+ console.groupCollapsed(
67
+ `%c API cache hit for ${method} ${tableName}`,
68
+ "color:#0c0",
69
+ );
70
+ console.log("Request Data:", requestData);
71
+ console.groupEnd();
72
+
73
+ return cached.request;
74
+ }
45
75
 
76
+ // -----------------------------------------------------------------------------
77
+ // Store Cache Entry (drop-in compatible)
78
+ // -----------------------------------------------------------------------------
79
+ export function setCache<ResponseDataType = any>(
80
+ method: string,
81
+ tableName: string | string[],
82
+ requestData: any,
83
+ cacheEntry: iCacheAPI<ResponseDataType>,
84
+ ): void {
85
+ const key = makeCacheKey(method, tableName, requestData);
86
+ apiRequestCache.set(key, cacheEntry);
46
87
  }
@@ -2,13 +2,15 @@ import {apiRequestCache} from "./cacheManager";
2
2
 
3
3
  export function checkAllRequestsComplete(): true | (string[]) {
4
4
 
5
- const stillRunning = apiRequestCache.filter((cache) => undefined === cache.response)
5
+ const cacheEntries = Array.from(apiRequestCache.values())
6
+
7
+ const stillRunning = cacheEntries.filter((cache) => undefined === cache.response)
6
8
 
7
9
  if (stillRunning.length !== 0) {
8
10
 
9
11
  if (document === null || document === undefined) {
10
12
 
11
- throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(apiRequestCache) + ')')
13
+ throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(cacheEntries) + ')')
12
14
 
13
15
  }
14
16
 
@@ -21,4 +23,4 @@ export function checkAllRequestsComplete(): true | (string[]) {
21
23
 
22
24
  return true
23
25
 
24
- }
26
+ }
@@ -1,12 +1,5 @@
1
1
  const isNode = () => {
2
-
3
- console.log('Checking if running in Node.js environment...');
4
-
5
- const isNodeEnv = typeof process !== 'undefined' && !!process.versions?.node;
6
-
7
- console.log(`Is Node.js environment: ${isNodeEnv}`);
8
-
9
- return isNodeEnv;
2
+ return typeof process !== 'undefined' && !!process.versions?.node;
10
3
  }
11
4
 
12
5
  export default isNode;