UNPKG

3.7 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.PDFWorkerStream = void 0;
28
29var _util = require("../shared/util.js");
30
31class PDFWorkerStream {
32 constructor(msgHandler) {
33 this._msgHandler = msgHandler;
34 this._contentLength = null;
35 this._fullRequestReader = null;
36 this._rangeRequestReaders = [];
37 }
38
39 getFullReader() {
40 (0, _util.assert)(!this._fullRequestReader, "PDFWorkerStream.getFullReader can only be called once.");
41 this._fullRequestReader = new PDFWorkerStreamReader(this._msgHandler);
42 return this._fullRequestReader;
43 }
44
45 getRangeReader(begin, end) {
46 const reader = new PDFWorkerStreamRangeReader(begin, end, this._msgHandler);
47
48 this._rangeRequestReaders.push(reader);
49
50 return reader;
51 }
52
53 cancelAllRequests(reason) {
54 if (this._fullRequestReader) {
55 this._fullRequestReader.cancel(reason);
56 }
57
58 for (const reader of this._rangeRequestReaders.slice(0)) {
59 reader.cancel(reason);
60 }
61 }
62
63}
64
65exports.PDFWorkerStream = PDFWorkerStream;
66
67class PDFWorkerStreamReader {
68 constructor(msgHandler) {
69 this._msgHandler = msgHandler;
70 this.onProgress = null;
71 this._contentLength = null;
72 this._isRangeSupported = false;
73 this._isStreamingSupported = false;
74
75 const readableStream = this._msgHandler.sendWithStream("GetReader");
76
77 this._reader = readableStream.getReader();
78 this._headersReady = this._msgHandler.sendWithPromise("ReaderHeadersReady").then(data => {
79 this._isStreamingSupported = data.isStreamingSupported;
80 this._isRangeSupported = data.isRangeSupported;
81 this._contentLength = data.contentLength;
82 });
83 }
84
85 get headersReady() {
86 return this._headersReady;
87 }
88
89 get contentLength() {
90 return this._contentLength;
91 }
92
93 get isStreamingSupported() {
94 return this._isStreamingSupported;
95 }
96
97 get isRangeSupported() {
98 return this._isRangeSupported;
99 }
100
101 async read() {
102 const {
103 value,
104 done
105 } = await this._reader.read();
106
107 if (done) {
108 return {
109 value: undefined,
110 done: true
111 };
112 }
113
114 return {
115 value: value.buffer,
116 done: false
117 };
118 }
119
120 cancel(reason) {
121 this._reader.cancel(reason);
122 }
123
124}
125
126class PDFWorkerStreamRangeReader {
127 constructor(begin, end, msgHandler) {
128 this._msgHandler = msgHandler;
129 this.onProgress = null;
130
131 const readableStream = this._msgHandler.sendWithStream("GetRangeReader", {
132 begin,
133 end
134 });
135
136 this._reader = readableStream.getReader();
137 }
138
139 get isStreamingSupported() {
140 return false;
141 }
142
143 async read() {
144 const {
145 value,
146 done
147 } = await this._reader.read();
148
149 if (done) {
150 return {
151 value: undefined,
152 done: true
153 };
154 }
155
156 return {
157 value: value.buffer,
158 done: false
159 };
160 }
161
162 cancel(reason) {
163 this._reader.cancel(reason);
164 }
165
166}
\No newline at end of file