Skip to content

Commit db1f047

Browse files
committed
Refactor code and add delay functionality
1 parent 13ccfef commit db1f047

File tree

9 files changed

+271
-57
lines changed

9 files changed

+271
-57
lines changed

src/app-proxy-server/mockManager.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { BrowserWindow, ipcMain } = require("electron");
44
const StoreManager = require('../storeManager');
55
const storeManager = StoreManager.getInstance();
66
const crypto = require('crypto');
7+
const { get } = require("jquery");
78

89
// Definisci il percorso della cartella che desideri controllare/creare
910

@@ -12,6 +13,19 @@ let folderPath;
1213
const startMockManager = () => {
1314
console.log('startMockManager', storeManager.getConfig().dirPath);
1415
folderPath = storeManager.getConfig().dirPath;
16+
updateModel();
17+
}
18+
19+
const updateModel = () => {
20+
console.log('updateModel')
21+
// cycle all file and update model
22+
const files = getAllMock();
23+
files.forEach((file) => {
24+
if (file.delay === undefined) {
25+
file["delay"] = 0;
26+
saveMock(file.uuid, file);
27+
}
28+
});
1529
}
1630

1731
const saveMock = (filename, mock) => {
@@ -44,7 +58,7 @@ const createMock = (data) => {
4458
} else {
4559
data.uuid = crypto.createHash('sha256').update(data.targetUrl + JSON.stringify({})).digest('hex');
4660
}
47-
61+
data["delay"] = 0;
4862
return saveMock(data.uuid, data);
4963
}
5064

src/app-proxy-server/proxy.helper.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const https = require('https'); // o `https` se necessario
22
const crypto = require('crypto');
33
const { getMock, saveMock } = require('./mockManager');
44
const StoreManager = require('../storeManager');
5+
const e = require('express');
56
const storeManager = StoreManager.getInstance();
67

78

