tencentcloud-sdk-nodejs 4.0.1042-beta.0 → 4.0.1042-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- import * as QueryString from "querystring";
1
+ import QueryString from "querystring";
2
2
  import { URL } from "url";
3
- import * as isStream from "is-stream";
4
- import * as getStream from "get-stream";
3
+ import isStream from "is-stream";
4
+ import getStream from "get-stream";
5
5
  import FormData from "form-data";
6
6
  import Sign from "../sign";
7
7
  import fetch from "./fetch";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tencentcloud-sdk-nodejs",
3
- "version": "4.0.1042-beta.0",
3
+ "version": "4.0.1042-beta.2",
4
4
  "description": "腾讯云 API NODEJS SDK",
5
5
  "main": "./tencentcloud/index.js",
6
6
  "module": "./es/index.js",
@@ -0,0 +1,62 @@
1
+ import { ClientProfile, Credential, ClientConfig, HttpProfile, DynamicCredential } from "./interface";
2
+ export declare type ResponseCallback<TReuslt = any> = (error: string, rep: TReuslt) => void;
3
+ export interface RequestOptions extends Partial<Pick<HttpProfile, "headers">> {
4
+ multipart?: boolean;
5
+ /**
6
+ * 中止请求信号
7
+ */
8
+ signal?: AbortSignal;
9
+ }
10
+ declare type ResponseData = any;
11
+ /**
12
+ * @inner
13
+ */
14
+ export declare class AbstractClient {
15
+ sdkVersion: string;
16
+ path: string;
17
+ credential: Credential | DynamicCredential;
18
+ region: string;
19
+ apiVersion: string;
20
+ endpoint: string;
21
+ profile: ClientProfile;
22
+ /**
23
+ * 实例化client对象
24
+ * @param {string} endpoint 接入点域名
25
+ * @param {string} version 产品版本
26
+ * @param {Credential} credential 认证信息实例
27
+ * @param {string} region 产品地域
28
+ * @param {ClientProfile} profile 可选配置实例
29
+ */
30
+ constructor(endpoint: string, version: string, { credential, region, profile }: ClientConfig);
31
+ getCredential(): Promise<Credential>;
32
+ /**
33
+ * @inner
34
+ */
35
+ request(action: string, req: any, options?: ResponseCallback | RequestOptions, cb?: ResponseCallback): Promise<ResponseData>;
36
+ /**
37
+ * @inner
38
+ */
39
+ requestOctetStream(action: string, req: any, options?: ResponseCallback | RequestOptions, cb?: ResponseCallback): Promise<any>;
40
+ /**
41
+ * @inner
42
+ */
43
+ private doRequest;
44
+ /**
45
+ * @inner
46
+ */
47
+ private doRequestWithSign3;
48
+ private parseResponse;
49
+ /**
50
+ * @inner
51
+ */
52
+ private mergeData;
53
+ /**
54
+ * @inner
55
+ */
56
+ private formatRequestData;
57
+ /**
58
+ * @inner
59
+ */
60
+ private formatSignString;
61
+ }
62
+ export {};
@@ -9,9 +9,23 @@ const http_connection_1 = require("./http/http_connection");
9
9
  const tencent_cloud_sdk_exception_1 = tslib_1.__importDefault(require("./exception/tencent_cloud_sdk_exception"));
10
10
  const sse_response_model_1 = require("./sse_response_model");
11
11
  const uuid_1 = require("uuid");
12
+ /**
13
+ * @inner
14
+ */
12
15
  class AbstractClient {
16
+ /**
17
+ * 实例化client对象
18
+ * @param {string} endpoint 接入点域名
19
+ * @param {string} version 产品版本
20
+ * @param {Credential} credential 认证信息实例
21
+ * @param {string} region 产品地域
22
+ * @param {ClientProfile} profile 可选配置实例
23
+ */
13
24
  constructor(endpoint, version, { credential, region, profile = {} }) {
14
25
  this.path = "/";
26
+ /**
27
+ * 认证信息实例
28
+ */
15
29
  if (credential && "getCredential" in credential) {
16
30
  this.credential = credential;
17
31
  }
@@ -22,10 +36,17 @@ class AbstractClient {
22
36
  token: null,
23
37
  }, credential);
24
38
  }
