sonic-boom
Advanced tools
Comparing version 2.5.0 to 2.6.0
13
index.js
@@ -65,7 +65,9 @@ 'use strict' | ||
const mode = sonic.append ? 'a' : 'w' | ||
const flags = sonic.append ? 'a' : 'w' | ||
const mode = sonic.mode | ||
if (sonic.sync) { | ||
try { | ||
if (sonic.mkdir) fs.mkdirSync(path.dirname(file), { recursive: true }) | ||
const fd = fs.openSync(file, mode) | ||
const fd = fs.openSync(file, flags, mode) | ||
fileOpened(null, fd) | ||
@@ -79,6 +81,6 @@ } catch (err) { | ||
if (err) return fileOpened(err) | ||
fs.open(file, mode, fileOpened) | ||
fs.open(file, flags, mode, fileOpened) | ||
}) | ||
} else { | ||
fs.open(file, mode, fileOpened) | ||
fs.open(file, flags, mode, fileOpened) | ||
} | ||
@@ -92,3 +94,3 @@ } | ||
let { fd, dest, minLength, maxLength, sync, append = true, mkdir, retryEAGAIN } = opts || {} | ||
let { fd, dest, minLength, maxLength, sync, append = true, mode, mkdir, retryEAGAIN } = opts || {} | ||
@@ -112,2 +114,3 @@ fd = fd || dest | ||
this.append = append || false | ||
this.mode = mode | ||
this.retryEAGAIN = retryEAGAIN || (() => true) | ||
@@ -114,0 +117,0 @@ this.mkdir = mkdir || false |
{ | ||
"name": "sonic-boom", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Extremely fast utf8 only stream implementation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -67,2 +67,3 @@ # sonic-boom | ||
* `append`: appends writes to dest file instead of truncating it (default `true`). | ||
* `mode`: specify the creating file `mode` (see [fs.open()](https://nodejs.org/api/fs.html#fsopenpath-flags-mode-callback) from Node.js core). | ||
* `mkdir`: ensure directory for dest file exists when `true` (default `false`). | ||
@@ -69,0 +70,0 @@ * `retryEAGAIN(err, writeBufferLen, remainingBufferLen)`: a function that will be called when sonic-boom |
108
test.js
@@ -12,2 +12,4 @@ 'use strict' | ||
const isWindows = process.platform === 'win32' | ||
const MAX_WRITE = 64 * 1024 | ||
@@ -47,2 +49,5 @@ const files = [] | ||
function buildTests (test, sync) { | ||
// Reset the umask for testing | ||
process.umask(0o000) | ||
test('write things to a file descriptor', (t) => { | ||
@@ -643,3 +648,3 @@ t.plan(6) | ||
} else { | ||
fakeFs.open = function (file, flags, cb) { | ||
fakeFs.open = function (file, flags, mode, cb) { | ||
t.pass('fake fs.open called') | ||
@@ -676,2 +681,103 @@ setTimeout(() => cb(new Error('open error')), 0) | ||
}) | ||
test('mode', { skip: isWindows }, (t) => { | ||
t.plan(6) | ||
const dest = file() | ||
const mode = 0o666 | ||
const stream = new SonicBoom({ dest, sync, mode }) | ||
stream.on('ready', () => { | ||
t.pass('ready emitted') | ||
}) | ||
t.ok(stream.write('hello world\n')) | ||
t.ok(stream.write('something else\n')) | ||
stream.end() | ||
stream.on('finish', () => { | ||
fs.readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'hello world\nsomething else\n') | ||
t.equal(fs.statSync(dest).mode & 0o777, stream.mode) | ||
}) | ||
}) | ||
}) | ||
test('mode default', { skip: isWindows }, (t) => { | ||
t.plan(6) | ||
const dest = file() | ||
const defaultMode = 0o666 | ||
const stream = new SonicBoom({ dest, sync }) | ||
stream.on('ready', () => { | ||
t.pass('ready emitted') | ||
}) | ||
t.ok(stream.write('hello world\n')) | ||
t.ok(stream.write('something else\n')) | ||
stream.end() | ||
stream.on('finish', () => { | ||
fs.readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'hello world\nsomething else\n') | ||
t.equal(fs.statSync(dest).mode & 0o777, defaultMode) | ||
}) | ||
}) | ||
}) | ||
test('mode on mkdir', { skip: isWindows }, (t) => { | ||
t.plan(5) | ||
const dest = path.join(file(), 'out.log') | ||
const mode = 0o666 | ||
const stream = new SonicBoom({ dest, mkdir: true, mode, sync }) | ||
stream.on('ready', () => { | ||
t.pass('ready emitted') | ||
}) | ||
t.ok(stream.write('hello world\n')) | ||
stream.flush() | ||
stream.on('drain', () => { | ||
fs.readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'hello world\n') | ||
t.equal(fs.statSync(dest).mode & 0o777, stream.mode) | ||
stream.end() | ||
}) | ||
}) | ||
}) | ||
test('mode on append', { skip: isWindows }, (t) => { | ||
t.plan(5) | ||
const dest = file() | ||
fs.writeFileSync(dest, 'hello world\n', 'utf8', 0o422) | ||
const mode = isWindows ? 0o444 : 0o666 | ||
const stream = new SonicBoom({ dest, append: false, mode, sync }) | ||
stream.on('ready', () => { | ||
t.pass('ready emitted') | ||
}) | ||
t.ok(stream.write('something else\n')) | ||
stream.flush() | ||
stream.on('drain', () => { | ||
fs.readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'something else\n') | ||
t.equal(fs.statSync(dest).mode & 0o777, stream.mode) | ||
stream.end() | ||
}) | ||
}) | ||
}) | ||
} | ||
@@ -678,0 +784,0 @@ |
@@ -16,2 +16,3 @@ // Type definitions for sonic-boom 0.7 | ||
append?: boolean | ||
mode?: string | number | ||
mkdir?: boolean | ||
@@ -18,0 +19,0 @@ retryEAGAIN?: (err: Error, writeBufferLen: number, remainingBufferLen: number) => boolean |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55662
1757
118