@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 1.0.0

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.
Files changed (172) hide show
  1. package/README.md +1 -1
  2. package/package.json +42 -26
  3. package/{src/display → pdfjs/dist}/display_utils.js +344 -139
  4. package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
  5. package/pdfjs/dist/l10n_utils.js +140 -0
  6. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  7. package/{src/display → pdfjs/dist}/network.js +149 -87
  8. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  9. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  10. package/pdfjs/dist/pdf.js +12778 -0
  11. package/pdfjs/dist/pdf_link_service.js +638 -0
  12. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  13. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  14. package/pdfjs/dist/pdf_viewer.js +3598 -0
  15. package/pdfjs/dist/ui_utils.js +1033 -0
  16. package/{src/shared → pdfjs/dist}/util.js +301 -287
  17. package/pdfjs/dist/worker.js +62813 -0
  18. package/src/vcf-pdf-viewer.js +98 -46
  19. package/theme/lumo/vcf-pdf-viewer-styles.js +4 -4
  20. package/theme/material/vcf-pdf-viewer-styles.js +4 -4
  21. package/theme/material/vcf-pdf-viewer.js +2 -2
  22. package/src/core/.eslintrc +0 -13
  23. package/src/core/annotation.js +0 -2948
  24. package/src/core/arithmetic_decoder.js +0 -182
  25. package/src/core/ascii_85_stream.js +0 -98
  26. package/src/core/ascii_hex_stream.js +0 -79
  27. package/src/core/base_stream.js +0 -110
  28. package/src/core/bidi.js +0 -438
  29. package/src/core/calibri_factors.js +0 -308
  30. package/src/core/catalog.js +0 -1459
  31. package/src/core/ccitt.js +0 -1062
  32. package/src/core/ccitt_stream.js +0 -60
  33. package/src/core/cff_font.js +0 -116
  34. package/src/core/cff_parser.js +0 -1949
  35. package/src/core/charsets.js +0 -119
  36. package/src/core/chunked_stream.js +0 -557
  37. package/src/core/cmap.js +0 -1039
  38. package/src/core/colorspace.js +0 -1533
  39. package/src/core/core_utils.js +0 -464
  40. package/src/core/crypto.js +0 -1900
  41. package/src/core/decode_stream.js +0 -170
  42. package/src/core/decrypt_stream.js +0 -59
  43. package/src/core/default_appearance.js +0 -99
  44. package/src/core/document.js +0 -1456
  45. package/src/core/encodings.js +0 -301
  46. package/src/core/evaluator.js +0 -4601
  47. package/src/core/file_spec.js +0 -108
  48. package/src/core/flate_stream.js +0 -402
  49. package/src/core/font_renderer.js +0 -882
  50. package/src/core/fonts.js +0 -3260
  51. package/src/core/fonts_utils.js +0 -221
  52. package/src/core/function.js +0 -1257
  53. package/src/core/glyf.js +0 -706
  54. package/src/core/glyphlist.js +0 -4558
  55. package/src/core/helvetica_factors.js +0 -353
  56. package/src/core/image.js +0 -802
  57. package/src/core/image_utils.js +0 -291
  58. package/src/core/jbig2.js +0 -2572
  59. package/src/core/jbig2_stream.js +0 -73
  60. package/src/core/jpeg_stream.js +0 -105
  61. package/src/core/jpg.js +0 -1416
  62. package/src/core/jpx.js +0 -2343
  63. package/src/core/jpx_stream.js +0 -87
  64. package/src/core/liberationsans_widths.js +0 -221
  65. package/src/core/lzw_stream.js +0 -150
  66. package/src/core/metadata_parser.js +0 -146
  67. package/src/core/metrics.js +0 -2970
  68. package/src/core/murmurhash3.js +0 -139
  69. package/src/core/myriadpro_factors.js +0 -290
  70. package/src/core/name_number_tree.js +0 -153
  71. package/src/core/object_loader.js +0 -149
  72. package/src/core/opentype_file_builder.js +0 -154
  73. package/src/core/operator_list.js +0 -734
  74. package/src/core/parser.js +0 -1416
  75. package/src/core/pattern.js +0 -985
  76. package/src/core/pdf_manager.js +0 -217
  77. package/src/core/predictor_stream.js +0 -238
  78. package/src/core/primitives.js +0 -402
  79. package/src/core/ps_parser.js +0 -272
  80. package/src/core/run_length_stream.js +0 -61
  81. package/src/core/segoeui_factors.js +0 -308
  82. package/src/core/standard_fonts.js +0 -817
  83. package/src/core/stream.js +0 -103
  84. package/src/core/struct_tree.js +0 -335
  85. package/src/core/to_unicode_map.js +0 -103
  86. package/src/core/type1_font.js +0 -421
  87. package/src/core/type1_parser.js +0 -776
  88. package/src/core/unicode.js +0 -1649
  89. package/src/core/worker.js +0 -848
  90. package/src/core/worker_stream.js +0 -135
  91. package/src/core/writer.js +0 -278
  92. package/src/core/xfa/bind.js +0 -652
  93. package/src/core/xfa/builder.js +0 -207
  94. package/src/core/xfa/config.js +0 -1926
  95. package/src/core/xfa/connection_set.js +0 -202
  96. package/src/core/xfa/data.js +0 -82
  97. package/src/core/xfa/datasets.js +0 -76
  98. package/src/core/xfa/factory.js +0 -111
  99. package/src/core/xfa/fonts.js +0 -181
  100. package/src/core/xfa/formcalc_lexer.js +0 -385
  101. package/src/core/xfa/formcalc_parser.js +0 -1340
  102. package/src/core/xfa/html_utils.js +0 -639
  103. package/src/core/xfa/layout.js +0 -383
  104. package/src/core/xfa/locale_set.js +0 -345
  105. package/src/core/xfa/namespaces.js +0 -81
  106. package/src/core/xfa/parser.js +0 -184
  107. package/src/core/xfa/setup.js +0 -38
  108. package/src/core/xfa/signature.js +0 -40
  109. package/src/core/xfa/som.js +0 -338
  110. package/src/core/xfa/stylesheet.js +0 -40
  111. package/src/core/xfa/template.js +0 -6260
  112. package/src/core/xfa/text.js +0 -290
  113. package/src/core/xfa/unknown.js +0 -29
  114. package/src/core/xfa/utils.js +0 -217
  115. package/src/core/xfa/xdp.js +0 -59
  116. package/src/core/xfa/xfa_object.js +0 -1130
  117. package/src/core/xfa/xhtml.js +0 -543
  118. package/src/core/xfa_fonts.js +0 -208
  119. package/src/core/xml_parser.js +0 -507
  120. package/src/core/xref.js +0 -899
  121. package/src/display/annotation_layer.js +0 -2107
  122. package/src/display/annotation_storage.js +0 -113
  123. package/src/display/api.js +0 -3292
  124. package/src/display/base_factory.js +0 -180
  125. package/src/display/canvas.js +0 -2828
  126. package/src/display/font_loader.js +0 -484
  127. package/src/display/metadata.js +0 -41
  128. package/src/display/network_utils.js +0 -100
  129. package/src/display/node_utils.js +0 -83
  130. package/src/display/optional_content_config.js +0 -189
  131. package/src/display/pattern_helper.js +0 -659
  132. package/src/display/svg.js +0 -1709
  133. package/src/display/text_layer.js +0 -847
  134. package/src/display/transport_stream.js +0 -303
  135. package/src/display/worker_options.js +0 -40
  136. package/src/display/xfa_layer.js +0 -204
  137. package/src/doc_helper.js +0 -25
  138. package/src/images/logo.svg +0 -41
  139. package/src/interfaces.js +0 -169
  140. package/src/license_header.js +0 -14
  141. package/src/license_header_libre.js +0 -21
  142. package/src/pdf.image_decoders.js +0 -46
  143. package/src/pdf.js +0 -146
  144. package/src/pdf.sandbox.external.js +0 -181
  145. package/src/pdf.sandbox.js +0 -151
  146. package/src/pdf.scripting.js +0 -25
  147. package/src/pdf.worker.entry.js +0 -19
  148. package/src/pdf.worker.js +0 -23
  149. package/src/scripting_api/aform.js +0 -608
  150. package/src/scripting_api/app.js +0 -621
  151. package/src/scripting_api/color.js +0 -129
  152. package/src/scripting_api/common.js +0 -58
  153. package/src/scripting_api/console.js +0 -38
  154. package/src/scripting_api/constants.js +0 -208
  155. package/src/scripting_api/doc.js +0 -1195
  156. package/src/scripting_api/error.js +0 -23
  157. package/src/scripting_api/event.js +0 -232
  158. package/src/scripting_api/field.js +0 -620
  159. package/src/scripting_api/fullscreen.js +0 -145
  160. package/src/scripting_api/initialization.js +0 -223
  161. package/src/scripting_api/pdf_object.js +0 -24
  162. package/src/scripting_api/print_params.js +0 -146
  163. package/src/scripting_api/proxy.js +0 -139
  164. package/src/scripting_api/thermometer.js +0 -69
  165. package/src/scripting_api/util.js +0 -581
  166. package/src/shared/.eslintrc +0 -13
  167. package/src/shared/cffStandardStrings.js +0 -311
  168. package/src/shared/compatibility.js +0 -114
  169. package/src/shared/fonts_utils.js +0 -429
  170. package/src/shared/is_node.js +0 -27
  171. package/src/shared/scripting_utils.js +0 -85
  172. package/src/worker_loader.js +0 -32
