Skip to content

Commit

Permalink
feat(utils): add bloat control utility to ensure JSON strings don't g…
Browse files Browse the repository at this point in the history
…et too long
  • Loading branch information
trieloff committed Aug 16, 2024
1 parent 802ef77 commit cee29ea
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,38 @@ export function getSubsystem(req) {
}
return 'undefined';
}

/**
* Cloudflare applies a 16kb limit to all log messages, so we need to make sure
* that the JSON we send to the logger is not too large. This function will
* apply a couple of tricks to make this happen
* 1. limit the length of any string value to 1024 characters
* 2. cast LCP, INP, TTFB to integers
* @param {Object} obj an object to be logged
* @returns {String} the sanitized object as a JSON string
*/
export function bloatControl(obj) {
// the object can have nested objects, so we may need recursion
const sanitize = (o, k) => {
if (typeof o === 'string' && o.length > 1024) {
return `${o.substring(0, 1024)}…`;
}
if (typeof o === 'number') {
if (['LCP', 'INP', 'TTFB'].includes(k)) {
return Math.floor(o);
}
return o;
}
if (Array.isArray(o)) {
return o.map(sanitize);
}
if (typeof o === 'object') {
return Object.entries(o).reduce((acc, [key, value]) => {
acc[key] = sanitize(value, key);
return acc;
}, {});
}
return o;
};
return JSON.stringify(sanitize(obj));
}
23 changes: 23 additions & 0 deletions test/utils.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,32 @@ import {
getSubsystem,
isValidId,
maskTime,
bloatControl,
} from '../src/utils.mjs';

describe('Test Utils', () => {
it('Bloat control', () => {
assert.equal('{}', bloatControl({}));
assert.equal('[]', bloatControl([]));
assert.equal('{"lorem":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac molestie ex. Suspendisse a imperdiet enim. Nulla tempor, tellus volutpat dictum auctor, purus justo consequat felis, sit amet condimentum odio urna ornare dolor. Pellentesque eget ultrices libero. Suspendisse quis diam eu augue consectetur lobortis at et leo. Quisque efficitur sit amet sem id aliquet. In hac habitasse platea dictumst. Maecenas sed orci tincidunt, tempus diam lobortis, egestas nibh. Nulla arcu purus, fermentum vitae augue vel, sodales porttitor arcu. Quisque iaculis porttitor lectus, id rhoncus arcu sollicitudin lobortis.\\n\\nMauris massa leo, feugiat ac congue sit amet, auctor id arcu. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vestibulum hendrerit urna sit amet quam accumsan consequat quis id diam. Morbi scelerisque a diam in mollis. Ut at convallis diam, a accumsan sem. Curabitur molestie sem nec orci mattis, ut convallis metus pellentesque. Morbi vitae erat felis. Curabitur purus n…"}', bloatControl({
lorem: `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac molestie ex. Suspendisse a imperdiet enim. Nulla tempor, tellus volutpat dictum auctor, purus justo consequat felis, sit amet condimentum odio urna ornare dolor. Pellentesque eget ultrices libero. Suspendisse quis diam eu augue consectetur lobortis at et leo. Quisque efficitur sit amet sem id aliquet. In hac habitasse platea dictumst. Maecenas sed orci tincidunt, tempus diam lobortis, egestas nibh. Nulla arcu purus, fermentum vitae augue vel, sodales porttitor arcu. Quisque iaculis porttitor lectus, id rhoncus arcu sollicitudin lobortis.
Mauris massa leo, feugiat ac congue sit amet, auctor id arcu. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vestibulum hendrerit urna sit amet quam accumsan consequat quis id diam. Morbi scelerisque a diam in mollis. Ut at convallis diam, a accumsan sem. Curabitur molestie sem nec orci mattis, ut convallis metus pellentesque. Morbi vitae erat felis. Curabitur purus nulla, tempus non arcu ut, convallis euismod justo. Etiam hendrerit risus ligula, a mattis libero placerat eu. Proin interdum elit quam. In sit amet dictum sapien. Duis tempor vulputate tellus. Nam fermentum ligula a nibh facilisis, et eleifend mi euismod.
Pellentesque viverra id magna vel varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec porta quis mauris sit amet aliquet. Duis non nulla sed metus sagittis condimentum. Nam rhoncus, risus et gravida tempus, nibh diam pellentesque tellus, vel accumsan arcu nibh in lorem. Pellentesque eu semper ipsum, ac lacinia ante. Phasellus neque urna, laoreet eu purus id, interdum tristique turpis. Nulla pretium fermentum elit non tristique. Aliquam ut orci elit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Sed a pulvinar urna. Nunc vel augue ultrices, aliquet sapien eget, ornare sem. Etiam sagittis porttitor arcu vitae tristique. Praesent tempus neque ac vehicula viverra. Donec justo nibh, faucibus a rhoncus nec.`,
}));

assert.equal('{"LCP":1234,"INP":2345,"CLS":0.789,"TTFB":1234}', bloatControl({
LCP: 1234.5678,
INP: 2345.6789,
CLS: 0.7890,
TTFB: 1234.5678,
}));

// what is neither a string nor a number nor an array nor an object gets stringified as is
assert.equal('true', bloatControl(true));
});

it('Mask the time', () => {
const now = Date.now();
const nearestHour = Math.floor(now / 3600000) * 3600000;
Expand Down

0 comments on commit cee29ea

Please sign in to comment.