Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

azuki-trusty / azk   deb

Repository URL to install this package:

Version: 0.5.1 

/ usr / lib / azk / node_modules / q-io / fs2http.js


var Q = require("q");
var HTTP = require("./http");
var URL = require("url");

exports.Client = Client;
function Client(fs) {
    var self = Object.create(Client.prototype);

    self.request = function (request) {
        return Q.when(request, function (request) {
            request = HTTP.normalizeRequest(request);
            var url = URL.parse(request.url);
            if (url.protocol !== "file:") {
                return {
                    status: 404,
                    headers: {},
                    body: ["Can't access protocol " + url.protocol]
                };
            } else {
                var path = url.pathname;
                return fs.open(path, {
                    charset: request.charset
                }).then(function (body) {
                    return {
                        status: 200,
                        headers: {},
                        body: body
                    };
                });
            }
        });
    };

    self.read = function (request, qualifier) {
        qualifier = qualifier || function (response) {
            return response.status === 200;
        };
        return Q.when(exports.request(request), function (response) {
            if (!qualifier(response)){
                var error = new Error("HTTP request failed with code " + response.status);
                error.response = response;
                throw error;
            }
            return Q.invoke(response.body, "read");
        });
    };

    return self;
}

exports.request = function (request) {
    return Q.fcall(require.async || require, "./fs")
    .then(function (fs) {
        return Client(fs).request(request);
    });
};

exports.read = function (request, qualifier) {
    return Q.fcall(require.async || require, "./fs")
    .then(function (fs) {
        return Client(fs).read(request);
    });
};