@koralabs/kora-labs-common 6.4.19 → 6.4.21

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/aws/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { decryptKmsCiphertext, hydrateKmsEnvironment } from './kmsEnvironment';
1
+ export { decryptKmsCiphertext, hydrateKmsEnvironment, loadAfterHydratingKmsEnvironment } from './kmsEnvironment';
2
2
  export type { KmsClientLike } from './kmsEnvironment';
package/aws/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hydrateKmsEnvironment = exports.decryptKmsCiphertext = void 0;
3
+ exports.loadAfterHydratingKmsEnvironment = exports.hydrateKmsEnvironment = exports.decryptKmsCiphertext = void 0;
4
4
  var kmsEnvironment_1 = require("./kmsEnvironment");
5
5
  Object.defineProperty(exports, "decryptKmsCiphertext", { enumerable: true, get: function () { return kmsEnvironment_1.decryptKmsCiphertext; } });
6
6
  Object.defineProperty(exports, "hydrateKmsEnvironment", { enumerable: true, get: function () { return kmsEnvironment_1.hydrateKmsEnvironment; } });
7
+ Object.defineProperty(exports, "loadAfterHydratingKmsEnvironment", { enumerable: true, get: function () { return kmsEnvironment_1.loadAfterHydratingKmsEnvironment; } });
@@ -7,3 +7,8 @@ export declare function hydrateKmsEnvironment({ env, client, keys }?: {
7
7
  client?: KmsClientLike;
8
8
  keys?: string[];
9
9
  }): Promise<string[]>;
10
+ export declare function loadAfterHydratingKmsEnvironment<T>(load: () => Promise<T>, options?: {
11
+ env?: NodeJS.ProcessEnv;
12
+ client?: KmsClientLike;
13
+ keys?: string[];
14
+ }): Promise<T>;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hydrateKmsEnvironment = exports.decryptKmsCiphertext = void 0;
3
+ exports.loadAfterHydratingKmsEnvironment = exports.hydrateKmsEnvironment = exports.decryptKmsCiphertext = void 0;
4
4
  const client_kms_1 = require("@aws-sdk/client-kms");
5
+ const KMS_ENV_BUNDLE_KEY = 'KMS_ENV_BUNDLE_ENC';
5
6
  async function decryptKmsCiphertext(ciphertext, client = new client_kms_1.KMSClient({})) {
6
7
  const response = await client.send(new client_kms_1.DecryptCommand({
7
8
  CiphertextBlob: Buffer.from(ciphertext, 'base64')
@@ -13,10 +14,35 @@ async function decryptKmsCiphertext(ciphertext, client = new client_kms_1.KMSCli
13
14
  }
14
15
  exports.decryptKmsCiphertext = decryptKmsCiphertext;
15
16
  async function hydrateKmsEnvironment({ env = process.env, client = new client_kms_1.KMSClient({}), keys } = {}) {
17
+ const hydratedKeys = [];
18
+ const bundleCiphertext = env[KMS_ENV_BUNDLE_KEY];
19
+ if (bundleCiphertext) {
20
+ const plaintext = await decryptKmsCiphertext(bundleCiphertext, client);
21
+ let bundle;
22
+ try {
23
+ bundle = JSON.parse(plaintext);
24
+ }
25
+ catch (_a) {
26
+ throw new Error('KMS env bundle plaintext must be a JSON object');
27
+ }
28
+ if (!bundle || typeof bundle !== 'object' || Array.isArray(bundle)) {
29
+ throw new Error('KMS env bundle plaintext must be a JSON object');
30
+ }
31
+ for (const key of Object.keys(bundle).sort()) {
32
+ if (env[key]) {
33
+ continue;
34
+ }
35
+ const value = bundle[key];
36
+ if (typeof value !== 'string') {
37
+ throw new Error(`KMS env bundle value for ${key} must be a string`);
38
+ }
39
+ env[key] = value;
40
+ hydratedKeys.push(key);
41
+ }
42
+ }
16
43
  const candidateKeys = (keys !== null && keys !== void 0 ? keys : Object.keys(env)
17
- .filter((key) => key.endsWith('_ENC'))
44
+ .filter((key) => key.endsWith('_ENC') && key !== KMS_ENV_BUNDLE_KEY)
18
45
  .map((key) => key.slice(0, -4))).sort();
19
- const hydratedKeys = [];
20
46
  for (const key of candidateKeys) {
21
47
  if (env[key]) {
22
48
  continue;
@@ -31,3 +57,8 @@ async function hydrateKmsEnvironment({ env = process.env, client = new client_km
31
57
  return hydratedKeys;
32
58
  }
33
59
  exports.hydrateKmsEnvironment = hydrateKmsEnvironment;
60
+ async function loadAfterHydratingKmsEnvironment(load, options) {
61
+ await hydrateKmsEnvironment(options);
62
+ return load();
63
+ }
64
+ exports.loadAfterHydratingKmsEnvironment = loadAfterHydratingKmsEnvironment;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koralabs/kora-labs-common",
3
- "version": "6.4.19",
3
+ "version": "6.4.21",
4
4
  "description": "Kora Labs Common Utilities",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",