@dxos/config 0.8.3 → 0.8.4-main.03d5cd7b56

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 (107) hide show
  1. package/dist/lib/browser/chunk-VQRJY4AQ.mjs +11 -0
  2. package/dist/lib/browser/chunk-VQRJY4AQ.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-ZY4D4E3Q.mjs +146 -0
  4. package/dist/lib/browser/chunk-ZY4D4E3Q.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 +73 -0
  8. package/dist/lib/browser/loaders/browser.mjs.map +7 -0
  9. package/dist/lib/browser/loaders/index.mjs +66 -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 +98 -0
  15. package/dist/lib/browser/plugin/index.mjs.map +7 -0
  16. package/dist/lib/browser/savers/browser.mjs +25 -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-T6NFH3CC.mjs +13 -0
  21. package/dist/lib/node-esm/chunk-T6NFH3CC.mjs.map +7 -0
  22. package/dist/lib/node-esm/chunk-WIJROCKQ.mjs +148 -0
  23. package/dist/lib/node-esm/chunk-WIJROCKQ.mjs.map +7 -0
  24. package/dist/lib/node-esm/index.mjs +133 -289
  25. package/dist/lib/node-esm/index.mjs.map +4 -4
  26. package/dist/lib/node-esm/loaders/browser.mjs +75 -0
  27. package/dist/lib/node-esm/loaders/browser.mjs.map +7 -0
  28. package/dist/lib/node-esm/loaders/index.mjs +67 -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 +99 -0
  34. package/dist/lib/node-esm/plugin/index.mjs.map +7 -0
  35. package/dist/lib/node-esm/savers/browser.mjs +27 -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-PAFU3TJI.mjs → chunk-XCZNYAQT.mjs} +27 -29
  40. package/dist/plugin/node-esm/chunk-XCZNYAQT.mjs.map +7 -0
  41. package/dist/plugin/node-esm/esbuild-plugin.mjs +2 -2
  42. package/dist/plugin/node-esm/meta.json +1 -1
  43. package/dist/plugin/node-esm/rollup-plugin.mjs +2 -2
  44. package/dist/plugin/node-esm/vite-plugin.mjs +6 -6
  45. package/dist/plugin/node-esm/vite-plugin.mjs.map +3 -3
  46. package/dist/types/src/config-service.d.ts +19 -0
  47. package/dist/types/src/config-service.d.ts.map +1 -0
  48. package/dist/types/src/config.d.ts +0 -1
  49. package/dist/types/src/config.d.ts.map +1 -1
  50. package/dist/types/src/index.d.ts +6 -3
  51. package/dist/types/src/index.d.ts.map +1 -1
  52. package/dist/types/src/loaders/browser.d.ts +11 -15
  53. package/dist/types/src/loaders/browser.d.ts.map +1 -1
  54. package/dist/types/src/loaders/index.d.ts.map +1 -1
  55. package/dist/types/src/plugin/browser.d.ts +3 -0
  56. package/dist/types/src/plugin/browser.d.ts.map +1 -0
  57. package/dist/types/src/plugin/definitions.d.ts +3 -3
  58. package/dist/types/src/plugin/definitions.d.ts.map +1 -1
  59. package/dist/types/src/plugin/esbuild-plugin.d.ts.map +1 -1
  60. package/dist/types/src/plugin/index.d.ts +1 -1
  61. package/dist/types/src/plugin/index.d.ts.map +1 -1
  62. package/dist/types/src/plugin/rollup-plugin.d.ts.map +1 -1
  63. package/dist/types/src/plugin/types.d.ts +4 -0
  64. package/dist/types/src/plugin/types.d.ts.map +1 -1
  65. package/dist/types/src/plugin/vite-plugin.d.ts +1 -1
  66. package/dist/types/src/plugin/vite-plugin.d.ts.map +1 -1
  67. package/dist/types/src/preset.d.ts +10 -0
  68. package/dist/types/src/preset.d.ts.map +1 -0
  69. package/dist/types/src/savers/browser.d.ts +2 -1
  70. package/dist/types/src/savers/browser.d.ts.map +1 -1
  71. package/dist/types/src/savers/index.d.ts.map +1 -1
  72. package/dist/types/src/telemetry.d.ts +17 -0
  73. package/dist/types/src/telemetry.d.ts.map +1 -0
  74. package/dist/types/src/testing/defaults.d.ts +6 -6
  75. package/dist/types/src/testing/defaults.d.ts.map +1 -1
  76. package/dist/types/src/testing/envs-map.d.ts +12 -14
  77. package/dist/types/src/testing/envs-map.d.ts.map +1 -1
  78. package/dist/types/tsconfig.tsbuildinfo +1 -1
  79. package/package.json +50 -30
  80. package/src/config-service.ts +116 -0
  81. package/src/config.ts +4 -7
  82. package/src/index.ts +6 -3
  83. package/src/loaders/{browser.js → browser.ts} +15 -9
  84. package/src/loaders/index.ts +2 -2
  85. package/src/plugin/browser.ts +11 -0
  86. package/src/plugin/definitions.ts +6 -6
  87. package/src/plugin/index.ts +1 -1
  88. package/src/plugin/types.ts +5 -0
  89. package/src/plugin/vite-plugin.ts +8 -5
  90. package/src/preset.ts +40 -0
  91. package/src/savers/{browser.js → browser.ts} +3 -3
  92. package/src/telemetry.ts +25 -0
  93. package/src/testing/build.js +3 -2
  94. package/README.yml +0 -1
  95. package/dist/lib/node/index.cjs +0 -357
  96. package/dist/lib/node/index.cjs.map +0 -7
  97. package/dist/lib/node/meta.json +0 -1
  98. package/dist/plugin/node/chunk-SLXRWZWZ.cjs +0 -249
  99. package/dist/plugin/node/chunk-SLXRWZWZ.cjs.map +0 -7
  100. package/dist/plugin/node/esbuild-plugin.cjs +0 -45
  101. package/dist/plugin/node/esbuild-plugin.cjs.map +0 -7
  102. package/dist/plugin/node/meta.json +0 -1
  103. package/dist/plugin/node/rollup-plugin.cjs +0 -39
  104. package/dist/plugin/node/rollup-plugin.cjs.map +0 -7
  105. package/dist/plugin/node/vite-plugin.cjs +0 -50
  106. package/dist/plugin/node/vite-plugin.cjs.map +0 -7
  107. package/dist/plugin/node-esm/chunk-PAFU3TJI.mjs.map +0 -7
