Skip to content

Commit

Permalink
Use an option object instead of positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Mar 16, 2020
1 parent a7d2cc5 commit 39726c0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ npm i sonic-boom
'use strict'

const SonicBoom = require('sonic-boom')
const sonic = new SonicBoom(process.stdout.fd) // or '/path/to/destination'
const sonic = new SonicBoom({ fd: process.stdout.fd }) // or { dest: '/path/to/destination' }

for (var i = 0; i < 10; i++) {
sonic.write('hello sonic\n')
Expand All @@ -43,22 +43,20 @@ for (var i = 0; i < 10; i++) {

## API

### SonicBoom(String|Number, [minLength], [sync])
### SonicBoom(opts) String|Number, [minLength], [sync])

Creates a new instance of SonicBoom.

The first argument can be:
The options are:

1. a string that is a path to a file to be written to (mode `'a'`)
2. a file descriptor, something that is returned by `fs.open` or
* `fd`: a file descriptor, something that is returned by `fs.open` or
`fs.openSync`.
* `dest`: a string that is a path to a file to be written to (mode `'a'`).
* `minLength`: the minimum lenght of the internal buffer that is
required to be full before flushing.
* `sync`: perform writes synchronously (similar to `console.log`).

The second argument is the minimum length of the internal buffer that is
required before flushing.

The third argument is a flag that, when true, causes `SonicBoom` to perform synchronous writes.

It will emit the `'ready'` event when a file descriptor is available.
A `SonicBoom` instance will emit the `'ready'` event when a file descriptor is available.

### SonicBoom#write(string)

Expand Down
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const SonicBoom = require('.')
const sonic = new SonicBoom(process.stdout.fd) // or 'destination'
const sonic = new SonicBoom({ fd: process.stdout.fd }) // or 'destination'

for (var i = 0; i < 10; i++) {
sonic.write('hello sonic\n')
Expand Down
2 changes: 1 addition & 1 deletion fixtures/firehose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const SonicBoom = require('..')

const out = new SonicBoom(process.stdout.fd)
const out = new SonicBoom({ fd: process.stdout.fd })
const str = Buffer.alloc(1000).fill('a').toString()

let i = 0
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ function openFile (file, sonic) {
})
}

function SonicBoom (fd, minLength, sync) {
function SonicBoom (opts) {
if (!(this instanceof SonicBoom)) {
return new SonicBoom(fd, minLength, sync)
return new SonicBoom(opts)
}

var { fd, dest, minLength, sync } = opts || {}

fd = fd || dest

this._buf = ''
this.fd = -1
this._writing = false
Expand Down
48 changes: 24 additions & 24 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function buildTests (test, sync) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, sync)
const stream = new SonicBoom({ fd, sync })

stream.on('ready', () => {
t.pass('ready emitted')
Expand All @@ -71,7 +71,7 @@ function buildTests (test, sync) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, sync)
const stream = new SonicBoom({ fd, sync })

t.ok(stream.write('hello world\n'))

Expand Down Expand Up @@ -104,7 +104,7 @@ function buildTests (test, sync) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, sync)
const stream = new SonicBoom({ fd, sync })
const source = fs.createReadStream(__filename)

source.pipe(stream)
Expand All @@ -127,7 +127,7 @@ function buildTests (test, sync) {
t.plan(6)

const dest = file()
const stream = new SonicBoom(dest, 0, sync)
const stream = new SonicBoom({ dest, sync })

stream.on('ready', () => {
t.pass('ready emitted')
Expand All @@ -154,7 +154,7 @@ function buildTests (test, sync) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 4096, sync)
const stream = new SonicBoom({ fd, minLength: 4096, sync })

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand All @@ -178,7 +178,7 @@ function buildTests (test, sync) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, sync)
const stream = new SonicBoom({ fd, sync })

t.ok(stream.write('hello world\n'))
stream.destroy()
Expand All @@ -202,7 +202,7 @@ function buildTests (test, sync) {
t.plan(1)

const dest = file()
const stream = new SonicBoom(dest)
const stream = new SonicBoom({ dest })

stream.destroy()
stream.on('close', () => {
Expand All @@ -214,7 +214,7 @@ function buildTests (test, sync) {
t.plan(8)

const dest = file()
const stream = new SonicBoom(dest, 4096, sync)
const stream = new SonicBoom({ dest, minLength: 4096, sync })

stream.on('ready', () => {
t.pass('ready emitted')
Expand Down Expand Up @@ -254,7 +254,7 @@ function buildTests (test, sync) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 4096, sync)
const stream = new SonicBoom({ fd, minLength: 4096, sync })

stream.on('ready', () => {
t.pass('ready emitted')
Expand All @@ -278,7 +278,7 @@ function buildTests (test, sync) {
t.plan(9)

const dest = file()
const stream = new SonicBoom(dest, 0, sync)
const stream = new SonicBoom({ dest, sync })

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -314,7 +314,7 @@ function buildTests (test, sync) {
t.plan(9)

const dest = file()
const stream = new SonicBoom(dest, 4096, sync)
const stream = new SonicBoom({ dest, minLength: 4096, sync })

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -352,7 +352,7 @@ function buildTests (test, sync) {
t.plan(3)

const dest = file()
const stream = new SonicBoom(dest, 0, sync)
const stream = new SonicBoom({ dest, sync })

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand All @@ -369,7 +369,7 @@ function buildTests (test, sync) {
t.plan(4)

const dest = file()
const stream = new SonicBoom(dest, 4096, sync)
const stream = new SonicBoom({ dest, minLength: 4096, sync })

stream.once('ready', () => {
t.pass('ready emitted')
Expand All @@ -391,7 +391,7 @@ function buildTests (test, sync) {
t.plan(4)

const dest = file()
const stream = new SonicBoom(dest, 4096, sync)
const stream = new SonicBoom({ dest, minLength: 4096, sync })

stream.once('ready', () => {
t.pass('ready emitted')
Expand All @@ -414,7 +414,7 @@ function buildTests (test, sync) {
t.plan(3)

const dest = file()
const stream = new SonicBoom(dest, 4096, sync)
const stream = new SonicBoom({ dest, minLength: 4096, sync })
const after = dest + '-moved'
stream.reopen(after)
stream.write('after reopen\n')
Expand All @@ -432,7 +432,7 @@ function buildTests (test, sync) {
t.plan(9)

const dest = file()
const stream = new SonicBoom(dest, 0, sync)
const stream = new SonicBoom({ dest, minLength: 0, sync })

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -502,7 +502,7 @@ test('retry on EAGAIN', (t) => {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, false)
const stream = new SonicBoom({ fd, sync: false, minLength: 0 })

stream.on('ready', () => {
t.pass('ready emitted')
Expand Down Expand Up @@ -541,7 +541,7 @@ test('retry on EAGAIN (sync)', (t) => {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, true)
const stream = new SonicBoom({ fd, minLength: 0, sync: true })

stream.on('ready', () => {
t.pass('ready emitted')
Expand Down Expand Up @@ -581,7 +581,7 @@ test('write buffers that are not totally written', (t) => {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, false)
const stream = new SonicBoom({ fd, minLength: 0, sync: false })

stream.on('ready', () => {
t.pass('ready emitted')
Expand Down Expand Up @@ -621,7 +621,7 @@ test('write buffers that are not totally written with sync mode', (t) => {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, true)
const stream = new SonicBoom({ fd, minLength: 0, sync: true })

stream.on('ready', () => {
t.pass('ready emitted')
Expand Down Expand Up @@ -657,7 +657,7 @@ test('sync writing is fully sync', (t) => {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, true)
const stream = new SonicBoom({ fd, minLength: 0, sync: true })
t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))

Expand All @@ -679,7 +679,7 @@ if (process.versions.node.indexOf('6.') !== 0) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, false)
const stream = new SonicBoom({ fd, minLength: 0, sync: false })

const buf = Buffer.alloc(1024).fill('x').toString() // 1 MB
let length = 0
Expand Down Expand Up @@ -707,7 +707,7 @@ if (process.versions.node.indexOf('6.') !== 0) {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, true)
const stream = new SonicBoom({ fd, minLength: 0, sync: true })

const buf = Buffer.alloc(1024).fill('x').toString() // 1 MB
let length = 0
Expand Down Expand Up @@ -736,7 +736,7 @@ test('write enormously large buffers sync with utf8 multi-byte split', (t) => {

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom(fd, 0, true)
const stream = new SonicBoom({ fd, minLength: 0, sync: true })

let buf = Buffer.alloc((1024 * 16) - 2).fill('x') // 16MB - 3B
const length = buf.length + 4
Expand Down

0 comments on commit 39726c0

Please sign in to comment.