base64.d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
  3. * (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
  4. *
  5. * ```js
  6. * import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
  7. * import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
  8. * import { fromBase64any } from '@exodus/bytes/base64.js'
  9. * ```
  10. *
  11. * @module @exodus/bytes/base64.js
  12. */
  13. /// <reference types="node" />
  14. import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
  15. /**
  16. * Options for base64 encoding
  17. */
  18. export interface ToBase64Options {
  19. /** Whether to include padding characters (default: true for base64, false for base64url) */
  20. padding?: boolean;
  21. }
  22. /**
  23. * Padding mode for base64 decoding
  24. * - `true`: padding is required
  25. * - `false`: padding is not allowed (default for base64url)
  26. * - `'both'`: padding is optional (default for base64)
  27. */
  28. export type PaddingMode = boolean | 'both';
  29. /**
  30. * Options for base64 decoding
  31. */
  32. export interface FromBase64Options {
  33. /** Output format (default: 'uint8') */
  34. format?: OutputFormat;
  35. /** Padding mode */
  36. padding?: PaddingMode;
  37. }
  38. /**
  39. * Encode a `Uint8Array` to a base64 string (RFC 4648)
  40. *
  41. * @param arr - The input bytes
  42. * @param options - Encoding options
  43. * @returns The base64 encoded string
  44. */
  45. export function toBase64(arr: Uint8Array, options?: ToBase64Options): string;
  46. /**
  47. * Encode a `Uint8Array` to a base64url string (RFC 4648)
  48. *
  49. * @param arr - The input bytes
  50. * @param options - Encoding options (padding defaults to false)
  51. * @returns The base64url encoded string
  52. */
  53. export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;
  54. /**
  55. * Decode a base64 string to bytes
  56. *
  57. * Operates in strict mode for last chunk, does not allow whitespace
  58. *
  59. * @param string - The base64 encoded string
  60. * @param options - Decoding options
  61. * @returns The decoded bytes
  62. */
  63. export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
  64. export function fromBase64(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
  65. /**
  66. * Decode a base64url string to bytes
  67. *
  68. * Operates in strict mode for last chunk, does not allow whitespace
  69. *
  70. * @param string - The base64url encoded string
  71. * @param options - Decoding options (padding defaults to false)
  72. * @returns The decoded bytes
  73. */
  74. export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
  75. export function fromBase64url(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
  76. /**
  77. * Decode either base64 or base64url string to bytes
  78. *
  79. * Automatically detects the variant based on characters present
  80. *
  81. * @param string - The base64 or base64url encoded string
  82. * @param options - Decoding options
  83. * @returns The decoded bytes
  84. */
  85. export function fromBase64any(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
  86. export function fromBase64any(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;