@@ -0,0 +1,148 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+
3
+ // src/config.ts
4
+ import defaultsDeep from "lodash.defaultsdeep";
5
+ import isMatch from "lodash.ismatch";
6
+ import { InvalidConfigError } from "@dxos/protocols";
7
+ import { schema } from "@dxos/protocols/proto";
8
+ import { trace } from "@dxos/tracing";
9
+ import { getDeep, setDeep } from "@dxos/util";
10
+ function _ts_decorate(decorators, target, key, desc) {
11
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
13
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
14
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
15
+ }
16
+ var configRootType = schema.getCodecForType("dxos.config.Config");
17
+ var mapFromKeyValues = (spec, values) => {
18
+ const config = {};
19
+ for (const [key, { path, type }] of Object.entries(spec)) {
20
+ let value = values[key];
21
+ if (value !== void 0) {
22
+ if (type) {
23
+ switch (type) {
24
+ case "boolean": {
25
+ value = String(value).toLowerCase() === "true" || value === "1";
26
+ break;
27
+ }
28
+ case "number": {
29
+ value = Number(value);
30
+ break;
31
+ }
32
+ case "string": {
33
+ break;
34
+ }
35
+ case "json": {
36
+ value = value ? JSON.parse(value) : null;
37
+ break;
38
+ }
39
+ default: {
40
+ throw new Error(`Invalid type: ${type}`);
41
+ }
42
+ }
43
+ }
44
+ setDeep(config, path.split("."), value);
45
+ }
46
+ }
47
+ return config;
48
+ };
49
+ var mapToKeyValues = (spec, values) => {
50
+ const config = {};
51
+ for (const [key, { path, type }] of Object.entries(spec)) {
52
+ const value = getDeep(values, path.split("."));
53
+ if (value !== void 0) {
54
+ switch (type) {
55
+ case "json":
56
+ config[key] = JSON.stringify(value);
57
+ break;
58
+ default:
59
+ config[key] = value;
60
+ }
61
+ }
62
+ }
63
+ return config;
64
+ };
65
+ var validateConfig = (config) => {
66
+ if (!("version" in config)) {
67
+ throw new InvalidConfigError({
68
+ message: "Version not specified"
69
+ });
70
+ }
71
+ if (config?.version !== 1) {
72
+ throw new InvalidConfigError({
73
+ message: `Invalid config version: ${config.version}`
74
+ });
75
+ }
76
+ const error = configRootType.protoType.verify(config);
77
+ if (error) {
78
+ throw new InvalidConfigError({
79
+ message: String(error)
80
+ });
81
+ }
82
+ return config;
83
+ };
84
+ var ConfigResource = /* @__PURE__ */ Symbol.for("dxos.resource.Config");
85
+ var Config = class {
86
+ _config;
87
+ /**
88
+ * Creates an immutable instance.
89
+ * @constructor
90
+ */
91
+ constructor(config = {}, ...objects) {
92
+ this._config = validateConfig(defaultsDeep(config, ...objects, {
93
+ version: 1
94
+ }));
95
+ }
96
+ /**
97
+ * Returns an immutable config JSON object.
98
+ */
99
+ get values() {
100
+ return this._config;
101
+ }
102
+ /**
103
+ * Returns the given config property.
104
+ *
105
+ * @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
106
+ * @param defaultValue Default value to return if option is not present in the config.
107
+ * @returns The config value or undefined if the option is not present.
108
+ */
109
+ get(key, defaultValue) {
110
+ return getDeep(this._config, key.split(".")) ?? defaultValue;
111
+ }
112
+ /**
113
+ * Get unique key.
114
+ */
115
+ find(path, test) {
116
+ const values = getDeep(this._config, path.split("."));
117
+ if (!Array.isArray(values)) {
118
+ return;
119
+ }
120
+ return values.find((value) => isMatch(value, test));
121
+ }
122
+ /**
123
+ * Returns the given config property or throw if it doesn't exist.
124
+ *
125
+ * @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
126
+ */
127
+ getOrThrow(key) {
128
+ const value = getDeep(this._config, key.split("."));
129
+ if (!value) {
130
+ throw new Error(`Config option not present: ${key}`);
131
+ }
132
+ return value;
133
+ }
134
+ };
135
+ Config = _ts_decorate([
136
+ trace.resource({
137
+ annotation: ConfigResource
138
+ })
139
+ ], Config);
140
+
141
+ export {
142
+ mapFromKeyValues,
143
+ mapToKeyValues,
144
+ validateConfig,
145
+ ConfigResource,
146
+ Config
147
+ };
148
+ //# sourceMappingURL=chunk-WIJROCKQ.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/config.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2021 DXOS.org\n//\n\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 * 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 if (value !== undefined) {\n if (type) {\n switch (type) {\n case 'boolean': {\n value = String(value).toLowerCase() === 'true' || value === '1';\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({ message: 'Version not specified' });\n }\n\n if (config?.version !== 1) {\n throw new InvalidConfigError({ message: `Invalid config version: ${config.version}` });\n }\n\n const error = configRootType.protoType.verify(config);\n if (error) {\n throw new InvalidConfigError({ message: String(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": "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",
6
+ "names": ["defaultsDeep", "isMatch", "InvalidConfigError", "schema", "trace", "getDeep", "setDeep", "configRootType", "getCodecForType", "mapFromKeyValues", "spec", "values", "config", "key", "path", "type", "Object", "entries", "value", "undefined", "String", "toLowerCase", "Number", "JSON", "parse", "Error", "split", "mapToKeyValues", "stringify", "validateConfig", "message", "version", "error", "protoType", "verify", "ConfigResource", "Symbol", "for", "Config", "_config", "objects", "get", "defaultValue", "find", "test", "Array", "isArray", "getOrThrow", "resource", "annotation"]
7
+ }
@@ -1,321 +1,165 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ FILE_DEFAULTS,
4
+ FILE_DYNAMICS,
5
+ FILE_ENVS
6
+ } from "./chunk-T6NFH3CC.mjs";
7
+ import {
8
+ Config,
9
+ ConfigResource,
10
+ mapFromKeyValues,
11
+ mapToKeyValues,
12
+ validateConfig
13
+ } from "./chunk-WIJROCKQ.mjs";
2
14
 
