Skip to content

Commit

Permalink
feat(ui): more accurate request timing + show head complete & body co…
Browse files Browse the repository at this point in the history
…mplete time when hovering over time taken tag in Response Panel
  • Loading branch information
flawiddsouza committed Oct 19, 2024
1 parent a66ec7a commit 2ef4a7d
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 11 deletions.
11 changes: 9 additions & 2 deletions packages/browser-extension/v2/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,32 @@ async function handleSendRequest(message, sendResponse) {
signal: abortController.get(eventId).signal
})

const endTime = new Date()
const headEndTime = new Date()

const status = response.status
const statusText = response.statusText
const responseHeaders = [...response.headers.entries()]

const responseBlob = await response.blob()

const endTime = new Date()

const mimeType = responseBlob.type
const buffer = await responseBlob.arrayBuffer()

const timeTaken = endTime - startTime
const headTimeTaken = headEndTime - startTime
const bodyTimeTaken = endTime - headEndTime

const responseToSend = {
status,
statusText,
headers: responseHeaders,
mimeType,
buffer: Array.from(new Uint8Array(buffer)),
timeTaken
timeTaken,
headTimeTaken,
bodyTimeTaken,
}

sendResponse({
Expand Down
11 changes: 9 additions & 2 deletions packages/browser-extension/v3/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,32 @@ async function handleSendRequest(message, sendResponse) {
signal: abortController.get(eventId).signal
})

const endTime = new Date()
const headEndTime = new Date()

const status = response.status
const statusText = response.statusText
const responseHeaders = [...response.headers.entries()]

const responseBlob = await response.blob()

const endTime = new Date()

const mimeType = responseBlob.type
const buffer = await responseBlob.arrayBuffer()

const timeTaken = endTime - startTime
const headTimeTaken = headEndTime - startTime
const bodyTimeTaken = endTime - headEndTime

const responseToSend = {
status,
statusText,
headers: responseHeaders,
mimeType,
buffer: Array.from(new Uint8Array(buffer)),
timeTaken
timeTaken,
headTimeTaken,
bodyTimeTaken,
}

sendResponse({
Expand Down
11 changes: 9 additions & 2 deletions packages/electron/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,32 @@ async function handleSendRequest(data) {
dispatcher: getAgentForRequest(urlParsed, disableSSLVerification),
})

const endTime = new Date()
const headEndTime = new Date()

const status = response.status
const statusText = response.statusText
const responseHeaders = [...response.headers.entries()]

const responseBlob = await response.blob()

const endTime = new Date()

const mimeType = responseBlob.type
const buffer = await responseBlob.arrayBuffer()

const timeTaken = endTime - startTime
const headTimeTaken = headEndTime - startTime
const bodyTimeTaken = endTime - headEndTime

const responseToSend = {
status,
statusText,
headers: responseHeaders,
mimeType,
buffer: Array.from(new Uint8Array(buffer)),
timeTaken
timeTaken,
headTimeTaken,
bodyTimeTaken,
}
return {
event: 'response',
Expand Down
1 change: 1 addition & 0 deletions packages/test-api-streaming-response/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
5 changes: 5 additions & 0 deletions packages/test-api-streaming-response/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Run

```sh
uv run app.py
```
19 changes: 19 additions & 0 deletions packages/test-api-streaming-response/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask import Flask, Response
import time
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route('/')
def stream():
def generate():
for i in range(1, 6):
yield f"data: Chunk {i}\n"
time.sleep(1)
yield 'data: End of streaming\n'

return Response(generate(), mimetype='text/event-stream')

if __name__ == '__main__':
app.run(debug=True)
10 changes: 10 additions & 0 deletions packages/test-api-streaming-response/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[project]
name = "test-api-streaming-response"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"flask-cors>=5.0.0",
"flask>=3.0.3",
]
166 changes: 166 additions & 0 deletions packages/test-api-streaming-response/uv.lock

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion packages/ui/src/components/ResponsePanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<span class="bold">{{ response.status }}</span>
{{ response.statusText === '' ? getStatusText(response.status) : response.statusText }}
</div>
<div class="tag ml-0_6rem" v-if="response.timeTaken">{{ humanFriendlyTime(response.timeTaken) }}</div>
<div class="tag ml-0_6rem" v-if="response.timeTaken" v-tooltip="getFullTimeTaken(response)" :key="response._id">{{ humanFriendlyTime(response.timeTaken) }}</div>
<div class="tag ml-0_6rem" v-if="responseSize">{{ humanFriendlySize(responseSize) }}</div>
</div>
<div class="response-panel-address-bar-select-container">
Expand Down Expand Up @@ -189,6 +189,7 @@ import {
import { emitter } from '@/event-bus'
import ResponseFilteringHelpModal from '@/components/modals/ResponseFilteringHelpModal.vue'
import constants from '@/constants'
import { vTooltip } from '@/directives/vTooltip'
export default {
components: {
Expand All @@ -199,6 +200,9 @@ export default {
IframeFromBuffer,
ResponsePanelTimeline,
},
directives: {
tooltip: vTooltip
},
props: {
activeTab: Object
},
Expand Down Expand Up @@ -601,6 +605,13 @@ export default {
}
})
},
getFullTimeTaken(response) {
if (!response.headTimeTaken && !response.bodyTimeTaken) {
return humanFriendlyTime(response.timeTaken)
}
return `Head: ${humanFriendlyTime(response.headTimeTaken)} | Body: ${humanFriendlyTime(response.bodyTimeTaken)}`
},
bufferToJSONString,
filterJSONResponse,
filterXmlResponse,
Expand Down
24 changes: 24 additions & 0 deletions packages/ui/src/directives/vTooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const vTooltip = {
mounted(el, binding) {
el.addEventListener('mouseenter', () => {
var tooltip = document.querySelector('.tooltip')
if(tooltip) {
tooltip.remove()
}
var div = document.createElement('div')
div.classList.add('tooltip')
div.innerText = binding.value
var dimens = el.getBoundingClientRect()
div.style.left = dimens.left + 'px'
div.style.top = (dimens.bottom + 5) + 'px'
document.body.appendChild(div)
})

el.addEventListener('mouseleave', () => {
var tooltip = document.querySelector('.tooltip')
if(tooltip) {
tooltip.remove()
}
})
}
}
4 changes: 4 additions & 0 deletions packages/ui/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export interface RequestInitialResponse {
mimeType: string
buffer: ArrayBuffer
timeTaken: number
headTimeTaken?: number
bodyTimeTaken?: number
}

export interface RequestFinalResponse {
Expand All @@ -98,6 +100,8 @@ export interface RequestFinalResponse {
mimeType: string
buffer: ArrayBuffer
timeTaken: number
headTimeTaken?: number
bodyTimeTaken?: number
request: {
method: string
query: string
Expand Down
13 changes: 11 additions & 2 deletions packages/ui/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,25 +287,32 @@ export async function fetchWrapper(url: URL, method: string, headers: Record<str
signal: abortControllerSignal
})

const endTime = new Date()
const headEndTime = new Date()

const status = response.status
const statusText = response.statusText
const responseHeaders = [...response.headers.entries()]

const responseBlob = await response.blob()

const endTime = new Date()

const mimeType = responseBlob.type
const buffer = await responseBlob.arrayBuffer()

const timeTaken = Number(endTime) - Number(startTime)
const headTimeTaken = Number(headEndTime) - Number(startTime)
const bodyTimeTaken = Number(endTime) - Number(headEndTime)

return {
status,
statusText,
headers: responseHeaders,
mimeType,
buffer,
timeTaken
timeTaken,
headTimeTaken,
bodyTimeTaken,
}
}

Expand Down Expand Up @@ -539,6 +546,8 @@ export async function handleRequest(
mimeType: response.mimeType,
buffer: response.buffer,
timeTaken: response.timeTaken,
headTimeTaken: response.headTimeTaken,
bodyTimeTaken: response.bodyTimeTaken,
request: {
method: request.method!,
query: url.search,
Expand Down
11 changes: 11 additions & 0 deletions packages/ui/src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,14 @@ select {
color: rgb(237, 39, 39);
font-size: 0.6rem !important;
}

.tooltip {
position: fixed;
z-index: 3;
background-color: var(--sidebar-item-active-color);
top: 0;
left: 0;
padding: 0.5em;
border-radius: var(--default-border-radius);
border: 3px solid var(--default-border-color);
}
11 changes: 9 additions & 2 deletions packages/web-standalone/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,32 @@ app.post('/proxy', async(req, res) => {
body: method !== 'GET' ? body : undefined
})

const endTime = new Date()
const headEndTime = new Date()

const status = response.status
const statusText = response.statusText
const responseHeaders = [...response.headers.entries()]

const responseBlob = await response.blob()

const endTime = new Date()

const mimeType = responseBlob.type
const buffer = await responseBlob.arrayBuffer()

const timeTaken = endTime - startTime
const headTimeTaken = headEndTime - startTime
const bodyTimeTaken = endTime - headEndTime

const responseToSend = {
status,
statusText,
headers: responseHeaders,
mimeType,
buffer: Array.from(new Uint8Array(buffer)),
timeTaken
timeTaken,
headTimeTaken,
bodyTimeTaken,
}

res.send({
Expand Down

0 comments on commit 2ef4a7d

Please sign in to comment.