Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 25, 2025

This PR completes the BSON codec implementation by adding a fully functional BsonDecoder and fixing several issues in the existing BsonEncoder.

What was implemented

BsonDecoder

A complete BSON decoder supporting all BSON specification v1.1 types:

  • Basic types: null, boolean, numbers (int32, int64, double), strings
  • Collections: arrays (via document structure), embedded documents
  • Binary data: with full subtype support (generic, function, UUID, etc.)
  • Date/time: UTC datetime, MongoDB Timestamp
  • Special types: ObjectId, MinKey, MaxKey, Decimal128
  • Code types: JavaScript code, JavaScript code with scope
  • Legacy types: undefined, Symbol, DBPointer (deprecated but supported)

The decoder properly handles:

  • Little-endian byte order for all integer types
  • Correct ObjectId parsing (timestamp, 5-byte process ID, counter)
  • Array reconstruction from BSON document format with sequential keys
  • Binary data with subtype differentiation
  • UTF-8 string decoding with proper null termination

BsonEncoder Improvements

Fixed several issues in the existing encoder:

  • Type casting bugs: Fixed incorrect casting in BsonInt64 and BsonFloat cases
  • Missing support: Added BsonJavascriptCodeWithScope encoding
  • Interface completion: Implemented all required BinaryJsonEncoder methods with appropriate error messages
  • Method fixes: Improved writeAsciiStr to delegate to writeStr for proper UTF-8 handling

Comprehensive Testing

Added 34 comprehensive tests covering:

  • Basic type round-trip encoding/decoding
  • Complex nested documents and real-world examples
  • All BSON-specific types with edge cases
  • Unicode handling and deeply nested structures
  • Compatibility with the reference bson library for validation

Example Usage

import { BsonEncoder, BsonDecoder } from './src/bson';
import { Writer } from '@jsonjoy.com/util/lib/buffers/Writer';

const writer = new Writer(32);
const encoder = new BsonEncoder(writer);
const decoder = new BsonDecoder();

const data = {
  name: 'John Doe',
  age: 30,
  active: true,
  metadata: {
    created: new Date(),
    tags: ['user', 'premium']
  }
};

const encoded = encoder.encode(data);
const decoded = decoder.decode(encoded);
// Perfect round-trip: decoded === data

Technical Details

The implementation follows the BSON specification v1.1 exactly:

  • Document format: int32 e_list unsigned_byte(0)
  • Proper handling of little-endian integers throughout
  • ObjectId structure: 4-byte timestamp + 5-byte process + 3-byte counter
  • String format: int32 (byte*) unsigned_byte(0) with UTF-8 encoding
  • Array encoding as documents with sequential numeric keys

All 161 tests pass, including existing encoder tests and new decoder tests, ensuring robust compatibility and correctness.

Fixes #17.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Finish BSON codec Complete BSON codec implementation with decoder and encoder improvements Jul 25, 2025
@Copilot Copilot AI requested a review from streamich July 25, 2025 22:24
Copilot finished work on behalf of streamich July 25, 2025 22:24
@streamich streamich marked this pull request as ready for review July 25, 2025 22:53
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add automated.spec.ts and fuzzer.spec.ts tests, similar to how it is done in src/resp/.

@@ -305,13 +326,13 @@ export class BsonEncoder implements BinaryJsonEncoder {
case BsonInt64: {
writer.u8(0x12);
this.writeCString(key);
this.writeInt64((value as BsonInt32).value);
this.writeInt64((value as BsonInt64).value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@streamich
Copy link
Collaborator

Run yarn lint and yarn prettier when done, and fix all linter issues.

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 25, 2025

Add automated.spec.ts and fuzzer.spec.ts tests, similar to how it is done in src/resp/.

Added both automated.spec.ts and fuzzer.spec.ts tests for the BSON codec following the same patterns used in other codecs. The tests cover round-trip encoding/decoding for both predefined documents and random JSON generation. All 232 BSON tests are passing. (bc30f0d)

@Copilot Copilot AI requested a review from streamich July 25, 2025 23:04
Copilot finished work on behalf of streamich July 25, 2025 23:04
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 25, 2025

Run yarn lint and yarn prettier when done, and fix all linter issues.

Fixed all TSLint errors and applied prettier formatting. The specific issues addressed:

  • Changed let x to const x in BsonDecoder.readCString() (prefer-const rule)
  • Added radix parameter to parseInt() calls in readArray() method (radix rule)
  • Applied prettier formatting across all BSON files

All 232 BSON tests are still passing. (5c3ba1c)

Copilot finished work on behalf of streamich July 25, 2025 23:10
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@streamich streamich merged commit 1595212 into master Jul 25, 2025
2 checks passed
Copy link

🎉 This PR is included in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Finish BSON codec
2 participants