1 |
|
2 | const debug = require('debug')('ali-oss');
|
3 | const sendToWormhole = require('stream-wormhole');
|
4 | const xml = require('xml2js');
|
5 | const AgentKeepalive = require('agentkeepalive');
|
6 | const HttpsAgentKeepalive = require('agentkeepalive').HttpsAgent;
|
7 | const merge = require('merge-descriptors');
|
8 | const platform = require('platform');
|
9 | const utility = require('utility');
|
10 | const urllib = require('urllib');
|
11 | const pkg = require('../package.json');
|
12 | const bowser = require('bowser');
|
13 | const signUtils = require('./common/signUtils');
|
14 | const _initOptions = require('./common/client/initOptions');
|
15 | const { createRequest } = require('./common/utils/createRequest');
|
16 | const { encoder } = require('./common/utils/encoder');
|
17 | const { getReqUrl } = require('./common/client/getReqUrl');
|
18 | const { setSTSToken } = require('./common/utils/setSTSToken');
|
19 | const { retry } = require('./common/utils/retry');
|
20 |
|
21 | const globalHttpAgent = new AgentKeepalive();
|
22 | const globalHttpsAgent = new HttpsAgentKeepalive();
|
23 |
|
24 | function Client(options, ctx) {
|
25 | if (!(this instanceof Client)) {
|
26 | return new Client(options, ctx);
|
27 | }
|
28 |
|
29 | if (options && options.inited) {
|
30 | this.options = options;
|
31 | } else {
|
32 | this.options = Client.initOptions(options);
|
33 | }
|
34 |
|
35 |
|
36 | if (this.options.urllib) {
|
37 | this.urllib = this.options.urllib;
|
38 | } else {
|
39 | this.urllib = urllib;
|
40 | this.agent = this.options.agent || globalHttpAgent;
|
41 | this.httpsAgent = this.options.httpsAgent || globalHttpsAgent;
|
42 | }
|
43 | this.ctx = ctx;
|
44 | this.userAgent = this._getUserAgent();
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | module.exports = Client;
|
52 |
|
53 | Client.initOptions = function initOptions(options) {
|
54 | return _initOptions(options);
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const proto = Client.prototype;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | merge(proto, require('./common/object'));
|
67 | merge(proto, require('./object'));
|
68 | merge(proto, require('./common/image'));
|
69 |
|
70 |
|
71 |
|
72 | merge(proto, require('./common/bucket'));
|
73 | merge(proto, require('./bucket'));
|
74 |
|
75 | merge(proto, require('./managed-upload'));
|
76 |
|
77 |
|
78 |
|
79 | merge(proto, require('./rtmp'));
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | merge(proto, require('./common/multipart-copy'));
|
85 |
|
86 |
|
87 |
|
88 | merge(proto, require('./common/parallel'));
|
89 |
|
90 |
|
91 |
|
92 | merge(proto, require('./common/multipart'));
|
93 |
|
94 |
|
95 |
|
96 | Client.ImageClient = require('./image')(Client);
|
97 |
|
98 |
|
99 |
|
100 | Client.ClusterClient = require('./cluster')(Client);
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | Client.STS = require('./sts');
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | proto.signature = function signature(stringToSign) {
|
113 | debug('authorization stringToSign: %s', stringToSign);
|
114 |
|
115 | return signUtils.computeSignature(this.options.accessKeySecret, stringToSign, this.options.headerEncoding);
|
116 | };
|
117 |
|
118 | proto._getReqUrl = getReqUrl;
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | proto.authorization = function authorization(method, resource, subres, headers) {
|
142 | const stringToSign = signUtils.buildCanonicalString(method.toUpperCase(), resource, {
|
143 | headers,
|
144 | parameters: subres
|
145 | });
|
146 |
|
147 | return signUtils.authorization(this.options.accessKeyId, this.options.accessKeySecret, stringToSign, this.options.headerEncoding);
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | proto.request = async function (params) {
|
170 | const isAvailableStream = params.stream ? params.stream.readable : true;
|
171 | if (this.options.retryMax && isAvailableStream) {
|
172 | this.request = retry(request.bind(this), this.options.retryMax, {
|
173 | errorHandler: (err) => {
|
174 | const _errHandle = (_err) => {
|
175 | const statusErr = [-1, -2].includes(_err.status);
|
176 | const requestErrorRetryHandle = this.options.requestErrorRetryHandle || (() => true);
|
177 | return statusErr && requestErrorRetryHandle(_err);
|
178 | };
|
179 | if (_errHandle(err)) return true;
|
180 | return false;
|
181 | }
|
182 | });
|
183 | } else {
|
184 | this.request = request.bind(this);
|
185 | }
|
186 |
|
187 | return await this.request(params);
|
188 | };
|
189 |
|
190 | async function request(params) {
|
191 | const reqParams = createRequest.call(this, params);
|
192 | let result;
|
193 | let reqErr;
|
194 | try {
|
195 | result = await this.urllib.request(reqParams.url, reqParams.params);
|
196 | debug('response %s %s, got %s, headers: %j', params.method, reqParams.url, result.status, result.headers);
|
197 | } catch (err) {
|
198 | reqErr = err;
|
199 | }
|
200 | let err;
|
201 | if (result && params.successStatuses && params.successStatuses.indexOf(result.status) === -1) {
|
202 | err = await this.requestError(result);
|
203 | err.params = params;
|
204 | } else if (reqErr) {
|
205 | err = await this.requestError(reqErr);
|
206 | }
|
207 |
|
208 | if (err) {
|
209 | if (params.customResponse && result && result.res) {
|
210 |
|
211 | await sendToWormhole(result.res);
|
212 | }
|
213 |
|
214 | if (err.status === 403 && err.code === 'InvalidAccessKeyId' &&
|
215 | this.options.accessKeyId.startsWith('STS.') &&
|
216 | typeof this.options.refreshSTSToken === 'function') {
|
217 |
|
218 | if (!this._setOptions || Date.now() - this._setOptions > 10000) {
|
219 | this._setOptions = Date.now();
|
220 | await setSTSToken.call(this);
|
221 | return this.request(params);
|
222 | }
|
223 | }
|
224 |
|
225 | if (err.name === 'ResponseTimeoutError') {
|
226 | err.message = `${err.message.split(',')[0]}, please increase the timeout or use multipartDownload.`;
|
227 | }
|
228 | throw err;
|
229 | }
|
230 |
|
231 | if (params.xmlResponse) {
|
232 | result.data = await this.parseXML(result.data);
|
233 | }
|
234 | return result;
|
235 | };
|
236 |
|
237 | proto._getResource = function _getResource(params) {
|
238 | let resource = '/';
|
239 | if (params.bucket) resource += `${params.bucket}/`;
|
240 | if (params.object) resource += encoder(params.object, this.options.headerEncoding);
|
241 |
|
242 | return resource;
|
243 | };
|
244 |
|
245 | proto._escape = function _escape(name) {
|
246 | return utility.encodeURIComponent(name).replace(/%2F/g, '/');
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | proto._getUserAgent = function _getUserAgent() {
|
258 | const agent = (process && process.browser) ? 'js' : 'nodejs';
|
259 | const sdk = `aliyun-sdk-${agent}/${pkg.version}`;
|
260 | let plat = platform.description;
|
261 | if (!plat && process) {
|
262 | plat = `Node.js ${process.version.slice(1)} on ${process.platform} ${process.arch}`;
|
263 | }
|
264 |
|
265 | return this._checkUserAgent(`${sdk} ${plat}`);
|
266 | };
|
267 |
|
268 | proto._checkUserAgent = function _checkUserAgent(ua) {
|
269 | const userAgent = ua.replace(/\u03b1/, 'alpha').replace(/\u03b2/, 'beta');
|
270 | return userAgent;
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | proto.checkBrowserAndVersion = function checkBrowserAndVersion(name, version) {
|
282 | return ((bowser.name === name) && (bowser.version.split('.')[0] === version));
|
283 | };
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | proto.parseXML = function parseXMLThunk(str) {
|
293 | return new Promise((resolve, reject) => {
|
294 | if (Buffer.isBuffer(str)) {
|
295 | str = str.toString();
|
296 | }
|
297 | xml.parseString(str, {
|
298 | explicitRoot: false,
|
299 | explicitArray: false
|
300 | }, (err, result) => {
|
301 | if (err) {
|
302 | reject(err);
|
303 | } else {
|
304 | resolve(result);
|
305 | }
|
306 | });
|
307 | });
|
308 | };
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | proto.requestError = async function requestError(result) {
|
318 | let err = null;
|
319 | if (result.name === 'ResponseTimeoutError') {
|
320 | err = new Error(result.message);
|
321 | err.name = result.name;
|
322 | } else if (!result.data || !result.data.length) {
|
323 | if (result.status === -1 || result.status === -2) {
|
324 | err = new Error(result.message);
|
325 | err.name = result.name;
|
326 | err.status = result.status;
|
327 | err.code = result.name;
|
328 | } else {
|
329 |
|
330 | if (result.status === 404) {
|
331 | err = new Error('Object not exists');
|
332 | err.name = 'NoSuchKeyError';
|
333 | err.status = 404;
|
334 | err.code = 'NoSuchKey';
|
335 | } else if (result.status === 412) {
|
336 | err = new Error('Pre condition failed');
|
337 | err.name = 'PreconditionFailedError';
|
338 | err.status = 412;
|
339 | err.code = 'PreconditionFailed';
|
340 | } else {
|
341 | err = new Error(`Unknow error, status: ${result.status}`);
|
342 | err.name = 'UnknowError';
|
343 | err.status = result.status;
|
344 | }
|
345 | err.requestId = result.headers['x-oss-request-id'];
|
346 | err.host = '';
|
347 | }
|
348 | } else {
|
349 | const message = String(result.data);
|
350 | debug('request response error data: %s', message);
|
351 |
|
352 | let info;
|
353 | try {
|
354 | info = await this.parseXML(message) || {};
|
355 | } catch (error) {
|
356 | debug(message);
|
357 | error.message += `\nraw xml: ${message}`;
|
358 | error.status = result.status;
|
359 | error.requestId = result.headers['x-oss-request-id'];
|
360 | return error;
|
361 | }
|
362 |
|
363 | let msg = info.Message || (`unknow request error, status: ${result.status}`);
|
364 | if (info.Condition) {
|
365 | msg += ` (condition: ${info.Condition})`;
|
366 | }
|
367 | err = new Error(msg);
|
368 | err.name = info.Code ? `${info.Code}Error` : 'UnknowError';
|
369 | err.status = result.status;
|
370 | err.code = info.Code;
|
371 | err.requestId = info.RequestId;
|
372 | err.hostId = info.HostId;
|
373 | }
|
374 |
|
375 | debug('generate error %j', err);
|
376 | return err;
|
377 | };
|
378 |
|
379 | proto.setSLDEnabled = function setSLDEnabled(enable) {
|
380 | this.options.sldEnable = !!enable;
|
381 | return this;
|
382 | };
|