-
Notifications
You must be signed in to change notification settings - Fork 754
/
handleProxyOption.ts
45 lines (35 loc) · 1.02 KB
/
handleProxyOption.ts
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
import * as url from 'url';
import {Map} from 'immutable';
import {BrowsersyncProxy} from "../../types";
import {BsTempOptions, TransformResult} from "../cli-options";
export function handleProxyOption(incoming: BsTempOptions): TransformResult {
let value = incoming.get('proxy');
let mw;
let target;
if (!value || value === true) {
return [incoming, []];
}
if (typeof value !== "string") {
target = value.get("target");
mw = value.get("middleware");
} else {
target = value;
value = Map({});
}
if (!target.match(/^(https?):\/\//)) {
target = "http://" + target;
}
const parsedUrl = url.parse(target);
if (!parsedUrl.port) {
parsedUrl.port = "80";
}
const out: BrowsersyncProxy = {
target: parsedUrl.protocol + "//" + parsedUrl.host,
url: Map(parsedUrl)
};
if (mw) {
out.middleware = mw;
}
const proxyOutput = value.mergeDeep(out);
return [incoming.set('proxy', proxyOutput), []];
}