@popcorn245/cornfield 0.34.0 → 0.34.2
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/core/strings.js +2 -0
- package/explorer/camera.js +144 -5
- package/explorer/explorer.css +52 -3
- package/explorer/forge-panel.js +2 -1
- package/explorer/index.html +4 -0
- package/explorer/input.js +3 -3
- package/explorer/keyboard.js +2 -2
- package/explorer/layers.js +33 -9
- package/explorer/layout.js +113 -22
- package/explorer/main.js +4 -0
- package/explorer/panels.js +21 -2
- package/explorer/proposal-model.js +111 -0
- package/explorer/proposal.js +158 -17
- package/explorer/render.js +33 -5
- package/explorer/renderer-canvas2d.js +110 -18
- package/explorer/review.js +16 -3
- package/explorer/scenario-ui.js +71 -12
- package/explorer/scenario.js +16 -24
- package/explorer/scene.js +2 -2
- package/explorer/search-status.js +41 -0
- package/explorer/sheets.js +26 -0
- package/package.json +1 -1
- package/server/compress.js +135 -0
- package/server/proxy.js +2 -0
- package/server/server.js +2 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module server/compress
|
|
3
|
+
*
|
|
4
|
+
* Compresses the text the server sends (the page, JS, CSS, JSON, SVG) with Node's own zlib: brotli when the browser
|
|
5
|
+
* accepts it, gzip otherwise, never for what is already compressed (images, fonts) or streamed (event streams), and never
|
|
6
|
+
* on top of an answer that already carries a `Content-Encoding`, never a proxied upstream's answer or a range, and never
|
|
7
|
+
* an answer marked `Cache-Control: no-store` (what may hold a secret: sign-in, tokens, the agent and runner APIs; BREACH).
|
|
8
|
+
* Every compressible answer says `Vary: Accept-Encoding`, so a cache keeps the two apart. ETags are weak (files.js) and
|
|
9
|
+
* the same for every encoding, so a conditional request still gets its 304.
|
|
10
|
+
*
|
|
11
|
+
* Used by wrapping the response once, at the top of the request: `compressResponse(req, res)`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import zlib from 'node:zlib';
|
|
15
|
+
|
|
16
|
+
/** Content types worth compressing: text, JSON (and `+json`), JavaScript, XML, SVG */
|
|
17
|
+
const COMPRESSIBLE = /^(?:text\/(?!event-stream)|application\/(?:json|javascript|ecmascript|xml|manifest\+json|[\w.+-]+\+(?:json|xml))|image\/svg\+xml)/i;
|
|
18
|
+
/** Below this many bytes (when the length is known) compressing costs more than it saves */
|
|
19
|
+
const MIN_BYTES = 512;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The encoding to answer with, from an `Accept-Encoding` header: `br`, `gzip`, or null. A `q=0` refuses one.
|
|
23
|
+
* @param {string|undefined} header
|
|
24
|
+
* @returns {'br'|'gzip'|null}
|
|
25
|
+
*/
|
|
26
|
+
export function pickEncoding(header) {
|
|
27
|
+
if (!header) return null;
|
|
28
|
+
const q = new Map();
|
|
29
|
+
for (const part of String(header).split(',')) {
|
|
30
|
+
const [name, ...params] = part.trim().toLowerCase().split(';');
|
|
31
|
+
if (!name) continue;
|
|
32
|
+
const m = params.map((p) => p.trim()).find((p) => p.startsWith('q='));
|
|
33
|
+
q.set(name.trim(), m ? Number(m.slice(2)) : 1);
|
|
34
|
+
}
|
|
35
|
+
const weight = (e) => { const v = q.has(e) ? q.get(e) : q.has('*') ? q.get('*') : 0; return Number.isFinite(v) && v > 0 ? v : 0; };
|
|
36
|
+
const br = weight('br');
|
|
37
|
+
const gzip = Math.max(weight('gzip'), weight('x-gzip'));
|
|
38
|
+
if (!br && !gzip) return null;
|
|
39
|
+
return br >= gzip ? 'br' : 'gzip'; // the one the browser prefers; brotli on a tie
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether a `Content-Type` is one of the compressible kinds */
|
|
43
|
+
export const isCompressible = (type) => typeof type === 'string' && COMPRESSIBLE.test(type.trim());
|
|
44
|
+
|
|
45
|
+
const SKIP = Symbol('cornfield.noCompress');
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Leaves this answer as it is: a proxied upstream's answer (it may be a stream, or a range, and the upstream saw the
|
|
49
|
+
* request's Accept-Encoding itself).
|
|
50
|
+
* @param {import('node:http').ServerResponse} res
|
|
51
|
+
*/
|
|
52
|
+
export function skipCompression(res) { /** @type {any} */ (res)[SKIP] = true; }
|
|
53
|
+
|
|
54
|
+
const hasVary = (v, name) => String(v || '').split(',').some((t) => t.trim().toLowerCase() === name.toLowerCase() || t.trim() === '*');
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wraps `res` so a compressible answer goes out compressed when the request allows it. Safe to call on every request:
|
|
58
|
+
* it changes nothing until `writeHead` (or the first write) shows a compressible body.
|
|
59
|
+
* @param {import('node:http').IncomingMessage} req
|
|
60
|
+
* @param {import('node:http').ServerResponse} res
|
|
61
|
+
*/
|
|
62
|
+
export function compressResponse(req, res) {
|
|
63
|
+
const encoding = pickEncoding(req.headers['accept-encoding']);
|
|
64
|
+
const origWriteHead = res.writeHead.bind(res);
|
|
65
|
+
const origWrite = res.write.bind(res);
|
|
66
|
+
const origEnd = res.end.bind(res);
|
|
67
|
+
let decided = false;
|
|
68
|
+
let writeImpl = origWrite;
|
|
69
|
+
let endImpl = origEnd;
|
|
70
|
+
// Sending a body without `writeHead` first (Node adds the head itself): decide before it goes
|
|
71
|
+
res.write = (...a) => { if (!decided) res.writeHead(res.statusCode); return writeImpl(...a); };
|
|
72
|
+
res.end = (...a) => { if (!decided) res.writeHead(res.statusCode); return endImpl(...a); };
|
|
73
|
+
|
|
74
|
+
const header = (extra, name) => {
|
|
75
|
+
if (extra && !Array.isArray(extra)) {
|
|
76
|
+
const key = Object.keys(extra).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
77
|
+
if (key !== undefined) return extra[key];
|
|
78
|
+
}
|
|
79
|
+
return res.getHeader(name);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
res.writeHead = (status, ...rest) => {
|
|
83
|
+
// The caller's headers may be a constant it sends again (server.js JSON_HEADERS): change a copy, never that
|
|
84
|
+
const at = rest.findIndex((x) => x && typeof x === 'object');
|
|
85
|
+
if (at >= 0 && !Array.isArray(rest[at])) rest[at] = { ...rest[at] };
|
|
86
|
+
const extra = at >= 0 ? rest[at] : undefined;
|
|
87
|
+
if (!decided) {
|
|
88
|
+
decided = true;
|
|
89
|
+
const type = header(extra, 'content-type');
|
|
90
|
+
const bodyless = status < 200 || status === 204 || (status === 304 && !isCompressible(type));
|
|
91
|
+
if (!bodyless && isCompressible(type)) {
|
|
92
|
+
// The answer varies by the request's Accept-Encoding whether or not this one is compressed (a 304 too)
|
|
93
|
+
const vary = header(extra, 'vary');
|
|
94
|
+
if (!hasVary(vary, 'accept-encoding')) {
|
|
95
|
+
if (extra && !Array.isArray(extra)) extra.Vary = vary ? `${vary}, Accept-Encoding` : 'Accept-Encoding';
|
|
96
|
+
else res.setHeader('Vary', vary ? `${vary}, Accept-Encoding` : 'Accept-Encoding');
|
|
97
|
+
}
|
|
98
|
+
const length = Number(header(extra, 'content-length'));
|
|
99
|
+
const done = header(extra, 'content-encoding');
|
|
100
|
+
// `no-store` marks what the server treats as secret (sign-in pages, tokens, the agent and runner APIs): a body
|
|
101
|
+
// that may hold a secret beside what the request sent is never compressed (the BREACH class of attack)
|
|
102
|
+
const cache = String(header(extra, 'cache-control') || '');
|
|
103
|
+
const keep = /no-transform|no-store/i.test(cache) || /** @type {any} */ (res)[SKIP]
|
|
104
|
+
|| status === 206 || header(extra, 'content-range') !== undefined;
|
|
105
|
+
if (encoding && status !== 304 && !done && !keep && !(Number.isFinite(length) && length > 0 && length < MIN_BYTES)) {
|
|
106
|
+
if (extra && !Array.isArray(extra)) {
|
|
107
|
+
Object.keys(extra).filter((k) => k.toLowerCase() === 'content-length').forEach((k) => { delete extra[k]; });
|
|
108
|
+
extra['Content-Encoding'] = encoding;
|
|
109
|
+
} else res.setHeader('Content-Encoding', encoding);
|
|
110
|
+
res.removeHeader('Content-Length');
|
|
111
|
+
if (req.method !== 'HEAD') startStream();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return origWriteHead(status, ...rest);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
function startStream() {
|
|
119
|
+
const stream = encoding === 'br'
|
|
120
|
+
// Quality 4 is the cheap end: about as fast as gzip, and smaller
|
|
121
|
+
? zlib.createBrotliCompress({ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4, [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT } })
|
|
122
|
+
: zlib.createGzip({ level: 6 });
|
|
123
|
+
stream.on('data', (chunk) => origWrite(chunk));
|
|
124
|
+
stream.on('end', () => origEnd());
|
|
125
|
+
stream.on('error', () => res.destroy());
|
|
126
|
+
stream.on('drain', () => res.emit('drain'));
|
|
127
|
+
writeImpl = (chunk, enc, cb) => stream.write(chunk, enc, cb);
|
|
128
|
+
endImpl = (chunk, enc, cb) => {
|
|
129
|
+
if (typeof chunk === 'function') { cb = chunk; chunk = undefined; }
|
|
130
|
+
if (chunk !== undefined && chunk !== null) stream.end(chunk, typeof enc === 'string' ? enc : undefined, cb);
|
|
131
|
+
else stream.end(cb);
|
|
132
|
+
return res;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
package/server/proxy.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* The session is a bearer credential for the whole dashboard, and the upstream is someone else's server.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import { skipCompression } from './compress.js';
|
|
14
15
|
import http from 'node:http';
|
|
15
16
|
import https from 'node:https';
|
|
16
17
|
|
|
@@ -40,6 +41,7 @@ export function withoutOwnSetCookies(values) {
|
|
|
40
41
|
* @param {string} target like `http://127.0.0.1:3245`
|
|
41
42
|
*/
|
|
42
43
|
export function proxyTo(req, res, target) {
|
|
44
|
+
skipCompression(res); // the upstream saw Accept-Encoding and answers as it likes: a stream or a range stays as it is
|
|
43
45
|
const url = new URL(target);
|
|
44
46
|
const client = url.protocol === 'https:' ? https : http;
|
|
45
47
|
const port = url.port || (url.protocol === 'https:' ? '443' : '80');
|
package/server/server.js
CHANGED
|
@@ -75,6 +75,7 @@ import { createLander } from './lander.js';
|
|
|
75
75
|
import { createSpecPoll } from './spec-poll.js';
|
|
76
76
|
import { createRunnerWatch } from './runner-presence.js';
|
|
77
77
|
import { createTriggers } from './triggers.js';
|
|
78
|
+
import { compressResponse } from './compress.js';
|
|
78
79
|
|
|
79
80
|
export const CORNFIELD_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
80
81
|
const EXPLORER_DIR = path.join(CORNFIELD_DIR, 'explorer');
|
|
@@ -403,6 +404,7 @@ export function createCornfieldServer(config, opts = {}) {
|
|
|
403
404
|
return server;
|
|
404
405
|
|
|
405
406
|
async function answer(req, res) {
|
|
407
|
+
compressResponse(req, res); // text, JSON, JS, CSS and SVG go out gzipped or brotli'd when the browser accepts it
|
|
406
408
|
res.setHeader('Content-Security-Policy', csp);
|
|
407
409
|
res.setHeader('Referrer-Policy', 'no-referrer');
|
|
408
410
|
res.setHeader('X-Frame-Options', 'DENY');
|