Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 92
perf(script): harden runtime lifecycle cleanup#829
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f1dd75d
perf: memory leak hardening
harlan-zw 0c4a5ac
fix: harden lifecycle cleanup
harlan-zw 4c4549c
test: avoid generated devtools tsconfig dependency
harlan-zw 2ed0a27
refactor: centralize abortable readiness
harlan-zw f39b7b4
fix: isolate readiness callback failures
harlan-zw 9e3eca0
fix: keep adopted readiness promises abortable
harlan-zw 1b19b51
fix(script): separate app and consumer lifecycles
harlan-zw 8cb590d
fix: preserve shared useScript behavior
harlan-zw b60ccde
fix(script): keep reload lifecycle live
harlan-zw 6dfd80c
fix(script): retain shared cleanup marker
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| export async function fetchScript(url: string) { | ||
| const compressedResponse = await fetch(url, { headers: { 'Accept-Encoding': 'gzip' } }).catch((err) => { | ||
| export async function fetchScript(url: string, signal?: AbortSignal) { | ||
| const compressedResponse = await fetch(url, { | ||
| headers: { 'Accept-Encoding': 'gzip' }, | ||
| signal, | ||
| }).catch((err) => { | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return { | ||
| size: null, | ||
| error: err, | ||
| @@ -9,6 +12,7 @@ export async function fetchScript(url: string) { | ||
| return compressedResponse as { size: null, error: Error } | ||
| } | ||
| if (!compressedResponse.ok) { | ||
| await cancelResponseBody(compressedResponse) | ||
| return { | ||
| size: null, | ||
| error: new Error(`Failed to fetch ${compressedResponse.status} ${compressedResponse.statusText}`), | ||
| @@ -17,9 +21,19 @@ export async function fetchScript(url: string) { | ||
| // Guard against measuring HTML error pages as script sizes | ||
| const contentType = compressedResponse.headers.get('Content-Type') || '' | ||
| if (contentType.includes('text/html')) { | ||
| await cancelResponseBody(compressedResponse) | ||
| return { size: null } | ||
| } | ||
| const size = await getResponseSize(compressedResponse) | ||
| let size: number | null | ||
| try { | ||
| size = await getResponseSize(compressedResponse) | ||
| } | ||
| catch (error) { | ||
| return { | ||
| size: null, | ||
| error: error instanceof Error ? error : new Error(String(error)), | ||
| } | ||
| } | ||
| if (!size) { | ||
| return { | ||
| size: null, | ||
| @@ -31,23 +45,38 @@ export async function fetchScript(url: string) { | ||
| } | ||
| async function getResponseSize(response: Response) { | ||
| const reader = response.body?.getReader() | ||
| const contentLength = response.headers.get('Content-Length') | ||
| if (contentLength) { | ||
| await cancelResponseBody(response) | ||
| return Number(contentLength) | ||
| } | ||
| const reader = response.body?.getReader() | ||
| if (!reader) { | ||
| return null | ||
| } | ||
| let total = 0 | ||
| let done = false | ||
| while (!done) { | ||
| const data = await reader.read() | ||
| done = data.done | ||
| total += data.value?.length || 0 | ||
| try { | ||
| let total = 0 | ||
| let done = false | ||
| while (!done) { | ||
| const data = await reader.read() | ||
| done = data.done | ||
| total += data.value?.length || 0 | ||
| } | ||
| return total > 0 ? total : null | ||
| } | ||
| finally { | ||
| reader.releaseLock() | ||
| } | ||
| } | ||
| async function cancelResponseBody(response: Response) { | ||
| try { | ||
| await response.body?.cancel() | ||
| } | ||
| catch { | ||
| // The response is being discarded, so cancellation failure is non-fatal. | ||
| } | ||
| return total > 0 ? total : null | ||
| } | ||
| function bytesToSize(bytes: number) { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.