Skip to content

Commit

Permalink
jshint light q-io v1
Browse files Browse the repository at this point in the history
  • Loading branch information
hthetiot committed Jul 20, 2018
1 parent b03ed4b commit 4dd05af
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 36 deletions.
2 changes: 2 additions & 0 deletions coverage-report.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global __dirname:true */

var Q = require("q");
var FS = require("./fs");

Expand Down
5 changes: 3 additions & 2 deletions deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* @param {Number} [stackTraceLimit] - depth of the stack trace to print out. Set to falsy value to disable stack.
*/
exports.deprecationWarning = function deprecationWarning(name, alternative, stackTraceLimit) {
var depth;
if (stackTraceLimit) {
var depth = Error.stackTraceLimit;
depth = Error.stackTraceLimit;
Error.stackTraceLimit = stackTraceLimit;
}
if (typeof console !== "undefined" && typeof console.warn === "function") {
Expand Down Expand Up @@ -43,7 +44,7 @@ exports.deprecationWarning = function deprecationWarning(name, alternative, stac
exports.deprecateMethod = function deprecate(scope, deprecatedFunction, name, alternative) {
var deprecationWrapper = function () {
// stackTraceLimit = 3 // deprecationWarning + deprecate + caller of the deprecated method
deprecationWarning(name, alternative, 3);
exports.deprecationWarning(name, alternative, 3);
return deprecatedFunction.apply(scope ? scope : this, arguments);
};
deprecationWrapper.deprecatedFunction = deprecatedFunction;
Expand Down
25 changes: 18 additions & 7 deletions fs-boot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global process:false, FS_BOOT:true */

(function (exports) {

// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
Expand All @@ -15,8 +17,9 @@
/*whatsupdoc*/
/*markup markdown*/

var ESCAPE_REG_EXP = /[-[\]{}()*+?.\\^$|,#\s]/g;
var regExpEscape = function (str) {
return str.replace(/[-[\]{}()*+?.\\^$|,#\s]/g, "\\$&");
return str.replace(ESCAPE_REG_EXP, "\\$&");
};

var path = require("path");
Expand Down Expand Up @@ -52,6 +55,7 @@ var separatorCached, altSeparatorCached, separatorReCached;
/**
* @function
*/

exports.SEPARATORS_RE = function () {
if (
separatorCached !== exports.SEPARATOR ||
Expand All @@ -60,8 +64,8 @@ exports.SEPARATORS_RE = function () {
separatorCached = exports.SEPARATOR;
altSeparatorCached = exports.ALT_SEPARATOR;
separatorReCached = new RegExp("[" +
(separatorCached || "").replace(/[-[\]{}()*+?.\\^$|,#\s]/g, "\\$&") +
(altSeparatorCached || "").replace(/[-[\]{}()*+?.\\^$|,#\s]/g, "\\$&") +
(separatorCached || "").replace(ESCAPE_REG_EXP, "\\$&") +
(altSeparatorCached || "").replace(ESCAPE_REG_EXP, "\\$&") +
"]", "g");
}
return separatorReCached;
Expand Down Expand Up @@ -120,8 +124,9 @@ exports.resolve = function () {
var parents = [];
var children = [];
var leaf = "";
var path;
for (var i = 0; i < arguments.length; i++) {
var path = String(arguments[i]);
path = String(arguments[i]);
if (path == "")
continue;
var parts = path.split(exports.SEPARATORS_RE());
Expand Down Expand Up @@ -150,10 +155,15 @@ exports.resolve = function () {
} else {
children.push(part);
}
};
}
}

path = parents.concat(children).join(exports.SEPARATOR);
if (path) leaf = exports.SEPARATOR + leaf;

if (path) {
leaf = exports.SEPARATOR + leaf;
}

return root + path + leaf;
};

Expand All @@ -168,8 +178,9 @@ exports.normal = function () {
var root = "";
var parents = [];
var children = [];
var path;
for (var i = 0, ii = arguments.length; i < ii; i++) {
var path = String(arguments[i]);
path = String(arguments[i]);
// empty paths have no affect
if (path === "")
continue;
Expand Down
3 changes: 2 additions & 1 deletion fs-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ exports.update = function (exports, workingDirectory) {
return Q.when(stat, function (stat) {
var paths = [];
var mode; // true:include, false:exclude, null:no-recur
var include;
try {
var include = guard(basePath, stat);
include = guard(basePath, stat);
} catch (exception) {
return Q.reject(exception);
}
Expand Down
19 changes: 11 additions & 8 deletions fs-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ MockFs.prototype.list = function (path) {
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));
throw new Error("Can't list non-directory: " + JSON.stringify(path));
}
return Object.keys(node._entries).sort();
});
Expand Down Expand Up @@ -91,14 +91,15 @@ MockFs.prototype.open = function (path, flags, charset, options) {
if (!binary) {
charset = charset || "utf-8";
}
var fileNode;
if (write || append) { // write
if (!node._entries[base]) {
node._entries[base] = new FileNode(this);
if ("mode" in options) {
node._entries[base].mode = options.mode;
}
}
var fileNode = node._entries[base]._follow(path);
fileNode = node._entries[base]._follow(path);
if (!fileNode.isFile()) {
throw new Error("Can't write non-file " + path);
}
Expand All @@ -112,7 +113,7 @@ MockFs.prototype.open = function (path, flags, charset, options) {
if (!node._entries[base]) {
throw new Error("Can't read non-existant " + path);
}
var fileNode = node._entries[base]._follow(path);
fileNode = node._entries[base]._follow(path);
if (!fileNode.isFile()) {
throw new Error("Can't read non-file " + path);
}
Expand Down Expand Up @@ -161,14 +162,15 @@ MockFs.prototype.makeDirectory = function (path, mode) {
var directory = self.directory(path);
var name = self.base(path);
var node = self._root._walk(directory);
var error;
if (!node.isDirectory()) {
var error = new Error("Can't make directory in non-directory: " + path);
error = new Error("Can't make directory in non-directory: " + path);
error.code = "EEXISTS";
error.exists = true;
throw error;
}
if (node._entries[name]) {
var error = new Error("Can't make directory. Entry exists: " + path);
error = new Error("Can't make directory. Entry exists: " + path);
error.code = "EISDIR";
error.exists = true;
error.isDirectory = true;
Expand Down Expand Up @@ -279,9 +281,10 @@ MockFs.prototype.rename = function (source, target) {
var sourceDirectoryNode = self._root._walk(sourceDirectory)._follow(sourceDirectory);
var sourceName = self.base(source);
var sourceNode = sourceDirectoryNode._entries[sourceName];
var error;

if (!sourceNode) {
var error = new Error("Can't copy non-existent file: " + source);
error = new Error("Can't copy non-existent file: " + source);
error.code = "ENOENT";
throw error;
}
Expand All @@ -290,7 +293,7 @@ MockFs.prototype.rename = function (source, target) {

// check again after following symbolic links
if (!sourceNode) {
var error = new Error("Can't copy non-existent file: " + source);
error = new Error("Can't copy non-existent file: " + source);
error.code = "ENOENT";
throw error;
}
Expand All @@ -305,7 +308,7 @@ MockFs.prototype.rename = function (source, target) {
}

if (targetNode && targetNode.isDirectory()) {
var error = new Error("Can't copy over existing directory: " + target);
error = new Error("Can't copy over existing directory: " + target);
error.code = "EISDIR";
throw error;
}
Expand Down
7 changes: 5 additions & 2 deletions fs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global process:true */

/**
* An asynchronous local file system API, based on a subset
* of the `narwhal/fs` API and the `narwhal/promise` API,
Expand Down Expand Up @@ -79,11 +81,12 @@ exports.open = dampen(function (path, flags, charset, options) {
} else {
charset = charset || 'utf-8';
}
var stream;
if (flags.indexOf("w") >= 0 || flags.indexOf("a") >= 0) {
var stream = FS.createWriteStream(String(path), nodeOptions);
stream = FS.createWriteStream(String(path), nodeOptions);
return Writer(stream, charset);
} else {
var stream = FS.createReadStream(String(path), nodeOptions);
stream = FS.createReadStream(String(path), nodeOptions);
return Reader(stream, charset);
}
});
Expand Down
4 changes: 2 additions & 2 deletions http-apps/chain.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

module.exports = Chain;

function Chain(end) {
var self = Object.create(Chain.prototype);
self.end = end || function (next) {
return next;
};
return self;
};
}

Chain.prototype.use = function (App /*, ...args*/) {
if (!App) throw new Error("App is not defined after " + this.app);
Expand All @@ -21,4 +22,3 @@ Chain.prototype.use = function (App /*, ...args*/) {
this.app = App;
return this;
};

2 changes: 2 additions & 0 deletions http-apps/content.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global inspect:true */

var Q = require("q");
var Negotiate = require("./negotiate");
var QS = require("qs");
Expand Down
4 changes: 2 additions & 2 deletions http-apps/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function allHostsContaining(host) {
} if (hostname === "localhost") {
return [hostname + port];
} else {
var parts = hostname.split(".");
parts = hostname.split(".");
var hosts = [];
while (parts.length > 1) {
hosts.push("." + parts.join(".") + port);
Expand Down Expand Up @@ -158,7 +158,7 @@ function hostContains(containerHost, contentHost) {
} else {
return containerHostname === contentHostname;
}
};
}

function pathContains(container, content) {
if (/^\/$/.test(container)) {
Expand Down
2 changes: 1 addition & 1 deletion http-apps/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var StatusApps = require("./status");

exports.Normalize = function (app) {
return function (request, response) {
var request = HTTP.normalizeRequest(request);
request = HTTP.normalizeRequest(request);
return Q.when(app(request, response), function (response) {
return HTTP.normalizeResponse(response);
});
Expand Down
3 changes: 2 additions & 1 deletion http-apps/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ exports.file = function (request, path, contentType, fs) {
// Invalid cache
if (
"if-range" in request.headers &&
etag != request.headers["if-range"]
etag != request.headers["if-range"]
) {
// Normal 200 for entire, altered content
range = null;
} else {
// Truncate to the first requested continuous range
range = interpretFirstRange(request.headers["range"], stat.size);
Expand Down
6 changes: 4 additions & 2 deletions http-apps/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ exports.Json = function (app, reviver, tabs) {
* @returns {Response}
*/
exports.json = function (content, reviver, tabs) {
var json;
try {
var json = JSON.stringify(content, reviver, tabs);
json = JSON.stringify(content, reviver, tabs);
} catch (exception) {
return Q.reject(exception);
}
Expand All @@ -67,8 +68,9 @@ exports.JsonRequest = function (app, badRequest) {
if (!badRequest)
badRequest = Status.badRequest;
return Content.ContentRequest(function (content, request, response) {
var object;
try {
var object = JSON.parse(content);
object = JSON.parse(content);
} catch (error) {
return badRequest(request, error);
}
Expand Down
20 changes: 13 additions & 7 deletions http-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ var QS = require("qs");
* @param {String} cookie
* @returns {Object}
*/
var DOMAIN_REG_EXP = /^domain$/i,
PATH_REG_EXP = /^path$/i,
EXPIRE_REG_EXP = /^expires$/i,
MAX_AGE_REG_EXP = /^max-age$/i,
SECURE_REG_EXP = /^secure$/i,
HTTP_ONLY_REG_EXP = /^httponly$/i;

exports.parse = function (cookie, date) {
date = date || new Date();
var parsed = {};
Expand All @@ -24,23 +31,23 @@ exports.parse = function (cookie, date) {
return part.trim();
});
var key = parts[0], value = parts[1];
if (/^domain$/i.test(key)) {
if (DOMAIN_REG_EXP.test(key)) {
parsed.domain = value;
} else if (/^path$/i.test(key)) {
} else if (PATH_REG_EXP.test(key)) {
parsed.path = value;
} else if (/^expires$/i.test(key)) {
} else if (EXPIRE_REG_EXP.test(key)) {
parsed.expires = new Date(
+new Date() + // actual now
(new Date(value) - date) // server offset
);
} else if (/^max-age$/i.test(key)) {
} else if (MAX_AGE_REG_EXP.test(key)) {
parsed.expires = new Date(
new Date().getTime() +
(value * 1000)
);
} else if (/^secure$/i.test(key)) {
} else if (SECURE_REG_EXP.test(key)) {
parsed.secure = true;
} else if (/^httponly$/i.test(key)) {
} else if (HTTP_ONLY_REG_EXP.test(key)) {
parsed.httpOnly = true;
}
});
Expand Down Expand Up @@ -72,4 +79,3 @@ exports.stringify = function (key, value, options) {
}
return cookie;
};

2 changes: 1 addition & 1 deletion writer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

/* global process:true */
var Q = require("q");

/**
Expand Down

0 comments on commit 4dd05af

Please sign in to comment.