pdfjs-dist 2.1.266 → 2.5.207

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pdfjs-dist might be problematic. Click here for more details.

Files changed (191) hide show
  1. package/CODE_OF_CONDUCT.md +15 -0
  2. package/README.md +4 -0
  3. package/bower.json +1 -1
  4. package/build/pdf.js +8382 -18492
  5. package/build/pdf.js.map +1 -1
  6. package/build/pdf.min.js +22 -1
  7. package/build/pdf.worker.entry.js +5 -3
  8. package/build/pdf.worker.js +20417 -29816
  9. package/build/pdf.worker.js.map +1 -1
  10. package/build/pdf.worker.min.js +22 -1
  11. package/es5/build/pdf.js +25688 -0
  12. package/es5/build/pdf.js.map +1 -0
  13. package/{lib/shared/global_scope.js → es5/build/pdf.worker.entry.js} +5 -10
  14. package/es5/build/pdf.worker.js +58239 -0
  15. package/es5/build/pdf.worker.js.map +1 -0
  16. package/es5/web/images/annotation-check.svg +11 -0
  17. package/es5/web/images/annotation-comment.svg +16 -0
  18. package/es5/web/images/annotation-help.svg +26 -0
  19. package/es5/web/images/annotation-insert.svg +10 -0
  20. package/es5/web/images/annotation-key.svg +11 -0
  21. package/es5/web/images/annotation-newparagraph.svg +11 -0
  22. package/es5/web/images/annotation-noicon.svg +7 -0
  23. package/es5/web/images/annotation-note.svg +42 -0
  24. package/es5/web/images/annotation-paragraph.svg +16 -0
  25. package/es5/web/images/loading-icon.gif +0 -0
  26. package/es5/web/images/shadow.png +0 -0
  27. package/es5/web/images/texture.png +0 -0
  28. package/es5/web/pdf_viewer.css +403 -0
  29. package/es5/web/pdf_viewer.js +7742 -0
  30. package/es5/web/pdf_viewer.js.map +1 -0
  31. package/image_decoders/pdf.image_decoders.js +1475 -4897
  32. package/image_decoders/pdf.image_decoders.js.map +1 -1
  33. package/image_decoders/pdf.image_decoders.min.js +22 -1
  34. package/lib/README.md +7 -0
  35. package/lib/core/annotation.js +750 -899
  36. package/lib/core/arithmetic_decoder.js +81 -97
  37. package/lib/core/bidi.js +54 -46
  38. package/lib/core/ccitt.js +88 -81
  39. package/lib/core/ccitt_stream.js +15 -14
  40. package/lib/core/cff_parser.js +235 -183
  41. package/lib/core/charsets.js +4 -4
  42. package/lib/core/chunked_stream.js +447 -542
  43. package/lib/core/cmap.js +222 -264
  44. package/lib/core/colorspace.js +699 -863
  45. package/lib/core/core_utils.js +152 -0
  46. package/lib/core/crypto.js +379 -437
  47. package/lib/core/document.js +573 -660
  48. package/lib/core/encodings.js +15 -15
  49. package/lib/core/evaluator.js +1103 -868
  50. package/lib/core/font_renderer.js +135 -178
  51. package/lib/core/fonts.js +570 -491
  52. package/lib/core/function.js +291 -288
  53. package/lib/core/glyphlist.js +4527 -4526
  54. package/lib/core/image.js +145 -149
  55. package/lib/core/image_utils.js +170 -0
  56. package/lib/core/jbig2.js +325 -316
  57. package/lib/core/jbig2_stream.js +18 -17
  58. package/lib/core/jpeg_stream.js +21 -26
  59. package/lib/core/jpg.js +284 -232
  60. package/lib/core/jpx.js +161 -143
  61. package/lib/core/jpx_stream.js +28 -28
  62. package/lib/core/metrics.js +2929 -2929
  63. package/lib/core/murmurhash3.js +90 -101
  64. package/lib/core/obj.js +1183 -1157
  65. package/lib/core/operator_list.js +99 -67
  66. package/lib/core/parser.js +972 -911
  67. package/lib/core/pattern.js +87 -70
  68. package/lib/core/pdf_manager.js +150 -315
  69. package/lib/core/primitives.js +83 -56
  70. package/lib/core/ps_parser.js +175 -214
  71. package/lib/core/standard_fonts.js +237 -236
  72. package/lib/core/stream.js +94 -74
  73. package/lib/core/type1_parser.js +87 -69
  74. package/lib/core/unicode.js +1654 -1654
  75. package/lib/core/worker.js +193 -390
  76. package/lib/core/worker_stream.js +168 -0
  77. package/lib/display/annotation_layer.js +741 -972
  78. package/lib/display/api.js +1500 -1791
  79. package/lib/display/api_compatibility.js +12 -17
  80. package/lib/display/canvas.js +165 -165
  81. package/lib/display/content_disposition.js +40 -59
  82. package/lib/display/display_utils.js +515 -0
  83. package/lib/display/fetch_stream.js +183 -298
  84. package/lib/display/font_loader.js +273 -413
  85. package/lib/display/metadata.js +86 -98
  86. package/lib/display/network.js +266 -359
  87. package/lib/display/network_utils.js +25 -18
  88. package/lib/display/node_stream.js +285 -458
  89. package/lib/display/pattern_helper.js +113 -65
  90. package/lib/display/svg.js +1166 -901
  91. package/lib/display/text_layer.js +156 -132
  92. package/lib/display/transport_stream.js +262 -278
  93. package/lib/display/webgl.js +70 -83
  94. package/lib/display/worker_options.js +3 -3
  95. package/lib/display/xml_parser.js +303 -392
  96. package/lib/examples/node/domstubs.js +40 -37
  97. package/lib/pdf.js +226 -59
  98. package/lib/pdf.worker.js +14 -6
  99. package/lib/shared/compatibility.js +3 -246
  100. package/lib/shared/is_node.js +7 -6
  101. package/lib/shared/message_handler.js +327 -332
  102. package/lib/shared/util.js +266 -416
  103. package/lib/test/unit/annotation_spec.js +1555 -701
  104. package/lib/test/unit/api_spec.js +802 -604
  105. package/lib/test/unit/bidi_spec.js +7 -7
  106. package/lib/test/unit/cff_parser_spec.js +84 -69
  107. package/lib/test/unit/clitests_helper.js +7 -9
  108. package/lib/test/unit/cmap_spec.js +74 -76
  109. package/lib/test/unit/colorspace_spec.js +166 -161
  110. package/lib/test/unit/core_utils_spec.js +211 -0
  111. package/lib/test/unit/crypto_spec.js +181 -181
  112. package/lib/test/unit/custom_spec.js +20 -22
  113. package/lib/test/unit/display_svg_spec.js +34 -39
  114. package/lib/test/unit/display_utils_spec.js +263 -0
  115. package/lib/test/unit/document_spec.js +16 -21
  116. package/lib/test/unit/encodings_spec.js +12 -34
  117. package/lib/test/unit/evaluator_spec.js +83 -83
  118. package/lib/test/unit/fetch_stream_spec.js +111 -0
  119. package/lib/test/unit/function_spec.js +206 -204
  120. package/lib/test/unit/jasmine-boot.js +46 -30
  121. package/lib/test/unit/message_handler_spec.js +173 -159
  122. package/lib/test/unit/metadata_spec.js +69 -69
  123. package/lib/test/unit/murmurhash3_spec.js +12 -12
  124. package/lib/test/unit/network_spec.js +13 -61
  125. package/lib/test/unit/network_utils_spec.js +183 -119
  126. package/lib/test/unit/node_stream_spec.js +78 -92
  127. package/lib/test/unit/parser_spec.js +172 -114
  128. package/lib/test/unit/pdf_find_controller_spec.js +55 -86
  129. package/lib/test/unit/pdf_find_utils_spec.js +32 -32
  130. package/lib/test/unit/pdf_history_spec.js +32 -32
  131. package/lib/test/unit/primitives_spec.js +140 -125
  132. package/lib/test/unit/stream_spec.js +16 -14
  133. package/lib/test/unit/test_utils.js +131 -143
  134. package/lib/test/unit/testreporter.js +19 -19
  135. package/lib/test/unit/type1_parser_spec.js +42 -42
  136. package/lib/test/unit/ui_utils_spec.js +297 -459
  137. package/lib/test/unit/unicode_spec.js +38 -38
  138. package/lib/test/unit/util_spec.js +121 -305
  139. package/lib/web/annotation_layer_builder.js +66 -103
  140. package/lib/web/app.js +1328 -1214
  141. package/lib/web/app_options.js +105 -107
  142. package/lib/web/base_viewer.js +824 -838
  143. package/lib/web/chromecom.js +165 -252
  144. package/lib/web/debugger.js +149 -205
  145. package/lib/web/download_manager.js +39 -55
  146. package/lib/web/firefox_print_service.js +37 -27
  147. package/lib/web/firefoxcom.js +212 -363
  148. package/lib/web/genericcom.js +26 -108
  149. package/lib/web/genericl10n.js +24 -153
  150. package/lib/web/grab_to_pan.js +32 -30
  151. package/lib/web/interfaces.js +80 -254
  152. package/lib/web/overlay_manager.js +70 -246
  153. package/lib/web/password_prompt.js +38 -64
  154. package/lib/web/pdf_attachment_viewer.js +113 -131
  155. package/lib/web/pdf_cursor_tools.js +75 -102
  156. package/lib/web/pdf_document_properties.js +221 -306
  157. package/lib/web/pdf_find_bar.js +136 -170
  158. package/lib/web/pdf_find_controller.js +491 -548
  159. package/lib/web/pdf_find_utils.js +13 -13
  160. package/lib/web/pdf_history.js +397 -406
  161. package/lib/web/pdf_link_service.js +304 -348
  162. package/lib/web/pdf_outline_viewer.js +140 -175
  163. package/lib/web/pdf_page_view.js +452 -523
  164. package/lib/web/pdf_presentation_mode.js +308 -357
  165. package/lib/web/pdf_print_service.js +90 -104
  166. package/lib/web/pdf_rendering_queue.js +84 -108
  167. package/lib/web/pdf_sidebar.js +276 -306
  168. package/lib/web/pdf_sidebar_resizer.js +92 -119
  169. package/lib/web/pdf_single_page_viewer.js +77 -126
  170. package/lib/web/pdf_thumbnail_view.js +276 -297
  171. package/lib/web/pdf_thumbnail_viewer.js +186 -206
  172. package/lib/web/pdf_viewer.component.js +20 -21
  173. package/lib/web/pdf_viewer.js +55 -115
  174. package/lib/web/preferences.js +82 -286
  175. package/lib/web/secondary_toolbar.js +164 -196
  176. package/lib/web/text_layer_builder.js +282 -339
  177. package/lib/web/toolbar.js +217 -210
  178. package/lib/web/ui_utils.js +267 -420
  179. package/lib/web/view_history.js +52 -226
  180. package/lib/web/viewer_compatibility.js +21 -6
  181. package/package.json +2 -9
  182. package/web/pdf_viewer.css +35 -25
  183. package/web/pdf_viewer.js +3489 -4855
  184. package/web/pdf_viewer.js.map +1 -1
  185. package/webpack.js +14 -5
  186. package/external/streams/streams-lib.js +0 -3962
  187. package/external/url/url-lib.js +0 -627
  188. package/lib/display/dom_utils.js +0 -494
  189. package/lib/shared/streams_polyfill.js +0 -43
  190. package/lib/shared/url_polyfill.js +0 -56
  191. package/lib/test/unit/dom_utils_spec.js +0 -89
@@ -2,7 +2,7 @@
2
2
  * @licstart The following is the entire license notice for the
3
3
  * Javascript code in this page
4
4
  *
5
- * Copyright 2018 Mozilla Foundation
5
+ * Copyright 2020 Mozilla Foundation
6
6
  *
7
7
  * Licensed under the Apache License, Version 2.0 (the "License");
8
8
  * you may not use this file except in compliance with the License.
@@ -21,48 +21,34 @@
21
21
  */
22
22
  "use strict";
23
23
 
24
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
24
+ var _test_utils = require("./test_utils.js");
25
25
 
26
- var _test_utils = require("./test_utils");
26
+ var _util = require("../../shared/util.js");
27
27
 
28
- var _util = require("../../shared/util");
28
+ var _display_utils = require("../../display/display_utils.js");
29
29
 
30
- var _dom_utils = require("../../display/dom_utils");
30
+ var _api = require("../../display/api.js");
31
31
 
32
- var _api = require("../../display/api");
32
+ var _ui_utils = require("../../web/ui_utils.js");
33
33
 
34
- var _worker_options = require("../../display/worker_options");
34
+ var _image_utils = require("../../core/image_utils.js");
35
35
 
36
- var _is_node = _interopRequireDefault(require("../../shared/is_node"));
36
+ var _worker_options = require("../../display/worker_options.js");
37
37
 
38
- var _metadata = require("../../display/metadata");
38
+ var _is_node = require("../../shared/is_node.js");
39
39
 
40
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
+ var _metadata = require("../../display/metadata.js");
41
41
 
