@dxos/config 0.8.4-main.ae835ea → 0.8.4-main.bc674ce

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/lib/browser/chunk-WETPAQBA.mjs +147 -0
  2. package/dist/lib/browser/chunk-WETPAQBA.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-ZI5JXCYY.mjs +11 -0
  4. package/dist/lib/browser/chunk-ZI5JXCYY.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +137 -209
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/loaders/browser.mjs +98 -0
  8. package/dist/lib/browser/loaders/browser.mjs.map +7 -0
  9. package/dist/lib/browser/loaders/index.mjs +71 -0
  10. package/dist/lib/browser/loaders/index.mjs.map +7 -0
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/plugin/browser.mjs +8 -0
  13. package/dist/lib/browser/plugin/browser.mjs.map +7 -0
  14. package/dist/lib/browser/plugin/index.mjs +116 -0
  15. package/dist/lib/browser/plugin/index.mjs.map +7 -0
  16. package/dist/lib/browser/savers/browser.mjs +35 -0
  17. package/dist/lib/browser/savers/browser.mjs.map +7 -0
  18. package/dist/lib/browser/savers/index.mjs +7 -0
  19. package/dist/lib/browser/savers/index.mjs.map +7 -0
  20. package/dist/lib/node-esm/chunk-FVCGIRXP.mjs +149 -0
  21. package/dist/lib/node-esm/chunk-FVCGIRXP.mjs.map +7 -0
  22. package/dist/lib/node-esm/chunk-ODEKAZ4J.mjs +13 -0
  23. package/dist/lib/node-esm/chunk-ODEKAZ4J.mjs.map +7 -0
  24. package/dist/lib/node-esm/index.mjs +134 -300
  25. package/dist/lib/node-esm/index.mjs.map +4 -4
  26. package/dist/lib/node-esm/loaders/browser.mjs +100 -0
  27. package/dist/lib/node-esm/loaders/browser.mjs.map +7 -0
  28. package/dist/lib/node-esm/loaders/index.mjs +72 -0
  29. package/dist/lib/node-esm/loaders/index.mjs.map +7 -0
  30. package/dist/lib/node-esm/meta.json +1 -1
  31. package/dist/lib/node-esm/plugin/browser.mjs +10 -0
  32. package/dist/lib/node-esm/plugin/browser.mjs.map +7 -0
  33. package/dist/lib/node-esm/plugin/index.mjs +117 -0
  34. package/dist/lib/node-esm/plugin/index.mjs.map +7 -0
  35. package/dist/lib/node-esm/savers/browser.mjs +37 -0
  36. package/dist/lib/node-esm/savers/browser.mjs.map +7 -0
  37. package/dist/lib/node-esm/savers/index.mjs +9 -0
  38. package/dist/lib/node-esm/savers/index.mjs.map +7 -0
  39. package/dist/plugin/node-esm/{chunk-C4YHC2RN.mjs → chunk-QQKWALKX.mjs} +12 -6
  40. package/dist/plugin/node-esm/chunk-QQKWALKX.mjs.map +7 -0
  41. package/dist/plugin/node-esm/esbuild-plugin.mjs +1 -1
  42. package/dist/plugin/node-esm/meta.json +1 -1
  43. package/dist/plugin/node-esm/rollup-plugin.mjs +1 -1
  44. package/dist/plugin/node-esm/vite-plugin.mjs +1 -1
  45. package/dist/types/src/config-service.d.ts +19 -0
  46. package/dist/types/src/config-service.d.ts.map +1 -0
  47. package/dist/types/src/config.d.ts +0 -1
  48. package/dist/types/src/config.d.ts.map +1 -1
  49. package/dist/types/src/index.d.ts +5 -3
  50. package/dist/types/src/index.d.ts.map +1 -1
  51. package/dist/types/src/loaders/browser.d.ts +11 -15
  52. package/dist/types/src/loaders/browser.d.ts.map +1 -1
  53. package/dist/types/src/plugin/browser.d.ts +3 -0
  54. package/dist/types/src/plugin/browser.d.ts.map +1 -0
  55. package/dist/types/src/preset.d.ts +10 -0
  56. package/dist/types/src/preset.d.ts.map +1 -0
  57. package/dist/types/src/savers/browser.d.ts +2 -1
  58. package/dist/types/src/savers/browser.d.ts.map +1 -1
  59. package/dist/types/tsconfig.tsbuildinfo +1 -1
  60. package/package.json +45 -16
  61. package/src/config-service.ts +118 -0
  62. package/src/config.ts +3 -5
  63. package/src/index.ts +5 -3
  64. package/src/loaders/{browser.js → browser.ts} +15 -9
  65. package/src/loaders/index.ts +2 -2
  66. package/src/plugin/browser.ts +11 -0
  67. package/src/plugin/definitions.ts +2 -2
  68. package/src/preset.ts +40 -0
  69. package/src/savers/{browser.js → browser.ts} +2 -2
  70. package/dist/plugin/node-esm/chunk-C4YHC2RN.mjs.map +0 -7
