-
Notifications
You must be signed in to change notification settings - Fork 448
Fix range-filtered remote ZIP decoding #4110
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
Open
chubes4
wants to merge
8
commits into
WordPress:trunk
Choose a base branch
from
chubes4:fix/4109-decode-remote-zip-paths
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e1e88e3
Fix range-filtered remote ZIP decoding
chubes4 eda01ca
Bound remote ZIP range decoding
chubes4 4ea1b5c
Initialize remote ZIP streams in pull
chubes4 ab39f2b
Allow large remote ZIP regression time
chubes4 afea9c0
Bound multi-file remote ZIP decoding
chubes4 3f1d39c
Address remote ZIP review feedback
chubes4 1035d28
Bound remote ZIP response concurrency
chubes4 ad6a8a1
Merge branch 'trunk' into fix/4109-decode-remote-zip-paths
brandonpayton 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
71 changes: 71 additions & 0 deletions
71
packages/php-wasm/stream-compression/src/test/decode-remote-zip.spec.ts
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,71 @@ | ||
| import { collectBytes } from '../utils/collect-bytes'; | ||
| import { decodeRemoteZip } from '../zip/decode-remote-zip'; | ||
| import { encodeZip } from '../zip/encode-zip'; | ||
|
|
||
| describe('decodeRemoteZip', () => { | ||
| it('emits selected files from range responses', async () => { | ||
| const targetPath = 'selected.txt'; | ||
| const zipBytes = await createLargeZip(targetPath); | ||
| const originalFetch = globalThis.fetch; | ||
| globalThis.fetch = createRangeFetch(zipBytes); | ||
|
|
||
| try { | ||
| const decoder = new TextDecoder(); | ||
| const stream = await decodeRemoteZip( | ||
| 'https://example.com/archive.zip', | ||
| (entry) => decoder.decode(entry.path) === targetPath | ||
| ); | ||
| const files = []; | ||
| for await (const file of stream) { | ||
| files.push(file); | ||
| } | ||
|
|
||
| expect(files).toHaveLength(1); | ||
| expect(decoder.decode(files[0].path)).toBe(targetPath); | ||
| expect(decoder.decode(files[0].bytes)).toBe('selected contents'); | ||
| } finally { | ||
| globalThis.fetch = originalFetch; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| async function createLargeZip(targetPath: string): Promise<Uint8Array> { | ||
| let state = 0x12345678; | ||
| const noise = new Uint8Array(1_200_000); | ||
| for (let i = 0; i < noise.length; i++) { | ||
| state ^= state << 13; | ||
| state ^= state >>> 17; | ||
| state ^= state << 5; | ||
| noise[i] = state; | ||
| } | ||
|
|
||
| return await collectBytes( | ||
| encodeZip([ | ||
| new File(['selected contents'], targetPath), | ||
| new File([noise], 'noise.bin'), | ||
| ]) | ||
| ); | ||
| } | ||
|
|
||
| function createRangeFetch(zipBytes: Uint8Array): typeof fetch { | ||
| return async (_input, init) => { | ||
| if (init?.method === 'HEAD') { | ||
| return new Response(null, { | ||
| headers: { 'Content-Length': String(zipBytes.byteLength) }, | ||
| }); | ||
| } | ||
|
|
||
| const range = new Headers(init?.headers).get('Range'); | ||
| if (!range) { | ||
| return new Response(zipBytes); | ||
| } | ||
|
|
||
| const match = /^bytes=(\d+)-(\d+)$/.exec(range); | ||
| if (!match) { | ||
| throw new Error(`Unexpected Range header: ${range}`); | ||
| } | ||
| const from = Number(match[1]); | ||
| const to = Number(match[2]); | ||
| return new Response(zipBytes.slice(from, to + 1), { status: 206 }); | ||
|
chubes4 marked this conversation as resolved.
Outdated
|
||
| }; | ||
| } | ||
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.
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.