-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.ts
More file actions
352 lines (349 loc) · 11.2 KB
/
Copy pathdev-server.ts
File metadata and controls
352 lines (349 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import {
createServer,
type IncomingMessage,
type ServerResponse,
} from 'node:http';
import { resolve } from 'node:path';
import {
PythonIdentifierSchema,
type CalculationDefinition,
} from '@cs-object/core';
import { UsageError } from './arguments.ts';
import { HttpError } from './dev-errors.ts';
import {
createDevRuntime,
type DevLimits,
type DevTarget,
} from './dev-runtime.ts';
import { jsonBytes } from './evidence.ts';
import { parseStrictJson } from './strict-json.ts';
import { devPage } from './dev-ui.ts';
type DevOptions = DevTarget & { port: number };
export const devHelp = `Usage: cso dev <file.cso.py> --function <name> [--port <0-65535>] [--reference <file.json>]
Serve a verified calculation on 127.0.0.1. Port 0 chooses an available port.
PYTHON selects the installed cs-object interpreter. Source path and function are fixed at startup.
`;
export function parseDevArgs(args: string[]): DevOptions {
const [source, ...rest] = args;
if (!source || source.startsWith('-')) throw new UsageError(devHelp);
const values = new Map<string, string>();
for (let index = 0; index < rest.length; index += 2) {
const key = rest[index];
const value = rest[index + 1];
if (
!['--function', '--port', '--reference'].includes(key) ||
values.has(key) ||
!value ||
value.startsWith('--')
)
throw new UsageError(devHelp);
values.set(key, value);
}
const functionName = values.get('--function');
if (!functionName || !PythonIdentifierSchema.safeParse(functionName).success)
throw new UsageError('--function requires a Python identifier');
const normalizedFunction = functionName.normalize('NFKC');
if (!PythonIdentifierSchema.safeParse(normalizedFunction).success)
throw new UsageError(
'--function does not normalize to a valid Python identifier',
);
const spelling = values.get('--port') ?? '3000';
const port = Number(spelling);
if (!/^\d+$/.test(spelling) || !Number.isInteger(port) || port > 65535)
throw new UsageError('--port must be an integer between 0 and 65535');
const reference = values.get('--reference');
return {
sourcePath: resolve(source),
functionName: normalizedFunction,
port,
...(reference ? { referencePath: resolve(reference) } : {}),
};
}
export function parseDevInputs(
text: string,
definition: Pick<CalculationDefinition, 'inputs'>,
): Record<string, number> {
let body: unknown;
try {
body = parseStrictJson(text);
} catch (error) {
throw new HttpError(
400,
error instanceof Error ? error.message : 'Invalid JSON',
);
}
if (
!body ||
typeof body !== 'object' ||
Array.isArray(body) ||
Object.keys(body).length !== 1 ||
!('inputs' in body)
)
throw new HttpError(400, 'Request must contain only an inputs object');
const values = body.inputs;
if (!values || typeof values !== 'object' || Array.isArray(values))
throw new HttpError(400, 'inputs must be an object');
const parameters = new Map(
definition.inputs.map((input) => [input.name, input]),
);
const entries: [string, number][] = [];
for (const [name, value] of Object.entries(values)) {
const input = parameters.get(name);
if (!input) throw new HttpError(400, `Unknown input ${name}`);
if (
typeof value !== 'number' ||
!Number.isFinite(value) ||
(input.numericType === 'int' && !Number.isSafeInteger(value))
)
throw new HttpError(
400,
`${name} requires a finite ${input.numericType === 'int' ? 'safe integer' : 'number'}`,
);
entries.push([
name,
input.numericType === 'int' && Object.is(value, -0) ? 0 : value,
]);
}
// JSON.parse rounds large integer tokens before numeric types can inspect them.
for (const match of text.matchAll(
/("(?:[^"\\]|\\.)*")\s*:\s*(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)/g,
)) {
const name: unknown = JSON.parse(match[1]);
const token = match[2];
if (
typeof name === 'string' &&
parameters.get(name)?.numericType === 'int' &&
/[.eE]/.test(token)
)
throw new HttpError(
400,
`${name} requires an integer token without a decimal or exponent`,
);
if (!/[.eE]/.test(token) && !Number.isSafeInteger(Number(token)))
throw new HttpError(
400,
'Integer tokens must be within the safe integer range; use a decimal or exponent for floats',
);
}
for (const input of definition.inputs) {
if (!Object.hasOwn(values, input.name) && input.default === undefined)
throw new HttpError(400, `Missing input ${input.name}`);
}
return Object.fromEntries(entries);
}
function withLinks(
summary: ReturnType<ReturnType<typeof createDevRuntime>['summary']>,
) {
const base = `/api/runs/${summary.id}`;
return {
...summary,
html: `${base}/report.html`,
pdf: `${base}/report.pdf`,
evidence: `${base}/evidence.json`,
};
}
function send(
response: ServerResponse,
status: number,
data: Buffer,
type: string,
) {
response.writeHead(status, {
'Content-Type': type,
'Content-Length': data.length,
'Cache-Control': 'no-store',
'X-Content-Type-Options': 'nosniff',
});
response.end(data);
}
async function readBody(request: IncomingMessage): Promise<string> {
const chunks: Buffer[] = [];
let length = 0;
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
length += bytes.length;
if (length > 64 * 1024) {
request.resume();
throw new HttpError(413, 'Request body exceeds 64 KiB');
}
chunks.push(bytes);
}
try {
return new TextDecoder('utf-8', { fatal: true }).decode(
Buffer.concat(chunks),
);
} catch {
throw new HttpError(400, 'Request must be UTF-8 JSON');
}
}
export async function startDevServer(options: DevOptions, limits?: DevLimits) {
const runtime = createDevRuntime(options, limits);
let origin = '';
let stopping = false;
const server = createServer(async (request, response) => {
try {
if (stopping) throw new HttpError(503, 'Server is shutting down');
if (
request.headers.host !== new URL(origin).host ||
(request.headers.origin !== undefined &&
request.headers.origin !== origin)
)
throw new HttpError(
403,
'Only same-origin localhost requests are accepted',
);
const url = new URL(request.url ?? '/', origin);
if (url.origin !== origin)
throw new HttpError(403, 'Invalid request origin');
if (request.method === 'GET' && url.pathname === '/') {
response.setHeader(
'Content-Security-Policy',
"default-src 'self'; script-src 'self'; style-src 'unsafe-inline'; frame-src 'self'; object-src 'none'; base-uri 'none'",
);
return send(
response,
200,
Buffer.from(devPage.html),
'text/html; charset=utf-8',
);
}
if (request.method === 'GET' && url.pathname === '/app.js')
return send(
response,
200,
Buffer.from(devPage.script),
'text/javascript; charset=utf-8',
);
if (request.method === 'GET' && url.pathname === '/api/definition')
return send(
response,
200,
jsonBytes(runtime.definition()),
'application/json',
);
if (
request.method === 'POST' &&
['/api/calculate', '/api/runs'].includes(url.pathname)
) {
if (
!/^application\/json(?:\s*;.*)?$/i.test(
request.headers['content-type'] ?? '',
)
)
throw new HttpError(415, 'Content-Type must be application/json');
const body = await readBody(request);
const definition = runtime.definition();
const invocation = {
definition,
inputs: parseDevInputs(body, definition),
};
if (url.pathname === '/api/calculate')
return send(
response,
200,
jsonBytes(runtime.calculate(invocation)),
'application/json',
);
return send(
response,
201,
jsonBytes(withLinks(runtime.createRun(invocation))),
'application/json',
);
}
const match =
/^\/api\/runs\/([\da-f-]{36})(?:\/(report\.html|report\.pdf|evidence\.json))?$/.exec(
url.pathname,
);
if (request.method === 'GET' && match) {
const id = match[1];
if (match[2] === 'report.html')
return send(
response,
200,
runtime.html(id),
'text/html; charset=utf-8',
);
if (match[2] === 'report.pdf') {
const artifact = await runtime.pdf(id);
response.setHeader(
'Content-Disposition',
'attachment; filename="calculation.pdf"',
);
response.setHeader('X-CSO-PDF-SHA256', artifact.sha256);
return send(response, 200, artifact.bytes, 'application/pdf');
}
if (match[2] === 'evidence.json') {
response.setHeader(
'Content-Disposition',
'attachment; filename="calculation-evidence.json"',
);
return send(response, 200, runtime.evidence(id), 'application/json');
}
return send(
response,
200,
jsonBytes(withLinks(runtime.summary(id))),
'application/json',
);
}
throw new HttpError(404, 'Not found');
} catch (error) {
if (response.destroyed || response.headersSent) return;
const failure =
error instanceof HttpError
? error
: new HttpError(
500,
error instanceof Error ? error.message : 'Request failed',
);
if (failure.status === 413) response.setHeader('Connection', 'close');
send(
response,
failure.status,
jsonBytes({ error: failure.message, diagnostics: failure.diagnostics }),
'application/json',
);
}
});
server.requestTimeout = 15_000;
server.headersTimeout = 10_000;
server.maxConnections = 16;
server.keepAliveTimeout = 5_000;
try {
await new Promise<void>((success, failure) => {
server.once('error', failure);
server.listen(options.port, '127.0.0.1', () => {
server.off('error', failure);
success();
});
});
} catch (error) {
await runtime.close();
throw error;
}
const address = server.address();
if (!address || typeof address === 'string')
throw new Error('Missing localhost address');
origin = `http://127.0.0.1:${address.port}`;
return {
origin,
close: async () => {
stopping = true;
const closed = new Promise<void>((done) => server.close(() => done()));
server.closeAllConnections();
await Promise.allSettled([closed, runtime.close()]);
},
};
}
export async function devCommand(args: string[]) {
const server = await startDevServer(parseDevArgs(args));
process.stdout.write(`CSO dev ${server.origin}\n`);
const stop = () => {
process.off('SIGINT', stop);
process.off('SIGTERM', stop);
void server.close();
};
process.on('SIGINT', stop);
process.on('SIGTERM', stop);
}