mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
fix(cli): improve stdin handling and add initial state check (#6747)
This commit is contained in:
@@ -9,40 +9,86 @@ export async function readStdin(): Promise<string> {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data = '';
|
let data = '';
|
||||||
let totalSize = 0;
|
let totalSize = 0;
|
||||||
|
let hasReceivedData = false;
|
||||||
|
|
||||||
process.stdin.setEncoding('utf8');
|
process.stdin.setEncoding('utf8');
|
||||||
|
|
||||||
const onReadable = () => {
|
function cleanup() {
|
||||||
let chunk;
|
clearTimeout(timeout);
|
||||||
while ((chunk = process.stdin.read()) !== null) {
|
process.stdin.removeListener('readable', onReadable);
|
||||||
|
process.stdin.removeListener('end', onEnd);
|
||||||
|
process.stdin.removeListener('error', onError);
|
||||||
|
}
|
||||||
|
|
||||||
|
function processChunk(chunk: string): boolean {
|
||||||
|
hasReceivedData = true;
|
||||||
if (totalSize + chunk.length > MAX_STDIN_SIZE) {
|
if (totalSize + chunk.length > MAX_STDIN_SIZE) {
|
||||||
const remainingSize = MAX_STDIN_SIZE - totalSize;
|
const remainingSize = MAX_STDIN_SIZE - totalSize;
|
||||||
data += chunk.slice(0, remainingSize);
|
data += chunk.slice(0, remainingSize);
|
||||||
console.warn(
|
console.warn(
|
||||||
`Warning: stdin input truncated to ${MAX_STDIN_SIZE} bytes.`,
|
`Warning: stdin input truncated to ${MAX_STDIN_SIZE} bytes.`,
|
||||||
);
|
);
|
||||||
process.stdin.destroy(); // Stop reading further
|
process.stdin.destroy();
|
||||||
break;
|
return true; // Indicates truncation occurred
|
||||||
}
|
} else {
|
||||||
data += chunk;
|
data += chunk;
|
||||||
totalSize += chunk.length;
|
totalSize += chunk.length;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const onEnd = () => {
|
function checkInitialState(): boolean {
|
||||||
|
if (process.stdin.destroyed || process.stdin.readableEnded) {
|
||||||
|
cleanup();
|
||||||
|
resolve('');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunk = process.stdin.read();
|
||||||
|
if (chunk !== null) {
|
||||||
|
processChunk(chunk);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!process.stdin.readable) {
|
||||||
|
cleanup();
|
||||||
|
resolve('');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkInitialState()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onReadable() {
|
||||||
|
let chunk;
|
||||||
|
while ((chunk = process.stdin.read()) !== null) {
|
||||||
|
const truncated = processChunk(chunk);
|
||||||
|
if (truncated) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEnd() {
|
||||||
cleanup();
|
cleanup();
|
||||||
resolve(data);
|
resolve(data);
|
||||||
};
|
}
|
||||||
|
|
||||||
const onError = (err: Error) => {
|
function onError(err: Error) {
|
||||||
cleanup();
|
cleanup();
|
||||||
reject(err);
|
reject(err);
|
||||||
};
|
}
|
||||||
|
|
||||||
const cleanup = () => {
|
const timeout = setTimeout(() => {
|
||||||
process.stdin.removeListener('readable', onReadable);
|
if (!hasReceivedData) {
|
||||||
process.stdin.removeListener('end', onEnd);
|
cleanup();
|
||||||
process.stdin.removeListener('error', onError);
|
resolve('');
|
||||||
};
|
}
|
||||||
|
}, 50);
|
||||||
|
|
||||||
process.stdin.on('readable', onReadable);
|
process.stdin.on('readable', onReadable);
|
||||||
process.stdin.on('end', onEnd);
|
process.stdin.on('end', onEnd);
|
||||||
|
|||||||
Reference in New Issue
Block a user