@@ -1,3 +1,5 @@
1
+ import { c as assert, l as createPromiseCapability, t as UnknownErrorException, v as UnexpectedResponseException, M as MissingPDFException, A as AbortException } from './util.js';
2
+
1
3
  /* Copyright 2018 Mozilla Foundation
2
4
  *
3
5
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,22 +14,11 @@
12
14
  * See the License for the specific language governing permissions and
13
15
  * limitations under the License.
14
16
  */
15
-
16
- import {
17
- AbortException,
18
- assert,
19
- createPromiseCapability,
20
- MissingPDFException,
21
- UnexpectedResponseException,
22
- UnknownErrorException,
23
- } from "./util.js";
24
-
25
17
  const CallbackKind = {
26
18
  UNKNOWN: 0,
27
19
  DATA: 1,
28
- ERROR: 2,
20
+ ERROR: 2
29
21
  };
30
-
31
22
  const StreamKind = {
32
23
  UNKNOWN: 0,
33
24
  CANCEL: 1,
@@ -37,33 +28,31 @@ const StreamKind = {
37
28
  ERROR: 5,
38
29
  PULL: 6,
39
30
  PULL_COMPLETE: 7,
40
- START_COMPLETE: 8,
31
+ START_COMPLETE: 8
41
32
  };
42
33
 
43
34
  function wrapReason(reason) {
44
- if (
45
- typeof PDFJSDev === "undefined" ||
46
- PDFJSDev.test("!PRODUCTION || TESTING")
47
- ) {
48
- assert(
49
- reason instanceof Error ||
50
- (typeof reason === "object" && reason !== null),
51
- 'wrapReason: Expected "reason" to be a (possibly cloned) Error.'
52
- );
35
+ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
36
+ assert(reason instanceof Error || typeof reason === "object" && reason !== null, 'wrapReason: Expected "reason" to be a (possibly cloned) Error.');
53
37
  } else {
54
38
  if (typeof reason !== "object" || reason === null) {
55
39
  return reason;
56
40
  }
57
41
  }
42
+
58
43
  switch (reason.name) {
59
44
  case "AbortException":
60
45
  return new AbortException(reason.message);
46
+
61
47
  case "MissingPDFException":
62
48
  return new MissingPDFException(reason.message);
49
+
63
50
  case "UnexpectedResponseException":
64
51
  return new UnexpectedResponseException(reason.message, reason.status);
52
+
65
53
  case "UnknownErrorException":
66
54
  return new UnknownErrorException(reason.message, reason.details);
55
+
67
56
  default:
68
57
  return new UnknownErrorException(reason.message, reason.toString());
69
58
  }
@@ -84,19 +73,25 @@ class MessageHandler {
84
73
 
85
74
  this._onComObjOnMessage = event => {
86
75
  const data = event.data;
76
+
87
77
  if (data.targetName !== this.sourceName) {
88
78
  return;
89
79
  }
80
+
90
81
  if (data.stream) {
91
82
  this._processStreamMessage(data);
83
+
92
84
  return;
93
85
  }
86
+
94
87
  if (data.callback) {
95
88
  const callbackId = data.callbackId;
96
89
  const capability = this.callbackCapabilities[callbackId];
90
+
97
91
  if (!capability) {
98
92
  throw new Error(`Cannot resolve callback ${callbackId}`);
99
93
  }
94
+
100
95
  delete this.callbackCapabilities[callbackId];
101
96
 
102
97
  if (data.callback === CallbackKind.DATA) {
@@ -106,83 +101,82 @@ class MessageHandler {
106
101
  } else {
107
102
  throw new Error("Unexpected callback case");
108
103
  }
104
+
109
105
  return;
110
106
  }
107
+
111
108
  const action = this.actionHandler[data.action];
109
+
112
110
  if (!action) {
113
111
  throw new Error(`Unknown action from worker: ${data.action}`);
114
112
  }
113
+
115
114
  if (data.callbackId) {
116
115
  const cbSourceName = this.sourceName;
117
116
  const cbTargetName = data.sourceName;
118
117
  new Promise(function (resolve) {
119
118
  resolve(action(data.data));
120
- }).then(
121
- function (result) {
122
- comObj.postMessage({
123
- sourceName: cbSourceName,
124
- targetName: cbTargetName,
125
- callback: CallbackKind.DATA,
126
- callbackId: data.callbackId,
127
- data: result,
128
- });
129
- },
130
- function (reason) {
131
- comObj.postMessage({
132
- sourceName: cbSourceName,
133
- targetName: cbTargetName,
134
- callback: CallbackKind.ERROR,
135
- callbackId: data.callbackId,
136
- reason: wrapReason(reason),
137
- });
138
- }
139
- );
119
+ }).then(function (result) {
120
+ comObj.postMessage({
121
+ sourceName: cbSourceName,
122
+ targetName: cbTargetName,
123
+ callback: CallbackKind.DATA,
124
+ callbackId: data.callbackId,
125
+ data: result
126
+ });
127
+ }, function (reason) {
128
+ comObj.postMessage({
129
+ sourceName: cbSourceName,
130
+ targetName: cbTargetName,
131
+ callback: CallbackKind.ERROR,
132
+ callbackId: data.callbackId,
133
+ reason: wrapReason(reason)
134
+ });
135
+ });
140
136
  return;
141
137
  }
138
+
142
139
  if (data.streamId) {
143
140
  this._createStreamSink(data);
141
+
144
142
  return;
145
143
  }
144
+
146
145
  action(data.data);
147
146
  };
147
+
148
148
  comObj.addEventListener("message", this._onComObjOnMessage);
149
149
  }
150
150
 
151
151
  on(actionName, handler) {
152
- if (
153
- typeof PDFJSDev === "undefined" ||
154
- PDFJSDev.test("!PRODUCTION || TESTING")
155
- ) {
156
- assert(
157
- typeof handler === "function",
158
- 'MessageHandler.on: Expected "handler" to be a function.'
159
- );
152
+ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
153
+ assert(typeof handler === "function", 'MessageHandler.on: Expected "handler" to be a function.');
160
154
  }
155
+
161
156
  const ah = this.actionHandler;
157
+
162
158
  if (ah[actionName]) {
163
159
  throw new Error(`There is already an actionName called "${actionName}"`);
164
160
  }
161
+
165
162
  ah[actionName] = handler;
166
163
  }
167
-
168
164
  /**
169
165
  * Sends a message to the comObj to invoke the action with the supplied data.
170
166
  * @param {string} actionName - Action to call.
171
167
  * @param {JSON} data - JSON data to send.
172
168
  * @param {Array} [transfers] - List of transfers/ArrayBuffers.
173
169
  */
170
+
171
+
174
172
  send(actionName, data, transfers) {
175
- this._postMessage(
176
- {
177
- sourceName: this.sourceName,
178
- targetName: this.targetName,
179
- action: actionName,
180
- data,
181
- },
182
- transfers
183
- );
173
+ this._postMessage({
174
+ sourceName: this.sourceName,
175
+ targetName: this.targetName,
176
+ action: actionName,
177
+ data
178
+ }, transfers);
184
179
  }
185
-
186
180
  /**
187
181
  * Sends a message to the comObj to invoke the action with the supplied data.
188
182
  * Expects that the other side will callback with the response.
@@ -191,27 +185,27 @@ class MessageHandler {
191
185
  * @param {Array} [transfers] - List of transfers/ArrayBuffers.
192
186
  * @returns {Promise} Promise to be resolved with response data.
193
187
  */
188
+
189
+
194
190
  sendWithPromise(actionName, data, transfers) {
195
191
  const callbackId = this.callbackId++;
196
192
  const capability = createPromiseCapability();
197
193
  this.callbackCapabilities[callbackId] = capability;
194
+
198
195
  try {
199
- this._postMessage(
200
- {
201
- sourceName: this.sourceName,
202
- targetName: this.targetName,
203
- action: actionName,
204
- callbackId,
205
- data,
206
- },
207
- transfers
208
- );
196
+ this._postMessage({
197
+ sourceName: this.sourceName,
198
+ targetName: this.targetName,
199
+ action: actionName,
200
+ callbackId,
201
+ data
202
+ }, transfers);
209
203
  } catch (ex) {
210
204
  capability.reject(ex);
211
205
  }
206
+
212
207
  return capability.promise;
213
208
  }
214
-
215
209
  /**
216
210
  * Sends a message to the comObj to invoke the action with the supplied data.
217
211
  * Expect that the other side will callback to signal 'start_complete'.
@@ -222,76 +216,72 @@ class MessageHandler {
222
216
  * @param {Array} [transfers] - List of transfers/ArrayBuffers.
223
217
  * @returns {ReadableStream} ReadableStream to read data in chunks.
224
218
  */
219
+
220
+
225
221
  sendWithStream(actionName, data, queueingStrategy, transfers) {
226
222
  const streamId = this.streamId++;
227
223
  const sourceName = this.sourceName;
228
224
  const targetName = this.targetName;
229
225
  const comObj = this.comObj;
226
+ return new ReadableStream({
227
+ start: controller => {
228
+ const startCapability = createPromiseCapability();
229
+ this.streamControllers[streamId] = {
230
+ controller,
231
+ startCall: startCapability,
232
+ pullCall: null,
233
+ cancelCall: null,
234
+ isClosed: false
235
+ };
230
236
 
231
- return new ReadableStream(
232
- {
233
- start: controller => {
234
- const startCapability = createPromiseCapability();
235
- this.streamControllers[streamId] = {
236
- controller,
237
- startCall: startCapability,
238
- pullCall: null,
239
- cancelCall: null,
240
- isClosed: false,
241
- };
242
- this._postMessage(
243
- {
244
- sourceName,
245
- targetName,
246
- action: actionName,
247
- streamId,
248
- data,
249
- desiredSize: controller.desiredSize,
250
- },
251
- transfers
252
- );
253
- // Return Promise for Async process, to signal success/failure.
254
- return startCapability.promise;
255
- },
256
-
257
- pull: controller => {
258
- const pullCapability = createPromiseCapability();
259
- this.streamControllers[streamId].pullCall = pullCapability;
260
- comObj.postMessage({
261
- sourceName,
262
- targetName,
263
- stream: StreamKind.PULL,
264
- streamId,
265
- desiredSize: controller.desiredSize,
266
- });
267
- // Returning Promise will not call "pull"
268
- // again until current pull is resolved.
269
- return pullCapability.promise;
270
- },
271
-
272
- cancel: reason => {
273
- assert(reason instanceof Error, "cancel must have a valid reason");
274
- const cancelCapability = createPromiseCapability();
275
- this.streamControllers[streamId].cancelCall = cancelCapability;
276
- this.streamControllers[streamId].isClosed = true;
277
- comObj.postMessage({
278
- sourceName,
279
- targetName,
280
- stream: StreamKind.CANCEL,
281
- streamId,
282
- reason: wrapReason(reason),
283
- });
284
- // Return Promise to signal success or failure.
285
- return cancelCapability.promise;
286
- },
237
+ this._postMessage({
238
+ sourceName,
239
+ targetName,
240
+ action: actionName,
241
+ streamId,
242
+ data,
243
+ desiredSize: controller.desiredSize
244
+ }, transfers); // Return Promise for Async process, to signal success/failure.
245
+
246
+
247
+ return startCapability.promise;
287
248
  },
288
- queueingStrategy
289
- );
290
- }
249
+ pull: controller => {
250
+ const pullCapability = createPromiseCapability();
251
+ this.streamControllers[streamId].pullCall = pullCapability;
252
+ comObj.postMessage({
253
+ sourceName,
254
+ targetName,
255
+ stream: StreamKind.PULL,
256
+ streamId,
257
+ desiredSize: controller.desiredSize
258
+ }); // Returning Promise will not call "pull"
259
+ // again until current pull is resolved.
291
260
 
261
+ return pullCapability.promise;
262
+ },
263
+ cancel: reason => {
264
+ assert(reason instanceof Error, "cancel must have a valid reason");
265
+ const cancelCapability = createPromiseCapability();
266
+ this.streamControllers[streamId].cancelCall = cancelCapability;
267
+ this.streamControllers[streamId].isClosed = true;
268
+ comObj.postMessage({
269
+ sourceName,
270
+ targetName,
271
+ stream: StreamKind.CANCEL,
272
+ streamId,
273
+ reason: wrapReason(reason)
274
+ }); // Return Promise to signal success or failure.
275
+
276
+ return cancelCapability.promise;
277
+ }
278
+ }, queueingStrategy);
279
+ }
292
280
  /**
293
281
  * @private
294
282
  */
283
+
284
+
295
285
  _createStreamSink(data) {
296
286
  const self = this;
297
287
  const action = this.actionHandler[data.action];
@@ -299,59 +289,60 @@ class MessageHandler {
299
289
  const sourceName = this.sourceName;
300
290
  const targetName = data.sourceName;
301
291
  const comObj = this.comObj;
302
-
303
292
  const streamSink = {
304
293
  enqueue(chunk, size = 1, transfers) {
305
294
  if (this.isCancelled) {
306
295
  return;
307
296
  }
297
+
308
298
  const lastDesiredSize = this.desiredSize;
309
- this.desiredSize -= size;
310
- // Enqueue decreases the desiredSize property of sink,
299
+ this.desiredSize -= size; // Enqueue decreases the desiredSize property of sink,
311
300
  // so when it changes from positive to negative,
312
301
  // set ready as unresolved promise.
302
+
313
303
  if (lastDesiredSize > 0 && this.desiredSize <= 0) {
314
304
  this.sinkCapability = createPromiseCapability();
315
305
  this.ready = this.sinkCapability.promise;
316
306
  }
317
- self._postMessage(
318
- {
319
- sourceName,
320
- targetName,
321
- stream: StreamKind.ENQUEUE,
322
- streamId,
323
- chunk,
324
- },
325
- transfers
326
- );
307
+
308
+ self._postMessage({
309
+ sourceName,
310
+ targetName,
311
+ stream: StreamKind.ENQUEUE,
312
+ streamId,
313
+ chunk
314
+ }, transfers);
327
315
  },
328
316
 
329
317
  close() {
330
318
  if (this.isCancelled) {
331
319
  return;
332
320
  }
321
+
333
322
  this.isCancelled = true;
334
323
  comObj.postMessage({
335
324
  sourceName,
336
325
  targetName,
337
326
  stream: StreamKind.CLOSE,
338
- streamId,
327
+ streamId
339
328
  });
340
329
  delete self.streamSinks[streamId];
341
330
  },
342
331
 
343
332
  error(reason) {
344
333
  assert(reason instanceof Error, "error must have a valid reason");
334
+
345
335
  if (this.isCancelled) {
346
336
  return;
347
337
  }
338
+
348
339
  this.isCancelled = true;
349
340
  comObj.postMessage({
350
341
  sourceName,
351
342
  targetName,
352
343
  stream: StreamKind.ERROR,
353
344
  streamId,
354
- reason: wrapReason(reason),
345
+ reason: wrapReason(reason)
355
346
  });
356
347
  },
357
348
 
@@ -360,39 +351,36 @@ class MessageHandler {
360
351
  onCancel: null,
361
352
  isCancelled: false,
362
353
  desiredSize: data.desiredSize,
363
- ready: null,
354
+ ready: null
364
355
  };
365
-
366
356
  streamSink.sinkCapability.resolve();
367
357
  streamSink.ready = streamSink.sinkCapability.promise;
368
358
  this.streamSinks[streamId] = streamSink;
369
359
  new Promise(function (resolve) {
370
360
  resolve(action(data.data, streamSink));
371
- }).then(
372
- function () {
373
- comObj.postMessage({
374
- sourceName,
375
- targetName,
376
- stream: StreamKind.START_COMPLETE,
377
- streamId,
378
- success: true,
379
- });
380
- },
381
- function (reason) {
382
- comObj.postMessage({
383
- sourceName,
384
- targetName,
385
- stream: StreamKind.START_COMPLETE,
386
- streamId,
387
- reason: wrapReason(reason),
388
- });
389
- }
390
- );
361
+ }).then(function () {
362
+ comObj.postMessage({
363
+ sourceName,
364
+ targetName,
365
+ stream: StreamKind.START_COMPLETE,
366
+ streamId,
367
+ success: true
368
+ });
369
+ }, function (reason) {
370
+ comObj.postMessage({
371
+ sourceName,
372
+ targetName,
373
+ stream: StreamKind.START_COMPLETE,
374
+ streamId,
375
+ reason: wrapReason(reason)
376
+ });
377
+ });
391
378
  }
392
-
393
379
  /**
394
380
  * @private
395
381
  */
382
+
383
+
396
384
  _processStreamMessage(data) {
397
385
  const streamId = data.streamId;
398
386
  const sourceName = this.sourceName;
@@ -404,20 +392,20 @@ class MessageHandler {
404
392
  if (data.success) {
405
393
  this.streamControllers[streamId].startCall.resolve();
406
394
  } else {
407
- this.streamControllers[streamId].startCall.reject(
408
- wrapReason(data.reason)
409
- );
395
+ this.streamControllers[streamId].startCall.reject(wrapReason(data.reason));
410
396
  }
397
+
411
398
  break;
399
+
412
400
  case StreamKind.PULL_COMPLETE:
413
401
  if (data.success) {
414
402
  this.streamControllers[streamId].pullCall.resolve();
415
403
  } else {
416
- this.streamControllers[streamId].pullCall.reject(
417
- wrapReason(data.reason)
418
- );
404
+ this.streamControllers[streamId].pullCall.reject(wrapReason(data.reason));
419
405
  }
406
+
420
407
  break;
408
+
421
409
  case StreamKind.PULL:
422
410
  // Ignore any pull after close is called.
423
411
  if (!this.streamSinks[streamId]) {
@@ -426,149 +414,144 @@ class MessageHandler {
426
414
  targetName,
427
415
  stream: StreamKind.PULL_COMPLETE,
428
416
  streamId,
429
- success: true,
417
+ success: true
430
418
  });
431
419
  break;
432
- }
433
- // Pull increases the desiredSize property of sink,
420
+ } // Pull increases the desiredSize property of sink,
434
421
  // so when it changes from negative to positive,
435
422
  // set ready property as resolved promise.
436
- if (
437
- this.streamSinks[streamId].desiredSize <= 0 &&
438
- data.desiredSize > 0
439
- ) {
423
+
424
+
425
+ if (this.streamSinks[streamId].desiredSize <= 0 && data.desiredSize > 0) {
440
426
  this.streamSinks[streamId].sinkCapability.resolve();
441
- }
442
- // Reset desiredSize property of sink on every pull.
427
+ } // Reset desiredSize property of sink on every pull.
428
+
429
+
443
430
  this.streamSinks[streamId].desiredSize = data.desiredSize;
444
- const { onPull } = this.streamSinks[data.streamId];
431
+ const {
432
+ onPull
433
+ } = this.streamSinks[data.streamId];
445
434
  new Promise(function (resolve) {
446
435
  resolve(onPull && onPull());
447
- }).then(
448
- function () {
449
- comObj.postMessage({
450
- sourceName,
451
- targetName,
452
- stream: StreamKind.PULL_COMPLETE,
453
- streamId,
454
- success: true,
455
- });
456
- },
457
- function (reason) {
458
- comObj.postMessage({
459
- sourceName,
460
- targetName,
461
- stream: StreamKind.PULL_COMPLETE,
462
- streamId,
463
- reason: wrapReason(reason),
464
- });
465
- }
466
- );
436
+ }).then(function () {
437
+ comObj.postMessage({
438
+ sourceName,
439
+ targetName,
440
+ stream: StreamKind.PULL_COMPLETE,
441
+ streamId,
442
+ success: true
443
+ });
444
+ }, function (reason) {
445
+ comObj.postMessage({
446
+ sourceName,
447
+ targetName,
448
+ stream: StreamKind.PULL_COMPLETE,
449
+ streamId,
450
+ reason: wrapReason(reason)
451
+ });
452
+ });
467
453
  break;
