@stuntman/server 0.1.3 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stuntman/server",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Stuntman - HTTP proxy / mock server with API",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
package/dist/api/api.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /// <reference types="node" />
2
- import http from 'http';
3
- import { NextFunction, Request, Response, Express as ExpressServer } from 'express';
4
- import type * as Stuntman from '@stuntman/shared';
5
- import LRUCache from 'lru-cache';
6
- type ApiOptions = Stuntman.ApiConfig & {
7
- mockUuid: string;
8
- };
9
- export declare class API {
10
- protected options: Required<ApiOptions>;
11
- protected apiApp: ExpressServer;
12
- trafficStore: LRUCache<string, Stuntman.LogEntry>;
13
- server: http.Server | null;
14
- auth: (req: Request, type: 'read' | 'write') => void;
15
- authReadOnly: (req: Request, res: Response, next: NextFunction) => void;
16
- authReadWrite: (req: Request, res: Response, next: NextFunction) => void;
17
- constructor(options: ApiOptions, webGuiOptions?: Stuntman.WebGuiConfig);
18
- private initWebGui;
19
- start(): void;
20
- stop(): void;
21
- }
22
- export {};
package/dist/api/api.js DELETED
@@ -1,185 +0,0 @@
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.API = void 0;
7
- const express_1 = __importDefault(require("express"));
8
- const uuid_1 = require("uuid");
9
- const storage_1 = require("../storage");
10
- const ruleExecutor_1 = require("../ruleExecutor");
11
- const shared_1 = require("@stuntman/shared");
12
- const requestContext_1 = __importDefault(require("../requestContext"));
13
- const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
14
- const validators_1 = require("./validators");
15
- const utils_1 = require("./utils");
16
- const API_KEY_HEADER = 'x-api-key';
17
- class API {
18
- constructor(options, webGuiOptions) {
19
- this.server = null;
20
- if (!options.apiKeyReadOnly !== !options.apiKeyReadWrite) {
21
- throw new Error('apiKeyReadOnly and apiKeyReadWrite options need to be set either both or none');
22
- }
23
- this.options = options;
24
- this.trafficStore = (0, storage_1.getTrafficStore)(this.options.mockUuid);
25
- this.apiApp = (0, express_1.default)();
26
- this.apiApp.use(express_1.default.json());
27
- this.apiApp.use(express_1.default.text());
28
- this.auth = (req, type) => {
29
- const hasValidReadKey = req.header(API_KEY_HEADER) === this.options.apiKeyReadOnly;
30
- const hasValidWriteKey = req.header(API_KEY_HEADER) === this.options.apiKeyReadWrite;
31
- const hasValidKey = type === 'read' ? hasValidReadKey || hasValidWriteKey : hasValidWriteKey;
32
- if (!hasValidKey) {
33
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.UNAUTHORIZED, message: 'unauthorized' });
34
- }
35
- return;
36
- };
37
- this.authReadOnly = (req, res, next) => {
38
- this.auth(req, 'read');
39
- next();
40
- };
41
- this.authReadWrite = (req, res, next) => {
42
- this.auth(req, 'write');
43
- next();
44
- };
45
- this.apiApp.use((req, res, next) => {
46
- requestContext_1.default.bind(req, this.options.mockUuid);
47
- next();
48
- });
49
- this.apiApp.get('/rule', this.authReadOnly, async (req, res) => {
50
- res.send((0, shared_1.stringify)(await (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).getRules()));
51
- });
52
- this.apiApp.get('/rule/:ruleId', this.authReadOnly, async (req, res) => {
53
- res.send((0, shared_1.stringify)(await (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).getRule(req.params.ruleId)));
54
- });
55
- this.apiApp.get('/rule/:ruleId/disable', this.authReadWrite, (req, res) => {
56
- (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).disableRule(req.params.ruleId);
57
- res.send();
58
- });
59
- this.apiApp.get('/rule/:ruleId/enable', this.authReadWrite, (req, res) => {
60
- (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).enableRule(req.params.ruleId);
61
- res.send();
62
- });
63
- this.apiApp.post('/rule', this.authReadWrite, async (req, res) => {
64
- const deserializedRule = (0, utils_1.deserializeRule)(req.body);
65
- (0, validators_1.validateDeserializedRule)(deserializedRule);
66
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
67
- // @ts-ignore
68
- const rule = await (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).addRule(deserializedRule);
69
- res.send((0, shared_1.stringify)(rule));
70
- });
71
- this.apiApp.get('/rule/:ruleId/remove', this.authReadWrite, async (req, res) => {
72
- await (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).removeRule(req.params.ruleId);
73
- res.send();
74
- });
75
- this.apiApp.get('/traffic', this.authReadOnly, (req, res) => {
76
- const serializedTraffic = {};
77
- for (const [key, value] of this.trafficStore.entries()) {
78
- serializedTraffic[key] = value;
79
- }
80
- res.json(serializedTraffic);
81
- });
82
- this.apiApp.get('/traffic/:ruleIdOrLabel', this.authReadOnly, (req, res) => {
83
- const serializedTraffic = {};
84
- for (const [key, value] of this.trafficStore.entries()) {
85
- if (value.mockRuleId === req.params.ruleIdOrLabel || (value.labels || []).includes(req.params.ruleIdOrLabel)) {
86
- serializedTraffic[key] = value;
87
- }
88
- }
89
- res.json(serializedTraffic);
90
- });
91
- if (!(webGuiOptions === null || webGuiOptions === void 0 ? void 0 : webGuiOptions.disabled)) {
92
- this.apiApp.set('views', __dirname + '/webgui');
93
- this.apiApp.set('view engine', 'pug');
94
- this.initWebGui();
95
- }
96
- this.apiApp.all(/.*/, (req, res) => res.status(404).send());
97
- this.apiApp.use((error, req, res) => {
98
- const ctx = requestContext_1.default.get(req);
99
- const uuid = (ctx === null || ctx === void 0 ? void 0 : ctx.uuid) || (0, uuid_1.v4)();
100
- if (error instanceof shared_1.AppError && error.isOperational && res) {
101
- shared_1.logger.error(error);
102
- res.status(error.httpCode).json({
103
- error: { message: error.message, httpCode: error.httpCode, stack: error.stack },
104
- });
105
- return;
106
- }
107
- shared_1.logger.error({ ...error, uuid }, 'Unexpected error');
108
- if (res) {
109
- res.status(shared_1.HttpCode.INTERNAL_SERVER_ERROR).json({
110
- error: { message: error.message, httpCode: shared_1.HttpCode.INTERNAL_SERVER_ERROR, uuid },
111
- });
112
- return;
113
- }
114
- // eslint-disable-next-line no-console
115
- console.log('API server encountered a critical error. Exiting');
116
- process.exit(1);
117
- });
118
- }
119
- initWebGui() {
120
- this.apiApp.get('/webgui/rules', this.authReadOnly, async (req, res) => {
121
- const rules = {};
122
- for (const rule of await (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).getRules()) {
123
- rules[rule.id] = (0, serialize_javascript_1.default)((0, utils_1.liveRuleToRule)(rule), { unsafe: true });
124
- }
125
- res.render('rules', { rules: (0, utils_1.escapedSerialize)(rules), INDEX_DTS: shared_1.INDEX_DTS, ruleKeys: Object.keys(rules) });
126
- });
127
- this.apiApp.get('/webgui/traffic', this.authReadOnly, async (req, res) => {
128
- const serializedTraffic = [];
129
- for (const value of this.trafficStore.values()) {
130
- serializedTraffic.push(value);
131
- }
132
- res.render('traffic', {
133
- traffic: JSON.stringify(serializedTraffic.sort((a, b) => b.originalRequest.timestamp - a.originalRequest.timestamp)),
134
- });
135
- });
136
- // TODO make webui way better and safer, nicer formatting, eslint/prettier, blackjack and hookers
137
- this.apiApp.post('/webgui/rules/unsafesave', this.authReadWrite, async (req, res) => {
138
- const rule = new Function(req.body)();
139
- if (!rule ||
140
- !rule.id ||
141
- typeof rule.matches !== 'function' ||
142
- typeof rule.ttlSeconds !== 'number' ||
143
- rule.ttlSeconds > shared_1.MAX_RULE_TTL_SECONDS) {
144
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'Invalid rule' });
145
- }
146
- await (0, ruleExecutor_1.getRuleExecutor)(this.options.mockUuid).addRule({
147
- id: rule.id,
148
- matches: rule.matches,
149
- ttlSeconds: rule.ttlSeconds,
150
- ...(rule.actions && {
151
- actions: {
152
- ...(rule.actions.mockResponse
153
- ? { mockResponse: rule.actions.mockResponse }
154
- : { modifyRequest: rule.actions.modifyRequest, modifyResponse: rule.actions.modifyResponse }),
155
- },
156
- }),
157
- ...(rule.disableAfterUse !== undefined && { disableAfterUse: rule.disableAfterUse }),
158
- ...(rule.isEnabled !== undefined && { isEnabled: rule.isEnabled }),
159
- ...(rule.labels !== undefined && { labels: rule.labels }),
160
- ...(rule.priority !== undefined && { priority: rule.priority }),
161
- ...(rule.removeAfterUse !== undefined && { removeAfterUse: rule.removeAfterUse }),
162
- ...(rule.storeTraffic !== undefined && { storeTraffic: rule.storeTraffic }),
163
- }, true);
164
- res.send();
165
- });
166
- }
167
- start() {
168
- if (this.server) {
169
- throw new Error('mock server already started');
170
- }
171
- this.server = this.apiApp.listen(this.options.port, () => {
172
- shared_1.logger.info(`API listening on ${this.options.port}`);
173
- });
174
- }
175
- stop() {
176
- if (!this.server) {
177
- throw new Error('mock server not started');
178
- }
179
- this.server.close((error) => {
180
- shared_1.logger.warn(error, 'problem closing server');
181
- this.server = null;
182
- });
183
- }
184
- }
185
- exports.API = API;
@@ -1,4 +0,0 @@
1
- import type * as Stuntman from '@stuntman/shared';
2
- export declare const deserializeRule: (serializedRule: Stuntman.SerializedRule) => Stuntman.Rule;
3
- export declare const escapedSerialize: (obj: any) => string;
4
- export declare const liveRuleToRule: (liveRule: Stuntman.LiveRule) => Stuntman.Rule;
package/dist/api/utils.js DELETED
@@ -1,69 +0,0 @@
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.liveRuleToRule = exports.escapedSerialize = exports.deserializeRule = void 0;
7
- const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
8
- const shared_1 = require("@stuntman/shared");
9
- const validators_1 = require("./validators");
10
- // TODO
11
- const deserializeRule = (serializedRule) => {
12
- shared_1.logger.debug(serializedRule, 'attempt to deserialize rule');
13
- (0, validators_1.validateSerializedRuleProperties)(serializedRule);
14
- const rule = {
15
- id: serializedRule.id,
16
- matches: (req) => new Function('____arg0', serializedRule.matches.remoteFn)(req),
17
- ttlSeconds: serializedRule.ttlSeconds,
18
- ...(serializedRule.disableAfterUse !== undefined && { disableAfterUse: serializedRule.disableAfterUse }),
19
- ...(serializedRule.removeAfterUse !== undefined && { removeAfterUse: serializedRule.removeAfterUse }),
20
- ...(serializedRule.labels !== undefined && { labels: serializedRule.labels }),
21
- ...(serializedRule.priority !== undefined && { priority: serializedRule.priority }),
22
- ...(serializedRule.isEnabled !== undefined && { isEnabled: serializedRule.isEnabled }),
23
- ...(serializedRule.storeTraffic !== undefined && { storeTraffic: serializedRule.storeTraffic }),
24
- };
25
- if (serializedRule.actions) {
26
- // TODO store the original localFn and variables sent from client for web UI editing maybe
27
- if (serializedRule.actions.mockResponse) {
28
- rule.actions = {
29
- mockResponse: 'remoteFn' in serializedRule.actions.mockResponse
30
- ? (req) => {
31
- var _a;
32
- return new Function('____arg0', ((_a = serializedRule.actions) === null || _a === void 0 ? void 0 : _a.mockResponse).remoteFn)(req);
33
- }
34
- : serializedRule.actions.mockResponse,
35
- };
36
- }
37
- else {
38
- rule.actions = {};
39
- if (serializedRule.actions.modifyRequest) {
40
- rule.actions.modifyRequest = (req) => {
41
- var _a;
42
- return new Function('____arg0', ((_a = serializedRule.actions) === null || _a === void 0 ? void 0 : _a.modifyRequest).remoteFn)(req);
43
- };
44
- }
45
- if (serializedRule.actions.modifyResponse) {
46
- rule.actions.modifyResponse = (req, res) => {
47
- var _a;
48
- return new Function('____arg0', '____arg1', ((_a = serializedRule.actions) === null || _a === void 0 ? void 0 : _a.modifyResponse).remoteFn)(req, res);
49
- };
50
- }
51
- }
52
- }
53
- shared_1.logger.debug(rule, 'deserialized rule');
54
- return rule;
55
- };
56
- exports.deserializeRule = deserializeRule;
57
- const escapedSerialize = (obj) => (0, serialize_javascript_1.default)(obj).replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, "\\n'\n+ '");
58
- exports.escapedSerialize = escapedSerialize;
59
- const liveRuleToRule = (liveRule) => {
60
- const ruleClone = { ...liveRule };
61
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
62
- // @ts-ignore
63
- delete ruleClone.counter;
64
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
65
- // @ts-ignore
66
- delete ruleClone.createdTimestamp;
67
- return ruleClone;
68
- };
69
- exports.liveRuleToRule = liveRuleToRule;
@@ -1,3 +0,0 @@
1
- import type * as Stuntman from '@stuntman/shared';
2
- export declare const validateSerializedRuleProperties: (rule: Stuntman.SerializedRule) => void;
3
- export declare const validateDeserializedRule: (deserializedRule: Stuntman.Rule) => void;
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateDeserializedRule = exports.validateSerializedRuleProperties = void 0;
4
- const shared_1 = require("@stuntman/shared");
5
- const validateSerializedRuleProperties = (rule) => {
6
- var _a;
7
- if (!rule) {
8
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid serialized rule' });
9
- }
10
- if (typeof rule.id !== 'string') {
11
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.id' });
12
- }
13
- if (typeof rule.matches !== 'object' || !('remoteFn' in rule.matches) || typeof rule.matches.remoteFn !== 'string') {
14
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.matches' });
15
- }
16
- if (rule.priority && typeof rule.priority !== 'number') {
17
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.priority' });
18
- }
19
- if (typeof rule.actions !== 'undefined') {
20
- if (typeof rule.actions !== 'object') {
21
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.actions' });
22
- }
23
- if (typeof rule.actions.mockResponse !== 'undefined') {
24
- if (typeof rule.actions.mockResponse !== 'object') {
25
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.actions.mockResponse' });
26
- }
27
- if ('remoteFn' in rule.actions.mockResponse && typeof rule.actions.mockResponse.remoteFn !== 'string') {
28
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.actions.mockResponse' });
29
- }
30
- else if ('status' in rule.actions.mockResponse) {
31
- if (typeof rule.actions.mockResponse.status !== 'number') {
32
- throw new shared_1.AppError({
33
- httpCode: shared_1.HttpCode.BAD_REQUEST,
34
- message: 'invalid rule.actions.mockResponse.status',
35
- });
36
- }
37
- if (typeof rule.actions.mockResponse.rawHeaders !== 'undefined' &&
38
- (!Array.isArray(rule.actions.mockResponse.rawHeaders) ||
39
- rule.actions.mockResponse.rawHeaders.some((header) => typeof header !== 'string'))) {
40
- throw new shared_1.AppError({
41
- httpCode: shared_1.HttpCode.BAD_REQUEST,
42
- message: 'invalid rule.actions.mockResponse.rawHeaders',
43
- });
44
- }
45
- if (typeof rule.actions.mockResponse.body !== 'undefined' && typeof rule.actions.mockResponse.body !== 'string') {
46
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.actions.mockResponse.body' });
47
- }
48
- }
49
- }
50
- if (typeof rule.actions.modifyRequest !== 'undefined' && typeof rule.actions.modifyRequest.remoteFn !== 'string') {
51
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.actions.modifyRequest' });
52
- }
53
- if (typeof rule.actions.modifyResponse !== 'undefined' && typeof rule.actions.modifyResponse.remoteFn !== 'string') {
54
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.actions.modifyResponse' });
55
- }
56
- }
57
- if (typeof rule.disableAfterUse !== 'undefined' &&
58
- typeof rule.disableAfterUse !== 'boolean' &&
59
- (typeof rule.disableAfterUse !== 'number' || rule.disableAfterUse < 0)) {
60
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.disableAfterUse' });
61
- }
62
- if (typeof rule.removeAfterUse !== 'undefined' &&
63
- typeof rule.removeAfterUse !== 'boolean' &&
64
- (typeof rule.removeAfterUse !== 'number' || rule.removeAfterUse < 0)) {
65
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.removeAfterUse' });
66
- }
67
- if (typeof rule.labels !== 'undefined' &&
68
- (!Array.isArray(rule.labels) || rule.labels.some((label) => typeof label !== 'string'))) {
69
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.labels' });
70
- }
71
- if (((_a = rule.actions) === null || _a === void 0 ? void 0 : _a.mockResponse) && rule.actions.modifyResponse) {
72
- throw new shared_1.AppError({
73
- httpCode: shared_1.HttpCode.BAD_REQUEST,
74
- message: 'rule.actions.mockResponse and rule.actions.modifyResponse are mutually exclusive',
75
- });
76
- }
77
- if (!rule.ttlSeconds || rule.ttlSeconds < shared_1.MIN_RULE_TTL_SECONDS || rule.ttlSeconds > shared_1.MAX_RULE_TTL_SECONDS) {
78
- throw new shared_1.AppError({
79
- httpCode: shared_1.HttpCode.BAD_REQUEST,
80
- message: `rule.ttlSeconds should be within ${shared_1.MIN_RULE_TTL_SECONDS} and ${shared_1.MAX_RULE_TTL_SECONDS}`,
81
- });
82
- }
83
- if (typeof rule.isEnabled !== 'undefined' && typeof rule.isEnabled !== 'boolean') {
84
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.isEnabled' });
85
- }
86
- if (typeof rule.storeTraffic !== 'undefined' && typeof rule.storeTraffic !== 'boolean') {
87
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.BAD_REQUEST, message: 'invalid rule.storeTraffic' });
88
- }
89
- };
90
- exports.validateSerializedRuleProperties = validateSerializedRuleProperties;
91
- const validateDeserializedRule = (deserializedRule) => {
92
- // TODO validate other functions ?
93
- let matchValidationResult;
94
- try {
95
- matchValidationResult = deserializedRule.matches({
96
- id: 'validation',
97
- method: 'GET',
98
- rawHeaders: new shared_1.RawHeaders(),
99
- timestamp: Date.now(),
100
- url: 'http://dummy.invalid/',
101
- });
102
- }
103
- catch (error) {
104
- shared_1.logger.error({ ruleId: deserializedRule.id }, error);
105
- throw new shared_1.AppError({
106
- httpCode: shared_1.HttpCode.UNPROCESSABLE_ENTITY,
107
- message: 'match function threw an error',
108
- });
109
- }
110
- if (matchValidationResult !== true &&
111
- matchValidationResult !== false &&
112
- matchValidationResult.result !== true &&
113
- matchValidationResult.result !== false) {
114
- shared_1.logger.error({ ruleId: deserializedRule.id, matchValidationResult }, 'match function retruned invalid value');
115
- throw new shared_1.AppError({ httpCode: shared_1.HttpCode.UNPROCESSABLE_ENTITY, message: 'match function returned invalid value' });
116
- }
117
- };
118
- exports.validateDeserializedRule = validateDeserializedRule;
@@ -1,145 +0,0 @@
1
- doctype html
2
- html
3
- head
4
- title Stuntman - rule editor
5
- script(src='https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.35.0/min/vs/loader.min.js')
6
- style
7
- include style.css
8
- body(style='color: rgb(204, 204, 204); background-color: rgb(50, 50, 50)')
9
- button#newRule(type='button', onclick='window.newRule()') New rule
10
- button#saveRule(type='button', onclick='window.saveRule()', disabled) Save rule
11
- div(style='width: 100%; overflow: hidden')
12
- div(style='width: 230px; float: left')
13
- h3 Rules
14
- ul#ruleKeys.no-bullets
15
- each ruleId in ruleKeys
16
- li
17
- button.rule(
18
- type='button',
19
- onclick='window.setRuleModel(this.getAttribute("data-rule-id"))',
20
- data-rule-id=ruleId
21
- )= ruleId
22
- div(style='margin-left: 240px')
23
- #container(style='height: 400px')
24
- script.
25
- const uuidv4 = () => {
26
- function getRandomSymbol(symbol) {
27
- var array;
28
-
29
- if (symbol === 'y') {
30
- array = ['8', '9', 'a', 'b'];
31
- return array[Math.floor(Math.random() * array.length)];
32
- }
33
-
34
- array = new Uint8Array(1);
35
- window.crypto.getRandomValues(array);
36
- return (array[0] % 16).toString(16);
37
- }
38
-
39
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, getRandomSymbol);
40
- };
41
- require.config({
42
- paths: {
43
- vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.35.0/min/vs',
44
- },
45
- });
46
- require(['vs/editor/editor.main'], function () {
47
- monaco.languages.typescript.javascriptDefaults.setEagerModelSync(true);
48
- monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
49
- noSemanticValidation: false,
50
- noSyntaxValidation: false,
51
- });
52
-
53
- // compiler options
54
- monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
55
- target: monaco.languages.typescript.ScriptTarget.ES6,
56
- allowNonTsExtensions: true,
57
- noLib: true,
58
- moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
59
- module: monaco.languages.typescript.ModuleKind.CommonJS,
60
- noEmit: true,
61
- checkJs: true,
62
- allowJs: true,
63
- isolatedModules: true,
64
- typeRoots: ['node_modules/@types'],
65
- });
66
-
67
- monaco.languages.typescript.typescriptDefaults.addExtraLib(`!{INDEX_DTS}`, 'file:///node_modules/@types/stuntman/index.d.ts');
68
- const models = {};
69
- const rules = eval('(!{rules})');
70
- for (const ruleId of Object.keys(rules)) {
71
- models[ruleId] = monaco.editor.createModel("import type * as Stuntman from 'stuntman';\n\nvar STUNTMAN_RULE: Stuntman.Rule = " + rules[ruleId] + ';', 'typescript', `file:///${ruleId}.ts`);
72
- }
73
- const editor = monaco.editor.create(document.getElementById('container'), {
74
- theme: 'vs-dark',
75
- autoIndent: true,
76
- formatOnPaste: true,
77
- formatOnType: true,
78
- automaticLayout: true,
79
- autoIndent: true,
80
- tabSize: 2,
81
- });
82
- editor.onDidChangeModel((event) => {
83
- const isInternal = /^file:\/\/\/internal\/.+/.test(event.newModelUrl);
84
- if (isInternal) {
85
- document.getElementById('saveRule').setAttribute('disabled', 'true');
86
- } else {
87
- document.getElementById('saveRule').removeAttribute('disabled');
88
- }
89
-
90
- setTimeout(() => {
91
- editor.getAction('editor.action.formatDocument').run();
92
- }, 100);
93
- });
94
-
95
- window.setRuleModel = (ruleId) => {
96
- if (history.pushState) {
97
- const newUrl = `${window.location.protocol}//${window.location.host}${window.location.pathname}?ruleId=${encodeURIComponent(ruleId)}`;
98
- window.history.pushState({ path: newUrl }, '', newUrl);
99
- }
100
- editor.setModel(models[ruleId]);
101
- };
102
-
103
- const urlSearchParams = new URLSearchParams(window.location.search);
104
- if (urlSearchParams.has('ruleId') && urlSearchParams.get('ruleId') in models) {
105
- editor.setModel(models[urlSearchParams.get('ruleId')]);
106
- }
107
-
108
- window.saveRule = () => {
109
- document.getElementById('saveRule').setAttribute('disabled', 'true');
110
- const modelUri = editor.getModel().uri;
111
- const result = monaco.languages.typescript.getTypeScriptWorker();
112
- result.then((worker) => {
113
- worker(modelUri).then(function (client) {
114
- client.getEmitOutput(modelUri.toString()).then((output) => {
115
- const ruleFunctionText = output.outputFiles[0].text.replace(/^export .+$/im, '');
116
- const newId = new Function(ruleFunctionText + '\n return STUNTMAN_RULE;')().id;
117
- fetch('/webgui/rules/unsafesave', {
118
- method: 'POST',
119
- headers: { 'content-type': 'text/plain' },
120
- body: ruleFunctionText + '\n return STUNTMAN_RULE;',
121
- }).then((response) => {
122
- if (response.ok) {
123
- window.location.reload();
124
- return;
125
- }
126
- alert('Error when saving rule');
127
- document.getElementById('saveRule').removeAttribute('disabled');
128
- });
129
- });
130
- });
131
- });
132
- };
133
-
134
- window.newRule = () => {
135
- const ruleId = uuidv4();
136
- const emptyRule = `import type * as Stuntman from \'stuntman\';\n\nvar STUNTMAN_RULE: Stuntman.Rule = { id: '${ruleId}', matches: (req: Stuntman.Request) => true, ttlSeconds: 600 };`;
137
- models[ruleId] = monaco.editor.createModel(emptyRule, 'typescript', `file:///${ruleId}.ts`);
138
- const ruleKeyNode = document.getElementById('ruleKeys').firstChild;
139
- const ruleKeyNodeClone = ruleKeyNode.cloneNode(true);
140
- ruleKeyNodeClone.getElementsByTagName('button')[0].setAttribute('data-rule-id', ruleId);
141
- ruleKeyNodeClone.getElementsByTagName('button')[0].innerText = ruleId;
142
- document.getElementById('ruleKeys').appendChild(ruleKeyNodeClone);
143
- window.setRuleModel(ruleId);
144
- };
145
- });
@@ -1,28 +0,0 @@
1
- div#container {
2
- resize: vertical;
3
- overflow: auto;
4
- }
5
-
6
- body {
7
- font-family: Menlo, Monaco, 'Courier New', monospace;
8
- }
9
-
10
- button.rule {
11
- font-family: Menlo, Monaco, 'Courier New', monospace;
12
- background: none !important;
13
- border: none;
14
- padding: 0 !important;
15
- color: #aaa;
16
- text-decoration: underline;
17
- cursor: pointer;
18
- margin-top: 10px;
19
- font-size: x-small;
20
- text-align: left;
21
- }
22
-
23
- ul.no-bullets {
24
- list-style-type: none; /* Remove bullets */
25
- padding: 0; /* Remove padding */
26
- margin: 0; /* Remove margins */
27
- text-align: left;
28
- }
@@ -1,37 +0,0 @@
1
- doctype html
2
- html
3
- head
4
- title Stuntman - rule editor
5
- script(src='https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.35.0/min/vs/loader.min.js')
6
- style
7
- include style.css
8
- body(style='color: rgb(204, 204, 204); background-color: rgb(50, 50, 50)')
9
- div(style='width: 100%; overflow: hidden')
10
- div(style='width: 200px; float: left')
11
- h3 Traffic log
12
- div(style='margin-left: 220px')
13
- #container(style='height: 800px')
14
- script.
15
- require.config({
16
- paths: {
17
- vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.35.0/min/vs',
18
- },
19
- });
20
- require(['vs/editor/editor.main'], function () {
21
- const traffic = !{ traffic };
22
- const model = monaco.editor.createModel(JSON.stringify(traffic, null, 2), 'json');
23
- const editor = monaco.editor.create(document.getElementById('container'), {
24
- theme: 'vs-dark',
25
- autoIndent: true,
26
- formatOnPaste: true,
27
- formatOnType: true,
28
- automaticLayout: true,
29
- readOnly: true,
30
- });
31
- editor.onDidChangeModel((event) => {
32
- setTimeout(() => {
33
- editor.getAction('editor.action.formatDocument').run();
34
- }, 100);
35
- });
36
- editor.setModel(model);
37
- });
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- const mock_1 = require("../mock");
5
- const shared_1 = require("@stuntman/shared");
6
- const mock = new mock_1.Mock(shared_1.serverConfig);
7
- mock.start();
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { Mock as StuntmanMock } from './mock';
package/dist/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StuntmanMock = void 0;
4
- var mock_1 = require("./mock");
5
- Object.defineProperty(exports, "StuntmanMock", { enumerable: true, get: function () { return mock_1.Mock; } });