Skip to content

Commit

Permalink
Fixes fetch and node behavior when disableAutoFetch adn disableStream…
Browse files Browse the repository at this point in the history
… is used.
  • Loading branch information
yurydelendik committed Aug 30, 2017
1 parent 3516a59 commit 3cff7da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/display/fetch_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* limitations under the License.
*/

import { assert, createPromiseCapability } from '../shared/util';
import {
AbortException, assert, createPromiseCapability
} from '../shared/util';
import {
createResponseStatusError, validateRangeRequestCapabilities,
validateResponseStatus
Expand Down Expand Up @@ -95,8 +97,8 @@ class PDFFetchStreamReader {
if (!validateResponseStatus(response.status, this._stream.isHttp)) {
throw createResponseStatusError(response.status, url);
}
this._headersCapability.resolve();
this._reader = response.body.getReader();
this._headersCapability.resolve();

let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({
Expand All @@ -110,6 +112,12 @@ class PDFFetchStreamReader {

this._contentLength = suggestedLength;
this._isRangeSupported = allowRangeRequests;

// We need to stop reading when range is supported and streaming is
// disabled.
if (!this._isStreamingSupported && this._isRangeSupported) {
this.cancel(new AbortException('streaming is disabled'));
}
}).catch(this._headersCapability.reject);

this.onProgress = null;
Expand Down
14 changes: 11 additions & 3 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ let http = __non_webpack_require__('http');
let https = __non_webpack_require__('https');
let url = __non_webpack_require__('url');

import { assert, createPromiseCapability } from '../shared/util';
import {
AbortException, assert, createPromiseCapability
} from '../shared/util';
import { validateRangeRequestCapabilities } from './network_utils';

class PDFNodeStream {
Expand Down Expand Up @@ -165,6 +167,12 @@ class BaseFullReader {
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'));
}

// Destroy ReadableStream if already in errored state.
if (this._errored) {
this._readableStream.destroy(this._reason);
Expand Down Expand Up @@ -353,8 +361,6 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
constructor(stream) {
super(stream);

this._setReadableStream(fs.createReadStream(this._url.path));

fs.lstat(this._url.path, (error, stat) => {
if (error) {
this._errored = true;
Expand All @@ -364,6 +370,8 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
}
// Setting right content length.
this._contentLength = stat.size;

this._setReadableStream(fs.createReadStream(this._url.path));
this._headersCapability.resolve();
});
}
Expand Down

0 comments on commit 3cff7da

Please sign in to comment.