<commit_before>'use strict';

const {parse, format} = require('url');
const querystring = require('querystring');
const crypto = require('crypto');

const uuid = require('uuid');
const httpx = require('httpx');

const ua = require('./ua');
const Base = require('./base');

/**
 * API Gateway Client
 */
class Client extends Base {
  constructor(key, secret, stage) {
    super();
    this.appKey = key;
    this.appSecret = Buffer.from(secret, 'utf8');
    this.stage = stage || 'RELEASE';
  }

  buildSignToString(method, headers, signedHeadersStr, url) {
    // accept, contentMD5, contentType,
    const lf = '\n';
    var list = [method, lf];

    var accept = headers['accept'];
    if (accept) {
      list.push(accept);
    }
    list.push(lf);

    var contentMD5 = headers['content-md5'];
    if (contentMD5) {
      list.push(contentMD5);
    }
    list.push(lf);

    var contentType = headers['content-type'];
    if (contentType) {
      list.push(contentType);
    }
    list.push(lf);

    var date = headers['date'];
    if (date) {
      list.push(date);
    }
    list.push(lf);

    if (signedHeadersStr) {
      list.push(signedHeadersStr);
      list.push(lf);
    }

    list.push(this.buildUrl(url));

    return list.join('');
  }

  sign(stringToSign) {
    return crypto.createHmac('sha256', this.appSecret)
      .update(stringToSign, 'utf8').digest('base64');
  }

  md5(content) {
    return crypto.createHash('md5')
      .update(content, 'utf8')
      .digest('base64');
  }

  getSignHeaders(headers) {
    var keys = Object.keys(headers).sort();
    var signKeys = [];
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      if (key.toLowerCase().startsWith('x-ca-')) {
        signKeys.push(key);
      }
    }

    return signKeys;
  }

  buildUrl(parsedUrl, query) {
    var toStringify = Object.assign({}, parsedUrl.query, query);
    var result = parsedUrl.pathname;
    if (Object.keys(toStringify).length) {
      result += '?' + querystring.stringify(toStringify);
    }
    return result;
  }

  buildHeaders(headers) {
    var now = new Date();

    var keys = Object.keys(headers || {});
    var lowered = {};
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      lowered[key.toLowerCase()] = headers[key];
    }

    return Object.assign({
      'x-ca-timestamp': now.getTime(),
      'x-ca-key': this.appKey,
      'x-ca-nonce': uuid.v4(),
      'x-ca-stage': this.stage,
      'date': now.toUTCString(),
      'content-type': 'application/json',
      'accept': 'application/json'
    }, lowered);
  }

  getSignedHeadersString(signHeaders, headers) {
    var list = [];
    for (var i = 0; i < signHeaders.length; i++) {
      var key = signHeaders[i];<commit_msg>Dont set Date header, so browserify can run it<commit_after>'use strict';

const {parse, format} = require('url');
const querystring = require('querystring');
const crypto = require('crypto');

const uuid = require('uuid');
const httpx = require('httpx');

const ua = require('./ua');
const Base = require('./base');

/**
 * API Gateway Client
 */
class Client extends Base {
  constructor(key, secret, stage) {
    super();
    this.appKey = key;
    this.appSecret = Buffer.from(secret, 'utf8');
    this.stage = stage || 'RELEASE';
  }

  buildSignToString(method, headers, signedHeadersStr, url) {
    // accept, contentMD5, contentType,
    const lf = '\n';
    var list = [method, lf];

    var accept = headers['accept'];
    if (accept) {
      list.push(accept);
    }
    list.push(lf);

    var contentMD5 = headers['content-md5'];
    if (contentMD5) {
      list.push(contentMD5);
    }
    list.push(lf);

    var contentType = headers['content-type'];
    if (contentType) {
      list.push(contentType);
    }
    list.push(lf);

    var date = headers['date'];
    if (date) {
      list.push(date);
    }
    list.push(lf);

    if (signedHeadersStr) {
      list.push(signedHeadersStr);
      list.push(lf);
    }

    list.push(this.buildUrl(url));

    return list.join('');
  }

  sign(stringToSign) {
    return crypto.createHmac('sha256', this.appSecret)
      .update(stringToSign, 'utf8').digest('base64');
  }

  md5(content) {
    return crypto.createHash('md5')
      .update(content, 'utf8')
      .digest('base64');
  }

  getSignHeaders(headers) {
    var keys = Object.keys(headers).sort();
    var signKeys = [];
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      if (key.toLowerCase().startsWith('x-ca-')) {
        signKeys.push(key);
      }
    }

    return signKeys;
  }

  buildUrl(parsedUrl, query) {
    var toStringify = Object.assign({}, parsedUrl.query, query);
    var result = parsedUrl.pathname;
    if (Object.keys(toStringify).length) {
      result += '?' + querystring.stringify(toStringify);
    }
    return result;
  }

  buildHeaders(headers) {
    var now = new Date();

    var keys = Object.keys(headers || {});
    var lowered = {};
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      lowered[key.toLowerCase()] = headers[key];
    }

    return Object.assign({
      'x-ca-timestamp': now.getTime(),
      'x-ca-key': this.appKey,
      'x-ca-nonce': uuid.v4(),
      'x-ca-stage': this.stage,
      'content-type': 'application/json',
      'accept': 'application/json'
    }, lowered);
  }

  getSignedHeadersString(signHeaders, headers) {
    var list = [];
    for (var i = 0; i < signHeaders.length; i++) {
      var key = signHeaders[i];
{'fraction_non_alphanumeric': 0.1693313953488372, 'fraction_numerical': 0.007630813953488372, 'mean_word_length': 2.7347354138398914, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '451270', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1828, 'n_words': 393}