jszip-utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*@preserve
  2. JSZipUtils - A collection of cross-browser utilities to go along with JSZip.
  3. <http://stuk.github.io/jszip-utils>
  4. (c) 2014-2019 Stuart Knightley, David Duponchel
  5. Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip-utils/master/LICENSE.markdown.
  6. */
  7. !function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.JSZipUtils=e():"undefined"!=typeof global?global.JSZipUtils=e():"undefined"!=typeof self&&(self.JSZipUtils=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  8. 'use strict';
  9. /*globals Promise */
  10. var JSZipUtils = {};
  11. // just use the responseText with xhr1, response with xhr2.
  12. // The transformation doesn't throw away high-order byte (with responseText)
  13. // because JSZip handles that case. If not used with JSZip, you may need to
  14. // do it, see https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data
  15. JSZipUtils._getBinaryFromXHR = function (xhr) {
  16. // for xhr.responseText, the 0xFF mask is applied by JSZip
  17. return xhr.response || xhr.responseText;
  18. };
  19. // taken from jQuery
  20. function createStandardXHR() {
  21. try {
  22. return new window.XMLHttpRequest();
  23. } catch( e ) {}
  24. }
  25. function createActiveXHR() {
  26. try {
  27. return new window.ActiveXObject("Microsoft.XMLHTTP");
  28. } catch( e ) {}
  29. }
  30. // Create the request object
  31. var createXHR = (typeof window !== "undefined" && window.ActiveXObject) ?
  32. /* Microsoft failed to properly
  33. * implement the XMLHttpRequest in IE7 (can't request local files),
  34. * so we use the ActiveXObject when it is available
  35. * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
  36. * we need a fallback.
  37. */
  38. function() {
  39. return createStandardXHR() || createActiveXHR();
  40. } :
  41. // For all other browsers, use the standard XMLHttpRequest object
  42. createStandardXHR;
  43. /**
  44. * @param {string} path The path to the resource to GET.
  45. * @param {function|{callback: function, progress: function}} options
  46. * @return {Promise|undefined} If no callback is passed then a promise is returned
  47. */
  48. JSZipUtils.getBinaryContent = function (path, options) {
  49. var promise, resolve, reject;
  50. var callback;
  51. if (!options) {
  52. options = {};
  53. }
  54. // backward compatible callback
  55. if (typeof options === "function") {
  56. callback = options;
  57. options = {};
  58. } else if (typeof options.callback === 'function') {
  59. // callback inside options object
  60. callback = options.callback;
  61. }
  62. if (!callback && typeof Promise !== "undefined") {
  63. promise = new Promise(function (_resolve, _reject) {
  64. resolve = _resolve;
  65. reject = _reject;
  66. });
  67. } else {
  68. resolve = function (data) { callback(null, data); };
  69. reject = function (err) { callback(err, null); };
  70. }
  71. /*
  72. * Here is the tricky part : getting the data.
  73. * In firefox/chrome/opera/... setting the mimeType to 'text/plain; charset=x-user-defined'
  74. * is enough, the result is in the standard xhr.responseText.
  75. * cf https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data_in_older_browsers
  76. * In IE <= 9, we must use (the IE only) attribute responseBody
  77. * (for binary data, its content is different from responseText).
  78. * In IE 10, the 'charset=x-user-defined' trick doesn't work, only the
  79. * responseType will work :
  80. * http://msdn.microsoft.com/en-us/library/ie/hh673569%28v=vs.85%29.aspx#Binary_Object_upload_and_download
  81. *
  82. * I'd like to use jQuery to avoid this XHR madness, but it doesn't support
  83. * the responseType attribute : http://bugs.jquery.com/ticket/11461
  84. */
  85. try {
  86. var xhr = createXHR();
  87. xhr.open('GET', path, true);
  88. // recent browsers
  89. if ("responseType" in xhr) {
  90. xhr.responseType = "arraybuffer";
  91. }
  92. // older browser
  93. if(xhr.overrideMimeType) {
  94. xhr.overrideMimeType("text/plain; charset=x-user-defined");
  95. }
  96. xhr.onreadystatechange = function (event) {
  97. // use `xhr` and not `this`... thanks IE
  98. if (xhr.readyState === 4) {
  99. if (xhr.status === 200 || xhr.status === 0) {
  100. try {
  101. resolve(JSZipUtils._getBinaryFromXHR(xhr));
  102. } catch(err) {
  103. reject(new Error(err));
  104. }
  105. } else {
  106. reject(new Error("Ajax error for " + path + " : " + this.status + " " + this.statusText));
  107. }
  108. }
  109. };
  110. if(options.progress) {
  111. xhr.onprogress = function(e) {
  112. options.progress({
  113. path: path,
  114. originalEvent: e,
  115. percent: e.loaded / e.total * 100,
  116. loaded: e.loaded,
  117. total: e.total
  118. });
  119. };
  120. }
  121. xhr.send();
  122. } catch (e) {
  123. reject(new Error(e), null);
  124. }
  125. // returns a promise or undefined depending on whether a callback was
  126. // provided
  127. return promise;
  128. };
  129. // export
  130. module.exports = JSZipUtils;
  131. // enforcing Stuk's coding style
  132. // vim: set shiftwidth=4 softtabstop=4:
  133. },{}]},{},[1])
  134. (1)
  135. });
  136. ;