Skip to content

Commit

Permalink
Merge pull request #300 from oslabs-beta/master
Browse files Browse the repository at this point in the history
Swell Update v.0.9.0
  • Loading branch information
pauleramirez authored May 14, 2021
2 parents ed852fb + f4d693d commit 9575b0e
Show file tree
Hide file tree
Showing 53 changed files with 1,611 additions and 802 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Swell is a one-stop shop for sending and monitoring your API requests
- **Miguel Gonzalez** - [MigGonzalez](https://github.com/MigGonzalez)
- **Jason Liggayu** - [jasonligg](https://github.com/jasonligg)
- **Warren Tait** - [whtait](https://github.com/whtait)
- **Nathan Fleming** - [njfleming](https://github.com/njfleming)
- **Konrad Kopko** - [konradkop](https://github.com/konradkop)
- **Andrea Li** - [Andrea-gli](https://github.com/Andrea-gli)
- **Paul Ramirez** - [pauleramirez](https://github.com/pauleramirez)
- **TJ Wetmore** - [TWetmore](https://github.com/TWetmore)

## License

Expand Down
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1;
// npm libraries
// debugger
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
//require("react-devtools-electron");
const {
default: installExtension,
REACT_DEVELOPER_TOOLS,
Expand Down Expand Up @@ -101,7 +102,7 @@ function createWindow() {
// allowRunningInsecureContent: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: process.env.NODE_ENV !== "test",
contextIsolation: process.env.NODE_ENV !== "test", //true if in dev mode
// enableRemoteModule: false,
sandbox: process.env.NODE_ENV !== "test",
webSecurity: true,
Expand Down Expand Up @@ -203,6 +204,7 @@ const sendStatusToWindow = (text) => {

ipcMain.on("check-for-update", () => {
//listens to ipcRenderer in UpdatePopUpContainer.jsx
console.log("check for update");
if (!isDev) {
autoUpdater.checkForUpdates();
}
Expand Down Expand Up @@ -350,7 +352,6 @@ ipcMain.on("import-collection", (event, args) => {
event.sender.send("add-collection", JSON.stringify(JSON.parse(data)));
});
});
//.catch( err => console.log('error in import-collection', err));
});

// ============ CONFIRM CLEAR HISTORY / RESPONSE COMMUNICATION ===============
Expand Down
20 changes: 17 additions & 3 deletions main_process/main_httpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const httpController = {
// ----------------------------------------------------------------------------

openHTTPconnection(event, reqResObj) {
//console.log("event=>", event);
// HTTP2 currently only on HTTPS
if (reqResObj.protocol === "https://") {
httpController.establishHTTP2Connection(event, reqResObj);
Expand Down Expand Up @@ -266,14 +267,19 @@ const httpController = {
// ----------------------------------------------------------------------------

makeFetch(args, event, reqResObj) {
console.log("args===>", args);
console.log("event===>", event);
console.log("reqRESSSSOBJ===>", reqResObj);
return new Promise((resolve) => {
const { method, headers, body } = args.options;

fetch2(headers.url, { method, headers, body })
.then((response) => {
console.log("responsefromendpoint====>", response);
const headers = response.headers.raw();

console.log("headersfromfetch==>", headers);
// check if the endpoint sends SSE
// add status code for regular http requests in the response header
// add status-==== code for regular http requests in the response header
if (headers["content-type"][0].includes("stream")) {
// invoke another func that fetches to SSE and reads stream
// params: method, headers, body
Expand All @@ -285,13 +291,17 @@ const httpController = {
headers[":status"] = response.status;

const receivedCookie = headers["set-cookie"];
console.log("receivedCookie===>", receivedCookie);
headers.cookies = receivedCookie;

console.log("newheaders==>", headers);
const contents = /json/.test(response.headers.get("content-type"))
? response.json()
: response.text();

contents
.then((body) => {
console.log("bodyyyy====>", body);

resolve({
headers,
body,
Expand Down Expand Up @@ -333,6 +343,7 @@ const httpController = {
} else {
this.makeFetch({ options }, event, reqResObj)
.then((response) => {
console.log("makefetchResponse===>", response);
// Parse response headers now to decide if SSE or not.
const heads = response.headers;
reqResObj.response.headers = heads;
Expand All @@ -354,6 +365,7 @@ const httpController = {
}
// update reqres object to include new event
reqResObj = this.addSingleEvent(body, reqResObj);
console.log("latest REQRESOBJ=>", reqResObj);
// check if there is a test script to run
if (reqResObj.request.testContent) {
reqResObj.response.testResult = testingController.runTest(
Expand All @@ -376,6 +388,8 @@ const httpController = {

parseFetchOptionsFromReqRes(reqResObject) {
const { headers, body, cookies } = reqResObject.request;
console.log("reqresOBJJJ=>", reqResObject);

let { method } = reqResObject.request;

method = method.toUpperCase();
Expand Down
2 changes: 2 additions & 0 deletions main_process/main_testingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ testHttpController.runTest = (
if (/^let|^const|^var/.test(script)) {
variables = script;
}

//assert.strictEqual(response.status, 200, 'response is 200')
return `
${variables}
try {
Expand Down
261 changes: 166 additions & 95 deletions main_process/main_wsController.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,173 @@
const { ipcMain } = require("electron");
const { dialog } = require("electron");
// const store = require('./src/client/store.js')
const WebSocketClient = require('websocket').client
const WebSocketClient = require("websocket").client;
const fs = require("fs");
const path = require("path");
const testingController = require("./main_testingController");

const wsController = {
wsConnect: null,
wsConnect: null,
openWSconnection(event, reqResObj, connectionArray) {
//set reqResObj for WS
reqResObj.response.messages = [];
reqResObj.request.messages = [];
reqResObj.connection = 'pending';
reqResObj.closeCode = 0;
reqResObj.timeSent = Date.now();

//update frontend its pending
event.sender.send("reqResUpdate", reqResObj);

//create socket
let socket;
try {
socket = new WebSocketClient()
}
catch (err) {
reqResObj.connection = 'error';
event.sender.send("reqResUpdate", reqResObj);
return;
}

//when it connects, update connectionArray
socket.on('connect', (connection) => {
this.wsConnect = connection;
reqResObj.connection = 'open';
const openConnectionObj = {
connection,
protocol: 'WS',
id: reqResObj.id,
};
connectionArray.push(openConnectionObj);
event.sender.send("update-connectionArray", connectionArray);
event.sender.send("reqResUpdate", reqResObj);
this.wsConnect.on('close', () => {
console.log('closed WS');
});
});

//listener for failed socket connection,
socket.on('connectFailed', (error) => {
console.log('WS Connect Error: ' + error.toString());
reqResObj.connection = "error";
reqResObj.timeReceived = Date.now();
// reqResObj.response.events.push(JSON.stringify(errorsObj));
event.sender.send("reqResUpdate", reqResObj);
});

//connect socket
socket.connect(reqResObj.url);
},

closeWs(event) {
this.wsConnect.close();
},

sendWebSocketMessage(event, reqResObj, inputMessage) {
//send message to ws server
this.wsConnect.send(inputMessage);

//push sent message to reqResObj message array as a request message
reqResObj.request.messages.push({
data: inputMessage,
timeReceived: Date.now(),
});

//update store
event.sender.send("reqResUpdate", reqResObj);

//listener for return message from ws server
//push into message array under responses
this.wsConnect.on('message', (e) => {
reqResObj.response.messages.push({
data: e.utf8Data,
timeReceived: Date.now(),
});
//update store
event.sender.send("reqResUpdate", reqResObj);
});
},
//set reqResObj for WS
reqResObj.response.messages = [];
reqResObj.request.messages = [];
reqResObj.connection = "pending";
reqResObj.closeCode = 0;
reqResObj.timeSent = Date.now();

//update frontend its pending
event.sender.send("reqResUpdate", reqResObj);

//create socket
//check websocket npm package doc
let socket;
try {
socket = new WebSocketClient();
} catch (err) {
reqResObj.connection = "error";
event.sender.send("reqResUpdate", reqResObj);
return;
}

//when it connects, update connectionArray
//connection here means a single connection being established
socket.on("connect", (connection) => {
console.log("websocket client connected");
this.wsConnect = connection;
reqResObj.connection = "open";
reqResObj.response.connection = "open";
// testingController.runTest(reqResObj.request.testContent, reqResObj);
const openConnectionObj = {
connection,
protocol: "WS",
id: reqResObj.id,
};
connectionArray.push(openConnectionObj);
event.sender.send("update-connectionArray", connectionArray);
event.sender.send("reqResUpdate", reqResObj);
//connection.on
this.wsConnect.on("close", () => {
console.log("closed WS");
});
});

//listener for failed socket connection,
socket.on("connectFailed", (error) => {
console.log("WS Connect Error: " + error.toString());
reqResObj.connection = "error";
reqResObj.timeReceived = Date.now();
// reqResObj.response.events.push(JSON.stringify(errorsObj)); need to move
event.sender.send("reqResUpdate", reqResObj);
});

//connect socket
socket.connect(reqResObj.url);
},

closeWs(event) {
//connection.close
this.wsConnect.close();
},

sendWebSocketMessage(event, reqResObj, inputMessage) {
//send message to ws server
//connection.send

//check datatype

if (inputMessage.includes("data:image/")) {
const buffer = Buffer.from(inputMessage, "utf8");
console.log("sending as buffer");
this.wsConnect.sendBytes(buffer);
reqResObj.request.messages.push({
data: buffer,
timeReceived: Date.now(),
});
} else {
this.wsConnect.send(inputMessage);
console.log("sending as string");
reqResObj.request.messages.push({
data: inputMessage,
timeReceived: Date.now(),
});
}

//update store
event.sender.send("reqResUpdate", reqResObj);

//listener for return message from ws server
//push into message array under responses
//connection.on
this.wsConnect.on("message", (e) => {
e.binaryData
? reqResObj.response.messages.push({
data: e.binaryData,
timeReceived: Date.now(),
})
: reqResObj.response.messages.push({
data: e.utf8Data,
timeReceived: Date.now(),
});

if (reqResObj.request.testContent) {
reqResObj.response.testResult = testingController.runTest(
reqResObj.request.testContent,
reqResObj
);
console.log("the test result", reqResObj.response.testResult);
}

//update store
event.sender.send("reqResUpdate", reqResObj);
});
},
};
module.exports = () => {
// we pass the event object into these controller functions so that we can invoke event.sender.send when we need to make response to renderer process
// listener to open wsconnection
ipcMain.on("open-ws", (event, reqResObj, connectionArray) => {
wsController.openWSconnection(event, reqResObj, connectionArray);
});
//listener for sending messages to server
ipcMain.on("send-ws", (event, reqResObj, inputMessage) => {
wsController.sendWebSocketMessage(event, reqResObj, inputMessage);
});
//listerner to close socket connection
ipcMain.on("close-ws", (event) => {
wsController.closeWs(event);
})
};
// we pass the event object into these controller functions so that we can invoke event.sender.send when we need to make response to renderer process
// listener to open wsconnection
ipcMain.on("open-ws", (event, reqResObj, connectionArray) => {
wsController.openWSconnection(event, reqResObj, connectionArray);
});
//listener for sending messages to server
ipcMain.on("send-ws", (event, reqResObj, inputMessage) => {
wsController.sendWebSocketMessage(event, reqResObj, inputMessage);
});
//listerner to close socket connection
ipcMain.on("close-ws", (event) => {
wsController.closeWs(event);
});
ipcMain.on("exportChatLog", (event, outgoingMessages, incomingMessages) => {
//making sure the messages are in order
let result = outgoingMessages
.map((message) => {
message.source = "client";
return message;
})
.concat(
incomingMessages.map((message) => {
message.source = "server";
return message;
})
)
.sort((a, b) => a.timeReceived - b.timeReceived)
.map((message, index) => ({
index: index,
source: message.source,
data: message.data,
timeReceived: message.timeReceived,
}));

const data = new Uint8Array(Buffer.from(JSON.stringify(result)));

//showSaveDialog is the windowexplorer that appears
dialog
.showSaveDialog({ defaultPath: "websocketLog.txt" })
.then((file_path) => {
fs.writeFile(file_path.filePath, data, (err) => {
if (err) throw err;
console.log("File saved to: ", file_path.filePath);
});
});
});
};
Loading

0 comments on commit 9575b0e

Please sign in to comment.