Skip to content

Commit

Permalink
fix(core): native fetch patch should not reuse logger instance
Browse files Browse the repository at this point in the history
  • Loading branch information
eliangcs committed Dec 20, 2024
1 parent 37b2b61 commit 26afeb4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/core/src/tools/fetch-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const stringifyResponseContent = async (response) => {
// global.fetch = wrapFetchWithLogger(global.fetch, logger);
const wrapFetchWithLogger = (fetchFunc, logger) => {
if (fetchFunc.patchedByZapier) {
// Important not to reuse logger between calls, because we always destroy
// the logger at the end of a Lambda call.
fetchFunc.zapierLogger = logger;
return fetchFunc;
}

Expand All @@ -82,25 +85,29 @@ const wrapFetchWithLogger = (fetchFunc, logger) => {
if (requestInfo && !isZapierUserAgent(requestInfo.headers)) {
const responseContentType = response.headers.get('content-type');

logger(`${response.status} ${requestInfo.method} ${requestInfo.url}`, {
log_type: 'http',
request_type: 'patched-devplatform-outbound',
request_url: requestInfo.url,
request_method: requestInfo.method,
request_headers: requestInfo.headers,
request_data: requestInfo.data,
request_via_client: false,
response_status_code: response.status,
response_headers: Object.fromEntries(response.headers.entries()),
response_content: shouldIncludeResponseContent(responseContentType)
? await stringifyResponseContent(response)
: '<unsupported format>',
});
newFetch.zapierLogger(
`${response.status} ${requestInfo.method} ${requestInfo.url}`,
{
log_type: 'http',
request_type: 'patched-devplatform-outbound',
request_url: requestInfo.url,
request_method: requestInfo.method,
request_headers: requestInfo.headers,
request_data: requestInfo.data,
request_via_client: false,
response_status_code: response.status,
response_headers: Object.fromEntries(response.headers.entries()),
response_content: shouldIncludeResponseContent(responseContentType)
? await stringifyResponseContent(response)
: '<unsupported format>',
},
);
}
return response;
};

newFetch.patchedByZapier = true;
newFetch.zapierLogger = logger;
return newFetch;
};

Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/tools/fetch-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,27 @@ describe('wrap fetch with logger', () => {
// No logs should be created
assert.equal(logs.length, 0);
});

it('should not reuse logger between calls', async () => {
const otherLogs = [];
const anotherLogger = (message, data) => {
otherLogs.push({ message, data });
};
const evenNewerFetch = wrapFetchWithLogger(newFetch, anotherLogger);

const url = `${HTTPBIN_URL}/get`;
const response = await evenNewerFetch(url);

assert.equal(response.status, 200);
assert.equal(logs.length, 0);

assert.equal(otherLogs.length, 1);

const log = otherLogs[0];
assert.equal(log.message, `200 GET ${url}`);
assert.equal(log.data.request_url, url);
assert.equal(log.data.request_method, 'GET');
assert.equal(log.data.request_data, '');
assert.equal(log.data.response_status_code, 200);
});
});

0 comments on commit 26afeb4

Please sign in to comment.