This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
148 lines (141 loc) · 4.22 KB
/
index.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
const fs = require('fs');
const url = require('url');
const pathLib = require('path');
const DEFAULT_OPTIONS = {
mimeType: 'application/json',
};
/**
* isDir is a function which take a path and return true/false
* @param {string} path
* @returns {Boolean}
*/
function isDir(path) {
return fs.lstatSync(path).isDirectory();
}
/**
* mergeHAR can take as many HAR as wanted and merge the log.entries
* @params {Object} har
*/
function mergeHAR(...args) {
const har = {
log: {
entries: [],
}
};
return args.reduce((acc, value) => {
acc.log.entries = acc.log.entries.concat(value.log.entries);
return acc;
}, har);
}
/**
* parse take the path to looking for HAR files and return HAR structure
* @param {string} path
*/
function parse(path) {
let HAR = {
log: {
entries: [],
},
};
try {
if (isDir(path)) {
HAR = fs.readdirSync(path).sort().reduce((acc, folderContent) => {
const subpath = pathLib.join(path, folderContent);
if (isDir(subpath)) {
acc = mergeHAR(acc, parse(subpath));
} else if (subpath.endsWith('.har')) {
try {
acc = mergeHAR(acc, JSON.parse(fs.readFileSync(subpath)));
} catch(e) {
console.error(`can't parse the file ${subpath}`, e);
}
}
return acc;
}, HAR);
} else {
HAR = JSON.parse(fs.readFileSync(path));
}
} catch(e) {
console.error(e);
}
HAR.log.entries.forEach(e => {
e.request.url = url.parse(e.request.url);
});
return HAR;
}
/**
* filter request on method and path.
* Note if it find more than one results it will try to be more restrictive
* using queryString and body payload.
*/
function filter(HAR, req, options=DEFAULT_OPTIONS) {
// lets do a first pass on it.
const entries = HAR.log.entries.filter(e => {
const u = e.request.url;
if (u.pathname !== req.path) {
return false;
}
if (e.request.method !== req.method) {
return false;
}
if (options.mimeType && e.response.content.mimeType !== options.mimeType) {
return false;
}
return true;
});
if (entries.length > 1) {
let results = entries;
// first lets filter on query params
const withSameQueryString = entries.filter(e => {
return e.request.queryString.every(qs => req.params[qs.name] === qs.value);
});
if (withSameQueryString.length > 0) {
results = withSameQueryString;
}
if (results.length > 1 && req.body) {
// then try to filter on body
const withTheSameBody = entries.filter(e => {
const data = e.request.postData;
if (data) {
return req.body === data.text;
}
return false;
});
if (withTheSameBody.length > 0) {
results = withTheSameBody;
}
}
return results;
}
return entries;
}
function getMiddleware(path, options) {
const HAR = parse(path);
const stateFilter = new Map();
return function harMiddleware(req, res, next) {
const found = filter(HAR, req, options);
if (found.length > 0) {
let candidate = found[0].response;
if (found.length > 1) {
// rotation using the stateFilter
const key = `${req.method}-${req.path}`;
let index = stateFilter.get(key) || 0;
if (index + 1 > found.length) {
index = 0;
}
stateFilter.set(key, index + 1); // update state
candidate = found[index].response;
}
if (candidate.status !== 200) {
res.status(candidate.status);
}
res.type(candidate.content.mimeType).send(Buffer.from(candidate.content.text, candidate.content.encoding));
} else {
next();
}
};
}
module.exports = {
parse,
getMiddleware,
}