From 06fef39a9fc416b19b766af4f28c9ae892d7c267 Mon Sep 17 00:00:00 2001 From: farmerx Date: Thu, 12 Aug 2021 17:12:14 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20cls=E6=94=AF=E6=8C=81=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tencentcloud/common/abstract_client.js | 2 + tencentcloud/common/http/http_connection.js | 38 +++++++++++++------ tencentcloud/common/interface.d.ts | 1 + tencentcloud/common/sign.d.ts | 3 +- tencentcloud/common/sign.js | 15 +++++++- .../services/cls/v20201016/cls_client.d.ts | 2 +- .../services/cls/v20201016/cls_models.d.ts | 2 +- 7 files changed, 48 insertions(+), 15 deletions(-) diff --git a/tencentcloud/common/abstract_client.js b/tencentcloud/common/abstract_client.js index a345c9654..f0dade452 100644 --- a/tencentcloud/common/abstract_client.js +++ b/tencentcloud/common/abstract_client.js @@ -48,6 +48,7 @@ class AbstractClient { reqTimeout: 60, }, profile && profile.httpProfile), language: profile.language, + headers: profile.headers, }; if (this.profile.language && !interface_1.SUPPORT_LANGUAGE_LIST.includes(this.profile.language)) { throw new tencent_cloud_sdk_exception_1.default(`Language invalid, choices: ${interface_1.SUPPORT_LANGUAGE_LIST.join("|")}`); @@ -115,6 +116,7 @@ class AbstractClient { token: this.credential.token, requestClient: this.sdkVersion, language: this.profile.language, + headers: this.profile.headers, }); } catch (e) { diff --git a/tencentcloud/common/http/http_connection.js b/tencentcloud/common/http/http_connection.js index 7e2c67f75..71ea71542 100644 --- a/tencentcloud/common/http/http_connection.js +++ b/tencentcloud/common/http/http_connection.js @@ -27,7 +27,7 @@ class HttpConnection { } return await fetch_1.default(url, config); } - static async doRequestWithSign3({ method, url, data, service, action, region, version, secretId, secretKey, multipart = false, timeout = 60000, token, requestClient, language, }) { + static async doRequestWithSign3({ method, url, data, service, action, region, version, secretId, secretKey, multipart = false, timeout = 60000, token, requestClient, language, headers, }) { // data 中可能带有 readStream,由于需要计算整个 body 的 hash, // 所以这里把 readStream 转为 Buffer // eslint-disable-next-line @typescript-eslint/no-use-before-define @@ -58,6 +58,8 @@ class HttpConnection { "X-TC-RequestClient": requestClient, }, }; + + if (token === null) { delete config.headers["X-TC-Token"]; } @@ -71,18 +73,28 @@ class HttpConnection { if (method === "GET") { config.headers["Content-Type"] = "application/x-www-form-urlencoded"; } - if (method === "POST" && !multipart) { - config.body = JSON.stringify(data); - config.headers["Content-Type"] = "application/json"; - } - if (method === "POST" && multipart) { - form = new FormData(); - for (const key in data) { - form.append(key, data[key]); + + var isOctetStream = false; + if (method === "POST" ) { + if (multipart) { + form = new FormData(); + for (const key in data) { + form.append(key, data[key]); + } + config.body = form; + config.headers = Object.assign({}, config.headers, form.getHeaders()); + } else if (headers !== null && headers !== undefined) { + config.body = data + config.headers = Object.assign({}, config.headers, headers) + if (config.headers["Content-Type"] == "application/octet-stream") { + isOctetStream = true + } + } else { + config.body = JSON.stringify(data); + config.headers["Content-Type"] = "application/json"; } - config.body = form; - config.headers = Object.assign({}, config.headers, form.getHeaders()); } + const signature = sign_1.default.sign3({ method, url, @@ -93,11 +105,14 @@ class HttpConnection { secretKey, multipart, boundary: form ? form.getBoundary() : undefined, + isOctetStream, }); config.headers["Authorization"] = signature; return await fetch_1.default(url, config); } } + + exports.HttpConnection = HttpConnection; async function convertReadStreamToBuffer(data) { for (const key in data) { @@ -106,6 +121,7 @@ async function convertReadStreamToBuffer(data) { } } } + function mergeData(data, prefix = "") { const ret = {}; for (const k in data) { diff --git a/tencentcloud/common/interface.d.ts b/tencentcloud/common/interface.d.ts index 5472a8fd8..29733ec1f 100644 --- a/tencentcloud/common/interface.d.ts +++ b/tencentcloud/common/interface.d.ts @@ -59,6 +59,7 @@ export interface ClientProfile { */ reqTimeout?: number; }; + headers?: any; /** * api请求时附带的 language 字段 * @type {"zh-CN" | "en-US"} diff --git a/tencentcloud/common/sign.d.ts b/tencentcloud/common/sign.d.ts index c785927ce..6d5a7179d 100644 --- a/tencentcloud/common/sign.d.ts +++ b/tencentcloud/common/sign.d.ts @@ -3,7 +3,7 @@ */ export default class Sign { static sign(secretKey: string, signStr: string, signMethod: string): string; - static sign3({ method, url, payload, timestamp, service, secretId, secretKey, multipart, boundary, }: { + static sign3({ method, url, payload, timestamp, service, secretId, secretKey, multipart, boundary, isOctetStream, }: { method?: string; url?: string; payload: any; @@ -13,5 +13,6 @@ export default class Sign { secretKey: string; multipart: boolean; boundary: string; + isOctetStream: boolean; }): string; } diff --git a/tencentcloud/common/sign.js b/tencentcloud/common/sign.js index 68c499979..11894ab47 100644 --- a/tencentcloud/common/sign.js +++ b/tencentcloud/common/sign.js @@ -18,7 +18,7 @@ class Sign { const hmac = crypto.createHmac(signMethodMap[signMethod], secretKey || ""); return hmac.update(Buffer.from(signStr, "utf8")).digest("base64"); } - static sign3({ method = "POST", url = "", payload, timestamp, service, secretId, secretKey, multipart, boundary, }) { + static sign3({ method = "POST", url = "", payload, timestamp, service, secretId, secretKey, multipart, boundary, isOctetStream = false}) { const urlObj = new url_1.URL(url); // 通用头部 let headers = ""; @@ -31,6 +31,9 @@ class Sign { signedHeaders = "content-type"; if (multipart) { headers = `content-type:multipart/form-data; boundary=${boundary}\n`; + } + else if (isOctetStream) { + headers = "content-type:application/octet-stream\n"; } else { headers = "content-type:application/json\n"; @@ -59,6 +62,8 @@ class Sign { } hash.update(`--\r\n`); payload_hash = hash.digest("hex"); + } else if (isOctetStream) { + payload_hash = payload ? getHash(payload) : getHash(""); } else { payload_hash = payload ? getHash(JSON.stringify(payload)) : getHash(""); @@ -89,6 +94,14 @@ class Sign { return `TC3-HMAC-SHA256 Credential=${secretId}/${date}/${service}/tc3_request, SignedHeaders=${signedHeaders}, Signature=${signature}`; } } + +function Uint8ArrayToString(fileData){ + var dataString = ""; + for (var i = 0; i < fileData.length; i++) { + dataString += String.fromCharCode(fileData[i]); + } + return dataString +} exports.default = Sign; function sha256(message, secret = "", encoding) { const hmac = crypto.createHmac("sha256", secret); diff --git a/tencentcloud/services/cls/v20201016/cls_client.d.ts b/tencentcloud/services/cls/v20201016/cls_client.d.ts index ba9eac142..202bc67cc 100644 --- a/tencentcloud/services/cls/v20201016/cls_client.d.ts +++ b/tencentcloud/services/cls/v20201016/cls_client.d.ts @@ -322,7 +322,7 @@ cls.pb.cc cls.pb.h cls.proto 将生成的 cls.pb.h 头文件引入代码中,调用接口进行数据格式封装。 */ - UploadLog(req?: UploadLogRequest, cb?: (error: string, rep: UploadLogResponse) => void): Promise; + UploadLog(req?: any, cb?: (error: string, rep: UploadLogResponse) => void): Promise; /** * 本接口用于创建异步上下文任务 */ diff --git a/tencentcloud/services/cls/v20201016/cls_models.d.ts b/tencentcloud/services/cls/v20201016/cls_models.d.ts index 057fa0421..5d9e89c60 100644 --- a/tencentcloud/services/cls/v20201016/cls_models.d.ts +++ b/tencentcloud/services/cls/v20201016/cls_models.d.ts @@ -266,7 +266,7 @@ export interface DescribeAsyncContextResultResponse { /** * UploadLog请求参数结构体 */ -export declare type UploadLogRequest = null; +// export declare type UploadLogRequest = null; /** * CreateAsyncContextTask请求参数结构体 */ From 8d4829ee35e4d8d595dbf79adc559aa6911fe224 Mon Sep 17 00:00:00 2001 From: farmerx Date: Thu, 12 Aug 2021 17:15:58 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20cls=E6=94=AF=E6=8C=81=E6=97=A5?= =?UTF-8?q?=E5=BF=97demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/cls/package.json | 21 + examples/cls/src/cls.proto | 34 + examples/cls/src/cls_pb.js | 1245 ++++++++++++++++++++++++++++++++++++ examples/cls/src/index.ts | 64 ++ examples/cls/tsconfig.json | 14 + 5 files changed, 1378 insertions(+) create mode 100644 examples/cls/package.json create mode 100644 examples/cls/src/cls.proto create mode 100644 examples/cls/src/cls_pb.js create mode 100644 examples/cls/src/index.ts create mode 100644 examples/cls/tsconfig.json diff --git a/examples/cls/package.json b/examples/cls/package.json new file mode 100644 index 000000000..3d011c579 --- /dev/null +++ b/examples/cls/package.json @@ -0,0 +1,21 @@ +{ + "name": "cls_node_demo", + "version": "1.0.0", + "description": "cls demo", + "main": "dist/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "ts-node ./src/index.ts" + }, + "author": "farmerx", + "license": "MIT", + "dependencies": { + "google-protobuf": "^3.17.3", + "tencentcloud-sdk-nodejs": "^4.0.181", + "tslint": "^6.1.3", + "typescript": "^4.3.5" + }, + "devDependencies": { + "ts-node": "^10.2.0" + } +} diff --git a/examples/cls/src/cls.proto b/examples/cls/src/cls.proto new file mode 100644 index 000000000..cffd1c074 --- /dev/null +++ b/examples/cls/src/cls.proto @@ -0,0 +1,34 @@ +syntax = "proto2"; + +package cls; + +message Log +{ + required int64 time = 1 [jstype = JS_STRING]; + message Content + { + required string key = 1; + required string value = 2; + } + repeated Content contents= 2; +} + +message LogTag +{ + required string key = 1; + required string value = 2; +} + +message LogGroup +{ + repeated Log logs= 1; + optional string contextFlow= 2; + optional string filename = 3; + optional string source = 4; + repeated LogTag logTags = 5; +} + +message LogGroupList +{ + repeated LogGroup logGroupList = 1; +} \ No newline at end of file diff --git a/examples/cls/src/cls_pb.js b/examples/cls/src/cls_pb.js new file mode 100644 index 000000000..646a26e08 --- /dev/null +++ b/examples/cls/src/cls_pb.js @@ -0,0 +1,1245 @@ +// source: cls.proto +/** + * @fileoverview + * @enhanceable + * @suppress {missingRequire} reports error on implicit type usages. + * @suppress {messageConventions} JS Compiler reports an error if a variable or + * field starts with 'MSG_' and isn't a translatable message. + * @public + */ +// GENERATED CODE -- DO NOT EDIT! +/* eslint-disable */ +// @ts-nocheck + +var jspb = require('google-protobuf'); +var goog = jspb; +var global = Function('return this')(); + +goog.exportSymbol('proto.cls.Log', null, global); +goog.exportSymbol('proto.cls.Log.Content', null, global); +goog.exportSymbol('proto.cls.LogGroup', null, global); +goog.exportSymbol('proto.cls.LogGroupList', null, global); +goog.exportSymbol('proto.cls.LogTag', null, global); +/** + * Generated by JsPbCodeGenerator. + * @param {Array=} opt_data Optional initial data array, typically from a + * server response, or constructed directly in Javascript. The array is used + * in place and becomes part of the constructed object. It is not cloned. + * If no data is provided, the constructed object will be empty, but still + * valid. + * @extends {jspb.Message} + * @constructor + */ +proto.cls.Log = function(opt_data) { + jspb.Message.initialize(this, opt_data, 0, -1, proto.cls.Log.repeatedFields_, null); +}; +goog.inherits(proto.cls.Log, jspb.Message); +if (goog.DEBUG && !COMPILED) { + /** + * @public + * @override + */ + proto.cls.Log.displayName = 'proto.cls.Log'; +} +/** + * Generated by JsPbCodeGenerator. + * @param {Array=} opt_data Optional initial data array, typically from a + * server response, or constructed directly in Javascript. The array is used + * in place and becomes part of the constructed object. It is not cloned. + * If no data is provided, the constructed object will be empty, but still + * valid. + * @extends {jspb.Message} + * @constructor + */ +proto.cls.Log.Content = function(opt_data) { + jspb.Message.initialize(this, opt_data, 0, -1, null, null); +}; +goog.inherits(proto.cls.Log.Content, jspb.Message); +if (goog.DEBUG && !COMPILED) { + /** + * @public + * @override + */ + proto.cls.Log.Content.displayName = 'proto.cls.Log.Content'; +} +/** + * Generated by JsPbCodeGenerator. + * @param {Array=} opt_data Optional initial data array, typically from a + * server response, or constructed directly in Javascript. The array is used + * in place and becomes part of the constructed object. It is not cloned. + * If no data is provided, the constructed object will be empty, but still + * valid. + * @extends {jspb.Message} + * @constructor + */ +proto.cls.LogTag = function(opt_data) { + jspb.Message.initialize(this, opt_data, 0, -1, null, null); +}; +goog.inherits(proto.cls.LogTag, jspb.Message); +if (goog.DEBUG && !COMPILED) { + /** + * @public + * @override + */ + proto.cls.LogTag.displayName = 'proto.cls.LogTag'; +} +/** + * Generated by JsPbCodeGenerator. + * @param {Array=} opt_data Optional initial data array, typically from a + * server response, or constructed directly in Javascript. The array is used + * in place and becomes part of the constructed object. It is not cloned. + * If no data is provided, the constructed object will be empty, but still + * valid. + * @extends {jspb.Message} + * @constructor + */ +proto.cls.LogGroup = function(opt_data) { + jspb.Message.initialize(this, opt_data, 0, -1, proto.cls.LogGroup.repeatedFields_, null); +}; +goog.inherits(proto.cls.LogGroup, jspb.Message); +if (goog.DEBUG && !COMPILED) { + /** + * @public + * @override + */ + proto.cls.LogGroup.displayName = 'proto.cls.LogGroup'; +} +/** + * Generated by JsPbCodeGenerator. + * @param {Array=} opt_data Optional initial data array, typically from a + * server response, or constructed directly in Javascript. The array is used + * in place and becomes part of the constructed object. It is not cloned. + * If no data is provided, the constructed object will be empty, but still + * valid. + * @extends {jspb.Message} + * @constructor + */ +proto.cls.LogGroupList = function(opt_data) { + jspb.Message.initialize(this, opt_data, 0, -1, proto.cls.LogGroupList.repeatedFields_, null); +}; +goog.inherits(proto.cls.LogGroupList, jspb.Message); +if (goog.DEBUG && !COMPILED) { + /** + * @public + * @override + */ + proto.cls.LogGroupList.displayName = 'proto.cls.LogGroupList'; +} + +/** + * List of repeated fields within this message type. + * @private {!Array} + * @const + */ +proto.cls.Log.repeatedFields_ = [2]; + + + +if (jspb.Message.GENERATE_TO_OBJECT) { +/** + * Creates an object representation of this proto. + * Field names that are reserved in JavaScript and will be renamed to pb_name. + * Optional fields that are not set will be set to undefined. + * To access a reserved field use, foo.pb_, eg, foo.pb_default. + * For the list of reserved names please see: + * net/proto2/compiler/js/internal/generator.cc#kKeyword. + * @param {boolean=} opt_includeInstance Deprecated. whether to include the + * JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @return {!Object} + */ +proto.cls.Log.prototype.toObject = function(opt_includeInstance) { + return proto.cls.Log.toObject(opt_includeInstance, this); +}; + + +/** + * Static version of the {@see toObject} method. + * @param {boolean|undefined} includeInstance Deprecated. Whether to include + * the JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @param {!proto.cls.Log} msg The msg instance to transform. + * @return {!Object} + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.Log.toObject = function(includeInstance, msg) { + var f, obj = { + time: (f = jspb.Message.getField(msg, 1)) == null ? undefined : f, + contentsList: jspb.Message.toObjectList(msg.getContentsList(), + proto.cls.Log.Content.toObject, includeInstance) + }; + + if (includeInstance) { + obj.$jspbMessageInstance = msg; + } + return obj; +}; +} + + +/** + * Deserializes binary data (in protobuf wire format). + * @param {jspb.ByteSource} bytes The bytes to deserialize. + * @return {!proto.cls.Log} + */ +proto.cls.Log.deserializeBinary = function(bytes) { + var reader = new jspb.BinaryReader(bytes); + var msg = new proto.cls.Log; + return proto.cls.Log.deserializeBinaryFromReader(msg, reader); +}; + + +/** + * Deserializes binary data (in protobuf wire format) from the + * given reader into the given message object. + * @param {!proto.cls.Log} msg The message object to deserialize into. + * @param {!jspb.BinaryReader} reader The BinaryReader to use. + * @return {!proto.cls.Log} + */ +proto.cls.Log.deserializeBinaryFromReader = function(msg, reader) { + while (reader.nextField()) { + if (reader.isEndGroup()) { + break; + } + var field = reader.getFieldNumber(); + switch (field) { + case 1: + var value = /** @type {string} */ (reader.readInt64String()); + msg.setTime(value); + break; + case 2: + var value = new proto.cls.Log.Content; + reader.readMessage(value,proto.cls.Log.Content.deserializeBinaryFromReader); + msg.addContents(value); + break; + default: + reader.skipField(); + break; + } + } + return msg; +}; + + +/** + * Serializes the message to binary data (in protobuf wire format). + * @return {!Uint8Array} + */ +proto.cls.Log.prototype.serializeBinary = function() { + var writer = new jspb.BinaryWriter(); + proto.cls.Log.serializeBinaryToWriter(this, writer); + return writer.getResultBuffer(); +}; + + +/** + * Serializes the given message to binary data (in protobuf wire + * format), writing to the given BinaryWriter. + * @param {!proto.cls.Log} message + * @param {!jspb.BinaryWriter} writer + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.Log.serializeBinaryToWriter = function(message, writer) { + var f = undefined; + f = /** @type {string} */ (jspb.Message.getField(message, 1)); + if (f != null) { + writer.writeInt64String( + 1, + f + ); + } + f = message.getContentsList(); + if (f.length > 0) { + writer.writeRepeatedMessage( + 2, + f, + proto.cls.Log.Content.serializeBinaryToWriter + ); + } +}; + + + + + +if (jspb.Message.GENERATE_TO_OBJECT) { +/** + * Creates an object representation of this proto. + * Field names that are reserved in JavaScript and will be renamed to pb_name. + * Optional fields that are not set will be set to undefined. + * To access a reserved field use, foo.pb_, eg, foo.pb_default. + * For the list of reserved names please see: + * net/proto2/compiler/js/internal/generator.cc#kKeyword. + * @param {boolean=} opt_includeInstance Deprecated. whether to include the + * JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @return {!Object} + */ +proto.cls.Log.Content.prototype.toObject = function(opt_includeInstance) { + return proto.cls.Log.Content.toObject(opt_includeInstance, this); +}; + + +/** + * Static version of the {@see toObject} method. + * @param {boolean|undefined} includeInstance Deprecated. Whether to include + * the JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @param {!proto.cls.Log.Content} msg The msg instance to transform. + * @return {!Object} + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.Log.Content.toObject = function(includeInstance, msg) { + var f, obj = { + key: (f = jspb.Message.getField(msg, 1)) == null ? undefined : f, + value: (f = jspb.Message.getField(msg, 2)) == null ? undefined : f + }; + + if (includeInstance) { + obj.$jspbMessageInstance = msg; + } + return obj; +}; +} + + +/** + * Deserializes binary data (in protobuf wire format). + * @param {jspb.ByteSource} bytes The bytes to deserialize. + * @return {!proto.cls.Log.Content} + */ +proto.cls.Log.Content.deserializeBinary = function(bytes) { + var reader = new jspb.BinaryReader(bytes); + var msg = new proto.cls.Log.Content; + return proto.cls.Log.Content.deserializeBinaryFromReader(msg, reader); +}; + + +/** + * Deserializes binary data (in protobuf wire format) from the + * given reader into the given message object. + * @param {!proto.cls.Log.Content} msg The message object to deserialize into. + * @param {!jspb.BinaryReader} reader The BinaryReader to use. + * @return {!proto.cls.Log.Content} + */ +proto.cls.Log.Content.deserializeBinaryFromReader = function(msg, reader) { + while (reader.nextField()) { + if (reader.isEndGroup()) { + break; + } + var field = reader.getFieldNumber(); + switch (field) { + case 1: + var value = /** @type {string} */ (reader.readString()); + msg.setKey(value); + break; + case 2: + var value = /** @type {string} */ (reader.readString()); + msg.setValue(value); + break; + default: + reader.skipField(); + break; + } + } + return msg; +}; + + +/** + * Serializes the message to binary data (in protobuf wire format). + * @return {!Uint8Array} + */ +proto.cls.Log.Content.prototype.serializeBinary = function() { + var writer = new jspb.BinaryWriter(); + proto.cls.Log.Content.serializeBinaryToWriter(this, writer); + return writer.getResultBuffer(); +}; + + +/** + * Serializes the given message to binary data (in protobuf wire + * format), writing to the given BinaryWriter. + * @param {!proto.cls.Log.Content} message + * @param {!jspb.BinaryWriter} writer + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.Log.Content.serializeBinaryToWriter = function(message, writer) { + var f = undefined; + f = /** @type {string} */ (jspb.Message.getField(message, 1)); + if (f != null) { + writer.writeString( + 1, + f + ); + } + f = /** @type {string} */ (jspb.Message.getField(message, 2)); + if (f != null) { + writer.writeString( + 2, + f + ); + } +}; + + +/** + * required string key = 1; + * @return {string} + */ +proto.cls.Log.Content.prototype.getKey = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.Log.Content} returns this + */ +proto.cls.Log.Content.prototype.setKey = function(value) { + return jspb.Message.setField(this, 1, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.Log.Content} returns this + */ +proto.cls.Log.Content.prototype.clearKey = function() { + return jspb.Message.setField(this, 1, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.Log.Content.prototype.hasKey = function() { + return jspb.Message.getField(this, 1) != null; +}; + + +/** + * required string value = 2; + * @return {string} + */ +proto.cls.Log.Content.prototype.getValue = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 2, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.Log.Content} returns this + */ +proto.cls.Log.Content.prototype.setValue = function(value) { + return jspb.Message.setField(this, 2, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.Log.Content} returns this + */ +proto.cls.Log.Content.prototype.clearValue = function() { + return jspb.Message.setField(this, 2, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.Log.Content.prototype.hasValue = function() { + return jspb.Message.getField(this, 2) != null; +}; + + +/** + * required int64 time = 1; + * @return {string} + */ +proto.cls.Log.prototype.getTime = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "0")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.Log} returns this + */ +proto.cls.Log.prototype.setTime = function(value) { + return jspb.Message.setField(this, 1, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.Log} returns this + */ +proto.cls.Log.prototype.clearTime = function() { + return jspb.Message.setField(this, 1, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.Log.prototype.hasTime = function() { + return jspb.Message.getField(this, 1) != null; +}; + + +/** + * repeated Content contents = 2; + * @return {!Array} + */ +proto.cls.Log.prototype.getContentsList = function() { + return /** @type{!Array} */ ( + jspb.Message.getRepeatedWrapperField(this, proto.cls.Log.Content, 2)); +}; + + +/** + * @param {!Array} value + * @return {!proto.cls.Log} returns this +*/ +proto.cls.Log.prototype.setContentsList = function(value) { + return jspb.Message.setRepeatedWrapperField(this, 2, value); +}; + + +/** + * @param {!proto.cls.Log.Content=} opt_value + * @param {number=} opt_index + * @return {!proto.cls.Log.Content} + */ +proto.cls.Log.prototype.addContents = function(opt_value, opt_index) { + return jspb.Message.addToRepeatedWrapperField(this, 2, opt_value, proto.cls.Log.Content, opt_index); +}; + + +/** + * Clears the list making it empty but non-null. + * @return {!proto.cls.Log} returns this + */ +proto.cls.Log.prototype.clearContentsList = function() { + return this.setContentsList([]); +}; + + + + + +if (jspb.Message.GENERATE_TO_OBJECT) { +/** + * Creates an object representation of this proto. + * Field names that are reserved in JavaScript and will be renamed to pb_name. + * Optional fields that are not set will be set to undefined. + * To access a reserved field use, foo.pb_, eg, foo.pb_default. + * For the list of reserved names please see: + * net/proto2/compiler/js/internal/generator.cc#kKeyword. + * @param {boolean=} opt_includeInstance Deprecated. whether to include the + * JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @return {!Object} + */ +proto.cls.LogTag.prototype.toObject = function(opt_includeInstance) { + return proto.cls.LogTag.toObject(opt_includeInstance, this); +}; + + +/** + * Static version of the {@see toObject} method. + * @param {boolean|undefined} includeInstance Deprecated. Whether to include + * the JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @param {!proto.cls.LogTag} msg The msg instance to transform. + * @return {!Object} + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.LogTag.toObject = function(includeInstance, msg) { + var f, obj = { + key: (f = jspb.Message.getField(msg, 1)) == null ? undefined : f, + value: (f = jspb.Message.getField(msg, 2)) == null ? undefined : f + }; + + if (includeInstance) { + obj.$jspbMessageInstance = msg; + } + return obj; +}; +} + + +/** + * Deserializes binary data (in protobuf wire format). + * @param {jspb.ByteSource} bytes The bytes to deserialize. + * @return {!proto.cls.LogTag} + */ +proto.cls.LogTag.deserializeBinary = function(bytes) { + var reader = new jspb.BinaryReader(bytes); + var msg = new proto.cls.LogTag; + return proto.cls.LogTag.deserializeBinaryFromReader(msg, reader); +}; + + +/** + * Deserializes binary data (in protobuf wire format) from the + * given reader into the given message object. + * @param {!proto.cls.LogTag} msg The message object to deserialize into. + * @param {!jspb.BinaryReader} reader The BinaryReader to use. + * @return {!proto.cls.LogTag} + */ +proto.cls.LogTag.deserializeBinaryFromReader = function(msg, reader) { + while (reader.nextField()) { + if (reader.isEndGroup()) { + break; + } + var field = reader.getFieldNumber(); + switch (field) { + case 1: + var value = /** @type {string} */ (reader.readString()); + msg.setKey(value); + break; + case 2: + var value = /** @type {string} */ (reader.readString()); + msg.setValue(value); + break; + default: + reader.skipField(); + break; + } + } + return msg; +}; + + +/** + * Serializes the message to binary data (in protobuf wire format). + * @return {!Uint8Array} + */ +proto.cls.LogTag.prototype.serializeBinary = function() { + var writer = new jspb.BinaryWriter(); + proto.cls.LogTag.serializeBinaryToWriter(this, writer); + return writer.getResultBuffer(); +}; + + +/** + * Serializes the given message to binary data (in protobuf wire + * format), writing to the given BinaryWriter. + * @param {!proto.cls.LogTag} message + * @param {!jspb.BinaryWriter} writer + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.LogTag.serializeBinaryToWriter = function(message, writer) { + var f = undefined; + f = /** @type {string} */ (jspb.Message.getField(message, 1)); + if (f != null) { + writer.writeString( + 1, + f + ); + } + f = /** @type {string} */ (jspb.Message.getField(message, 2)); + if (f != null) { + writer.writeString( + 2, + f + ); + } +}; + + +/** + * required string key = 1; + * @return {string} + */ +proto.cls.LogTag.prototype.getKey = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.LogTag} returns this + */ +proto.cls.LogTag.prototype.setKey = function(value) { + return jspb.Message.setField(this, 1, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.LogTag} returns this + */ +proto.cls.LogTag.prototype.clearKey = function() { + return jspb.Message.setField(this, 1, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.LogTag.prototype.hasKey = function() { + return jspb.Message.getField(this, 1) != null; +}; + + +/** + * required string value = 2; + * @return {string} + */ +proto.cls.LogTag.prototype.getValue = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 2, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.LogTag} returns this + */ +proto.cls.LogTag.prototype.setValue = function(value) { + return jspb.Message.setField(this, 2, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.LogTag} returns this + */ +proto.cls.LogTag.prototype.clearValue = function() { + return jspb.Message.setField(this, 2, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.LogTag.prototype.hasValue = function() { + return jspb.Message.getField(this, 2) != null; +}; + + + +/** + * List of repeated fields within this message type. + * @private {!Array} + * @const + */ +proto.cls.LogGroup.repeatedFields_ = [1,5]; + + + +if (jspb.Message.GENERATE_TO_OBJECT) { +/** + * Creates an object representation of this proto. + * Field names that are reserved in JavaScript and will be renamed to pb_name. + * Optional fields that are not set will be set to undefined. + * To access a reserved field use, foo.pb_, eg, foo.pb_default. + * For the list of reserved names please see: + * net/proto2/compiler/js/internal/generator.cc#kKeyword. + * @param {boolean=} opt_includeInstance Deprecated. whether to include the + * JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @return {!Object} + */ +proto.cls.LogGroup.prototype.toObject = function(opt_includeInstance) { + return proto.cls.LogGroup.toObject(opt_includeInstance, this); +}; + + +/** + * Static version of the {@see toObject} method. + * @param {boolean|undefined} includeInstance Deprecated. Whether to include + * the JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @param {!proto.cls.LogGroup} msg The msg instance to transform. + * @return {!Object} + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.LogGroup.toObject = function(includeInstance, msg) { + var f, obj = { + logsList: jspb.Message.toObjectList(msg.getLogsList(), + proto.cls.Log.toObject, includeInstance), + contextflow: (f = jspb.Message.getField(msg, 2)) == null ? undefined : f, + filename: (f = jspb.Message.getField(msg, 3)) == null ? undefined : f, + source: (f = jspb.Message.getField(msg, 4)) == null ? undefined : f, + logtagsList: jspb.Message.toObjectList(msg.getLogtagsList(), + proto.cls.LogTag.toObject, includeInstance) + }; + + if (includeInstance) { + obj.$jspbMessageInstance = msg; + } + return obj; +}; +} + + +/** + * Deserializes binary data (in protobuf wire format). + * @param {jspb.ByteSource} bytes The bytes to deserialize. + * @return {!proto.cls.LogGroup} + */ +proto.cls.LogGroup.deserializeBinary = function(bytes) { + var reader = new jspb.BinaryReader(bytes); + var msg = new proto.cls.LogGroup; + return proto.cls.LogGroup.deserializeBinaryFromReader(msg, reader); +}; + + +/** + * Deserializes binary data (in protobuf wire format) from the + * given reader into the given message object. + * @param {!proto.cls.LogGroup} msg The message object to deserialize into. + * @param {!jspb.BinaryReader} reader The BinaryReader to use. + * @return {!proto.cls.LogGroup} + */ +proto.cls.LogGroup.deserializeBinaryFromReader = function(msg, reader) { + while (reader.nextField()) { + if (reader.isEndGroup()) { + break; + } + var field = reader.getFieldNumber(); + switch (field) { + case 1: + var value = new proto.cls.Log; + reader.readMessage(value,proto.cls.Log.deserializeBinaryFromReader); + msg.addLogs(value); + break; + case 2: + var value = /** @type {string} */ (reader.readString()); + msg.setContextflow(value); + break; + case 3: + var value = /** @type {string} */ (reader.readString()); + msg.setFilename(value); + break; + case 4: + var value = /** @type {string} */ (reader.readString()); + msg.setSource(value); + break; + case 5: + var value = new proto.cls.LogTag; + reader.readMessage(value,proto.cls.LogTag.deserializeBinaryFromReader); + msg.addLogtags(value); + break; + default: + reader.skipField(); + break; + } + } + return msg; +}; + + +/** + * Serializes the message to binary data (in protobuf wire format). + * @return {!Uint8Array} + */ +proto.cls.LogGroup.prototype.serializeBinary = function() { + var writer = new jspb.BinaryWriter(); + proto.cls.LogGroup.serializeBinaryToWriter(this, writer); + return writer.getResultBuffer(); +}; + + +/** + * Serializes the given message to binary data (in protobuf wire + * format), writing to the given BinaryWriter. + * @param {!proto.cls.LogGroup} message + * @param {!jspb.BinaryWriter} writer + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.LogGroup.serializeBinaryToWriter = function(message, writer) { + var f = undefined; + f = message.getLogsList(); + if (f.length > 0) { + writer.writeRepeatedMessage( + 1, + f, + proto.cls.Log.serializeBinaryToWriter + ); + } + f = /** @type {string} */ (jspb.Message.getField(message, 2)); + if (f != null) { + writer.writeString( + 2, + f + ); + } + f = /** @type {string} */ (jspb.Message.getField(message, 3)); + if (f != null) { + writer.writeString( + 3, + f + ); + } + f = /** @type {string} */ (jspb.Message.getField(message, 4)); + if (f != null) { + writer.writeString( + 4, + f + ); + } + f = message.getLogtagsList(); + if (f.length > 0) { + writer.writeRepeatedMessage( + 5, + f, + proto.cls.LogTag.serializeBinaryToWriter + ); + } +}; + + +/** + * repeated Log logs = 1; + * @return {!Array} + */ +proto.cls.LogGroup.prototype.getLogsList = function() { + return /** @type{!Array} */ ( + jspb.Message.getRepeatedWrapperField(this, proto.cls.Log, 1)); +}; + + +/** + * @param {!Array} value + * @return {!proto.cls.LogGroup} returns this +*/ +proto.cls.LogGroup.prototype.setLogsList = function(value) { + return jspb.Message.setRepeatedWrapperField(this, 1, value); +}; + + +/** + * @param {!proto.cls.Log=} opt_value + * @param {number=} opt_index + * @return {!proto.cls.Log} + */ +proto.cls.LogGroup.prototype.addLogs = function(opt_value, opt_index) { + return jspb.Message.addToRepeatedWrapperField(this, 1, opt_value, proto.cls.Log, opt_index); +}; + + +/** + * Clears the list making it empty but non-null. + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.clearLogsList = function() { + return this.setLogsList([]); +}; + + +/** + * optional string contextFlow = 2; + * @return {string} + */ +proto.cls.LogGroup.prototype.getContextflow = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 2, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.setContextflow = function(value) { + return jspb.Message.setField(this, 2, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.clearContextflow = function() { + return jspb.Message.setField(this, 2, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.LogGroup.prototype.hasContextflow = function() { + return jspb.Message.getField(this, 2) != null; +}; + + +/** + * optional string filename = 3; + * @return {string} + */ +proto.cls.LogGroup.prototype.getFilename = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 3, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.setFilename = function(value) { + return jspb.Message.setField(this, 3, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.clearFilename = function() { + return jspb.Message.setField(this, 3, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.LogGroup.prototype.hasFilename = function() { + return jspb.Message.getField(this, 3) != null; +}; + + +/** + * optional string source = 4; + * @return {string} + */ +proto.cls.LogGroup.prototype.getSource = function() { + return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 4, "")); +}; + + +/** + * @param {string} value + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.setSource = function(value) { + return jspb.Message.setField(this, 4, value); +}; + + +/** + * Clears the field making it undefined. + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.clearSource = function() { + return jspb.Message.setField(this, 4, undefined); +}; + + +/** + * Returns whether this field is set. + * @return {boolean} + */ +proto.cls.LogGroup.prototype.hasSource = function() { + return jspb.Message.getField(this, 4) != null; +}; + + +/** + * repeated LogTag logTags = 5; + * @return {!Array} + */ +proto.cls.LogGroup.prototype.getLogtagsList = function() { + return /** @type{!Array} */ ( + jspb.Message.getRepeatedWrapperField(this, proto.cls.LogTag, 5)); +}; + + +/** + * @param {!Array} value + * @return {!proto.cls.LogGroup} returns this +*/ +proto.cls.LogGroup.prototype.setLogtagsList = function(value) { + return jspb.Message.setRepeatedWrapperField(this, 5, value); +}; + + +/** + * @param {!proto.cls.LogTag=} opt_value + * @param {number=} opt_index + * @return {!proto.cls.LogTag} + */ +proto.cls.LogGroup.prototype.addLogtags = function(opt_value, opt_index) { + return jspb.Message.addToRepeatedWrapperField(this, 5, opt_value, proto.cls.LogTag, opt_index); +}; + + +/** + * Clears the list making it empty but non-null. + * @return {!proto.cls.LogGroup} returns this + */ +proto.cls.LogGroup.prototype.clearLogtagsList = function() { + return this.setLogtagsList([]); +}; + + + +/** + * List of repeated fields within this message type. + * @private {!Array} + * @const + */ +proto.cls.LogGroupList.repeatedFields_ = [1]; + + + +if (jspb.Message.GENERATE_TO_OBJECT) { +/** + * Creates an object representation of this proto. + * Field names that are reserved in JavaScript and will be renamed to pb_name. + * Optional fields that are not set will be set to undefined. + * To access a reserved field use, foo.pb_, eg, foo.pb_default. + * For the list of reserved names please see: + * net/proto2/compiler/js/internal/generator.cc#kKeyword. + * @param {boolean=} opt_includeInstance Deprecated. whether to include the + * JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @return {!Object} + */ +proto.cls.LogGroupList.prototype.toObject = function(opt_includeInstance) { + return proto.cls.LogGroupList.toObject(opt_includeInstance, this); +}; + + +/** + * Static version of the {@see toObject} method. + * @param {boolean|undefined} includeInstance Deprecated. Whether to include + * the JSPB instance for transitional soy proto support: + * http://goto/soy-param-migration + * @param {!proto.cls.LogGroupList} msg The msg instance to transform. + * @return {!Object} + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.LogGroupList.toObject = function(includeInstance, msg) { + var f, obj = { + loggrouplistList: jspb.Message.toObjectList(msg.getLoggrouplistList(), + proto.cls.LogGroup.toObject, includeInstance) + }; + + if (includeInstance) { + obj.$jspbMessageInstance = msg; + } + return obj; +}; +} + + +/** + * Deserializes binary data (in protobuf wire format). + * @param {jspb.ByteSource} bytes The bytes to deserialize. + * @return {!proto.cls.LogGroupList} + */ +proto.cls.LogGroupList.deserializeBinary = function(bytes) { + var reader = new jspb.BinaryReader(bytes); + var msg = new proto.cls.LogGroupList; + return proto.cls.LogGroupList.deserializeBinaryFromReader(msg, reader); +}; + + +/** + * Deserializes binary data (in protobuf wire format) from the + * given reader into the given message object. + * @param {!proto.cls.LogGroupList} msg The message object to deserialize into. + * @param {!jspb.BinaryReader} reader The BinaryReader to use. + * @return {!proto.cls.LogGroupList} + */ +proto.cls.LogGroupList.deserializeBinaryFromReader = function(msg, reader) { + while (reader.nextField()) { + if (reader.isEndGroup()) { + break; + } + var field = reader.getFieldNumber(); + switch (field) { + case 1: + var value = new proto.cls.LogGroup; + reader.readMessage(value,proto.cls.LogGroup.deserializeBinaryFromReader); + msg.addLoggrouplist(value); + break; + default: + reader.skipField(); + break; + } + } + return msg; +}; + + +/** + * Serializes the message to binary data (in protobuf wire format). + * @return {!Uint8Array} + */ +proto.cls.LogGroupList.prototype.serializeBinary = function() { + var writer = new jspb.BinaryWriter(); + proto.cls.LogGroupList.serializeBinaryToWriter(this, writer); + return writer.getResultBuffer(); +}; + + +/** + * Serializes the given message to binary data (in protobuf wire + * format), writing to the given BinaryWriter. + * @param {!proto.cls.LogGroupList} message + * @param {!jspb.BinaryWriter} writer + * @suppress {unusedLocalVariables} f is only used for nested messages + */ +proto.cls.LogGroupList.serializeBinaryToWriter = function(message, writer) { + var f = undefined; + f = message.getLoggrouplistList(); + if (f.length > 0) { + writer.writeRepeatedMessage( + 1, + f, + proto.cls.LogGroup.serializeBinaryToWriter + ); + } +}; + + +/** + * repeated LogGroup logGroupList = 1; + * @return {!Array} + */ +proto.cls.LogGroupList.prototype.getLoggrouplistList = function() { + return /** @type{!Array} */ ( + jspb.Message.getRepeatedWrapperField(this, proto.cls.LogGroup, 1)); +}; + + +/** + * @param {!Array} value + * @return {!proto.cls.LogGroupList} returns this +*/ +proto.cls.LogGroupList.prototype.setLoggrouplistList = function(value) { + return jspb.Message.setRepeatedWrapperField(this, 1, value); +}; + + +/** + * @param {!proto.cls.LogGroup=} opt_value + * @param {number=} opt_index + * @return {!proto.cls.LogGroup} + */ +proto.cls.LogGroupList.prototype.addLoggrouplist = function(opt_value, opt_index) { + return jspb.Message.addToRepeatedWrapperField(this, 1, opt_value, proto.cls.LogGroup, opt_index); +}; + + +/** + * Clears the list making it empty but non-null. + * @return {!proto.cls.LogGroupList} returns this + */ +proto.cls.LogGroupList.prototype.clearLoggrouplistList = function() { + return this.setLoggrouplistList([]); +}; + + +goog.object.extend(exports, proto.cls); diff --git a/examples/cls/src/index.ts b/examples/cls/src/index.ts new file mode 100644 index 000000000..3aa7ebb84 --- /dev/null +++ b/examples/cls/src/index.ts @@ -0,0 +1,64 @@ +import * as tencentcloud from "tencentcloud-sdk-nodejs" +var messages = require('./cls_pb'); + +// 导入对应产品模块的client models。 +const ClsClient = tencentcloud.cls.v20201016.Client + +const clientConfig: any = { + // 腾讯云认证信息 + credential: { + secretId: "【必填】", + secretKey: "【必填】", + }, + // 产品地域 + region: "ap-guangzhou", + // 可选配置实例 + profile: { + signMethod: "TC3-HMAC-SHA256", // 签名方法 + httpProfile: { + endpoint: "cls.tencentcloudapi.com", + reqMethod: "POST", // 请求方法 + reqTimeout: 30, // 请求超时时间,默认60s + }, + headers: { + "X-CLS-TopicId": "【必填topicid】", + "Content-Type": "application/octet-stream", + } + }, +} + +const client = new ClsClient(clientConfig) + + +var grouplist = new messages.LogGroupList(); +var loggroup = new messages.LogGroup(); +var log = new messages.Log(); +var content = new messages.Log.Content(["system1,log", "start1,report"]) +log.setTime(123456) +log.addContents(content) +loggroup.addLogs(log) +grouplist.addLoggrouplist(loggroup) + + +console.log( Number.isSafeInteger( parseInt( "123456" ) ) ); + + +var tmp = Uint8ArrayToString(grouplist.serializeBinary()); + +client.UploadLog(tmp).then( + (data) => { + console.log(data) + }, + (err) => { + console.error("error", err) + } +); + +function Uint8ArrayToString(fileData: any){ + var dataString = ""; + for (var i = 0; i < fileData.length; i++) { + dataString += String.fromCharCode(fileData[i]); + } + return dataString +} + diff --git a/examples/cls/tsconfig.json b/examples/cls/tsconfig.json new file mode 100644 index 000000000..2fd7dc61a --- /dev/null +++ b/examples/cls/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "outDir": "./dist", + "rootDir": "./src", + "strict": true, + "moduleResolution": "node", + "esModuleInterop": true, + }, + "exclude":[ + "./node_modules" + ] +} \ No newline at end of file From 1209295a674d5a06e1e56b7331539a21b0dc4e12 Mon Sep 17 00:00:00 2001 From: farmerx Date: Thu, 12 Aug 2021 17:22:25 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0readme=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/cls/README.md | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 examples/cls/README.md diff --git a/examples/cls/README.md b/examples/cls/README.md new file mode 100644 index 000000000..583babd51 --- /dev/null +++ b/examples/cls/README.md @@ -0,0 +1,53 @@ +### CLS Node SDK Demo +--- + + +### 注意事项 + +js版本的pb文件和其他语言的不太一样, 多增加 [jstype = JS_STRING],来解决js proto int64的问题。 + +``` +syntax = "proto2"; + +package cls; + +message Log +{ + required int64 time = 1 [jstype = JS_STRING]; + message Content + { + required string key = 1; + required string value = 2; + } + repeated Content contents= 2; +} + +message LogTag +{ + required string key = 1; + required string value = 2; +} + +message LogGroup +{ + repeated Log logs= 1; + optional string contextFlow= 2; + optional string filename = 3; + optional string source = 4; + repeated LogTag logTags = 5; +} + +message LogGroupList +{ + repeated LogGroup logGroupList = 1; +} +``` + +### 运行 + +1、 npm install +2、 npm run start + + + + From 26663f5a79d37ec6c16ffd883dfd9447928c181c Mon Sep 17 00:00:00 2001 From: farmerx Date: Thu, 12 Aug 2021 18:27:13 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9sdk=20ts=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=BA=90=E7=A0=81=EF=BC=8C=E6=94=AF=E6=8C=81cls?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/http/http_connection.ts | 31 ++++++++++++++++-------- src/common/interface.ts | 6 +++++ src/common/sign.ts | 6 +++++ src/services/cls/v20201016/cls_client.ts | 2 +- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/common/http/http_connection.ts b/src/common/http/http_connection.ts index b2d4e9de6..8ac3e7d78 100755 --- a/src/common/http/http_connection.ts +++ b/src/common/http/http_connection.ts @@ -58,6 +58,7 @@ export class HttpConnection { token, requestClient, language, + headers, }: { method: string url: string @@ -73,6 +74,7 @@ export class HttpConnection { token: string requestClient: string language: string + headers?: any, }): Promise { // data 中可能带有 readStream,由于需要计算整个 body 的 hash, // 所以这里把 readStream 转为 Buffer @@ -130,17 +132,25 @@ export class HttpConnection { if (method === "GET") { config.headers["Content-Type"] = "application/x-www-form-urlencoded" } - if (method === "POST" && !multipart) { - config.body = JSON.stringify(data) - config.headers["Content-Type"] = "application/json" - } - if (method === "POST" && multipart) { - form = new FormData() - for (const key in data) { - form.append(key, data[key]) + let isOctetStream = false; + if (method === "POST") { + if (multipart) { + form = new FormData() + for (const key in data) { + form.append(key, data[key]) + } + config.body = form + config.headers = Object.assign({}, config.headers, form.getHeaders()) + } else if (headers !== null && headers !== undefined) { + config.body = data + config.headers = Object.assign({}, config.headers, headers) + if (config.headers["Content-Type"] == "application/octet-stream") { + isOctetStream = true + } else { + config.body = JSON.stringify(data); + config.headers["Content-Type"] = "application/json"; + } } - config.body = form - config.headers = Object.assign({}, config.headers, form.getHeaders()) } const signature = Sign.sign3({ @@ -153,6 +163,7 @@ export class HttpConnection { secretKey, multipart, boundary: form ? form.getBoundary() : undefined, + isOctetStream }) config.headers["Authorization"] = signature diff --git a/src/common/interface.ts b/src/common/interface.ts index b8da46216..2130d37c1 100755 --- a/src/common/interface.ts +++ b/src/common/interface.ts @@ -66,6 +66,12 @@ export interface ClientProfile { * 非必选 */ language?: "zh-CN" | "en-US" + /** + * api请求时附带的 language 字段 + * @type {"zh-CN" | "en-US"} + * 非必选 + */ + headers?: any } /** diff --git a/src/common/sign.ts b/src/common/sign.ts index fef7f47bd..79e40525d 100755 --- a/src/common/sign.ts +++ b/src/common/sign.ts @@ -33,6 +33,7 @@ export default class Sign { secretKey, multipart, boundary, + isOctetStream = false }: { method?: string url?: string @@ -43,6 +44,7 @@ export default class Sign { secretKey: string multipart: boolean boundary: string + isOctetStream: boolean }): string { const urlObj = new URL(url) @@ -56,6 +58,8 @@ export default class Sign { signedHeaders = "content-type" if (multipart) { headers = `content-type:multipart/form-data; boundary=${boundary}\n` + } else if (isOctetStream) { + headers = "content-type:application/octet-stream\n"; } else { headers = "content-type:application/json\n" } @@ -86,6 +90,8 @@ export default class Sign { } hash.update(`--\r\n`) payload_hash = hash.digest("hex") + } else if (isOctetStream) { + payload_hash = payload ? getHash(payload) : getHash(""); } else { payload_hash = payload ? getHash(JSON.stringify(payload)) : getHash("") } diff --git a/src/services/cls/v20201016/cls_client.ts b/src/services/cls/v20201016/cls_client.ts index 0547f7f76..d8ff8f83a 100644 --- a/src/services/cls/v20201016/cls_client.ts +++ b/src/services/cls/v20201016/cls_client.ts @@ -721,7 +721,7 @@ cls.pb.cc cls.pb.h cls.proto 将生成的 cls.pb.h 头文件引入代码中,调用接口进行数据格式封装。 */ async UploadLog( - req?: UploadLogRequest, + req?: any, cb?: (error: string, rep: UploadLogResponse) => void ): Promise { return this.request("UploadLog", req, cb) From cee1d12d991b31aa865a6d407951fc3bf7474b84 Mon Sep 17 00:00:00 2001 From: farmerx Date: Thu, 12 Aug 2021 18:29:06 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9sdk=20ts=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=BA=90=E7=A0=81=EF=BC=8C=E6=94=AF=E6=8C=81cls?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/http/http_connection.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common/http/http_connection.ts b/src/common/http/http_connection.ts index 8ac3e7d78..79fb3ec15 100755 --- a/src/common/http/http_connection.ts +++ b/src/common/http/http_connection.ts @@ -146,10 +146,10 @@ export class HttpConnection { config.headers = Object.assign({}, config.headers, headers) if (config.headers["Content-Type"] == "application/octet-stream") { isOctetStream = true - } else { - config.body = JSON.stringify(data); - config.headers["Content-Type"] = "application/json"; - } + } + } else { + config.body = JSON.stringify(data); + config.headers["Content-Type"] = "application/json"; } }