Skip to content

Commit 369d2e8

Browse files
steipetewings1029
andcommitted
refactor(scripts): share UTF-8-safe output tails
Co-authored-by: 陈志强0668000989 <[email protected]>
1 parent 89aa5bd commit 369d2e8

7 files changed

Lines changed: 69 additions & 50 deletions

File tree

‎scripts/format-docs.d.mts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env node
2-
export function outputTail(value: string | Buffer): string;
32
export function docsFiles(root?: string, deps?: Record<string, unknown>): string[];
43
export function chunkFilesForCommand(
54
files: unknown,

‎scripts/format-docs.mjs‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from "node:path";
88
import { pathToFileURL } from "node:url";
99
import { resolveRepoToolBinPath } from "./lib/local-heavy-check-runtime.mjs";
1010
import { repairMintlifyAccordionIndentation } from "./lib/mintlify-accordion.mjs";
11+
import { outputTail } from "./lib/output-tail.mjs";
1112
import { buildCmdExeCommandLine, resolveWindowsCmdExePath } from "./windows-cmd-helpers.mjs";
1213

1314
const ROOT = path.resolve(import.meta.dirname, "..");
@@ -26,26 +27,6 @@ function outputText(value) {
2627
return "";
2728
}
2829

29-
function decodeUtf8Tail(buffer) {
30-
let start = 0;
31-
while (start < buffer.length && (buffer[start] & 0xc0) === 0x80) {
32-
start += 1;
33-
}
34-
return buffer.subarray(start).toString("utf8");
35-
}
36-
37-
export function outputTail(value) {
38-
const text = outputText(value).trim();
39-
if (!text) {
40-
return "";
41-
}
42-
const bytes = Buffer.from(text, "utf8");
43-
if (bytes.byteLength <= FAILURE_OUTPUT_TAIL_BYTES) {
44-
return text;
45-
}
46-
return decodeUtf8Tail(bytes.subarray(bytes.byteLength - FAILURE_OUTPUT_TAIL_BYTES));
47-
}
48-
4930
function commandFailureMessage(label, result, invocation) {
5031
const details = [];
5132
if (invocation) {
@@ -65,11 +46,11 @@ function commandFailureMessage(label, result, invocation) {
6546
if (result.signal) {
6647
details.push(`signal: ${result.signal}`);
6748
}
68-
const stderrTail = outputTail(result.stderr);
49+
const stderrTail = outputTail(result.stderr, FAILURE_OUTPUT_TAIL_BYTES);
6950
if (stderrTail) {
7051
details.push(`stderr tail:\n${stderrTail}`);
7152
}
72-
const stdoutTail = outputTail(result.stdout);
53+
const stdoutTail = outputTail(result.stdout, FAILURE_OUTPUT_TAIL_BYTES);
7354
if (stdoutTail) {
7455
details.push(`stdout tail:\n${stdoutTail}`);
7556
}

‎scripts/lib/format-generated-module.mjs‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,12 @@ import { spawnSync } from "node:child_process";
33
import fs from "node:fs";
44
import os from "node:os";
55
import path from "node:path";
6+
import { outputTail } from "./output-tail.mjs";
67

78
export const GENERATED_MODULE_FORMAT_TIMEOUT_MS = 30_000;
89
export const GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES = 1024 * 1024;
910
const FORMATTER_OUTPUT_TAIL_BYTES = 16 * 1024;
1011

11-
function outputText(value) {
12-
if (typeof value === "string") {
13-
return value;
14-
}
15-
if (Buffer.isBuffer(value)) {
16-
return value.toString("utf8");
17-
}
18-
return "";
19-
}
20-
21-
function outputTail(value) {
22-
const text = outputText(value).trim();
23-
if (!text) {
24-
return "";
25-
}
26-
const bytes = Buffer.from(text, "utf8");
27-
if (bytes.byteLength <= FORMATTER_OUTPUT_TAIL_BYTES) {
28-
return text;
29-
}
30-
return bytes.subarray(bytes.byteLength - FORMATTER_OUTPUT_TAIL_BYTES).toString("utf8");
31-
}
32-
3312
function formatterFailureDetails(formatter) {
3413
const details = [];
3514
const errorCode = formatter.error?.code;
@@ -46,11 +25,11 @@ function formatterFailureDetails(formatter) {
4625
if (formatter.signal) {
4726
details.push(`formatter exited with signal ${formatter.signal}`);
4827
}
49-
const stderrTail = outputTail(formatter.stderr);
28+
const stderrTail = outputTail(formatter.stderr, FORMATTER_OUTPUT_TAIL_BYTES);
5029
if (stderrTail) {
5130
details.push(`stderr tail:\n${stderrTail}`);
5231
}
53-
const stdoutTail = outputTail(formatter.stdout);
32+
const stdoutTail = outputTail(formatter.stdout, FORMATTER_OUTPUT_TAIL_BYTES);
5433
if (stdoutTail) {
5534
details.push(`stdout tail:\n${stdoutTail}`);
5635
}

‎scripts/lib/output-tail.d.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function outputTail(value: string | Buffer | null | undefined, maxBytes: number): string;

‎scripts/lib/output-tail.mjs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Keeps child-process diagnostic tails byte-bounded without splitting UTF-8 characters.
2+
3+
function outputText(value) {
4+
if (typeof value === "string") {
5+
return value;
6+
}
7+
if (Buffer.isBuffer(value)) {
8+
return value.toString("utf8");
9+
}
10+
return "";
11+
}
12+
13+
function decodeUtf8Tail(buffer) {
14+
let start = 0;
15+
while (start < buffer.length && (buffer[start] & 0b1100_0000) === 0b1000_0000) {
16+
start += 1;
17+
}
18+
return buffer.subarray(start).toString("utf8");
19+
}
20+
21+
export function outputTail(value, maxBytes) {
22+
const text = outputText(value).trim();
23+
if (!text) {
24+
return "";
25+
}
26+
const bytes = Buffer.from(text, "utf8");
27+
if (bytes.byteLength <= maxBytes) {
28+
return text;
29+
}
30+
return decodeUtf8Tail(bytes.subarray(bytes.byteLength - maxBytes));
31+
}

‎test/scripts/format-docs.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
chunkFilesForCommand,
77
docsFiles,
88
formatDocs,
9-
outputTail,
109
resolveOxfmtInvocation,
1110
runOxfmt,
1211
} from "../../scripts/format-docs.mjs";
12+
import { outputTail } from "../../scripts/lib/output-tail.mjs";
1313
import { createScriptTestHarness } from "./test-helpers.js";
1414

1515
const { createTempDir } = createScriptTestHarness();
@@ -159,13 +159,13 @@ describe("outputTail UTF-8 safety", () => {
159159
// "你好" (6 bytes) + 16380 x's = 16386 bytes, exceeding the 16 KiB cap.
160160
// subarray(-16384) starts at byte 2 = 0xA0 (continuation of 你).
161161
const bigText = "你好" + "x".repeat(16380);
162-
const result = outputTail(bigText);
162+
const result = outputTail(bigText, 16 * 1024);
163163
expect(result).not.toContain("�");
164164
// The continuation byte is skipped; the tail starts with "好".
165165
expect(result).toMatch(/^好x/);
166166
});
167167

168168
it("passes through output that fits within the tail budget", () => {
169-
expect(outputTail("hello world")).toBe("hello world");
169+
expect(outputTail("hello world", 16 * 1024)).toBe("hello world");
170170
});
171171
});

‎test/scripts/format-generated-module.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,32 @@ describe("formatGeneratedModule", () => {
115115
expect(message).not.toContain("DO_NOT_DUMP_OLD_STDOUT");
116116
}
117117
});
118+
119+
it("keeps truncated formatter diagnostics UTF-8 safe", () => {
120+
const repoRoot = makeRepoRoot();
121+
const splitBoundaryOutput = `你好${"x".repeat(16_380)}`;
122+
let message = "";
123+
124+
try {
125+
formatGeneratedModule(
126+
"export const value=1;",
127+
{
128+
errorLabel: "test module",
129+
outputPath: "generated.ts",
130+
repoRoot,
131+
},
132+
{
133+
spawnSync: () => ({
134+
status: 1,
135+
stderr: splitBoundaryOutput,
136+
stdout: "",
137+
}),
138+
},
139+
);
140+
} catch (error) {
141+
message = error instanceof Error ? error.message : String(error);
142+
}
143+
expect(message).toMatch(/stderr tail:\n好x/u);
144+
expect(message).not.toContain("�");
145+
});
118146
});

0 commit comments

Comments
 (0)