@testsmith/api-spector 0.0.7 → 0.0.9
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/out/main/chunks/mock-server-C0RlwZf_.js +240 -0
- package/out/main/chunks/request-handler-9MdHOWVf.js +1041 -0
- package/out/main/index.js +54 -519
- package/out/main/mock.js +3 -1
- package/out/main/runner.js +28 -23
- package/out/preload/index.js +4 -0
- package/out/renderer/assets/{index-x-2wMDPZ.js → index-C67sPzTG.js} +5986 -5315
- package/out/renderer/assets/index-YkI5DFME.css +2 -0
- package/out/renderer/index.html +2 -2
- package/package.json +1 -1
- package/out/main/chunks/mock-server-ZB7zpXh9.js +0 -102
- package/out/main/chunks/script-runner-CmdLWmKN.js +0 -511
- package/out/renderer/assets/index-DRGkbz0a.css +0 -2
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
const http = require("http");
|
|
25
|
+
const crypto = require("crypto");
|
|
26
|
+
const vm = require("vm");
|
|
27
|
+
const dayjs = require("dayjs");
|
|
28
|
+
function _interopNamespaceDefault(e) {
|
|
29
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
30
|
+
if (e) {
|
|
31
|
+
for (const k in e) {
|
|
32
|
+
if (k !== "default") {
|
|
33
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
34
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: () => e[k]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
n.default = e;
|
|
42
|
+
return Object.freeze(n);
|
|
43
|
+
}
|
|
44
|
+
const vm__namespace = /* @__PURE__ */ _interopNamespaceDefault(vm);
|
|
45
|
+
let _fakerCache = null;
|
|
46
|
+
async function getFaker() {
|
|
47
|
+
if (!_fakerCache) _fakerCache = await import("@faker-js/faker");
|
|
48
|
+
return _fakerCache.faker;
|
|
49
|
+
}
|
|
50
|
+
const running = /* @__PURE__ */ new Map();
|
|
51
|
+
const liveRoutes = /* @__PURE__ */ new Map();
|
|
52
|
+
let hitCallback = null;
|
|
53
|
+
function setHitCallback(cb) {
|
|
54
|
+
hitCallback = cb;
|
|
55
|
+
}
|
|
56
|
+
function updateMockRoutes(id, routes) {
|
|
57
|
+
liveRoutes.set(id, routes);
|
|
58
|
+
}
|
|
59
|
+
function matchPath(pattern, urlPath) {
|
|
60
|
+
const regexStr = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/:[^/]+/g, "[^/]+");
|
|
61
|
+
try {
|
|
62
|
+
const regex = new RegExp("^" + regexStr + "/?$");
|
|
63
|
+
return regex.test(urlPath.split("?")[0]);
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function findRoute(routes, method, urlPath) {
|
|
69
|
+
const path = urlPath.split("?")[0];
|
|
70
|
+
return routes.find((r) => r.method === method && matchPath(r.path, path)) ?? routes.find((r) => r.method === "ANY" && matchPath(r.path, path)) ?? null;
|
|
71
|
+
}
|
|
72
|
+
function extractPathParams(pattern, urlPath) {
|
|
73
|
+
const params = {};
|
|
74
|
+
const patParts = pattern.split("/");
|
|
75
|
+
const urlParts = urlPath.split("?")[0].split("/");
|
|
76
|
+
patParts.forEach((part, i) => {
|
|
77
|
+
if (part.startsWith(":")) {
|
|
78
|
+
params[part.slice(1)] = decodeURIComponent(urlParts[i] ?? "");
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return params;
|
|
82
|
+
}
|
|
83
|
+
function extractQueryParams(url) {
|
|
84
|
+
const params = {};
|
|
85
|
+
const qs = url.split("?")[1];
|
|
86
|
+
if (qs) new URLSearchParams(qs).forEach((v, k) => {
|
|
87
|
+
params[k] = v;
|
|
88
|
+
});
|
|
89
|
+
return params;
|
|
90
|
+
}
|
|
91
|
+
function readBody(req) {
|
|
92
|
+
return new Promise((resolve) => {
|
|
93
|
+
let data = "";
|
|
94
|
+
req.on("data", (chunk) => {
|
|
95
|
+
data += chunk.toString();
|
|
96
|
+
});
|
|
97
|
+
req.on("end", () => resolve(data));
|
|
98
|
+
req.on("error", () => resolve(""));
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function interpolateMockBody(body, context) {
|
|
102
|
+
return body.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
|
|
103
|
+
const expr = key.trim();
|
|
104
|
+
if (expr.includes(".") || expr.includes("(")) {
|
|
105
|
+
try {
|
|
106
|
+
const result = vm__namespace.runInNewContext(expr, context);
|
|
107
|
+
if (result !== void 0 && result !== null) return String(result);
|
|
108
|
+
} catch {
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return match;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async function handleRequest(serverId, req, res, reqStart) {
|
|
115
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
116
|
+
const urlPath = req.url ?? "/";
|
|
117
|
+
if (urlPath === "/favicon.ico") {
|
|
118
|
+
res.writeHead(204);
|
|
119
|
+
res.end();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const bodyRaw = await readBody(req);
|
|
123
|
+
let bodyParsed = {};
|
|
124
|
+
try {
|
|
125
|
+
bodyParsed = JSON.parse(bodyRaw);
|
|
126
|
+
} catch {
|
|
127
|
+
}
|
|
128
|
+
const routes = liveRoutes.get(serverId) ?? [];
|
|
129
|
+
const route = findRoute(routes, method, urlPath);
|
|
130
|
+
if (!route) {
|
|
131
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
132
|
+
res.end(JSON.stringify({ error: "No matching mock route", method, path: urlPath }));
|
|
133
|
+
hitCallback?.({
|
|
134
|
+
id: crypto.randomUUID(),
|
|
135
|
+
serverId,
|
|
136
|
+
timestamp: reqStart,
|
|
137
|
+
method,
|
|
138
|
+
path: urlPath,
|
|
139
|
+
matchedRouteId: null,
|
|
140
|
+
status: 404,
|
|
141
|
+
durationMs: Date.now() - reqStart
|
|
142
|
+
});
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const reqHeaders = {};
|
|
146
|
+
Object.entries(req.headers).forEach(([k, v]) => {
|
|
147
|
+
if (v !== void 0) reqHeaders[k] = Array.isArray(v) ? v.join(", ") : v;
|
|
148
|
+
});
|
|
149
|
+
const requestCtx = {
|
|
150
|
+
method,
|
|
151
|
+
path: urlPath.split("?")[0],
|
|
152
|
+
params: extractPathParams(route.path, urlPath),
|
|
153
|
+
query: extractQueryParams(urlPath),
|
|
154
|
+
body: bodyParsed,
|
|
155
|
+
bodyRaw,
|
|
156
|
+
headers: reqHeaders
|
|
157
|
+
};
|
|
158
|
+
const responseDraft = {
|
|
159
|
+
statusCode: route.statusCode,
|
|
160
|
+
body: route.body,
|
|
161
|
+
headers: { "Content-Type": "application/json", ...route.headers }
|
|
162
|
+
};
|
|
163
|
+
if (route.script?.trim()) {
|
|
164
|
+
try {
|
|
165
|
+
const faker2 = await getFaker();
|
|
166
|
+
vm__namespace.runInNewContext(route.script, {
|
|
167
|
+
request: requestCtx,
|
|
168
|
+
response: responseDraft,
|
|
169
|
+
faker: faker2,
|
|
170
|
+
dayjs,
|
|
171
|
+
console: { log: (...args) => console.log("[mock-script]", ...args) }
|
|
172
|
+
});
|
|
173
|
+
} catch (err) {
|
|
174
|
+
console.error("[mock-script error]", err);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const faker = await getFaker();
|
|
178
|
+
const exprCtx = { request: requestCtx, faker, dayjs };
|
|
179
|
+
const finalBody = interpolateMockBody(responseDraft.body, exprCtx);
|
|
180
|
+
const respond = () => {
|
|
181
|
+
res.writeHead(responseDraft.statusCode, responseDraft.headers);
|
|
182
|
+
res.end(finalBody);
|
|
183
|
+
hitCallback?.({
|
|
184
|
+
id: crypto.randomUUID(),
|
|
185
|
+
serverId,
|
|
186
|
+
timestamp: reqStart,
|
|
187
|
+
method,
|
|
188
|
+
path: urlPath,
|
|
189
|
+
matchedRouteId: route.id,
|
|
190
|
+
status: responseDraft.statusCode,
|
|
191
|
+
durationMs: Date.now() - reqStart
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
if (route.delay && route.delay > 0) setTimeout(respond, route.delay);
|
|
195
|
+
else respond();
|
|
196
|
+
}
|
|
197
|
+
async function startMock(server) {
|
|
198
|
+
if (running.has(server.id)) await stopMock(server.id);
|
|
199
|
+
liveRoutes.set(server.id, server.routes ?? []);
|
|
200
|
+
const httpServer = http.createServer((req, res) => {
|
|
201
|
+
const reqStart = Date.now();
|
|
202
|
+
handleRequest(server.id, req, res, reqStart).catch((err) => {
|
|
203
|
+
console.error("[mock-handler uncaught]", err);
|
|
204
|
+
if (!res.headersSent) {
|
|
205
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
206
|
+
res.end(JSON.stringify({ error: "Mock handler error", message: String(err) }));
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
await new Promise((resolve, reject) => {
|
|
211
|
+
httpServer.once("error", reject);
|
|
212
|
+
httpServer.listen(server.port, "127.0.0.1", resolve);
|
|
213
|
+
});
|
|
214
|
+
running.set(server.id, httpServer);
|
|
215
|
+
}
|
|
216
|
+
async function stopMock(id) {
|
|
217
|
+
const srv = running.get(id);
|
|
218
|
+
if (!srv) return;
|
|
219
|
+
await new Promise(
|
|
220
|
+
(resolve, reject) => srv.close((err) => err ? reject(err) : resolve())
|
|
221
|
+
);
|
|
222
|
+
running.delete(id);
|
|
223
|
+
liveRoutes.delete(id);
|
|
224
|
+
}
|
|
225
|
+
function isRunning(id) {
|
|
226
|
+
return running.has(id);
|
|
227
|
+
}
|
|
228
|
+
function getRunningIds() {
|
|
229
|
+
return [...running.keys()];
|
|
230
|
+
}
|
|
231
|
+
async function stopAll() {
|
|
232
|
+
await Promise.all([...running.keys()].map(stopMock));
|
|
233
|
+
}
|
|
234
|
+
exports.getRunningIds = getRunningIds;
|
|
235
|
+
exports.isRunning = isRunning;
|
|
236
|
+
exports.setHitCallback = setHitCallback;
|
|
237
|
+
exports.startMock = startMock;
|
|
238
|
+
exports.stopAll = stopAll;
|
|
239
|
+
exports.stopMock = stopMock;
|
|
240
|
+
exports.updateMockRoutes = updateMockRoutes;
|