File size: 1,146 Bytes
026255b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorResponsePlugin = void 0;
const status_code_1 = require("../../status-code");
function isResponseLike(obj) {
    return obj && typeof obj.writeHead === 'function';
}
function isSocketLike(obj) {
    return obj && typeof obj.write === 'function' && !('writeHead' in obj);
}
const errorResponsePlugin = (proxyServer, options) => {
    proxyServer.on('error', (err, req, res, target) => {
        // Re-throw error. Not recoverable since req & res are empty.
        if (!req && !res) {
            throw err; // "Error: Must provide a proper URL as target"
        }
        if (isResponseLike(res)) {
            if (!res.headersSent) {
                const statusCode = (0, status_code_1.getStatusCode)(err.code);
                res.writeHead(statusCode);
            }
            const host = req.headers && req.headers.host;
            res.end(`Error occurred while trying to proxy: ${host}${req.url}`);
        }
        else if (isSocketLike(res)) {
            res.destroy();
        }
    });
};
exports.errorResponsePlugin = errorResponsePlugin;