|
|
@ -41,10 +41,12 @@ var PasswordException = sharedUtil.PasswordException; |
|
|
|
var PasswordResponses = sharedUtil.PasswordResponses; |
|
|
|
var PasswordResponses = sharedUtil.PasswordResponses; |
|
|
|
var UnknownErrorException = sharedUtil.UnknownErrorException; |
|
|
|
var UnknownErrorException = sharedUtil.UnknownErrorException; |
|
|
|
var XRefParseException = sharedUtil.XRefParseException; |
|
|
|
var XRefParseException = sharedUtil.XRefParseException; |
|
|
|
|
|
|
|
var arrayByteLength = sharedUtil.arrayByteLength; |
|
|
|
|
|
|
|
var arraysToBytes = sharedUtil.arraysToBytes; |
|
|
|
|
|
|
|
var assert = sharedUtil.assert; |
|
|
|
var createPromiseCapability = sharedUtil.createPromiseCapability; |
|
|
|
var createPromiseCapability = sharedUtil.createPromiseCapability; |
|
|
|
var error = sharedUtil.error; |
|
|
|
var error = sharedUtil.error; |
|
|
|
var info = sharedUtil.info; |
|
|
|
var info = sharedUtil.info; |
|
|
|
var isInt = sharedUtil.isInt; |
|
|
|
|
|
|
|
var warn = sharedUtil.warn; |
|
|
|
var warn = sharedUtil.warn; |
|
|
|
var Ref = corePrimitives.Ref; |
|
|
|
var Ref = corePrimitives.Ref; |
|
|
|
var LocalPdfManager = corePdfManager.LocalPdfManager; |
|
|
|
var LocalPdfManager = corePdfManager.LocalPdfManager; |
|
|
@ -82,6 +84,350 @@ var WorkerTask = (function WorkerTaskClosure() { |
|
|
|
return WorkerTask; |
|
|
|
return WorkerTask; |
|
|
|
})(); |
|
|
|
})(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#if !PRODUCTION
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Interface that represents PDF data transport. If possible, it allows |
|
|
|
|
|
|
|
* progressively load entire or fragment of the PDF binary data. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @interface |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
function IPDFStream() {} |
|
|
|
|
|
|
|
IPDFStream.prototype = { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets a reader for the entire PDF data. |
|
|
|
|
|
|
|
* @returns {IPDFStreamReader} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
getFullReader: function () { return null; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets a reader for the range of the PDF data. |
|
|
|
|
|
|
|
* @param {number} begin - the start offset of the data. |
|
|
|
|
|
|
|
* @param {number} end - the end offset of the data. |
|
|
|
|
|
|
|
* @returns {IPDFStreamRangeReader} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
getRangeReader: function (begin, end) { return null; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Cancels all opened reader and closes all their opened requests. |
|
|
|
|
|
|
|
* @param {Object} reason - the reason for cancelling |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
cancelAllRequests: function (reason) {}, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Interface for a PDF binary data reader. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @interface |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function IPDFStreamReader() {} |
|
|
|
|
|
|
|
IPDFStreamReader.prototype = { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets a promise that is resolved when the headers and other metadata of |
|
|
|
|
|
|
|
* the PDF data stream are available. |
|
|
|
|
|
|
|
* @returns {Promise} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
get headersReady() { return null; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets PDF binary data length. It is defined after the headersReady promise |
|
|
|
|
|
|
|
* is resolved. |
|
|
|
|
|
|
|
* @returns {number} The data length (or 0 if unknown). |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
get contentLength() { return 0; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets ability of the stream to handle range requests. It is defined after |
|
|
|
|
|
|
|
* the headersReady promise is resolved. Rejected when the reader is cancelled |
|
|
|
|
|
|
|
* or an error occurs. |
|
|
|
|
|
|
|
* @returns {boolean} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
get isRangeSupported() { return false; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets ability of the stream to progressively load binary data. It is defined |
|
|
|
|
|
|
|
* after the headersReady promise is resolved. |
|
|
|
|
|
|
|
* @returns {boolean} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
get isStreamingSupported() { return false; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Requests a chunk of the binary data. The method returns the promise, which |
|
|
|
|
|
|
|
* is resolved into object with properties "value" and "done". If the done |
|
|
|
|
|
|
|
* is set to true, then the stream has reached its end, otherwise the value |
|
|
|
|
|
|
|
* contains binary data. Cancelled requests will be resolved with the done is |
|
|
|
|
|
|
|
* set to true. |
|
|
|
|
|
|
|
* @returns {Promise} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
read: function () {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Cancels all pending read requests and closes the stream. |
|
|
|
|
|
|
|
* @param {Object} reason |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
cancel: function (reason) {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sets or gets the progress callback. The callback can be useful when the |
|
|
|
|
|
|
|
* isStreamingSupported property of the object is defined as false. |
|
|
|
|
|
|
|
* The callback is called with one parameter: an object with the loaded and |
|
|
|
|
|
|
|
* total properties. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
onProgress: null, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Interface for a PDF binary data fragment reader. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @interface |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function IPDFStreamRangeReader() {} |
|
|
|
|
|
|
|
IPDFStreamRangeReader.prototype = { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets ability of the stream to progressively load binary data. |
|
|
|
|
|
|
|
* @returns {boolean} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
get isStreamingSupported() { return false; }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Requests a chunk of the binary data. The method returns the promise, which |
|
|
|
|
|
|
|
* is resolved into object with properties "value" and "done". If the done |
|
|
|
|
|
|
|
* is set to true, then the stream has reached its end, otherwise the value |
|
|
|
|
|
|
|
* contains binary data. Cancelled requests will be resolved with the done is |
|
|
|
|
|
|
|
* set to true. |
|
|
|
|
|
|
|
* @returns {Promise} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
read: function () {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Cancels all pending read requests and closes the stream. |
|
|
|
|
|
|
|
* @param {Object} reason |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
cancel: function (reason) {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sets or gets the progress callback. The callback can be useful when the |
|
|
|
|
|
|
|
* isStreamingSupported property of the object is defined as false. |
|
|
|
|
|
|
|
* The callback is called with one parameter: an object with the loaded |
|
|
|
|
|
|
|
* property. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
onProgress: null, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @implements {IPDFStream} */ |
|
|
|
|
|
|
|
var PDFWorkerStream = (function PDFWorkerStreamClosure() { |
|
|
|
|
|
|
|
function PDFWorkerStream(params, msgHandler) { |
|
|
|
|
|
|
|
this._queuedChunks = []; |
|
|
|
|
|
|
|
var initialData = params.initialData; |
|
|
|
|
|
|
|
if (initialData && initialData.length > 0) { |
|
|
|
|
|
|
|
this._queuedChunks.push(initialData); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this._msgHandler = msgHandler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._isRangeSupported = !(params.disableRange); |
|
|
|
|
|
|
|
this._isStreamingSupported = !(params.disableStream); |
|
|
|
|
|
|
|
this._contentLength = params.length; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._fullRequestReader = null; |
|
|
|
|
|
|
|
this._rangeReaders = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msgHandler.on('OnDataRange', this._onReceiveData.bind(this)); |
|
|
|
|
|
|
|
msgHandler.on('OnDataProgress', this._onProgress.bind(this)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
PDFWorkerStream.prototype = { |
|
|
|
|
|
|
|
_onReceiveData: function PDFWorkerStream_onReceiveData(args) { |
|
|
|
|
|
|
|
if (args.begin === undefined) { |
|
|
|
|
|
|
|
if (this._fullRequestReader) { |
|
|
|
|
|
|
|
this._fullRequestReader._enqueue(args.chunk); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
this._queuedChunks.push(args.chunk); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
var found = this._rangeReaders.some(function (rangeReader) { |
|
|
|
|
|
|
|
if (rangeReader._begin !== args.begin) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rangeReader._enqueue(args.chunk); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
assert(found); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_onProgress: function PDFWorkerStream_onProgress(evt) { |
|
|
|
|
|
|
|
if (this._rangeReaders.length > 0) { |
|
|
|
|
|
|
|
// Reporting to first range reader.
|
|
|
|
|
|
|
|
var firstReader = this._rangeReaders[0]; |
|
|
|
|
|
|
|
if (firstReader.onProgress) { |
|
|
|
|
|
|
|
firstReader.onProgress({loaded: evt.loaded}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_removeRangeReader: function PDFWorkerStream_removeRangeReader(reader) { |
|
|
|
|
|
|
|
var i = this._rangeReaders.indexOf(reader); |
|
|
|
|
|
|
|
if (i >= 0) { |
|
|
|
|
|
|
|
this._rangeReaders.splice(i, 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getFullReader: function PDFWorkerStream_getFullReader() { |
|
|
|
|
|
|
|
assert(!this._fullRequestReader); |
|
|
|
|
|
|
|
var queuedChunks = this._queuedChunks; |
|
|
|
|
|
|
|
this._queuedChunks = null; |
|
|
|
|
|
|
|
return new PDFWorkerStreamReader(this, queuedChunks); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getRangeReader: function PDFWorkerStream_getRangeReader(begin, end) { |
|
|
|
|
|
|
|
var reader = new PDFWorkerStreamRangeReader(this, begin, end); |
|
|
|
|
|
|
|
this._msgHandler.send('RequestDataRange', { begin: begin, end: end }); |
|
|
|
|
|
|
|
this._rangeReaders.push(reader); |
|
|
|
|
|
|
|
return reader; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cancelAllRequests: function PDFWorkerStream_cancelAllRequests(reason) { |
|
|
|
|
|
|
|
if (this._fullRequestReader) { |
|
|
|
|
|
|
|
this._fullRequestReader.cancel(reason); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var readers = this._rangeReaders.slice(0); |
|
|
|
|
|
|
|
readers.forEach(function (rangeReader) { |
|
|
|
|
|
|
|
rangeReader.cancel(reason); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @implements {IPDFStreamReader} */ |
|
|
|
|
|
|
|
function PDFWorkerStreamReader(stream, queuedChunks) { |
|
|
|
|
|
|
|
this._stream = stream; |
|
|
|
|
|
|
|
this._done = false; |
|
|
|
|
|
|
|
this._queuedChunks = queuedChunks || []; |
|
|
|
|
|
|
|
this._requests = []; |
|
|
|
|
|
|
|
this._headersReady = Promise.resolve(); |
|
|
|
|
|
|
|
stream._fullRequestReader = this; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.onProgress = null; // not used
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
PDFWorkerStreamReader.prototype = { |
|
|
|
|
|
|
|
_enqueue: function PDFWorkerStreamReader_enqueue(chunk) { |
|
|
|
|
|
|
|
if (this._done) { |
|
|
|
|
|
|
|
return; // ignore new data
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this._requests.length > 0) { |
|
|
|
|
|
|
|
var requestCapability = this._requests.shift(); |
|
|
|
|
|
|
|
requestCapability.resolve({value: chunk, done: false}); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this._queuedChunks.push(chunk); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get headersReady() { |
|
|
|
|
|
|
|
return this._headersReady; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get isRangeSupported() { |
|
|
|
|
|
|
|
return this._stream._isRangeSupported; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get isStreamingSupported() { |
|
|
|
|
|
|
|
return this._stream._isStreamingSupported; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get contentLength() { |
|
|
|
|
|
|
|
return this._stream._contentLength; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
read: function PDFWorkerStreamReader_read() { |
|
|
|
|
|
|
|
if (this._queuedChunks.length > 0) { |
|
|
|
|
|
|
|
var chunk = this._queuedChunks.shift(); |
|
|
|
|
|
|
|
return Promise.resolve({value: chunk, done: false}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this._done) { |
|
|
|
|
|
|
|
return Promise.resolve({value: undefined, done: true}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var requestCapability = createPromiseCapability(); |
|
|
|
|
|
|
|
this._requests.push(requestCapability); |
|
|
|
|
|
|
|
return requestCapability.promise; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cancel: function PDFWorkerStreamReader_cancel(reason) { |
|
|
|
|
|
|
|
this._done = true; |
|
|
|
|
|
|
|
this._requests.forEach(function (requestCapability) { |
|
|
|
|
|
|
|
requestCapability.resolve({value: undefined, done: true}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
this._requests = []; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @implements {IPDFStreamRangeReader} */ |
|
|
|
|
|
|
|
function PDFWorkerStreamRangeReader(stream, begin, end) { |
|
|
|
|
|
|
|
this._stream = stream; |
|
|
|
|
|
|
|
this._begin = begin; |
|
|
|
|
|
|
|
this._end = end; |
|
|
|
|
|
|
|
this._queuedChunk = null; |
|
|
|
|
|
|
|
this._requests = []; |
|
|
|
|
|
|
|
this._done = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.onProgress = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
PDFWorkerStreamRangeReader.prototype = { |
|
|
|
|
|
|
|
_enqueue: function PDFWorkerStreamRangeReader_enqueue(chunk) { |
|
|
|
|
|
|
|
if (this._done) { |
|
|
|
|
|
|
|
return; // ignore new data
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this._requests.length === 0) { |
|
|
|
|
|
|
|
this._queuedChunk = chunk; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
var requestsCapability = this._requests.shift(); |
|
|
|
|
|
|
|
requestsCapability.resolve({value: chunk, done: false}); |
|
|
|
|
|
|
|
this._requests.forEach(function (requestCapability) { |
|
|
|
|
|
|
|
requestCapability.resolve({value: undefined, done: true}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
this._requests = []; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this._done = true; |
|
|
|
|
|
|
|
this._stream._removeRangeReader(this); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get isStreamingSupported() { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
read: function PDFWorkerStreamRangeReader_read() { |
|
|
|
|
|
|
|
if (this._queuedChunk) { |
|
|
|
|
|
|
|
return Promise.resolve({value: this._queuedChunk, done: false}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this._done) { |
|
|
|
|
|
|
|
return Promise.resolve({value: undefined, done: true}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var requestCapability = createPromiseCapability(); |
|
|
|
|
|
|
|
this._requests.push(requestCapability); |
|
|
|
|
|
|
|
return requestCapability.promise; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cancel: function PDFWorkerStreamRangeReader_cancel(reason) { |
|
|
|
|
|
|
|
this._done = true; |
|
|
|
|
|
|
|
this._requests.forEach(function (requestCapability) { |
|
|
|
|
|
|
|
requestCapability.resolve({value: undefined, done: true}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
this._requests = []; |
|
|
|
|
|
|
|
this._stream._removeRangeReader(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return PDFWorkerStream; |
|
|
|
|
|
|
|
})(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @type IPDFStream */ |
|
|
|
|
|
|
|
var PDFNetworkStream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sets PDFNetworkStream class to be used as alternative PDF data transport. |
|
|
|
|
|
|
|
* @param {IPDFStream} cls - the PDF data transport. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function setPDFNetworkStreamClass(cls) { |
|
|
|
|
|
|
|
PDFNetworkStream = cls; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var WorkerMessageHandler = PDFJS.WorkerMessageHandler = { |
|
|
|
var WorkerMessageHandler = PDFJS.WorkerMessageHandler = { |
|
|
|
setup: function wphSetup(handler, port) { |
|
|
|
setup: function wphSetup(handler, port) { |
|
|
|
var testMessageProcessed = false; |
|
|
|
var testMessageProcessed = false; |
|
|
@ -188,7 +534,6 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = { |
|
|
|
var pdfManager; |
|
|
|
var pdfManager; |
|
|
|
|
|
|
|
|
|
|
|
var source = data.source; |
|
|
|
var source = data.source; |
|
|
|
var disableRange = data.disableRange; |
|
|
|
|
|
|
|
if (source.data) { |
|
|
|
if (source.data) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
pdfManager = new LocalPdfManager(docId, source.data, source.password); |
|
|
|
pdfManager = new LocalPdfManager(docId, source.data, source.password); |
|
|
@ -196,144 +541,113 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = { |
|
|
|
} catch (ex) { |
|
|
|
} catch (ex) { |
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
return pdfManagerCapability.promise; |
|
|
|
} |
|
|
|
} else if (source.chunkedViewerLoading) { |
|
|
|
|
|
|
|
try { |
|
|
|
var pdfStream; |
|
|
|
pdfManager = new NetworkPdfManager(docId, source, handler); |
|
|
|
try { |
|
|
|
pdfManagerCapability.resolve(pdfManager); |
|
|
|
if (source.chunkedViewerLoading) { |
|
|
|
} catch (ex) { |
|
|
|
pdfStream = new PDFWorkerStream(source, handler); |
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
} else { |
|
|
|
|
|
|
|
assert(PDFNetworkStream, 'pdfjs/core/network module is not loaded'); |
|
|
|
|
|
|
|
pdfStream = new PDFNetworkStream(data); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} catch (ex) { |
|
|
|
|
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
return pdfManagerCapability.promise; |
|
|
|
return pdfManagerCapability.promise; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var networkManager = new NetworkManager(source.url, { |
|
|
|
var fullRequest = pdfStream.getFullReader(); |
|
|
|
httpHeaders: source.httpHeaders, |
|
|
|
fullRequest.headersReady.then(function () { |
|
|
|
withCredentials: source.withCredentials |
|
|
|
if (!fullRequest.isStreamingSupported || |
|
|
|
}); |
|
|
|
!fullRequest.isRangeSupported) { |
|
|
|
var cachedChunks = []; |
|
|
|
// If stream or range are disabled, it's our only way to report
|
|
|
|
var fullRequestXhrId = networkManager.requestFull({ |
|
|
|
// loading progress.
|
|
|
|
onHeadersReceived: function onHeadersReceived() { |
|
|
|
fullRequest.onProgress = function (evt) { |
|
|
|
if (disableRange) { |
|
|
|
handler.send('DocProgress', { |
|
|
|
return; |
|
|
|
loaded: evt.loaded, |
|
|
|
} |
|
|
|
total: evt.total |
|
|
|
|
|
|
|
}); |
|
|
|
var fullRequestXhr = networkManager.getRequestXhr(fullRequestXhrId); |
|
|
|
}; |
|
|
|
if (fullRequestXhr.getResponseHeader('Accept-Ranges') !== 'bytes') { |
|
|
|
} |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var contentEncoding = |
|
|
|
|
|
|
|
fullRequestXhr.getResponseHeader('Content-Encoding') || 'identity'; |
|
|
|
|
|
|
|
if (contentEncoding !== 'identity') { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var length = fullRequestXhr.getResponseHeader('Content-Length'); |
|
|
|
if (!fullRequest.isRangeSupported) { |
|
|
|
length = parseInt(length, 10); |
|
|
|
return; |
|
|
|
if (!isInt(length)) { |
|
|
|
} |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
source.length = length; |
|
|
|
|
|
|
|
if (length <= 2 * source.rangeChunkSize) { |
|
|
|
|
|
|
|
// The file size is smaller than the size of two chunks, so it does
|
|
|
|
|
|
|
|
// not make any sense to abort the request and retry with a range
|
|
|
|
|
|
|
|
// request.
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (networkManager.isStreamingRequest(fullRequestXhrId)) { |
|
|
|
// We don't need auto-fetch when streaming is enabled.
|
|
|
|
// We can continue fetching when progressive loading is enabled,
|
|
|
|
var disableAutoFetch = source.disableAutoFetch || |
|
|
|
// and we don't need the autoFetch feature.
|
|
|
|
fullRequest.isStreamingSupported; |
|
|
|
source.disableAutoFetch = true; |
|
|
|
pdfManager = new NetworkPdfManager(docId, pdfStream, { |
|
|
|
} else { |
|
|
|
msgHandler: handler, |
|
|
|
// NOTE: by cancelling the full request, and then issuing range
|
|
|
|
url: source.url, |
|
|
|
// requests, there will be an issue for sites where you can only
|
|
|
|
password: source.password, |
|
|
|
// request the pdf once. However, if this is the case, then the
|
|
|
|
length: fullRequest.contentLength, |
|
|
|
// server should not be returning that it can support range
|
|
|
|
disableAutoFetch: disableAutoFetch, |
|
|
|
// requests.
|
|
|
|
rangeChunkSize: source.rangeChunkSize |
|
|
|
networkManager.abortRequest(fullRequestXhrId); |
|
|
|
}); |
|
|
|
} |
|
|
|
pdfManagerCapability.resolve(pdfManager); |
|
|
|
|
|
|
|
cancelXHRs = null; |
|
|
|
|
|
|
|
}).catch(function (reason) { |
|
|
|
|
|
|
|
pdfManagerCapability.reject(reason); |
|
|
|
|
|
|
|
cancelXHRs = null; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cachedChunks = [], loaded = 0; |
|
|
|
|
|
|
|
var flushChunks = function () { |
|
|
|
|
|
|
|
var pdfFile = arraysToBytes(cachedChunks); |
|
|
|
|
|
|
|
if (source.length && pdfFile.length !== source.length) { |
|
|
|
|
|
|
|
warn('reported HTTP length is different from actual'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// the data is array, instantiating directly from it
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
pdfManager = new LocalPdfManager(docId, pdfFile, source.password); |
|
|
|
|
|
|
|
pdfManagerCapability.resolve(pdfManager); |
|
|
|
|
|
|
|
} catch (ex) { |
|
|
|
|
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
cachedChunks = []; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
var readPromise = new Promise(function (resolve, reject) { |
|
|
|
|
|
|
|
var readChunk = function (chunk) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
pdfManager = new NetworkPdfManager(docId, source, handler); |
|
|
|
ensureNotTerminated(); |
|
|
|
pdfManagerCapability.resolve(pdfManager); |
|
|
|
if (chunk.done) { |
|
|
|
} catch (ex) { |
|
|
|
if (!pdfManager) { |
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
flushChunks(); |
|
|
|
} |
|
|
|
} |
|
|
|
cancelXHRs = null; |
|
|
|
cancelXHRs = null; |
|
|
|
}, |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
onProgressiveData: source.disableStream ? null : |
|
|
|
|
|
|
|
function onProgressiveData(chunk) { |
|
|
|
|
|
|
|
if (!pdfManager) { |
|
|
|
|
|
|
|
cachedChunks.push(chunk); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
pdfManager.sendProgressiveData(chunk); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onDone: function onDone(args) { |
|
|
|
|
|
|
|
if (pdfManager) { |
|
|
|
|
|
|
|
return; // already processed
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var pdfFile; |
|
|
|
var data = chunk.value; |
|
|
|
if (args === null) { |
|
|
|
loaded += arrayByteLength(data); |
|
|
|
// TODO add some streaming manager, e.g. for unknown length files.
|
|
|
|
if (!fullRequest.isStreamingSupported) { |
|
|
|
// The data was returned in the onProgressiveData, combining...
|
|
|
|
handler.send('DocProgress', { |
|
|
|
var pdfFileLength = 0, pos = 0; |
|
|
|
loaded: loaded, |
|
|
|
cachedChunks.forEach(function (chunk) { |
|
|
|
total: Math.max(loaded, fullRequest.contentLength || 0) |
|
|
|
pdfFileLength += chunk.byteLength; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
if (source.length && pdfFileLength !== source.length) { |
|
|
|
|
|
|
|
warn('reported HTTP length is different from actual'); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
var pdfFileArray = new Uint8Array(pdfFileLength); |
|
|
|
|
|
|
|
cachedChunks.forEach(function (chunk) { |
|
|
|
|
|
|
|
pdfFileArray.set(new Uint8Array(chunk), pos); |
|
|
|
|
|
|
|
pos += chunk.byteLength; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
pdfFile = pdfFileArray.buffer; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
pdfFile = args.chunk; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the data is array, instantiating directly from it
|
|
|
|
if (pdfManager) { |
|
|
|
try { |
|
|
|
pdfManager.sendProgressiveData(data); |
|
|
|
pdfManager = new LocalPdfManager(docId, pdfFile, source.password); |
|
|
|
} else { |
|
|
|
pdfManagerCapability.resolve(pdfManager); |
|
|
|
cachedChunks.push(data); |
|
|
|
} catch (ex) { |
|
|
|
} |
|
|
|
pdfManagerCapability.reject(ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
cancelXHRs = null; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onError: function onError(status) { |
|
|
|
fullRequest.read().then(readChunk, reject); |
|
|
|
var exception; |
|
|
|
} catch (e) { |
|
|
|
if (status === 404 || status === 0 && /^file:/.test(source.url)) { |
|
|
|
reject(e); |
|
|
|
exception = new MissingPDFException('Missing PDF "' + |
|
|
|
|
|
|
|
source.url + '".'); |
|
|
|
|
|
|
|
handler.send('MissingPDF', exception); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
exception = new UnexpectedResponseException( |
|
|
|
|
|
|
|
'Unexpected server response (' + status + |
|
|
|
|
|
|
|
') while retrieving PDF "' + source.url + '".', status); |
|
|
|
|
|
|
|
handler.send('UnexpectedResponse', exception); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
cancelXHRs = null; |
|
|
|
}; |
|
|
|
}, |
|
|
|
fullRequest.read().then(readChunk, reject); |
|
|
|
|
|
|
|
}); |
|
|
|
onProgress: function onProgress(evt) { |
|
|
|
readPromise.catch(function (e) { |
|
|
|
handler.send('DocProgress', { |
|
|
|
pdfManagerCapability.reject(e); |
|
|
|
loaded: evt.loaded, |
|
|
|
cancelXHRs = null; |
|
|
|
total: evt.lengthComputable ? evt.total : source.length |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
cancelXHRs = function () { |
|
|
|
cancelXHRs = function () { |
|
|
|
networkManager.abortRequest(fullRequestXhrId); |
|
|
|
pdfStream.cancelAllRequests('abort'); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return pdfManagerCapability.promise; |
|
|
|
return pdfManagerCapability.promise; |
|
|
@ -676,6 +990,7 @@ if (typeof window === 'undefined' && |
|
|
|
initializeWorker(); |
|
|
|
initializeWorker(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.setPDFNetworkStreamClass = setPDFNetworkStreamClass; |
|
|
|
exports.WorkerTask = WorkerTask; |
|
|
|
exports.WorkerTask = WorkerTask; |
|
|
|
exports.WorkerMessageHandler = WorkerMessageHandler; |
|
|
|
exports.WorkerMessageHandler = WorkerMessageHandler; |
|
|
|
})); |
|
|
|
})); |
|
|
|