pdfjs-dist 2.2.228 → 2.6.347

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 (229) hide show
  1. package/README.md +4 -0
  2. package/bower.json +1 -1
  3. package/build/pdf.js +8816 -19300
  4. package/build/pdf.js.map +1 -1
  5. package/build/pdf.min.js +22 -1
  6. package/build/pdf.worker.entry.js +5 -3
  7. package/build/pdf.worker.js +27185 -35746
  8. package/build/pdf.worker.js.map +1 -1
  9. package/build/pdf.worker.min.js +22 -1
  10. package/es5/build/pdf.js +26665 -0
  11. package/es5/build/pdf.js.map +1 -0
  12. package/es5/build/pdf.min.js +22 -0
  13. package/{lib/shared/global_scope.js → es5/build/pdf.worker.entry.js} +5 -10
  14. package/es5/build/pdf.worker.js +61231 -0
  15. package/es5/build/pdf.worker.js.map +1 -0
  16. package/es5/build/pdf.worker.min.js +22 -0
  17. package/es5/image_decoders/pdf.image_decoders.js +12529 -0
  18. package/es5/image_decoders/pdf.image_decoders.js.map +1 -0
  19. package/es5/image_decoders/pdf.image_decoders.min.js +22 -0
  20. package/es5/web/images/annotation-check.svg +11 -0
  21. package/es5/web/images/annotation-comment.svg +16 -0
  22. package/es5/web/images/annotation-help.svg +26 -0
  23. package/es5/web/images/annotation-insert.svg +10 -0
  24. package/es5/web/images/annotation-key.svg +11 -0
  25. package/es5/web/images/annotation-newparagraph.svg +11 -0
  26. package/es5/web/images/annotation-noicon.svg +7 -0
  27. package/es5/web/images/annotation-note.svg +42 -0
  28. package/es5/web/images/annotation-paragraph.svg +16 -0
  29. package/es5/web/images/loading-icon.gif +0 -0
  30. package/es5/web/images/shadow.png +0 -0
  31. package/es5/web/pdf_viewer.css +406 -0
  32. package/es5/web/pdf_viewer.js +7886 -0
  33. package/es5/web/pdf_viewer.js.map +1 -0
  34. package/image_decoders/pdf.image_decoders.js +1498 -4832
  35. package/image_decoders/pdf.image_decoders.js.map +1 -1
  36. package/image_decoders/pdf.image_decoders.min.js +22 -1
  37. package/lib/README.md +7 -0
  38. package/lib/core/annotation.js +1144 -927
  39. package/lib/core/arithmetic_decoder.js +81 -97
  40. package/lib/core/bidi.js +54 -46
  41. package/lib/core/ccitt.js +88 -81
  42. package/lib/core/ccitt_stream.js +15 -14
  43. package/lib/core/cff_parser.js +747 -716
  44. package/lib/core/charsets.js +4 -4
  45. package/lib/core/chunked_stream.js +462 -553
  46. package/lib/core/cmap.js +231 -277
  47. package/lib/core/colorspace.js +715 -857
  48. package/lib/core/core_utils.js +57 -52
  49. package/lib/core/crypto.js +416 -437
  50. package/lib/core/document.js +629 -670
  51. package/lib/core/encodings.js +15 -15
  52. package/lib/core/evaluator.js +3229 -2800
  53. package/lib/core/font_renderer.js +135 -178
  54. package/lib/core/fonts.js +602 -534
  55. package/lib/core/function.js +355 -279
  56. package/lib/core/glyphlist.js +4527 -4526
  57. package/lib/core/image.js +442 -439
  58. package/lib/core/image_utils.js +221 -61
  59. package/lib/core/jbig2.js +325 -316
  60. package/lib/core/jbig2_stream.js +18 -17
  61. package/lib/core/jpeg_stream.js +21 -26
  62. package/lib/core/jpg.js +289 -233
  63. package/lib/core/jpx.js +161 -143
  64. package/lib/core/jpx_stream.js +28 -28
  65. package/lib/core/metrics.js +2928 -2928
  66. package/lib/core/murmurhash3.js +87 -102
  67. package/lib/core/obj.js +1343 -1300
  68. package/lib/core/operator_list.js +90 -67
  69. package/lib/core/parser.js +990 -987
  70. package/lib/core/pattern.js +105 -77
  71. package/lib/core/pdf_manager.js +149 -316
  72. package/lib/core/primitives.js +156 -104
  73. package/lib/core/ps_parser.js +175 -214
  74. package/lib/core/standard_fonts.js +237 -236
  75. package/lib/core/stream.js +94 -74
  76. package/lib/core/type1_parser.js +89 -69
  77. package/lib/core/unicode.js +1654 -1654
  78. package/lib/core/worker.js +275 -290
  79. package/lib/core/worker_stream.js +101 -210
  80. package/lib/core/writer.js +242 -0
  81. package/lib/display/annotation_layer.js +772 -1041
  82. package/lib/display/annotation_storage.js +89 -0
  83. package/lib/display/api.js +1575 -1868
  84. package/lib/display/api_compatibility.js +12 -17
  85. package/lib/display/canvas.js +370 -222
  86. package/lib/display/content_disposition.js +40 -59
  87. package/lib/display/display_utils.js +341 -536
  88. package/lib/display/fetch_stream.js +188 -304
  89. package/lib/display/font_loader.js +282 -414
  90. package/lib/display/metadata.js +81 -99
  91. package/lib/display/network.js +362 -511
  92. package/lib/display/network_utils.js +25 -18
  93. package/lib/display/node_stream.js +283 -465
  94. package/lib/display/node_utils.js +87 -0
  95. package/lib/display/optional_content_config.js +184 -0
  96. package/lib/display/pattern_helper.js +93 -44
  97. package/lib/display/svg.js +1163 -1401
  98. package/lib/display/text_layer.js +154 -137
  99. package/lib/display/transport_stream.js +237 -375
  100. package/lib/display/webgl.js +70 -83
  101. package/lib/display/worker_options.js +3 -3
  102. package/lib/display/xml_parser.js +303 -392
  103. package/lib/examples/node/domstubs.js +37 -37
  104. package/lib/pdf.js +220 -58
  105. package/lib/pdf.worker.js +14 -6
  106. package/lib/shared/compatibility.js +3 -246
  107. package/lib/shared/is_node.js +7 -6
  108. package/lib/shared/message_handler.js +327 -332
  109. package/lib/shared/util.js +271 -306
  110. package/lib/test/unit/annotation_spec.js +1902 -763
  111. package/lib/test/unit/annotation_storage_spec.js +83 -0
  112. package/lib/test/unit/api_spec.js +790 -592
  113. package/lib/test/unit/bidi_spec.js +7 -7
  114. package/lib/test/unit/cff_parser_spec.js +73 -72
  115. package/lib/test/unit/clitests_helper.js +7 -9
  116. package/lib/test/unit/cmap_spec.js +86 -88
  117. package/lib/test/unit/colorspace_spec.js +346 -154
  118. package/lib/test/unit/core_utils_spec.js +125 -105
  119. package/lib/test/unit/crypto_spec.js +232 -185
  120. package/lib/test/unit/custom_spec.js +150 -24
  121. package/lib/test/unit/display_svg_spec.js +34 -39
  122. package/lib/test/unit/display_utils_spec.js +139 -149
  123. package/lib/test/unit/document_spec.js +128 -17
  124. package/lib/test/unit/encodings_spec.js +12 -34
  125. package/lib/test/unit/evaluator_spec.js +112 -91
  126. package/lib/test/unit/fetch_stream_spec.js +31 -29
  127. package/lib/test/unit/function_spec.js +206 -204
  128. package/lib/test/unit/jasmine-boot.js +48 -32
  129. package/lib/test/unit/message_handler_spec.js +173 -159
  130. package/lib/test/unit/metadata_spec.js +71 -71
  131. package/lib/test/unit/murmurhash3_spec.js +12 -12
  132. package/lib/test/unit/network_spec.js +13 -11
  133. package/lib/test/unit/network_utils_spec.js +158 -144
  134. package/lib/test/unit/node_stream_spec.js +76 -90
  135. package/lib/test/unit/parser_spec.js +108 -114
  136. package/lib/test/unit/pdf_find_controller_spec.js +55 -86
  137. package/lib/test/unit/pdf_find_utils_spec.js +32 -32
  138. package/lib/test/unit/pdf_history_spec.js +32 -32
  139. package/lib/test/unit/primitives_spec.js +337 -156
  140. package/lib/test/unit/stream_spec.js +16 -14
  141. package/lib/test/unit/test_utils.js +91 -304
  142. package/lib/test/unit/testreporter.js +33 -22
  143. package/lib/test/unit/type1_parser_spec.js +42 -42
  144. package/lib/test/unit/ui_utils_spec.js +299 -461
  145. package/lib/test/unit/unicode_spec.js +42 -42
  146. package/lib/test/unit/util_spec.js +129 -149
  147. package/lib/test/unit/writer_spec.js +84 -0
  148. package/lib/web/annotation_layer_builder.js +78 -111
  149. package/lib/web/app.js +1542 -1267
  150. package/lib/web/app_options.js +76 -91
  151. package/lib/web/base_tree_viewer.js +100 -0
  152. package/lib/web/base_viewer.js +869 -834
  153. package/lib/web/chromecom.js +165 -252
  154. package/lib/web/debugger.js +149 -205
  155. package/lib/web/download_manager.js +34 -58
  156. package/lib/web/firefox_print_service.js +62 -34
  157. package/lib/web/firefoxcom.js +206 -374
  158. package/lib/web/genericcom.js +26 -108
  159. package/lib/web/genericl10n.js +24 -153
  160. package/lib/web/grab_to_pan.js +30 -30
  161. package/lib/web/interfaces.js +80 -254
  162. package/lib/web/overlay_manager.js +70 -246
  163. package/lib/web/password_prompt.js +38 -64
  164. package/lib/web/pdf_attachment_viewer.js +150 -126
  165. package/lib/web/pdf_cursor_tools.js +75 -102
  166. package/lib/web/pdf_document_properties.js +229 -383
  167. package/lib/web/pdf_find_bar.js +136 -170
  168. package/lib/web/pdf_find_controller.js +492 -548
  169. package/lib/web/pdf_find_utils.js +13 -13
  170. package/lib/web/pdf_history.js +397 -406
  171. package/lib/web/pdf_layer_viewer.js +208 -0
  172. package/lib/web/pdf_link_service.js +304 -348
  173. package/lib/web/pdf_outline_viewer.js +119 -212
  174. package/lib/web/pdf_page_view.js +474 -513
  175. package/lib/web/pdf_presentation_mode.js +308 -357
  176. package/lib/web/pdf_print_service.js +96 -110
  177. package/lib/web/pdf_rendering_queue.js +87 -108
  178. package/lib/web/pdf_sidebar.js +286 -302
  179. package/lib/web/pdf_sidebar_resizer.js +92 -119
  180. package/lib/web/pdf_single_page_viewer.js +77 -126
  181. package/lib/web/pdf_thumbnail_view.js +289 -297
  182. package/lib/web/pdf_thumbnail_viewer.js +202 -206
  183. package/lib/web/pdf_viewer.component.js +20 -21
  184. package/lib/web/pdf_viewer.js +55 -115
  185. package/lib/web/preferences.js +69 -273
  186. package/lib/web/secondary_toolbar.js +164 -196
  187. package/lib/web/text_layer_builder.js +284 -319
  188. package/lib/web/toolbar.js +217 -210
  189. package/lib/web/ui_utils.js +274 -421
  190. package/lib/web/view_history.js +52 -226
  191. package/lib/web/viewer_compatibility.js +21 -6
  192. package/package.json +4 -9
  193. package/types/display/annotation_layer.d.ts +61 -0
  194. package/types/display/annotation_storage.d.ts +36 -0
  195. package/types/display/api.d.ts +1107 -0
  196. package/types/display/api_compatibility.d.ts +1 -0
  197. package/types/display/canvas.d.ts +4 -0
  198. package/types/display/content_disposition.d.ts +7 -0
  199. package/types/display/display_utils.d.ts +243 -0
  200. package/types/display/fetch_stream.d.ts +67 -0
  201. package/types/display/font_loader.d.ts +19 -0
  202. package/types/display/metadata.d.ts +9 -0
  203. package/types/display/network.d.ts +80 -0
  204. package/types/display/network_utils.d.ts +14 -0
  205. package/types/display/node_stream.d.ts +72 -0
  206. package/types/display/node_utils.d.ts +6 -0
  207. package/types/display/optional_content_config.d.ts +12 -0
  208. package/types/display/pattern_helper.d.ts +5 -0
  209. package/types/display/svg.d.ts +2 -0
  210. package/types/display/text_layer.d.ts +83 -0
  211. package/types/display/transport_stream.d.ts +57 -0
  212. package/types/display/webgl.d.ts +20 -0
  213. package/types/display/worker_options.d.ts +27 -0
  214. package/types/display/xml_parser.d.ts +35 -0
  215. package/types/pdf.d.ts +33 -0
  216. package/types/shared/compatibility.d.ts +1 -0
  217. package/types/shared/is_node.d.ts +1 -0
  218. package/types/shared/message_handler.d.ts +62 -0
  219. package/types/shared/util.d.ts +421 -0
  220. package/web/images/shadow.png +0 -0
  221. package/web/pdf_viewer.css +29 -27
  222. package/web/pdf_viewer.js +3524 -4746
  223. package/web/pdf_viewer.js.map +1 -1
  224. package/webpack.js +6 -5
  225. package/external/streams/streams-lib.js +0 -3962
  226. package/external/url/url-lib.js +0 -627
  227. package/lib/shared/streams_polyfill.js +0 -43
  228. package/lib/shared/url_polyfill.js +0 -56
  229. package/web/images/texture.png +0 -0
