Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hostname as prefix of har filename #362

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
)

// 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 All @@ -415,9 +415,9 @@
try {
fileHandle = await open(path.join(RECORDINGS_PATH, fileName), 'r')
const data = await fileHandle?.readFile({ encoding: 'utf-8' })
const har = await JSON.parse(data)

Check warning on line 418 in src/main.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe assignment of an `any` value

return { name: fileName, content: har }

Check warning on line 420 in src/main.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe assignment of an `any` value
} finally {
await fileHandle?.close()
}
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
Loading