@scaleway/configuration-loader 2.5.0 → 2.7.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.
- package/CHANGELOG.md +15 -1
- package/dist/config-loader.d.ts +63 -0
- package/dist/config-loader.js +112 -0
- package/dist/env.d.ts +24 -0
- package/dist/env.js +28 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/path-resolver.d.ts +16 -0
- package/dist/path-resolver.js +31 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.js +0 -0
- package/dist/yml-loader.d.ts +51 -0
- package/dist/yml-loader.js +88 -0
- package/package.json +3 -6
- package/biome.jsonc +0 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 2.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- fix(oxlint): enable eslint/curly as error (#3511)
|
|
8
|
+
|
|
9
|
+
## 2.6.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- chore(engines): remove node engines (#3336)
|
|
14
|
+
|
|
3
15
|
## 2.5.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
6
18
|
|
|
7
|
-
-
|
|
19
|
+
- chore(lerna): remove lerna and use pnpm auto release repo pkg (#3174)
|
|
20
|
+
|
|
21
|
+
- fix(release): run by commit and github release cli (#3185)
|
|
8
22
|
|
|
9
23
|
All notable changes to this project will be documented in this file.
|
|
10
24
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { AllProfilesFromFileParams, Profile, ProfileFromFileParams } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Loads profile from environment values.
|
|
4
|
+
*
|
|
5
|
+
* @returns The profile filled with values found in the environment
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare const loadProfileFromEnvironmentValues: () => Profile;
|
|
10
|
+
/**
|
|
11
|
+
* Loads all the profiles from configuration file.
|
|
12
|
+
*
|
|
13
|
+
* @param params - The parameters to load the profile
|
|
14
|
+
* @returns The profiles filled with values found in the configuration profile
|
|
15
|
+
*
|
|
16
|
+
* @throws Error
|
|
17
|
+
* Thrown if the configuration file couldn't be found.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export declare const loadAllProfilesFromConfigurationFile: (params?: Readonly<AllProfilesFromFileParams>) => Record<string, Profile>;
|
|
22
|
+
/**
|
|
23
|
+
* Loads profile from configuration file.
|
|
24
|
+
*
|
|
25
|
+
* @param params - The parameters to load the profile
|
|
26
|
+
* @returns The profile filled with values found in the configuration profile
|
|
27
|
+
*
|
|
28
|
+
* @throws Error
|
|
29
|
+
* Thrown if the configuration file couldn't be found,
|
|
30
|
+
* or if the specified profile can't be found.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare const loadProfileFromConfigurationFile: (params?: Readonly<ProfileFromFileParams>) => Profile;
|
|
35
|
+
/**
|
|
36
|
+
* Loads all profiles from configuration file (asynchronous).
|
|
37
|
+
*
|
|
38
|
+
* Non-blocking alternative to {@link loadAllProfilesFromConfigurationFile}.
|
|
39
|
+
*
|
|
40
|
+
* @param params - The parameters to load the profile
|
|
41
|
+
* @returns The profiles filled with values found in the configuration profile
|
|
42
|
+
*
|
|
43
|
+
* @throws Error
|
|
44
|
+
* Thrown if the configuration file couldn't be found.
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export declare const loadAllProfilesFromConfigurationFileAsync: (params?: Readonly<AllProfilesFromFileParams>) => Promise<Record<string, Profile>>;
|
|
49
|
+
/**
|
|
50
|
+
* Loads profile from configuration file (asynchronous).
|
|
51
|
+
*
|
|
52
|
+
* Non-blocking alternative to {@link loadProfileFromConfigurationFile}.
|
|
53
|
+
*
|
|
54
|
+
* @param params - The parameters to load the profile
|
|
55
|
+
* @returns The profile filled with values found in the configuration profile
|
|
56
|
+
*
|
|
57
|
+
* @throws Error
|
|
58
|
+
* Thrown if the configuration file couldn't be found,
|
|
59
|
+
* or if the specified profile can't be found.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export declare const loadProfileFromConfigurationFileAsync: (params?: Readonly<ProfileFromFileParams>) => Promise<Profile>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { EnvironmentKey } from "./env.js";
|
|
2
|
+
import { resolveConfigurationFilePath } from "./path-resolver.js";
|
|
3
|
+
import { loadConfigurationFromFile, loadConfigurationFromFileAsync } from "./yml-loader.js";
|
|
4
|
+
import { env } from "node:process";
|
|
5
|
+
//#region src/config-loader.ts
|
|
6
|
+
var convertFileConfigToSDK = (obj) => ({
|
|
7
|
+
accessKey: obj.access_key,
|
|
8
|
+
apiURL: obj.api_url,
|
|
9
|
+
defaultOrganizationId: obj.default_organization_id,
|
|
10
|
+
defaultProjectId: obj.default_project_id,
|
|
11
|
+
defaultRegion: obj.default_region,
|
|
12
|
+
defaultZone: obj.default_zone,
|
|
13
|
+
secretKey: obj.secret_key
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Loads profile from environment values.
|
|
17
|
+
*
|
|
18
|
+
* @returns The profile filled with values found in the environment
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
var loadProfileFromEnvironmentValues = () => ({
|
|
23
|
+
accessKey: env[EnvironmentKey.ScwAccessKey],
|
|
24
|
+
apiURL: env[EnvironmentKey.ScwAPIURL],
|
|
25
|
+
defaultOrganizationId: env[EnvironmentKey.ScwDefaultOrganizationId],
|
|
26
|
+
defaultProjectId: env[EnvironmentKey.ScwDefaultProjectId],
|
|
27
|
+
defaultRegion: env[EnvironmentKey.ScwDefaultRegion],
|
|
28
|
+
defaultZone: env[EnvironmentKey.ScwDefaultZone],
|
|
29
|
+
secretKey: env[EnvironmentKey.ScwSecretKey]
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Loads all the profiles from configuration file.
|
|
33
|
+
*
|
|
34
|
+
* @param params - The parameters to load the profile
|
|
35
|
+
* @returns The profiles filled with values found in the configuration profile
|
|
36
|
+
*
|
|
37
|
+
* @throws Error
|
|
38
|
+
* Thrown if the configuration file couldn't be found.
|
|
39
|
+
*
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
var loadAllProfilesFromConfigurationFile = (params) => {
|
|
43
|
+
const filePath = params?.filepath ?? resolveConfigurationFilePath();
|
|
44
|
+
if (typeof filePath !== "string" || filePath.length === 0) throw new Error("Could not find the path to the configuration file.");
|
|
45
|
+
const configs = loadConfigurationFromFile(filePath);
|
|
46
|
+
const result = {};
|
|
47
|
+
for (const pKey of Object.keys(configs)) result[pKey] = convertFileConfigToSDK(configs[pKey]);
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Loads profile from configuration file.
|
|
52
|
+
*
|
|
53
|
+
* @param params - The parameters to load the profile
|
|
54
|
+
* @returns The profile filled with values found in the configuration profile
|
|
55
|
+
*
|
|
56
|
+
* @throws Error
|
|
57
|
+
* Thrown if the configuration file couldn't be found,
|
|
58
|
+
* or if the specified profile can't be found.
|
|
59
|
+
*
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
var loadProfileFromConfigurationFile = (params) => {
|
|
63
|
+
const configs = loadAllProfilesFromConfigurationFile(params);
|
|
64
|
+
const profileName = params?.profileName ?? "default";
|
|
65
|
+
const profileMap = configs[profileName];
|
|
66
|
+
if (typeof profileMap !== "object") throw new Error(`Could not find the desired profile '${profileName}' in the configuration file.`);
|
|
67
|
+
return profileMap;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Loads all profiles from configuration file (asynchronous).
|
|
71
|
+
*
|
|
72
|
+
* Non-blocking alternative to {@link loadAllProfilesFromConfigurationFile}.
|
|
73
|
+
*
|
|
74
|
+
* @param params - The parameters to load the profile
|
|
75
|
+
* @returns The profiles filled with values found in the configuration profile
|
|
76
|
+
*
|
|
77
|
+
* @throws Error
|
|
78
|
+
* Thrown if the configuration file couldn't be found.
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
var loadAllProfilesFromConfigurationFileAsync = async (params) => {
|
|
83
|
+
const filePath = params?.filepath ?? resolveConfigurationFilePath();
|
|
84
|
+
if (typeof filePath !== "string" || filePath.length === 0) throw new Error("Could not find the path to the configuration file.");
|
|
85
|
+
const configs = await loadConfigurationFromFileAsync(filePath);
|
|
86
|
+
const result = {};
|
|
87
|
+
for (const pKey of Object.keys(configs)) result[pKey] = convertFileConfigToSDK(configs[pKey]);
|
|
88
|
+
return result;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Loads profile from configuration file (asynchronous).
|
|
92
|
+
*
|
|
93
|
+
* Non-blocking alternative to {@link loadProfileFromConfigurationFile}.
|
|
94
|
+
*
|
|
95
|
+
* @param params - The parameters to load the profile
|
|
96
|
+
* @returns The profile filled with values found in the configuration profile
|
|
97
|
+
*
|
|
98
|
+
* @throws Error
|
|
99
|
+
* Thrown if the configuration file couldn't be found,
|
|
100
|
+
* or if the specified profile can't be found.
|
|
101
|
+
*
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
var loadProfileFromConfigurationFileAsync = async (params) => {
|
|
105
|
+
const configs = await loadAllProfilesFromConfigurationFileAsync(params);
|
|
106
|
+
const profileName = params?.profileName ?? "default";
|
|
107
|
+
const profileMap = configs[profileName];
|
|
108
|
+
if (typeof profileMap !== "object") throw new Error(`Could not find the desired profile '${profileName}' in the configuration file.`);
|
|
109
|
+
return profileMap;
|
|
110
|
+
};
|
|
111
|
+
//#endregion
|
|
112
|
+
export { loadAllProfilesFromConfigurationFile, loadAllProfilesFromConfigurationFileAsync, loadProfileFromConfigurationFile, loadProfileFromConfigurationFileAsync, loadProfileFromEnvironmentValues };
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment Key.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum EnvironmentKey {
|
|
5
|
+
/** Path to the Scaleway configuration file */
|
|
6
|
+
ScwConfigPath = "SCW_CONFIG_PATH",
|
|
7
|
+
/** Scaleway access key */
|
|
8
|
+
ScwAccessKey = "SCW_ACCESS_KEY",
|
|
9
|
+
/**
|
|
10
|
+
* Scaleway secret key
|
|
11
|
+
* @remarks #nosec G101
|
|
12
|
+
*/
|
|
13
|
+
ScwSecretKey = "SCW_SECRET_KEY",
|
|
14
|
+
/** Scaleway API URL */
|
|
15
|
+
ScwAPIURL = "SCW_API_URL",
|
|
16
|
+
/** Scaleway default organization ID */
|
|
17
|
+
ScwDefaultOrganizationId = "SCW_DEFAULT_ORGANIZATION_ID",
|
|
18
|
+
/** Scaleway default project ID */
|
|
19
|
+
ScwDefaultProjectId = "SCW_DEFAULT_PROJECT_ID",
|
|
20
|
+
/** Scaleway default region */
|
|
21
|
+
ScwDefaultRegion = "SCW_DEFAULT_REGION",
|
|
22
|
+
/** Scaleway default zone */
|
|
23
|
+
ScwDefaultZone = "SCW_DEFAULT_ZONE"
|
|
24
|
+
}
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/env.ts
|
|
2
|
+
/**
|
|
3
|
+
* Environment Key.
|
|
4
|
+
*/
|
|
5
|
+
var EnvironmentKey = /* @__PURE__ */ function(EnvironmentKey) {
|
|
6
|
+
/** Path to the Scaleway configuration file */
|
|
7
|
+
EnvironmentKey["ScwConfigPath"] = "SCW_CONFIG_PATH";
|
|
8
|
+
/** Scaleway access key */
|
|
9
|
+
EnvironmentKey["ScwAccessKey"] = "SCW_ACCESS_KEY";
|
|
10
|
+
/**
|
|
11
|
+
* Scaleway secret key
|
|
12
|
+
* @remarks #nosec G101
|
|
13
|
+
*/
|
|
14
|
+
EnvironmentKey["ScwSecretKey"] = "SCW_SECRET_KEY";
|
|
15
|
+
/** Scaleway API URL */
|
|
16
|
+
EnvironmentKey["ScwAPIURL"] = "SCW_API_URL";
|
|
17
|
+
/** Scaleway default organization ID */
|
|
18
|
+
EnvironmentKey["ScwDefaultOrganizationId"] = "SCW_DEFAULT_ORGANIZATION_ID";
|
|
19
|
+
/** Scaleway default project ID */
|
|
20
|
+
EnvironmentKey["ScwDefaultProjectId"] = "SCW_DEFAULT_PROJECT_ID";
|
|
21
|
+
/** Scaleway default region */
|
|
22
|
+
EnvironmentKey["ScwDefaultRegion"] = "SCW_DEFAULT_REGION";
|
|
23
|
+
/** Scaleway default zone */
|
|
24
|
+
EnvironmentKey["ScwDefaultZone"] = "SCW_DEFAULT_ZONE";
|
|
25
|
+
return EnvironmentKey;
|
|
26
|
+
}({});
|
|
27
|
+
//#endregion
|
|
28
|
+
export { EnvironmentKey };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { loadAllProfilesFromConfigurationFile, loadAllProfilesFromConfigurationFileAsync, loadProfileFromConfigurationFile, loadProfileFromConfigurationFileAsync, loadProfileFromEnvironmentValues, } from './config-loader.js';
|
|
2
|
+
export type { AllProfilesFromFileParams, Profile, ProfileFromFileParams } from './types.js';
|
|
3
|
+
export { hasSecureFilePermissions } from './yml-loader.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { hasSecureFilePermissions } from "./yml-loader.js";
|
|
2
|
+
import { loadAllProfilesFromConfigurationFile, loadAllProfilesFromConfigurationFileAsync, loadProfileFromConfigurationFile, loadProfileFromConfigurationFileAsync, loadProfileFromEnvironmentValues } from "./config-loader.js";
|
|
3
|
+
export { hasSecureFilePermissions, loadAllProfilesFromConfigurationFile, loadAllProfilesFromConfigurationFileAsync, loadProfileFromConfigurationFile, loadProfileFromConfigurationFileAsync, loadProfileFromEnvironmentValues };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the Scaleway directory.
|
|
3
|
+
*
|
|
4
|
+
* @returns The path to the Scaleway diretory
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare const getScwConfigurationDirectory: () => string;
|
|
9
|
+
/**
|
|
10
|
+
* Gets the configuration file path.
|
|
11
|
+
*
|
|
12
|
+
* @returns The path to the configuration file
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare const resolveConfigurationFilePath: () => string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { EnvironmentKey } from "./env.js";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { env } from "node:process";
|
|
5
|
+
//#region src/path-resolver.ts
|
|
6
|
+
/**
|
|
7
|
+
* Gets the Scaleway directory.
|
|
8
|
+
*
|
|
9
|
+
* @returns The path to the Scaleway diretory
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
var getScwConfigurationDirectory = () => {
|
|
14
|
+
const xdgConfigPath = env.XDG_CONFIG_HOME;
|
|
15
|
+
if (typeof xdgConfigPath === "string" && xdgConfigPath.length > 0) return path.join(xdgConfigPath, "scw");
|
|
16
|
+
return path.join(homedir(), ".config", "scw");
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Gets the configuration file path.
|
|
20
|
+
*
|
|
21
|
+
* @returns The path to the configuration file
|
|
22
|
+
*
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
var resolveConfigurationFilePath = () => {
|
|
26
|
+
const envFilePath = env[EnvironmentKey.ScwConfigPath];
|
|
27
|
+
if (typeof envFilePath === "string" && envFilePath.length > 0) return envFilePath;
|
|
28
|
+
return path.join(getScwConfigurationDirectory(), "config.yaml");
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { getScwConfigurationDirectory, resolveConfigurationFilePath };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile contains information to help instanciating the Scaleway client.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type Profile = {
|
|
7
|
+
/**
|
|
8
|
+
* You need an access key and a secret key to connect to Scaleway API.
|
|
9
|
+
* Generate your token at the following address: {@link https://console.scaleway.com/project/credentials}
|
|
10
|
+
*/
|
|
11
|
+
accessKey?: string;
|
|
12
|
+
/**
|
|
13
|
+
* APIURL overrides the API URL of the Scaleway API to the given URL.
|
|
14
|
+
* Change that if you want to direct requests to a different endpoint.
|
|
15
|
+
*
|
|
16
|
+
* @defaultValue `https://api.scaleway.com`
|
|
17
|
+
*/
|
|
18
|
+
apiURL?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Your organization ID is the identifier of your account inside Scaleway infrastructure.
|
|
21
|
+
*/
|
|
22
|
+
defaultOrganizationId?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Your project ID is the identifier of the project your resources are attached to.
|
|
25
|
+
*/
|
|
26
|
+
defaultProjectId?: string;
|
|
27
|
+
/**
|
|
28
|
+
* A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
|
|
29
|
+
* It can contain multiple availability zones.
|
|
30
|
+
*
|
|
31
|
+
* Examples: fr-par, nl-ams.
|
|
32
|
+
*/
|
|
33
|
+
defaultRegion?: string;
|
|
34
|
+
/**
|
|
35
|
+
* A region can be split into many availability zones (AZ).
|
|
36
|
+
* Latency between multiple AZ of the same region are low as they have a common network layer.
|
|
37
|
+
*
|
|
38
|
+
* Examples: fr-par-1, nl-ams-1
|
|
39
|
+
*/
|
|
40
|
+
defaultZone?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
|
|
43
|
+
* The secret key MUST remain secret and not given to anyone or published online.
|
|
44
|
+
*/
|
|
45
|
+
secretKey?: string;
|
|
46
|
+
};
|
|
47
|
+
/** Configuration type. */
|
|
48
|
+
export type ConfigurationType = Record<string, Record<string, string>>;
|
|
49
|
+
/** Parameters to load the all the profiles from the configuration file */
|
|
50
|
+
export type AllProfilesFromFileParams = {
|
|
51
|
+
/**
|
|
52
|
+
* The path at which to locate the configuration file.
|
|
53
|
+
*
|
|
54
|
+
* Defaults to the value of the `SCW_CONFIG_PATH` environment variable
|
|
55
|
+
* or `~/.scw/config` otherwise.
|
|
56
|
+
*/
|
|
57
|
+
filepath?: string;
|
|
58
|
+
};
|
|
59
|
+
/** Parameters to load the profile from the configuration file */
|
|
60
|
+
export type ProfileFromFileParams = AllProfilesFromFileParams & {
|
|
61
|
+
/**
|
|
62
|
+
* Name of the profile to load.
|
|
63
|
+
*
|
|
64
|
+
* @defaultValue `default`
|
|
65
|
+
* */
|
|
66
|
+
profileName?: string;
|
|
67
|
+
};
|
package/dist/types.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ConfigurationType } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Converts YAML to configuration map.
|
|
4
|
+
*
|
|
5
|
+
* @param input - YAML string
|
|
6
|
+
* @returns The configuration map
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare const convertYamlToConfiguration: (input: string | null) => ConfigurationType;
|
|
11
|
+
/**
|
|
12
|
+
* Loads configuration from a file (synchronous).
|
|
13
|
+
*
|
|
14
|
+
* @param filePath - Path to the configuration file
|
|
15
|
+
* @returns The configuration
|
|
16
|
+
*
|
|
17
|
+
* @throws Error
|
|
18
|
+
* Thrown if the file doesn't exist.
|
|
19
|
+
*
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
export declare const loadConfigurationFromFile: (filePath: string) => ConfigurationType;
|
|
23
|
+
/**
|
|
24
|
+
* Loads configuration from a file (asynchronous).
|
|
25
|
+
*
|
|
26
|
+
* Non-blocking alternative to {@link loadConfigurationFromFile} that avoids
|
|
27
|
+
* stalling the event loop on slow filesystems or large config files.
|
|
28
|
+
*
|
|
29
|
+
* @param filePath - Path to the configuration file
|
|
30
|
+
* @returns The configuration
|
|
31
|
+
*
|
|
32
|
+
* @throws Error
|
|
33
|
+
* Thrown if the file doesn't exist.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export declare const loadConfigurationFromFileAsync: (filePath: string) => Promise<ConfigurationType>;
|
|
38
|
+
/**
|
|
39
|
+
* Checks whether a configuration file has secure permissions.
|
|
40
|
+
*
|
|
41
|
+
* The Scaleway config file contains secret keys that grant full API access.
|
|
42
|
+
* On POSIX systems this function verifies the file is not readable by group
|
|
43
|
+
* or others (mode 0o600 or stricter). Returns `true` on Windows where POSIX
|
|
44
|
+
* permission bits are not meaningful.
|
|
45
|
+
*
|
|
46
|
+
* @param filePath - Path to the configuration file
|
|
47
|
+
* @returns `true` if the file has secure permissions (or on Windows)
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export declare const hasSecureFilePermissions: (filePath: string) => Promise<boolean>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { readFile, readFileSync, stat } from "node:fs";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
//#region src/yml-loader.ts
|
|
4
|
+
var readFileAsync = promisify(readFile);
|
|
5
|
+
var statAsync = promisify(stat);
|
|
6
|
+
var STRIP_COMMENT_REGEX = /(^|\s)[;#]/;
|
|
7
|
+
var DETECT_SECTION_REGEX = /^\s*([\s\S]+?):\s*$/;
|
|
8
|
+
var DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
|
|
9
|
+
/**
|
|
10
|
+
* Converts YAML to configuration map.
|
|
11
|
+
*
|
|
12
|
+
* @param input - YAML string
|
|
13
|
+
* @returns The configuration map
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
var convertYamlToConfiguration = (input) => {
|
|
18
|
+
let foundProfilesKey = false;
|
|
19
|
+
let currentSection = "default";
|
|
20
|
+
const map = {};
|
|
21
|
+
if (typeof input !== "string") return map;
|
|
22
|
+
input.split(/\r?\n/).forEach((rawLine) => {
|
|
23
|
+
const line = rawLine.split(STRIP_COMMENT_REGEX)[0];
|
|
24
|
+
const newSection = DETECT_SECTION_REGEX.exec(line);
|
|
25
|
+
if (newSection) {
|
|
26
|
+
currentSection = void 0;
|
|
27
|
+
if (newSection[1] === "profiles") foundProfilesKey = true;
|
|
28
|
+
else if (foundProfilesKey) [, currentSection] = newSection;
|
|
29
|
+
} else if (currentSection) {
|
|
30
|
+
const item = DETECT_ITEM_REGEX.exec(line);
|
|
31
|
+
if (item) {
|
|
32
|
+
if (typeof map[currentSection] !== "object") map[currentSection] = {};
|
|
33
|
+
[, , map[currentSection][item[1]]] = item;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return map;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Loads configuration from a file (synchronous).
|
|
41
|
+
*
|
|
42
|
+
* @param filePath - Path to the configuration file
|
|
43
|
+
* @returns The configuration
|
|
44
|
+
*
|
|
45
|
+
* @throws Error
|
|
46
|
+
* Thrown if the file doesn't exist.
|
|
47
|
+
*
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
var loadConfigurationFromFile = (filePath) => {
|
|
51
|
+
return convertYamlToConfiguration(readFileSync(filePath, "utf-8"));
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Loads configuration from a file (asynchronous).
|
|
55
|
+
*
|
|
56
|
+
* Non-blocking alternative to {@link loadConfigurationFromFile} that avoids
|
|
57
|
+
* stalling the event loop on slow filesystems or large config files.
|
|
58
|
+
*
|
|
59
|
+
* @param filePath - Path to the configuration file
|
|
60
|
+
* @returns The configuration
|
|
61
|
+
*
|
|
62
|
+
* @throws Error
|
|
63
|
+
* Thrown if the file doesn't exist.
|
|
64
|
+
*
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
var loadConfigurationFromFileAsync = async (filePath) => {
|
|
68
|
+
return convertYamlToConfiguration(await readFileAsync(filePath, "utf-8"));
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Checks whether a configuration file has secure permissions.
|
|
72
|
+
*
|
|
73
|
+
* The Scaleway config file contains secret keys that grant full API access.
|
|
74
|
+
* On POSIX systems this function verifies the file is not readable by group
|
|
75
|
+
* or others (mode 0o600 or stricter). Returns `true` on Windows where POSIX
|
|
76
|
+
* permission bits are not meaningful.
|
|
77
|
+
*
|
|
78
|
+
* @param filePath - Path to the configuration file
|
|
79
|
+
* @returns `true` if the file has secure permissions (or on Windows)
|
|
80
|
+
*
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
var hasSecureFilePermissions = async (filePath) => {
|
|
84
|
+
if (process.platform === "win32") return true;
|
|
85
|
+
return ((await statAsync(filePath)).mode & 63) === 0;
|
|
86
|
+
};
|
|
87
|
+
//#endregion
|
|
88
|
+
export { convertYamlToConfiguration, hasSecureFilePermissions, loadConfigurationFromFile, loadConfigurationFromFileAsync };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scaleway/configuration-loader",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "Load configuration via file or environment for NodeJS.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -19,11 +19,8 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@
|
|
23
|
-
"@
|
|
24
|
-
},
|
|
25
|
-
"engines": {
|
|
26
|
-
"node": ">=20.20.2"
|
|
22
|
+
"@repo/configs": "^0.1.1",
|
|
23
|
+
"@types/node": "26.5.1"
|
|
27
24
|
},
|
|
28
25
|
"scripts": {
|
|
29
26
|
"typecheck": "tsc --noEmit",
|