smartbuffer/ts/index.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-06-15 23:36:51 +02:00
import * as plugins from './smartbuffer.plugins.js';
export const uInt8ArrayExtras = plugins.uInt8ArrayExtras;
2022-06-16 00:05:30 +02:00
export function uInt8ArrayToBase64(uInt8Array: Uint8Array): string {
return plugins.uInt8ArrayExtras.uint8ArrayToBase64(uInt8Array);
2022-06-16 00:05:30 +02:00
}
export function base64ToUint8Array(base64: string): Uint8Array {
return plugins.uInt8ArrayExtras.base64ToUint8Array(base64);
2024-02-25 01:17:47 +01:00
}
2024-04-17 20:30:32 +02:00
export const isUint8Array = (obj: any): obj is Uint8Array => {
return plugins.uInt8ArrayExtras.isUint8Array(obj);
2024-04-25 18:06:04 +02:00
};
2024-04-17 20:33:24 +02:00
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;
2024-04-25 18:06:04 +02:00
}
2024-04-25 18:08:49 +02:00
export function ensurePureUint8Array(bufferArg: Uint8Array | Buffer): Uint8Array {
2024-04-25 18:06:04 +02:00
// Create a new Uint8Array with the same length as the buffer
const uint8Array: Uint8Array = new Uint8Array(bufferArg.length);
// Copy the contents of the Buffer to the new Uint8Array
uint8Array.set(bufferArg);
return uint8Array;
}