Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cp-file

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cp-file - npm Package Compare versions

Comparing version 2.0.2 to 2.1.0

76

index.js

@@ -6,2 +6,3 @@ 'use strict';

var objectAssign = require('object-assign');
var onetime = require('onetime');

@@ -18,17 +19,13 @@ module.exports = function (src, dest, opts, cb) {

cb = cb || function () {};
cb = onetime(cb || function () {});
opts = objectAssign({overwrite: true}, opts);
var cbCalled = false;
var read = fs.createReadStream(src);
var readListener = onetime(startWrite);
function done(err) {
if (cbCalled) {
return;
}
read.on('error', cb);
read.on('readable', readListener);
read.on('end', readListener);
cbCalled = true;
cb(err);
}
function copy() {
function startWrite() {
mkdirp(path.dirname(dest), function (err) {

@@ -40,15 +37,21 @@ if (err && err.code !== 'EEXIST') {

var read = fs.createReadStream(src);
var write = fs.createWriteStream(dest);
var write = fs.createWriteStream(dest, {flags: opts.overwrite ? 'w' : 'wx'});
read.on('error', done);
write.on('error', done);
write.on('error', function (err) {
if (!opts.overwrite && err.code === 'EEXIST') {
cb();
return;
}
cb(err);
});
write.on('close', function () {
fs.lstat(src, function (err, stats) {
if (err) {
done(err);
cb(err);
return;
}
fs.utimes(dest, stats.atime, stats.mtime, done);
fs.utimes(dest, stats.atime, stats.mtime, cb);
});

@@ -60,15 +63,2 @@ });

}
if (opts.overwrite) {
copy();
} else {
fs.exists(dest, function (exists) {
if (exists) {
cb();
return;
}
copy();
});
}
};

@@ -83,5 +73,8 @@

if (!opts.overwrite && fs.existsSync(dest)) {
return;
}
var read = fs.openSync(src, 'r');
var BUF_LENGTH = 100 * 1024;
var buf = new Buffer(BUF_LENGTH);
var bytesRead = fs.readSync(read, buf, 0, BUF_LENGTH, 0);
var pos = bytesRead;
var write;

@@ -96,10 +89,12 @@ try {

var BUF_LENGTH = 100 * 1024;
var buf = new Buffer(BUF_LENGTH);
var read = fs.openSync(src, 'r');
var write = fs.openSync(dest, 'w');
var stat = fs.fstatSync(read);
var bytesRead = BUF_LENGTH;
var pos = 0;
try {
write = fs.openSync(dest, opts.overwrite ? 'w' : 'wx');
} catch (err) {
if (!opts.overwrite && err.code === 'EEXIST') {
return;
}
}
fs.writeSync(write, buf, 0, bytesRead);
while (bytesRead === BUF_LENGTH) {

@@ -111,2 +106,3 @@ bytesRead = fs.readSync(read, buf, 0, BUF_LENGTH, pos);

var stat = fs.fstatSync(read);
fs.futimesSync(write, stat.atime, stat.mtime);

@@ -113,0 +109,0 @@ fs.closeSync(read);

{
"name": "cp-file",
"version": "2.0.2",
"version": "2.1.0",
"description": "Copy a file",

@@ -32,7 +32,9 @@ "license": "MIT",

"mkdirp": "^0.5.0",
"object-assign": "^2.0.0"
"object-assign": "^2.0.0",
"onetime": "^1.0.0"
},
"devDependencies": {
"mocha": "*"
"mocha": "*",
"rimraf": "^2.3.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc