pdf.js/src/display/node_stream.js

422 lines
11 KiB
JavaScript
Raw Normal View History

/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals __non_webpack_require__ */
let fs = __non_webpack_require__('fs');
2017-07-30 23:58:32 +09:00
let http = __non_webpack_require__('http');
let https = __non_webpack_require__('https');
let url = __non_webpack_require__('url');
import {
AbortException, assert, createPromiseCapability
} from '../shared/util';
import {
extractFilenameFromHeader, validateRangeRequestCapabilities
} from './network_utils';
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
class PDFNodeStream {
constructor(source) {
this.source = source;
this.url = url.parse(source.url);
2017-07-30 23:58:32 +09:00
this.isHttp = this.url.protocol === 'http:' ||
this.url.protocol === 'https:';
2017-08-05 04:30:37 +09:00
// Check if url refers to filesystem.
this.isFsUrl = this.url.protocol === 'file:' || !this.url.host;
this.httpHeaders = (this.isHttp && source.httpHeaders) || {};
2017-07-30 23:58:32 +09:00
this._fullRequest = null;
this._rangeRequestReaders = [];
}
getFullReader() {
assert(!this._fullRequest);
2017-07-30 23:58:32 +09:00
this._fullRequest = this.isFsUrl ?
new PDFNodeStreamFsFullReader(this) :
new PDFNodeStreamFullReader(this);
return this._fullRequest;
}
2017-07-30 23:58:32 +09:00
getRangeReader(start, end) {
let rangeReader = this.isFsUrl ?
new PDFNodeStreamFsRangeReader(this, start, end) :
new PDFNodeStreamRangeReader(this, start, end);
this._rangeRequestReaders.push(rangeReader);
return rangeReader;
}
cancelAllRequests(reason) {
if (this._fullRequest) {
this._fullRequest.cancel(reason);
}
let readers = this._rangeRequestReaders.slice(0);
readers.forEach(function(reader) {
reader.cancel(reason);
});
}
}
2017-07-30 23:58:32 +09:00
class BaseFullReader {
constructor(stream) {
this._url = stream.url;
this._done = false;
this._errored = false;
this._reason = null;
this._fileName = null;
this.onProgress = null;
let source = stream.source;
this._contentLength = source.length; // optional
this._loaded = 0;
2017-07-30 23:58:32 +09:00
this._disableRange = source.disableRange || false;
this._rangeChunkSize = source.rangeChunkSize;
2017-08-05 04:30:37 +09:00
if (!this._rangeChunkSize && !this._disableRange) {
this._disableRange = true;
}
this._isStreamingSupported = !source.disableStream;
this._isRangeSupported = !source.disableRange;
2017-08-05 04:30:37 +09:00
this._readableStream = null;
this._readCapability = createPromiseCapability();
this._headersCapability = createPromiseCapability();
}
get headersReady() {
return this._headersCapability.promise;
}
get contentLength() {
2017-08-05 04:30:37 +09:00
return this._contentLength;
}
get isRangeSupported() {
2017-07-30 23:58:32 +09:00
return this._isRangeSupported;
}
get isStreamingSupported() {
2017-07-30 23:58:32 +09:00
return this._isStreamingSupported;
}
get fileName() {
return this._fileName;
}
read() {
return this._readCapability.promise.then(() => {
if (this._done) {
return Promise.resolve({ value: undefined, done: true, });
}
if (this._errored) {
return Promise.reject(this._reason);
}
2017-08-05 04:30:37 +09:00
let chunk = this._readableStream.read();
if (chunk === null) {
this._readCapability = createPromiseCapability();
return this.read();
}
this._loaded += chunk.length;
if (this.onProgress) {
this.onProgress({
loaded: this._loaded,
2017-08-05 04:30:37 +09:00
total: this._contentLength,
});
}
2017-08-05 04:30:37 +09:00
// Ensure that `read()` method returns ArrayBuffer.
let buffer = new Uint8Array(chunk).buffer;
return Promise.resolve({ value: buffer, done: false, });
});
}
cancel(reason) {
2017-08-05 04:30:37 +09:00
// Call `this._error()` method when cancel is called
// before _readableStream is set.
if (!this._readableStream) {
this._error(reason);
return;
}
this._readableStream.destroy(reason);
}
_error(reason) {
this._errored = true;
this._reason = reason;
this._readCapability.resolve();
}
_setReadableStream(readableStream) {
this._readableStream = readableStream;
readableStream.on('readable', () => {
this._readCapability.resolve();
});
readableStream.on('end', () => {
// Destroy readable to minimize resource usage.
readableStream.destroy();
this._done = true;
this._readCapability.resolve();
});
readableStream.on('error', (reason) => {
this._error(reason);
});
// We need to stop reading when range is supported and streaming is
// disabled.
if (!this._isStreamingSupported && this._isRangeSupported) {
this._error(new AbortException('streaming is disabled'));
}
2017-08-05 04:30:37 +09:00
// Destroy ReadableStream if already in errored state.
if (this._errored) {
this._readableStream.destroy(this._reason);
}
}
}
2017-07-30 23:58:32 +09:00
class BaseRangeReader {
constructor(stream) {
this._url = stream.url;
this._done = false;
this._errored = false;
this._reason = null;
this.onProgress = null;
this._loaded = 0;
2017-08-05 04:30:37 +09:00
this._readableStream = null;
this._readCapability = createPromiseCapability();
let source = stream.source;
this._isStreamingSupported = !source.disableStream;
}
get isStreamingSupported() {
2017-08-05 04:30:37 +09:00
return this._isStreamingSupported;
}
read() {
return this._readCapability.promise.then(() => {
if (this._done) {
return Promise.resolve({ value: undefined, done: true, });
}
if (this._errored) {
return Promise.reject(this._reason);
}
2017-08-05 04:30:37 +09:00
let chunk = this._readableStream.read();
if (chunk === null) {
this._readCapability = createPromiseCapability();
return this.read();
}
this._loaded += chunk.length;
if (this.onProgress) {
2017-08-05 04:30:37 +09:00
this.onProgress({ loaded: this._loaded, });
}
2017-08-05 04:30:37 +09:00
// Ensure that `read()` method returns ArrayBuffer.
let buffer = new Uint8Array(chunk).buffer;
return Promise.resolve({ value: buffer, done: false, });
});
}
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
cancel(reason) {
// Call `this._error()` method when cancel is called
// before _readableStream is set.
if (!this._readableStream) {
this._error(reason);
return;
2017-07-30 23:58:32 +09:00
}
2017-08-05 04:30:37 +09:00
this._readableStream.destroy(reason);
}
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
_error(reason) {
this._errored = true;
this._reason = reason;
this._readCapability.resolve();
}
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
_setReadableStream(readableStream) {
this._readableStream = readableStream;
readableStream.on('readable', () => {
this._readCapability.resolve();
});
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
readableStream.on('end', () => {
// Destroy readableStream to minimize resource usage.
readableStream.destroy();
this._done = true;
this._readCapability.resolve();
});
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
readableStream.on('error', (reason) => {
this._error(reason);
});
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
// Destroy readableStream if already in errored state.
if (this._errored) {
this._readableStream.destroy(this._reason);
}
}
}
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
function createRequestOptions(url, headers) {
return {
protocol: url.protocol,
auth: url.auth,
host: url.hostname,
port: url.port,
path: url.path,
method: 'GET',
headers,
};
}
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
class PDFNodeStreamFullReader extends BaseFullReader {
constructor(stream) {
super(stream);
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
let handleResponse = (response) => {
this._headersCapability.resolve();
this._setReadableStream(response);
2017-07-30 23:58:32 +09:00
const getResponseHeader = (name) => {
// Make sure that headers name are in lower case, as mentioned
// here: https://nodejs.org/api/http.html#http_message_headers.
return this._readableStream.headers[name.toLowerCase()];
};
2017-07-30 23:58:32 +09:00
let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({
getResponseHeader,
2017-07-30 23:58:32 +09:00
isHttp: stream.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
});
if (allowRangeRequests) {
this._isRangeSupported = true;
}
2017-08-05 04:30:37 +09:00
// Setting right content length.
this._contentLength = suggestedLength;
// Setting the file name from the response header
this._fileName = extractFilenameFromHeader(getResponseHeader);
2017-08-05 04:30:37 +09:00
};
this._request = null;
if (this._url.protocol === 'http:') {
this._request = http.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse);
2017-08-05 04:30:37 +09:00
} else {
this._request = https.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse);
2017-08-05 04:30:37 +09:00
}
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
this._request.on('error', (reason) => {
this._errored = true;
this._reason = reason;
this._headersCapability.reject(reason);
});
// Note: `request.end(data)` is used to write `data` to request body
// and notify end of request. But one should always call `request.end()`
// even if there is no data to write -- (to notify the end of request).
this._request.end();
2017-07-30 23:58:32 +09:00
}
}
class PDFNodeStreamRangeReader extends BaseRangeReader {
constructor(stream, start, end) {
super(stream);
2017-08-05 04:30:37 +09:00
this._httpHeaders = {};
for (let property in stream.httpHeaders) {
let value = stream.httpHeaders[property];
if (typeof value === 'undefined') {
continue;
}
this._httpHeaders[property] = value;
}
this._httpHeaders['Range'] = `bytes=${start}-${end - 1}`;
2017-07-30 23:58:32 +09:00
2017-08-05 04:30:37 +09:00
this._request = null;
if (this._url.protocol === 'http:') {
this._request = http.request(createRequestOptions(
this._url, this._httpHeaders), (response) => {
this._setReadableStream(response);
});
} else {
this._request = https.request(createRequestOptions(
this._url, this._httpHeaders), (response) => {
this._setReadableStream(response);
});
}
2017-07-30 23:58:32 +09:00
this._request.on('error', (reason) => {
this._errored = true;
this._reason = reason;
});
this._request.end();
}
}
class PDFNodeStreamFsFullReader extends BaseFullReader {
constructor(stream) {
super(stream);
let path = decodeURIComponent(this._url.path);
2017-07-30 23:58:32 +09:00
// Remove the extra slash to get right path from url like `file:///C:/`
if (fileUriRegex.test(this._url.href)) {
path = path.replace(/^\//, '');
}
fs.lstat(path, (error, stat) => {
2017-07-30 23:58:32 +09:00
if (error) {
this._errored = true;
this._reason = error;
this._headersCapability.reject(error);
return;
}
2017-08-05 04:30:37 +09:00
// Setting right content length.
this._contentLength = stat.size;
this._setReadableStream(fs.createReadStream(path));
2017-07-30 23:58:32 +09:00
this._headersCapability.resolve();
});
}
}
class PDFNodeStreamFsRangeReader extends BaseRangeReader {
constructor(stream, start, end) {
super(stream);
let path = decodeURIComponent(this._url.path);
// Remove the extra slash to get right path from url like `file:///C:/`
if (fileUriRegex.test(this._url.href)) {
path = path.replace(/^\//, '');
}
2017-08-05 04:30:37 +09:00
this._setReadableStream(
fs.createReadStream(path, { start, end: end - 1, }));
}
}
2017-07-30 23:58:32 +09:00
export {
PDFNodeStream,
};