Skip to content

Commit

Permalink
Add common.buffer (#710)
Browse files Browse the repository at this point in the history
* Add common.buffer

* Fix comment
  • Loading branch information
freitagbr committed May 3, 2017
1 parent 92d3f39 commit 951ff22
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/common.js
Expand Up @@ -38,6 +38,7 @@ var DEFAULT_CONFIG = {
silent: false,
verbose: false,
execPath: null,
bufLength: 64 * 1024, // 64KB
};

var config = {
Expand Down Expand Up @@ -273,6 +274,17 @@ function expand(list) {
}
exports.expand = expand;

// Normalizes Buffer creation, using Buffer.alloc if possible.
// Also provides a good default buffer length for most use cases.
var buffer = typeof Buffer.alloc === 'function' ?
function (len) {
return Buffer.alloc(len || config.bufLength);
} :
function (len) {
return new Buffer(len || config.bufLength);
};
exports.buffer = buffer;

// Normalizes _unlinkSync() across platforms to match Unix behavior, i.e.
// file can be unlinked even if it's read-only, see https://github.com/joyent/node/issues/3006
function unlinkSync(file) {
Expand Down
10 changes: 5 additions & 5 deletions src/cp.js
Expand Up @@ -45,9 +45,9 @@ function copyFileSync(srcFile, destFile, options) {
var symlinkFull = fs.readlinkSync(srcFile);
fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null);
} else {
var BUF_LENGTH = 64 * 1024;
var buf = new Buffer(BUF_LENGTH);
var bytesRead = BUF_LENGTH;
var buf = common.buffer();
var bufLength = buf.length;
var bytesRead = bufLength;
var pos = 0;
var fdr = null;
var fdw = null;
Expand All @@ -66,8 +66,8 @@ function copyFileSync(srcFile, destFile, options) {
common.error('copyFileSync: could not write to dest file (code=' + e.code + '):' + destFile);
}

while (bytesRead === BUF_LENGTH) {
bytesRead = fs.readSync(fdr, buf, 0, BUF_LENGTH, pos);
while (bytesRead === bufLength) {
bytesRead = fs.readSync(fdr, buf, 0, bufLength, pos);
fs.writeSync(fdw, buf, 0, bytesRead);
pos += bytesRead;
}
Expand Down
10 changes: 5 additions & 5 deletions src/head.js
Expand Up @@ -10,9 +10,9 @@ common.register('head', _head, {

// This reads n or more lines, or the entire file, whichever is less.
function readSomeLines(file, numLines) {
var BUF_LENGTH = 64 * 1024;
var buf = new Buffer(BUF_LENGTH);
var bytesRead = BUF_LENGTH;
var buf = common.buffer();
var bufLength = buf.length;
var bytesRead = bufLength;
var pos = 0;
var fdr = null;

Expand All @@ -24,8 +24,8 @@ function readSomeLines(file, numLines) {

var numLinesRead = 0;
var ret = '';
while (bytesRead === BUF_LENGTH && numLinesRead < numLines) {
bytesRead = fs.readSync(fdr, buf, 0, BUF_LENGTH, pos);
while (bytesRead === bufLength && numLinesRead < numLines) {
bytesRead = fs.readSync(fdr, buf, 0, bufLength, pos);
var bufStr = buf.toString('utf8', 0, bytesRead);
numLinesRead += bufStr.split('\n').length - 1;
ret += bufStr;
Expand Down
26 changes: 26 additions & 0 deletions test/common.js
Expand Up @@ -188,6 +188,32 @@ test('non-string', t => {
t.deepEqual(result, [5]);
});

//
// common.buffer()
//
test('common.buffer returns buffer', t => {
const buf = common.buffer();
t.falsy(shell.error());
t.truthy(buf instanceof Buffer);
t.is(buf.length, 64 * 1024);
});

test('common.buffer with explicit length', t => {
const buf = common.buffer(20);
t.falsy(shell.error());
t.truthy(buf instanceof Buffer);
t.is(buf.length, 20);
});

test('common.buffer with different config.bufLength', t => {
common.config.bufLength = 20;
const buf = common.buffer();
t.falsy(shell.error());
t.truthy(buf instanceof Buffer);
t.is(buf.length, 20);
common.config.reset();
});

test('common.parseOptions (normal case)', t => {
const result = common.parseOptions('-Rf', {
R: 'recursive',
Expand Down

0 comments on commit 951ff22

Please sign in to comment.