Skip to content

Commit

Permalink
fix(electron): Totally mind-blowing ghost bug here for years
Browse files Browse the repository at this point in the history
  • Loading branch information
fguitton committed Oct 7, 2024
1 parent f0940c0 commit 29b4b59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
22 changes: 16 additions & 6 deletions packages/optimise-electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ const optimise_server = new optimiseCore({

let cookie;
const httpify = ({ url, options = {} }) => new Promise((resolve, reject) => {
if (options.method === undefined) options.method = 'GET';

if (options.method === undefined)
options.method = 'GET';

if (cookie !== undefined) {
if (options.headers === undefined) options.headers = {};
options.headers.cookie = cookie;
Expand Down Expand Up @@ -131,21 +134,28 @@ const httpify = ({ url, options = {} }) => new Promise((resolve, reject) => {
});

let type;
res.writeHead(res.statusCode);
// res.writeHead(res.statusCode);
Object.keys(res._headers).forEach((e) => {
if (e.toLowerCase() === 'set-cookie')
cookie = res._headers[e][0].split(';')[0];
if (e.toLowerCase() === 'content-type')
type = res._headers[e];
});

if (type.search('application/json') >= 0)
if (type.search('application/json') >= 0) {
let json = {
error: 'Could not parse JSON response'
};
try {
json = JSON.parse(res._sent.toString());
} catch (e) {
console.error(e);
}
resolve({
headers: res._headers,
statusCode: res.statusCode,
json: JSON.parse(res._sent.toString())
json
});
else {
} else {
resolve({
headers: res._headers,
statusCode: res.statusCode,
Expand Down
9 changes: 6 additions & 3 deletions packages/optimise-electron/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ ipcRenderer.on('update-message', function (__unused__event, message) {
});

ipcRenderer.on('optimiseApiResult', function (__unused__event, { cid, res }) {

callStack[cid]({
headers: new Headers(res.headers),
status: res.statusCode,
json: () => Promise.resolve(res.json)
headers: new Headers(res?.headers ?? {}),
status: res?.statusCode ?? 500,
json: () => Promise.resolve(res?.json ?? { error: 'An error occurred querying the API' })
});

delete callStack[cid];
});

window['ipcFetch'] = (url, options) => new Promise((resolve) => {

const cid = `${Math.random().toString(36).substr(2, 5)}`;
callStack[cid] = resolve;

Expand Down

0 comments on commit 29b4b59

Please sign in to comment.