Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use IPC bridge for fs relating to responses #8311

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

jackkav
Copy link
Contributor

@jackkav jackkav commented Jan 22, 2025

Motivation: it was identified in an earlier PR to reduce the complexity of the getBodyBuffer and some of the workarounds build to wrap it.

Approach: remove it and any wrappers, replace with fs readFile and a main bridge in the renderer.

TODO:

  • replace getBodyBuffer
  • fix runner and pre-req stuff
  • fix types
  • review useEffect usage in ResponseViewer

future work:

  • scope getBodyStream
  • render flash caused by useEffect

Takeaways:

  • most of the time we just read the response directly as utf8
  • in the response template tag plugin the encoding can also be selected
  • in the response viewer we don't try to render responses over a given size, unless a button is clicked
  • as above but above a greater size will try to render using a filestream, for which we don't have tests

given these takeaways its worth considering what would happen in the reset of the system given an oversized response.

Option 1: continue to ignore these cases
Option 2: build a file reading abstraction in main which avoids loading large files into chromium, by offering to open to file in another application.

@jackkav jackkav changed the title Feature/ins-3621-revamp-getbodybuffer-for-reading-response use IPC bridge for fs relating to responses Feb 3, 2025
@jackkav jackkav requested a review from gatzjames February 5, 2025 11:55
@jackkav jackkav marked this pull request as ready for review February 5, 2025 11:55
Comment on lines -233 to -268
export const readCurlResponse = async (options: { bodyPath?: string; bodyCompression?: Compression }) => {
const readFailureMsg = '[main/curlBridgeAPI] failed to read response body message';
const bodyBufferOrErrMsg = getBodyBuffer(options, readFailureMsg);
// TODO(jackkav): simplify the fail msg and reuse in other getBodyBuffer renderer calls

if (!bodyBufferOrErrMsg) {
return { body: '', error: readFailureMsg };
} else if (typeof bodyBufferOrErrMsg === 'string') {
if (bodyBufferOrErrMsg === readFailureMsg) {
return { body: '', error: readFailureMsg };
}
return { body: '', error: `unknown error in loading response body: ${bodyBufferOrErrMsg}` };
}

return { body: bodyBufferOrErrMsg.toString('utf8'), error: '' };
};
export const getBodyBuffer = (
response?: { bodyPath?: string; bodyCompression?: Compression },
readFailureValue?: string,
): Buffer | string | null => {
if (!response?.bodyPath) {
// No body, so return empty Buffer
return Buffer.alloc(0);
}
try {
const rawBuffer = fs.readFileSync(response?.bodyPath);
if (response?.bodyCompression === 'zip') {
return zlib.gunzipSync(rawBuffer);
} else {
return rawBuffer;
}
} catch (err) {
console.warn('Failed to read response body', err.message);
return readFailureValue === undefined ? null : readFailureValue;
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡

Copy link
Contributor

@gatzjames gatzjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
Not sure if there's anything better we can do for now for the useEffect.
In react 19 we can replace this with the use function or we could come up with a way to read the data using the defer api from react-router but that will also be simpler in v7.
Another idea could be to use file://{PATH} urls and get read this using fetch and the electron api protocol and remove the need for the bridge api but it wouldn't change the ergonomics of how we read this

gatzjames
gatzjames previously approved these changes Feb 5, 2025
@jackkav
Copy link
Contributor Author

jackkav commented Feb 5, 2025

Not sure if there's anything better we can do for now for the useEffect.
We could check the size of the body in the loader and if smaller than the setting, then prop drill it down. Alternatively I could keep the body state outside the component somewhere else, but not sure if the juice is worth the squeeze.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants