1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.PDFWorkerStream = void 0;
|
28 |
|
29 | var _util = require("../shared/util.js");
|
30 |
|
31 | class 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 |
|
65 | exports.PDFWorkerStream = PDFWorkerStream;
|
66 |
|
67 | class 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 |
|
126 | class 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 |