-
Type:
Task
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Use Case
As a... js-bson maintainer (and, indirectly, a js-bson / Node driver user)
I want... BSON serialization (serialize), BSON size calculation (calculateObjectSize), and EJSON serialization (EJSON.stringify / EJSON.serialize) to walk a JS object through one shared traversal, instead of three slightly different copies
So that... the three can't disagree on corner cases, and fixes and optimizations made to one path aren't missed in the others
User Experience
- calculateObjectSize always agrees with serialize, EJSON picks the same types BSON does (or differs only where the EJSON spec requires it), and valid documents see no behavior change apart from deliberate fixes like NODE-4377.
Additional Context
- Three separate functions walk the same JS object graph and make the same per-value decisions (which BSON type a value maps to, whether it is skipped, how Maps and circular references are handled):
- BSON serialize: serializeInto in src/parser/serializer.ts
- BSON size: src/parser/calculate_size.ts
- EJSON serialize: serializeDocument / serializeValue in src/extended_json.ts
- The copies have drifted. A condition written one way in one file (hypothetical example: <) is written slightly differently in another (<=). As a result, a function like calculateObjectSize(doc) doesn't (didn't) always match serialize(doc).byteLength
- Circular-reference detection is also implemented separately in each path (the two BSON files throw Cannot convert circular structure to BSON; EJSON has its own seenObjects tracking and message).
- This drift has produced a run of bug tickets, each closed by patching one path to match another:
NODE-7631: calculateObjectSize over-counted Int32 and BSONSymbol by 12 bytes each (missing Int32 branch, and a type guard checking 'Symbol' instead of 'BSONSymbol')NODE-7652(external PRNODE-7644): -0 was sized as an int32 even though it is serialized as a doubleNODE-7646(dupNODE-7650): ES Map entries were skipped by calculateObjectSize but serializedNODE-7661: empty-scope Code. The two BSON files used different conditionals to decide whether to emit code_with_scope (serializer.ts: codeValue.scope && typeof codeValue.scope === 'object'; calculate_size.ts: value.scope != null && Object.keys(value.scope).length > 0)NODE-7696: calculateObjectSize hung forever on a circular reference, which the serializer's circular-reference guard rejects- NODE-4377 (backlog since 2022): a JS number of Int32.max + 1 becomes $numberLong in EJSON but a double in BSON
- Changes are done per function. This leaves some paths behind/unimplemented
- Impact: no known customer escalations. The bugs above came from external contributors and audits. When the paths disagree, size-dependent code (pre-allocated buffers, 16 MB limit checks) can wrongly reject valid documents or allocate the wrong buffer size, and EJSON round-trips can change a value's BSON type.
Dependencies
- N/A
Risks/Unknowns
- Performance: serialize is a hot path, and a shared traversal (for example, a visitor that measures, writes bytes, or builds EJSON) could add overhead.
- Output shape: BSON serialize and size calculation produce bytes or byte counts, while EJSON builds a JSON-shaped value with canonical, relaxed and legacy modes. It's unclear whether one traversal can serve all three without leaking EJSON-only concerns into the BSON hot path
- Inadvertent behavior changes: converging forces a decision on every case where the paths disagree today. Each one is a potential behavior change
- Public API: serialize, calculateObjectSize, and EJSON.stringify/EJSON.serialize signatures and options (serializeFunctions, ignoreUndefined, relaxed, legacy, etc.) must not change.
Acceptance Criteria
Implementation Requirements
- Catalogue every place where the three paths currently disagree, or could, (including the cases fixed in the tickets above; they stand as evidence of the benefits of this refactor)
- Propose a design with an analysis of trade-offs: performance, complexity, and risk of behavior change
- Recommend whether to proceed, and file follow-up implementation ticket(s) with estimates if so
Testing Requirements
- N/A
Documentation Requirements
- N/A
Follow Up Requirements
- Implementation ticket if the spike recommends proceeding
- related to
-
NODE-7696 BSON.calculateObjectSize hangs indefinitely on an object containing a circular reference
-
- Closed
-
-
NODE-7598 js-bson: three input-validation gaps in parsing/serialization
-
- Closed
-
-
NODE-7631 Count Int32 and BSONSymbol in calculateObjectSize
-
- Closed
-
-
NODE-7646 js-bson - PR #910: fix: count ES Map entries in calculateObjectSize
-
- Closed
-
-
NODE-7652 count negative zero as a double in calculateObjectSize
-
- Closed
-
-
NODE-7661 count empty-scope Code as code_with_scope in calculateObjectSize
-
- Closed
-