UNPKG

7.72 kBJavaScriptView Raw
1'use strict';
2var __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};
10const osLocale = require('os-locale');
11const MNSClient = require('@alicloud/mns');
12const hashedMachineId = require('node-machine-id').machineId;
13const pkg = require('../package.json');
14const CloudAPI = require('@alicloud/cloudapi');
15const TableStore = require('tablestore');
16const Log = require('@alicloud/log');
17const FC = require('@alicloud/fc2');
18const FnFClient = require('@alicloud/fnf-2019-03-15');
19const Pop = require('@alicloud/pop-core');
20const getProfile = require('./profile').getProfile;
21const OSS = require('ali-oss');
22const debug = require('debug');
23const { throwProcessedFCPermissionError, throwProcessedPopPermissionError, throwProcessedSLSPermissionError } = require('./error-message');
24const getRosClient = () => __awaiter(this, void 0, void 0, function* () {
25 return yield getPopClient('http://ros.aliyuncs.com', '2019-09-10');
26});
27const 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});
53const 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 fc = new FC(profile.accountId, {
61 accessKeyID: profile.accessKeyId,
62 accessKeySecret: profile.accessKeySecret,
63 endpoint: profile.fcEndpoint,
64 region: profile.defaultRegion,
65 timeout: opts.timeout || profile.timeout * 1000,
66 secure: profile.protocol !== 'http',
67 headers: {
68 'user-agent': `${pkg.name}/v${pkg.version} ( Node.js ${process.version}; OS ${process.platform} ${process.arch}; language ${locale}; mid ${mid})`
69 }
70 });
71 const realRequest = fc.request.bind(fc);
72 fc.request = (method, path, query, body, headers, opts = {}) => __awaiter(this, void 0, void 0, function* () {
73 try {
74 return yield realRequest(method, path, query, body, headers || {}, opts || {});
75 }
76 catch (ex) {
77 yield throwProcessedFCPermissionError(ex, ...path.split('/').filter(p => !!p));
78 throw ex;
79 }
80 });
81 return fc;
82});
83const getFnFClient = () => __awaiter(this, void 0, void 0, function* () {
84 const profile = yield getProfile();
85 return new FnFClient({
86 endpoint: `https://${profile.accountId}.${profile.defaultRegion}.fnf.aliyuncs.com`,
87 accessKeyId: profile.accessKeyId,
88 accessKeySecret: profile.accessKeySecret
89 });
90});
91const getPopClient = (endpoint, apiVersion) => __awaiter(this, void 0, void 0, function* () {
92 const profile = yield getProfile();
93 const pop = new Pop({
94 endpoint: endpoint,
95 apiVersion: apiVersion,
96 accessKeyId: profile.accessKeyId,
97 accessKeySecret: profile.accessKeySecret,
98 opts: {
99 timeout: profile.timeout * 1000
100 }
101 });
102 const realRequest = pop.request.bind(pop);
103 pop.request = (action, params, options) => __awaiter(this, void 0, void 0, function* () {
104 try {
105 return yield realRequest(action, params, options);
106 }
107 catch (ex) {
108 yield throwProcessedPopPermissionError(ex, action);
109 throw ex;
110 }
111 });
112 return pop;
113});
114const getOtsPopClient = () => __awaiter(this, void 0, void 0, function* () {
115 const profile = yield getProfile();
116 return yield getPopClient(`http://ots.${profile.defaultRegion}.aliyuncs.com`, '2016-06-20');
117});
118const getVpcPopClient = () => __awaiter(this, void 0, void 0, function* () {
119 return yield getPopClient('https://vpc.aliyuncs.com', '2016-04-28');
120});
121const getEcsPopClient = () => __awaiter(this, void 0, void 0, function* () {
122 return yield getPopClient('https://ecs.aliyuncs.com', '2014-05-26');
123});
124const getNasPopClient = () => __awaiter(this, void 0, void 0, function* () {
125 const profile = yield getProfile();
126 return yield getPopClient(`http://nas.${profile.defaultRegion}.aliyuncs.com`, '2017-06-26');
127});
128const getOtsClient = (instanceName) => __awaiter(this, void 0, void 0, function* () {
129 const profile = yield getProfile();
130 var endpoint = `http://${instanceName}.${profile.defaultRegion}.ots.aliyuncs.com`;
131 return new TableStore.Client({
132 accessKeyId: profile.accessKeyId,
133 secretAccessKey: profile.accessKeySecret,
134 endpoint: endpoint,
135 instancename: instanceName
136 });
137});
138const getMnsClient = (topicName, region) => __awaiter(this, void 0, void 0, function* () {
139 const profile = yield getProfile();
140 return new MNSClient(profile.accountId, {
141 region: region,
142 accessKeyId: profile.accessKeyId,
143 accessKeySecret: profile.accessKeySecret,
144 // optional & default
145 secure: false,
146 internal: false,
147 vpc: false // use vpc endpoint
148 });
149});
150const getCloudApiClient = () => __awaiter(this, void 0, void 0, function* () {
151 const profile = yield getProfile();
152 return new CloudAPI({
153 accessKeyId: profile.accessKeyId,
154 accessKeySecret: profile.accessKeySecret,
155 endpoint: `http://apigateway.${profile.defaultRegion}.aliyuncs.com`,
156 opts: {
157 timeout: profile.timeout * 1000
158 }
159 });
160});
161const getSlsClient = () => __awaiter(this, void 0, void 0, function* () {
162 const profile = yield getProfile();
163 const log = new Log({
164 region: profile.defaultRegion,
165 accessKeyId: profile.accessKeyId,
166 accessKeySecret: profile.accessKeySecret
167 });
168 const realRequest = log._request.bind(log);
169 log._request = (verb, projectName, path, queries, body, headers, options) => __awaiter(this, void 0, void 0, function* () {
170 try {
171 return yield realRequest(verb, projectName, path, queries, body, headers, options);
172 }
173 catch (ex) {
174 yield throwProcessedSLSPermissionError(ex);
175 throw ex;
176 }
177 });
178 return log;
179});
180module.exports = {
181 getFcClient,
182 getOtsClient,
183 getOtsPopClient,
184 getMnsClient,
185 getCloudApiClient,
186 getSlsClient,
187 getPopClient,
188 getVpcPopClient,
189 getEcsPopClient,
190 getNasPopClient,
191 getOssClient,
192 getRosClient,
193 getFnFClient
194};