39
+ /**
40
+ * 产品地域
41
+ */
25
42
  this.region = region || null;
26
43
  this.sdkVersion = "SDK_NODEJS_" + sdk_version_1.sdkVersion;
27
44
  this.apiVersion = version;
28
45
  this.endpoint = (profile && profile.httpProfile && profile.httpProfile.endpoint) || endpoint;
46
+ /**
47
+ * 可选配置实例
48
+ * @type {ClientProfile}
49
+ */
29
50
  this.profile = {
30
51
  signMethod: (profile && profile.signMethod) || "TC3-HMAC-SHA256",
31
52
  httpProfile: Object.assign({
@@ -46,6 +67,9 @@ class AbstractClient {
46
67
  }
47
68
  return this.credential;
48
69
  }
70
+ /**
71
+ * @inner
72
+ */
49
73
  async request(action, req, options, cb) {
50
74
  if (typeof options === "function") {
51
75
  cb = options;
@@ -61,6 +85,9 @@ class AbstractClient {
61
85
  throw e;
62
86
  }
63
87
  }
88
+ /**
89
+ * @inner
90
+ */
64
91
  async requestOctetStream(action, req, options, cb) {
65
92
  if (typeof options === "function") {
66
93
  cb = options;
@@ -80,6 +107,9 @@ class AbstractClient {
80
107
  throw e;
81
108
  }
82
109
  }
110
+ /**
111
+ * @inner
112
+ */
83
113
  async doRequest(action, req, options = {}) {
84
114
  if (this.profile.signMethod === "TC3-HMAC-SHA256") {
85
115
  return this.doRequestWithSign3(action, req, options);
@@ -116,6 +146,9 @@ class AbstractClient {
116
146
  }
117
147
  return this.parseResponse(res);
118
148
  }
149
+ /**
150
+ * @inner
151
+ */
119
152
  async doRequestWithSign3(action, params, options = {}) {
120
153
  const headers = Object.assign({}, this.profile.httpProfile.headers, options.headers);
121
154
  let traceId = "";
@@ -182,6 +215,9 @@ class AbstractClient {
182
215
  }
183
216
  }
184
217
  }
218
+ /**
219
+ * @inner
220
+ */
185
221
  mergeData(data, prefix = "") {
186
222
  const ret = {};
187
223
  for (const k in data) {
@@ -197,6 +233,9 @@ class AbstractClient {
197
233
  }
198
234
  return ret;
199
235
  }
236
+ /**
237
+ * @inner
238
+ */
200
239
  async formatRequestData(action, params) {
201
240
  params.Action = action;
202
241
  params.RequestClient = this.sdkVersion;
@@ -223,6 +262,9 @@ class AbstractClient {
223
262
  params.Signature = sign_1.default.sign(credential.secretKey, signStr, this.profile.signMethod);
224
263
  return params;
225
264
  }
265
+ /**
266
+ * @inner
267
+ */
226
268
  formatSignString(params) {
227
269
  let strParam = "";
228
270
  const keys = Object.keys(params);
@@ -231,6 +273,7 @@ class AbstractClient {
231
273
  if (!keys.hasOwnProperty(k)) {
232
274
  continue;
233
275
  }
276
+ //k = k.replace(/_/g, '.');
234
277
  strParam += "&" + keys[k] + "=" + params[keys[k]];
235
278
  }
236
279
  const strSign = this.profile.httpProfile.reqMethod.toLocaleUpperCase() +
@@ -0,0 +1,3 @@
1
+ import { AbstractClient } from "./abstract_client";
2
+ export declare class CommonClient extends AbstractClient {
3
+ }
@@ -0,0 +1,17 @@
1
+ import { Credential, DynamicCredential } from "./interface";
2
+ interface CvmRoleCredentialResult {
3
+ TmpSecretId: string;
4
+ TmpSecretKey: string;
5
+ ExpiredTime: 1671330188;
6
+ Expiration: string;
7
+ Token: string;
8
+ Code: string;
9
+ }
10
+ export default class CvmRoleCredential implements DynamicCredential {
11
+ protected roleNameTask: Promise<string> | null;
12
+ protected credentialTask: Promise<CvmRoleCredentialResult> | null;
13
+ protected getRoleName(): Promise<string>;
14
+ protected getRoleCredential(roleName: string): Promise<CvmRoleCredentialResult>;
15
+ getCredential(): Promise<Credential>;
16
+ }
17
+ export {};
@@ -33,6 +33,7 @@ class CvmRoleCredential {
33
33
  this.credentialTask = this.getRoleCredential(roleName);
34
34
  }
35
35
  const credential = await this.credentialTask;
36
+ // expired
36
37
  if (credential.ExpiredTime * 1000 - EXPIRE_BUFFER <= Date.now()) {
37
38
  this.credentialTask = null;
38
39
  return this.getCredential();
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @inner
3
+ */
4
+ export default class TencentCloudSDKHttpException extends Error {
5
+ /**
6
+ * 请求id
7
+ */
8
+ requestId: string;
9
+ /**
10
+ * 请求traceId
11
+ */
12
+ traceId: string;
13
+ /**
14
+ * http状态码
15
+ */
16
+ httpCode?: number;
17
+ /**
18
+ * 接口返回状态码
19
+ */
20
+ code?: string;
21
+ constructor(error: string, requestId?: string, traceId?: string);
22
+ getMessage(): string;
23
+ getRequestId(): string;
24
+ getTraceId(): string;
25
+ toString(): string;
26
+ toLocaleString(): string;
27
+ }
@@ -1,5 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * @inner
5
+ */
3
6
  class TencentCloudSDKHttpException extends Error {
4
7
  constructor(error, requestId = "", traceId = "") {
5
8
  super(error);
@@ -0,0 +1,7 @@
1
+ import { RequestInit, Response } from "node-fetch";
2
+ export interface FetchOptions extends Omit<RequestInit, "signal"> {
3
+ proxy?: string;
4
+ headers?: Record<string, string>;
5
+ signal?: AbortSignal;
6
+ }
7
+ export default function (url: string, options: FetchOptions): Promise<Response>;
@@ -0,0 +1,38 @@
1
+ /// <reference types="node" />
2
+ import { Response } from "node-fetch";
3
+ import { Agent } from "http";
4
+ /**
5
+ * @inner
6
+ */
7
+ export declare class HttpConnection {
8
+ static doRequest({ method, url, data, timeout, headers, agent, proxy, signal, }: {
9
+ method: string;
10
+ url: string;
11
+ data: any;
12
+ timeout: number;
13
+ headers?: Record<string, string>;
14
+ agent?: Agent;
15
+ proxy?: string;
16
+ signal?: AbortSignal;
17
+ }): Promise<Response>;
18
+ static doRequestWithSign3({ method, url, data, service, action, region, version, secretId, secretKey, multipart, timeout, token, requestClient, language, headers, agent, proxy, signal, }: {
19
+ method: string;
20
+ url: string;
21
+ data: any;
22
+ service: string;
23
+ action: string;
24
+ region: string;
25
+ version: string;
26
+ secretId: string;
27
+ secretKey: string;
28
+ multipart?: boolean;
29
+ timeout?: number;
30
+ token: string;
31
+ requestClient: string;
32
+ language: string;
33
+ headers?: Record<string, string>;
34
+ agent?: Agent;
35
+ proxy?: string;
36
+ signal?: AbortSignal;
37
+ }): Promise<Response>;
38
+ }
@@ -2,15 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpConnection = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const QueryString = tslib_1.__importStar(require("querystring"));
5
+ const querystring_1 = tslib_1.__importDefault(require("querystring"));
6
6
  const url_1 = require("url");
7
- const isStream = tslib_1.__importStar(require("is-stream"));
8
- const getStream = tslib_1.__importStar(require("get-stream"));
7
+ const is_stream_1 = tslib_1.__importDefault(require("is-stream"));
8
+ const get_stream_1 = tslib_1.__importDefault(require("get-stream"));
9
9
  const form_data_1 = tslib_1.__importDefault(require("form-data"));
10
10
  const sign_1 = tslib_1.__importDefault(require("../sign"));
11
11
  const fetch_1 = tslib_1.__importDefault(require("./fetch"));
12
12
  const json_bigint_1 = tslib_1.__importDefault(require("json-bigint"));
13
13
  const JSONbigNative = json_bigint_1.default({ useNativeBigInt: true });
14
+ /**
15
+ * @inner
16
+ */
14
17
  class HttpConnection {
15
18
  static async doRequest({ method, url, data, timeout, headers = {}, agent, proxy, signal, }) {
16
19
  const config = {
@@ -22,23 +25,28 @@ class HttpConnection {
22
25
  signal,
23
26
  };
24
27
  if (method === "GET") {
25
- url += "?" + QueryString.stringify(data);
28
+ url += "?" + querystring_1.default.stringify(data);
26
29
  }
27
30
  else {
28
31
  config.headers["Content-Type"] = "application/x-www-form-urlencoded";
29
- config.body = QueryString.stringify(data);
32
+ config.body = querystring_1.default.stringify(data);
30
33
  }
31
34
  return await fetch_1.default(url, config);
32
35
  }
33
36
  static async doRequestWithSign3({ method, url, data, service, action, region, version, secretId, secretKey, multipart = false, timeout = 60000, token, requestClient, language, headers = {}, agent, proxy, signal, }) {
37
+ // data 中可能带有 readStream,由于需要计算整个 body 的 hash,
38
+ // 所以这里把 readStream 转为 Buffer
39
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
34
40
  await convertReadStreamToBuffer(data);
41
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
35
42
  data = deepRemoveNull(data);
36
43
  const timestamp = parseInt(String(new Date().getTime() / 1000));
37
44
  method = method.toUpperCase();
38
45
  let payload = "";
39
46
  if (method === "GET") {
47
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
40
48
  data = mergeData(data);
41
- url += "?" + QueryString.stringify(data);
49
+ url += "?" + querystring_1.default.stringify(data);
42
50
  }
43
51
  if (method === "POST") {
44
52
  payload = data;
@@ -106,8 +114,8 @@ class HttpConnection {
106
114
  exports.HttpConnection = HttpConnection;
107
115
  async function convertReadStreamToBuffer(data) {
108
116
  for (const key in data) {
109
- if (isStream(data[key])) {
110
- data[key] = await getStream.buffer(data[key]);
117
+ if (is_stream_1.default(data[key])) {
118
+ data[key] = await get_stream_1.default.buffer(data[key]);
111
119
  }
112
120
  }
113
121
  }
@@ -151,7 +159,7 @@ function isArray(x) {
151
159
  return Array.isArray(x);
152
160
  }
153
161
  function isObject(x) {
154
- return typeof x === "object" && !isArray(x) && !isStream(x) && !isBuffer(x) && x !== null;
162
+ return typeof x === "object" && !isArray(x) && !is_stream_1.default(x) && !isBuffer(x) && x !== null;
155
163
  }
156
164
  function isNull(x) {
157
165
  return x === null;
@@ -0,0 +1,3 @@
1
+ export * from "./abstract_client";
2
+ export * from "./common_client";
3
+ export * from "./interface";
@@ -0,0 +1,117 @@
1
+ /// <reference types="node" />
2
+ import { Agent } from "http";
3
+ /**
4
+ * 初始化client对象参数类型
5
+ */
6
+ export interface ClientConfig {
7
+ /**
8
+ * @param {Credential} credential 认证信息
9
+ * 必选
10
+ */
11
+ credential: Credential | DynamicCredential;
12
+ /**
13
+ * @param {string} region 产品地域
14
+ * 对于要求区分地域的产品,此参数必选(如 cvm);对于不区分地域的产品(如 sms),无需传入。
15
+ */
16
+ region?: string;
17
+ /**
18
+ * @param {ClientProfile} profile 可选配置实例
19
+ * 可选,没有特殊需求可以跳过。
20
+ */
21
+ profile?: ClientProfile;
22
+ }
23
+ /**
24
+ * 可选配置实例
25
+ */
26
+ export interface ClientProfile {
27
+ /**
28
+ * 签名方法 (TC3-HMAC-SHA256 HmacSHA1 HmacSHA256)
29
+ * @type {string}
30
+ * 非必选
31
+ */
32
+ signMethod?: "TC3-HMAC-SHA256" | "HmacSHA256" | "HmacSHA1";
33
+ /**
34
+ * http相关选项实例
35
+ * @type {HttpProfile}
36
+ * 非必选
37
+ */
38
+ httpProfile?: HttpProfile;
39
+ /**
40
+ * api请求时附带的 language 字段
41
+ * @type {"zh-CN" | "en-US"}
42
+ * 非必选
43
+ */
44
+ language?: "zh-CN" | "en-US";
45
+ }
46
+ export interface HttpProfile {
47
+ /**
48
+ * 请求方法
49
+ * @type {"POST" | "GET"}
50
+ * 非必选
51
+ */
52
+ reqMethod?: "POST" | "GET";
53
+ /**
54
+ * 接入点域名,形如(cvm.ap-shanghai.tencentcloud.com)
55
+ * @type {string}
56
+ * 非必选
57
+ */
58
+ endpoint?: string;
59
+ /**
60
+ * 协议,目前支持(https://)
61
+ * @type {string}
62
+ * 非必选
63
+ */
64
+ protocol?: string;
65
+ /**
66
+ * 请求超时时间,默认60s
67
+ * @type {number}
68
+ * 非必选
69
+ */
70
+ reqTimeout?: number;
71
+ /**
72
+ * 自定义请求头,例如 { "X-TC-TraceId": "ffe0c072-8a5d-4e17-8887-a8a60252abca" }
73
+ * @type {Record<string, string>}
74
+ * 非必选
75
+ */
76
+ headers?: Record<string, string>;
77
+ /**
78
+ * 高级请求代理,例如 new HttpsProxyAgent("http://127.0.0.1:8899")
79
+ *
80
+ * 优先级高于 proxy 配置
81
+ */
82
+ agent?: Agent;
83
+ /**
84
+ * http请求代理,例如 "http://127.0.0.1:8899"
85
+ */
86
+ proxy?: string;
87
+ }
88
+ /**
89
+ * ClientProfile.language 属性支持的取值列表
90
+ */
91
+ export declare const SUPPORT_LANGUAGE_LIST: string[];
92
+ /**
93
+ * 认证信息类
94
+ */
95
+ export interface Credential {
96
+ /**
97
+ * 腾讯云账户secretId,secretKey
98
+ * 非必选,和 token 二选一
99
+ */
100
+ secretId?: string;
101
+ /**
102
+ * 腾讯云账户secretKey
103
+ * 非必选,和 token 二选一
104
+ */
105
+ secretKey?: string;
106
+ /**
107
+ * 腾讯云账户token
108
+ * 非必选,和 secretId 二选一
109
+ */
110
+ token?: string;
111
+ }
112
+ /**
113
+ * 动态认证信息
114
+ */
115
+ export interface DynamicCredential {
116
+ getCredential(): Promise<Credential>;
117
+ }
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SUPPORT_LANGUAGE_LIST = void 0;
4
+ /**
5
+ * ClientProfile.language 属性支持的取值列表
6
+ */
4
7
  exports.SUPPORT_LANGUAGE_LIST = ["zh-CN", "en-US"];
@@ -0,0 +1 @@
1
+ export declare const sdkVersion = "4.0.1041";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @inner
3
+ */
4
+ export default class Sign {
5
+ static sign(secretKey: string, signStr: string, signMethod: string): string;
6
+ static sign3({ method, url, payload, timestamp, service, secretId, secretKey, multipart, boundary, headers: configHeaders, }: {
7
+ method?: string;
8
+ url?: string;
9
+ payload: any;
10
+ timestamp: number;
11
+ service: string;
12
+ secretId: string;
13
+ secretKey: string;
14
+ multipart: boolean;
15
+ boundary: string;
16
+ headers: Record<string, string>;
17
+ }): string;
18
+ }
@@ -6,6 +6,9 @@ const crypto = tslib_1.__importStar(require("crypto"));
6
6
  const url_1 = require("url");
7
7
  const json_bigint_1 = tslib_1.__importDefault(require("json-bigint"));
8
8
  const JSONbigNative = json_bigint_1.default({ useNativeBigInt: true });
9
+ /**
10
+ * @inner
11
+ */
9
12
  class Sign {
10
13
  static sign(secretKey, signStr, signMethod) {
11
14
  const signMethodMap = {
@@ -21,6 +24,7 @@ class Sign {
21
24
  static sign3({ method = "POST", url = "", payload, timestamp, service, secretId, secretKey, multipart, boundary, headers: configHeaders = {}, }) {
22
25
  const urlObj = new url_1.URL(url);
23
26
  const contentType = configHeaders["Content-Type"];
27
+ // 通用头部
24
28
  let headers = "";
25
29
  let signedHeaders = "";
26
30
  if (method === "GET") {
@@ -0,0 +1,32 @@
1
+ /// <reference types="node" />
2
+ interface EventSourceMessage {
3
+ /** The event ID to set the EventSource object's last event ID value. */
4
+ id: string;
5
+ /** A string identifying the type of event described. */
6
+ event: string;
7
+ /** The event data */
8
+ data: string;
9
+ /** The reconnection interval (in milliseconds) to wait before retrying the connection */
10
+ retry?: number;
11
+ }
12
+ export declare class SSEResponseModel {
13
+ private stream;
14
+ private eventSource;
15
+ constructor(stream: NodeJS.ReadableStream);
16
+ /**
17
+ * @inner
18
+ */
19
+ private init;
20
+ /**
21
+ * @inner
22
+ */
23
+ private parseSSEMessage;
24
+ on(event: "message", listener: (message: EventSourceMessage) => void): this;
25
+ on(event: "close", listener: () => void): this;
26
+ on(event: "error", listener: (err: Error) => void): this;
27
+ removeListener(event: "message", listener: (message: EventSourceMessage) => void): this;
28
+ removeListener(event: "close", listener: () => void): this;
29
+ removeListener(event: "error", listener: (err: Error) => void): this;
30
+ [Symbol.asyncIterator](): AsyncIterableIterator<EventSourceMessage>;
31
+ }
32
+ export {};
@@ -10,6 +10,9 @@ class SSEResponseModel {
10
10
  this.eventSource = new SSEEventEmitter();
11
11
  this.init();
12
12
  }
13
+ /**
14
+ * @inner
15
+ */
13
16
  init() {
14
17
  const { stream, eventSource } = this;
15
18
  stream.on("data", (chunk) => {
@@ -29,6 +32,9 @@ class SSEResponseModel {
29
32
  eventSource.emit("error", err);
30
33
  });
31
34
  }
35
+ /**
36
+ * @inner
37
+ */
32
38
  parseSSEMessage(chunk) {
33
39
  const message = {
34
40
  data: "",
@@ -39,9 +45,10 @@ class SSEResponseModel {
39
45
  const lines = chunk.split("\n");
40
46
  for (let i = 0; i < lines.length; i++) {
41
47
  const line = lines[i];
48
+ // line is of format "<field>:<value>" or "<field>: <value>"
42
49
  const colonIndex = line.indexOf(":");
43
50
  if (colonIndex <= 0)
44
- continue;
51
+ continue; // exclude comments and lines with no values
45
52
  const field = line.slice(0, colonIndex);
46
53
  const value = line.slice(colonIndex + (line[colonIndex + 1] === " " ? 2 : 1));
47
54
  switch (field) {
@@ -57,6 +64,7 @@ class SSEResponseModel {
57
64
  case "retry":
58
65
  const retry = parseInt(value, 10);
59
66
  if (!isNaN(retry)) {
67
+ // per spec, ignore non-integers
60
68
  message.retry = retry;
61
69
  }
62
70
  break;
@@ -0,0 +1 @@
1
+ export * from "./services";