Skip to content

Commit 2f9cc5f

Browse files
authored
Merge pull request #14 from oslabs-beta/paul-anthony/http1-sse
Paul anthony/http1 sse
2 parents 63186ab + 32a8667 commit 2f9cc5f

File tree

8 files changed

+93
-59
lines changed

8 files changed

+93
-59
lines changed

main.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function createWindow() {
138138
webPreferences: {
139139
"nodeIntegration": true,
140140
"sandbox": false,
141-
webSecurity: false,
141+
webSecurity: true,
142142
},
143143
icon: `${__dirname}/src/assets/icons/64x64.png`
144144
})
@@ -285,22 +285,26 @@ ipcMain.on('http1-fetch-message', (event, arg) => {
285285
.then((response) => {
286286
const headers = response.headers.raw();
287287
// check if the endpoint sends SSE
288-
if (headers['content-type'][0].includes('stream')) {
289-
event.sender.send('http1-fetch-reply', { headers, body: { error: 'This Is An SSE endpoint' } })
290-
} else {
291288
// add status code for regular http requests in the response header
292-
headers[':status'] = response.status;
293-
294-
const receivedCookie = headers['set-cookie'];
295-
headers.cookies = receivedCookie;
296-
297-
const contents = /json/.test(response.headers.get('content-type')) ? response.json() : response.text();
298-
contents
299-
.then(body => {
300-
event.sender.send('http1-fetch-reply', { headers, body })
301-
})
302-
.catch(error => console.log('ERROR', error))
303-
}
289+
290+
if (headers['content-type'][0].includes('stream')) {
291+
// invoke another func that fetches to SSE and reads stream
292+
// params: method, headers, body
293+
event.sender.send('http1-fetch-reply', { headers, body: { error: 'This Is An SSE endpoint' } })
294+
}
295+
else {
296+
headers[':status'] = response.status;
297+
298+
const receivedCookie = headers['set-cookie'];
299+
headers.cookies = receivedCookie;
300+
301+
const contents = /json/.test(response.headers.get('content-type')) ? response.json() : response.text();
302+
contents
303+
.then(body => {
304+
event.sender.send('http1-fetch-reply', { headers, body })
305+
})
306+
.catch(error => console.log('ERROR', error))
307+
}
304308
})
305309
.catch(error => console.log(error))
306310
})

src/client/actions/actionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const SET_NEW_REQUEST_FIELDS = "SET_NEW_REQUEST_FIELDS";
1818
export const SET_NEW_REQUEST_HEADERS = "SET_NEW_REQUEST_HEADERS";
1919
export const SET_NEW_REQUEST_BODY = "SET_NEW_REQUEST_BODY";
2020
export const SET_NEW_REQUEST_COOKIES = "SET_NEW_REQUEST_COOKIES";
21+
export const SET_NEW_REQUEST_SSE = "SET_NEW_REQUEST_SSE";
2122

2223
export const SET_CURRENT_TAB = "SET_CURRENT_TAB";
2324

src/client/actions/actions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export const setNewRequestCookies = (cookies) => ({
7676
payload : cookies
7777
});
7878

