forked from annnhan/imitator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimitator.js
143 lines (122 loc) · 3.63 KB
/
imitator.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
/**
* Created by an.han on 15/7/20.
*/
var express = require('express');
var fs = require('fs');
var path = require('path');
var util = require('./util');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
var app = global.app;
// log proxy data
proxy.on('open', function (proxySocket) {
proxySocket.on('data', function (chunk) {
console.log(chunk.toString());
});
});
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
// 根据参数个数获取配置
function getOption(arg) {
var len = arg.length;
// 默认配置
var option = {
headers: {
'Cache-Control': 'no-cache'
},
statusCode: 200,
cookies: [],
timeout: 0
};
if (len === 0) {
return imitator;
}
else if (len === 1) {
var newOption = arg[0];
if (util.isObject(newOption)) {
util.each(newOption, function (value, key) {
if (key === 'headers') {
util.each(newOption.headers, function (headervalue, headerkey) {
option.headers[headerkey] = newOption.headers[headerkey];
})
}
else {
option[key] = newOption[key];
}
});
}
}
else {
option.url = arg[0];
option.result = arg[1];
}
return option;
}
// 把基于 Imitatorfile 的相对绝对转成绝对路径
function parsePath(value) {
return path.resolve(global.imitatorFilePath, value);
}
/**
* 数据模拟函数
*/
function imitator() {
var option = getOption(arguments);
if (!option.url || !option.result) {
return;
}
// option.action is one of ['get','post','delete','put'...]
var action = option.action || 'use';
app[action](option.url, function (req, res) {
setTimeout(function () {
// set header
res.set(option.headers);
// set Content-Type
option.type && res.type(option.type);
// set status code
res.status(option.statusCode);
// set cookie
util.each(option.cookies, function (item, index) {
var name = item.name;
var value = item.value;
delete item.name;
delete item.value;
res.cookie(name, value, item);
});
// do result
if (util.isFunction(option.result)) {
option.result(req, res);
}
else if (util.isArray(option.result) || util.isObject(option.result)) {
!option.type && res.type('json');
res.json(option.result);
}
else {
!option.type && res.type('text');
res.send(option.result.toString());
}
}, option.timeout);
});
}
// 规则之外的请求转发
imitator.base = function (host) {
process.nextTick(function () {
app.use(function (req, res) {
proxy.web(req, res, {target: host});
});
});
}
// 读取文件内容
imitator.file = function (file) {
return fs.readFileSync(parsePath(file));
}
// 设置静态文件路径
imitator.static = function (url, dir) {
app.use(url, express.static(parsePath(dir)));
}
imitator.jsonp = function (context, callbackName) {
callbackName = callbackName || 'callback';
context = typeof context === 'string' ? context : JSON.stringify(context);
return callbackName + '(' + context + ')';
};
module.exports = imitator;