Comparing version 0.1.1 to 0.2.0
134
index.js
"use strict"; | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var inherits = require("util").inherits; | ||
var WriteStream = fs.WriteStream; | ||
var rx = /(.+)\{([^#\{\}]*)(#+)([^#\{\}]*)\}(.+)/; | ||
var rx = /(.*)\{([^#\{\}]*)(#+)([^#\{\}]*)\}(.*)/; | ||
var defaultDirMode = parseInt("0777", 8) & (~process.umask()); | ||
var 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; | ||
z = z || "0"; | ||
n = n + ""; | ||
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | ||
}; | ||
@@ -33,8 +37,34 @@ | ||
var openUniqueHandler = function(tryNum, head, padLeft, pad, padRight, tail, mode, cb) { | ||
var file = tryNum ? (head + padLeft + padNum(tryNum, pad) + padRight + tail) : (head + tail); | ||
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); | ||
} | ||
}); | ||
}; | ||
fs.open(file, "wx", mode || 438, function(err, fd) { | ||
if(err && err.errno === 47) { | ||
openUniqueHandler(++tryNum, head, padLeft, pad, padRight, tail, mode, cb); | ||
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); | ||
fs.open(path.join(fileParts.path, file), 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 { | ||
@@ -46,14 +76,27 @@ cb(err, fd); | ||
var openUnique = function(filename, mode, cb) { | ||
if(cb === undefined) { | ||
cb = mode; | ||
mode = 438; | ||
} | ||
var openUnique = function(file, options, cb) { | ||
file = path.resolve(file); | ||
var filePath = path.dirname(file), | ||
fileName = path.basename(file); | ||
filename = rx.exec(filename); | ||
if(!filename) { | ||
cb(new Error("Can't find a counter pattern in filename")); | ||
var fileParts = rx.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); | ||
} | ||
openUniqueHandler(0, filename[1], filename[2], filename[3].length, filename[4], filename[5], mode, cb); | ||
}; | ||
@@ -64,8 +107,12 @@ | ||
cb = options; | ||
options = { encoding: 'utf8', mode: 438 /*=0666*/ }; | ||
options = { encoding: "utf8", mode: defaultFileMode, flags: "w" }; | ||
} | ||
openUnique(filename, options.mode, function(err, fd) { | ||
var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, options.encoding || 'utf8'); | ||
writeAll(fd, buffer, 0, buffer.length, 0, cb); | ||
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); | ||
} | ||
}); | ||
@@ -75,22 +122,32 @@ }; | ||
// stream | ||
var UniqueWriteStream = function(path, options) { | ||
WriteStream.call(this, path, options); | ||
var WriteStreamUnique = function(file, options) { | ||
if(options && options.force) { | ||
this.force = options.force; | ||
delete options.force; | ||
} | ||
WriteStream.call(this, file, options); | ||
}; | ||
util.inherits(UniqueWriteStream, WriteStream); | ||
inherits(WriteStreamUnique, WriteStream); | ||
UniqueWriteStream.prototype.open = function() { | ||
openUnique(this.path, this.mode, function(err, fd) { | ||
WriteStreamUnique.prototype.open = function() { | ||
var self = this; | ||
openUnique(this.path, { | ||
flags: this.flags, | ||
mode: this.mode, | ||
force: this.force | ||
}, function(err, fd) { | ||
if (err) { | ||
this.destroy(); | ||
this.emit('error', err); | ||
self.destroy(); | ||
self.emit("error", err); | ||
return; | ||
} | ||
this.fd = fd; | ||
this.emit('open', fd); | ||
}.bind(this)); | ||
self.fd = fd; | ||
self.emit("open", fd); | ||
}); | ||
}; | ||
var createUniqueWriteStream = function(path, options) { | ||
return new UniqueWriteStream(path, options); | ||
var createWriteStreamUnique = function(file, options) { | ||
return new WriteStreamUnique(file, options); | ||
}; | ||
@@ -101,4 +158,3 @@ | ||
writeFileUnique: writeFileUnique, | ||
createUniqueWriteStream: createUniqueWriteStream | ||
createWriteStreamUnique: createWriteStreamUnique | ||
}; | ||
{ | ||
"name": "fsu", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Unique file name with streams support", | ||
@@ -10,3 +10,4 @@ "license" : "MIT", | ||
"engines": { | ||
"node": "~0.10.0" | ||
"node": ">0.10.0", | ||
"iojs": ">1.0.0" | ||
}, | ||
@@ -13,0 +14,0 @@ "repository": { |
@@ -31,12 +31,15 @@ #fsu (fs unique) | ||
## createUniqueWriteStream(path, [options]) | ||
Same as [fs.createReadStream](http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options) but returns writable stream for unique file. | ||
## createWriteStreamUnique(path, [options]) | ||
Same as [fs.createWriteStream](https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options) but returns writable stream for unique file. | ||
```js | ||
var fsu = require('fsu'); | ||
var stream = fsu.createUniqueWriteStream("text{_###}.txt"); | ||
var stream = fsu.createWriteStreamUnique("text{_###}.txt"); | ||
``` | ||
## force path creation | ||
Add `force = true` to options, and it will recursively create directories if they are not exist. | ||
## pattern | ||
You must use `{#}` pattern in filename and path. All `#` characters will be change with counter for existing files. Number of `#` means padding for unique counter | ||
You must use `{#}` pattern in filename and path. All `#` characters will be change with counter for existing files. Number of `#` means padding for unique counter. **With no pattern in the filename works as usual 'fs' module.** | ||
@@ -43,0 +46,0 @@ If we run second example several times filenames will be |
12
test.js
"use strict"; | ||
var fs = require('fs'); | ||
var fsu = require('./index.js'); | ||
var fs = require("fs"); | ||
var fsu = require("./index.js"); | ||
var stream = fsu.createUniqueWriteStream("text{_stream###}.txt"); | ||
var stream = fsu.createWriteStreamUnique("text{_stream###}.txt"); | ||
fsu.writeFileUnique("text{_file###}.txt", "test", function(err) { | ||
if(!err) { | ||
fsu.writeFileUnique("css/text{_file###}.txt", "test", {force: true}, function(err) { | ||
if(err) { | ||
console.log(err); | ||
} else { | ||
fs.createReadStream("readme.md").pipe(stream); | ||
} | ||
}); |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
7359
148
54