Skip to content

Commit

Permalink
util/ts:fix getDate,getSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
imbooo committed Feb 19, 2021
1 parent d81cc63 commit 74f2f2b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ export default class Client {
request_.pathname = pathname;
request_.headers = {
'user-agent': this.getUserAgent(),
'Content-Type': 'application/json',
Date: OpenSearchUtil.getDate(),
host: Util.defaultString(this._endpoint, `opensearch-cn-hangzhou.aliyuncs.com`),
'X-Opensearch-Nonce': Util.getNonce(),
Expand Down
3 changes: 1 addition & 2 deletions util/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"typescript": "^3.7.5"
},
"dependencies": {
"@alicloud/tea-typescript": "latest",
"moment": "^2.27.0"
"@alicloud/tea-typescript": "latest"
},
"files": [
"dist",
Expand Down
29 changes: 14 additions & 15 deletions util/ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* This module used for OpenSearch SDK
*/
import * as $tea from '@alicloud/tea-typescript';
import moment from 'moment';
import crypto from 'crypto';

function getSignedStr(request: $tea.Request, resource: string, accessKeySecret: string): string {
Expand Down Expand Up @@ -36,20 +35,20 @@ function getSignedStr(request: $tea.Request, resource: string, accessKeySecret:

function getSignature(request: $tea.Request, accessKeySecret: string): string {
let resource = request.pathname;
const keys = Object.keys(request.query);
if (resource.indexOf('?') === -1 && keys.length > 0) {
resource += '?';
const params = Object
.entries<string>(request.query)
.filter(([, value]) => typeof value !== "undefined" && value !== null)
.map(([key, value]) => {
let valueStr = encodeURIComponent(value);
valueStr = valueStr.replace(/'/g, "%27");
return `${key}=${valueStr}`;
});

if (params.length) {
resource += `?${params.join("&")}`;
}
keys.forEach(key => {
let value = request.query[key];
if (typeof value === 'undefined' || value === null) {
return;
}
let valueStr = encodeURIComponent(value);
valueStr = valueStr.replace(/'/g, '%27');
resource += `${key}=${valueStr}&`;
})
return getSignedStr(request, resource.slice(0, -1), accessKeySecret)

return getSignedStr(request, resource, accessKeySecret)
}


Expand Down Expand Up @@ -92,7 +91,7 @@ export default class Client {
* @return date string
*/
static getDate(): string {
return moment().format('YYYY-MM-DDTHH:mm:ss\\Z');
return new Date().toISOString().replace(/\.\d+/,'');
}

/**
Expand Down

0 comments on commit 74f2f2b

Please sign in to comment.