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 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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,36 @@ This repository is a monorepo that we manage using [Lerna](https://github.com/le
### Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md).

## Credits

First-party code (under `packages/optimizely-sdk/lib/`, `packages/datafile-manager/lib`, `packages/datafile-manager/src`, `packages/datafile-manager/__test__`, `packages/event-processor/src`, `packages/event-processor/__tests__`, `packages/logging/src`, `packages/logging/__tests__`, `packages/utils/src`, `packages/utils/__tests__`) is copyright Optimizely, Inc. and contributors, licensed under Apache 2.0.

## Additional Code

Prod dependencies are as follows:

```json
{
"[email protected]": {
"licenses": [
"AFLv2.1",
"BSD"
],
"publisher": "Kris Zyp",
"repository": "https://github.com/kriszyp/json-schema"
},
"[email protected]": {
"licenses": "MIT*",
"repository": "https://github.com/perezd/node-murmurhash"
},
"[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/kelektiv/node-uuid"
},
"[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/sindresorhus/decompress-response"
}
}
```
5 changes: 5 additions & 0 deletions packages/datafile-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Changes that have landed but are not yet released.

### Breaking Changes
- Removed `StaticDatafileManager` from all top level exports
- Dropped support for Node.js version <8

### Fixed

- Node datafile manager requests use gzip,deflate compression

## [0.4.0] - June 12, 2019

Expand Down
8 changes: 8 additions & 0 deletions packages/datafile-manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This package provides datafile manager implementations for Node.js, browsers, and React Native.

## Requirements
In general, an ES5-compatible environment is required, as well as `Promise` (must be polyfilled if absent).

Platform-specific minimum supported versions:

- Node.js: `8`
- React Native: `0.61.5`

## Installation

```sh
Expand Down
15 changes: 15 additions & 0 deletions packages/datafile-manager/__test__/nodeRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import nock from 'nock';
import zlib from 'zlib';
import { makeGetRequest } from '../src/nodeRequest';
import { advanceTimersByTime } from './testUtils';

Expand Down Expand Up @@ -80,6 +81,20 @@ describe('nodeEnvironment', () => {
scope.done();
});

it('adds an Accept-Encoding request header and unzips a gzipped response body', async () => {
const scope = nock(host)
.matchHeader('accept-encoding', 'gzip,deflate')
.get(path)
.reply(200, () => zlib.gzipSync('{"foo":"bar"}'), { 'content-encoding': 'gzip' });
const req = makeGetRequest(`${host}${path}`, {});
const resp = await req.responsePromise;
expect(resp).toMatchObject({
statusCode: 200,
body: '{"foo":"bar"}',
});
scope.done();
});

it('includes headers from the response in the eventual response in the return value', async () => {
const scope = nock(host)
.get(path)
Expand Down
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
16 changes: 5 additions & 11 deletions packages/optimizely-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,17 @@ Prod dependencies are as follows:
"publisher": "Kris Zyp",
"repository": "https://github.com/kriszyp/json-schema"
},
"[email protected]": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, thanks for cleaning this up as well!

"licenses": "MIT",
"publisher": "John-David Dalton",
"repository": "https://github.com/lodash/lodash"
},
"[email protected]": {
"licenses": "MIT*",
"repository": "https://github.com/perezd/node-murmurhash"
},
"[email protected]": {
"licenses": "BSD-3-Clause",
"publisher": "Moritz Peters",
"repository": "https://github.com/maritz/node-sprintf"
},
"[email protected]": {
"[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/kelektiv/node-uuid"
},
"[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/sindresorhus/decompress-response"
}
}
```
Expand Down