@stuntman/server 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/api.d.ts +25 -0
- package/dist/api/api.d.ts.map +1 -0
- package/dist/api/api.js +212 -0
- package/dist/api/api.js.map +1 -0
- package/dist/api/utils.d.ts +5 -0
- package/dist/api/utils.d.ts.map +1 -0
- package/dist/api/utils.js +61 -0
- package/dist/api/utils.js.map +1 -0
- package/dist/api/validators.d.ts +4 -0
- package/dist/api/validators.d.ts.map +1 -0
- package/dist/api/validators.js +125 -0
- package/dist/api/validators.js.map +1 -0
- package/dist/api/webgui/rules.pug +147 -0
- package/dist/api/webgui/style.css +28 -0
- package/dist/api/webgui/traffic.pug +38 -0
- package/dist/bin/stuntman.d.ts +3 -0
- package/dist/bin/stuntman.d.ts.map +1 -0
- package/dist/bin/stuntman.js +8 -0
- package/dist/bin/stuntman.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/ipUtils.d.ts +18 -0
- package/dist/ipUtils.d.ts.map +1 -0
- package/dist/ipUtils.js +102 -0
- package/dist/ipUtils.js.map +1 -0
- package/dist/mock.d.ts +32 -0
- package/dist/mock.d.ts.map +1 -0
- package/dist/mock.js +306 -0
- package/dist/mock.js.map +1 -0
- package/dist/requestContext.d.ts +10 -0
- package/dist/requestContext.d.ts.map +1 -0
- package/dist/requestContext.js +19 -0
- package/dist/requestContext.js.map +1 -0
- package/dist/ruleExecutor.d.ts +23 -0
- package/dist/ruleExecutor.d.ts.map +1 -0
- package/dist/ruleExecutor.js +189 -0
- package/dist/ruleExecutor.js.map +1 -0
- package/dist/rules/catchAll.d.ts +3 -0
- package/dist/rules/catchAll.d.ts.map +1 -0
- package/dist/rules/catchAll.js +16 -0
- package/dist/rules/catchAll.js.map +1 -0
- package/dist/rules/echo.d.ts +3 -0
- package/dist/rules/echo.d.ts.map +1 -0
- package/dist/rules/echo.js +16 -0
- package/dist/rules/echo.js.map +1 -0
- package/dist/rules/index.d.ts +4 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +71 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/storage.d.ts +5 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +43 -0
- package/dist/storage.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type * as Stuntman from '@stuntman/shared';
|
|
2
|
+
declare class RuleExecutor implements Stuntman.RuleExecutorInterface {
|
|
3
|
+
private _rules;
|
|
4
|
+
private rulesLock;
|
|
5
|
+
private get enabledRules();
|
|
6
|
+
constructor(rules?: Stuntman.Rule[]);
|
|
7
|
+
private hasExpired;
|
|
8
|
+
private cleanUpExpired;
|
|
9
|
+
addRule(rule: Stuntman.Rule, overwrite?: boolean): Promise<Stuntman.LiveRule>;
|
|
10
|
+
private _removeRule;
|
|
11
|
+
removeRule(id: string): Promise<void>;
|
|
12
|
+
removeRule(rule: Stuntman.Rule): Promise<void>;
|
|
13
|
+
enableRule(id: string): void;
|
|
14
|
+
enableRule(rule: Stuntman.Rule): void;
|
|
15
|
+
disableRule(id: string): void;
|
|
16
|
+
disableRule(rule: Stuntman.Rule): void;
|
|
17
|
+
findMatchingRule(request: Stuntman.Request): Promise<Stuntman.LiveRule | null>;
|
|
18
|
+
getRules(): Promise<readonly Stuntman.LiveRule[]>;
|
|
19
|
+
getRule(id: string): Promise<Stuntman.LiveRule | undefined>;
|
|
20
|
+
}
|
|
21
|
+
export declare const getRuleExecutor: (mockUuid: string) => RuleExecutor;
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=ruleExecutor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ruleExecutor.d.ts","sourceRoot":"","sources":["../src/ruleExecutor.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,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ule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getRuleExecutor = void 0;
|
|
7
|
+
const await_lock_1 = __importDefault(require("await-lock"));
|
|
8
|
+
const shared_1 = require("@stuntman/shared");
|
|
9
|
+
const rules_1 = require("./rules");
|
|
10
|
+
const ruleExecutors = {};
|
|
11
|
+
const transformMockRuleToLive = (rule) => {
|
|
12
|
+
var _a;
|
|
13
|
+
return {
|
|
14
|
+
...rule,
|
|
15
|
+
counter: 0,
|
|
16
|
+
isEnabled: (_a = rule.isEnabled) !== null && _a !== void 0 ? _a : true,
|
|
17
|
+
createdTimestamp: Date.now(),
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
class RuleExecutor {
|
|
21
|
+
get enabledRules() {
|
|
22
|
+
if (!this._rules) {
|
|
23
|
+
return new Array();
|
|
24
|
+
}
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
return this._rules
|
|
27
|
+
.filter((r) => (r.isEnabled && !Number.isFinite(r.ttlSeconds)) || r.createdTimestamp + r.ttlSeconds * 1000 > now)
|
|
28
|
+
.sort((a, b) => { var _a, _b; return ((_a = a.priority) !== null && _a !== void 0 ? _a : shared_1.DEFAULT_RULE_PRIORITY) - ((_b = b.priority) !== null && _b !== void 0 ? _b : shared_1.DEFAULT_RULE_PRIORITY); });
|
|
29
|
+
}
|
|
30
|
+
constructor(rules) {
|
|
31
|
+
this.rulesLock = new await_lock_1.default();
|
|
32
|
+
this._rules = (rules || []).map(transformMockRuleToLive);
|
|
33
|
+
}
|
|
34
|
+
hasExpired() {
|
|
35
|
+
const now = Date.now();
|
|
36
|
+
return this._rules.some((r) => Number.isFinite(r.ttlSeconds) && r.createdTimestamp + r.ttlSeconds * 1000 < now);
|
|
37
|
+
}
|
|
38
|
+
async cleanUpExpired() {
|
|
39
|
+
if (!this.hasExpired()) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
await this.rulesLock.acquireAsync();
|
|
43
|
+
const now = Date.now();
|
|
44
|
+
try {
|
|
45
|
+
this._rules = this._rules.filter((r) => {
|
|
46
|
+
const shouldKeep = !Number.isFinite(r.ttlSeconds) || r.createdTimestamp + r.ttlSeconds * 1000 > now;
|
|
47
|
+
if (!shouldKeep) {
|
|
48
|
+
shared_1.logger.debug({ ruleId: r.id }, 'removing expired rule');
|
|
49
|
+
}
|
|
50
|
+
return shouldKeep;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
await this.rulesLock.release();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async addRule(rule, overwrite) {
|
|
58
|
+
await this.cleanUpExpired();
|
|
59
|
+
await this.rulesLock.acquireAsync();
|
|
60
|
+
try {
|
|
61
|
+
if (this._rules.some((r) => r.id === rule.id)) {
|
|
62
|
+
if (!overwrite) {
|
|
63
|
+
throw new shared_1.AppError({ httpCode: shared_1.HttpCode.CONFLICT, message: 'rule with given ID already exists' });
|
|
64
|
+
}
|
|
65
|
+
this._removeRule(rule.id);
|
|
66
|
+
}
|
|
67
|
+
const liveRule = transformMockRuleToLive(rule);
|
|
68
|
+
this._rules.push(liveRule);
|
|
69
|
+
shared_1.logger.debug(liveRule, 'rule added');
|
|
70
|
+
return liveRule;
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await this.rulesLock.release();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
_removeRule(ruleOrId) {
|
|
77
|
+
this._rules = this._rules.filter((r) => {
|
|
78
|
+
const notFound = r.id !== (typeof ruleOrId === 'string' ? ruleOrId : ruleOrId.id);
|
|
79
|
+
if (!notFound) {
|
|
80
|
+
shared_1.logger.debug({ ruleId: r.id }, 'rule removed');
|
|
81
|
+
}
|
|
82
|
+
return notFound;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
async removeRule(ruleOrId) {
|
|
86
|
+
await this.cleanUpExpired();
|
|
87
|
+
await this.rulesLock.acquireAsync();
|
|
88
|
+
try {
|
|
89
|
+
this._removeRule(ruleOrId);
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
await this.rulesLock.release();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
enableRule(ruleOrId) {
|
|
96
|
+
this._rules.forEach((r) => {
|
|
97
|
+
if (r.id === (typeof ruleOrId === 'string' ? ruleOrId : ruleOrId.id)) {
|
|
98
|
+
r.counter = 0;
|
|
99
|
+
r.isEnabled = true;
|
|
100
|
+
shared_1.logger.debug({ ruleId: r.id }, 'rule enabled');
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
disableRule(ruleOrId) {
|
|
105
|
+
this._rules.forEach((r) => {
|
|
106
|
+
if (r.id === (typeof ruleOrId === 'string' ? ruleOrId : ruleOrId.id)) {
|
|
107
|
+
r.isEnabled = false;
|
|
108
|
+
shared_1.logger.debug({ ruleId: r.id }, 'rule disabled');
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async findMatchingRule(request) {
|
|
113
|
+
const logContext = {
|
|
114
|
+
requestId: request.id,
|
|
115
|
+
};
|
|
116
|
+
const matchingRule = this.enabledRules.find((rule) => {
|
|
117
|
+
try {
|
|
118
|
+
const matchResult = rule.matches(request);
|
|
119
|
+
shared_1.logger.trace({ ...logContext, matchResult }, `rule match attempt for ${rule.id}`);
|
|
120
|
+
if (typeof matchResult === 'boolean') {
|
|
121
|
+
return matchResult;
|
|
122
|
+
}
|
|
123
|
+
return matchResult.result;
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
shared_1.logger.error({ ...logContext, ruleId: rule === null || rule === void 0 ? void 0 : rule.id, error }, 'error in rule match function');
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
});
|
|
130
|
+
if (!matchingRule) {
|
|
131
|
+
shared_1.logger.debug(logContext, 'no matching rule found');
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const matchResult = matchingRule.matches(request);
|
|
135
|
+
logContext.ruleId = matchingRule.id;
|
|
136
|
+
shared_1.logger.debug({ ...logContext, matchResultMessage: typeof matchResult !== 'boolean' ? matchResult.description : null }, 'found matching rule');
|
|
137
|
+
const matchingRuleClone = Object.freeze(Object.assign({}, matchingRule));
|
|
138
|
+
++matchingRule.counter;
|
|
139
|
+
logContext.ruleCounter = matchingRule.counter;
|
|
140
|
+
if (Number.isNaN(matchingRule.counter) || !Number.isFinite(matchingRule.counter)) {
|
|
141
|
+
matchingRule.counter = 0;
|
|
142
|
+
shared_1.logger.warn(logContext, "it's over 9000!!!");
|
|
143
|
+
}
|
|
144
|
+
// TODO check if that works
|
|
145
|
+
if (matchingRule.disableAfterUse) {
|
|
146
|
+
if (typeof matchingRule.disableAfterUse === 'boolean' || matchingRule.disableAfterUse <= matchingRule.counter) {
|
|
147
|
+
shared_1.logger.debug(logContext, 'disabling rule for future requests');
|
|
148
|
+
this.disableRule(matchingRule);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (matchingRule.removeAfterUse) {
|
|
152
|
+
if (typeof matchingRule.removeAfterUse === 'boolean' || matchingRule.removeAfterUse <= matchingRule.counter) {
|
|
153
|
+
shared_1.logger.debug(logContext, 'removing rule for future requests');
|
|
154
|
+
await this.removeRule(matchingRule);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (typeof matchResult !== 'boolean') {
|
|
158
|
+
if (matchResult.disableRuleIds && matchResult.disableRuleIds.length > 0) {
|
|
159
|
+
shared_1.logger.debug({ ...logContext, disableRuleIds: matchResult.disableRuleIds }, 'disabling rules based on matchResult');
|
|
160
|
+
for (const ruleId of matchResult.disableRuleIds) {
|
|
161
|
+
this.disableRule(ruleId);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (matchResult.enableRuleIds && matchResult.enableRuleIds.length > 0) {
|
|
165
|
+
shared_1.logger.debug({ ...logContext, disableRuleIds: matchResult.disableRuleIds }, 'enabling rules based on matchResult');
|
|
166
|
+
for (const ruleId of matchResult.enableRuleIds) {
|
|
167
|
+
this.enableRule(ruleId);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return matchingRuleClone;
|
|
172
|
+
}
|
|
173
|
+
async getRules() {
|
|
174
|
+
await this.cleanUpExpired();
|
|
175
|
+
return this._rules;
|
|
176
|
+
}
|
|
177
|
+
async getRule(id) {
|
|
178
|
+
await this.cleanUpExpired();
|
|
179
|
+
return this._rules.find((r) => r.id === id);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const getRuleExecutor = (mockUuid) => {
|
|
183
|
+
if (!ruleExecutors[mockUuid]) {
|
|
184
|
+
ruleExecutors[mockUuid] = new RuleExecutor([...rules_1.DEFAULT_RULES, ...rules_1.CUSTOM_RULES].map((r) => ({ ...r, ttlSeconds: Infinity })));
|
|
185
|
+
}
|
|
186
|
+
return ruleExecutors[mockUuid];
|
|
187
|
+
};
|
|
188
|
+
exports.getRuleExecutor = getRuleExecutor;
|
|
189
|
+
//# sourceMappingURL=ruleExecutor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ruleExecutor.js","sourceRoot":"","sources":["../src/ruleExecutor.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAmC;AACnC,6CAAqF;AAErF,mCAAsD;AAEtD,MAAM,aAAa,GAAiC,EAAE,CAAC;AAEvD,MAAM,uBAAuB,GAAG,CAAC,IAAmB,EAAqB,EAAE;;IACvE,OAAO;QACH,GAAG,IAAI;QACP,OAAO,EAAE,CAAC;QACV,SAAS,EAAE,MAAA,IAAI,CAAC,SAAS,mCAAI,IAAI;QACjC,gBAAgB,EAAE,IAAI,CAAC,GAAG,EAAE;KAC/B,CAAC;AACN,CAAC,CAAC;AAEF,MAAM,YAAY;IAKd,IAAY,YAAY;QACpB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YACd,OAAO,IAAI,KAAK,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AG,CAAC,QAAgB,EAAgB,EAAE;IAC9D,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE;QAC1B,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,YAAY,CACtC,CAAC,GAAG,qBAAa,EAAE,GAAG,oBAAY,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC,CACnF,CAAC;KACL;IACD,OAAO,aAAa,CAAC,QAAQ,CAAE,CAAC;AACpC,CAAC,CAAC;AAPW,QAAA,eAAe,mBAO1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catchAll.d.ts","sourceRoot":"","sources":["../../src/rules/catchAll.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,YAAY,EAAE,QAAQ,CAAC,YAUnC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.catchAllRule = void 0;
|
|
4
|
+
const shared_1 = require("@stuntman/shared");
|
|
5
|
+
exports.catchAllRule = {
|
|
6
|
+
id: shared_1.CATCH_RULE_NAME,
|
|
7
|
+
matches: () => true,
|
|
8
|
+
priority: shared_1.CATCH_ALL_RULE_PRIORITY,
|
|
9
|
+
actions: {
|
|
10
|
+
mockResponse: (req) => ({
|
|
11
|
+
body: `Request received by Stuntman mock <pre>${JSON.stringify(req, null, 4)}</pre>`,
|
|
12
|
+
status: 200,
|
|
13
|
+
}),
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=catchAll.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catchAll.js","sourceRoot":"","sources":["../../src/rules/catchAll.ts"],"names":[],"mappings":";;;AAAA,6CAA4E;AAG/D,QAAA,YAAY,GAA0B;IAC/C,EAAE,EAAE,wBAAe;IACnB,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI;IACnB,QAAQ,EAAE,gCAAuB;IACjC,OAAO,EAAE;QACL,YAAY,EAAE,CAAC,GAAqB,EAAE,EAAE,CAAC,CAAC;YACtC,IAAI,EAAE,0CAA0C,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,QAAQ;YACpF,MAAM,EAAE,GAAG;SACd,CAAC;KACL;CACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"echo.d.ts","sourceRoot":"","sources":["../../src/rules/echo.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,QAAQ,EAAE,QAAQ,CAAC,YAU/B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.echoRule = void 0;
|
|
4
|
+
const shared_1 = require("@stuntman/shared");
|
|
5
|
+
exports.echoRule = {
|
|
6
|
+
id: 'internal/echo',
|
|
7
|
+
priority: shared_1.DEFAULT_RULE_PRIORITY + 1,
|
|
8
|
+
matches: (req) => /https?:\/\/echo\/.*/.test(req.url),
|
|
9
|
+
actions: {
|
|
10
|
+
mockResponse: (req) => ({
|
|
11
|
+
body: req,
|
|
12
|
+
status: 200,
|
|
13
|
+
}),
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=echo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"echo.js","sourceRoot":"","sources":["../../src/rules/echo.ts"],"names":[],"mappings":";;;AAAA,6CAAyD;AAG5C,QAAA,QAAQ,GAA0B;IAC3C,EAAE,EAAE,eAAe;IACnB,QAAQ,EAAE,8BAAqB,GAAG,CAAC;IACnC,OAAO,EAAE,CAAC,GAAqB,EAAE,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACvE,OAAO,EAAE;QACL,YAAY,EAAE,CAAC,GAAqB,EAAE,EAAE,CAAC,CAAC;YACtC,IAAI,EAAE,GAAG;YACT,MAAM,EAAE,GAAG;SACd,CAAC;KACL;CACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,YAAY,EAA6B,CAAC;AAC/E,eAAO,MAAM,YAAY,EAAE,QAAQ,CAAC,YAAY,EAAO,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.CUSTOM_RULES = exports.DEFAULT_RULES = void 0;
|
|
30
|
+
const fs_1 = __importDefault(require("fs"));
|
|
31
|
+
const glob_1 = __importDefault(require("glob"));
|
|
32
|
+
const tsImport = __importStar(require("ts-import"));
|
|
33
|
+
const catchAll_1 = require("./catchAll");
|
|
34
|
+
const echo_1 = require("./echo");
|
|
35
|
+
const shared_1 = require("@stuntman/shared");
|
|
36
|
+
exports.DEFAULT_RULES = [catchAll_1.catchAllRule, echo_1.echoRule];
|
|
37
|
+
exports.CUSTOM_RULES = [];
|
|
38
|
+
const loadAdditionalRules = () => {
|
|
39
|
+
if (!shared_1.stuntmanConfig.mock.rulesPath || !fs_1.default.existsSync(shared_1.stuntmanConfig.mock.rulesPath)) {
|
|
40
|
+
shared_1.logger.debug({ rulesPath: shared_1.stuntmanConfig.mock.rulesPath }, `additional rules directory not found`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
shared_1.logger.debug({ rulesPath: shared_1.stuntmanConfig.mock.rulesPath }, `loading additional rules`);
|
|
44
|
+
const filePaths = glob_1.default.sync('*.[tj]s', { absolute: true, cwd: shared_1.stuntmanConfig.mock.rulesPath });
|
|
45
|
+
for (const filePath of filePaths) {
|
|
46
|
+
// TODO add .ts rule support
|
|
47
|
+
try {
|
|
48
|
+
const loadedFile = /\.js$/.test(filePath) ? require(filePath) : tsImport.loadSync(filePath);
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
50
|
+
const exportedRules = Object.values(loadedFile).filter((rule) => {
|
|
51
|
+
if (!rule || !rule.id || typeof rule.matches !== 'function') {
|
|
52
|
+
shared_1.logger.error({ filePath, rule }, 'invalid exported rule');
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
});
|
|
57
|
+
exports.CUSTOM_RULES.push(...exportedRules);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
shared_1.logger.error({ filePath, error }, 'error importing rule');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const ruleIds = [...exports.DEFAULT_RULES, ...exports.CUSTOM_RULES].map((rule) => rule.id);
|
|
64
|
+
const duplicatedRuleIds = ruleIds.filter((currentValue, currentIndex) => ruleIds.indexOf(currentValue) !== currentIndex);
|
|
65
|
+
if (duplicatedRuleIds.length > 0) {
|
|
66
|
+
shared_1.logger.error({ duplicatedRuleIds }, 'duplicated rule ids');
|
|
67
|
+
throw new Error('duplicated rule ids');
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
loadAdditionalRules();
|
|
71
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,oDAAsC;AACtC,yCAA0C;AAC1C,iCAAkC;AAClC,6CAA0D;AAG7C,QAAA,aAAa,GAA4B,CAAC,uBAAY,EAAE,eAAQ,CAAC,CAAC;AAClE,QAAA,YAAY,GAA4B,EAAE,CAAC;AAExD,MAAM,mBAAmB,GAAG,GAAG,EAAE;IAC7B,IAAI,CAAC,uBAAc,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,uBAAc,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;QACjF,eAAM,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,uBAAc,CAAC,IAAI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CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC3E,MAAM,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,YAAY,EAAE,YAAY,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,YAAY,CAAC,CAAC;IACzH,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;QAC9B,eAAM,CAAC,KAAK,CAAC,EAAE,iBAAiB,EAAE,EAAE,qBAAqB,CAAC,CAAC;QAC3D,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;KAC1C;AACL,CAAC,CAAC;AAEF,mBAAmB,EAAE,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import LRUCache from 'lru-cache';
|
|
2
|
+
import type * as Stuntman from '@stuntman/shared';
|
|
3
|
+
export declare const getTrafficStore: (key: string, options?: Stuntman.StorageConfig) => LRUCache<string, Stuntman.LogEntry>;
|
|
4
|
+
export declare const getDnsResolutionCache: (key: string) => LRUCache<string, string>;
|
|
5
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAclD,eAAO,MAAM,eAAe,QAAS,MAAM,YAAY,SAAS,aAAa,wCAgB5E,CAAC;AAEF,eAAO,MAAM,qBAAqB,QAAS,MAAM,6BAKhD,CAAC"}
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getDnsResolutionCache = exports.getTrafficStore = void 0;
|
|
7
|
+
const lru_cache_1 = __importDefault(require("lru-cache"));
|
|
8
|
+
const object_sizeof_1 = __importDefault(require("object-sizeof"));
|
|
9
|
+
const DNS_CACHE_OPTIONS = {
|
|
10
|
+
max: 1000,
|
|
11
|
+
ttl: 1000 * 60 * 15,
|
|
12
|
+
allowStale: false,
|
|
13
|
+
updateAgeOnGet: false,
|
|
14
|
+
updateAgeOnHas: false,
|
|
15
|
+
};
|
|
16
|
+
const trafficStoreInstances = {};
|
|
17
|
+
const dnsResolutionCacheInstances = {};
|
|
18
|
+
const getTrafficStore = (key, options) => {
|
|
19
|
+
if (!(key in trafficStoreInstances)) {
|
|
20
|
+
if (!options) {
|
|
21
|
+
throw new Error('initialize with options first');
|
|
22
|
+
}
|
|
23
|
+
trafficStoreInstances[key] = new lru_cache_1.default({
|
|
24
|
+
max: options.limitCount,
|
|
25
|
+
maxSize: options.limitSize,
|
|
26
|
+
ttl: options.ttl,
|
|
27
|
+
allowStale: false,
|
|
28
|
+
updateAgeOnGet: false,
|
|
29
|
+
updateAgeOnHas: false,
|
|
30
|
+
sizeCalculation: (value) => (0, object_sizeof_1.default)(value),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return trafficStoreInstances[key];
|
|
34
|
+
};
|
|
35
|
+
exports.getTrafficStore = getTrafficStore;
|
|
36
|
+
const getDnsResolutionCache = (key) => {
|
|
37
|
+
if (!(key in dnsResolutionCacheInstances)) {
|
|
38
|
+
dnsResolutionCacheInstances[key] = new lru_cache_1.default(DNS_CACHE_OPTIONS);
|
|
39
|
+
}
|
|
40
|
+
return dnsResolutionCacheInstances[key];
|
|
41
|
+
};
|
|
42
|
+
exports.getDnsResolutionCache = getDnsResolutionCache;
|
|
43
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stuntman/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Stuntman - HTTP proxy / mock server with API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"CHANGELOG.md"
|
|
67
67
|
],
|
|
68
68
|
"scripts": {
|
|
69
|
-
"test": "SUPPRESS_NO_CONFIG_WARNING=1 jest",
|
|
69
|
+
"test": "SUPPRESS_NO_CONFIG_WARNING=1 jest --coverage",
|
|
70
70
|
"clean": "rm -fr dist",
|
|
71
71
|
"build": "tsc && cp -rv src/api/webgui dist/api",
|
|
72
72
|
"lint": "prettier --check \"./{src,test}/**/*\" && eslint \"./{src,test}/**/*\"",
|