@dxos/config 0.8.3 → 0.8.4-main.1c7ec43d41
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/dist/lib/browser/chunk-VQRJY4AQ.mjs +11 -0
- package/dist/lib/browser/chunk-VQRJY4AQ.mjs.map +7 -0
- package/dist/lib/browser/chunk-ZY4D4E3Q.mjs +146 -0
- package/dist/lib/browser/chunk-ZY4D4E3Q.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +137 -209
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/loaders/browser.mjs +73 -0
- package/dist/lib/browser/loaders/browser.mjs.map +7 -0
- package/dist/lib/browser/loaders/index.mjs +66 -0
- package/dist/lib/browser/loaders/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/plugin/browser.mjs +8 -0
- package/dist/lib/browser/plugin/browser.mjs.map +7 -0
- package/dist/lib/browser/plugin/index.mjs +98 -0
- package/dist/lib/browser/plugin/index.mjs.map +7 -0
- package/dist/lib/browser/savers/browser.mjs +25 -0
- package/dist/lib/browser/savers/browser.mjs.map +7 -0
- package/dist/lib/browser/savers/index.mjs +7 -0
- package/dist/lib/browser/savers/index.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-T6NFH3CC.mjs +13 -0
- package/dist/lib/node-esm/chunk-T6NFH3CC.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-WIJROCKQ.mjs +148 -0
- package/dist/lib/node-esm/chunk-WIJROCKQ.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +133 -289
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/loaders/browser.mjs +75 -0
- package/dist/lib/node-esm/loaders/browser.mjs.map +7 -0
- package/dist/lib/node-esm/loaders/index.mjs +67 -0
- package/dist/lib/node-esm/loaders/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/plugin/browser.mjs +10 -0
- package/dist/lib/node-esm/plugin/browser.mjs.map +7 -0
- package/dist/lib/node-esm/plugin/index.mjs +99 -0
- package/dist/lib/node-esm/plugin/index.mjs.map +7 -0
- package/dist/lib/node-esm/savers/browser.mjs +27 -0
- package/dist/lib/node-esm/savers/browser.mjs.map +7 -0
- package/dist/lib/node-esm/savers/index.mjs +9 -0
- package/dist/lib/node-esm/savers/index.mjs.map +7 -0
- package/dist/plugin/node-esm/{chunk-PAFU3TJI.mjs → chunk-XCZNYAQT.mjs} +27 -29
- package/dist/plugin/node-esm/chunk-XCZNYAQT.mjs.map +7 -0
- package/dist/plugin/node-esm/esbuild-plugin.mjs +2 -2
- package/dist/plugin/node-esm/meta.json +1 -1
- package/dist/plugin/node-esm/rollup-plugin.mjs +2 -2
- package/dist/plugin/node-esm/vite-plugin.mjs +6 -6
- package/dist/plugin/node-esm/vite-plugin.mjs.map +3 -3
- package/dist/types/src/config-service.d.ts +19 -0
- package/dist/types/src/config-service.d.ts.map +1 -0
- package/dist/types/src/config.d.ts +0 -1
- package/dist/types/src/config.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +6 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/loaders/browser.d.ts +11 -15
- package/dist/types/src/loaders/browser.d.ts.map +1 -1
- package/dist/types/src/loaders/index.d.ts.map +1 -1
- package/dist/types/src/plugin/browser.d.ts +3 -0
- package/dist/types/src/plugin/browser.d.ts.map +1 -0
- package/dist/types/src/plugin/definitions.d.ts +3 -3
- package/dist/types/src/plugin/definitions.d.ts.map +1 -1
- package/dist/types/src/plugin/esbuild-plugin.d.ts.map +1 -1
- package/dist/types/src/plugin/index.d.ts +1 -1
- package/dist/types/src/plugin/index.d.ts.map +1 -1
- package/dist/types/src/plugin/rollup-plugin.d.ts.map +1 -1
- package/dist/types/src/plugin/types.d.ts +4 -0
- package/dist/types/src/plugin/types.d.ts.map +1 -1
- package/dist/types/src/plugin/vite-plugin.d.ts +1 -1
- package/dist/types/src/plugin/vite-plugin.d.ts.map +1 -1
- package/dist/types/src/preset.d.ts +10 -0
- package/dist/types/src/preset.d.ts.map +1 -0
- package/dist/types/src/savers/browser.d.ts +2 -1
- package/dist/types/src/savers/browser.d.ts.map +1 -1
- package/dist/types/src/savers/index.d.ts.map +1 -1
- package/dist/types/src/telemetry.d.ts +17 -0
- package/dist/types/src/telemetry.d.ts.map +1 -0
- package/dist/types/src/testing/defaults.d.ts +6 -6
- package/dist/types/src/testing/defaults.d.ts.map +1 -1
- package/dist/types/src/testing/envs-map.d.ts +12 -14
- package/dist/types/src/testing/envs-map.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +50 -30
- package/src/config-service.ts +116 -0
- package/src/config.ts +4 -7
- package/src/index.ts +6 -3
- package/src/loaders/{browser.js → browser.ts} +15 -9
- package/src/loaders/index.ts +2 -2
- package/src/plugin/browser.ts +11 -0
- package/src/plugin/definitions.ts +6 -6
- package/src/plugin/index.ts +1 -1
- package/src/plugin/types.ts +5 -0
- package/src/plugin/vite-plugin.ts +8 -5
- package/src/preset.ts +40 -0
- package/src/savers/{browser.js → browser.ts} +3 -3
- package/src/telemetry.ts +25 -0
- package/src/testing/build.js +3 -2
- package/README.yml +0 -1
- package/dist/lib/node/index.cjs +0 -357
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/plugin/node/chunk-SLXRWZWZ.cjs +0 -249
- package/dist/plugin/node/chunk-SLXRWZWZ.cjs.map +0 -7
- package/dist/plugin/node/esbuild-plugin.cjs +0 -45
- package/dist/plugin/node/esbuild-plugin.cjs.map +0 -7
- package/dist/plugin/node/meta.json +0 -1
- package/dist/plugin/node/rollup-plugin.cjs +0 -39
- package/dist/plugin/node/rollup-plugin.cjs.map +0 -7
- package/dist/plugin/node/vite-plugin.cjs +0 -50
- package/dist/plugin/node/vite-plugin.cjs.map +0 -7
- package/dist/plugin/node-esm/chunk-PAFU3TJI.mjs.map +0 -7
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/types.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2021 DXOS.org\n//\n\nimport { type Config as ConfigProto } from '@dxos/protocols/proto/dxos/config';\n\nexport const FILE_DEFAULTS = 'defaults.yml';\nexport const FILE_ENVS = 'envs-map.yml';\nexport const FILE_DYNAMICS = 'config.yml';\n\ntype DotPrefix<T extends string> = T extends '' ? '' : `.${T}`;\n\n/**\n * Returns all dot-separated nested keys for an object.\n *\n * Read more: https://stackoverflow.com/a/68404823.\n */\ntype DotNestedKeys<T> = (\n T extends object\n ? {\n [K in Exclude<keyof T, symbol>]: `${K}${DotPrefix<DotNestedKeys<T[K]>>}`;\n }[Exclude<keyof T, symbol>]\n : ''\n) extends infer D\n ? Extract<D, string>\n : never;\n\n/**\n * Parse a dot separated nested key into an array of keys.\n *\n * Example: 'services.signal.server' -> ['services', 'signal', 'server'].\n */\nexport type ParseKey<K extends string> = K extends `${infer L}.${infer Rest}` ? [L, ...ParseKey<Rest>] : [K];\n\n/**\n * Array of types that can act as an object key.\n */\ntype Keys = (keyof any)[];\n\n/**\n * Retrieves a property type in a series of nested objects.\n *\n * Read more: https://stackoverflow.com/a/61648690.\n */\nexport type DeepIndex<T, KS extends Keys, Fail = undefined> = KS extends [infer F, ...infer R]\n ? F extends keyof Exclude<T, undefined>\n ? R extends Keys\n ? DeepIndex<Exclude<T, undefined>[F], R, Fail>\n : Fail\n : Fail\n : T;\n\n/**\n * Any nested dot separated key that can be in config.\n */\n// TODO(egorgripasov): Clean once old config deprecated.\nexport type ConfigKey = DotNestedKeys<ConfigProto>;\n"],
|
|
5
|
+
"mappings": ";AAMO,IAAMA,gBAAgB;AACtB,IAAMC,YAAY;AAClB,IAAMC,gBAAgB;",
|
|
6
|
+
"names": ["FILE_DEFAULTS", "FILE_ENVS", "FILE_DYNAMICS"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import defaultsDeep from "lodash.defaultsdeep";
|
|
3
|
+
import isMatch from "lodash.ismatch";
|
|
4
|
+
import { InvalidConfigError } from "@dxos/protocols";
|
|
5
|
+
import { schema } from "@dxos/protocols/proto";
|
|
6
|
+
import { trace } from "@dxos/tracing";
|
|
7
|
+
import { getDeep, setDeep } from "@dxos/util";
|
|
8
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
9
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
10
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
11
|
+
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;
|
|
12
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
13
|
+
}
|
|
14
|
+
var configRootType = schema.getCodecForType("dxos.config.Config");
|
|
15
|
+
var mapFromKeyValues = (spec, values) => {
|
|
16
|
+
const config = {};
|
|
17
|
+
for (const [key, { path, type }] of Object.entries(spec)) {
|
|
18
|
+
let value = values[key];
|
|
19
|
+
if (value !== void 0) {
|
|
20
|
+
if (type) {
|
|
21
|
+
switch (type) {
|
|
22
|
+
case "boolean": {
|
|
23
|
+
value = String(value).toLowerCase() === "true" || value === "1";
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case "number": {
|
|
27
|
+
value = Number(value);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case "string": {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
case "json": {
|
|
34
|
+
value = value ? JSON.parse(value) : null;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
default: {
|
|
38
|
+
throw new Error(`Invalid type: ${type}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
setDeep(config, path.split("."), value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return config;
|
|
46
|
+
};
|
|
47
|
+
var mapToKeyValues = (spec, values) => {
|
|
48
|
+
const config = {};
|
|
49
|
+
for (const [key, { path, type }] of Object.entries(spec)) {
|
|
50
|
+
const value = getDeep(values, path.split("."));
|
|
51
|
+
if (value !== void 0) {
|
|
52
|
+
switch (type) {
|
|
53
|
+
case "json":
|
|
54
|
+
config[key] = JSON.stringify(value);
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
config[key] = value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return config;
|
|
62
|
+
};
|
|
63
|
+
var validateConfig = (config) => {
|
|
64
|
+
if (!("version" in config)) {
|
|
65
|
+
throw new InvalidConfigError({
|
|
66
|
+
message: "Version not specified"
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (config?.version !== 1) {
|
|
70
|
+
throw new InvalidConfigError({
|
|
71
|
+
message: `Invalid config version: ${config.version}`
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const error = configRootType.protoType.verify(config);
|
|
75
|
+
if (error) {
|
|
76
|
+
throw new InvalidConfigError({
|
|
77
|
+
message: String(error)
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return config;
|
|
81
|
+
};
|
|
82
|
+
var ConfigResource = /* @__PURE__ */ Symbol.for("dxos.resource.Config");
|
|
83
|
+
var Config = class {
|
|
84
|
+
_config;
|
|
85
|
+
/**
|
|
86
|
+
* Creates an immutable instance.
|
|
87
|
+
* @constructor
|
|
88
|
+
*/
|
|
89
|
+
constructor(config = {}, ...objects) {
|
|
90
|
+
this._config = validateConfig(defaultsDeep(config, ...objects, {
|
|
91
|
+
version: 1
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Returns an immutable config JSON object.
|
|
96
|
+
*/
|
|
97
|
+
get values() {
|
|
98
|
+
return this._config;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns the given config property.
|
|
102
|
+
*
|
|
103
|
+
* @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
|
|
104
|
+
* @param defaultValue Default value to return if option is not present in the config.
|
|
105
|
+
* @returns The config value or undefined if the option is not present.
|
|
106
|
+
*/
|
|
107
|
+
get(key, defaultValue) {
|
|
108
|
+
return getDeep(this._config, key.split(".")) ?? defaultValue;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get unique key.
|
|
112
|
+
*/
|
|
113
|
+
find(path, test) {
|
|
114
|
+
const values = getDeep(this._config, path.split("."));
|
|
115
|
+
if (!Array.isArray(values)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
return values.find((value) => isMatch(value, test));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns the given config property or throw if it doesn't exist.
|
|
122
|
+
*
|
|
123
|
+
* @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
|
|
124
|
+
*/
|
|
125
|
+
getOrThrow(key) {
|
|
126
|
+
const value = getDeep(this._config, key.split("."));
|
|
127
|
+
if (!value) {
|
|
128
|
+
throw new Error(`Config option not present: ${key}`);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
Config = _ts_decorate([
|
|
134
|
+
trace.resource({
|
|
135
|
+
annotation: ConfigResource
|
|
136
|
+
})
|
|
137
|
+
], Config);
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
mapFromKeyValues,
|
|
141
|
+
mapToKeyValues,
|
|
142
|
+
validateConfig,
|
|
143
|
+
ConfigResource,
|
|
144
|
+
Config
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=chunk-ZY4D4E3Q.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": ";AAIA,OAAOA,kBAAkB;AACzB,OAAOC,aAAa;AAEp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;QAzDO8B,SAAAA;IAAWC,YAAYd;;;",
|
|
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,236 +1,164 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
FILE_DEFAULTS,
|
|
3
|
+
FILE_DYNAMICS,
|
|
4
|
+
FILE_ENVS
|
|
5
|
+
} from "./chunk-VQRJY4AQ.mjs";
|
|
6
|
+
import {
|
|
7
|
+
Config,
|
|
8
|
+
ConfigResource,
|
|
9
|
+
mapFromKeyValues,
|
|
10
|
+
mapToKeyValues,
|
|
11
|
+
validateConfig
|
|
12
|
+
} from "./chunk-ZY4D4E3Q.mjs";
|
|
13
|
+
|
|
14
|
+
// src/index.ts
|
|
2
15
|
import * as defs from "@dxos/protocols/proto/dxos/config";
|
|
3
16
|
|
|
4
|
-
//
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (value !== void 0) {
|
|
24
|
-
if (type) {
|
|
25
|
-
switch (type) {
|
|
26
|
-
case "boolean": {
|
|
27
|
-
value = boolean(value);
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
case "number": {
|
|
31
|
-
value = Number(value);
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
case "string": {
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
case "json": {
|
|
38
|
-
value = value ? JSON.parse(value) : null;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
default: {
|
|
42
|
-
throw new Error(`Invalid type: ${type}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
17
|
+
// src/config-service.ts
|
|
18
|
+
import * as FileSystem from "@effect/platform/FileSystem";
|
|
19
|
+
import * as Context from "effect/Context";
|
|
20
|
+
import * as Effect from "effect/Effect";
|
|
21
|
+
import * as Layer from "effect/Layer";
|
|
22
|
+
import * as Option from "effect/Option";
|
|
23
|
+
import { dirname } from "@dxos/node-std/path";
|
|
24
|
+
import * as Yaml from "yaml";
|
|
25
|
+
import { DX_CONFIG, DX_DATA, getProfileConfigPath, getProfilePath } from "@dxos/client-protocol";
|
|
26
|
+
import { invariant } from "@dxos/invariant";
|
|
27
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/sdk/config/src/config-service.ts";
|
|
28
|
+
var memoryConfig = new Config({
|
|
29
|
+
runtime: {
|
|
30
|
+
client: {
|
|
31
|
+
edgeFeatures: {
|
|
32
|
+
echoReplicator: true,
|
|
33
|
+
feedReplicator: true,
|
|
34
|
+
signaling: true,
|
|
35
|
+
agents: true
|
|
45
36
|
}
|
|
46
|
-
setDeep(config, path.split("."), value);
|
|
47
37
|
}
|
|
48
38
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
39
|
+
});
|
|
40
|
+
var defaultConfig = new Config({
|
|
41
|
+
runtime: {
|
|
42
|
+
client: {
|
|
43
|
+
edgeFeatures: {
|
|
44
|
+
echoReplicator: true,
|
|
45
|
+
feedReplicator: true,
|
|
46
|
+
signaling: true,
|
|
47
|
+
agents: true
|
|
48
|
+
},
|
|
49
|
+
storage: {
|
|
50
|
+
persistent: true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
services: {
|
|
54
|
+
edge: {
|
|
55
|
+
url: "wss://edge-production.dxos.workers.dev/"
|
|
56
|
+
},
|
|
57
|
+
iceProviders: [
|
|
58
|
+
{
|
|
59
|
+
urls: "https://edge-production.dxos.workers.dev/ice"
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
ai: {
|
|
63
|
+
server: "https://ai-service.dxos.workers.dev"
|
|
64
|
+
},
|
|
65
|
+
ipfs: {
|
|
66
|
+
server: "https://api.ipfs.dxos.network/api/v0",
|
|
67
|
+
gateway: "https://gateway.ipfs.dxos.network/ipfs"
|
|
62
68
|
}
|
|
63
69
|
}
|
|
64
70
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Returns an immutable config JSON object.
|
|
93
|
-
*/
|
|
94
|
-
get values() {
|
|
95
|
-
return this._config;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Returns the given config property.
|
|
99
|
-
*
|
|
100
|
-
* @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
|
|
101
|
-
* @param defaultValue Default value to return if option is not present in the config.
|
|
102
|
-
* @returns The config value or undefined if the option is not present.
|
|
103
|
-
*/
|
|
104
|
-
get(key, defaultValue) {
|
|
105
|
-
return getDeep(this._config, key.split(".")) ?? defaultValue;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Get unique key.
|
|
109
|
-
*/
|
|
110
|
-
find(path, test) {
|
|
111
|
-
const values = getDeep(this._config, path.split("."));
|
|
112
|
-
if (!Array.isArray(values)) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
return values.find((value) => isMatch(value, test));
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Returns the given config property or throw if it doesn't exist.
|
|
119
|
-
*
|
|
120
|
-
* @param key A key in the config object. Can be a nested property with keys separated by dots: 'services.signal.server'.
|
|
121
|
-
*/
|
|
122
|
-
getOrThrow(key) {
|
|
123
|
-
const value = getDeep(this._config, key.split("."));
|
|
124
|
-
if (!value) {
|
|
125
|
-
throw new Error(`Config option not present: ${key}`);
|
|
126
|
-
}
|
|
127
|
-
return value;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
Config = _ts_decorate([
|
|
131
|
-
trace.resource({
|
|
132
|
-
annotation: ConfigResource
|
|
133
|
-
})
|
|
134
|
-
], Config);
|
|
135
|
-
|
|
136
|
-
// packages/sdk/config/src/loaders/browser.js
|
|
137
|
-
import localforage from "localforage";
|
|
138
|
-
import { log } from "@dxos/log";
|
|
139
|
-
var CONFIG_ENDPOINT = "/.well-known/dx/config";
|
|
140
|
-
var Local = () => {
|
|
141
|
-
return typeof __CONFIG_LOCAL__ !== "undefined" ? __CONFIG_LOCAL__ : {};
|
|
142
|
-
};
|
|
143
|
-
var Dynamics = async () => {
|
|
144
|
-
const { publicUrl = "", dynamic } = __DXOS_CONFIG__;
|
|
145
|
-
if (!dynamic) {
|
|
146
|
-
log("dynamics disabled");
|
|
147
|
-
return {};
|
|
148
|
-
}
|
|
149
|
-
log("fetching config...", { publicUrl });
|
|
150
|
-
return await fetch(`${publicUrl}${CONFIG_ENDPOINT}`).then((res) => res.json()).catch((error) => {
|
|
151
|
-
log.warn("Failed to fetch dynamic config.", error);
|
|
152
|
-
return {};
|
|
153
|
-
});
|
|
154
|
-
};
|
|
155
|
-
var Envs = () => {
|
|
156
|
-
return typeof __CONFIG_ENVS__ !== "undefined" ? __CONFIG_ENVS__ : {};
|
|
157
|
-
};
|
|
158
|
-
var Defaults = () => {
|
|
159
|
-
return typeof __CONFIG_DEFAULTS__ !== "undefined" ? __CONFIG_DEFAULTS__ : {};
|
|
160
|
-
};
|
|
161
|
-
var Storage = async () => {
|
|
162
|
-
try {
|
|
163
|
-
const config = await localforage.getItem("dxos.org/settings/config");
|
|
164
|
-
if (config) {
|
|
165
|
-
return config;
|
|
166
|
-
}
|
|
167
|
-
} catch (err) {
|
|
168
|
-
log.warn("Failed to load config", { err });
|
|
169
|
-
}
|
|
170
|
-
return {};
|
|
71
|
+
});
|
|
72
|
+
var ConfigService = class _ConfigService extends Context.Tag("ConfigService")() {
|
|
73
|
+
static layerMemory = Layer.effect(_ConfigService, Effect.succeed(memoryConfig));
|
|
74
|
+
static fromConfig = (config) => Layer.succeed(_ConfigService, config);
|
|
75
|
+
static load = (args) => {
|
|
76
|
+
const defaultConfigPath = getProfileConfigPath(DX_CONFIG, args.profile);
|
|
77
|
+
return Effect.gen(function* () {
|
|
78
|
+
const fs = yield* FileSystem.FileSystem;
|
|
79
|
+
const configPath = Option.getOrElse(args.config, () => defaultConfigPath);
|
|
80
|
+
const configContent = yield* fs.readFileString(configPath);
|
|
81
|
+
const configValues = Yaml.parse(configContent);
|
|
82
|
+
return _ConfigService.of(new Config(configValues, profileBuiltinDefaults(args.profile).values));
|
|
83
|
+
}).pipe(
|
|
84
|
+
// If the config file doesn't exist, create it.
|
|
85
|
+
Effect.catchTag("SystemError", () => Effect.gen(function* () {
|
|
86
|
+
const configValues = defaultConfig.values;
|
|
87
|
+
const fs = yield* FileSystem.FileSystem;
|
|
88
|
+
const pathToCreate = Option.getOrElse(args.config, () => defaultConfigPath);
|
|
89
|
+
yield* fs.makeDirectory(dirname(pathToCreate), {
|
|
90
|
+
recursive: true
|
|
91
|
+
});
|
|
92
|
+
yield* fs.writeFileString(pathToCreate, Yaml.stringify(configValues));
|
|
93
|
+
return _ConfigService.of(new Config(configValues));
|
|
94
|
+
}))
|
|
95
|
+
);
|
|
96
|
+
};
|
|
171
97
|
};
|
|
172
|
-
var
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
98
|
+
var profileBuiltinDefaults = (profile) => {
|
|
99
|
+
invariant(!profile.endsWith(".yml"), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 86, S: void 0, A: ["!profile.endsWith('.yml')", ""] });
|
|
100
|
+
return new Config({
|
|
101
|
+
runtime: {
|
|
102
|
+
client: {
|
|
103
|
+
edgeFeatures: {
|
|
104
|
+
echoReplicator: true,
|
|
105
|
+
feedReplicator: true,
|
|
106
|
+
signaling: true,
|
|
107
|
+
agents: true
|
|
108
|
+
},
|
|
109
|
+
storage: {
|
|
110
|
+
persistent: true,
|
|
111
|
+
dataRoot: getProfilePath(DX_DATA, profile)
|
|
185
112
|
}
|
|
186
113
|
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
log.catch(err);
|
|
190
|
-
return {};
|
|
191
|
-
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
192
116
|
};
|
|
193
117
|
|
|
194
|
-
//
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
PERFORMING_CONFIG_SAVE = true;
|
|
204
|
-
try {
|
|
205
|
-
await localforage2.setItem("dxos.org/settings/config", config);
|
|
206
|
-
} catch (err) {
|
|
207
|
-
log2.warn("Failed to save config", { err });
|
|
208
|
-
return {};
|
|
209
|
-
} finally {
|
|
210
|
-
PERFORMING_CONFIG_SAVE = false;
|
|
211
|
-
}
|
|
118
|
+
// src/index.ts
|
|
119
|
+
export * from "#loaders";
|
|
120
|
+
export * from "#savers";
|
|
121
|
+
export * from "#plugin";
|
|
122
|
+
|
|
123
|
+
// src/telemetry.ts
|
|
124
|
+
import { isNode } from "@dxos/util";
|
|
125
|
+
var resolveTelemetryTag = (config) => {
|
|
126
|
+
return config?.get("runtime.app.env.DX_TELEMETRY_TAG") ?? (isNode() ? process.env.DX_TELEMETRY_TAG : void 0);
|
|
212
127
|
};
|
|
213
128
|
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
var
|
|
217
|
-
|
|
129
|
+
// src/preset.ts
|
|
130
|
+
import * as Match from "effect/Match";
|
|
131
|
+
var configPreset = ({ edge = "main" } = {}) => new Config({
|
|
132
|
+
version: 1,
|
|
133
|
+
runtime: {
|
|
134
|
+
client: {
|
|
135
|
+
edgeFeatures: {
|
|
136
|
+
signaling: true,
|
|
137
|
+
echoReplicator: true,
|
|
138
|
+
feedReplicator: true
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
services: {
|
|
142
|
+
edge: {
|
|
143
|
+
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)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
218
148
|
export {
|
|
219
149
|
Config,
|
|
220
150
|
ConfigResource,
|
|
221
|
-
|
|
222
|
-
Dynamics,
|
|
223
|
-
Envs,
|
|
151
|
+
ConfigService,
|
|
224
152
|
FILE_DEFAULTS,
|
|
225
153
|
FILE_DYNAMICS,
|
|
226
154
|
FILE_ENVS,
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
SaveConfig,
|
|
230
|
-
Storage,
|
|
155
|
+
configPreset,
|
|
156
|
+
defaultConfig,
|
|
231
157
|
defs,
|
|
232
158
|
mapFromKeyValues,
|
|
233
159
|
mapToKeyValues,
|
|
160
|
+
memoryConfig,
|
|
161
|
+
resolveTelemetryTag,
|
|
234
162
|
validateConfig
|
|
235
163
|
};
|
|
236
164
|
//# sourceMappingURL=index.mjs.map
|