Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit debug logs to 4096 bytes on vercel to avoid errors #1598

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-walls-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Logs that exceed maximum allowed length on Vercel deployments will now be truncated to max length exactly
20 changes: 20 additions & 0 deletions packages/nextjs/src/utils/debugLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,24 @@ describe('withLogger', () => {
expect(logger.commit).toHaveBeenCalled();
}
});

it('should truncate bytes to 4096 when deploying on vercel', () => {
// setup: mock vercel environment, mock console log so we can intercept its value
process.env.VERCEL = 'true';
const oldConsoleLog = console.log.bind(console);
const log = jest.fn();
console.log = log;

const veryLongString = new Array(6000).join('a');
const handler = withLogger('test-logger', logger => () => {
logger.enable();
logger.debug(veryLongString);
});
handler();
expect(log.mock.calls[0][0]).toHaveLength(4096);

// restore original console log and reset environment value
process.env.VERCEL = undefined;
console.log = oldConsoleLog;
});
});
27 changes: 21 additions & 6 deletions packages/nextjs/src/utils/debugLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ export const createDebugLogger = (name: string, formatter: (val: LogEntry) => st
},
commit: () => {
if (isEnabled) {
console.log(
`Clerk debug start :: ${name}\n${entries
.map(log => formatter(log))
.map(e => `-- ${e}\n`)
.join('')}`,
);
const log = `Clerk debug start :: ${name}\n${entries
.map(log => formatter(log))
.map(e => `-- ${e}\n`)
.join('')}`;
if (process.env.VERCEL) {
console.log(truncate(log, 4096));
} else {
console.log(log);
}
}
},
};
Expand Down Expand Up @@ -73,3 +76,15 @@ export const withLogger: WithLogger = (loggerFactoryOrName, handlerCtor) => {
}
}) as ReturnType<typeof handlerCtor>;
};

// ref: https://stackoverflow.com/questions/57769465/javascript-truncate-text-by-bytes-length
function truncate(str: string, maxLength: number) {
const encoder = new TextEncoder();
const decoder = new TextDecoder('utf-8');

const encodedString = encoder.encode(str);
const truncatedString = encodedString.slice(0, maxLength);

// return the truncated string, removing any replacement characters that result from partially truncated characters
return decoder.decode(truncatedString).replace(/\uFFFD/g, '');
}