@@ -19,6 +20,9 @@ async function proxySniffer(req, res, next) {
1920
const mockArchive = getMock(sha256);
2021
if (!config.bypassGlobal && mockArchive && !mockArchive.bypassCache) {
2122
// Restituisci la risposta dall'archivio
23+
if (mockArchive.delay) {
24+
await new Promise((resolve) => setTimeout(resolve, mockArchive.delay));
25+
}
2226
res.status(mockArchive.statusCode).json(mockArchive.response);
2327
} else {
2428
// Inoltra la richiesta al server esterno
@@ -33,7 +37,18 @@ async function proxySniffer(req, res, next) {
3337
body: req.method === 'GET' || req.method === 'HEAD' ? undefined : JSON.stringify(req.body),
3438
});
3539

36-
const responseData = await externalResponse.json();
40+
// check if response is json
41+
let responseData;
42+
const contentType = externalResponse.headers.get('content-type');
43+
44+
if (contentType && contentType.includes('application/json')) {
45+
responseData = await externalResponse.json();
46+
}else{
47+
// La risposta è un file media o altro tipo di contenuto
48+
const responseData = await externalResponse
49+
res.set('Content-Type', contentType);
50+
res.send(responseData);
51+
}
3752

3853
let newMock = {
3954
bypassCache: config.bypassCache,
@@ -47,7 +62,8 @@ async function proxySniffer(req, res, next) {
4762
};
4863

4964
// Salva la risposta in json
50-
if (!config.bypassGlobal && !mockArchive) {
65+
const excludeThisUrl = excludeUrl(target);
66+
if (!config.bypassGlobal && !mockArchive && !excludeThisUrl) {
5167
saveMock(sha256, newMock);
5268
}
5369

@@ -60,6 +76,16 @@ async function proxySniffer(req, res, next) {
6076
}
6177
}
6278

79+
// method for exclude some url from archive
80+
const excludeUrl = (url) => {
81+
// if media file
82+
if (url.match(/\.(jpeg|jpg|gif|png|ico|css|js|woff|woff2|ttf|svg|eot)$/)) {
83+
return true;
84+
}
85+
return false;
86+
};
87+
88+
6389
module.exports = {
6490
proxySniffer,
6591
};

src/assets/scss/main.scss

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ $secondary: #245563;
88
0% {
99
width: 100%;
1010
}
11+
1112
100% {
1213
width: 0;
1314
}
@@ -19,12 +20,13 @@ body {
1920
}
2021

2122
.top-bar {
22-
&>div{
23+
&>div {
2324
margin-top: .5rem;
2425
display: flex;
2526
justify-content: space-between;
2627
align-items: center;
2728
}
29+
2830
.header {
2931
display: flex;
3032

@@ -33,25 +35,29 @@ body {
3335
height: 60px;
3436
}
3537
}
36-
.wrap-path{
37-
p{
38+
39+
.wrap-path {
40+
p {
3841
text-wrap: nowrap;
3942
overflow: hidden;
4043
text-overflow: ellipsis;
4144
}
42-
#path{
43-
}
45+
46+
#path {}
4447
}
4548
}
4649

47-
.top-action-all{
50+
.top-action-all {
4851
margin-bottom: .3rem;
49-
.delete-section{
52+
53+
.delete-section {
5054
margin-left: 1rem;
5155
}
52-
.confirm-delete-all{
56+
57+
.confirm-delete-all {
5358
position: relative;
54-
.time-delete{
59+
60+
.time-delete {
5561
position: absolute;
5662
bottom: 0;
5763
right: 0;
@@ -143,6 +149,7 @@ body {
143149
background-color: #2b3947;
144150
color: white;
145151
border-radius: 10px;
152+
146153
@media (max-width: 1000px) {
147154
grid-template-columns: 50px 1fr auto;
148155
grid-template-rows: 1fr 1fr 1fr;
@@ -160,34 +167,42 @@ body {
160167
text-overflow: ellipsis;
161168
white-space: nowrap;
162169
}
163-
.target-url{
170+
171+
.target-url {
164172
grid-column: 2 / 3;
165173
grid-row: 1 / 2;
166174
margin-left: .5rem;
167175
}
176+
168177
.actions {
169178
grid-column: 3 / 4;
170179
grid-row: 1 / 3;
180+
171181
@media (max-width: 1000px) {
172182
grid-column: 2 / 4;
173183
grid-row: 3 / 4;
174184
justify-content: flex-end;
175185
}
186+
176187
display: flex;
177188
justify-content: center;
178189
align-items: center;
190+
179191
.form-check-input:checked {
180192
background-color: #c30808;
181193
border-color: #d43f3f;
182194
}
183195
}
196+
184197
.method {
185198
grid-column: 1 / 2;
186199
grid-row: 1 / 3;
200+
187201
@media (max-width: 1000px) {
188202
grid-column: 1 / 2;
189203
grid-row: 1 / 4;
190204
}
205+
191206
border-radius: 5px;
192207
background-color: #ea2929;
193208
color: white;
@@ -198,15 +213,19 @@ body {
198213
justify-content: center;
199214
align-items: center;
200215
font-weight: 700;
216+
201217
&-get {
202218
background-color: #0baa20;
203219
}
220+
204221
&-post {
205222
background-color: #df9401;
206223
}
224+
207225
&-put {
208226
background-color: #ea5b29;
209227
}
228+
210229
&-patch {
211230
background-color: #89460c;
212231
}
@@ -248,19 +267,71 @@ body {
248267
}
249268

250269
.jsoneditor-tree-inner {
270+
.jsoneditor-tree {
271+
.jsoneditor-field {
272+
color: #ff9d00;
273+
}
274+
275+
.jsoneditor-value.jsoneditor-string {
276+
color: #5fd1ff !important;
277+
}
278+
279+
.jsoneditor-value.jsoneditor-number {
280+
color: #fbf406;
281+
}
282+
}
283+
251284
tbody {
252285
background-color: $primary;
253286
}
254287
}
255288

256-
.jsoneditor-tree {
257-
.jsoneditor-readonly {
258-
color: #bfb9b9;
259-
}
289+
.jsoneditor-outer {
290+
background-color: $primary;
260291

261-
button {
262-
&.jsoneditor-expanded {
263-
filter: brightness(100);
292+
.ace_editor {
293+
height: 85vh !important;
294+
max-height: calc(100vh - 180px);
295+
296+
.ace_gutter {
297+
background-color: $primary;
298+
color: white;
299+
}
300+
.ace_gutter-active-line.ace_gutter-cell {
301+
background-color: #ff9d005c;
302+
}
303+
.ace_active-line {
304+
background-color: #ff9d005c;
305+
}
306+
307+
.ace_line_group {
308+
.ace_line {
309+
color: #ffffff;
310+
311+
.ace_variable {
312+
color: #ff9d00;
313+
}
314+
315+
.ace_string {
316+
color: #5fd1ff;
317+
}
318+
319+
.ace_parent {
320+
color: #ff9d00;
321+
}
322+
323+
.ace_indent-guide {
324+
color: #ff9d00;
325+
}
326+
327+
.ace_numeric {
328+
color: #fbf406;
329+
}
330+
}
331+
}
332+
333+
.ace_scroller {
334+
background-color: $primary;
264335
}
265336
}
266337
}
@@ -291,6 +362,10 @@ body {
291362
filter: brightness(100);
292363

293364
}
365+
.jsoneditor-statusbar {
366+
background-color: $primary;
367+
color: white;
368+
}
294369
}
295370

296371
.controller-json {
@@ -304,6 +379,7 @@ body {
304379

305380
.row-config {
306381
margin-top: 2rem;
382+
307383
.config-add {
308384
flex-direction: column;
309385
display: flex;
@@ -312,21 +388,24 @@ body {
312388
}
313389
}
314390

315-
.path-history{
391+
.path-history {
316392
display: flex;
317393
flex-direction: column;
318394
justify-content: center;
319395
align-items: center;
320-
.path{
396+
397+
.path {
321398
width: fit-content;
322399
height: fit-content;
323400
cursor: pointer;
324401
margin: .2rem 1rem;
325402
border-radius: 5px;
326-
&:hover{
403+
404+
&:hover {
327405
background-color: #3b4e62;
328406
}
329-
p{
407+
408+
p {
330409
padding: .5rem;
331410
margin: 0;
332411
}

0 commit comments

Comments
 (0)