-
Notifications
You must be signed in to change notification settings - Fork 2
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: Websocket communication between k6 Studio and browser #382
Merged
allansson
merged 5 commits into
wip/browser-recording
from
feat/websocket-communication-between-studio-and-extension
Dec 17, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a71211c
add tsconfigPaths to main and preload configs
allansson c056924
e2e communication between browser and renderer
allansson bd55047
remove eslint warning fix
allansson 308b9e1
move browser schemas into versioned folder
allansson 73f02c9
Merge branch 'wip/browser-recording' into feat/websocket-communicatio…
allansson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,80 @@ | ||
import { MessageEnvelope, MessagePayload } from '@/services/browser/schemas' | ||
import { browser } from 'webextension-polyfill-ts' | ||
|
||
let socket: WebSocket | null = null | ||
let buffer: MessageEnvelope[] = [] | ||
|
||
function send(message: MessagePayload) { | ||
const envelope: MessageEnvelope = { | ||
messageId: crypto.randomUUID(), | ||
payload: message, | ||
} | ||
|
||
if (socket) { | ||
socket.send(JSON.stringify(envelope)) | ||
} else { | ||
buffer.push(envelope) | ||
} | ||
} | ||
|
||
function flush(socket: WebSocket) { | ||
for (const message of buffer) { | ||
socket.send(JSON.stringify(message)) | ||
} | ||
|
||
buffer = [] | ||
} | ||
|
||
function reconnect() { | ||
socket?.close() | ||
socket = null | ||
|
||
setTimeout(() => { | ||
connect() | ||
}, 1000) | ||
} | ||
|
||
function connect() { | ||
const ws = new WebSocket('ws://localhost:7554') | ||
|
||
ws.onopen = () => { | ||
console.log('Connected to server...') | ||
|
||
socket = ws | ||
|
||
flush(ws) | ||
} | ||
|
||
ws.onerror = (err) => { | ||
console.log('Connection error...', err) | ||
|
||
reconnect() | ||
} | ||
|
||
ws.onclose = () => { | ||
console.log('Connection closed...') | ||
|
||
reconnect() | ||
} | ||
} | ||
|
||
setInterval(() => { | ||
send({ | ||
type: 'events-captured', | ||
events: [ | ||
{ | ||
eventId: crypto.randomUUID(), | ||
timestamp: Date.now(), | ||
type: 'dummy', | ||
selector: 'button', | ||
message: 'Clicked button', | ||
}, | ||
], | ||
}) | ||
}, 5000) | ||
|
||
connect() | ||
|
||
browser.runtime.onInstalled.addListener(() => { | ||
console.log('Extension installed...') | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BrowserEvent } from '@/schemas/recording' | ||
import { useEffect, useState } from 'react' | ||
|
||
export function useListenBrowserEvent() { | ||
const [events, setEvents] = useState<BrowserEvent[]>([]) | ||
|
||
useEffect(() => { | ||
return window.studio.browser.onBrowserEvent((events: BrowserEvent[]) => { | ||
console.log('Received browser events', events) | ||
|
||
setEvents((prevEvents) => [...prevEvents, ...events]) | ||
}) | ||
}, []) | ||
|
||
return events | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './v1/browser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod' | ||
|
||
const DummyEvent = z.object({ | ||
eventId: z.string(), | ||
timestamp: z.number(), | ||
type: z.literal('dummy'), | ||
selector: z.string(), | ||
message: z.string(), | ||
}) | ||
|
||
export const BrowserEventSchema = DummyEvent | ||
|
||
export type DummyEvent = z.infer<typeof DummyEvent> | ||
export type BrowserEvent = z.infer<typeof BrowserEventSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { BrowserEventSchema } from '@/schemas/recording' | ||
import { z } from 'zod' | ||
|
||
export const EventsCapturedMessage = z.object({ | ||
type: z.literal('events-captured'), | ||
events: z.array(BrowserEventSchema), | ||
}) | ||
|
||
export const MessagePayload = EventsCapturedMessage | ||
|
||
export const MessageEnvelope = z.object({ | ||
messageId: z.string(), | ||
payload: MessagePayload, | ||
}) | ||
|
||
export type EventsCapturedMessage = z.infer<typeof EventsCapturedMessage> | ||
export type MessagePayload = z.infer<typeof MessagePayload> | ||
export type MessageEnvelope = z.infer<typeof MessageEnvelope> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed we're going to want to write zod schemas for recordings, i.e. HAR files, at some point so I structured this like we've done with generator/settings schemas.