42
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
43
-
44
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
45
-
46
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
47
-
48
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
49
-
50
- function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
51
-
52
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
53
-
54
- function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
55
-
56
- describe('api', function () {
57
- var basicApiFileName = 'basicapi.pdf';
58
- var basicApiFileLength = 105779;
59
- var basicApiGetDocumentParams = (0, _test_utils.buildGetDocumentParams)(basicApiFileName);
60
- var CanvasFactory;
42
+ describe("api", function () {
43
+ const basicApiFileName = "basicapi.pdf";
44
+ const basicApiFileLength = 105779;
45
+ const basicApiGetDocumentParams = (0, _test_utils.buildGetDocumentParams)(basicApiFileName);
46
+ let CanvasFactory;
61
47
  beforeAll(function (done) {
62
- if ((0, _is_node.default)()) {
48
+ if (_is_node.isNodeJS) {
63
49
  CanvasFactory = new _test_utils.NodeCanvasFactory();
64
50
  } else {
65
- CanvasFactory = new _dom_utils.DOMCanvasFactory();
51
+ CanvasFactory = new _display_utils.DOMCanvasFactory();
66
52
  }
67
53
 
68
54
  done();
@@ -79,8 +65,8 @@ describe('api', function () {
79
65
  }, WAIT_TIMEOUT);
80
66
  }
81
67
 
82
- describe('getDocument', function () {
83
- it('creates pdf doc from URL', function (done) {
68
+ describe("getDocument", function () {
69
+ it("creates pdf doc from URL", function (done) {
84
70
  var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
85
71
  var progressReportedCapability = (0, _util.createPromiseCapability)();
86
72
 
@@ -98,17 +84,17 @@ describe('api', function () {
98
84
  loadingTask.destroy().then(done);
99
85
  }).catch(done.fail);
100
86
  });
101
- it('creates pdf doc from URL and aborts before worker initialized', function (done) {
87
+ it("creates pdf doc from URL and aborts before worker initialized", function (done) {
102
88
  var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
103
- var destroyed = loadingTask.destroy();
89
+ const destroyed = loadingTask.destroy();
104
90
  loadingTask.promise.then(function (reason) {
105
- done.fail('shall fail loading');
91
+ done.fail("shall fail loading");
106
92
  }).catch(function (reason) {
107
93
  expect(true).toEqual(true);
108
94
  destroyed.then(done);
109
95
  });
110
96
  });
111
- it('creates pdf doc from URL and aborts loading after worker initialized', function (done) {
97
+ it("creates pdf doc from URL and aborts loading after worker initialized", function (done) {
112
98
  var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
113
99
 
114
100
  var destroyed = loadingTask._worker.promise.then(function () {
@@ -120,85 +106,69 @@ describe('api', function () {
120
106
  done();
121
107
  }).catch(done.fail);
122
108
  });
123
- it('creates pdf doc from typed array', function (done) {
124
- var typedArrayPdf;
109
+ it("creates pdf doc from typed array", function (done) {
110
+ let typedArrayPdfPromise;
125
111
 
126
- if ((0, _is_node.default)()) {
127
- typedArrayPdf = _test_utils.NodeFileReaderFactory.fetch({
112
+ if (_is_node.isNodeJS) {
113
+ typedArrayPdfPromise = _test_utils.NodeFileReaderFactory.fetch({
128
114
  path: _test_utils.TEST_PDFS_PATH.node + basicApiFileName
129
115
  });
130
116
  } else {
131
- var nonBinaryRequest = false;
132
- var request = new XMLHttpRequest();
133
- request.open('GET', _test_utils.TEST_PDFS_PATH.dom + basicApiFileName, false);
134
-
135
- try {
136
- request.responseType = 'arraybuffer';
137
- nonBinaryRequest = request.responseType !== 'arraybuffer';
138
- } catch (e) {
139
- nonBinaryRequest = true;
140
- }
141
-
142
- if (nonBinaryRequest && request.overrideMimeType) {
143
- request.overrideMimeType('text/plain; charset=x-user-defined');
144
- }
145
-
146
- request.send(null);
147
-
148
- if (nonBinaryRequest) {
149
- typedArrayPdf = (0, _util.stringToBytes)(request.responseText);
150
- } else {
151
- typedArrayPdf = new Uint8Array(request.response);
152
- }
117
+ typedArrayPdfPromise = _test_utils.DOMFileReaderFactory.fetch({
118
+ path: _test_utils.TEST_PDFS_PATH.dom + basicApiFileName
119
+ });
153
120
  }
154
121
 
155
- expect(typedArrayPdf.length).toEqual(basicApiFileLength);
156
- var loadingTask = (0, _api.getDocument)(typedArrayPdf);
157
- var progressReportedCapability = (0, _util.createPromiseCapability)();
122
+ typedArrayPdfPromise.then(typedArrayPdf => {
123
+ expect(typedArrayPdf.length).toEqual(basicApiFileLength);
124
+ const loadingTask = (0, _api.getDocument)(typedArrayPdf);
125
+ const progressReportedCapability = (0, _util.createPromiseCapability)();
158
126
 
159
- loadingTask.onProgress = function (data) {
160
- progressReportedCapability.resolve(data);
161
- };
127
+ loadingTask.onProgress = function (data) {
128
+ progressReportedCapability.resolve(data);
129
+ };
162
130
 
163
- Promise.all([loadingTask.promise, progressReportedCapability.promise]).then(function (data) {
164
- expect(data[0] instanceof _api.PDFDocumentProxy).toEqual(true);
165
- expect(data[1].loaded / data[1].total).toEqual(1);
166
- loadingTask.destroy().then(done);
131
+ return Promise.all([loadingTask.promise, progressReportedCapability.promise]).then(function (data) {
132
+ expect(data[0] instanceof _api.PDFDocumentProxy).toEqual(true);
133
+ expect(data[1].loaded / data[1].total).toEqual(1);
134
+ loadingTask.destroy().then(done);
135
+ });
167
136
  }).catch(done.fail);
168
137
  });
169
- it('creates pdf doc from invalid PDF file', function (done) {
170
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1020226.pdf'));
138
+ it("creates pdf doc from invalid PDF file", function (done) {
139
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("bug1020226.pdf"));
171
140
  loadingTask.promise.then(function () {
172
- done.fail('shall fail loading');
173
- }).catch(function (error) {
174
- expect(error instanceof _util.InvalidPDFException).toEqual(true);
141
+ done.fail("shall fail loading");
142
+ }).catch(function (reason) {
143
+ expect(reason instanceof _util.InvalidPDFException).toEqual(true);
144
+ expect(reason.message).toEqual("Invalid PDF structure.");
175
145
  loadingTask.destroy().then(done);
176
146
  });
177
147
  });
178
- it('creates pdf doc from non-existent URL', function (done) {
179
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('non-existent.pdf'));
148
+ it("creates pdf doc from non-existent URL", function (done) {
149
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("non-existent.pdf"));
180
150
  loadingTask.promise.then(function (error) {
181
- done.fail('shall fail loading');
151
+ done.fail("shall fail loading");
182
152
  }).catch(function (error) {
183
153
  expect(error instanceof _util.MissingPDFException).toEqual(true);
184
154
  loadingTask.destroy().then(done);
185
155
  });
186
156
  });
187
- it('creates pdf doc from PDF file protected with user and owner password', function (done) {
188
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('pr6531_1.pdf'));
157
+ it("creates pdf doc from PDF file protected with user and owner password", function (done) {
158
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("pr6531_1.pdf"));
189
159
  var passwordNeededCapability = (0, _util.createPromiseCapability)();
190
160
  var passwordIncorrectCapability = (0, _util.createPromiseCapability)();
191
161
 
192
162
  loadingTask.onPassword = function (updatePassword, reason) {
193
163
  if (reason === _util.PasswordResponses.NEED_PASSWORD && !passwordNeededCapability.settled) {
194
164
  passwordNeededCapability.resolve();
195
- updatePassword('qwerty');
165
+ updatePassword("qwerty");
196
166
  return;
197
167
  }
198
168
 
199
169
  if (reason === _util.PasswordResponses.INCORRECT_PASSWORD && !passwordIncorrectCapability.settled) {
200
170
  passwordIncorrectCapability.resolve();
201
- updatePassword('asdfasdf');
171
+ updatePassword("asdfasdf");
202
172
  return;
203
173
  }
204
174
 
@@ -211,32 +181,32 @@ describe('api', function () {
211
181
  loadingTask.destroy().then(done);
212
182
  }).catch(done.fail);
213
183
  });
214
- it('creates pdf doc from PDF file protected with only a user password', function (done) {
215
- var filename = 'pr6531_2.pdf';
184
+ it("creates pdf doc from PDF file protected with only a user password", function (done) {
185
+ var filename = "pr6531_2.pdf";
216
186
  var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
217
- password: ''
187
+ password: ""
218
188
  }));
219
189
  var result1 = passwordNeededLoadingTask.promise.then(function () {
220
- done.fail('shall fail with no password');
221
- return Promise.reject(new Error('loadingTask should be rejected'));
190
+ done.fail("shall fail with no password");
191
+ return Promise.reject(new Error("loadingTask should be rejected"));
222
192
  }, function (data) {
223
193
  expect(data instanceof _util.PasswordException).toEqual(true);
224
194
  expect(data.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
225
195
  return passwordNeededLoadingTask.destroy();
226
196
  });
227
197
  var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
228
- password: 'qwerty'
198
+ password: "qwerty"
229
199
  }));
230
200
  var result2 = passwordIncorrectLoadingTask.promise.then(function () {
231
- done.fail('shall fail with wrong password');
232
- return Promise.reject(new Error('loadingTask should be rejected'));
201
+ done.fail("shall fail with wrong password");
202
+ return Promise.reject(new Error("loadingTask should be rejected"));
233
203
  }, function (data) {
234
204
  expect(data instanceof _util.PasswordException).toEqual(true);
235
205
  expect(data.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
236
206
  return passwordIncorrectLoadingTask.destroy();
237
207
  });
238
208
  var passwordAcceptedLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
239
- password: 'asdfasdf'
209
+ password: "asdfasdf"
240
210
  }));
241
211
  var result3 = passwordAcceptedLoadingTask.promise.then(function (data) {
242
212
  expect(data instanceof _api.PDFDocumentProxy).toEqual(true);
@@ -246,13 +216,13 @@ describe('api', function () {
246
216
  done();
247
217
  }).catch(done.fail);
248
218
  });
249
- it('creates pdf doc from password protected PDF file and aborts/throws ' + 'in the onPassword callback (issue 7806)', function (done) {
250
- var filename = 'issue3371.pdf';
219
+ it("creates pdf doc from password protected PDF file and aborts/throws " + "in the onPassword callback (issue 7806)", function (done) {
220
+ var filename = "issue3371.pdf";
251
221
  var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
252
222
  var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
253
- password: 'qwerty'
223
+ password: "qwerty"
254
224
  }));
255
- var passwordNeededDestroyed;
225
+ let passwordNeededDestroyed;
256
226
 
257
227
  passwordNeededLoadingTask.onPassword = function (callback, reason) {
258
228
  if (reason === _util.PasswordResponses.NEED_PASSWORD) {
@@ -264,8 +234,8 @@ describe('api', function () {
264
234
  };
265
235
 
266
236
  var result1 = passwordNeededLoadingTask.promise.then(function () {
267
- done.fail('shall fail since the loadingTask should be destroyed');
268
- return Promise.reject(new Error('loadingTask should be rejected'));
237
+ done.fail("shall fail since the loadingTask should be destroyed");
238
+ return Promise.reject(new Error("loadingTask should be rejected"));
269
239
  }, function (reason) {
270
240
  expect(reason instanceof _util.PasswordException).toEqual(true);
271
241
  expect(reason.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
@@ -274,15 +244,15 @@ describe('api', function () {
274
244
 
275
245
  passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
276
246
  if (reason === _util.PasswordResponses.INCORRECT_PASSWORD) {
277
- throw new Error('Incorrect password');
247
+ throw new Error("Incorrect password");
278
248
  }
279
249
 
280
250
  expect(false).toEqual(true);
281
251
  };
282
252
 
283
253
  var result2 = passwordIncorrectLoadingTask.promise.then(function () {
284
- done.fail('shall fail since the onPassword callback should throw');
285
- return Promise.reject(new Error('loadingTask should be rejected'));
254
+ done.fail("shall fail since the onPassword callback should throw");
255
+ return Promise.reject(new Error("loadingTask should be rejected"));
286
256
  }, function (reason) {
287
257
  expect(reason instanceof _util.PasswordException).toEqual(true);
288
258
  expect(reason.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
@@ -292,18 +262,28 @@ describe('api', function () {
292
262
  done();
293
263
  }).catch(done.fail);
294
264
  });
265
+ it("creates pdf doc from empty typed array", function (done) {
266
+ const loadingTask = (0, _api.getDocument)(new Uint8Array(0));
267
+ loadingTask.promise.then(function () {
268
+ done.fail("shall not open empty file");
269
+ }, function (reason) {
270
+ expect(reason instanceof _util.InvalidPDFException);
271
+ expect(reason.message).toEqual("The PDF file is empty, i.e. its size is zero bytes.");
272
+ loadingTask.destroy().then(done);
273
+ });
274
+ });
295
275
  });
296
- describe('PDFWorker', function () {
297
- it('worker created or destroyed', function (done) {
298
- if ((0, _is_node.default)()) {
299
- pending('Worker is not supported in Node.js.');
276
+ describe("PDFWorker", function () {
277
+ it("worker created or destroyed", function (done) {
278
+ if (_is_node.isNodeJS) {
279
+ pending("Worker is not supported in Node.js.");
300
280
  }
301
281
 
302
282
  var worker = new _api.PDFWorker({
303
- name: 'test1'
283
+ name: "test1"
304
284
  });
305
285
  worker.promise.then(function () {
306
- expect(worker.name).toEqual('test1');
286
+ expect(worker.name).toEqual("test1");
307
287
  expect(!!worker.port).toEqual(true);
308
288
  expect(worker.destroyed).toEqual(false);
309
289
  expect(!!worker._webWorker).toEqual(true);
@@ -314,9 +294,9 @@ describe('api', function () {
314
294
  done();
315
295
  }).catch(done.fail);
316
296
  });
317
- it('worker created or destroyed by getDocument', function (done) {
318
- if ((0, _is_node.default)()) {
319
- pending('Worker is not supported in Node.js.');
297
+ it("worker created or destroyed by getDocument", function (done) {
298
+ if (_is_node.isNodeJS) {
299
+ pending("Worker is not supported in Node.js.");
320
300
  }
321
301
 
322
302
  var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
@@ -335,16 +315,16 @@ describe('api', function () {
335
315
  done();
336
316
  }).catch(done.fail);
337
317
  });
338
- it('worker created and can be used in getDocument', function (done) {
339
- if ((0, _is_node.default)()) {
340
- pending('Worker is not supported in Node.js.');
318
+ it("worker created and can be used in getDocument", function (done) {
319
+ if (_is_node.isNodeJS) {
320
+ pending("Worker is not supported in Node.js.");
341
321
  }
342
322
 
343
323
  var worker = new _api.PDFWorker({
344
- name: 'test1'
324
+ name: "test1"
345
325
  });
346
326
  var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
347
- worker: worker
327
+ worker
348
328
  }));
349
329
  loadingTask.promise.then(function () {
350
330
  var docWorker = loadingTask._worker;
@@ -361,19 +341,19 @@ describe('api', function () {
361
341
  done();
362
342
  }).catch(done.fail);
363
343
  });
364
- it('creates more than one worker', function (done) {
365
- if ((0, _is_node.default)()) {
366
- pending('Worker is not supported in Node.js.');
344
+ it("creates more than one worker", function (done) {
345
+ if (_is_node.isNodeJS) {
346
+ pending("Worker is not supported in Node.js.");
367
347
  }
368
348
 
369
349
  var worker1 = new _api.PDFWorker({
370
- name: 'test1'
350
+ name: "test1"
371
351
  });
372
352
  var worker2 = new _api.PDFWorker({
373
- name: 'test2'
353
+ name: "test2"
374
354
  });
375
355
  var worker3 = new _api.PDFWorker({
376
- name: 'test3'
356
+ name: "test3"
377
357
  });
378
358
  var ready = Promise.all([worker1.promise, worker2.promise, worker3.promise]);
379
359
  ready.then(function () {
@@ -384,62 +364,59 @@ describe('api', function () {
384
364
  done();
385
365
  }).catch(done.fail);
386
366
  });
387
- it('gets current workerSrc', function () {
388
- if ((0, _is_node.default)()) {
389
- pending('Worker is not supported in Node.js.');
367
+ it("gets current workerSrc", function () {
368
+ if (_is_node.isNodeJS) {
369
+ pending("Worker is not supported in Node.js.");
390
370
  }
391
371
 
392
- var workerSrc = _api.PDFWorker.getWorkerSrc();
372
+ const workerSrc = _api.PDFWorker.getWorkerSrc();
393
373
 
394
- expect(_typeof(workerSrc)).toEqual('string');
374
+ expect(typeof workerSrc).toEqual("string");
395
375
  expect(workerSrc).toEqual(_worker_options.GlobalWorkerOptions.workerSrc);
396
376
  });
397
377
  });
398
- describe('PDFDocument', function () {
399
- var loadingTask;
400
- var doc;
378
+ describe("PDFDocument", function () {
379
+ let pdfLoadingTask, pdfDocument;
401
380
  beforeAll(function (done) {
402
- loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
403
- loadingTask.promise.then(function (data) {
404
- doc = data;
381
+ pdfLoadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
382
+ pdfLoadingTask.promise.then(function (data) {
383
+ pdfDocument = data;
405
384
  done();
406
385
  });
407
386
  });
408
387
  afterAll(function (done) {
409
- loadingTask.destroy().then(done);
388
+ pdfLoadingTask.destroy().then(done);
410
389
  });
411
- it('gets number of pages', function () {
412
- expect(doc.numPages).toEqual(3);
390
+ it("gets number of pages", function () {
391
+ expect(pdfDocument.numPages).toEqual(3);
413
392
  });
414
- it('gets fingerprint', function () {
415
- var fingerprint = doc.fingerprint;
416
- expect(_typeof(fingerprint)).toEqual('string');
417
- expect(fingerprint.length > 0).toEqual(true);
393
+ it("gets fingerprint", function () {
394
+ expect(pdfDocument.fingerprint).toEqual("ea8b35919d6279a369e835bde778611b");
418
395
  });
419
- it('gets page', function (done) {
420
- var promise = doc.getPage(1);
396
+ it("gets page", function (done) {
397
+ var promise = pdfDocument.getPage(1);
421
398
  promise.then(function (data) {
422
399
  expect(data instanceof _api.PDFPageProxy).toEqual(true);
423
- expect(data.pageIndex).toEqual(0);
400
+ expect(data.pageNumber).toEqual(1);
424
401
  done();
425
402
  }).catch(done.fail);
426
403
  });
427
- it('gets non-existent page', function (done) {
428
- var outOfRangePromise = doc.getPage(100);
429
- var nonIntegerPromise = doc.getPage(2.5);
430
- var nonNumberPromise = doc.getPage('1');
404
+ it("gets non-existent page", function (done) {
405
+ var outOfRangePromise = pdfDocument.getPage(100);
406
+ var nonIntegerPromise = pdfDocument.getPage(2.5);
407
+ var nonNumberPromise = pdfDocument.getPage("1");
431
408
  outOfRangePromise = outOfRangePromise.then(function () {
432
- throw new Error('shall fail for out-of-range pageNumber parameter');
409
+ throw new Error("shall fail for out-of-range pageNumber parameter");
433
410
  }, function (reason) {
434
411
  expect(reason instanceof Error).toEqual(true);
435
412
  });
436
413
  nonIntegerPromise = nonIntegerPromise.then(function () {
437
- throw new Error('shall fail for non-integer pageNumber parameter');
414
+ throw new Error("shall fail for non-integer pageNumber parameter");
438
415
  }, function (reason) {
439
416
  expect(reason instanceof Error).toEqual(true);
440
417
  });
441
418
  nonNumberPromise = nonNumberPromise.then(function () {
442
- throw new Error('shall fail for non-number pageNumber parameter');
419
+ throw new Error("shall fail for non-number pageNumber parameter");
443
420
  }, function (reason) {
444
421
  expect(reason instanceof Error).toEqual(true);
445
422
  });
@@ -447,275 +424,335 @@ describe('api', function () {
447
424
  done();
448
425
  }).catch(done.fail);
449
426
  });
450
- it('gets page index', function (done) {
427
+ it("gets page, from /Pages tree with circular reference", function (done) {
428
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("Pages-tree-refs.pdf"));
429
+ const page1 = loadingTask.promise.then(function (pdfDoc) {
430
+ return pdfDoc.getPage(1).then(function (pdfPage) {
431
+ expect(pdfPage instanceof _api.PDFPageProxy).toEqual(true);
432
+ expect(pdfPage.ref).toEqual({
433
+ num: 6,
434
+ gen: 0
435
+ });
436
+ }, function (reason) {
437
+ throw new Error("shall not fail for valid page");
438
+ });
439
+ });
440
+ const page2 = loadingTask.promise.then(function (pdfDoc) {
441
+ return pdfDoc.getPage(2).then(function (pdfPage) {
442
+ throw new Error("shall fail for invalid page");
443
+ }, function (reason) {
444
+ expect(reason instanceof Error).toEqual(true);
445
+ expect(reason.message).toEqual("Pages tree contains circular reference.");
446
+ });
447
+ });
448
+ Promise.all([page1, page2]).then(function () {
449
+ loadingTask.destroy().then(done);
450
+ }, done.fail);
451
+ });
452
+ it("gets page index", function (done) {
451
453
  var ref = {
452
454
  num: 17,
453
455
  gen: 0
454
456
  };
455
- var promise = doc.getPageIndex(ref);
457
+ var promise = pdfDocument.getPageIndex(ref);
456
458
  promise.then(function (pageIndex) {
457
459
  expect(pageIndex).toEqual(1);
458
460
  done();
459
461
  }).catch(done.fail);
460
462
  });
461
- it('gets invalid page index', function (done) {
463
+ it("gets invalid page index", function (done) {
462
464
  var ref = {
463
465
  num: 3,
464
466
  gen: 0
465
467
  };
466
- var promise = doc.getPageIndex(ref);
468
+ var promise = pdfDocument.getPageIndex(ref);
467
469
  promise.then(function () {
468
- done.fail('shall fail for invalid page reference.');
470
+ done.fail("shall fail for invalid page reference.");
469
471
  }).catch(function (reason) {
470
472
  expect(reason instanceof Error).toEqual(true);
471
473
  done();
472
474
  });
473
475
  });
474
- it('gets destinations, from /Dests dictionary', function (done) {
475
- var promise = doc.getDestinations();
476
+ it("gets destinations, from /Dests dictionary", function (done) {
477
+ var promise = pdfDocument.getDestinations();
476
478
  promise.then(function (data) {
477
479
  expect(data).toEqual({
478
480
  chapter1: [{
479
481
  gen: 0,
480
482
  num: 17
481
483
  }, {
482
- name: 'XYZ'
484
+ name: "XYZ"
483
485
  }, 0, 841.89, null]
484
486
  });
485
487
  done();
486
488
  }).catch(done.fail);
487
489
  });
488
- it('gets a destination, from /Dests dictionary', function (done) {
489
- var promise = doc.getDestination('chapter1');
490
+ it("gets a destination, from /Dests dictionary", function (done) {
491
+ var promise = pdfDocument.getDestination("chapter1");
490
492
  promise.then(function (data) {
491
493
  expect(data).toEqual([{
492
494
  gen: 0,
493
495
  num: 17
494
496
  }, {
495
- name: 'XYZ'
497
+ name: "XYZ"
496
498
  }, 0, 841.89, null]);
497
499
  done();
498
500
  }).catch(done.fail);
499
501
  });
500
- it('gets a non-existent destination, from /Dests dictionary', function (done) {
501
- var promise = doc.getDestination('non-existent-named-destination');
502
+ it("gets a non-existent destination, from /Dests dictionary", function (done) {
503
+ var promise = pdfDocument.getDestination("non-existent-named-destination");
502
504
  promise.then(function (data) {
503
505
  expect(data).toEqual(null);
504
506
  done();
505
507
  }).catch(done.fail);
506
508
  });
507
- it('gets destinations, from /Names (NameTree) dictionary', function (done) {
508
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
509
- var promise = loadingTask.promise.then(function (pdfDocument) {
510
- return pdfDocument.getDestinations();
509
+ it("gets destinations, from /Names (NameTree) dictionary", function (done) {
510
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue6204.pdf"));
511
+ var promise = loadingTask.promise.then(function (pdfDoc) {
512
+ return pdfDoc.getDestinations();
511
513
  });
512
514
  promise.then(function (destinations) {
513
515
  expect(destinations).toEqual({
514
- 'Page.1': [{
516
+ "Page.1": [{
515
517
  num: 1,
516
518
  gen: 0
517
519
  }, {
518
- name: 'XYZ'
520
+ name: "XYZ"
519
521
  }, 0, 375, null],
520
- 'Page.2': [{
522
+ "Page.2": [{
521
523
  num: 6,
522
524
  gen: 0
523
525
  }, {
524
- name: 'XYZ'
526
+ name: "XYZ"
525
527
  }, 0, 375, null]
526
528
  });
527
529
  loadingTask.destroy().then(done);
528
530
  }).catch(done.fail);
529
531
  });
530
- it('gets a destination, from /Names (NameTree) dictionary', function (done) {
531
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
532
- var promise = loadingTask.promise.then(function (pdfDocument) {
533
- return pdfDocument.getDestination('Page.1');
532
+ it("gets a destination, from /Names (NameTree) dictionary", function (done) {
533
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue6204.pdf"));
534
+ var promise = loadingTask.promise.then(function (pdfDoc) {
535
+ return pdfDoc.getDestination("Page.1");
534
536
  });
535
537
  promise.then(function (destination) {
536
538
  expect(destination).toEqual([{
537
539
  num: 1,
538
540
  gen: 0
539
541
  }, {
540
- name: 'XYZ'
542
+ name: "XYZ"
541
543
  }, 0, 375, null]);
542
544
  loadingTask.destroy().then(done);
543
545
  }).catch(done.fail);
544
546
  });
545
- it('gets a non-existent destination, from /Names (NameTree) dictionary', function (done) {
546
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
547
- var promise = loadingTask.promise.then(function (pdfDocument) {
548
- return pdfDocument.getDestination('non-existent-named-destination');
547
+ it("gets a non-existent destination, from /Names (NameTree) dictionary", function (done) {
548
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue6204.pdf"));
549
+ var promise = loadingTask.promise.then(function (pdfDoc) {
550
+ return pdfDoc.getDestination("non-existent-named-destination");
549
551
  });
550
552
  promise.then(function (destination) {
551
553
  expect(destination).toEqual(null);
552
554
  loadingTask.destroy().then(done);
553
555
  }).catch(done.fail);
554
556
  });
555
- it('gets non-string destination', function (done) {
556
- var numberPromise = doc.getDestination(4.3);
557
- var booleanPromise = doc.getDestination(true);
558
- var arrayPromise = doc.getDestination([{
557
+ it("gets non-string destination", function (done) {
558
+ let numberPromise = pdfDocument.getDestination(4.3);
559
+ let booleanPromise = pdfDocument.getDestination(true);
560
+ let arrayPromise = pdfDocument.getDestination([{
559
561
  num: 17,
560
562
  gen: 0
561
563
  }, {
562
- name: 'XYZ'
564
+ name: "XYZ"
563
565
  }, 0, 841.89, null]);
564
566
  numberPromise = numberPromise.then(function () {
565
- throw new Error('shall fail for non-string destination.');
567
+ throw new Error("shall fail for non-string destination.");
566
568
  }, function (reason) {
567
569
  expect(reason instanceof Error).toEqual(true);
568
570
  });
569
571
  booleanPromise = booleanPromise.then(function () {
570
- throw new Error('shall fail for non-string destination.');
572
+ throw new Error("shall fail for non-string destination.");
571
573
  }, function (reason) {
572
574
  expect(reason instanceof Error).toEqual(true);
573
575
  });
574
576
  arrayPromise = arrayPromise.then(function () {
575
- throw new Error('shall fail for non-string destination.');
577
+ throw new Error("shall fail for non-string destination.");
576
578
  }, function (reason) {
577
579
  expect(reason instanceof Error).toEqual(true);
578
580
  });
579
581
  Promise.all([numberPromise, booleanPromise, arrayPromise]).then(done, done.fail);
580
582
  });
581
- it('gets non-existent page labels', function (done) {
582
- var promise = doc.getPageLabels();
583
+ it("gets non-existent page labels", function (done) {
584
+ var promise = pdfDocument.getPageLabels();
583
585
  promise.then(function (data) {
584
586
  expect(data).toEqual(null);
585
587
  done();
586
588
  }).catch(done.fail);
587
589
  });
588
- it('gets page labels', function (done) {
589
- var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug793632.pdf'));
590
+ it("gets page labels", function (done) {
591
+ var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("bug793632.pdf"));
590
592
  var promise0 = loadingTask0.promise.then(function (pdfDoc) {
591
593
  return pdfDoc.getPageLabels();
592
594
  });
593
- var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue1453.pdf'));
595
+ var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue1453.pdf"));
594
596
  var promise1 = loadingTask1.promise.then(function (pdfDoc) {
595
597
  return pdfDoc.getPageLabels();
596
598
  });
597
- var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('rotation.pdf'));
599
+ var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("rotation.pdf"));
598
600
  var promise2 = loadingTask2.promise.then(function (pdfDoc) {
599
601
  return pdfDoc.getPageLabels();
600
602
  });
601
- var loadingTask3 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bad-PageLabels.pdf'));
603
+ var loadingTask3 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("bad-PageLabels.pdf"));
602
604
  var promise3 = loadingTask3.promise.then(function (pdfDoc) {
603
605
  return pdfDoc.getPageLabels();
604
606
  });
605
607
  Promise.all([promise0, promise1, promise2, promise3]).then(function (pageLabels) {
606
- expect(pageLabels[0]).toEqual(['i', 'ii', 'iii', '1']);
607
- expect(pageLabels[1]).toEqual(['Front Page1']);
608
- expect(pageLabels[2]).toEqual(['1', '2']);
609
- expect(pageLabels[3]).toEqual(['X3']);
608
+ expect(pageLabels[0]).toEqual(["i", "ii", "iii", "1"]);
609
+ expect(pageLabels[1]).toEqual(["Front Page1"]);
610
+ expect(pageLabels[2]).toEqual(["1", "2"]);
611
+ expect(pageLabels[3]).toEqual(["X3"]);
610
612
  Promise.all([loadingTask0.destroy(), loadingTask1.destroy(), loadingTask2.destroy(), loadingTask3.destroy()]).then(done);
611
613
  }).catch(done.fail);
612
614
  });
613
- it('gets default page mode', function (done) {
614
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
615
- loadingTask.promise.then(function (pdfDocument) {
616
- return pdfDocument.getPageMode();
615
+ it("gets default page layout", function (done) {
616
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
617
+ loadingTask.promise.then(function (pdfDoc) {
618
+ return pdfDoc.getPageLayout();
617
619
  }).then(function (mode) {
618
- expect(mode).toEqual('UseNone');
620
+ expect(mode).toEqual("");
621
+ loadingTask.destroy().then(done);
622
+ }).catch(done.fail);
623
+ });
624
+ it("gets non-default page layout", function (done) {
625
+ pdfDocument.getPageLayout().then(function (mode) {
626
+ expect(mode).toEqual("SinglePage");
627
+ done();
628
+ }).catch(done.fail);
629
+ });
630
+ it("gets default page mode", function (done) {
631
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
632
+ loadingTask.promise.then(function (pdfDoc) {
633
+ return pdfDoc.getPageMode();
634
+ }).then(function (mode) {
635
+ expect(mode).toEqual("UseNone");
636
+ loadingTask.destroy().then(done);
637
+ }).catch(done.fail);
638
+ });
639
+ it("gets non-default page mode", function (done) {
640
+ pdfDocument.getPageMode().then(function (mode) {
641
+ expect(mode).toEqual("UseOutlines");
642
+ done();
643
+ }).catch(done.fail);
644
+ });
645
+ it("gets default viewer preferences", function (done) {
646
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
647
+ loadingTask.promise.then(function (pdfDoc) {
648
+ return pdfDoc.getViewerPreferences();
649
+ }).then(function (prefs) {
650
+ expect(prefs).toEqual(null);
619
651
  loadingTask.destroy().then(done);
620
652
  }).catch(done.fail);
621
653
  });
622
- it('gets non-default page mode', function (done) {
623
- doc.getPageMode().then(function (mode) {
624
- expect(mode).toEqual('UseOutlines');
654
+ it("gets non-default viewer preferences", function (done) {
655
+ pdfDocument.getViewerPreferences().then(function (prefs) {
656
+ expect(prefs).toEqual({
657
+ Direction: "L2R"
658
+ });
625
659
  done();
626
660
  }).catch(done.fail);
627
661
  });
628
- it('gets default open action destination', function (done) {
629
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
630
- loadingTask.promise.then(function (pdfDocument) {
631
- return pdfDocument.getOpenActionDestination();
632
- }).then(function (dest) {
633
- expect(dest).toEqual(null);
662
+ it("gets default open action", function (done) {
663
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
664
+ loadingTask.promise.then(function (pdfDoc) {
665
+ return pdfDoc.getOpenAction();
666
+ }).then(function (openAction) {
667
+ expect(openAction).toEqual(null);
634
668
  loadingTask.destroy().then(done);
635
669
  }).catch(done.fail);
636
670
  });
637
- it('gets non-default open action destination', function (done) {
638
- doc.getOpenActionDestination().then(function (dest) {
639
- expect(dest).toEqual([{
671
+ it("gets non-default open action (with destination)", function (done) {
672
+ pdfDocument.getOpenAction().then(function (openAction) {
673
+ expect(openAction.dest).toEqual([{
640
674
  num: 15,
641
675
  gen: 0
642
676
  }, {
643
- name: 'FitH'
677
+ name: "FitH"
644
678
  }, null]);
679
+ expect(openAction.action).toBeUndefined();
645
680
  done();
646
681
  }).catch(done.fail);
647
682
  });
648
- it('gets non-existent attachments', function (done) {
649
- var promise = doc.getAttachments();
683
+ it("gets non-default open action (with Print action)", function (done) {
684
+ const loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("bug1001080.pdf"));
685
+ const loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue11442_reduced.pdf"));
686
+ const promise1 = loadingTask1.promise.then(function (pdfDoc) {
687
+ return pdfDoc.getOpenAction();
688
+ }).then(function (openAction) {
689
+ expect(openAction.dest).toBeUndefined();
690
+ expect(openAction.action).toEqual("Print");
691
+ return loadingTask1.destroy();
692
+ });
693
+ const promise2 = loadingTask2.promise.then(function (pdfDoc) {
694
+ return pdfDoc.getOpenAction();
695
+ }).then(function (openAction) {
696
+ expect(openAction.dest).toBeUndefined();
697
+ expect(openAction.action).toEqual("Print");
698
+ return loadingTask2.destroy();
699
+ });
700
+ Promise.all([promise1, promise2]).then(done, done.fail);
701
+ });
702
+ it("gets non-existent attachments", function (done) {
703
+ var promise = pdfDocument.getAttachments();
650
704
  promise.then(function (data) {
651
705
  expect(data).toEqual(null);
652
706
  done();
653
707
  }).catch(done.fail);
654
708
  });
655
- it('gets attachments', function (done) {
656
- if ((0, _is_node.default)()) {
657
- pending('TODO: Use a non-linked test-case.');
658
- }
659
-
660
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug766138.pdf'));
709
+ it("gets attachments", function (done) {
710
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("attachment.pdf"));
661
711
  var promise = loadingTask.promise.then(function (pdfDoc) {
662
712
  return pdfDoc.getAttachments();
663
713
  });
664
714
  promise.then(function (data) {
665
- var attachment = data['Press Quality.joboptions'];
666
- expect(attachment.filename).toEqual('Press Quality.joboptions');
667
- expect(attachment.content instanceof Uint8Array).toBeTruthy();
668
- expect(attachment.content.length).toEqual(30098);
715
+ var attachment = data["foo.txt"];
716
+ expect(attachment.filename).toEqual("foo.txt");
717
+ expect(attachment.content).toEqual(new Uint8Array([98, 97, 114, 32, 98, 97, 122, 32, 10]));
669
718
  loadingTask.destroy().then(done);
670
719
  }).catch(done.fail);
671
720
  });
672
- it('gets javascript', function (done) {
673
- var promise = doc.getJavaScript();
721
+ it("gets javascript", function (done) {
722
+ var promise = pdfDocument.getJavaScript();
674
723
  promise.then(function (data) {
675
724
  expect(data).toEqual(null);
676
725
  done();
677
726
  }).catch(done.fail);
678
727
  });
679
- var viewerPrintRegExp = /\bprint\s*\(/;
680
- it('gets javascript with printing instructions (Print action)', function (done) {
681
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1001080.pdf'));
682
- var promise = loadingTask.promise.then(function (doc) {
683
- return doc.getJavaScript();
684
- });
685
- promise.then(function (data) {
686
- expect(data).toEqual(['print({});']);
687
- expect(data[0]).toMatch(viewerPrintRegExp);
688
- loadingTask.destroy().then(done);
689
- }).catch(done.fail);
690
- });
691
- it('gets javascript with printing instructions (JS action)', function (done) {
692
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6106.pdf'));
693
- var promise = loadingTask.promise.then(function (doc) {
694
- return doc.getJavaScript();
728
+ it("gets javascript with printing instructions (JS action)", function (done) {
729
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue6106.pdf"));
730
+ var promise = loadingTask.promise.then(function (pdfDoc) {
731
+ return pdfDoc.getJavaScript();
695
732
  });
696
733
  promise.then(function (data) {
697
- expect(data).toEqual(['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']);
698
- expect(data[0]).toMatch(viewerPrintRegExp);
734
+ expect(data).toEqual(["this.print({bUI:true,bSilent:false,bShrinkToFit:true});"]);
735
+ expect(data[0]).toMatch(_ui_utils.AutoPrintRegExp);
699
736
  loadingTask.destroy().then(done);
700
737
  }).catch(done.fail);
701
738
  });
702
- it('gets non-existent outline', function (done) {
703
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
704
- var promise = loadingTask.promise.then(function (pdfDocument) {
705
- return pdfDocument.getOutline();
739
+ it("gets non-existent outline", function (done) {
740
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
741
+ var promise = loadingTask.promise.then(function (pdfDoc) {
742
+ return pdfDoc.getOutline();
706
743
  });
707
744
  promise.then(function (outline) {
708
745
  expect(outline).toEqual(null);
709
746
  loadingTask.destroy().then(done);
710
747
  }).catch(done.fail);
711
748
  });
712
- it('gets outline', function (done) {
713
- var promise = doc.getOutline();
749
+ it("gets outline", function (done) {
750
+ var promise = pdfDocument.getOutline();
714
751
  promise.then(function (outline) {
715
752
  expect(Array.isArray(outline)).toEqual(true);
716
753
  expect(outline.length).toEqual(2);
717
754
  var outlineItem = outline[1];
718
- expect(outlineItem.title).toEqual('Chapter 1');
755
+ expect(outlineItem.title).toEqual("Chapter 1");
719
756
  expect(Array.isArray(outlineItem.dest)).toEqual(true);
720
757
  expect(outlineItem.url).toEqual(null);
721
758
  expect(outlineItem.unsafeUrl).toBeUndefined();
@@ -724,21 +761,21 @@ describe('api', function () {
724
761
  expect(outlineItem.italic).toEqual(false);
725
762
  expect(outlineItem.color).toEqual(new Uint8ClampedArray([0, 64, 128]));
726
763
  expect(outlineItem.items.length).toEqual(1);
727
- expect(outlineItem.items[0].title).toEqual('Paragraph 1.1');
764
+ expect(outlineItem.items[0].title).toEqual("Paragraph 1.1");
728
765
  done();
729
766
  }).catch(done.fail);
730
767
  });
731
- it('gets outline containing a url', function (done) {
732
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue3214.pdf'));
733
- loadingTask.promise.then(function (pdfDocument) {
734
- pdfDocument.getOutline().then(function (outline) {
768
+ it("gets outline containing a url", function (done) {
769
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue3214.pdf"));
770
+ loadingTask.promise.then(function (pdfDoc) {
771
+ pdfDoc.getOutline().then(function (outline) {
735
772
  expect(Array.isArray(outline)).toEqual(true);
736
773
  expect(outline.length).toEqual(5);
737
774
  var outlineItemTwo = outline[2];
738
- expect(_typeof(outlineItemTwo.title)).toEqual('string');
775
+ expect(typeof outlineItemTwo.title).toEqual("string");
739
776
  expect(outlineItemTwo.dest).toEqual(null);
740
- expect(outlineItemTwo.url).toEqual('http://google.com/');
741
- expect(outlineItemTwo.unsafeUrl).toEqual('http://google.com');
777
+ expect(outlineItemTwo.url).toEqual("http://google.com/");
778
+ expect(outlineItemTwo.unsafeUrl).toEqual("http://google.com");
742
779
  expect(outlineItemTwo.newWindow).toBeUndefined();
743
780
  var outlineItemOne = outline[1];
744
781
  expect(outlineItemOne.bold).toEqual(false);
@@ -748,26 +785,26 @@ describe('api', function () {
748
785
  });
749
786
  }).catch(done.fail);
750
787
  });
751
- it('gets non-existent permissions', function (done) {
752
- doc.getPermissions().then(function (permissions) {
788
+ it("gets non-existent permissions", function (done) {
789
+ pdfDocument.getPermissions().then(function (permissions) {
753
790
  expect(permissions).toEqual(null);
754
791
  done();
755
792
  }).catch(done.fail);
756
793
  });
757
- it('gets permissions', function (done) {
758
- var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue9972-1.pdf'));
759
- var promise0 = loadingTask0.promise.then(function (pdfDocument) {
760
- return pdfDocument.getPermissions();
794
+ it("gets permissions", function (done) {
795
+ const loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue9972-1.pdf"));
796
+ const promise0 = loadingTask0.promise.then(function (pdfDoc) {
797
+ return pdfDoc.getPermissions();
761
798
  });
762
- var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue9972-2.pdf'));
763
- var promise1 = loadingTask1.promise.then(function (pdfDocument) {
764
- return pdfDocument.getPermissions();
799
+ const loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue9972-2.pdf"));
800
+ const promise1 = loadingTask1.promise.then(function (pdfDoc) {
801
+ return pdfDoc.getPermissions();
765
802
  });
766
- var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue9972-3.pdf'));
767
- var promise2 = loadingTask2.promise.then(function (pdfDocument) {
768
- return pdfDocument.getPermissions();
803
+ const loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue9972-3.pdf"));
804
+ const promise2 = loadingTask2.promise.then(function (pdfDoc) {
805
+ return pdfDoc.getPermissions();
769
806
  });
770
- var totalPermissionCount = Object.keys(_util.PermissionFlag).length;
807
+ const totalPermissionCount = Object.keys(_util.PermissionFlag).length;
771
808
  Promise.all([promise0, promise1, promise2]).then(function (permissions) {
772
809
  expect(permissions[0].length).toEqual(totalPermissionCount - 1);
773
810
  expect(permissions[0].includes(_util.PermissionFlag.MODIFY_CONTENTS)).toBeFalsy();
@@ -779,57 +816,80 @@ describe('api', function () {
779
816
  Promise.all([loadingTask0.destroy(), loadingTask1.destroy(), loadingTask2.destroy()]).then(done);
780
817
  }).catch(done.fail);
781
818
  });
782
- it('gets metadata', function (done) {
783
- var promise = doc.getMetadata();
784
- promise.then(function (_ref) {
785
- var info = _ref.info,
786
- metadata = _ref.metadata,
787
- contentDispositionFilename = _ref.contentDispositionFilename;
788
- expect(info['Title']).toEqual('Basic API Test');
789
- expect(info['Custom']).toEqual(undefined);
790
- expect(info['PDFFormatVersion']).toEqual('1.7');
791
- expect(info['IsLinearized']).toEqual(false);
792
- expect(info['IsAcroFormPresent']).toEqual(false);
793
- expect(info['IsXFAPresent']).toEqual(false);
819
+ it("gets metadata", function (done) {
820
+ var promise = pdfDocument.getMetadata();
821
+ promise.then(function ({
822
+ info,
823
+ metadata,
824
+ contentDispositionFilename
825
+ }) {
826
+ expect(info.Title).toEqual("Basic API Test");
827
+ expect(info.Custom).toEqual(undefined);
828
+ expect(info.PDFFormatVersion).toEqual("1.7");
829
+ expect(info.IsLinearized).toEqual(false);
830
+ expect(info.IsAcroFormPresent).toEqual(false);
831
+ expect(info.IsXFAPresent).toEqual(false);
832
+ expect(info.IsCollectionPresent).toEqual(false);
794
833
  expect(metadata instanceof _metadata.Metadata).toEqual(true);
795
- expect(metadata.get('dc:title')).toEqual('Basic API Test');
834
+ expect(metadata.get("dc:title")).toEqual("Basic API Test");
796
835
  expect(contentDispositionFilename).toEqual(null);
797
836
  done();
798
837
  }).catch(done.fail);
799
838
  });
800
- it('gets metadata, with custom info dict entries', function (done) {
801
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
802
- loadingTask.promise.then(function (pdfDocument) {
803
- return pdfDocument.getMetadata();
804
- }).then(function (_ref2) {
805
- var info = _ref2.info,
806
- metadata = _ref2.metadata,
807
- contentDispositionFilename = _ref2.contentDispositionFilename;
808
- expect(info['Creator']).toEqual('TeX');
809
- expect(info['Producer']).toEqual('pdfeTeX-1.21a');
810
- expect(info['CreationDate']).toEqual('D:20090401163925-07\'00\'');
811
- var custom = info['Custom'];
812
- expect(_typeof(custom) === 'object' && custom !== null).toEqual(true);
813
- expect(custom['PTEX.Fullbanner']).toEqual('This is pdfeTeX, ' + 'Version 3.141592-1.21a-2.2 (Web2C 7.5.4) kpathsea version 3.5.6');
814
- expect(info['PDFFormatVersion']).toEqual('1.4');
815
- expect(info['IsLinearized']).toEqual(false);
816
- expect(info['IsAcroFormPresent']).toEqual(false);
817
- expect(info['IsXFAPresent']).toEqual(false);
839
+ it("gets metadata, with custom info dict entries", function (done) {
840
+ var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
841
+ loadingTask.promise.then(function (pdfDoc) {
842
+ return pdfDoc.getMetadata();
843
+ }).then(function ({
844
+ info,
845
+ metadata,
846
+ contentDispositionFilename
847
+ }) {
848
+ expect(info.Creator).toEqual("TeX");
849
+ expect(info.Producer).toEqual("pdfeTeX-1.21a");
850
+ expect(info.CreationDate).toEqual("D:20090401163925-07'00'");
851
+ const custom = info.Custom;
852
+ expect(typeof custom === "object" && custom !== null).toEqual(true);
853
+ expect(custom["PTEX.Fullbanner"]).toEqual("This is pdfeTeX, " + "Version 3.141592-1.21a-2.2 (Web2C 7.5.4) kpathsea version 3.5.6");
854
+ expect(info.PDFFormatVersion).toEqual("1.4");
855
+ expect(info.IsLinearized).toEqual(false);
856
+ expect(info.IsAcroFormPresent).toEqual(false);
857
+ expect(info.IsXFAPresent).toEqual(false);
858
+ expect(info.IsCollectionPresent).toEqual(false);
818
859
  expect(metadata).toEqual(null);
819
860
  expect(contentDispositionFilename).toEqual(null);
820
861
  loadingTask.destroy().then(done);
821
862
  }).catch(done.fail);
822
863
  });
823
- it('gets data', function (done) {
824
- var promise = doc.getData();
864
+ it("gets metadata, with missing PDF header (bug 1606566)", function (done) {
865
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("bug1606566.pdf"));
866
+ loadingTask.promise.then(function (pdfDoc) {
867
+ return pdfDoc.getMetadata();
868
+ }).then(function ({
869
+ info,
870
+ metadata,
871
+ contentDispositionFilename
872
+ }) {
873
+ expect(info.PDFFormatVersion).toEqual(null);
874
+ expect(info.IsLinearized).toEqual(false);
875
+ expect(info.IsAcroFormPresent).toEqual(false);
876
+ expect(info.IsXFAPresent).toEqual(false);
877
+ expect(info.IsCollectionPresent).toEqual(false);
878
+ expect(metadata).toEqual(null);
879
+ expect(contentDispositionFilename).toEqual(null);
880
+ loadingTask.destroy().then(done);
881
+ }).catch(done.fail);
882
+ });
883
+ it("gets data", function (done) {
884
+ var promise = pdfDocument.getData();
825
885
  promise.then(function (data) {
826
886
  expect(data instanceof Uint8Array).toEqual(true);
827
887
  expect(data.length).toEqual(basicApiFileLength);
828
888
  done();
829
889
  }).catch(done.fail);
830
890
  });
831
- it('gets download info', function (done) {
832
- var promise = doc.getDownloadInfo();
891
+ it("gets download info", function (done) {
892
+ var promise = pdfDocument.getDownloadInfo();
833
893
  promise.then(function (data) {
834
894
  expect(data).toEqual({
835
895
  length: basicApiFileLength
@@ -837,48 +897,52 @@ describe('api', function () {
837
897
  done();
838
898
  }).catch(done.fail);
839
899
  });
840
- it('gets document stats', function (done) {
841
- var promise = doc.getStats();
900
+ it("gets document stats", function (done) {
901
+ var promise = pdfDocument.getStats();
842
902
  promise.then(function (stats) {
843
903
  expect(stats).toEqual({
844
- streamTypes: [],
845
- fontTypes: []
904
+ streamTypes: {},
905
+ fontTypes: {}
846
906
  });
847
907
  done();
848
908
  }).catch(done.fail);
849
909
  });
850
- it('checks that fingerprints are unique', function (done) {
851
- var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4436r.pdf'));
852
- var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4575.pdf'));
853
- var promises = [loadingTask1.promise, loadingTask2.promise];
854
- Promise.all(promises).then(function (data) {
855
- var fingerprint1 = data[0].fingerprint;
856
- expect(_typeof(fingerprint1)).toEqual('string');
857
- expect(fingerprint1.length > 0).toEqual(true);
858
- var fingerprint2 = data[1].fingerprint;
859
- expect(_typeof(fingerprint2)).toEqual('string');
860
- expect(fingerprint2.length > 0).toEqual(true);
910
+ it("cleans up document resources", function (done) {
911
+ const promise = pdfDocument.cleanup();
912
+ promise.then(function () {
913
+ expect(true).toEqual(true);
914
+ done();
915
+ }, done.fail);
916
+ });
917
+ it("checks that fingerprints are unique", function (done) {
918
+ const loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue4436r.pdf"));
919
+ const loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue4575.pdf"));
920
+ Promise.all([loadingTask1.promise, loadingTask2.promise]).then(function (data) {
921
+ const fingerprint1 = data[0].fingerprint;
922
+ const fingerprint2 = data[1].fingerprint;
861
923
  expect(fingerprint1).not.toEqual(fingerprint2);
924
+ expect(fingerprint1).toEqual("2f695a83d6e7553c24fc08b7ac69712d");
925
+ expect(fingerprint2).toEqual("04c7126b34a46b6d4d6e7a1eff7edcb6");
862
926
  Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]).then(done);
863
927
  }).catch(done.fail);
864
928
  });
865
- describe('Cross-origin', function () {
929
+ describe("Cross-origin", function () {
866
930
  var loadingTask;
867
931
 
868
932
  function _checkCanLoad(expectSuccess, filename, options) {
869
- if ((0, _is_node.default)()) {
870
- pending('Cannot simulate cross-origin requests in Node.js');
933
+ if (_is_node.isNodeJS) {
934
+ pending("Cannot simulate cross-origin requests in Node.js");
871
935
  }
872
936
 
873
937
  var params = (0, _test_utils.buildGetDocumentParams)(filename, options);
874
938
  var url = new URL(params.url);
875
939
 
876
- if (url.hostname === 'localhost') {
877
- url.hostname = '127.0.0.1';
878
- } else if (params.url.hostname === '127.0.0.1') {
879
- url.hostname = 'localhost';
940
+ if (url.hostname === "localhost") {
941
+ url.hostname = "127.0.0.1";
942
+ } else if (params.url.hostname === "127.0.0.1") {
943
+ url.hostname = "localhost";
880
944
  } else {
881
- pending('Can only run cross-origin test on localhost!');
945
+ pending("Can only run cross-origin test on localhost!");
882
946
  }
883
947
 
884
948
  params.url = url.href;
@@ -889,7 +953,7 @@ describe('api', function () {
889
953
  expect(expectSuccess).toEqual(true);
890
954
  }, function (error) {
891
955
  if (expectSuccess) {
892
- expect(error).toEqual('There should not be any error');
956
+ expect(error).toEqual("There should not be any error");
893
957
  }
894
958
 
895
959
  expect(expectSuccess).toEqual(false);
@@ -905,46 +969,45 @@ describe('api', function () {
905
969
  }
906
970
 
907
971
  afterEach(function (done) {
908
- if (loadingTask) {
972
+ if (loadingTask && !loadingTask.destroyed) {
909
973
  loadingTask.destroy().then(done);
910
974
  } else {
911
975
  done();
912
976
  }
913
977
  });
914
- it('server disallows cors', function (done) {
915
- testCannotLoad('basicapi.pdf').then(done);
978
+ it("server disallows cors", function (done) {
979
+ testCannotLoad("basicapi.pdf").then(done);
916
980
  });
917
- it('server allows cors without credentials, default withCredentials', function (done) {
918
- testCanLoad('basicapi.pdf?cors=withoutCredentials').then(done);
981
+ it("server allows cors without credentials, default withCredentials", function (done) {
982
+ testCanLoad("basicapi.pdf?cors=withoutCredentials").then(done);
919
983
  });
920
- it('server allows cors without credentials, and withCredentials=false', function (done) {
921
- testCanLoad('basicapi.pdf?cors=withoutCredentials', {
984
+ it("server allows cors without credentials, and withCredentials=false", function (done) {
985
+ testCanLoad("basicapi.pdf?cors=withoutCredentials", {
922
986
  withCredentials: false
923
987
  }).then(done);
924
988
  });
925
- it('server allows cors without credentials, but withCredentials=true', function (done) {
926
- testCannotLoad('basicapi.pdf?cors=withoutCredentials', {
989
+ it("server allows cors without credentials, but withCredentials=true", function (done) {
990
+ testCannotLoad("basicapi.pdf?cors=withoutCredentials", {
927
991
  withCredentials: true
928
992
  }).then(done);
929
993
  });
930
- it('server allows cors with credentials, and withCredentials=true', function (done) {
931
- testCanLoad('basicapi.pdf?cors=withCredentials', {
994
+ it("server allows cors with credentials, and withCredentials=true", function (done) {
995
+ testCanLoad("basicapi.pdf?cors=withCredentials", {
932
996
  withCredentials: true
933
997
  }).then(done);
934
998
  });
935
- it('server allows cors with credentials, and withCredentials=false', function (done) {
936
- testCanLoad('basicapi.pdf?cors=withCredentials', {
999
+ it("server allows cors with credentials, and withCredentials=false", function (done) {
1000
+ testCanLoad("basicapi.pdf?cors=withCredentials", {
937
1001
  withCredentials: false
938
1002
  }).then(done);
939
1003
  });
940
1004
  });
941
1005
  });
942
- describe('Page', function () {
943
- var loadingTask;
944
- var pdfDocument, page;
1006
+ describe("Page", function () {
1007
+ let pdfLoadingTask, pdfDocument, page;
945
1008
  beforeAll(function (done) {
946
- loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
947
- loadingTask.promise.then(function (doc) {
1009
+ pdfLoadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
1010
+ pdfLoadingTask.promise.then(function (doc) {
948
1011
  pdfDocument = doc;
949
1012
  pdfDocument.getPage(1).then(function (data) {
950
1013
  page = data;
@@ -953,27 +1016,48 @@ describe('api', function () {
953
1016
  }).catch(done.fail);
954
1017
  });
955
1018
  afterAll(function (done) {
956
- loadingTask.destroy().then(done);
1019
+ pdfLoadingTask.destroy().then(done);
957
1020
  });
958
- it('gets page number', function () {
1021
+ it("gets page number", function () {
959
1022
  expect(page.pageNumber).toEqual(1);
960
1023
  });
961
- it('gets rotate', function () {
1024
+ it("gets rotate", function () {
962
1025
  expect(page.rotate).toEqual(0);
963
1026
  });
964
- it('gets ref', function () {
1027
+ it("gets ref", function () {
965
1028
  expect(page.ref).toEqual({
966
1029
  num: 15,
967
1030
  gen: 0
968
1031
  });
969
1032
  });
970
- it('gets userUnit', function () {
1033
+ it("gets userUnit", function () {
971
1034
  expect(page.userUnit).toEqual(1.0);
972
1035
  });
973
- it('gets view', function () {
1036
+ it("gets view", function () {
974
1037
  expect(page.view).toEqual([0, 0, 595.28, 841.89]);
975
1038
  });
976
- it('gets viewport', function () {
1039
+ it("gets view, with empty/invalid bounding boxes", function (done) {
1040
+ const viewLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("boundingBox_invalid.pdf"));
1041
+ viewLoadingTask.promise.then(pdfDoc => {
1042
+ const numPages = pdfDoc.numPages;
1043
+ expect(numPages).toEqual(3);
1044
+ const viewPromises = [];
1045
+
1046
+ for (let i = 0; i < numPages; i++) {
1047
+ viewPromises[i] = pdfDoc.getPage(i + 1).then(pdfPage => {
1048
+ return pdfPage.view;
1049
+ });
1050
+ }
1051
+
1052
+ Promise.all(viewPromises).then(([page1, page2, page3]) => {
1053
+ expect(page1).toEqual([0, 0, 612, 792]);
1054
+ expect(page2).toEqual([0, 0, 800, 600]);
1055
+ expect(page3).toEqual([0, 0, 600, 800]);
1056
+ viewLoadingTask.destroy().then(done);
1057
+ });
1058
+ }).catch(done.fail);
1059
+ });
1060
+ it("gets viewport", function () {
977
1061
  var viewport = page.getViewport({
978
1062
  scale: 1.5,
979
1063
  rotation: 90
@@ -985,16 +1069,25 @@ describe('api', function () {
985
1069
  expect(viewport.width).toEqual(1262.835);
986
1070
  expect(viewport.height).toEqual(892.92);
987
1071
  });
988
- it('gets viewport respecting "dontFlip" argument', function () {
989
- var scale = 1;
990
- var rotation = 135;
991
- var viewport = page.getViewport({
992
- scale: scale,
993
- rotation: rotation
1072
+ it('gets viewport with "offsetX/offsetY" arguments', function () {
1073
+ const viewport = page.getViewport({
1074
+ scale: 1,
1075
+ rotation: 0,
1076
+ offsetX: 100,
1077
+ offsetY: -100
994
1078
  });
995
- var dontFlipViewport = page.getViewport({
996
- scale: scale,
997
- rotation: rotation,
1079
+ expect(viewport.transform).toEqual([1, 0, 0, -1, 100, 741.89]);
1080
+ });
1081
+ it('gets viewport respecting "dontFlip" argument', function () {
1082
+ const scale = 1,
1083
+ rotation = 0;
1084
+ const viewport = page.getViewport({
1085
+ scale,
1086
+ rotation
1087
+ });
1088
+ const dontFlipViewport = page.getViewport({
1089
+ scale,
1090
+ rotation,
998
1091
  dontFlip: true
999
1092
  });
1000
1093
  expect(dontFlipViewport).not.toEqual(viewport);
@@ -1004,17 +1097,25 @@ describe('api', function () {
1004
1097
  expect(viewport.transform).toEqual([1, 0, 0, -1, 0, 841.89]);
1005
1098
  expect(dontFlipViewport.transform).toEqual([1, 0, -0, 1, 0, 0]);
1006
1099
  });
1007
- it('gets annotations', function (done) {
1100
+ it("gets viewport with invalid rotation", function () {
1101
+ expect(function () {
1102
+ page.getViewport({
1103
+ scale: 1,
1104
+ rotation: 45
1105
+ });
1106
+ }).toThrow(new Error("PageViewport: Invalid rotation, must be a multiple of 90 degrees."));
1107
+ });
1108
+ it("gets annotations", function (done) {
1008
1109
  var defaultPromise = page.getAnnotations().then(function (data) {
1009
1110
  expect(data.length).toEqual(4);
1010
1111
  });
1011
1112
  var displayPromise = page.getAnnotations({
1012
- intent: 'display'
1113
+ intent: "display"
1013
1114
  }).then(function (data) {
1014
1115
  expect(data.length).toEqual(4);
1015
1116
  });
1016
1117
  var printPromise = page.getAnnotations({
1017
- intent: 'print'
1118
+ intent: "print"
1018
1119
  }).then(function (data) {
1019
1120
  expect(data.length).toEqual(4);
1020
1121
  });
@@ -1022,8 +1123,8 @@ describe('api', function () {
1022
1123
  done();
1023
1124
  }).catch(done.fail);
1024
1125
  });
1025
- it('gets annotations containing relative URLs (bug 766086)', function (done) {
1026
- var filename = 'bug766086.pdf';
1126
+ it("gets annotations containing relative URLs (bug 766086)", function (done) {
1127
+ var filename = "bug766086.pdf";
1027
1128
  var defaultLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
1028
1129
  var defaultPromise = defaultLoadingTask.promise.then(function (pdfDoc) {
1029
1130
  return pdfDoc.getPage(1).then(function (pdfPage) {
@@ -1031,7 +1132,7 @@ describe('api', function () {
1031
1132
  });
1032
1133
  });
1033
1134
  var docBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
1034
- docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf'
1135
+ docBaseUrl: "http://www.example.com/test/pdfs/qwerty.pdf"
1035
1136
  }));
1036
1137
  var docBaseUrlPromise = docBaseUrlLoadingTask.promise.then(function (pdfDoc) {
1037
1138
  return pdfDoc.getPage(1).then(function (pdfPage) {
@@ -1039,7 +1140,7 @@ describe('api', function () {
1039
1140
  });
1040
1141
  });
1041
1142
  var invalidDocBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, {
1042
- docBaseUrl: 'qwerty.pdf'
1143
+ docBaseUrl: "qwerty.pdf"
1043
1144
  }));
1044
1145
  var invalidDocBaseUrlPromise = invalidDocBaseUrlLoadingTask.promise.then(function (pdfDoc) {
1045
1146
  return pdfDoc.getPage(1).then(function (pdfPage) {
@@ -1051,15 +1152,15 @@ describe('api', function () {
1051
1152
  var docBaseUrlAnnotations = data[1];
1052
1153
  var invalidDocBaseUrlAnnotations = data[2];
1053
1154
  expect(defaultAnnotations[0].url).toBeUndefined();
1054
- expect(defaultAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
1055
- expect(docBaseUrlAnnotations[0].url).toEqual('http://www.example.com/0021/002156/215675E.pdf#15');
1056
- expect(docBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
1155
+ expect(defaultAnnotations[0].unsafeUrl).toEqual("../../0021/002156/215675E.pdf#15");
1156
+ expect(docBaseUrlAnnotations[0].url).toEqual("http://www.example.com/0021/002156/215675E.pdf#15");
1157
+ expect(docBaseUrlAnnotations[0].unsafeUrl).toEqual("../../0021/002156/215675E.pdf#15");
1057
1158
  expect(invalidDocBaseUrlAnnotations[0].url).toBeUndefined();
1058
- expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
1159
+ expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual("../../0021/002156/215675E.pdf#15");
1059
1160
  Promise.all([defaultLoadingTask.destroy(), docBaseUrlLoadingTask.destroy(), invalidDocBaseUrlLoadingTask.destroy()]).then(done);
1060
1161
  }).catch(done.fail);
1061
1162
  });
1062
- it('gets text content', function (done) {
1163
+ it("gets text content", function (done) {
1063
1164
  var defaultPromise = page.getTextContent();
1064
1165
  var parametersPromise = page.getTextContent({
1065
1166
  normalizeWhitespace: true,
@@ -1074,25 +1175,26 @@ describe('api', function () {
1074
1175
  done();
1075
1176
  }).catch(done.fail);
1076
1177
  });
1077
- it('gets text content, with correct properties (issue 8276)', function (done) {
1078
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue8276_reduced.pdf'));
1079
- loadingTask.promise.then(function (pdfDoc) {
1080
- pdfDoc.getPage(1).then(function (pdfPage) {
1081
- pdfPage.getTextContent().then(function (_ref3) {
1082
- var items = _ref3.items,
1083
- styles = _ref3.styles;
1178
+ it("gets text content, with correct properties (issue 8276)", function (done) {
1179
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue8276_reduced.pdf"));
1180
+ loadingTask.promise.then(pdfDoc => {
1181
+ pdfDoc.getPage(1).then(pdfPage => {
1182
+ pdfPage.getTextContent().then(({
1183
+ items,
1184
+ styles
1185
+ }) => {
1084
1186
  expect(items.length).toEqual(1);
1085
- expect(Object.keys(styles)).toEqual(['Times']);
1187
+ expect(Object.keys(styles)).toEqual(["Times"]);
1086
1188
  expect(items[0]).toEqual({
1087
- dir: 'ltr',
1088
- fontName: 'Times',
1189
+ dir: "ltr",
1190
+ fontName: "Times",
1089
1191
  height: 18,
1090
- str: 'Issue 8276',
1192
+ str: "Issue 8276",
1091
1193
  transform: [18, 0, 0, 18, 441.81, 708.4499999999999],
1092
1194
  width: 77.49
1093
1195
  });
1094
1196
  expect(styles.Times).toEqual({
1095
- fontFamily: 'serif',
1197
+ fontFamily: "serif",
1096
1198
  ascent: NaN,
1097
1199
  descent: NaN,
1098
1200
  vertical: false
@@ -1102,7 +1204,7 @@ describe('api', function () {
1102
1204
  });
1103
1205
  }).catch(done.fail);
1104
1206
  });
1105
- it('gets operator list', function (done) {
1207
+ it("gets operator list", function (done) {
1106
1208
  var promise = page.getOperatorList();
1107
1209
  promise.then(function (oplist) {
1108
1210
  expect(!!oplist.fnArray).toEqual(true);
@@ -1111,17 +1213,16 @@ describe('api', function () {
1111
1213
  done();
1112
1214
  }).catch(done.fail);
1113
1215
  });
1114
- it('gets operatorList with JPEG image (issue 4888)', function (done) {
1115
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('cmykjpeg.pdf'));
1116
- loadingTask.promise.then(function (pdfDoc) {
1117
- pdfDoc.getPage(1).then(function (pdfPage) {
1118
- pdfPage.getOperatorList().then(function (opList) {
1119
- var imgIndex = opList.fnArray.indexOf(_util.OPS.paintImageXObject);
1120
- var imgArgs = opList.argsArray[imgIndex];
1121
-
1122
- var _pdfPage$objs$get = pdfPage.objs.get(imgArgs[0]),
1123
- data = _pdfPage$objs$get.data;
1124
-
1216
+ it("gets operatorList with JPEG image (issue 4888)", function (done) {
1217
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("cmykjpeg.pdf"));
1218
+ loadingTask.promise.then(pdfDoc => {
1219
+ pdfDoc.getPage(1).then(pdfPage => {
1220
+ pdfPage.getOperatorList().then(opList => {
1221
+ const imgIndex = opList.fnArray.indexOf(_util.OPS.paintImageXObject);
1222
+ const imgArgs = opList.argsArray[imgIndex];
1223
+ const {
1224
+ data
1225
+ } = pdfPage.objs.get(imgArgs[0]);
1125
1226
  expect(data instanceof Uint8ClampedArray).toEqual(true);
1126
1227
  expect(data.length).toEqual(90000);
1127
1228
  loadingTask.destroy().then(done);
@@ -1129,13 +1230,42 @@ describe('api', function () {
1129
1230
  });
1130
1231
  }).catch(done.fail);
1131
1232
  });
1132
- it('gets document stats after parsing page', function (done) {
1233
+ it("gets operatorList, from corrupt PDF file (issue 8702), " + "with/without `stopAtErrors` set", function (done) {
1234
+ const loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue8702.pdf", {
1235
+ stopAtErrors: false
1236
+ }));
1237
+ const loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue8702.pdf", {
1238
+ stopAtErrors: true
1239
+ }));
1240
+ const result1 = loadingTask1.promise.then(pdfDoc => {
1241
+ return pdfDoc.getPage(1).then(pdfPage => {
1242
+ return pdfPage.getOperatorList().then(opList => {
1243
+ expect(opList.fnArray.length).toEqual(722);
1244
+ expect(opList.argsArray.length).toEqual(722);
1245
+ expect(opList.lastChunk).toEqual(true);
1246
+ return loadingTask1.destroy();
1247
+ });
1248
+ });
1249
+ });
1250
+ const result2 = loadingTask2.promise.then(pdfDoc => {
1251
+ return pdfDoc.getPage(1).then(pdfPage => {
1252
+ return pdfPage.getOperatorList().then(opList => {
1253
+ expect(opList.fnArray.length).toEqual(0);
1254
+ expect(opList.argsArray.length).toEqual(0);
1255
+ expect(opList.lastChunk).toEqual(true);
1256
+ return loadingTask2.destroy();
1257
+ });
1258
+ });
1259
+ });
1260
+ Promise.all([result1, result2]).then(done, done.fail);
1261
+ });
1262
+ it("gets document stats after parsing page", function (done) {
1133
1263
  var promise = page.getOperatorList().then(function () {
1134
1264
  return pdfDocument.getStats();
1135
1265
  });
1136
- var expectedStreamTypes = [];
1266
+ var expectedStreamTypes = {};
1137
1267
  expectedStreamTypes[_util.StreamType.FLATE] = true;
1138
- var expectedFontTypes = [];
1268
+ var expectedFontTypes = {};
1139
1269
  expectedFontTypes[_util.FontType.TYPE1] = true;
1140
1270
  expectedFontTypes[_util.FontType.CIDFONTTYPE2] = true;
1141
1271
  promise.then(function (stats) {
@@ -1146,76 +1276,68 @@ describe('api', function () {
1146
1276
  done();
1147
1277
  }).catch(done.fail);
1148
1278
  });
1149
- it('gets page stats after parsing page, without `pdfBug` set', function (done) {
1150
- page.getOperatorList().then(function (opList) {
1279
+ it("gets page stats after parsing page, without `pdfBug` set", function (done) {
1280
+ page.getOperatorList().then(opList => {
1151
1281
  return page.stats;
1152
- }).then(function (stats) {
1282
+ }).then(stats => {
1153
1283
  expect(stats).toEqual(null);
1154
1284
  done();
1155
1285
  }, done.fail);
1156
1286
  });
1157
- it('gets page stats after parsing page, with `pdfBug` set', function (done) {
1158
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
1287
+ it("gets page stats after parsing page, with `pdfBug` set", function (done) {
1288
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
1159
1289
  pdfBug: true
1160
1290
  }));
1161
- loadingTask.promise.then(function (pdfDoc) {
1162
- return pdfDoc.getPage(1).then(function (pdfPage) {
1163
- return pdfPage.getOperatorList().then(function (opList) {
1291
+ loadingTask.promise.then(pdfDoc => {
1292
+ return pdfDoc.getPage(1).then(pdfPage => {
1293
+ return pdfPage.getOperatorList().then(opList => {
1164
1294
  return pdfPage.stats;
1165
1295
  });
1166
1296
  });
1167
- }).then(function (stats) {
1168
- expect(stats instanceof _dom_utils.StatTimer).toEqual(true);
1297
+ }).then(stats => {
1298
+ expect(stats instanceof _display_utils.StatTimer).toEqual(true);
1169
1299
  expect(stats.times.length).toEqual(1);
1170
-
1171
- var _stats$times = _slicedToArray(stats.times, 1),
1172
- statEntry = _stats$times[0];
1173
-
1174
- expect(statEntry.name).toEqual('Page Request');
1175
- expect(statEntry.end - statEntry.start).toBeGreaterThan(0);
1300
+ const [statEntry] = stats.times;
1301
+ expect(statEntry.name).toEqual("Page Request");
1302
+ expect(statEntry.end - statEntry.start).toBeGreaterThanOrEqual(0);
1176
1303
  loadingTask.destroy().then(done);
1177
1304
  }, done.fail);
1178
1305
  });
1179
- it('gets page stats after rendering page, with `pdfBug` set', function (done) {
1180
- var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
1306
+ it("gets page stats after rendering page, with `pdfBug` set", function (done) {
1307
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, {
1181
1308
  pdfBug: true
1182
1309
  }));
1183
- var canvasAndCtx;
1184
- loadingTask.promise.then(function (pdfDoc) {
1185
- return pdfDoc.getPage(1).then(function (pdfPage) {
1186
- var viewport = pdfPage.getViewport({
1310
+ let canvasAndCtx;
1311
+ loadingTask.promise.then(pdfDoc => {
1312
+ return pdfDoc.getPage(1).then(pdfPage => {
1313
+ const viewport = pdfPage.getViewport({
1187
1314
  scale: 1
1188
1315
  });
1189
1316
  canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1190
- var renderTask = pdfPage.render({
1317
+ const renderTask = pdfPage.render({
1191
1318
  canvasContext: canvasAndCtx.context,
1192
1319
  canvasFactory: CanvasFactory,
1193
- viewport: viewport
1320
+ viewport
1194
1321
  });
1195
- return renderTask.promise.then(function () {
1322
+ return renderTask.promise.then(() => {
1196
1323
  return pdfPage.stats;
1197
1324
  });
1198
1325
  });
1199
- }).then(function (stats) {
1200
- expect(stats instanceof _dom_utils.StatTimer).toEqual(true);
1326
+ }).then(stats => {
1327
+ expect(stats instanceof _display_utils.StatTimer).toEqual(true);
1201
1328
  expect(stats.times.length).toEqual(3);
1202
-
1203
- var _stats$times2 = _slicedToArray(stats.times, 3),
1204
- statEntryOne = _stats$times2[0],
1205
- statEntryTwo = _stats$times2[1],
1206
- statEntryThree = _stats$times2[2];
1207
-
1208
- expect(statEntryOne.name).toEqual('Page Request');
1209
- expect(statEntryOne.end - statEntryOne.start).toBeGreaterThan(0);
1210
- expect(statEntryTwo.name).toEqual('Rendering');
1329
+ const [statEntryOne, statEntryTwo, statEntryThree] = stats.times;
1330
+ expect(statEntryOne.name).toEqual("Page Request");
1331
+ expect(statEntryOne.end - statEntryOne.start).toBeGreaterThanOrEqual(0);
1332
+ expect(statEntryTwo.name).toEqual("Rendering");
1211
1333
  expect(statEntryTwo.end - statEntryTwo.start).toBeGreaterThan(0);
1212
- expect(statEntryThree.name).toEqual('Overall');
1334
+ expect(statEntryThree.name).toEqual("Overall");
1213
1335
  expect(statEntryThree.end - statEntryThree.start).toBeGreaterThan(0);
1214
1336
  CanvasFactory.destroy(canvasAndCtx);
1215
1337
  loadingTask.destroy().then(done);
1216
1338
  }, done.fail);
1217
1339
  });
1218
- it('cancels rendering of page', function (done) {
1340
+ it("cancels rendering of page", function (done) {
1219
1341
  var viewport = page.getViewport({
1220
1342
  scale: 1
1221
1343
  });
@@ -1223,46 +1345,47 @@ describe('api', function () {
1223
1345
  var renderTask = page.render({
1224
1346
  canvasContext: canvasAndCtx.context,
1225
1347
  canvasFactory: CanvasFactory,
1226
- viewport: viewport
1348
+ viewport
1227
1349
  });
1228
1350
  renderTask.cancel();
1229
1351
  renderTask.promise.then(function () {
1230
- done.fail('shall cancel rendering');
1352
+ done.fail("shall cancel rendering");
1231
1353
  }).catch(function (error) {
1232
- expect(error instanceof _dom_utils.RenderingCancelledException).toEqual(true);
1233
- expect(error.type).toEqual('canvas');
1354
+ expect(error instanceof _display_utils.RenderingCancelledException).toEqual(true);
1355
+ expect(error.message).toEqual("Rendering cancelled, page 1");
1356
+ expect(error.type).toEqual("canvas");
1234
1357
  CanvasFactory.destroy(canvasAndCtx);
1235
1358
  done();
1236
1359
  });
1237
1360
  });
1238
- it('re-render page, using the same canvas, after cancelling rendering', function (done) {
1239
- var viewport = page.getViewport({
1361
+ it("re-render page, using the same canvas, after cancelling rendering", function (done) {
1362
+ const viewport = page.getViewport({
1240
1363
  scale: 1
1241
1364
  });
1242
- var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1243
- var renderTask = page.render({
1365
+ const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1366
+ const renderTask = page.render({
1244
1367
  canvasContext: canvasAndCtx.context,
1245
1368
  canvasFactory: CanvasFactory,
1246
- viewport: viewport
1369
+ viewport
1247
1370
  });
1248
1371
  renderTask.cancel();
1249
- renderTask.promise.then(function () {
1250
- throw new Error('shall cancel rendering');
1251
- }, function (reason) {
1252
- expect(reason instanceof _dom_utils.RenderingCancelledException).toEqual(true);
1253
- }).then(function () {
1254
- var reRenderTask = page.render({
1372
+ renderTask.promise.then(() => {
1373
+ throw new Error("shall cancel rendering");
1374
+ }, reason => {
1375
+ expect(reason instanceof _display_utils.RenderingCancelledException).toEqual(true);
1376
+ }).then(() => {
1377
+ const reRenderTask = page.render({
1255
1378
  canvasContext: canvasAndCtx.context,
1256
1379
  canvasFactory: CanvasFactory,
1257
- viewport: viewport
1380
+ viewport
1258
1381
  });
1259
1382
  return reRenderTask.promise;
1260
- }).then(function () {
1383
+ }).then(() => {
1261
1384
  CanvasFactory.destroy(canvasAndCtx);
1262
1385
  done();
1263
1386
  }, done.fail);
1264
1387
  });
1265
- it('multiple render() on the same canvas', function (done) {
1388
+ it("multiple render() on the same canvas", function (done) {
1266
1389
  var viewport = page.getViewport({
1267
1390
  scale: 1
1268
1391
  });
@@ -1270,92 +1393,167 @@ describe('api', function () {
1270
1393
  var renderTask1 = page.render({
1271
1394
  canvasContext: canvasAndCtx.context,
1272
1395
  canvasFactory: CanvasFactory,
1273
- viewport: viewport
1396
+ viewport
1274
1397
  });
1275
1398
  var renderTask2 = page.render({
1276
1399
  canvasContext: canvasAndCtx.context,
1277
1400
  canvasFactory: CanvasFactory,
1278
- viewport: viewport
1401
+ viewport
1279
1402
  });
1280
- Promise.all([renderTask1.promise, renderTask2.promise.then(function () {
1281
- done.fail('shall fail rendering');
1282
- }, function (reason) {
1403
+ Promise.all([renderTask1.promise, renderTask2.promise.then(() => {
1404
+ done.fail("shall fail rendering");
1405
+ }, reason => {
1283
1406
  expect(/multiple render\(\)/.test(reason.message)).toEqual(true);
1284
1407
  })]).then(done);
1285
1408
  });
1409
+ it("cleans up document resources after rendering of page", function (done) {
1410
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName));
1411
+ let canvasAndCtx;
1412
+ loadingTask.promise.then(pdfDoc => {
1413
+ return pdfDoc.getPage(1).then(pdfPage => {
1414
+ const viewport = pdfPage.getViewport({
1415
+ scale: 1
1416
+ });
1417
+ canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1418
+ const renderTask = pdfPage.render({
1419
+ canvasContext: canvasAndCtx.context,
1420
+ canvasFactory: CanvasFactory,
1421
+ viewport
1422
+ });
1423
+ return renderTask.promise.then(() => {
1424
+ return pdfDoc.cleanup();
1425
+ });
1426
+ });
1427
+ }).then(() => {
1428
+ expect(true).toEqual(true);
1429
+ CanvasFactory.destroy(canvasAndCtx);
1430
+ loadingTask.destroy().then(done);
1431
+ }, done.fail);
1432
+ });
1433
+ it("cleans up document resources during rendering of page", function (done) {
1434
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
1435
+ let canvasAndCtx;
1436
+ loadingTask.promise.then(pdfDoc => {
1437
+ return pdfDoc.getPage(1).then(pdfPage => {
1438
+ const viewport = pdfPage.getViewport({
1439
+ scale: 1
1440
+ });
1441
+ canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1442
+ const renderTask = pdfPage.render({
1443
+ canvasContext: canvasAndCtx.context,
1444
+ canvasFactory: CanvasFactory,
1445
+ viewport
1446
+ });
1447
+ pdfDoc.cleanup().then(() => {
1448
+ throw new Error("shall fail cleanup");
1449
+ }, reason => {
1450
+ expect(reason instanceof Error).toEqual(true);
1451
+ expect(reason.message).toEqual("startCleanup: Page 1 is currently rendering.");
1452
+ }).then(() => {
1453
+ return renderTask.promise;
1454
+ }).then(() => {
1455
+ CanvasFactory.destroy(canvasAndCtx);
1456
+ loadingTask.destroy().then(done);
1457
+ });
1458
+ });
1459
+ }).catch(done.fail);
1460
+ });
1461
+ it("caches image resources at the document/page level as expected (issue 11878)", async function (done) {
1462
+ const {
1463
+ NUM_PAGES_THRESHOLD
1464
+ } = _image_utils.GlobalImageCache,
1465
+ EXPECTED_WIDTH = 2550,
1466
+ EXPECTED_HEIGHT = 3300;
1467
+ const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("issue11878.pdf"));
1468
+ let firstImgData = null;
1469
+
1470
+ try {
1471
+ const pdfDoc = await loadingTask.promise;
1472
+
1473
+ for (let i = 1; i <= pdfDoc.numPages; i++) {
1474
+ const pdfPage = await pdfDoc.getPage(i);
1475
+ const opList = await pdfPage.getOperatorList();
1476
+ const {
1477
+ commonObjs,
1478
+ objs
1479
+ } = pdfPage;
1480
+ const imgIndex = opList.fnArray.indexOf(_util.OPS.paintImageXObject);
1481
+ const [objId, width, height] = opList.argsArray[imgIndex];
1482
+
1483
+ if (i < NUM_PAGES_THRESHOLD) {
1484
+ expect(objId).toEqual(`img_p${i - 1}_1`);
1485
+ expect(objs.has(objId)).toEqual(true);
1486
+ expect(commonObjs.has(objId)).toEqual(false);
1487
+ } else {
1488
+ expect(objId).toEqual(`g_${loadingTask.docId}_img_p${NUM_PAGES_THRESHOLD - 1}_1`);
1489
+ expect(objs.has(objId)).toEqual(false);
1490
+ expect(commonObjs.has(objId)).toEqual(true);
1491
+ }
1492
+
1493
+ expect(width).toEqual(EXPECTED_WIDTH);
1494
+ expect(height).toEqual(EXPECTED_HEIGHT);
1495
+
1496
+ if (i === 1) {
1497
+ firstImgData = objs.get(objId);
1498
+ expect(firstImgData.width).toEqual(EXPECTED_WIDTH);
1499
+ expect(firstImgData.height).toEqual(EXPECTED_HEIGHT);
1500
+ expect(firstImgData.kind).toEqual(_util.ImageKind.RGB_24BPP);
1501
+ expect(firstImgData.data instanceof Uint8ClampedArray).toEqual(true);
1502
+ expect(firstImgData.data.length).toEqual(25245000);
1503
+ } else {
1504
+ const objsPool = i >= NUM_PAGES_THRESHOLD ? commonObjs : objs;
1505
+ const currentImgData = objsPool.get(objId);
1506
+ expect(currentImgData.width).toEqual(firstImgData.width);
1507
+ expect(currentImgData.height).toEqual(firstImgData.height);
1508
+ expect(currentImgData.kind).toEqual(firstImgData.kind);
1509
+ expect(currentImgData.data instanceof Uint8ClampedArray).toEqual(true);
1510
+ expect(currentImgData.data.every((value, index) => {
1511
+ return value === firstImgData.data[index];
1512
+ })).toEqual(true);
1513
+ }
1514
+ }
1515
+
1516
+ await loadingTask.destroy();
1517
+ firstImgData = null;
1518
+ done();
1519
+ } catch (ex) {
1520
+ done.fail(ex);
1521
+ }
1522
+ });
1286
1523
  });
1287
- describe('Multiple `getDocument` instances', function () {
1288
- var pdf1 = (0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf');
1289
- var pdf2 = (0, _test_utils.buildGetDocumentParams)('TAMReview.pdf');
1290
- var pdf3 = (0, _test_utils.buildGetDocumentParams)('issue6068.pdf');
1524
+ describe("Multiple `getDocument` instances", function () {
1525
+ var pdf1 = (0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf");
1526
+ var pdf2 = (0, _test_utils.buildGetDocumentParams)("TAMReview.pdf");
1527
+ var pdf3 = (0, _test_utils.buildGetDocumentParams)("issue6068.pdf");
1291
1528
  var loadingTasks = [];
1292
- var pdfDocuments = [];
1293
-
1294
- function renderPDF(_x) {
1295
- return _renderPDF.apply(this, arguments);
1296
- }
1297
1529
 
1298
- function _renderPDF() {
1299
- _renderPDF = _asyncToGenerator(
1300
- /*#__PURE__*/
1301
- _regenerator.default.mark(function _callee(filename) {
1302
- var loadingTask, pdf, page, viewport, canvasAndCtx, renderTask, data;
1303
- return _regenerator.default.wrap(function _callee$(_context) {
1304
- while (1) {
1305
- switch (_context.prev = _context.next) {
1306
- case 0:
1307
- loadingTask = (0, _api.getDocument)(filename);
1308
- loadingTasks.push(loadingTask);
1309
- _context.next = 4;
1310
- return loadingTask.promise;
1311
-
1312
- case 4:
1313
- pdf = _context.sent;
1314
- pdfDocuments.push(pdf);
1315
- _context.next = 8;
1316
- return pdf.getPage(1);
1317
-
1318
- case 8:
1319
- page = _context.sent;
1320
- viewport = page.getViewport({
1321
- scale: 1.2
1322
- });
1323
- canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1324
- renderTask = page.render({
1325
- canvasContext: canvasAndCtx.context,
1326
- canvasFactory: CanvasFactory,
1327
- viewport: viewport
1328
- });
1329
- _context.next = 14;
1330
- return renderTask.promise;
1331
-
1332
- case 14:
1333
- data = canvasAndCtx.canvas.toDataURL();
1334
- CanvasFactory.destroy(canvasAndCtx);
1335
- return _context.abrupt("return", data);
1336
-
1337
- case 17:
1338
- case "end":
1339
- return _context.stop();
1340
- }
1341
- }
1342
- }, _callee, this);
1343
- }));
1344
- return _renderPDF.apply(this, arguments);
1530
+ async function renderPDF(filename) {
1531
+ const loadingTask = (0, _api.getDocument)(filename);
1532
+ loadingTasks.push(loadingTask);
1533
+ const pdf = await loadingTask.promise;
1534
+ const page = await pdf.getPage(1);
1535
+ const viewport = page.getViewport({
1536
+ scale: 1.2
1537
+ });
1538
+ const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
1539
+ const renderTask = page.render({
1540
+ canvasContext: canvasAndCtx.context,
1541
+ canvasFactory: CanvasFactory,
1542
+ viewport
1543
+ });
1544
+ await renderTask.promise;
1545
+ const data = canvasAndCtx.canvas.toDataURL();
1546
+ CanvasFactory.destroy(canvasAndCtx);
1547
+ return data;
1345
1548
  }
1346
1549
 
1347
1550
  afterEach(function (done) {
1348
- var destroyPromises = pdfDocuments.map(function (pdfDocument) {
1349
- return pdfDocument.destroy();
1350
- });
1351
- var destroyPromises2 = loadingTasks.map(function (loadingTask) {
1551
+ const destroyPromises = loadingTasks.map(function (loadingTask) {
1352
1552
  return loadingTask.destroy();
1353
1553
  });
1354
- Promise.all(destroyPromises.concat(destroyPromises2)).then(function () {
1355
- done();
1356
- });
1554
+ Promise.all(destroyPromises).then(done);
1357
1555
  });
1358
- it('should correctly render PDFs in parallel', function (done) {
1556
+ it("should correctly render PDFs in parallel", function (done) {
1359
1557
  var baseline1, baseline2, baseline3;
1360
1558
  var promiseDone = renderPDF(pdf1).then(function (data1) {
1361
1559
  baseline1 = data1;
@@ -1377,45 +1575,33 @@ describe('api', function () {
1377
1575
  }).catch(done.fail);
1378
1576
  });
1379
1577
  });
1380
- describe('PDFDataRangeTransport', function () {
1381
- var loadPromise;
1382
-
1383
- function getDocumentData() {
1384
- var pdfPath = new URL('../pdfs/tracemonkey.pdf', window.location).href;
1385
-
1386
- if (loadPromise) {
1387
- return loadPromise;
1388
- }
1389
-
1390
- loadPromise = new Promise(function (resolve, reject) {
1391
- var xhr = new XMLHttpRequest(pdfPath);
1392
- xhr.open('GET', pdfPath);
1393
- xhr.responseType = 'arraybuffer';
1394
-
1395
- xhr.onload = function () {
1396
- resolve(new Uint8Array(xhr.response));
1397
- };
1398
-
1399
- xhr.onerror = function () {
1400
- reject(new Error('PDF is not loaded'));
1401
- };
1402
-
1403
- xhr.send();
1404
- });
1405
- return loadPromise;
1406
- }
1578
+ describe("PDFDataRangeTransport", function () {
1579
+ let dataPromise;
1580
+ beforeAll(function (done) {
1581
+ const fileName = "tracemonkey.pdf";
1407
1582
 
1408
- it('should fetch document info and page using ranges', function (done) {
1409
- if ((0, _is_node.default)()) {
1410
- pending('XMLHttpRequest is not supported in Node.js.');
1583
+ if (_is_node.isNodeJS) {
1584
+ dataPromise = _test_utils.NodeFileReaderFactory.fetch({
1585
+ path: _test_utils.TEST_PDFS_PATH.node + fileName
1586
+ });
1587
+ } else {
1588
+ dataPromise = _test_utils.DOMFileReaderFactory.fetch({
1589
+ path: _test_utils.TEST_PDFS_PATH.dom + fileName
1590
+ });
1411
1591
  }
1412
1592
 
1413
- var transport;
1414
- var initialDataLength = 4000;
1415
- var fetches = 0;
1416
- var getDocumentPromise = getDocumentData().then(function (data) {
1417
- var initialData = data.subarray(0, initialDataLength);
1418
- transport = new _api.PDFDataRangeTransport(data.length, initialData);
1593
+ done();
1594
+ });
1595
+ afterAll(function () {
1596
+ dataPromise = null;
1597
+ });
1598
+ it("should fetch document info and page using ranges", function (done) {
1599
+ const initialDataLength = 4000;
1600
+ let fetches = 0,
1601
+ loadingTask;
1602
+ dataPromise.then(function (data) {
1603
+ const initialData = data.subarray(0, initialDataLength);
1604
+ const transport = new _api.PDFDataRangeTransport(data.length, initialData);
1419
1605
 
1420
1606
  transport.requestDataRange = function (begin, end) {
1421
1607
  fetches++;
@@ -1425,33 +1611,24 @@ describe('api', function () {
1425
1611
  });
1426
1612
  };
1427
1613
 
1428
- var loadingTask = (0, _api.getDocument)(transport);
1614
+ loadingTask = (0, _api.getDocument)(transport);
1429
1615
  return loadingTask.promise;
1430
- });
1431
- var pdfDocument;
1432
- var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
1433
- pdfDocument = pdfDocument_;
1434
- var pagePromise = pdfDocument.getPage(10);
1435
- return pagePromise;
1436
- });
1437
- getPagePromise.then(function (page) {
1616
+ }).then(function (pdfDocument) {
1438
1617
  expect(pdfDocument.numPages).toEqual(14);
1439
- expect(page.rotate).toEqual(0);
1618
+ return pdfDocument.getPage(10);
1619
+ }).then(function (pdfPage) {
1620
+ expect(pdfPage.rotate).toEqual(0);
1440
1621
  expect(fetches).toBeGreaterThan(2);
1441
- done();
1622
+ loadingTask.destroy().then(done);
1442
1623
  }).catch(done.fail);
1443
1624
  });
1444
- it('should fetch document info and page using range and streaming', function (done) {
1445
- if ((0, _is_node.default)()) {
1446
- pending('XMLHttpRequest is not supported in Node.js.');
1447
- }
1448
-
1449
- var transport;
1450
- var initialDataLength = 4000;
1451
- var fetches = 0;
1452
- var getDocumentPromise = getDocumentData().then(function (data) {
1453
- var initialData = data.subarray(0, initialDataLength);
1454
- transport = new _api.PDFDataRangeTransport(data.length, initialData);
1625
+ it("should fetch document info and page using range and streaming", function (done) {
1626
+ const initialDataLength = 4000;
1627
+ let fetches = 0,
1628
+ loadingTask;
1629
+ dataPromise.then(function (data) {
1630
+ const initialData = data.subarray(0, initialDataLength);
1631
+ const transport = new _api.PDFDataRangeTransport(data.length, initialData);
1455
1632
 
1456
1633
  transport.requestDataRange = function (begin, end) {
1457
1634
  fetches++;
@@ -1465,20 +1642,41 @@ describe('api', function () {
1465
1642
  });
1466
1643
  };
1467
1644
 
1468
- var loadingTask = (0, _api.getDocument)(transport);
1645
+ loadingTask = (0, _api.getDocument)(transport);
1469
1646
  return loadingTask.promise;
1470
- });
1471
- var pdfDocument;
1472
- var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
1473
- pdfDocument = pdfDocument_;
1474
- var pagePromise = pdfDocument.getPage(10);
1475
- return pagePromise;
1476
- });
1477
- getPagePromise.then(function (page) {
1647
+ }).then(function (pdfDocument) {
1478
1648
  expect(pdfDocument.numPages).toEqual(14);
1479
- expect(page.rotate).toEqual(0);
1649
+ return pdfDocument.getPage(10);
1650
+ }).then(function (pdfPage) {
1651
+ expect(pdfPage.rotate).toEqual(0);
1480
1652
  expect(fetches).toEqual(1);
1481
- done();
1653
+ waitSome(function () {
1654
+ loadingTask.destroy().then(done);
1655
+ });
1656
+ }).catch(done.fail);
1657
+ });
1658
+ it("should fetch document info and page, without range, " + "using complete initialData", function (done) {
1659
+ let fetches = 0,
1660
+ loadingTask;
1661
+ dataPromise.then(function (data) {
1662
+ const transport = new _api.PDFDataRangeTransport(data.length, data, true);
1663
+
1664
+ transport.requestDataRange = function (begin, end) {
1665
+ fetches++;
1666
+ };
1667
+
1668
+ loadingTask = (0, _api.getDocument)({
1669
+ disableRange: true,
1670
+ range: transport
1671
+ });
1672
+ return loadingTask.promise;
1673
+ }).then(function (pdfDocument) {
1674
+ expect(pdfDocument.numPages).toEqual(14);
1675
+ return pdfDocument.getPage(10);
1676
+ }).then(function (pdfPage) {
1677
+ expect(pdfPage.rotate).toEqual(0);
1678
+ expect(fetches).toEqual(0);
1679
+ loadingTask.destroy().then(done);
1482
1680
  }).catch(done.fail);
1483
1681
  });
1484
1682
  });