@dxos/config 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef

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