package/package.json CHANGED
@@ -1,13 +1,46 @@
1
1
  {
2
2
  "name": "@dxos/config",
3
- "version": "0.8.4-main.ae835ea",
3
+ "version": "0.8.4-main.bc674ce",
4
4
  "description": "Config utilities",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
15
+ "imports": {
16
+ "#loaders": {
17
+ "source": {
18
+ "browser": "./src/loaders/browser.ts",
19
+ "default": "./src/loaders/index.ts"
20
+ },
21
+ "types": "./dist/types/src/loaders/index.d.ts",
22
+ "browser": "./dist/lib/browser/loaders/browser.mjs",
23
+ "default": "./dist/lib/node-esm/loaders/index.mjs"
24
+ },
25
+ "#savers": {
26
+ "source": {
27
+ "browser": "./src/savers/browser.ts",
28
+ "default": "./src/savers/index.ts"
29
+ },
30
+ "types": "./dist/types/src/savers/index.d.ts",
31
+ "browser": "./dist/lib/browser/savers/browser.mjs",
32
+ "default": "./dist/lib/node-esm/savers/index.mjs"
33
+ },
34
+ "#plugin": {
35
+ "source": {
36
+ "browser": "./src/plugin/browser.ts",
37
+ "default": "./src/plugin/index.ts"
38
+ },
39
+ "types": "./dist/types/src/plugin/index.d.ts",
40
+ "browser": "./dist/lib/browser/plugin/browser.mjs",
41
+ "default": "./dist/lib/node-esm/plugin/index.mjs"
42
+ }
43
+ },
11
44
  "exports": {
12
45
  ".": {
13
46
  "source": "./src/index.ts",
@@ -38,12 +71,6 @@
38
71
  }
39
72
  },
40
73
  "main": "dist/lib/node/index.cjs",
41
- "browser": {
42
- "./src/loaders/index.ts": "./src/loaders/browser.js",
43
- "./src/savers/index.ts": "./src/savers/browser.js",
44
- "./src/plugin/index.ts": false,
45
- "./dist/lib/node/index.cjs": "./dist/lib/browser/index.mjs"
46
- },
47
74
  "types": "dist/types/src/index.d.ts",
48
75
  "typesVersions": {
49
76
  "*": {
@@ -63,21 +90,23 @@
63
90
  "src"
64
91
  ],
65
92
  "dependencies": {
93
+ "@effect/platform": "0.93.6",
66
94
  "boolean": "^3.0.1",
67
- "js-yaml": "^4.1.0",
95
+ "effect": "3.19.11",
68
96
  "localforage": "^1.10.0",
69
97
  "lodash.defaultsdeep": "^4.6.1",
70
98
  "lodash.ismatch": "^4.4.0",
71
99
  "pkg-up": "^3.1.0",
72
- "@dxos/invariant": "0.8.4-main.ae835ea",
73
- "@dxos/log": "0.8.4-main.ae835ea",
74
- "@dxos/node-std": "0.8.4-main.ae835ea",
75
- "@dxos/protocols": "0.8.4-main.ae835ea",
76
- "@dxos/tracing": "0.8.4-main.ae835ea",
77
- "@dxos/util": "0.8.4-main.ae835ea"
100
+ "yaml": "^2.7.0",
101
+ "@dxos/client-protocol": "0.8.4-main.bc674ce",
102
+ "@dxos/invariant": "0.8.4-main.bc674ce",
103
+ "@dxos/protocols": "0.8.4-main.bc674ce",
104
+ "@dxos/tracing": "0.8.4-main.bc674ce",
105
+ "@dxos/util": "0.8.4-main.bc674ce",
106
+ "@dxos/node-std": "0.8.4-main.bc674ce",
107
+ "@dxos/log": "0.8.4-main.bc674ce"
78
108
  },
79
109
  "devDependencies": {
80
- "@types/js-yaml": "^4.0.5",
81
110
  "@types/lodash.defaultsdeep": "^4.6.6",
82
111
  "@types/lodash.ismatch": "^4.4.9",
83
112
  "webpack": "^5.88.2"
@@ -0,0 +1,118 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { dirname } from 'node:path';
6
+
7
+ import * as FileSystem from '@effect/platform/FileSystem';
8
+ import * as Context from 'effect/Context';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as Layer from 'effect/Layer';
11
+ import * as Option from 'effect/Option';
12
+ import * as Yaml from 'yaml';
13
+
14
+ import { DX_CONFIG, DX_DATA } from '@dxos/client-protocol';
15
+ import { getProfilePath } from '@dxos/client-protocol';
16
+ import { invariant } from '@dxos/invariant';
17
+
18
+ import { Config } from './config';
19
+
20
+ export const memoryConfig = new Config({
21
+ runtime: {
22
+ client: {
23
+ edgeFeatures: {
24
+ echoReplicator: true,
25
+ feedReplicator: true,
26
+ signaling: true,
27
+ agents: true,
28
+ },
29
+ },
30
+ },
31
+ });
32
+
33
+ export const defaultConfig = new Config({
34
+ runtime: {
35
+ client: {
36
+ edgeFeatures: {
37
+ echoReplicator: true,
38
+ feedReplicator: true,
39
+ signaling: true,
40
+ agents: true,
41
+ },
42
+ storage: {
43
+ persistent: true,
44
+ },
45
+ },
46
+ services: {
47
+ edge: {
48
+ url: 'wss://edge-production.dxos.workers.dev/',
49
+ },
50
+ iceProviders: [
51
+ {
52
+ urls: 'https://edge-production.dxos.workers.dev/ice',
53
+ },
54
+ ],
55
+ ai: {
56
+ server: 'https://ai-service.dxos.workers.dev',
57
+ },
58
+ ipfs: {
59
+ server: 'https://api.ipfs.dxos.network/api/v0',
60
+ gateway: 'https://gateway.ipfs.dxos.network/ipfs',
61
+ },
62
+ },
63
+ },
64
+ });
65
+
66
+ export class ConfigService extends Context.Tag('ConfigService')<ConfigService, Config>() {
67
+ static layerMemory = Layer.effect(ConfigService, Effect.succeed(memoryConfig));
68
+
69
+ static fromConfig = (config: Config) => Layer.succeed(ConfigService, config);
70
+
71
+ static load = (args: { config: Option.Option<string>; profile: string }) => {
72
+ const defaultConfigPath = `${getProfilePath(DX_CONFIG, args.profile)}.yml`;
73
+ return Effect.gen(function* () {
74
+ const fs = yield* FileSystem.FileSystem;
75
+ const configPath = Option.getOrElse(args.config, () => defaultConfigPath);
76
+ const configContent = yield* fs.readFileString(configPath);
77
+ const configValues = Yaml.parse(configContent);
78
+ return ConfigService.of(new Config(configValues, profileBuiltinDefaults(args.profile).values));
79
+ }).pipe(
80
+ // If the config file doesn't exist, create it.
81
+ Effect.catchTag('SystemError', () =>
82
+ Effect.gen(function* () {
83
+ const configValues = defaultConfig.values;
84
+ const fs = yield* FileSystem.FileSystem;
85
+ yield* fs.makeDirectory(dirname(defaultConfigPath), { recursive: true });
86
+ yield* fs.writeFileString(defaultConfigPath, Yaml.stringify(configValues));
87
+
88
+ return ConfigService.of(new Config(configValues));
89
+ }),
90
+ ),
91
+ );
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Default config for a profile.
97
+ * Always merged with the default config.
98
+ */
99
+ const profileBuiltinDefaults = (profile: string) => {
100
+ invariant(!profile.endsWith('.yml'));
101
+
102
+ return new Config({
103
+ runtime: {
104
+ client: {
105
+ edgeFeatures: {
106
+ echoReplicator: true,
107
+ feedReplicator: true,
108
+ signaling: true,
109
+ agents: true,
110
+ },
111
+ storage: {
112
+ persistent: true,
113
+ dataRoot: getProfilePath(DX_DATA, profile),
114
+ },
115
+ },
116
+ },
117
+ });
118
+ };
package/src/config.ts CHANGED
@@ -19,7 +19,6 @@ const configRootType = schema.getCodecForType('dxos.config.Config');
19
19
 
20
20
  /**
21
21
  * Maps the given objects onto a flattened set of (key x values).
22
- *
23
22
  * Expects parsed yaml content of the form:
24
23
  *
25
24
  * ```
@@ -36,7 +35,6 @@ export const mapFromKeyValues = (spec: MappingSpec, values: Record<string, any>)
36
35
 
37
36
  for (const [key, { path, type }] of Object.entries(spec)) {
38
37
  let value = values[key];
39
-
40
38
  if (value !== undefined) {
41
39
  if (type) {
42
40
  switch (type) {
@@ -101,16 +99,16 @@ export const mapToKeyValues = (spec: MappingSpec, values: any) => {
101
99
  */
102
100
  export const validateConfig = (config: ConfigProto): ConfigProto => {
103
101
  if (!('version' in config)) {
104
- throw new InvalidConfigError('Version not specified');
102
+ throw new InvalidConfigError({ message: 'Version not specified' });
105
103
  }
106
104
 
107
105
  if (config?.version !== 1) {
108
- throw new InvalidConfigError(`Invalid config version: ${config.version}`);
106
+ throw new InvalidConfigError({ message: `Invalid config version: ${config.version}` });
109
107
  }
110
108
 
111
109
  const error = configRootType.protoType.verify(config);
112
110
  if (error) {
113
- throw new InvalidConfigError(error);
111
+ throw new InvalidConfigError({ message: String(error) });
114
112
  }
115
113
 
116
114
  return config;
package/src/index.ts CHANGED
@@ -8,7 +8,9 @@ export * as defs from '@dxos/protocols/proto/dxos/config';
8
8
  export { type Config as ConfigProto } from '@dxos/protocols/proto/dxos/config';
9
9
 
10
10
  export * from './config';
11
- export * from './loaders';
12
- export * from './savers';
13
- export * from './plugin';
11
+ export * from './config-service';
12
+ export * from '#loaders';
13
+ export * from '#savers';
14
+ export * from '#plugin';
14
15
  export * from './types';
16
+ export * from './preset';
@@ -4,19 +4,25 @@
4
4
 
5
5
  /* THIS FILE WILL BE LOADED BY CONTEXT REPLACEMENT PLUGIN IN BROWSER ENVS. */
6
6
 
7
- /* global __DXOS_CONFIG__ __CONFIG_ENVS__ __CONFIG_DEFAULTS__ __CONFIG_LOCAL__ */
8
-
9
7
  import localforage from 'localforage';
10
8
 
11
9
  import { log } from '@dxos/log';
10
+ import { type Config as ConfigProto } from '@dxos/protocols/proto/dxos/config';
11
+
12
+ declare const __DXOS_CONFIG__: { publicUrl?: string; dynamic?: boolean };
13
+ declare const __CONFIG_ENVS__: Partial<ConfigProto> | undefined;
14
+ declare const __CONFIG_DEFAULTS__: Partial<ConfigProto> | undefined;
15
+ declare const __CONFIG_LOCAL__: Partial<ConfigProto> | undefined;
12
16
 
13
17
  const CONFIG_ENDPOINT = '/.well-known/dx/config';
14
18
 
15
- export const Local = () => {
19
+ export const Profile = (_profile = 'default'): Partial<ConfigProto> => ({});
20
+
21
+ export const Local = (): Partial<ConfigProto> => {
16
22
  return typeof __CONFIG_LOCAL__ !== 'undefined' ? __CONFIG_LOCAL__ : {};
17
23
  };
18
24
 
19
- export const Dynamics = async () => {
25
+ export const Dynamics = async (): Promise<Partial<ConfigProto>> => {
20
26
  const { publicUrl = '', dynamic } = __DXOS_CONFIG__;
21
27
  if (!dynamic) {
22
28
  log('dynamics disabled');
@@ -32,20 +38,20 @@ export const Dynamics = async () => {
32
38
  });
33
39
  };
34
40
 
35
- export const Envs = () => {
41
+ export const Envs = (_basePath?: string): Partial<ConfigProto> => {
36
42
  return typeof __CONFIG_ENVS__ !== 'undefined' ? __CONFIG_ENVS__ : {};
37
43
  };
38
44
 
39
- export const Defaults = () => {
45
+ export const Defaults = (_basePath?: string): Partial<ConfigProto> => {
40
46
  return typeof __CONFIG_DEFAULTS__ !== 'undefined' ? __CONFIG_DEFAULTS__ : {};
41
47
  };
42
48
 
43
49
  /**
44
50
  * Settings config from browser storage.
45
51
  */
46
- export const Storage = async () => {
52
+ export const Storage = async (): Promise<Partial<ConfigProto>> => {
47
53
  try {
48
- const config = await localforage.getItem('dxos.org/settings/config');
54
+ const config = await localforage.getItem<Partial<ConfigProto>>('dxos.org/settings/config');
49
55
  if (config) {
50
56
  return config;
51
57
  }
@@ -55,7 +61,7 @@ export const Storage = async () => {
55
61
  return {};
56
62
  };
57
63
 
58
- export const Remote = (target, authenticationToken) => {
64
+ export const Remote = (target: string | undefined, authenticationToken?: string): Partial<ConfigProto> => {
59
65
  if (!target) {
60
66
  return {};
61
67
  }
@@ -5,7 +5,7 @@
5
5
  import fs from 'node:fs';
6
6
  import path from 'node:path';
7
7
 
8
- import yaml from 'js-yaml';
8
+ import { parse } from 'yaml';
9
9
 
10
10
  import { log } from '@dxos/log';
11
11
  import { type Config as ConfigProto } from '@dxos/protocols/proto/dxos/config';
@@ -19,7 +19,7 @@ const DEFAULT_BASE_PATH = path.resolve(process.cwd(), 'config');
19
19
 
20
20
  const maybeLoadFile = (file: string): any => {
21
21
  try {
22
- return yaml.load(fs.readFileSync(file, { encoding: 'utf8' }));
22
+ return parse(fs.readFileSync(file, { encoding: 'utf8' }));
23
23
  } catch (err: any) {
24
24
  // Ignored.
25
25
  }
@@ -0,0 +1,11 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ // Browser stub for plugin module - plugins are build-time only.
6
+
7
+ export type * from './types';
8
+
9
+ export const definitions = (): Record<string, unknown> => {
10
+ throw new Error('Config plugin definitions are not available in the browser');
11
+ };
@@ -6,8 +6,8 @@ import { execSync } from 'node:child_process';
6
6
  import { readFileSync } from 'node:fs';
7
7
  import { resolve } from 'node:path';
8
8
 
9
- import yaml from 'js-yaml';
10
9
  import pkgUp from 'pkg-up';
10
+ import { parse } from 'yaml';
11
11
 
12
12
  import { invariant } from '@dxos/invariant';
13
13
  import { log } from '@dxos/log';
@@ -40,7 +40,7 @@ export const definitions = ({
40
40
  invariant(key);
41
41
  let content = {};
42
42
  try {
43
- content = yaml.load(readFileSync(value, 'utf-8')) as any;
43
+ content = parse(readFileSync(value, 'utf-8')) as any;
44
44
 
45
45
  // Map environment variables to config values.
46
46
  if (key === '__CONFIG_ENVS__') {
package/src/preset.ts ADDED
@@ -0,0 +1,40 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Match from 'effect/Match';
6
+
7
+ import { Config } from './config';
8
+
9
+ export type ConfigPresetOptions = {
10
+ /**
11
+ * Edge service.
12
+ * @default main
13
+ */
14
+ edge?: 'local' | 'dev' | 'main' | 'production';
15
+ };
16
+
17
+ export const configPreset = ({ edge = 'main' }: ConfigPresetOptions = {}) =>
18
+ new Config({
19
+ version: 1,
20
+ runtime: {
21
+ client: {
22
+ edgeFeatures: {
23
+ signaling: true,
24
+ echoReplicator: true,
25
+ feedReplicator: true,
26
+ },
27
+ },
28
+ services: {
29
+ edge: {
30
+ url: Match.value(edge).pipe(
31
+ Match.when('local', () => 'http://localhost:8787'),
32
+ Match.when('dev', () => 'https://edge.dxos.workers.dev'),
33
+ Match.when('main', () => 'https://edge-main.dxos.workers.dev'),
34
+ Match.when('production', () => 'https://edge-production.dxos.workers.dev'),
35
+ Match.exhaustive,
36
+ ),
37
+ },
38
+ },
39
+ },
40
+ });
@@ -7,10 +7,11 @@
7
7
  import localforage from 'localforage';
8
8
 
9
9
  import { log } from '@dxos/log';
10
+ import { type Config as ConfigProto } from '@dxos/protocols/proto/dxos/config';
10
11
 
11
12
  let PERFORMING_CONFIG_SAVE = false;
12
13
 
13
- export const SaveConfig = async (config) => {
14
+ export const SaveConfig = async (config: ConfigProto): Promise<void> => {
14
15
  if (PERFORMING_CONFIG_SAVE) {
15
16
  log.warn('Already performing config save');
16
17
  return;
@@ -21,7 +22,6 @@ export const SaveConfig = async (config) => {
21
22
  await localforage.setItem('dxos.org/settings/config', config);
22
23
  } catch (err) {
23
24
  log.warn('Failed to save config', { err });
24
- return {};
25
25
  } finally {
26
26
  PERFORMING_CONFIG_SAVE = false;
27
27
  }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/plugin/definitions.ts", "../../../src/config.ts"],
4
- "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { execSync } from 'node:child_process';\nimport { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\n\nimport yaml from 'js-yaml';\nimport pkgUp from 'pkg-up';\n\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\nimport { setDeep } from '@dxos/util';\n\nimport { mapFromKeyValues } from '../config';\n\nimport { type ConfigPluginOpts } from './types';\n\nconst CWD = process.cwd();\n\nexport const definitions = ({\n configPath,\n envPath,\n devPath,\n mode = process.env.NODE_ENV,\n publicUrl = '',\n env,\n}: ConfigPluginOpts) => {\n const KEYS_TO_FILE = {\n __CONFIG_DEFAULTS__: configPath?.length ? configPath : resolve(CWD, 'dx.yml'),\n __CONFIG_ENVS__: envPath?.length ? envPath : resolve(CWD, 'dx-env.yml'),\n } as { [key: string]: string };\n if (mode !== 'production') {\n KEYS_TO_FILE.__CONFIG_LOCAL__ = devPath ?? resolve(CWD, 'dx-local.yml');\n }\n\n return Object.entries(KEYS_TO_FILE).reduce(\n (prev, [key, value]) => {\n invariant(key);\n let content = {};\n try {\n content = yaml.load(readFileSync(value, 'utf-8')) as any;\n\n // Map environment variables to config values.\n if (key === '__CONFIG_ENVS__') {\n content = mapFromKeyValues(content, process.env);\n }\n\n if (key === '__CONFIG_DEFAULTS__') {\n // Load app environment variables into default config.\n Object.entries(process.env).forEach(([key, value]) => {\n if (key.startsWith('DX_') || env?.includes(key)) {\n setDeep(content, ['runtime', 'app', 'env', key], value);\n }\n });\n\n // Set build info automatically if available.\n try {\n const timestamp = new Date().toISOString();\n const commitHash =\n process.env.DX_COMMIT_HASH ??\n execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).replace('\\n', '');\n const packagePath = pkgUp.sync();\n const packageJson = packagePath && JSON.parse(readFileSync(packagePath, 'utf-8'));\n setDeep(content, ['runtime', 'app', 'build', 'timestamp'], timestamp);\n setDeep(content, ['runtime', 'app', 'build', 'commitHash'], commitHash);\n setDeep(content, ['runtime', 'app', 'build', 'version'], packageJson?.version);\n } catch {}\n }\n } catch (err: any) {\n if (err.message.includes('YAMLException')) {\n log.error(`Failed to parse file ${value}:`, err);\n } else {\n log(`Failed to load file ${value}:`, err);\n }\n\n if (key === '__CONFIG_DEFAULTS__') {\n // Default config is required.\n throw new Error(`Failed to load default config file from ${value}`);\n }\n }\n\n return {\n ...prev,\n [key]: content,\n };\n },\n {\n __CONFIG_DEFAULTS__: {},\n __CONFIG_ENVS__: {},\n __CONFIG_LOCAL__: {},\n __DXOS_CONFIG__: { dynamic: mode === 'production', publicUrl },\n },\n );\n};\n", "//\n// Copyright 2021 DXOS.org\n//\n\nimport { boolean } from 'boolean';\nimport defaultsDeep from 'lodash.defaultsdeep';\nimport isMatch from 'lodash.ismatch';\n\nimport { InvalidConfigError } from '@dxos/protocols';\nimport { schema } from '@dxos/protocols/proto';\nimport { type Config as ConfigProto } from '@dxos/protocols/proto/dxos/config';\nimport { trace } from '@dxos/tracing';\nimport { getDeep, setDeep } from '@dxos/util';\n\nimport { type ConfigKey, type DeepIndex, type ParseKey } from './types';\n\ntype MappingSpec = Record<string, { path: string; type?: string }>;\nconst configRootType = schema.getCodecForType('dxos.config.Config');\n\n/**\n * Maps the given objects onto a flattened set of (key x values).\n *\n * Expects parsed yaml content of the form:\n *\n * ```\n * ENV_VAR:\n * path: config.selector.path\n * ```\n *\n * @param {object} spec\n * @param {object} values\n * @return {object}\n */\nexport const mapFromKeyValues = (spec: MappingSpec, values: Record<string, any>) => {\n const config = {};\n\n for (const [key, { path, type }] of Object.entries(spec)) {\n let value = values[key];\n\n if (value !== undefined) {\n if (type) {\n switch (type) {\n case 'boolean': {\n value = boolean(value);\n break;\n }\n\n case 'number': {\n value = Number(value);\n break;\n }\n\n case 'string': {\n break;\n }\n\n case 'json': {\n value = value ? JSON.parse(value) : null;\n break;\n }\n\n default: {\n throw new Error(`Invalid type: ${type}`);\n }\n }\n }\n\n setDeep(config, path.split('.'), value);\n }\n }\n\n return config;\n};\n\n/**\n * Maps the given flattend set of (key x values) onto a JSON object.\n * @param {object} spec\n * @param {object} values\n */\nexport const mapToKeyValues = (spec: MappingSpec, values: any) => {\n const config: Record<string, any> = {};\n\n for (const [key, { path, type }] of Object.entries(spec)) {\n const value = getDeep(values, path.split('.'));\n if (value !== undefined) {\n switch (type) {\n case 'json':\n config[key] = JSON.stringify(value);\n break;\n default:\n config[key] = value;\n }\n }\n }\n\n return config;\n};\n\n/**\n * Validate config object.\n */\nexport const validateConfig = (config: ConfigProto): ConfigProto => {\n if (!('version' in config)) {\n throw new InvalidConfigError('Version not specified');\n }\n\n if (config?.version !== 1) {\n throw new InvalidConfigError(`Invalid config version: ${config.version}`);\n }\n\n const error = configRootType.protoType.verify(config);\n if (error) {\n throw new InvalidConfigError(error);\n }\n\n return config;\n};\n\nexport const ConfigResource = Symbol.for('dxos.resource.Config');\n\n/**\n * Global configuration object.\n * NOTE: Config objects are immutable.\n */\n@trace.resource({ annotation: ConfigResource })\nexport class Config {\n private readonly _config: any;\n\n /**\n * Creates an immutable instance.\n * @constructor\n */\n constructor(config: ConfigProto = {}, ...objects: ConfigProto[]) {\n this._config = validateConfig(defaultsDeep(config, ...objects, { version: 1 }));\n }\n\n /**\n * Returns an immutable config JSON object.\n */\n get values(): ConfigProto {\n return this._config;\n }\n\n /**\n * Returns the given config property.\n *\n * @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.\n * @param defaultValue Default value to return if option is not present in the config.\n * @returns The config value or undefined if the option is not present.\n */\n get<K extends ConfigKey>(\n key: K,\n defaultValue?: DeepIndex<ConfigProto, ParseKey<K>>,\n ): DeepIndex<ConfigProto, ParseKey<K>> | undefined {\n return getDeep(this._config, key.split('.')) ?? defaultValue;\n }\n\n /**\n * Get unique key.\n */\n find<T = any>(path: string, test: object): T | undefined {\n const values = getDeep(this._config, path.split('.'));\n if (!Array.isArray(values)) {\n return;\n }\n\n return values.find((value) => isMatch(value, test));\n }\n\n /**\n * Returns the given config property or throw if it doesn't exist.\n *\n * @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.\n */\n getOrThrow<K extends ConfigKey>(key: K): Exclude<DeepIndex<ConfigProto, ParseKey<K>>, undefined> {\n const value: DeepIndex<ConfigProto, ParseKey<K>> | undefined = getDeep(this._config, key.split('.'));\n if (!value) {\n throw new Error(`Config option not present: ${key}`);\n }\n return value;\n }\n}\n"],
5
- "mappings": ";;;AAIA,SAASA,gBAAgB;AACzB,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAExB,OAAOC,UAAU;AACjB,OAAOC,WAAW;AAElB,SAASC,iBAAiB;AAC1B,SAASC,WAAW;AACpB,SAASC,WAAAA,gBAAe;;;ACTxB,SAASC,eAAe;AACxB,OAAOC,kBAAkB;AACzB,OAAOC,aAAa;AAEpB,SAASC,0BAA0B;AACnC,SAASC,cAAc;AAEvB,SAASC,aAAa;AACtB,SAASC,SAASC,eAAe;;;;;;;AAKjC,IAAMC,iBAAiBC,OAAOC,gBAA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BAAiB;MAAEC,SAAS/C,SAAS;MAAcG;IAAU;EAC/D,CAAA;AAEJ;",
6
- "names": ["execSync", "readFileSync", "resolve", "yaml", "pkgUp", "invariant", "log", "setDeep", "boolean", "defaultsDeep", "isMatch", "InvalidConfigError", "schema", "trace", "getDeep", "setDeep", "configRootType", "schema", "getCodecForType", "mapFromKeyValues", "spec", "values", "config", "key", "path", "type", "Object", "entries", "value", "undefined", "boolean", "Number", "JSON", "parse", "Error", "setDeep", "split", "validateConfig", "config", "InvalidConfigError", "version", "error", "configRootType", "protoType", "verify", "ConfigResource", "Symbol", "for", "Config", "_config", "objects", "defaultsDeep", "values", "get", "key", "defaultValue", "getDeep", "split", "find", "path", "test", "Array", "isArray", "value", "isMatch", "getOrThrow", "Error", "resource", "annotation", "CWD", "process", "cwd", "definitions", "configPath", "envPath", "devPath", "mode", "env", "NODE_ENV", "publicUrl", "KEYS_TO_FILE", "__CONFIG_DEFAULTS__", "length", "resolve", "__CONFIG_ENVS__", "__CONFIG_LOCAL__", "Object", "entries", "reduce", "prev", "key", "value", "invariant", "content", "yaml", "load", "readFileSync", "mapFromKeyValues", "forEach", "startsWith", "includes", "setDeep", "timestamp", "Date", "toISOString", "commitHash", "DX_COMMIT_HASH", "execSync", "encoding", "replace", "packagePath", "pkgUp", "sync", "packageJson", "JSON", "parse", "version", "err", "message", "log", "error", "Error", "__DXOS_CONFIG__", "dynamic"]
7
- }