Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@workflow/errors": "4.1.0-beta.14",
"@workflow/world": "4.1.0-beta.2",
"@workflow/errors": "4.1.0-beta.16",
"@workflow/world": "4.1.0-beta.6",
"@vercel/queue": "^0.0.0-alpha.29",
"mongodb": "^6.0.0",
"ulid": "^2.3.0",
Expand All @@ -39,7 +39,7 @@
"devDependencies": {
"@testcontainers/mongodb": "^10.0.0",
"@types/node": "^22.0.0",
"@workflow/world-testing": "4.1.0-beta.54",
"@workflow/world-testing": "4.1.0-beta.60",
"@workflow-worlds/testing": "workspace:*",
"typescript": "^5.7.0",
"vitest": "^3.0.0"
Expand Down
24 changes: 22 additions & 2 deletions packages/mongodb/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,24 @@ function isMongoDuplicateKeyError(error: unknown): boolean {
);
}

function bufferToUint8Array(buffer: Buffer): Uint8Array {
// Create a new Uint8Array that shares the same underlying ArrayBuffer
// This ensures the result is a true Uint8Array, not a Buffer
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}

function stripUndefined<T>(doc: T): T {
if (doc === null || doc === undefined) return doc;
if (typeof doc !== 'object') return doc;
// Check for MongoDB Binary type first (has _bsontype marker)
if (isMongoBinaryLike(doc)) {
return bufferToUint8Array(doc.value()) as T;
}
// Convert Buffer to Uint8Array for browser compatibility
if (Buffer.isBuffer(doc)) {
return bufferToUint8Array(doc) as T;
}
if (ArrayBuffer.isView(doc)) return doc;
if (isMongoBinaryLike(doc)) return doc.value() as T;
if (Array.isArray(doc)) return doc.map(stripUndefined) as T;
if (doc instanceof Date) return doc as T;

Expand All @@ -103,8 +116,15 @@ function stripUndefined<T>(doc: T): T {
function cleanMongoDoc<T>(doc: T): T {
if (doc === null || doc === undefined) return doc;
if (typeof doc !== 'object') return doc;
// Check for MongoDB Binary type first (has _bsontype marker)
if (isMongoBinaryLike(doc)) {
return bufferToUint8Array(doc.value()) as T;
}
// Convert Buffer to Uint8Array for browser compatibility
if (Buffer.isBuffer(doc)) {
return bufferToUint8Array(doc) as T;
}
if (ArrayBuffer.isView(doc)) return doc;
if (isMongoBinaryLike(doc)) return doc.value() as T;
if (Array.isArray(doc)) return doc.map(cleanMongoDoc) as T;
if (doc instanceof Date) return doc;

Expand Down
Loading