-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Support web downloads in browser adapter #2534
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
9 commits
Select commit
Hold shift + click to select a range
9fed315
Support web downloads in browser adapter
a1573595 085d1ab
Address web download review feedback
a1573595 3a86afa
Merge branch 'main' into cw/2408
a1573595 b8192c8
Merge branch 'main' into cw/2408
a1573595 d78d5c3
Use Headers.add for redirects/uri extras in web download
a1573595 8f5c5a8
Merge branch 'main' into cw/2408
a1573595 c05e7e8
Merge branch 'main' into cw/2408
a1573595 91f96e4
Merge branch 'main' into cw/2408
AlexV525 0c608de
test: defer package-scoped web coverage selection
AlexV525 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
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
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the file needs more code comments.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated.
|
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,84 @@ | ||
| import 'dart:js_interop'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
| import 'package:web/web.dart' as web; | ||
|
|
||
| typedef TriggerBrowserDownload = void Function({ | ||
| required Uint8List bytes, | ||
| required String filename, | ||
| String? contentType, | ||
| }); | ||
|
|
||
| typedef CreateObjectUrl = String Function(JSObject object); | ||
|
|
||
| typedef RevokeObjectUrl = void Function(String objectUrl); | ||
|
|
||
| typedef CreateDownloadAnchor = web.HTMLAnchorElement Function( | ||
| String href, | ||
| String filename, | ||
| ); | ||
|
|
||
| typedef ClickDownloadAnchor = void Function(web.HTMLAnchorElement anchor); | ||
|
|
||
| TriggerBrowserDownload triggerBrowserDownload = _triggerBrowserDownload; | ||
|
|
||
| @visibleForTesting | ||
| CreateObjectUrl createObjectUrl = _createObjectUrl; | ||
|
|
||
| @visibleForTesting | ||
| RevokeObjectUrl revokeObjectUrl = _revokeObjectUrl; | ||
|
|
||
| @visibleForTesting | ||
| CreateDownloadAnchor createDownloadAnchor = _createDownloadAnchor; | ||
|
|
||
| @visibleForTesting | ||
| ClickDownloadAnchor clickDownloadAnchor = (anchor) => anchor.click(); | ||
|
|
||
| @visibleForTesting | ||
| void resetBrowserDownloadHooks() { | ||
| triggerBrowserDownload = _triggerBrowserDownload; | ||
| createObjectUrl = _createObjectUrl; | ||
| revokeObjectUrl = _revokeObjectUrl; | ||
| createDownloadAnchor = _createDownloadAnchor; | ||
| clickDownloadAnchor = (anchor) => anchor.click(); | ||
| } | ||
|
|
||
| void _triggerBrowserDownload({ | ||
| required Uint8List bytes, | ||
| required String filename, | ||
| String? contentType, | ||
| }) { | ||
| // Use JS typed arrays and package:web APIs instead of dart:html so this path | ||
| // remains compatible with Dart's WebAssembly compilation target. | ||
| final blobParts = <JSUint8Array>[bytes.toJS].toJS; | ||
| final blob = contentType == null | ||
| ? web.Blob(blobParts) | ||
| : web.Blob(blobParts, web.BlobPropertyBag(type: contentType)); | ||
| final objectUrl = createObjectUrl(blob); | ||
| web.HTMLAnchorElement? anchor; | ||
| try { | ||
| anchor = createDownloadAnchor(objectUrl, filename); | ||
| // Keep the anchor connected while clicking; some browsers are stricter | ||
| // about dispatching downloads from detached elements. | ||
| web.document.body?.appendChild(anchor); | ||
| clickDownloadAnchor(anchor); | ||
| } finally { | ||
| // Once the click is dispatched, cleanup should happen even if browser | ||
| // policies later block, rename, or prompt for the actual download. | ||
| anchor?.remove(); | ||
| revokeObjectUrl(objectUrl); | ||
| } | ||
| } | ||
|
|
||
| web.HTMLAnchorElement _createDownloadAnchor(String href, String filename) { | ||
| return web.HTMLAnchorElement() | ||
| ..href = href | ||
| ..download = filename; | ||
| } | ||
|
|
||
| String _createObjectUrl(JSObject object) => web.URL.createObjectURL(object); | ||
|
|
||
| void _revokeObjectUrl(String objectUrl) { | ||
| web.URL.revokeObjectURL(objectUrl); | ||
| } |
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.