@altopelago/sansa 0.9.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 +41 -0
- package/CODE_OF_CONDUCT.md +28 -0
- package/CONTRIBUTING.md +52 -0
- package/GOVERNANCE.md +35 -0
- package/LICENSE +21 -0
- package/README.md +146 -0
- package/SECURITY.md +38 -0
- package/docs/api-contract.md +788 -0
- package/docs/capabilities.json +80 -0
- package/docs/query-tool.md +228 -0
- package/docs/releasing.md +37 -0
- package/fixtures/address-v1.json +316 -0
- package/fixtures/query-inventory.aeon +69 -0
- package/fixtures/query-inventory.json +671 -0
- package/fixtures/query-params-single.json +10 -0
- package/package.json +70 -0
- package/scripts/cts-address.mjs +107 -0
- package/scripts/cts-query.mjs +271 -0
- package/scripts/cts-resolve.mjs +151 -0
- package/scripts/cts-value-semantics.mjs +83 -0
- package/scripts/query-web.mjs +145 -0
- package/scripts/query.mjs +579 -0
- package/scripts/stress-addresses.mjs +65 -0
- package/src/index.d.ts +575 -0
- package/src/index.js +3389 -0
- package/stress-tests/README.md +19 -0
- package/stress-tests/address/invalid.sansa-cases +60 -0
- package/stress-tests/address/valid.sansa-cases +76 -0
- package/tools/query-web/app.js +266 -0
- package/tools/query-web/examples.mjs +478 -0
- package/tools/query-web/index.html +117 -0
- package/tools/query-web/runtime.mjs +796 -0
- package/tools/query-web/styles.css +251 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createReadStream, existsSync, statSync } from 'node:fs';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
4
|
+
import { extname, join, normalize, resolve, sep } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { evaluateQueryForWorkbench, parseQueryForWorkbench } from '../tools/query-web/runtime.mjs';
|
|
7
|
+
|
|
8
|
+
const root = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
|
9
|
+
const port = Number(readArg('--port') ?? process.env.PORT ?? 4173);
|
|
10
|
+
const host = readArg('--host') ?? '127.0.0.1';
|
|
11
|
+
|
|
12
|
+
const server = createServer((request, response) => {
|
|
13
|
+
const url = new URL(request.url ?? '/', `http://${host}:${port}`);
|
|
14
|
+
if (url.pathname === '/api/query') {
|
|
15
|
+
handleQueryApi(request, response);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const requestedPath = url.pathname === '/' ? '/tools/query-web/index.html' : url.pathname;
|
|
20
|
+
let filePath = resolve(root, `.${normalize(requestedPath)}`);
|
|
21
|
+
|
|
22
|
+
if (!filePath.startsWith(`${root}${sep}`) && filePath !== root) {
|
|
23
|
+
response.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' });
|
|
24
|
+
response.end('Forbidden');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!existsSync(filePath)) {
|
|
29
|
+
response.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
|
|
30
|
+
response.end('Not found');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (statSync(filePath).isDirectory()) {
|
|
35
|
+
filePath = join(filePath, 'index.html');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
|
|
39
|
+
response.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
|
|
40
|
+
response.end('Not found');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
response.writeHead(200, { 'content-type': contentType(filePath) });
|
|
45
|
+
createReadStream(filePath)
|
|
46
|
+
.on('error', (error) => {
|
|
47
|
+
response.destroy(error);
|
|
48
|
+
})
|
|
49
|
+
.pipe(response);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
server.listen(port, host, () => {
|
|
53
|
+
console.log(`SANSA Query Workbench: http://${host}:${port}/tools/query-web/`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
server.on('error', (error) => {
|
|
57
|
+
console.error(`SANSA Query Workbench failed to start on ${host}:${port}: ${error.message}`);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
function readArg(name) {
|
|
62
|
+
const index = process.argv.indexOf(name);
|
|
63
|
+
return index === -1 ? undefined : process.argv[index + 1];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function handleQueryApi(request, response) {
|
|
67
|
+
if (request.method !== 'POST') {
|
|
68
|
+
writeJson(response, 405, {
|
|
69
|
+
ok: false,
|
|
70
|
+
errors: [{ code: 'SANSA_QUERY_WORKBENCH_METHOD_NOT_ALLOWED', message: 'Expected POST.' }],
|
|
71
|
+
});
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
readRequestBody(request, 1_000_000)
|
|
76
|
+
.then(async (body) => {
|
|
77
|
+
let payload;
|
|
78
|
+
try {
|
|
79
|
+
payload = JSON.parse(body);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
writeJson(response, 400, {
|
|
82
|
+
ok: false,
|
|
83
|
+
errors: [{ code: 'SANSA_QUERY_WORKBENCH_INVALID_REQUEST_JSON', message: error.message }],
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const action = payload.action === 'parse' ? 'parse' : 'evaluate';
|
|
89
|
+
const result = action === 'parse'
|
|
90
|
+
? parseQueryForWorkbench(String(payload.query ?? ''))
|
|
91
|
+
: await evaluateQueryForWorkbench({
|
|
92
|
+
sourceKind: payload.sourceKind === 'json' ? 'json' : 'aeon',
|
|
93
|
+
source: String(payload.source ?? ''),
|
|
94
|
+
query: String(payload.query ?? ''),
|
|
95
|
+
paramsSource: String(payload.paramsSource ?? ''),
|
|
96
|
+
policy: payload.policy === 'validation' ? 'validation' : '',
|
|
97
|
+
transformExtensions: payload.transformExtensions !== false,
|
|
98
|
+
budget: payload.budget,
|
|
99
|
+
});
|
|
100
|
+
writeJson(response, result.ok ? 200 : 400, result);
|
|
101
|
+
})
|
|
102
|
+
.catch((error) => {
|
|
103
|
+
writeJson(response, 400, {
|
|
104
|
+
ok: false,
|
|
105
|
+
errors: [{ code: 'SANSA_QUERY_WORKBENCH_REQUEST_ERROR', message: error.message }],
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function readRequestBody(request, maxBytes) {
|
|
111
|
+
return new Promise((resolveBody, rejectBody) => {
|
|
112
|
+
let body = '';
|
|
113
|
+
request.setEncoding('utf8');
|
|
114
|
+
request.on('data', (chunk) => {
|
|
115
|
+
body += chunk;
|
|
116
|
+
if (body.length > maxBytes) {
|
|
117
|
+
request.destroy();
|
|
118
|
+
rejectBody(new Error(`Request body exceeds ${maxBytes} bytes.`));
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
request.on('end', () => resolveBody(body));
|
|
122
|
+
request.on('error', rejectBody);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function writeJson(response, statusCode, payload) {
|
|
127
|
+
response.writeHead(statusCode, { 'content-type': 'application/json; charset=utf-8' });
|
|
128
|
+
response.end(JSON.stringify(payload, null, 2));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function contentType(filePath) {
|
|
132
|
+
switch (extname(filePath)) {
|
|
133
|
+
case '.html':
|
|
134
|
+
return 'text/html; charset=utf-8';
|
|
135
|
+
case '.js':
|
|
136
|
+
case '.mjs':
|
|
137
|
+
return 'text/javascript; charset=utf-8';
|
|
138
|
+
case '.css':
|
|
139
|
+
return 'text/css; charset=utf-8';
|
|
140
|
+
case '.json':
|
|
141
|
+
return 'application/json; charset=utf-8';
|
|
142
|
+
default:
|
|
143
|
+
return 'application/octet-stream';
|
|
144
|
+
}
|
|
145
|
+
}
|