454
+
468
455
  case StreamKind.ENQUEUE:
469
- assert(
470
- this.streamControllers[streamId],
471
- "enqueue should have stream controller"
472
- );
456
+ assert(this.streamControllers[streamId], "enqueue should have stream controller");
457
+
473
458
  if (this.streamControllers[streamId].isClosed) {
474
459
  break;
475
460
  }
461
+
476
462
  this.streamControllers[streamId].controller.enqueue(data.chunk);
477
463
  break;
464
+
478
465
  case StreamKind.CLOSE:
479
- assert(
480
- this.streamControllers[streamId],
481
- "close should have stream controller"
482
- );
466
+ assert(this.streamControllers[streamId], "close should have stream controller");
467
+
483
468
  if (this.streamControllers[streamId].isClosed) {
484
469
  break;
485
470
  }
471
+
486
472
  this.streamControllers[streamId].isClosed = true;
487
473
  this.streamControllers[streamId].controller.close();
474
+
488
475
  this._deleteStreamController(streamId);
476
+
489
477
  break;
478
+
490
479
  case StreamKind.ERROR:
491
- assert(
492
- this.streamControllers[streamId],
493
- "error should have stream controller"
494
- );
495
- this.streamControllers[streamId].controller.error(
496
- wrapReason(data.reason)
497
- );
480
+ assert(this.streamControllers[streamId], "error should have stream controller");
481
+ this.streamControllers[streamId].controller.error(wrapReason(data.reason));
482
+
498
483
  this._deleteStreamController(streamId);
