Use Buffer.allocUnsafe() in enviroments that support it

This commit is contained in:
RyanZim 2017-03-16 15:33:03 -04:00
parent 84717b8d03
commit a86d8b4ba1
3 changed files with 13 additions and 2 deletions

View File

@ -3,7 +3,7 @@
const fs = require('graceful-fs')
const BUF_LENGTH = 64 * 1024
const _buff = new Buffer(BUF_LENGTH)
const _buff = require('../util/buffer')(BUF_LENGTH)
function copyFileSync (srcFile, destFile, options) {
const overwrite = options.overwrite

View File

@ -5,6 +5,7 @@ const path = require('path')
const copySync = require('../copy-sync').copySync
const removeSync = require('../remove').removeSync
const mkdirpSync = require('../mkdirs').mkdirsSync
const buffer = require('../util/buffer')
function moveSync (src, dest, options) {
options = options || {}
@ -60,7 +61,7 @@ function moveSyncAcrossDevice (src, dest, overwrite) {
function moveFileSyncAcrossDevice (src, dest, overwrite) {
const BUF_LENGTH = 64 * 1024
const _buff = new Buffer(BUF_LENGTH)
const _buff = buffer(BUF_LENGTH)
const flags = overwrite ? 'w' : 'wx'

10
lib/util/buffer.js Normal file
View File

@ -0,0 +1,10 @@
module.exports = function (size) {
if (typeof Buffer.allocUnsafe === 'function') {
try {
return Buffer.allocUnsafe(size)
} catch (e) {
return new Buffer(size)
}
}
return new Buffer(size)
}