Compare commits

..

No commits in common. "870bc8e3988a83c0ef8d4730db14885939eafa8b" and "37dc1b173216ae98a72aece3945f3e443fd1047e" have entirely different histories.

2 changed files with 4 additions and 37 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@because/shout", "name": "@because/shout",
"version": "0.0.10", "version": "0.0.9",
"description": "shell output tester", "description": "shell output tester",
"module": "src/index.ts", "module": "src/index.ts",
"type": "module", "type": "module",

View File

@ -228,12 +228,13 @@ export async function runFile(
proc.stdin.end() proc.stdin.end()
const totalTimeout = options.timeout * file.commands.length const totalTimeout = options.timeout * file.commands.length
const lastSentinelSuffix = `_${file.commands.length - 1}__` const stdout = await readWithTimeout(proc.stdout, totalTimeout)
const stdout = await readUntilSentinel(proc.stdout, sentinel, lastSentinelSuffix, totalTimeout)
if (!verbose) { if (!verbose) {
await readWithTimeout(proc.stderr, 1000).catch(() => "") await readWithTimeout(proc.stderr, 1000).catch(() => "")
} }
await proc.exited
const { outputs, exitCodes } = parseSentinelOutput( const { outputs, exitCodes } = parseSentinelOutput(
stdout, stdout,
sentinel, sentinel,
@ -261,40 +262,6 @@ export async function runFile(
} }
} }
async function readUntilSentinel(
stream: ReadableStream<Uint8Array>,
sentinelPrefix: string,
sentinelSuffix: string,
timeoutMs: number,
): Promise<string> {
const reader = stream.getReader()
const decoder = new TextDecoder()
let accumulated = ""
let timerId: ReturnType<typeof setTimeout>
const timeout = new Promise<never>((_, reject) =>
timerId = setTimeout(() => reject(new Error("Timeout reading output")), timeoutMs),
)
try {
while (true) {
const { done, value } = await Promise.race([reader.read(), timeout]) as ReadableStreamReadResult<Uint8Array>
if (done) break
if (value) {
accumulated += decoder.decode(value, { stream: true })
// Check if the last sentinel has appeared (prefix + exitcode + suffix)
const prefixIdx = accumulated.lastIndexOf(sentinelPrefix)
if (prefixIdx !== -1 && accumulated.indexOf(sentinelSuffix, prefixIdx) !== -1) break
}
}
} finally {
clearTimeout(timerId!)
reader.releaseLock()
}
return accumulated + decoder.decode()
}
async function readWithTimeout( async function readWithTimeout(
stream: ReadableStream<Uint8Array>, stream: ReadableStream<Uint8Array>,
timeoutMs: number, timeoutMs: number,