refactor(http): Improves base64 encoding/decoding with feature detection#67002
refactor(http): Improves base64 encoding/decoding with feature detection#67002SkyZeroZx wants to merge 5 commits intoangular:mainfrom
Conversation
alan-agius4
left a comment
There was a problem hiding this comment.
LGTM, thanks for this.
packages/common/http/src/util.ts
Outdated
| export function toBase64(buffer: unknown): string { | ||
| const bytes = new Uint8Array(buffer as ArrayBufferLike); |
There was a problem hiding this comment.
This looks off. Since toBase64 can get a Blob would it throw ?
I would very much prefer that this function is correctly typed and that the type assertion is made in the transfer cache.
There was a problem hiding this comment.
I’ve just updated it. It’s weird, but it doesn’t throw an exception if we pass a Blob; it just sets the Uint8Array length to 0.
There was a problem hiding this comment.
Is the Uint8Array array empty if we pass it a blob ?
There was a problem hiding this comment.
If we can pass some like
const myBlob = new Blob(['hello' , 'world'])
const u8 = new Uint8Array(myBlob)
console.log(u8)bd809c2 to
3fe3545
Compare
| [BODY]: | ||
| req.responseType === 'arraybuffer' || req.responseType === 'blob' | ||
| ? toBase64(event.body) | ||
| ? toBase64(event.body as ArrayBufferLike) |
There was a problem hiding this comment.
Is this correct for a blob? If yes we should at least have a test that would show that this is okay.
There was a problem hiding this comment.
I agree, I'll add a test for that and validate it.
There was a problem hiding this comment.
Can you also add a comment, because that type assertion doesn't make it obvious
There was a problem hiding this comment.
Is this correct for a blob? If yes we should at least have a test that would show that this is okay.
I added a fix in this PR because parsing was needed. I tested it with Protocol Buffer, and I was confused by the response because it was always a Uint8Array (which is curious, since it works fine when setting a Blob). That was misleading on my part. This should fix the issue, and I tested both cases ( Blob and Uint8Array ) to ensure consistency
7e21743 to
62f6ea1
Compare
|
We can rerun the pipeline that the test failed in (hydration_spec.ts), since TransferCache now has asynchronous behavior |
32db6da to
086710c
Compare
Previously, Blob values were passed to `Uint8Array` this resulted in silently producing an empty array (length = 0) without throwing an error, leading to empty cached data PR Close #67002
Previously, Blob values were passed to `Uint8Array` this resulted in silently producing an empty array (length = 0) without throwing an error, leading to empty cached data PR Close #67002
|
This appears to have broken tests in main. We're looking into it. |
Could this be related to how tests were written , now that it behaves asynchronously due to the recent changes? angular/packages/common/http/src/transfer_cache.ts Lines 269 to 280 in 9e64147 |
…ngular#67002)" This reverts commit 1f057af.
…e detection (angular#67002)" This reverts commit aafeb1d.
|
This definitely was the root cause. We're reverting this for now. Please take a look, @SkyZeroZx. |
Previously, Blob values were passed to `Uint8Array` this resulted in silently producing an empty array (length = 0) without throwing an error, leading to empty cached data
Previously, Blob values were passed to `Uint8Array` this resulted in silently producing an empty array (length = 0) without throwing an error, leading to empty cached data
Previously, Blob values were passed to `Uint8Array` this resulted in silently producing an empty array (length = 0) without throwing an error, leading to empty cached data
Ensure httpResource properly handles the response flow
086710c to
8312083
Compare
| @@ -424,15 +425,10 @@ describe('httpResource', () => { | |||
|
|
|||
| it('should synchronously resolve with a cached value from TransferState', async () => { | |||
There was a problem hiding this comment.
According to my review, this test was failing due to the recent changes to the transfer cache, which now behaves asynchronously.
I'm not sure if we should change the test name or if it's correct.
There was a problem hiding this comment.
This change was intentional to ensure there's no loading state when pulling data from the transfer cache. #67307
There was a problem hiding this comment.
So I think it would stay as it is, only keeping the change I made due to the TransferCache update
There was a problem hiding this comment.
@thePunderWoman With the latest changes, I think it would be okay to merge, or would we still need to adjust something?
Use feature detection for
Uint8Array.prototype.toBase64andUint8Array.fromBase64, falling back to the existing implementation when native support is not availableThis is considerably faster when we have sizes like 50kb some like
https://github.com/SkyZeroZx/benchmark-encode-decode-64