Skip to content

Commit

Permalink
Chore: track encoding type for documents (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Feb 5, 2019
1 parent c2f5d97 commit d2a0e46
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ class Preview extends EventEmitter {
}

// Log now that loading is finished
this.emitLoadMetrics();
this.emitLoadMetrics(data);

// Show download and print buttons if user can download
if (canDownload(this.file, this.options)) {
Expand Down Expand Up @@ -1522,9 +1522,10 @@ class Preview extends EventEmitter {
* A value of 0 means that the load milestone was never reached.
*
* @private
* @param {string} [encoding] - Type of encoding applied to the downloaded content. ie) GZIP
* @return {void}
*/
emitLoadMetrics() {
emitLoadMetrics({ encoding } = {}) {
if (!this.file || !this.file.id) {
return;
}
Expand All @@ -1544,6 +1545,7 @@ class Preview extends EventEmitter {
const total = times.reduce((acc, current) => acc + current);

const event = {
encoding,
event_name: LOAD_METRIC.previewLoadEvent,
value: total, // Sum of all available load times.
[LOAD_METRIC.fileInfoTime]: times[0],
Expand Down
10 changes: 9 additions & 1 deletion src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PreviewError from '../PreviewError';
import DownloadReachability from '../DownloadReachability';
import * as file from '../file';
import * as util from '../util';
import { API_HOST, CLASS_NAVIGATION_VISIBILITY, PERMISSION_PREVIEW } from '../constants';
import { API_HOST, CLASS_NAVIGATION_VISIBILITY, PERMISSION_PREVIEW, ENCODING_TYPES } from '../constants';
import { VIEWER_EVENT, ERROR_CODE, LOAD_METRIC, PREVIEW_METRIC } from '../events';
import Timer from '../Timer';

Expand Down Expand Up @@ -2387,6 +2387,14 @@ describe('lib/Preview', () => {
});
preview.emitLoadMetrics();
});

it('should append encoding field to load metric, when provided', (done) => {
preview.once(PREVIEW_METRIC, (metric) => {
expect(metric.encoding).to.equal(ENCODING_TYPES.GZIP);
done();
});
preview.emitLoadMetrics({ encoding: ENCODING_TYPES.GZIP });
});
});

describe('getRequestHeaders()', () => {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class DocBaseViewer extends BaseViewer {
// Public
//--------------------------------------------------------------------------

/** @property {string} - Tracks the type of encoding, if applicable, that was requested for the viewable content */
encoding;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -571,6 +574,9 @@ class DocBaseViewer extends BaseViewer {
// Use chunk size set in viewer options if available
let rangeChunkSize = this.getViewerOption('rangeChunkSize');

// If range requests are disabled, request the gzip compressed version of the representation
this.encoding = PDFJS.disableRange ? ENCODING_TYPES.GZIP : undefined;

// Otherwise, use large chunk size if locale is en-US and the default,
// smaller chunk size if not. This is using a rough assumption that
// en-US users have higher bandwidth to Box.
Expand All @@ -583,10 +589,10 @@ class DocBaseViewer extends BaseViewer {

let url = pdfUrl;

// If range requests are disabled, request the gzip compressed version of the representation
if (PDFJS.disableRange) {
// Apply encoding request to the content request
if (this.encoding) {
url = appendQueryParams(url, {
[QUERY_PARAM_ENCODING]: ENCODING_TYPES.GZIP
[QUERY_PARAM_ENCODING]: this.encoding
});
}

Expand Down Expand Up @@ -996,6 +1002,7 @@ class DocBaseViewer extends BaseViewer {
if (!this.loaded) {
this.loaded = true;
this.emit(VIEWER_EVENT.load, {
encoding: this.encoding,
numPages: pagesCount,
endProgress: false, // Indicate that viewer will end progress later
scale: currentScale
Expand Down
2 changes: 2 additions & 0 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1569,9 +1569,11 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
};
docBase.loaded = false;
docBase.pdfViewer.pagesCount = 5;
docBase.encoding = 'gzip';

docBase.pagesinitHandler();
expect(stubs.emit).to.be.calledWith(VIEWER_EVENT.load, {
encoding: docBase.encoding,
endProgress: false,
numPages: 5,
scale: sinon.match.any
Expand Down

0 comments on commit d2a0e46

Please sign in to comment.