index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.dataUriToBuffer = void 0;
  4. const common_1 = require("./common");
  5. function base64ToArrayBuffer(base64) {
  6. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  7. const bytes = [];
  8. for (let i = 0; i < base64.length; i += 4) {
  9. const idx0 = chars.indexOf(base64.charAt(i));
  10. const idx1 = chars.indexOf(base64.charAt(i + 1));
  11. const idx2 = base64.charAt(i + 2) === '='
  12. ? 0
  13. : chars.indexOf(base64.charAt(i + 2));
  14. const idx3 = base64.charAt(i + 3) === '='
  15. ? 0
  16. : chars.indexOf(base64.charAt(i + 3));
  17. const bin0 = (idx0 << 2) | (idx1 >> 4);
  18. const bin1 = ((idx1 & 15) << 4) | (idx2 >> 2);
  19. const bin2 = ((idx2 & 3) << 6) | idx3;
  20. bytes.push(bin0);
  21. if (base64.charAt(i + 2) !== '=')
  22. bytes.push(bin1);
  23. if (base64.charAt(i + 3) !== '=')
  24. bytes.push(bin2);
  25. }
  26. const buffer = new ArrayBuffer(bytes.length);
  27. const view = new Uint8Array(buffer);
  28. view.set(bytes);
  29. return buffer;
  30. }
  31. function stringToBuffer(str) {
  32. // Create a buffer with length equal to the string length
  33. const buffer = new ArrayBuffer(str.length);
  34. // Create a view to manipulate the buffer content
  35. const view = new Uint8Array(buffer);
  36. // Iterate over the string and populate the buffer with ASCII codes
  37. for (let i = 0; i < str.length; i++) {
  38. view[i] = str.charCodeAt(i);
  39. }
  40. return buffer;
  41. }
  42. /**
  43. * Returns a `Buffer` instance from the given data URI `uri`.
  44. *
  45. * @param {String} uri Data URI to turn into a Buffer instance
  46. */
  47. exports.dataUriToBuffer = (0, common_1.makeDataUriToBuffer)({ stringToBuffer, base64ToArrayBuffer });
  48. //# sourceMappingURL=index.js.map