Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: track encoding type for documents #911

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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