@vaadin-component-factory/vcf-pdf-viewer 3.1.0 → 4.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.
- package/README.md +2 -1
- package/package.json +56 -52
- package/pdfjs/dist/display_utils.js +609 -714
- package/pdfjs/dist/fetch_stream.js +239 -293
- package/pdfjs/dist/l10n_utils.js +113 -122
- package/pdfjs/dist/message_handler.js +451 -524
- package/pdfjs/dist/network.js +441 -552
- package/pdfjs/dist/network_utils.js +262 -309
- package/pdfjs/dist/node_stream.js +383 -481
- package/pdfjs/dist/pdf.js +9883 -11687
- package/pdfjs/dist/pdf_link_service.js +440 -534
- package/pdfjs/dist/pdf_rendering_queue.js +134 -154
- package/pdfjs/dist/pdf_thumbnail_viewer.js +620 -738
- package/pdfjs/dist/pdf_viewer.js +2667 -3207
- package/pdfjs/dist/ui_utils.js +762 -881
- package/pdfjs/dist/util.js +867 -991
- package/pdfjs/dist/worker.js +51434 -60846
- package/src/vcf-pdf-viewer.js +999 -886
- package/theme/base/vcf-pdf-viewer-styles.js +144 -0
- package/theme/base/vcf-pdf-viewer-toolbar-icons-styles.js +39 -0
- package/theme/base/vcf-pdf-viewer.js +3 -0
- package/vcf-pdf-viewer.js +1 -1
- package/theme/lumo/vcf-pdf-viewer-styles.js +0 -153
- package/theme/lumo/vcf-pdf-viewer-toolbar-icons-styles.js +0 -35
- package/theme/lumo/vcf-pdf-viewer.js +0 -3
- package/theme/material/baseline-keyboard_arrow_down-24px.svg +0 -1
- package/theme/material/baseline-keyboard_arrow_up-24px.svg +0 -1
- package/theme/material/vcf-pdf-viewer-styles.js +0 -123
- package/theme/material/vcf-pdf-viewer.js +0 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { d as assert, c as createPromiseCapability, t as UnknownErrorException, v as UnexpectedResponseException, M as MissingPDFException, A as AbortException } from './util.js';
|
|
2
|
+
|
|
3
3
|
/* Copyright 2018 Mozilla Foundation
|
|
4
4
|
*
|
|
5
5
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -13,170 +13,142 @@ import { c as assert, l as createPromiseCapability, t as UnknownErrorException,
|
|
|
13
13
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
DATA: 1,
|
|
20
|
-
ERROR: 2
|
|
21
|
-
};
|
|
22
|
-
const StreamKind = {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
case "
|
|
48
|
-
return new
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
65
|
-
this.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (data.streamId) {
|
|
140
|
-
this._createStreamSink(data);
|
|
141
|
-
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
action(data.data);
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
comObj.addEventListener("message", this._onComObjOnMessage);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
on(actionName, handler) {
|
|
152
|
-
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
|
|
153
|
-
assert(typeof handler === "function", 'MessageHandler.on: Expected "handler" to be a function.');
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const ah = this.actionHandler;
|
|
157
|
-
|
|
158
|
-
if (ah[actionName]) {
|
|
159
|
-
throw new Error(`There is already an actionName called "${actionName}"`);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
ah[actionName] = handler;
|
|
163
|
-
}
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const CallbackKind = {
|
|
19
|
+
DATA: 1,
|
|
20
|
+
ERROR: 2
|
|
21
|
+
};
|
|
22
|
+
const StreamKind = {
|
|
23
|
+
CANCEL: 1,
|
|
24
|
+
CANCEL_COMPLETE: 2,
|
|
25
|
+
CLOSE: 3,
|
|
26
|
+
ENQUEUE: 4,
|
|
27
|
+
ERROR: 5,
|
|
28
|
+
PULL: 6,
|
|
29
|
+
PULL_COMPLETE: 7,
|
|
30
|
+
START_COMPLETE: 8
|
|
31
|
+
};
|
|
32
|
+
function wrapReason(reason) {
|
|
33
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
|
|
34
|
+
assert(reason instanceof Error || typeof reason === "object" && reason !== null, 'wrapReason: Expected "reason" to be a (possibly cloned) Error.');
|
|
35
|
+
} else {
|
|
36
|
+
if (typeof reason !== "object" || reason === null) {
|
|
37
|
+
return reason;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
switch (reason.name) {
|
|
41
|
+
case "AbortException":
|
|
42
|
+
return new AbortException(reason.message);
|
|
43
|
+
case "MissingPDFException":
|
|
44
|
+
return new MissingPDFException(reason.message);
|
|
45
|
+
case "UnexpectedResponseException":
|
|
46
|
+
return new UnexpectedResponseException(reason.message, reason.status);
|
|
47
|
+
case "UnknownErrorException":
|
|
48
|
+
return new UnknownErrorException(reason.message, reason.details);
|
|
49
|
+
default:
|
|
50
|
+
return new UnknownErrorException(reason.message, reason.toString());
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class MessageHandler {
|
|
54
|
+
constructor(sourceName, targetName, comObj) {
|
|
55
|
+
this.sourceName = sourceName;
|
|
56
|
+
this.targetName = targetName;
|
|
57
|
+
this.comObj = comObj;
|
|
58
|
+
this.callbackId = 1;
|
|
59
|
+
this.streamId = 1;
|
|
60
|
+
this.postMessageTransfers = true;
|
|
61
|
+
this.streamSinks = Object.create(null);
|
|
62
|
+
this.streamControllers = Object.create(null);
|
|
63
|
+
this.callbackCapabilities = Object.create(null);
|
|
64
|
+
this.actionHandler = Object.create(null);
|
|
65
|
+
this._onComObjOnMessage = event => {
|
|
66
|
+
const data = event.data;
|
|
67
|
+
if (data.targetName !== this.sourceName) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (data.stream) {
|
|
71
|
+
this._processStreamMessage(data);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (data.callback) {
|
|
75
|
+
const callbackId = data.callbackId;
|
|
76
|
+
const capability = this.callbackCapabilities[callbackId];
|
|
77
|
+
if (!capability) {
|
|
78
|
+
throw new Error(`Cannot resolve callback ${callbackId}`);
|
|
79
|
+
}
|
|
80
|
+
delete this.callbackCapabilities[callbackId];
|
|
81
|
+
if (data.callback === CallbackKind.DATA) {
|
|
82
|
+
capability.resolve(data.data);
|
|
83
|
+
} else if (data.callback === CallbackKind.ERROR) {
|
|
84
|
+
capability.reject(wrapReason(data.reason));
|
|
85
|
+
} else {
|
|
86
|
+
throw new Error("Unexpected callback case");
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const action = this.actionHandler[data.action];
|
|
91
|
+
if (!action) {
|
|
92
|
+
throw new Error(`Unknown action from worker: ${data.action}`);
|
|
93
|
+
}
|
|
94
|
+
if (data.callbackId) {
|
|
95
|
+
const cbSourceName = this.sourceName;
|
|
96
|
+
const cbTargetName = data.sourceName;
|
|
97
|
+
new Promise(function (resolve) {
|
|
98
|
+
resolve(action(data.data));
|
|
99
|
+
}).then(function (result) {
|
|
100
|
+
comObj.postMessage({
|
|
101
|
+
sourceName: cbSourceName,
|
|
102
|
+
targetName: cbTargetName,
|
|
103
|
+
callback: CallbackKind.DATA,
|
|
104
|
+
callbackId: data.callbackId,
|
|
105
|
+
data: result
|
|
106
|
+
});
|
|
107
|
+
}, function (reason) {
|
|
108
|
+
comObj.postMessage({
|
|
109
|
+
sourceName: cbSourceName,
|
|
110
|
+
targetName: cbTargetName,
|
|
111
|
+
callback: CallbackKind.ERROR,
|
|
112
|
+
callbackId: data.callbackId,
|
|
113
|
+
reason: wrapReason(reason)
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (data.streamId) {
|
|
119
|
+
this._createStreamSink(data);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
action(data.data);
|
|
123
|
+
};
|
|
124
|
+
comObj.addEventListener("message", this._onComObjOnMessage);
|
|
125
|
+
}
|
|
126
|
+
on(actionName, handler) {
|
|
127
|
+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")) {
|
|
128
|
+
assert(typeof handler === "function", 'MessageHandler.on: Expected "handler" to be a function.');
|
|
129
|
+
}
|
|
130
|
+
const ah = this.actionHandler;
|
|
131
|
+
if (ah[actionName]) {
|
|
132
|
+
throw new Error(`There is already an actionName called "${actionName}"`);
|
|
133
|
+
}
|
|
134
|
+
ah[actionName] = handler;
|
|
135
|
+
}
|
|
136
|
+
|
|
164
137
|
/**
|
|
165
138
|
* Sends a message to the comObj to invoke the action with the supplied data.
|
|
166
139
|
* @param {string} actionName - Action to call.
|
|
167
140
|
* @param {JSON} data - JSON data to send.
|
|
168
141
|
* @param {Array} [transfers] - List of transfers/ArrayBuffers.
|
|
169
|
-
*/
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
142
|
+
*/
|
|
143
|
+
send(actionName, data, transfers) {
|
|
144
|
+
this._postMessage({
|
|
145
|
+
sourceName: this.sourceName,
|
|
146
|
+
targetName: this.targetName,
|
|
147
|
+
action: actionName,
|
|
148
|
+
data
|
|
149
|
+
}, transfers);
|
|
150
|
+
}
|
|
151
|
+
|
|
180
152
|
/**
|
|
181
153
|
* Sends a message to the comObj to invoke the action with the supplied data.
|
|
182
154
|
* Expects that the other side will callback with the response.
|
|
@@ -184,28 +156,25 @@ class MessageHandler {
|
|
|
184
156
|
* @param {JSON} data - JSON data to send.
|
|
185
157
|
* @param {Array} [transfers] - List of transfers/ArrayBuffers.
|
|
186
158
|
* @returns {Promise} Promise to be resolved with response data.
|
|
187
|
-
*/
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return capability.promise;
|
|
208
|
-
}
|
|
159
|
+
*/
|
|
160
|
+
sendWithPromise(actionName, data, transfers) {
|
|
161
|
+
const callbackId = this.callbackId++;
|
|
162
|
+
const capability = createPromiseCapability();
|
|
163
|
+
this.callbackCapabilities[callbackId] = capability;
|
|
164
|
+
try {
|
|
165
|
+
this._postMessage({
|
|
166
|
+
sourceName: this.sourceName,
|
|
167
|
+
targetName: this.targetName,
|
|
168
|
+
action: actionName,
|
|
169
|
+
callbackId,
|
|
170
|
+
data
|
|
171
|
+
}, transfers);
|
|
172
|
+
} catch (ex) {
|
|
173
|
+
capability.reject(ex);
|
|
174
|
+
}
|
|
175
|
+
return capability.promise;
|
|
176
|
+
}
|
|
177
|
+
|
|
209
178
|
/**
|
|
210
179
|
* Sends a message to the comObj to invoke the action with the supplied data.
|
|
211
180
|
* Expect that the other side will callback to signal 'start_complete'.
|
|
@@ -215,355 +184,313 @@ class MessageHandler {
|
|
|
215
184
|
* internal queue.
|
|
216
185
|
* @param {Array} [transfers] - List of transfers/ArrayBuffers.
|
|
217
186
|
* @returns {ReadableStream} ReadableStream to read data in chunks.
|
|
218
|
-
*/
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
}, queueingStrategy);
|
|
279
|
-
}
|
|
187
|
+
*/
|
|
188
|
+
sendWithStream(actionName, data, queueingStrategy, transfers) {
|
|
189
|
+
const streamId = this.streamId++;
|
|
190
|
+
const sourceName = this.sourceName;
|
|
191
|
+
const targetName = this.targetName;
|
|
192
|
+
const comObj = this.comObj;
|
|
193
|
+
return new ReadableStream({
|
|
194
|
+
start: controller => {
|
|
195
|
+
const startCapability = createPromiseCapability();
|
|
196
|
+
this.streamControllers[streamId] = {
|
|
197
|
+
controller,
|
|
198
|
+
startCall: startCapability,
|
|
199
|
+
pullCall: null,
|
|
200
|
+
cancelCall: null,
|
|
201
|
+
isClosed: false
|
|
202
|
+
};
|
|
203
|
+
this._postMessage({
|
|
204
|
+
sourceName,
|
|
205
|
+
targetName,
|
|
206
|
+
action: actionName,
|
|
207
|
+
streamId,
|
|
208
|
+
data,
|
|
209
|
+
desiredSize: controller.desiredSize
|
|
210
|
+
}, transfers);
|
|
211
|
+
// Return Promise for Async process, to signal success/failure.
|
|
212
|
+
return startCapability.promise;
|
|
213
|
+
},
|
|
214
|
+
pull: controller => {
|
|
215
|
+
const pullCapability = createPromiseCapability();
|
|
216
|
+
this.streamControllers[streamId].pullCall = pullCapability;
|
|
217
|
+
comObj.postMessage({
|
|
218
|
+
sourceName,
|
|
219
|
+
targetName,
|
|
220
|
+
stream: StreamKind.PULL,
|
|
221
|
+
streamId,
|
|
222
|
+
desiredSize: controller.desiredSize
|
|
223
|
+
});
|
|
224
|
+
// Returning Promise will not call "pull"
|
|
225
|
+
// again until current pull is resolved.
|
|
226
|
+
return pullCapability.promise;
|
|
227
|
+
},
|
|
228
|
+
cancel: reason => {
|
|
229
|
+
assert(reason instanceof Error, "cancel must have a valid reason");
|
|
230
|
+
const cancelCapability = createPromiseCapability();
|
|
231
|
+
this.streamControllers[streamId].cancelCall = cancelCapability;
|
|
232
|
+
this.streamControllers[streamId].isClosed = true;
|
|
233
|
+
comObj.postMessage({
|
|
234
|
+
sourceName,
|
|
235
|
+
targetName,
|
|
236
|
+
stream: StreamKind.CANCEL,
|
|
237
|
+
streamId,
|
|
238
|
+
reason: wrapReason(reason)
|
|
239
|
+
});
|
|
240
|
+
// Return Promise to signal success or failure.
|
|
241
|
+
return cancelCapability.promise;
|
|
242
|
+
}
|
|
243
|
+
}, queueingStrategy);
|
|
244
|
+
}
|
|
245
|
+
|
|
280
246
|
/**
|
|
281
247
|
* @private
|
|
282
|
-
*/
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
const
|
|
288
|
-
const
|
|
289
|
-
const
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
comObj.postMessage({
|
|
371
|
-
sourceName,
|
|
372
|
-
targetName,
|
|
373
|
-
stream: StreamKind.START_COMPLETE,
|
|
374
|
-
streamId,
|
|
375
|
-
reason: wrapReason(reason)
|
|
376
|
-
});
|
|
377
|
-
});
|
|
378
|
-
}
|
|
248
|
+
*/
|
|
249
|
+
_createStreamSink(data) {
|
|
250
|
+
const self = this;
|
|
251
|
+
const action = this.actionHandler[data.action];
|
|
252
|
+
const streamId = data.streamId;
|
|
253
|
+
const sourceName = this.sourceName;
|
|
254
|
+
const targetName = data.sourceName;
|
|
255
|
+
const comObj = this.comObj;
|
|
256
|
+
const streamSink = {
|
|
257
|
+
enqueue(chunk, size = 1, transfers) {
|
|
258
|
+
if (this.isCancelled) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const lastDesiredSize = this.desiredSize;
|
|
262
|
+
this.desiredSize -= size;
|
|
263
|
+
// Enqueue decreases the desiredSize property of sink,
|
|
264
|
+
// so when it changes from positive to negative,
|
|
265
|
+
// set ready as unresolved promise.
|
|
266
|
+
if (lastDesiredSize > 0 && this.desiredSize <= 0) {
|
|
267
|
+
this.sinkCapability = createPromiseCapability();
|
|
268
|
+
this.ready = this.sinkCapability.promise;
|
|
269
|
+
}
|
|
270
|
+
self._postMessage({
|
|
271
|
+
sourceName,
|
|
272
|
+
targetName,
|
|
273
|
+
stream: StreamKind.ENQUEUE,
|
|
274
|
+
streamId,
|
|
275
|
+
chunk
|
|
276
|
+
}, transfers);
|
|
277
|
+
},
|
|
278
|
+
close() {
|
|
279
|
+
if (this.isCancelled) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
this.isCancelled = true;
|
|
283
|
+
comObj.postMessage({
|
|
284
|
+
sourceName,
|
|
285
|
+
targetName,
|
|
286
|
+
stream: StreamKind.CLOSE,
|
|
287
|
+
streamId
|
|
288
|
+
});
|
|
289
|
+
delete self.streamSinks[streamId];
|
|
290
|
+
},
|
|
291
|
+
error(reason) {
|
|
292
|
+
assert(reason instanceof Error, "error must have a valid reason");
|
|
293
|
+
if (this.isCancelled) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
this.isCancelled = true;
|
|
297
|
+
comObj.postMessage({
|
|
298
|
+
sourceName,
|
|
299
|
+
targetName,
|
|
300
|
+
stream: StreamKind.ERROR,
|
|
301
|
+
streamId,
|
|
302
|
+
reason: wrapReason(reason)
|
|
303
|
+
});
|
|
304
|
+
},
|
|
305
|
+
sinkCapability: createPromiseCapability(),
|
|
306
|
+
onPull: null,
|
|
307
|
+
onCancel: null,
|
|
308
|
+
isCancelled: false,
|
|
309
|
+
desiredSize: data.desiredSize,
|
|
310
|
+
ready: null
|
|
311
|
+
};
|
|
312
|
+
streamSink.sinkCapability.resolve();
|
|
313
|
+
streamSink.ready = streamSink.sinkCapability.promise;
|
|
314
|
+
this.streamSinks[streamId] = streamSink;
|
|
315
|
+
new Promise(function (resolve) {
|
|
316
|
+
resolve(action(data.data, streamSink));
|
|
317
|
+
}).then(function () {
|
|
318
|
+
comObj.postMessage({
|
|
319
|
+
sourceName,
|
|
320
|
+
targetName,
|
|
321
|
+
stream: StreamKind.START_COMPLETE,
|
|
322
|
+
streamId,
|
|
323
|
+
success: true
|
|
324
|
+
});
|
|
325
|
+
}, function (reason) {
|
|
326
|
+
comObj.postMessage({
|
|
327
|
+
sourceName,
|
|
328
|
+
targetName,
|
|
329
|
+
stream: StreamKind.START_COMPLETE,
|
|
330
|
+
streamId,
|
|
331
|
+
reason: wrapReason(reason)
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
379
336
|
/**
|
|
380
337
|
* @private
|
|
381
|
-
*/
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
const
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
this.streamControllers[streamId]
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
this.
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
this.
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
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));
|
|
526
|
-
this.streamSinks[streamId].isCancelled = true;
|
|
527
|
-
delete this.streamSinks[streamId];
|
|
528
|
-
break;
|
|
529
|
-
|
|
530
|
-
default:
|
|
531
|
-
throw new Error("Unexpected stream case");
|
|
532
|
-
}
|
|
533
|
-
}
|
|
338
|
+
*/
|
|
339
|
+
_processStreamMessage(data) {
|
|
340
|
+
const streamId = data.streamId;
|
|
341
|
+
const sourceName = this.sourceName;
|
|
342
|
+
const targetName = data.sourceName;
|
|
343
|
+
const comObj = this.comObj;
|
|
344
|
+
switch (data.stream) {
|
|
345
|
+
case StreamKind.START_COMPLETE:
|
|
346
|
+
if (data.success) {
|
|
347
|
+
this.streamControllers[streamId].startCall.resolve();
|
|
348
|
+
} else {
|
|
349
|
+
this.streamControllers[streamId].startCall.reject(wrapReason(data.reason));
|
|
350
|
+
}
|
|
351
|
+
break;
|
|
352
|
+
case StreamKind.PULL_COMPLETE:
|
|
353
|
+
if (data.success) {
|
|
354
|
+
this.streamControllers[streamId].pullCall.resolve();
|
|
355
|
+
} else {
|
|
356
|
+
this.streamControllers[streamId].pullCall.reject(wrapReason(data.reason));
|
|
357
|
+
}
|
|
358
|
+
break;
|
|
359
|
+
case StreamKind.PULL:
|
|
360
|
+
// Ignore any pull after close is called.
|
|
361
|
+
if (!this.streamSinks[streamId]) {
|
|
362
|
+
comObj.postMessage({
|
|
363
|
+
sourceName,
|
|
364
|
+
targetName,
|
|
365
|
+
stream: StreamKind.PULL_COMPLETE,
|
|
366
|
+
streamId,
|
|
367
|
+
success: true
|
|
368
|
+
});
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
// Pull increases the desiredSize property of sink,
|
|
372
|
+
// so when it changes from negative to positive,
|
|
373
|
+
// set ready property as resolved promise.
|
|
374
|
+
if (this.streamSinks[streamId].desiredSize <= 0 && data.desiredSize > 0) {
|
|
375
|
+
this.streamSinks[streamId].sinkCapability.resolve();
|
|
376
|
+
}
|
|
377
|
+
// Reset desiredSize property of sink on every pull.
|
|
378
|
+
this.streamSinks[streamId].desiredSize = data.desiredSize;
|
|
379
|
+
const {
|
|
380
|
+
onPull
|
|
381
|
+
} = this.streamSinks[data.streamId];
|
|
382
|
+
new Promise(function (resolve) {
|
|
383
|
+
resolve(onPull && onPull());
|
|
384
|
+
}).then(function () {
|
|
385
|
+
comObj.postMessage({
|
|
386
|
+
sourceName,
|
|
387
|
+
targetName,
|
|
388
|
+
stream: StreamKind.PULL_COMPLETE,
|
|
389
|
+
streamId,
|
|
390
|
+
success: true
|
|
391
|
+
});
|
|
392
|
+
}, function (reason) {
|
|
393
|
+
comObj.postMessage({
|
|
394
|
+
sourceName,
|
|
395
|
+
targetName,
|
|
396
|
+
stream: StreamKind.PULL_COMPLETE,
|
|
397
|
+
streamId,
|
|
398
|
+
reason: wrapReason(reason)
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
break;
|
|
402
|
+
case StreamKind.ENQUEUE:
|
|
403
|
+
assert(this.streamControllers[streamId], "enqueue should have stream controller");
|
|
404
|
+
if (this.streamControllers[streamId].isClosed) {
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
this.streamControllers[streamId].controller.enqueue(data.chunk);
|
|
408
|
+
break;
|
|
409
|
+
case StreamKind.CLOSE:
|
|
410
|
+
assert(this.streamControllers[streamId], "close should have stream controller");
|
|
411
|
+
if (this.streamControllers[streamId].isClosed) {
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
this.streamControllers[streamId].isClosed = true;
|
|
415
|
+
this.streamControllers[streamId].controller.close();
|
|
416
|
+
this._deleteStreamController(streamId);
|
|
417
|
+
break;
|
|
418
|
+
case StreamKind.ERROR:
|
|
419
|
+
assert(this.streamControllers[streamId], "error should have stream controller");
|
|
420
|
+
this.streamControllers[streamId].controller.error(wrapReason(data.reason));
|
|
421
|
+
this._deleteStreamController(streamId);
|
|
422
|
+
break;
|
|
423
|
+
case StreamKind.CANCEL_COMPLETE:
|
|
424
|
+
if (data.success) {
|
|
425
|
+
this.streamControllers[streamId].cancelCall.resolve();
|
|
426
|
+
} else {
|
|
427
|
+
this.streamControllers[streamId].cancelCall.reject(wrapReason(data.reason));
|
|
428
|
+
}
|
|
429
|
+
this._deleteStreamController(streamId);
|
|
430
|
+
break;
|
|
431
|
+
case StreamKind.CANCEL:
|
|
432
|
+
if (!this.streamSinks[streamId]) {
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
const {
|
|
436
|
+
onCancel
|
|
437
|
+
} = this.streamSinks[data.streamId];
|
|
438
|
+
new Promise(function (resolve) {
|
|
439
|
+
resolve(onCancel && onCancel(wrapReason(data.reason)));
|
|
440
|
+
}).then(function () {
|
|
441
|
+
comObj.postMessage({
|
|
442
|
+
sourceName,
|
|
443
|
+
targetName,
|
|
444
|
+
stream: StreamKind.CANCEL_COMPLETE,
|
|
445
|
+
streamId,
|
|
446
|
+
success: true
|
|
447
|
+
});
|
|
448
|
+
}, function (reason) {
|
|
449
|
+
comObj.postMessage({
|
|
450
|
+
sourceName,
|
|
451
|
+
targetName,
|
|
452
|
+
stream: StreamKind.CANCEL_COMPLETE,
|
|
453
|
+
streamId,
|
|
454
|
+
reason: wrapReason(reason)
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
this.streamSinks[streamId].sinkCapability.reject(wrapReason(data.reason));
|
|
458
|
+
this.streamSinks[streamId].isCancelled = true;
|
|
459
|
+
delete this.streamSinks[streamId];
|
|
460
|
+
break;
|
|
461
|
+
default:
|
|
462
|
+
throw new Error("Unexpected stream case");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
534
466
|
/**
|
|
535
467
|
* @private
|
|
536
|
-
*/
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
}
|
|
468
|
+
*/
|
|
469
|
+
async _deleteStreamController(streamId) {
|
|
470
|
+
// Delete the `streamController` only when the start, pull, and cancel
|
|
471
|
+
// capabilities have settled, to prevent `TypeError`s.
|
|
472
|
+
await Promise.allSettled([this.streamControllers[streamId].startCall, this.streamControllers[streamId].pullCall, this.streamControllers[streamId].cancelCall].map(function (capability) {
|
|
473
|
+
return capability && capability.promise;
|
|
474
|
+
}));
|
|
475
|
+
delete this.streamControllers[streamId];
|
|
476
|
+
}
|
|
477
|
+
|
|
547
478
|
/**
|
|
548
479
|
* Sends raw message to the comObj.
|
|
549
480
|
* @param {Object} message - Raw message.
|
|
550
481
|
* @param transfers List of transfers/ArrayBuffers, or undefined.
|
|
551
482
|
* @private
|
|
552
|
-
*/
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
this.comObj.postMessage(message
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
export { MessageHandler as M };
|
|
483
|
+
*/
|
|
484
|
+
_postMessage(message, transfers) {
|
|
485
|
+
if (transfers && this.postMessageTransfers) {
|
|
486
|
+
this.comObj.postMessage(message, transfers);
|
|
487
|
+
} else {
|
|
488
|
+
this.comObj.postMessage(message);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
destroy() {
|
|
492
|
+
this.comObj.removeEventListener("message", this._onComObjOnMessage);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export { MessageHandler as M };
|