1 | 'use strict';
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | const osLocale = require('os-locale');
|
11 | const MNSClient = require('@alicloud/mns');
|
12 | const hashedMachineId = require('node-machine-id').machineId;
|
13 | const pkg = require('../package.json');
|
14 | const CloudAPI = require('@alicloud/cloudapi');
|
15 | const TableStore = require('tablestore');
|
16 | const Log = require('@alicloud/log');
|
17 | const FC = require('@alicloud/fc2');
|
18 | const FnFClient = require('@alicloud/fnf-2019-03-15');
|
19 | const Pop = require('@alicloud/pop-core');
|
20 | const getProfile = require('./profile').getProfile;
|
21 | const OSS = require('ali-oss');
|
22 | const debug = require('debug');
|
23 | const { throwProcessedFCPermissionError, throwProcessedPopPermissionError, throwProcessedSLSPermissionError } = require('./error-message');
|
24 | const getRosClient = () => __awaiter(this, void 0, void 0, function* () {
|
25 | return yield getPopClient('http://ros.aliyuncs.com', '2019-09-10');
|
26 | });
|
27 | const getOssClient = (bucket) => __awaiter(this, void 0, void 0, function* () {
|
28 | const profile = yield getProfile();
|
29 | if (!bucket) {
|
30 | return OSS({
|
31 | region: 'oss-' + profile.defaultRegion,
|
32 | accessKeyId: profile.accessKeyId,
|
33 | accessKeySecret: profile.accessKeySecret,
|
34 | timeout: profile.timeout * 1000
|
35 | });
|
36 | }
|
37 | const location = yield OSS({
|
38 | accessKeyId: profile.accessKeyId,
|
39 | accessKeySecret: profile.accessKeySecret,
|
40 | bucket,
|
41 | region: 'oss-' + profile.defaultRegion
|
42 | }).getBucketLocation(bucket);
|
43 | debug('use bucket region %s', location.location);
|
44 | const client = OSS({
|
45 | accessKeyId: profile.accessKeyId,
|
46 | accessKeySecret: profile.accessKeySecret,
|
47 | bucket,
|
48 | region: location.location,
|
49 | timeout: profile.timeout * 1000
|
50 | });
|
51 | return client;
|
52 | });
|
53 | const getFcClient = (opts = {}) => __awaiter(this, void 0, void 0, function* () {
|
54 | const profile = yield getProfile();
|
55 | const locale = yield osLocale();
|
56 | const mid = yield hashedMachineId();
|
57 | FC.prototype.getAccountSettings = function (options = {}, headers = {}) {
|
58 | return this.get('/account-settings', options, headers);
|
59 | };
|
60 | const accountId = profile.accountId ? profile.accountId : 'accountId';
|
61 | const accessKeyID = profile.accessKeyId ? profile.accessKeyId : 'accessKeyID';
|
62 | const accessKeySecret = profile.accessKeySecret ? profile.accessKeySecret : 'accessKeySecret';
|
63 | const region = profile.defaultRegion ? profile.defaultRegion : 'cn-hangzhou';
|
64 | const enable = profile.enableCustomEndpoint === true || profile.enableCustomEndpoint === 'true';
|
65 | const endpoint = profile.fcEndpoint ? profile.fcEndpoint : (enable ? profile.endpoint : undefined);
|
66 | const fc = new FC(accountId, {
|
67 | accessKeyID, accessKeySecret, region, endpoint,
|
68 | timeout: opts.timeout || profile.timeout * 1000,
|
69 | secure: profile.protocol !== 'http',
|
70 | headers: {
|
71 | 'user-agent': `${pkg.name}/v${pkg.version} ( Node.js ${process.version}; OS ${process.platform} ${process.arch}; language ${locale}; mid ${mid})`
|
72 | }
|
73 | });
|
74 | const realRequest = fc.request.bind(fc);
|
75 | fc.request = (method, path, query, body, headers, opts = {}) => __awaiter(this, void 0, void 0, function* () {
|
76 | try {
|
77 | return yield realRequest(method, path, query, body, headers || {}, opts || {});
|
78 | }
|
79 | catch (ex) {
|
80 | yield throwProcessedFCPermissionError(ex, ...path.split('/').filter(p => !!p));
|
81 | throw ex;
|
82 | }
|
83 | });
|
84 | return fc;
|
85 | });
|
86 | const getFnFClient = () => __awaiter(this, void 0, void 0, function* () {
|
87 | const profile = yield getProfile();
|
88 | return new FnFClient({
|
89 | endpoint: `https://${profile.accountId}.${profile.defaultRegion}.fnf.aliyuncs.com`,
|
90 | accessKeyId: profile.accessKeyId,
|
91 | accessKeySecret: profile.accessKeySecret
|
92 | });
|
93 | });
|
94 | const getPopClient = (endpoint, apiVersion) => __awaiter(this, void 0, void 0, function* () {
|
95 | const profile = yield getProfile();
|
96 | const pop = new Pop({
|
97 | endpoint: endpoint,
|
98 | apiVersion: apiVersion,
|
99 | accessKeyId: profile.accessKeyId,
|
100 | accessKeySecret: profile.accessKeySecret,
|
101 | opts: {
|
102 | timeout: profile.timeout * 1000
|
103 | }
|
104 | });
|
105 | const realRequest = pop.request.bind(pop);
|
106 | pop.request = (action, params, options) => __awaiter(this, void 0, void 0, function* () {
|
107 | try {
|
108 | return yield realRequest(action, params, options);
|
109 | }
|
110 | catch (ex) {
|
111 | yield throwProcessedPopPermissionError(ex, action);
|
112 | throw ex;
|
113 | }
|
114 | });
|
115 | return pop;
|
116 | });
|
117 | const getOtsPopClient = () => __awaiter(this, void 0, void 0, function* () {
|
118 | const profile = yield getProfile();
|
119 | return yield getPopClient(`http://ots.${profile.defaultRegion}.aliyuncs.com`, '2016-06-20');
|
120 | });
|
121 | const getVpcPopClient = () => __awaiter(this, void 0, void 0, function* () {
|
122 | return yield getPopClient('https://vpc.aliyuncs.com', '2016-04-28');
|
123 | });
|
124 | const getEcsPopClient = () => __awaiter(this, void 0, void 0, function* () {
|
125 | return yield getPopClient('https://ecs.aliyuncs.com', '2014-05-26');
|
126 | });
|
127 | const getNasPopClient = () => __awaiter(this, void 0, void 0, function* () {
|
128 | const profile = yield getProfile();
|
129 | return yield getPopClient(`http://nas.${profile.defaultRegion}.aliyuncs.com`, '2017-06-26');
|
130 | });
|
131 | const getOtsClient = (instanceName) => __awaiter(this, void 0, void 0, function* () {
|
132 | const profile = yield getProfile();
|
133 | var endpoint = `http://${instanceName}.${profile.defaultRegion}.ots.aliyuncs.com`;
|
134 | return new TableStore.Client({
|
135 | accessKeyId: profile.accessKeyId,
|
136 | secretAccessKey: profile.accessKeySecret,
|
137 | endpoint: endpoint,
|
138 | instancename: instanceName
|
139 | });
|
140 | });
|
141 | const getMnsClient = (topicName, region) => __awaiter(this, void 0, void 0, function* () {
|
142 | const profile = yield getProfile();
|
143 | return new MNSClient(profile.accountId, {
|
144 | region: region,
|
145 | accessKeyId: profile.accessKeyId,
|
146 | accessKeySecret: profile.accessKeySecret,
|
147 |
|
148 | secure: false,
|
149 | internal: false,
|
150 | vpc: false
|
151 | });
|
152 | });
|
153 | const getCloudApiClient = () => __awaiter(this, void 0, void 0, function* () {
|
154 | const profile = yield getProfile();
|
155 | return new CloudAPI({
|
156 | accessKeyId: profile.accessKeyId,
|
157 | accessKeySecret: profile.accessKeySecret,
|
158 | endpoint: `http://apigateway.${profile.defaultRegion}.aliyuncs.com`,
|
159 | opts: {
|
160 | timeout: profile.timeout * 1000
|
161 | }
|
162 | });
|
163 | });
|
164 | const getSlsClient = () => __awaiter(this, void 0, void 0, function* () {
|
165 | const profile = yield getProfile();
|
166 | const log = new Log({
|
167 | region: profile.defaultRegion,
|
168 | accessKeyId: profile.accessKeyId,
|
169 | accessKeySecret: profile.accessKeySecret
|
170 | });
|
171 | const realRequest = log._request.bind(log);
|
172 | log._request = (verb, projectName, path, queries, body, headers, options) => __awaiter(this, void 0, void 0, function* () {
|
173 | try {
|
174 | return yield realRequest(verb, projectName, path, queries, body, headers, options);
|
175 | }
|
176 | catch (ex) {
|
177 | yield throwProcessedSLSPermissionError(ex);
|
178 | throw ex;
|
179 | }
|
180 | });
|
181 | return log;
|
182 | });
|
183 | module.exports = {
|
184 | getFcClient,
|
185 | getOtsClient,
|
186 | getOtsPopClient,
|
187 | getMnsClient,
|
188 | getCloudApiClient,
|
189 | getSlsClient,
|
190 | getPopClient,
|
191 | getVpcPopClient,
|
192 | getEcsPopClient,
|
193 | getNasPopClient,
|
194 | getOssClient,
|
195 | getRosClient,
|
196 | getFnFClient
|
197 | };
|