Skip to content

Commit

Permalink
PDCL-9709 Fix an engine crash when a large response was received by a…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
dompuiu authored Dec 12, 2022
1 parent ee13b42 commit 2eb2222
Show file tree
Hide file tree
Showing 10 changed files with 52,127 additions and 26 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/__tests_helpers__/largeJsonResponse.json
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
/coverage
/dist
reactor-turbine-edge-*.tgz
.eslintcache
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

$(npm bin)/pretty-quick --staged && $(npm bin)/lint-staged
npx pretty-quick --staged && npx lint-staged
5 changes: 5 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.js": [
"eslint --cache --fix"
]
}
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ coverage
jest.config.js
rollup.config.js
reactor-turbine-edge-*.tgz
__tests__
adobe-reactor-turbine-edge*.tgz
__tests*
__mocks__
\.*
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
"url": "http://adobe.com",
"email": "[email protected]"
},
"lint-staged": {
"*.js": [
"npm run lint -- --fix"
]
},
"repository": {
"type": "git",
"url": "[email protected]:adobe/reactor-turbine-edge.git"
Expand Down
43 changes: 25 additions & 18 deletions src/__tests__/getRuleFetchFn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ governing permissions and limitations under the License.

const getRuleFetchFn = require('../getRuleFetchFn');
const createNewLogger = require('../createNewLogger');

const createFakeFetch = (returnStatus = 200) =>
jest.fn((resource) =>
Promise.resolve({
clone: () => ({
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
status: returnStatus
}),
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
status: returnStatus
})
);
const createFakeFetch = require('../__tests_helpers__/createFakeFetchResponse');

describe('getRuleFetchFn', () => {
test('returns a function that will make a successful fetch and returns the response', () => {
Expand All @@ -32,11 +21,27 @@ describe('getRuleFetchFn', () => {
return ruleFetchFn('http://www.google.com').then((r) => {
expect(r.status).toBe(200);
return r.arrayBuffer().then((b) => {
expect(b).toBe('http://www.google.com:arrayBuffer');
expect(new TextDecoder('utf-8').decode(b)).toBe(
'http://www.google.com:arrayBuffer'
);
});
});
});

test('returns a function that will make a successful fetch and handles large responses', async () => {
const logger = createNewLogger();
const ruleFetchFn = getRuleFetchFn(
createFakeFetch(200, true),
[],
{},
logger
);

await expect(
ruleFetchFn('http://www.google.com')
).resolves.not.toThrowError();
});

test('returns a function that logs a successful fetch', () => {
const logger = createNewLogger({ ruleId: 1 });
const ruleFetchFn = getRuleFetchFn(createFakeFetch(), [], {}, logger);
Expand All @@ -56,7 +61,7 @@ describe('getRuleFetchFn', () => {
'Response Status',
'200',
'Response Body',
'empty'
'http://www.google.com:arrayBuffer'
],
name: 'evaluatingRule',
timestampMs: expect.any(Number)
Expand Down Expand Up @@ -86,7 +91,7 @@ describe('getRuleFetchFn', () => {
'Response Status',
'200',
'Response Body',
'empty'
'http://www.google.com:arrayBuffer'
],
name: 'evaluatingRule',
timestampMs: expect.any(Number)
Expand Down Expand Up @@ -122,7 +127,7 @@ describe('getRuleFetchFn', () => {
'Response Status',
'200',
'Response Body',
'empty'
'http://www.google.com:arrayBuffer'
],
name: 'evaluatingRule',
timestampMs: expect.any(Number)
Expand All @@ -137,6 +142,8 @@ describe('getRuleFetchFn', () => {
const ruleFetchFn = getRuleFetchFn(createFakeFetch(), [], {}, logger);

return ruleFetchFn({
url: 'http://www.google.com',
method: 'POST',
headers: { entries: () => [['X-Id-Resource', 123]] }
}).then(() => {
expect(logger.getJsonLogs()).toStrictEqual([
Expand All @@ -147,13 +154,13 @@ describe('getRuleFetchFn', () => {
'🚀',
'FETCH',
'Resource',
'{"headers":{}}',
'{"url":"http://www.google.com","method":"POST","headers":{}}',
'Options',
'{"headers":{"X-Id-Resource":123}}',
'Response Status',
'200',
'Response Body',
'empty'
'http://www.google.com:arrayBuffer'
],
name: 'evaluatingRule',
timestampMs: expect.any(Number)
Expand Down
29 changes: 29 additions & 0 deletions src/__tests_helpers__/createFakeFetchResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const largeJson = require('./largeJsonResponse.json');

module.exports = (returnStatus = 200, largeResponse = false) =>
jest.fn((resource) => {
const response = largeResponse
? JSON.stringify(largeJson)
: `${resource?.url || resource}:arrayBuffer`;
const arrayBuffer = Promise.resolve(new TextEncoder().encode(response));

return Promise.resolve({
clone: () => ({
arrayBuffer: () => arrayBuffer,
status: returnStatus
}),
arrayBuffer: () => arrayBuffer,
status: returnStatus
});
});
Loading

0 comments on commit 2eb2222

Please sign in to comment.