484
+
499
485
  break;
486
+
500
487
  case StreamKind.CANCEL_COMPLETE:
501
488
  if (data.success) {
502
489
  this.streamControllers[streamId].cancelCall.resolve();
503
490
  } else {
504
- this.streamControllers[streamId].cancelCall.reject(
505
- wrapReason(data.reason)
506
- );
491
+ this.streamControllers[streamId].cancelCall.reject(wrapReason(data.reason));
507
492
  }
493
+
508
494
  this._deleteStreamController(streamId);
495
+
509
496
  break;
497
+
510
498
  case StreamKind.CANCEL:
511
499
  if (!this.streamSinks[streamId]) {
512
500
  break;
513
501
  }
514
- const { onCancel } = this.streamSinks[data.streamId];
502
+
503
+ const {
504
+ onCancel
505
+ } = this.streamSinks[data.streamId];
515
506
  new Promise(function (resolve) {
516
507
  resolve(onCancel && onCancel(wrapReason(data.reason)));
517
- }).then(
518
- function () {
519
- comObj.postMessage({
520
- sourceName,
521
- targetName,
522
- stream: StreamKind.CANCEL_COMPLETE,
523
- streamId,
524
- success: true,
525
- });
526
- },
527
- function (reason) {
528
- comObj.postMessage({
529
- sourceName,
530
- targetName,
531
- stream: StreamKind.CANCEL_COMPLETE,
532
- streamId,
533
- reason: wrapReason(reason),
534
- });
535
- }
536
- );
537
- this.streamSinks[streamId].sinkCapability.reject(
538
- wrapReason(data.reason)
539
- );
508
+ }).then(function () {
509
+ comObj.postMessage({
510
+ sourceName,
511
+ targetName,
512
+ stream: StreamKind.CANCEL_COMPLETE,
513
+ streamId,
514
+ success: true
515
+ });
516
+ }, function (reason) {
517
+ comObj.postMessage({
518
+ sourceName,
519
+ targetName,
520
+ stream: StreamKind.CANCEL_COMPLETE,
521
+ streamId,
522
+ reason: wrapReason(reason)
523
+ });
524
+ });
525
+ this.streamSinks[streamId].sinkCapability.reject(wrapReason(data.reason));
540
526
  this.streamSinks[streamId].isCancelled = true;
