townmouse 0.0.0-stage → 0.2.1
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 +126 -0
- package/DESIGN.md +169 -0
- package/LICENSE +661 -0
- package/README.md +368 -2
- package/SECURITY.md +57 -0
- package/TOWNMOUSE.md +91 -0
- package/assets/brand/mascot/README.md +23 -0
- package/assets/brand/mascot/townmouse-aristocrat.png +0 -0
- package/assets/brand/mascot/townmouse-icon.svg +1 -0
- package/assets/brand/mascot/townmouse-monocled.png +0 -0
- package/bin/townmouse +68 -0
- package/docs/PACKAGING.md +275 -0
- package/docs/browser-workspace.md +184 -0
- package/docs/panes.md +252 -0
- package/native/TownmouseApp.swift +108 -0
- package/package.json +89 -4
- package/public/apple-touch-icon.png +0 -0
- package/public/brand/floydslabs-credit.png +0 -0
- package/public/brand/floydslabs-credit.webp +0 -0
- package/public/brand/townmouse-aristocrat.webp +0 -0
- package/public/brand/townmouse-monocled.webp +0 -0
- package/public/clipboard-text.mjs +5 -0
- package/public/favicon-16x16.png +0 -0
- package/public/favicon-32x32.png +0 -0
- package/public/favicon.ico +0 -0
- package/public/favicon.svg +1 -0
- package/public/features/autosave.mjs +76 -0
- package/public/features/clipboard.mjs +69 -0
- package/public/features/command-palette.mjs +181 -0
- package/public/features/desktop-palette.mjs +19 -0
- package/public/features/export.mjs +37 -0
- package/public/features/file-drop.mjs +77 -0
- package/public/features/floating-palette.mjs +113 -0
- package/public/features/fontsize.mjs +72 -0
- package/public/features/haptic-keys.mjs +42 -0
- package/public/features/history-undo.mjs +102 -0
- package/public/features/keepalive.mjs +36 -0
- package/public/features/keybar-config.mjs +70 -0
- package/public/features/keyboard-shortcuts.mjs +74 -0
- package/public/features/links.mjs +139 -0
- package/public/features/multi-window-sync.mjs +74 -0
- package/public/features/osc-bridge.mjs +59 -0
- package/public/features/phone-landscape-focus.mjs +157 -0
- package/public/features/recent-commands.mjs +100 -0
- package/public/features/search.mjs +134 -0
- package/public/features/session-lock.mjs +96 -0
- package/public/features/session-manager.mjs +149 -0
- package/public/features/settings-sync.mjs +77 -0
- package/public/features/shortcut-help.mjs +85 -0
- package/public/features/smart-completions.mjs +101 -0
- package/public/features/smart-suggest.mjs +124 -0
- package/public/features/snippets.mjs +85 -0
- package/public/features/split-view.mjs +102 -0
- package/public/features/status-metrics.mjs +114 -0
- package/public/features/stylus-support.mjs +124 -0
- package/public/features/swipe-history.mjs +141 -0
- package/public/features/swipe-sessions.mjs +85 -0
- package/public/features/tab-reorder.mjs +62 -0
- package/public/features/theme-system.mjs +51 -0
- package/public/features/toast-status.mjs +118 -0
- package/public/features/two-finger-menu.mjs +87 -0
- package/public/features/voice-input.mjs +244 -0
- package/public/features/wake-fullscreen.mjs +67 -0
- package/public/features/window-title.mjs +40 -0
- package/public/features/zoom.mjs +80 -0
- package/public/icon-128.png +0 -0
- package/public/icon-180.png +0 -0
- package/public/icon-192.png +0 -0
- package/public/icon-48.png +0 -0
- package/public/icon-512-maskable.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/icon.svg +1 -0
- package/public/index.html +1765 -0
- package/public/input-chunks.mjs +22 -0
- package/public/input-guard.mjs +210 -0
- package/public/manifest.json +56 -0
- package/public/panes.css +1257 -0
- package/public/panes.html +208 -0
- package/public/panes.mjs +2625 -0
- package/public/service-worker.js +67 -0
- package/public/workspace.css +1447 -0
- package/public/workspace.mjs +1475 -0
- package/scripts/install-service.sh +269 -0
- package/scripts/node-runtime.sh +138 -0
- package/scripts/panes +439 -0
- package/scripts/prepare-native.sh +35 -0
- package/scripts/run-hooks +69 -0
- package/scripts/t1.sh +113 -0
- package/scripts/uninstall-service.sh +49 -0
- package/src/agent-guide.js +260 -0
- package/src/attachments.js +128 -0
- package/src/browser-dom.js +545 -0
- package/src/browser-workspace.js +1246 -0
- package/src/chrome-resolve.js +80 -0
- package/src/pty-permissions.js +99 -0
- package/src/regex-worker.js +13 -0
- package/src/replay-text.js +189 -0
- package/src/request-boundary.js +58 -0
- package/src/safe-regex.js +87 -0
- package/src/server.js +1122 -0
- package/src/shell-resolve.js +42 -0
- package/src/static-deps.js +61 -0
- package/src/workspace-panes.js +1099 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
// Where a browser engine might already be. A packaged install cannot assume
|
|
8
|
+
// Puppeteer's own download exists: npm 12 skips dependency install scripts by
|
|
9
|
+
// default (`allow-scripts = [""]`), and even when it runs, the pinned download
|
|
10
|
+
// version may differ from what is cached. A system Chrome/Chromium is a normal,
|
|
11
|
+
// safe fallback for a local tool.
|
|
12
|
+
const CANDIDATES = {
|
|
13
|
+
darwin: [
|
|
14
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
15
|
+
'{home}/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
16
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
17
|
+
'{home}/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
18
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
19
|
+
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
|
|
20
|
+
],
|
|
21
|
+
linux: [
|
|
22
|
+
'/usr/bin/google-chrome',
|
|
23
|
+
'/usr/bin/google-chrome-stable',
|
|
24
|
+
'/usr/bin/chromium',
|
|
25
|
+
'/usr/bin/chromium-browser',
|
|
26
|
+
'/snap/bin/chromium',
|
|
27
|
+
'/usr/bin/microsoft-edge'
|
|
28
|
+
],
|
|
29
|
+
win32: [
|
|
30
|
+
'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
|
|
31
|
+
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
|
|
32
|
+
'C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe'
|
|
33
|
+
]
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function defaultPuppeteerPath() {
|
|
37
|
+
try {
|
|
38
|
+
const puppeteer = require('puppeteer');
|
|
39
|
+
return typeof puppeteer.executablePath === 'function' ? puppeteer.executablePath() : null;
|
|
40
|
+
} catch (_) { return null; }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Resolve a Chromium-compatible executable.
|
|
45
|
+
*
|
|
46
|
+
* Order: explicit configuration, Puppeteer's downloaded build, then a system
|
|
47
|
+
* browser. Returns `{ path, source }` or null — never throws, so a missing
|
|
48
|
+
* browser produces a clear message at use time rather than at startup.
|
|
49
|
+
*/
|
|
50
|
+
function resolveChrome(options = {}) {
|
|
51
|
+
const platform = options.platform || process.platform;
|
|
52
|
+
const env = options.env || process.env;
|
|
53
|
+
const home = options.home || os.homedir();
|
|
54
|
+
const exists = options.exists || ((file) => { try { return fs.existsSync(file); } catch (_) { return false; } });
|
|
55
|
+
const puppeteerPath = options.puppeteerPath || defaultPuppeteerPath;
|
|
56
|
+
|
|
57
|
+
const configured = env.TERMINALONE_CHROME;
|
|
58
|
+
if (configured && exists(configured)) return { path: configured, source: 'TERMINALONE_CHROME' };
|
|
59
|
+
|
|
60
|
+
const downloaded = puppeteerPath();
|
|
61
|
+
if (downloaded && exists(downloaded)) return { path: downloaded, source: 'puppeteer' };
|
|
62
|
+
|
|
63
|
+
for (const candidate of CANDIDATES[platform] || []) {
|
|
64
|
+
const file = candidate.replace('{home}', home);
|
|
65
|
+
if (exists(file)) return { path: file, source: 'system' };
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function missingBrowserMessage() {
|
|
71
|
+
return [
|
|
72
|
+
'No browser engine was found for browser panes.',
|
|
73
|
+
'Use one of:',
|
|
74
|
+
' - install Chrome or Chromium; or',
|
|
75
|
+
' - set TERMINALONE_CHROME=/path/to/chrome; or',
|
|
76
|
+
' - run: npx puppeteer browsers install chrome'
|
|
77
|
+
].join('\n');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = { resolveChrome, missingBrowserMessage, CANDIDATES, defaultPuppeteerPath };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
// node-pty launches a small `spawn-helper` binary on macOS. An npm tarball does
|
|
7
|
+
// not carry the executable bit, and npm 12 skips dependency install scripts by
|
|
8
|
+
// default (`allow-scripts = [""]`), so the package's postinstall cannot be the
|
|
9
|
+
// only thing that fixes it. Without the bit, every terminal pane fails with
|
|
10
|
+
// "posix_spawnp failed" while the rest of the app looks healthy.
|
|
11
|
+
//
|
|
12
|
+
// The repair is a mode change on a file inside the installed package, so the
|
|
13
|
+
// server can safely do it at startup on the machine that owns the file.
|
|
14
|
+
|
|
15
|
+
function nodePtyDir() {
|
|
16
|
+
let dir;
|
|
17
|
+
try { dir = path.dirname(require.resolve('node-pty')); } catch (_) { return null; }
|
|
18
|
+
const root = path.parse(dir).root;
|
|
19
|
+
let current = dir;
|
|
20
|
+
while (current !== root && !fs.existsSync(path.join(current, 'package.json'))) {
|
|
21
|
+
current = path.dirname(current);
|
|
22
|
+
}
|
|
23
|
+
return fs.existsSync(path.join(current, 'package.json')) ? current : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function spawnHelpers(dir) {
|
|
27
|
+
const helpers = [];
|
|
28
|
+
const prebuilds = path.join(dir, 'prebuilds');
|
|
29
|
+
let entries = [];
|
|
30
|
+
try { entries = fs.readdirSync(prebuilds); } catch (_) { entries = []; }
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const helper = path.join(prebuilds, entry, 'spawn-helper');
|
|
33
|
+
if (fs.existsSync(helper)) helpers.push(helper);
|
|
34
|
+
}
|
|
35
|
+
// A source build (node-gyp) puts it here instead.
|
|
36
|
+
const built = path.join(dir, 'build', 'Release', 'spawn-helper');
|
|
37
|
+
if (fs.existsSync(built)) helpers.push(built);
|
|
38
|
+
return helpers;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function fixHelpersIn(dir) {
|
|
42
|
+
const fixed = [];
|
|
43
|
+
for (const helper of spawnHelpers(dir)) {
|
|
44
|
+
try {
|
|
45
|
+
const mode = fs.statSync(helper).mode;
|
|
46
|
+
if ((mode & 0o111) === 0o111) continue;
|
|
47
|
+
fs.chmodSync(helper, 0o755);
|
|
48
|
+
fixed.push(helper);
|
|
49
|
+
} catch (_) { /* not ours to change; the spawn error will name it */ }
|
|
50
|
+
}
|
|
51
|
+
return fixed;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ensurePtyHelpers() {
|
|
55
|
+
// Only macOS needs the helper. Elsewhere this is a no-op by design.
|
|
56
|
+
if (process.platform !== 'darwin') return { dir: null, fixed: [] };
|
|
57
|
+
const dir = nodePtyDir();
|
|
58
|
+
if (!dir) return { dir: null, fixed: [] };
|
|
59
|
+
return { dir, fixed: fixHelpersIn(dir) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// A missing or unbuilt native engine otherwise surfaces as a raw stack trace
|
|
63
|
+
// from a `require` deep inside startup. The condition is common: npm 12 skips
|
|
64
|
+
// dependency install scripts by default and node-pty has no Linux prebuild.
|
|
65
|
+
const TERMINAL_ENGINE_ERROR = /node-pty|posix_spawn|pty\.node|bindings/i;
|
|
66
|
+
|
|
67
|
+
function isTerminalEngineError(error) {
|
|
68
|
+
if (!error) return false;
|
|
69
|
+
return TERMINAL_ENGINE_ERROR.test(`${error.code || ''} ${error.message || error}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function terminalEngineHelp(platform = process.platform, detail = '') {
|
|
73
|
+
const linux = platform === 'linux';
|
|
74
|
+
const lines = ['Townmouse cannot start: the terminal engine (node-pty) could not load.'];
|
|
75
|
+
if (detail) lines.push(String(detail).split('\n')[0]);
|
|
76
|
+
lines.push('', 'The native module is missing or was built for another platform.');
|
|
77
|
+
if (linux) {
|
|
78
|
+
lines.push('npm 12 does not run dependency install scripts by default, and node-pty');
|
|
79
|
+
lines.push('has no prebuilt Linux binary, so an install that skipped scripts has none.');
|
|
80
|
+
}
|
|
81
|
+
lines.push(
|
|
82
|
+
'',
|
|
83
|
+
'Fix one of these, then check with `npm run doctor`:',
|
|
84
|
+
' npm rebuild node-pty (needs python3, make, g++)'
|
|
85
|
+
);
|
|
86
|
+
if (linux) lines.push(' npm install --allow-scripts=node-pty townmouse (Linux, npm 12)');
|
|
87
|
+
lines.push(' container channel: see docs/PACKAGING.md');
|
|
88
|
+
return lines.join('\n');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = {
|
|
92
|
+
ensurePtyHelpers,
|
|
93
|
+
nodePtyDir,
|
|
94
|
+
spawnHelpers,
|
|
95
|
+
fixHelpersIn,
|
|
96
|
+
isTerminalEngineError,
|
|
97
|
+
terminalEngineHelp,
|
|
98
|
+
TERMINAL_ENGINE_ERROR
|
|
99
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Runs client-supplied wait patterns off the main thread so a catastrophic
|
|
4
|
+
// backtrack can be terminated instead of stalling every request.
|
|
5
|
+
const { parentPort } = require('worker_threads');
|
|
6
|
+
|
|
7
|
+
parentPort.on('message', ({ id, pattern, flags, text }) => {
|
|
8
|
+
try {
|
|
9
|
+
parentPort.postMessage({ id, matched: new RegExp(pattern, flags).test(text) });
|
|
10
|
+
} catch (error) {
|
|
11
|
+
parentPort.postMessage({ id, matched: false, error: error.message });
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const STRIP = [
|
|
4
|
+
// OSC (titles, colors, clipboard) terminated by BEL or ST.
|
|
5
|
+
/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g,
|
|
6
|
+
// CSI (cursor movement, erase, SGR, modes).
|
|
7
|
+
/\x1b\[[0-?]*[ -/]*[@-~]/g,
|
|
8
|
+
// Remaining two-character escapes.
|
|
9
|
+
/\x1b[@-_]/g
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
function stripEscapes(value) {
|
|
13
|
+
let text = String(value || '');
|
|
14
|
+
for (const pattern of STRIP) text = text.replace(pattern, '');
|
|
15
|
+
return text;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function cellWidth(codePoint) {
|
|
19
|
+
if (codePoint < 32) return 0;
|
|
20
|
+
if ((codePoint >= 0x0300 && codePoint <= 0x036f) ||
|
|
21
|
+
(codePoint >= 0x1ab0 && codePoint <= 0x1aff) ||
|
|
22
|
+
(codePoint >= 0x1dc0 && codePoint <= 0x1dff) ||
|
|
23
|
+
(codePoint >= 0x20d0 && codePoint <= 0x20ff) ||
|
|
24
|
+
(codePoint >= 0xfe20 && codePoint <= 0xfe2f)) return 0;
|
|
25
|
+
if (codePoint >= 0x1100 && (
|
|
26
|
+
codePoint <= 0x115f ||
|
|
27
|
+
codePoint === 0x2329 || codePoint === 0x232a ||
|
|
28
|
+
(codePoint >= 0x2e80 && codePoint <= 0xa4cf && codePoint !== 0x303f) ||
|
|
29
|
+
(codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
|
|
30
|
+
(codePoint >= 0xf900 && codePoint <= 0xfaff) ||
|
|
31
|
+
(codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
|
|
32
|
+
(codePoint >= 0xfe30 && codePoint <= 0xfe6f) ||
|
|
33
|
+
(codePoint >= 0xff00 && codePoint <= 0xff60) ||
|
|
34
|
+
(codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
|
|
35
|
+
(codePoint >= 0x1f000 && codePoint <= 0x1faff) ||
|
|
36
|
+
(codePoint >= 0x20000 && codePoint <= 0x3fffd)
|
|
37
|
+
)) return 2;
|
|
38
|
+
return 1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Turn raw PTY output into the final text a terminal would display. Used only
|
|
42
|
+
// when a session resumes after a resize: replaying raw cursor movements makes
|
|
43
|
+
// xterm reflow fragments of commands. `segments` is a sorted list of
|
|
44
|
+
// { at, cols } offsets into the raw string, one per width change, so bytes
|
|
45
|
+
// produced at 56 columns are still interpreted at 56 even if the newest output
|
|
46
|
+
// was produced at 134.
|
|
47
|
+
function replayTranscript(raw, segments = 120) {
|
|
48
|
+
const source = String(raw || '');
|
|
49
|
+
const widths = Array.isArray(segments)
|
|
50
|
+
? segments
|
|
51
|
+
.map((segment) => ({ at: Math.max(0, Math.floor(Number(segment.at) || 0)), cols: Number(segment.cols) || 120 }))
|
|
52
|
+
.sort((a, b) => a.at - b.at)
|
|
53
|
+
: [{ at: 0, cols: Number(segments) || 120 }];
|
|
54
|
+
if (!widths.length || widths[0].at > 0) widths.unshift({ at: 0, cols: widths[0]?.cols || 120 });
|
|
55
|
+
|
|
56
|
+
const rows = [];
|
|
57
|
+
let row = 0;
|
|
58
|
+
let col = 0;
|
|
59
|
+
let wrapPending = false;
|
|
60
|
+
|
|
61
|
+
const ensure = (index) => {
|
|
62
|
+
while (rows.length <= index) rows.push([]);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Erasing either half of a wide glyph clears the whole glyph, while
|
|
66
|
+
// preserving the remaining cell's blank column for subsequent writes.
|
|
67
|
+
const eraseCells = (start, end) => {
|
|
68
|
+
const line = rows[row];
|
|
69
|
+
if (start > 0 && line[start] === '') line[start - 1] = ' ';
|
|
70
|
+
if (end < line.length && line[end] === '') line[end] = ' ';
|
|
71
|
+
line.fill(' ', start, Math.min(end, line.length));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
let width = Math.max(2, Math.min(400, Math.floor(widths[0].cols)));
|
|
75
|
+
|
|
76
|
+
const put = (value, cells = 1) => {
|
|
77
|
+
ensure(row);
|
|
78
|
+
if (wrapPending) {
|
|
79
|
+
row += 1;
|
|
80
|
+
col = 0;
|
|
81
|
+
wrapPending = false;
|
|
82
|
+
ensure(row);
|
|
83
|
+
}
|
|
84
|
+
if (cells === 2 && col >= width - 1) {
|
|
85
|
+
row += 1;
|
|
86
|
+
col = 0;
|
|
87
|
+
ensure(row);
|
|
88
|
+
}
|
|
89
|
+
const line = rows[row];
|
|
90
|
+
while (line.length < col) line.push(' ');
|
|
91
|
+
line[col] = value;
|
|
92
|
+
if (cells === 2) line[col + 1] = '';
|
|
93
|
+
col += cells;
|
|
94
|
+
if (col >= width) {
|
|
95
|
+
col = width - 1;
|
|
96
|
+
wrapPending = true;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
for (let index = 0; index < source.length; index++) {
|
|
101
|
+
while (widths.length > 1 && widths[1].at <= index) {
|
|
102
|
+
widths.shift();
|
|
103
|
+
width = Math.max(2, Math.min(400, Math.floor(widths[0].cols)));
|
|
104
|
+
}
|
|
105
|
+
const ch = source[index];
|
|
106
|
+
if (ch === '\x1b') {
|
|
107
|
+
const rest = source.slice(index);
|
|
108
|
+
const osc = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/);
|
|
109
|
+
if (osc) { index += osc[0].length - 1; continue; }
|
|
110
|
+
const csi = rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/);
|
|
111
|
+
if (csi) {
|
|
112
|
+
const command = csi[0].slice(-1);
|
|
113
|
+
const parameters = csi[0].slice(2, -1).split(';').map(Number);
|
|
114
|
+
const mode = parameters[0] || 0;
|
|
115
|
+
const amount = Math.max(1, mode);
|
|
116
|
+
ensure(row);
|
|
117
|
+
if (command === 'K') {
|
|
118
|
+
if (mode === 0) { eraseCells(col, rows[row].length); rows[row].splice(col); }
|
|
119
|
+
else if (mode === 1) eraseCells(0, col + 1);
|
|
120
|
+
else if (mode === 2) rows[row] = [];
|
|
121
|
+
} else if (command === 'J') {
|
|
122
|
+
if (mode === 0) { eraseCells(col, rows[row].length); rows[row].splice(col); rows.splice(row + 1); }
|
|
123
|
+
else if (mode === 1) { for (let r = 0; r < row; r++) rows[r] = []; eraseCells(0, col + 1); }
|
|
124
|
+
else if (mode === 2) { for (let r = 0; r < rows.length; r++) rows[r] = []; }
|
|
125
|
+
} else if (command === 'X') {
|
|
126
|
+
eraseCells(col, col + amount);
|
|
127
|
+
} else if (command === 'P') {
|
|
128
|
+
eraseCells(col, col + amount);
|
|
129
|
+
rows[row].splice(col, amount);
|
|
130
|
+
} else if (command === 'G') {
|
|
131
|
+
col = Math.min(width - 1, amount - 1);
|
|
132
|
+
} else if (command === 'C') {
|
|
133
|
+
col = Math.min(width - 1, col + amount);
|
|
134
|
+
} else if (command === 'D') {
|
|
135
|
+
col = Math.max(0, col - amount);
|
|
136
|
+
}
|
|
137
|
+
if ('KJXPGCD'.includes(command)) wrapPending = false;
|
|
138
|
+
index += csi[0].length - 1;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const two = rest.match(/^\x1b[@-_]/);
|
|
142
|
+
if (two) { index += two[0].length - 1; continue; }
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (ch === '\r') {
|
|
146
|
+
col = 0;
|
|
147
|
+
wrapPending = false;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (ch === '\n') {
|
|
151
|
+
row += 1;
|
|
152
|
+
col = 0;
|
|
153
|
+
wrapPending = false;
|
|
154
|
+
ensure(row);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (ch === '\b') {
|
|
158
|
+
if (wrapPending) wrapPending = false;
|
|
159
|
+
else if (col > 0) col -= 1;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (ch === '\t') {
|
|
163
|
+
const target = Math.min(width - 1, (col + 8) - (col % 8));
|
|
164
|
+
while (col < target) put(' ');
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (ch === '\x07' || ch === '\x00') continue;
|
|
168
|
+
if (ch < ' ') continue;
|
|
169
|
+
|
|
170
|
+
const codePoint = source.codePointAt(index);
|
|
171
|
+
const value = String.fromCodePoint(codePoint);
|
|
172
|
+
if (codePoint > 0xffff) index += 1;
|
|
173
|
+
const cells = cellWidth(codePoint);
|
|
174
|
+
if (cells === 0) {
|
|
175
|
+
const line = rows[row];
|
|
176
|
+
if (line && col > 0) line[col - 1] = (line[col - 1] || '') + value;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
put(value, cells);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return rows
|
|
183
|
+
.map((line) => line.join('').replace(/\s+$/, ''))
|
|
184
|
+
.join('\n')
|
|
185
|
+
.replace(/^\n+/, '')
|
|
186
|
+
.replace(/\n+$/, '\n');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports = { replayTranscript, stripEscapes };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]']);
|
|
4
|
+
|
|
5
|
+
// A container is published on a host port that need not equal the port inside
|
|
6
|
+
// it, so an exact `host:port` match would refuse every request (including
|
|
7
|
+
// /health) for `-p 127.0.0.1:8080:11001`. The loopback *hostname* is what makes
|
|
8
|
+
// a request local; the port is the publisher's choice. When an Origin is
|
|
9
|
+
// present it must still match the Host exactly and pass the origin checks
|
|
10
|
+
// below, so a page on another local port cannot drive this server.
|
|
11
|
+
function isLoopbackHost(host) {
|
|
12
|
+
if (typeof host !== 'string') return false;
|
|
13
|
+
const name = host.startsWith('[')
|
|
14
|
+
? host.slice(0, host.indexOf(']') + 1)
|
|
15
|
+
: host.split(':')[0];
|
|
16
|
+
return LOOPBACK_HOSTS.has(name.toLowerCase());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The listener remains on loopback. These origins belong to separately
|
|
20
|
+
// authenticated HTTPS proxies that preserve the external Host header.
|
|
21
|
+
function createRequestBoundary(port, trustedOrigins = '') {
|
|
22
|
+
const allowedOrigins = new Set([
|
|
23
|
+
`http://localhost:${port}`, `http://127.0.0.1:${port}`, `http://[::1]:${port}`
|
|
24
|
+
].map((origin) => new URL(origin).origin));
|
|
25
|
+
for (const entry of trustedOrigins.split(',').map((value) => value.trim()).filter(Boolean)) {
|
|
26
|
+
let url;
|
|
27
|
+
try { url = new URL(entry); } catch { /* rejected below */ }
|
|
28
|
+
if (!url || url.protocol !== 'https:' || url.hostname.includes('*') || url.username || url.password || url.pathname !== '/' || url.search || url.hash) {
|
|
29
|
+
throw new Error('TERMINALONE_TRUSTED_ORIGINS must contain exact HTTPS origins, separated by commas (for example https://terminal.example.com).');
|
|
30
|
+
}
|
|
31
|
+
allowedOrigins.add(url.origin);
|
|
32
|
+
}
|
|
33
|
+
const allowedHosts = new Set([...allowedOrigins].map((origin) => new URL(origin).host));
|
|
34
|
+
return (req) => {
|
|
35
|
+
const host = req.headers.host;
|
|
36
|
+
if (typeof host !== 'string') return false;
|
|
37
|
+
const normalized = host.toLowerCase();
|
|
38
|
+
if (!isLoopbackHost(normalized) && !allowedHosts.has(normalized)) return false;
|
|
39
|
+
if (req.headers['sec-fetch-site'] === 'cross-site') return false;
|
|
40
|
+
const origin = req.headers.origin;
|
|
41
|
+
if (origin !== undefined) {
|
|
42
|
+
if (typeof origin !== 'string') return false;
|
|
43
|
+
try {
|
|
44
|
+
const url = new URL(origin);
|
|
45
|
+
const sameLoopback = isLoopbackHost(url.host) && url.host.toLowerCase() === normalized;
|
|
46
|
+
if (url.origin !== origin || url.host.toLowerCase() !== normalized) return false;
|
|
47
|
+
if (!allowedOrigins.has(url.origin) && !sameLoopback) return false;
|
|
48
|
+
} catch { return false; }
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isTrustedRequest(req, port) {
|
|
55
|
+
return createRequestBoundary(port)(req);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { isTrustedRequest, createRequestBoundary };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { Worker } = require('worker_threads');
|
|
5
|
+
|
|
6
|
+
// A wait pattern arrives from an API client and is tested against up to 200 KB
|
|
7
|
+
// of terminal output, repeatedly. JavaScript cannot interrupt a catastrophic
|
|
8
|
+
// backtrack, so a textual "is this pattern safe" heuristic is not enough:
|
|
9
|
+
// `((a+))+Z` and `(a|aa)+Z` defeat every simple guard, while `^ready.*?done$`
|
|
10
|
+
// and `\*+` are legitimate patterns a naive guard wrongly rejects.
|
|
11
|
+
//
|
|
12
|
+
// Evaluate in a worker thread and terminate it when a match exceeds its
|
|
13
|
+
// deadline. The event loop, and every other request, stays responsive.
|
|
14
|
+
const MAX_PATTERN = 200;
|
|
15
|
+
const DEFAULT_MATCH_TIMEOUT_MS = 500;
|
|
16
|
+
|
|
17
|
+
function createPatternMatcher(pattern, flags = 'im') {
|
|
18
|
+
if (typeof pattern !== 'string' || !pattern.trim()) {
|
|
19
|
+
return { error: 'Pattern must be a non-empty string.' };
|
|
20
|
+
}
|
|
21
|
+
if (pattern.length > MAX_PATTERN) {
|
|
22
|
+
return { error: `Pattern is longer than ${MAX_PATTERN} characters; use a literal match instead.` };
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
new RegExp(pattern, flags);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return { error: `Invalid regex: ${error.message}` };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const worker = new Worker(path.join(__dirname, 'regex-worker.js'));
|
|
31
|
+
worker.unref?.();
|
|
32
|
+
let sequence = 0;
|
|
33
|
+
let closed = false;
|
|
34
|
+
const pending = new Map();
|
|
35
|
+
|
|
36
|
+
const failPending = (error) => {
|
|
37
|
+
for (const entry of pending.values()) {
|
|
38
|
+
clearTimeout(entry.timer);
|
|
39
|
+
entry.reject(error);
|
|
40
|
+
}
|
|
41
|
+
pending.clear();
|
|
42
|
+
};
|
|
43
|
+
worker.on('message', ({ id, matched }) => {
|
|
44
|
+
const entry = pending.get(id);
|
|
45
|
+
if (!entry) return;
|
|
46
|
+
pending.delete(id);
|
|
47
|
+
clearTimeout(entry.timer);
|
|
48
|
+
entry.resolve(Boolean(matched));
|
|
49
|
+
});
|
|
50
|
+
worker.on('error', (error) => {
|
|
51
|
+
failPending(Object.assign(new Error(`Pattern worker failed: ${error.message}`), { status: 500 }));
|
|
52
|
+
});
|
|
53
|
+
worker.on('exit', () => {
|
|
54
|
+
if (!closed) failPending(Object.assign(new Error('Pattern worker exited.'), { status: 500 }));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const close = () => {
|
|
58
|
+
closed = true;
|
|
59
|
+
for (const entry of pending.values()) clearTimeout(entry.timer);
|
|
60
|
+
pending.clear();
|
|
61
|
+
worker.terminate().catch(() => {});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const test = (text, timeoutMs = DEFAULT_MATCH_TIMEOUT_MS) => {
|
|
65
|
+
if (closed) return Promise.reject(Object.assign(new Error('Pattern matcher is closed.'), { status: 500 }));
|
|
66
|
+
const id = ++sequence;
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const timer = setTimeout(() => {
|
|
69
|
+
pending.delete(id);
|
|
70
|
+
// The worker may be wedged inside the regex engine; kill it and refuse
|
|
71
|
+
// the pattern rather than waiting for a backtrack that may never end.
|
|
72
|
+
closed = true;
|
|
73
|
+
worker.terminate().catch(() => {});
|
|
74
|
+
reject(Object.assign(new Error('Pattern took too long; use a literal match instead.'), {
|
|
75
|
+
status: 400,
|
|
76
|
+
code: 'PATTERN_TIMEOUT'
|
|
77
|
+
}));
|
|
78
|
+
}, timeoutMs);
|
|
79
|
+
pending.set(id, { resolve, reject, timer });
|
|
80
|
+
worker.postMessage({ id, pattern, flags, text });
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return { test, close };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = { createPatternMatcher, MAX_PATTERN, DEFAULT_MATCH_TIMEOUT_MS };
|