Socket
Socket
Sign inDemoInstall

q-io

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

q-io - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

buffer-stream.js

2

fs-boot.js

@@ -103,3 +103,3 @@ (function (exports) {

exports.join = function () {
if (arguments.length === 1 && typeof arguments[0] === "object")
if (arguments.length === 1 && Array.isArray(arguments[0]))
return exports.normal.apply(exports, arguments[0]);

@@ -106,0 +106,0 @@ return exports.normal.apply(exports, arguments);

@@ -10,3 +10,2 @@

// TODO match
// TODO copyTree

@@ -39,8 +38,7 @@ var concat = function (arrays) {

return stream.read();
}, function (reason) {
var error = new Error("Can't read " + path + " because " + reason.message);
}, function (error) {
error.message = "Can't read " + path + " because " + error.message;
error.path = path;
error.flags = flags;
error.charset = charset;
error.cause = reason;
throw error;

@@ -110,12 +108,12 @@ });

var self = this;
return Q.when(exports.stat(source), function (stat) {
return Q.when(self.stat(source), function (stat) {
if (stat.isFile()) {
return exports.copy(source, target);
return self.copy(source, target);
} else if (stat.isDirectory()) {
return Q.when(exports.makeDirectory(target), function () {
return Q.when(exports.list(source), function (list) {
return Q.when(self.makeDirectory(target), function () {
return Q.when(self.list(source), function (list) {
return Q.all(list.map(function (child) {
return exports.copyTree(
exports.join(source, child),
exports.join(target, child)
return self.copyTree(
self.join(source, child),
self.join(target, child)
);

@@ -126,3 +124,3 @@ }));

} else if (stat.isSymbolicLink()) {
return exports.symbolicCopy(source, target);
return self.symbolicCopy(source, target);
}

@@ -221,2 +219,9 @@ });

exports.symbolicCopy = function (source, target) {
var self = this;
return Q.when(self.relative(target, source), function (relative) {
return self.symbolicLink(target, relative, "file");
});
};
exports.exists = function (path) {

@@ -246,2 +251,20 @@ return Q.when(this.stat(path), function () {

exports.isSymbolicLink = function (path) {
return Q.when(this.statLink(path), function (stat) {
return stat.isSymbolicLink();
}, function (reason) {
return false;
});
};
exports.lastModified = function (path) {
var self = this;
return self.stat(path).invoke('lastModified');
};
exports.lastAccessed = function (path) {
var self = this;
return self.stat(path).invoke('lastAccessed');
};
exports.absolute = function (path) {

@@ -386,32 +409,3 @@ if (this.isAbsolute(path))

var Stats = exports.Stats = function (nodeStat) {
this.node = nodeStat;
this.size = nodeStat.size;
};
var stats = [
"isDirectory",
"isFile",
"isBlockDevice",
"isCharacterDevice",
"isSymbolicLink",
"isFIFO",
"isSocket"
];
stats.forEach(function (name) {
Stats.prototype[name] = function () {
return this.node[name]();
};
});
Stats.prototype.lastModified = function () {
return new Date(this.node.mtime);
};
Stats.prototype.lastAccessed = function () {
return new Date(this.node.atime);
};
}

@@ -1,7 +0,8 @@

(function (require, exports) {
var Q = require("q");
var BOOT = require("./fs-boot");
var FS = require("./fs");
var COMMON = require("./fs-common");
var Boot = require("./fs-boot");
var Common = require("./fs-common");
var BufferStream = require("./buffer-stream");
var Reader = require("./reader");
var Set = require("collections/set");

@@ -11,66 +12,67 @@ module.exports = MockFs;

function MockFs(files) {
var fs = Object.create(BOOT);
var root = {};
var now = new Date();
if (!(this instanceof MockFs)) {
return new MockFs(files);
}
this._root = new DirectoryNode(this, "/");
function init() {
// construct a file tree
Object.keys(files).forEach(function (path) {
var content = files[path];
find(root, path).set(content);
});
}
function find(at, path) {
path = fs.absolute(path);
if (path === "" || path === FS.ROOT) {
return Node(function get() {
return root;
}, function set(content) {
root = content;
});
}
var parts = FS.split(path);
var empty = parts.shift();
if (empty !== "")
throw new Error("assertion: first component of root should be empty");
var i, ii;
var manifest = function () {};
for (i = 0, ii = parts.length - 1; i < ii; i++) {
var part = parts[i];
if (part === ".") {
continue;
} if (typeof at[part] !== "object") {
manifest = (function (on, part, manifest) {
var created = {};
at = created;
return function () {
on[part] = created;
manifest();
};
})(at, part, manifest);
Common.update(this, function () {
return workingDirectory;
});
var workingDirectory = this.ROOT;
if (files) {
this._init(files);
}
}
MockFs.prototype = Object.create(Boot);
MockFs.prototype._init = function (files, tree) {
tree = tree || this.ROOT;
Object.keys(files).forEach(function (path) {
var content = files[path];
path = this.join(tree, path);
var directory = this.directory(path);
var base = this.base(path);
var directoryNode = this._root._walk(directory, true);
var fileNode = new FileNode(this);
if (!(content instanceof Buffer)) {
if (typeof content === "object") {
this._init(content, path);
return;
} else {
at = at[part];
content = new Buffer(String(content), "utf-8");
}
}
var leaf = parts[i];
return Node(function get() {
return at[leaf];
}, function set(content) {
manifest();
at[leaf] = content;
});
}
directoryNode._entries[base] = fileNode;
fileNode._chunks = [content];
}, this);
};
fs.list = function (path) {
path = String(path);
return Q.when(fs.stat(path), function (stat) {
if (!stat.isDirectory())
throw new Error("Can't list non-directory " + path);
var node = find(root, path).get();
return Object.keys(node);
});
};
MockFs.prototype.list = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var node = self._root._walk(path)._follow(path);
if (!node.isDirectory()) {
new Error("Can't list non-directory: " + JSON.stringify(path));
}
return Object.keys(node._entries).sort();
});
};
fs.open = function (path, flags, charset, options) {
MockFs.prototype.open = function (path, flags, charset, options) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var directory = self.directory(path);
var base = self.base(path);
var node = self._root._walk(directory);
if (!node.isDirectory()) {
throw new Error("Can't find " + path + " because " + directory + " is not a directory");
}
if (typeof flags == "object") {

@@ -83,68 +85,213 @@ options = flags;

}
var node = find(root, path);
// TODO create an actual open file object, rather
// than this rather primitive duck
flags = flags || "r";
var binary = flags.indexOf("b") >= 0;
charset = charset || "utf-8";
if (flags.indexOf("w") === -1) {
return fs.stat(path).post("isFile")
.then(function (isFile) {
if (!isFile) {
throw new Error("Can't open non-file " + path);
}
return {
"read": function () {
var content = node.get();
if (!binary)
content = content.toString(charset);
return content;
}
};
});
} else {
throw new Error("Can't open files for writing in read-only mock file system");
var write = flags.indexOf("w") >= 0;
if (!binary) {
charset = charset || "utf-8";
}
};
if (write) {
if (!node._entries[base]) {
node._entries[base] = new FileNode(this);
}
var fileNode = node._entries[base]._follow(path);
if (!fileNode.isFile()) {
throw new Error("Can't write non-file " + path);
}
fileNode._lastModified = new Date();
fileNode._lastAccessed = new Date();
return new BufferStream(fileNode._chunks, charset);
} else { // read
if (!node._entries[base]) {
throw new Error("Can't read non-existant " + path);
}
var fileNode = node._entries[base]._follow(path);
if (!fileNode.isFile()) {
throw new Error("Can't read non-file " + path);
}
fileNode._lastAccessed = new Date();
if ("begin" in options && "end" in options) {
return new BufferStream(
[
Reader.join(fileNode._chunks)
.slice(options.begin, options.end)
],
charset
);
} else {
return new BufferStream(fileNode._chunks, charset);
}
}
});
};
fs.stat = function (path) {
var stat = find(root, path);
if (stat.get() === undefined)
return Q.reject(new Error("No such file: " + path));
return Q.resolve(stat);
};
MockFs.prototype.remove = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var directory = self.directory(path);
var name = self.base(path);
var node = self._root._walk(directory);
if (!node.isDirectory()) {
throw new Error("Can't remove file from non-directory: " + path);
}
if (!node._entries[name]) {
throw new Error("Can't remove non-existant file: " + path);
}
if (node._entries[name].isDirectory()) {
throw new Error("Can't remove directory. Use removeDirectory: " + path);
}
delete node._entries[name];
});
};
fs.getNode = function (path) {
path = path || "";
return find(root, path).get();
};
MockFs.prototype.makeDirectory = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var directory = self.directory(path);
var name = self.base(path);
var node = self._root._walk(directory);
if (!node.isDirectory()) {
throw new Error("Can't make directory in non-directory: " + path);
}
if (node._entries[name]) {
throw new Error("Can't make directory. Entry exists: " + path);
}
node._entries[name] = new DirectoryNode(self);
});
};
fs.canonical = function (path) {
return fs.normal(path);
};
MockFs.prototype.removeDirectory = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var directory = self.directory(path);
var name = self.base(path);
var node = self._root._walk(directory);
if (!node.isDirectory()) {
throw new Error("Can't remove directory from non-directory: " + path);
}
if (!node._entries[name]) {
throw new Error("Can't remove non-existant directory: " + path);
}
if (!node._entries[name].isDirectory()) {
throw new Error("Can't remove non-directory: " + path);
}
delete node._entries[name];
});
};
var Node = function (get, set) {
var self = Object.create(Node.prototype);
self.get = get;
self.set = set;
return self;
};
MockFs.prototype.stat = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
return self._root._walk(path)._follow(path);
});
};
Node.prototype = Object.create(GenericNode.prototype);
MockFs.prototype.statLink = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var node = self._root._walk(path);
if (!node.isSymbolicLink()) {
throw new Error("Path is not symbolic link: " + JSON.stringify(path));
}
return node;
});
};
Node.prototype.constructor = Node;
MockFs.prototype.link = function (source, target) {
var self = this;
return Q.fcall(function () {
source = self.absolute(source);
target = self.absolute(target);
var sourceNode = self._root._walk(source)._follow(source);
if (!sourceNode.isFile()) {
throw new Error("Can't link non-file: " + source);
}
var directory = self.directory(target);
var base = self.base(target);
var targetNode = self._root._walk(directory)._follow(directory);
if (!targetNode.isDirectory()) {
throw new Error("Can't create link in non-directory: " + target);
}
if (targetNode._entries[base] && targetNode._entries[base].isDirectory()) {
throw new Error("Can't overwrite existing directory with hard link: " + target);
}
targetNode._entries[base] = sourceNode;
});
};
Node.prototype.lastModified = function () {
return now;
};
MockFs.prototype.symbolicLink = function (target, relative, type) {
var self = this;
return Q.fcall(function () {
target = self.absolute(target);
var directory = self.directory(target);
var base = self.base(target);
var node = self._root._walk(directory);
if (node._entries[base] && node._entries[base].isDirectory()) {
throw new Error("Can't overwrite existing directory with symbolic link: " + target);
}
node._entries[base] = new LinkNode(self, relative);
});
};
COMMON.update(fs, function () {
return fs.ROOT;
MockFs.prototype.chown = function (path, owner) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
self._root._walk(path)._follow(path)._owner = owner;
});
};
init();
MockFs.prototype.chmod = function (path, mode) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
self._root._walk(path)._follow(path)._mode = mode;
});
};
return fs;
}
MockFs.prototype.move = function (source, target) {
var self = this;
return Q.fcall(function () {
source = self.absolute(source);
target = self.absolute(target);
var sourceDirectory = self.directory(source);
var sourceDirectoryNode = self._root._walk(sourceDirectory)._follow(sourceDirectory);
var sourceName = self.base(source);
var sourceNode = sourceDirectoryNode._entries[sourceName]; // not followed
var targetDirectory = self.directory(target);
var targetDirectoryNode = self._root._walk(targetDirectory)._follow(targetDirectory);
var targetName = self.base(target);
if (targetDirectoryNode._entries[targetName] && targetDirectoryNode._entires[targetName].isDirectory()) {
// move the node into the directory
targetDirectoryNode = targetDirectoryNode._entries[targetName]._follow(target);
}
targetDirectoryNode._entries[targetName] = sourceNode;
delete sourceDirectoryNode._entries[sourceName];
});
};
MockFs.prototype.readLink = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
var node = self._root._walk(path);
if (!self.isSymbolicLink()) {
throw new Error("Can't read non-symbolic link: " + path);
}
return node._link;
});
};
MockFs.prototype.canonical = function (path) {
var self = this;
return Q.fcall(function () {
path = self.absolute(path);
return self._root._canonical(path);
});
};
MockFs.mock = mock;

@@ -170,37 +317,176 @@ function mock(fs, root) {

var GenericNode = function () {};
function Node(fs) {
if (!fs)
throw new Error("FS required argument");
this._fs = fs;
this._accessed = this._modified = new Date();
this._mode = parseInt("0644", 8);
this._owner = null;
}
GenericNode.prototype.exists = function () {
var node = this.get();
return typeof node !== "undefined";
Node.prototype._walk = function (path, make, via) {
var parts = this._fs.split(path);
if (this._fs.isAbsolute(path)) {
parts.shift();
return this._fs._root._walkParts(parts, make, this._fs.ROOT);
} else {
return this._walkParts(parts, make, via || this._fs.ROOT);
}
};
GenericNode.prototype.isFile = function () {
var node = this.get();
return typeof node !== "undefined" && (
typeof node !== "object" ||
node.constructor !== Object
Node.prototype._walkParts = function (parts, make, via) {
if (parts.length === 0) {
return this;
} else {
var part = parts.shift();
if (part === "") {
return this._walkParts(parts, make, this._fs.join(via, part));
} else {
throw new Error("Can't find " + JSON.stringify(this._fs.resolve(part, this._fs.join(parts))) + " via " + JSON.stringify(via));
}
}
};
Node.prototype._canonical = function (path) {
if (!this._fs.isAbsolute(path)) {
throw new Error("Path must be absolute for _canonical: " + path);
}
var parts = this._fs.split(path);
parts.shift();
var via = this._fs.ROOT;
return via + this._fs._root._canonicalParts(parts, via);
};
Node.prototype._canonicalParts = function (parts, via) {
if (parts.length === 0) {
return via;
}
return this._fs.join(via, this._fs.join(parts));
};
Node.prototype._follow = function () {
return this;
};
Node.prototype._touch = function () {
this._modified = new Date();
};
var stats = [
"isDirectory",
"isFile",
"isBlockDevice",
"isCharacterDevice",
"isSymbolicLink",
"isFIFO",
"isSocket"
];
stats.forEach(function (name) {
Node.prototype[name] = function () {
return false;
};
});
Node.prototype.lastAccessed = function () {
return this._accessed;
};
Node.prototype.lastModified = function () {
return this._modified;
};
function FileNode(fs) {
Node.call(this, fs);
this._chunks = [];
}
FileNode.prototype = Object.create(Node.prototype);
FileNode.prototype.isFile = function () {
return true;
};
function DirectoryNode(fs) {
Node.call(this, fs);
this._entries = Object.create(null);
this._mode = parseInt("0755", 8);
}
DirectoryNode.prototype = Object.create(Node.prototype);
DirectoryNode.prototype.isDirectory = function () {
return true;
};
DirectoryNode.prototype._walkParts = function (parts, make, via) {
via = via || this._fs.ROOT;
if (parts.length === 0) {
return this;
}
var part = parts.shift();
if (part === "") {
return this._walkParts(parts, make, this._fs.join(via, part));
}
if (!this._entries[part]) {
if (make) {
this._entries[part] = new DirectoryNode(this._fs);
} else {
throw new Error("Can't find " + JSON.stringify(this._fs.join(parts)) + " via " + JSON.stringify(via));
}
}
return this._entries[part]._walkParts(parts, make, this._fs.join(via, part));
};
DirectoryNode.prototype._canonicalParts = function (parts, via) {
if (parts.length === 0) {
return via;
}
var part = parts.shift();
if (part === "") {
return via;
}
if (via === this._fs.ROOT) {
via = "";
}
if (!this._entries[part]) {
return this._fs.join(via, part, this._fs.join(parts));
}
return this._entries[part]._canonicalParts(
parts,
this._fs.join(via, part)
);
};
GenericNode.prototype.isDirectory = function () {
var node = this.get();
return (
typeof node === "object" &&
node.constructor === Object
)
function LinkNode(fs, link) {
Node.call(this, fs);
this._link = link;
}
LinkNode.prototype = Object.create(Node.prototype);
LinkNode.prototype.isSymbolicLink = function () {
return true;
};
}).apply(null, typeof exports !== "undefined" ?
[require, exports] :
[
function (id) {
id = id.toUpperCase()
.replace(".", "Q_FS")
.replace("/", "$")
.replace("-", "_");
return window[id];
},
Q_FS$MOCK
]
)
LinkNode.prototype._follow = function (via, memo) {
memo = memo || Set();
if (memo.has(this)) {
throw new Error("Can't follow symbolic link cycle at " + JSON.stringify(via));
}
memo.add(this);
var link = this._fs.join(via, "..", this._link);
return this._walk(link, null, "<link>")._follow(link, memo);
};
LinkNode.prototype._canonicalParts = function (parts, via) {
return this._fs.relativeFromDirectory(this._fs.ROOT,
this._fs._root._canonical(
this._fs.absolute(this._fs.join(via, "..", this._link))
)
);
};
// cycle breaking
var FS = require("./fs");

@@ -45,3 +45,3 @@

} else {
return Q.reject("No such file: " + JSON.stringify(path));
return Q.reject("Can't find: " + JSON.stringify(path));
}

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

@@ -79,2 +79,4 @@ /**

}
} else {
charset = charset || 'utf-8';
}

@@ -174,3 +176,3 @@ if (flags.indexOf("w") >= 0) {

} else {
done.resolve(new self.Stats(stat));
done.resolve(new Stats(stat));
}

@@ -184,2 +186,31 @@ });

var Stats = function (nodeStat) {
this.node = nodeStat;
this.size = nodeStat.size;
};
var stats = [
"isDirectory",
"isFile",
"isBlockDevice",
"isCharacterDevice",
"isSymbolicLink",
"isFIFO",
"isSocket"
];
stats.forEach(function (name) {
Stats.prototype[name] = function () {
return this.node[name]();
};
});
Stats.prototype.lastModified = function () {
return new Date(this.node.mtime);
};
Stats.prototype.lastAccessed = function () {
return new Date(this.node.atime);
};
exports.statLink = function (path) {

@@ -271,8 +302,2 @@ path = String(path);

exports.symbolicCopy = function (source, target) {
return Q.when(exports.relative(target, source), function (relative) {
return exports.symbolicLink(target, relative, "file");
});
};
exports.chown = function (path, uid, gid) {

@@ -315,10 +340,2 @@ path = String(path);

exports.lastModified = function (path) {
return exports.stat(path).invoke('lastModified');
};
exports.lastAccessed = function (path) {
return exports.stat(path).invoke('lastAccessed');
};
exports.canonical = function (path) {

@@ -325,0 +342,0 @@ var result = Q.defer();

{
"name": "q-io",
"version": "1.4.1",
"version": "1.5.0",
"description": "IO using Q promises",

@@ -5,0 +5,0 @@ "homepage": "http://github.com/kriskowal/q-io/",

@@ -23,5 +23,2 @@

// prevent indefinite buffering; resume on demand
//_stream.pause();
_stream.on("error", function (reason) {

@@ -66,3 +63,2 @@ begin.reject(reason);

receiver = undefined;
//_stream.resume();
var deferred = Q.defer();

@@ -84,3 +80,2 @@ Q.done(end.promise, function () {

self.forEach = function (write) {
//_stream.resume();
if (chunks && chunks.length)

@@ -87,0 +82,0 @@ write(slurp());

exports["test fs"] = require("./fs/all");
exports["test http"] = require("./http/all");
exports["test http apps"] = require("./http-apps/all");
exports["test issue 1"] = require("./issues/1");

@@ -7,0 +5,0 @@ if (require.main === module) {

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