541
527
  delete this.streamSinks[streamId];
542
528
  break;
529
+
543
530
  default:
544
531
  throw new Error("Unexpected stream case");
545
532
  }
546
533
  }
547
-
548
534
  /**
549
535
  * @private
550
536
  */
537
+
538
+
551
539
  async _deleteStreamController(streamId) {
552
540
  // Delete the `streamController` only when the start, pull, and cancel
553
541
  // capabilities have settled, to prevent `TypeError`s.
554
- await Promise.allSettled(
555
- [
556
- this.streamControllers[streamId].startCall,
557
- this.streamControllers[streamId].pullCall,
558
- this.streamControllers[streamId].cancelCall,
559
- ].map(function (capability) {
560
- return capability && capability.promise;
561
- })
562
- );
542
+ await Promise.allSettled([this.streamControllers[streamId].startCall, this.streamControllers[streamId].pullCall, this.streamControllers[streamId].cancelCall].map(function (capability) {
543
+ return capability && capability.promise;
544
+ }));
563
545
  delete this.streamControllers[streamId];
564
546
  }
565
-
566
547
  /**
567
548
  * Sends raw message to the comObj.
568
549
  * @param {Object} message - Raw message.
569
550
  * @param transfers List of transfers/ArrayBuffers, or undefined.
570
551
  * @private
571
552
  */
553
+
554
+
572
555
  _postMessage(message, transfers) {
573
556
  if (transfers && this.postMessageTransfers) {
574
557
  this.comObj.postMessage(message, transfers);
@@ -580,6 +563,7 @@ class MessageHandler {
580
563
  destroy() {
581
564
  this.comObj.removeEventListener("message", this._onComObjOnMessage);
582
565
  }
566
+
583
567
  }
584
568
 
585
- export { MessageHandler };
569
+ export { MessageHandler as M };