pdfjs-dist 2.0.550 → 2.3.200

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