browser-atob.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. (function (w) {
  2. "use strict";
  3. function findBest(atobNative) {
  4. // normal window
  5. if ('function' === typeof atobNative) { return atobNative; }
  6. // browserify (web worker)
  7. if ('function' === typeof Buffer) {
  8. return function atobBrowserify(a) {
  9. //!! Deliberately using an API that's deprecated in node.js because
  10. //!! this file is for browsers and we expect them to cope with it.
  11. //!! Discussion: github.com/node-browser-compat/atob/pull/9
  12. return new Buffer(a, 'base64').toString('binary');
  13. };
  14. }
  15. // ios web worker with base64js
  16. if ('object' === typeof w.base64js) {
  17. // bufferToBinaryString
  18. // https://git.coolaj86.com/coolaj86/unibabel.js/blob/master/index.js#L50
  19. return function atobWebWorker_iOS(a) {
  20. var buf = w.base64js.b64ToByteArray(a);
  21. return Array.prototype.map.call(buf, function (ch) {
  22. return String.fromCharCode(ch);
  23. }).join('');
  24. };
  25. }
  26. return function () {
  27. // ios web worker without base64js
  28. throw new Error("You're probably in an old browser or an iOS webworker." +
  29. " It might help to include beatgammit's base64-js.");
  30. };
  31. }
  32. var atobBest = findBest(w.atob);
  33. w.atob = atobBest;
  34. if ((typeof module === 'object') && module && module.exports) {
  35. module.exports = atobBest;
  36. }
  37. }(window));