Merge pull request #9037 from Snuffleupagus/refactor-streams-params

Re-factor how parameters are passed to the network streams
This commit is contained in:
Tim van der Meij 2017-11-18 15:41:15 +01:00 committed by GitHub
commit edaf4b3173
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 81 deletions

View File

@ -251,10 +251,7 @@ function getDocument(src) {
if (rangeTransport) { if (rangeTransport) {
networkStream = new PDFDataTransportStream(params, rangeTransport); networkStream = new PDFDataTransportStream(params, rangeTransport);
} else if (!params.data) { } else if (!params.data) {
networkStream = new PDFNetworkStream({ networkStream = new PDFNetworkStream(params);
source: params,
disableRange: getDefaultSetting('disableRange'),
});
} }
var messageHandler = new MessageHandler(docId, workerId, worker.port); var messageHandler = new MessageHandler(docId, workerId, worker.port);
@ -286,9 +283,9 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
let apiVersion = let apiVersion =
typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('BUNDLE_VERSION') : null; typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('BUNDLE_VERSION') : null;
source.disableRange = getDefaultSetting('disableRange');
source.disableAutoFetch = getDefaultSetting('disableAutoFetch'); source.disableAutoFetch = getDefaultSetting('disableAutoFetch');
source.disableStream = getDefaultSetting('disableStream'); source.disableStream = getDefaultSetting('disableStream');
source.chunkedViewerLoading = !!pdfDataRangeTransport;
if (pdfDataRangeTransport) { if (pdfDataRangeTransport) {
source.length = pdfDataRangeTransport.length; source.length = pdfDataRangeTransport.length;
source.initialData = pdfDataRangeTransport.initialData; source.initialData = pdfDataRangeTransport.initialData;

View File

@ -32,11 +32,10 @@ function createFetchOptions(headers, withCredentials) {
} }
class PDFFetchStream { class PDFFetchStream {
constructor(options) { constructor(source) {
this.options = options; this.source = source;
this.source = options.source; this.isHttp = /^https?:/i.test(source.url);
this.isHttp = /^https?:/i.test(this.source.url); this.httpHeaders = (this.isHttp && source.httpHeaders) || {};
this.httpHeaders = (this.isHttp && this.source.httpHeaders) || {};
this._fullRequestReader = null; this._fullRequestReader = null;
this._rangeRequestReaders = []; this._rangeRequestReaders = [];
@ -70,17 +69,18 @@ class PDFFetchStreamReader {
this._stream = stream; this._stream = stream;
this._reader = null; this._reader = null;
this._loaded = 0; this._loaded = 0;
this._withCredentials = stream.source.withCredentials; let source = stream.source;
this._contentLength = this._stream.source.length; this._withCredentials = source.withCredentials;
this._contentLength = source.length;
this._headersCapability = createPromiseCapability(); this._headersCapability = createPromiseCapability();
this._disableRange = this._stream.options.disableRange; this._disableRange = source.disableRange;
this._rangeChunkSize = this._stream.source.rangeChunkSize; this._rangeChunkSize = source.rangeChunkSize;
if (!this._rangeChunkSize && !this._disableRange) { if (!this._rangeChunkSize && !this._disableRange) {
this._disableRange = true; this._disableRange = true;
} }
this._isRangeSupported = !this._stream.options.disableRange; this._isRangeSupported = !source.disableRange;
this._isStreamingSupported = !this._stream.source.disableStream; this._isStreamingSupported = !source.disableStream;
this._headers = new Headers(); this._headers = new Headers();
for (let property in this._stream.httpHeaders) { for (let property in this._stream.httpHeaders) {
@ -91,7 +91,7 @@ class PDFFetchStreamReader {
this._headers.append(property, value); this._headers.append(property, value);
} }
let url = this._stream.source.url; let url = source.url;
fetch(url, createFetchOptions(this._headers, this._withCredentials)). fetch(url, createFetchOptions(this._headers, this._withCredentials)).
then((response) => { then((response) => {
if (!validateResponseStatus(response.status)) { if (!validateResponseStatus(response.status)) {
@ -170,9 +170,10 @@ class PDFFetchStreamRangeReader {
this._stream = stream; this._stream = stream;
this._reader = null; this._reader = null;
this._loaded = 0; this._loaded = 0;
this._withCredentials = stream.source.withCredentials; let source = stream.source;
this._withCredentials = source.withCredentials;
this._readCapability = createPromiseCapability(); this._readCapability = createPromiseCapability();
this._isStreamingSupported = !stream.source.disableStream; this._isStreamingSupported = !source.disableStream;
this._headers = new Headers(); this._headers = new Headers();
for (let property in this._stream.httpHeaders) { for (let property in this._stream.httpHeaders) {
@ -185,7 +186,7 @@ class PDFFetchStreamRangeReader {
let rangeStr = begin + '-' + (end - 1); let rangeStr = begin + '-' + (end - 1);
this._headers.append('Range', 'bytes=' + rangeStr); this._headers.append('Range', 'bytes=' + rangeStr);
let url = this._stream.source.url; let url = source.url;
fetch(url, createFetchOptions(this._headers, this._withCredentials)). fetch(url, createFetchOptions(this._headers, this._withCredentials)).
then((response) => { then((response) => {
if (!validateResponseStatus(response.status)) { if (!validateResponseStatus(response.status)) {

View File

@ -265,9 +265,8 @@ NetworkManager.prototype = {
}; };
/** @implements {IPDFStream} */ /** @implements {IPDFStream} */
function PDFNetworkStream(options) { function PDFNetworkStream(source) {
this._options = options; this._source = source;
var source = options.source;
this._manager = new NetworkManager(source.url, { this._manager = new NetworkManager(source.url, {
httpHeaders: source.httpHeaders, httpHeaders: source.httpHeaders,
withCredentials: source.withCredentials, withCredentials: source.withCredentials,
@ -289,7 +288,7 @@ PDFNetworkStream.prototype = {
getFullReader: function PDFNetworkStream_getFullReader() { getFullReader: function PDFNetworkStream_getFullReader() {
assert(!this._fullRequestReader); assert(!this._fullRequestReader);
this._fullRequestReader = this._fullRequestReader =
new PDFNetworkStreamFullRequestReader(this._manager, this._options); new PDFNetworkStreamFullRequestReader(this._manager, this._source);
return this._fullRequestReader; return this._fullRequestReader;
}, },
@ -313,10 +312,9 @@ PDFNetworkStream.prototype = {
}; };
/** @implements {IPDFStreamReader} */ /** @implements {IPDFStreamReader} */
function PDFNetworkStreamFullRequestReader(manager, options) { function PDFNetworkStreamFullRequestReader(manager, source) {
this._manager = manager; this._manager = manager;
var source = options.source;
var args = { var args = {
onHeadersReceived: this._onHeadersReceived.bind(this), onHeadersReceived: this._onHeadersReceived.bind(this),
onProgressiveData: source.disableStream ? null : onProgressiveData: source.disableStream ? null :
@ -328,7 +326,7 @@ function PDFNetworkStreamFullRequestReader(manager, options) {
this._url = source.url; this._url = source.url;
this._fullRequestId = manager.requestFull(args); this._fullRequestId = manager.requestFull(args);
this._headersReceivedCapability = createPromiseCapability(); this._headersReceivedCapability = createPromiseCapability();
this._disableRange = options.disableRange || false; this._disableRange = source.disableRange || false;
this._contentLength = source.length; // optional this._contentLength = source.length; // optional
this._rangeChunkSize = source.rangeChunkSize; this._rangeChunkSize = source.rangeChunkSize;
if (!this._rangeChunkSize && !this._disableRange) { if (!this._rangeChunkSize && !this._disableRange) {

View File

@ -25,15 +25,14 @@ import {
import { validateRangeRequestCapabilities } from './network_utils'; import { validateRangeRequestCapabilities } from './network_utils';
class PDFNodeStream { class PDFNodeStream {
constructor(options) { constructor(source) {
this.options = options; this.source = source;
this.source = options.source; this.url = url.parse(source.url);
this.url = url.parse(this.source.url);
this.isHttp = this.url.protocol === 'http:' || this.isHttp = this.url.protocol === 'http:' ||
this.url.protocol === 'https:'; this.url.protocol === 'https:';
// Check if url refers to filesystem. // Check if url refers to filesystem.
this.isFsUrl = this.url.protocol === 'file:' || !this.url.host; this.isFsUrl = this.url.protocol === 'file:' || !this.url.host;
this.httpHeaders = (this.isHttp && this.source.httpHeaders) || {}; this.httpHeaders = (this.isHttp && source.httpHeaders) || {};
this._fullRequest = null; this._fullRequest = null;
this._rangeRequestReaders = []; this._rangeRequestReaders = [];
@ -74,17 +73,18 @@ class BaseFullReader {
this._errored = false; this._errored = false;
this._reason = null; this._reason = null;
this.onProgress = null; this.onProgress = null;
this._contentLength = stream.source.length; // optional let source = stream.source;
this._contentLength = source.length; // optional
this._loaded = 0; this._loaded = 0;
this._disableRange = stream.options.disableRange || false; this._disableRange = source.disableRange || false;
this._rangeChunkSize = stream.source.rangeChunkSize; this._rangeChunkSize = source.rangeChunkSize;
if (!this._rangeChunkSize && !this._disableRange) { if (!this._rangeChunkSize && !this._disableRange) {
this._disableRange = true; this._disableRange = true;
} }
this._isStreamingSupported = !stream.source.disableStream; this._isStreamingSupported = !source.disableStream;
this._isRangeSupported = !stream.options.disableRange; this._isRangeSupported = !source.disableRange;
this._readableStream = null; this._readableStream = null;
this._readCapability = createPromiseCapability(); this._readCapability = createPromiseCapability();
@ -190,8 +190,8 @@ class BaseRangeReader {
this._loaded = 0; this._loaded = 0;
this._readableStream = null; this._readableStream = null;
this._readCapability = createPromiseCapability(); this._readCapability = createPromiseCapability();
let source = stream.source;
this._isStreamingSupported = !stream.source.disableStream; this._isStreamingSupported = !source.disableStream;
} }
get isStreamingSupported() { get isStreamingSupported() {
@ -303,11 +303,13 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._request = null; this._request = null;
if (this._url.protocol === 'http:') { if (this._url.protocol === 'http:') {
this._request = http.request(createRequestOptions( this._request = http.request(
this._url, stream.httpHeaders), handleResponse); createRequestOptions(this._url, stream.httpHeaders),
handleResponse);
} else { } else {
this._request = https.request(createRequestOptions( this._request = https.request(
this._url, stream.httpHeaders), handleResponse); createRequestOptions(this._url, stream.httpHeaders),
handleResponse);
} }
this._request.on('error', (reason) => { this._request.on('error', (reason) => {

View File

@ -23,11 +23,9 @@ describe('network', function() {
it('read without stream and range', function(done) { it('read without stream and range', function(done) {
var stream = new PDFNetworkStream({ var stream = new PDFNetworkStream({
source: { url: pdf1,
url: pdf1, rangeChunkSize: 65536,
rangeChunkSize: 65536, disableStream: true,
disableStream: true,
},
disableRange: true, disableRange: true,
}); });
@ -77,11 +75,9 @@ describe('network', function() {
} }
var stream = new PDFNetworkStream({ var stream = new PDFNetworkStream({
source: { url: pdf2,
url: pdf2, rangeChunkSize: 65536,
rangeChunkSize: 65536, disableStream: false,
disableStream: false,
},
disableRange: false, disableRange: false,
}); });
@ -123,12 +119,10 @@ describe('network', function() {
// requiring this test to pass. // requiring this test to pass.
var rangeSize = 32768; var rangeSize = 32768;
var stream = new PDFNetworkStream({ var stream = new PDFNetworkStream({
source: { url: pdf1,
url: pdf1, length: pdf1Length,
length: pdf1Length, rangeChunkSize: rangeSize,
rangeChunkSize: rangeSize, disableStream: true,
disableStream: true,
},
disableRange: false, disableRange: false,
}); });

View File

@ -76,20 +76,16 @@ describe('node_stream', function() {
it('read both http(s) and filesystem pdf files', function(done) { it('read both http(s) and filesystem pdf files', function(done) {
let stream1 = new PDFNodeStream({ let stream1 = new PDFNodeStream({
source: { url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
url: `http://127.0.0.1:${port}/tracemonkey.pdf`, rangeChunkSize: 65536,
rangeChunkSize: 65536, disableStream: true,
disableStream: true,
},
disableRange: true, disableRange: true,
}); });
let stream2 = new PDFNodeStream({ let stream2 = new PDFNodeStream({
source: { url: pdf,
url: pdf, rangeChunkSize: 65536,
rangeChunkSize: 65536, disableStream: true,
disableStream: true,
},
disableRange: true, disableRange: true,
}); });
@ -146,21 +142,17 @@ describe('node_stream', function() {
function(done) { function(done) {
let rangeSize = 32768; let rangeSize = 32768;
let stream1 = new PDFNodeStream({ let stream1 = new PDFNodeStream({
source: { url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
url: `http://127.0.0.1:${port}/tracemonkey.pdf`, length: pdfLength,
length: pdfLength, rangeChunkSize: rangeSize,
rangeChunkSize: rangeSize, disableStream: true,
disableStream: true,
},
disableRange: false, disableRange: false,
}); });
let stream2 = new PDFNodeStream({ let stream2 = new PDFNodeStream({
source: { url: pdf,
url: pdf, length: pdfLength,
length: pdfLength, rangeChunkSize: rangeSize,
rangeChunkSize: rangeSize, disableStream: true,
disableStream: true,
},
disableRange: false, disableRange: false,
}); });