pdfjs-dist 2.3.200 → 2.4.456

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