Skip to content

Commit

Permalink
refactor: check content encoding instead of forcing atob
Browse files Browse the repository at this point in the history
  • Loading branch information
e-fisher committed Oct 31, 2024
1 parent f97b179 commit 7adcc3b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/utils/harToProxyData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DEFAULT_GROUP_NAME } from '@/constants'
import { Method, ProxyData, Request, Response } from '@/types'
import { HarWithOptionalResponse } from '@/types/har'
import type { Entry } from 'har-format'
import { safeAtob } from './format'
import type { Content, Entry } from 'har-format'

export function harToProxyData(har: HarWithOptionalResponse): ProxyData[] {
return har.log.entries.map((entry) => ({
Expand Down Expand Up @@ -54,14 +53,13 @@ function parseRequest(request: Entry['request']): Request {
}

function parseResponse(response: Entry['response']): Response {
const content = response.content?.text ? safeAtob(response.content.text) : ''
return {
statusCode: response.status,
reason: response.statusText,
httpVersion: response.httpVersion,
headers: response.headers.map((h) => [h.name, h.value]),
cookies: response.cookies.map((c) => [c.name, c.value]),
content,
content: parseContent(response.content),
contentLength: response.content?.size ?? 0,
timestampStart: 0,
path: '',
Expand All @@ -71,3 +69,12 @@ function parseResponse(response: Entry['response']): Response {
function isoToUnixTimestamp(isoString: string): number {
return new Date(isoString).getTime() / 1000
}

function parseContent(content: Content): string {
if (!content.text) return ''

if (content.encoding === 'base64') {
return atob(content.text)
}
return content.text
}

0 comments on commit 7adcc3b

Please sign in to comment.