@@ -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 2019 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.
@@ -24,307 +24,249 @@
24
24
  Object.defineProperty(exports, "__esModule", {
25
25
  value: true
26
26
  });
27
- exports.MessageHandler = MessageHandler;
28
-
29
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
30
-
31
- var _util = require("./util");
32
-
33
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
34
-
35
- 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); }
36
-
37
- 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); } }
38
-
39
- 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); }); }; }
40
-
41
- function resolveCall(_x, _x2) {
42
- return _resolveCall.apply(this, arguments);
43
- }
44
-
45
- function _resolveCall() {
46
- _resolveCall = _asyncToGenerator(
47
- /*#__PURE__*/
48
- _regenerator["default"].mark(function _callee(fn, args) {
49
- var thisArg,
50
- _args = arguments;
51
- return _regenerator["default"].wrap(function _callee$(_context) {
52
- while (1) {
53
- switch (_context.prev = _context.next) {
54
- case 0:
55
- thisArg = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
56
-
57
- if (fn) {
58
- _context.next = 3;
59
- break;
60
- }
61
-
62
- return _context.abrupt("return", undefined);
63
-
64
- case 3:
65
- return _context.abrupt("return", fn.apply(thisArg, args));
66
-
67
- case 4:
68
- case "end":
69
- return _context.stop();
70
- }
71
- }
72
- }, _callee);
73
- }));
74
- return _resolveCall.apply(this, arguments);
75
- }
27
+ exports.MessageHandler = void 0;
28
+
29
+ var _util = require("./util.js");
30
+
31
+ const CallbackKind = {
32
+ UNKNOWN: 0,
33
+ DATA: 1,
34
+ ERROR: 2
35
+ };
36
+ const StreamKind = {
37
+ UNKNOWN: 0,
38
+ CANCEL: 1,
39
+ CANCEL_COMPLETE: 2,
40
+ CLOSE: 3,
41
+ ENQUEUE: 4,
42
+ ERROR: 5,
43
+ PULL: 6,
44
+ PULL_COMPLETE: 7,
45
+ START_COMPLETE: 8
46
+ };
76
47
 
