-
Notifications
You must be signed in to change notification settings - Fork 1.8k
refactor(NODE-6616): short circuit EJSON stringifying #4360
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
Closed
+258
−3
Closed
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d588ada
refactor(NODE-6616): short circuit EJSON stringifying
nbbeeken a4cce5f
chore(NODE-6637): remove drivers tools env setting (#4363)
durran d21c4ff
perf(NODE-6452): Optimize CommandStartedEvent and CommandSucceededEve…
W-A-James 31f941e
finish accounting for other BSON types
W-A-James befdc47
update comments
W-A-James cca1316
Merge branch 'main' into NODE-6616-ejson-max-len
W-A-James 2c25525
update condition to check binary
W-A-James 0e08c22
add wip tests
W-A-James 891cee3
refactor to use switch
W-A-James b2fddf1
remove unneeded tests
W-A-James File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
import { inspect, promisify } from 'util'; | ||
import { isUint8Array } from 'util/types'; | ||
|
||
import { type Document, EJSON, type EJSONOptions, type ObjectId } from './bson'; | ||
import { | ||
type Binary, | ||
type BSONRegExp, | ||
type BSONSymbol, | ||
type Code, | ||
type DBRef, | ||
type Decimal128, | ||
type Document, | ||
type Double, | ||
EJSON, | ||
type EJSONOptions, | ||
type Int32, | ||
type Long, | ||
type MaxKey, | ||
type MinKey, | ||
type ObjectId, | ||
type Timestamp | ||
} from './bson'; | ||
import type { CommandStartedEvent } from './cmap/command_monitoring_events'; | ||
import type { | ||
ConnectionCheckedInEvent, | ||
|
@@ -413,6 +431,20 @@ export interface LogConvertible extends Record<string, any> { | |
toLog(): Record<string, any>; | ||
} | ||
|
||
type BSONObject = | ||
| BSONRegExp | ||
| BSONSymbol | ||
| Code | ||
| DBRef | ||
| Decimal128 | ||
| Double | ||
| Int32 | ||
| Long | ||
| MaxKey | ||
| MinKey | ||
| ObjectId | ||
| Timestamp | ||
| Binary; | ||
/** @internal */ | ||
export function stringifyWithMaxLen( | ||
value: any, | ||
|
@@ -421,13 +453,117 @@ export function stringifyWithMaxLen( | |
): string { | ||
let strToTruncate = ''; | ||
|
||
let currentLength = 0; | ||
const maxDocumentLengthEnsurer = function maxDocumentLengthEnsurer(key: string, value: any) { | ||
if (currentLength >= maxDocumentLength) { | ||
return undefined; | ||
} | ||
// Account for root document | ||
if (key === '') { | ||
// Account for starting brace | ||
currentLength += 1; | ||
return value; | ||
} | ||
|
||
// +4 accounts for 2 quotation marks, colon and comma after value | ||
currentLength += key.length + 4; | ||
|
||
if (typeof value === 'string') { | ||
// +2 accounts for quotes | ||
currentLength += value.length + 2; | ||
} else if (typeof value === 'number' || typeof value === 'bigint') { | ||
currentLength += String(value).length; | ||
} else if (typeof value === 'boolean') { | ||
currentLength += value ? 4 : 5; | ||
} else if ('buffer' in value && isUint8Array(value.buffer)) { | ||
// Handle binData | ||
currentLength += (value.buffer.byteLength + value.buffer.byteLength * 0.5) | 0; | ||
} else if (value != null && typeof value === 'object' && '_bsontype' in value) { | ||
const v = value as BSONObject; | ||
if (v._bsontype === 'Binary') { | ||
// '{"$binary":{"base64":"<base64 string>","subType":"XX"}}' | ||
// This is an estimate based on the fact that the base64 is approximately 1.33x the length of | ||
// the actual binary sequence https://en.wikipedia.org/wiki/Base64 | ||
currentLength += (22 + value.position + value.position * 0.33 + 18) | 0; | ||
} else if (v._bsontype === 'Code') { | ||
// '{"$code":"<code>"}' or '{"$code":"<code>","$scope":<scope>}' | ||
if (v.scope == null) { | ||
currentLength += v.code.length + 10 + 2; | ||
} else { | ||
// Ignoring actual scope object | ||
|
||
currentLength += v.code.length + 10 + 11; | ||
} | ||
} else if (v._bsontype === 'Decimal128') { | ||
currentLength += value.toExtendedJSON().length; | ||
} else if (v._bsontype === 'Double') { | ||
// Doesn't account for representing integers as <value>.0 | ||
if ('value' in v && typeof v.value === 'number') currentLength += String(v.value).length; | ||
} else if (v._bsontype === 'Int32') { | ||
if ('value' in v && typeof v.value === 'number') currentLength += String(v.value).length; | ||
} else if (v._bsontype === 'Long') { | ||
if ('toString' in v && typeof v.toString === 'function') { | ||
currentLength += v.toString().length; | ||
} | ||
} else if (v._bsontype === 'MaxKey' || v._bsontype === 'MinKey') { | ||
// '{"$maxKey":1}' or '{"$minKey":1}' | ||
currentLength += 13; | ||
} else if (v._bsontype === 'ObjectId') { | ||
// '{"$oid":"XXXXXXXXXXXXXXXXXXXXXXXX"}' | ||
currentLength += 35; | ||
} else if ( | ||
v._bsontype === 'BSONRegExp' && | ||
'pattern' in v && | ||
typeof v.pattern === 'string' && | ||
'options' in v && | ||
typeof v.options === 'string' | ||
) { | ||
// '{"$regularExpression":{"pattern":"<pattern>","options":"<options>"}}' | ||
currentLength += 34 + v.pattern.length + 13 + v.options.length + 3; | ||
} else if (v._bsontype === 'BSONSymbol' && 'value' in v && v.value === 'string') { | ||
// '{"$symbol": "<value>"}' | ||
currentLength += 12 + v.value.length + 2; | ||
} else if ( | ||
v._bsontype === 'Timestamp' && | ||
't' in v && | ||
typeof v.t === 'string' && | ||
'i' in v && | ||
typeof v.i === 'string' | ||
) { | ||
currentLength += 19 + String(v.t).length + 5 + String(v.i).length + 2; | ||
} else if (v._bsontype === 'DBRef') { | ||
// '{"$ref":"<collection>","$id":<stringified oid>}' or '{"$ref":"<collection>","$id":<stringified oid>,"$db":"test"}' | ||
currentLength += 9; | ||
// account for collection | ||
if ('collection' in v) { | ||
currentLength += v.collection.length + 1; | ||
} | ||
|
||
// account for db if present | ||
if ('db' in v && typeof v.db === 'string') { | ||
currentLength += 8 + v.db.length + 2; | ||
} | ||
|
||
// account for oid if present | ||
if ('oid' in v) { | ||
currentLength += 35; | ||
} | ||
} | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
if (typeof value === 'string') { | ||
strToTruncate = value; | ||
} else if (typeof value === 'function') { | ||
strToTruncate = value.name; | ||
} else { | ||
try { | ||
strToTruncate = EJSON.stringify(value, options); | ||
if (maxDocumentLength !== 0) { | ||
strToTruncate = EJSON.stringify(value, maxDocumentLengthEnsurer, 0, options); | ||
} else { | ||
strToTruncate = EJSON.stringify(value, options); | ||
} | ||
} catch (e) { | ||
strToTruncate = `Extended JSON serialization failed with: ${e.message}`; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.