Skip to content

Commit 5338b59

Browse files
Revert "refactor(http): Improves base64 encoding/decoding with feature detection (#67002)"
This reverts commit aafeb1d.
1 parent 7eb3371 commit 5338b59

File tree

3 files changed

+23
-50
lines changed

3 files changed

+23
-50
lines changed

packages/common/http/src/transfer_cache.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {HTTP_ROOT_INTERCEPTOR_FNS, HttpHandlerFn} from './interceptor';
2929
import {HttpRequest} from './request';
3030
import {HttpEvent, HttpResponse} from './response';
3131
import {HttpParams} from './params';
32-
import {fromBase64, toBase64} from './util';
3332

3433
/**
3534
* Options to configure how TransferCache should be used to cache requests made via HttpClient.
@@ -273,7 +272,7 @@ export function transferCacheInterceptorFn(
273272
transferState.set<TransferHttpResponse>(storeKey, {
274273
[BODY]:
275274
req.responseType === 'arraybuffer' || req.responseType === 'blob'
276-
? toBase64(event.body as ArrayBufferLike)
275+
? toBase64(event.body)
277276
: event.body,
278277
[HEADERS]: getFilteredHeaders(event.headers, headersToInclude),
279278
[STATUS]: event.status,
@@ -361,6 +360,28 @@ function generateHash(value: string): string {
361360
return hash.toString();
362361
}
363362

363+
function toBase64(buffer: unknown): string {
364+
//TODO: replace with when is Baseline widely available
365+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64
366+
const bytes = new Uint8Array(buffer as ArrayBufferLike);
367+
368+
const CHUNK_SIZE = 0x8000; // 32,768 bytes (~32 KB) per chunk, to avoid stack overflow
369+
370+
let binaryString = '';
371+
372+
for (let i = 0; i < bytes.length; i += CHUNK_SIZE) {
373+
const chunk = bytes.subarray(i, i + CHUNK_SIZE);
374+
binaryString += String.fromCharCode.apply(null, chunk as unknown as number[]);
375+
}
376+
return btoa(binaryString);
377+
}
378+
379+
function fromBase64(base64: string): ArrayBuffer {
380+
const binary = atob(base64);
381+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
382+
return bytes.buffer;
383+
}
384+
364385
/**
365386
* Returns the DI providers needed to enable HTTP transfer cache.
366387
*

packages/common/http/src/util.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@
531531
"hasApplyArgsData",
532532
"hasAuthHeaders",
533533
"hasDeps",
534-
"hasFromBase64",
535534
"hasInSkipHydrationBlockFlag",
536535
"hasLift",
537536
"hasMatchingDehydratedView",

0 commit comments

Comments
 (0)