@smithy/shared-ini-file-loader 2.0.13 → 2.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,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConfigData = void 0;
4
+ const types_1 = require("@smithy/types");
5
+ const loadSharedConfigFiles_1 = require("./loadSharedConfigFiles");
6
+ const getConfigData = (data) => Object.entries(data)
7
+ .filter(([key]) => {
8
+ const sections = key.split(loadSharedConfigFiles_1.CONFIG_PREFIX_SEPARATOR);
9
+ if (sections.length === 2 && Object.values(types_1.IniSectionType).includes(sections[0])) {
10
+ return true;
11
+ }
12
+ return false;
13
+ })
14
+ .reduce((acc, [key, value]) => {
15
+ const updatedKey = key.startsWith(types_1.IniSectionType.PROFILE) ? key.split(loadSharedConfigFiles_1.CONFIG_PREFIX_SEPARATOR)[1] : key;
16
+ acc[updatedKey] = value;
17
+ return acc;
18
+ }, {
19
+ ...(data.default && { default: data.default }),
20
+ });
21
+ exports.getConfigData = getConfigData;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getSsoSessionData = void 0;
4
- const ssoSessionKeyRegex = /^sso-session\s(["'])?([^\1]+)\1$/;
4
+ const types_1 = require("@smithy/types");
5
+ const loadSharedConfigFiles_1 = require("./loadSharedConfigFiles");
5
6
  const getSsoSessionData = (data) => Object.entries(data)
6
- .filter(([key]) => ssoSessionKeyRegex.test(key))
7
- .reduce((acc, [key, value]) => ({ ...acc, [ssoSessionKeyRegex.exec(key)[2]]: value }), {});
7
+ .filter(([key]) => key.startsWith(types_1.IniSectionType.SSO_SESSION + loadSharedConfigFiles_1.CONFIG_PREFIX_SEPARATOR))
8
+ .reduce((acc, [key, value]) => ({ ...acc, [key.split(loadSharedConfigFiles_1.CONFIG_PREFIX_SEPARATOR)[1]]: value }), {});
8
9
  exports.getSsoSessionData = getSsoSessionData;
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadSharedConfigFiles = void 0;
3
+ exports.loadSharedConfigFiles = exports.CONFIG_PREFIX_SEPARATOR = void 0;
4
+ const getConfigData_1 = require("./getConfigData");
4
5
  const getConfigFilepath_1 = require("./getConfigFilepath");
5
6
  const getCredentialsFilepath_1 = require("./getCredentialsFilepath");
6
- const getProfileData_1 = require("./getProfileData");
7
7
  const parseIni_1 = require("./parseIni");
8
8
  const slurpFile_1 = require("./slurpFile");
9
9
  const swallowError = () => ({});
10
+ exports.CONFIG_PREFIX_SEPARATOR = ".";
10
11
  const loadSharedConfigFiles = async (init = {}) => {
11
12
  const { filepath = (0, getCredentialsFilepath_1.getCredentialsFilepath)(), configFilepath = (0, getConfigFilepath_1.getConfigFilepath)() } = init;
12
13
  const parsedFiles = await Promise.all([
@@ -14,7 +15,7 @@ const loadSharedConfigFiles = async (init = {}) => {
14
15
  ignoreCache: init.ignoreCache,
15
16
  })
16
17
  .then(parseIni_1.parseIni)
17
- .then(getProfileData_1.getProfileData)
18
+ .then(getConfigData_1.getConfigData)
18
19
  .catch(swallowError),
19
20
  (0, slurpFile_1.slurpFile)(filepath, {
20
21
  ignoreCache: init.ignoreCache,
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseIni = void 0;
4
+ const types_1 = require("@smithy/types");
5
+ const loadSharedConfigFiles_1 = require("./loadSharedConfigFiles");
6
+ const prefixKeyRegex = /^([\w-]+)\s(["'])?([\w-]+)\2$/;
4
7
  const profileNameBlockList = ["__proto__", "profile __proto__"];
5
8
  const parseIni = (iniData) => {
6
9
  const map = {};
@@ -10,10 +13,21 @@ const parseIni = (iniData) => {
10
13
  line = line.split(/(^|\s)[;#]/)[0].trim();
11
14
  const isSection = line[0] === "[" && line[line.length - 1] === "]";
12
15
  if (isSection) {
16
+ currentSection = undefined;
13
17
  currentSubSection = undefined;
14
- currentSection = line.substring(1, line.length - 1);
15
- if (profileNameBlockList.includes(currentSection)) {
16
- throw new Error(`Found invalid profile name "${currentSection}"`);
18
+ const sectionName = line.substring(1, line.length - 1);
19
+ const matches = prefixKeyRegex.exec(sectionName);
20
+ if (matches) {
21
+ const [, prefix, , name] = matches;
22
+ if (Object.values(types_1.IniSectionType).includes(prefix)) {
23
+ currentSection = [prefix, name].join(loadSharedConfigFiles_1.CONFIG_PREFIX_SEPARATOR);
24
+ }
25
+ }
26
+ else {
27
+ currentSection = sectionName;
28
+ }
29
+ if (profileNameBlockList.includes(sectionName)) {
30
+ throw new Error(`Found invalid profile name "${sectionName}"`);
17
31
  }
18
32
  }
19
33
  else if (currentSection) {
@@ -26,9 +40,10 @@ const parseIni = (iniData) => {
26
40
  if (value === "") {
27
41
  currentSubSection = name;
28
42
  }
29
- else if (currentSubSection === undefined) {
43
+ else {
30
44
  map[currentSection] = map[currentSection] || {};
31
- map[currentSection][name] = value;
45
+ const key = currentSubSection ? [currentSubSection, name].join(loadSharedConfigFiles_1.CONFIG_PREFIX_SEPARATOR) : name;
46
+ map[currentSection][key] = value;
32
47
  }
33
48
  }
34
49
  }
@@ -0,0 +1,17 @@
1
+ import { IniSectionType } from "@smithy/types";
2
+ import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
3
+ export const getConfigData = (data) => Object.entries(data)
4
+ .filter(([key]) => {
5
+ const sections = key.split(CONFIG_PREFIX_SEPARATOR);
6
+ if (sections.length === 2 && Object.values(IniSectionType).includes(sections[0])) {
7
+ return true;
8
+ }
9
+ return false;
10
+ })
11
+ .reduce((acc, [key, value]) => {
12
+ const updatedKey = key.startsWith(IniSectionType.PROFILE) ? key.split(CONFIG_PREFIX_SEPARATOR)[1] : key;
13
+ acc[updatedKey] = value;
14
+ return acc;
15
+ }, {
16
+ ...(data.default && { default: data.default }),
17
+ });
@@ -1,4 +1,5 @@
1
- const ssoSessionKeyRegex = /^sso-session\s(["'])?([^\1]+)\1$/;
1
+ import { IniSectionType } from "@smithy/types";
2
+ import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
2
3
  export const getSsoSessionData = (data) => Object.entries(data)
3
- .filter(([key]) => ssoSessionKeyRegex.test(key))
4
- .reduce((acc, [key, value]) => ({ ...acc, [ssoSessionKeyRegex.exec(key)[2]]: value }), {});
4
+ .filter(([key]) => key.startsWith(IniSectionType.SSO_SESSION + CONFIG_PREFIX_SEPARATOR))
5
+ .reduce((acc, [key, value]) => ({ ...acc, [key.split(CONFIG_PREFIX_SEPARATOR)[1]]: value }), {});
@@ -1,9 +1,10 @@
1
+ import { getConfigData } from "./getConfigData";
1
2
  import { getConfigFilepath } from "./getConfigFilepath";
2
3
  import { getCredentialsFilepath } from "./getCredentialsFilepath";
3
- import { getProfileData } from "./getProfileData";
4
4
  import { parseIni } from "./parseIni";
5
5
  import { slurpFile } from "./slurpFile";
6
6
  const swallowError = () => ({});
7
+ export const CONFIG_PREFIX_SEPARATOR = ".";
7
8
  export const loadSharedConfigFiles = async (init = {}) => {
8
9
  const { filepath = getCredentialsFilepath(), configFilepath = getConfigFilepath() } = init;
9
10
  const parsedFiles = await Promise.all([
@@ -11,7 +12,7 @@ export const loadSharedConfigFiles = async (init = {}) => {
11
12
  ignoreCache: init.ignoreCache,
12
13
  })
13
14
  .then(parseIni)
14
- .then(getProfileData)
15
+ .then(getConfigData)
15
16
  .catch(swallowError),
16
17
  slurpFile(filepath, {
17
18
  ignoreCache: init.ignoreCache,
@@ -1,3 +1,6 @@
1
+ import { IniSectionType } from "@smithy/types";
2
+ import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
3
+ const prefixKeyRegex = /^([\w-]+)\s(["'])?([\w-]+)\2$/;
1
4
  const profileNameBlockList = ["__proto__", "profile __proto__"];
2
5
  export const parseIni = (iniData) => {
3
6
  const map = {};
@@ -7,10 +10,21 @@ export const parseIni = (iniData) => {
7
10
  line = line.split(/(^|\s)[;#]/)[0].trim();
8
11
  const isSection = line[0] === "[" && line[line.length - 1] === "]";
9
12
  if (isSection) {
13
+ currentSection = undefined;
10
14
  currentSubSection = undefined;
11
- currentSection = line.substring(1, line.length - 1);
12
- if (profileNameBlockList.includes(currentSection)) {
13
- throw new Error(`Found invalid profile name "${currentSection}"`);
15
+ const sectionName = line.substring(1, line.length - 1);
16
+ const matches = prefixKeyRegex.exec(sectionName);
17
+ if (matches) {
18
+ const [, prefix, , name] = matches;
19
+ if (Object.values(IniSectionType).includes(prefix)) {
20
+ currentSection = [prefix, name].join(CONFIG_PREFIX_SEPARATOR);
21
+ }
22
+ }
23
+ else {
24
+ currentSection = sectionName;
25
+ }
26
+ if (profileNameBlockList.includes(sectionName)) {
27
+ throw new Error(`Found invalid profile name "${sectionName}"`);
14
28
  }
15
29
  }
16
30
  else if (currentSection) {
@@ -23,9 +37,10 @@ export const parseIni = (iniData) => {
23
37
  if (value === "") {
24
38
  currentSubSection = name;
25
39
  }
26
- else if (currentSubSection === undefined) {
40
+ else {
27
41
  map[currentSection] = map[currentSection] || {};
28
- map[currentSection][name] = value;
42
+ const key = currentSubSection ? [currentSubSection, name].join(CONFIG_PREFIX_SEPARATOR) : name;
43
+ map[currentSection][key] = value;
29
44
  }
30
45
  }
31
46
  }
@@ -0,0 +1,8 @@
1
+ import { ParsedIniData } from "@smithy/types";
2
+ /**
3
+ * Returns the config data from parsed ini data.
4
+ * * Returns data for `default`
5
+ * * Returns profile name without prefix.
6
+ * * Returns non-profiles as is.
7
+ */
8
+ export declare const getConfigData: (data: ParsedIniData) => ParsedIniData;
@@ -18,4 +18,5 @@ export interface SharedConfigInit {
18
18
  */
19
19
  ignoreCache?: boolean;
20
20
  }
21
+ export declare const CONFIG_PREFIX_SEPARATOR = ".";
21
22
  export declare const loadSharedConfigFiles: (init?: SharedConfigInit) => Promise<SharedConfigFiles>;
@@ -0,0 +1,8 @@
1
+ import { ParsedIniData } from "@smithy/types";
2
+ /**
3
+ * Returns the config data from parsed ini data.
4
+ * * Returns data for `default`
5
+ * * Returns profile name without prefix.
6
+ * * Returns non-profiles as is.
7
+ */
8
+ export declare const getConfigData: (data: ParsedIniData) => ParsedIniData;
@@ -18,4 +18,5 @@ export interface SharedConfigInit {
18
18
  */
19
19
  ignoreCache?: boolean;
20
20
  }
21
+ export declare const CONFIG_PREFIX_SEPARATOR = ".";
21
22
  export declare const loadSharedConfigFiles: (init?: SharedConfigInit) => Promise<SharedConfigFiles>;
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@smithy/shared-ini-file-loader",
3
- "version": "2.0.13",
3
+ "version": "2.2.0",
4
4
  "dependencies": {
5
- "@smithy/types": "^2.3.4",
5
+ "@smithy/types": "^2.3.5",
6
6
  "tslib": "^2.5.0"
7
7
  },
8
8
  "devDependencies": {
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getProfileData = void 0;
4
- const profileKeyRegex = /^profile\s(["'])?([^\1]+)\1$/;
5
- const getProfileData = (data) => Object.entries(data)
6
- .filter(([key]) => profileKeyRegex.test(key))
7
- .reduce((acc, [key, value]) => ({ ...acc, [profileKeyRegex.exec(key)[2]]: value }), {
8
- ...(data.default && { default: data.default }),
9
- });
10
- exports.getProfileData = getProfileData;
@@ -1,6 +0,0 @@
1
- const profileKeyRegex = /^profile\s(["'])?([^\1]+)\1$/;
2
- export const getProfileData = (data) => Object.entries(data)
3
- .filter(([key]) => profileKeyRegex.test(key))
4
- .reduce((acc, [key, value]) => ({ ...acc, [profileKeyRegex.exec(key)[2]]: value }), {
5
- ...(data.default && { default: data.default }),
6
- });
@@ -1,7 +0,0 @@
1
- import { ParsedIniData } from "@smithy/types";
2
- /**
3
- * Returns the profile data from parsed ini data.
4
- * * Returns data for `default`
5
- * * Reads profileName after profile prefix including/excluding quotes
6
- */
7
- export declare const getProfileData: (data: ParsedIniData) => ParsedIniData;
@@ -1,7 +0,0 @@
1
- import { ParsedIniData } from "@smithy/types";
2
- /**
3
- * Returns the profile data from parsed ini data.
4
- * * Returns data for `default`
5
- * * Reads profileName after profile prefix including/excluding quotes
6
- */
7
- export declare const getProfileData: (data: ParsedIniData) => ParsedIniData;