Skip to content

Commit 2eb2222

Browse files
authored
PDCL-9709 Fix an engine crash when a large response was received by a fetch call. (#9)
* PDCL-9709 Fix an engine crash when a large response was received by a fetch call. * Move lint staged config out of package.json. * npm bin was removed from NPM. Use npx. * Ignore eslit cache file. * Update the files that get added to the NPM package. * Ignore the large response JSON file.
1 parent ee13b42 commit 2eb2222

File tree

10 files changed

+52127
-26
lines changed

10 files changed

+52127
-26
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/__tests_helpers__/largeJsonResponse.json

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
/coverage
33
/dist
44
reactor-turbine-edge-*.tgz
5+
.eslintcache

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
$(npm bin)/pretty-quick --staged && $(npm bin)/lint-staged
4+
npx pretty-quick --staged && npx lint-staged

.lintstagedrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"*.js": [
3+
"eslint --cache --fix"
4+
]
5+
}

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ coverage
22
jest.config.js
33
rollup.config.js
44
reactor-turbine-edge-*.tgz
5-
__tests__
5+
adobe-reactor-turbine-edge*.tgz
6+
__tests*
67
__mocks__
78
\.*

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
"url": "http://adobe.com",
2020
"email": "[email protected]"
2121
},
22-
"lint-staged": {
23-
"*.js": [
24-
"npm run lint -- --fix"
25-
]
26-
},
2722
"repository": {
2823
"type": "git",
2924
"url": "[email protected]:adobe/reactor-turbine-edge.git"

src/__tests__/getRuleFetchFn.test.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@ governing permissions and limitations under the License.
1111

1212
const getRuleFetchFn = require('../getRuleFetchFn');
1313
const createNewLogger = require('../createNewLogger');
14-
15-
const createFakeFetch = (returnStatus = 200) =>
16-
jest.fn((resource) =>
17-
Promise.resolve({
18-
clone: () => ({
19-
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
20-
status: returnStatus
21-
}),
22-
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
23-
status: returnStatus
24-
})
25-
);
14+
const createFakeFetch = require('../__tests_helpers__/createFakeFetchResponse');
2615

2716
describe('getRuleFetchFn', () => {
2817
test('returns a function that will make a successful fetch and returns the response', () => {
@@ -32,11 +21,27 @@ describe('getRuleFetchFn', () => {
3221
return ruleFetchFn('http://www.google.com').then((r) => {
3322
expect(r.status).toBe(200);
3423
return r.arrayBuffer().then((b) => {
35-
expect(b).toBe('http://www.google.com:arrayBuffer');
24+
expect(new TextDecoder('utf-8').decode(b)).toBe(
25+
'http://www.google.com:arrayBuffer'
26+
);
3627
});
3728
});
3829
});
3930

31+
test('returns a function that will make a successful fetch and handles large responses', async () => {
32+
const logger = createNewLogger();
33+
const ruleFetchFn = getRuleFetchFn(
34+
createFakeFetch(200, true),
35+
[],
36+
{},
37+
logger
38+
);
39+
40+
await expect(
41+
ruleFetchFn('http://www.google.com')
42+
).resolves.not.toThrowError();
43+
});
44+
4045
test('returns a function that logs a successful fetch', () => {
4146
const logger = createNewLogger({ ruleId: 1 });
4247
const ruleFetchFn = getRuleFetchFn(createFakeFetch(), [], {}, logger);
@@ -56,7 +61,7 @@ describe('getRuleFetchFn', () => {
5661
'Response Status',
5762
'200',
5863
'Response Body',
59-
'empty'
64+
'http://www.google.com:arrayBuffer'
6065
],
6166
name: 'evaluatingRule',
6267
timestampMs: expect.any(Number)
@@ -86,7 +91,7 @@ describe('getRuleFetchFn', () => {
8691
'Response Status',
8792
'200',
8893
'Response Body',
89-
'empty'
94+
'http://www.google.com:arrayBuffer'
9095
],
9196
name: 'evaluatingRule',
9297
timestampMs: expect.any(Number)
@@ -122,7 +127,7 @@ describe('getRuleFetchFn', () => {
122127
'Response Status',
123128
'200',
124129
'Response Body',
125-
'empty'
130+
'http://www.google.com:arrayBuffer'
126131
],
127132
name: 'evaluatingRule',
128133
timestampMs: expect.any(Number)
@@ -137,6 +142,8 @@ describe('getRuleFetchFn', () => {
137142
const ruleFetchFn = getRuleFetchFn(createFakeFetch(), [], {}, logger);
138143

139144
return ruleFetchFn({
145+
url: 'http://www.google.com',
146+
method: 'POST',
140147
headers: { entries: () => [['X-Id-Resource', 123]] }
141148
}).then(() => {
142149
expect(logger.getJsonLogs()).toStrictEqual([
@@ -147,13 +154,13 @@ describe('getRuleFetchFn', () => {
147154
'🚀',
148155
'FETCH',
149156
'Resource',
150-
'{"headers":{}}',
157+
'{"url":"http://www.google.com","method":"POST","headers":{}}',
151158
'Options',
152159
'{"headers":{"X-Id-Resource":123}}',
153160
'Response Status',
154161
'200',
155162
'Response Body',
156-
'empty'
163+
'http://www.google.com:arrayBuffer'
157164
],
158165
name: 'evaluatingRule',
159166
timestampMs: expect.any(Number)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2020 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software distributed under
7+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
OF ANY KIND, either express or implied. See the License for the specific language
9+
governing permissions and limitations under the License.
10+
*/
11+
12+
const largeJson = require('./largeJsonResponse.json');
13+
14+
module.exports = (returnStatus = 200, largeResponse = false) =>
15+
jest.fn((resource) => {
16+
const response = largeResponse
17+
? JSON.stringify(largeJson)
18+
: `${resource?.url || resource}:arrayBuffer`;
19+
const arrayBuffer = Promise.resolve(new TextEncoder().encode(response));
20+
21+
return Promise.resolve({
22+
clone: () => ({
23+
arrayBuffer: () => arrayBuffer,
24+
status: returnStatus
25+
}),
26+
arrayBuffer: () => arrayBuffer,
27+
status: returnStatus
28+
});
29+
});

0 commit comments

Comments
 (0)