Skip to content

Commit

Permalink
Improve comments in BufferUtils
Browse files Browse the repository at this point in the history
Also removes comment about old browsers support as it's not needed after
#1633
  • Loading branch information
VeskeR committed May 2, 2024
1 parent 3317106 commit bebe0cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/common/types/IBufferUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput> {
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;
Expand All @@ -13,6 +15,9 @@ export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput> {
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;
}
7 changes: 2 additions & 5 deletions src/platform/web/lib/util/bufferutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +13,7 @@ class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput> {
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;
Expand Down Expand Up @@ -81,7 +80,6 @@ class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput> {
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");
Expand Down Expand Up @@ -193,7 +191,6 @@ class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput> {
return -1;
}

/* Returns ArrayBuffer on browser and Buffer on Node.js */
arrayBufferViewToBuffer(arrayBufferView: ArrayBufferView): ArrayBuffer {
return this.toArrayBuffer(arrayBufferView);
}
Expand Down

0 comments on commit bebe0cd

Please sign in to comment.