@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.
- package/CHANGELOG.md +544 -0
- package/LICENSE.md +9 -0
- package/README.md +237 -2
- package/package.json +161 -3
- package/src/build-dev/cli/add/addServerHook.js +30 -0
- package/src/build-dev/cli/add/addTypeRef.js +19 -0
- package/src/build-dev/cli/add/updateGitIgnore.js +33 -0
- package/src/build-dev/cli/add/updatePackageJson.js +26 -0
- package/src/build-dev/cli/add/updateSvelteConfig.js +199 -0
- package/src/build-dev/cli/add/updateViteConfig.js +152 -0
- package/src/build-dev/cli/add.js +95 -0
- package/src/build-dev/cli/build.js +30 -0
- package/src/build-dev/cli/dev.js +49 -0
- package/src/build-dev/cli/index.js +14 -0
- package/src/build-dev/cli/upgrade.js +54 -0
- package/src/build-dev/svelte/index.js +54 -0
- package/src/build-dev/svelte/lib/isTargetFile.js +6 -0
- package/src/build-dev/svelte/metadatas.js +39 -0
- package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js +29 -0
- package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.spec.js +82 -0
- package/src/build-dev/svelte/processors/addScriptTags.js +17 -0
- package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.js +19 -0
- package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.spec.js +123 -0
- package/src/build-dev/svelte/processors/convertToHtml/processMarkdown.js +59 -0
- package/src/build-dev/svelte/processors/extractQueries/extractQueries.js +49 -0
- package/src/build-dev/svelte/processors/extractQueries/extractQueries.spec.js +54 -0
- package/src/build-dev/svelte/processors/highlight/highlight.js +52 -0
- package/src/build-dev/svelte/processors/highlight/highlight.spec.js +31 -0
- package/src/build-dev/svelte/processors/initializeUniversalSql.js +15 -0
- package/src/build-dev/svelte/processors/injectComponents/injectComponents.js +56 -0
- package/src/build-dev/svelte/processors/injectHighlightStyles.js +14 -0
- package/src/build-dev/svelte/processors/injectQueries/injectQueries.js +77 -0
- package/src/build-dev/svelte/processors/markRootLayoutPrerenderable.js +56 -0
- package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.js +17 -0
- package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.spec.js +21 -0
- package/src/build-dev/svelte/processors/transformQueries/transformQueries.js +58 -0
- package/src/build-dev/vite/config-virtual.js +24 -0
- package/src/build-dev/vite/constants.js +7 -0
- package/src/build-dev/vite/datasource-plugins/source-query-hmr.js +117 -0
- package/src/build-dev/vite/evidence-config.d.ts +6 -0
- package/src/build-dev/vite/index.js +214 -0
- package/src/build-dev/vite/layout-plugins/copy-to-layout.js +71 -0
- package/src/build-dev/vite/plugins.js +4 -0
- package/src/build-dev/vite/query-directory-hmr.js +32 -0
- package/src/build-dev/vite/sidecar-app.js +79 -0
- package/src/build-dev/vite/virtuals/QuerySSR.svelte +13 -0
- package/src/build-dev/vite/virtuals/__tests/proxyStack.spec.js +52 -0
- package/src/build-dev/vite/virtuals/bootstrapContexts.svelte.js +27 -0
- package/src/build-dev/vite/virtuals/browser/static-assets.js +3 -0
- package/src/build-dev/vite/virtuals/contextKeys.js +3 -0
- package/src/build-dev/vite/virtuals/initUsql.js +23 -0
- package/src/build-dev/vite/virtuals/node/static-assets.js +56 -0
- package/src/build-dev/vite/virtuals/proxyStack.js +37 -0
- package/src/build-dev/vite/virtuals/queries.svelte.js +150 -0
- package/src/build-dev/vite/virtuals/ssrHook.svelte.js +60 -0
- package/src/build-dev/vite/virtuals/updateQueryContext.svelte.js +31 -0
- package/src/build-dev/vite/virtuals-dynamic.js +12 -0
- package/src/cli.js +5 -0
- package/src/cli.root.js +90 -0
- package/src/configuration/cli/create.js +20 -0
- package/src/configuration/cli/index.js +12 -0
- package/src/configuration/createEvidenceConfig.js +51 -0
- package/src/configuration/getEvidenceConfig.js +60 -0
- package/src/configuration/getEvidenceConfig.legacy.js +27 -0
- package/src/configuration/index.js +1 -0
- package/src/configuration/schemas/config.schema.js +11 -0
- package/src/configuration/schemas/deployment.schema.js +18 -0
- package/src/configuration/schemas/deployment.schema.spec.js +34 -0
- package/src/configuration/updateEvidenceConfig.js +25 -0
- package/src/legacy-compat.js +8 -0
- package/src/lib/EvidenceError.js +35 -0
- package/src/lib/b64Deep.js +55 -0
- package/src/lib/check-import.js +12 -0
- package/src/lib/cleanZodErrors.js +22 -0
- package/src/lib/cli/cmdFail.js +33 -0
- package/src/lib/cli/notices.js +44 -0
- package/src/lib/debug.js +40 -0
- package/src/lib/flows.d.ts +21 -0
- package/src/lib/forceLink.js +28 -0
- package/src/lib/history/index.js +70 -0
- package/src/lib/history/types.d.ts +8 -0
- package/src/lib/pkg-types.helpers.js +16 -0
- package/src/lib/processing-queue.js +96 -0
- package/src/lib/processing-queue.spec.js +148 -0
- package/src/lib/projectPaths.js +29 -0
- package/src/lib/projectRoot.js +12 -0
- package/src/lib/sharedPromise.js +77 -0
- package/src/lib/sharedPromise.spec.js +23 -0
- package/src/lib/strict.js +19 -0
- package/src/lib/tests/fs.js +21 -0
- package/src/lib/tokens.js +7 -0
- package/src/lib/unnest-zod-error.js +14 -0
- package/src/logger/EvidenceLogger.js +164 -0
- package/src/logger/index.js +4 -0
- package/src/meta.js +8 -0
- package/src/plugins/cli/index.js +13 -0
- package/src/plugins/cli/install.js +26 -0
- package/src/plugins/cli/install.prompt.js +121 -0
- package/src/plugins/components/getAllComponents.js +82 -0
- package/src/plugins/components/getComponentsInPlugin.js +33 -0
- package/src/plugins/components/loadComponentPlugins.js +48 -0
- package/src/plugins/components/loaders/file-loader.js +125 -0
- package/src/plugins/components/loaders/manifest-loader.js +53 -0
- package/src/plugins/components/schemas/component-manifest.schema.js +5 -0
- package/src/plugins/components/types.d.ts +21 -0
- package/src/plugins/components/validateOverrides.js +30 -0
- package/src/plugins/components/validateOverrides.spec.js +32 -0
- package/src/plugins/datasources/Datasources.js +85 -0
- package/src/plugins/datasources/Datasources.spec.js +78 -0
- package/src/plugins/datasources/SourceResultCache.js +62 -0
- package/src/plugins/datasources/buildSourceDirectoryProxy.js +21 -0
- package/src/plugins/datasources/cli/edit/Options.fixtures.js +69 -0
- package/src/plugins/datasources/cli/edit/Options.js +253 -0
- package/src/plugins/datasources/cli/edit/Options.spec.js +496 -0
- package/src/plugins/datasources/cli/edit/editSourceConfig.js +201 -0
- package/src/plugins/datasources/cli/edit.js +20 -0
- package/src/plugins/datasources/cli/edit.prompt.js +100 -0
- package/src/plugins/datasources/cli/index.js +18 -0
- package/src/plugins/datasources/cli/sources.js +77 -0
- package/src/plugins/datasources/evalSources.js +257 -0
- package/src/plugins/datasources/getPublishedDatasources.js +62 -0
- package/src/plugins/datasources/index.js +6 -0
- package/src/plugins/datasources/loadSourceConfig.js +147 -0
- package/src/plugins/datasources/loadSourceConfig.spec.js +127 -0
- package/src/plugins/datasources/loadSourcePlugins.js +49 -0
- package/src/plugins/datasources/loadSources.js +48 -0
- package/src/plugins/datasources/schemas/datasource.schema.js +52 -0
- package/src/plugins/datasources/schemas/datasourceCache.schema.js +3 -0
- package/src/plugins/datasources/schemas/datasourcePlugin.schema.js +37 -0
- package/src/plugins/datasources/schemas/datasourcePluginOptions.schema.js +64 -0
- package/src/plugins/datasources/schemas/index.js +2 -0
- package/src/plugins/datasources/schemas/manifest.schema.js +8 -0
- package/src/plugins/datasources/schemas/results.schema.js +126 -0
- package/src/plugins/datasources/sub-source-vars.js +55 -0
- package/src/plugins/datasources/sub-source-vars.spec.js +46 -0
- package/src/plugins/datasources/types.d.ts +53 -0
- package/src/plugins/datasources/updateManifest.js +115 -0
- package/src/plugins/datasources/updateManifest.spec.js +200 -0
- package/src/plugins/datasources/wrapSimpleConnector.js +95 -0
- package/src/plugins/datasources/writeSourceConfig.js +60 -0
- package/src/plugins/index.js +2 -0
- package/src/plugins/layouts/copyMethods/index.js +10 -0
- package/src/plugins/layouts/copyMethods/svelte.js +53 -0
- package/src/plugins/layouts/copyMethods/sveltekit.js +75 -0
- package/src/plugins/layouts/copyToLayout.js +106 -0
- package/src/plugins/layouts/loadLayoutPlugin.js +23 -0
- package/src/plugins/loadPluginPackage.js +77 -0
- package/src/plugins/schemas/index.js +1 -0
- package/src/plugins/schemas/plugin-config.schema.js +41 -0
- package/src/plugins/schemas/plugin-config.schema.spec.js +21 -0
- package/src/plugins/schemas/plugin-package.schema.js +154 -0
- package/src/plugins/schemas/plugin-package.schema.spec.js +30 -0
- package/src/query-builder.d.ts +1 -0
- package/src/query-builder.js +1 -0
- package/src/types/duckdb-wellknown.d.ts +35 -0
- package/src/types/html-attributes.ts +13 -0
- package/src/types/index.d.ts +18 -0
- package/src/types/mosaic-sql.d.ts +27 -0
- package/src/types/sveltekit-autoimport.d.ts +13 -0
- package/src/types/virtuals.d.ts +113 -0
- package/src/types.d.ts +1 -0
- package/src/usql/index.d.ts +4 -0
- package/src/usql/index.js +3 -0
- package/src/usql/query/Query.js +1391 -0
- package/src/usql/query/Query.spec.js +827 -0
- package/src/usql/query/queryScore.js +92 -0
- package/src/usql/query/queryScore.spec.js +26 -0
- package/src/usql/query/sterilizeQuery.js +89 -0
- package/src/usql/query/sterilizeQuery.spec.js +172 -0
- package/src/usql/setTrackProxy/setTrackProxy.js +80 -0
- package/src/usql/setTrackProxy/setTrackProxy.spec.js +166 -0
- package/src/usql/types.ts +86 -0
- package/src/usql/utilities/duckdbSerialize.js +29 -0
- package/src/usql/utilities/index.js +2 -0
- package/src/usql/utilities/resolveMaybePromise.js +30 -0
- package/src/utils/batchUp.js +22 -0
- package/src/utils/batchUp.spec.js +69 -0
- package/src/utils/browserDebounce.js +16 -0
- package/src/utils/index.js +7 -0
- package/src/utils/svelte/addBasePath.js +74 -0
- package/src/utils/svelte/addBasePath.spec.js +108 -0
- package/src/utils/svelte/index.js +13 -0
- package/src/utils/svelte/inputs.js +138 -0
- package/tsconfig.json +22 -0
- package/vite.config.js +3 -0
- package/vitest.config.js +15 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {number} length
|
|
3
|
+
* @param {import('../index.js').DescribeResultRow[]} columns
|
|
4
|
+
* @returns {number}
|
|
5
|
+
*/
|
|
6
|
+
export const getQueryScore = (length, columns) => {
|
|
7
|
+
const scorePerRow = columns.reduce(
|
|
8
|
+
(a, v) => a + columnTypeToScore(v.column_type),
|
|
9
|
+
columns.length * 4 // include overhead for each column
|
|
10
|
+
);
|
|
11
|
+
return Math.abs(scorePerRow * length);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Util Functions
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} columnType
|
|
18
|
+
* @returns {columnType is `STRUCT(${string})` | `${string}[]`}
|
|
19
|
+
*/
|
|
20
|
+
function isObjectType(columnType) {
|
|
21
|
+
return columnType.startsWith('STRUCT') || columnType.endsWith('[]');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} columnType
|
|
26
|
+
* @returns {columnType is `DECIMAL(${number},${number})`}
|
|
27
|
+
*/
|
|
28
|
+
function isDecimalType(columnType) {
|
|
29
|
+
return columnType.startsWith('DECIMAL');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param {import('../../types/duckdb-wellknown.js').DuckDBColumnType} columnType
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
function columnTypeToScore(columnType) {
|
|
38
|
+
const roundedColumnScores = {
|
|
39
|
+
string: 2 * 15,
|
|
40
|
+
number: 12,
|
|
41
|
+
boolean: 4,
|
|
42
|
+
date: 48
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
if (isObjectType(columnType)) {
|
|
46
|
+
console.warn(
|
|
47
|
+
'[!] Evidence does not support DuckDB Struct or Array\nIf you need to use one, convert it to JSON in your query, and then manually parse it in your project'
|
|
48
|
+
);
|
|
49
|
+
return roundedColumnScores.string;
|
|
50
|
+
}
|
|
51
|
+
if (isDecimalType(columnType)) return roundedColumnScores.number;
|
|
52
|
+
|
|
53
|
+
switch (columnType) {
|
|
54
|
+
case 'BOOLEAN':
|
|
55
|
+
return roundedColumnScores.boolean;
|
|
56
|
+
case 'BIGINT':
|
|
57
|
+
case 'DOUBLE':
|
|
58
|
+
case 'FLOAT':
|
|
59
|
+
case 'INTEGER':
|
|
60
|
+
case 'SMALLINT':
|
|
61
|
+
case 'TINYINT':
|
|
62
|
+
case 'UBIGINT':
|
|
63
|
+
case 'UINTEGER':
|
|
64
|
+
case 'USMALLINT':
|
|
65
|
+
case 'UTINYINT':
|
|
66
|
+
case 'HUGEINT':
|
|
67
|
+
return roundedColumnScores.number;
|
|
68
|
+
case 'UUID':
|
|
69
|
+
case 'VARCHAR':
|
|
70
|
+
return roundedColumnScores.string;
|
|
71
|
+
case 'DATE':
|
|
72
|
+
case 'TIMESTAMP':
|
|
73
|
+
case 'TIMESTAMP_S':
|
|
74
|
+
case 'TIMESTAMP_MS':
|
|
75
|
+
case 'TIMESTAMP_NS':
|
|
76
|
+
case 'TIMESTAMP WITH TIME ZONE':
|
|
77
|
+
return roundedColumnScores.date;
|
|
78
|
+
|
|
79
|
+
// the badlands
|
|
80
|
+
// we should probably convert these in the client library too
|
|
81
|
+
case 'INTERVAL': // return 'Uint32Array';
|
|
82
|
+
case 'TIME':
|
|
83
|
+
case 'TIME WITH TIME ZONE': // return 'bigint';
|
|
84
|
+
case 'BLOB':
|
|
85
|
+
case 'BIT': // return 'Uint8Array';
|
|
86
|
+
return roundedColumnScores.string;
|
|
87
|
+
default:
|
|
88
|
+
// columnType should be `never`
|
|
89
|
+
console.error(`Column type ${columnType} is not supported`);
|
|
90
|
+
return roundedColumnScores.string;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { getQueryScore } from './queryScore';
|
|
3
|
+
|
|
4
|
+
describe('queryScore', () => {
|
|
5
|
+
it('should return 8 for a Query with a single boolean column, and 1 row', async () => {
|
|
6
|
+
const cols = [{ column_name: 'bool_col', column_type: 'BOOLEAN' }];
|
|
7
|
+
const len = 1;
|
|
8
|
+
|
|
9
|
+
const result = getQueryScore(len, cols);
|
|
10
|
+
|
|
11
|
+
expect(result).toBe(8);
|
|
12
|
+
});
|
|
13
|
+
it('should return x for a Query with 5 string columms, and 5 rows', async () => {
|
|
14
|
+
const cols = [
|
|
15
|
+
{ column_name: 'var_char_col1', column_type: 'VARCHAR' },
|
|
16
|
+
{ column_name: 'var_char_col2', column_type: 'VARCHAR' },
|
|
17
|
+
{ column_name: 'var_char_col3', column_type: 'VARCHAR' },
|
|
18
|
+
{ column_name: 'var_char_col4', column_type: 'VARCHAR' },
|
|
19
|
+
{ column_name: 'var_char_col5', column_type: 'VARCHAR' }
|
|
20
|
+
];
|
|
21
|
+
const len = 5;
|
|
22
|
+
const result = getQueryScore(len, cols);
|
|
23
|
+
|
|
24
|
+
expect(result).toBe(850);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const unescapedComment = /--([^']|'.*')+$/;
|
|
2
|
+
const inlinedMultilineComments = /(\/\*.*\*\/)/g;
|
|
3
|
+
/**
|
|
4
|
+
* This function ensures that a query can safely be sent to DuckDB
|
|
5
|
+
* It adds a newline to the query if it ends in a comment, and
|
|
6
|
+
* ensures that the statement does not end with a semicolon
|
|
7
|
+
*
|
|
8
|
+
* It is not responsible for removing semicolons in
|
|
9
|
+
* subqueries or nested expressions, only the root expression
|
|
10
|
+
*
|
|
11
|
+
* @param {string} query
|
|
12
|
+
*/
|
|
13
|
+
export const sterilizeQuery = (query) => {
|
|
14
|
+
const lines = query.split('\n');
|
|
15
|
+
let inMultilineComment = false;
|
|
16
|
+
// iterate in reverse order because we only want to find the last failing line
|
|
17
|
+
for (let i = lines.length; i > 0; i--) {
|
|
18
|
+
// grab the line
|
|
19
|
+
let line = lines[i - 1];
|
|
20
|
+
// keep track of any content we remove from the end of the line that we want to add back
|
|
21
|
+
let append = '';
|
|
22
|
+
|
|
23
|
+
// detect if there are any inlined multiline comments
|
|
24
|
+
const multilineMatches = Array.from(line.matchAll(inlinedMultilineComments));
|
|
25
|
+
for (const multilineMatch of multilineMatches) {
|
|
26
|
+
// remove all inline multiline comments (these are re-added at the end)
|
|
27
|
+
const before = line.slice(0, multilineMatch.index);
|
|
28
|
+
const after = line.slice(multilineMatch.index + multilineMatch[0].length);
|
|
29
|
+
line = `${before}${after}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// if we are in a multiline comment, we don't want to do anything
|
|
33
|
+
if (inMultilineComment) {
|
|
34
|
+
// TODO: Does duckdb handle nested comments? This does not
|
|
35
|
+
if (line.includes('/*')) {
|
|
36
|
+
// we are at the start
|
|
37
|
+
inMultilineComment = false; // we are no longer in a multiline comment
|
|
38
|
+
const parts = line.split('/*');
|
|
39
|
+
line = parts.slice(0, -1).join('/*'); // remove the last part of the comment
|
|
40
|
+
// (assume that "/* /* xx" is only "/* xx" as the comment)
|
|
41
|
+
append += '/*' + parts.slice(-1); // ensure we re-add the comment at the end
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (line.trim().endsWith('*/')) {
|
|
45
|
+
// we are at the end of a multiline comment
|
|
46
|
+
inMultilineComment = true;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Handle single line comments
|
|
51
|
+
|
|
52
|
+
const match = unescapedComment.exec(line);
|
|
53
|
+
// If we have an unescaped comment
|
|
54
|
+
if (match) {
|
|
55
|
+
// get the content before the comment, check if it ends with a semicolon
|
|
56
|
+
const before = line.slice(0, match.index);
|
|
57
|
+
const trimmed = before.trimEnd();
|
|
58
|
+
if (trimmed.endsWith(';')) {
|
|
59
|
+
// reconstruct the line without the ;
|
|
60
|
+
const after = line.slice(match.index);
|
|
61
|
+
const mid = before.slice(trimmed.length, before.length);
|
|
62
|
+
line = `${before.slice(0, -1 + -1 * (before.length - trimmed.length))}${mid}${after}`;
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
// no comments, we can just remove trailing semicolons
|
|
66
|
+
const trimmed = line.trimEnd();
|
|
67
|
+
if (trimmed.endsWith(';')) {
|
|
68
|
+
const lastIdx = line.lastIndexOf(';');
|
|
69
|
+
line = line.slice(0, lastIdx) + line.slice(lastIdx + 1);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// re-add multiline comments that may have been removed at the beginning
|
|
74
|
+
for (const multilineMatch of multilineMatches) {
|
|
75
|
+
const before = line.slice(0, multilineMatch.index);
|
|
76
|
+
const after = line.slice(multilineMatch.index);
|
|
77
|
+
line = `${before}${multilineMatch[0]}${after}`;
|
|
78
|
+
}
|
|
79
|
+
// if we have modified the line, update and escape the loop ( we don't handle inline statements - that's on the user )
|
|
80
|
+
if (line !== lines[i - 1]) {
|
|
81
|
+
lines[i - 1] = line + append;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
lines.push(''); // add a blank newline at the end
|
|
87
|
+
|
|
88
|
+
return lines.join('\n');
|
|
89
|
+
};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { sterilizeQuery } from './sterilizeQuery.js';
|
|
3
|
+
|
|
4
|
+
describe('seterilizeQuery', () => {
|
|
5
|
+
it('should not change queries with no comments or semicolons', () => {
|
|
6
|
+
const query = 'SELECT 1'.trim();
|
|
7
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
8
|
+
});
|
|
9
|
+
it('should not remove evidence auto-injected comments', () => {
|
|
10
|
+
const query = `
|
|
11
|
+
---- Data queryId queryHash
|
|
12
|
+
SELECT * FROM table
|
|
13
|
+
`.trim();
|
|
14
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
15
|
+
});
|
|
16
|
+
it('should add a newline if the last line is a comment', () => {
|
|
17
|
+
const query = `
|
|
18
|
+
SELECT * FROM table
|
|
19
|
+
-- Data queryId queryHash
|
|
20
|
+
`.trim();
|
|
21
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
22
|
+
});
|
|
23
|
+
it('should add a newline if the last line has a comment', () => {
|
|
24
|
+
const query = `
|
|
25
|
+
SELECT * FROM table -- Data queryId queryHash
|
|
26
|
+
`.trim();
|
|
27
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
28
|
+
});
|
|
29
|
+
it('should remove any trailing semicolons', () => {
|
|
30
|
+
const query = `
|
|
31
|
+
SELECT *
|
|
32
|
+
FROM table;`;
|
|
33
|
+
const adjustedQuery = `
|
|
34
|
+
SELECT *
|
|
35
|
+
FROM table`;
|
|
36
|
+
expect(sterilizeQuery(query)).toBe(adjustedQuery + '\n');
|
|
37
|
+
});
|
|
38
|
+
it('should remove any trailing semicolons, while maintaining any comments', () => {
|
|
39
|
+
const query = `
|
|
40
|
+
SELECT *
|
|
41
|
+
FROM table; -- comment
|
|
42
|
+
`;
|
|
43
|
+
const adjustedQuery = `
|
|
44
|
+
SELECT *
|
|
45
|
+
FROM table -- comment
|
|
46
|
+
`;
|
|
47
|
+
expect(sterilizeQuery(query)).toBe(adjustedQuery + '\n');
|
|
48
|
+
});
|
|
49
|
+
it('should ignore semicolons inside strings', () => {
|
|
50
|
+
const query = `
|
|
51
|
+
SELECT ';' as valid
|
|
52
|
+
FROM table -- comment
|
|
53
|
+
`;
|
|
54
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
55
|
+
});
|
|
56
|
+
it('should ignore semicolons inside strings when they are not alone', () => {
|
|
57
|
+
const query = `
|
|
58
|
+
SELECT 'SELECT 1;' as valid
|
|
59
|
+
FROM table -- comment
|
|
60
|
+
`;
|
|
61
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
62
|
+
});
|
|
63
|
+
it('should ignore semicolons inside strings, but still remove a trailing semicolon', () => {
|
|
64
|
+
const query = `
|
|
65
|
+
SELECT ';' as valid;
|
|
66
|
+
`.trim();
|
|
67
|
+
expect(sterilizeQuery(query)).toBe(query.substring(0, query.length - 1) + '\n');
|
|
68
|
+
});
|
|
69
|
+
it('should ignore trailing semicolons that appear in comments', () => {
|
|
70
|
+
const query = `
|
|
71
|
+
SELECT 1; -- ;
|
|
72
|
+
`.trim();
|
|
73
|
+
const expectedQuery = `
|
|
74
|
+
SELECT 1 -- ;
|
|
75
|
+
`.trim();
|
|
76
|
+
expect(sterilizeQuery(query)).toBe(expectedQuery + '\n');
|
|
77
|
+
});
|
|
78
|
+
it('should ignore trailing semicolons that appear in comments and are quoted', () => {
|
|
79
|
+
const query = `
|
|
80
|
+
SELECT 1, 'stringliteral'; -- '; ; ;'
|
|
81
|
+
`.trim();
|
|
82
|
+
const expectedQuery = `
|
|
83
|
+
SELECT 1, 'stringliteral' -- '; ; ;'
|
|
84
|
+
`.trim();
|
|
85
|
+
expect(sterilizeQuery(query)).toBe(expectedQuery + '\n');
|
|
86
|
+
});
|
|
87
|
+
it('should ignore semicolons that appear in comments, and remove the trailing semicolon', () => {
|
|
88
|
+
const query = `
|
|
89
|
+
SELECT 1, '-- ;';
|
|
90
|
+
`.trim();
|
|
91
|
+
const expectedQuery = `
|
|
92
|
+
SELECT 1, '-- ;'
|
|
93
|
+
`.trim();
|
|
94
|
+
expect(sterilizeQuery(query)).toBe(expectedQuery + '\n');
|
|
95
|
+
});
|
|
96
|
+
it('should ignore semicolons that appear in comments', () => {
|
|
97
|
+
const query = `
|
|
98
|
+
SELECT 1, '-- ;'
|
|
99
|
+
`.trim();
|
|
100
|
+
const expectedQuery = `
|
|
101
|
+
SELECT 1, '-- ;'
|
|
102
|
+
`.trim();
|
|
103
|
+
expect(sterilizeQuery(query)).toBe(expectedQuery + '\n');
|
|
104
|
+
});
|
|
105
|
+
it('should work with all cases', () => {
|
|
106
|
+
const query = `
|
|
107
|
+
SELECT ';' as valid, ' ; ; ' as also_valid; -- ;
|
|
108
|
+
`.trim();
|
|
109
|
+
const expectedQuery = `
|
|
110
|
+
SELECT ';' as valid, ' ; ; ' as also_valid -- ;
|
|
111
|
+
`.trim();
|
|
112
|
+
expect(sterilizeQuery(query)).toBe(expectedQuery + '\n');
|
|
113
|
+
});
|
|
114
|
+
it('should ignore semicolons in multiline comments (on one line)', () => {
|
|
115
|
+
const query = `SELECT 1 /* ; */`;
|
|
116
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
117
|
+
});
|
|
118
|
+
it('should ignore semicolons in multiline comments (on multiple line)', () => {
|
|
119
|
+
const query = `SELECT 1 /* ;
|
|
120
|
+
*/`;
|
|
121
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
122
|
+
});
|
|
123
|
+
it('should ignore semicolons in multiline comments (on single line), but remove trailing', () => {
|
|
124
|
+
const query = `SELECT 1; /* ; */`;
|
|
125
|
+
const expected = `SELECT 1 /* ; */` + '\n';
|
|
126
|
+
expect(sterilizeQuery(query)).toBe(expected);
|
|
127
|
+
});
|
|
128
|
+
it('should ignore semicolons in multiline comments (on multiple line), but remove trailing', () => {
|
|
129
|
+
const query = `
|
|
130
|
+
SELECT 1; /* ;
|
|
131
|
+
*/`;
|
|
132
|
+
const expected =
|
|
133
|
+
`
|
|
134
|
+
SELECT 1 /* ;
|
|
135
|
+
*/` + '\n';
|
|
136
|
+
expect(sterilizeQuery(query)).toBe(expected);
|
|
137
|
+
});
|
|
138
|
+
it('should ignore semicolons in inline multiline comments', () => {
|
|
139
|
+
const query = `SELECT /* ; */ 1 `;
|
|
140
|
+
expect(sterilizeQuery(query)).toBe(query + '\n');
|
|
141
|
+
});
|
|
142
|
+
it('should ignore semicolons in inline multiline comments, but remove trailing', () => {
|
|
143
|
+
const query = `SELECT /* ; */ 1;`;
|
|
144
|
+
expect(sterilizeQuery(query)).toBe(query.slice(0, -1) + '\n');
|
|
145
|
+
});
|
|
146
|
+
it('should handle trailing semicolons that appear at the end of a multiline comment across multiple lines', () => {
|
|
147
|
+
const query = `
|
|
148
|
+
SELECT 1 /*
|
|
149
|
+
|
|
150
|
+
*/;
|
|
151
|
+
`;
|
|
152
|
+
const expected = `
|
|
153
|
+
SELECT 1 /*
|
|
154
|
+
|
|
155
|
+
*/
|
|
156
|
+
`;
|
|
157
|
+
expect(sterilizeQuery(query)).toBe(expected + '\n');
|
|
158
|
+
});
|
|
159
|
+
it('should handle trailing semicolons that appear at the end of a multiline comment across multiple lines, even when the comment contains semicolons', () => {
|
|
160
|
+
const query = `
|
|
161
|
+
SELECT 1 /*;
|
|
162
|
+
;
|
|
163
|
+
;*/;
|
|
164
|
+
`;
|
|
165
|
+
const expected = `
|
|
166
|
+
SELECT 1 /*;
|
|
167
|
+
;
|
|
168
|
+
;*/
|
|
169
|
+
`;
|
|
170
|
+
expect(sterilizeQuery(query)).toBe(expected + '\n');
|
|
171
|
+
});
|
|
172
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export const Unset = Symbol('Unset');
|
|
2
|
+
export const IsSetTracked = Symbol('IsSetTracked');
|
|
3
|
+
const GetModKeys = Symbol('GetModKeys');
|
|
4
|
+
const GetOwnKey = Symbol('GetOwnKey');
|
|
5
|
+
const GetOwnPath = Symbol('GetOwnPath');
|
|
6
|
+
const GetParent = Symbol('GetParent');
|
|
7
|
+
|
|
8
|
+
export const setTrackProxy = (
|
|
9
|
+
/** @type {Record<string|number|symbol,string>} */ defaultStringMap = {},
|
|
10
|
+
/** @type {any} */ root = {},
|
|
11
|
+
/** @type {any|undefined} */ parent = undefined,
|
|
12
|
+
/** @type {string|number|symbol|undefined} */ ownKey = undefined
|
|
13
|
+
) => {
|
|
14
|
+
if (parent && !parent[IsSetTracked]) throw new Error(`SetTracked parent must be SetTracked`);
|
|
15
|
+
|
|
16
|
+
const wrapped = Object.assign(() => {}, root ?? {});
|
|
17
|
+
|
|
18
|
+
/** @type {Array<string|number|symbol>} */
|
|
19
|
+
const modifiedKeys = Object.keys(wrapped);
|
|
20
|
+
const self = new Proxy(wrapped, {
|
|
21
|
+
get(target, prop) {
|
|
22
|
+
switch (prop) {
|
|
23
|
+
case Unset:
|
|
24
|
+
return !parent?.[GetModKeys].includes(ownKey);
|
|
25
|
+
case GetModKeys:
|
|
26
|
+
return modifiedKeys;
|
|
27
|
+
case GetOwnKey:
|
|
28
|
+
return ownKey;
|
|
29
|
+
case GetParent:
|
|
30
|
+
return parent;
|
|
31
|
+
case GetOwnPath: {
|
|
32
|
+
const path = [ownKey];
|
|
33
|
+
let tmpParent = parent;
|
|
34
|
+
while (tmpParent !== undefined) {
|
|
35
|
+
path.unshift(tmpParent[GetOwnKey]);
|
|
36
|
+
tmpParent = tmpParent[GetParent];
|
|
37
|
+
}
|
|
38
|
+
return path.join('.');
|
|
39
|
+
}
|
|
40
|
+
case IsSetTracked:
|
|
41
|
+
return true;
|
|
42
|
+
case 'toJSON':
|
|
43
|
+
return () => ({ ...target });
|
|
44
|
+
case 'toString':
|
|
45
|
+
case 'toPrimitive':
|
|
46
|
+
case Symbol.toPrimitive:
|
|
47
|
+
if (self[Unset]) {
|
|
48
|
+
if (ownKey && ownKey in defaultStringMap) return () => defaultStringMap[ownKey];
|
|
49
|
+
return () => ``;
|
|
50
|
+
} else return root.toString.bind(root);
|
|
51
|
+
default:
|
|
52
|
+
if (!(prop in target)) {
|
|
53
|
+
target[prop] = setTrackProxy(defaultStringMap, undefined, self, prop);
|
|
54
|
+
}
|
|
55
|
+
return target[prop];
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
set(target, prop, value) {
|
|
59
|
+
modifiedKeys.push(prop);
|
|
60
|
+
if (typeof value === 'object') {
|
|
61
|
+
value = setTrackProxy(defaultStringMap, value, self, prop);
|
|
62
|
+
}
|
|
63
|
+
target[prop] = value;
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return self;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {TemplateStringsArray} strings
|
|
73
|
+
* @param {...any} args
|
|
74
|
+
* @returns {boolean}
|
|
75
|
+
*/
|
|
76
|
+
export const hasUnsetValues = (strings, ...args) => {
|
|
77
|
+
const unsetValues = args.filter((arg) => arg?.[Unset]);
|
|
78
|
+
if (unsetValues.length === 0) return false;
|
|
79
|
+
return true;
|
|
80
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { IsSetTracked, Unset, hasUnsetValues, setTrackProxy } from './setTrackProxy.js';
|
|
3
|
+
|
|
4
|
+
describe('setTrackProxy', () => {
|
|
5
|
+
describe('function emulation', () => {
|
|
6
|
+
it('should be callable, even if a function is not assigned', () => {
|
|
7
|
+
const v = setTrackProxy();
|
|
8
|
+
expect(v()).toBeUndefined();
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
describe('stringification', () => {
|
|
12
|
+
it('should respect default stringification for set values', () => {
|
|
13
|
+
// v is a bare object (e.g. nothing set on it)
|
|
14
|
+
const v = setTrackProxy();
|
|
15
|
+
// unset values should be represented as (select null where 0) to prevent duckdb syntax errors
|
|
16
|
+
|
|
17
|
+
// It should not interfere with stringification of set values
|
|
18
|
+
v.a.b.c = { x: 5 };
|
|
19
|
+
expect(v.a.b.c.toString()).toEqual({}.toString());
|
|
20
|
+
|
|
21
|
+
v.a.b.c.y = 'Hello World';
|
|
22
|
+
expect(v.a.b.c.y.toString()).toEqual('Hello World');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should stringify to empty string when not provided with a map', () => {
|
|
26
|
+
// v is a bare object (e.g. nothing set on it)
|
|
27
|
+
const v = setTrackProxy();
|
|
28
|
+
// unset values should be represented as (select null where 0) to prevent duckdb syntax errors
|
|
29
|
+
expect(v.a.b.c.toString()).toEqual('');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should stringify to the values in the defaultStringMap when dealing with unsets', () => {
|
|
33
|
+
const v = setTrackProxy({ value: '(select null where 0)', label: 'Hello World' });
|
|
34
|
+
expect(v.a.b.c.toString()).toEqual('');
|
|
35
|
+
expect(v.a.b.c.label.toString()).toEqual('Hello World');
|
|
36
|
+
expect(v.a.b.c.value.toString()).toEqual('(select null where 0)');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should JSON serialize properly', () => {
|
|
40
|
+
const v = setTrackProxy();
|
|
41
|
+
v.abc = 'something';
|
|
42
|
+
const foo = { abc: 'something' };
|
|
43
|
+
expect(JSON.stringify(v)).toEqual(JSON.stringify(foo));
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should identify itself with the IsSetTracked symbol', () => {
|
|
48
|
+
const v = setTrackProxy();
|
|
49
|
+
expect(v[IsSetTracked]).toBeTruthy();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should let you retrieve arbitrarily deep values', () => {
|
|
53
|
+
// v is a bare object (e.g. nothing set on it)
|
|
54
|
+
const v = setTrackProxy();
|
|
55
|
+
// users should be able to `.` on everything, this prevents syntax errors
|
|
56
|
+
expect(v.a.b.c).toBeDefined();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should let you set values arbitrarily deep', () => {
|
|
60
|
+
const v = setTrackProxy();
|
|
61
|
+
// a and b have not been set, but we can modify a.b.c and retain that value
|
|
62
|
+
v.a.b.c = 5;
|
|
63
|
+
expect(v.a.b.c).toBe(5);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should tell you if a key has been modified', () => {
|
|
67
|
+
const v = setTrackProxy();
|
|
68
|
+
v.a.b.c = 5;
|
|
69
|
+
|
|
70
|
+
// `c` was set
|
|
71
|
+
expect(v.a.b.c[Unset]).toBeFalsy();
|
|
72
|
+
// `e` was not set
|
|
73
|
+
expect(v.a.b.e[Unset]).toBeTruthy();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should not think a key has been set if you have only accessed it', () => {
|
|
77
|
+
const v = setTrackProxy();
|
|
78
|
+
// Access b but don't mutate anything
|
|
79
|
+
v.a.b;
|
|
80
|
+
|
|
81
|
+
// we accessed b, so it may exist on the a object
|
|
82
|
+
// but we never gave it a value, so it should remain
|
|
83
|
+
// unset
|
|
84
|
+
expect(v.a.b[Unset]).toBeTruthy();
|
|
85
|
+
|
|
86
|
+
v.a.b = { x: 5 };
|
|
87
|
+
// a was not directly modified, only b
|
|
88
|
+
expect(v.a[Unset]).toBeTruthy();
|
|
89
|
+
// b was directly modified, and is now set
|
|
90
|
+
expect(v.a.b[Unset]).toBeFalsy();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should wrap any child objects in itself', () => {
|
|
94
|
+
const v = setTrackProxy();
|
|
95
|
+
v.a.b = { x: 1 };
|
|
96
|
+
// b was directly modified
|
|
97
|
+
expect(v.a.b[IsSetTracked]).toBeTruthy();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should mark any existing values as set when receving an object', () => {
|
|
101
|
+
const v = setTrackProxy();
|
|
102
|
+
v.a.b = { x: 1 };
|
|
103
|
+
|
|
104
|
+
// b was set with a value at x, so we should consider it modified
|
|
105
|
+
expect(v.a.b.x[Unset]).toBeFalsy();
|
|
106
|
+
|
|
107
|
+
// x was set as a bare object, but should be made into this proxy
|
|
108
|
+
// so that we can check if it's y property has been set
|
|
109
|
+
expect(v.a.b.y[Unset]).toBeTruthy();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should not break when JSON serializing', () => {
|
|
113
|
+
const v = setTrackProxy();
|
|
114
|
+
expect(() => JSON.stringify(v)).not.toThrow();
|
|
115
|
+
expect(() => v.toJSON()).not.toThrow();
|
|
116
|
+
});
|
|
117
|
+
it('should not break when primative serializing', () => {
|
|
118
|
+
const v = setTrackProxy();
|
|
119
|
+
|
|
120
|
+
expect(() => v.toPrimitive()).not.toThrow();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('hasUnsetValues', () => {
|
|
125
|
+
it('should return true for a string without interpolation', () => {
|
|
126
|
+
expect(hasUnsetValues``).toBeFalsy();
|
|
127
|
+
});
|
|
128
|
+
it('should return true for a string without input interpolation', () => {
|
|
129
|
+
expect(hasUnsetValues`${1}`).toBeFalsy();
|
|
130
|
+
});
|
|
131
|
+
describe('root props', () => {
|
|
132
|
+
it('should return true for a string with input interpolation (set)', () => {
|
|
133
|
+
const v = setTrackProxy();
|
|
134
|
+
v.a = 1;
|
|
135
|
+
expect(hasUnsetValues`${v.a}`).toBeFalsy();
|
|
136
|
+
});
|
|
137
|
+
it('should return false for a string with input interpolation (unset)', () => {
|
|
138
|
+
const v = setTrackProxy();
|
|
139
|
+
expect(hasUnsetValues`${v.a}`).toBeTruthy();
|
|
140
|
+
});
|
|
141
|
+
it('should return true for a string with input interpolation (unset -> set)', () => {
|
|
142
|
+
const v = setTrackProxy();
|
|
143
|
+
expect(hasUnsetValues`${v.a}`).toBeTruthy();
|
|
144
|
+
v.a = 1;
|
|
145
|
+
expect(hasUnsetValues`${v.a}`).toBeFalsy();
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('nested props', () => {
|
|
150
|
+
it('should return true for a string with input interpolation (set)', () => {
|
|
151
|
+
const v = setTrackProxy();
|
|
152
|
+
v.a.b = 1;
|
|
153
|
+
expect(hasUnsetValues`${v.a.b}`).toBeFalsy();
|
|
154
|
+
});
|
|
155
|
+
it('should return false for a string with input interpolation (unset)', () => {
|
|
156
|
+
const v = setTrackProxy();
|
|
157
|
+
expect(hasUnsetValues`${v.a.b}`).toBeTruthy();
|
|
158
|
+
});
|
|
159
|
+
it('should return true for a string with input interpolation (unset -> set)', () => {
|
|
160
|
+
const v = setTrackProxy();
|
|
161
|
+
expect(hasUnsetValues`${v.a.b}`).toBeTruthy();
|
|
162
|
+
v.a.b = 1;
|
|
163
|
+
expect(hasUnsetValues`${v.a.b}`).toBeFalsy();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|