base64.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { AnthropicError } from "../../core/error.mjs";
  3. import { encodeUTF8 } from "./bytes.mjs";
  4. export const toBase64 = (data) => {
  5. if (!data)
  6. return '';
  7. if (typeof globalThis.Buffer !== 'undefined') {
  8. return globalThis.Buffer.from(data).toString('base64');
  9. }
  10. if (typeof data === 'string') {
  11. data = encodeUTF8(data);
  12. }
  13. if (typeof btoa !== 'undefined') {
  14. return btoa(String.fromCharCode.apply(null, data));
  15. }
  16. throw new AnthropicError('Cannot generate base64 string; Expected `Buffer` or `btoa` to be defined');
  17. };
  18. export const fromBase64 = (str) => {
  19. if (typeof globalThis.Buffer !== 'undefined') {
  20. const buf = globalThis.Buffer.from(str, 'base64');
  21. return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
  22. }
  23. if (typeof atob !== 'undefined') {
  24. const bstr = atob(str);
  25. const buf = new Uint8Array(bstr.length);
  26. for (let i = 0; i < bstr.length; i++) {
  27. buf[i] = bstr.charCodeAt(i);
  28. }
  29. return buf;
  30. }
  31. throw new AnthropicError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');
  32. };
  33. //# sourceMappingURL=base64.mjs.map