Skip to content

Commit

Permalink
Merge main into feat/target-headers-by-name
Browse files Browse the repository at this point in the history
  • Loading branch information
e-fisher committed Nov 25, 2024
2 parents 2083453 + 3c5bec3 commit ed84be5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
38 changes: 19 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ ipcMain.handle(
)

// HAR
ipcMain.handle('har:save', async (_, data) => {
const fileName = generateFileNameWithTimestamp('har')
ipcMain.handle('har:save', async (_, data: string, prefix?: string) => {
const fileName = generateFileNameWithTimestamp('har', prefix)
await writeFile(path.join(RECORDINGS_PATH, fileName), data)
return fileName
})
Expand Down
4 changes: 2 additions & 2 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ const script = {
} as const

const har = {
saveFile: (data: string): Promise<string> => {
return ipcRenderer.invoke('har:save', data)
saveFile: (data: string, prefix?: string): Promise<string> => {
return ipcRenderer.invoke('har:save', data, prefix)
},
openFile: (filePath: string): Promise<HarFile> => {
return ipcRenderer.invoke('har:open', filePath)
Expand Down
10 changes: 7 additions & 3 deletions src/views/Recorder/Recorder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { View } from '@/components/Layout/View'
import { RequestsSection } from './RequestsSection'
import { useListenProxyData } from '@/hooks/useListenProxyData'
import {
getHostNameFromURL,
startRecording,
stopRecording,
useDebouncedProxyData,
Expand All @@ -35,7 +36,7 @@ const INITIAL_GROUPS: Group[] = [

export function Recorder() {
const [selectedRequest, setSelectedRequest] = useState<ProxyData | null>(null)

const [startUrl, setStartUrl] = useState<string>()
const [groups, setGroups] = useState<Group[]>(() => INITIAL_GROUPS)

const group = useMemo(() => groups[groups.length - 1], [groups])
Expand All @@ -57,6 +58,7 @@ export function Recorder() {

const handleStartRecording = useCallback(
async (url?: string) => {
setStartUrl(url)
try {
resetProxyData()
setRecorderState('starting')
Expand Down Expand Up @@ -103,15 +105,17 @@ export function Recorder() {
})

const har = proxyDataToHar(grouped)
const prefix = startUrl && getHostNameFromURL(startUrl)
const fileName = await window.studio.har.saveFile(
JSON.stringify(har, null, 4)
JSON.stringify(har, null, 4),
prefix
)

return fileName
} finally {
setRecorderState('idle')
}
}, [groups, proxyData])
}, [groups, proxyData, startUrl])

async function handleStopRecording() {
stopRecording()
Expand Down
10 changes: 10 additions & 0 deletions src/views/Recorder/Recorder.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ function getRequestSignature(request: Request) {
return `${request.method} ${request.url}`
}

export function getHostNameFromURL(url: string) {
// ensure that a URL without protocol is parsed correctly
const urlWithProtocol = url?.startsWith('http') ? url : `http://${url}`
try {
return new URL(urlWithProtocol).hostname
} catch {
return undefined
}
}

// TODO: add error and timeout handling
export async function startRecording(url?: string) {
// Kill previous browser window
Expand Down

0 comments on commit ed84be5

Please sign in to comment.