-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.js
135 lines (117 loc) · 4.01 KB
/
proxy.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
#! /usr/bin/env node
const ramda = require('ramda');
const https = require('https');
const http = require('http');
const url = require('url');
const httpProxy = require('http-proxy');
const fs = require('fs');
const path = require('path');
const proxy = httpProxy.createProxyServer({});
const noop = () => undefined;
let mockData;
let mockFn = (url, params, mock) => mock;
let responseMode;
const vorpal = require('vorpal')();
const httpModes = {
error: {
statusCode: 202,
status: 'Error'
},
success: {
statusCode: 200,
status: 'Ok'
}
};
vorpal
.command('start <api>', 'starts the API mocking proxy server')
.option('--mode <string>', 'specify mode for proxy server (success or error, default is success)')
.option('--mockFile <mock.json>', 'specify json mock file')
.option('--mockScript <mock.js>', 'specify postprocessing file. receives req params')
.option('--port <number>', 'specify port for proxy server')
.types({
string: ['api', 'm', 'mockFile', 'mode']
})
.action(function(args, callback) {
const { port = 5050, mockFile = 'mock.json', mockScript, mode = 'success' } = args.options;
const { api } = args;
let mockFileMessege = '';
responseMode = mode;
try {
mockData = JSON.parse(fs.readFileSync(mockFile));
} catch (e) {
exitWithError(`${mockFile} not found`)
}
if (mockScript) {
try {
const mockPath = path.resolve(process.cwd(), mockScript);
mockFn = require(mockPath);
mockFileMessege = `\n Mock file loaded ${mockPath}`;
} catch (e) {
exitWithError(`${mockScript} not found`)
}
}
fs.watchFile(mockFile, { encoding: 'buffer' }, noop).on('change', () => {
try {
mockData = JSON.parse(fs.readFileSync(mockFile));
} catch (e) {
console.error('cant parse mock file', e);
}
});
const server = http.createServer((req, res) => {
mock(req, res, () => {
proxy.web(req, res, {
agent: https.globalAgent,
headers: { host: url.parse(api).host },
target: api
})
});
});
console.log(`listening on port ${port}.\n API (${api}).\n Mock data (${path.resolve(mockFile)}) \n Mode ${responseMode} ${mockFileMessege}`);
server.listen(port);
});
vorpal
.parse(process.argv);
function exitWithError(msg) {
console.log(`error: ${msg}`);
console.log('use: aproxy --help');
process.exit(1);
}
function methodPath(url) {
return url.split('?')[0].split('/').filter(x => x);
}
function getReqParams(request) {
return new Promise(resolve => {
if (request.method === 'POST') {
let body = '';
request.on('data', data => body += data);
request.on('end', () => resolve(JSON.parse(body)));
} else {
resolve('');
}
});
}
function mock(request, response, next) {
const result = ramda.path(methodPath(request.url), mockData);
if (!result) {
next();
return;
}
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Request-Method', '*');
response.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
response.setHeader('Access-Control-Allow-Headers', '*');
response.setHeader('Content-Type', 'application/json');
if (request.method === 'OPTIONS') {
response.writeHead(httpModes[responseMode].statusCode);
response.end();
} else {
getReqParams(request).then(params => {
response.writeHead(httpModes[responseMode].statusCode);
response.end(JSON.stringify({
payload: mockFn(request.url, params, result),
status: httpModes[responseMode].status
}))
});
}
console.log(`info: ${request.url} has been proxied`);
}