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