From c52bd5b36d725365bf820ee3cea44b9ed0370ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BA=E5=88=9A?= <735073554@qq.com> Date: Tue, 13 Aug 2024 14:34:26 +0800 Subject: [PATCH] fix: add check to file of get(#1228) * fix: fix issue with get(name, null, options) call not meeting expectations * test: add test case * fix: fix issue with get(name, null, options) call not meeting expectations * fix: fix issue with get(name, null, options) call not meeting expectations * fix: fix issue with get(name, null, options) call not meeting expectations * fix: fix issue with get(name, null, options) call not meeting expectations --------- Co-authored-by: csg01123119 --- .eslintignore | 2 ++ .prettierignore | 2 ++ README.md | 10 +++++++++- lib/common/object/get.js | 5 +++-- lib/common/object/head.js | 1 + test/browser/browser.test.js | 35 +++++++++++++++++++++++++++++++++ test/node/object.test.js | 38 ++++++++++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 3 deletions(-) diff --git a/.eslintignore b/.eslintignore index 28baaae9c..677069b64 100644 --- a/.eslintignore +++ b/.eslintignore @@ -21,5 +21,7 @@ dist/ lib/common/utils/createRequest.js lib/common/utils/encodeString.js lib/common/utils/getStandardRegion.js +lib/common/utils/checkEnv.js +lib/common/utils/isDingTalk.js lib/common/bucket/putBucketInventory.d.ts \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 23dd2e9bb..297b08d50 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,5 +4,7 @@ dist/ lib/common/utils/createRequest.js lib/common/utils/encodeString.js lib/common/utils/getStandardRegion.js +lib/common/utils/checkEnv.js +lib/common/utils/isDingTalk.js lib/common/bucket/putBucketInventory.d.ts \ No newline at end of file diff --git a/README.md b/README.md index ce035339a..39420d69f 100644 --- a/README.md +++ b/README.md @@ -2078,8 +2078,9 @@ Get an object from the bucket. parameters: - name {String} object name store on OSS -- [file] {String|WriteStream} file path or WriteStream instance to store the content +- [file] {String|WriteStream|Object} file path or WriteStream instance to store the content If `file` is null or ignore this parameter, function will return info contains `content` property. + If `file` is Object, it will be treated as options. - [options] {Object} optional parameters - [versionId] {String} the version id of history object - [timeout] {Number} the operation timeout @@ -2157,6 +2158,13 @@ await store.get('ossdemo/not-exists-demo.txt', filepath, { }); ``` +- If `file` is Object, it will be treated as options. + +```js +const versionId = 'versionId string'; +await store.get('ossdemo/not-exists-demo.txt', { versionId }); +``` + ### .getStream(name[, options]) Get an object read stream. diff --git a/lib/common/object/get.js b/lib/common/object/get.js index 2ce5e1941..f119b6a42 100644 --- a/lib/common/object/get.js +++ b/lib/common/object/get.js @@ -1,11 +1,12 @@ const fs = require('fs'); const is = require('is-type-of'); +const { isObject } = require('../utils/isObject'); const proto = exports; /** * get * @param {String} name - object name - * @param {String | Stream} file + * @param {String | Stream | Object} file - file path or file stream or options * @param {Object} options * @param {{res}} */ @@ -18,7 +19,7 @@ proto.get = async function get(name, file, options = {}) { } else if (is.string(file)) { writeStream = fs.createWriteStream(file); needDestroy = true; - } else { + } else if (isObject(file)) { // get(name, options) options = file; } diff --git a/lib/common/object/head.js b/lib/common/object/head.js index 8cbcd30d3..1d51e2687 100644 --- a/lib/common/object/head.js +++ b/lib/common/object/head.js @@ -1,4 +1,5 @@ const { checkEnv } = require('../utils/checkEnv'); + const proto = exports; /** * head diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 6658198ec..43498415c 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -694,6 +694,41 @@ describe('browser', () => { }); assert(!requestUrls2[0].includes('response-cache-control=no-cache')); }); + + it('test file is not a stream or string', async () => { + let result = await store.get(name, null, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + result = await store.get(name, 2, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + result = await store.get(name, undefined, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + result = await store.get(name, true, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + }); + it('test file is options', async () => { + const result = await store.get(name, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + }); }); describe('put', () => { diff --git a/test/node/object.test.js b/test/node/object.test.js index 48a5883b8..992b7bd08 100644 --- a/test/node/object.test.js +++ b/test/node/object.test.js @@ -884,6 +884,44 @@ describe('test/object.test.js', () => { ); }); + it('test file is not a stream or string', async () => { + let result = await store.get(name, null, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + assert(Buffer.isBuffer(result.content), 'content should be Buffer'); + result = await store.get(name, undefined, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + result = await store.get(name, 1, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + result = await store.get(name, true, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + }); + + it('test file is options', async () => { + const result = await store.get(name, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + assert(Buffer.isBuffer(result.content), 'content should be Buffer'); + }); + describe('If-Modified-Since header', () => { it('should 200 when If-Modified-Since < object modified time', async () => { let lastYear = new Date(resHeaders.date);