From 13a2691e49ee04667f41070d63478301fbc983e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BA=E5=88=9A?= <735073554@qq.com> Date: Mon, 4 Sep 2023 13:32:27 +0800 Subject: [PATCH] chore: develop to master by xutao (#1244) * chore: adjust download request hints (#1061) * chore: adjust download hints * chore: adjust download hints * chore: some optimized (#1062) - add head method warning - adjust PR githubCi * chore(develop): remove cleanbucket workflow (#1094) * feat(develop): ios dingding add default content-type (#1070) * feat(develop): ios dingding add default content-type * feat(develop): ios dingding add default content-type * feat(develop): ios dingding add default content-type * feat(develop): ios dingding add default content-type * feat(develop): ios dingding add default content-type * feat(develop): ios dingding add default content-type * chore: remove remove redundant judgments * resolve master and develop conflict (#1122) Co-authored-by: xt01102058 * resolve master and develop conflict * chore: marge develop to master * test: fix test ConnectionTimeoutError bug * chore: add put() timeout unit * chore: optimize the translation of readme.md ResponseTimeoutError --------- Co-authored-by: moca_tao7 Co-authored-by: xt01102058 Co-authored-by: PeterRao Co-authored-by: csg01123119 --- README.md | 26 ++++++++++++++++++++++++-- lib/client.js | 9 ++++++++- lib/common/object/head.js | 4 ++++ lib/common/utils/checkEnv.d.ts | 1 + lib/common/utils/checkEnv.js | 9 +++++++++ lib/common/utils/checkEnv.ts | 5 +++++ lib/common/utils/createRequest.js | 4 ++++ lib/common/utils/createRequest.ts | 3 +++ lib/common/utils/isDingTalk.d.ts | 1 + lib/common/utils/isDingTalk.js | 10 ++++++++++ lib/common/utils/isDingTalk.ts | 6 ++++++ test/browser/browser.test.js | 3 --- 12 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 lib/common/utils/checkEnv.d.ts create mode 100644 lib/common/utils/checkEnv.js create mode 100644 lib/common/utils/checkEnv.ts create mode 100644 lib/common/utils/isDingTalk.d.ts create mode 100644 lib/common/utils/isDingTalk.js create mode 100644 lib/common/utils/isDingTalk.ts diff --git a/README.md b/README.md index 203860350..c6e09a521 100644 --- a/README.md +++ b/README.md @@ -1666,7 +1666,7 @@ parameters: - name {String} object name store on OSS - file {String|Buffer|ReadStream|File(only support Browser)|Blob(only support Browser)} object local path, content buffer or ReadStream content instance use in Node, Blob and html5 File - [options] {Object} optional parameters - - [timeout] {Number} the operation timeout + - [timeout] {Number} the operation timeout (ms) - [mime] {String} custom mime, will send with `Content-Type` entity header - [meta] {Object} user meta, will send with `x-oss-meta-` prefix string e.g.: `{ uid: 123, pid: 110 }` @@ -4557,7 +4557,29 @@ Each error return by OSS server will contains these properties: you can send this request id to OSS engineer to find out what's happend. - hostId {String} OSS cluster name for this request -The following table lists the OSS error codes: +### ResponseTimeoutError + +The default timeout is 60 seconds. Please set the timeout as needed. The timeout unit is milliseconds. + +```javascript +client.get('example.txt', { timeout: 60000 * 2 }); + +client.get('example.txt', { headers: { Range: `bytes=0-${1024 * 1024 * 100}` } }); // Download the first 100MB +``` + +### ConnectionTimeoutError + +The network link timed out. Please check the network status. If there is no problem with the network, please reduce the partSize or increase the timeout. + +```javascript +const client = new OSS({ ak, sk, retryMax: 10 }); + +client.multipartUpload('example.txt', { timeout: 60000 * 2 }); + +client.multipartUpload('example.txt', { partSize: 1024 * 512 }); // partSize 512KB +``` + +### The following table lists the OSS error codes: [More code info](https://help.aliyun.com/knowledge_detail/32005.html) diff --git a/lib/client.js b/lib/client.js index 520323714..421b500fe 100644 --- a/lib/client.js +++ b/lib/client.js @@ -223,7 +223,14 @@ async function request(params) { } if (err.name === 'ResponseTimeoutError') { - err.message = `${err.message.split(',')[0]}, please increase the timeout or use multipartDownload.`; + err.message = `${ + err.message.split(',')[0] + }, please increase the timeout, see more details at https://github.com/ali-sdk/ali-oss#responsetimeouterror`; + } + if (err.name === 'ConnectionTimeoutError') { + err.message = `${ + err.message.split(',')[0] + }, please increase the timeout or reduce the partSize, see more details at https://github.com/ali-sdk/ali-oss#connectiontimeouterror`; } throw err; } diff --git a/lib/common/object/head.js b/lib/common/object/head.js index 11964294a..8cbcd30d3 100644 --- a/lib/common/object/head.js +++ b/lib/common/object/head.js @@ -1,3 +1,4 @@ +const { checkEnv } = require('../utils/checkEnv'); const proto = exports; /** * head @@ -7,6 +8,9 @@ const proto = exports; */ proto.head = async function head(name, options = {}) { + checkEnv( + 'Because HeadObject has gzip enabled, head cannot get the file size correctly. If you need to get the file size, please use getObjectMeta' + ); options.subres = Object.assign({}, options.subres); if (options.versionId) { options.subres.versionId = options.versionId; diff --git a/lib/common/utils/checkEnv.d.ts b/lib/common/utils/checkEnv.d.ts new file mode 100644 index 000000000..e536606ae --- /dev/null +++ b/lib/common/utils/checkEnv.d.ts @@ -0,0 +1 @@ +export declare function checkEnv(msg: string): void; diff --git a/lib/common/utils/checkEnv.js b/lib/common/utils/checkEnv.js new file mode 100644 index 000000000..1a0f7b2c8 --- /dev/null +++ b/lib/common/utils/checkEnv.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.checkEnv = void 0; +function checkEnv(msg) { + if (process.browser) { + console.warn(msg); + } +} +exports.checkEnv = checkEnv; diff --git a/lib/common/utils/checkEnv.ts b/lib/common/utils/checkEnv.ts new file mode 100644 index 000000000..b37365808 --- /dev/null +++ b/lib/common/utils/checkEnv.ts @@ -0,0 +1,5 @@ +export function checkEnv(msg: string) { + if (process.browser) { + console.warn(msg); + } +} diff --git a/lib/common/utils/createRequest.js b/lib/common/utils/createRequest.js index 1842f3934..3dbec395f 100644 --- a/lib/common/utils/createRequest.js +++ b/lib/common/utils/createRequest.js @@ -11,6 +11,7 @@ const { encoder } = require('./encoder'); const { isIP } = require('./isIP'); const { setRegion } = require('./setRegion'); const { getReqUrl } = require('../client/getReqUrl'); +const { isDingTalk } = require('./isDingTalk'); function getHeader(headers, name) { return headers[name] || headers[name.toLowerCase()]; } @@ -43,6 +44,9 @@ function createRequest(params) { if (params.mime && params.mime.indexOf('/') > 0) { headers['Content-Type'] = params.mime; } + else if (isDingTalk()) { + headers['Content-Type'] = 'application/octet-stream'; + } else { headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || '')); } diff --git a/lib/common/utils/createRequest.ts b/lib/common/utils/createRequest.ts index ad03d7938..dfbbf0618 100644 --- a/lib/common/utils/createRequest.ts +++ b/lib/common/utils/createRequest.ts @@ -8,6 +8,7 @@ const { encoder } = require('./encoder'); const { isIP } = require('./isIP'); const { setRegion } = require('./setRegion'); const { getReqUrl } = require('../client/getReqUrl'); +const { isDingTalk } = require('./isDingTalk'); interface Headers { [propName: string]: any; @@ -58,6 +59,8 @@ export function createRequest(this: any, params) { if (!getHeader(headers, 'Content-Type')) { if (params.mime && params.mime.indexOf('/') > 0) { headers['Content-Type'] = params.mime; + } else if (isDingTalk()) { + headers['Content-Type'] = 'application/octet-stream'; } else { headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || '')); } diff --git a/lib/common/utils/isDingTalk.d.ts b/lib/common/utils/isDingTalk.d.ts new file mode 100644 index 000000000..42cb892b3 --- /dev/null +++ b/lib/common/utils/isDingTalk.d.ts @@ -0,0 +1 @@ +export declare function isDingTalk(): boolean; diff --git a/lib/common/utils/isDingTalk.js b/lib/common/utils/isDingTalk.js new file mode 100644 index 000000000..04d89d9e4 --- /dev/null +++ b/lib/common/utils/isDingTalk.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.isDingTalk = void 0; +function isDingTalk() { + if (process.browser && window.navigator.userAgent.toLowerCase().includes('aliapp(dingtalk')) { + return true; + } + return false; +} +exports.isDingTalk = isDingTalk; diff --git a/lib/common/utils/isDingTalk.ts b/lib/common/utils/isDingTalk.ts new file mode 100644 index 000000000..aa7ff33b4 --- /dev/null +++ b/lib/common/utils/isDingTalk.ts @@ -0,0 +1,6 @@ +export function isDingTalk() { + if (process.browser && window.navigator.userAgent.toLowerCase().includes('aliapp(dingtalk')) { + return true; + } + return false; +} diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 7f3fb930b..75c4ea897 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -726,9 +726,6 @@ describe('browser', () => { timeout: 300 }; try { - setTimeout(() => { - options.timeout = 60000; - }, 200); await store.put(name, body, options); assert(false); } catch (error) {