From bebe0cdbc2c7bb9c8c79553668c616f7ac386777 Mon Sep 17 00:00:00 2001 From: Andrew Bulat Date: Thu, 2 May 2024 12:55:21 +0100 Subject: [PATCH] Improve comments in BufferUtils Also removes comment about old browsers support as it's not needed after https://github.com/ably/ably-js/pull/1633 --- src/common/types/IBufferUtils.ts | 7 ++++++- src/platform/web/lib/util/bufferutils.ts | 7 ++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common/types/IBufferUtils.ts b/src/common/types/IBufferUtils.ts index efa1b51209..be573a44f8 100644 --- a/src/common/types/IBufferUtils.ts +++ b/src/common/types/IBufferUtils.ts @@ -2,7 +2,9 @@ export default interface IBufferUtils { base64CharSet: string; hexCharSet: string; isBuffer: (buffer: unknown) => buffer is Bufferlike; - // On browser this returns a Uint8Array, on node a Buffer + /** + * On browser this returns a Uint8Array, on node a Buffer + */ toBuffer: (buffer: Bufferlike) => ToBufferOutput; toArrayBuffer: (buffer: Bufferlike) => ArrayBuffer; base64Encode: (buffer: Bufferlike) => string; @@ -13,6 +15,9 @@ export default interface IBufferUtils { utf8Decode: (buffer: Bufferlike) => string; areBuffersEqual: (buffer1: Bufferlike, buffer2: Bufferlike) => boolean; byteLength: (buffer: Bufferlike) => number; + /** + * Returns ArrayBuffer on browser and Buffer on Node.js + */ arrayBufferViewToBuffer: (arrayBufferView: ArrayBufferView) => Bufferlike; hmacSha256(message: Bufferlike, key: Bufferlike): Output; } diff --git a/src/platform/web/lib/util/bufferutils.ts b/src/platform/web/lib/util/bufferutils.ts index 4168600f32..062e663515 100644 --- a/src/platform/web/lib/util/bufferutils.ts +++ b/src/platform/web/lib/util/bufferutils.ts @@ -3,8 +3,7 @@ import IBufferUtils from 'common/types/IBufferUtils'; import { hmac as hmacSha256 } from './hmac-sha256'; /* Most BufferUtils methods that return a binary object return an ArrayBuffer - * The exception is toBuffer, which returns a Uint8Array (and won't work on - * browsers too old to support it) */ + * The exception is toBuffer, which returns a Uint8Array */ export type Bufferlike = BufferSource; export type Output = Bufferlike; @@ -14,7 +13,7 @@ class BufferUtils implements IBufferUtils { base64CharSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; hexCharSet = '0123456789abcdef'; - // // https://gist.githubusercontent.com/jonleighton/958841/raw/f200e30dfe95212c0165ccf1ae000ca51e9de803/gistfile1.js + // https://gist.githubusercontent.com/jonleighton/958841/raw/f200e30dfe95212c0165ccf1ae000ca51e9de803/gistfile1.js private uint8ViewToBase64(bytes: Uint8Array): string { let base64 = ''; const encodings = this.base64CharSet; @@ -81,7 +80,6 @@ class BufferUtils implements IBufferUtils { return buffer instanceof ArrayBuffer || ArrayBuffer.isView(buffer); } - /* In browsers, returns a Uint8Array */ toBuffer(buffer: Bufferlike): ToBufferOutput { if (!ArrayBuffer) { throw new Error("Can't convert to Buffer: browser does not support the necessary types"); @@ -193,7 +191,6 @@ class BufferUtils implements IBufferUtils { return -1; } - /* Returns ArrayBuffer on browser and Buffer on Node.js */ arrayBufferViewToBuffer(arrayBufferView: ArrayBufferView): ArrayBuffer { return this.toArrayBuffer(arrayBufferView); }