๐Ÿ“ฆ Kong / insomnia-mockbin

๐Ÿ“„ utils.js ยท 107 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107const pkg = require("../package.json");

const utils = {
	// HTTP method + ID + route name: POST1234/foo OR 1234/foo
	// method is gathered from the insomnia-mock-method header
	// id is first slug of the url and path is the rest of the url
	// NOTE: http method GET is ingored in the compound id in order to preserve existing mocks
	// compoundId is used to store and retrieve mocks from redis
	createCompoundId: (method, id, path) => {
		return method?.toUpperCase() === "GET"
			? id + path
			: (method?.toUpperCase() || "") + id + path;
	},
	objectToArray: (obj) => {
		if (!obj || typeof obj !== "object") {
			return [];
		}

		const results = Object.keys(obj).reduce((results, name) => {
			results.push({
				name: name,
				value: obj[name],
			});

			return results;
		}, []);

		return results;
	},

	getReqHeaderSize: (req) => {
		const keys = Object.keys(req.headers);

		const values = keys.map((key) => req.headers[key]);

		const headers =
			req.method +
			req.url +
			req.versionMajor +
			req.versionMinor +
			keys.join() +
			values.join();

		// startline: [method] [url] HTTP/1.1\r\n = 12
		// endline: \r\n = 2
		// every header + \r\n = * 2
		return Buffer.from(headers).length + keys.length * 2 + 12 + 2;
	},

	createHar: (req) => ({
		log: {
			version: "1.2",
			creator: {
				name: "mockbin.com",
				version: pkg.version,
			},
			entries: [
				{
					startedDateTime: new Date().toISOString(),
					clientIPAddress: req.ip,
					request: {
						method: req.method,
						url: `${req.protocol}://${req.hostname}${req.originalUrl}`,
						httpVersion: "HTTP/1.1",
						// TODO, add cookie details
						cookies: utils.objectToArray(req.cookies),
						headers: utils.objectToArray(req.headers),
						queryString: utils.objectToArray(req.query),
						// TODO
						postData: {
							mimeType: req.contentType
								? req.contentType
								: "application/octet-stream",
							text: req.body,
							params: [],
						},
						headersSize: utils.getReqHeaderSize(req),
						bodySize: req.rawBody.length,
					},
				},
			],
		},
	}),

	createSimpleHar: (req) => ({
		startedDateTime: new Date().toISOString(),
		clientIPAddress: req.ip,
		method: req.method,
		url: `${req.protocol}://${req.hostname}${req.originalUrl}`,
		httpVersion: "HTTP/1.1",
		// TODO, add cookie details
		cookies: req.cookies,
		headers: req.headers,
		queryString: req.query,
		// TODO
		postData: {
			mimeType: req.contentType ? req.contentType : "application/octet-stream",
			text: req.body,
			params: [],
		},
		headersSize: utils.getReqHeaderSize(req),
		bodySize: req.rawBody.length,
	}),
};

module.exports = utils;