@stuntman/shared 0.1.1 → 0.1.3

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/config.js CHANGED
@@ -6,18 +6,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.serverConfig = void 0;
7
7
  const _1 = require(".");
8
8
  const config_1 = __importDefault(require("config"));
9
- const defaults_1 = __importDefault(require("defaults"));
9
+ const path_1 = __importDefault(require("path"));
10
10
  // TODO safeguards & defaults
11
11
  const defaultConfig = {
12
12
  api: {
13
13
  disabled: false,
14
14
  port: _1.DEFAULT_API_PORT,
15
+ apiKeyReadOnly: null,
16
+ apiKeyReadWrite: null,
15
17
  },
16
18
  mock: {
17
19
  domain: _1.DEFAULT_MOCK_DOMAIN,
18
20
  externalDns: _1.EXTERNAL_DNS,
19
21
  port: _1.DEFAULT_MOCK_PORT,
20
22
  timeout: _1.DEFAULT_PROXY_TIMEOUT,
23
+ rulesPath: path_1.default.join(process.cwd(), 'rules'),
21
24
  },
22
25
  storage: {
23
26
  traffic: {
@@ -30,11 +33,13 @@ const defaultConfig = {
30
33
  disabled: false,
31
34
  },
32
35
  };
36
+ config_1.default.util.setModuleDefaults('stuntman', defaultConfig);
33
37
  let configFromFile = {};
34
38
  try {
35
39
  configFromFile = config_1.default.get('stuntman');
36
40
  }
37
41
  catch (error) {
38
- console.warn('unable to find correct config');
42
+ // eslint-disable-next-line no-console
43
+ console.warn('unable to find correct config - starting with defaults');
39
44
  }
40
- exports.serverConfig = (0, defaults_1.default)(configFromFile, defaultConfig);
45
+ exports.serverConfig = configFromFile;
package/dist/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export type RuleMatchResult = boolean | {
28
28
  result: boolean;
29
29
  enableRuleIds?: string[];
30
30
  disableRuleIds?: string[];
31
+ description?: string;
31
32
  };
32
33
  export type RemotableFunction<T extends Function> = {
33
34
  localFn: T;
@@ -122,6 +123,8 @@ export type WebGuiConfig = {
122
123
  export type ApiConfig = {
123
124
  port: number;
124
125
  disabled: boolean;
126
+ apiKeyReadWrite: string | null;
127
+ apiKeyReadOnly: string | null;
125
128
  };
126
129
  export type ClientConfig = {
127
130
  protocol?: 'http' | 'https';
@@ -137,6 +140,7 @@ export type MockConfig = {
137
140
  httpsCert?: string;
138
141
  timeout: number;
139
142
  externalDns: string[];
143
+ rulesPath: string;
140
144
  };
141
145
  export type StorageConfig = {
142
146
  limitCount: number;
@@ -151,3 +155,12 @@ export type ServerConfig = {
151
155
  traffic: StorageConfig;
152
156
  };
153
157
  };
158
+ export interface RuleExecutorInterface {
159
+ addRule: (rule: Rule, overwrite?: boolean) => Promise<LiveRule>;
160
+ removeRule: (id: string) => Promise<void>;
161
+ enableRule: (id: string) => void;
162
+ disableRule: (id: string) => void;
163
+ findMatchingRule: (request: Request) => Promise<LiveRule | null>;
164
+ getRules: () => Promise<readonly LiveRule[]>;
165
+ getRule: (id: string) => Promise<LiveRule | undefined>;
166
+ }
@@ -7,5 +7,6 @@ export declare class RawHeaders extends Array<string> implements Stuntman.RawHea
7
7
  remove(name: string): void;
8
8
  toHeaderPairs(): readonly [string, string][];
9
9
  static fromHeaderPairs(headerPairs: [string, string][]): RawHeaders;
10
+ static fromHeadersRecord(headersRecord: Record<string, string | string[] | undefined>): RawHeaders;
10
11
  static toHeaderPairs(rawHeaders: string[]): readonly [string, string][];
11
12
  }
@@ -59,6 +59,19 @@ class RawHeaders extends Array {
59
59
  static fromHeaderPairs(headerPairs) {
60
60
  return new RawHeaders(...headerPairs.flatMap((x) => x));
61
61
  }
62
+ static fromHeadersRecord(headersRecord) {
63
+ const output = new RawHeaders();
64
+ for (const [key, value] of Object.entries(headersRecord)) {
65
+ if (typeof value === 'string' || value === undefined) {
66
+ output.add(key, value !== null && value !== void 0 ? value : '');
67
+ continue;
68
+ }
69
+ for (const subValue of value) {
70
+ output.add(key, subValue);
71
+ }
72
+ }
73
+ return output;
74
+ }
62
75
  static toHeaderPairs(rawHeaders) {
63
76
  const headers = new Array();
64
77
  for (let headerIndex = 0; headerIndex < rawHeaders.length; headerIndex += 2) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stuntman/shared",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Stuntman - HTTP proxy / mock shared types and utils",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -33,18 +33,16 @@
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
35
  "config": "3.3.9",
36
- "defaults": "1.0.4",
37
36
  "pino": "8.10.0"
38
37
  },
39
38
  "devDependencies": {
40
- "@types/config": "3.3.0",
41
- "@types/defaults": "1.0.3"
39
+ "@types/config": "3.3.0"
42
40
  },
43
41
  "scripts": {
44
42
  "test": "echo \"Error: no test specified\" && exit 1",
45
43
  "clean": "rm -fr dist",
46
44
  "build": "tsc",
47
45
  "lint": "prettier --check . && eslint . --ext ts",
48
- "lint:fix": "prettier --write ./src && eslint ./src --ext ts --fix"
46
+ "lint:fix": "prettier --write ./{src,test} && eslint ./{src,test} --ext ts --fix"
49
47
  }
50
48
  }
package/src/config.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import {
2
- RecursivePartial,
3
2
  ServerConfig,
4
3
  DEFAULT_API_PORT,
5
4
  DEFAULT_MOCK_DOMAIN,
@@ -11,7 +10,7 @@ import {
11
10
  DEFAULT_CACHE_TTL,
12
11
  } from '.';
13
12
  import config from 'config';
14
- import defaults from 'defaults';
13
+ import path from 'path';
15
14
 
16
15
  // TODO safeguards & defaults
17
16
 
@@ -19,12 +18,15 @@ const defaultConfig: ServerConfig = {
19
18
  api: {
20
19
  disabled: false,
21
20
  port: DEFAULT_API_PORT,
21
+ apiKeyReadOnly: null,
22
+ apiKeyReadWrite: null,
22
23
  },
23
24
  mock: {
24
25
  domain: DEFAULT_MOCK_DOMAIN,
25
26
  externalDns: EXTERNAL_DNS,
26
27
  port: DEFAULT_MOCK_PORT,
27
28
  timeout: DEFAULT_PROXY_TIMEOUT,
29
+ rulesPath: path.join(process.cwd(), 'rules'),
28
30
  },
29
31
  storage: {
30
32
  traffic: {
@@ -38,11 +40,14 @@ const defaultConfig: ServerConfig = {
38
40
  },
39
41
  };
40
42
 
41
- let configFromFile: RecursivePartial<ServerConfig> = {};
43
+ config.util.setModuleDefaults('stuntman', defaultConfig);
44
+
45
+ let configFromFile = {} as ServerConfig;
42
46
  try {
43
- configFromFile = config.get<RecursivePartial<ServerConfig>>('stuntman');
47
+ configFromFile = config.get<ServerConfig>('stuntman');
44
48
  } catch (error) {
45
- console.warn('unable to find correct config');
49
+ // eslint-disable-next-line no-console
50
+ console.warn('unable to find correct config - starting with defaults');
46
51
  }
47
52
 
48
- export const serverConfig = defaults(configFromFile, defaultConfig) as ServerConfig;
53
+ export const serverConfig = configFromFile;
package/src/index.ts CHANGED
@@ -42,7 +42,9 @@ export enum HttpCode {
42
42
 
43
43
  export type LocalVariables = Record<string, SerializableTypes>;
44
44
 
45
- export type RuleMatchResult = boolean | { result: boolean; enableRuleIds?: string[]; disableRuleIds?: string[] };
45
+ export type RuleMatchResult =
46
+ | boolean
47
+ | { result: boolean; enableRuleIds?: string[]; disableRuleIds?: string[]; description?: string };
46
48
 
47
49
  export type RemotableFunction<T extends Function> = {
48
50
  localFn: T;
@@ -164,6 +166,8 @@ export type WebGuiConfig = {
164
166
  export type ApiConfig = {
165
167
  port: number;
166
168
  disabled: boolean;
169
+ apiKeyReadWrite: string | null;
170
+ apiKeyReadOnly: string | null;
167
171
  };
168
172
 
169
173
  export type ClientConfig = {
@@ -181,6 +185,7 @@ export type MockConfig = {
181
185
  httpsCert?: string;
182
186
  timeout: number;
183
187
  externalDns: string[];
188
+ rulesPath: string;
184
189
  };
185
190
 
186
191
  export type StorageConfig = {
@@ -197,3 +202,13 @@ export type ServerConfig = {
197
202
  traffic: StorageConfig;
198
203
  };
199
204
  };
205
+
206
+ export interface RuleExecutorInterface {
207
+ addRule: (rule: Rule, overwrite?: boolean) => Promise<LiveRule>;
208
+ removeRule: (id: string) => Promise<void>;
209
+ enableRule: (id: string) => void;
210
+ disableRule: (id: string) => void;
211
+ findMatchingRule: (request: Request) => Promise<LiveRule | null>;
212
+ getRules: () => Promise<readonly LiveRule[]>;
213
+ getRule: (id: string) => Promise<LiveRule | undefined>;
214
+ }
package/src/rawHeaders.ts CHANGED
@@ -65,6 +65,20 @@ export class RawHeaders extends Array<string> implements Stuntman.RawHeadersInte
65
65
  return new RawHeaders(...headerPairs.flatMap((x) => x));
66
66
  }
67
67
 
68
+ static fromHeadersRecord(headersRecord: Record<string, string | string[] | undefined>): RawHeaders {
69
+ const output = new RawHeaders();
70
+ for (const [key, value] of Object.entries(headersRecord)) {
71
+ if (typeof value === 'string' || value === undefined) {
72
+ output.add(key, value ?? '');
73
+ continue;
74
+ }
75
+ for (const subValue of value) {
76
+ output.add(key, subValue);
77
+ }
78
+ }
79
+ return output;
80
+ }
81
+
68
82
  static toHeaderPairs(rawHeaders: string[]): readonly [string, string][] {
69
83
  const headers = new Array<[string, string]>();
70
84
  for (let headerIndex = 0; headerIndex < rawHeaders.length; headerIndex += 2) {
@@ -0,0 +1 @@
1
+ // TODO
package/tsconfig.json CHANGED
@@ -11,6 +11,12 @@
11
11
  "skipLibCheck": true,
12
12
  "typeRoots": ["node_modules/@types"],
13
13
  "allowJs": true,
14
- "moduleResolution": "node16"
15
- }
14
+ "moduleResolution": "node16",
15
+ "types": ["node", "jest"]
16
+ },
17
+ "exclude": [
18
+ "jest.config.js",
19
+ "./dist/**",
20
+ "./test/**"
21
+ ]
16
22
  }