This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] consolidate network code into httpFetch #39657
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,52 +63,23 @@ class AssetManager { | |
| return Uri.encodeFull('${_baseUrl ?? ''}$assetsDir/$asset'); | ||
| } | ||
|
|
||
| /// Loads an asset and returns the server response. | ||
| Future<HttpFetchResponse> loadAsset(String asset) { | ||
| return httpFetch(getAssetUrl(asset)); | ||
| } | ||
|
|
||
| /// Loads an asset using an [DomXMLHttpRequest] and returns data as [ByteData]. | ||
| Future<ByteData> load(String asset) async { | ||
| final String url = getAssetUrl(asset); | ||
| try { | ||
| final DomXMLHttpRequest request = | ||
| await domHttpRequest(url, responseType: 'arraybuffer'); | ||
|
|
||
| final ByteBuffer response = request.response as ByteBuffer; | ||
| return response.asByteData(); | ||
| } catch (e) { | ||
| if (!domInstanceOfString(e, 'ProgressEvent')){ | ||
| rethrow; | ||
| } | ||
| final DomProgressEvent p = e as DomProgressEvent; | ||
| final DomEventTarget? target = p.target; | ||
| if (domInstanceOfString(target,'XMLHttpRequest')) { | ||
| final DomXMLHttpRequest request = target! as DomXMLHttpRequest; | ||
| if (request.status == 404 && asset == 'AssetManifest.json') { | ||
| printWarning('Asset manifest does not exist at `$url` – ignoring.'); | ||
| return Uint8List.fromList(utf8.encode('{}')).buffer.asByteData(); | ||
| } | ||
| throw AssetManagerException(url, request.status!.toInt()); | ||
| } | ||
|
|
||
| final String? constructorName = target == null ? 'null' : | ||
| domGetConstructorName(target); | ||
| printWarning('Caught ProgressEvent with unknown target: ' | ||
| '$constructorName'); | ||
| rethrow; | ||
| } | ||
| } | ||
| } | ||
| final HttpFetchResponse response = await httpFetch(url); | ||
|
|
||
| /// Thrown to indicate http failure during asset loading. | ||
| class AssetManagerException implements Exception { | ||
| /// Initializes exception with request url and http status. | ||
| AssetManagerException(this.url, this.httpStatus); | ||
|
|
||
| /// Http request url for asset. | ||
| final String url; | ||
|
|
||
| /// Http status of response. | ||
| final int httpStatus; | ||
| if (response.status == 404 && asset == 'AssetManifest.json') { | ||
| printWarning('Asset manifest does not exist at `$url` - ignoring.'); | ||
| return Uint8List.fromList(utf8.encode('{}')).buffer.asByteData(); | ||
| } | ||
|
|
||
| @override | ||
| String toString() => 'Failed to load asset at "$url" ($httpStatus)'; | ||
| return (await response.payload.asByteBuffer()).asByteData(); | ||
| } | ||
| } | ||
|
|
||
| /// An asset manager that gives fake empty responses for assets. | ||
|
Contributor
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. Can the |
||
|
|
@@ -141,6 +112,33 @@ class WebOnlyMockAssetManager implements AssetManager { | |
| @override | ||
| String getAssetUrl(String asset) => asset; | ||
|
|
||
| @override | ||
| Future<HttpFetchResponse> loadAsset(String asset) async { | ||
| if (asset == getAssetUrl('AssetManifest.json')) { | ||
| return MockHttpFetchResponse( | ||
| url: asset, | ||
| status: 200, | ||
| payload: MockHttpFetchPayload( | ||
| byteBuffer: _toByteData(utf8.encode(defaultAssetManifest)).buffer, | ||
| ) | ||
| ); | ||
| } | ||
| if (asset == getAssetUrl('FontManifest.json')) { | ||
| return MockHttpFetchResponse( | ||
| url: asset, | ||
| status: 200, | ||
| payload: MockHttpFetchPayload( | ||
| byteBuffer: _toByteData(utf8.encode(defaultFontManifest)).buffer, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return MockHttpFetchResponse( | ||
| url: asset, | ||
| status: 404, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<ByteData> load(String asset) { | ||
| if (asset == getAssetUrl('AssetManifest.json')) { | ||
|
|
@@ -151,7 +149,7 @@ class WebOnlyMockAssetManager implements AssetManager { | |
| return Future<ByteData>.value( | ||
| _toByteData(utf8.encode(defaultFontManifest))); | ||
| } | ||
| throw AssetManagerException(asset, 404); | ||
| throw HttpFetchNoPayloadError(asset, status: 404); | ||
| } | ||
|
|
||
| ByteData _toByteData(List<int> bytes) { | ||
|
|
||
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.
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.
copy-paste?
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.
Yep! Fixed.