Skip to content

Commit 97ff0ad

Browse files
boomyaocursoragent
andcommitted
fix: convert Buffer to Uint8Array for browser hydration compatibility
The workflow web inspect now performs client-side hydration using hydrateData() from @workflow/core/serialization-format. This function checks for Uint8Array using instanceof, which fails for Buffer in the browser since Buffer doesn't exist in browser environments. This fix ensures that MongoDB Binary data is converted to true Uint8Array before being returned, enabling proper data hydration in workflow web inspect. Changes: - Add bufferToUint8Array() helper function - Update stripUndefined() to convert Buffer/Binary to Uint8Array - Update cleanMongoDoc() to convert Buffer/Binary to Uint8Array - Update dependencies to latest workflow versions Signed-off-by: Assistant <assistant@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 4c2fed0 commit 97ff0ad

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

packages/mongodb/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"typecheck": "tsc --noEmit"
3030
},
3131
"dependencies": {
32-
"@workflow/errors": "4.1.0-beta.14",
33-
"@workflow/world": "4.1.0-beta.2",
32+
"@workflow/errors": "4.1.0-beta.16",
33+
"@workflow/world": "4.1.0-beta.6",
3434
"@vercel/queue": "^0.0.0-alpha.29",
3535
"mongodb": "^6.0.0",
3636
"ulid": "^2.3.0",
@@ -39,7 +39,7 @@
3939
"devDependencies": {
4040
"@testcontainers/mongodb": "^10.0.0",
4141
"@types/node": "^22.0.0",
42-
"@workflow/world-testing": "4.1.0-beta.54",
42+
"@workflow/world-testing": "4.1.0-beta.60",
4343
"@workflow-worlds/testing": "workspace:*",
4444
"typescript": "^5.7.0",
4545
"vitest": "^3.0.0"

packages/mongodb/src/storage.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,24 @@ function isMongoDuplicateKeyError(error: unknown): boolean {
8181
);
8282
}
8383

84+
function bufferToUint8Array(buffer: Buffer): Uint8Array {
85+
// Create a new Uint8Array that shares the same underlying ArrayBuffer
86+
// This ensures the result is a true Uint8Array, not a Buffer
87+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
88+
}
89+
8490
function stripUndefined<T>(doc: T): T {
8591
if (doc === null || doc === undefined) return doc;
8692
if (typeof doc !== 'object') return doc;
93+
// Check for MongoDB Binary type first (has _bsontype marker)
94+
if (isMongoBinaryLike(doc)) {
95+
return bufferToUint8Array(doc.value()) as T;
96+
}
97+
// Convert Buffer to Uint8Array for browser compatibility
98+
if (Buffer.isBuffer(doc)) {
99+
return bufferToUint8Array(doc) as T;
100+
}
87101
if (ArrayBuffer.isView(doc)) return doc;
88-
if (isMongoBinaryLike(doc)) return doc.value() as T;
89102
if (Array.isArray(doc)) return doc.map(stripUndefined) as T;
90103
if (doc instanceof Date) return doc as T;
91104

@@ -103,8 +116,15 @@ function stripUndefined<T>(doc: T): T {
103116
function cleanMongoDoc<T>(doc: T): T {
104117
if (doc === null || doc === undefined) return doc;
105118
if (typeof doc !== 'object') return doc;
119+
// Check for MongoDB Binary type first (has _bsontype marker)
120+
if (isMongoBinaryLike(doc)) {
121+
return bufferToUint8Array(doc.value()) as T;
122+
}
123+
// Convert Buffer to Uint8Array for browser compatibility
124+
if (Buffer.isBuffer(doc)) {
125+
return bufferToUint8Array(doc) as T;
126+
}
106127
if (ArrayBuffer.isView(doc)) return doc;
107-
if (isMongoBinaryLike(doc)) return doc.value() as T;
108128
if (Array.isArray(doc)) return doc.map(cleanMongoDoc) as T;
109129
if (doc instanceof Date) return doc;
110130

0 commit comments

Comments
 (0)