guard against null requests in the history dropdown (#5890)

This commit is contained in:
James Gatz 2023-04-13 11:54:10 +02:00 committed by GitHub
parent 76420c6748
commit 650897ac5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 10 deletions

View File

@ -216,13 +216,13 @@ export function compressObject(obj: any) {
return compressed.toString('base64');
}
export function decompressObject(input: string | null): any {
export function decompressObject<ObjectType>(input: string | null): ObjectType | null {
if (typeof input !== 'string') {
return null;
}
const jsonBuffer = zlib.gunzipSync(Buffer.from(input, 'base64'));
return JSON.parse(jsonBuffer.toString('utf8'));
return JSON.parse(jsonBuffer.toString('utf8')) as ObjectType;
}
export function resolveHomePath(p: string) {

View File

@ -33,7 +33,7 @@ const FIELDS_TO_IGNORE = [
'description',
'parentId',
'name',
];
] as const;
export const isRequestVersion = (model: Pick<BaseModel, 'type'>): model is RequestVersion => (
model.type === type
@ -61,7 +61,7 @@ export async function create(request: Request | WebSocketRequest | GrpcRequest)
const parentId = request._id;
const latestRequestVersion: RequestVersion | null = await getLatestByParentId(parentId);
const latestRequest = latestRequestVersion
? decompressObject(latestRequestVersion.compressedRequest)
? decompressObject<Request | WebSocketRequest>(latestRequestVersion.compressedRequest)
: null;
const hasChanged = _diffRequests(latestRequest, request);
@ -91,7 +91,12 @@ export async function restore(requestVersionId: string) {
return null;
}
const requestPatch = decompressObject(requestVersion.compressedRequest);
const requestPatch = decompressObject<Request | WebSocketRequest | GrpcRequest>(requestVersion.compressedRequest);
if (!requestPatch) {
return null;
}
const originalRequest = await requestOperations.getById(requestPatch._id);
if (!originalRequest) {
@ -100,8 +105,10 @@ export async function restore(requestVersionId: string) {
// Only restore fields that aren't blacklisted
for (const field of FIELDS_TO_IGNORE) {
if (field in requestPatch) {
delete requestPatch[field];
}
}
return requestOperations.update(originalRequest, requestPatch);
}
@ -112,7 +119,7 @@ function _diffRequests(rOld: Request | WebSocketRequest | null, rNew: Request |
for (const key of Object.keys(rOld) as (keyof typeof rOld)[]) {
// Skip fields that aren't useful
if (FIELDS_TO_IGNORE.includes(key)) {
if (FIELDS_TO_IGNORE.find(field => field === key)) {
continue;
}
if (!deepEqual(rOld[key], rNew[key])) {

View File

@ -4,7 +4,9 @@ import { useSelector } from 'react-redux';
import { decompressObject } from '../../../common/misc';
import * as models from '../../../models/index';
import { isRequest, Request } from '../../../models/request';
import { Response } from '../../../models/response';
import { WebSocketRequest } from '../../../models/websocket-request';
import { isWebSocketResponse, WebSocketResponse } from '../../../models/websocket-response';
import { updateRequestMetaByParentId } from '../../hooks/create-request';
import { selectActiveEnvironment, selectActiveRequest, selectActiveRequestResponses, selectRequestVersions } from '../../redux/selectors';
@ -119,7 +121,7 @@ export const ResponseHistoryDropdown = <GenericResponse extends Response | WebSo
const activeResponseId = activeResponse ? activeResponse._id : 'n/a';
const active = response._id === activeResponseId;
const requestVersion = requestVersions.find(({ _id }) => _id === response.requestVersionId);
const request = requestVersion ? decompressObject(requestVersion.compressedRequest) : null;
const request = requestVersion ? decompressObject<Request | WebSocketRequest>(requestVersion.compressedRequest) : null;
return (
<DropdownItem
@ -140,8 +142,8 @@ export const ResponseHistoryDropdown = <GenericResponse extends Response | WebSo
/>
<URLTag
small
url={request.url}
method={request ? request.method : ''}
url={request?.url || ''}
method={request && isRequest(request) ? request.method : ''}
tooltipDelay={1000}
/>
<TimeTag milliseconds={response.elapsedTime} small tooltipDelay={1000} />