@yolean/evidence 0.0.0-stage → 40.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/cli.js +436 -0
  3. package/package.json +66 -3
  4. package/template/README.md +11 -0
  5. package/template/postcss.config.cjs +15 -0
  6. package/template/src/app.css +327 -0
  7. package/template/src/app.html +96 -0
  8. package/template/src/fix-tprotocol-service-worker.js +36 -0
  9. package/template/src/global.d.ts +1 -0
  10. package/template/src/hooks.client.js +19 -0
  11. package/template/src/hooks.server.js +41 -0
  12. package/template/src/pages/+error.svelte +57 -0
  13. package/template/src/pages/+layout.js +187 -0
  14. package/template/src/pages/+layout.svelte +10 -0
  15. package/template/src/pages/+page.md +16 -0
  16. package/template/src/pages/api/[...route]/evidencemeta.json/+server.js +48 -0
  17. package/template/src/pages/api/[route_hash]/[additional_hash]/all-queries.json/+server.js +10 -0
  18. package/template/src/pages/api/customFormattingSettings.json/+server.js +82 -0
  19. package/template/src/pages/api/customFormattingSettings.json/GET.json/+server.js +6 -0
  20. package/template/src/pages/api/pagesManifest.json/+server.js +92 -0
  21. package/template/src/pages/api/pagesManifest.json/buildPageManifest.spec.js +274 -0
  22. package/template/src/pages/api/prerendered_queries/[query_hash].arrow/+server.js +10 -0
  23. package/template/src/pages/api/settings.json/+server.js +107 -0
  24. package/template/src/pages/explore/+layout.js +5 -0
  25. package/template/src/pages/explore/console/+page.svelte +5 -0
  26. package/template/src/pages/explore/schema/+page.svelte +13 -0
  27. package/template/src/pages/fix-tprotocol-service-worker.js/+server.js +20 -0
  28. package/template/src/pages/manifest.webmanifest/+server.js +21 -0
  29. package/template/src/pages/settings/+layout.server.js +1 -0
  30. package/template/src/pages/settings/+page.js +25 -0
  31. package/template/src/pages/settings/+page.server.js +110 -0
  32. package/template/src/pages/settings/+page.svelte +111 -0
  33. package/template/static/apple-touch-icon.png +0 -0
  34. package/template/static/favicon.ico +0 -0
  35. package/template/static/icon-192.png +0 -0
  36. package/template/static/icon-512.png +0 -0
  37. package/template/static/icon.svg +6 -0
  38. package/template/svelte.config.js +106 -0
  39. package/template/tailwind.config.cjs +48 -0
  40. package/template/vite.config.js +99 -0
  41. package/README.md +0 -3
@@ -0,0 +1,187 @@
1
+ import { browser, building, dev } from '$app/environment';
2
+ import {
3
+ tableFromIPC,
4
+ initDB,
5
+ setParquetURLs,
6
+ query as usqlQuery,
7
+ updateSearchPath,
8
+ arrowTableToJSON
9
+ } from '@evidence-dev/universal-sql/client-duckdb';
10
+ import { profile } from '@evidence-dev/component-utilities/profile';
11
+ import { toasts } from '@evidence-dev/component-utilities/stores';
12
+ import { setTrackProxy } from '@evidence-dev/sdk/usql';
13
+ import { addBasePath } from '@evidence-dev/sdk/utils/svelte';
14
+ import md5 from 'blueimp-md5';
15
+
16
+ export const ssr = !dev;
17
+ export const prerender = import.meta.env.VITE_EVIDENCE_SPA !== 'true';
18
+ export const trailingSlash = 'always';
19
+
20
+ const loadDB = async () => {
21
+ let renderedFiles = {};
22
+
23
+ if (!browser) {
24
+ const { readFile } = await import('fs/promises');
25
+ ({ renderedFiles } = JSON.parse(
26
+ await readFile('./static/data/manifest.json', 'utf-8').catch(() => '{}')
27
+ ));
28
+ } else {
29
+ const res = await fetch(addBasePath('/data/manifest.json'));
30
+ if (res.ok) ({ renderedFiles } = await res.json());
31
+ }
32
+ await profile(initDB);
33
+
34
+ if (Object.keys(renderedFiles ?? {}).length === 0) {
35
+ console.warn(`No sources found, execute "npm run sources" to generate`.trim());
36
+ if (dev) {
37
+ toasts.add(
38
+ {
39
+ id: 'MissingManifest',
40
+ status: 'warning',
41
+ title: 'No Sources Found',
42
+ message: 'Configure and run sources to include data in your project.'
43
+ },
44
+ 10000
45
+ );
46
+ }
47
+ } else {
48
+ await profile(setParquetURLs, renderedFiles, { addBasePath });
49
+ await profile(updateSearchPath, Object.keys(renderedFiles));
50
+ }
51
+ };
52
+
53
+ const database_initialization = profile(loadDB);
54
+
55
+ /**
56
+ *
57
+ * @param {string} routeHash
58
+ * @param {string} paramsHash
59
+ * @param {typeof fetch} fetch
60
+ * @returns {Promise<Record<string, unknown[]>>}
61
+ */
62
+ async function getPrerenderedQueries(routeHash, paramsHash, fetch) {
63
+ // get every query that's run in the component
64
+ const res = await fetch(addBasePath(`/api/${routeHash}/${paramsHash}/all-queries.json`));
65
+ if (!res.ok) return {};
66
+
67
+ const sql_cache_with_hashed_query_strings = await res.json();
68
+
69
+ const resolved_entries = await Promise.all(
70
+ Object.entries(sql_cache_with_hashed_query_strings).map(async ([query_name, query_hash]) => {
71
+ const res = await fetch(addBasePath(`/api/prerendered_queries/${query_hash}.arrow`));
72
+ if (!res.ok) return null;
73
+
74
+ const table = await tableFromIPC(res);
75
+ return [query_name, arrowTableToJSON(table)];
76
+ })
77
+ );
78
+
79
+ return Object.fromEntries(resolved_entries.filter(Boolean));
80
+ }
81
+
82
+ const system_routes = ['/settings', '/explore'];
83
+
84
+ /** @type {Map<string, { inputs: Record<string, string> }>} */
85
+ const dummy_pages = new Map();
86
+
87
+ /** @satisfies {import("./$types").LayoutLoad} */
88
+ export const load = async ({ fetch, route, params, url }) => {
89
+ const [{ customFormattingSettings }, pagesManifest, evidencemeta] = await Promise.all([
90
+ fetch(addBasePath('/api/customFormattingSettings.json/GET.json')).then((x) => x.json()),
91
+ fetch(addBasePath('/api/pagesManifest.json')).then((x) => x.json()),
92
+ fetch(addBasePath(`/api/${route.id}/evidencemeta.json`))
93
+ .then((x) => x.json())
94
+ .catch(() => ({ queries: [] }))
95
+ ]);
96
+
97
+ const routeHash = md5(route.id);
98
+ const paramsHash = md5(
99
+ Object.entries(params)
100
+ .sort()
101
+ .map(([key, value]) => `${key}\x1F${value}`)
102
+ .join('\x1E')
103
+ );
104
+ const isUserPage =
105
+ route.id && system_routes.every((system_route) => !route.id.startsWith(system_route));
106
+
107
+ /** @type {App.PageData["data"]} */
108
+ let data = {};
109
+
110
+ const {
111
+ inputs = setTrackProxy({
112
+ label: '',
113
+ value: '(SELECT NULL WHERE 0 /* An Input has not been set */)'
114
+ }) /* Create a proxy by default */
115
+ } = dummy_pages.get(url.pathname) ?? {};
116
+
117
+ const is_dummy_page = dummy_pages.has(url.pathname);
118
+ if ((dev || building) && !browser && !is_dummy_page) {
119
+ dummy_pages.set(url.pathname, { inputs });
120
+ await fetch(url);
121
+ dummy_pages.delete(url.pathname);
122
+ }
123
+
124
+ if (!browser) await database_initialization;
125
+ // account for potential changes in manifest (source query hmr)
126
+ if (!browser && dev) await initDB();
127
+
128
+ // let SSR saturate the cache first
129
+ if (browser && isUserPage && prerender) {
130
+ data = await getPrerenderedQueries(routeHash, paramsHash, fetch);
131
+ }
132
+
133
+ /** @type {App.PageData["__db"]["query"]} */
134
+ function query(sql, { query_name, callback = (x) => x } = {}) {
135
+ if (browser) {
136
+ return (async () => {
137
+ await database_initialization;
138
+ const result = await usqlQuery(sql);
139
+ return callback(result);
140
+ })();
141
+ }
142
+
143
+ return callback(
144
+ usqlQuery(sql, {
145
+ route_hash: routeHash,
146
+ additional_hash: paramsHash,
147
+ query_name,
148
+ prerendering: building
149
+ })
150
+ );
151
+ }
152
+
153
+ let tree = pagesManifest;
154
+ for (const part of (route.id ?? '').split('/').slice(1)) {
155
+ tree = tree.children[part];
156
+ if (!tree) break;
157
+ if (tree.frontMatter?.title) {
158
+ tree.title = tree.frontMatter.title;
159
+ } else if (tree.frontMatter?.breadcrumb) {
160
+ let { breadcrumb } = tree.frontMatter;
161
+ for (const [param, value] of Object.entries(params)) {
162
+ breadcrumb = breadcrumb.replaceAll(`\${params.${param}}`, value);
163
+ }
164
+ tree.title = (await query(breadcrumb))[0]?.breadcrumb;
165
+ }
166
+ }
167
+
168
+ return /** @type {App.PageData} */ ({
169
+ __db: {
170
+ query,
171
+ async load() {
172
+ return database_initialization;
173
+ },
174
+ async updateParquetURLs(manifest) {
175
+ // todo: maybe diff with old?
176
+ const { renderedFiles } = JSON.parse(manifest);
177
+ await profile(setParquetURLs, renderedFiles, { addBasePath });
178
+ }
179
+ },
180
+ inputs,
181
+ data,
182
+ customFormattingSettings,
183
+ isUserPage,
184
+ evidencemeta,
185
+ pagesManifest
186
+ });
187
+ };
@@ -0,0 +1,10 @@
1
+ <script>
2
+ import '@evidence-dev/tailwind/fonts.css';
3
+ import '../app.css';
4
+ import { EvidenceDefaultLayout } from '@evidence-dev/core-components';
5
+ export let data;
6
+ </script>
7
+
8
+ <EvidenceDefaultLayout {data}>
9
+ <slot slot="content" />
10
+ </EvidenceDefaultLayout>
@@ -0,0 +1,16 @@
1
+ # Welcome to the Evidence Development Workspace!
2
+
3
+ <Alert status=warning>
4
+ If you see this page in your Evidence App, you have deleted the `index.md` file from your project. Add a `pages/index.md` to your project to override this page.
5
+ </Alert>
6
+
7
+ This workspace contains examples of all Evidence features.
8
+
9
+ If you are developing a new feature, add a page to this project. The page will be used as a reference for the feature, as a sandbox for anyone making changes to the feature in the future, and for general product testing.
10
+
11
+ If you notice a feature is missing from this project, please:
12
+
13
+ - add a page or
14
+ - create an issue on Github.
15
+
16
+ Thanks!
@@ -0,0 +1,48 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+ import preprocessor from '@evidence-dev/preprocess';
4
+ import { json } from '@sveltejs/kit';
5
+ import { paths, isExampleProject } from '@evidence-dev/sdk/meta';
6
+
7
+ export const prerender = true;
8
+
9
+ /** @type {import('./$types').EntryGenerator} */
10
+ export const entries = async () => {
11
+ const pagesDir = paths.pagesDirectory;
12
+
13
+ const allPages = (await fs.readdir(pagesDir, { recursive: true })).filter((f) =>
14
+ f.endsWith('.md')
15
+ ); // Discard everything that isn't a page
16
+
17
+ const output = allPages.map((filepath) => {
18
+ // Example Project is special
19
+ const removal = isExampleProject ? '/+page.md' : '.md';
20
+ let result = filepath.slice(0, -removal.length);
21
+ if (filepath.endsWith('index.md')) result = result.replaceAll(/\/?index/g, '');
22
+ return { route: result };
23
+ });
24
+
25
+ return output;
26
+ };
27
+
28
+ /** @type {import("./$types").RequestHandler} */
29
+ export async function GET({ params: { route } }) {
30
+ if (route === '/settings') {
31
+ const queries = [];
32
+ return json({ queries });
33
+ }
34
+ let routesDir;
35
+ if ((await fs.readdir(process.cwd())).includes('src')) {
36
+ routesDir = path.join('src', 'pages'); // example project wackiness
37
+ } else {
38
+ routesDir = path.join('.evidence', 'template', 'src', 'pages');
39
+ }
40
+ const routePath = path.join(process.cwd(), routesDir, route, '+page.md');
41
+
42
+ const content = await fs.readFile(routePath, 'utf8');
43
+
44
+ const partialInjectedContent = preprocessor.injectPartials(content);
45
+ const queries = preprocessor.extractQueries(partialInjectedContent);
46
+
47
+ return json({ queries });
48
+ }
@@ -0,0 +1,10 @@
1
+ import { get_all_page_queries } from '@evidence-dev/universal-sql';
2
+ import { error } from '@sveltejs/kit';
3
+
4
+ export const GET = ({ params }) => {
5
+ try {
6
+ return new Response(get_all_page_queries(params.route_hash, params.additional_hash));
7
+ } catch (e) {
8
+ throw error(404, 'not found');
9
+ }
10
+ };
@@ -0,0 +1,82 @@
1
+ export const prerender = false;
2
+
3
+ import { json } from '@sveltejs/kit';
4
+ import fs from 'fs';
5
+ import path from 'path';
6
+
7
+ const CUSTOMIZATION_DIRECTORY = '../customization';
8
+ const CUSTOM_FORMATTING_SETTINGS_PATH = `${CUSTOMIZATION_DIRECTORY}/custom-formatting.json`;
9
+ const DEFAULT_CUSTOM_FORMATTING_SETTINGS = {
10
+ version: '1.0',
11
+ customFormats: []
12
+ };
13
+
14
+ export async function GET() {
15
+ let customFormattingSettings = {};
16
+ try {
17
+ customFormattingSettings = getCustomFormattingSettings() || customFormattingSettings;
18
+ } catch {
19
+ // custom settings will be empty for now.
20
+ }
21
+ let result = { customFormattingSettings };
22
+ return json(result);
23
+ }
24
+
25
+ export async function POST({ request }) {
26
+ const { newCustomFormat } = await request.json();
27
+
28
+ let customFormattingSettings = getCustomFormattingSettings() || {};
29
+
30
+ if (newCustomFormat) {
31
+ if (!customFormattingSettings.customFormats) {
32
+ customFormattingSettings.customFormats = [];
33
+ }
34
+ if (newCustomFormat.formatTag && newCustomFormat.formatCode) {
35
+ customFormattingSettings.customFormats.push(newCustomFormat);
36
+ }
37
+ saveCustomFormattingSettings(customFormattingSettings);
38
+ }
39
+ return json(customFormattingSettings);
40
+ }
41
+
42
+ export async function DELETE({ request }) {
43
+ const { formatTag } = await request.json();
44
+ let customFormattingSettings = getCustomFormattingSettings() || {};
45
+ if (formatTag) {
46
+ if (!customFormattingSettings.customFormats) {
47
+ customFormattingSettings.customFormats = [];
48
+ }
49
+ let index = customFormattingSettings.customFormats.findIndex(
50
+ (format) => format.formatTag === formatTag
51
+ );
52
+ if (index >= 0) {
53
+ customFormattingSettings.customFormats.splice(index, 1);
54
+ }
55
+ saveCustomFormattingSettings(customFormattingSettings);
56
+ }
57
+ return json(customFormattingSettings);
58
+ }
59
+
60
+ function getCustomFormattingSettings() {
61
+ let customFormattingSettingsPath = getCustomFormattingSettingsFilePath();
62
+ return JSON.parse(fs.readFileSync(customFormattingSettingsPath, 'utf8'));
63
+ }
64
+
65
+ function saveCustomFormattingSettings(customFormattingSettings) {
66
+ let customFormattingSettingsPath = getCustomFormattingSettingsFilePath();
67
+ fs.writeFileSync(customFormattingSettingsPath, JSON.stringify(customFormattingSettings, null, 2));
68
+ }
69
+
70
+ function getCustomFormattingSettingsFilePath() {
71
+ let customFormattingSettingsPath = path.join(path.resolve('./'), CUSTOM_FORMATTING_SETTINGS_PATH);
72
+ if (!fs.existsSync(customFormattingSettingsPath)) {
73
+ if (!fs.existsSync(CUSTOMIZATION_DIRECTORY)) {
74
+ fs.mkdirSync(CUSTOMIZATION_DIRECTORY);
75
+ }
76
+ fs.writeFileSync(
77
+ customFormattingSettingsPath,
78
+ JSON.stringify(DEFAULT_CUSTOM_FORMATTING_SETTINGS, null, 2)
79
+ );
80
+ }
81
+ return customFormattingSettingsPath;
82
+ }
@@ -0,0 +1,6 @@
1
+ export const prerender = true;
2
+
3
+ /**
4
+ * Re-exported beacuse ../+server.js has routes that can't be prerendered
5
+ */
6
+ export { GET } from '../+server.js';
@@ -0,0 +1,92 @@
1
+ import preprocess from '@evidence-dev/preprocess';
2
+ import { error } from '@sveltejs/kit';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+
6
+ /**
7
+ * @typedef {Object} PageManifestNode
8
+ * @property {string | undefined} label
9
+ * @property {string | undefined} href
10
+ * @property {Record<string, PageManifestNode>} children
11
+ * @property {Record<string, any>} frontMatter
12
+ * @property {boolean} isTemplated
13
+ * @property {boolean} isPage
14
+ */
15
+
16
+ /**
17
+ * @param {Record<string, string>} pages
18
+ * @returns {PageManifestNode}
19
+ */
20
+ export function _buildPageManifest(pages) {
21
+ const fileTree = {
22
+ label: 'Home',
23
+ href: undefined,
24
+ children: {},
25
+ frontMatter: {},
26
+ isTemplated: false,
27
+ isPage: false
28
+ };
29
+ for (const [pagePath, pageContent] of Object.entries(pages)) {
30
+ const path = pagePath.replace('/src/pages/', '');
31
+ let node = fileTree;
32
+ for (const part of path.split('/')) {
33
+ if (part === '+page.md') {
34
+ // cover home page
35
+ if (path === '+page.md') {
36
+ node.href = '/';
37
+ } else if (!path.includes('[')) {
38
+ node.href = encodeURI('/' + path.replace('/+page.md', ''));
39
+ }
40
+ node.frontMatter = preprocess.parseFrontmatter(pageContent);
41
+ node.isPage = true;
42
+ } else {
43
+ const label = part.includes('[') ? undefined : part.replace(/_/g, ' ').replace(/-/g, ' ');
44
+ node = node.children[part] = node.children[part] ?? {
45
+ label,
46
+ href: undefined,
47
+ children: {},
48
+ frontMatter: {},
49
+ isTemplated: part.includes('['),
50
+ isPage: false
51
+ };
52
+ }
53
+ }
54
+ }
55
+ return fileTree;
56
+ }
57
+
58
+ export const prerender = true;
59
+
60
+ /**
61
+ * @type {import("@sveltejs/kit").RequestHandler}
62
+ */
63
+ export async function GET() {
64
+ const pages = {};
65
+
66
+ /** @param {string} dir */
67
+ async function recursiveReadDir(dir) {
68
+ const dirents = await fs.readdir(dir, { withFileTypes: true });
69
+ for (const dirent of dirents) {
70
+ if (dirent.isFile() && dirent.name.endsWith('.md')) {
71
+ const relative_path = path.join(dirent.parentPath ?? dirent.path ?? dir, dirent.name);
72
+ const content = await fs.readFile(relative_path, 'utf-8');
73
+ // regularize for windows
74
+ const normalized_path = '/' + path.normalize(relative_path).split(path.sep).join('/');
75
+ pages[normalized_path] = content;
76
+ } else if (dirent.isDirectory()) {
77
+ await recursiveReadDir(path.join(dir, dirent.name));
78
+ }
79
+ }
80
+ }
81
+
82
+ try {
83
+ await recursiveReadDir(path.join('src', 'pages'));
84
+
85
+ const fileTree = _buildPageManifest(pages);
86
+
87
+ return new Response(JSON.stringify(fileTree));
88
+ } catch (e) {
89
+ console.log('Failed to build pages manifest with error: ', e);
90
+ throw error(500, 'Failed to build pages manifest.');
91
+ }
92
+ }