Skip to content

Commit 4418e49

Browse files
committed
chore: fix buffer overwrite
1 parent 1c20cd5 commit 4418e49

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

packages/vitest/src/runtime/console.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,18 @@ export function createCustomConsole(defaultState?: WorkerGlobalState) {
5454
})
5555
}
5656
function sendStdout(taskId: string) {
57-
const buffer = stdoutBuffer.get(taskId)
58-
if (!buffer)
59-
return
60-
sendBuffer(buffer, taskId, 'stdout')
57+
sendBuffer('stdout', taskId)
6158
}
6259

6360
function sendStderr(taskId: string) {
64-
const buffer = stderrBuffer.get(taskId)
65-
if (!buffer)
66-
return
67-
sendBuffer(buffer, taskId, 'stderr')
61+
sendBuffer('stderr', taskId)
6862
}
6963

70-
function sendBuffer(buffer: any[], taskId: string, type: 'stdout' | 'stderr') {
64+
function sendBuffer(type: 'stdout' | 'stderr', taskId: string) {
65+
const buffers = type === 'stdout' ? stdoutBuffer : stderrBuffer
66+
const buffer = buffers.get(taskId)
67+
if (!buffer)
68+
return
7169
if (state().config.printConsoleTrace) {
7270
buffer.forEach(([buffer, origin]) => {
7371
sendLog(type, taskId, String(buffer), buffer.length, origin)
@@ -78,7 +76,7 @@ export function createCustomConsole(defaultState?: WorkerGlobalState) {
7876
sendLog(type, taskId, content, buffer.length)
7977
}
8078
const timer = timers.get(taskId)!
81-
stderrBuffer.set(taskId, [])
79+
buffers.set(taskId, [])
8280
if (type === 'stderr')
8381
timer.stderrTime = 0
8482
else
@@ -93,11 +91,12 @@ export function createCustomConsole(defaultState?: WorkerGlobalState) {
9391
origin?: string,
9492
) {
9593
const timer = timers.get(taskId)!
94+
const time = type === 'stderr' ? timer.stderrTime : timer.stdoutTime
9695
state().rpc.onUserConsoleLog({
9796
type,
9897
content: content || '<empty line>',
9998
taskId,
100-
time: timer.stderrTime || RealDate.now(),
99+
time: time || RealDate.now(),
101100
size,
102101
origin,
103102
})

0 commit comments

Comments
 (0)