You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 6.8 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.loadFromFile = exports.isCancelException = exports.makePageCallback = exports.cancelRunningTask = exports.displayCORSWarning = exports.errorOnDev = exports.warnOnDev = exports.getPixelRatio = exports.dataURItoUint8Array = exports.isDataURI = exports.isFile = exports.isBlob = exports.isArrayBuffer = exports.isString = exports.isProvided = exports.isDefined = exports.isProduction = exports.isLocalFileSystem = exports.isBrowser = void 0;
  6. /**
  7. * Checks if we're running in a browser environment.
  8. */
  9. var isBrowser = typeof window !== 'undefined';
  10. /**
  11. * Checks whether we're running from a local file system.
  12. */
  13. exports.isBrowser = isBrowser;
  14. var isLocalFileSystem = isBrowser && window.location.protocol === 'file:';
  15. /**
  16. * Checks whether we're running on a production build or not.
  17. */
  18. exports.isLocalFileSystem = isLocalFileSystem;
  19. var isProduction = process.env.NODE_ENV === 'production';
  20. /**
  21. * Checks whether a variable is defined.
  22. *
  23. * @param {*} variable Variable to check
  24. */
  25. exports.isProduction = isProduction;
  26. var isDefined = function isDefined(variable) {
  27. return typeof variable !== 'undefined';
  28. };
  29. /**
  30. * Checks whether a variable is defined and not null.
  31. *
  32. * @param {*} variable Variable to check
  33. */
  34. exports.isDefined = isDefined;
  35. var isProvided = function isProvided(variable) {
  36. return isDefined(variable) && variable !== null;
  37. };
  38. /**
  39. * Checkes whether a variable provided is a string.
  40. *
  41. * @param {*} variable Variable to check
  42. */
  43. exports.isProvided = isProvided;
  44. var isString = function isString(variable) {
  45. return typeof variable === 'string';
  46. };
  47. /**
  48. * Checks whether a variable provided is an ArrayBuffer.
  49. *
  50. * @param {*} variable Variable to check
  51. */
  52. exports.isString = isString;
  53. var isArrayBuffer = function isArrayBuffer(variable) {
  54. return variable instanceof ArrayBuffer;
  55. };
  56. /**
  57. * Checkes whether a variable provided is a Blob.
  58. *
  59. * @param {*} variable Variable to check
  60. */
  61. exports.isArrayBuffer = isArrayBuffer;
  62. var isBlob = function isBlob(variable) {
  63. if (!isBrowser) {
  64. throw new Error('Attempted to check if a variable is a Blob on a non-browser environment.');
  65. }
  66. return variable instanceof Blob;
  67. };
  68. /**
  69. * Checkes whether a variable provided is a File.
  70. *
  71. * @param {*} variable Variable to check
  72. */
  73. exports.isBlob = isBlob;
  74. var isFile = function isFile(variable) {
  75. if (!isBrowser) {
  76. throw new Error('Attempted to check if a variable is a File on a non-browser environment.');
  77. }
  78. return variable instanceof File;
  79. };
  80. /**
  81. * Checks whether a string provided is a data URI.
  82. *
  83. * @param {String} str String to check
  84. */
  85. exports.isFile = isFile;
  86. var isDataURI = function isDataURI(str) {
  87. return isString(str) && /^data:/.test(str);
  88. };
  89. exports.isDataURI = isDataURI;
  90. var dataURItoUint8Array = function dataURItoUint8Array(dataURI) {
  91. if (!isDataURI(dataURI)) {
  92. throw new Error('dataURItoUint8Array was provided with an argument which is not a valid data URI.');
  93. }
  94. var byteString;
  95. if (dataURI.split(',')[0].indexOf('base64') >= 0) {
  96. byteString = atob(dataURI.split(',')[1]);
  97. } else {
  98. byteString = unescape(dataURI.split(',')[1]);
  99. }
  100. var ia = new Uint8Array(byteString.length);
  101. for (var i = 0; i < byteString.length; i += 1) {
  102. ia[i] = byteString.charCodeAt(i);
  103. }
  104. return ia;
  105. };
  106. exports.dataURItoUint8Array = dataURItoUint8Array;
  107. var getPixelRatio = function getPixelRatio() {
  108. return isBrowser && window.devicePixelRatio || 1;
  109. };
  110. exports.getPixelRatio = getPixelRatio;
  111. var consoleOnDev = function consoleOnDev(method) {
  112. if (!isProduction) {
  113. var _console;
  114. for (var _len = arguments.length, message = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  115. message[_key - 1] = arguments[_key];
  116. }
  117. // eslint-disable-next-line no-console
  118. (_console = console)[method].apply(_console, message);
  119. }
  120. };
  121. var warnOnDev = function warnOnDev() {
  122. for (var _len2 = arguments.length, message = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  123. message[_key2] = arguments[_key2];
  124. }
  125. return consoleOnDev.apply(void 0, ['warn'].concat(message));
  126. };
  127. exports.warnOnDev = warnOnDev;
  128. var errorOnDev = function errorOnDev() {
  129. for (var _len3 = arguments.length, message = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  130. message[_key3] = arguments[_key3];
  131. }
  132. return consoleOnDev.apply(void 0, ['error'].concat(message));
  133. };
  134. exports.errorOnDev = errorOnDev;
  135. var displayCORSWarning = function displayCORSWarning() {
  136. if (isLocalFileSystem) {
  137. warnOnDev('Loading PDF as base64 strings/URLs might not work on protocols other than HTTP/HTTPS. On Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.');
  138. }
  139. };
  140. exports.displayCORSWarning = displayCORSWarning;
  141. var cancelRunningTask = function cancelRunningTask(runningTask) {
  142. if (runningTask && runningTask.cancel) runningTask.cancel();
  143. };
  144. exports.cancelRunningTask = cancelRunningTask;
  145. var makePageCallback = function makePageCallback(page, scale) {
  146. Object.defineProperty(page, 'width', {
  147. get: function get() {
  148. return this.view[2] * scale;
  149. },
  150. configurable: true
  151. });
  152. Object.defineProperty(page, 'height', {
  153. get: function get() {
  154. return this.view[3] * scale;
  155. },
  156. configurable: true
  157. });
  158. Object.defineProperty(page, 'originalWidth', {
  159. get: function get() {
  160. return this.view[2];
  161. },
  162. configurable: true
  163. });
  164. Object.defineProperty(page, 'originalHeight', {
  165. get: function get() {
  166. return this.view[3];
  167. },
  168. configurable: true
  169. });
  170. return page;
  171. };
  172. exports.makePageCallback = makePageCallback;
  173. var isCancelException = function isCancelException(error) {
  174. return error.name === 'RenderingCancelledException';
  175. };
  176. exports.isCancelException = isCancelException;
  177. var loadFromFile = function loadFromFile(file) {
  178. return new Promise(function (resolve, reject) {
  179. var reader = new FileReader();
  180. reader.onload = function () {
  181. return resolve(new Uint8Array(reader.result));
  182. };
  183. reader.onerror = function (event) {
  184. switch (event.target.error.code) {
  185. case event.target.error.NOT_FOUND_ERR:
  186. return reject(new Error('Error while reading a file: File not found.'));
  187. case event.target.error.NOT_READABLE_ERR:
  188. return reject(new Error('Error while reading a file: File not readable.'));
  189. case event.target.error.SECURITY_ERR:
  190. return reject(new Error('Error while reading a file: Security error.'));
  191. case event.target.error.ABORT_ERR:
  192. return reject(new Error('Error while reading a file: Aborted.'));
  193. default:
  194. return reject(new Error('Error while reading a file.'));
  195. }
  196. };
  197. reader.readAsArrayBuffer(file);
  198. return null;
  199. });
  200. };
  201. exports.loadFromFile = loadFromFile;