fix(core): update

This commit is contained in:
Philipp Kunz 2024-02-29 22:43:37 +01:00
parent 031f6449bd
commit e1faf34660
3 changed files with 16 additions and 6 deletions

View File

@ -6,14 +6,14 @@ tap.test('first test', async () => {
});
tap.test('should recognize different buffer like objects', async () => {
const testBuffer = Buffer.from('test');
// const testBuffer = Buffer.from('test');
const testArrayBuffer = new ArrayBuffer(4);
const testUint8Array = new Uint8Array(testArrayBuffer);
testUint8Array[0] = 116;
testUint8Array[1] = 101;
testUint8Array[2] = 115;
testUint8Array[3] = 116;
expect(smartbuffer.isBufferLike(testBuffer)).toBeTrue();
// expect(smartbuffer.isBufferLike(testBuffer)).toBeTrue();
expect(smartbuffer.isBufferLike(testArrayBuffer)).toBeTrue();
expect(smartbuffer.isBufferLike(testUint8Array)).toBeTrue();
});

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartbuffer',
version: '1.0.6',
version: '1.0.7',
description: 'handle ArrayBufferLike structures'
}

View File

@ -62,6 +62,16 @@ export function base64ToArrayBuffer(base64: string): Uint8Array {
return arrayBuffer;
}
export function isBufferLike(obj: any): obj is ArrayBufferLike {
return obj && typeof obj.byteLength === 'number';
}
export function isBufferLike(obj: any): obj is ArrayBufferLike | Buffer {
// Check for ArrayBufferLike objects in any environment
if (obj && typeof obj.byteLength === 'number') {
return true;
}
// Additional check specific to Node.js environment for Buffer objects
if (typeof Buffer !== 'undefined' && Buffer.isBuffer) {
return Buffer.isBuffer(obj);
}
return false;
}