@stuntman/server 0.1.4 → 0.1.6

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.
Files changed (46) hide show
  1. package/dist/api/api.d.ts +22 -0
  2. package/dist/api/api.js +182 -0
  3. package/dist/api/utils.d.ts +4 -0
  4. package/dist/api/utils.js +60 -0
  5. package/dist/api/validators.d.ts +3 -0
  6. package/dist/api/validators.js +124 -0
  7. package/dist/api/webgui/rules.pug +147 -0
  8. package/dist/api/webgui/style.css +28 -0
  9. package/dist/api/webgui/traffic.pug +37 -0
  10. package/dist/bin/stuntman.d.ts +2 -0
  11. package/dist/bin/stuntman.js +7 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/index.js +5 -0
  14. package/dist/ipUtils.d.ts +17 -0
  15. package/dist/ipUtils.js +101 -0
  16. package/dist/mock.d.ts +30 -0
  17. package/dist/mock.js +327 -0
  18. package/dist/requestContext.d.ts +9 -0
  19. package/dist/requestContext.js +18 -0
  20. package/dist/ruleExecutor.d.ts +22 -0
  21. package/dist/ruleExecutor.js +187 -0
  22. package/dist/rules/catchAll.d.ts +2 -0
  23. package/dist/rules/catchAll.js +15 -0
  24. package/dist/rules/echo.d.ts +2 -0
  25. package/dist/rules/echo.js +15 -0
  26. package/dist/rules/index.d.ts +3 -0
  27. package/dist/rules/index.js +70 -0
  28. package/dist/storage.d.ts +4 -0
  29. package/dist/storage.js +42 -0
  30. package/package.json +8 -5
  31. package/src/api/api.ts +225 -0
  32. package/src/api/utils.ts +69 -0
  33. package/src/api/validators.ts +132 -0
  34. package/src/api/webgui/rules.pug +147 -0
  35. package/src/api/webgui/style.css +28 -0
  36. package/src/api/webgui/traffic.pug +37 -0
  37. package/src/bin/stuntman.ts +8 -0
  38. package/src/index.ts +1 -0
  39. package/src/ipUtils.ts +83 -0
  40. package/src/mock.ts +382 -0
  41. package/src/requestContext.ts +23 -0
  42. package/src/ruleExecutor.ts +211 -0
  43. package/src/rules/catchAll.ts +14 -0
  44. package/src/rules/echo.ts +14 -0
  45. package/src/rules/index.ts +44 -0
  46. package/src/storage.ts +39 -0
@@ -0,0 +1,44 @@
1
+ import fs from 'fs';
2
+ import glob from 'glob';
3
+ import * as tsImport from 'ts-import';
4
+ import { catchAllRule } from './catchAll';
5
+ import { echoRule } from './echo';
6
+ import { serverConfig, logger } from '@stuntman/shared';
7
+ import type * as Stuntman from '@stuntman/shared';
8
+
9
+ export const DEFAULT_RULES: Stuntman.DeployedRule[] = [catchAllRule, echoRule];
10
+ export const CUSTOM_RULES: Stuntman.DeployedRule[] = [];
11
+
12
+ const loadAdditionalRules = () => {
13
+ if (!serverConfig.mock.rulesPath || !fs.existsSync(serverConfig.mock.rulesPath)) {
14
+ logger.debug({ rulesPath: serverConfig.mock.rulesPath }, `additional rules directory not found`);
15
+ return;
16
+ }
17
+ logger.debug({ rulesPath: serverConfig.mock.rulesPath }, `loading additional rules`);
18
+ const filePaths = glob.sync('*.[tj]s', { absolute: true, cwd: serverConfig.mock.rulesPath });
19
+ for (const filePath of filePaths) {
20
+ // TODO add .ts rule support
21
+ try {
22
+ const loadedFile = /\.js$/.test(filePath) ? require(filePath) : tsImport.loadSync(filePath);
23
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
24
+ const exportedRules = (Object.values(loadedFile) as Stuntman.DeployedRule[]).filter((rule) => {
25
+ if (!rule || !rule.id || typeof rule.matches !== 'function') {
26
+ logger.error({ filePath, rule }, 'invalid exported rule');
27
+ return false;
28
+ }
29
+ return true;
30
+ });
31
+ CUSTOM_RULES.push(...exportedRules);
32
+ } catch (error) {
33
+ logger.error({ filePath, error }, 'error importing rule');
34
+ }
35
+ }
36
+ const ruleIds = [...DEFAULT_RULES, ...CUSTOM_RULES].map((rule) => rule.id);
37
+ const duplicatedRuleIds = ruleIds.filter((currentValue, currentIndex) => ruleIds.indexOf(currentValue) !== currentIndex);
38
+ if (duplicatedRuleIds.length > 0) {
39
+ logger.error({ duplicatedRuleIds }, 'duplicated rule ids');
40
+ throw new Error('duplicated rule ids');
41
+ }
42
+ };
43
+
44
+ loadAdditionalRules();
package/src/storage.ts ADDED
@@ -0,0 +1,39 @@
1
+ import LRUCache from 'lru-cache';
2
+ import type * as Stuntman from '@stuntman/shared';
3
+ import sizeof from 'object-sizeof';
4
+
5
+ const DNS_CACHE_OPTIONS: LRUCache.Options<string, string> = {
6
+ max: 1000,
7
+ ttl: 1000 * 60 * 15,
8
+ allowStale: false,
9
+ updateAgeOnGet: false,
10
+ updateAgeOnHas: false,
11
+ };
12
+
13
+ const trafficStoreInstances: Record<string, LRUCache<string, Stuntman.LogEntry>> = {};
14
+ const dnsResolutionCacheInstances: Record<string, LRUCache<string, string>> = {};
15
+
16
+ export const getTrafficStore = (key: string, options?: Stuntman.StorageConfig) => {
17
+ if (!(key in trafficStoreInstances)) {
18
+ if (!options) {
19
+ throw new Error('initialize with options first');
20
+ }
21
+ trafficStoreInstances[key] = new LRUCache<string, Stuntman.LogEntry>({
22
+ max: options.limitCount,
23
+ maxSize: options.limitSize,
24
+ ttl: options.ttl,
25
+ allowStale: false,
26
+ updateAgeOnGet: false,
27
+ updateAgeOnHas: false,
28
+ sizeCalculation: (value) => sizeof(value),
29
+ });
30
+ }
31
+ return trafficStoreInstances[key];
32
+ };
33
+
34
+ export const getDnsResolutionCache = (key: string) => {
35
+ if (!(key in dnsResolutionCacheInstances)) {
36
+ dnsResolutionCacheInstances[key] = new LRUCache<string, string>(DNS_CACHE_OPTIONS);
37
+ }
38
+ return dnsResolutionCacheInstances[key];
39
+ };