pi-querit 1.1.0 → 1.1.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/LICENSE +21 -21
- package/README.md +186 -186
- package/index.ts +2 -0
- package/package.json +64 -63
- package/src/client.ts +385 -385
- package/src/config.ts +200 -200
- package/src/format.ts +101 -105
- package/src/index.ts +378 -378
- package/src/output.ts +138 -138
- package/src/sanitize.ts +97 -97
- package/src/setup.ts +330 -330
package/src/output.ts
CHANGED
|
@@ -1,138 +1,138 @@
|
|
|
1
|
-
import { rmSync } from "node:fs";
|
|
2
|
-
import { mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
DEFAULT_MAX_BYTES,
|
|
7
|
-
DEFAULT_MAX_LINES,
|
|
8
|
-
formatSize,
|
|
9
|
-
truncateHead,
|
|
10
|
-
withFileMutationQueue,
|
|
11
|
-
type TruncationResult,
|
|
12
|
-
} from "@earendil-works/pi-coding-agent";
|
|
13
|
-
|
|
14
|
-
const TEMP_DIRECTORY_NAME = "pi-querit";
|
|
15
|
-
const TEMP_FILE_PATTERN = /^(.+)-(\d+)-(\d+)(\.[^.]+)?$/;
|
|
16
|
-
|
|
17
|
-
const createdFiles = new Set<string>();
|
|
18
|
-
let fileCounter = 0;
|
|
19
|
-
let exitCleanupRegistered = false;
|
|
20
|
-
|
|
21
|
-
export interface LimitedOutput {
|
|
22
|
-
text: string;
|
|
23
|
-
truncation?: TruncationResult;
|
|
24
|
-
fullOutputPath?: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function limitToolOutput(text: string, fileName: string): Promise<LimitedOutput> {
|
|
28
|
-
const initial = truncateHead(text, {
|
|
29
|
-
maxBytes: DEFAULT_MAX_BYTES,
|
|
30
|
-
maxLines: DEFAULT_MAX_LINES,
|
|
31
|
-
});
|
|
32
|
-
if (!initial.truncated) return { text };
|
|
33
|
-
|
|
34
|
-
const fullOutputPath = await reserveTempFile(fileName);
|
|
35
|
-
await withFileMutationQueue(fullOutputPath, async () => {
|
|
36
|
-
await writeFile(fullOutputPath, text, "utf8");
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const notice = `[Output truncated from ${initial.totalLines} lines (${formatSize(initial.totalBytes)}). Full output saved to: ${fullOutputPath}]`;
|
|
40
|
-
const suffix = `\n\n${notice}`;
|
|
41
|
-
let bodyByteLimit = Math.max(1, DEFAULT_MAX_BYTES - Buffer.byteLength(suffix, "utf8"));
|
|
42
|
-
let bodyLineLimit = Math.max(1, DEFAULT_MAX_LINES - 2);
|
|
43
|
-
let truncation = truncateHead(text, { maxBytes: bodyByteLimit, maxLines: bodyLineLimit });
|
|
44
|
-
let resultText = `${truncation.content}${suffix}`;
|
|
45
|
-
|
|
46
|
-
while (Buffer.byteLength(resultText, "utf8") > DEFAULT_MAX_BYTES || lineCount(resultText) > DEFAULT_MAX_LINES) {
|
|
47
|
-
const byteOverflow = Math.max(0, Buffer.byteLength(resultText, "utf8") - DEFAULT_MAX_BYTES);
|
|
48
|
-
const lineOverflow = Math.max(0, lineCount(resultText) - DEFAULT_MAX_LINES);
|
|
49
|
-
const nextByteLimit = Math.max(1, bodyByteLimit - byteOverflow);
|
|
50
|
-
const nextLineLimit = Math.max(1, bodyLineLimit - lineOverflow);
|
|
51
|
-
// Safety valve: if limits cannot shrink further the suffix alone exceeds the
|
|
52
|
-
// cap — stop iterating instead of looping forever.
|
|
53
|
-
if (nextByteLimit === bodyByteLimit && nextLineLimit === bodyLineLimit) break;
|
|
54
|
-
bodyByteLimit = nextByteLimit;
|
|
55
|
-
bodyLineLimit = nextLineLimit;
|
|
56
|
-
truncation = truncateHead(text, { maxBytes: bodyByteLimit, maxLines: bodyLineLimit });
|
|
57
|
-
resultText = `${truncation.content}${suffix}`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
text: resultText,
|
|
62
|
-
truncation,
|
|
63
|
-
fullOutputPath,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function tempDirectoryPath(): string {
|
|
68
|
-
return join(tmpdir(), TEMP_DIRECTORY_NAME);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* All truncated output lives in one shared temp directory so search content never
|
|
73
|
-
* scatters across per-call folders. File names embed the creating pid so concurrent
|
|
74
|
-
* Pi instances cannot collide and stale files from dead processes can be swept.
|
|
75
|
-
*/
|
|
76
|
-
async function reserveTempFile(fileName: string): Promise<string> {
|
|
77
|
-
// mkdir on every call: the directory may have been removed externally since a
|
|
78
|
-
// previous write; recursive mkdir on an existing directory is a cheap no-op.
|
|
79
|
-
await mkdir(tempDirectoryPath(), { recursive: true });
|
|
80
|
-
const dot = fileName.lastIndexOf(".");
|
|
81
|
-
const base = dot === -1 ? fileName : fileName.slice(0, dot);
|
|
82
|
-
const extension = dot === -1 ? "" : fileName.slice(dot);
|
|
83
|
-
const path = join(tempDirectoryPath(), `${base}-${process.pid}-${++fileCounter}${extension}`);
|
|
84
|
-
createdFiles.add(path);
|
|
85
|
-
return path;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Deletes only the files this process created; the shared folder is never removed. */
|
|
89
|
-
export async function cleanupTempFiles(): Promise<void> {
|
|
90
|
-
const paths = [...createdFiles];
|
|
91
|
-
createdFiles.clear();
|
|
92
|
-
await Promise.all(paths.map((path) => rm(path, { force: true }).catch(() => undefined)));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Synchronous best-effort fallback for exits that skip session_shutdown (e.g. signals). */
|
|
96
|
-
export function registerProcessExitCleanup(): void {
|
|
97
|
-
if (exitCleanupRegistered) return;
|
|
98
|
-
exitCleanupRegistered = true;
|
|
99
|
-
process.once("exit", () => {
|
|
100
|
-
for (const path of createdFiles) {
|
|
101
|
-
try {
|
|
102
|
-
rmSync(path, { force: true });
|
|
103
|
-
} catch {
|
|
104
|
-
// Best effort only.
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
createdFiles.clear();
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** Removes leftovers from previous runs whose process no longer exists. */
|
|
112
|
-
export async function sweepStaleTempFiles(): Promise<void> {
|
|
113
|
-
let entries: string[];
|
|
114
|
-
try {
|
|
115
|
-
entries = await readdir(tempDirectoryPath());
|
|
116
|
-
} catch {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
await Promise.all(entries.map(async (entry) => {
|
|
120
|
-
const match = TEMP_FILE_PATTERN.exec(entry);
|
|
121
|
-
if (!match || isProcessAlive(Number(match[2]))) return;
|
|
122
|
-
await rm(join(tempDirectoryPath(), entry), { force: true }).catch(() => undefined);
|
|
123
|
-
}));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function isProcessAlive(pid: number): boolean {
|
|
127
|
-
try {
|
|
128
|
-
process.kill(pid, 0);
|
|
129
|
-
return true;
|
|
130
|
-
} catch (error) {
|
|
131
|
-
// EPERM means the process exists but belongs to another user.
|
|
132
|
-
return (error as NodeJS.ErrnoException).code === "EPERM";
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function lineCount(value: string): number {
|
|
137
|
-
return value.length === 0 ? 0 : value.split("\n").length;
|
|
138
|
-
}
|
|
1
|
+
import { rmSync } from "node:fs";
|
|
2
|
+
import { mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_MAX_BYTES,
|
|
7
|
+
DEFAULT_MAX_LINES,
|
|
8
|
+
formatSize,
|
|
9
|
+
truncateHead,
|
|
10
|
+
withFileMutationQueue,
|
|
11
|
+
type TruncationResult,
|
|
12
|
+
} from "@earendil-works/pi-coding-agent";
|
|
13
|
+
|
|
14
|
+
const TEMP_DIRECTORY_NAME = "pi-querit";
|
|
15
|
+
const TEMP_FILE_PATTERN = /^(.+)-(\d+)-(\d+)(\.[^.]+)?$/;
|
|
16
|
+
|
|
17
|
+
const createdFiles = new Set<string>();
|
|
18
|
+
let fileCounter = 0;
|
|
19
|
+
let exitCleanupRegistered = false;
|
|
20
|
+
|
|
21
|
+
export interface LimitedOutput {
|
|
22
|
+
text: string;
|
|
23
|
+
truncation?: TruncationResult;
|
|
24
|
+
fullOutputPath?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function limitToolOutput(text: string, fileName: string): Promise<LimitedOutput> {
|
|
28
|
+
const initial = truncateHead(text, {
|
|
29
|
+
maxBytes: DEFAULT_MAX_BYTES,
|
|
30
|
+
maxLines: DEFAULT_MAX_LINES,
|
|
31
|
+
});
|
|
32
|
+
if (!initial.truncated) return { text };
|
|
33
|
+
|
|
34
|
+
const fullOutputPath = await reserveTempFile(fileName);
|
|
35
|
+
await withFileMutationQueue(fullOutputPath, async () => {
|
|
36
|
+
await writeFile(fullOutputPath, text, "utf8");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const notice = `[Output truncated from ${initial.totalLines} lines (${formatSize(initial.totalBytes)}). Full output saved to: ${fullOutputPath}]`;
|
|
40
|
+
const suffix = `\n\n${notice}`;
|
|
41
|
+
let bodyByteLimit = Math.max(1, DEFAULT_MAX_BYTES - Buffer.byteLength(suffix, "utf8"));
|
|
42
|
+
let bodyLineLimit = Math.max(1, DEFAULT_MAX_LINES - 2);
|
|
43
|
+
let truncation = truncateHead(text, { maxBytes: bodyByteLimit, maxLines: bodyLineLimit });
|
|
44
|
+
let resultText = `${truncation.content}${suffix}`;
|
|
45
|
+
|
|
46
|
+
while (Buffer.byteLength(resultText, "utf8") > DEFAULT_MAX_BYTES || lineCount(resultText) > DEFAULT_MAX_LINES) {
|
|
47
|
+
const byteOverflow = Math.max(0, Buffer.byteLength(resultText, "utf8") - DEFAULT_MAX_BYTES);
|
|
48
|
+
const lineOverflow = Math.max(0, lineCount(resultText) - DEFAULT_MAX_LINES);
|
|
49
|
+
const nextByteLimit = Math.max(1, bodyByteLimit - byteOverflow);
|
|
50
|
+
const nextLineLimit = Math.max(1, bodyLineLimit - lineOverflow);
|
|
51
|
+
// Safety valve: if limits cannot shrink further the suffix alone exceeds the
|
|
52
|
+
// cap — stop iterating instead of looping forever.
|
|
53
|
+
if (nextByteLimit === bodyByteLimit && nextLineLimit === bodyLineLimit) break;
|
|
54
|
+
bodyByteLimit = nextByteLimit;
|
|
55
|
+
bodyLineLimit = nextLineLimit;
|
|
56
|
+
truncation = truncateHead(text, { maxBytes: bodyByteLimit, maxLines: bodyLineLimit });
|
|
57
|
+
resultText = `${truncation.content}${suffix}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
text: resultText,
|
|
62
|
+
truncation,
|
|
63
|
+
fullOutputPath,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function tempDirectoryPath(): string {
|
|
68
|
+
return join(tmpdir(), TEMP_DIRECTORY_NAME);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* All truncated output lives in one shared temp directory so search content never
|
|
73
|
+
* scatters across per-call folders. File names embed the creating pid so concurrent
|
|
74
|
+
* Pi instances cannot collide and stale files from dead processes can be swept.
|
|
75
|
+
*/
|
|
76
|
+
async function reserveTempFile(fileName: string): Promise<string> {
|
|
77
|
+
// mkdir on every call: the directory may have been removed externally since a
|
|
78
|
+
// previous write; recursive mkdir on an existing directory is a cheap no-op.
|
|
79
|
+
await mkdir(tempDirectoryPath(), { recursive: true });
|
|
80
|
+
const dot = fileName.lastIndexOf(".");
|
|
81
|
+
const base = dot === -1 ? fileName : fileName.slice(0, dot);
|
|
82
|
+
const extension = dot === -1 ? "" : fileName.slice(dot);
|
|
83
|
+
const path = join(tempDirectoryPath(), `${base}-${process.pid}-${++fileCounter}${extension}`);
|
|
84
|
+
createdFiles.add(path);
|
|
85
|
+
return path;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Deletes only the files this process created; the shared folder is never removed. */
|
|
89
|
+
export async function cleanupTempFiles(): Promise<void> {
|
|
90
|
+
const paths = [...createdFiles];
|
|
91
|
+
createdFiles.clear();
|
|
92
|
+
await Promise.all(paths.map((path) => rm(path, { force: true }).catch(() => undefined)));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Synchronous best-effort fallback for exits that skip session_shutdown (e.g. signals). */
|
|
96
|
+
export function registerProcessExitCleanup(): void {
|
|
97
|
+
if (exitCleanupRegistered) return;
|
|
98
|
+
exitCleanupRegistered = true;
|
|
99
|
+
process.once("exit", () => {
|
|
100
|
+
for (const path of createdFiles) {
|
|
101
|
+
try {
|
|
102
|
+
rmSync(path, { force: true });
|
|
103
|
+
} catch {
|
|
104
|
+
// Best effort only.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
createdFiles.clear();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Removes leftovers from previous runs whose process no longer exists. */
|
|
112
|
+
export async function sweepStaleTempFiles(): Promise<void> {
|
|
113
|
+
let entries: string[];
|
|
114
|
+
try {
|
|
115
|
+
entries = await readdir(tempDirectoryPath());
|
|
116
|
+
} catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
await Promise.all(entries.map(async (entry) => {
|
|
120
|
+
const match = TEMP_FILE_PATTERN.exec(entry);
|
|
121
|
+
if (!match || isProcessAlive(Number(match[2]))) return;
|
|
122
|
+
await rm(join(tempDirectoryPath(), entry), { force: true }).catch(() => undefined);
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isProcessAlive(pid: number): boolean {
|
|
127
|
+
try {
|
|
128
|
+
process.kill(pid, 0);
|
|
129
|
+
return true;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
// EPERM means the process exists but belongs to another user.
|
|
132
|
+
return (error as NodeJS.ErrnoException).code === "EPERM";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function lineCount(value: string): number {
|
|
137
|
+
return value.length === 0 ? 0 : value.split("\n").length;
|
|
138
|
+
}
|
package/src/sanitize.ts
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
const ESC = 0x1b;
|
|
2
|
-
const BEL = 0x07;
|
|
3
|
-
const DELETE = 0x7f;
|
|
4
|
-
const CSI = 0x9b;
|
|
5
|
-
const ST = 0x9c;
|
|
6
|
-
const OSC = 0x9d;
|
|
7
|
-
const CONTROL_STRING_STARTS = new Set([0x90, 0x98, 0x9e, 0x9f]);
|
|
8
|
-
const ESC_CONTROL_STRING_STARTS = new Set([0x50, 0x58, 0x5e, 0x5f]);
|
|
9
|
-
const BIDI_CONTROLS = /[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/gu;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Remove terminal escape/control sequences from untrusted remote text.
|
|
13
|
-
* Newlines and tabs are preserved so page content remains readable.
|
|
14
|
-
*/
|
|
15
|
-
export function sanitizeTerminalText(value: string): string {
|
|
16
|
-
let output = "";
|
|
17
|
-
|
|
18
|
-
for (let index = 0; index < value.length;) {
|
|
19
|
-
const code = value.charCodeAt(index);
|
|
20
|
-
|
|
21
|
-
if (code === ESC) {
|
|
22
|
-
const next = value.charCodeAt(index + 1);
|
|
23
|
-
if (next === 0x5b) {
|
|
24
|
-
index = skipCsi(value, index + 2);
|
|
25
|
-
} else if (next === 0x5d) {
|
|
26
|
-
index = skipOsc(value, index + 2);
|
|
27
|
-
} else if (ESC_CONTROL_STRING_STARTS.has(next)) {
|
|
28
|
-
index = skipControlString(value, index + 2);
|
|
29
|
-
} else {
|
|
30
|
-
index += Number.isNaN(next) ? 1 : 2;
|
|
31
|
-
}
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (code === CSI) {
|
|
36
|
-
index = skipCsi(value, index + 1);
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (code === OSC) {
|
|
40
|
-
index = skipOsc(value, index + 1);
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
if (CONTROL_STRING_STARTS.has(code)) {
|
|
44
|
-
index = skipControlString(value, index + 1);
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (code === 0x0a || code === 0x09) {
|
|
49
|
-
output += value[index];
|
|
50
|
-
index += 1;
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
if (code < 0x20 || (code >= 0x80 && code <= 0x9f) || code === DELETE) {
|
|
54
|
-
index += 1;
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const codePoint = value.codePointAt(index);
|
|
59
|
-
if (codePoint === undefined) break;
|
|
60
|
-
output += String.fromCodePoint(codePoint);
|
|
61
|
-
index += codePoint > 0xffff ? 2 : 1;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return output.replace(BIDI_CONTROLS, "");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function skipCsi(value: string, start: number): number {
|
|
68
|
-
let index = start;
|
|
69
|
-
while (index < value.length) {
|
|
70
|
-
const code = value.charCodeAt(index);
|
|
71
|
-
index += 1;
|
|
72
|
-
if (code >= 0x40 && code <= 0x7e) return index;
|
|
73
|
-
}
|
|
74
|
-
return index;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function skipOsc(value: string, start: number): number {
|
|
78
|
-
let index = start;
|
|
79
|
-
while (index < value.length) {
|
|
80
|
-
const code = value.charCodeAt(index);
|
|
81
|
-
if (code === BEL || code === ST) return index + 1;
|
|
82
|
-
if (code === ESC && value.charCodeAt(index + 1) === 0x5c) return index + 2;
|
|
83
|
-
index += 1;
|
|
84
|
-
}
|
|
85
|
-
return index;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function skipControlString(value: string, start: number): number {
|
|
89
|
-
let index = start;
|
|
90
|
-
while (index < value.length) {
|
|
91
|
-
const code = value.charCodeAt(index);
|
|
92
|
-
if (code === ST) return index + 1;
|
|
93
|
-
if (code === ESC && value.charCodeAt(index + 1) === 0x5c) return index + 2;
|
|
94
|
-
index += 1;
|
|
95
|
-
}
|
|
96
|
-
return index;
|
|
97
|
-
}
|
|
1
|
+
const ESC = 0x1b;
|
|
2
|
+
const BEL = 0x07;
|
|
3
|
+
const DELETE = 0x7f;
|
|
4
|
+
const CSI = 0x9b;
|
|
5
|
+
const ST = 0x9c;
|
|
6
|
+
const OSC = 0x9d;
|
|
7
|
+
const CONTROL_STRING_STARTS = new Set([0x90, 0x98, 0x9e, 0x9f]);
|
|
8
|
+
const ESC_CONTROL_STRING_STARTS = new Set([0x50, 0x58, 0x5e, 0x5f]);
|
|
9
|
+
const BIDI_CONTROLS = /[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/gu;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Remove terminal escape/control sequences from untrusted remote text.
|
|
13
|
+
* Newlines and tabs are preserved so page content remains readable.
|
|
14
|
+
*/
|
|
15
|
+
export function sanitizeTerminalText(value: string): string {
|
|
16
|
+
let output = "";
|
|
17
|
+
|
|
18
|
+
for (let index = 0; index < value.length;) {
|
|
19
|
+
const code = value.charCodeAt(index);
|
|
20
|
+
|
|
21
|
+
if (code === ESC) {
|
|
22
|
+
const next = value.charCodeAt(index + 1);
|
|
23
|
+
if (next === 0x5b) {
|
|
24
|
+
index = skipCsi(value, index + 2);
|
|
25
|
+
} else if (next === 0x5d) {
|
|
26
|
+
index = skipOsc(value, index + 2);
|
|
27
|
+
} else if (ESC_CONTROL_STRING_STARTS.has(next)) {
|
|
28
|
+
index = skipControlString(value, index + 2);
|
|
29
|
+
} else {
|
|
30
|
+
index += Number.isNaN(next) ? 1 : 2;
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (code === CSI) {
|
|
36
|
+
index = skipCsi(value, index + 1);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (code === OSC) {
|
|
40
|
+
index = skipOsc(value, index + 1);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (CONTROL_STRING_STARTS.has(code)) {
|
|
44
|
+
index = skipControlString(value, index + 1);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (code === 0x0a || code === 0x09) {
|
|
49
|
+
output += value[index];
|
|
50
|
+
index += 1;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (code < 0x20 || (code >= 0x80 && code <= 0x9f) || code === DELETE) {
|
|
54
|
+
index += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const codePoint = value.codePointAt(index);
|
|
59
|
+
if (codePoint === undefined) break;
|
|
60
|
+
output += String.fromCodePoint(codePoint);
|
|
61
|
+
index += codePoint > 0xffff ? 2 : 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return output.replace(BIDI_CONTROLS, "");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function skipCsi(value: string, start: number): number {
|
|
68
|
+
let index = start;
|
|
69
|
+
while (index < value.length) {
|
|
70
|
+
const code = value.charCodeAt(index);
|
|
71
|
+
index += 1;
|
|
72
|
+
if (code >= 0x40 && code <= 0x7e) return index;
|
|
73
|
+
}
|
|
74
|
+
return index;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function skipOsc(value: string, start: number): number {
|
|
78
|
+
let index = start;
|
|
79
|
+
while (index < value.length) {
|
|
80
|
+
const code = value.charCodeAt(index);
|
|
81
|
+
if (code === BEL || code === ST) return index + 1;
|
|
82
|
+
if (code === ESC && value.charCodeAt(index + 1) === 0x5c) return index + 2;
|
|
83
|
+
index += 1;
|
|
84
|
+
}
|
|
85
|
+
return index;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function skipControlString(value: string, start: number): number {
|
|
89
|
+
let index = start;
|
|
90
|
+
while (index < value.length) {
|
|
91
|
+
const code = value.charCodeAt(index);
|
|
92
|
+
if (code === ST) return index + 1;
|
|
93
|
+
if (code === ESC && value.charCodeAt(index + 1) === 0x5c) return index + 2;
|
|
94
|
+
index += 1;
|
|
95
|
+
}
|
|
96
|
+
return index;
|
|
97
|
+
}
|