Skip to content

Commit

Permalink
Add some last small fixes/tweaks for gRPC compression change
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Oct 2, 2024
1 parent 3d713b3 commit 2fdcab7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/components/editor/content-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { styled } from '../../styles';
import { ObservablePromise, isObservablePromise } from '../../util/observable';
import { asError, unreachableCheck } from '../../util/error';
import { stringToBuffer } from '../../util/buffer';
import { lastHeader } from '../../util/headers';

import { ViewableContentType } from '../../model/events/content-types';
import { Formatters, isEditorFormatter } from '../../model/events/body-formatting';
Expand Down Expand Up @@ -200,7 +201,7 @@ export class ContentViewer extends React.Component<ContentViewerProps> {
return <FormatterContainer expanded={this.props.expanded}>
<formatterConfig.Component
content={this.contentBuffer}
headers={this.props.headers}
rawContentType={lastHeader(this.props.headers?.['content-type'])}
/>
</FormatterContainer>;
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/intercept/config/android-device-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ const Spacer = styled.div`
`;

function urlSafeBase64(content: string) {
return stringToBuffer(content).toString('base64url');
return stringToBuffer(content)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_');
}

function getConfigRequestIds(eventsStore: EventsStore) {
Expand Down
8 changes: 5 additions & 3 deletions src/model/events/body-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface EditorFormatter {

type FormatComponentProps = {
content: Buffer;
headers?: Headers;
rawContentType: string | undefined;
};

type FormatComponent = React.ComponentType<FormatComponentProps>;
Expand Down Expand Up @@ -65,7 +65,7 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
language: 'text',
cacheKey: Symbol('text'),
isEditApplicable: false,
render: (input: Buffer, headers?: Headers) => {
render: (input: Buffer) => {
return bufferToString(input);
}
},
Expand Down Expand Up @@ -112,7 +112,9 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
// showing the loading spinner that churns the layout in short content cases.
return JSON.stringify(
JSON.parse(inputAsString),
null, 2);
null,
2
);
// ^ Same logic as in UI-worker-formatter
} catch (e) {
// Fallback to showing the raw un-formatted JSON:
Expand Down
12 changes: 7 additions & 5 deletions src/model/events/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function getDefaultMimeType(contentType: ViewableContentType): string {
return _.findKey(mimeTypeToContentTypeMap, (c) => c === contentType)!;
}

function isValidAlphaNumOrSpace(byte: number) {
function isAlphaNumOrEquals(byte: number) {
return (byte >= 65 && byte <= 90) || // A-Z
(byte >= 97 && byte <= 122) || // a-z
(byte >= 48 && byte <= 57) || // 0-9
Expand All @@ -162,14 +162,16 @@ function isValidAlphaNumOrSpace(byte: number) {

function isValidStandardBase64Byte(byte: number) {
// + / (standard)
return byte === 43 || byte === 47
|| isValidAlphaNumOrSpace(byte);
return byte === 43 ||
byte === 47 ||
isAlphaNumOrEquals(byte);
}

function isValidURLSafeBase64Byte(byte: number) {
// - _ (URL-safe version)
return byte === 45 || byte === 95
|| isValidAlphaNumOrSpace(byte);
return byte === 45 ||
byte === 95 ||
isAlphaNumOrEquals(byte);
}

export function getCompatibleTypes(
Expand Down
10 changes: 5 additions & 5 deletions src/model/http/har.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export type RequestContentData = {

export interface ExtendedHarRequest extends HarFormat.Request {
_requestBodyStatus?:
| 'discarded:too-large'
| 'discarded:not-representable' // to indicate that extended field `_content` is populated with base64 `postData`
| 'discarded:not-decodable';
| 'discarded:too-large'
| 'discarded:not-representable' // to indicate that extended field `_content` is populated with base64 `postData`
| 'discarded:not-decodable';
_content?: RequestContentData;
_trailers?: HarFormat.Header[];
}
Expand Down Expand Up @@ -435,10 +435,10 @@ function generateHarWebSocketMessage(
return {
// Note that msg.direction is from the perspective of Mockttp, not the client.
type: message.direction === 'sent'
? 'receive'
? 'receive'
: message.direction === 'received'
? 'send'
: unreachableCheck(message.direction),
: unreachableCheck(message.direction),

opcode: message.isBinary ? 2 : 1,
data: message.isBinary
Expand Down
3 changes: 1 addition & 2 deletions src/services/ui-worker-formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ const WorkerFormatters = {
},
base64: (content: Buffer) => {
const b64 = content.toString('ascii');
const encoding = b64.match(/[-_]/) ? 'base64url' : 'base64';
return Buffer.from(b64, encoding).toString('utf8');
return Buffer.from(b64, 'base64').toString('utf8');
},
markdown: (content: Buffer) => {
return content.toString('utf8');
Expand Down

0 comments on commit 2fdcab7

Please sign in to comment.