@lambada/utils 1.1.1 → 1.2.0

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,31 @@
1
+ import { SecretsManager } from 'aws-sdk';
2
+ export declare class CachedSecret {
3
+ readonly value: string;
4
+ readonly ttl: number;
5
+ readonly expiresAt: number;
6
+ constructor(value: string, ttl: number);
7
+ hasExpired(): boolean;
8
+ }
9
+ export declare type SecretsManagerCacheOptions = Partial<SecretsManagerCacheConfig>;
10
+ interface SecretsManagerCacheConfig {
11
+ /**
12
+ * How many milliseconds to cache each secret for.
13
+ * @default 300000
14
+ */
15
+ ttl: number;
16
+ /** AWS SDK SecretsManager instance */
17
+ secretsManager: SecretsManager;
18
+ }
19
+ export declare class SecretsManagerCache {
20
+ config: SecretsManagerCacheConfig;
21
+ private cache;
22
+ constructor(options?: SecretsManagerCacheOptions);
23
+ /**
24
+ * Fetches a secret from SecretsManager and caches it as long as the given
25
+ * `ttl`.
26
+ */
27
+ getSecret(secretName: string, isJSON?: boolean): Promise<string | undefined>;
28
+ private static _manager;
29
+ static get(): SecretsManagerCache;
30
+ }
31
+ export {};
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ // FROM https://github.com/c-bandy/aws-secrets-manager-cache
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.SecretsManagerCache = exports.CachedSecret = void 0;
14
+ const aws_sdk_1 = require("aws-sdk");
15
+ const defaultTTL = (5 * 60 * 1000); // 5 minutes
16
+ class CachedSecret {
17
+ constructor(value, ttl) {
18
+ this.value = value;
19
+ this.ttl = ttl;
20
+ this.expiresAt = Date.now() + ttl;
21
+ }
22
+ hasExpired() {
23
+ return (Date.now() > this.expiresAt);
24
+ }
25
+ }
26
+ exports.CachedSecret = CachedSecret;
27
+ class SecretsManagerCache {
28
+ constructor(options) {
29
+ this.cache = new Map();
30
+ this.config = Object.assign({
31
+ // set defaults
32
+ ttl: defaultTTL, secretsManager: new aws_sdk_1.SecretsManager() }, options);
33
+ }
34
+ /**
35
+ * Fetches a secret from SecretsManager and caches it as long as the given
36
+ * `ttl`.
37
+ */
38
+ getSecret(secretName, isJSON = false) {
39
+ var _a, _b;
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ const itemExistsInCache = this.cache.has(secretName);
42
+ const itemHasExpired = (_a = this.cache.get(secretName)) === null || _a === void 0 ? void 0 : _a.hasExpired();
43
+ if (!itemExistsInCache || itemHasExpired) {
44
+ const getSecretValueResponse = yield this.config.secretsManager
45
+ .getSecretValue({ SecretId: secretName })
46
+ .promise();
47
+ if (getSecretValueResponse.SecretString) {
48
+ this.cache.set(secretName, new CachedSecret(getSecretValueResponse.SecretString, this.config.ttl));
49
+ }
50
+ }
51
+ const secret = (_b = this.cache.get(secretName)) === null || _b === void 0 ? void 0 : _b.value;
52
+ if (isJSON) {
53
+ try {
54
+ return JSON.parse(secret);
55
+ }
56
+ catch (error) {
57
+ throw new Error('Attempted to parse non-JSON secret string as JSON.');
58
+ }
59
+ }
60
+ return secret;
61
+ });
62
+ }
63
+ static get() {
64
+ return this._manager;
65
+ }
66
+ }
67
+ exports.SecretsManagerCache = SecretsManagerCache;
68
+ SecretsManagerCache._manager = new SecretsManagerCache();
69
+ //# sourceMappingURL=secretManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secretManager.js","sourceRoot":"","sources":["../src/secretManager.ts"],"names":[],"mappings":";AAAA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AsB,CAAC,YAAY,EACnC,IAAI,CAAC,MAAM,CAAC,GAAG,CAClB,CACJ,CAAC;iBACL;aACJ;YAED,MAAM,MAAM,GAAG,MAAA,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,0CAAE,KAAK,CAAC;YAEjD,IAAI,MAAM,EAAE;gBACR,IAAI;oBACA,OAAO,IAAI,CAAC,KAAK,CAAC,MAAa,CAAC,CAAC;iBACpC;gBAAC,OAAO,KAAK,EAAE;oBACZ,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;iBACxE;aACJ;YAED,OAAO,MAAM,CAAC;;KACjB;IAGM,MAAM,CAAC,GAAG;QACb,OAAO,IAAI,CAAC,QAAQ,CAAA;IACxB,CAAC;;AAtDL,kDAuDC;AAJkB,4BAAQ,GAAG,IAAI,mBAAmB,EAAE,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambada/utils",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -30,5 +30,5 @@
30
30
  "@aws/dynamodb-auto-marshaller": "0.7.1"
31
31
  },
32
32
  "description": "",
33
- "gitHead": "f26fc9347992e4d0a1b9c203a4139dff4b459f2b"
33
+ "gitHead": "6dbdd5ef70c63fe6a1f616f5a8052c2ffca4ea36"
34
34
  }