3
- // packages/sdk/config/src/index.ts
15
+ // src/index.ts
4
16
  import * as defs from "@dxos/protocols/proto/dxos/config";
5
17
 
6
- // packages/sdk/config/src/config.ts
7
- import { boolean } from "boolean";
8
- import defaultsDeep from "lodash.defaultsdeep";
9
- import isMatch from "lodash.ismatch";
10
- import { InvalidConfigError } from "@dxos/protocols";
11
- import { schema } from "@dxos/protocols/proto";
12
- import { trace } from "@dxos/tracing";
13
- import { getDeep, setDeep } from "@dxos/util";
14
- function _ts_decorate(decorators, target, key, desc) {
15
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
17
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
18
- return c > 3 && r && Object.defineProperty(target, key, r), r;
19
- }
20
- var configRootType = schema.getCodecForType("dxos.config.Config");
21
- var mapFromKeyValues = (spec, values) => {
22
- const config = {};
23
- for (const [key, { path: path2, type }] of Object.entries(spec)) {
24
- let value = values[key];
25
- if (value !== void 0) {
26
- if (type) {
27
- switch (type) {
28
- case "boolean": {
29
- value = boolean(value);
30
- break;
31
- }
32
- case "number": {
33
- value = Number(value);
34
- break;
35
- }
36
- case "string": {
37
- break;
38
- }
39
- case "json": {
40
- value = value ? JSON.parse(value) : null;
41
- break;
42
- }
43
- default: {
44
- throw new Error(`Invalid type: ${type}`);
45
- }
46
- }
18
+ // src/config-service.ts
19
+ import * as FileSystem from "@effect/platform/FileSystem";
20
+ import * as Context from "effect/Context";
21
+ import * as Effect from "effect/Effect";
22
+ import * as Layer from "effect/Layer";
23
+ import * as Option from "effect/Option";
24
+ import { dirname } from "node:path";
25
+ import * as Yaml from "yaml";
26
+ import { DX_CONFIG, DX_DATA, getProfileConfigPath, getProfilePath } from "@dxos/client-protocol";
27
+ import { invariant } from "@dxos/invariant";
28
+ var __dxlog_file = "/__w/dxos/dxos/packages/sdk/config/src/config-service.ts";
29
+ var memoryConfig = new Config({
30
+ runtime: {
31
+ client: {
32
+ edgeFeatures: {
33
+ echoReplicator: true,
34
+ feedReplicator: true,
35
+ signaling: true,
36
+ agents: true
47
37
  }
48
- setDeep(config, path2.split("."), value);
49
38
  }
50
39
  }
51
- return config;
52
- };
53
- var mapToKeyValues = (spec, values) => {
54
- const config = {};
55
- for (const [key, { path: path2, type }] of Object.entries(spec)) {
56
- const value = getDeep(values, path2.split("."));
57
- if (value !== void 0) {
58
- switch (type) {
59
- case "json":
60
- config[key] = JSON.stringify(value);
61
- break;
62
- default:
63
- config[key] = value;
40
+ });
41
+ var defaultConfig = new Config({
42
+ runtime: {
43
+ client: {
44
+ edgeFeatures: {
45
+ echoReplicator: true,
46
+ feedReplicator: true,
47
+ signaling: true,
48
+ agents: true
49
+ },
50
+ storage: {
51
+ persistent: true
52
+ }
53
+ },
54
+ services: {
55
+ edge: {
56
+ url: "wss://edge-production.dxos.workers.dev/"
57
+ },
58
+ iceProviders: [
59
+ {
60
+ urls: "https://edge-production.dxos.workers.dev/ice"
61
+ }
62
+ ],
63
+ ai: {
64
+ server: "https://ai-service.dxos.workers.dev"
65
+ },
66
+ ipfs: {
67
+ server: "https://api.ipfs.dxos.network/api/v0",
68
+ gateway: "https://gateway.ipfs.dxos.network/ipfs"
64
69
  }
65
70
  }
66
71
  }
67
- return config;
68
- };
69
- var validateConfig = (config) => {
70
- if (!("version" in config)) {
71
- throw new InvalidConfigError("Version not specified");
72
- }
73
- if (config?.version !== 1) {
74
- throw new InvalidConfigError(`Invalid config version: ${config.version}`);
75
- }
76
- const error = configRootType.protoType.verify(config);
77
- if (error) {
78
- throw new InvalidConfigError(error);
79
- }
80
- return config;
81
- };
82
- var ConfigResource = Symbol.for("dxos.resource.Config");
83
- var Config = class {
84
- /**
85
- * Creates an immutable instance.
86
- * @constructor
87
- */
88
- constructor(config = {}, ...objects) {
89
- this._config = validateConfig(defaultsDeep(config, ...objects, {
90
- version: 1
91
- }));
92
- }
93
- /**
94
- * Returns an immutable config JSON object.
95
- */
96
- get values() {
97
- return this._config;
98
- }
99
- /**
100
- * Returns the given config property.
101
- *
102
- * @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
103
- * @param defaultValue Default value to return if option is not present in the config.
104
- * @returns The config value or undefined if the option is not present.
105
- */
106
- get(key, defaultValue) {
107
- return getDeep(this._config, key.split(".")) ?? defaultValue;
108
- }
109
- /**
110
- * Get unique key.
111
- */
112
- find(path2, test) {
113
- const values = getDeep(this._config, path2.split("."));
114
- if (!Array.isArray(values)) {
115
- return;
116
- }
117
- return values.find((value) => isMatch(value, test));
118
- }
119
- /**
120
- * Returns the given config property or throw if it doesn't exist.
121
- *
122
- * @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
123
- */
124
- getOrThrow(key) {
125
- const value = getDeep(this._config, key.split("."));
126
- if (!value) {
127
- throw new Error(`Config option not present: ${key}`);
128
- }
129
- return value;
130
- }
131
- };
132
- Config = _ts_decorate([
133
- trace.resource({
134
- annotation: ConfigResource
135
- })
136
- ], Config);
137
-
138
- // packages/sdk/config/src/loaders/index.ts
139
- import yaml from "js-yaml";
140
- import fs from "node:fs";
141
- import path from "node:path";
142
- import { log } from "@dxos/log";
143
-
144
- // packages/sdk/config/src/types.ts
145
- var FILE_DEFAULTS = "defaults.yml";
146
- var FILE_ENVS = "envs-map.yml";
147
- var FILE_DYNAMICS = "config.yml";
148
-
149
- // packages/sdk/config/src/loaders/index.ts
150
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/config/src/loaders/index.ts";
151
- var DEFAULT_BASE_PATH = path.resolve(process.cwd(), "config");
152
- var maybeLoadFile = (file) => {
153
- try {
154
- return yaml.load(fs.readFileSync(file, {
155
- encoding: "utf8"
156
- }));
157
- } catch (err) {
158
- }
159
- };
160
- var Profile = (profile = "default") => {
161
- const configFile = path.join(process.env.HOME ?? "~", `.config/dx/profile/${profile}.yml`);
162
- return maybeLoadFile(configFile);
163
- };
164
- var Local = () => ({});
165
- var Dynamics = () => ({});
166
- var Envs = (basePath = DEFAULT_BASE_PATH) => {
167
- const content = maybeLoadFile(path.resolve(basePath, FILE_ENVS));
168
- return content ? mapFromKeyValues(content, process.env) : {};
72
+ });
73
+ var ConfigService = class _ConfigService extends Context.Tag("ConfigService")() {
74
+ static layerMemory = Layer.effect(_ConfigService, Effect.succeed(memoryConfig));
75
+ static fromConfig = (config) => Layer.succeed(_ConfigService, config);
76
+ static load = (args) => {
77
+ const defaultConfigPath = getProfileConfigPath(DX_CONFIG, args.profile);
78
+ return Effect.gen(function* () {
79
+ const fs = yield* FileSystem.FileSystem;
80
+ const configPath = Option.getOrElse(args.config, () => defaultConfigPath);
81
+ const configContent = yield* fs.readFileString(configPath);
82
+ const configValues = Yaml.parse(configContent);
83
+ return _ConfigService.of(new Config(configValues, profileBuiltinDefaults(args.profile).values));
84
+ }).pipe(
85
+ // If the config file doesn't exist, create it.
86
+ Effect.catchTag("SystemError", () => Effect.gen(function* () {
87
+ const configValues = defaultConfig.values;
88
+ const fs = yield* FileSystem.FileSystem;
89
+ const pathToCreate = Option.getOrElse(args.config, () => defaultConfigPath);
90
+ yield* fs.makeDirectory(dirname(pathToCreate), {
91
+ recursive: true
92
+ });
93
+ yield* fs.writeFileString(pathToCreate, Yaml.stringify(configValues));
94
+ return _ConfigService.of(new Config(configValues));
95
+ }))
96
+ );
97
+ };
169
98
  };
170
- var Defaults = (basePath = DEFAULT_BASE_PATH) => maybeLoadFile(path.resolve(basePath, FILE_DEFAULTS)) ?? {};
171
- var Storage = async () => ({});
172
- var Remote = (target, authenticationToken) => {
173
- if (!target) {
174
- return {};
175
- }
176
- try {
177
- const url = new URL(target);
178
- const protocol = url.protocol.slice(0, -1);
179
- return {
180
- runtime: {
181
- client: {
182
- // TODO(burdon): Remove vault.html.
183
- remoteSource: url.origin + (protocol.startsWith("http") ? "/vault.html" : ""),
184
- remoteSourceAuthenticationToken: authenticationToken
99
+ var profileBuiltinDefaults = (profile) => {
100
+ invariant(!profile.endsWith(".yml"), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 86, S: void 0, A: ["!profile.endsWith('.yml')", ""] });
101
+ return new Config({
102
+ runtime: {
103
+ client: {
104
+ edgeFeatures: {
105
+ echoReplicator: true,
106
+ feedReplicator: true,
107
+ signaling: true,
108
+ agents: true
109
+ },
110
+ storage: {
111
+ persistent: true,
112
+ dataRoot: getProfilePath(DX_DATA, profile)
185
113
  }
186
114
  }
187
- };
188
- } catch (err) {
189
- log.catch(err, void 0, {
190
- F: __dxlog_file,
191
- L: 87,
192
- S: void 0,
193
- C: (f, a) => f(...a)
194
- });
195
- return {};
196
- }
115
+ }
116
+ });
197
117
  };
198
118
 
199
- // packages/sdk/config/src/savers/index.ts
200
- var SaveConfig = async (_) => {
119
+ // src/index.ts
120
+ export * from "#loaders";
121
+ export * from "#savers";
122
+ export * from "#plugin";
123
+
124
+ // src/telemetry.ts
125
+ import { isNode } from "@dxos/util";
126
+ var resolveTelemetryTag = (config) => {
127
+ return config?.get("runtime.app.env.DX_TELEMETRY_TAG") ?? (isNode() ? process.env.DX_TELEMETRY_TAG : void 0);
201
128
  };
202
129
 
203
- // packages/sdk/config/src/plugin/definitions.ts
204
- import yaml2 from "js-yaml";
205
- import { execSync } from "node:child_process";
206
- import { readFileSync } from "node:fs";
207
- import { resolve } from "node:path";
208
- import pkgUp from "pkg-up";
209
- import { log as log2 } from "@dxos/log";
210
- import { setDeep as setDeep2 } from "@dxos/util";
211
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/config/src/plugin/definitions.ts";
212
- var CWD = process.cwd();
213
- var definitions = ({ configPath, envPath, devPath, mode = process.env.NODE_ENV, publicUrl = "", env }) => {
214
- const KEYS_TO_FILE = {
215
- __CONFIG_DEFAULTS__: configPath?.length ? configPath : resolve(CWD, "dx.yml"),
216
- __CONFIG_ENVS__: envPath?.length ? envPath : resolve(CWD, "dx-env.yml")
217
- };
218
- if (mode !== "production") {
219
- KEYS_TO_FILE.__CONFIG_LOCAL__ = devPath ?? resolve(CWD, "dx-local.yml");
220
- }
221
- return Object.entries(KEYS_TO_FILE).reduce((prev, [key, value]) => {
222
- let content = {};
223
- try {
224
- content = yaml2.load(readFileSync(value, "utf-8"));
225
- if (key === "__CONFIG_ENVS__") {
226
- content = mapFromKeyValues(content, process.env);
227
- }
228
- if (key === "__CONFIG_DEFAULTS__") {
229
- Object.entries(process.env).forEach(([key2, value2]) => {
230
- if (key2.startsWith("DX_") || env?.includes(key2)) {
231
- setDeep2(content, [
232
- "runtime",
233
- "app",
234
- "env",
235
- key2
236
- ], value2);
237
- }
238
- });
239
- try {
240
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
241
- const commitHash = process.env.DX_COMMIT_HASH ?? execSync("git rev-parse --short HEAD", {
242
- encoding: "utf-8"
243
- }).replace("\n", "");
244
- const packagePath = pkgUp.sync();
245
- const packageJson = packagePath && JSON.parse(readFileSync(packagePath, "utf-8"));
246
- setDeep2(content, [
247
- "runtime",
248
- "app",
249
- "build",
250
- "timestamp"
251
- ], timestamp);
252
- setDeep2(content, [
253
- "runtime",
254
- "app",
255
- "build",
256
- "commitHash"
257
- ], commitHash);
258
- setDeep2(content, [
259
- "runtime",
260
- "app",
261
- "build",
262
- "version"
263
- ], packageJson?.version);
264
- } catch {
265
- }
266
- }
267
- } catch (err) {
268
- if (err.message.includes("YAMLException")) {
269
- log2.error(`Failed to parse file ${value}:`, err, {
270
- F: __dxlog_file2,
271
- L: 71,
272
- S: void 0,
273
- C: (f, a) => f(...a)
274
- });
275
- } else {
276
- log2(`Failed to load file ${value}:`, err, {
277
- F: __dxlog_file2,
278
- L: 73,
279
- S: void 0,
280
- C: (f, a) => f(...a)
281
- });
130
+ // src/preset.ts
131
+ import * as Match from "effect/Match";
132
+ var configPreset = ({ edge = "main" } = {}) => new Config({
133
+ version: 1,
134
+ runtime: {
135
+ client: {
136
+ edgeFeatures: {
137
+ signaling: true,
138
+ echoReplicator: true,
139
+ feedReplicator: true
282
140
  }
283
- if (key === "__CONFIG_DEFAULTS__") {
284
- throw new Error(`Failed to load default config file from ${value}`);
141
+ },
142
+ services: {
143
+ edge: {
144
+ url: Match.value(edge).pipe(Match.when("local", () => "http://localhost:8787"), Match.when("dev", () => "https://edge.dxos.workers.dev"), Match.when("main", () => "https://edge-main.dxos.workers.dev"), Match.when("production", () => "https://edge-production.dxos.workers.dev"), Match.exhaustive)
285
145
  }
286
146
  }
287
- return {
288
- ...prev,
289
- [key]: content
290
- };
291
- }, {
292
- __DXOS_CONFIG__: {
293
- dynamic: mode === "production",
294
- publicUrl
295
- },
296
- __CONFIG_DEFAULTS__: {},
297
- __CONFIG_ENVS__: {},
298
- __CONFIG_LOCAL__: {}
299
- });
300
- };
147
+ }
148
+ });
301
149
  export {
302
150
  Config,
303
151
  ConfigResource,
304
- Defaults,
305
- Dynamics,
306
- Envs,
152
+ ConfigService,
307
153
  FILE_DEFAULTS,
308
154
  FILE_DYNAMICS,
309
155
  FILE_ENVS,
310
- Local,
311
- Profile,
312
- Remote,
313
- SaveConfig,
314
- Storage,
315
- definitions,
156
+ configPreset,
157
+ defaultConfig,
316
158
  defs,
317
159
  mapFromKeyValues,
318
160
  mapToKeyValues,
161
+ memoryConfig,
162
+ resolveTelemetryTag,
319
163
  validateConfig
320
164
  };
321
165
  //# sourceMappingURL=index.mjs.map