Source: httpProxy.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
/**
 * HTTP Proxy.
 *
 * @class
 * @name HttpProxy
 * @arg {object} opts - Proxy options.
 * @arg {string} opts.url - URL which should be proxied.
 *  them from cache.
 */

var http = require("http");
var https = require("https");
var url = require("url");
var util = require("util");

require("colors");
var expect = require("chai").expect;
var express = require("express");
var httpProxy = require("http-proxy");
var U = require("glace-utils");

var BaseProxy = require("./baseProxy");
var cache = require("./middleware/cache");
var middleware = require("./middleware");

var LOG = U.logger;

var HttpProxy = function (opts) {

    BaseProxy.call(this, opts);
    this.url = null;

    this._initUrl = null;
    this._server = null;
    this._proxyOptions = {};

    this._proxy = httpProxy.createProxyServer();

    this._proxy.on("proxyReq", (proxyReq, req) => {
        if (req.body) proxyReq.end(req.body);
    });

    this._proxy.on("error", this.__onError.bind(this));

    this.setUrl(opts.url);

    var app = express();
    app.use(async (req, res) => {

        this.req = req;
        if (this.req._reconnect === undefined) {
            this.req._reconnect = this._reconnect;
        };
        this.res = res;

        for (var mw of middleware) if (await mw.call(this)) return;

        delete this.req;
        delete this.res;

        this._proxy.web(req, res, this._proxyOptions);
    });
    this._server = http.createServer(app);
};
util.inherits(HttpProxy, BaseProxy);
module.exports = HttpProxy;
/**
 * Sets proxied URL.
 *
 * @method
 * @arg {string} targetUrl - URL which should be proxied.
 */
HttpProxy.prototype.setUrl = function (targetUrl) {
    this._initUrl = targetUrl;
    var parsedUrl = url.parse(targetUrl);

    expect(["http:", "https:"],
        "Unsupported protocol").include(parsedUrl.protocol);

    // FIXME work around bug in `node-http-proxy`: https://github.com/nodejitsu/node-http-proxy/pull/1074
    this._proxy.options = { proxyTimeout: this._timeout };

    if (parsedUrl.protocol === "https:") {

        this._proxyOptions = {
            target: "https://" + parsedUrl.host,
            agent: https.globalAgent,
            headers: { host: parsedUrl.hostname },
        };
    } else {

        this._proxyOptions = {
            target: {
                host: parsedUrl.hostname,
                port: parsedUrl.port,
            },
        };
    };
};
/**
 * Starts proxy server if it’s not started yet.
 *
 * @async
 * @method
 * @return {Promise<string>} - Proxy URL.
 */
HttpProxy.prototype.start = function () {
    return new Promise((resolve, reject) => {
        if (this.isRunning) return resolve();

        this._server.listen(this._port, err => {
            if (err) return reject(err);

            this._port = this._server.address().port;
            this.url = `http://${U.hostname}:${this._port}`;

            this.isRunning = true;
            resolve();
        });
    })
        .then(() => cache.init())
        .then(() => this.url);
};
/**
 * Stops proxy server if it’s not stopped yet.
 *
 * @method
 */
HttpProxy.prototype.stop = function () {
    if (!this.isRunning) return;
    this._server.close();
    this._proxy.close();
    this.proxyUrl = null;
    this.isRunning = false;
};
/**
 * Helper to be called on proxy error to retry request.
 *
 * @ignore
 * @method
 * @protected
 * @arg {Error} err
 * @arg {Request} req
 * @arg {Response} res
 */
HttpProxy.prototype.__onError = function (err, req, res) {
    if (req._reconnect > 0 && !req.socket.destroyed) {
        LOG.warn(util.format("Request reconnected", U.getReqKey(req)));
        req._reconnect--;
        this._proxy.web(req, res, this._proxyOptions);
    } else {
        LOG.error(util.format(U.getReqKey(req), err));
    };
};