Skip to content

Commit

Permalink
Adds tests for node_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mukulmishra18 committed Aug 16, 2017
1 parent b148a29 commit d6e330c
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 81 deletions.
15 changes: 6 additions & 9 deletions src/display/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,16 @@ function PDFNetworkStreamFullRequestReader(manager, options) {
}

PDFNetworkStreamFullRequestReader.prototype = {
getResponseHeader(name) {
let fullRequestXhrId = this._fullRequestId;
let fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);

return fullRequestXhr.getResponseHeader(name);
},

_onHeadersReceived:
function PDFNetworkStreamFullRequestReader_onHeadersReceived() {
var fullRequestXhrId = this._fullRequestId;
var fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);

let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({
getResponseHeader: this.getResponseHeader.bind(this),
getResponseHeader: (name) => {
return fullRequestXhr.getResponseHeader(name);
},
isHttp: this._manager.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
Expand All @@ -376,7 +374,6 @@ PDFNetworkStreamFullRequestReader.prototype = {
}

var networkManager = this._manager;
var fullRequestXhrId = this._fullRequestId;
if (networkManager.isStreamingRequest(fullRequestXhrId)) {
// We can continue fetching when progressive loading is enabled,
// and we don't need the autoFetch feature.
Expand Down
154 changes: 82 additions & 72 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PDFNodeStream {
this.url = url.parse(this.source.url);
this.isHttp = this.url.protocol === 'http:' ||
this.url.protocol === 'https:';
this.isFsUrl = !this.url.host;
this.isFsUrl = this.url.protocol === 'file:' || !this.url.host;
this.httpHeaders = (this.isHttp && this.source.httpHeaders) || {};

this._fullRequest = null;
Expand Down Expand Up @@ -74,7 +74,10 @@ class BaseFullReader {
this._length = stream.source.length;
this._loaded = 0;

this._fullRequest = null;
this._isStreamingSupported = !stream.source.disableStream;
this._isRangeSupported = !stream.options.disableRange;

this._readableStream = null;
this._readCapability = createPromiseCapability();
this._headersCapability = createPromiseCapability();
}
Expand Down Expand Up @@ -104,7 +107,7 @@ class BaseFullReader {
return Promise.reject(this._reason);
}

let chunk = this._fullRequest.read();
let chunk = this._readableStream.read();
if (chunk === null) {
this._readCapability = createPromiseCapability();
return this.read();
Expand All @@ -121,8 +124,7 @@ class BaseFullReader {
}

cancel(reason) {
this._fullRequest.close(reason);
this._fullRequest.destroy(reason);
this._readableStream.destroy(reason);
}
}

Expand All @@ -133,38 +135,14 @@ class BaseRangeReader {
this._errored = false;
this._reason = null;
this.onProgress = null;
this._length = stream.source.length;
this._loaded = 0;
this._readCapability = createPromiseCapability();
}

get isStreamingSupported() {
return false;
this._isStreamingSupported = !stream.source.disableStream;
}

read() {
return this._readCapability.promise.then(() => {
if (this._done) {
return Promise.resolve({ value: undefined, done: true, });
}
if (this._errored) {
return Promise.reject(this._reason);
}

let chunk = this._read();
if (chunk === null) {
this._readCapability = createPromiseCapability();
return this.read();
}
this._loaded += chunk.length;
if (this.onProgress) {
this.onProgress({
loaded: this._loaded,
total: this._length,
});
}
return Promise.resolve({ value: chunk, done: false, });
});
get isStreamingSupported() {
return this._isStreamingSupported;
}
}

Expand All @@ -178,9 +156,6 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._disableRange = true;
}

this._isStreamingSupported = !stream.source.disableStream;
this._isRangeSupported = false;

let options = {
host: this._url.host,
path: this._url.path,
Expand All @@ -190,7 +165,7 @@ class PDFNodeStreamFullReader extends BaseFullReader {

let handleResponse = (response) => {
this._headersCapability.resolve();
this._fullRequest = response;
this._readableStream = response;

response.on('readable', () => {
this._readCapability.resolve();
Expand All @@ -208,20 +183,7 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._reason = reason;
this._readCapability.resolve();
});
};

this._request = this._url.protocol === 'http:' ?
http.request(options, handleResponse) :
https.request(options, handleResponse);

this._request.on('error', (reason) => {
this._errored = true;
this._reason = reason;
this._headersCapability.reject(reason);
});
this._request.end();

this._headersCapability.promise.then(() => {
let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({
getResponseHeader: this.getResponseHeader.bind(this),
Expand All @@ -234,20 +196,30 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._isRangeSupported = true;
}
this._length = suggestedLength;
};

this._request = this._url.protocol === 'http:' ?
http.request(options, handleResponse) :
https.request(options, handleResponse);

this._request.on('error', (reason) => {
this._errored = true;
this._reason = reason;
this._headersCapability.reject(reason);
});
this._request.end();
}

getReasponseHeader(name) {
return this._fullRequest.headers[name];
getResponseHeader(name) {
return this._readableStream.headers[name];
}
}

class PDFNodeStreamRangeReader extends BaseRangeReader {
constructor(stream, start, end) {
super(stream);

this._rangeRequest = null;
this._read = null;
this._readableStream = null;
let rangeStr = start + '-' + (end - 1);
stream.httpHeaders['Range'] = 'bytes=' + rangeStr;

Expand All @@ -258,8 +230,7 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
headers: stream.httpHeaders,
};
let handleResponse = (response) => {
this._rangeRequest = response;
this._read = this._rangeRequest.read;
this._readableStream = response;

response.on('readable', () => {
this._readCapability.resolve();
Expand Down Expand Up @@ -288,19 +259,38 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
this._request.end();
}

read() {
return this._readCapability.promise.then(() => {
if (this._done) {
return Promise.resolve({ value: undefined, done: true, });
}
if (this._errored) {
return Promise.reject(this._reason);
}

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, });
}
return Promise.resolve({ value: chunk, done: false, });
});
}

cancel(reason) {
this._rangeRequest.close(reason);
this._rangeRequest.destroy(reason);
this._readableStream.destroy(reason);
}
}

class PDFNodeStreamFsFullReader extends BaseFullReader {
constructor(stream) {
super(stream);

this._isRangeSupported = true;
this._isStreamingSupported = true;
this._fullRequest = fs.createReadStream(this._url.path);
this._readableStream = fs.createReadStream(this._url.path);

fs.lstat(this._url.path, (error, stat) => {
if (error) {
Expand All @@ -313,17 +303,17 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
this._headersCapability.resolve();
});

this._fullRequest.on('readable', () => {
this._readableStream.on('readable', () => {
this._readCapability.resolve();
});

this._fullRequest.on('end', () => {
this._fullRequest.destroy();
this._readableStream.on('end', () => {
this._readableStream.destroy();
this._done = true;
this._readCapability.resolve();
});

this._fullRequest.on('error', (reason) => {
this._readableStream.on('error', (reason) => {
this._errored = true;
this._reason = reason;
this._readCapability.resolve();
Expand All @@ -335,7 +325,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
constructor(stream, start, end) {
super(stream);

this._rangeRequest = fs.createReadStream(this._url.path, { start, end, });
this._readableStream = fs.createReadStream(this._url.path, { start, end, });
fs.lstat(this._url.path, (error, stat) => {
if (error) {
this._errored = true;
Expand All @@ -344,28 +334,48 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
}
this._length = stat.size;
});
this._read = this._rangeRequest.read;

this._rangeRequest.on('readable', () => {
this._readableStream.on('readable', () => {
this._readCapability.resolve();
});

this._rangeRequest.on('end', () => {
this._rangeRequest.destroy();
this._readableStream.on('end', () => {
this._readableStream.destroy();
this._done = true;
this._readCapability.resolve();
});

this._rangeRequest.on('error', (reason) => {
this._readableStream.on('error', (reason) => {
this._errored = true;
this._reason = reason;
this._readCapability.resolve();
});
}

read() {
return this._readCapability.promise.then(() => {
if (this._done) {
return Promise.resolve({ value: undefined, done: true, });
}
if (this._errored) {
return Promise.reject(this._reason);
}

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, });
}
return Promise.resolve({ value: chunk, done: false, });
});
}

cancel(reason) {
this._rangeRequest.close(reason);
this._rangeRequest.destroy(reason);
this._readableStream.destroy(reason);
}
}

Expand Down
1 change: 1 addition & 0 deletions test/unit/clitests.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"fonts_spec.js",
"function_spec.js",
"murmurhash3_spec.js",
"node_stream_spec.js",
"parser_spec.js",
"primitives_spec.js",
"stream_spec.js",
Expand Down
6 changes: 6 additions & 0 deletions test/unit/jasmine-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
function initializePDFJS(callback) {
Promise.all([
'pdfjs/display/global',
'pdfjs/display/api',
'pdfjs/display/network',
'pdfjs-test/unit/annotation_spec',
'pdfjs-test/unit/api_spec',
'pdfjs-test/unit/bidi_spec',
Expand Down Expand Up @@ -71,7 +73,11 @@ function initializePDFJS(callback) {
return SystemJS.import(moduleName);
})).then(function (modules) {
var displayGlobal = modules[0];
var displayApi = modules[1];
var PDFNetworkStream = modules[2].PDFNetworkStream;

// Set network stream class for unit tests.
displayApi.setPDFNetworkStreamClass(PDFNetworkStream);
// Configure the worker.
displayGlobal.PDFJS.workerSrc = '../../build/generic/build/pdf.worker.js';
// Opt-in to using the latest API.
Expand Down
Loading

0 comments on commit d6e330c

Please sign in to comment.