@yolean/evidence-sdk 0.0.0-stage → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/CHANGELOG.md +538 -0
  2. package/LICENSE.md +9 -0
  3. package/README.md +237 -2
  4. package/package.json +161 -3
  5. package/src/build-dev/cli/add/addServerHook.js +30 -0
  6. package/src/build-dev/cli/add/addTypeRef.js +19 -0
  7. package/src/build-dev/cli/add/updateGitIgnore.js +33 -0
  8. package/src/build-dev/cli/add/updatePackageJson.js +26 -0
  9. package/src/build-dev/cli/add/updateSvelteConfig.js +199 -0
  10. package/src/build-dev/cli/add/updateViteConfig.js +152 -0
  11. package/src/build-dev/cli/add.js +95 -0
  12. package/src/build-dev/cli/build.js +30 -0
  13. package/src/build-dev/cli/dev.js +49 -0
  14. package/src/build-dev/cli/index.js +14 -0
  15. package/src/build-dev/cli/upgrade.js +54 -0
  16. package/src/build-dev/svelte/index.js +54 -0
  17. package/src/build-dev/svelte/lib/isTargetFile.js +6 -0
  18. package/src/build-dev/svelte/metadatas.js +39 -0
  19. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js +29 -0
  20. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.spec.js +82 -0
  21. package/src/build-dev/svelte/processors/addScriptTags.js +17 -0
  22. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.js +19 -0
  23. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.spec.js +123 -0
  24. package/src/build-dev/svelte/processors/convertToHtml/processMarkdown.js +59 -0
  25. package/src/build-dev/svelte/processors/extractQueries/extractQueries.js +49 -0
  26. package/src/build-dev/svelte/processors/extractQueries/extractQueries.spec.js +54 -0
  27. package/src/build-dev/svelte/processors/highlight/highlight.js +52 -0
  28. package/src/build-dev/svelte/processors/highlight/highlight.spec.js +31 -0
  29. package/src/build-dev/svelte/processors/initializeUniversalSql.js +15 -0
  30. package/src/build-dev/svelte/processors/injectComponents/injectComponents.js +56 -0
  31. package/src/build-dev/svelte/processors/injectHighlightStyles.js +14 -0
  32. package/src/build-dev/svelte/processors/injectQueries/injectQueries.js +77 -0
  33. package/src/build-dev/svelte/processors/markRootLayoutPrerenderable.js +56 -0
  34. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.js +17 -0
  35. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.spec.js +21 -0
  36. package/src/build-dev/svelte/processors/transformQueries/transformQueries.js +58 -0
  37. package/src/build-dev/vite/config-virtual.js +24 -0
  38. package/src/build-dev/vite/constants.js +7 -0
  39. package/src/build-dev/vite/datasource-plugins/source-query-hmr.js +117 -0
  40. package/src/build-dev/vite/evidence-config.d.ts +6 -0
  41. package/src/build-dev/vite/index.js +214 -0
  42. package/src/build-dev/vite/layout-plugins/copy-to-layout.js +71 -0
  43. package/src/build-dev/vite/plugins.js +4 -0
  44. package/src/build-dev/vite/query-directory-hmr.js +32 -0
  45. package/src/build-dev/vite/sidecar-app.js +79 -0
  46. package/src/build-dev/vite/virtuals/QuerySSR.svelte +13 -0
  47. package/src/build-dev/vite/virtuals/__tests/proxyStack.spec.js +52 -0
  48. package/src/build-dev/vite/virtuals/bootstrapContexts.svelte.js +27 -0
  49. package/src/build-dev/vite/virtuals/browser/static-assets.js +3 -0
  50. package/src/build-dev/vite/virtuals/contextKeys.js +3 -0
  51. package/src/build-dev/vite/virtuals/initUsql.js +23 -0
  52. package/src/build-dev/vite/virtuals/node/static-assets.js +56 -0
  53. package/src/build-dev/vite/virtuals/proxyStack.js +37 -0
  54. package/src/build-dev/vite/virtuals/queries.svelte.js +150 -0
  55. package/src/build-dev/vite/virtuals/ssrHook.svelte.js +60 -0
  56. package/src/build-dev/vite/virtuals/updateQueryContext.svelte.js +31 -0
  57. package/src/build-dev/vite/virtuals-dynamic.js +12 -0
  58. package/src/cli.js +5 -0
  59. package/src/cli.root.js +90 -0
  60. package/src/configuration/cli/create.js +20 -0
  61. package/src/configuration/cli/index.js +12 -0
  62. package/src/configuration/createEvidenceConfig.js +51 -0
  63. package/src/configuration/getEvidenceConfig.js +60 -0
  64. package/src/configuration/getEvidenceConfig.legacy.js +27 -0
  65. package/src/configuration/index.js +1 -0
  66. package/src/configuration/schemas/config.schema.js +11 -0
  67. package/src/configuration/schemas/deployment.schema.js +9 -0
  68. package/src/configuration/schemas/deployment.schema.spec.js +34 -0
  69. package/src/configuration/updateEvidenceConfig.js +25 -0
  70. package/src/legacy-compat.js +8 -0
  71. package/src/lib/EvidenceError.js +35 -0
  72. package/src/lib/b64Deep.js +55 -0
  73. package/src/lib/check-import.js +12 -0
  74. package/src/lib/cleanZodErrors.js +22 -0
  75. package/src/lib/cli/cmdFail.js +33 -0
  76. package/src/lib/cli/notices.js +44 -0
  77. package/src/lib/debug.js +40 -0
  78. package/src/lib/flows.d.ts +21 -0
  79. package/src/lib/forceLink.js +28 -0
  80. package/src/lib/history/index.js +70 -0
  81. package/src/lib/history/types.d.ts +8 -0
  82. package/src/lib/pkg-types.helpers.js +16 -0
  83. package/src/lib/processing-queue.js +96 -0
  84. package/src/lib/processing-queue.spec.js +148 -0
  85. package/src/lib/projectPaths.js +29 -0
  86. package/src/lib/projectRoot.js +12 -0
  87. package/src/lib/sharedPromise.js +77 -0
  88. package/src/lib/sharedPromise.spec.js +23 -0
  89. package/src/lib/strict.js +19 -0
  90. package/src/lib/tests/fs.js +21 -0
  91. package/src/lib/tokens.js +7 -0
  92. package/src/lib/unnest-zod-error.js +14 -0
  93. package/src/logger/EvidenceLogger.js +164 -0
  94. package/src/logger/index.js +4 -0
  95. package/src/meta.js +8 -0
  96. package/src/plugins/cli/index.js +13 -0
  97. package/src/plugins/cli/install.js +26 -0
  98. package/src/plugins/cli/install.prompt.js +121 -0
  99. package/src/plugins/components/getAllComponents.js +82 -0
  100. package/src/plugins/components/getComponentsInPlugin.js +33 -0
  101. package/src/plugins/components/loadComponentPlugins.js +48 -0
  102. package/src/plugins/components/loaders/file-loader.js +125 -0
  103. package/src/plugins/components/loaders/manifest-loader.js +53 -0
  104. package/src/plugins/components/schemas/component-manifest.schema.js +5 -0
  105. package/src/plugins/components/types.d.ts +21 -0
  106. package/src/plugins/components/validateOverrides.js +30 -0
  107. package/src/plugins/components/validateOverrides.spec.js +32 -0
  108. package/src/plugins/datasources/Datasources.js +85 -0
  109. package/src/plugins/datasources/Datasources.spec.js +78 -0
  110. package/src/plugins/datasources/SourceResultCache.js +62 -0
  111. package/src/plugins/datasources/buildSourceDirectoryProxy.js +21 -0
  112. package/src/plugins/datasources/cli/edit/Options.fixtures.js +69 -0
  113. package/src/plugins/datasources/cli/edit/Options.js +253 -0
  114. package/src/plugins/datasources/cli/edit/Options.spec.js +496 -0
  115. package/src/plugins/datasources/cli/edit/editSourceConfig.js +201 -0
  116. package/src/plugins/datasources/cli/edit.js +20 -0
  117. package/src/plugins/datasources/cli/edit.prompt.js +100 -0
  118. package/src/plugins/datasources/cli/index.js +18 -0
  119. package/src/plugins/datasources/cli/sources.js +77 -0
  120. package/src/plugins/datasources/evalSources.js +257 -0
  121. package/src/plugins/datasources/getPublishedDatasources.js +62 -0
  122. package/src/plugins/datasources/index.js +6 -0
  123. package/src/plugins/datasources/loadSourceConfig.js +147 -0
  124. package/src/plugins/datasources/loadSourceConfig.spec.js +127 -0
  125. package/src/plugins/datasources/loadSourcePlugins.js +49 -0
  126. package/src/plugins/datasources/loadSources.js +48 -0
  127. package/src/plugins/datasources/schemas/datasource.schema.js +52 -0
  128. package/src/plugins/datasources/schemas/datasourceCache.schema.js +3 -0
  129. package/src/plugins/datasources/schemas/datasourcePlugin.schema.js +37 -0
  130. package/src/plugins/datasources/schemas/datasourcePluginOptions.schema.js +64 -0
  131. package/src/plugins/datasources/schemas/index.js +2 -0
  132. package/src/plugins/datasources/schemas/manifest.schema.js +8 -0
  133. package/src/plugins/datasources/schemas/results.schema.js +126 -0
  134. package/src/plugins/datasources/sub-source-vars.js +55 -0
  135. package/src/plugins/datasources/sub-source-vars.spec.js +46 -0
  136. package/src/plugins/datasources/types.d.ts +53 -0
  137. package/src/plugins/datasources/updateManifest.js +115 -0
  138. package/src/plugins/datasources/updateManifest.spec.js +200 -0
  139. package/src/plugins/datasources/wrapSimpleConnector.js +95 -0
  140. package/src/plugins/datasources/writeSourceConfig.js +60 -0
  141. package/src/plugins/index.js +2 -0
  142. package/src/plugins/layouts/copyMethods/index.js +10 -0
  143. package/src/plugins/layouts/copyMethods/svelte.js +53 -0
  144. package/src/plugins/layouts/copyMethods/sveltekit.js +75 -0
  145. package/src/plugins/layouts/copyToLayout.js +106 -0
  146. package/src/plugins/layouts/loadLayoutPlugin.js +23 -0
  147. package/src/plugins/loadPluginPackage.js +77 -0
  148. package/src/plugins/schemas/index.js +1 -0
  149. package/src/plugins/schemas/plugin-config.schema.js +41 -0
  150. package/src/plugins/schemas/plugin-config.schema.spec.js +21 -0
  151. package/src/plugins/schemas/plugin-package.schema.js +154 -0
  152. package/src/plugins/schemas/plugin-package.schema.spec.js +30 -0
  153. package/src/query-builder.d.ts +1 -0
  154. package/src/query-builder.js +1 -0
  155. package/src/types/duckdb-wellknown.d.ts +35 -0
  156. package/src/types/html-attributes.ts +13 -0
  157. package/src/types/index.d.ts +18 -0
  158. package/src/types/mosaic-sql.d.ts +27 -0
  159. package/src/types/sveltekit-autoimport.d.ts +13 -0
  160. package/src/types/virtuals.d.ts +113 -0
  161. package/src/types.d.ts +1 -0
  162. package/src/usql/index.d.ts +4 -0
  163. package/src/usql/index.js +3 -0
  164. package/src/usql/query/Query.js +1391 -0
  165. package/src/usql/query/Query.spec.js +827 -0
  166. package/src/usql/query/queryScore.js +92 -0
  167. package/src/usql/query/queryScore.spec.js +26 -0
  168. package/src/usql/query/sterilizeQuery.js +89 -0
  169. package/src/usql/query/sterilizeQuery.spec.js +172 -0
  170. package/src/usql/setTrackProxy/setTrackProxy.js +80 -0
  171. package/src/usql/setTrackProxy/setTrackProxy.spec.js +166 -0
  172. package/src/usql/types.ts +86 -0
  173. package/src/usql/utilities/duckdbSerialize.js +29 -0
  174. package/src/usql/utilities/index.js +2 -0
  175. package/src/usql/utilities/resolveMaybePromise.js +30 -0
  176. package/src/utils/batchUp.js +22 -0
  177. package/src/utils/batchUp.spec.js +69 -0
  178. package/src/utils/browserDebounce.js +16 -0
  179. package/src/utils/index.js +7 -0
  180. package/src/utils/svelte/addBasePath.js +45 -0
  181. package/src/utils/svelte/addBasePath.spec.js +86 -0
  182. package/src/utils/svelte/index.js +5 -0
  183. package/src/utils/svelte/inputs.js +138 -0
  184. package/tsconfig.json +22 -0
  185. package/vite.config.js +3 -0
  186. package/vitest.config.js +15 -0
