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.
@@ -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");
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, this);
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;