79+
export const setNewRequestSSE = (SSEBool) => ({
80+
type: types.SET_NEW_REQUEST_SSE,
81+
payload : SSEBool
82+
});
83+
7984
export const setCurrentTab = (tab) => ({
8085
type: types.SET_CURRENT_TAB,
8186
payload: tab,

src/client/components/composer/ComposerContainer.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ const mapStateToProps = store => ({
1212
newRequestHeaders: store.business.newRequestHeaders,
1313
newRequestBody: store.business.newRequestBody,
1414
newRequestCookies: store.business.newRequestCookies,
15+
newRequestSSE: store.business.newRequestSSE,
1516
currentTab: store.business.currentTab,
16-
warningMessage: store.business.warningMessage,
17+
warningMessage: store.business.warningMessage
1718
});
1819

1920
const mapDispatchToProps = dispatch => ({
@@ -24,6 +25,7 @@ const mapDispatchToProps = dispatch => ({
2425
setNewRequestFields: (requestFields) => { dispatch(actions.setNewRequestFields(requestFields)) },
2526
setNewRequestBody: (requestBodyObj) => { dispatch(actions.setNewRequestBody(requestBodyObj)) },
2627
setNewRequestCookies: (requestCookiesObj) => { dispatch(actions.setNewRequestCookies(requestCookiesObj)) },
28+
setNewRequestSSE: (requestSSEBool) => { dispatch(actions.setNewRequestSSE(requestSSEBool)) }
2729
});
2830

2931
class ComposerContainer extends Component {
@@ -56,6 +58,7 @@ class ComposerContainer extends Component {
5658
newRequestHeaders={this.props.newRequestHeaders}
5759
newRequestCookies={this.props.newRequestCookies}
5860
newRequestBody={this.props.newRequestBody}
61+
newRequestSSE={this.props.newRequestSSE}
5962
currentTab={this.props.currentTab}
6063

6164
reqResAdd={this.props.reqResAdd}
@@ -67,6 +70,7 @@ class ComposerContainer extends Component {
6770
setNewRequestHeaders={this.props.setNewRequestHeaders}
6871
setNewRequestCookies={this.props.setNewRequestCookies}
6972
setNewRequestBody={this.props.setNewRequestBody}
73+
setNewRequestSSE={this.props.setNewRequestSSE}
7074
/>;
7175
break;
7276
}

src/client/components/composer/NewRequest/ComposerNewRequest.jsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@ import GraphQLBodyEntryForm from "./GraphQLBodyEntryForm.jsx";
66
import FieldEntryForm from "./FieldEntryForm.jsx";
77
import CookieEntryForm from './CookieEntryForm.jsx';
88
import historyController from '../../../controllers/historyController'
9+
import { CLIENT_RENEG_LIMIT } from 'tls';
10+
911

1012
class ComposerNewRequest extends Component {
1113
constructor(props) {
1214
super(props);
15+
1316
this.addNewRequest = this.addNewRequest.bind(this);
17+
this.handleSSEPayload = this.handleSSEPayload.bind(this);
18+
}
19+
20+
componentDidMount(){
21+
console.log('this.props.newRequestSSE.isSSE: ', this.props.newRequestSSE.isSSE);
22+
1423
}
1524

1625
requestValidationCheck() {
@@ -35,6 +44,10 @@ class ComposerNewRequest extends Component {
3544
return validationMessage || true;
3645
}
3746

47+
handleSSEPayload(e){
48+
this.props.setNewRequestSSE(e.target.checked);
49+
}
50+
3851
addNewRequest() {
3952
const validated = this.requestValidationCheck();
4053

@@ -61,6 +74,8 @@ class ComposerNewRequest extends Component {
6174
let historyBodyVariables;
6275
if (document.querySelector('#gqlVariableEntryTextArea')) { historyBodyVariables = document.querySelector('#gqlVariableEntryTextArea').value } //grabs the input value in case tab was last key pressed
6376
else historyBodyVariables = '';
77+
// console.log('in add new request, this.props.newRequestSSE: ', this.props.newRequestSSE)
78+
6479
reqRes = {
6580

6681
id: uuid(),
@@ -83,7 +98,8 @@ class ComposerNewRequest extends Component {
8398
body: historyBodyContent,
8499
bodyType: this.props.newRequestBody.bodyType,
85100
bodyVariables: historyBodyVariables,
86-
rawType: this.props.newRequestBody.rawType
101+
rawType: this.props.newRequestBody.rawType,
102+
isSSE: this.props.newRequestSSE.isSSE,
87103
},
88104
response: {
89105
headers: null,
@@ -199,7 +215,8 @@ class ComposerNewRequest extends Component {
199215
protocol: '',
200216
url: '',
201217
graphQL: false
202-
})
218+
});
219+
this.props.setNewRequestSSE(false);
203220
}
204221
else {
205222
this.props.setComposerWarningMessage(validated);
@@ -269,6 +286,14 @@ class ComposerNewRequest extends Component {
269286
/>
270287
}
271288

289+
{/* SSE CHeckbox, update newRequestSSE in store */}
290+
{
291+
!this.props.newRequestFields.graphQL
292+
&& !/wss?:\/\//.test(this.props.newRequestFields.protocol)
293+
&&
294+
<div>Server Sent Event: <input type="checkbox" onChange={this.handleSSEPayload} checked={this.props.newRequestSSE.isSSE}/></div>
295+
}
296+
272297
<button className={SubmitButtonClassName} onClick={this.addNewRequest} type="button">
273298
Add New Request
274299
</button>

src/client/components/composer/NewRequest/HeaderEntryForm.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ class HeaderEntryForm extends Component {
160160
/>
161161
));
162162

163-
const arrowClass = this.state.show ? 'composer_subtitle_arrow-open' : 'composer_subtitle_arrow-closed';
164-
const headersContainerClass = this.state.show ? 'composer_headers_container-open' : 'composer_headers_container-closed'
163+
const arrowClass = this.state.show ? 'composer_subtitle_arrow-closed' : 'composer_subtitle_arrow-open';
164+
const headersContainerClass = this.state.show ? 'composer_headers_container-closed' : 'composer_headers_container-open'
165165

166166
return <div style={this.props.stylesObj}>
167167
<div

src/client/controllers/httpController.js

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const httpController = {
3434
/*
3535
* TRY TO CONNECT AS HTTP2 FIRST IF HTTPS. If error, fallback to HTTP1.1 (WebAPI fetch)
3636
*/
37+
console.log('Inside openHTTPconnection in controller, reqResObj.request: ', reqResObj);
3738
if (reqResObj.protocol === 'https://' || reqResObj.protocol === 'http://') {
3839
console.log('HTTPS, TRYING HTTP2');
3940
httpController.establishHTTP2Connection(reqResObj, connectionArray);
@@ -281,50 +282,34 @@ const httpController = {
281282
//--------------------------------------------------------------------------------------------------------------
282283
// Check if the URL provided is a stream
283284
//--------------------------------------------------------------------------------------------------------------
284-
285+
286+
// if isSSE is true, then node-fetch to stream,
287+
if (reqResObj.request.isSSE) {
288+
// invoke another func that fetches to SSE and reads stream
289+
// params: method, headers, body
290+
const { method, headers, body } = options;
291+
292+
fetch2(headers.url, { method, headers, body })
293+
.then(response => {
294+
const heads = {};
295+
for (const entry of response.headers.entries()) {
296+
heads[entry[0].toLowerCase()] = entry[1];
297+
}
298+
reqResObj.response.headers = heads;
299+
this.handleSSE(response, reqResObj, heads);
300+
})
301+
}
302+
// if not SSE, talk to main to fetch data and receive
303+
else {
285304
// send information to the NODE side to do the fetch request
286305
this.sendToMainForFetch({ options })
287306
.then((response) => {
288-
console.log('response from main fetch', response.headers);
307+
console.log('Response from main fetch:', response.headers);
289308

290309
// Parse response headers now to decide if SSE or not.
291310
const heads = response.headers;
292-
293311
reqResObj.response.headers = heads;
294312

295-
// store extracted headers in heads object
296-
// check if the content-type header contains the word stream
297-
let isStream = false;
298-
299-
if (heads['content-type'] && heads['content-type'].includes('stream')) isStream = true;
300-
301-
if (isStream) { // if url is sse... //! <REVISIT>
302-
const http1Sesh = session.defaultSession;
303-
let domain = reqResObj.host.split('//');
304-
domain.shift();
305-
[domain] = domain.join('').split('.').splice(-2).join('.')
306-
.split(':');
307-
308-
http1Sesh.cookies.get({ domain }, (err, cookies) => {
309-
if (cookies) {
310-
reqResObj.response.cookies = cookies;
311-
store.default.dispatch(actions.reqResUpdate(reqResObj));
312-
cookies.forEach((cook) => {
313-
let url = '';
314-
url += cook.secure ? 'https://' : 'http://';
315-
url += cook.domain.charAt(0) === '.' ? 'www' : '';
316-
url += cook.domain;
317-
url += cook.path;
318-
319-
http1Sesh.cookies.remove(url, cook.name, x => console.log(x));
320-
});
321-
}
322-
this.handleSSE(response, reqResObj, heads);
323-
});
324-
}
325-
326-
else { // if url is not not sse ...
327-
328313
reqResObj.timeSent = Date.now();
329314
store.default.dispatch(actions.reqResUpdate(reqResObj));
330315

@@ -339,12 +324,12 @@ const httpController = {
339324
store.default.dispatch(actions.reqResUpdate(reqResObj));
340325
this.handleSingleEvent(body, reqResObj, theResponseHeaders);
341326
}
342-
}
343327
})
344328
.catch((err) => {
345329
reqResObj.connection = 'error';
346330
store.default.dispatch(actions.reqResUpdate(reqResObj));
347331
});
332+
}
348333
},
349334

350335
// ----------------------------------------------------------------------------
@@ -402,7 +387,6 @@ const httpController = {
402387
/* handle SSE Streams for HTTP1.1 */
403388
handleSSE(response, originalObj, headers) {
404389
const reader = response.body.getReader();
405-
406390
let data = '';
407391
read();
408392

src/client/reducers/business.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const initialState = {
2929
rawType: 'Text (text/plain)',
3030
JSONFormatted: true,
3131
},
32+
newRequestSSE: {
33+
isSSE: false
34+
}
3235
};
3336

3437
const businessReducer = (state = initialState, action) => {
@@ -201,6 +204,14 @@ const businessReducer = (state = initialState, action) => {
201204
};
202205
}
203206

207+
case types.SET_NEW_REQUEST_SSE: {
208+
console.log('action.payload',action.payload)
209+
return {
210+
...state,
211+
newRequestSSE: {isSSE: action.payload},
212+
};
213+
}
214+
204215
case types.SET_CURRENT_TAB: {
205216
return {
206217
...state,

0 commit comments

Comments
 (0)