@spytecgps/lambda-utils 1.0.2 → 1.0.4

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.
package/dist/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './validation';
4
4
  export * from './types';
5
5
  export * from './middleware';
6
6
  export * from './logger/logger';
7
+ export * from './utils';
package/dist/index.js CHANGED
@@ -16,3 +16,4 @@ __exportStar(require("./validation"), exports);
16
16
  __exportStar(require("./types"), exports);
17
17
  __exportStar(require("./middleware"), exports);
18
18
  __exportStar(require("./logger/logger"), exports);
19
+ __exportStar(require("./utils"), exports);
package/dist/types.d.ts CHANGED
@@ -9,13 +9,13 @@ export interface SpytecAuthorizedResources {
9
9
  }
10
10
  export declare type AuthClass = 'user' | 'client' | 'system';
11
11
  export declare type DevicesAccess = 'full' | 'limited';
12
- export declare type BoundaryAccess = 'full' | 'limited';
12
+ export declare type BoundariesAccess = 'full' | 'limited';
13
13
  export interface SpytecAuthContext {
14
14
  type: AuthClass;
15
15
  userId?: string;
16
16
  clientId: number;
17
17
  devicesAccess: DevicesAccess;
18
- boundaryAccess: BoundaryAccess;
18
+ boundariesAccess: BoundariesAccess;
19
19
  minAccessDate?: string;
20
20
  resources: SpytecAuthorizedResources;
21
21
  principalId: string;
@@ -0,0 +1,23 @@
1
+ export declare class LambdaCache {
2
+ private collectionName;
3
+ /**
4
+ * @param {String} collectionName (not required) - The collection key used to store the cache values.
5
+ * If not provide default collection name uses :
6
+ * ${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_FUNCTION_VERSION}
7
+ * */
8
+ constructor(collectionName?: string);
9
+ /**
10
+ * @param {String} key (required) - cache key
11
+ * @param {Object} value (required) - cache value
12
+ * @param {Number} expire (required) - cache expiration time (seconds)
13
+ * */
14
+ set(key: string, value: unknown, ttl: number): void;
15
+ /**
16
+ * @param {String} key (required) - cache key to get
17
+ * */
18
+ get(key: string): unknown;
19
+ /**
20
+ * @param {String} key (required) - cache key to remove
21
+ * */
22
+ remove(key: any): void;
23
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LambdaCache = void 0;
4
+ var LambdaCache = /** @class */ (function () {
5
+ /**
6
+ * @param {String} collectionName (not required) - The collection key used to store the cache values.
7
+ * If not provide default collection name uses :
8
+ * ${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_FUNCTION_VERSION}
9
+ * */
10
+ function LambdaCache(collectionName) {
11
+ this.collectionName = collectionName !== null && collectionName !== void 0 ? collectionName : process.env.AWS_LAMBDA_FUNCTION_NAME + "-" + process.env.AWS_LAMBDA_FUNCTION_VERSION;
12
+ if (!global['CACHE_STORAGE']) {
13
+ global['CACHE_STORAGE'] = {};
14
+ }
15
+ if (!global['CACHE_STORAGE'][this.collectionName]) {
16
+ global['CACHE_STORAGE'][this.collectionName] = new Map();
17
+ }
18
+ }
19
+ /**
20
+ * @param {String} key (required) - cache key
21
+ * @param {Object} value (required) - cache value
22
+ * @param {Number} expire (required) - cache expiration time (seconds)
23
+ * */
24
+ LambdaCache.prototype.set = function (key, value, ttl) {
25
+ var expire = 1000 * ttl + Date.now();
26
+ global['CACHE_STORAGE'][this.collectionName].set(key, { value: value, expire: expire });
27
+ };
28
+ /**
29
+ * @param {String} key (required) - cache key to get
30
+ * */
31
+ LambdaCache.prototype.get = function (key) {
32
+ if (!key) {
33
+ throw new Error('key is required!');
34
+ }
35
+ var record = global['CACHE_STORAGE'][this.collectionName].get(key);
36
+ if (!record) {
37
+ return null;
38
+ }
39
+ if (!record.expire || record.expire > Date.now()) {
40
+ return record.value;
41
+ }
42
+ else {
43
+ return this.remove(key);
44
+ }
45
+ };
46
+ /**
47
+ * @param {String} key (required) - cache key to remove
48
+ * */
49
+ LambdaCache.prototype.remove = function (key) {
50
+ var record = global['CACHE_STORAGE'][this.collectionName].get(key);
51
+ if (!record) {
52
+ return;
53
+ }
54
+ global['CACHE_STORAGE'][this.collectionName].delete(key);
55
+ };
56
+ return LambdaCache;
57
+ }());
58
+ exports.LambdaCache = LambdaCache;
@@ -0,0 +1,2 @@
1
+ import { LambdaCache } from './cache';
2
+ export { LambdaCache };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LambdaCache = void 0;
4
+ var cache_1 = require("./cache");
5
+ Object.defineProperty(exports, "LambdaCache", { enumerable: true, get: function () { return cache_1.LambdaCache; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spytecgps/lambda-utils",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Lambda Utils",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",