diff --git a/src/tests/utils/helpers.test.ts b/src/tests/utils/helpers.test.ts new file mode 100644 index 0000000..2c917ba --- /dev/null +++ b/src/tests/utils/helpers.test.ts @@ -0,0 +1,46 @@ +import { + areDataViewsEqual, + nodeBufferToDataView, + dataViewToNodeBuffer, + nodeBufferToArrayBuffer, + arrayBufferToNodeBuffer +} from '../../utils/helpers'; + +test('nodeBufferToDataView() and dataViewToNodeBuffer()', () => +{ + const array = new Uint8Array( + [ + 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08 + ] + ); + + const view1 = new DataView( + array.buffer, + array.byteOffset, + array.byteLength + ); + + const nodeBuffer = dataViewToNodeBuffer(view1); + const view2 = nodeBufferToDataView(nodeBuffer); + + expect(areDataViewsEqual(view1, view2)).toBe(true); +}); + +test('nodeBufferToArrayBuffer() and arrayBufferToNodeBuffer()', () => +{ + const array = new Uint8Array( + [ + 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08 + ] + ); + + const arrayBuffer1 = array.buffer; + const nodeBuffer = arrayBufferToNodeBuffer(arrayBuffer1); + const arrayBuffer2 = nodeBufferToArrayBuffer(nodeBuffer); + const view1 = new DataView(arrayBuffer1); + const view2 = new DataView(arrayBuffer2); + + expect(areDataViewsEqual(view1, view2)).toBe(true); +}); diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index b05cbe5..b7c2cdd 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -134,9 +134,23 @@ export function areDataViewsEqual(view1: DataView, view2: DataView) * @remarks * - Just for Node.js. */ -export function nodeBufferToDataView(buffer: Buffer): DataView +export function nodeBufferToDataView(nodeBuffer: Buffer): DataView { - return new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + return new DataView( + nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.byteLength + ); +} + +/** + * Convert DataView to Node.js Buffer using the same underlying ArrayBuffer. + * + * @category Utils + * @remarks + * - Just for Node.js. + */ +export function dataViewToNodeBuffer(view: DataView): Buffer +{ + return Buffer.from(view.buffer, view.byteOffset, view.byteLength); } /** @@ -146,9 +160,23 @@ export function nodeBufferToDataView(buffer: Buffer): DataView * @remarks * - Just for Node.js. */ -export function nodeBufferToArrayBuffer(buffer: Buffer): ArrayBuffer +export function nodeBufferToArrayBuffer(nodeBuffer: Buffer): ArrayBuffer +{ + return nodeBuffer.buffer.slice( + nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.byteLength + ); +} + +/** + * Convert ArrayBuffer to Node.js Buffer. + * + * @category Utils + * @remarks + * - Just for Node.js. + */ +export function arrayBufferToNodeBuffer(arrayBuffer: ArrayBuffer): Buffer { - return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); + return Buffer.from(arrayBuffer, 0, arrayBuffer.byteLength); } /** @@ -205,11 +233,11 @@ export function dataViewToString(view: DataView): string * * @category Utils */ -export function arrayBufferToString(buffer: ArrayBuffer): string +export function arrayBufferToString(arrayBuffer: ArrayBuffer): string { const decoder = new TextDecoder(); - return decoder.decode(buffer); + return decoder.decode(arrayBuffer); } /**