feat: Use real content size, fix runner, use the correct content charset

This commit is contained in:
Its-treason 2023-10-17 20:53:51 +02:00
parent 187e482feb
commit b3ee0af226
4 changed files with 32 additions and 16 deletions

View File

@ -43,11 +43,10 @@ const QueryResultPreview = ({
); );
} }
case 'preview-image': { case 'preview-image': {
return <img src={`data:${contentType.replace(/\;(.*)/, '')};base64,${dataBuffer}`} />; return <img src={`data:${contentType.replace(/\;(.*)/, '')};base64,${dataBuffer}`} className="mx-auto" />;
} }
default: default:
case 'raw': { case 'raw': {
console.log(mode, storedTheme);
return ( return (
<CodeEditor <CodeEditor
collection={collection} collection={collection}

View File

@ -72,6 +72,7 @@ const QueryResult = ({ item, collection, data, width, disableRunEventListener, h
className={classnames('select-none capitalize', previewMode === previewTab ? 'active' : 'cursor-pointer')} className={classnames('select-none capitalize', previewMode === previewTab ? 'active' : 'cursor-pointer')}
role="tab" role="tab"
onClick={() => setPreviewTab(previewMode)} onClick={() => setPreviewTab(previewMode)}
key={previewMode}
> >
{previewMode.replace(/-(.*)/, ' ')} {previewMode.replace(/-(.*)/, ' ')}
</div> </div>

View File

@ -11,7 +11,7 @@ export const sendNetworkRequest = async (item, collection, environment, collecti
// Note that the Buffer is encoded as a base64 string, because Buffers / TypedArrays are not allowed in the redux store // Note that the Buffer is encoded as a base64 string, because Buffers / TypedArrays are not allowed in the redux store
dataBuffer: response.dataBuffer, dataBuffer: response.dataBuffer,
headers: Object.entries(response.headers), headers: Object.entries(response.headers),
size: getResponseSize(response), size: response.size,
status: response.status, status: response.status,
statusText: response.statusText, statusText: response.statusText,
duration: response.duration duration: response.duration
@ -33,10 +33,6 @@ const sendHttpRequest = async (item, collection, environment, collectionVariable
}); });
}; };
const getResponseSize = (response) => {
return response.headers['content-length'] || Buffer.byteLength(safeStringifyJSON(response.data)) || 0;
};
export const fetchGqlSchema = async (endpoint, environment, request, collection) => { export const fetchGqlSchema = async (endpoint, environment, request, collection) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { ipcRenderer } = window; const { ipcRenderer } = window;

View File

@ -174,6 +174,20 @@ const configureRequest = async (collectionUid, request, envVars, collectionVaria
return axiosInstance; return axiosInstance;
}; };
const parseDataFromResponse = (response) => {
const dataBuffer = Buffer.from(response.data);
// Parse the charset from content type: https://stackoverflow.com/a/33192813
const charset = /charset=([^()<>@,;:\"/[\]?.=\s]*)/i.exec(response.headers['Content-Type'] || '');
// Overwrite the original data for backwards compatability
let data = dataBuffer.toString(charset || 'utf-8');
// Try to parse response to JSON, this can quitly fail
try {
data = JSON.parse(response.data);
} catch {}
return { data, dataBuffer };
};
const registerNetworkIpc = (mainWindow) => { const registerNetworkIpc = (mainWindow) => {
// handler for sending http request // handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => { ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => {
@ -307,13 +321,8 @@ const registerNetworkIpc = (mainWindow) => {
/** @type {import('axios').AxiosResponse} */ /** @type {import('axios').AxiosResponse} */
const response = await axiosInstance(request); const response = await axiosInstance(request);
const dataBuffer = Buffer.from(response.data); const { data, dataBuffer } = parseDataFromResponse(response);
// Overwrite the original data for backwards compatability response.data = data;
response.data = dataBuffer.toString('utf-8');
// Try to parse response to JSON, this can quitly fail
try {
response.data = JSON.parse(response.data);
} catch {}
// run post-response vars // run post-response vars
const postResponseVars = get(request, 'vars.res', []); const postResponseVars = get(request, 'vars.res', []);
@ -433,6 +442,7 @@ const registerNetworkIpc = (mainWindow) => {
headers: response.headers, headers: response.headers,
data: response.data, data: response.data,
dataBuffer: dataBuffer.toString('base64'), dataBuffer: dataBuffer.toString('base64'),
size: Buffer.byteLength(dataBuffer),
duration: requestDuration duration: requestDuration
}; };
} catch (error) { } catch (error) {
@ -448,6 +458,8 @@ const registerNetworkIpc = (mainWindow) => {
} }
if (error?.response) { if (error?.response) {
const { data, dataBuffer } = parseDataFromResponse(error.response);
error.response.data = data;
// run assertions // run assertions
const assertions = get(request, 'assertions'); const assertions = get(request, 'assertions');
if (assertions) { if (assertions) {
@ -513,6 +525,8 @@ const registerNetworkIpc = (mainWindow) => {
statusText: error.response.statusText, statusText: error.response.statusText,
headers: error.response.headers, headers: error.response.headers,
data: error.response.data, data: error.response.data,
dataBuffer: dataBuffer.toString('base64'),
size: Buffer.byteLength(dataBuffer),
duration: requestDuration ?? 0 duration: requestDuration ?? 0
}; };
} }
@ -729,6 +743,9 @@ const registerNetworkIpc = (mainWindow) => {
const response = await axiosInstance(request); const response = await axiosInstance(request);
timeEnd = Date.now(); timeEnd = Date.now();
const { data, dataBuffer } = parseDataFromResponse(response);
response.data = data;
// run post-response vars // run post-response vars
const postResponseVars = get(request, 'vars.res', []); const postResponseVars = get(request, 'vars.res', []);
if (postResponseVars?.length) { if (postResponseVars?.length) {
@ -839,7 +856,7 @@ const registerNetworkIpc = (mainWindow) => {
statusText: response.statusText, statusText: response.statusText,
headers: Object.entries(response.headers), headers: Object.entries(response.headers),
duration: timeEnd - timeStart, duration: timeEnd - timeStart,
size: response.headers['content-length'] || getSize(response.data), size: Buffer.byteLength(dataBuffer),
data: response.data data: response.data
} }
}); });
@ -852,12 +869,15 @@ const registerNetworkIpc = (mainWindow) => {
} }
if (error?.response) { if (error?.response) {
const { data, dataBuffer } = parseDataFromResponse(error.response);
error.response.data = data;
responseReceived = { responseReceived = {
status: error.response.status, status: error.response.status,
statusText: error.response.statusText, statusText: error.response.statusText,
headers: Object.entries(error.response.headers), headers: Object.entries(error.response.headers),
duration: duration, duration: duration,
size: error.response.headers['content-length'] || getSize(error.response.data), size: Buffer.byteLength(dataBuffer),
data: error.response.data data: error.response.data
}; };