All files / src digest.ts

100% Statements 21/21
100% Branches 2/2
100% Functions 9/9
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                  3x           4x 4x                               6x   5x     5x         3x         18x             49x 49x 49x                           3x 24x   23x     1x                     3x 55x   54x     1x                 3x 1x    
export type Encoding = "hex" | "base64" | "safe-base64";
 
/**
 *
 *
 * @export
 * @class UnknownEncodingError
 * @extends {Error}
 */
export class UnknownEncodingError extends Error {
  /**
   * Creates an instance of UnknownEncodingError.
   * @param {string} digest
   * @memberof UnknownEncodingError
   */
  constructor(public digest: string) {
    super(`Unknown digest: ${digest}`);
  }
}
 
interface EncoderDecoder {
  encode(buffer: Buffer): string;
  decode(string: string): Buffer;
}
 
/**
 *
 *
 * @param {BufferEncoding} encoding
 * @return {EncoderDecoder}  {EncoderDecoder}
 */
function bufferInternalEncoder(encoding: BufferEncoding): EncoderDecoder {
  return {
    encode(buffer) {
      return buffer.toString(encoding);
    },
    decode(string) {
      return Buffer.from(string, encoding);
    }
  };
}
 
const encoders: Record<Encoding, EncoderDecoder> = {
  base64: bufferInternalEncoder("base64"),
  hex: bufferInternalEncoder("hex"),
  "safe-base64": {
    encode(buffer) {
      return buffer
        .toString("base64")
        .replace(/\+/g, "-")
        .replace(/\//g, "_")
        .replace(/=/g, "");
    },
    decode(string) {
      string = string + "===".slice((string.length + 3) % 4);
      string.replace(/-/g, "+").replace(/_/g, "/");
      return Buffer.alloc(Buffer.byteLength(string, "base64"), string, "base64");
    }
  }
};
 
/**
 *
 *
 * @export
 * @param {Encoding} encoding
 * @param {Buffer} buffer
 * @return {string}  {string}
 * @throws {UnknownEncodingError}
 */
export function encode(encoding: Encoding, buffer: Buffer): string {
  if (encoding in encoders) {
    // eslint-disable-next-line security/detect-object-injection
    return encoders[encoding].encode(buffer);
  }
 
  throw new UnknownEncodingError(encoding);
}
 
/**
 *
 *
 * @export
 * @param {Encoding} encoding
 * @param {string} string
 * @return {Buffer}  {Buffer}
 */
export function decode(encoding: Encoding, string: string): Buffer {
  if (encoding in encoders) {
    // eslint-disable-next-line security/detect-object-injection
    return encoders[encoding].decode(string);
  }
 
  throw new UnknownEncodingError(encoding);
}
 
/**
 *
 *
 * @export
 * @return {Encoding[]} {Encoding[]}
 */
export function getEncodings(): Encoding[] {
  return Object.keys(encoders) as Encoding[];
}