Ejemplo n.º 1
0
function Sessions(opts) {
    opts || (opts = {});

    this.name = isString(opts.name) ? opts.name : "sid";
    this.store = isObject(opts.store) ? opts.store : new MemoryStore();
    this.secret = isString(opts.secret) ? opts.secret : "sessions";
    this.generateID = isFunction(opts.generateID) ? opts.generateID : uid;
}
Ejemplo n.º 2
0
function sign(val, secret) {
    if (!isString(val)) {
        throw new TypeError("cookie required");
    }
    if (!isString(secret)) {
        throw new TypeError("secret required");
    }

    return val + "." + crypto.createHmac("sha256", secret).update(val).digest("base64").replace(sign.reReplacer, "");
}
Ejemplo n.º 3
0
function unsign(val, secret) {
    var str, mac;

    if (!isString(val)) {
        throw new TypeError("cookie required");
    }
    if (!isString(secret)) {
        throw new TypeError("secret required");
    }

    str = val.slice(0, val.lastIndexOf("."));
    mac = sign(str, secret);

    return sha1(mac) === sha1(val) ? str : false;
}
Ejemplo n.º 4
0
function mkdirPSync(path, mode, made) {
    var stat;

    mode = mode || (mode = 511 & (~process.umask()));
    if (isString(mode)) {
        mode = parseInt(mode, 8);
    }

    if (!made) {
        made = null;
    }

    try {
        fs.mkdirSync(path, mode);
        made = made || path;
    } catch (e) {
        if (e.code === "ENOENT") {
            made = mkdirPSync(filePath.dirname(path), mode, made);
            mkdirPSync(path, mode, made);
        } else {
            try {
                stat = fs.statSync(path);
            } catch (error) {
                throw e;
            }
            if (!stat.isDirectory()) {
                throw e;
            }
        }
    }

    return made;
}
Ejemplo n.º 5
0
 objectForEach(options.headers, function(value, key) {
     if (isString(value)) {
         if (key === "Content-Type" && canOverrideMimeType) {
             xhr.overrideMimeType(value);
         }
         xhr.setRequestHeader(key, value);
     }
 });
Ejemplo n.º 6
0
 success: function(response) {
     cache[locale] = true;
     if (isString(response.data)) {
         response.data = JSON.parse(response.data);
     }
     i18n.add(locale, response.data);
     next();
 },
