Skip to content

Commit

Permalink
fix: handle large throughput writes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 3, 2024
1 parent 38306e1 commit 5e336ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/ux/write.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import {format} from 'node:util'

export const stdout = (str?: string | string[] | undefined, ...args: string[]): void => {
if (!str && args) {
process.stdout.write(format(...args) + '\n')
console.log(format(...args))
} else if (!str) {
process.stdout.write('\n')
console.log()
} else if (typeof str === 'string') {
process.stdout.write((str && format(str, ...args)) + '\n')
console.log(format(str, ...args))
} else {
process.stdout.write(format(...str, ...args) + '\n')
console.log(format(...str, ...args))
}
}

export const stderr = (str?: string | string[] | undefined, ...args: string[]): void => {
if (!str && args) {
process.stderr.write(format(...args) + '\n')
console.error(format(...args))
} else if (!str) {
process.stderr.write('\n')
console.error()
} else if (typeof str === 'string') {
process.stderr.write((str && format(str, ...args)) + '\n')
console.error(format(str, ...args))
} else {
process.stderr.write(format(...str, ...args) + '\n')
console.error(format(...str, ...args))
}
}
32 changes: 32 additions & 0 deletions test/ux/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ describe('write', () => {
const {stdout} = await captureOutput(async () => writeStdout())
expect(stdout).to.equal('\n')
})

it('should not lose data', async () => {
const lines = Array.from(
{length: 100_000},
(_, i) =>
`Line ${i} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer convallis fringilla sollicitudin. Nunc scelerisque neque non ipsum accumsan commodo. In et porttitor eros, ut vestibulum magna. Morbi felis diam, pharetra eu dui non, sollicitudin feugiat nisi. Aliquam cursus malesuada risus, vel luctus leo ornare sed. Morbi condimentum odio id ex facilisis bibendum. Nullam consectetur consectetur viverra. Donec nec ante dui. Integer lacinia facilisis urna vitae feugiat.`,
)

const {stdout} = await captureOutput(async () => {
for (const line of lines) {
writeStdout(line)
}
})

expect(stdout).to.equal(lines.join('\n') + '\n')
})
})

describe('stderr', () => {
Expand Down Expand Up @@ -66,5 +82,21 @@ describe('write', () => {
const {stderr} = await captureOutput(async () => writeStderr())
expect(stderr).to.equal('\n')
})

it('should not lose data', async () => {
const lines = Array.from(
{length: 100_000},
(_, i) =>
`Line ${i} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer convallis fringilla sollicitudin. Nunc scelerisque neque non ipsum accumsan commodo. In et porttitor eros, ut vestibulum magna. Morbi felis diam, pharetra eu dui non, sollicitudin feugiat nisi. Aliquam cursus malesuada risus, vel luctus leo ornare sed. Morbi condimentum odio id ex facilisis bibendum. Nullam consectetur consectetur viverra. Donec nec ante dui. Integer lacinia facilisis urna vitae feugiat.`,
)

const {stderr} = await captureOutput(async () => {
for (const line of lines) {
writeStderr(line)
}
})

expect(stderr).to.equal(lines.join('\n') + '\n')
})
})
})

0 comments on commit 5e336ce

Please sign in to comment.