-
-
Notifications
You must be signed in to change notification settings - Fork 201
feat: support performance tracks for server components #1984
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7f3c81d
define debugChannel option types
dai-shi d74e26d
add type
dai-shi 0d3ab32
add debug in examples
dai-shi de3059d
Merge branch 'main' into support-debug-channel
dai-shi 753c197
reduce delay
dai-shi a1fa25f
patch rsdw
dai-shi 51d7c8d
implement debug channel
dai-shi 9561e17
fix format
dai-shi cdda711
Merge branch 'main' into support-debug-channel
dai-shi 4adbeba
fix type
dai-shi 7a7221e
Apply suggestion from @dai-shi
dai-shi 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,149 @@ | ||
| // This file should not include Node specific code. | ||
|
|
||
| export const DEBUG_ID_HEADER = 'X-Waku-Debug-Id'; | ||
| export const DEBUG_CMD_EVENT = 'waku:debug-cmd'; | ||
| export const DEBUG_DATA_EVENT = 'waku:debug-data'; | ||
|
|
||
| const bytesToBase64 = (bytes: Uint8Array) => { | ||
| let binary = ''; | ||
| for (let i = 0; i < bytes.length; i++) { | ||
| binary += String.fromCharCode(bytes[i]!); | ||
| } | ||
| return btoa(binary); | ||
| }; | ||
|
|
||
| const base64ToBytes = (base64: string) => | ||
| Uint8Array.from(atob(base64), (char) => char.charCodeAt(0)); | ||
|
|
||
| type DebugCmdEventReadyPayload = { | ||
| i: string; // debugId | ||
| }; | ||
| type DebugCmdEventChunkPayload = { | ||
| i: string; // debugId | ||
| b: string; // base64 encoded chunk | ||
| }; | ||
| type DebugCmdEventDonePayload = { | ||
| i: string; // debugId | ||
| d: true; // done flag | ||
| }; | ||
| type DebugDataEventChunkPayload = { | ||
| i: string; // debugId | ||
| b: string; // base64 encoded chunk | ||
| }; | ||
| type DebugDataEventDonePayload = { | ||
| i: string; // debugId | ||
| d: true; // done flag | ||
| }; | ||
| export type DebugEventPayload = | ||
| | DebugCmdEventReadyPayload | ||
| | DebugCmdEventChunkPayload | ||
| | DebugCmdEventDonePayload | ||
| | DebugDataEventChunkPayload | ||
| | DebugDataEventDonePayload; | ||
|
|
||
| export function assertIsDebugEventPayload( | ||
| payload: unknown, | ||
| ): asserts payload is DebugEventPayload { | ||
| if ( | ||
| !payload || | ||
| typeof payload !== 'object' || | ||
| typeof (payload as { i?: unknown }).i !== 'string' || | ||
| ('b' in payload && typeof (payload as { b?: unknown }).b !== 'string') || | ||
| ('d' in payload && (payload as { d?: unknown }).d !== true) | ||
| ) { | ||
| throw new Error('Invalid debug event payload'); | ||
| } | ||
| } | ||
|
|
||
| const createWsDebugChannel = (debugId: string) => { | ||
| const hot = import.meta.hot!; | ||
| let closed = false; | ||
| let onDataEvent: ((payload: unknown) => void) | undefined; | ||
|
|
||
| const cleanup = (notify?: boolean) => { | ||
| if (closed) { | ||
| return; | ||
| } | ||
| closed = true; | ||
| if (onDataEvent) { | ||
| hot.off(DEBUG_DATA_EVENT, onDataEvent); | ||
| } | ||
| if (notify) { | ||
| hot.send(DEBUG_CMD_EVENT, { | ||
| i: debugId, | ||
| d: true, | ||
| } satisfies DebugEventPayload); | ||
| } | ||
| }; | ||
|
|
||
| const readable = new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| onDataEvent = (payload: unknown) => { | ||
| assertIsDebugEventPayload(payload); | ||
| if (closed || payload.i !== debugId) { | ||
| return; | ||
| } | ||
| if ('b' in payload) { | ||
| // chunk | ||
| controller.enqueue(base64ToBytes(payload.b)); | ||
| } | ||
| if ('d' in payload) { | ||
| // done | ||
| cleanup(); | ||
| controller.close(); | ||
| } | ||
| }; | ||
| hot.on(DEBUG_DATA_EVENT, onDataEvent); | ||
| hot.send(DEBUG_CMD_EVENT, { i: debugId } satisfies DebugEventPayload); | ||
| }, | ||
| cancel() { | ||
| cleanup(true); | ||
| }, | ||
| }); | ||
|
|
||
| const writable = new WritableStream<Uint8Array>({ | ||
| write(chunk) { | ||
| if (closed) { | ||
| throw new TypeError('Channel is closed'); | ||
| } | ||
| hot.send(DEBUG_CMD_EVENT, { | ||
| i: debugId, | ||
| b: bytesToBase64(chunk), | ||
| } satisfies DebugEventPayload); | ||
| }, | ||
| close() { | ||
| cleanup(true); | ||
| }, | ||
| abort() { | ||
| cleanup(true); | ||
| }, | ||
| }); | ||
|
|
||
| return { readable, writable }; | ||
| }; | ||
|
|
||
| export const setupDebugChannel = ( | ||
| baseFetchFn: typeof fetch, | ||
| prefetchedEntry: { d?: string } | undefined, | ||
| ) => { | ||
| if (prefetchedEntry) { | ||
| const debugId = prefetchedEntry.d; | ||
| if (debugId) { | ||
| const debugChannel = createWsDebugChannel(debugId); | ||
| return { debugChannel }; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| const debugId = crypto.randomUUID(); | ||
| const debugChannel = createWsDebugChannel(debugId); | ||
| const fetchFn = ((input: RequestInfo | URL, init?: RequestInit) => { | ||
| const headers = new Headers(init?.headers); | ||
| headers.set(DEBUG_ID_HEADER, debugId); | ||
| return baseFetchFn(input, { | ||
| ...init, | ||
| headers, | ||
| }); | ||
| }) as typeof fetch; | ||
| return { fetchFn, debugChannel }; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.