Skip to content

refactor: add cross-environment decodeBase64 method#95

Open
Phillip9587 wants to merge 2 commits intojshttp:masterfrom
Phillip9587:remove-node
Open

refactor: add cross-environment decodeBase64 method#95
Phillip9587 wants to merge 2 commits intojshttp:masterfrom
Phillip9587:remove-node

Conversation

@Phillip9587
Copy link
Copy Markdown
Contributor

Based on #93

This PR decouples the package from Node.js APIs and adds support for browser and other runtimes. Base64 decoding now uses runtime feature detection:

  1. Uint8Array.fromBase64() when available (modern browsers/runtimes)
  2. Buffer.from(..., 'base64') in Node.js and compatible runtimes
  3. custom implementation using atob() and TextDecoder as a fallback

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 41.17647% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.60%. Comparing base (ee70132) to head (2b6d3e0).

Files with missing lines Patch % Lines
src/index.ts 41.17% 10 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##            master      #95       +/-   ##
============================================
- Coverage   100.00%   83.60%   -16.40%     
============================================
  Files            1        1               
  Lines           48       61       +13     
  Branches        21       23        +2     
============================================
+ Hits            48       51        +3     
- Misses           0       10       +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blakeembrey
Copy link
Copy Markdown
Member

Will need rebasing too, but would you be able to add a benchmarks folder with the different ways to decode for posterity? That way we can quickly re-run benchmarks anytime and confirm we've got the right methods selected.

@Phillip9587
Copy link
Copy Markdown
Contributor Author

@blakeembrey I added the benchmarks and switched the Uin8Array.fromBase64 and Node.js Buffer decode methods in the cross-environment decodeBase64() function because it looks like the Uint8Array.fromBase64() + TextDecoder method is still significantly slower than the Buffer version on Node.js v25.9.0

 ✓ benchmarks/base64.bench.ts > decode base64 for basic-auth payloads > short username/password 3494ms
     name                                            hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · Uint8Array.fromBase64 + TextDecoder   1,989,391.31  0.0003  3.1667  0.0005  0.0005  0.0019  0.0025  0.0044  ±1.39%   994696
   · Buffer.from(base64).toString(utf-8)   6,569,899.93  0.0001  0.4416  0.0002  0.0002  0.0003  0.0005  0.0012  ±0.35%  3284950
   · atob + Uint8Array.from + TextDecoder  1,282,955.78  0.0007  0.3789  0.0008  0.0008  0.0014  0.0015  0.0044  ±0.29%   641478

 ✓ benchmarks/base64.bench.ts > decode base64 for basic-auth payloads > common API style credential 3231ms
     name                                            hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · Uint8Array.fromBase64 + TextDecoder   1,856,823.64  0.0003  1.4238  0.0005  0.0006  0.0018  0.0022  0.0049  ±1.14%   928412
   · Buffer.from(base64).toString(utf-8)   6,761,025.03  0.0001  0.2447  0.0001  0.0001  0.0004  0.0006  0.0012  ±0.27%  3380513
   · atob + Uint8Array.from + TextDecoder    414,072.23  0.0022  0.3207  0.0024  0.0024  0.0033  0.0057  0.0063  ±0.29%   207037

 ✓ benchmarks/base64.bench.ts > decode base64 for basic-auth payloads > long token-like password 3170ms
     name                                            hz     min      max    mean     p75     p99    p995    p999     rme  samples
   · Uint8Array.fromBase64 + TextDecoder   1,774,143.02  0.0003   2.4190  0.0006  0.0006  0.0018  0.0021  0.0040  ±1.45%   887073
   · Buffer.from(base64).toString(utf-8)   5,983,805.38  0.0001  20.8329  0.0002  0.0002  0.0006  0.0008  0.0016  ±8.17%  2991903
   · atob + Uint8Array.from + TextDecoder    256,825.88  0.0033   0.2730  0.0039  0.0039  0.0054  0.0070  0.0098  ±0.26%   128413

 BENCH  Summary

  Buffer.from(base64).toString(utf-8) - benchmarks/base64.bench.ts > decode base64 for basic-auth payloads > short username/password
    3.30x faster than Uint8Array.fromBase64 + TextDecoder
    5.12x faster than atob + Uint8Array.from + TextDecoder

  Buffer.from(base64).toString(utf-8) - benchmarks/base64.bench.ts > decode base64 for basic-auth payloads > common API style credential
    3.64x faster than Uint8Array.fromBase64 + TextDecoder
    16.33x faster than atob + Uint8Array.from + TextDecoder

  Buffer.from(base64).toString(utf-8) - benchmarks/base64.bench.ts > decode base64 for basic-auth payloads > long token-like password
    3.37x faster than Uint8Array.fromBase64 + TextDecoder
    23.30x faster than atob + Uint8Array.from + TextDecoder

@Phillip9587
Copy link
Copy Markdown
Contributor Author

@blakeembrey Any suggestions on how to fix the test coverage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants