Skip to content

Commit eaf605d

Browse files
committed
Move the NetworkManager functionality into the PDFNetworkStream class
The `NetworkManager` is very old code at this point, and it predates the introduction of the streaming functionality by many years. To simplify things, especially with upcoming re-factoring patches, let's move this functionality into private (and semi-private) methods in the `PDFNetworkStream` class to avoid having to deal with too many different scopes.
1 parent 4d9301f commit eaf605d

1 file changed

Lines changed: 53 additions & 67 deletions

File tree

src/display/network.js

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,27 @@ function getArrayBuffer(val) {
3535
return typeof val !== "string" ? val : stringToBytes(val).buffer;
3636
}
3737

38-
class NetworkManager {
38+
/** @implements {IPDFStream} */
39+
class PDFNetworkStream {
3940
#pendingRequests = new WeakMap();
4041

42+
_fullRequestReader = null;
43+
44+
_rangeRequestReaders = [];
45+
4146
_responseOrigin = null;
4247

43-
constructor({ url, httpHeaders, withCredentials }) {
44-
this.url = url;
45-
this.isHttp = /^https?:/i.test(url);
46-
this.headers = createHeaders(this.isHttp, httpHeaders);
47-
this.withCredentials = withCredentials || false;
48+
constructor(source) {
49+
this._source = source;
50+
this.url = source.url;
51+
this.isHttp = /^https?:/i.test(this.url);
52+
this.headers = createHeaders(this.isHttp, source.httpHeaders);
4853
}
4954

50-
request(args) {
55+
/**
56+
* @ignore
57+
*/
58+
_request(args) {
5159
const xhr = new XMLHttpRequest();
5260
const pendingRequest = {
5361
validateStatus: null,
@@ -59,7 +67,7 @@ class NetworkManager {
5967
this.#pendingRequests.set(xhr, pendingRequest);
6068

6169
xhr.open("GET", this.url);
62-
xhr.withCredentials = this.withCredentials;
70+
xhr.withCredentials = this._source.withCredentials;
6371
for (const [key, val] of this.headers) {
6472
xhr.setRequestHeader(key, val);
6573
}
@@ -141,51 +149,34 @@ class NetworkManager {
141149
}
142150
}
143151

144-
// Abort the request, if it's pending.
145-
abortRequest(xhr) {
152+
/**
153+
* Abort the request, if it's pending.
154+
* @ignore
155+
*/
156+
_abortRequest(xhr) {
146157
if (this.#pendingRequests.has(xhr)) {
147158
this.#pendingRequests.delete(xhr);
148159
xhr.abort();
149160
}
150161
}
151-
}
152-
153-
/** @implements {IPDFStream} */
154-
class PDFNetworkStream {
155-
constructor(source) {
156-
this._source = source;
157-
this._manager = new NetworkManager(source);
158-
this._rangeChunkSize = source.rangeChunkSize;
159-
this._fullRequestReader = null;
160-
this._rangeRequestReaders = [];
161-
}
162-
163-
_onRangeRequestReaderClosed(reader) {
164-
const i = this._rangeRequestReaders.indexOf(reader);
165-
if (i >= 0) {
166-
this._rangeRequestReaders.splice(i, 1);
167-
}
168-
}
169162

170163
getFullReader() {
171164
assert(
172165
!this._fullRequestReader,
173166
"PDFNetworkStream.getFullReader can only be called once."
174167
);
175-
this._fullRequestReader = new PDFNetworkStreamFullRequestReader(
176-
this._manager,
177-
this._source
178-
);
168+
this._fullRequestReader = new PDFNetworkStreamFullRequestReader(this);
179169
return this._fullRequestReader;
180170
}
181171

182172
getRangeReader(begin, end) {
183-
const reader = new PDFNetworkStreamRangeRequestReader(
184-
this._manager,
185-
begin,
186-
end
187-
);
188-
reader.onClosed = this._onRangeRequestReaderClosed.bind(this);
173+
const reader = new PDFNetworkStreamRangeRequestReader(this, begin, end);
174+
reader.onClosed = () => {
175+
const i = this._rangeRequestReaders.indexOf(reader);
176+
if (i >= 0) {
177+
this._rangeRequestReaders.splice(i, 1);
178+
}
179+
};
189180
this._rangeRequestReaders.push(reader);
190181
return reader;
191182
}
@@ -201,20 +192,20 @@ class PDFNetworkStream {
201192

202193
/** @implements {IPDFStreamReader} */
203194
class PDFNetworkStreamFullRequestReader {
204-
constructor(manager, source) {
205-
this._manager = manager;
195+
constructor(stream) {
196+
this._stream = stream;
197+
const { disableRange, length, rangeChunkSize } = stream._source;
206198

207-
this._url = source.url;
208-
this._fullRequestXhr = manager.request({
199+
this._fullRequestXhr = stream._request({
209200
onHeadersReceived: this._onHeadersReceived.bind(this),
210201
onDone: this._onDone.bind(this),
211202
onError: this._onError.bind(this),
212203
onProgress: this._onProgress.bind(this),
213204
});
214205
this._headersCapability = Promise.withResolvers();
215-
this._disableRange = source.disableRange || false;
216-
this._contentLength = source.length; // Optional
217-
this._rangeChunkSize = source.rangeChunkSize;
206+
this._disableRange = disableRange || false;
207+
this._contentLength = length; // Optional
208+
this._rangeChunkSize = rangeChunkSize;
218209
if (!this._rangeChunkSize && !this._disableRange) {
219210
this._disableRange = true;
220211
}
@@ -232,11 +223,10 @@ class PDFNetworkStreamFullRequestReader {
232223
}
233224

234225
_onHeadersReceived() {
226+
const stream = this._stream;
235227
const fullRequestXhr = this._fullRequestXhr;
236228

237-
this._manager._responseOrigin = getResponseOrigin(
238-
fullRequestXhr.responseURL
239-
);
229+
stream._responseOrigin = getResponseOrigin(fullRequestXhr.responseURL);
240230

241231
const rawResponseHeaders = fullRequestXhr.getAllResponseHeaders();
242232
const responseHeaders = new Headers(
@@ -255,7 +245,7 @@ class PDFNetworkStreamFullRequestReader {
255245
const { allowRangeRequests, suggestedLength } =
256246
validateRangeRequestCapabilities({
257247
responseHeaders,
258-
isHttp: this._manager.isHttp,
248+
isHttp: stream.isHttp,
259249
rangeChunkSize: this._rangeChunkSize,
260250
disableRange: this._disableRange,
261251
});
@@ -273,7 +263,7 @@ class PDFNetworkStreamFullRequestReader {
273263
// requests, there will be an issue for sites where you can only
274264
// request the pdf once. However, if this is the case, then the
275265
// server should not be returning that it can support range requests.
276-
this._manager.abortRequest(fullRequestXhr);
266+
stream._abortRequest(fullRequestXhr);
277267
}
278268

279269
this._headersCapability.resolve();
@@ -297,7 +287,7 @@ class PDFNetworkStreamFullRequestReader {
297287
}
298288

299289
_onError(status) {
300-
this._storedError = createResponseError(status, this._url);
290+
this._storedError = createResponseError(status, this._stream.url);
301291
this._headersCapability.reject(this._storedError);
302292
for (const requestCapability of this._requests) {
303293
requestCapability.reject(this._storedError);
@@ -359,18 +349,19 @@ class PDFNetworkStreamFullRequestReader {
359349
}
360350
this._requests.length = 0;
361351

362-
this._manager.abortRequest(this._fullRequestXhr);
352+
this._stream._abortRequest(this._fullRequestXhr);
363353
this._fullRequestXhr = null;
364354
}
365355
}
366356

367357
/** @implements {IPDFStreamRangeReader} */
368358
class PDFNetworkStreamRangeRequestReader {
369-
constructor(manager, begin, end) {
370-
this._manager = manager;
359+
onClosed = null;
360+
361+
constructor(stream, begin, end) {
362+
this._stream = stream;
371363

372-
this._url = manager.url;
373-
this._requestXhr = manager.request({
364+
this._requestXhr = stream._request({
374365
begin,
375366
end,
376367
onHeadersReceived: this._onHeadersReceived.bind(this),
@@ -384,24 +375,19 @@ class PDFNetworkStreamRangeRequestReader {
384375
this._storedError = undefined;
385376

386377
this.onProgress = null;
387-
this.onClosed = null;
388378
}
389379

390380
_onHeadersReceived() {
391381
const responseOrigin = getResponseOrigin(this._requestXhr?.responseURL);
392382

393-
if (responseOrigin !== this._manager._responseOrigin) {
383+
if (responseOrigin !== this._stream._responseOrigin) {
394384
this._storedError = new Error(
395-
`Expected range response-origin "${responseOrigin}" to match "${this._manager._responseOrigin}".`
385+
`Expected range response-origin "${responseOrigin}" to match "${this._stream._responseOrigin}".`
396386
);
397387
this._onError(0);
398388
}
399389
}
400390

401-
_close() {
402-
this.onClosed?.(this);
403-
}
404-
405391
_onDone(chunk) {
406392
if (this._requests.length > 0) {
407393
const requestCapability = this._requests.shift();
@@ -414,11 +400,11 @@ class PDFNetworkStreamRangeRequestReader {
414400
requestCapability.resolve({ value: undefined, done: true });
415401
}
416402
this._requests.length = 0;
417-
this._close();
403+
this.onClosed?.(this);
418404
}
419405

420406
_onError(status) {
421-
this._storedError ??= createResponseError(status, this._url);
407+
this._storedError ??= createResponseError(status, this._stream.url);
422408
for (const requestCapability of this._requests) {
423409
requestCapability.reject(this._storedError);
424410
}
@@ -460,8 +446,8 @@ class PDFNetworkStreamRangeRequestReader {
460446
}
461447
this._requests.length = 0;
462448

463-
this._manager.abortRequest(this._requestXhr);
464-
this._close();
449+
this._stream._abortRequest(this._requestXhr);
450+
this.onClosed?.(this);
465451
}
466452
}
467453

0 commit comments

Comments
 (0)