Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3523f66

Browse files
authored
fix(datafile manager): Node datafile requests use gzip,deflate compression (optimizely#456)
Summary Update Node datafile manager request code to support gzip or deflate compression. Uses the decompress-response library. - Add "accept-encoding: gzip,deflate" request header - Decode response body when "content-encoding" response header indicates encoded body Test plan: Manual testing, new unit test, existing unit tests Issues: https://optimizely.atlassian.net/browse/OASIS-6294
1 parent ac3258f commit 3523f66

File tree

8 files changed

+91
-16
lines changed

8 files changed

+91
-16
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,36 @@ This repository is a monorepo that we manage using [Lerna](https://github.com/le
2727
### Contributing
2828

2929
Please see [CONTRIBUTING](CONTRIBUTING.md).
30+
31+
## Credits
32+
33+
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.
34+
35+
## Additional Code
36+
37+
Prod dependencies are as follows:
38+
39+
```json
40+
{
41+
"json-schema@0.2.3": {
42+
"licenses": [
43+
"AFLv2.1",
44+
"BSD"
45+
],
46+
"publisher": "Kris Zyp",
47+
"repository": "https://github.com/kriszyp/json-schema"
48+
},
49+
"murmurhash@0.0.2": {
50+
"licenses": "MIT*",
51+
"repository": "https://github.com/perezd/node-murmurhash"
52+
},
53+
"uuid@3.3.2": {
54+
"licenses": "MIT",
55+
"repository": "https://github.com/kelektiv/node-uuid"
56+
},
57+
"decompress-response@4.2.1": {
58+
"licenses": "MIT",
59+
"repository": "https://github.com/sindresorhus/decompress-response"
60+
}
61+
}
62+
```

packages/datafile-manager/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Changes that have landed but are not yet released.
99

1010
### Breaking Changes
1111
- Removed `StaticDatafileManager` from all top level exports
12+
- Dropped support for Node.js version <8
13+
14+
### Fixed
15+
16+
- Node datafile manager requests use gzip,deflate compression
1217

1318
## [0.4.0] - June 12, 2019
1419

packages/datafile-manager/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

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

5+
## Requirements
6+
In general, an ES5-compatible environment is required, as well as `Promise` (must be polyfilled if absent).
7+
8+
Platform-specific minimum supported versions:
9+
10+
- Node.js: `8`
11+
- React Native: `0.61.5`
12+
513
## Installation
614

715
```sh

packages/datafile-manager/__test__/nodeRequest.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import nock from 'nock';
18+
import zlib from 'zlib';
1819
import { makeGetRequest } from '../src/nodeRequest';
1920
import { advanceTimersByTime } from './testUtils';
2021

@@ -80,6 +81,20 @@ describe('nodeEnvironment', () => {
8081
scope.done();
8182
});
8283

84+
it('adds an Accept-Encoding request header and unzips a gzipped response body', async () => {
85+
const scope = nock(host)
86+
.matchHeader('accept-encoding', 'gzip,deflate')
87+
.get(path)
88+
.reply(200, () => zlib.gzipSync('{"foo":"bar"}'), { 'content-encoding': 'gzip' });
89+
const req = makeGetRequest(`${host}${path}`, {});
90+
const resp = await req.responsePromise;
91+
expect(resp).toMatchObject({
92+
statusCode: 200,
93+
body: '{"foo":"bar"}',
94+
});
95+
scope.done();
96+
});
97+
8398
it('includes headers from the response in the eventual response in the return value', async () => {
8499
const scope = nock(host)
85100
.get(path)

packages/datafile-manager/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/datafile-manager/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
},
4545
"dependencies": {
4646
"@optimizely/js-sdk-logging": "^0.1.0",
47-
"@optimizely/js-sdk-utils": "^0.2.0"
47+
"@optimizely/js-sdk-utils": "^0.2.0",
48+
"decompress-response": "^4.2.1"
4849
},
4950
"peerDependencies": {
5051
"@react-native-community/async-storage": "^1.2.0"

packages/datafile-manager/src/nodeRequest.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import https from 'https';
1919
import url from 'url';
2020
import { Headers, AbortableRequest, Response } from './http';
2121
import { REQUEST_TIMEOUT_MS } from './config';
22+
import decompressResponse from 'decompress-response';
2223

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

77-
incomingMessage.setEncoding('utf8');
78+
const response = decompressResponse(incomingMessage);
79+
80+
response.setEncoding('utf8');
7881

7982
let responseData = '';
80-
incomingMessage.on('data', (chunk: string) => {
83+
response.on('data', (chunk: string) => {
8184
if (!request.aborted) {
8285
responseData += chunk;
8386
}
8487
});
8588

86-
incomingMessage.on('end', () => {
89+
response.on('end', () => {
8790
if (request.aborted) {
8891
return;
8992
}
@@ -131,7 +134,10 @@ export function makeGetRequest(reqUrl: string, headers: Headers): AbortableReque
131134
const requestOptions: http.RequestOptions = {
132135
...getRequestOptionsFromUrl(parsedUrl),
133136
method: 'GET',
134-
headers,
137+
headers: {
138+
...headers,
139+
'accept-encoding': 'gzip,deflate',
140+
},
135141
};
136142

137143
const request = requester(requestOptions);

packages/optimizely-sdk/README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,17 @@ Prod dependencies are as follows:
9696
"publisher": "Kris Zyp",
9797
"repository": "https://github.com/kriszyp/json-schema"
9898
},
99-
"lodash@4.17.10": {
100-
"licenses": "MIT",
101-
"publisher": "John-David Dalton",
102-
"repository": "https://github.com/lodash/lodash"
103-
},
10499
"murmurhash@0.0.2": {
105100
"licenses": "MIT*",
106101
"repository": "https://github.com/perezd/node-murmurhash"
107102
},
108-
"sprintf@0.1.5": {
109-
"licenses": "BSD-3-Clause",
110-
"publisher": "Moritz Peters",
111-
"repository": "https://github.com/maritz/node-sprintf"
112-
},
113-
"uuid@3.2.1": {
103+
"uuid@3.3.2": {
114104
"licenses": "MIT",
115105
"repository": "https://github.com/kelektiv/node-uuid"
106+
},
107+
"decompress-response@4.2.1": {
108+
"licenses": "MIT",
109+
"repository": "https://github.com/sindresorhus/decompress-response"
116110
}
117111
}
118112
```

0 commit comments

Comments
 (0)