@backstage/config-loader 0.6.7 → 0.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 +38 -0
- package/dist/index.cjs.js +96 -35
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +37 -13
- package/dist/index.esm.js +95 -35
- package/dist/index.esm.js.map +1 -1
- package/package.json +9 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# @backstage/config-loader
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 7e97d0b8c1: Removed the `EnvFunc` public export. Its only usage was to be passed in to `LoadConfigOptions.experimentalEnvFunc`. If you were using this type, add a definition in your own project instead with the signature `(name: string) => Promise<string | undefined>`.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 223e8de6b4: Configuration schema errors are now filtered using the provided visibility option. This means that schema errors due to missing backend configuration will no longer break frontend builds.
|
|
12
|
+
- 7e97d0b8c1: Add public tags and documentation
|
|
13
|
+
- 36e67d2f24: Internal updates to apply more strict checks to throw errors.
|
|
14
|
+
- Updated dependencies
|
|
15
|
+
- @backstage/errors@0.1.3
|
|
16
|
+
|
|
17
|
+
## 0.6.10
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 957e4b3351: Updated dependencies
|
|
22
|
+
- Updated dependencies
|
|
23
|
+
- @backstage/cli-common@0.1.4
|
|
24
|
+
|
|
25
|
+
## 0.6.9
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- ee7a1a4b64: Add option to collect configuration schemas from explicit package paths in addition to by package name.
|
|
30
|
+
- e68bd978e2: Allow collection of configuration schemas from multiple versions of the same package.
|
|
31
|
+
|
|
32
|
+
## 0.6.8
|
|
33
|
+
|
|
34
|
+
### Patch Changes
|
|
35
|
+
|
|
36
|
+
- d1da88a19: Properly export all used types.
|
|
37
|
+
- Updated dependencies
|
|
38
|
+
- @backstage/cli-common@0.1.3
|
|
39
|
+
- @backstage/config@0.1.9
|
|
40
|
+
|
|
3
41
|
## 0.6.7
|
|
4
42
|
|
|
5
43
|
### Patch Changes
|
package/dist/index.cjs.js
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var errors = require('@backstage/errors');
|
|
5
6
|
var yaml = require('yaml');
|
|
6
7
|
var path = require('path');
|
|
7
8
|
var Ajv = require('ajv');
|
|
8
9
|
var mergeAllOf = require('json-schema-merge-allof');
|
|
10
|
+
var traverse = require('json-schema-traverse');
|
|
9
11
|
var config = require('@backstage/config');
|
|
10
12
|
var fs = require('fs-extra');
|
|
11
13
|
var typescriptJsonSchema = require('typescript-json-schema');
|
|
@@ -16,6 +18,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
16
18
|
var yaml__default = /*#__PURE__*/_interopDefaultLegacy(yaml);
|
|
17
19
|
var Ajv__default = /*#__PURE__*/_interopDefaultLegacy(Ajv);
|
|
18
20
|
var mergeAllOf__default = /*#__PURE__*/_interopDefaultLegacy(mergeAllOf);
|
|
21
|
+
var traverse__default = /*#__PURE__*/_interopDefaultLegacy(traverse);
|
|
19
22
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
20
23
|
var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
|
|
21
24
|
|
|
@@ -65,6 +68,7 @@ function safeJsonParse(str) {
|
|
|
65
68
|
try {
|
|
66
69
|
return [null, JSON.parse(str)];
|
|
67
70
|
} catch (err) {
|
|
71
|
+
errors.assertError(err);
|
|
68
72
|
return [err, str];
|
|
69
73
|
}
|
|
70
74
|
}
|
|
@@ -95,6 +99,7 @@ async function applyConfigTransforms(initialDir, input, transforms) {
|
|
|
95
99
|
break;
|
|
96
100
|
}
|
|
97
101
|
} catch (error) {
|
|
102
|
+
errors.assertError(error);
|
|
98
103
|
throw new Error(`error at ${path}, ${error.message}`);
|
|
99
104
|
}
|
|
100
105
|
}
|
|
@@ -232,7 +237,7 @@ const CONFIG_VISIBILITIES = ["frontend", "backend", "secret"];
|
|
|
232
237
|
const DEFAULT_CONFIG_VISIBILITY = "backend";
|
|
233
238
|
|
|
234
239
|
function compileConfigSchemas(schemas) {
|
|
235
|
-
const
|
|
240
|
+
const visibilityByDataPath = new Map();
|
|
236
241
|
const ajv = new Ajv__default['default']({
|
|
237
242
|
allErrors: true,
|
|
238
243
|
allowUnionTypes: true,
|
|
@@ -252,7 +257,7 @@ function compileConfigSchemas(schemas) {
|
|
|
252
257
|
}
|
|
253
258
|
if (visibility && visibility !== "backend") {
|
|
254
259
|
const normalizedPath = context.dataPath.replace(/\['?(.*?)'?\]/g, (_, segment) => `/${segment}`);
|
|
255
|
-
|
|
260
|
+
visibilityByDataPath.set(normalizedPath, visibility);
|
|
256
261
|
}
|
|
257
262
|
return true;
|
|
258
263
|
};
|
|
@@ -267,23 +272,27 @@ function compileConfigSchemas(schemas) {
|
|
|
267
272
|
}
|
|
268
273
|
const merged = mergeConfigSchemas(schemas.map((_) => _.value));
|
|
269
274
|
const validate = ajv.compile(merged);
|
|
275
|
+
const visibilityBySchemaPath = new Map();
|
|
276
|
+
traverse__default['default'](merged, (schema, path) => {
|
|
277
|
+
if (schema.visibility && schema.visibility !== "backend") {
|
|
278
|
+
visibilityBySchemaPath.set(path, schema.visibility);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
270
281
|
return (configs) => {
|
|
271
282
|
var _a;
|
|
272
283
|
const config$1 = config.ConfigReader.fromConfigs(configs).get();
|
|
273
|
-
|
|
284
|
+
visibilityByDataPath.clear();
|
|
274
285
|
const valid = validate(config$1);
|
|
275
286
|
if (!valid) {
|
|
276
|
-
const errors = (_a = validate.errors) != null ? _a : [];
|
|
277
287
|
return {
|
|
278
|
-
errors:
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}),
|
|
282
|
-
visibilityByPath: new Map()
|
|
288
|
+
errors: (_a = validate.errors) != null ? _a : [],
|
|
289
|
+
visibilityByDataPath: new Map(visibilityByDataPath),
|
|
290
|
+
visibilityBySchemaPath
|
|
283
291
|
};
|
|
284
292
|
}
|
|
285
293
|
return {
|
|
286
|
-
|
|
294
|
+
visibilityByDataPath: new Map(visibilityByDataPath),
|
|
295
|
+
visibilityBySchemaPath
|
|
287
296
|
};
|
|
288
297
|
};
|
|
289
298
|
}
|
|
@@ -309,26 +318,41 @@ function mergeConfigSchemas(schemas) {
|
|
|
309
318
|
}
|
|
310
319
|
|
|
311
320
|
const req = typeof __non_webpack_require__ === "undefined" ? require : __non_webpack_require__;
|
|
312
|
-
async function collectConfigSchemas(packageNames) {
|
|
313
|
-
const
|
|
314
|
-
const
|
|
315
|
-
const
|
|
321
|
+
async function collectConfigSchemas(packageNames, packagePaths) {
|
|
322
|
+
const schemas = new Array();
|
|
323
|
+
const tsSchemaPaths = new Array();
|
|
324
|
+
const visitedPackageVersions = new Map();
|
|
316
325
|
const currentDir = await fs__default['default'].realpath(process.cwd());
|
|
317
|
-
async function processItem(
|
|
326
|
+
async function processItem(item) {
|
|
318
327
|
var _a, _b, _c, _d;
|
|
319
|
-
|
|
320
|
-
|
|
328
|
+
let pkgPath = item.packagePath;
|
|
329
|
+
if (pkgPath) {
|
|
330
|
+
const pkgExists = await fs__default['default'].pathExists(pkgPath);
|
|
331
|
+
if (!pkgExists) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
} else if (item.name) {
|
|
335
|
+
const {name, parentPath} = item;
|
|
336
|
+
try {
|
|
337
|
+
pkgPath = req.resolve(`${name}/package.json`, parentPath && {
|
|
338
|
+
paths: [parentPath]
|
|
339
|
+
});
|
|
340
|
+
} catch {
|
|
341
|
+
}
|
|
321
342
|
}
|
|
322
|
-
|
|
323
|
-
let pkgPath;
|
|
324
|
-
try {
|
|
325
|
-
pkgPath = req.resolve(`${name}/package.json`, parentPath && {
|
|
326
|
-
paths: [parentPath]
|
|
327
|
-
});
|
|
328
|
-
} catch {
|
|
343
|
+
if (!pkgPath) {
|
|
329
344
|
return;
|
|
330
345
|
}
|
|
331
346
|
const pkg = await fs__default['default'].readJson(pkgPath);
|
|
347
|
+
let versions = visitedPackageVersions.get(pkg.name);
|
|
348
|
+
if (versions == null ? void 0 : versions.has(pkg.version)) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (!versions) {
|
|
352
|
+
versions = new Set();
|
|
353
|
+
visitedPackageVersions.set(pkg.name, versions);
|
|
354
|
+
}
|
|
355
|
+
versions.add(pkg.version);
|
|
332
356
|
const depNames = [
|
|
333
357
|
...Object.keys((_a = pkg.dependencies) != null ? _a : {}),
|
|
334
358
|
...Object.keys((_b = pkg.devDependencies) != null ? _b : {}),
|
|
@@ -366,7 +390,10 @@ async function collectConfigSchemas(packageNames) {
|
|
|
366
390
|
}
|
|
367
391
|
await Promise.all(depNames.map((depName) => processItem({name: depName, parentPath: pkgPath})));
|
|
368
392
|
}
|
|
369
|
-
await Promise.all(
|
|
393
|
+
await Promise.all([
|
|
394
|
+
...packageNames.map((name) => processItem({name, parentPath: currentDir})),
|
|
395
|
+
...packagePaths.map((path) => processItem({name: path, packagePath: path}))
|
|
396
|
+
]);
|
|
370
397
|
const tsSchemas = compileTsSchemas(tsSchemaPaths);
|
|
371
398
|
return schemas.concat(tsSchemas);
|
|
372
399
|
}
|
|
@@ -394,6 +421,7 @@ function compileTsSchemas(paths) {
|
|
|
394
421
|
validationKeywords: ["visibility"]
|
|
395
422
|
}, [path$1.split(path.sep).join("/")]);
|
|
396
423
|
} catch (error) {
|
|
424
|
+
errors.assertError(error);
|
|
397
425
|
if (error.message !== "type Config not found") {
|
|
398
426
|
throw error;
|
|
399
427
|
}
|
|
@@ -406,12 +434,12 @@ function compileTsSchemas(paths) {
|
|
|
406
434
|
return tsSchemas;
|
|
407
435
|
}
|
|
408
436
|
|
|
409
|
-
function filterByVisibility(data, includeVisibilities,
|
|
437
|
+
function filterByVisibility(data, includeVisibilities, visibilityByDataPath, transformFunc, withFilteredKeys) {
|
|
410
438
|
var _a;
|
|
411
439
|
const filteredKeys = new Array();
|
|
412
440
|
function transform(jsonVal, visibilityPath, filterPath) {
|
|
413
441
|
var _a2;
|
|
414
|
-
const visibility = (_a2 =
|
|
442
|
+
const visibility = (_a2 = visibilityByDataPath.get(visibilityPath)) != null ? _a2 : DEFAULT_CONFIG_VISIBILITY;
|
|
415
443
|
const isVisible = includeVisibilities.includes(visibility);
|
|
416
444
|
if (typeof jsonVal !== "object") {
|
|
417
445
|
if (isVisible) {
|
|
@@ -461,11 +489,45 @@ function filterByVisibility(data, includeVisibilities, visibilityByPath, transfo
|
|
|
461
489
|
data: (_a = transform(data, "", "")) != null ? _a : {}
|
|
462
490
|
};
|
|
463
491
|
}
|
|
492
|
+
function filterErrorsByVisibility(errors, includeVisibilities, visibilityByDataPath, visibilityBySchemaPath) {
|
|
493
|
+
if (!errors) {
|
|
494
|
+
return [];
|
|
495
|
+
}
|
|
496
|
+
if (!includeVisibilities) {
|
|
497
|
+
return errors;
|
|
498
|
+
}
|
|
499
|
+
const visibleSchemaPaths = Array.from(visibilityBySchemaPath).filter(([, v]) => includeVisibilities.includes(v)).map(([k]) => k);
|
|
500
|
+
return errors.filter((error) => {
|
|
501
|
+
var _a;
|
|
502
|
+
if (error.keyword === "type" && ["object", "array"].includes(error.params.type)) {
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
if (error.keyword === "required") {
|
|
506
|
+
const trimmedPath = error.schemaPath.slice(1, -"/required".length);
|
|
507
|
+
const fullPath = `${trimmedPath}/properties/${error.params.missingProperty}`;
|
|
508
|
+
if (visibleSchemaPaths.some((visiblePath) => visiblePath.startsWith(fullPath))) {
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
const vis = (_a = visibilityByDataPath.get(error.dataPath)) != null ? _a : DEFAULT_CONFIG_VISIBILITY;
|
|
513
|
+
return vis && includeVisibilities.includes(vis);
|
|
514
|
+
});
|
|
515
|
+
}
|
|
464
516
|
|
|
517
|
+
function errorsToError(errors) {
|
|
518
|
+
const messages = errors.map(({dataPath, message, params}) => {
|
|
519
|
+
const paramStr = Object.entries(params).map(([name, value]) => `${name}=${value}`).join(" ");
|
|
520
|
+
return `Config ${message || ""} { ${paramStr} } at ${dataPath}`;
|
|
521
|
+
});
|
|
522
|
+
const error = new Error(`Config validation failed, ${messages.join("; ")}`);
|
|
523
|
+
error.messages = messages;
|
|
524
|
+
return error;
|
|
525
|
+
}
|
|
465
526
|
async function loadConfigSchema(options) {
|
|
527
|
+
var _a;
|
|
466
528
|
let schemas;
|
|
467
529
|
if ("dependencies" in options) {
|
|
468
|
-
schemas = await collectConfigSchemas(options.dependencies);
|
|
530
|
+
schemas = await collectConfigSchemas(options.dependencies, (_a = options.packagePaths) != null ? _a : []);
|
|
469
531
|
} else {
|
|
470
532
|
const {serialized} = options;
|
|
471
533
|
if ((serialized == null ? void 0 : serialized.backstageConfigSchemaVersion) !== 1) {
|
|
@@ -477,21 +539,20 @@ async function loadConfigSchema(options) {
|
|
|
477
539
|
return {
|
|
478
540
|
process(configs, {visibility, valueTransform, withFilteredKeys} = {}) {
|
|
479
541
|
const result = validate(configs);
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
throw error;
|
|
542
|
+
const visibleErrors = filterErrorsByVisibility(result.errors, visibility, result.visibilityByDataPath, result.visibilityBySchemaPath);
|
|
543
|
+
if (visibleErrors.length > 0) {
|
|
544
|
+
throw errorsToError(visibleErrors);
|
|
484
545
|
}
|
|
485
546
|
let processedConfigs = configs;
|
|
486
547
|
if (visibility) {
|
|
487
548
|
processedConfigs = processedConfigs.map(({data, context}) => ({
|
|
488
549
|
context,
|
|
489
|
-
...filterByVisibility(data, visibility, result.
|
|
550
|
+
...filterByVisibility(data, visibility, result.visibilityByDataPath, valueTransform, withFilteredKeys)
|
|
490
551
|
}));
|
|
491
552
|
} else if (valueTransform) {
|
|
492
553
|
processedConfigs = processedConfigs.map(({data, context}) => ({
|
|
493
554
|
context,
|
|
494
|
-
...filterByVisibility(data, Array.from(CONFIG_VISIBILITIES), result.
|
|
555
|
+
...filterByVisibility(data, Array.from(CONFIG_VISIBILITIES), result.visibilityByDataPath, valueTransform, withFilteredKeys)
|
|
495
556
|
}));
|
|
496
557
|
}
|
|
497
558
|
return processedConfigs;
|
|
@@ -538,7 +599,7 @@ async function loadConfig(options) {
|
|
|
538
599
|
try {
|
|
539
600
|
fileConfigs = await loadConfigFiles();
|
|
540
601
|
} catch (error) {
|
|
541
|
-
throw new
|
|
602
|
+
throw new errors.ForwardedError("Failed to read static configuration file", error);
|
|
542
603
|
}
|
|
543
604
|
const envConfigs = await readEnvConfig(process.env);
|
|
544
605
|
if (watch) {
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/lib/env.ts","../src/lib/transform/utils.ts","../src/lib/transform/apply.ts","../src/lib/transform/include.ts","../src/lib/transform/substitution.ts","../src/lib/schema/types.ts","../src/lib/schema/compile.ts","../src/lib/schema/collect.ts","../src/lib/schema/filtering.ts","../src/lib/schema/load.ts","../src/loader.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\n\nconst ENV_PREFIX = 'APP_CONFIG_';\n\n// Update the same pattern in config package if this is changed\nconst CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i;\n\n/**\n * Read runtime configuration from the environment.\n *\n * Only environment variables prefixed with APP_CONFIG_ will be considered.\n *\n * For each variable, the prefix will be removed, and rest of the key will\n * be split by '_'. Each part will then be used as keys to build up a nested\n * config object structure. The treatment of the entire environment variable\n * is case-sensitive.\n *\n * The value of the variable should be JSON serialized, as it will be parsed\n * and the type will be kept intact. For example \"true\" and true are treated\n * differently, as well as \"42\" and 42.\n *\n * For example, to set the config app.title to \"My Title\", use the following:\n *\n * APP_CONFIG_app_title='\"My Title\"'\n */\nexport function readEnvConfig(env: {\n [name: string]: string | undefined;\n}): AppConfig[] {\n let data: JsonObject | undefined = undefined;\n\n for (const [name, value] of Object.entries(env)) {\n if (!value) {\n continue;\n }\n if (name.startsWith(ENV_PREFIX)) {\n const key = name.replace(ENV_PREFIX, '');\n const keyParts = key.split('_');\n\n let obj = (data = data ?? {});\n for (const [index, part] of keyParts.entries()) {\n if (!CONFIG_KEY_PART_PATTERN.test(part)) {\n throw new TypeError(`Invalid env config key '${key}'`);\n }\n if (index < keyParts.length - 1) {\n obj = (obj[part] = obj[part] ?? {}) as JsonObject;\n if (typeof obj !== 'object' || Array.isArray(obj)) {\n const subKey = keyParts.slice(0, index + 1).join('_');\n throw new TypeError(\n `Could not nest config for key '${key}' under existing value '${subKey}'`,\n );\n }\n } else {\n if (part in obj) {\n throw new TypeError(\n `Refusing to override existing config at key '${key}'`,\n );\n }\n try {\n const [, parsedValue] = safeJsonParse(value);\n if (parsedValue === null) {\n throw new Error('value may not be null');\n }\n obj[part] = parsedValue;\n } catch (error) {\n throw new TypeError(\n `Failed to parse JSON-serialized config value for key '${key}', ${error}`,\n );\n }\n }\n }\n }\n }\n\n return data ? [{ data, context: 'env' }] : [];\n}\n\nfunction safeJsonParse(str: string): [Error | null, any] {\n try {\n return [null, JSON.parse(str)];\n } catch (err) {\n return [err, str];\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue, JsonObject } from '@backstage/config';\n\nexport function isObject(obj: JsonValue | undefined): obj is JsonObject {\n if (typeof obj !== 'object') {\n return false;\n } else if (Array.isArray(obj)) {\n return false;\n }\n return obj !== null;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { TransformFunc } from './types';\nimport { isObject } from './utils';\n\n/**\n * Applies a set of transforms to raw configuration data.\n */\nexport async function applyConfigTransforms(\n initialDir: string,\n input: JsonValue,\n transforms: TransformFunc[],\n): Promise<JsonObject> {\n async function transform(\n inputObj: JsonValue,\n path: string,\n baseDir: string,\n ): Promise<JsonValue | undefined> {\n let obj = inputObj;\n let dir = baseDir;\n\n for (const tf of transforms) {\n try {\n const result = await tf(inputObj, baseDir);\n if (result.applied) {\n if (result.value === undefined) {\n return undefined;\n }\n obj = result.value;\n dir = result.newBaseDir ?? dir;\n break;\n }\n } catch (error) {\n throw new Error(`error at ${path}, ${error.message}`);\n }\n }\n\n if (typeof obj !== 'object') {\n return obj;\n } else if (obj === null) {\n return undefined;\n } else if (Array.isArray(obj)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of obj.entries()) {\n const out = await transform(value, `${path}[${index}]`, dir);\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n return arr;\n }\n\n const out: JsonObject = {};\n\n for (const [key, value] of Object.entries(obj)) {\n // undefined covers optional fields\n if (value !== undefined) {\n const result = await transform(value, `${path}.${key}`, dir);\n if (result !== undefined) {\n out[key] = result;\n }\n }\n }\n\n return out;\n }\n\n const finalData = await transform(input, '', initialDir);\n if (!isObject(finalData)) {\n throw new TypeError('expected object at config root');\n }\n return finalData;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport yaml from 'yaml';\nimport { extname, dirname, resolve as resolvePath } from 'path';\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { isObject } from './utils';\nimport { TransformFunc, EnvFunc, ReadFileFunc } from './types';\n\n// Parsers for each type of included file\nconst includeFileParser: {\n [ext in string]: (content: string) => Promise<JsonObject>;\n} = {\n '.json': async content => JSON.parse(content),\n '.yaml': async content => yaml.parse(content),\n '.yml': async content => yaml.parse(content),\n};\n\n/**\n * Transforms a include description into the actual included value.\n */\nexport function createIncludeTransform(\n env: EnvFunc,\n readFile: ReadFileFunc,\n substitute: TransformFunc,\n): TransformFunc {\n return async (input: JsonValue, baseDir: string) => {\n if (!isObject(input)) {\n return { applied: false };\n }\n // Check if there's any key that starts with a '$', in that case we treat\n // this entire object as an include description.\n const [includeKey] = Object.keys(input).filter(key => key.startsWith('$'));\n if (includeKey) {\n if (Object.keys(input).length !== 1) {\n throw new Error(\n `include key ${includeKey} should not have adjacent keys`,\n );\n }\n } else {\n return { applied: false };\n }\n\n const rawIncludedValue = input[includeKey];\n if (typeof rawIncludedValue !== 'string') {\n throw new Error(`${includeKey} include value is not a string`);\n }\n\n const substituteResults = await substitute(rawIncludedValue, baseDir);\n const includeValue = substituteResults.applied\n ? substituteResults.value\n : rawIncludedValue;\n\n // The second string check is needed for Typescript to know this is a string.\n if (includeValue === undefined || typeof includeValue !== 'string') {\n throw new Error(`${includeKey} substitution value was undefined`);\n }\n\n switch (includeKey) {\n case '$file':\n try {\n const value = await readFile(resolvePath(baseDir, includeValue));\n return { applied: true, value };\n } catch (error) {\n throw new Error(`failed to read file ${includeValue}, ${error}`);\n }\n case '$env':\n try {\n return { applied: true, value: await env(includeValue) };\n } catch (error) {\n throw new Error(`failed to read env ${includeValue}, ${error}`);\n }\n\n case '$include': {\n const [filePath, dataPath] = includeValue.split(/#(.*)/);\n\n const ext = extname(filePath);\n const parser = includeFileParser[ext];\n if (!parser) {\n throw new Error(\n `no configuration parser available for included file ${filePath}`,\n );\n }\n\n const path = resolvePath(baseDir, filePath);\n const content = await readFile(path);\n const newBaseDir = dirname(path);\n\n const parts = dataPath ? dataPath.split('.') : [];\n\n let value: JsonValue | undefined;\n try {\n value = await parser(content);\n } catch (error) {\n throw new Error(\n `failed to parse included file ${filePath}, ${error}`,\n );\n }\n\n // This bit handles selecting a subtree in the included file, if a path was provided after a #\n for (const [index, part] of parts.entries()) {\n if (!isObject(value)) {\n const errPath = parts.slice(0, index).join('.');\n throw new Error(\n `value at '${errPath}' in included file ${filePath} is not an object`,\n );\n }\n value = value[part];\n }\n\n return {\n applied: true,\n value,\n newBaseDir: newBaseDir !== baseDir ? newBaseDir : undefined,\n };\n }\n\n default:\n throw new Error(`unknown include ${includeKey}`);\n }\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue } from '@backstage/config';\nimport { TransformFunc, EnvFunc } from './types';\n\n/**\n * A environment variable substitution transform that transforms e.g. 'token ${MY_TOKEN}'\n * to 'token abc' if MY_TOKEN is 'abc'. If any of the substituted variables are undefined,\n * the entire expression ends up undefined.\n */\nexport function createSubstitutionTransform(env: EnvFunc): TransformFunc {\n return async (input: JsonValue) => {\n if (typeof input !== 'string') {\n return { applied: false };\n }\n\n const parts: (string | undefined)[] = input.split(/(\\$?\\$\\{[^{}]*\\})/);\n for (let i = 1; i < parts.length; i += 2) {\n const part = parts[i]!;\n if (part.startsWith('$$')) {\n parts[i] = part.slice(1);\n } else {\n parts[i] = await env(part.slice(2, -1).trim());\n }\n }\n\n if (parts.some(part => part === undefined)) {\n return { applied: true, value: undefined };\n }\n return { applied: true, value: parts.join('') };\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\n\n/**\n * An sub-set of configuration schema.\n */\nexport type ConfigSchemaPackageEntry = {\n /**\n * The configuration schema itself.\n */\n value: JsonObject;\n /**\n * The relative path that the configuration schema was discovered at.\n */\n path: string;\n};\n\n/**\n * A list of all possible configuration value visibilities.\n */\nexport const CONFIG_VISIBILITIES = ['frontend', 'backend', 'secret'] as const;\n\n/**\n * A type representing the possible configuration value visibilities\n */\nexport type ConfigVisibility = typeof CONFIG_VISIBILITIES[number];\n\n/**\n * The default configuration visibility if no other values is given.\n */\nexport const DEFAULT_CONFIG_VISIBILITY: ConfigVisibility = 'backend';\n\n/**\n * An explanation of a configuration validation error.\n */\ntype ValidationError = string;\n\n/**\n * The result of validating configuration data using a schema.\n */\ntype ValidationResult = {\n /**\n * Errors that where emitted during validation, if any.\n */\n errors?: ValidationError[];\n /**\n * The configuration visibilities that were discovered during validation.\n *\n * The path in the key uses the form `/<key>/<sub-key>/<array-index>/<leaf-key>`\n */\n visibilityByPath: Map<string, ConfigVisibility>;\n};\n\n/**\n * A function used validate configuration data.\n */\nexport type ValidationFunc = (configs: AppConfig[]) => ValidationResult;\n\n/**\n * A function used to transform primitive configuration values.\n */\nexport type TransformFunc<T extends number | string | boolean> = (\n value: T,\n context: { visibility: ConfigVisibility },\n) => T | undefined;\n\n/**\n * Options used to process configuration data with a schema.\n */\ntype ConfigProcessingOptions = {\n /**\n * The visibilities that should be included in the output data.\n * If omitted, the data will not be filtered by visibility.\n */\n visibility?: ConfigVisibility[];\n\n /**\n * A transform function that can be used to transform primitive configuration values\n * during validation. The value returned from the transform function will be used\n * instead of the original value. If the transform returns `undefined`, the value\n * will be omitted.\n */\n valueTransform?: TransformFunc<any>;\n\n /**\n * Whether or not to include the `filteredKeys` property in the output `AppConfig`s.\n *\n * Default: `false`.\n */\n withFilteredKeys?: boolean;\n};\n\n/**\n * A loaded configuration schema that is ready to process configuration data.\n */\nexport type ConfigSchema = {\n process(\n appConfigs: AppConfig[],\n options?: ConfigProcessingOptions,\n ): AppConfig[];\n\n serialize(): JsonObject;\n};\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport Ajv from 'ajv';\nimport { JSONSchema7 as JSONSchema } from 'json-schema';\nimport mergeAllOf, { Resolvers } from 'json-schema-merge-allof';\nimport { ConfigReader } from '@backstage/config';\nimport {\n ConfigSchemaPackageEntry,\n ValidationFunc,\n CONFIG_VISIBILITIES,\n ConfigVisibility,\n} from './types';\n\n/**\n * This takes a collection of Backstage configuration schemas from various\n * sources and compiles them down into a single schema validation function.\n *\n * It also handles the implementation of the custom \"visibility\" keyword used\n * to specify the scope of different config paths.\n */\nexport function compileConfigSchemas(\n schemas: ConfigSchemaPackageEntry[],\n): ValidationFunc {\n // The ajv instance below is stateful and doesn't really allow for additional\n // output during validation. We work around this by having this extra piece\n // of state that we reset before each validation.\n const visibilityByPath = new Map<string, ConfigVisibility>();\n\n const ajv = new Ajv({\n allErrors: true,\n allowUnionTypes: true,\n schemas: {\n 'https://backstage.io/schema/config-v1': true,\n },\n }).addKeyword({\n keyword: 'visibility',\n metaSchema: {\n type: 'string',\n enum: CONFIG_VISIBILITIES,\n },\n compile(visibility: ConfigVisibility) {\n return (_data, context) => {\n if (context?.dataPath === undefined) {\n return false;\n }\n if (visibility && visibility !== 'backend') {\n const normalizedPath = context.dataPath.replace(\n /\\['?(.*?)'?\\]/g,\n (_, segment) => `/${segment}`,\n );\n visibilityByPath.set(normalizedPath, visibility);\n }\n return true;\n };\n },\n });\n\n for (const schema of schemas) {\n try {\n ajv.compile(schema.value);\n } catch (error) {\n throw new Error(`Schema at ${schema.path} is invalid, ${error}`);\n }\n }\n\n const merged = mergeConfigSchemas(schemas.map(_ => _.value));\n const validate = ajv.compile(merged);\n\n return configs => {\n const config = ConfigReader.fromConfigs(configs).get();\n\n visibilityByPath.clear();\n\n const valid = validate(config);\n if (!valid) {\n const errors = validate.errors ?? [];\n return {\n errors: errors.map(({ dataPath, message, params }) => {\n const paramStr = Object.entries(params)\n .map(([name, value]) => `${name}=${value}`)\n .join(' ');\n return `Config ${message || ''} { ${paramStr} } at ${dataPath}`;\n }),\n visibilityByPath: new Map(),\n };\n }\n\n return {\n visibilityByPath: new Map(visibilityByPath),\n };\n };\n}\n\n/**\n * Given a list of configuration schemas from packages, merge them\n * into a single json schema.\n */\nexport function mergeConfigSchemas(schemas: JSONSchema[]): JSONSchema {\n const merged = mergeAllOf(\n { allOf: schemas },\n {\n // JSONSchema is typically subtractive, as in it always reduces the set of allowed\n // inputs through constraints. This changes the object property merging to be additive\n // rather than subtractive.\n ignoreAdditionalProperties: true,\n resolvers: {\n // This ensures that the visibilities across different schemas are sound, and\n // selects the most specific visibility for each path.\n visibility(values: string[], path: string[]) {\n const hasFrontend = values.some(_ => _ === 'frontend');\n const hasSecret = values.some(_ => _ === 'secret');\n if (hasFrontend && hasSecret) {\n throw new Error(\n `Config schema visibility is both 'frontend' and 'secret' for ${path.join(\n '/',\n )}`,\n );\n } else if (hasFrontend) {\n return 'frontend';\n } else if (hasSecret) {\n return 'secret';\n }\n\n return 'backend';\n },\n } as Partial<Resolvers<JSONSchema>>,\n },\n );\n return merged;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport {\n resolve as resolvePath,\n relative as relativePath,\n dirname,\n sep,\n} from 'path';\nimport { ConfigSchemaPackageEntry } from './types';\nimport { getProgramFromFiles, generateSchema } from 'typescript-json-schema';\nimport { JsonObject } from '@backstage/config';\n\ntype Item = {\n name: string;\n parentPath?: string;\n};\n\nconst req =\n typeof __non_webpack_require__ === 'undefined'\n ? require\n : __non_webpack_require__;\n\n/**\n * This collects all known config schemas across all dependencies of the app.\n */\nexport async function collectConfigSchemas(\n packageNames: string[],\n): Promise<ConfigSchemaPackageEntry[]> {\n const visitedPackages = new Set<string>();\n const schemas = Array<ConfigSchemaPackageEntry>();\n const tsSchemaPaths = Array<string>();\n const currentDir = await fs.realpath(process.cwd());\n\n async function processItem({ name, parentPath }: Item) {\n // Ensures that we only process each package once. We don't bother with\n // loading in schemas from duplicates of different versions, as that's not\n // supported by Backstage right now anyway. We may want to change that in\n // the future though, if it for example becomes possible to load in two\n // different versions of e.g. @backstage/core at once.\n if (visitedPackages.has(name)) {\n return;\n }\n visitedPackages.add(name);\n\n let pkgPath: string;\n try {\n pkgPath = req.resolve(\n `${name}/package.json`,\n parentPath && {\n paths: [parentPath],\n },\n );\n } catch {\n // We can somewhat safely ignore packages that don't export package.json,\n // as they are likely not part of the Backstage ecosystem anyway.\n return;\n }\n\n const pkg = await fs.readJson(pkgPath);\n const depNames = [\n ...Object.keys(pkg.dependencies ?? {}),\n ...Object.keys(pkg.devDependencies ?? {}),\n ...Object.keys(pkg.optionalDependencies ?? {}),\n ...Object.keys(pkg.peerDependencies ?? {}),\n ];\n\n // TODO(Rugvip): Trying this out to avoid having to traverse the full dependency graph,\n // since that's pretty slow. We probably need a better way to determine when\n // we've left the Backstage ecosystem, but this will do for now.\n const hasSchema = 'configSchema' in pkg;\n const hasBackstageDep = depNames.some(_ => _.startsWith('@backstage/'));\n if (!hasSchema && !hasBackstageDep) {\n return;\n }\n if (hasSchema) {\n if (typeof pkg.configSchema === 'string') {\n const isJson = pkg.configSchema.endsWith('.json');\n const isDts = pkg.configSchema.endsWith('.d.ts');\n if (!isJson && !isDts) {\n throw new Error(\n `Config schema files must be .json or .d.ts, got ${pkg.configSchema}`,\n );\n }\n if (isDts) {\n tsSchemaPaths.push(\n relativePath(\n currentDir,\n resolvePath(dirname(pkgPath), pkg.configSchema),\n ),\n );\n } else {\n const path = resolvePath(dirname(pkgPath), pkg.configSchema);\n const value = await fs.readJson(path);\n schemas.push({\n value,\n path: relativePath(currentDir, path),\n });\n }\n } else {\n schemas.push({\n value: pkg.configSchema,\n path: relativePath(currentDir, pkgPath),\n });\n }\n }\n\n await Promise.all(\n depNames.map(depName =>\n processItem({ name: depName, parentPath: pkgPath }),\n ),\n );\n }\n\n await Promise.all(\n packageNames.map(name => processItem({ name, parentPath: currentDir })),\n );\n\n const tsSchemas = compileTsSchemas(tsSchemaPaths);\n\n return schemas.concat(tsSchemas);\n}\n\n// This handles the support of TypeScript .d.ts config schema declarations.\n// We collect all typescript schema definition and compile them all in one go.\n// This is much faster than compiling them separately.\nfunction compileTsSchemas(paths: string[]) {\n if (paths.length === 0) {\n return [];\n }\n\n const program = getProgramFromFiles(paths, {\n incremental: false,\n isolatedModules: true,\n lib: ['ES5'], // Skipping most libs speeds processing up a lot, we just need the primitive types anyway\n noEmit: true,\n noResolve: true,\n skipLibCheck: true, // Skipping lib checks speeds things up\n skipDefaultLibCheck: true,\n strict: true,\n typeRoots: [], // Do not include any additional types\n types: [],\n });\n\n const tsSchemas = paths.map(path => {\n let value;\n try {\n value = generateSchema(\n program,\n // All schemas should export a `Config` symbol\n 'Config',\n // This enables usage of @visibility is doc comments\n {\n required: true,\n validationKeywords: ['visibility'],\n },\n [path.split(sep).join('/')], // Unix paths are expected for all OSes here\n ) as JsonObject | null;\n } catch (error) {\n if (error.message !== 'type Config not found') {\n throw error;\n }\n }\n\n if (!value) {\n throw new Error(`Invalid schema in ${path}, missing Config export`);\n }\n return { path, value };\n });\n\n return tsSchemas;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport {\n ConfigVisibility,\n DEFAULT_CONFIG_VISIBILITY,\n TransformFunc,\n} from './types';\n\n/**\n * This filters data by visibility by discovering the visibility of each\n * value, and then only keeping the ones that are specified in `includeVisibilities`.\n */\nexport function filterByVisibility(\n data: JsonObject,\n includeVisibilities: ConfigVisibility[],\n visibilityByPath: Map<string, ConfigVisibility>,\n transformFunc?: TransformFunc<number | string | boolean>,\n withFilteredKeys?: boolean,\n): { data: JsonObject; filteredKeys?: string[] } {\n const filteredKeys = new Array<string>();\n\n function transform(\n jsonVal: JsonValue,\n visibilityPath: string, // Matches the format we get from ajv\n filterPath: string, // Matches the format of the ConfigReader\n ): JsonValue | undefined {\n const visibility =\n visibilityByPath.get(visibilityPath) ?? DEFAULT_CONFIG_VISIBILITY;\n const isVisible = includeVisibilities.includes(visibility);\n\n if (typeof jsonVal !== 'object') {\n if (isVisible) {\n if (transformFunc) {\n return transformFunc(jsonVal, { visibility });\n }\n return jsonVal;\n }\n if (withFilteredKeys) {\n filteredKeys.push(filterPath);\n }\n return undefined;\n } else if (jsonVal === null) {\n return undefined;\n } else if (Array.isArray(jsonVal)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of jsonVal.entries()) {\n const out = transform(\n value,\n `${visibilityPath}/${index}`,\n `${filterPath}[${index}]`,\n );\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n if (arr.length > 0 || isVisible) {\n return arr;\n }\n return undefined;\n }\n\n const outObj: JsonObject = {};\n let hasOutput = false;\n\n for (const [key, value] of Object.entries(jsonVal)) {\n if (value === undefined) {\n continue;\n }\n const out = transform(\n value,\n `${visibilityPath}/${key}`,\n filterPath ? `${filterPath}.${key}` : key,\n );\n if (out !== undefined) {\n outObj[key] = out;\n hasOutput = true;\n }\n }\n\n if (hasOutput || isVisible) {\n return outObj;\n }\n return undefined;\n }\n\n return {\n filteredKeys: withFilteredKeys ? filteredKeys : undefined,\n data: (transform(data, '', '') as JsonObject) ?? {},\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\nimport { compileConfigSchemas } from './compile';\nimport { collectConfigSchemas } from './collect';\nimport { filterByVisibility } from './filtering';\nimport {\n ConfigSchema,\n ConfigSchemaPackageEntry,\n CONFIG_VISIBILITIES,\n} from './types';\n\ntype Options =\n | {\n dependencies: string[];\n }\n | {\n serialized: JsonObject;\n };\n\n/**\n * Loads config schema for a Backstage instance.\n */\nexport async function loadConfigSchema(\n options: Options,\n): Promise<ConfigSchema> {\n let schemas: ConfigSchemaPackageEntry[];\n\n if ('dependencies' in options) {\n schemas = await collectConfigSchemas(options.dependencies);\n } else {\n const { serialized } = options;\n if (serialized?.backstageConfigSchemaVersion !== 1) {\n throw new Error(\n 'Serialized configuration schema is invalid or has an invalid version number',\n );\n }\n schemas = serialized.schemas as ConfigSchemaPackageEntry[];\n }\n\n const validate = compileConfigSchemas(schemas);\n\n return {\n process(\n configs: AppConfig[],\n { visibility, valueTransform, withFilteredKeys } = {},\n ): AppConfig[] {\n const result = validate(configs);\n if (result.errors) {\n const error = new Error(\n `Config validation failed, ${result.errors.join('; ')}`,\n );\n (error as any).messages = result.errors;\n throw error;\n }\n\n let processedConfigs = configs;\n\n if (visibility) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n visibility,\n result.visibilityByPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n } else if (valueTransform) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n Array.from(CONFIG_VISIBILITIES),\n result.visibilityByPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n }\n\n return processedConfigs;\n },\n serialize(): JsonObject {\n return {\n schemas,\n backstageConfigSchemaVersion: 1,\n };\n },\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport yaml from 'yaml';\nimport chokidar from 'chokidar';\nimport { resolve as resolvePath, dirname, isAbsolute, basename } from 'path';\nimport { AppConfig } from '@backstage/config';\nimport {\n applyConfigTransforms,\n readEnvConfig,\n createIncludeTransform,\n createSubstitutionTransform,\n} from './lib';\nimport { EnvFunc } from './lib/transform/types';\n\nexport type LoadConfigOptions = {\n // The root directory of the config loading context. Used to find default configs.\n configRoot: string;\n\n // Absolute paths to load config files from. Configs from earlier paths have lower priority.\n configPaths: string[];\n\n /** @deprecated This option has been removed */\n env?: string;\n\n /**\n * Custom environment variable loading function\n *\n * @experimental This API is not stable and may change at any point\n */\n experimentalEnvFunc?: EnvFunc;\n\n /**\n * An optional configuration that enables watching of config files.\n */\n watch?: {\n /**\n * A listener that is called when a config file is changed.\n */\n onChange: (configs: AppConfig[]) => void;\n\n /**\n * An optional signal that stops the watcher once the promise resolves.\n */\n stopSignal?: Promise<void>;\n };\n};\n\nexport async function loadConfig(\n options: LoadConfigOptions,\n): Promise<AppConfig[]> {\n const { configRoot, experimentalEnvFunc: envFunc, watch } = options;\n const configPaths = options.configPaths.slice();\n\n // If no paths are provided, we default to reading\n // `app-config.yaml` and, if it exists, `app-config.local.yaml`\n if (configPaths.length === 0) {\n configPaths.push(resolvePath(configRoot, 'app-config.yaml'));\n\n const localConfig = resolvePath(configRoot, 'app-config.local.yaml');\n if (await fs.pathExists(localConfig)) {\n configPaths.push(localConfig);\n }\n }\n\n const env = envFunc ?? (async (name: string) => process.env[name]);\n\n const loadConfigFiles = async () => {\n const configs = [];\n\n for (const configPath of configPaths) {\n if (!isAbsolute(configPath)) {\n throw new Error(`Config load path is not absolute: '${configPath}'`);\n }\n\n const dir = dirname(configPath);\n const readFile = (path: string) =>\n fs.readFile(resolvePath(dir, path), 'utf8');\n\n const input = yaml.parse(await readFile(configPath));\n const substitutionTransform = createSubstitutionTransform(env);\n const data = await applyConfigTransforms(dir, input, [\n createIncludeTransform(env, readFile, substitutionTransform),\n substitutionTransform,\n ]);\n\n configs.push({ data, context: basename(configPath) });\n }\n\n return configs;\n };\n\n let fileConfigs;\n try {\n fileConfigs = await loadConfigFiles();\n } catch (error) {\n throw new Error(\n `Failed to read static configuration file, ${error.message}`,\n );\n }\n\n const envConfigs = await readEnvConfig(process.env);\n\n // Set up config file watching if requested by the caller\n if (watch) {\n let currentSerializedConfig = JSON.stringify(fileConfigs);\n\n const watcher = chokidar.watch(configPaths, {\n usePolling: process.env.NODE_ENV === 'test',\n });\n watcher.on('change', async () => {\n try {\n const newConfigs = await loadConfigFiles();\n const newSerializedConfig = JSON.stringify(newConfigs);\n\n if (currentSerializedConfig === newSerializedConfig) {\n return;\n }\n currentSerializedConfig = newSerializedConfig;\n\n watch.onChange([...newConfigs, ...envConfigs]);\n } catch (error) {\n console.error(`Failed to reload configuration files, ${error}`);\n }\n });\n\n if (watch.stopSignal) {\n watch.stopSignal.then(() => {\n watcher.close();\n });\n }\n }\n\n return [...fileConfigs, ...envConfigs];\n}\n"],"names":["yaml","resolvePath","extname","path","dirname","Ajv","config","ConfigReader","mergeAllOf","fs","relativePath","getProgramFromFiles","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,YAAM,MAAMV,aAAQ;AACpB,YAAM,WAAW,CAACD,WAChBM,uBAAG,SAASR,aAAY,KAAKE,SAAO;AAEtC,YAAM,QAAQH,yBAAK,MAAM,MAAM,SAAS;AACxC,YAAM,wBAAwB,4BAA4B;AAC1D,YAAM,OAAO,MAAM,sBAAsB,KAAK,OAAO;AAAA,QACnD,uBAAuB,KAAK,UAAU;AAAA,QACtC;AAAA;AAGF,cAAQ,KAAK,CAAE,MAAM,SAASe,cAAS;AAAA;AAGzC,WAAO;AAAA;AAGT,MAAI;AACJ,MAAI;AACF,kBAAc,MAAM;AAAA,WACb,OAAP;AACA,UAAM,IAAI,MACR,6CAA6C,MAAM;AAAA;AAIvD,QAAM,aAAa,MAAM,cAAc,QAAQ;AAG/C,MAAI,OAAO;AACT,QAAI,0BAA0B,KAAK,UAAU;AAE7C,UAAM,UAAUC,6BAAS,MAAM,aAAa;AAAA,MAC1C,YAAY,QAAQ,IAAI,aAAa;AAAA;AAEvC,YAAQ,GAAG,UAAU,YAAY;AAC/B,UAAI;AACF,cAAM,aAAa,MAAM;AACzB,cAAM,sBAAsB,KAAK,UAAU;AAE3C,YAAI,4BAA4B,qBAAqB;AACnD;AAAA;AAEF,kCAA0B;AAE1B,cAAM,SAAS,CAAC,GAAG,YAAY,GAAG;AAAA,eAC3B,OAAP;AACA,gBAAQ,MAAM,yCAAyC;AAAA;AAAA;AAI3D,QAAI,MAAM,YAAY;AACpB,YAAM,WAAW,KAAK,MAAM;AAC1B,gBAAQ;AAAA;AAAA;AAAA;AAKd,SAAO,CAAC,GAAG,aAAa,GAAG;AAAA;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/lib/env.ts","../src/lib/transform/utils.ts","../src/lib/transform/apply.ts","../src/lib/transform/include.ts","../src/lib/transform/substitution.ts","../src/lib/schema/types.ts","../src/lib/schema/compile.ts","../src/lib/schema/collect.ts","../src/lib/schema/filtering.ts","../src/lib/schema/load.ts","../src/loader.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\nimport { assertError } from '@backstage/errors';\n\nconst ENV_PREFIX = 'APP_CONFIG_';\n\n// Update the same pattern in config package if this is changed\nconst CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i;\n\n/**\n * Read runtime configuration from the environment.\n *\n * Only environment variables prefixed with APP_CONFIG_ will be considered.\n *\n * For each variable, the prefix will be removed, and rest of the key will\n * be split by '_'. Each part will then be used as keys to build up a nested\n * config object structure. The treatment of the entire environment variable\n * is case-sensitive.\n *\n * The value of the variable should be JSON serialized, as it will be parsed\n * and the type will be kept intact. For example \"true\" and true are treated\n * differently, as well as \"42\" and 42.\n *\n * For example, to set the config app.title to \"My Title\", use the following:\n *\n * APP_CONFIG_app_title='\"My Title\"'\n *\n * @public\n */\nexport function readEnvConfig(env: {\n [name: string]: string | undefined;\n}): AppConfig[] {\n let data: JsonObject | undefined = undefined;\n\n for (const [name, value] of Object.entries(env)) {\n if (!value) {\n continue;\n }\n if (name.startsWith(ENV_PREFIX)) {\n const key = name.replace(ENV_PREFIX, '');\n const keyParts = key.split('_');\n\n let obj = (data = data ?? {});\n for (const [index, part] of keyParts.entries()) {\n if (!CONFIG_KEY_PART_PATTERN.test(part)) {\n throw new TypeError(`Invalid env config key '${key}'`);\n }\n if (index < keyParts.length - 1) {\n obj = (obj[part] = obj[part] ?? {}) as JsonObject;\n if (typeof obj !== 'object' || Array.isArray(obj)) {\n const subKey = keyParts.slice(0, index + 1).join('_');\n throw new TypeError(\n `Could not nest config for key '${key}' under existing value '${subKey}'`,\n );\n }\n } else {\n if (part in obj) {\n throw new TypeError(\n `Refusing to override existing config at key '${key}'`,\n );\n }\n try {\n const [, parsedValue] = safeJsonParse(value);\n if (parsedValue === null) {\n throw new Error('value may not be null');\n }\n obj[part] = parsedValue;\n } catch (error) {\n throw new TypeError(\n `Failed to parse JSON-serialized config value for key '${key}', ${error}`,\n );\n }\n }\n }\n }\n }\n\n return data ? [{ data, context: 'env' }] : [];\n}\n\nfunction safeJsonParse(str: string): [Error | null, any] {\n try {\n return [null, JSON.parse(str)];\n } catch (err) {\n assertError(err);\n return [err, str];\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue, JsonObject } from '@backstage/config';\n\nexport function isObject(obj: JsonValue | undefined): obj is JsonObject {\n if (typeof obj !== 'object') {\n return false;\n } else if (Array.isArray(obj)) {\n return false;\n }\n return obj !== null;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { assertError } from '@backstage/errors';\nimport { TransformFunc } from './types';\nimport { isObject } from './utils';\n\n/**\n * Applies a set of transforms to raw configuration data.\n */\nexport async function applyConfigTransforms(\n initialDir: string,\n input: JsonValue,\n transforms: TransformFunc[],\n): Promise<JsonObject> {\n async function transform(\n inputObj: JsonValue,\n path: string,\n baseDir: string,\n ): Promise<JsonValue | undefined> {\n let obj = inputObj;\n let dir = baseDir;\n\n for (const tf of transforms) {\n try {\n const result = await tf(inputObj, baseDir);\n if (result.applied) {\n if (result.value === undefined) {\n return undefined;\n }\n obj = result.value;\n dir = result.newBaseDir ?? dir;\n break;\n }\n } catch (error) {\n assertError(error);\n throw new Error(`error at ${path}, ${error.message}`);\n }\n }\n\n if (typeof obj !== 'object') {\n return obj;\n } else if (obj === null) {\n return undefined;\n } else if (Array.isArray(obj)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of obj.entries()) {\n const out = await transform(value, `${path}[${index}]`, dir);\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n return arr;\n }\n\n const out: JsonObject = {};\n\n for (const [key, value] of Object.entries(obj)) {\n // undefined covers optional fields\n if (value !== undefined) {\n const result = await transform(value, `${path}.${key}`, dir);\n if (result !== undefined) {\n out[key] = result;\n }\n }\n }\n\n return out;\n }\n\n const finalData = await transform(input, '', initialDir);\n if (!isObject(finalData)) {\n throw new TypeError('expected object at config root');\n }\n return finalData;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport yaml from 'yaml';\nimport { extname, dirname, resolve as resolvePath } from 'path';\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { isObject } from './utils';\nimport { TransformFunc, EnvFunc, ReadFileFunc } from './types';\n\n// Parsers for each type of included file\nconst includeFileParser: {\n [ext in string]: (content: string) => Promise<JsonObject>;\n} = {\n '.json': async content => JSON.parse(content),\n '.yaml': async content => yaml.parse(content),\n '.yml': async content => yaml.parse(content),\n};\n\n/**\n * Transforms a include description into the actual included value.\n */\nexport function createIncludeTransform(\n env: EnvFunc,\n readFile: ReadFileFunc,\n substitute: TransformFunc,\n): TransformFunc {\n return async (input: JsonValue, baseDir: string) => {\n if (!isObject(input)) {\n return { applied: false };\n }\n // Check if there's any key that starts with a '$', in that case we treat\n // this entire object as an include description.\n const [includeKey] = Object.keys(input).filter(key => key.startsWith('$'));\n if (includeKey) {\n if (Object.keys(input).length !== 1) {\n throw new Error(\n `include key ${includeKey} should not have adjacent keys`,\n );\n }\n } else {\n return { applied: false };\n }\n\n const rawIncludedValue = input[includeKey];\n if (typeof rawIncludedValue !== 'string') {\n throw new Error(`${includeKey} include value is not a string`);\n }\n\n const substituteResults = await substitute(rawIncludedValue, baseDir);\n const includeValue = substituteResults.applied\n ? substituteResults.value\n : rawIncludedValue;\n\n // The second string check is needed for Typescript to know this is a string.\n if (includeValue === undefined || typeof includeValue !== 'string') {\n throw new Error(`${includeKey} substitution value was undefined`);\n }\n\n switch (includeKey) {\n case '$file':\n try {\n const value = await readFile(resolvePath(baseDir, includeValue));\n return { applied: true, value };\n } catch (error) {\n throw new Error(`failed to read file ${includeValue}, ${error}`);\n }\n case '$env':\n try {\n return { applied: true, value: await env(includeValue) };\n } catch (error) {\n throw new Error(`failed to read env ${includeValue}, ${error}`);\n }\n\n case '$include': {\n const [filePath, dataPath] = includeValue.split(/#(.*)/);\n\n const ext = extname(filePath);\n const parser = includeFileParser[ext];\n if (!parser) {\n throw new Error(\n `no configuration parser available for included file ${filePath}`,\n );\n }\n\n const path = resolvePath(baseDir, filePath);\n const content = await readFile(path);\n const newBaseDir = dirname(path);\n\n const parts = dataPath ? dataPath.split('.') : [];\n\n let value: JsonValue | undefined;\n try {\n value = await parser(content);\n } catch (error) {\n throw new Error(\n `failed to parse included file ${filePath}, ${error}`,\n );\n }\n\n // This bit handles selecting a subtree in the included file, if a path was provided after a #\n for (const [index, part] of parts.entries()) {\n if (!isObject(value)) {\n const errPath = parts.slice(0, index).join('.');\n throw new Error(\n `value at '${errPath}' in included file ${filePath} is not an object`,\n );\n }\n value = value[part];\n }\n\n return {\n applied: true,\n value,\n newBaseDir: newBaseDir !== baseDir ? newBaseDir : undefined,\n };\n }\n\n default:\n throw new Error(`unknown include ${includeKey}`);\n }\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue } from '@backstage/config';\nimport { TransformFunc, EnvFunc } from './types';\n\n/**\n * A environment variable substitution transform that transforms e.g. 'token ${MY_TOKEN}'\n * to 'token abc' if MY_TOKEN is 'abc'. If any of the substituted variables are undefined,\n * the entire expression ends up undefined.\n */\nexport function createSubstitutionTransform(env: EnvFunc): TransformFunc {\n return async (input: JsonValue) => {\n if (typeof input !== 'string') {\n return { applied: false };\n }\n\n const parts: (string | undefined)[] = input.split(/(\\$?\\$\\{[^{}]*\\})/);\n for (let i = 1; i < parts.length; i += 2) {\n const part = parts[i]!;\n if (part.startsWith('$$')) {\n parts[i] = part.slice(1);\n } else {\n parts[i] = await env(part.slice(2, -1).trim());\n }\n }\n\n if (parts.some(part => part === undefined)) {\n return { applied: true, value: undefined };\n }\n return { applied: true, value: parts.join('') };\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\n\n/**\n * An sub-set of configuration schema.\n */\nexport type ConfigSchemaPackageEntry = {\n /**\n * The configuration schema itself.\n */\n value: JsonObject;\n /**\n * The relative path that the configuration schema was discovered at.\n */\n path: string;\n};\n\n/**\n * A list of all possible configuration value visibilities.\n */\nexport const CONFIG_VISIBILITIES = ['frontend', 'backend', 'secret'] as const;\n\n/**\n * A type representing the possible configuration value visibilities\n *\n * @public\n */\nexport type ConfigVisibility = 'frontend' | 'backend' | 'secret';\n\n/**\n * The default configuration visibility if no other values is given.\n */\nexport const DEFAULT_CONFIG_VISIBILITY: ConfigVisibility = 'backend';\n\n/**\n * An explanation of a configuration validation error.\n */\nexport type ValidationError = {\n keyword: string;\n dataPath: string;\n schemaPath: string;\n params: Record<string, any>;\n propertyName?: string;\n message?: string;\n};\n\n/**\n * The result of validating configuration data using a schema.\n */\ntype ValidationResult = {\n /**\n * Errors that where emitted during validation, if any.\n */\n errors?: ValidationError[];\n /**\n * The configuration visibilities that were discovered during validation.\n *\n * The path in the key uses the form `/<key>/<sub-key>/<array-index>/<leaf-key>`\n */\n visibilityByDataPath: Map<string, ConfigVisibility>;\n\n /**\n * The configuration visibilities that were discovered during validation.\n *\n * The path in the key uses the form `/properties/<key>/items/additionalProperties/<leaf-key>`\n */\n visibilityBySchemaPath: Map<string, ConfigVisibility>;\n};\n\n/**\n * A function used validate configuration data.\n */\nexport type ValidationFunc = (configs: AppConfig[]) => ValidationResult;\n\n/**\n * A function used to transform primitive configuration values.\n *\n * @public\n */\nexport type TransformFunc<T extends number | string | boolean> = (\n value: T,\n context: { visibility: ConfigVisibility },\n) => T | undefined;\n\n/**\n * Options used to process configuration data with a schema.\n *\n * @public\n */\nexport type ConfigSchemaProcessingOptions = {\n /**\n * The visibilities that should be included in the output data.\n * If omitted, the data will not be filtered by visibility.\n */\n visibility?: ConfigVisibility[];\n\n /**\n * A transform function that can be used to transform primitive configuration values\n * during validation. The value returned from the transform function will be used\n * instead of the original value. If the transform returns `undefined`, the value\n * will be omitted.\n */\n valueTransform?: TransformFunc<any>;\n\n /**\n * Whether or not to include the `filteredKeys` property in the output `AppConfig`s.\n *\n * Default: `false`.\n */\n withFilteredKeys?: boolean;\n};\n\n/**\n * A loaded configuration schema that is ready to process configuration data.\n *\n * @public\n */\nexport type ConfigSchema = {\n process(\n appConfigs: AppConfig[],\n options?: ConfigSchemaProcessingOptions,\n ): AppConfig[];\n\n serialize(): JsonObject;\n};\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport Ajv from 'ajv';\nimport { JSONSchema7 as JSONSchema } from 'json-schema';\nimport mergeAllOf, { Resolvers } from 'json-schema-merge-allof';\nimport traverse from 'json-schema-traverse';\nimport { ConfigReader } from '@backstage/config';\nimport {\n ConfigSchemaPackageEntry,\n ValidationFunc,\n CONFIG_VISIBILITIES,\n ConfigVisibility,\n} from './types';\n\n/**\n * This takes a collection of Backstage configuration schemas from various\n * sources and compiles them down into a single schema validation function.\n *\n * It also handles the implementation of the custom \"visibility\" keyword used\n * to specify the scope of different config paths.\n */\nexport function compileConfigSchemas(\n schemas: ConfigSchemaPackageEntry[],\n): ValidationFunc {\n // The ajv instance below is stateful and doesn't really allow for additional\n // output during validation. We work around this by having this extra piece\n // of state that we reset before each validation.\n const visibilityByDataPath = new Map<string, ConfigVisibility>();\n\n const ajv = new Ajv({\n allErrors: true,\n allowUnionTypes: true,\n schemas: {\n 'https://backstage.io/schema/config-v1': true,\n },\n }).addKeyword({\n keyword: 'visibility',\n metaSchema: {\n type: 'string',\n enum: CONFIG_VISIBILITIES,\n },\n compile(visibility: ConfigVisibility) {\n return (_data, context) => {\n if (context?.dataPath === undefined) {\n return false;\n }\n if (visibility && visibility !== 'backend') {\n const normalizedPath = context.dataPath.replace(\n /\\['?(.*?)'?\\]/g,\n (_, segment) => `/${segment}`,\n );\n visibilityByDataPath.set(normalizedPath, visibility);\n }\n return true;\n };\n },\n });\n\n for (const schema of schemas) {\n try {\n ajv.compile(schema.value);\n } catch (error) {\n throw new Error(`Schema at ${schema.path} is invalid, ${error}`);\n }\n }\n\n const merged = mergeConfigSchemas(schemas.map(_ => _.value));\n const validate = ajv.compile(merged);\n\n const visibilityBySchemaPath = new Map<string, ConfigVisibility>();\n traverse(merged, (schema, path) => {\n if (schema.visibility && schema.visibility !== 'backend') {\n visibilityBySchemaPath.set(path, schema.visibility);\n }\n });\n\n return configs => {\n const config = ConfigReader.fromConfigs(configs).get();\n\n visibilityByDataPath.clear();\n\n const valid = validate(config);\n if (!valid) {\n return {\n errors: validate.errors ?? [],\n visibilityByDataPath: new Map(visibilityByDataPath),\n visibilityBySchemaPath,\n };\n }\n\n return {\n visibilityByDataPath: new Map(visibilityByDataPath),\n visibilityBySchemaPath,\n };\n };\n}\n\n/**\n * Given a list of configuration schemas from packages, merge them\n * into a single json schema.\n *\n * @public\n */\nexport function mergeConfigSchemas(schemas: JSONSchema[]): JSONSchema {\n const merged = mergeAllOf(\n { allOf: schemas },\n {\n // JSONSchema is typically subtractive, as in it always reduces the set of allowed\n // inputs through constraints. This changes the object property merging to be additive\n // rather than subtractive.\n ignoreAdditionalProperties: true,\n resolvers: {\n // This ensures that the visibilities across different schemas are sound, and\n // selects the most specific visibility for each path.\n visibility(values: string[], path: string[]) {\n const hasFrontend = values.some(_ => _ === 'frontend');\n const hasSecret = values.some(_ => _ === 'secret');\n if (hasFrontend && hasSecret) {\n throw new Error(\n `Config schema visibility is both 'frontend' and 'secret' for ${path.join(\n '/',\n )}`,\n );\n } else if (hasFrontend) {\n return 'frontend';\n } else if (hasSecret) {\n return 'secret';\n }\n\n return 'backend';\n },\n } as Partial<Resolvers<JSONSchema>>,\n },\n );\n return merged;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport {\n resolve as resolvePath,\n relative as relativePath,\n dirname,\n sep,\n} from 'path';\nimport { ConfigSchemaPackageEntry } from './types';\nimport { getProgramFromFiles, generateSchema } from 'typescript-json-schema';\nimport { JsonObject } from '@backstage/config';\nimport { assertError } from '@backstage/errors';\n\ntype Item = {\n name?: string;\n parentPath?: string;\n packagePath?: string;\n};\n\nconst req =\n typeof __non_webpack_require__ === 'undefined'\n ? require\n : __non_webpack_require__;\n\n/**\n * This collects all known config schemas across all dependencies of the app.\n */\nexport async function collectConfigSchemas(\n packageNames: string[],\n packagePaths: string[],\n): Promise<ConfigSchemaPackageEntry[]> {\n const schemas = new Array<ConfigSchemaPackageEntry>();\n const tsSchemaPaths = new Array<string>();\n const visitedPackageVersions = new Map<string, Set<string>>(); // pkgName: [versions...]\n\n const currentDir = await fs.realpath(process.cwd());\n\n async function processItem(item: Item) {\n let pkgPath = item.packagePath;\n\n if (pkgPath) {\n const pkgExists = await fs.pathExists(pkgPath);\n if (!pkgExists) {\n return;\n }\n } else if (item.name) {\n const { name, parentPath } = item;\n\n try {\n pkgPath = req.resolve(\n `${name}/package.json`,\n parentPath && {\n paths: [parentPath],\n },\n );\n } catch {\n // We can somewhat safely ignore packages that don't export package.json,\n // as they are likely not part of the Backstage ecosystem anyway.\n }\n }\n if (!pkgPath) {\n return;\n }\n\n const pkg = await fs.readJson(pkgPath);\n\n // Ensures that we only process the same version of each package once.\n let versions = visitedPackageVersions.get(pkg.name);\n if (versions?.has(pkg.version)) {\n return;\n }\n if (!versions) {\n versions = new Set();\n visitedPackageVersions.set(pkg.name, versions);\n }\n versions.add(pkg.version);\n\n const depNames = [\n ...Object.keys(pkg.dependencies ?? {}),\n ...Object.keys(pkg.devDependencies ?? {}),\n ...Object.keys(pkg.optionalDependencies ?? {}),\n ...Object.keys(pkg.peerDependencies ?? {}),\n ];\n\n // TODO(Rugvip): Trying this out to avoid having to traverse the full dependency graph,\n // since that's pretty slow. We probably need a better way to determine when\n // we've left the Backstage ecosystem, but this will do for now.\n const hasSchema = 'configSchema' in pkg;\n const hasBackstageDep = depNames.some(_ => _.startsWith('@backstage/'));\n if (!hasSchema && !hasBackstageDep) {\n return;\n }\n if (hasSchema) {\n if (typeof pkg.configSchema === 'string') {\n const isJson = pkg.configSchema.endsWith('.json');\n const isDts = pkg.configSchema.endsWith('.d.ts');\n if (!isJson && !isDts) {\n throw new Error(\n `Config schema files must be .json or .d.ts, got ${pkg.configSchema}`,\n );\n }\n if (isDts) {\n tsSchemaPaths.push(\n relativePath(\n currentDir,\n resolvePath(dirname(pkgPath), pkg.configSchema),\n ),\n );\n } else {\n const path = resolvePath(dirname(pkgPath), pkg.configSchema);\n const value = await fs.readJson(path);\n schemas.push({\n value,\n path: relativePath(currentDir, path),\n });\n }\n } else {\n schemas.push({\n value: pkg.configSchema,\n path: relativePath(currentDir, pkgPath),\n });\n }\n }\n\n await Promise.all(\n depNames.map(depName =>\n processItem({ name: depName, parentPath: pkgPath }),\n ),\n );\n }\n\n await Promise.all([\n ...packageNames.map(name => processItem({ name, parentPath: currentDir })),\n ...packagePaths.map(path => processItem({ name: path, packagePath: path })),\n ]);\n\n const tsSchemas = compileTsSchemas(tsSchemaPaths);\n\n return schemas.concat(tsSchemas);\n}\n\n// This handles the support of TypeScript .d.ts config schema declarations.\n// We collect all typescript schema definition and compile them all in one go.\n// This is much faster than compiling them separately.\nfunction compileTsSchemas(paths: string[]) {\n if (paths.length === 0) {\n return [];\n }\n\n const program = getProgramFromFiles(paths, {\n incremental: false,\n isolatedModules: true,\n lib: ['ES5'], // Skipping most libs speeds processing up a lot, we just need the primitive types anyway\n noEmit: true,\n noResolve: true,\n skipLibCheck: true, // Skipping lib checks speeds things up\n skipDefaultLibCheck: true,\n strict: true,\n typeRoots: [], // Do not include any additional types\n types: [],\n });\n\n const tsSchemas = paths.map(path => {\n let value;\n try {\n value = generateSchema(\n program,\n // All schemas should export a `Config` symbol\n 'Config',\n // This enables usage of @visibility is doc comments\n {\n required: true,\n validationKeywords: ['visibility'],\n },\n [path.split(sep).join('/')], // Unix paths are expected for all OSes here\n ) as JsonObject | null;\n } catch (error) {\n assertError(error);\n if (error.message !== 'type Config not found') {\n throw error;\n }\n }\n\n if (!value) {\n throw new Error(`Invalid schema in ${path}, missing Config export`);\n }\n return { path, value };\n });\n\n return tsSchemas;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport {\n ConfigVisibility,\n DEFAULT_CONFIG_VISIBILITY,\n TransformFunc,\n ValidationError,\n} from './types';\n\n/**\n * This filters data by visibility by discovering the visibility of each\n * value, and then only keeping the ones that are specified in `includeVisibilities`.\n */\nexport function filterByVisibility(\n data: JsonObject,\n includeVisibilities: ConfigVisibility[],\n visibilityByDataPath: Map<string, ConfigVisibility>,\n transformFunc?: TransformFunc<number | string | boolean>,\n withFilteredKeys?: boolean,\n): { data: JsonObject; filteredKeys?: string[] } {\n const filteredKeys = new Array<string>();\n\n function transform(\n jsonVal: JsonValue,\n visibilityPath: string, // Matches the format we get from ajv\n filterPath: string, // Matches the format of the ConfigReader\n ): JsonValue | undefined {\n const visibility =\n visibilityByDataPath.get(visibilityPath) ?? DEFAULT_CONFIG_VISIBILITY;\n const isVisible = includeVisibilities.includes(visibility);\n\n if (typeof jsonVal !== 'object') {\n if (isVisible) {\n if (transformFunc) {\n return transformFunc(jsonVal, { visibility });\n }\n return jsonVal;\n }\n if (withFilteredKeys) {\n filteredKeys.push(filterPath);\n }\n return undefined;\n } else if (jsonVal === null) {\n return undefined;\n } else if (Array.isArray(jsonVal)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of jsonVal.entries()) {\n const out = transform(\n value,\n `${visibilityPath}/${index}`,\n `${filterPath}[${index}]`,\n );\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n if (arr.length > 0 || isVisible) {\n return arr;\n }\n return undefined;\n }\n\n const outObj: JsonObject = {};\n let hasOutput = false;\n\n for (const [key, value] of Object.entries(jsonVal)) {\n if (value === undefined) {\n continue;\n }\n const out = transform(\n value,\n `${visibilityPath}/${key}`,\n filterPath ? `${filterPath}.${key}` : key,\n );\n if (out !== undefined) {\n outObj[key] = out;\n hasOutput = true;\n }\n }\n\n if (hasOutput || isVisible) {\n return outObj;\n }\n return undefined;\n }\n\n return {\n filteredKeys: withFilteredKeys ? filteredKeys : undefined,\n data: (transform(data, '', '') as JsonObject) ?? {},\n };\n}\n\nexport function filterErrorsByVisibility(\n errors: ValidationError[] | undefined,\n includeVisibilities: ConfigVisibility[] | undefined,\n visibilityByDataPath: Map<string, ConfigVisibility>,\n visibilityBySchemaPath: Map<string, ConfigVisibility>,\n): ValidationError[] {\n if (!errors) {\n return [];\n }\n if (!includeVisibilities) {\n return errors;\n }\n\n const visibleSchemaPaths = Array.from(visibilityBySchemaPath)\n .filter(([, v]) => includeVisibilities.includes(v))\n .map(([k]) => k);\n\n // If we're filtering by visibility we only care about the errors that happened\n // in a visible path.\n return errors.filter(error => {\n // We always include structural errors as we don't know whether there are\n // any visible paths within the structures.\n if (\n error.keyword === 'type' &&\n ['object', 'array'].includes(error.params.type)\n ) {\n return true;\n }\n\n // For fields that were required we use the schema path to determine whether\n // it was visible in addition to the data path. This is because the data path\n // visibilities are only populated for values that we reached, which we won't\n // if the value is missing.\n // We don't use this method for all the errors as the data path is more robust\n // and doesn't require us to properly trim the schema path.\n if (error.keyword === 'required') {\n const trimmedPath = error.schemaPath.slice(1, -'/required'.length);\n const fullPath = `${trimmedPath}/properties/${error.params.missingProperty}`;\n if (\n visibleSchemaPaths.some(visiblePath => visiblePath.startsWith(fullPath))\n ) {\n return true;\n }\n }\n\n const vis =\n visibilityByDataPath.get(error.dataPath) ?? DEFAULT_CONFIG_VISIBILITY;\n return vis && includeVisibilities.includes(vis);\n });\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\nimport { compileConfigSchemas } from './compile';\nimport { collectConfigSchemas } from './collect';\nimport { filterByVisibility, filterErrorsByVisibility } from './filtering';\nimport {\n ValidationError,\n ConfigSchema,\n ConfigSchemaPackageEntry,\n CONFIG_VISIBILITIES,\n} from './types';\n\n/**\n * Options that control the loading of configuration schema files in the backend.\n *\n * @public\n */\nexport type LoadConfigSchemaOptions =\n | {\n dependencies: string[];\n packagePaths?: string[];\n }\n | {\n serialized: JsonObject;\n };\n\nfunction errorsToError(errors: ValidationError[]): Error {\n const messages = errors.map(({ dataPath, message, params }) => {\n const paramStr = Object.entries(params)\n .map(([name, value]) => `${name}=${value}`)\n .join(' ');\n return `Config ${message || ''} { ${paramStr} } at ${dataPath}`;\n });\n const error = new Error(`Config validation failed, ${messages.join('; ')}`);\n (error as any).messages = messages;\n return error;\n}\n\n/**\n * Loads config schema for a Backstage instance.\n *\n * @public\n */\nexport async function loadConfigSchema(\n options: LoadConfigSchemaOptions,\n): Promise<ConfigSchema> {\n let schemas: ConfigSchemaPackageEntry[];\n\n if ('dependencies' in options) {\n schemas = await collectConfigSchemas(\n options.dependencies,\n options.packagePaths ?? [],\n );\n } else {\n const { serialized } = options;\n if (serialized?.backstageConfigSchemaVersion !== 1) {\n throw new Error(\n 'Serialized configuration schema is invalid or has an invalid version number',\n );\n }\n schemas = serialized.schemas as ConfigSchemaPackageEntry[];\n }\n\n const validate = compileConfigSchemas(schemas);\n\n return {\n process(\n configs: AppConfig[],\n { visibility, valueTransform, withFilteredKeys } = {},\n ): AppConfig[] {\n const result = validate(configs);\n\n const visibleErrors = filterErrorsByVisibility(\n result.errors,\n visibility,\n result.visibilityByDataPath,\n result.visibilityBySchemaPath,\n );\n if (visibleErrors.length > 0) {\n throw errorsToError(visibleErrors);\n }\n\n let processedConfigs = configs;\n\n if (visibility) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n visibility,\n result.visibilityByDataPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n } else if (valueTransform) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n Array.from(CONFIG_VISIBILITIES),\n result.visibilityByDataPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n }\n\n return processedConfigs;\n },\n serialize(): JsonObject {\n return {\n schemas,\n backstageConfigSchemaVersion: 1,\n };\n },\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport yaml from 'yaml';\nimport chokidar from 'chokidar';\nimport { resolve as resolvePath, dirname, isAbsolute, basename } from 'path';\nimport { AppConfig } from '@backstage/config';\nimport { ForwardedError } from '@backstage/errors';\nimport {\n applyConfigTransforms,\n readEnvConfig,\n createIncludeTransform,\n createSubstitutionTransform,\n} from './lib';\n\n/**\n * Options that control the loading of configuration files in the backend.\n *\n * @public\n */\nexport type LoadConfigOptions = {\n // The root directory of the config loading context. Used to find default configs.\n configRoot: string;\n\n // Absolute paths to load config files from. Configs from earlier paths have lower priority.\n configPaths: string[];\n\n /** @deprecated This option has been removed */\n env?: string;\n\n /**\n * Custom environment variable loading function\n *\n * @experimental This API is not stable and may change at any point\n */\n experimentalEnvFunc?: (name: string) => Promise<string | undefined>;\n\n /**\n * An optional configuration that enables watching of config files.\n */\n watch?: {\n /**\n * A listener that is called when a config file is changed.\n */\n onChange: (configs: AppConfig[]) => void;\n\n /**\n * An optional signal that stops the watcher once the promise resolves.\n */\n stopSignal?: Promise<void>;\n };\n};\n\n/**\n * Load configuration data.\n *\n * @public\n */\nexport async function loadConfig(\n options: LoadConfigOptions,\n): Promise<AppConfig[]> {\n const { configRoot, experimentalEnvFunc: envFunc, watch } = options;\n const configPaths = options.configPaths.slice();\n\n // If no paths are provided, we default to reading\n // `app-config.yaml` and, if it exists, `app-config.local.yaml`\n if (configPaths.length === 0) {\n configPaths.push(resolvePath(configRoot, 'app-config.yaml'));\n\n const localConfig = resolvePath(configRoot, 'app-config.local.yaml');\n if (await fs.pathExists(localConfig)) {\n configPaths.push(localConfig);\n }\n }\n\n const env = envFunc ?? (async (name: string) => process.env[name]);\n\n const loadConfigFiles = async () => {\n const configs = [];\n\n for (const configPath of configPaths) {\n if (!isAbsolute(configPath)) {\n throw new Error(`Config load path is not absolute: '${configPath}'`);\n }\n\n const dir = dirname(configPath);\n const readFile = (path: string) =>\n fs.readFile(resolvePath(dir, path), 'utf8');\n\n const input = yaml.parse(await readFile(configPath));\n const substitutionTransform = createSubstitutionTransform(env);\n const data = await applyConfigTransforms(dir, input, [\n createIncludeTransform(env, readFile, substitutionTransform),\n substitutionTransform,\n ]);\n\n configs.push({ data, context: basename(configPath) });\n }\n\n return configs;\n };\n\n let fileConfigs;\n try {\n fileConfigs = await loadConfigFiles();\n } catch (error) {\n throw new ForwardedError('Failed to read static configuration file', error);\n }\n\n const envConfigs = await readEnvConfig(process.env);\n\n // Set up config file watching if requested by the caller\n if (watch) {\n let currentSerializedConfig = JSON.stringify(fileConfigs);\n\n const watcher = chokidar.watch(configPaths, {\n usePolling: process.env.NODE_ENV === 'test',\n });\n watcher.on('change', async () => {\n try {\n const newConfigs = await loadConfigFiles();\n const newSerializedConfig = JSON.stringify(newConfigs);\n\n if (currentSerializedConfig === newSerializedConfig) {\n return;\n }\n currentSerializedConfig = newSerializedConfig;\n\n watch.onChange([...newConfigs, ...envConfigs]);\n } catch (error) {\n console.error(`Failed to reload configuration files, ${error}`);\n }\n });\n\n if (watch.stopSignal) {\n watch.stopSignal.then(() => {\n watcher.close();\n });\n }\n }\n\n return [...fileConfigs, ...envConfigs];\n}\n"],"names":["yaml","resolvePath","extname","path","dirname","Ajv","config","ConfigReader","mergeAllOf","fs","relativePath","getProgramFromFiles","generateSchema","sep","isAbsolute","basename","ForwardedError","chokidar"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAM,aAAa;AAGnB,MAAM,0BAA0B;uBAsBF,KAEd;AA9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cAAc,aAAa;AACpC,UAAI,CAACK,gBAAW,aAAa;AAC3B,cAAM,IAAI,MAAM,sCAAsC;AAAA;AAGxD,YAAM,MAAMV,aAAQ;AACpB,YAAM,WAAW,CAACD,WAChBM,uBAAG,SAASR,aAAY,KAAKE,SAAO;AAEtC,YAAM,QAAQH,yBAAK,MAAM,MAAM,SAAS;AACxC,YAAM,wBAAwB,4BAA4B;AAC1D,YAAM,OAAO,MAAM,sBAAsB,KAAK,OAAO;AAAA,QACnD,uBAAuB,KAAK,UAAU;AAAA,QACtC;AAAA;AAGF,cAAQ,KAAK,CAAE,MAAM,SAASe,cAAS;AAAA;AAGzC,WAAO;AAAA;AAGT,MAAI;AACJ,MAAI;AACF,kBAAc,MAAM;AAAA,WACb,OAAP;AACA,UAAM,IAAIC,sBAAe,4CAA4C;AAAA;AAGvE,QAAM,aAAa,MAAM,cAAc,QAAQ;AAG/C,MAAI,OAAO;AACT,QAAI,0BAA0B,KAAK,UAAU;AAE7C,UAAM,UAAUC,6BAAS,MAAM,aAAa;AAAA,MAC1C,YAAY,QAAQ,IAAI,aAAa;AAAA;AAEvC,YAAQ,GAAG,UAAU,YAAY;AAC/B,UAAI;AACF,cAAM,aAAa,MAAM;AACzB,cAAM,sBAAsB,KAAK,UAAU;AAE3C,YAAI,4BAA4B,qBAAqB;AACnD;AAAA;AAEF,kCAA0B;AAE1B,cAAM,SAAS,CAAC,GAAG,YAAY,GAAG;AAAA,eAC3B,OAAP;AACA,gBAAQ,MAAM,yCAAyC;AAAA;AAAA;AAI3D,QAAI,MAAM,YAAY;AACpB,YAAM,WAAW,KAAK,MAAM;AAC1B,gBAAQ;AAAA;AAAA;AAAA;AAKd,SAAO,CAAC,GAAG,aAAa,GAAG;AAAA;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -18,31 +18,33 @@ import { JSONSchema7 } from 'json-schema';
|
|
|
18
18
|
* For example, to set the config app.title to "My Title", use the following:
|
|
19
19
|
*
|
|
20
20
|
* APP_CONFIG_app_title='"My Title"'
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
21
23
|
*/
|
|
22
24
|
declare function readEnvConfig(env: {
|
|
23
25
|
[name: string]: string | undefined;
|
|
24
26
|
}): AppConfig[];
|
|
25
27
|
|
|
26
|
-
declare type EnvFunc = (name: string) => Promise<string | undefined>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* A list of all possible configuration value visibilities.
|
|
30
|
-
*/
|
|
31
|
-
declare const CONFIG_VISIBILITIES: readonly ["frontend", "backend", "secret"];
|
|
32
28
|
/**
|
|
33
29
|
* A type representing the possible configuration value visibilities
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
34
32
|
*/
|
|
35
|
-
declare type ConfigVisibility =
|
|
33
|
+
declare type ConfigVisibility = 'frontend' | 'backend' | 'secret';
|
|
36
34
|
/**
|
|
37
35
|
* A function used to transform primitive configuration values.
|
|
36
|
+
*
|
|
37
|
+
* @public
|
|
38
38
|
*/
|
|
39
39
|
declare type TransformFunc<T extends number | string | boolean> = (value: T, context: {
|
|
40
40
|
visibility: ConfigVisibility;
|
|
41
41
|
}) => T | undefined;
|
|
42
42
|
/**
|
|
43
43
|
* Options used to process configuration data with a schema.
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
44
46
|
*/
|
|
45
|
-
declare type
|
|
47
|
+
declare type ConfigSchemaProcessingOptions = {
|
|
46
48
|
/**
|
|
47
49
|
* The visibilities that should be included in the output data.
|
|
48
50
|
* If omitted, the data will not be filtered by visibility.
|
|
@@ -64,28 +66,45 @@ declare type ConfigProcessingOptions = {
|
|
|
64
66
|
};
|
|
65
67
|
/**
|
|
66
68
|
* A loaded configuration schema that is ready to process configuration data.
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
67
71
|
*/
|
|
68
72
|
declare type ConfigSchema = {
|
|
69
|
-
process(appConfigs: AppConfig[], options?:
|
|
73
|
+
process(appConfigs: AppConfig[], options?: ConfigSchemaProcessingOptions): AppConfig[];
|
|
70
74
|
serialize(): JsonObject;
|
|
71
75
|
};
|
|
72
76
|
|
|
73
77
|
/**
|
|
74
78
|
* Given a list of configuration schemas from packages, merge them
|
|
75
79
|
* into a single json schema.
|
|
80
|
+
*
|
|
81
|
+
* @public
|
|
76
82
|
*/
|
|
77
83
|
declare function mergeConfigSchemas(schemas: JSONSchema7[]): JSONSchema7;
|
|
78
84
|
|
|
79
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Options that control the loading of configuration schema files in the backend.
|
|
87
|
+
*
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
declare type LoadConfigSchemaOptions = {
|
|
80
91
|
dependencies: string[];
|
|
92
|
+
packagePaths?: string[];
|
|
81
93
|
} | {
|
|
82
94
|
serialized: JsonObject;
|
|
83
95
|
};
|
|
84
96
|
/**
|
|
85
97
|
* Loads config schema for a Backstage instance.
|
|
98
|
+
*
|
|
99
|
+
* @public
|
|
86
100
|
*/
|
|
87
|
-
declare function loadConfigSchema(options:
|
|
101
|
+
declare function loadConfigSchema(options: LoadConfigSchemaOptions): Promise<ConfigSchema>;
|
|
88
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Options that control the loading of configuration files in the backend.
|
|
105
|
+
*
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
89
108
|
declare type LoadConfigOptions = {
|
|
90
109
|
configRoot: string;
|
|
91
110
|
configPaths: string[];
|
|
@@ -96,7 +115,7 @@ declare type LoadConfigOptions = {
|
|
|
96
115
|
*
|
|
97
116
|
* @experimental This API is not stable and may change at any point
|
|
98
117
|
*/
|
|
99
|
-
experimentalEnvFunc?:
|
|
118
|
+
experimentalEnvFunc?: (name: string) => Promise<string | undefined>;
|
|
100
119
|
/**
|
|
101
120
|
* An optional configuration that enables watching of config files.
|
|
102
121
|
*/
|
|
@@ -111,6 +130,11 @@ declare type LoadConfigOptions = {
|
|
|
111
130
|
stopSignal?: Promise<void>;
|
|
112
131
|
};
|
|
113
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* Load configuration data.
|
|
135
|
+
*
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
114
138
|
declare function loadConfig(options: LoadConfigOptions): Promise<AppConfig[]>;
|
|
115
139
|
|
|
116
|
-
export { ConfigSchema, ConfigVisibility, LoadConfigOptions, loadConfig, loadConfigSchema, mergeConfigSchemas, readEnvConfig };
|
|
140
|
+
export { ConfigSchema, ConfigSchemaProcessingOptions, ConfigVisibility, LoadConfigOptions, LoadConfigSchemaOptions, TransformFunc, loadConfig, loadConfigSchema, mergeConfigSchemas, readEnvConfig };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { assertError, ForwardedError } from '@backstage/errors';
|
|
1
2
|
import yaml from 'yaml';
|
|
2
3
|
import { extname, resolve, dirname, sep, relative, isAbsolute, basename } from 'path';
|
|
3
4
|
import Ajv from 'ajv';
|
|
4
5
|
import mergeAllOf from 'json-schema-merge-allof';
|
|
6
|
+
import traverse from 'json-schema-traverse';
|
|
5
7
|
import { ConfigReader } from '@backstage/config';
|
|
6
8
|
import fs from 'fs-extra';
|
|
7
9
|
import { getProgramFromFiles, generateSchema } from 'typescript-json-schema';
|
|
@@ -53,6 +55,7 @@ function safeJsonParse(str) {
|
|
|
53
55
|
try {
|
|
54
56
|
return [null, JSON.parse(str)];
|
|
55
57
|
} catch (err) {
|
|
58
|
+
assertError(err);
|
|
56
59
|
return [err, str];
|
|
57
60
|
}
|
|
58
61
|
}
|
|
@@ -83,6 +86,7 @@ async function applyConfigTransforms(initialDir, input, transforms) {
|
|
|
83
86
|
break;
|
|
84
87
|
}
|
|
85
88
|
} catch (error) {
|
|
89
|
+
assertError(error);
|
|
86
90
|
throw new Error(`error at ${path}, ${error.message}`);
|
|
87
91
|
}
|
|
88
92
|
}
|
|
@@ -220,7 +224,7 @@ const CONFIG_VISIBILITIES = ["frontend", "backend", "secret"];
|
|
|
220
224
|
const DEFAULT_CONFIG_VISIBILITY = "backend";
|
|
221
225
|
|
|
222
226
|
function compileConfigSchemas(schemas) {
|
|
223
|
-
const
|
|
227
|
+
const visibilityByDataPath = new Map();
|
|
224
228
|
const ajv = new Ajv({
|
|
225
229
|
allErrors: true,
|
|
226
230
|
allowUnionTypes: true,
|
|
@@ -240,7 +244,7 @@ function compileConfigSchemas(schemas) {
|
|
|
240
244
|
}
|
|
241
245
|
if (visibility && visibility !== "backend") {
|
|
242
246
|
const normalizedPath = context.dataPath.replace(/\['?(.*?)'?\]/g, (_, segment) => `/${segment}`);
|
|
243
|
-
|
|
247
|
+
visibilityByDataPath.set(normalizedPath, visibility);
|
|
244
248
|
}
|
|
245
249
|
return true;
|
|
246
250
|
};
|
|
@@ -255,23 +259,27 @@ function compileConfigSchemas(schemas) {
|
|
|
255
259
|
}
|
|
256
260
|
const merged = mergeConfigSchemas(schemas.map((_) => _.value));
|
|
257
261
|
const validate = ajv.compile(merged);
|
|
262
|
+
const visibilityBySchemaPath = new Map();
|
|
263
|
+
traverse(merged, (schema, path) => {
|
|
264
|
+
if (schema.visibility && schema.visibility !== "backend") {
|
|
265
|
+
visibilityBySchemaPath.set(path, schema.visibility);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
258
268
|
return (configs) => {
|
|
259
269
|
var _a;
|
|
260
270
|
const config = ConfigReader.fromConfigs(configs).get();
|
|
261
|
-
|
|
271
|
+
visibilityByDataPath.clear();
|
|
262
272
|
const valid = validate(config);
|
|
263
273
|
if (!valid) {
|
|
264
|
-
const errors = (_a = validate.errors) != null ? _a : [];
|
|
265
274
|
return {
|
|
266
|
-
errors:
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}),
|
|
270
|
-
visibilityByPath: new Map()
|
|
275
|
+
errors: (_a = validate.errors) != null ? _a : [],
|
|
276
|
+
visibilityByDataPath: new Map(visibilityByDataPath),
|
|
277
|
+
visibilityBySchemaPath
|
|
271
278
|
};
|
|
272
279
|
}
|
|
273
280
|
return {
|
|
274
|
-
|
|
281
|
+
visibilityByDataPath: new Map(visibilityByDataPath),
|
|
282
|
+
visibilityBySchemaPath
|
|
275
283
|
};
|
|
276
284
|
};
|
|
277
285
|
}
|
|
@@ -297,26 +305,41 @@ function mergeConfigSchemas(schemas) {
|
|
|
297
305
|
}
|
|
298
306
|
|
|
299
307
|
const req = typeof __non_webpack_require__ === "undefined" ? require : __non_webpack_require__;
|
|
300
|
-
async function collectConfigSchemas(packageNames) {
|
|
301
|
-
const
|
|
302
|
-
const
|
|
303
|
-
const
|
|
308
|
+
async function collectConfigSchemas(packageNames, packagePaths) {
|
|
309
|
+
const schemas = new Array();
|
|
310
|
+
const tsSchemaPaths = new Array();
|
|
311
|
+
const visitedPackageVersions = new Map();
|
|
304
312
|
const currentDir = await fs.realpath(process.cwd());
|
|
305
|
-
async function processItem(
|
|
313
|
+
async function processItem(item) {
|
|
306
314
|
var _a, _b, _c, _d;
|
|
307
|
-
|
|
308
|
-
|
|
315
|
+
let pkgPath = item.packagePath;
|
|
316
|
+
if (pkgPath) {
|
|
317
|
+
const pkgExists = await fs.pathExists(pkgPath);
|
|
318
|
+
if (!pkgExists) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
} else if (item.name) {
|
|
322
|
+
const {name, parentPath} = item;
|
|
323
|
+
try {
|
|
324
|
+
pkgPath = req.resolve(`${name}/package.json`, parentPath && {
|
|
325
|
+
paths: [parentPath]
|
|
326
|
+
});
|
|
327
|
+
} catch {
|
|
328
|
+
}
|
|
309
329
|
}
|
|
310
|
-
|
|
311
|
-
let pkgPath;
|
|
312
|
-
try {
|
|
313
|
-
pkgPath = req.resolve(`${name}/package.json`, parentPath && {
|
|
314
|
-
paths: [parentPath]
|
|
315
|
-
});
|
|
316
|
-
} catch {
|
|
330
|
+
if (!pkgPath) {
|
|
317
331
|
return;
|
|
318
332
|
}
|
|
319
333
|
const pkg = await fs.readJson(pkgPath);
|
|
334
|
+
let versions = visitedPackageVersions.get(pkg.name);
|
|
335
|
+
if (versions == null ? void 0 : versions.has(pkg.version)) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!versions) {
|
|
339
|
+
versions = new Set();
|
|
340
|
+
visitedPackageVersions.set(pkg.name, versions);
|
|
341
|
+
}
|
|
342
|
+
versions.add(pkg.version);
|
|
320
343
|
const depNames = [
|
|
321
344
|
...Object.keys((_a = pkg.dependencies) != null ? _a : {}),
|
|
322
345
|
...Object.keys((_b = pkg.devDependencies) != null ? _b : {}),
|
|
@@ -354,7 +377,10 @@ async function collectConfigSchemas(packageNames) {
|
|
|
354
377
|
}
|
|
355
378
|
await Promise.all(depNames.map((depName) => processItem({name: depName, parentPath: pkgPath})));
|
|
356
379
|
}
|
|
357
|
-
await Promise.all(
|
|
380
|
+
await Promise.all([
|
|
381
|
+
...packageNames.map((name) => processItem({name, parentPath: currentDir})),
|
|
382
|
+
...packagePaths.map((path) => processItem({name: path, packagePath: path}))
|
|
383
|
+
]);
|
|
358
384
|
const tsSchemas = compileTsSchemas(tsSchemaPaths);
|
|
359
385
|
return schemas.concat(tsSchemas);
|
|
360
386
|
}
|
|
@@ -382,6 +408,7 @@ function compileTsSchemas(paths) {
|
|
|
382
408
|
validationKeywords: ["visibility"]
|
|
383
409
|
}, [path.split(sep).join("/")]);
|
|
384
410
|
} catch (error) {
|
|
411
|
+
assertError(error);
|
|
385
412
|
if (error.message !== "type Config not found") {
|
|
386
413
|
throw error;
|
|
387
414
|
}
|
|
@@ -394,12 +421,12 @@ function compileTsSchemas(paths) {
|
|
|
394
421
|
return tsSchemas;
|
|
395
422
|
}
|
|
396
423
|
|
|
397
|
-
function filterByVisibility(data, includeVisibilities,
|
|
424
|
+
function filterByVisibility(data, includeVisibilities, visibilityByDataPath, transformFunc, withFilteredKeys) {
|
|
398
425
|
var _a;
|
|
399
426
|
const filteredKeys = new Array();
|
|
400
427
|
function transform(jsonVal, visibilityPath, filterPath) {
|
|
401
428
|
var _a2;
|
|
402
|
-
const visibility = (_a2 =
|
|
429
|
+
const visibility = (_a2 = visibilityByDataPath.get(visibilityPath)) != null ? _a2 : DEFAULT_CONFIG_VISIBILITY;
|
|
403
430
|
const isVisible = includeVisibilities.includes(visibility);
|
|
404
431
|
if (typeof jsonVal !== "object") {
|
|
405
432
|
if (isVisible) {
|
|
@@ -449,11 +476,45 @@ function filterByVisibility(data, includeVisibilities, visibilityByPath, transfo
|
|
|
449
476
|
data: (_a = transform(data, "", "")) != null ? _a : {}
|
|
450
477
|
};
|
|
451
478
|
}
|
|
479
|
+
function filterErrorsByVisibility(errors, includeVisibilities, visibilityByDataPath, visibilityBySchemaPath) {
|
|
480
|
+
if (!errors) {
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
if (!includeVisibilities) {
|
|
484
|
+
return errors;
|
|
485
|
+
}
|
|
486
|
+
const visibleSchemaPaths = Array.from(visibilityBySchemaPath).filter(([, v]) => includeVisibilities.includes(v)).map(([k]) => k);
|
|
487
|
+
return errors.filter((error) => {
|
|
488
|
+
var _a;
|
|
489
|
+
if (error.keyword === "type" && ["object", "array"].includes(error.params.type)) {
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
if (error.keyword === "required") {
|
|
493
|
+
const trimmedPath = error.schemaPath.slice(1, -"/required".length);
|
|
494
|
+
const fullPath = `${trimmedPath}/properties/${error.params.missingProperty}`;
|
|
495
|
+
if (visibleSchemaPaths.some((visiblePath) => visiblePath.startsWith(fullPath))) {
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
const vis = (_a = visibilityByDataPath.get(error.dataPath)) != null ? _a : DEFAULT_CONFIG_VISIBILITY;
|
|
500
|
+
return vis && includeVisibilities.includes(vis);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
452
503
|
|
|
504
|
+
function errorsToError(errors) {
|
|
505
|
+
const messages = errors.map(({dataPath, message, params}) => {
|
|
506
|
+
const paramStr = Object.entries(params).map(([name, value]) => `${name}=${value}`).join(" ");
|
|
507
|
+
return `Config ${message || ""} { ${paramStr} } at ${dataPath}`;
|
|
508
|
+
});
|
|
509
|
+
const error = new Error(`Config validation failed, ${messages.join("; ")}`);
|
|
510
|
+
error.messages = messages;
|
|
511
|
+
return error;
|
|
512
|
+
}
|
|
453
513
|
async function loadConfigSchema(options) {
|
|
514
|
+
var _a;
|
|
454
515
|
let schemas;
|
|
455
516
|
if ("dependencies" in options) {
|
|
456
|
-
schemas = await collectConfigSchemas(options.dependencies);
|
|
517
|
+
schemas = await collectConfigSchemas(options.dependencies, (_a = options.packagePaths) != null ? _a : []);
|
|
457
518
|
} else {
|
|
458
519
|
const {serialized} = options;
|
|
459
520
|
if ((serialized == null ? void 0 : serialized.backstageConfigSchemaVersion) !== 1) {
|
|
@@ -465,21 +526,20 @@ async function loadConfigSchema(options) {
|
|
|
465
526
|
return {
|
|
466
527
|
process(configs, {visibility, valueTransform, withFilteredKeys} = {}) {
|
|
467
528
|
const result = validate(configs);
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
throw error;
|
|
529
|
+
const visibleErrors = filterErrorsByVisibility(result.errors, visibility, result.visibilityByDataPath, result.visibilityBySchemaPath);
|
|
530
|
+
if (visibleErrors.length > 0) {
|
|
531
|
+
throw errorsToError(visibleErrors);
|
|
472
532
|
}
|
|
473
533
|
let processedConfigs = configs;
|
|
474
534
|
if (visibility) {
|
|
475
535
|
processedConfigs = processedConfigs.map(({data, context}) => ({
|
|
476
536
|
context,
|
|
477
|
-
...filterByVisibility(data, visibility, result.
|
|
537
|
+
...filterByVisibility(data, visibility, result.visibilityByDataPath, valueTransform, withFilteredKeys)
|
|
478
538
|
}));
|
|
479
539
|
} else if (valueTransform) {
|
|
480
540
|
processedConfigs = processedConfigs.map(({data, context}) => ({
|
|
481
541
|
context,
|
|
482
|
-
...filterByVisibility(data, Array.from(CONFIG_VISIBILITIES), result.
|
|
542
|
+
...filterByVisibility(data, Array.from(CONFIG_VISIBILITIES), result.visibilityByDataPath, valueTransform, withFilteredKeys)
|
|
483
543
|
}));
|
|
484
544
|
}
|
|
485
545
|
return processedConfigs;
|
|
@@ -526,7 +586,7 @@ async function loadConfig(options) {
|
|
|
526
586
|
try {
|
|
527
587
|
fileConfigs = await loadConfigFiles();
|
|
528
588
|
} catch (error) {
|
|
529
|
-
throw new
|
|
589
|
+
throw new ForwardedError("Failed to read static configuration file", error);
|
|
530
590
|
}
|
|
531
591
|
const envConfigs = await readEnvConfig(process.env);
|
|
532
592
|
if (watch) {
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/lib/env.ts","../src/lib/transform/utils.ts","../src/lib/transform/apply.ts","../src/lib/transform/include.ts","../src/lib/transform/substitution.ts","../src/lib/schema/types.ts","../src/lib/schema/compile.ts","../src/lib/schema/collect.ts","../src/lib/schema/filtering.ts","../src/lib/schema/load.ts","../src/loader.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\n\nconst ENV_PREFIX = 'APP_CONFIG_';\n\n// Update the same pattern in config package if this is changed\nconst CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i;\n\n/**\n * Read runtime configuration from the environment.\n *\n * Only environment variables prefixed with APP_CONFIG_ will be considered.\n *\n * For each variable, the prefix will be removed, and rest of the key will\n * be split by '_'. Each part will then be used as keys to build up a nested\n * config object structure. The treatment of the entire environment variable\n * is case-sensitive.\n *\n * The value of the variable should be JSON serialized, as it will be parsed\n * and the type will be kept intact. For example \"true\" and true are treated\n * differently, as well as \"42\" and 42.\n *\n * For example, to set the config app.title to \"My Title\", use the following:\n *\n * APP_CONFIG_app_title='\"My Title\"'\n */\nexport function readEnvConfig(env: {\n [name: string]: string | undefined;\n}): AppConfig[] {\n let data: JsonObject | undefined = undefined;\n\n for (const [name, value] of Object.entries(env)) {\n if (!value) {\n continue;\n }\n if (name.startsWith(ENV_PREFIX)) {\n const key = name.replace(ENV_PREFIX, '');\n const keyParts = key.split('_');\n\n let obj = (data = data ?? {});\n for (const [index, part] of keyParts.entries()) {\n if (!CONFIG_KEY_PART_PATTERN.test(part)) {\n throw new TypeError(`Invalid env config key '${key}'`);\n }\n if (index < keyParts.length - 1) {\n obj = (obj[part] = obj[part] ?? {}) as JsonObject;\n if (typeof obj !== 'object' || Array.isArray(obj)) {\n const subKey = keyParts.slice(0, index + 1).join('_');\n throw new TypeError(\n `Could not nest config for key '${key}' under existing value '${subKey}'`,\n );\n }\n } else {\n if (part in obj) {\n throw new TypeError(\n `Refusing to override existing config at key '${key}'`,\n );\n }\n try {\n const [, parsedValue] = safeJsonParse(value);\n if (parsedValue === null) {\n throw new Error('value may not be null');\n }\n obj[part] = parsedValue;\n } catch (error) {\n throw new TypeError(\n `Failed to parse JSON-serialized config value for key '${key}', ${error}`,\n );\n }\n }\n }\n }\n }\n\n return data ? [{ data, context: 'env' }] : [];\n}\n\nfunction safeJsonParse(str: string): [Error | null, any] {\n try {\n return [null, JSON.parse(str)];\n } catch (err) {\n return [err, str];\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue, JsonObject } from '@backstage/config';\n\nexport function isObject(obj: JsonValue | undefined): obj is JsonObject {\n if (typeof obj !== 'object') {\n return false;\n } else if (Array.isArray(obj)) {\n return false;\n }\n return obj !== null;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { TransformFunc } from './types';\nimport { isObject } from './utils';\n\n/**\n * Applies a set of transforms to raw configuration data.\n */\nexport async function applyConfigTransforms(\n initialDir: string,\n input: JsonValue,\n transforms: TransformFunc[],\n): Promise<JsonObject> {\n async function transform(\n inputObj: JsonValue,\n path: string,\n baseDir: string,\n ): Promise<JsonValue | undefined> {\n let obj = inputObj;\n let dir = baseDir;\n\n for (const tf of transforms) {\n try {\n const result = await tf(inputObj, baseDir);\n if (result.applied) {\n if (result.value === undefined) {\n return undefined;\n }\n obj = result.value;\n dir = result.newBaseDir ?? dir;\n break;\n }\n } catch (error) {\n throw new Error(`error at ${path}, ${error.message}`);\n }\n }\n\n if (typeof obj !== 'object') {\n return obj;\n } else if (obj === null) {\n return undefined;\n } else if (Array.isArray(obj)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of obj.entries()) {\n const out = await transform(value, `${path}[${index}]`, dir);\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n return arr;\n }\n\n const out: JsonObject = {};\n\n for (const [key, value] of Object.entries(obj)) {\n // undefined covers optional fields\n if (value !== undefined) {\n const result = await transform(value, `${path}.${key}`, dir);\n if (result !== undefined) {\n out[key] = result;\n }\n }\n }\n\n return out;\n }\n\n const finalData = await transform(input, '', initialDir);\n if (!isObject(finalData)) {\n throw new TypeError('expected object at config root');\n }\n return finalData;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport yaml from 'yaml';\nimport { extname, dirname, resolve as resolvePath } from 'path';\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { isObject } from './utils';\nimport { TransformFunc, EnvFunc, ReadFileFunc } from './types';\n\n// Parsers for each type of included file\nconst includeFileParser: {\n [ext in string]: (content: string) => Promise<JsonObject>;\n} = {\n '.json': async content => JSON.parse(content),\n '.yaml': async content => yaml.parse(content),\n '.yml': async content => yaml.parse(content),\n};\n\n/**\n * Transforms a include description into the actual included value.\n */\nexport function createIncludeTransform(\n env: EnvFunc,\n readFile: ReadFileFunc,\n substitute: TransformFunc,\n): TransformFunc {\n return async (input: JsonValue, baseDir: string) => {\n if (!isObject(input)) {\n return { applied: false };\n }\n // Check if there's any key that starts with a '$', in that case we treat\n // this entire object as an include description.\n const [includeKey] = Object.keys(input).filter(key => key.startsWith('$'));\n if (includeKey) {\n if (Object.keys(input).length !== 1) {\n throw new Error(\n `include key ${includeKey} should not have adjacent keys`,\n );\n }\n } else {\n return { applied: false };\n }\n\n const rawIncludedValue = input[includeKey];\n if (typeof rawIncludedValue !== 'string') {\n throw new Error(`${includeKey} include value is not a string`);\n }\n\n const substituteResults = await substitute(rawIncludedValue, baseDir);\n const includeValue = substituteResults.applied\n ? substituteResults.value\n : rawIncludedValue;\n\n // The second string check is needed for Typescript to know this is a string.\n if (includeValue === undefined || typeof includeValue !== 'string') {\n throw new Error(`${includeKey} substitution value was undefined`);\n }\n\n switch (includeKey) {\n case '$file':\n try {\n const value = await readFile(resolvePath(baseDir, includeValue));\n return { applied: true, value };\n } catch (error) {\n throw new Error(`failed to read file ${includeValue}, ${error}`);\n }\n case '$env':\n try {\n return { applied: true, value: await env(includeValue) };\n } catch (error) {\n throw new Error(`failed to read env ${includeValue}, ${error}`);\n }\n\n case '$include': {\n const [filePath, dataPath] = includeValue.split(/#(.*)/);\n\n const ext = extname(filePath);\n const parser = includeFileParser[ext];\n if (!parser) {\n throw new Error(\n `no configuration parser available for included file ${filePath}`,\n );\n }\n\n const path = resolvePath(baseDir, filePath);\n const content = await readFile(path);\n const newBaseDir = dirname(path);\n\n const parts = dataPath ? dataPath.split('.') : [];\n\n let value: JsonValue | undefined;\n try {\n value = await parser(content);\n } catch (error) {\n throw new Error(\n `failed to parse included file ${filePath}, ${error}`,\n );\n }\n\n // This bit handles selecting a subtree in the included file, if a path was provided after a #\n for (const [index, part] of parts.entries()) {\n if (!isObject(value)) {\n const errPath = parts.slice(0, index).join('.');\n throw new Error(\n `value at '${errPath}' in included file ${filePath} is not an object`,\n );\n }\n value = value[part];\n }\n\n return {\n applied: true,\n value,\n newBaseDir: newBaseDir !== baseDir ? newBaseDir : undefined,\n };\n }\n\n default:\n throw new Error(`unknown include ${includeKey}`);\n }\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue } from '@backstage/config';\nimport { TransformFunc, EnvFunc } from './types';\n\n/**\n * A environment variable substitution transform that transforms e.g. 'token ${MY_TOKEN}'\n * to 'token abc' if MY_TOKEN is 'abc'. If any of the substituted variables are undefined,\n * the entire expression ends up undefined.\n */\nexport function createSubstitutionTransform(env: EnvFunc): TransformFunc {\n return async (input: JsonValue) => {\n if (typeof input !== 'string') {\n return { applied: false };\n }\n\n const parts: (string | undefined)[] = input.split(/(\\$?\\$\\{[^{}]*\\})/);\n for (let i = 1; i < parts.length; i += 2) {\n const part = parts[i]!;\n if (part.startsWith('$$')) {\n parts[i] = part.slice(1);\n } else {\n parts[i] = await env(part.slice(2, -1).trim());\n }\n }\n\n if (parts.some(part => part === undefined)) {\n return { applied: true, value: undefined };\n }\n return { applied: true, value: parts.join('') };\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\n\n/**\n * An sub-set of configuration schema.\n */\nexport type ConfigSchemaPackageEntry = {\n /**\n * The configuration schema itself.\n */\n value: JsonObject;\n /**\n * The relative path that the configuration schema was discovered at.\n */\n path: string;\n};\n\n/**\n * A list of all possible configuration value visibilities.\n */\nexport const CONFIG_VISIBILITIES = ['frontend', 'backend', 'secret'] as const;\n\n/**\n * A type representing the possible configuration value visibilities\n */\nexport type ConfigVisibility = typeof CONFIG_VISIBILITIES[number];\n\n/**\n * The default configuration visibility if no other values is given.\n */\nexport const DEFAULT_CONFIG_VISIBILITY: ConfigVisibility = 'backend';\n\n/**\n * An explanation of a configuration validation error.\n */\ntype ValidationError = string;\n\n/**\n * The result of validating configuration data using a schema.\n */\ntype ValidationResult = {\n /**\n * Errors that where emitted during validation, if any.\n */\n errors?: ValidationError[];\n /**\n * The configuration visibilities that were discovered during validation.\n *\n * The path in the key uses the form `/<key>/<sub-key>/<array-index>/<leaf-key>`\n */\n visibilityByPath: Map<string, ConfigVisibility>;\n};\n\n/**\n * A function used validate configuration data.\n */\nexport type ValidationFunc = (configs: AppConfig[]) => ValidationResult;\n\n/**\n * A function used to transform primitive configuration values.\n */\nexport type TransformFunc<T extends number | string | boolean> = (\n value: T,\n context: { visibility: ConfigVisibility },\n) => T | undefined;\n\n/**\n * Options used to process configuration data with a schema.\n */\ntype ConfigProcessingOptions = {\n /**\n * The visibilities that should be included in the output data.\n * If omitted, the data will not be filtered by visibility.\n */\n visibility?: ConfigVisibility[];\n\n /**\n * A transform function that can be used to transform primitive configuration values\n * during validation. The value returned from the transform function will be used\n * instead of the original value. If the transform returns `undefined`, the value\n * will be omitted.\n */\n valueTransform?: TransformFunc<any>;\n\n /**\n * Whether or not to include the `filteredKeys` property in the output `AppConfig`s.\n *\n * Default: `false`.\n */\n withFilteredKeys?: boolean;\n};\n\n/**\n * A loaded configuration schema that is ready to process configuration data.\n */\nexport type ConfigSchema = {\n process(\n appConfigs: AppConfig[],\n options?: ConfigProcessingOptions,\n ): AppConfig[];\n\n serialize(): JsonObject;\n};\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport Ajv from 'ajv';\nimport { JSONSchema7 as JSONSchema } from 'json-schema';\nimport mergeAllOf, { Resolvers } from 'json-schema-merge-allof';\nimport { ConfigReader } from '@backstage/config';\nimport {\n ConfigSchemaPackageEntry,\n ValidationFunc,\n CONFIG_VISIBILITIES,\n ConfigVisibility,\n} from './types';\n\n/**\n * This takes a collection of Backstage configuration schemas from various\n * sources and compiles them down into a single schema validation function.\n *\n * It also handles the implementation of the custom \"visibility\" keyword used\n * to specify the scope of different config paths.\n */\nexport function compileConfigSchemas(\n schemas: ConfigSchemaPackageEntry[],\n): ValidationFunc {\n // The ajv instance below is stateful and doesn't really allow for additional\n // output during validation. We work around this by having this extra piece\n // of state that we reset before each validation.\n const visibilityByPath = new Map<string, ConfigVisibility>();\n\n const ajv = new Ajv({\n allErrors: true,\n allowUnionTypes: true,\n schemas: {\n 'https://backstage.io/schema/config-v1': true,\n },\n }).addKeyword({\n keyword: 'visibility',\n metaSchema: {\n type: 'string',\n enum: CONFIG_VISIBILITIES,\n },\n compile(visibility: ConfigVisibility) {\n return (_data, context) => {\n if (context?.dataPath === undefined) {\n return false;\n }\n if (visibility && visibility !== 'backend') {\n const normalizedPath = context.dataPath.replace(\n /\\['?(.*?)'?\\]/g,\n (_, segment) => `/${segment}`,\n );\n visibilityByPath.set(normalizedPath, visibility);\n }\n return true;\n };\n },\n });\n\n for (const schema of schemas) {\n try {\n ajv.compile(schema.value);\n } catch (error) {\n throw new Error(`Schema at ${schema.path} is invalid, ${error}`);\n }\n }\n\n const merged = mergeConfigSchemas(schemas.map(_ => _.value));\n const validate = ajv.compile(merged);\n\n return configs => {\n const config = ConfigReader.fromConfigs(configs).get();\n\n visibilityByPath.clear();\n\n const valid = validate(config);\n if (!valid) {\n const errors = validate.errors ?? [];\n return {\n errors: errors.map(({ dataPath, message, params }) => {\n const paramStr = Object.entries(params)\n .map(([name, value]) => `${name}=${value}`)\n .join(' ');\n return `Config ${message || ''} { ${paramStr} } at ${dataPath}`;\n }),\n visibilityByPath: new Map(),\n };\n }\n\n return {\n visibilityByPath: new Map(visibilityByPath),\n };\n };\n}\n\n/**\n * Given a list of configuration schemas from packages, merge them\n * into a single json schema.\n */\nexport function mergeConfigSchemas(schemas: JSONSchema[]): JSONSchema {\n const merged = mergeAllOf(\n { allOf: schemas },\n {\n // JSONSchema is typically subtractive, as in it always reduces the set of allowed\n // inputs through constraints. This changes the object property merging to be additive\n // rather than subtractive.\n ignoreAdditionalProperties: true,\n resolvers: {\n // This ensures that the visibilities across different schemas are sound, and\n // selects the most specific visibility for each path.\n visibility(values: string[], path: string[]) {\n const hasFrontend = values.some(_ => _ === 'frontend');\n const hasSecret = values.some(_ => _ === 'secret');\n if (hasFrontend && hasSecret) {\n throw new Error(\n `Config schema visibility is both 'frontend' and 'secret' for ${path.join(\n '/',\n )}`,\n );\n } else if (hasFrontend) {\n return 'frontend';\n } else if (hasSecret) {\n return 'secret';\n }\n\n return 'backend';\n },\n } as Partial<Resolvers<JSONSchema>>,\n },\n );\n return merged;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport {\n resolve as resolvePath,\n relative as relativePath,\n dirname,\n sep,\n} from 'path';\nimport { ConfigSchemaPackageEntry } from './types';\nimport { getProgramFromFiles, generateSchema } from 'typescript-json-schema';\nimport { JsonObject } from '@backstage/config';\n\ntype Item = {\n name: string;\n parentPath?: string;\n};\n\nconst req =\n typeof __non_webpack_require__ === 'undefined'\n ? require\n : __non_webpack_require__;\n\n/**\n * This collects all known config schemas across all dependencies of the app.\n */\nexport async function collectConfigSchemas(\n packageNames: string[],\n): Promise<ConfigSchemaPackageEntry[]> {\n const visitedPackages = new Set<string>();\n const schemas = Array<ConfigSchemaPackageEntry>();\n const tsSchemaPaths = Array<string>();\n const currentDir = await fs.realpath(process.cwd());\n\n async function processItem({ name, parentPath }: Item) {\n // Ensures that we only process each package once. We don't bother with\n // loading in schemas from duplicates of different versions, as that's not\n // supported by Backstage right now anyway. We may want to change that in\n // the future though, if it for example becomes possible to load in two\n // different versions of e.g. @backstage/core at once.\n if (visitedPackages.has(name)) {\n return;\n }\n visitedPackages.add(name);\n\n let pkgPath: string;\n try {\n pkgPath = req.resolve(\n `${name}/package.json`,\n parentPath && {\n paths: [parentPath],\n },\n );\n } catch {\n // We can somewhat safely ignore packages that don't export package.json,\n // as they are likely not part of the Backstage ecosystem anyway.\n return;\n }\n\n const pkg = await fs.readJson(pkgPath);\n const depNames = [\n ...Object.keys(pkg.dependencies ?? {}),\n ...Object.keys(pkg.devDependencies ?? {}),\n ...Object.keys(pkg.optionalDependencies ?? {}),\n ...Object.keys(pkg.peerDependencies ?? {}),\n ];\n\n // TODO(Rugvip): Trying this out to avoid having to traverse the full dependency graph,\n // since that's pretty slow. We probably need a better way to determine when\n // we've left the Backstage ecosystem, but this will do for now.\n const hasSchema = 'configSchema' in pkg;\n const hasBackstageDep = depNames.some(_ => _.startsWith('@backstage/'));\n if (!hasSchema && !hasBackstageDep) {\n return;\n }\n if (hasSchema) {\n if (typeof pkg.configSchema === 'string') {\n const isJson = pkg.configSchema.endsWith('.json');\n const isDts = pkg.configSchema.endsWith('.d.ts');\n if (!isJson && !isDts) {\n throw new Error(\n `Config schema files must be .json or .d.ts, got ${pkg.configSchema}`,\n );\n }\n if (isDts) {\n tsSchemaPaths.push(\n relativePath(\n currentDir,\n resolvePath(dirname(pkgPath), pkg.configSchema),\n ),\n );\n } else {\n const path = resolvePath(dirname(pkgPath), pkg.configSchema);\n const value = await fs.readJson(path);\n schemas.push({\n value,\n path: relativePath(currentDir, path),\n });\n }\n } else {\n schemas.push({\n value: pkg.configSchema,\n path: relativePath(currentDir, pkgPath),\n });\n }\n }\n\n await Promise.all(\n depNames.map(depName =>\n processItem({ name: depName, parentPath: pkgPath }),\n ),\n );\n }\n\n await Promise.all(\n packageNames.map(name => processItem({ name, parentPath: currentDir })),\n );\n\n const tsSchemas = compileTsSchemas(tsSchemaPaths);\n\n return schemas.concat(tsSchemas);\n}\n\n// This handles the support of TypeScript .d.ts config schema declarations.\n// We collect all typescript schema definition and compile them all in one go.\n// This is much faster than compiling them separately.\nfunction compileTsSchemas(paths: string[]) {\n if (paths.length === 0) {\n return [];\n }\n\n const program = getProgramFromFiles(paths, {\n incremental: false,\n isolatedModules: true,\n lib: ['ES5'], // Skipping most libs speeds processing up a lot, we just need the primitive types anyway\n noEmit: true,\n noResolve: true,\n skipLibCheck: true, // Skipping lib checks speeds things up\n skipDefaultLibCheck: true,\n strict: true,\n typeRoots: [], // Do not include any additional types\n types: [],\n });\n\n const tsSchemas = paths.map(path => {\n let value;\n try {\n value = generateSchema(\n program,\n // All schemas should export a `Config` symbol\n 'Config',\n // This enables usage of @visibility is doc comments\n {\n required: true,\n validationKeywords: ['visibility'],\n },\n [path.split(sep).join('/')], // Unix paths are expected for all OSes here\n ) as JsonObject | null;\n } catch (error) {\n if (error.message !== 'type Config not found') {\n throw error;\n }\n }\n\n if (!value) {\n throw new Error(`Invalid schema in ${path}, missing Config export`);\n }\n return { path, value };\n });\n\n return tsSchemas;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport {\n ConfigVisibility,\n DEFAULT_CONFIG_VISIBILITY,\n TransformFunc,\n} from './types';\n\n/**\n * This filters data by visibility by discovering the visibility of each\n * value, and then only keeping the ones that are specified in `includeVisibilities`.\n */\nexport function filterByVisibility(\n data: JsonObject,\n includeVisibilities: ConfigVisibility[],\n visibilityByPath: Map<string, ConfigVisibility>,\n transformFunc?: TransformFunc<number | string | boolean>,\n withFilteredKeys?: boolean,\n): { data: JsonObject; filteredKeys?: string[] } {\n const filteredKeys = new Array<string>();\n\n function transform(\n jsonVal: JsonValue,\n visibilityPath: string, // Matches the format we get from ajv\n filterPath: string, // Matches the format of the ConfigReader\n ): JsonValue | undefined {\n const visibility =\n visibilityByPath.get(visibilityPath) ?? DEFAULT_CONFIG_VISIBILITY;\n const isVisible = includeVisibilities.includes(visibility);\n\n if (typeof jsonVal !== 'object') {\n if (isVisible) {\n if (transformFunc) {\n return transformFunc(jsonVal, { visibility });\n }\n return jsonVal;\n }\n if (withFilteredKeys) {\n filteredKeys.push(filterPath);\n }\n return undefined;\n } else if (jsonVal === null) {\n return undefined;\n } else if (Array.isArray(jsonVal)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of jsonVal.entries()) {\n const out = transform(\n value,\n `${visibilityPath}/${index}`,\n `${filterPath}[${index}]`,\n );\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n if (arr.length > 0 || isVisible) {\n return arr;\n }\n return undefined;\n }\n\n const outObj: JsonObject = {};\n let hasOutput = false;\n\n for (const [key, value] of Object.entries(jsonVal)) {\n if (value === undefined) {\n continue;\n }\n const out = transform(\n value,\n `${visibilityPath}/${key}`,\n filterPath ? `${filterPath}.${key}` : key,\n );\n if (out !== undefined) {\n outObj[key] = out;\n hasOutput = true;\n }\n }\n\n if (hasOutput || isVisible) {\n return outObj;\n }\n return undefined;\n }\n\n return {\n filteredKeys: withFilteredKeys ? filteredKeys : undefined,\n data: (transform(data, '', '') as JsonObject) ?? {},\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\nimport { compileConfigSchemas } from './compile';\nimport { collectConfigSchemas } from './collect';\nimport { filterByVisibility } from './filtering';\nimport {\n ConfigSchema,\n ConfigSchemaPackageEntry,\n CONFIG_VISIBILITIES,\n} from './types';\n\ntype Options =\n | {\n dependencies: string[];\n }\n | {\n serialized: JsonObject;\n };\n\n/**\n * Loads config schema for a Backstage instance.\n */\nexport async function loadConfigSchema(\n options: Options,\n): Promise<ConfigSchema> {\n let schemas: ConfigSchemaPackageEntry[];\n\n if ('dependencies' in options) {\n schemas = await collectConfigSchemas(options.dependencies);\n } else {\n const { serialized } = options;\n if (serialized?.backstageConfigSchemaVersion !== 1) {\n throw new Error(\n 'Serialized configuration schema is invalid or has an invalid version number',\n );\n }\n schemas = serialized.schemas as ConfigSchemaPackageEntry[];\n }\n\n const validate = compileConfigSchemas(schemas);\n\n return {\n process(\n configs: AppConfig[],\n { visibility, valueTransform, withFilteredKeys } = {},\n ): AppConfig[] {\n const result = validate(configs);\n if (result.errors) {\n const error = new Error(\n `Config validation failed, ${result.errors.join('; ')}`,\n );\n (error as any).messages = result.errors;\n throw error;\n }\n\n let processedConfigs = configs;\n\n if (visibility) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n visibility,\n result.visibilityByPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n } else if (valueTransform) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n Array.from(CONFIG_VISIBILITIES),\n result.visibilityByPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n }\n\n return processedConfigs;\n },\n serialize(): JsonObject {\n return {\n schemas,\n backstageConfigSchemaVersion: 1,\n };\n },\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport yaml from 'yaml';\nimport chokidar from 'chokidar';\nimport { resolve as resolvePath, dirname, isAbsolute, basename } from 'path';\nimport { AppConfig } from '@backstage/config';\nimport {\n applyConfigTransforms,\n readEnvConfig,\n createIncludeTransform,\n createSubstitutionTransform,\n} from './lib';\nimport { EnvFunc } from './lib/transform/types';\n\nexport type LoadConfigOptions = {\n // The root directory of the config loading context. Used to find default configs.\n configRoot: string;\n\n // Absolute paths to load config files from. Configs from earlier paths have lower priority.\n configPaths: string[];\n\n /** @deprecated This option has been removed */\n env?: string;\n\n /**\n * Custom environment variable loading function\n *\n * @experimental This API is not stable and may change at any point\n */\n experimentalEnvFunc?: EnvFunc;\n\n /**\n * An optional configuration that enables watching of config files.\n */\n watch?: {\n /**\n * A listener that is called when a config file is changed.\n */\n onChange: (configs: AppConfig[]) => void;\n\n /**\n * An optional signal that stops the watcher once the promise resolves.\n */\n stopSignal?: Promise<void>;\n };\n};\n\nexport async function loadConfig(\n options: LoadConfigOptions,\n): Promise<AppConfig[]> {\n const { configRoot, experimentalEnvFunc: envFunc, watch } = options;\n const configPaths = options.configPaths.slice();\n\n // If no paths are provided, we default to reading\n // `app-config.yaml` and, if it exists, `app-config.local.yaml`\n if (configPaths.length === 0) {\n configPaths.push(resolvePath(configRoot, 'app-config.yaml'));\n\n const localConfig = resolvePath(configRoot, 'app-config.local.yaml');\n if (await fs.pathExists(localConfig)) {\n configPaths.push(localConfig);\n }\n }\n\n const env = envFunc ?? (async (name: string) => process.env[name]);\n\n const loadConfigFiles = async () => {\n const configs = [];\n\n for (const configPath of configPaths) {\n if (!isAbsolute(configPath)) {\n throw new Error(`Config load path is not absolute: '${configPath}'`);\n }\n\n const dir = dirname(configPath);\n const readFile = (path: string) =>\n fs.readFile(resolvePath(dir, path), 'utf8');\n\n const input = yaml.parse(await readFile(configPath));\n const substitutionTransform = createSubstitutionTransform(env);\n const data = await applyConfigTransforms(dir, input, [\n createIncludeTransform(env, readFile, substitutionTransform),\n substitutionTransform,\n ]);\n\n configs.push({ data, context: basename(configPath) });\n }\n\n return configs;\n };\n\n let fileConfigs;\n try {\n fileConfigs = await loadConfigFiles();\n } catch (error) {\n throw new Error(\n `Failed to read static configuration file, ${error.message}`,\n );\n }\n\n const envConfigs = await readEnvConfig(process.env);\n\n // Set up config file watching if requested by the caller\n if (watch) {\n let currentSerializedConfig = JSON.stringify(fileConfigs);\n\n const watcher = chokidar.watch(configPaths, {\n usePolling: process.env.NODE_ENV === 'test',\n });\n watcher.on('change', async () => {\n try {\n const newConfigs = await loadConfigFiles();\n const newSerializedConfig = JSON.stringify(newConfigs);\n\n if (currentSerializedConfig === newSerializedConfig) {\n return;\n }\n currentSerializedConfig = newSerializedConfig;\n\n watch.onChange([...newConfigs, ...envConfigs]);\n } catch (error) {\n console.error(`Failed to reload configuration files, ${error}`);\n }\n });\n\n if (watch.stopSignal) {\n watch.stopSignal.then(() => {\n watcher.close();\n });\n }\n }\n\n return [...fileConfigs, ...envConfigs];\n}\n"],"names":["resolvePath","relativePath"],"mappings":";;;;;;;;;AAkBA,MAAM,aAAa;AAGnB,MAAM,0BAA0B;uBAoBF,KAEd;AA3ChB;AA4CE,MAAI,OAA+B;AAEnC,aAAW,CAAC,MAAM,UAAU,OAAO,QAAQ,MAAM;AAC/C,QAAI,CAAC,OAAO;AACV;AAAA;AAEF,QAAI,KAAK,WAAW,aAAa;AAC/B,YAAM,MAAM,KAAK,QAAQ,YAAY;AACrC,YAAM,WAAW,IAAI,MAAM;AAE3B,UAAI,MAAO,OAAO,sBAAQ;AAC1B,iBAAW,CAAC,OAAO,SAAS,SAAS,WAAW;AAC9C,YAAI,CAAC,wBAAw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OAAO;AACT,QAAI,0BAA0B,KAAK,UAAU;AAE7C,UAAM,UAAU,SAAS,MAAM,aAAa;AAAA,MAC1C,YAAY,QAAQ,IAAI,aAAa;AAAA;AAEvC,YAAQ,GAAG,UAAU,YAAY;AAC/B,UAAI;AACF,cAAM,aAAa,MAAM;AACzB,cAAM,sBAAsB,KAAK,UAAU;AAE3C,YAAI,4BAA4B,qBAAqB;AACnD;AAAA;AAEF,kCAA0B;AAE1B,cAAM,SAAS,CAAC,GAAG,YAAY,GAAG;AAAA,eAC3B,OAAP;AACA,gBAAQ,MAAM,yCAAyC;AAAA;AAAA;AAI3D,QAAI,MAAM,YAAY;AACpB,YAAM,WAAW,KAAK,MAAM;AAC1B,gBAAQ;AAAA;AAAA;AAAA;AAKd,SAAO,CAAC,GAAG,aAAa,GAAG;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/lib/env.ts","../src/lib/transform/utils.ts","../src/lib/transform/apply.ts","../src/lib/transform/include.ts","../src/lib/transform/substitution.ts","../src/lib/schema/types.ts","../src/lib/schema/compile.ts","../src/lib/schema/collect.ts","../src/lib/schema/filtering.ts","../src/lib/schema/load.ts","../src/loader.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\nimport { assertError } from '@backstage/errors';\n\nconst ENV_PREFIX = 'APP_CONFIG_';\n\n// Update the same pattern in config package if this is changed\nconst CONFIG_KEY_PART_PATTERN = /^[a-z][a-z0-9]*(?:[-_][a-z][a-z0-9]*)*$/i;\n\n/**\n * Read runtime configuration from the environment.\n *\n * Only environment variables prefixed with APP_CONFIG_ will be considered.\n *\n * For each variable, the prefix will be removed, and rest of the key will\n * be split by '_'. Each part will then be used as keys to build up a nested\n * config object structure. The treatment of the entire environment variable\n * is case-sensitive.\n *\n * The value of the variable should be JSON serialized, as it will be parsed\n * and the type will be kept intact. For example \"true\" and true are treated\n * differently, as well as \"42\" and 42.\n *\n * For example, to set the config app.title to \"My Title\", use the following:\n *\n * APP_CONFIG_app_title='\"My Title\"'\n *\n * @public\n */\nexport function readEnvConfig(env: {\n [name: string]: string | undefined;\n}): AppConfig[] {\n let data: JsonObject | undefined = undefined;\n\n for (const [name, value] of Object.entries(env)) {\n if (!value) {\n continue;\n }\n if (name.startsWith(ENV_PREFIX)) {\n const key = name.replace(ENV_PREFIX, '');\n const keyParts = key.split('_');\n\n let obj = (data = data ?? {});\n for (const [index, part] of keyParts.entries()) {\n if (!CONFIG_KEY_PART_PATTERN.test(part)) {\n throw new TypeError(`Invalid env config key '${key}'`);\n }\n if (index < keyParts.length - 1) {\n obj = (obj[part] = obj[part] ?? {}) as JsonObject;\n if (typeof obj !== 'object' || Array.isArray(obj)) {\n const subKey = keyParts.slice(0, index + 1).join('_');\n throw new TypeError(\n `Could not nest config for key '${key}' under existing value '${subKey}'`,\n );\n }\n } else {\n if (part in obj) {\n throw new TypeError(\n `Refusing to override existing config at key '${key}'`,\n );\n }\n try {\n const [, parsedValue] = safeJsonParse(value);\n if (parsedValue === null) {\n throw new Error('value may not be null');\n }\n obj[part] = parsedValue;\n } catch (error) {\n throw new TypeError(\n `Failed to parse JSON-serialized config value for key '${key}', ${error}`,\n );\n }\n }\n }\n }\n }\n\n return data ? [{ data, context: 'env' }] : [];\n}\n\nfunction safeJsonParse(str: string): [Error | null, any] {\n try {\n return [null, JSON.parse(str)];\n } catch (err) {\n assertError(err);\n return [err, str];\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue, JsonObject } from '@backstage/config';\n\nexport function isObject(obj: JsonValue | undefined): obj is JsonObject {\n if (typeof obj !== 'object') {\n return false;\n } else if (Array.isArray(obj)) {\n return false;\n }\n return obj !== null;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { assertError } from '@backstage/errors';\nimport { TransformFunc } from './types';\nimport { isObject } from './utils';\n\n/**\n * Applies a set of transforms to raw configuration data.\n */\nexport async function applyConfigTransforms(\n initialDir: string,\n input: JsonValue,\n transforms: TransformFunc[],\n): Promise<JsonObject> {\n async function transform(\n inputObj: JsonValue,\n path: string,\n baseDir: string,\n ): Promise<JsonValue | undefined> {\n let obj = inputObj;\n let dir = baseDir;\n\n for (const tf of transforms) {\n try {\n const result = await tf(inputObj, baseDir);\n if (result.applied) {\n if (result.value === undefined) {\n return undefined;\n }\n obj = result.value;\n dir = result.newBaseDir ?? dir;\n break;\n }\n } catch (error) {\n assertError(error);\n throw new Error(`error at ${path}, ${error.message}`);\n }\n }\n\n if (typeof obj !== 'object') {\n return obj;\n } else if (obj === null) {\n return undefined;\n } else if (Array.isArray(obj)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of obj.entries()) {\n const out = await transform(value, `${path}[${index}]`, dir);\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n return arr;\n }\n\n const out: JsonObject = {};\n\n for (const [key, value] of Object.entries(obj)) {\n // undefined covers optional fields\n if (value !== undefined) {\n const result = await transform(value, `${path}.${key}`, dir);\n if (result !== undefined) {\n out[key] = result;\n }\n }\n }\n\n return out;\n }\n\n const finalData = await transform(input, '', initialDir);\n if (!isObject(finalData)) {\n throw new TypeError('expected object at config root');\n }\n return finalData;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport yaml from 'yaml';\nimport { extname, dirname, resolve as resolvePath } from 'path';\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport { isObject } from './utils';\nimport { TransformFunc, EnvFunc, ReadFileFunc } from './types';\n\n// Parsers for each type of included file\nconst includeFileParser: {\n [ext in string]: (content: string) => Promise<JsonObject>;\n} = {\n '.json': async content => JSON.parse(content),\n '.yaml': async content => yaml.parse(content),\n '.yml': async content => yaml.parse(content),\n};\n\n/**\n * Transforms a include description into the actual included value.\n */\nexport function createIncludeTransform(\n env: EnvFunc,\n readFile: ReadFileFunc,\n substitute: TransformFunc,\n): TransformFunc {\n return async (input: JsonValue, baseDir: string) => {\n if (!isObject(input)) {\n return { applied: false };\n }\n // Check if there's any key that starts with a '$', in that case we treat\n // this entire object as an include description.\n const [includeKey] = Object.keys(input).filter(key => key.startsWith('$'));\n if (includeKey) {\n if (Object.keys(input).length !== 1) {\n throw new Error(\n `include key ${includeKey} should not have adjacent keys`,\n );\n }\n } else {\n return { applied: false };\n }\n\n const rawIncludedValue = input[includeKey];\n if (typeof rawIncludedValue !== 'string') {\n throw new Error(`${includeKey} include value is not a string`);\n }\n\n const substituteResults = await substitute(rawIncludedValue, baseDir);\n const includeValue = substituteResults.applied\n ? substituteResults.value\n : rawIncludedValue;\n\n // The second string check is needed for Typescript to know this is a string.\n if (includeValue === undefined || typeof includeValue !== 'string') {\n throw new Error(`${includeKey} substitution value was undefined`);\n }\n\n switch (includeKey) {\n case '$file':\n try {\n const value = await readFile(resolvePath(baseDir, includeValue));\n return { applied: true, value };\n } catch (error) {\n throw new Error(`failed to read file ${includeValue}, ${error}`);\n }\n case '$env':\n try {\n return { applied: true, value: await env(includeValue) };\n } catch (error) {\n throw new Error(`failed to read env ${includeValue}, ${error}`);\n }\n\n case '$include': {\n const [filePath, dataPath] = includeValue.split(/#(.*)/);\n\n const ext = extname(filePath);\n const parser = includeFileParser[ext];\n if (!parser) {\n throw new Error(\n `no configuration parser available for included file ${filePath}`,\n );\n }\n\n const path = resolvePath(baseDir, filePath);\n const content = await readFile(path);\n const newBaseDir = dirname(path);\n\n const parts = dataPath ? dataPath.split('.') : [];\n\n let value: JsonValue | undefined;\n try {\n value = await parser(content);\n } catch (error) {\n throw new Error(\n `failed to parse included file ${filePath}, ${error}`,\n );\n }\n\n // This bit handles selecting a subtree in the included file, if a path was provided after a #\n for (const [index, part] of parts.entries()) {\n if (!isObject(value)) {\n const errPath = parts.slice(0, index).join('.');\n throw new Error(\n `value at '${errPath}' in included file ${filePath} is not an object`,\n );\n }\n value = value[part];\n }\n\n return {\n applied: true,\n value,\n newBaseDir: newBaseDir !== baseDir ? newBaseDir : undefined,\n };\n }\n\n default:\n throw new Error(`unknown include ${includeKey}`);\n }\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonValue } from '@backstage/config';\nimport { TransformFunc, EnvFunc } from './types';\n\n/**\n * A environment variable substitution transform that transforms e.g. 'token ${MY_TOKEN}'\n * to 'token abc' if MY_TOKEN is 'abc'. If any of the substituted variables are undefined,\n * the entire expression ends up undefined.\n */\nexport function createSubstitutionTransform(env: EnvFunc): TransformFunc {\n return async (input: JsonValue) => {\n if (typeof input !== 'string') {\n return { applied: false };\n }\n\n const parts: (string | undefined)[] = input.split(/(\\$?\\$\\{[^{}]*\\})/);\n for (let i = 1; i < parts.length; i += 2) {\n const part = parts[i]!;\n if (part.startsWith('$$')) {\n parts[i] = part.slice(1);\n } else {\n parts[i] = await env(part.slice(2, -1).trim());\n }\n }\n\n if (parts.some(part => part === undefined)) {\n return { applied: true, value: undefined };\n }\n return { applied: true, value: parts.join('') };\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\n\n/**\n * An sub-set of configuration schema.\n */\nexport type ConfigSchemaPackageEntry = {\n /**\n * The configuration schema itself.\n */\n value: JsonObject;\n /**\n * The relative path that the configuration schema was discovered at.\n */\n path: string;\n};\n\n/**\n * A list of all possible configuration value visibilities.\n */\nexport const CONFIG_VISIBILITIES = ['frontend', 'backend', 'secret'] as const;\n\n/**\n * A type representing the possible configuration value visibilities\n *\n * @public\n */\nexport type ConfigVisibility = 'frontend' | 'backend' | 'secret';\n\n/**\n * The default configuration visibility if no other values is given.\n */\nexport const DEFAULT_CONFIG_VISIBILITY: ConfigVisibility = 'backend';\n\n/**\n * An explanation of a configuration validation error.\n */\nexport type ValidationError = {\n keyword: string;\n dataPath: string;\n schemaPath: string;\n params: Record<string, any>;\n propertyName?: string;\n message?: string;\n};\n\n/**\n * The result of validating configuration data using a schema.\n */\ntype ValidationResult = {\n /**\n * Errors that where emitted during validation, if any.\n */\n errors?: ValidationError[];\n /**\n * The configuration visibilities that were discovered during validation.\n *\n * The path in the key uses the form `/<key>/<sub-key>/<array-index>/<leaf-key>`\n */\n visibilityByDataPath: Map<string, ConfigVisibility>;\n\n /**\n * The configuration visibilities that were discovered during validation.\n *\n * The path in the key uses the form `/properties/<key>/items/additionalProperties/<leaf-key>`\n */\n visibilityBySchemaPath: Map<string, ConfigVisibility>;\n};\n\n/**\n * A function used validate configuration data.\n */\nexport type ValidationFunc = (configs: AppConfig[]) => ValidationResult;\n\n/**\n * A function used to transform primitive configuration values.\n *\n * @public\n */\nexport type TransformFunc<T extends number | string | boolean> = (\n value: T,\n context: { visibility: ConfigVisibility },\n) => T | undefined;\n\n/**\n * Options used to process configuration data with a schema.\n *\n * @public\n */\nexport type ConfigSchemaProcessingOptions = {\n /**\n * The visibilities that should be included in the output data.\n * If omitted, the data will not be filtered by visibility.\n */\n visibility?: ConfigVisibility[];\n\n /**\n * A transform function that can be used to transform primitive configuration values\n * during validation. The value returned from the transform function will be used\n * instead of the original value. If the transform returns `undefined`, the value\n * will be omitted.\n */\n valueTransform?: TransformFunc<any>;\n\n /**\n * Whether or not to include the `filteredKeys` property in the output `AppConfig`s.\n *\n * Default: `false`.\n */\n withFilteredKeys?: boolean;\n};\n\n/**\n * A loaded configuration schema that is ready to process configuration data.\n *\n * @public\n */\nexport type ConfigSchema = {\n process(\n appConfigs: AppConfig[],\n options?: ConfigSchemaProcessingOptions,\n ): AppConfig[];\n\n serialize(): JsonObject;\n};\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport Ajv from 'ajv';\nimport { JSONSchema7 as JSONSchema } from 'json-schema';\nimport mergeAllOf, { Resolvers } from 'json-schema-merge-allof';\nimport traverse from 'json-schema-traverse';\nimport { ConfigReader } from '@backstage/config';\nimport {\n ConfigSchemaPackageEntry,\n ValidationFunc,\n CONFIG_VISIBILITIES,\n ConfigVisibility,\n} from './types';\n\n/**\n * This takes a collection of Backstage configuration schemas from various\n * sources and compiles them down into a single schema validation function.\n *\n * It also handles the implementation of the custom \"visibility\" keyword used\n * to specify the scope of different config paths.\n */\nexport function compileConfigSchemas(\n schemas: ConfigSchemaPackageEntry[],\n): ValidationFunc {\n // The ajv instance below is stateful and doesn't really allow for additional\n // output during validation. We work around this by having this extra piece\n // of state that we reset before each validation.\n const visibilityByDataPath = new Map<string, ConfigVisibility>();\n\n const ajv = new Ajv({\n allErrors: true,\n allowUnionTypes: true,\n schemas: {\n 'https://backstage.io/schema/config-v1': true,\n },\n }).addKeyword({\n keyword: 'visibility',\n metaSchema: {\n type: 'string',\n enum: CONFIG_VISIBILITIES,\n },\n compile(visibility: ConfigVisibility) {\n return (_data, context) => {\n if (context?.dataPath === undefined) {\n return false;\n }\n if (visibility && visibility !== 'backend') {\n const normalizedPath = context.dataPath.replace(\n /\\['?(.*?)'?\\]/g,\n (_, segment) => `/${segment}`,\n );\n visibilityByDataPath.set(normalizedPath, visibility);\n }\n return true;\n };\n },\n });\n\n for (const schema of schemas) {\n try {\n ajv.compile(schema.value);\n } catch (error) {\n throw new Error(`Schema at ${schema.path} is invalid, ${error}`);\n }\n }\n\n const merged = mergeConfigSchemas(schemas.map(_ => _.value));\n const validate = ajv.compile(merged);\n\n const visibilityBySchemaPath = new Map<string, ConfigVisibility>();\n traverse(merged, (schema, path) => {\n if (schema.visibility && schema.visibility !== 'backend') {\n visibilityBySchemaPath.set(path, schema.visibility);\n }\n });\n\n return configs => {\n const config = ConfigReader.fromConfigs(configs).get();\n\n visibilityByDataPath.clear();\n\n const valid = validate(config);\n if (!valid) {\n return {\n errors: validate.errors ?? [],\n visibilityByDataPath: new Map(visibilityByDataPath),\n visibilityBySchemaPath,\n };\n }\n\n return {\n visibilityByDataPath: new Map(visibilityByDataPath),\n visibilityBySchemaPath,\n };\n };\n}\n\n/**\n * Given a list of configuration schemas from packages, merge them\n * into a single json schema.\n *\n * @public\n */\nexport function mergeConfigSchemas(schemas: JSONSchema[]): JSONSchema {\n const merged = mergeAllOf(\n { allOf: schemas },\n {\n // JSONSchema is typically subtractive, as in it always reduces the set of allowed\n // inputs through constraints. This changes the object property merging to be additive\n // rather than subtractive.\n ignoreAdditionalProperties: true,\n resolvers: {\n // This ensures that the visibilities across different schemas are sound, and\n // selects the most specific visibility for each path.\n visibility(values: string[], path: string[]) {\n const hasFrontend = values.some(_ => _ === 'frontend');\n const hasSecret = values.some(_ => _ === 'secret');\n if (hasFrontend && hasSecret) {\n throw new Error(\n `Config schema visibility is both 'frontend' and 'secret' for ${path.join(\n '/',\n )}`,\n );\n } else if (hasFrontend) {\n return 'frontend';\n } else if (hasSecret) {\n return 'secret';\n }\n\n return 'backend';\n },\n } as Partial<Resolvers<JSONSchema>>,\n },\n );\n return merged;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport {\n resolve as resolvePath,\n relative as relativePath,\n dirname,\n sep,\n} from 'path';\nimport { ConfigSchemaPackageEntry } from './types';\nimport { getProgramFromFiles, generateSchema } from 'typescript-json-schema';\nimport { JsonObject } from '@backstage/config';\nimport { assertError } from '@backstage/errors';\n\ntype Item = {\n name?: string;\n parentPath?: string;\n packagePath?: string;\n};\n\nconst req =\n typeof __non_webpack_require__ === 'undefined'\n ? require\n : __non_webpack_require__;\n\n/**\n * This collects all known config schemas across all dependencies of the app.\n */\nexport async function collectConfigSchemas(\n packageNames: string[],\n packagePaths: string[],\n): Promise<ConfigSchemaPackageEntry[]> {\n const schemas = new Array<ConfigSchemaPackageEntry>();\n const tsSchemaPaths = new Array<string>();\n const visitedPackageVersions = new Map<string, Set<string>>(); // pkgName: [versions...]\n\n const currentDir = await fs.realpath(process.cwd());\n\n async function processItem(item: Item) {\n let pkgPath = item.packagePath;\n\n if (pkgPath) {\n const pkgExists = await fs.pathExists(pkgPath);\n if (!pkgExists) {\n return;\n }\n } else if (item.name) {\n const { name, parentPath } = item;\n\n try {\n pkgPath = req.resolve(\n `${name}/package.json`,\n parentPath && {\n paths: [parentPath],\n },\n );\n } catch {\n // We can somewhat safely ignore packages that don't export package.json,\n // as they are likely not part of the Backstage ecosystem anyway.\n }\n }\n if (!pkgPath) {\n return;\n }\n\n const pkg = await fs.readJson(pkgPath);\n\n // Ensures that we only process the same version of each package once.\n let versions = visitedPackageVersions.get(pkg.name);\n if (versions?.has(pkg.version)) {\n return;\n }\n if (!versions) {\n versions = new Set();\n visitedPackageVersions.set(pkg.name, versions);\n }\n versions.add(pkg.version);\n\n const depNames = [\n ...Object.keys(pkg.dependencies ?? {}),\n ...Object.keys(pkg.devDependencies ?? {}),\n ...Object.keys(pkg.optionalDependencies ?? {}),\n ...Object.keys(pkg.peerDependencies ?? {}),\n ];\n\n // TODO(Rugvip): Trying this out to avoid having to traverse the full dependency graph,\n // since that's pretty slow. We probably need a better way to determine when\n // we've left the Backstage ecosystem, but this will do for now.\n const hasSchema = 'configSchema' in pkg;\n const hasBackstageDep = depNames.some(_ => _.startsWith('@backstage/'));\n if (!hasSchema && !hasBackstageDep) {\n return;\n }\n if (hasSchema) {\n if (typeof pkg.configSchema === 'string') {\n const isJson = pkg.configSchema.endsWith('.json');\n const isDts = pkg.configSchema.endsWith('.d.ts');\n if (!isJson && !isDts) {\n throw new Error(\n `Config schema files must be .json or .d.ts, got ${pkg.configSchema}`,\n );\n }\n if (isDts) {\n tsSchemaPaths.push(\n relativePath(\n currentDir,\n resolvePath(dirname(pkgPath), pkg.configSchema),\n ),\n );\n } else {\n const path = resolvePath(dirname(pkgPath), pkg.configSchema);\n const value = await fs.readJson(path);\n schemas.push({\n value,\n path: relativePath(currentDir, path),\n });\n }\n } else {\n schemas.push({\n value: pkg.configSchema,\n path: relativePath(currentDir, pkgPath),\n });\n }\n }\n\n await Promise.all(\n depNames.map(depName =>\n processItem({ name: depName, parentPath: pkgPath }),\n ),\n );\n }\n\n await Promise.all([\n ...packageNames.map(name => processItem({ name, parentPath: currentDir })),\n ...packagePaths.map(path => processItem({ name: path, packagePath: path })),\n ]);\n\n const tsSchemas = compileTsSchemas(tsSchemaPaths);\n\n return schemas.concat(tsSchemas);\n}\n\n// This handles the support of TypeScript .d.ts config schema declarations.\n// We collect all typescript schema definition and compile them all in one go.\n// This is much faster than compiling them separately.\nfunction compileTsSchemas(paths: string[]) {\n if (paths.length === 0) {\n return [];\n }\n\n const program = getProgramFromFiles(paths, {\n incremental: false,\n isolatedModules: true,\n lib: ['ES5'], // Skipping most libs speeds processing up a lot, we just need the primitive types anyway\n noEmit: true,\n noResolve: true,\n skipLibCheck: true, // Skipping lib checks speeds things up\n skipDefaultLibCheck: true,\n strict: true,\n typeRoots: [], // Do not include any additional types\n types: [],\n });\n\n const tsSchemas = paths.map(path => {\n let value;\n try {\n value = generateSchema(\n program,\n // All schemas should export a `Config` symbol\n 'Config',\n // This enables usage of @visibility is doc comments\n {\n required: true,\n validationKeywords: ['visibility'],\n },\n [path.split(sep).join('/')], // Unix paths are expected for all OSes here\n ) as JsonObject | null;\n } catch (error) {\n assertError(error);\n if (error.message !== 'type Config not found') {\n throw error;\n }\n }\n\n if (!value) {\n throw new Error(`Invalid schema in ${path}, missing Config export`);\n }\n return { path, value };\n });\n\n return tsSchemas;\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject, JsonValue } from '@backstage/config';\nimport {\n ConfigVisibility,\n DEFAULT_CONFIG_VISIBILITY,\n TransformFunc,\n ValidationError,\n} from './types';\n\n/**\n * This filters data by visibility by discovering the visibility of each\n * value, and then only keeping the ones that are specified in `includeVisibilities`.\n */\nexport function filterByVisibility(\n data: JsonObject,\n includeVisibilities: ConfigVisibility[],\n visibilityByDataPath: Map<string, ConfigVisibility>,\n transformFunc?: TransformFunc<number | string | boolean>,\n withFilteredKeys?: boolean,\n): { data: JsonObject; filteredKeys?: string[] } {\n const filteredKeys = new Array<string>();\n\n function transform(\n jsonVal: JsonValue,\n visibilityPath: string, // Matches the format we get from ajv\n filterPath: string, // Matches the format of the ConfigReader\n ): JsonValue | undefined {\n const visibility =\n visibilityByDataPath.get(visibilityPath) ?? DEFAULT_CONFIG_VISIBILITY;\n const isVisible = includeVisibilities.includes(visibility);\n\n if (typeof jsonVal !== 'object') {\n if (isVisible) {\n if (transformFunc) {\n return transformFunc(jsonVal, { visibility });\n }\n return jsonVal;\n }\n if (withFilteredKeys) {\n filteredKeys.push(filterPath);\n }\n return undefined;\n } else if (jsonVal === null) {\n return undefined;\n } else if (Array.isArray(jsonVal)) {\n const arr = new Array<JsonValue>();\n\n for (const [index, value] of jsonVal.entries()) {\n const out = transform(\n value,\n `${visibilityPath}/${index}`,\n `${filterPath}[${index}]`,\n );\n if (out !== undefined) {\n arr.push(out);\n }\n }\n\n if (arr.length > 0 || isVisible) {\n return arr;\n }\n return undefined;\n }\n\n const outObj: JsonObject = {};\n let hasOutput = false;\n\n for (const [key, value] of Object.entries(jsonVal)) {\n if (value === undefined) {\n continue;\n }\n const out = transform(\n value,\n `${visibilityPath}/${key}`,\n filterPath ? `${filterPath}.${key}` : key,\n );\n if (out !== undefined) {\n outObj[key] = out;\n hasOutput = true;\n }\n }\n\n if (hasOutput || isVisible) {\n return outObj;\n }\n return undefined;\n }\n\n return {\n filteredKeys: withFilteredKeys ? filteredKeys : undefined,\n data: (transform(data, '', '') as JsonObject) ?? {},\n };\n}\n\nexport function filterErrorsByVisibility(\n errors: ValidationError[] | undefined,\n includeVisibilities: ConfigVisibility[] | undefined,\n visibilityByDataPath: Map<string, ConfigVisibility>,\n visibilityBySchemaPath: Map<string, ConfigVisibility>,\n): ValidationError[] {\n if (!errors) {\n return [];\n }\n if (!includeVisibilities) {\n return errors;\n }\n\n const visibleSchemaPaths = Array.from(visibilityBySchemaPath)\n .filter(([, v]) => includeVisibilities.includes(v))\n .map(([k]) => k);\n\n // If we're filtering by visibility we only care about the errors that happened\n // in a visible path.\n return errors.filter(error => {\n // We always include structural errors as we don't know whether there are\n // any visible paths within the structures.\n if (\n error.keyword === 'type' &&\n ['object', 'array'].includes(error.params.type)\n ) {\n return true;\n }\n\n // For fields that were required we use the schema path to determine whether\n // it was visible in addition to the data path. This is because the data path\n // visibilities are only populated for values that we reached, which we won't\n // if the value is missing.\n // We don't use this method for all the errors as the data path is more robust\n // and doesn't require us to properly trim the schema path.\n if (error.keyword === 'required') {\n const trimmedPath = error.schemaPath.slice(1, -'/required'.length);\n const fullPath = `${trimmedPath}/properties/${error.params.missingProperty}`;\n if (\n visibleSchemaPaths.some(visiblePath => visiblePath.startsWith(fullPath))\n ) {\n return true;\n }\n }\n\n const vis =\n visibilityByDataPath.get(error.dataPath) ?? DEFAULT_CONFIG_VISIBILITY;\n return vis && includeVisibilities.includes(vis);\n });\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppConfig, JsonObject } from '@backstage/config';\nimport { compileConfigSchemas } from './compile';\nimport { collectConfigSchemas } from './collect';\nimport { filterByVisibility, filterErrorsByVisibility } from './filtering';\nimport {\n ValidationError,\n ConfigSchema,\n ConfigSchemaPackageEntry,\n CONFIG_VISIBILITIES,\n} from './types';\n\n/**\n * Options that control the loading of configuration schema files in the backend.\n *\n * @public\n */\nexport type LoadConfigSchemaOptions =\n | {\n dependencies: string[];\n packagePaths?: string[];\n }\n | {\n serialized: JsonObject;\n };\n\nfunction errorsToError(errors: ValidationError[]): Error {\n const messages = errors.map(({ dataPath, message, params }) => {\n const paramStr = Object.entries(params)\n .map(([name, value]) => `${name}=${value}`)\n .join(' ');\n return `Config ${message || ''} { ${paramStr} } at ${dataPath}`;\n });\n const error = new Error(`Config validation failed, ${messages.join('; ')}`);\n (error as any).messages = messages;\n return error;\n}\n\n/**\n * Loads config schema for a Backstage instance.\n *\n * @public\n */\nexport async function loadConfigSchema(\n options: LoadConfigSchemaOptions,\n): Promise<ConfigSchema> {\n let schemas: ConfigSchemaPackageEntry[];\n\n if ('dependencies' in options) {\n schemas = await collectConfigSchemas(\n options.dependencies,\n options.packagePaths ?? [],\n );\n } else {\n const { serialized } = options;\n if (serialized?.backstageConfigSchemaVersion !== 1) {\n throw new Error(\n 'Serialized configuration schema is invalid or has an invalid version number',\n );\n }\n schemas = serialized.schemas as ConfigSchemaPackageEntry[];\n }\n\n const validate = compileConfigSchemas(schemas);\n\n return {\n process(\n configs: AppConfig[],\n { visibility, valueTransform, withFilteredKeys } = {},\n ): AppConfig[] {\n const result = validate(configs);\n\n const visibleErrors = filterErrorsByVisibility(\n result.errors,\n visibility,\n result.visibilityByDataPath,\n result.visibilityBySchemaPath,\n );\n if (visibleErrors.length > 0) {\n throw errorsToError(visibleErrors);\n }\n\n let processedConfigs = configs;\n\n if (visibility) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n visibility,\n result.visibilityByDataPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n } else if (valueTransform) {\n processedConfigs = processedConfigs.map(({ data, context }) => ({\n context,\n ...filterByVisibility(\n data,\n Array.from(CONFIG_VISIBILITIES),\n result.visibilityByDataPath,\n valueTransform,\n withFilteredKeys,\n ),\n }));\n }\n\n return processedConfigs;\n },\n serialize(): JsonObject {\n return {\n schemas,\n backstageConfigSchemaVersion: 1,\n };\n },\n };\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'fs-extra';\nimport yaml from 'yaml';\nimport chokidar from 'chokidar';\nimport { resolve as resolvePath, dirname, isAbsolute, basename } from 'path';\nimport { AppConfig } from '@backstage/config';\nimport { ForwardedError } from '@backstage/errors';\nimport {\n applyConfigTransforms,\n readEnvConfig,\n createIncludeTransform,\n createSubstitutionTransform,\n} from './lib';\n\n/**\n * Options that control the loading of configuration files in the backend.\n *\n * @public\n */\nexport type LoadConfigOptions = {\n // The root directory of the config loading context. Used to find default configs.\n configRoot: string;\n\n // Absolute paths to load config files from. Configs from earlier paths have lower priority.\n configPaths: string[];\n\n /** @deprecated This option has been removed */\n env?: string;\n\n /**\n * Custom environment variable loading function\n *\n * @experimental This API is not stable and may change at any point\n */\n experimentalEnvFunc?: (name: string) => Promise<string | undefined>;\n\n /**\n * An optional configuration that enables watching of config files.\n */\n watch?: {\n /**\n * A listener that is called when a config file is changed.\n */\n onChange: (configs: AppConfig[]) => void;\n\n /**\n * An optional signal that stops the watcher once the promise resolves.\n */\n stopSignal?: Promise<void>;\n };\n};\n\n/**\n * Load configuration data.\n *\n * @public\n */\nexport async function loadConfig(\n options: LoadConfigOptions,\n): Promise<AppConfig[]> {\n const { configRoot, experimentalEnvFunc: envFunc, watch } = options;\n const configPaths = options.configPaths.slice();\n\n // If no paths are provided, we default to reading\n // `app-config.yaml` and, if it exists, `app-config.local.yaml`\n if (configPaths.length === 0) {\n configPaths.push(resolvePath(configRoot, 'app-config.yaml'));\n\n const localConfig = resolvePath(configRoot, 'app-config.local.yaml');\n if (await fs.pathExists(localConfig)) {\n configPaths.push(localConfig);\n }\n }\n\n const env = envFunc ?? (async (name: string) => process.env[name]);\n\n const loadConfigFiles = async () => {\n const configs = [];\n\n for (const configPath of configPaths) {\n if (!isAbsolute(configPath)) {\n throw new Error(`Config load path is not absolute: '${configPath}'`);\n }\n\n const dir = dirname(configPath);\n const readFile = (path: string) =>\n fs.readFile(resolvePath(dir, path), 'utf8');\n\n const input = yaml.parse(await readFile(configPath));\n const substitutionTransform = createSubstitutionTransform(env);\n const data = await applyConfigTransforms(dir, input, [\n createIncludeTransform(env, readFile, substitutionTransform),\n substitutionTransform,\n ]);\n\n configs.push({ data, context: basename(configPath) });\n }\n\n return configs;\n };\n\n let fileConfigs;\n try {\n fileConfigs = await loadConfigFiles();\n } catch (error) {\n throw new ForwardedError('Failed to read static configuration file', error);\n }\n\n const envConfigs = await readEnvConfig(process.env);\n\n // Set up config file watching if requested by the caller\n if (watch) {\n let currentSerializedConfig = JSON.stringify(fileConfigs);\n\n const watcher = chokidar.watch(configPaths, {\n usePolling: process.env.NODE_ENV === 'test',\n });\n watcher.on('change', async () => {\n try {\n const newConfigs = await loadConfigFiles();\n const newSerializedConfig = JSON.stringify(newConfigs);\n\n if (currentSerializedConfig === newSerializedConfig) {\n return;\n }\n currentSerializedConfig = newSerializedConfig;\n\n watch.onChange([...newConfigs, ...envConfigs]);\n } catch (error) {\n console.error(`Failed to reload configuration files, ${error}`);\n }\n });\n\n if (watch.stopSignal) {\n watch.stopSignal.then(() => {\n watcher.close();\n });\n }\n }\n\n return [...fileConfigs, ...envConfigs];\n}\n"],"names":["resolvePath","relativePath"],"mappings":";;;;;;;;;;;AAmBA,MAAM,aAAa;AAGnB,MAAM,0BAA0B;uBAsBF,KAEd;AA9ChB;AA+CE,MAAI,OAA+B;AAEnC,aAAW,CAAC,MAAM,UAAU,OAAO,QAAQ,MAAM;AAC/C,QAAI,CAAC,OAAO;AACV;AAAA;AAEF,QAAI,KAAK,WAAW,aAAa;AAC/B,YAAM,MAAM,KAAK,QAAQ,YAAY;AACrC,YAAM,WAAW,IAAI,MAAM;AAE3B,UAAI,MAAO,OAAO,sBAAQ;AAC1B,iBAAW,CAAC,OAAO,SAAS,SAAS,WAAW;AAC9C,YAAI,CAAC,wBAAwB,KAAK,OAAO;AACvC,gBAAM,IAAI,UAAU,2BAA2B;AAAA;AAEjD,YAAI,QAAQ,SAAS,SAAS,GAAG;AAC/B,gBAAO,IAAI,QAAQ,UAAI,UAAJ,YAAa;AAChC,cAAI,OAAO,QAAQ,YAAY,MAAM,QAAQ,MAAM;AACjD,kBAAM,SAAS,SAAS,MAAM,GAAG,QAAQ,GAAG,KAAK;AACjD,kBAAM,IAAI,UACR,kCAAkC,8BA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MAC1C,YAAY,QAAQ,IAAI,aAAa;AAAA;AAEvC,YAAQ,GAAG,UAAU,YAAY;AAC/B,UAAI;AACF,cAAM,aAAa,MAAM;AACzB,cAAM,sBAAsB,KAAK,UAAU;AAE3C,YAAI,4BAA4B,qBAAqB;AACnD;AAAA;AAEF,kCAA0B;AAE1B,cAAM,SAAS,CAAC,GAAG,YAAY,GAAG;AAAA,eAC3B,OAAP;AACA,gBAAQ,MAAM,yCAAyC;AAAA;AAAA;AAI3D,QAAI,MAAM,YAAY;AACpB,YAAM,WAAW,KAAK,MAAM;AAC1B,gBAAQ;AAAA;AAAA;AAAA;AAKd,SAAO,CAAC,GAAG,aAAa,GAAG;AAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/config-loader",
|
|
3
3
|
"description": "Config loading functionality used by Backstage backend, and CLI",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public",
|
|
@@ -30,29 +30,31 @@
|
|
|
30
30
|
"clean": "backstage-cli clean"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@backstage/cli-common": "^0.1.
|
|
34
|
-
"@backstage/config": "^0.1.
|
|
33
|
+
"@backstage/cli-common": "^0.1.4",
|
|
34
|
+
"@backstage/config": "^0.1.9",
|
|
35
|
+
"@backstage/errors": "^0.1.3",
|
|
35
36
|
"@types/json-schema": "^7.0.6",
|
|
36
37
|
"ajv": "^7.0.3",
|
|
37
38
|
"chokidar": "^3.5.2",
|
|
38
39
|
"fs-extra": "9.1.0",
|
|
39
40
|
"json-schema": "^0.3.0",
|
|
40
41
|
"json-schema-merge-allof": "^0.8.1",
|
|
42
|
+
"json-schema-traverse": "^1.0.0",
|
|
41
43
|
"typescript-json-schema": "^0.50.1",
|
|
42
44
|
"yaml": "^1.9.2",
|
|
43
|
-
"yup": "^0.
|
|
45
|
+
"yup": "^0.32.9"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
46
48
|
"@types/jest": "^26.0.7",
|
|
47
49
|
"@types/json-schema-merge-allof": "^0.6.0",
|
|
48
50
|
"@types/mock-fs": "^4.10.0",
|
|
49
51
|
"@types/node": "^14.14.32",
|
|
50
|
-
"@types/yup": "^0.29.
|
|
51
|
-
"mock-fs": "^
|
|
52
|
+
"@types/yup": "^0.29.13",
|
|
53
|
+
"mock-fs": "^5.1.0"
|
|
52
54
|
},
|
|
53
55
|
"files": [
|
|
54
56
|
"dist"
|
|
55
57
|
],
|
|
56
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "017a47e585642733ac3b4b294cd00fc517d3ba08",
|
|
57
59
|
"module": "dist/index.esm.js"
|
|
58
60
|
}
|