@stuntman/server 0.2.4 → 0.3.0
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/README.md +1 -1
- package/dist/api/api.d.ts +1 -1
- package/dist/api/api.d.ts.map +1 -1
- package/dist/api/api.js +53 -54
- package/dist/api/api.js.map +1 -1
- package/dist/api/utils.js +9 -18
- package/dist/api/utils.js.map +1 -1
- package/dist/api/validators.js +38 -43
- package/dist/api/validators.js.map +1 -1
- package/dist/bin/stuntman.js +3 -5
- package/dist/bin/stuntman.js.map +1 -1
- package/dist/index.js +1 -5
- package/dist/index.js.map +1 -1
- package/dist/ipUtils.d.ts +1 -1
- package/dist/ipUtils.d.ts.map +1 -1
- package/dist/ipUtils.js +18 -44
- package/dist/ipUtils.js.map +1 -1
- package/dist/mock.d.ts +1 -1
- package/dist/mock.d.ts.map +1 -1
- package/dist/mock.js +66 -69
- package/dist/mock.js.map +1 -1
- package/dist/requestContext.js +6 -8
- package/dist/requestContext.js.map +1 -1
- package/dist/ruleExecutor.d.ts.map +1 -1
- package/dist/ruleExecutor.js +28 -34
- package/dist/ruleExecutor.js.map +1 -1
- package/dist/rules/catchAll.js +4 -7
- package/dist/rules/catchAll.js.map +1 -1
- package/dist/rules/echo.js +3 -6
- package/dist/rules/echo.js.map +1 -1
- package/dist/rules/index.d.ts.map +1 -1
- package/dist/rules/index.js +3 -7
- package/dist/rules/index.js.map +1 -1
- package/dist/rules/loadRules.d.ts +1 -1
- package/dist/rules/loadRules.d.ts.map +1 -1
- package/dist/rules/loadRules.js +18 -48
- package/dist/rules/loadRules.js.map +1 -1
- package/dist/storage.d.ts +3 -3
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +7 -15
- package/dist/storage.js.map +1 -1
- package/package.json +26 -20
- package/src/api/api.ts +5 -1
- package/src/mock.ts +1 -1
- package/src/ruleExecutor.ts +5 -5
- package/src/rules/index.ts +1 -1
- package/src/rules/loadRules.ts +2 -2
- package/src/storage.ts +2 -2
package/dist/ruleExecutor.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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");
|
|
1
|
+
import { Lock } from 'async-await-mutex-lock';
|
|
2
|
+
import { AppError, DEFAULT_RULE_PRIORITY, HttpCode, errorToLog, logger } from '@stuntman/shared';
|
|
3
|
+
import { CUSTOM_RULES, DEFAULT_RULES } from './rules';
|
|
10
4
|
const ruleExecutors = {};
|
|
11
5
|
const transformMockRuleToLive = (rule) => {
|
|
12
|
-
var _a;
|
|
13
6
|
return {
|
|
14
7
|
...rule,
|
|
15
8
|
counter: 0,
|
|
16
|
-
isEnabled:
|
|
9
|
+
isEnabled: rule.isEnabled ?? true,
|
|
17
10
|
createdTimestamp: Date.now(),
|
|
18
11
|
};
|
|
19
12
|
};
|
|
20
13
|
class RuleExecutor {
|
|
14
|
+
// TODO persistent rule storage maybe
|
|
15
|
+
_rules;
|
|
16
|
+
rulesLock = new Lock();
|
|
21
17
|
get enabledRules() {
|
|
22
18
|
if (!this._rules) {
|
|
23
19
|
this._rules = new Array();
|
|
@@ -25,10 +21,9 @@ class RuleExecutor {
|
|
|
25
21
|
const now = Date.now();
|
|
26
22
|
return this._rules
|
|
27
23
|
.filter((r) => r.isEnabled && (!Number.isFinite(r.ttlSeconds) || r.createdTimestamp + r.ttlSeconds * 1000 > now))
|
|
28
|
-
.sort((a, b) =>
|
|
24
|
+
.sort((a, b) => (a.priority ?? DEFAULT_RULE_PRIORITY) - (b.priority ?? DEFAULT_RULE_PRIORITY));
|
|
29
25
|
}
|
|
30
26
|
constructor(rules) {
|
|
31
|
-
this.rulesLock = new await_lock_1.default();
|
|
32
27
|
this._rules = (rules || []).map(transformMockRuleToLive);
|
|
33
28
|
}
|
|
34
29
|
hasExpired() {
|
|
@@ -39,13 +34,13 @@ class RuleExecutor {
|
|
|
39
34
|
if (!this.hasExpired()) {
|
|
40
35
|
return;
|
|
41
36
|
}
|
|
42
|
-
await this.rulesLock.
|
|
37
|
+
await this.rulesLock.acquire();
|
|
43
38
|
const now = Date.now();
|
|
44
39
|
try {
|
|
45
40
|
this._rules = this._rules.filter((r) => {
|
|
46
41
|
const shouldKeep = !Number.isFinite(r.ttlSeconds) || r.createdTimestamp + r.ttlSeconds * 1000 > now;
|
|
47
42
|
if (!shouldKeep) {
|
|
48
|
-
|
|
43
|
+
logger.debug({ ruleId: r.id }, 'removing expired rule');
|
|
49
44
|
}
|
|
50
45
|
return shouldKeep;
|
|
51
46
|
});
|
|
@@ -56,17 +51,17 @@ class RuleExecutor {
|
|
|
56
51
|
}
|
|
57
52
|
async addRule(rule, overwrite) {
|
|
58
53
|
await this.cleanUpExpired();
|
|
59
|
-
await this.rulesLock.
|
|
54
|
+
await this.rulesLock.acquire();
|
|
60
55
|
try {
|
|
61
56
|
if (this._rules.some((r) => r.id === rule.id)) {
|
|
62
57
|
if (!overwrite) {
|
|
63
|
-
throw new
|
|
58
|
+
throw new AppError({ httpCode: HttpCode.CONFLICT, message: 'rule with given ID already exists' });
|
|
64
59
|
}
|
|
65
60
|
this._removeRule(rule.id);
|
|
66
61
|
}
|
|
67
62
|
const liveRule = transformMockRuleToLive(rule);
|
|
68
63
|
this._rules.push(liveRule);
|
|
69
|
-
|
|
64
|
+
logger.debug(liveRule, 'rule added');
|
|
70
65
|
return liveRule;
|
|
71
66
|
}
|
|
72
67
|
finally {
|
|
@@ -77,14 +72,14 @@ class RuleExecutor {
|
|
|
77
72
|
this._rules = this._rules.filter((r) => {
|
|
78
73
|
const notFound = r.id !== (typeof ruleOrId === 'string' ? ruleOrId : ruleOrId.id);
|
|
79
74
|
if (!notFound) {
|
|
80
|
-
|
|
75
|
+
logger.debug({ ruleId: r.id }, 'rule removed');
|
|
81
76
|
}
|
|
82
77
|
return notFound;
|
|
83
78
|
});
|
|
84
79
|
}
|
|
85
80
|
async removeRule(ruleOrId) {
|
|
86
81
|
await this.cleanUpExpired();
|
|
87
|
-
await this.rulesLock.
|
|
82
|
+
await this.rulesLock.acquire();
|
|
88
83
|
try {
|
|
89
84
|
this._removeRule(ruleOrId);
|
|
90
85
|
}
|
|
@@ -98,7 +93,7 @@ class RuleExecutor {
|
|
|
98
93
|
if (r.id === ruleId) {
|
|
99
94
|
r.counter = 0;
|
|
100
95
|
r.isEnabled = true;
|
|
101
|
-
|
|
96
|
+
logger.debug({ ruleId: r.id }, 'rule enabled');
|
|
102
97
|
}
|
|
103
98
|
});
|
|
104
99
|
}
|
|
@@ -107,7 +102,7 @@ class RuleExecutor {
|
|
|
107
102
|
this._rules.forEach((r) => {
|
|
108
103
|
if (r.id === ruleId) {
|
|
109
104
|
r.isEnabled = false;
|
|
110
|
-
|
|
105
|
+
logger.debug({ ruleId: r.id }, 'rule disabled');
|
|
111
106
|
}
|
|
112
107
|
});
|
|
113
108
|
}
|
|
@@ -119,7 +114,7 @@ class RuleExecutor {
|
|
|
119
114
|
const matchingRule = this.enabledRules.find((rule) => {
|
|
120
115
|
try {
|
|
121
116
|
const matchResult = rule.matches(request);
|
|
122
|
-
|
|
117
|
+
logger.trace({ ...logContext, matchResult }, `rule match attempt for ${rule.id}`);
|
|
123
118
|
if (typeof matchResult === 'boolean') {
|
|
124
119
|
return matchResult;
|
|
125
120
|
}
|
|
@@ -129,17 +124,17 @@ class RuleExecutor {
|
|
|
129
124
|
return matchResult.result;
|
|
130
125
|
}
|
|
131
126
|
catch (error) {
|
|
132
|
-
|
|
127
|
+
logger.error({ ...logContext, ruleId: rule.id, error: errorToLog(error) }, 'error in rule match function');
|
|
133
128
|
}
|
|
134
129
|
return undefined;
|
|
135
130
|
});
|
|
136
131
|
if (!matchingRule) {
|
|
137
|
-
|
|
132
|
+
logger.debug(logContext, 'no matching rule found');
|
|
138
133
|
return null;
|
|
139
134
|
}
|
|
140
135
|
const matchResult = matchingRule.matches(request);
|
|
141
136
|
logContext.ruleId = matchingRule.id;
|
|
142
|
-
|
|
137
|
+
logger.debug({ ...logContext, matchResultMessage: typeof matchResult !== 'boolean' ? matchResult.description : null }, 'found matching rule');
|
|
143
138
|
const matchingRuleClone = Object.freeze(Object.assign({}, matchingRule, {
|
|
144
139
|
labels: matchingRule.labels ? [...(matchingRule.labels || []), ...dynamicLabels] : dynamicLabels,
|
|
145
140
|
}));
|
|
@@ -147,29 +142,29 @@ class RuleExecutor {
|
|
|
147
142
|
logContext.ruleCounter = matchingRule.counter;
|
|
148
143
|
if (Number.isNaN(matchingRule.counter) || !Number.isFinite(matchingRule.counter)) {
|
|
149
144
|
matchingRule.counter = 0;
|
|
150
|
-
|
|
145
|
+
logger.warn(logContext, "it's over 9000!!!");
|
|
151
146
|
}
|
|
152
147
|
if (matchingRule.disableAfterUse) {
|
|
153
148
|
if (matchingRule.disableAfterUse === true || matchingRule.disableAfterUse <= matchingRule.counter) {
|
|
154
|
-
|
|
149
|
+
logger.debug(logContext, 'disabling rule for future requests');
|
|
155
150
|
matchingRule.isEnabled = false;
|
|
156
151
|
}
|
|
157
152
|
}
|
|
158
153
|
if (matchingRule.removeAfterUse) {
|
|
159
154
|
if (matchingRule.removeAfterUse === true || matchingRule.removeAfterUse <= matchingRule.counter) {
|
|
160
|
-
|
|
155
|
+
logger.debug(logContext, 'removing rule for future requests');
|
|
161
156
|
await this.removeRule(matchingRule);
|
|
162
157
|
}
|
|
163
158
|
}
|
|
164
159
|
if (typeof matchResult !== 'boolean') {
|
|
165
160
|
if (matchResult.disableRuleIds && matchResult.disableRuleIds.length > 0) {
|
|
166
|
-
|
|
161
|
+
logger.debug({ ...logContext, disableRuleIds: matchResult.disableRuleIds }, 'disabling rules based on matchResult');
|
|
167
162
|
for (const ruleId of matchResult.disableRuleIds) {
|
|
168
163
|
this.disableRule(ruleId);
|
|
169
164
|
}
|
|
170
165
|
}
|
|
171
166
|
if (matchResult.enableRuleIds && matchResult.enableRuleIds.length > 0) {
|
|
172
|
-
|
|
167
|
+
logger.debug({ ...logContext, disableRuleIds: matchResult.enableRuleIds }, 'enabling rules based on matchResult');
|
|
173
168
|
for (const ruleId of matchResult.enableRuleIds) {
|
|
174
169
|
this.enableRule(ruleId);
|
|
175
170
|
}
|
|
@@ -186,16 +181,15 @@ class RuleExecutor {
|
|
|
186
181
|
return this._rules.find((r) => r.id === id);
|
|
187
182
|
}
|
|
188
183
|
}
|
|
189
|
-
const getRuleExecutor = (mockUuid, overrideRules) => {
|
|
184
|
+
export const getRuleExecutor = (mockUuid, overrideRules) => {
|
|
190
185
|
if (!ruleExecutors[mockUuid]) {
|
|
191
186
|
if (overrideRules === null) {
|
|
192
187
|
ruleExecutors[mockUuid] = new RuleExecutor();
|
|
193
188
|
}
|
|
194
189
|
else {
|
|
195
|
-
ruleExecutors[mockUuid] = new RuleExecutor((overrideRules
|
|
190
|
+
ruleExecutors[mockUuid] = new RuleExecutor((overrideRules ?? [...DEFAULT_RULES, ...CUSTOM_RULES]).map((r) => ({ ...r, ttlSeconds: Infinity })));
|
|
196
191
|
}
|
|
197
192
|
}
|
|
198
193
|
return ruleExecutors[mockUuid];
|
|
199
194
|
};
|
|
200
|
-
exports.getRuleExecutor = getRuleExecutor;
|
|
201
195
|
//# sourceMappingURL=ruleExecutor.js.map
|
package/dist/ruleExecutor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ruleExecutor.js","sourceRoot":"","sources":["../src/ruleExecutor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ruleExecutor.js","sourceRoot":"","sources":["../src/ruleExecutor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,qBAAqB,EAAE,QAAQ,EAAE,UAAU,EAAE,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,CAAC;QAC3B,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;YACzB,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,YAAY,EAAE,CAAC;QACjD,CAAC;aAAM,CAAC;YACJ,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,YAAY,CACtC,CAAC,aAAa,IAAI,CAAC,GAAG,aAAa,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC,CACtG,CAAC;QACN,CAAC;IACL,CAAC;IACD,OAAO,aAAa,CAAC,QAAQ,CAAE,CAAC;AACpC,CAAC,CAAC"}
|
package/dist/rules/catchAll.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const shared_1 = require("@stuntman/shared");
|
|
5
|
-
exports.catchAllRule = {
|
|
6
|
-
id: shared_1.CATCH_RULE_NAME,
|
|
1
|
+
import { CATCH_ALL_RULE_PRIORITY, CATCH_RULE_NAME } from '@stuntman/shared';
|
|
2
|
+
export const catchAllRule = {
|
|
3
|
+
id: CATCH_RULE_NAME,
|
|
7
4
|
matches: () => true,
|
|
8
|
-
priority:
|
|
5
|
+
priority: CATCH_ALL_RULE_PRIORITY,
|
|
9
6
|
actions: {
|
|
10
7
|
mockResponse: (req) => ({
|
|
11
8
|
body: `Request received by Stuntman mock <pre>${JSON.stringify(req, null, 4)}</pre>`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catchAll.js","sourceRoot":"","sources":["../../src/rules/catchAll.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"catchAll.js","sourceRoot":"","sources":["../../src/rules/catchAll.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAG5E,MAAM,CAAC,MAAM,YAAY,GAA0B;IAC/C,EAAE,EAAE,eAAe;IACnB,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI;IACnB,QAAQ,EAAE,uBAAuB;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"}
|
package/dist/rules/echo.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.echoRule = void 0;
|
|
4
|
-
const shared_1 = require("@stuntman/shared");
|
|
5
|
-
exports.echoRule = {
|
|
1
|
+
import { DEFAULT_RULE_PRIORITY } from '@stuntman/shared';
|
|
2
|
+
export const echoRule = {
|
|
6
3
|
id: 'internal/echo',
|
|
7
|
-
priority:
|
|
4
|
+
priority: DEFAULT_RULE_PRIORITY + 1,
|
|
8
5
|
matches: (req) => /https?:\/\/echo\/.*/.test(req.url),
|
|
9
6
|
actions: {
|
|
10
7
|
mockResponse: (req) => ({
|
package/dist/rules/echo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"echo.js","sourceRoot":"","sources":["../../src/rules/echo.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"echo.js","sourceRoot":"","sources":["../../src/rules/echo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAGzD,MAAM,CAAC,MAAM,QAAQ,GAA0B;IAC3C,EAAE,EAAE,eAAe;IACnB,QAAQ,EAAE,qBAAqB,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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,eAAO,MAAM,YAAY,EAAE,QAAQ,CAAC,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,eAAO,MAAM,YAAY,EAAE,QAAQ,CAAC,YAAY,EAAsB,CAAC"}
|
package/dist/rules/index.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const loadRules_1 = require("./loadRules");
|
|
5
|
-
var loadRules_2 = require("./loadRules");
|
|
6
|
-
Object.defineProperty(exports, "DEFAULT_RULES", { enumerable: true, get: function () { return loadRules_2.DEFAULT_RULES; } });
|
|
7
|
-
exports.CUSTOM_RULES = (0, loadRules_1.loadRules)();
|
|
1
|
+
import { loadRules } from './loadRules';
|
|
2
|
+
export { DEFAULT_RULES } from './loadRules';
|
|
3
|
+
export const CUSTOM_RULES = await loadRules();
|
|
8
4
|
//# sourceMappingURL=index.js.map
|
package/dist/rules/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,CAAC,MAAM,YAAY,GAA4B,MAAM,SAAS,EAAE,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type * as Stuntman from '@stuntman/shared';
|
|
2
2
|
export declare const DEFAULT_RULES: Stuntman.DeployedRule[];
|
|
3
|
-
export declare const loadRules: () => Stuntman.DeployedRule[]
|
|
3
|
+
export declare const loadRules: () => Promise<Stuntman.DeployedRule[]>;
|
|
4
4
|
//# sourceMappingURL=loadRules.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadRules.d.ts","sourceRoot":"","sources":["../../src/rules/loadRules.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,YAAY,EAA6B,CAAC;AAE/E,eAAO,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"loadRules.d.ts","sourceRoot":"","sources":["../../src/rules/loadRules.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,YAAY,EAA6B,CAAC;AAE/E,eAAO,MAAM,SAAS,QAAa,QAAQ,SAAS,YAAY,EAAE,CA2CjE,CAAC"}
|
package/dist/rules/loadRules.js
CHANGED
|
@@ -1,61 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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.loadRules = exports.DEFAULT_RULES = void 0;
|
|
30
|
-
const fs_1 = __importDefault(require("fs"));
|
|
31
|
-
const glob_1 = 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
|
-
const loadRules = () => {
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { globSync } from 'glob';
|
|
3
|
+
import * as tsImport from 'ts-import';
|
|
4
|
+
import { catchAllRule } from './catchAll';
|
|
5
|
+
import { echoRule } from './echo';
|
|
6
|
+
import { stuntmanConfig, logger, errorToLog } from '@stuntman/shared';
|
|
7
|
+
export const DEFAULT_RULES = [catchAllRule, echoRule];
|
|
8
|
+
export const loadRules = async () => {
|
|
38
9
|
const customRules = [];
|
|
39
|
-
if (!
|
|
40
|
-
|
|
10
|
+
if (!stuntmanConfig.mock.rulesPath || !fs.existsSync(stuntmanConfig.mock.rulesPath)) {
|
|
11
|
+
logger.debug({ rulesPath: stuntmanConfig.mock.rulesPath }, `additional rules directory not found`);
|
|
41
12
|
return [];
|
|
42
13
|
}
|
|
43
|
-
|
|
44
|
-
const filePaths =
|
|
14
|
+
logger.debug({ rulesPath: stuntmanConfig.mock.rulesPath }, `loading additional rules`);
|
|
15
|
+
const filePaths = globSync('*.[tj]s', { absolute: true, cwd: stuntmanConfig.mock.rulesPath });
|
|
45
16
|
const tsImportCompiledFileNames = filePaths.map((p) => p.replace(/\.[^/.]+$/u, '.js'));
|
|
46
17
|
const tsImportDuplicatedCompiledFileNames = tsImportCompiledFileNames.filter((currentValue, currentIndex) => tsImportCompiledFileNames.indexOf(currentValue) !== currentIndex);
|
|
47
18
|
if (tsImportDuplicatedCompiledFileNames.length > 0) {
|
|
48
|
-
|
|
19
|
+
logger.error({ tsImportDuplicatedCompiledFileNames }, 'duplicated compiled file names https://github.com/radarsu/ts-import/issues/32');
|
|
49
20
|
throw new Error('duplicated compiled file names https://github.com/radarsu/ts-import/issues/32');
|
|
50
21
|
}
|
|
51
22
|
for (const filePath of filePaths) {
|
|
52
23
|
// TODO add .ts rule support
|
|
53
24
|
try {
|
|
54
|
-
const loadedFile = tsImport.
|
|
25
|
+
const loadedFile = await tsImport.load(filePath, { useCache: false });
|
|
55
26
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
56
27
|
const exportedRules = Object.values(loadedFile).filter((rule) => {
|
|
57
28
|
if (!rule || !rule.id || typeof rule.matches !== 'function') {
|
|
58
|
-
|
|
29
|
+
logger.error({ filePath, rule }, 'invalid exported rule');
|
|
59
30
|
return false;
|
|
60
31
|
}
|
|
61
32
|
return true;
|
|
@@ -63,16 +34,15 @@ const loadRules = () => {
|
|
|
63
34
|
customRules.push(...exportedRules);
|
|
64
35
|
}
|
|
65
36
|
catch (error) {
|
|
66
|
-
|
|
37
|
+
logger.error({ filePath, error: errorToLog(error) }, 'error importing rule');
|
|
67
38
|
}
|
|
68
39
|
}
|
|
69
|
-
const ruleIds = [...
|
|
40
|
+
const ruleIds = [...DEFAULT_RULES, ...customRules].map((rule) => rule.id);
|
|
70
41
|
const duplicatedRuleIds = ruleIds.filter((currentValue, currentIndex) => ruleIds.indexOf(currentValue) !== currentIndex);
|
|
71
42
|
if (duplicatedRuleIds.length > 0) {
|
|
72
|
-
|
|
43
|
+
logger.error({ duplicatedRuleIds }, 'duplicated rule ids');
|
|
73
44
|
throw new Error('duplicated rule ids');
|
|
74
45
|
}
|
|
75
46
|
return customRules;
|
|
76
47
|
};
|
|
77
|
-
exports.loadRules = loadRules;
|
|
78
48
|
//# sourceMappingURL=loadRules.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadRules.js","sourceRoot":"","sources":["../../src/rules/loadRules.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"loadRules.js","sourceRoot":"","sources":["../../src/rules/loadRules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,KAAK,QAAQ,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAGtE,MAAM,CAAC,MAAM,aAAa,GAA4B,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;AAE/E,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,IAAsC,EAAE;IAClE,MAAM,WAAW,GAA4B,EAAE,CAAC;IAChD,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QAClF,MAAM,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,cAAc,CAAC,IAAI,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EAAE,CAAC;oBAC1D,MAAM,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,uBAAuB,CAAC,CAAC;oBAC1D,OAAO,KAAK,CAAC;gBACjB,CAAC;gBACD,OAAO,IAAI,CAAC;YAChB,CAAC,CAAC,CAAC;YACH,WAAW,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,KAAc,CAAC,EAAE,EAAE,sBAAsB,CAAC,CAAC;QAC1F,CAAC;IACL,CAAC;IACD,MAAM,OAAO,GAAG,CAAC,GAAG,aAAa,EAAE,GAAG,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1E,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,CAAC;QAC/B,MAAM,CAAC,KAAK,CAAC,EAAE,iBAAiB,EAAE,EAAE,qBAAqB,CAAC,CAAC;QAC3D,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,WAAW,CAAC;AACvB,CAAC,CAAC"}
|
package/dist/storage.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import LRUCache from 'lru-cache';
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
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>;
|
|
3
|
+
export declare const getTrafficStore: (key: string, options?: Stuntman.StorageConfig) => LRUCache<string, Stuntman.LogEntry, unknown>;
|
|
4
|
+
export declare const getDnsResolutionCache: (key: string) => LRUCache<string, string, unknown>;
|
|
5
5
|
//# sourceMappingURL=storage.d.ts.map
|
package/dist/storage.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAclD,eAAO,MAAM,eAAe,QAAS,MAAM,YAAY,SAAS,aAAa,iDAgB5E,CAAC;AAEF,eAAO,MAAM,qBAAqB,QAAS,MAAM,sCAKhD,CAAC"}
|
package/dist/storage.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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"));
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import sizeof from 'object-sizeof';
|
|
9
3
|
const DNS_CACHE_OPTIONS = {
|
|
10
4
|
max: 1000,
|
|
11
5
|
ttl: 1000 * 60 * 15,
|
|
@@ -15,29 +9,27 @@ const DNS_CACHE_OPTIONS = {
|
|
|
15
9
|
};
|
|
16
10
|
const trafficStoreInstances = {};
|
|
17
11
|
const dnsResolutionCacheInstances = {};
|
|
18
|
-
const getTrafficStore = (key, options) => {
|
|
12
|
+
export const getTrafficStore = (key, options) => {
|
|
19
13
|
if (!(key in trafficStoreInstances)) {
|
|
20
14
|
if (!options) {
|
|
21
15
|
throw new Error('initialize with options first');
|
|
22
16
|
}
|
|
23
|
-
trafficStoreInstances[key] = new
|
|
17
|
+
trafficStoreInstances[key] = new LRUCache({
|
|
24
18
|
max: options.limitCount,
|
|
25
19
|
maxSize: options.limitSize,
|
|
26
20
|
ttl: options.ttl,
|
|
27
21
|
allowStale: false,
|
|
28
22
|
updateAgeOnGet: false,
|
|
29
23
|
updateAgeOnHas: false,
|
|
30
|
-
sizeCalculation: (value) => (
|
|
24
|
+
sizeCalculation: (value) => sizeof(value),
|
|
31
25
|
});
|
|
32
26
|
}
|
|
33
27
|
return trafficStoreInstances[key];
|
|
34
28
|
};
|
|
35
|
-
|
|
36
|
-
const getDnsResolutionCache = (key) => {
|
|
29
|
+
export const getDnsResolutionCache = (key) => {
|
|
37
30
|
if (!(key in dnsResolutionCacheInstances)) {
|
|
38
|
-
dnsResolutionCacheInstances[key] = new
|
|
31
|
+
dnsResolutionCacheInstances[key] = new LRUCache(DNS_CACHE_OPTIONS);
|
|
39
32
|
}
|
|
40
33
|
return dnsResolutionCacheInstances[key];
|
|
41
34
|
};
|
|
42
|
-
exports.getDnsResolutionCache = getDnsResolutionCache;
|
|
43
35
|
//# sourceMappingURL=storage.js.map
|
package/dist/storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC,OAAO,MAAM,MAAM,eAAe,CAAC;AAEnC,MAAM,iBAAiB,GAAQ;IAC3B,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,I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"0.3.0",
|
|
4
4
|
"description": "Stuntman - HTTP proxy / mock server with API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "https://github.com/andrzej-woof/stuntman.git"
|
|
@@ -32,31 +33,31 @@
|
|
|
32
33
|
"author": "Andrzej Pasterczyk",
|
|
33
34
|
"license": "MIT",
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"@stuntman/shared": "^0.
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"object-sizeof": "2.6.1",
|
|
36
|
+
"@stuntman/shared": "^0.3.0",
|
|
37
|
+
"express": "5.0.0-beta.3",
|
|
38
|
+
"glob": "10.3.12",
|
|
39
|
+
"lru-cache": "10.2.0",
|
|
40
|
+
"object-sizeof": "2.6.4",
|
|
41
41
|
"pug": "3.0.2",
|
|
42
|
-
"serialize-javascript": "6.0.
|
|
43
|
-
"ts-import": "
|
|
42
|
+
"serialize-javascript": "6.0.2",
|
|
43
|
+
"ts-import": "5.0.0-beta.0",
|
|
44
44
|
"ts-jest": "^29.1.2",
|
|
45
|
-
"undici": "
|
|
46
|
-
"uuid": "9.0.
|
|
45
|
+
"undici": "6.11.1",
|
|
46
|
+
"uuid": "9.0.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@jest-mock/express": "^2.0.
|
|
50
|
-
"@jest/globals": "29.
|
|
51
|
-
"@prettier/plugin-pug": "
|
|
52
|
-
"@types/express": "4.17.
|
|
49
|
+
"@jest-mock/express": "^2.0.2",
|
|
50
|
+
"@jest/globals": "29.7.0",
|
|
51
|
+
"@prettier/plugin-pug": "3.0.0",
|
|
52
|
+
"@types/express": "4.17.21",
|
|
53
53
|
"@types/glob": "8.1.0",
|
|
54
54
|
"@types/jest": "^29.5.12",
|
|
55
|
-
"@types/serialize-javascript": "5.0.
|
|
56
|
-
"@types/uuid": "9.0.
|
|
55
|
+
"@types/serialize-javascript": "5.0.4",
|
|
56
|
+
"@types/uuid": "9.0.8",
|
|
57
|
+
"async-await-mutex-lock": "^1.0.11",
|
|
57
58
|
"jest": "29.7.0",
|
|
58
|
-
"prettier": "2.
|
|
59
|
-
"typescript": "5.4.
|
|
59
|
+
"prettier": "3.2.5",
|
|
60
|
+
"typescript": "5.4.3"
|
|
60
61
|
},
|
|
61
62
|
"bin": {
|
|
62
63
|
"stuntman": "./dist/bin/stuntman.js"
|
|
@@ -68,8 +69,13 @@
|
|
|
68
69
|
"LICENSE",
|
|
69
70
|
"CHANGELOG.md"
|
|
70
71
|
],
|
|
72
|
+
"overrides": {
|
|
73
|
+
"ts-import": {
|
|
74
|
+
"typescript": "5.4.3"
|
|
75
|
+
}
|
|
76
|
+
},
|
|
71
77
|
"scripts": {
|
|
72
|
-
"test": "NODE_ENV=test LOG_LEVEL=silent SUPPRESS_NO_CONFIG_WARNING=1 jest --coverage",
|
|
78
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test LOG_LEVEL=silent NODE_CONFIG_STRICT_MODE= SUPPRESS_NO_CONFIG_WARNING=1 jest --coverage",
|
|
73
79
|
"clean": "rm -fr dist",
|
|
74
80
|
"build": "tsc && cp -rv src/api/webgui dist/api",
|
|
75
81
|
"lint": "prettier --check \"./{src,test}/**/*\" && eslint \"./{src,test}/**/*\"",
|
package/src/api/api.ts
CHANGED
|
@@ -7,14 +7,18 @@ import { logger, AppError, HttpCode, MAX_RULE_TTL_SECONDS, stringify, INDEX_DTS,
|
|
|
7
7
|
import type * as Stuntman from '@stuntman/shared';
|
|
8
8
|
import { RequestContext } from '../requestContext';
|
|
9
9
|
import serializeJavascript from 'serialize-javascript';
|
|
10
|
-
import LRUCache from 'lru-cache';
|
|
10
|
+
import { LRUCache } from 'lru-cache';
|
|
11
11
|
import { validateDeserializedRule } from './validators';
|
|
12
12
|
import { deserializeRule, escapedSerialize, liveRuleToRule } from './utils';
|
|
13
|
+
import { dirname } from 'path';
|
|
14
|
+
import { fileURLToPath } from 'url';
|
|
13
15
|
|
|
14
16
|
type ApiOptions = Stuntman.ApiConfig & {
|
|
15
17
|
mockUuid: string;
|
|
16
18
|
};
|
|
17
19
|
|
|
20
|
+
const __dirname = dirname(fileURLToPath(import.meta.url)).replace(/\/src$/, '/dist');
|
|
21
|
+
|
|
18
22
|
const API_KEY_HEADER = 'x-api-key';
|
|
19
23
|
|
|
20
24
|
export class API {
|
package/src/mock.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { RawHeaders, logger, HttpCode, naiveGQLParser, escapeStringRegexp, error
|
|
|
10
10
|
import { RequestContext } from './requestContext';
|
|
11
11
|
import type * as Stuntman from '@stuntman/shared';
|
|
12
12
|
import { IPUtils } from './ipUtils';
|
|
13
|
-
import LRUCache from 'lru-cache';
|
|
13
|
+
import { LRUCache } from 'lru-cache';
|
|
14
14
|
import { API } from './api/api';
|
|
15
15
|
|
|
16
16
|
// TODO add proper web proxy mode
|