Skip to content

Commit deeddb4

Browse files
committed
Merge branch 'dev' into esa/subscriptions
2 parents 3ac6a23 + 11204f3 commit deeddb4

18 files changed

+674
-281
lines changed

main.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
// ** Entry point for Electron **
3434
const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron');
3535

36-
3736
const { autoUpdater } = require('electron-updater');
3837
const {
3938
default: installExtension,
@@ -60,7 +59,7 @@ require('./main_process/main_graphqlController')();
6059
require('./main_process/main_grpcController.js')();
6160
require('./main_process/main_wsController.js')();
6261
require('./main_process/main_mockController.js')();
63-
62+
require('./main_process/main_trpcController.js')();
6463

6564
// require mac touchbar
6665
const { touchBar } = require('./main_process/main_touchbar.js');
@@ -218,8 +217,6 @@ app.on('ready', () => {
218217
}
219218
});
220219

221-
222-
223220
// Quit when all windows are closed.
224221
app.on('window-all-closed', () => {
225222
if (mockServerProcess) {
@@ -427,9 +424,13 @@ ipcMain.on('import-proto', (event) => {
427424
if (err) {
428425
return console.log('import-proto error reading file : ', err);
429426
}
430-
427+
431428
protoParserFunc(importedProto).then((protoObj) => {
432-
mainWindow.webContents.send('proto-info', JSON.stringify(importedProto), JSON.stringify(protoObj));
429+
mainWindow.webContents.send(
430+
'proto-info',
431+
JSON.stringify(importedProto),
432+
JSON.stringify(protoObj)
433+
);
433434
});
434435
});
435436
})
@@ -442,8 +443,11 @@ ipcMain.on('import-proto', (event) => {
442443
// Runs the function and returns the value back to GRPCProtoEntryForm
443444
ipcMain.on('protoParserFunc-request', async (event, data) => {
444445
try {
445-
const result = await protoParserFunc(data)
446-
mainWindow.webContents.send('protoParserFunc-return', JSON.stringify(result));
446+
const result = await protoParserFunc(data);
447+
mainWindow.webContents.send(
448+
'protoParserFunc-return',
449+
JSON.stringify(result)
450+
);
447451
} catch (err) {
448452
console.log('error in protoParserFunc-request:, ', err);
449453
mainWindow.webContents.send('protoParserFunc-return', { error: err });
@@ -464,28 +468,30 @@ ipcMain.on('import-openapi', (event) => {
464468
.then((filePaths) => {
465469
if (!filePaths) return undefined;
466470
// read uploaded document & save in the redux store
467-
fs.readFile(filePaths.filePaths[0], 'utf-8', async (err, importedFile) => {
468-
// handle read error
469-
if (err) {
470-
return console.log('import-openapi error reading file : ', err);
471+
fs.readFile(
472+
filePaths.filePaths[0],
473+
'utf-8',
474+
async (err, importedFile) => {
475+
// handle read error
476+
if (err) {
477+
return console.log('import-openapi error reading file : ', err);
478+
}
479+
480+
try {
481+
const documentObj = await openapiParserFunc(importedFile);
482+
// console.log('Main.js - Working here!',documentObj);
483+
mainWindow.webContents.send('openapi-info', documentObj);
484+
} catch (err) {
485+
return console.log('import-openapi error reading file : ', err);
486+
}
471487
}
472-
473-
try {
474-
const documentObj = await openapiParserFunc(importedFile);
475-
// console.log('Main.js - Working here!',documentObj);
476-
mainWindow.webContents.send('openapi-info', documentObj);
477-
} catch (err) {
478-
return console.log('import-openapi error reading file : ', err);
479-
}
480-
481-
});
488+
);
482489
})
483490
.catch((err) => {
484491
console.log('error in import-openapi', err);
485492
});
486493
});
487494

488-
489495
/////////////////////////////////////////////////////////////////////////////////
490496
/////////////////////////////// MOCK SERVER //////////////////////////////////////
491497
/////////////////////////////////////////////////////////////////////////////////
@@ -513,3 +519,8 @@ ipcMain.on('stop-mock-server', () => {
513519
console.log('No mock server to kill');
514520
}
515521
});
522+
523+
ipcMain.on('error', (event, errorMessage) => {
524+
console.log(errorMessage);
525+
dialog.showErrorBox('Title', errorMessage);
526+
});

main_process/main_httpController.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ const httpController = {
191191
reqResObj.connectionType = 'plain';
192192
}
193193

194-
// FIXME: There is something wrong with the logic below.
195-
// First, it is checking if the headers have a 'content-length'
196-
// or 'Content-Length' property. If it does not, it sets the
197-
// responseSize to null. It it does, it checks if the headers have
198-
// a 'content-length' property. If it does, it sets the contentLength
199-
// to 'content-length' or 'Content-Length'. This does not make sense.
200-
// I think it should be setting contentLength to headers['content-length']
194+
// FIXME: There is something wrong with the logic below.
195+
// First, it is checking if the headers have a 'content-length'
196+
// or 'Content-Length' property. If it does not, it sets the
197+
// responseSize to null. It it does, it checks if the headers have
198+
// a 'content-length' property. If it does, it sets the contentLength
199+
// to 'content-length' or 'Content-Length'. This does not make sense.
200+
// I think it should be setting contentLength to headers['content-length']
201201
// or headers['Content-Length'].
202202

203203
// check if response comes with 'content-length' header
@@ -209,14 +209,14 @@ const httpController = {
209209
? (contentLength = 'content-length')
210210
: (contentLength = 'Content-Length');
211211

212-
// FIXME: A previous group used a conversion figure of 1023.89427 to
213-
// convert octets to bytes. This is incorrect because both an octet
214-
// and byte are exactly 8 bits in modern computing. There could, however,
215-
// be some ambiguity because "bytes" may have a different meaning in legacy
216-
// systems. Check out this link for more info: https://en.wikipedia.org/wiki/Octet_(computing).
217-
// If the desired responseSize is in bytes, it is enough to simply assign the value
212+
// FIXME: A previous group used a conversion figure of 1023.89427 to
213+
// convert octets to bytes. This is incorrect because both an octet
214+
// and byte are exactly 8 bits in modern computing. There could, however,
215+
// be some ambiguity because "bytes" may have a different meaning in legacy
216+
// systems. Check out this link for more info: https://en.wikipedia.org/wiki/Octet_(computing).
217+
// If the desired responseSize is in bytes, it is enough to simply assign the value
218218
// of the content-length header.
219-
219+
220220
// Converting content length octets into bytes
221221
const conversionFigure = 1023.89427;
222222
const octetToByteConversion =
@@ -295,7 +295,6 @@ const httpController = {
295295
const { method, headers, body } = args.options;
296296
const response = await fetch(headers.url, { method, headers, body });
297297
const headersResponse = response.headers.raw();
298-
299298
if (headersResponse['content-type'][0].includes('stream')) {
300299
return {
301300
headers: headersResponse,
@@ -334,7 +333,6 @@ const httpController = {
334333
reqResObj.timeSent = Date.now();
335334

336335
const options = this.parseFetchOptionsFromReqRes(reqResObj);
337-
338336
//-----------------------------------------
339337
// Check if the URL provided is a stream
340338
//-----------------------------------------
@@ -404,7 +402,7 @@ const httpController = {
404402
cookies.forEach((cookie) => {
405403
const cookieString = `${cookie.key}=${cookie.value}`;
406404
// attach to formattedHeaders so options object includes this
407-
formattedHeaders.cookie = cookieString;
405+
formattedHeaders.cookie = formattedHeaders.cookie + ';' + cookieString;
408406
});
409407
}
410408

main_process/main_trpcController.js

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
const fetch = require('cross-fetch');
2+
const setCookie = require('set-cookie-parser');
3+
const { ipcMain } = require('electron');
4+
5+
const trpcController = {
6+
makeFetch: async function (event, reqRes, get, post) {
7+
try {
8+
reqRes.timeSent = Date.now();
9+
const cookies = [];
10+
const getHeaders = {};
11+
const postHeaders = {};
12+
const events = [];
13+
14+
const reqArr = [get, post];
15+
const resArr = await Promise.all(
16+
reqArr.map((req) => {
17+
if (req) {
18+
return fetch(req.url, {
19+
// credentials: req.credentials,
20+
// mode: req.mode,
21+
cache: req.cache,
22+
headers: req.headers,
23+
method: req.method,
24+
redirect: req.redirect,
25+
referrer: req.referrer,
26+
...(req.body && { body: JSON.stringify(req.body) }),
27+
});
28+
} else {
29+
return Promise.resolve(false);
30+
}
31+
})
32+
);
33+
reqRes.timeReceived = Date.now();
34+
resArr.forEach((res, index) => {
35+
if (res) {
36+
const headersResponse = res.headers.raw();
37+
if (headersResponse['set-cookie']) {
38+
cookies.push(
39+
...this.cookieFormatter(
40+
setCookie.parse(headersResponse['set-cookie'])
41+
)
42+
);
43+
}
44+
if (index === 0) {
45+
res.headers.forEach((value, key) => {
46+
getHeaders[key] = value;
47+
});
48+
} else {
49+
res.headers.forEach((value, key) => {
50+
postHeaders[key] = value;
51+
});
52+
}
53+
}
54+
});
55+
56+
const resData = await Promise.all(
57+
resArr.map((res) => {
58+
return res ? res.json() : res;
59+
})
60+
);
61+
console.dir(resData, { depth: null });
62+
resData.forEach((res) => events.push(res));
63+
reqRes.response.cookies = cookies;
64+
reqRes.response.events = events;
65+
reqRes.response.headers = [getHeaders, postHeaders];
66+
reqRes.connection = 'closed';
67+
reqRes.connectionType = 'plain';
68+
event.sender.send('reqResUpdate', reqRes);
69+
} catch (error) {
70+
reqRes.connection = 'error';
71+
reqRes.error = error;
72+
reqRes.response.events.push(error);
73+
event.sender.send('reqResUpdate', reqRes);
74+
}
75+
},
76+
parseOptionForFetch(reqResObject, method, procedures) {
77+
function parseString(str) {
78+
if (str === 'true') {
79+
return true;
80+
}
81+
82+
if (str === 'false') {
83+
return false;
84+
}
85+
86+
if (!isNaN(str)) {
87+
return parseFloat(str);
88+
}
89+
90+
try {
91+
const parsedJson = JSON.parse(str.replace(/\s/g, ''));
92+
93+
if (typeof parsedJson === 'object' && parsedJson !== null) {
94+
return parsedJson;
95+
} else {
96+
throw 'is String';
97+
}
98+
} catch (error) {
99+
return JSON.parse(str);
100+
}
101+
}
102+
const { headers, cookie } = reqResObject.request;
103+
104+
const formattedHeaders = {};
105+
headers.forEach((head) => {
106+
if (head.active) {
107+
formattedHeaders[head.key] = head.value;
108+
}
109+
});
110+
if (cookie) {
111+
cookie.forEach((cookie) => {
112+
const cookieString = `${cookie.key}=${cookie.value}`;
113+
// attach to formattedHeaders so options object includes this
114+
115+
formattedHeaders.cookie = formattedHeaders.cookie
116+
? formattedHeaders.cookie + ';' + cookieString
117+
: cookieString;
118+
});
119+
}
120+
121+
const outputObj = {
122+
method,
123+
mode: 'cors', // no-cors, cors, *same-origin
124+
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
125+
credentials: 'include', // include, *same-origin, omit
126+
headers: formattedHeaders,
127+
redirect: 'follow', // manual, *follow, error
128+
referrer: 'no-referrer', // no-referrer, *client
129+
};
130+
let url = '';
131+
const body = {};
132+
procedures.forEach((procedure, index) => {
133+
if (procedure.variable) {
134+
body[index] = parseString(procedure.variable);
135+
} else {
136+
body[index] = {};
137+
}
138+
url = url ? url + ',' + procedure.endpoint : procedure.endpoint;
139+
});
140+
if (method === 'POST') {
141+
url = reqResObject.url + '/' + url + '?batch=1';
142+
outputObj.body = body;
143+
} else {
144+
url =
145+
reqResObject.url +
146+
'/' +
147+
url +
148+
'?batch=1' +
149+
`&input=${encodeURIComponent(JSON.stringify(body))}`;
150+
}
151+
outputObj.url = url;
152+
return outputObj;
153+
},
154+
155+
openRequest: async function (event, reqRes) {
156+
// const reqRes = Object.assign({}, reqResOriginal);
157+
// reqRes.response = Object.assign({}, reqRes.response);
158+
const procedures = reqRes.request.procedures;
159+
const getReq = procedures.filter(
160+
(procedure) => procedure.method === 'QUERY'
161+
);
162+
const postReq = procedures.filter(
163+
(procedure) => procedure.method === 'MUTATE'
164+
);
165+
166+
const getOption = getReq.length
167+
? this.parseOptionForFetch(reqRes, 'GET', getReq)
168+
: false;
169+
const postOption = postReq.length
170+
? this.parseOptionForFetch(reqRes, 'POST', postReq)
171+
: false;
172+
173+
const updatedReqRes = await this.makeFetch(
174+
event,
175+
reqRes,
176+
getOption,
177+
postOption
178+
);
179+
},
180+
cookieFormatter(cookieArray) {
181+
return cookieArray.map((eachCookie) => {
182+
const cookieFormat = {
183+
name: eachCookie.name,
184+
value: eachCookie.value,
185+
domain: eachCookie.domain,
186+
hostOnly: eachCookie.hostOnly ? eachCookie.hostOnly : false,
187+
path: eachCookie.path,
188+
secure: eachCookie.secure ? eachCookie.secure : false,
189+
httpOnly: eachCookie.httpOnly ? eachCookie.httpOnly : false,
190+
session: eachCookie.session ? eachCookie.session : false,
191+
expirationDate: eachCookie.expires ? eachCookie.expires : '',
192+
};
193+
return cookieFormat;
194+
});
195+
},
196+
};
197+
198+
module.exports = () => {
199+
// create event listeners for IPC events
200+
ipcMain.on('open-trpc', (event, reqResObj) => {
201+
trpcController.openRequest(event, reqResObj);
202+
});
203+
};

0 commit comments

Comments
 (0)