constructor()

in lib/rpc.js [106:162]


  constructor(config, verbose) {
    assert(config, 'must pass "config"');
    assert(config.endpoint, 'must pass "config.endpoint"');
    if (!config.endpoint.startsWith('https://') &&
      !config.endpoint.startsWith('http://')) {
      throw new Error(`"config.endpoint" must starts with 'https://' or 'http://'.`);
    }
    assert(config.apiVersion, 'must pass "config.apiVersion"');
    if (config.credentialsProvider) {
      if (typeof config.credentialsProvider.getCredentials !== 'function') {
        throw new Error(`must pass "config.credentialsProvider" with function "getCredentials()"`);
      }
      this.credentialsProvider = config.credentialsProvider;
    } else {
      assert(config.accessKeyId, 'must pass "config.accessKeyId"');
      var accessKeySecret = config.secretAccessKey || config.accessKeySecret;
      assert(accessKeySecret, 'must pass "config.accessKeySecret"');
      this.accessKeyId = config.accessKeyId;
      this.accessKeySecret = accessKeySecret;
      this.securityToken = config.securityToken;
      this.credentialsProvider = {
        getCredentials: async () => {
          return {
            accessKeyId: config.accessKeyId,
            accessKeySecret: accessKeySecret,
            securityToken: config.securityToken,
          };
        }
      };
    }


    if (config.endpoint.endsWith('/')) {
      config.endpoint = config.endpoint.slice(0, -1);
    }

    this.endpoint = config.endpoint;
    this.apiVersion = config.apiVersion;
    this.verbose = verbose === true;
    // 非 codes 里的值,将抛出异常
    this.codes = new Set([200, '200', 'OK', 'Success', 'success']);
    if (config.codes) {
      // 合并 codes
      for (var elem of config.codes) {
        this.codes.add(elem);
      }
    }

    this.opts = config.opts || {};

    var httpModule = this.endpoint.startsWith('https://')
      ? require('https') : require('http');
    this.keepAliveAgent = new httpModule.Agent({
      keepAlive: true,
      keepAliveMsecs: 3000
    });
  }