Skip to content

Commit b2afa8e

Browse files
committed
Allow the request body to be read by action modules.
1 parent 94dd2d5 commit b2afa8e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/__tests__/getRuleFetchFn.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const createNewLogger = require('../createNewLogger');
1414

1515
const createFakeFetch = (returnStatus = 200) => (resource) =>
1616
Promise.resolve({
17+
clone: () => ({
18+
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
19+
status: returnStatus
20+
}),
1721
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
1822
status: returnStatus
1923
});

src/__tests__/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const containerInitFunction = require('../__mocks__/containerInitFunction');
1414

1515
const globalFetch = (resource) =>
1616
Promise.resolve({
17+
clone: () => ({
18+
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
19+
status: 200
20+
}),
1721
arrayBuffer: () => Promise.resolve(`${resource}:arrayBuffer`),
1822
status: 200
1923
});

src/getRuleFetchFn.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ module.exports = (globalFetch, headersForSubrequests, logger) => {
3535

3636
return globalFetch(resource, init).then(
3737
(r) => {
38+
// Below we will read the body of the response. The body can be read only once.
39+
// We are cloning the response and sending it down to the actions so in case
40+
// they also need to read the response, they can do it.
41+
const clonedResponse = r.clone();
42+
3843
// We read r.text() in order to get rid of the potential CF warning:
3944
// A stalled HTTP response was canceled to prevent deadlock. This can happen when a
4045
// Worker calls fetch() or cache.match() several times without reading the bodies
@@ -61,7 +66,7 @@ module.exports = (globalFetch, headersForSubrequests, logger) => {
6166
body || 'empty'
6267
);
6368

64-
return r;
69+
return clonedResponse;
6570
});
6671
},
6772
(e) => {

0 commit comments

Comments
 (0)