77
48
  function wrapReason(reason) {
78
- if (_typeof(reason) !== 'object') {
49
+ if (typeof reason !== "object" || reason === null) {
79
50
  return reason;
80
51
  }
81
52
 
82
53
  switch (reason.name) {
83
- case 'AbortException':
54
+ case "AbortException":
84
55
  return new _util.AbortException(reason.message);
85
56
 
86
- case 'MissingPDFException':
57
+ case "MissingPDFException":
87
58
  return new _util.MissingPDFException(reason.message);
88
59
 
89
- case 'UnexpectedResponseException':
60
+ case "UnexpectedResponseException":
90
61
  return new _util.UnexpectedResponseException(reason.message, reason.status);
91
62
 
92
- default:
63
+ case "UnknownErrorException":
93
64
  return new _util.UnknownErrorException(reason.message, reason.details);
94
- }
95
- }
96
65
 
97
- function makeReasonSerializable(reason) {
98
- if (!(reason instanceof Error) || reason instanceof _util.AbortException || reason instanceof _util.MissingPDFException || reason instanceof _util.UnexpectedResponseException || reason instanceof _util.UnknownErrorException) {
99
- return reason;
66
+ default:
67
+ return new _util.UnknownErrorException(reason.message, reason.toString());
100
68
  }
101
-
102
- return new _util.UnknownErrorException(reason.message, reason.toString());
103
69
  }
104
70
 
105
- function resolveOrReject(capability, success, reason) {
106
- if (success) {
107
- capability.resolve();
108
- } else {
109
- capability.reject(reason);
110
- }
111
- }
71
+ class MessageHandler {
72
+ constructor(sourceName, targetName, comObj) {
73
+ this.sourceName = sourceName;
74
+ this.targetName = targetName;
75
+ this.comObj = comObj;
76
+ this.callbackId = 1;
77
+ this.streamId = 1;
78
+ this.postMessageTransfers = true;
79
+ this.streamSinks = Object.create(null);
80
+ this.streamControllers = Object.create(null);
81
+ this.callbackCapabilities = Object.create(null);
82
+ this.actionHandler = Object.create(null);
83
+
84
+ this._onComObjOnMessage = event => {
85
+ const data = event.data;
86
+
87
+ if (data.targetName !== this.sourceName) {
88
+ return;
89
+ }
112
90
 
113
- function finalize(promise) {
114
- return Promise.resolve(promise)["catch"](function () {});
115
- }
91
+ if (data.stream) {
92
+ this._processStreamMessage(data);
116
93
 
117
- function MessageHandler(sourceName, targetName, comObj) {
118
- var _this = this;
119
-
120
- this.sourceName = sourceName;
121
- this.targetName = targetName;
122
- this.comObj = comObj;
123
- this.callbackId = 1;
124
- this.streamId = 1;
125
- this.postMessageTransfers = true;
126
- this.streamSinks = Object.create(null);
127
- this.streamControllers = Object.create(null);
128
- var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
129
- var ah = this.actionHandler = Object.create(null);
130
-
131
- this._onComObjOnMessage = function (event) {
132
- var data = event.data;
133
-
134
- if (data.targetName !== _this.sourceName) {
135
- return;
136
- }
94
+ return;
95
+ }
137
96
 
138
- if (data.stream) {
139
- _this._processStreamMessage(data);
140
- } else if (data.isReply) {
141
- var callbackId = data.callbackId;
97
+ if (data.callback) {
98
+ const callbackId = data.callbackId;
99
+ const capability = this.callbackCapabilities[callbackId];
142
100
 
143
- if (data.callbackId in callbacksCapabilities) {
144
- var callback = callbacksCapabilities[callbackId];
145
- delete callbacksCapabilities[callbackId];
101
+ if (!capability) {
102
+ throw new Error(`Cannot resolve callback ${callbackId}`);
103
+ }
146
104
 
147
- if ('error' in data) {
148
- callback.reject(wrapReason(data.error));
105
+ delete this.callbackCapabilities[callbackId];
106
+
107
+ if (data.callback === CallbackKind.DATA) {
108
+ capability.resolve(data.data);
109
+ } else if (data.callback === CallbackKind.ERROR) {
110
+ capability.reject(wrapReason(data.reason));
149
111
  } else {
150
- callback.resolve(data.data);
112
+ throw new Error("Unexpected callback case");
151
113
  }
152
- } else {
153
- throw new Error("Cannot resolve callback ".concat(callbackId));
114
+
115
+ return;
116
+ }
117
+
118
+ const action = this.actionHandler[data.action];
119
+
120
+ if (!action) {
121
+ throw new Error(`Unknown action from worker: ${data.action}`);
154
122
  }
155
- } else if (data.action in ah) {
156
- var action = ah[data.action];
157
123
 
158
124
  if (data.callbackId) {
159
- var _sourceName = _this.sourceName;
160
- var _targetName = data.sourceName;
161
- Promise.resolve().then(function () {
162
- return action[0].call(action[1], data.data);
125
+ const cbSourceName = this.sourceName;
126
+ const cbTargetName = data.sourceName;
127
+ new Promise(function (resolve) {
128
+ resolve(action(data.data));
163
129
  }).then(function (result) {
164
130
  comObj.postMessage({
165
- sourceName: _sourceName,
166
- targetName: _targetName,
167
- isReply: true,
131
+ sourceName: cbSourceName,
132
+ targetName: cbTargetName,
133
+ callback: CallbackKind.DATA,
168
134
  callbackId: data.callbackId,
169
135
  data: result
170
136
  });
171
137
  }, function (reason) {
172
138
  comObj.postMessage({
173
- sourceName: _sourceName,
174
- targetName: _targetName,
175
- isReply: true,
139
+ sourceName: cbSourceName,
140
+ targetName: cbTargetName,
141
+ callback: CallbackKind.ERROR,
176
142
  callbackId: data.callbackId,
177
- error: makeReasonSerializable(reason)
143
+ reason: wrapReason(reason)
178
144
  });
179
145
  });
180
- } else if (data.streamId) {
181
- _this._createStreamSink(data);
182
- } else {
183
- action[0].call(action[1], data.data);
146
+ return;
184
147
  }
185
- } else {
186
- throw new Error("Unknown action from worker: ".concat(data.action));
187
- }
188
- };
189
148
 
190
- comObj.addEventListener('message', this._onComObjOnMessage);
191
- }
149
+ if (data.streamId) {
150
+ this._createStreamSink(data);
151
+
152
+ return;
153
+ }
154
+
155
+ action(data.data);
156
+ };
192
157
 
193
- MessageHandler.prototype = {
194
- on: function on(actionName, handler, scope) {
195
- var ah = this.actionHandler;
158
+ comObj.addEventListener("message", this._onComObjOnMessage);
159
+ }
160
+
161
+ on(actionName, handler) {
162
+ const ah = this.actionHandler;
196
163
 
197
164
  if (ah[actionName]) {
198
- throw new Error("There is already an actionName called \"".concat(actionName, "\""));
165
+ throw new Error(`There is already an actionName called "${actionName}"`);
199
166
  }
200
167
 
201
- ah[actionName] = [handler, scope];
202
- },
203
- send: function send(actionName, data, transfers) {
204
- var message = {
205
- sourceName: this.sourceName,
206
- targetName: this.targetName,
207
- action: actionName,
208
- data: data
209
- };
210
- this.postMessage(message, transfers);
211
- },
212
- sendWithPromise: function sendWithPromise(actionName, data, transfers) {
213
- var callbackId = this.callbackId++;
214
- var message = {
168
+ ah[actionName] = handler;
169
+ }
170
+
171
+ send(actionName, data, transfers) {
172
+ this._postMessage({
215
173
  sourceName: this.sourceName,
216
174
  targetName: this.targetName,
217
175
  action: actionName,
218
- data: data,
219
- callbackId: callbackId
220
- };
221
- var capability = (0, _util.createPromiseCapability)();
222
- this.callbacksCapabilities[callbackId] = capability;
176
+ data
177
+ }, transfers);
178
+ }
179
+
180
+ sendWithPromise(actionName, data, transfers) {
181
+ const callbackId = this.callbackId++;
182
+ const capability = (0, _util.createPromiseCapability)();
183
+ this.callbackCapabilities[callbackId] = capability;
223
184
 
224
185
  try {
225
- this.postMessage(message, transfers);
226
- } catch (e) {
227
- capability.reject(e);
186
+ this._postMessage({
187
+ sourceName: this.sourceName,
188
+ targetName: this.targetName,
189
+ action: actionName,
190
+ callbackId,
191
+ data
192
+ }, transfers);
193
+ } catch (ex) {
194
+ capability.reject(ex);
228
195
  }
229
196
 
230
197
  return capability.promise;
231
- },
232
- sendWithStream: function sendWithStream(actionName, data, queueingStrategy, transfers) {
233
- var _this2 = this;
234
-
235
- var streamId = this.streamId++;
236
- var sourceName = this.sourceName;
237
- var targetName = this.targetName;
238
- return new _util.ReadableStream({
239
- start: function start(controller) {
240
- var startCapability = (0, _util.createPromiseCapability)();
241
- _this2.streamControllers[streamId] = {
242
- controller: controller,
198
+ }
199
+
200
+ sendWithStream(actionName, data, queueingStrategy, transfers) {
201
+ const streamId = this.streamId++;
202
+ const sourceName = this.sourceName;
203
+ const targetName = this.targetName;
204
+ const comObj = this.comObj;
205
+ return new ReadableStream({
206
+ start: controller => {
207
+ const startCapability = (0, _util.createPromiseCapability)();
208
+ this.streamControllers[streamId] = {
209
+ controller,
243
210
  startCall: startCapability,
211
+ pullCall: null,
212
+ cancelCall: null,
244
213
  isClosed: false
245
214
  };
246
215
 
247
- _this2.postMessage({
248
- sourceName: sourceName,
249
- targetName: targetName,
216
+ this._postMessage({
217
+ sourceName,
218
+ targetName,
250
219
  action: actionName,
251
- streamId: streamId,
252
- data: data,
220
+ streamId,
221
+ data,
253
222
  desiredSize: controller.desiredSize
254
- });
223
+ }, transfers);
255
224
 
256
225
  return startCapability.promise;
257
226
  },
258
- pull: function pull(controller) {
259
- var pullCapability = (0, _util.createPromiseCapability)();
260
- _this2.streamControllers[streamId].pullCall = pullCapability;
261
-
262
- _this2.postMessage({
263
- sourceName: sourceName,
264
- targetName: targetName,
265
- stream: 'pull',
266
- streamId: streamId,
227
+ pull: controller => {
228
+ const pullCapability = (0, _util.createPromiseCapability)();
229
+ this.streamControllers[streamId].pullCall = pullCapability;
230
+ comObj.postMessage({
231
+ sourceName,
232
+ targetName,
233
+ stream: StreamKind.PULL,
234
+ streamId,
267
235
  desiredSize: controller.desiredSize
268
236
  });
269
-
270
237
  return pullCapability.promise;
271
238
  },
272
- cancel: function cancel(reason) {
273
- var cancelCapability = (0, _util.createPromiseCapability)();
274
- _this2.streamControllers[streamId].cancelCall = cancelCapability;
275
- _this2.streamControllers[streamId].isClosed = true;
276
-
277
- _this2.postMessage({
278
- sourceName: sourceName,
279
- targetName: targetName,
280
- stream: 'cancel',
281
- reason: reason,
282
- streamId: streamId
239
+ cancel: reason => {
240
+ (0, _util.assert)(reason instanceof Error, "cancel must have a valid reason");
241
+ const cancelCapability = (0, _util.createPromiseCapability)();
242
+ this.streamControllers[streamId].cancelCall = cancelCapability;
243
+ this.streamControllers[streamId].isClosed = true;
244
+ comObj.postMessage({
245
+ sourceName,
246
+ targetName,
247
+ stream: StreamKind.CANCEL,
248
+ streamId,
249
+ reason: wrapReason(reason)
283
250
  });
284
-
285
251
  return cancelCapability.promise;
286
252
  }
287
253
  }, queueingStrategy);
288
- },
289
- _createStreamSink: function _createStreamSink(data) {
290
- var _this3 = this;
291
-
292
- var self = this;
293
- var action = this.actionHandler[data.action];
294
- var streamId = data.streamId;
295
- var desiredSize = data.desiredSize;
296
- var sourceName = this.sourceName;
297
- var targetName = data.sourceName;
298
- var capability = (0, _util.createPromiseCapability)();
299
-
300
- var sendStreamRequest = function sendStreamRequest(_ref) {
301
- var stream = _ref.stream,
302
- chunk = _ref.chunk,
303
- transfers = _ref.transfers,
304
- success = _ref.success,
305
- reason = _ref.reason;
306
-
307
- _this3.postMessage({
308
- sourceName: sourceName,
309
- targetName: targetName,
310
- stream: stream,
311
- streamId: streamId,
312
- chunk: chunk,
313
- success: success,
314
- reason: reason
315
- }, transfers);
316
- };
317
-
318
- var streamSink = {
319
- enqueue: function enqueue(chunk) {
320
- var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
321
- var transfers = arguments.length > 2 ? arguments[2] : undefined;
254
+ }
322
255
 
256
+ _createStreamSink(data) {
257
+ const self = this;
258
+ const action = this.actionHandler[data.action];
259
+ const streamId = data.streamId;
260
+ const sourceName = this.sourceName;
261
+ const targetName = data.sourceName;
262
+ const comObj = this.comObj;
263
+ const streamSink = {
264
+ enqueue(chunk, size = 1, transfers) {
323
265
  if (this.isCancelled) {
324
266
  return;
325
267
  }
326
268
 
327
- var lastDesiredSize = this.desiredSize;
269
+ const lastDesiredSize = this.desiredSize;
328
270
  this.desiredSize -= size;
329
271
 
330
272
  if (lastDesiredSize > 0 && this.desiredSize <= 0) {
@@ -332,190 +274,243 @@ MessageHandler.prototype = {
332
274
  this.ready = this.sinkCapability.promise;
333
275
  }
334
276
 
335
- sendStreamRequest({
336
- stream: 'enqueue',
337
- chunk: chunk,
338
- transfers: transfers
339
- });
277
+ self._postMessage({
278
+ sourceName,
279
+ targetName,
280
+ stream: StreamKind.ENQUEUE,
281
+ streamId,
282
+ chunk
283
+ }, transfers);
340
284
  },
341
- close: function close() {
285
+
286
+ close() {
342
287
  if (this.isCancelled) {
343
288
  return;
344
289
  }
345
290
 
346
291
  this.isCancelled = true;
347
- sendStreamRequest({
348
- stream: 'close'
292
+ comObj.postMessage({
293
+ sourceName,
294
+ targetName,
295
+ stream: StreamKind.CLOSE,
296
+ streamId
349
297
  });
350
298
  delete self.streamSinks[streamId];
351
299
  },
352
- error: function error(reason) {
300
+
301
+ error(reason) {
302
+ (0, _util.assert)(reason instanceof Error, "error must have a valid reason");
303
+
353
304
  if (this.isCancelled) {
354
305
  return;
355
306
  }
356
307
 
357
308
  this.isCancelled = true;
358
- sendStreamRequest({
359
- stream: 'error',
360
- reason: reason
309
+ comObj.postMessage({
310
+ sourceName,
311
+ targetName,
312
+ stream: StreamKind.ERROR,
313
+ streamId,
314
+ reason: wrapReason(reason)
361
315
  });
362
316
  },
363
- sinkCapability: capability,
317
+
318
+ sinkCapability: (0, _util.createPromiseCapability)(),
364
319
  onPull: null,
365
320
  onCancel: null,
366
321
  isCancelled: false,
367
- desiredSize: desiredSize,
322
+ desiredSize: data.desiredSize,
368
323
  ready: null
369
324
  };
370
325
  streamSink.sinkCapability.resolve();
371
326
  streamSink.ready = streamSink.sinkCapability.promise;
372
327
  this.streamSinks[streamId] = streamSink;
373
- resolveCall(action[0], [data.data, streamSink], action[1]).then(function () {
374
- sendStreamRequest({
375
- stream: 'start_complete',
328
+ new Promise(function (resolve) {
329
+ resolve(action(data.data, streamSink));
330
+ }).then(function () {
331
+ comObj.postMessage({
332
+ sourceName,
333
+ targetName,
334
+ stream: StreamKind.START_COMPLETE,
335
+ streamId,
376
336
  success: true
377
337
  });
378
338
  }, function (reason) {
379
- sendStreamRequest({
380
- stream: 'start_complete',
381
- success: false,
382
- reason: reason
339
+ comObj.postMessage({
340
+ sourceName,
341
+ targetName,
342
+ stream: StreamKind.START_COMPLETE,
343
+ streamId,
344
+ reason: wrapReason(reason)
383
345
  });
384
346
  });
385
- },
386
- _processStreamMessage: function _processStreamMessage(data) {
387
- var _this4 = this;
388
-
389
- var sourceName = this.sourceName;
390
- var targetName = data.sourceName;
391
- var streamId = data.streamId;
392
-
393
- var sendStreamResponse = function sendStreamResponse(_ref2) {
394
- var stream = _ref2.stream,
395
- success = _ref2.success,
396
- reason = _ref2.reason;
397
-
398
- _this4.comObj.postMessage({
399
- sourceName: sourceName,
400
- targetName: targetName,
401
- stream: stream,
402
- success: success,
403
- streamId: streamId,
404
- reason: reason
405
- });
406
- };
347
+ }
407
348
 
408
- var deleteStreamController = function deleteStreamController() {
409
- Promise.all([_this4.streamControllers[data.streamId].startCall, _this4.streamControllers[data.streamId].pullCall, _this4.streamControllers[data.streamId].cancelCall].map(function (capability) {
410
- return capability && finalize(capability.promise);
411
- })).then(function () {
412
- delete _this4.streamControllers[data.streamId];
413
- });
414
- };
349
+ _processStreamMessage(data) {
350
+ const streamId = data.streamId;
351
+ const sourceName = this.sourceName;
352
+ const targetName = data.sourceName;
353
+ const comObj = this.comObj;
415
354
 
416
355
  switch (data.stream) {
417
- case 'start_complete':
418
- resolveOrReject(this.streamControllers[data.streamId].startCall, data.success, wrapReason(data.reason));
356
+ case StreamKind.START_COMPLETE:
357
+ if (data.success) {
358
+ this.streamControllers[streamId].startCall.resolve();
359
+ } else {
360
+ this.streamControllers[streamId].startCall.reject(wrapReason(data.reason));
361
+ }
362
+
419
363
  break;
420
364
 
421
- case 'pull_complete':
422
- resolveOrReject(this.streamControllers[data.streamId].pullCall, data.success, wrapReason(data.reason));
365
+ case StreamKind.PULL_COMPLETE:
366
+ if (data.success) {
367
+ this.streamControllers[streamId].pullCall.resolve();
368
+ } else {
369
+ this.streamControllers[streamId].pullCall.reject(wrapReason(data.reason));
370
+ }
371
+
423
372
  break;
424
373
 
425
- case 'pull':
426
- if (!this.streamSinks[data.streamId]) {
427
- sendStreamResponse({
428
- stream: 'pull_complete',
374
+ case StreamKind.PULL:
375
+ if (!this.streamSinks[streamId]) {
376
+ comObj.postMessage({
377
+ sourceName,
378
+ targetName,
379
+ stream: StreamKind.PULL_COMPLETE,
380
+ streamId,
429
381
  success: true
430
382
  });
431
383
  break;
432
384
  }
433
385
 
434
- if (this.streamSinks[data.streamId].desiredSize <= 0 && data.desiredSize > 0) {
435
- this.streamSinks[data.streamId].sinkCapability.resolve();
386
+ if (this.streamSinks[streamId].desiredSize <= 0 && data.desiredSize > 0) {
387
+ this.streamSinks[streamId].sinkCapability.resolve();
436
388
  }
437
389
 
438
- this.streamSinks[data.streamId].desiredSize = data.desiredSize;
439
- resolveCall(this.streamSinks[data.streamId].onPull).then(function () {
440
- sendStreamResponse({
441
- stream: 'pull_complete',
390
+ this.streamSinks[streamId].desiredSize = data.desiredSize;
391
+ const {
392
+ onPull
393
+ } = this.streamSinks[data.streamId];
394
+ new Promise(function (resolve) {
395
+ resolve(onPull && onPull());
396
+ }).then(function () {
397
+ comObj.postMessage({
398
+ sourceName,
399
+ targetName,
400
+ stream: StreamKind.PULL_COMPLETE,
401
+ streamId,
442
402
  success: true
443
403
  });
444
404
  }, function (reason) {
445
- sendStreamResponse({
446
- stream: 'pull_complete',
447
- success: false,
448
- reason: reason
405
+ comObj.postMessage({
406
+ sourceName,
407
+ targetName,
408
+ stream: StreamKind.PULL_COMPLETE,
409
+ streamId,
410
+ reason: wrapReason(reason)
449
411
  });
450
412
  });
451
413
  break;
452
414
 
453
- case 'enqueue':
454
- (0, _util.assert)(this.streamControllers[data.streamId], 'enqueue should have stream controller');
415
+ case StreamKind.ENQUEUE:
416
+ (0, _util.assert)(this.streamControllers[streamId], "enqueue should have stream controller");
455
417
 
456
- if (!this.streamControllers[data.streamId].isClosed) {
457
- this.streamControllers[data.streamId].controller.enqueue(data.chunk);
418
+ if (this.streamControllers[streamId].isClosed) {
419
+ break;
458
420
  }
459
421
 
422
+ this.streamControllers[streamId].controller.enqueue(data.chunk);
460
423
  break;
461
424
 
462
- case 'close':
463
- (0, _util.assert)(this.streamControllers[data.streamId], 'close should have stream controller');
425
+ case StreamKind.CLOSE:
426
+ (0, _util.assert)(this.streamControllers[streamId], "close should have stream controller");
464
427
 
465
- if (this.streamControllers[data.streamId].isClosed) {
428
+ if (this.streamControllers[streamId].isClosed) {
466
429
  break;
467
430
  }
468
431
 
469
- this.streamControllers[data.streamId].isClosed = true;
470
- this.streamControllers[data.streamId].controller.close();
471
- deleteStreamController();
432
+ this.streamControllers[streamId].isClosed = true;
433
+ this.streamControllers[streamId].controller.close();
434
+
435
+ this._deleteStreamController(streamId);
436
+
472
437
  break;
473
438
 
474
- case 'error':
475
- (0, _util.assert)(this.streamControllers[data.streamId], 'error should have stream controller');
476
- this.streamControllers[data.streamId].controller.error(wrapReason(data.reason));
477
- deleteStreamController();
439
+ case StreamKind.ERROR:
440
+ (0, _util.assert)(this.streamControllers[streamId], "error should have stream controller");
441
+ this.streamControllers[streamId].controller.error(wrapReason(data.reason));
442
+
443
+ this._deleteStreamController(streamId);
444
+
478
445
  break;
479
446
 
480
- case 'cancel_complete':
481
- resolveOrReject(this.streamControllers[data.streamId].cancelCall, data.success, wrapReason(data.reason));
482
- deleteStreamController();
447
+ case StreamKind.CANCEL_COMPLETE:
448
+ if (data.success) {
449
+ this.streamControllers[streamId].cancelCall.resolve();
450
+ } else {
451
+ this.streamControllers[streamId].cancelCall.reject(wrapReason(data.reason));
452
+ }
453
+
454
+ this._deleteStreamController(streamId);
455
+
483
456
  break;
484
457
 
485
- case 'cancel':
486
- if (!this.streamSinks[data.streamId]) {
458
+ case StreamKind.CANCEL:
459
+ if (!this.streamSinks[streamId]) {
487
460
  break;
488
461
  }
489
462
 
490
- resolveCall(this.streamSinks[data.streamId].onCancel, [wrapReason(data.reason)]).then(function () {
491
- sendStreamResponse({
492
- stream: 'cancel_complete',
463
+ const {
464
+ onCancel
465
+ } = this.streamSinks[data.streamId];
466
+ new Promise(function (resolve) {
467
+ resolve(onCancel && onCancel(wrapReason(data.reason)));
468
+ }).then(function () {
469
+ comObj.postMessage({
470
+ sourceName,
471
+ targetName,
472
+ stream: StreamKind.CANCEL_COMPLETE,
473
+ streamId,
493
474
  success: true
494
475
  });
495
476
  }, function (reason) {
496
- sendStreamResponse({
497
- stream: 'cancel_complete',
498
- success: false,
499
- reason: reason
477
+ comObj.postMessage({
478
+ sourceName,
479
+ targetName,
480
+ stream: StreamKind.CANCEL_COMPLETE,
481
+ streamId,
482
+ reason: wrapReason(reason)
500
483
  });
501
484
  });
502
- this.streamSinks[data.streamId].sinkCapability.reject(wrapReason(data.reason));
503
- this.streamSinks[data.streamId].isCancelled = true;
504
- delete this.streamSinks[data.streamId];
485
+ this.streamSinks[streamId].sinkCapability.reject(wrapReason(data.reason));
486
+ this.streamSinks[streamId].isCancelled = true;
487
+ delete this.streamSinks[streamId];
505
488
  break;
506
489
 
507
490
  default:
508
- throw new Error('Unexpected stream case');
491
+ throw new Error("Unexpected stream case");
509
492
  }
510
- },
511
- postMessage: function postMessage(message, transfers) {
493
+ }
494
+
495
+ async _deleteStreamController(streamId) {
496
+ await Promise.allSettled([this.streamControllers[streamId].startCall, this.streamControllers[streamId].pullCall, this.streamControllers[streamId].cancelCall].map(function (capability) {
497
+ return capability && capability.promise;
498
+ }));
499
+ delete this.streamControllers[streamId];
500
+ }
501
+
502
+ _postMessage(message, transfers) {
512
503
  if (transfers && this.postMessageTransfers) {
513
504
  this.comObj.postMessage(message, transfers);
514
505
  } else {
515
506
  this.comObj.postMessage(message);
516
507
  }
517
- },
518
- destroy: function destroy() {
519
- this.comObj.removeEventListener('message', this._onComObjOnMessage);
520
508
  }
521
- };
509
+
510
+ destroy() {
511
+ this.comObj.removeEventListener("message", this._onComObjOnMessage);
512
+ }
513
+
514
+ }
515
+
516
+ exports.MessageHandler = MessageHandler;