@@ -0,0 +1,78 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { Datasources } from './Datasources.js';
3
+
4
+ describe('Datasources', () => {
5
+ it('should be a class', () => {
6
+ expect(new Datasources()).toBeDefined();
7
+ });
8
+
9
+ it('should throw if an invalid package is given', () => {
10
+ const datasources = new Datasources();
11
+ const pack = { evidence: { datasources: [] }, name: 'test-package' };
12
+
13
+ expect(() => datasources.add(pack, {})).toThrowError();
14
+ });
15
+
16
+ it('should add a datasource', () => {
17
+ const datasources = new Datasources();
18
+ const pack = { evidence: { datasources: ['ddb'] }, name: 'test-package' };
19
+ const source = {};
20
+
21
+ datasources.add(pack, source);
22
+ let [_pack, _source] = datasources.getByPackageName(pack.name);
23
+
24
+ expect(_pack).toBe(pack);
25
+ expect(_source).toBe(source);
26
+
27
+ [_pack, _source] = datasources.getBySource('ddb');
28
+ expect(_pack).toBe(pack);
29
+ expect(_source).toBe(source);
30
+ });
31
+
32
+ it('should add a datasource', () => {
33
+ const datasources = new Datasources();
34
+ const pack = { evidence: { datasources: ['ddb'] }, name: 'test-package' };
35
+ const source = {};
36
+
37
+ datasources.add(pack, source);
38
+ const result = datasources.getByPackageName(pack.name);
39
+ expect(result).toBeDefined();
40
+ expect(result[0]).toBe(pack);
41
+ expect(result[1]).toBe(source);
42
+ });
43
+
44
+ describe('overrides', () => {
45
+ it('should always return the override package', () => {
46
+ const datasources = new Datasources();
47
+ const pack = { evidence: { datasources: ['ddb'] }, name: 'test-package' };
48
+ const pack2 = { evidence: { datasources: ['ddb'] }, name: 'test-package-2' };
49
+
50
+ const source1 = { name: 1 };
51
+ const source2 = { name: 2 };
52
+ datasources.add(pack, source1, ['ddb']);
53
+ datasources.add(pack2, source2);
54
+
55
+ const result = datasources.getByPackageName(pack.name);
56
+ expect(result).toBeDefined();
57
+ expect(result[0]).toBe(pack);
58
+ expect(result[1]).toBe(source1);
59
+ });
60
+ it('should throw if a source is overriden twice', () => {
61
+ const datasources = new Datasources();
62
+ const pack = { evidence: { datasources: ['ddb'] }, name: 'test-package' };
63
+ const pack2 = { evidence: { datasources: ['ddb'] }, name: 'test-package-2' };
64
+
65
+ const source1 = { name: 1 };
66
+ const source2 = { name: 2 };
67
+ datasources.add(pack, source1, ['ddb']);
68
+ // Adding pack2 throws
69
+ expect(() => datasources.add(pack2, source2, ['ddb'])).toThrowError();
70
+
71
+ // pack is still accessible
72
+ const result = datasources.getByPackageName(pack.name);
73
+ expect(result).toBeDefined();
74
+ expect(result[0]).toBe(pack);
75
+ expect(result[1]).toBe(source1);
76
+ });
77
+ });
78
+ });
@@ -0,0 +1,62 @@
1
+ import { createHash } from 'crypto';
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
+ import { DatasourceCacheSchema } from './schemas/datasourceCache.schema.js';
5
+ import merge from 'lodash.merge';
6
+
7
+ /**
8
+ * hashes[source][query] = contentHash
9
+ * @type {Record<string, Record<string, string>>}
10
+ */
11
+ const knownQueryHashes = {};
12
+
13
+ /**
14
+ * @param {string} metaDir
15
+ */
16
+ export const loadCache = async (metaDir) => {
17
+ const cachePath = path.join(metaDir, 'query-cache');
18
+ await fs.mkdir(cachePath, { recursive: true });
19
+ const cacheFile = path.join(cachePath, 'hashes.json');
20
+ if (!(await fs.readdir(cachePath)).includes('hashes.json')) return;
21
+
22
+ const hashes = DatasourceCacheSchema.parse(JSON.parse(await fs.readFile(cacheFile, 'utf-8')));
23
+ Object.assign(knownQueryHashes, merge(knownQueryHashes, hashes));
24
+ };
25
+
26
+ /**
27
+ * @param {string} metaDir
28
+ */
29
+ export const flushCache = async (metaDir) => {
30
+ const cachePath = path.join(metaDir, 'query-cache/hashes.json');
31
+ await fs.writeFile(cachePath, JSON.stringify(knownQueryHashes));
32
+ };
33
+
34
+ /**
35
+ * @param {string} str
36
+ * @returns {string}
37
+ */
38
+ export const hash = (str) => createHash('md5').update(str).digest('hex');
39
+
40
+ /**
41
+ *
42
+ * @param {string} sourceName
43
+ * @param {string} queryName
44
+ * @param {string} content
45
+ */
46
+ export const addToCache = (sourceName, queryName, content) => {
47
+ const contentHash = hash(content);
48
+ if (!knownQueryHashes[sourceName]) knownQueryHashes[sourceName] = {};
49
+ knownQueryHashes[sourceName][queryName] = contentHash;
50
+ };
51
+
52
+ /**
53
+ *
54
+ * @param {string} sourceName
55
+ * @param {string} queryName
56
+ * @param {string} content
57
+ * @returns {boolean}
58
+ */
59
+ export const checkCache = (sourceName, queryName, content) => {
60
+ const contentHash = hash(content);
61
+ return knownQueryHashes?.[sourceName]?.[queryName] === contentHash;
62
+ };
@@ -0,0 +1,21 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+
4
+ /**
5
+ * @param {string} directory
6
+ * @returns {Promise<import("./types.js").SourceDirectory>}
7
+ */
8
+ export const buildSourceDirectoryProxy = async (directory) => {
9
+ /** @type {import("./types.js").SourceDirectory} */
10
+ const output = {};
11
+
12
+ for (const f of await fs.readdir(directory, { withFileTypes: true })) {
13
+ if (f.isDirectory()) {
14
+ output[f.name] = await buildSourceDirectoryProxy(path.join(directory, f.name));
15
+ } else {
16
+ output[f.name] = () => fs.readFile(path.join(directory, f.name), { encoding: 'utf-8' });
17
+ }
18
+ }
19
+
20
+ return output;
21
+ };
@@ -0,0 +1,69 @@
1
+ export const bigQuerySchema = {
2
+ project_id: {
3
+ title: 'Project ID',
4
+ type: 'string',
5
+ secret: true,
6
+ required: true,
7
+ references: '$.keyfile.project_id',
8
+ forceReference: false
9
+ },
10
+ authenticator: {
11
+ title: 'Authentication Method',
12
+ type: 'select',
13
+ secret: false,
14
+ nest: false,
15
+ required: true,
16
+ default: 'service-account',
17
+ options: [
18
+ {
19
+ value: 'service-account',
20
+ label: 'Service Account'
21
+ },
22
+ {
23
+ value: 'gcloud-cli',
24
+ label: 'GCloud CLI'
25
+ },
26
+ {
27
+ value: 'oauth',
28
+ label: 'OAuth Access Token'
29
+ }
30
+ ],
31
+ children: {
32
+ 'service-account': {
33
+ keyfile: {
34
+ title: 'Credentials File',
35
+ type: 'file',
36
+ fileFormat: 'json',
37
+ virtual: true
38
+ },
39
+ client_email: {
40
+ title: 'Client Email',
41
+ type: 'string',
42
+ secret: true,
43
+ required: true,
44
+ references: '$.keyfile.client_email',
45
+ forceReference: true
46
+ },
47
+ private_key: {
48
+ title: 'Private Key',
49
+ type: 'string',
50
+ secret: true,
51
+ required: true,
52
+ references: '$.keyfile.private_key',
53
+ forceReference: true
54
+ }
55
+ },
56
+ 'gcloud-cli': {
57
+ /* no-op; only needs projectId */
58
+ },
59
+ oauth: {
60
+ token: {
61
+ type: 'string',
62
+ title: 'Token',
63
+ secret: true,
64
+ required: true
65
+ }
66
+ }
67
+ }
68
+ }
69
+ };
@@ -0,0 +1,253 @@
1
+ import { EvidenceError } from '../../../../lib/EvidenceError.js';
2
+
3
+ // Symbols are used here to avoid conflicting with a potential connector option name
4
+ export const OptionDebug = Symbol();
5
+ export const OptionSpecMode = Symbol();
6
+ export const OptionSecretMode = Symbol();
7
+ export const OptionSafeMode = Symbol();
8
+ export const OptionParentMode = Symbol();
9
+ export const OptionGetSpec = Symbol();
10
+ export const IsOptions = Symbol();
11
+
12
+ /**
13
+ * @param {import('../../schemas/datasourcePluginOptions.schema.js').IDatasourceOptionSpec} fieldSpec
14
+ * @param {string} prop
15
+ * @param {*} sourceOptions
16
+ * @param {OptionsOpts} [opts]
17
+ * @returns
18
+ */
19
+ const resolveChildOptions = (fieldSpec, prop, sourceOptions, opts) => {
20
+ if (!fieldSpec.children)
21
+ throw new EvidenceError('Internal Error', [
22
+ 'Attempted to access nested value in field with no children'
23
+ ]);
24
+ const valueKey = fieldSpec.nest ? `_${prop}` : prop;
25
+ const currentValue = sourceOptions[valueKey];
26
+
27
+ if (currentValue && !(currentValue in fieldSpec.children)) {
28
+ throw new EvidenceError('Internal Error', [
29
+ 'Attempted to access value not found in Options specification',
30
+ 'Error occured while trying to access a child value',
31
+ JSON.stringify({ currentValue, fieldSpec })
32
+ ]);
33
+ }
34
+
35
+ if (!sourceOptions[prop]) sourceOptions[prop] = {};
36
+
37
+ // Assign the empty object
38
+ return Options(
39
+ fieldSpec.children[currentValue] ?? {},
40
+ fieldSpec.nest ? sourceOptions[prop] : sourceOptions,
41
+ { ...opts, value: currentValue }
42
+ );
43
+ };
44
+
45
+ /**
46
+ * @typedef {Object} OptionsOpts
47
+ * @property {boolean} [specMode = false] Changes the Options object to return booleans for all fields, indicating if they are secret or not
48
+ * @property {boolean} [secretsMode = false] Returns values only for secrets, non-secrets will all be undefined
49
+ * @property {boolean} [optionsMode = false] Returns values only for non-secrets, secrets will all be undefined
50
+ * @property {boolean} [parentMode = false] Does not return nested options, instead always resolves to a value
51
+ * @property {string} [value] Returns values only for non-secrets, secrets will all be undefined
52
+ * @property {ReturnType<typeof Options>} [parent] Parent options proxy
53
+ */
54
+
55
+ /**
56
+ * @typedef {*} OptionsObj
57
+ */
58
+
59
+ /**
60
+ * @param {import('../../Datasources.js').Datasource["options"]} spec
61
+ * @param {*} sourceOptions
62
+ * @param {OptionsOpts} [opts]
63
+ * @returns {OptionsObj}
64
+ */
65
+ export const Options = (spec, sourceOptions, opts) => {
66
+ return new Proxy(sourceOptions, {
67
+ /** @returns {(string|symbol)[]} */
68
+ ownKeys() {
69
+ const output = [];
70
+ for (const [specKey, specEntry] of Object.entries(spec)) {
71
+ if (specEntry.virtual) continue;
72
+ const valueKey = specEntry.children && specEntry.nest ? `_${specKey}` : specKey;
73
+ if (
74
+ !opts?.specMode /* Value Mode */ &&
75
+ !(valueKey in sourceOptions) /* Value is not defined */
76
+ )
77
+ continue;
78
+ output.push(specKey);
79
+ }
80
+
81
+ return output.sort((a, b) => a.localeCompare(b));
82
+ },
83
+ get(self, p) {
84
+ // Mode Toggles
85
+ if (p === IsOptions) return true;
86
+ if (p === OptionSpecMode)
87
+ return Options(spec, sourceOptions, { ...opts, specMode: !opts?.specMode });
88
+ if (p === OptionSecretMode)
89
+ return Options(spec, sourceOptions, {
90
+ ...opts,
91
+ secretsMode: !opts?.secretsMode,
92
+ optionsMode: false
93
+ });
94
+ if (p === OptionSafeMode)
95
+ return Options(spec, sourceOptions, {
96
+ ...opts,
97
+ optionsMode: !opts?.optionsMode,
98
+ secretsMode: false
99
+ });
100
+ if (p === OptionParentMode)
101
+ return Options(spec, sourceOptions, { ...opts, parentMode: !opts?.parentMode });
102
+ if (p === OptionGetSpec) return spec;
103
+ // Special Cases
104
+ if (p === OptionDebug) return { spec, sourceOptions, opts };
105
+ if (p === 'toJSON') return () => sourceOptions;
106
+ if (p === 'toString' || p === Symbol.toStringTag) {
107
+ return () => opts?.value;
108
+ }
109
+
110
+ const prop = p.toString();
111
+
112
+ const fieldSpec = spec[prop];
113
+
114
+ if (!fieldSpec) {
115
+ throw new EvidenceError('Internal Error', [
116
+ 'Attempted to get property that is not in spec',
117
+ JSON.stringify({ prop }),
118
+ JSON.stringify({ spec })
119
+ ]);
120
+ }
121
+
122
+ const { specMode, parentMode, secretsMode, optionsMode } = opts ?? {};
123
+ const hasChildren = Boolean(fieldSpec.children);
124
+ const secret = Boolean(fieldSpec.secret);
125
+ const allMode = Boolean(!secretsMode && !optionsMode);
126
+ const validSecret = Boolean(secret && secretsMode);
127
+ const validOption = Boolean(!secret && optionsMode);
128
+
129
+ let returnValue;
130
+ let returnSpec;
131
+
132
+ if (hasChildren) {
133
+ const valueKey = fieldSpec.nest ? `_${prop}` : prop;
134
+
135
+ if (parentMode) {
136
+ // Never resolveChildOptions
137
+ if (allMode || validSecret || validOption) returnValue = self[valueKey];
138
+ returnSpec = fieldSpec;
139
+ } else {
140
+ // We are in child mode, we resolveChildOptions if needed
141
+ returnValue = resolveChildOptions(fieldSpec, prop, sourceOptions, {
142
+ ...opts,
143
+ parent: self
144
+ });
145
+ returnSpec = fieldSpec?.children?.[self[valueKey]];
146
+ }
147
+ } else {
148
+ returnSpec = fieldSpec;
149
+
150
+ if (allMode || validSecret || validOption) returnValue = self[prop];
151
+ else returnValue = undefined;
152
+ }
153
+
154
+ if (specMode) return returnSpec;
155
+ else return returnValue;
156
+ },
157
+ set(self, p, setVal) {
158
+ if (opts?.specMode) return false;
159
+ const prop = p.toString();
160
+
161
+ const fieldSpec = spec[prop];
162
+ let target = prop;
163
+ if (fieldSpec.children) {
164
+ if (fieldSpec.nest) {
165
+ if (typeof setVal !== 'object') target = `_${prop}`;
166
+ }
167
+ }
168
+ if (typeof setVal === 'object') {
169
+ const child = resolveChildOptions(spec[prop], prop, self, { ...opts, parent: self });
170
+ for (const [k, v] of Object.entries(setVal)) {
171
+ // This checks against spec using .get
172
+ child[k];
173
+
174
+ child[k] = v;
175
+ }
176
+ }
177
+
178
+ self[target] = setVal;
179
+ return true;
180
+ }
181
+ });
182
+ };
183
+
184
+ /**
185
+ * @param {ReturnType<typeof Options>} options
186
+ * @param {OptionSafeMode | OptionSecretMode} mode
187
+ */
188
+ const filterByMode = (options, mode) => {
189
+ const filterMode = options[mode];
190
+ const parentMode = options[OptionParentMode];
191
+ const rootSpec = options[OptionGetSpec];
192
+ /** @type {*} */
193
+ const output = {};
194
+ for (const [key, value] of Object.entries(filterMode)) {
195
+ if (typeof value === 'undefined') continue;
196
+ const parentSpec = rootSpec[key];
197
+
198
+ if (typeof value !== 'object') {
199
+ output[key] = value;
200
+ } else {
201
+ if (!parentSpec.nest) {
202
+ Object.assign(output, filterByMode(options[key], mode));
203
+
204
+ if (typeof parentMode[mode][key] !== 'undefined') output[key] = parentMode[mode][key];
205
+ } else {
206
+ output[key] = {
207
+ ...filterByMode(options[key], mode)
208
+ };
209
+ if (`_${key}` in options) {
210
+ output[`_${key}`] = parentMode[mode][key];
211
+ }
212
+ }
213
+ }
214
+ }
215
+
216
+ return output;
217
+ };
218
+ /**
219
+ * @param {ReturnType<typeof Options>} options
220
+ */
221
+ export const getSafeOptions = (options) => filterByMode(options, OptionSafeMode);
222
+
223
+ /**
224
+ * @param {ReturnType<typeof Options>} options
225
+ */
226
+ export const getSecretOptions = (options) => filterByMode(options, OptionSecretMode);
227
+
228
+ /**
229
+ * @param {*} options
230
+ * @param {string[]} optionPath
231
+ * @returns {import('../../schemas/datasourcePluginOptions.schema.js').IDatasourceOptionSpec}
232
+ */
233
+ export const getSpecAtPath = (options, optionPath) => {
234
+ let x = options[OptionSpecMode];
235
+ const finalKey = optionPath.at(-1);
236
+ if (!finalKey)
237
+ throw new EvidenceError('Internal Error', [
238
+ 'optionPath ends with an invalid key',
239
+ JSON.stringify(optionPath)
240
+ ]);
241
+
242
+ for (const k of optionPath.slice(0, -1)) {
243
+ x = x[k];
244
+ }
245
+
246
+ const out = x[OptionGetSpec]
247
+ ? x[OptionGetSpec][
248
+ finalKey
249
+ ] /* Option is a parent, so we need to look at the parent's spec @ this value */
250
+ : x[finalKey]; /* Option is not a parent, so we can look at it's spec directly */
251
+
252
+ return out;
253
+ };