You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP

fsu

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fsu - npm Package Compare versions

Comparing version

to
1.0.0

@@ -1,126 +0,128 @@

"use strict";
var fs = require("fs");
var path = require("path");
var inherits = require("util").inherits;
var WriteStream = fs.WriteStream;
'use strict';
const fs = require('fs');
const path = require('path');
const inherits = require('util').inherits;
const WriteStream = fs.WriteStream;
var rxFileParts = /(.*)\{([^#\{\}]*)(#+)([^#\{\}]*)\}(.*)/;
let rxFileParts = /(.*)\{([^#\{\}]*)(#+)([^#\{\}]*)\}(.*)/;
var defaultDirMode = parseInt("0777", 8) & (~process.umask());
var defaultFileMode = parseInt("0666", 8) & (~process.umask());
const defaultDirMode = parseInt('0777', 8) & (~process.umask());
const defaultFileMode = parseInt('0666', 8) & (~process.umask());
var padNum = function(n, width, z) {
z = z || "0";
n = n + "";
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
let padNum = function(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
};
var writeAll = function(fd, buffer, offset, length, position, cb) {
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
if (writeErr) {
fs.close(fd, function() {
cb(writeErr);
});
} else {
if (written === length) {
fs.close(fd, cb);
} else {
offset += written;
length -= written;
position += written;
writeAll(fd, buffer, offset, length, position, cb);
}
}
});
let writeAll = function(fd, buffer, offset, length, position, cb) {
fs.write(fd, buffer, offset, length, position, (writeErr, written) => {
if (writeErr) {
fs.close(fd, () => cb(writeErr));
} else if (written === length) {
fs.close(fd, cb);
} else {
offset += written;
length -= written;
position += written;
writeAll(fd, buffer, offset, length, position, cb);
}
});
};
var mkdirp = function(p, mode, cb) {
fs.mkdir(p, mode, function (err) {
if(!err) {
cb();
} else if(err.code === "ENOENT") {
mkdirp(path.dirname(p), mode, function(er) {
if(er) {
cb(er);
} else {
mkdirp(p, mode, cb);
}
});
} else {
cb(err);
}
});
let mkdirp = function(p, mode, cb) {
fs.mkdir(p, mode, err => {
if (!err) {
cb();
} else if (err.code === 'ENOENT') {
mkdirp(path.dirname(p), mode, err => {
if (err) {
cb(err);
} else {
mkdirp(p, mode, cb);
}
});
} else if (err.code === 'EEXIST') {
cb();
} else {
cb(err);
}
});
};
var openUniqueHandler = function(tryNum, fileParts, options, cb) {
var file = options.simple ? fileParts.tail : tryNum ? (fileParts.head + fileParts.padLeft + padNum(tryNum, fileParts.pad) + fileParts.padRight + fileParts.tail) : (fileParts.head + fileParts.tail);
var newPath = path.join(fileParts.path, file);
let openUniqueHandler = function(tryNum, fileParts, options, cb) {
let file = options.simple ? fileParts.tail : tryNum ? (fileParts.head + fileParts.padLeft + padNum(tryNum, fileParts.pad) + fileParts.padRight + fileParts.tail) : (fileParts.head + fileParts.tail);
let newPath = path.join(fileParts.path, file);
fs.open(newPath, options.flags || "w", options.mode || defaultFileMode, function(err, fd) {
if(err && err.code === "EEXIST" && !options.simple) {
openUniqueHandler(++tryNum, fileParts, options, cb);
} else if(err && err.code === "ENOENT" && options.force) {
mkdirp(fileParts.path, defaultDirMode, function(er) {
if(er) {
cb(er);
} else {
openUniqueHandler(tryNum, fileParts, options, cb);
}
});
} else {
cb(err, fd, newPath);
}
});
fs.open(newPath, options.flags || 'w', options.mode || defaultFileMode, (err, fd) => {
if (err && err.code === 'EEXIST' && !options.simple) {
openUniqueHandler(++tryNum, fileParts, options, cb);
} else if (err && err.code === 'ENOENT' && options.force) {
mkdirp(fileParts.path, defaultDirMode, ere => {
if (ere) {
cb(ere);
} else {
openUniqueHandler(tryNum, fileParts, options, cb);
}
});
} else {
cb(err, fd, newPath);
}
});
};
var openUnique = function(file, options, cb) {
file = path.resolve(file);
var filePath = path.dirname(file),
fileName = path.basename(file);
let openUnique = function(file, options, cb) {
file = path.resolve(file);
let filePath = path.dirname(file);
let fileName = path.basename(file);
var fileParts = rxFileParts.exec(fileName);
let fileParts = rxFileParts.exec(fileName);
if(!fileParts) {
options.simple = true;
openUniqueHandler(0, {
path: filePath,
tail: fileName
}, options, cb);
} else {
options.simple = false;
options.flags = "wx";
openUniqueHandler(0, {
path: filePath,
head: fileParts[1] || "",
padLeft: fileParts[2],
pad: fileParts[3].length,
padRight: fileParts[4],
tail: fileParts[5] || ""
}, options, cb);
}
if (!fileParts) {
options.simple = true;
openUniqueHandler(0, {
path: filePath,
tail: fileName
}, options, cb);
} else {
options.simple = false;
options.flags = 'wx';
openUniqueHandler(0, {
path: filePath,
head: fileParts[1] || '',
padLeft: fileParts[2],
pad: fileParts[3].length,
padRight: fileParts[4],
tail: fileParts[5] || ''
}, options, cb);
}
};
var writeFileUnique = function(filename, data, options, cb) {
if(cb === undefined) {
cb = options;
options = { encoding: "utf8", mode: defaultFileMode, flags: "w" };
}
let writeFileUnique = function(filename, data, options, cb) {
if (cb === undefined) {
cb = options;
options = {
encoding: 'utf8',
mode: defaultFileMode,
flags: 'w'
};
}
openUnique(filename, options, function(err, fd) {
if(err) {
cb(err);
} else {
var buffer = Buffer.isBuffer(data) ? data : new Buffer("" + data, options.encoding || "utf8");
writeAll(fd, buffer, 0, buffer.length, 0, cb);
}
});
openUnique(filename, options, (err, fd) => {
if (err) {
cb(err);
} else {
let buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, options.encoding || 'utf8');
writeAll(fd, buffer, 0, buffer.length, 0, cb);
}
});
};
// stream
var WriteStreamUnique = function(file, options) {
if(options && options.force) {
this.force = options.force;
delete options.force;
}
WriteStream.call(this, file, options);
let WriteStreamUnique = function(file, options) {
if (options && options.force) {
this.force = options.force;
delete options.force;
}
WriteStream.call(this, file, options);
};

@@ -130,28 +132,26 @@ inherits(WriteStreamUnique, WriteStream);

WriteStreamUnique.prototype.open = function() {
var self = this;
openUnique(this.path, {
flags: this.flags,
mode: this.mode,
force: this.force
}, function(err, fd, newPath) {
if (err) {
self.destroy();
self.emit("error", err);
return;
}
self.path = newPath;
self.fd = fd;
self.emit("open", fd);
});
openUnique(this.path, {
flags: this.flags,
mode: this.mode,
force: this.force
}, (err, fd, newPath) => {
if (err) {
this.destroy();
this.emit('error', err);
return;
}
this.path = newPath;
this.fd = fd;
this.emit('open', fd);
});
};
var createWriteStreamUnique = function(file, options) {
return new WriteStreamUnique(file, options);
let createWriteStreamUnique = function(file, options) {
return new WriteStreamUnique(file, options);
};
module.exports = {
openUnique: openUnique,
writeFileUnique: writeFileUnique,
createWriteStreamUnique: createWriteStreamUnique
openUnique: openUnique,
writeFileUnique: writeFileUnique,
createWriteStreamUnique: createWriteStreamUnique
};
{
"name": "fsu",
"version": "0.2.2",
"author": {
"name": "Alexey Novikov",
"url": "http://2dubs.com"
},
"bugs": {
"url": "https://github.com/velocityzen/fsu/issues"
},
"description": "Unique file name with streams support",
"license": "MIT",
"homepage": "https://github.com/velocityzen/fsu#readme",
"keywords": [
"counter",
"file",
"filename",
"fs",
"unique",
"counter",
"filename",
"stream"
"stream",
"unique"
],
"author": "Alexey Novikov (http://2dubs.com)",
"license": "MIT",
"main": "./index",
"name": "fsu",
"private": false,
"engines": {
"node": ">0.10.0",
"iojs": ">1.0.0"
},
"repository": {
"type": "git",
"url": "git@github.com:velocityzen/fsu.git"
"url": "git+ssh://git@github.com/velocityzen/fsu.git"
},
"main": "./index"
"version": "1.0.0"
}

@@ -15,3 +15,3 @@ # fsu (fs unique)

fsu.openUnique("text{_###}.txt", function(err, fd) {
fsu.openUnique("text{_###}.txt", (err, fd) => {
//now we can use file descriptor as usual

@@ -27,3 +27,3 @@ });

fsu.writeFileUnique("text{_###}.txt", "test", function(err) {
fsu.writeFileUnique("text{_###}.txt", "test", err => {
console.log("Done");

@@ -30,0 +30,0 @@ });

@@ -1,13 +0,13 @@

"use strict";
var fs = require("fs");
var fsu = require("./index.js");
'use strict';
const fs = require('fs');
const fsu = require('./index.js');
var stream = fsu.createWriteStreamUnique("text{_stream###}.txt");
let stream = fsu.createWriteStreamUnique('test{_stream###}.txt');
fsu.writeFileUnique("css/text{_file###}.txt", "test", {force: true}, function(err) {
if(err) {
console.log(err);
} else {
fs.createReadStream("readme.md").pipe(stream);
}
fsu.writeFileUnique('test/test{_file###}.txt', 'test', { force: true }, err => {
if (err) {
console.log(err);
} else {
fs.createReadStream('readme.md').pipe(stream);
}
});