Ejemplo n.º 7
0
function toString(value) {
    if (isString(value)) {
        return value;
    } else if (isNullOrUndefined(value)) {
        return "";
    } else {
        return value + "";
    }
}
Ejemplo n.º 8
0
RouterPrototype.use = function(path) {
    var _this = this,
        layers = this.__layers,
        middleware, middlewareStack, stack;

    if (isString(path)) {
        stack = fastSlice(arguments, 1);
    } else {
        stack = fastSlice(arguments);
        path = "/";
    }

    middlewareStack = [];

    arrayForEach(stack, function(handler) {
        var mw;

        if (isFunction(handler)) {
            middlewareStack[middlewareStack.length] = handler;
        } else if (handler.__isRouter__) {
            _this.scope(handler);
        } else if (isObject(handler)) {
            if (isFunction(handler.middleware)) {
                mw = handler.middleware;

                if (mw.length >= 3) {
                    middlewareStack[middlewareStack.length] = function(err, ctx, next) {
                        handler.middleware(err, ctx, next);
                    };
                } else {
                    middlewareStack[middlewareStack.length] = function(ctx, next) {
                        handler.middleware(ctx, next);
                    };
                }
            } else {
                throw new Error("use(handlers...) handler middleware must be a function");
            }
        } else {
            throw new Error("use(handlers...) handlers must be functions or objects with a middleware function");
        }
    });

    if (middlewareStack.length !== 0) {
        middleware = new this.Middleware(path, this, false);
        middleware.__isMiddleware__ = true;
        layers[layers.length] = middleware;
        middleware.mount(middlewareStack);
    }

    return this;
};
Ejemplo n.º 9
0
function cleanPath(path) {
    if (!isString(path) || !path || path === "/") {
        return "/";
    } else {
        if (path.charAt(0) !== "/") {
            path = "/" + path;
        }
        if (path.charAt(path.length - 1) === "/") {
            path = path.slice(0, -1);
        }

        return urlPath.normalize(path);
    }
}
Ejemplo n.º 10
0
RouterPrototype.route = function(path) {
    var layers = this.__layers,
        route, stack;

    if (isString(path)) {
        stack = fastSlice(arguments, 1);
    } else {
        stack = fastSlice(arguments);
        path = "/";
    }

    route = new this.Route(path, this, true);
    layers[layers.length] = route;

    if (stack.length !== 0) {
        route.mount(stack);
    }

    return route;
};
Ejemplo n.º 11
0
function buildPath(parent, path) {
    if (!isString(path) || !path || (!parent && path === "/")) {
        return "/";
    } else {
        if (path.charAt(0) === "/") {
            path = path.slice(1);
        }
        if (path.charAt(path.length - 1) === "/") {
            path = path.slice(0, -1);
        }

        if (parent) {
            path = urlPath.resolve(parent.__path, path);
        }

        if (path.charAt(0) !== "/") {
            path = "/" + path;
        }

        return urlPath.normalize(path);
    }
}
Ejemplo n.º 12
0
function request(options) {
    var plugins = request.plugins,
        defer = null,
        xhr, canSetRequestHeader, canOverrideMimeType, isFormData;

    options = defaults(options);

    xhr = new options.XMLHttpRequest(options.xmlHttpRequestOptions);
    canSetRequestHeader = isFunction(xhr.setRequestHeader);
    canOverrideMimeType = isFunction(xhr.overrideMimeType);

    plugins.emit("before", xhr, options);

    isFormData = (supportsFormData && options.data instanceof FormData);

    if (options.isPromise) {
        defer = PromisePolyfill.defer();
    }

    function onSuccess(response) {
        plugins.emit("response", response, xhr, options);
        plugins.emit("load", response, xhr, options);

        if (options.isPromise) {
            defer.resolve(response);
        } else {
            if (!isNull(options.success)) {
                options.success(response);
            }
        }
    }

    function onError(response) {
        plugins.emit("response", response, xhr, options);
        plugins.emit("error", response, xhr, options);

        if (options.isPromise) {
            defer.reject(response);
        } else {
            if (!isNull(options.error)) {
                options.error(response);
            }
        }
    }

    function onComplete() {
        var statusCode = +xhr.status,
            responseText = xhr.responseText,
            response = new Response();

        response.url = xhr.responseURL || options.url;
        response.method = options.method;

        response.statusCode = statusCode;

        response.responseHeaders = xhr.getAllResponseHeaders ? parseResponseHeaders(xhr.getAllResponseHeaders()) : {};
        response.requestHeaders = options.headers ? extend({}, options.headers) : {};

        response.data = null;

        if (responseText) {
            if (options.transformResponse) {
                response.data = options.transformResponse(responseText);
            } else {
                if (parseContentType(response.responseHeaders["Content-Type"]) === "application/json") {
                    try {
                        response.data = JSON.parse(responseText);
                    } catch (e) {
                        response.data = e;
                        onError(response);
                        return;
                    }
                } else if (responseText) {
                    response.data = responseText;
                }
            }
        }

        if ((statusCode > 199 && statusCode < 301) || statusCode === 304) {
            onSuccess(response);
        } else {
            onError(response);
        }
    }

    function onReadyStateChange() {
        switch (+xhr.readyState) {
            case 1:
                plugins.emit("request", xhr, options);
                break;
            case 4:
                onComplete();
                break;
        }
    }

    addEventListener(xhr, "readystatechange", onReadyStateChange);

    if (options.withCredentials && options.async) {
        xhr.withCredentials = options.withCredentials;
    }

    xhr.open(
        options.method,
        options.url,
        options.async,
        options.username,
        options.password
    );

    if (canSetRequestHeader) {
        if (options.headers && options.headers["Content-Type"] && isFormData) {
            delete options.headers["Content-Type"];
        }

        objectForEach(options.headers, function(value, key) {
            if (isString(value)) {
                if (key === "Content-Type" && canOverrideMimeType) {
                    xhr.overrideMimeType(value);
                }
                xhr.setRequestHeader(key, value);
            }
        });
    }

    if (options.transformRequest) {
        options.data = options.transformRequest(options.data);
    } else if (options.data) {
        if (!isString(options.data) && !isFormData) {
            if (options.headers["Content-Type"] === "application/json") {
                options.data = JSON.stringify(options.data);
            } else {
                options.data = options.data + "";
            }
        }
    }

    xhr.send(options.data);

    return isNull(defer) ? undefined : defer.promise;
}