gcf-common-lib 0.8.2 → 0.9.2

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.
Files changed (3) hide show
  1. package/index.js +19 -2
  2. package/index.ts +16 -2
  3. package/package.json +4 -3
package/index.js CHANGED
@@ -10,12 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.GcfCommon = exports.storage = exports.pubSub = void 0;
13
- const lodash_1 = require("lodash");
14
- const storage_1 = require("@google-cloud/storage");
15
13
  const pubsub_1 = require("@google-cloud/pubsub");
14
+ const secret_manager_1 = require("@google-cloud/secret-manager");
15
+ const storage_1 = require("@google-cloud/storage");
16
+ const lodash_1 = require("lodash");
16
17
  const utils_1 = require("./utils");
17
18
  exports.pubSub = new pubsub_1.PubSub();
18
19
  exports.storage = new storage_1.Storage();
20
+ const secretClient = new secret_manager_1.SecretManagerServiceClient();
19
21
  class GcfCommon {
20
22
  static process(event, context, handler, timeout = 535) {
21
23
  return __awaiter(this, void 0, void 0, function* () {
@@ -79,5 +81,20 @@ class GcfCommon {
79
81
  }
80
82
  });
81
83
  }
84
+ static getSecret(name, version) {
85
+ var _a, _b;
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ const projectId = yield secretClient.getProjectId();
88
+ const secretName = `projects/${projectId}/secrets/${name}`;
89
+ const secretVersion = `${secretName}/versions/${version !== null && version !== void 0 ? version : 'latest'}`;
90
+ const [response] = yield secretClient.accessSecretVersion({ name: secretVersion });
91
+ return (_b = (_a = response === null || response === void 0 ? void 0 : response.payload) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.toString();
92
+ });
93
+ }
94
+ static getSecrets(names, versions) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ return Promise.all(names.map((name, idx) => __awaiter(this, void 0, void 0, function* () { return this.getSecret(name, versions === null || versions === void 0 ? void 0 : versions[idx]); })));
97
+ });
98
+ }
82
99
  }
83
100
  exports.GcfCommon = GcfCommon;
package/index.ts CHANGED
@@ -1,6 +1,7 @@
1
- import {isEmpty, noop} from 'lodash';
2
- import {File, Storage} from "@google-cloud/storage";
3
1
  import {PubSub} from "@google-cloud/pubsub";
2
+ import {SecretManagerServiceClient} from "@google-cloud/secret-manager";
3
+ import {File, Storage} from "@google-cloud/storage";
4
+ import {isEmpty, noop} from 'lodash';
4
5
  import {timeoutAfter} from "./utils";
5
6
 
6
7
  export type TEvent = {
@@ -45,6 +46,7 @@ export type TResponse = {
45
46
 
46
47
  export const pubSub = new PubSub();
47
48
  export const storage = new Storage();
49
+ const secretClient = new SecretManagerServiceClient();
48
50
 
49
51
  export class GcfCommon {
50
52
 
@@ -118,4 +120,16 @@ export class GcfCommon {
118
120
  return JSON.parse(meta?.metadata?.options ?? '{}');
119
121
  }
120
122
  }
123
+
124
+ static async getSecret(name: string, version?: string) {
125
+ const projectId = await secretClient.getProjectId();
126
+ const secretName = `projects/${projectId}/secrets/${name}`;
127
+ const secretVersion = `${secretName}/versions/${version ?? 'latest'}`;
128
+ const [response] = await secretClient.accessSecretVersion({name: secretVersion});
129
+ return response?.payload?.data?.toString();
130
+ }
131
+
132
+ static async getSecrets(names: string[], versions?: string[]) {
133
+ return Promise.all(names.map(async (name, idx) => this.getSecret(name, versions?.[idx])));
134
+ }
121
135
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.8.2",
4
+ "version": "0.9.2",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
@@ -18,12 +18,13 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@google-cloud/pubsub": "^2.18.4",
21
+ "@google-cloud/secret-manager": "^3.10.1",
21
22
  "@google-cloud/storage": "^5.17.0",
22
23
  "lodash": "^4.17.21"
23
24
  },
24
25
  "devDependencies": {
25
- "@types/lodash": "^4.14.178",
26
- "@tsconfig/node14": "^1.0.1"
26
+ "@tsconfig/node14": "^1.0.1",
27
+ "@types/lodash": "^4.14.178"
27
28
  },
28
29
  "keywords": [],
29
30
  "author": "alert83@gmail.com",