nacos-config2
Version:
nacos config client
241 lines • 9.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpAgent = void 0;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const const_1 = require("./const");
const interface_1 = require("./interface");
const urllib = require("urllib");
const crypto = require("crypto");
const utils_1 = require("./utils");
class HttpAgent {
constructor(options) {
this.loggerDomain = 'Nacos';
this.debugPrefix = this.loggerDomain.toLowerCase();
this.debug = require('debug')(`${this.debugPrefix}:${process.pid}:http_agent`);
this.retryed = 0;
this.debug('options: %s', options);
this.options = options;
}
get configuration() {
return this.options.configuration;
}
get serverListMgr() {
return this.configuration.get(interface_1.ClientOptionKeys.SERVER_MGR);
}
/**
* HTTP 请求客户端
*/
get httpclient() {
return this.configuration.get(interface_1.ClientOptionKeys.HTTPCLIENT) || urllib;
}
get unit() {
return this.configuration.get(interface_1.ClientOptionKeys.UNIT);
}
get secretKey() {
return this.configuration.get(interface_1.ClientOptionKeys.SECRETKEY);
}
get requestTimeout() {
return this.configuration.get(interface_1.ClientOptionKeys.REQUEST_TIMEOUT);
}
get accessKey() {
return this.configuration.get(interface_1.ClientOptionKeys.ACCESSKEY);
}
get ssl() {
return this.configuration.get(interface_1.ClientOptionKeys.SSL);
}
get serverPort() {
return this.configuration.get(interface_1.ClientOptionKeys.SERVER_PORT);
}
get contextPath() {
return this.configuration.get(interface_1.ClientOptionKeys.CONTEXTPATH) || 'nacos';
}
get clusterName() {
return this.configuration.get(interface_1.ClientOptionKeys.CLUSTER_NAME) || 'serverlist';
}
get defaultEncoding() {
return this.configuration.get(interface_1.ClientOptionKeys.DEFAULT_ENCODING) || 'utf8';
}
get nacosName() {
return this.configuration.get(interface_1.ClientOptionKeys.NACOS_NAME);
}
get nacosPassword() {
return this.configuration.get(interface_1.ClientOptionKeys.NACOS_PASSWORD);
}
/**
* 请求
* @param {String} path - 请求 path
* @param {Object} [options] - 参数
* @return {String} value
*/
async request(path, options = {}) {
// 默认为当前单元
const unit = options.unit || this.unit;
const ts = String(Date.now());
const { encode = false, method = 'GET', data, timeout = this.requestTimeout, headers = {}, dataAsQueryString = false } = options;
const endTime = Date.now() + timeout;
let lastErr;
let signStr = data.tenant;
if (data.group && data.tenant) {
signStr = data.tenant + '+' + data.group;
}
else if (data.group) {
signStr = data.group;
}
const signature = crypto.createHmac('sha1', this.secretKey)
.update(signStr + '+' + ts).digest()
.toString('base64');
// 携带统一的头部信息
Object.assign(headers, {
'Client-Version': const_1.VERSION,
'Content-Type': 'application/x-www-form-urlencoded; charset=GBK',
'Spas-AccessKey': this.accessKey,
timeStamp: ts,
exConfigInfo: 'true',
'Spas-Signature': signature,
});
let requestData = data;
if (encode) {
requestData = utils_1.encodingParams(data, this.defaultEncoding);
}
do {
const currentServer = await this.serverListMgr.getCurrentServerAddr(unit);
let url = this.getRequestUrl(currentServer) + `${path}`;
this.debug('request unit: [%s] with url: %s', unit, url);
const token = await this.authorize(currentServer);
let reqUrl = url;
if (token) {
reqUrl = url.indexOf('?') > 0 ? `${url}&accessToken=${token}` : `${url}?accessToken=${token}`;
}
try {
const res = await this.httpclient.request(reqUrl, {
rejectUnauthorized: false,
httpsAgent: false,
method,
data: requestData,
dataType: 'text',
headers,
timeout,
secureProtocol: 'TLSv1_2_method',
dataAsQueryString,
});
this.debug('%s %s, got %s, body: %j', method, reqUrl, res.status, res.data);
switch (res.status) {
case const_1.HTTP_OK:
return this.decodeResData(res, method);
case const_1.HTTP_UNAUTHORIZED:
this.accessToken = '';
this.retryed++;
if (this.retryed < 3) {
return await this.request(path, options);
}
else {
this.retryed = 0;
return null;
}
case const_1.HTTP_NOT_FOUND:
return null;
case const_1.HTTP_CONFLICT:
await this.serverListMgr.updateCurrentServer(unit);
// JAVA 在外面业务类处理的这个逻辑,应该是需要重试的
lastErr = new Error(`[Client Worker] ${this.loggerDomain} server config being modified concurrently, data: ${JSON.stringify(data)}`);
lastErr.name = `${this.loggerDomain}ServerConflictError`;
break;
default:
await this.serverListMgr.updateCurrentServer(unit);
// JAVA 还有一个针对 HTTP_FORBIDDEN 的处理,不过合并到 default 应该也没问题
lastErr = new Error(`${this.loggerDomain} Server Error Status: ${res.status}, url: ${reqUrl}, data: ${JSON.stringify(data)}`);
lastErr.name = `${this.loggerDomain}ServerResponseError`;
lastErr.body = res.data;
break;
}
}
catch (err) {
err.url = `${method} ${url}`;
err.data = data;
err.headers = headers;
lastErr = err;
}
} while (Date.now() < endTime);
throw lastErr;
}
// 获取请求 url
getRequestUrl(currentServer) {
let url;
if (/:/.test(currentServer)) {
url = `http://${currentServer}`;
if (this.ssl) {
url = `https://${currentServer}`;
}
}
else {
url = `http://${currentServer}:${this.serverPort}`;
if (this.ssl) {
url = `https://${currentServer}:${this.serverPort}`;
}
}
return `${url}/${this.contextPath}`;
}
decodeResData(res, method = 'GET') {
if (method === 'GET' && /charset=GBK/.test(res.headers['content-type']) && this.defaultEncoding === 'utf8') {
try {
return utils_1.transformGBKToUTF8(res.data);
}
catch (err) {
console.error(`transform gbk data to utf8 error, msg=${err.messager}`);
return res.data;
}
}
else {
return res.data;
}
}
async authorize(currentServer) {
this.debug('先进行授权');
if (this.accessToken) {
return this.accessToken;
}
if (!this.nacosName) {
return false;
}
const authUrl = this.getRequestUrl(currentServer) + '/v1/auth/login';
try {
const res = await this.httpclient.request(authUrl, {
method: 'POST',
data: {
username: this.nacosName,
password: this.nacosPassword
},
contentType: 'text',
dataType: 'json',
});
switch (res.status) {
case const_1.HTTP_OK:
const data = this.decodeResData(res, 'POST');
this.accessToken = data.accessToken;
return this.accessToken;
default:
throw new Error('Nacos 鉴权失败!');
}
}
catch (e) {
throw new Error('Nacos 鉴权失败2!');
}
}
}
exports.HttpAgent = HttpAgent;
//# sourceMappingURL=http_agent.js.map