Skip to content

Commit 053f01f

Browse files
committed
refactor: replace isBuffer check with isUint8Array
1 parent df5121a commit 053f01f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/client-side-encryption/auto_encrypter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { MongoDBResponse, type MongoDBResponseConstructor } from '../cmap/wire_p
1010
import { getMongoDBClientEncryption } from '../deps';
1111
import { MongoRuntimeError } from '../error';
1212
import { MongoClient, type MongoClientOptions } from '../mongo_client';
13-
import { MongoDBCollectionNamespace } from '../utils';
13+
import { isUint8Array, MongoDBCollectionNamespace } from '../utils';
1414
import * as cryptoCallbacks from './crypto_callbacks';
1515
import { MongoCryptInvalidArgumentError } from './errors';
1616
import { MongocryptdManager } from './mongocryptd_manager';
@@ -480,7 +480,7 @@ export class AutoEncrypter {
480480
): Promise<Document> {
481481
const buffer = MongoDBResponse.is(response)
482482
? response.toBytes()
483-
: Buffer.isBuffer(response)
483+
: isUint8Array(response)
484484
? response
485485
: serialize(response, options);
486486

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ export const ByteUtils = {
6464
}
6565
};
6666

67+
/**
68+
* Returns true if value is a Uint8Array or a Buffer
69+
* @param value - any value that may be a Uint8Array
70+
*/
71+
export function isUint8Array(value: unknown): value is Uint8Array {
72+
return (
73+
value != null &&
74+
typeof value === 'object' &&
75+
Symbol.toStringTag in value &&
76+
value[Symbol.toStringTag] === 'Uint8Array'
77+
);
78+
}
79+
6780
/**
6881
* Determines if a connection's address matches a user provided list
6982
* of domain wildcards.

test/unit/utils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
HostAddress,
88
hostMatchesWildcards,
99
isHello,
10+
isUint8Array,
1011
LEGACY_HELLO_COMMAND,
1112
List,
1213
matchesParentDomain,
@@ -981,4 +982,29 @@ describe('driver utils', function () {
981982
});
982983
});
983984
});
985+
986+
describe('isUint8Array()', () => {
987+
describe('when given a UintArray', () =>
988+
it('returns true', () => expect(isUint8Array(Uint8Array.from([1]))).to.be.true));
989+
990+
describe('when given a Buffer', () =>
991+
it('returns true', () => expect(isUint8Array(Buffer.from([1]))).to.be.true));
992+
993+
describe('when given a value that does not have `Uint8Array` at Symbol.toStringTag', () => {
994+
it('returns false', () => {
995+
const weirdArray = Uint8Array.from([1]);
996+
Object.defineProperty(weirdArray, Symbol.toStringTag, { value: 'blah' });
997+
expect(isUint8Array(weirdArray)).to.be.false;
998+
});
999+
});
1000+
1001+
describe('when given null', () =>
1002+
it('returns false', () => expect(isUint8Array(null)).to.be.false));
1003+
1004+
describe('when given a non object', () =>
1005+
it('returns false', () => expect(isUint8Array('')).to.be.false));
1006+
1007+
describe('when given an object that does not respond to Symbol.toStringTag', () =>
1008+
it('returns false', () => expect(isUint8Array(Object.create(null))).to.be.false));
1009+
});
9841010
});

0 commit comments

Comments
 (0)