Skip to content

Commit

Permalink
fix(NODE-5839): support for multibyte code-points in stringifyWithMax…
Browse files Browse the repository at this point in the history
…Len (#3979)
  • Loading branch information
aditi-khare-mongoDB committed Jan 26, 2024
1 parent 8f7bb59 commit aed1cf0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
27 changes: 23 additions & 4 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,29 @@ export function stringifyWithMaxLen(
): string {
let strToTruncate = '';

try {
strToTruncate = typeof value !== 'function' ? EJSON.stringify(value, options) : value.name;
} catch (e) {
strToTruncate = `Extended JSON serialization failed with: ${e.message}`;
if (typeof value === 'string') {
strToTruncate = value;
} else if (typeof value === 'function') {
strToTruncate = value.name;
} else {
try {
strToTruncate = EJSON.stringify(value, options);
} catch (e) {
strToTruncate = `Extended JSON serialization failed with: ${e.message}`;
}
}

// handle truncation that occurs in the middle of multi-byte codepoints
if (
maxDocumentLength !== 0 &&
strToTruncate.length > maxDocumentLength &&
strToTruncate.charCodeAt(maxDocumentLength - 1) !==
strToTruncate.codePointAt(maxDocumentLength - 1)
) {
maxDocumentLength--;
if (maxDocumentLength === 0) {
return '';
}
}

return maxDocumentLength !== 0 && strToTruncate.length > maxDocumentLength
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Command Logging and Monitoring Prose Tests', function () {
});
});

context.skip('Truncation with multi-byte codepoints', function () {
context('Truncation with multi-byte codepoints', function () {
/*
A specific test case is not provided here due to the allowed variations in truncation logic
as well as varying extended JSON whitespace usage.
Expand Down Expand Up @@ -216,12 +216,12 @@ describe('Command Logging and Monitoring Prose Tests', function () {
);

// multi-byte codepoint in middle of truncated string
expect(insertManyCommandStarted?.command.charCodeAt(maxDocLength - 1)).to.equal(
firstByteChar
);
expect(insertManyCommandStarted?.command.charCodeAt(maxDocLength - 1)).to.equal(
expect(insertManyCommandStarted?.command.charCodeAt(maxDocLength - 2)).to.equal(
secondByteChar
);
expect(insertManyCommandStarted?.command.charCodeAt(maxDocLength - 3)).to.equal(
firstByteChar
);

const insertManyCommandSucceeded = writable.buffer[1];
expect(insertManyCommandSucceeded?.message).to.equal('Command succeeded');
Expand All @@ -230,5 +230,5 @@ describe('Command Logging and Monitoring Prose Tests', function () {
maxDocLength + ELLIPSES_LENGTH
);
});
}).skipReason = 'todo(NODE-5839)';
});
});
31 changes: 26 additions & 5 deletions test/unit/mongo_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1268,14 +1268,36 @@ describe('class MongoLogger', async function () {

context('when maxDocumentLength is non-zero', function () {
context('when document has length greater than maxDocumentLength', function () {
it('truncates ejson string to length of maxDocumentLength + 3', function () {
expect(stringifyWithMaxLen(largeDoc, DEFAULT_MAX_DOCUMENT_LENGTH)).to.have.lengthOf(
DEFAULT_MAX_DOCUMENT_LENGTH + 3
);
context('when truncation does not occur mid-multibyte codepoint', function () {
it('truncates ejson string to length of maxDocumentLength + 3', function () {
expect(stringifyWithMaxLen(largeDoc, DEFAULT_MAX_DOCUMENT_LENGTH)).to.have.lengthOf(
DEFAULT_MAX_DOCUMENT_LENGTH + 3
);
});
});

it('ends with "..."', function () {
expect(stringifyWithMaxLen(largeDoc, DEFAULT_MAX_DOCUMENT_LENGTH)).to.match(/^.*\.\.\.$/);
});

context('when truncation occurs mid-multibyte codepoint', function () {
const multiByteCodePoint = '\ud83d\ude0d'; // heart eyes emoji
context('when maxDocumentLength = 1 but greater than 0', function () {
it('should return an empty string', function () {
expect(stringifyWithMaxLen(multiByteCodePoint, 1, { relaxed: true })).to.equal('');
});
});

context('when maxDocumentLength > 1', function () {
it('should round down maxDocLength to previous codepoint', function () {
const randomStringMinusACodePoint = `random ${multiByteCodePoint}random random${multiByteCodePoint}`;
const randomString = `${randomStringMinusACodePoint}${multiByteCodePoint}`;
expect(
stringifyWithMaxLen(randomString, randomString.length - 1, { relaxed: true })
).to.equal(`${randomStringMinusACodePoint}...`);
});
});
});
});

context('when document has length less than or equal to maxDocumentLength', function () {
Expand All @@ -1289,7 +1311,6 @@ describe('class MongoLogger', async function () {
/^.*\.\.\./
);
});

it('produces valid relaxed EJSON', function () {
expect(() => {
EJSON.parse(stringifyWithMaxLen(smallDoc, DEFAULT_MAX_DOCUMENT_LENGTH));
Expand Down

0 comments on commit aed1cf0

Please sign in to comment.