All files / platform/packages/url-signer/src Base64.js

100% Statements 18/18
100% Branches 20/20
100% Functions 4/4
100% Lines 18/18

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                    81x 32x   49x       3x 1x   2x 2x 1x   1x       79x 79x       28x 1x   27x 27x 1x   26x     1x  
'use strict';
 
/**
 * Copy from https://github.com/poppinss/utils/blob/master/src/Helpers/base64.ts
 *
 * Helper class to base64 encode/decode values with option
 * for url encoding and decoding
 */
class Base64 {
  encode(data, encoding) {
    if (typeof data === 'string') {
      return Buffer.from(data, encoding).toString('base64');
    }
    return Buffer.from(data).toString('base64');
  }
 
  decode(encoded, encoding = 'utf-8', strict = false) {
    if (Buffer.isBuffer(encoded)) {
      return encoded.toString(encoding);
    }
    const decoded = Buffer.from(encoded, 'base64').toString(encoding);
    if (strict && this.encode(decoded, encoding) !== encoded) {
      return null;
    }
    return decoded;
  }
 
  urlEncode(data, encoding) {
    const encoded = typeof data === 'string' ? this.encode(data, encoding) : this.encode(data);
    return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
  }
 
  urlDecode(encoded, encoding = 'utf-8', strict = false) {
    if (Buffer.isBuffer(encoded)) {
      return encoded.toString(encoding);
    }
    const decoded = Buffer.from(encoded, 'base64').toString(encoding);
    if (strict && this.urlEncode(decoded, encoding) !== encoded) {
      return null;
    }
    return decoded;
  }
}
module.exports = new Base64();