@yolean/evidence-sdk 0.0.0-stage → 4.2.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 +544 -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 +18 -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 +74 -0
  181. package/src/utils/svelte/addBasePath.spec.js +108 -0
  182. package/src/utils/svelte/index.js +13 -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,32 @@
1
+ import fs from 'fs/promises';
2
+ /** @type {import("vite").Plugin} */
3
+ export const queryDirectoryHmr = {
4
+ name: 'evidence:query-directory-hmr',
5
+
6
+ configureServer(server) {
7
+ server.watcher.add('../../queries/**.sql');
8
+ server.watcher.addListener(
9
+ 'change',
10
+ /**
11
+ *
12
+ * @param {string} filename
13
+ * @returns {Promise<void>}
14
+ */
15
+ async (filename) => {
16
+ if (!(filename.includes('.evidence/template/queries') && filename.endsWith('.sql'))) return;
17
+ const content = await fs.readFile(filename, 'utf-8');
18
+ const queryId = filename
19
+ .split('.evidence/template/queries/')
20
+ .pop()
21
+ ?.split('.sql')[0]
22
+ .replace('/', '_')
23
+ .replace('\\', '_');
24
+
25
+ server.ws.send('evidence:queryChange', {
26
+ queryId,
27
+ content
28
+ });
29
+ }
30
+ );
31
+ }
32
+ };
@@ -0,0 +1,79 @@
1
+ import express from 'express';
2
+ import { getManifest } from './virtuals/node/static-assets.js';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import { dataDirectory } from '../../lib/projectPaths.js';
6
+
7
+ /**
8
+ * Creates an express application that handles Evidence-specific functionality
9
+ * e.g. Fetching parquet files,
10
+ *
11
+ * @returns {import("vite").Connect.HandleFunction}
12
+ */
13
+ export const getSidecarApp = () => {
14
+ const app = express();
15
+
16
+ // Handle custom URLs
17
+ app.get('/_evidence/manifest.json', (req, res) => {
18
+ res.send(getManifest('browser')).end();
19
+ });
20
+
21
+ app.get('/_evidence/:route_hash/:additional_hash/all-queries.json', (req, res) => {
22
+ res
23
+ .send(
24
+ JSON.stringify({
25
+ params: req.params
26
+ })
27
+ )
28
+ .end();
29
+ });
30
+ app.get('/_evidence/prerendered-queries/:query_hash.arrow', (req, res) => {
31
+ return res.send('Arrow file here!').end();
32
+ });
33
+ app.get('/_evidence/query/:schema/:filename.parquet', async (req, res) => {
34
+ // TODO: Check if the file really exists
35
+ const filepath = path.join(
36
+ dataDirectory,
37
+ req.params.schema,
38
+ req.params.filename,
39
+ req.params.filename + '.parquet'
40
+ );
41
+
42
+ if (
43
+ await fs
44
+ .stat(filepath)
45
+ .then(() => true)
46
+ .catch(() => false)
47
+ ) {
48
+ res.sendFile(filepath, {
49
+ acceptRanges: true /* Important to allow predicate pushdown */
50
+ });
51
+ } else {
52
+ res.sendStatus(404);
53
+ }
54
+ });
55
+
56
+ app.get('/_evidence/*', (req, res) => res.sendStatus(404).end());
57
+ app.post('/_evidence/*', (req, res) => res.sendStatus(405).end());
58
+ app.patch('/_evidence/*', (req, res) => res.sendStatus(405).end());
59
+ app.put('/_evidence/*', (req, res) => res.sendStatus(405).end());
60
+ app.delete('/_evidence/*', (req, res) => res.sendStatus(405).end());
61
+
62
+ return app;
63
+ };
64
+
65
+ /**
66
+ *
67
+ * @param {import("vite").UserConfig} cfg
68
+ */
69
+ export const applySidecarConfig = (cfg) => {
70
+ if (!cfg.server) cfg.server = {};
71
+ if (!cfg.server.proxy) cfg.server.proxy = {};
72
+ cfg.server.proxy['/_evidence'] = {};
73
+
74
+ if (!cfg.preview) cfg.preview = {};
75
+ if (!cfg.preview.proxy) cfg.preview.proxy = {};
76
+ cfg.preview.proxy['/_evidence'] = {};
77
+
78
+ return cfg;
79
+ };
@@ -0,0 +1,13 @@
1
+ <script>
2
+ /** @type {import("@evidence-dev/sdk/usql").Query[]} */
3
+ export let queries = [];
4
+ </script>
5
+
6
+ <svelte:head>
7
+ {#each queries as query}
8
+ <meta
9
+ evidence-query-presence={btoa(query.originalText)}
10
+ evidence-query-content={query.originalText}
11
+ />
12
+ {/each}
13
+ </svelte:head>
@@ -0,0 +1,52 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { ProxyStack } from '../proxyStack.js';
3
+
4
+ /**
5
+ * @param {import("svelte/store").Readable<*> s}
6
+ */
7
+ const getValue = (s) => {
8
+ let out;
9
+ s.subscribe((v) => (out = v))();
10
+ return out;
11
+ };
12
+
13
+ describe('ProxyStack', () => {
14
+ it('Should return a bare object when nothing has been added', () => {
15
+ const { value } = ProxyStack();
16
+ const content = getValue(value);
17
+ expect(content).toEqual({});
18
+ });
19
+ it('Should return a matching object when one object has been added', () => {
20
+ const { value, push } = ProxyStack();
21
+ const entry = { x: 1 };
22
+ push(entry);
23
+ const content = getValue(value);
24
+ expect(content.x).toEqual(1);
25
+ });
26
+ it('Should return the most recent value for a prop', () => {
27
+ const { value, push } = ProxyStack();
28
+
29
+ push({ x: 1 });
30
+ push({ x: 2 });
31
+ push({ x: 3 });
32
+ const content = getValue(value);
33
+ expect(content.x).toEqual(3);
34
+ });
35
+ it('Should return the most recent value for a prop, unless it has been deleted', () => {
36
+ const stack = ProxyStack();
37
+
38
+ stack.push({ x: 1 });
39
+ stack.push({ x: 2 });
40
+ const id = stack.push({ x: 3 });
41
+ stack.rm(id);
42
+ const content = getValue(stack.value);
43
+ expect(content.x).toEqual(2);
44
+ });
45
+ it('Should JSON.stringify properly', () => {
46
+ const stack = ProxyStack();
47
+ const entry = { x: 1 };
48
+ stack.push(entry);
49
+ const content = getValue(stack.value);
50
+ expect(content).toEqual(entry);
51
+ });
52
+ });
@@ -0,0 +1,27 @@
1
+ import { setContext } from 'svelte';
2
+ import {
3
+ QUERIES_CONTEXT_KEY,
4
+ ALL_QUERIES_CONTEXT_KEY,
5
+ ALL_QUERIES_MUTS_CONTEXT_KEY
6
+ } from '$evidence/contextKeys';
7
+ import { ProxyStack } from '$evidence/proxyStack';
8
+ import { readable } from 'svelte/store';
9
+
10
+ export default () => {
11
+ const queryContext = readable({}); // TODO: Stores
12
+ const allQueries = ProxyStack();
13
+
14
+ setContext(QUERIES_CONTEXT_KEY, queryContext);
15
+ setContext(ALL_QUERIES_CONTEXT_KEY, allQueries.value);
16
+ setContext(ALL_QUERIES_MUTS_CONTEXT_KEY, { push: allQueries.push, rm: allQueries.rm });
17
+ };
18
+
19
+ export const asObj = () => {
20
+ const queryContext = readable({}); // TODO: Stores
21
+ const allQueries = ProxyStack();
22
+ return {
23
+ [QUERIES_CONTEXT_KEY]: queryContext,
24
+ [ALL_QUERIES_CONTEXT_KEY]: allQueries.value,
25
+ [ALL_QUERIES_MUTS_CONTEXT_KEY]: { push: allQueries.push, rm: allQueries.rm }
26
+ };
27
+ };
@@ -0,0 +1,3 @@
1
+ export const getManifest = () => {
2
+ return fetch('/_evidence/manifest.json').then((r) => r.text());
3
+ };
@@ -0,0 +1,3 @@
1
+ export const QUERIES_CONTEXT_KEY = '__EVIDENCE_CONTEXTUAL_QUERIES';
2
+ export const ALL_QUERIES_CONTEXT_KEY = '__EVIDENCE_ALL_QUERIES';
3
+ export const ALL_QUERIES_MUTS_CONTEXT_KEY = '__EVIDENCE_ALL_QUERIES_MUTS';
@@ -0,0 +1,23 @@
1
+ import {
2
+ initDB,
3
+ setParquetURLs,
4
+ updateSearchPath,
5
+ query
6
+ } from '@evidence-dev/universal-sql/client-duckdb';
7
+
8
+ import { getManifest } from '$evidence/static-assets';
9
+
10
+ export default (async () => {
11
+ await initDB();
12
+ let res;
13
+ // TODO: Optionally take in a filepath and/or URL for the manifest
14
+ res = await getManifest();
15
+ res = JSON.parse(res);
16
+
17
+ await setParquetURLs(res.renderedFiles ?? {});
18
+ await updateSearchPath(Object.keys(res.renderedFiles ?? {}));
19
+ if (!res.renderedFiles) console.error('No fixture data available!');
20
+ // Test Query
21
+ await query('SELECT * FROM information_schema.tables');
22
+ console.log('Universal SQL has been initialized successfully');
23
+ })();
@@ -0,0 +1,56 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ const inTemplate = process.cwd().includes(path.join('.evidence', 'template'));
5
+ const evidenceDirectory = inTemplate ? '..' : '.evidence';
6
+ const dataDirectory = path.resolve(evidenceDirectory, 'data');
7
+
8
+ /**
9
+ * @param {"browser" | "node"} [dest = "node"]
10
+ */
11
+ export const getManifest = (dest = 'node') => {
12
+ // TODO: Does this need to be sync? Would simplify types if not
13
+ try {
14
+ const manifestContent = fs.readFileSync(path.join(dataDirectory, 'manifest.json'), 'utf-8');
15
+ /** @type {import('../../../../plugins/datasources/types.js').Manifest} */
16
+ const manifest = JSON.parse(manifestContent);
17
+
18
+ if (!manifest.renderedFiles) throw new Error('Malformed Manifest');
19
+
20
+ if (dest === 'node') {
21
+ const renderedFiles = Object.fromEntries(
22
+ Object.entries(manifest.renderedFiles).map(([schema, queries]) => {
23
+ return [
24
+ schema,
25
+ queries.map((queryPath) => {
26
+ // Don't modify external paths
27
+ if (queryPath.startsWith('http')) return queryPath;
28
+ // Adjust prefix
29
+ queryPath = queryPath.replace(
30
+ '_evidence/query',
31
+ path.join(evidenceDirectory, 'data')
32
+ );
33
+ const filename = queryPath.match(/(.+\/)(.+).parquet/);
34
+ if (filename?.length !== 3) {
35
+ // TODO: Debug log?
36
+ return queryPath;
37
+ } else {
38
+ return `${queryPath.startsWith('/') ? '.' : './'}${filename[1]}${filename[2]}/${
39
+ filename[2]
40
+ }.parquet`;
41
+ }
42
+ })
43
+ ];
44
+ })
45
+ );
46
+ manifest.renderedFiles = renderedFiles;
47
+ }
48
+
49
+ return JSON.stringify(manifest);
50
+ } catch (e) {
51
+ console.warn('Failed to load manifest file || ', e instanceof Error ? e.message : e);
52
+ return JSON.stringify({
53
+ renderedFiles: {}
54
+ });
55
+ }
56
+ };
@@ -0,0 +1,37 @@
1
+ import { nanoid } from 'nanoid';
2
+ import { derived, writable } from 'svelte/store';
3
+
4
+ /**
5
+ * @returns {{ value: Object, push: (v: Object) => string, rm: (id: string) => void }}
6
+ */
7
+ export const ProxyStack = () => {
8
+ /**
9
+ * @type {Array<[string, Object]>}
10
+ */
11
+ let stack = [];
12
+ /** @type {import("svelte/store").Writable<Record<string,Object>>} */
13
+ const write = writable({});
14
+
15
+ const flush = () => {
16
+ write.update((v) => {
17
+ for (const k of Object.keys(v)) delete v[k];
18
+ return Object.assign({}, v, ...stack.map((s) => s[1]));
19
+ });
20
+ };
21
+
22
+ /** @param {Object} value */
23
+ const push = (value) => {
24
+ const id = nanoid();
25
+ stack.push([id, value]);
26
+ flush();
27
+ return id;
28
+ };
29
+
30
+ /** @param {string} id */
31
+ const rm = (id) => {
32
+ stack = stack.filter(([itemId]) => itemId !== id);
33
+ flush();
34
+ };
35
+
36
+ return { value: derived([write], ([$write]) => $write), push, rm };
37
+ };
@@ -0,0 +1,150 @@
1
+ // @ts-nocheck Adding nocheck because type errors in this file were failing the build and @ItsMeBrianD told said this code is dead
2
+
3
+ import { getContext } from 'svelte';
4
+ import { ALL_QUERIES_CONTEXT_KEY, QUERIES_CONTEXT_KEY } from '$evidence/contextKeys';
5
+ import { Query } from '@evidence-dev/sdk/usql';
6
+ import { query } from '@evidence-dev/universal-sql/client-duckdb';
7
+ import { derived } from 'svelte/store';
8
+
9
+ let ssrHookInstalled = false;
10
+ /**
11
+ *
12
+ * @param {boolean} isInstalled
13
+ * @returns
14
+ */
15
+ export const setSsrHookState = (isInstalled) => {
16
+ ssrHookInstalled = isInstalled;
17
+ };
18
+
19
+ /**
20
+ *
21
+ * @param {string} sql
22
+ * @param {string} id
23
+ * @returns
24
+ */
25
+ const runner = (sql, id) => query(sql, { query_name: id, prerendering: true, route_hash: '500' });
26
+
27
+ /**
28
+ * @param {string} name
29
+ * @param {string} sql
30
+ * @param {import('@evidence-dev/sdk/usql').QueryOpts} [opts]
31
+ */
32
+ export const runQuery = (name, sql, opts) => {
33
+ let initialData = undefined;
34
+ let initialError = undefined;
35
+
36
+ try {
37
+ if (typeof window === 'undefined') {
38
+ // SSR
39
+ if (ssrHookInstalled) initialData = query(sql);
40
+ } else {
41
+ const ssrKey = btoa(sql);
42
+ if (
43
+ window.__evidence_ssr &&
44
+ name in window.__evidence_ssr &&
45
+ sql === window.__evidence_ssr[ssrKey].initialQuery
46
+ ) {
47
+ initialData = window.__evidence_ssr[ssrKey].data;
48
+ Object.defineProperty(initialData, '_evidenceColumnTypes', {
49
+ enumerable: false,
50
+ value: window.__evidence_ssr[ssrKey].columns
51
+ });
52
+ }
53
+ }
54
+ } catch (e) {
55
+ initialError = new Error(`Error encountered while running query ${name}`, { cause: e });
56
+ }
57
+
58
+ return Query.create(sql, runner, { ...opts, initialData, initialError, id: name });
59
+ };
60
+
61
+ /**
62
+ * @returns {import("svelte/store").Readable<Record<string, import('@evidence-dev/sdk/usql').QueryValue>>}
63
+ */
64
+ export const getQueries = () => {
65
+ /** @type {import("svelte/store").Readable<Record<string,string>>} */
66
+ const context = getContext(QUERIES_CONTEXT_KEY);
67
+
68
+ /** @type {Record<string,import('@evidence-dev/sdk/usql').QueryValue<any>>} */
69
+ const queries = {};
70
+ const proxy = new Proxy(queries, {
71
+ /**
72
+ * @param {Record<string,Query>} t
73
+ * @param {*} p
74
+ * @returns
75
+ */
76
+ get(t, p) {
77
+ if (p in t) return t[p];
78
+ return Query.create(p, runner, {
79
+ id: p,
80
+ initialError: new Error(`Query ${p} is not available in this context`),
81
+ disableCache: true
82
+ });
83
+ }
84
+ });
85
+
86
+ return derived(
87
+ context,
88
+ (
89
+ /** @type {Record<string,string>} */ availableQueries,
90
+ /** @type {(x: Record<string,Query>) => void} */ set
91
+ ) => {
92
+ /** @type {Array<Promise<unknown> | unknown>} */
93
+ const updates = [];
94
+ Object.entries(availableQueries).forEach(([k, v]) => {
95
+ // Don't use $app/environment as that creates a hard dep on SvelteKit
96
+ // We want this to be portable to plain svelte projects
97
+ let initialData = undefined;
98
+ if (!(k in queries)) {
99
+ if (typeof window === 'undefined') {
100
+ // SSR
101
+ if (ssrHookInstalled) initialData = query(v);
102
+ } else {
103
+ const ssrKey = btoa(v);
104
+
105
+ if (
106
+ window.__evidence_ssr &&
107
+ ssrKey in window.__evidence_ssr &&
108
+ v === window.__evidence_ssr[ssrKey].initialQuery
109
+ ) {
110
+ initialData = window.__evidence_ssr[ssrKey].data;
111
+ Object.defineProperty(initialData, '_evidenceColumnTypes', {
112
+ enumerable: false,
113
+ value: window.__evidence_ssr[ssrKey].columns
114
+ });
115
+ }
116
+ }
117
+ }
118
+
119
+ const newStore = Query.create(v, runner, {
120
+ initialData,
121
+ id: k
122
+ });
123
+
124
+ if (queries[k]?.hash !== newStore.hash) {
125
+ updates.push(
126
+ // Loading states appear after 500ms
127
+ Promise.race([
128
+ newStore.fetch(),
129
+ new Promise((r) => {
130
+ setTimeout(r, 500);
131
+ })
132
+ ]).then(() => (queries[k] = newStore))
133
+ );
134
+ // Has updated
135
+ queries[k] = newStore;
136
+ }
137
+ });
138
+
139
+ Promise.all(updates).then(() => set(proxy));
140
+ return proxy;
141
+ },
142
+ proxy
143
+ );
144
+ };
145
+
146
+ export const getAllQueries = () => getContext(ALL_QUERIES_CONTEXT_KEY);
147
+
148
+ /** @deprecated Use Query instead of QueryStore */
149
+ const QueryStore = Query;
150
+ export { QueryStore };
@@ -0,0 +1,60 @@
1
+ import initUsqlPromise from '$evidence/initUsql';
2
+ import { runQuery, setSsrHookState } from '$evidence/queries';
3
+
4
+ setSsrHookState(true);
5
+
6
+ /**
7
+ * SvelteKit server hook to enable SSR
8
+ * @param {Array<{name: string, queryString: string}>} presentQueries
9
+ * @returns {(chunk: {html: string, done:boolean}) => Promise<string>}
10
+ */
11
+ export const ssrHook =
12
+ (presentQueries) =>
13
+ async ({ html, done }) => {
14
+ const regex = /<meta evidence-query-presence="(.+?)" evidence-query-content="((?:.|\s)+?)">/g;
15
+ let queryMatch;
16
+
17
+ while ((queryMatch = regex.exec(html) ?? []).length) {
18
+ if (queryMatch)
19
+ presentQueries.push({
20
+ name: queryMatch[1],
21
+ queryString: queryMatch[2].replaceAll('&quot;', '"')
22
+ });
23
+ }
24
+
25
+ const loadDetectedQueries = async () => {
26
+ await initUsqlPromise;
27
+ const results = await Promise.all(
28
+ presentQueries.map(async ({ name, queryString }) => {
29
+ const result = runQuery(`${name}-ssr`, `--ssr\n${queryString}`, {
30
+ disableCache: true
31
+ });
32
+
33
+ await result.fetch();
34
+ if ((result.length && result[0] === null) || result[0] === undefined)
35
+ throw new Error(`Failed to render queries via SSR`);
36
+ if (result.error) throw result.error;
37
+ return [
38
+ name,
39
+ {
40
+ data: Array.from(result),
41
+ columns: result.columns,
42
+ initialQuery: queryString
43
+ }
44
+ ];
45
+ })
46
+ );
47
+
48
+ return Object.fromEntries(results);
49
+ };
50
+
51
+ if (done) {
52
+ const r = await loadDetectedQueries();
53
+ html = html.replace(
54
+ '<script>',
55
+ `<script>
56
+ window.__evidence_ssr = ${JSON.stringify(r)}\n`
57
+ );
58
+ }
59
+ return html;
60
+ };
@@ -0,0 +1,31 @@
1
+ import { ALL_QUERIES_MUTS_CONTEXT_KEY, QUERIES_CONTEXT_KEY } from '$evidence/contextKeys';
2
+ import { getContext, setContext } from 'svelte';
3
+ import { derived, writable } from 'svelte/store';
4
+
5
+ export default () => {
6
+ /** @type {import("svelte/store").Readable<Record<string,string>>} */
7
+ const parent = getContext(QUERIES_CONTEXT_KEY);
8
+
9
+ const { push, rm } = getContext(ALL_QUERIES_MUTS_CONTEXT_KEY);
10
+ /** @type {string} */
11
+ let id;
12
+ /*
13
+ These stores are created in a closure so that the references in context remain the same
14
+ The returned function is set up to make reactivity easier
15
+ */
16
+ const write = writable({});
17
+ const childQueries = derived([parent, write], ([$parent, $write]) => ({
18
+ ...$parent,
19
+ ...$write
20
+ }));
21
+ setContext(QUERIES_CONTEXT_KEY, childQueries);
22
+
23
+ /** @param {Record<string,string>} layerQueries */
24
+ return (layerQueries) => {
25
+ write.set(layerQueries);
26
+ // todo: handle removal of all queries on dismount
27
+ if (id) rm(id);
28
+
29
+ id = push(layerQueries);
30
+ };
31
+ };
@@ -0,0 +1,12 @@
1
+ import { getEvidenceConfig } from '../../configuration/getEvidenceConfig.js';
2
+ import { nanoid } from 'nanoid';
3
+
4
+ export const build = () => `
5
+ export const BUILD_ID = import.meta.env.MODE === "development" ? "DEV" : "${nanoid(8)}";
6
+ export const BUILD_DATE = new Date(${new Date().getTime()});
7
+ `;
8
+
9
+ export const config = () => {
10
+ const cfg = getEvidenceConfig();
11
+ return `export default ${JSON.stringify(cfg)}`;
12
+ };
package/src/cli.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ import { runMain } from '@brianmd/citty';
3
+ import { rootCli } from './cli.root.js';
4
+
5
+ runMain(rootCli);
@@ -0,0 +1,90 @@
1
+ import { buildDevCli } from './build-dev/cli/index.js';
2
+ import { config } from './configuration/cli/index.js';
3
+
4
+ import { cmdFail } from './lib/cli/cmdFail.js';
5
+ import { enableDebug } from './lib/debug.js';
6
+ import { datasourcesCli } from './plugins/datasources/cli/index.js';
7
+ import { pluginsCli } from './plugins/cli/index.js';
8
+ import { defineCommand } from '@brianmd/citty';
9
+ import { writeFile, mkdir } from 'node:fs/promises';
10
+
11
+ // This is split into it's own file so that we can import it elsewhere without running the CLI twice
12
+
13
+ export const rootCli = defineCommand({
14
+ meta: {
15
+ name: 'evidence',
16
+ description: 'CLI to make interacting with Evidence projects easier'
17
+ },
18
+
19
+ args: {
20
+ debug: {
21
+ type: 'boolean',
22
+ default: false,
23
+ description: 'Enable additional debug logging'
24
+ },
25
+ profile: {
26
+ type: 'boolean',
27
+ default: false,
28
+ description: 'Enable profiling'
29
+ }
30
+ },
31
+
32
+ async setup(ctx) {
33
+ process.on('uncaughtException', (e) => {
34
+ cmdFail(e);
35
+ });
36
+ process.on('unhandledRejection', (e) => {
37
+ if (e instanceof Error) cmdFail(e);
38
+ else cmdFail(new Error('Unhandled Promise Rejection', { cause: e }));
39
+ });
40
+
41
+ if (ctx.args.debug) enableDebug();
42
+ else {
43
+ const debug = console.debug;
44
+ /* Disable console.debug */
45
+ console.debug = () => {};
46
+ // TODO: Check on this, it seems to leak debug once the dev server has started
47
+ /* Restore console.debug in a wrapped cleanup function */
48
+ const cleanup = this.cleanup;
49
+ this.cleanup = async (_ctx) => {
50
+ await cleanup?.(_ctx);
51
+ console.debug = debug;
52
+ };
53
+ }
54
+
55
+ if (ctx.args.profile) {
56
+ const { Session } = await import('node:inspector/promises');
57
+ const session = new Session();
58
+ session.connect();
59
+ await session.post('Profiler.enable');
60
+ await session.post('Profiler.start');
61
+
62
+ const cleanup = this.cleanup;
63
+ this.cleanup = async (_ctx) => {
64
+ const { resolve, dirname } = await import('node:path');
65
+
66
+ const path = resolve(`./.evidence/template/profiles/${ctx.args._.join('-')}.cpuprofile`);
67
+ await mkdir(dirname(path), { recursive: true });
68
+ const { profile } = await session.post('Profiler.stop');
69
+ await writeFile(path, JSON.stringify(profile));
70
+
71
+ await session.post('Profiler.disable');
72
+ session.disconnect();
73
+
74
+ await cleanup?.(_ctx);
75
+ };
76
+ }
77
+ },
78
+
79
+ catch(ctx, e) {
80
+ cmdFail(e);
81
+ },
82
+
83
+ subCommands: {
84
+ ...buildDevCli,
85
+ ...datasourcesCli,
86
+ ...pluginsCli,
87
+
88
+ config
89
+ }
90
+ });