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

fix(datafile manager): Node datafile requests use gzip,deflate compression #456

Merged
merged 8 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions packages/datafile-manager/__test__/rawTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HttpPollingDatafileManager } from '../src/index.node';
import { setLogLevel, setLogHandler, ConsoleLogHandler } from '@optimizely/js-sdk-logging';

setLogLevel('debug');
setLogHandler(new ConsoleLogHandler());

const manager = new HttpPollingDatafileManager({
sdkKey: '9LCprAQyd1bs1BBXZ3nVji',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this isn't sensitive at all.

updateInterval: 5000,
});

manager.start();
manager.onReady().then(() => {
console.log('ready');
});

manager.on('update', () => {
console.log('update: ');
});
13 changes: 13 additions & 0 deletions packages/datafile-manager/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/datafile-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
},
"dependencies": {
"@optimizely/js-sdk-logging": "^0.1.0",
"@optimizely/js-sdk-utils": "^0.2.0"
"@optimizely/js-sdk-utils": "^0.2.0",
"decompress-response": "^4.2.1"
},
"peerDependencies": {
"@react-native-community/async-storage": "^1.2.0"
Expand Down
14 changes: 10 additions & 4 deletions packages/datafile-manager/src/nodeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import https from 'https';
import url from 'url';
import { Headers, AbortableRequest, Response } from './http';
import { REQUEST_TIMEOUT_MS } from './config';
import decompressResponse from 'decompress-response';

// Shared signature between http.request and https.request
type ClientRequestCreator = (options: http.RequestOptions) => http.ClientRequest;
Expand Down Expand Up @@ -74,16 +75,18 @@ function getResponseFromRequest(request: http.ClientRequest): Promise<Response>
return;
}

incomingMessage.setEncoding('utf8');
const response = decompressResponse(incomingMessage);

response.setEncoding('utf8');

let responseData = '';
incomingMessage.on('data', (chunk: string) => {
response.on('data', (chunk: string) => {
if (!request.aborted) {
responseData += chunk;
}
});

incomingMessage.on('end', () => {
response.on('end', () => {
if (request.aborted) {
return;
}
Expand Down Expand Up @@ -131,7 +134,10 @@ export function makeGetRequest(reqUrl: string, headers: Headers): AbortableReque
const requestOptions: http.RequestOptions = {
...getRequestOptionsFromUrl(parsedUrl),
method: 'GET',
headers,
headers: {
...headers,
'Accept-Encoding': 'gzip,deflate',
},
};

const request = requester(requestOptions);
Expand Down