diff --git a/.eslintignore b/.eslintignore index 1e63fed15..231c10192 100644 --- a/.eslintignore +++ b/.eslintignore @@ -16,4 +16,9 @@ task/ browser-build.js es cjs -dist/ \ No newline at end of file +dist/ + +lib/common/utils/createRequest.js +lib/common/utils/encodeString.js +lib/common/utils/getStandardRegion.js + diff --git a/.github/workflows/codeCov.yml b/.github/workflows/codeCov.yml index 62364220b..e6d22b9d6 100644 --- a/.github/workflows/codeCov.yml +++ b/.github/workflows/codeCov.yml @@ -25,7 +25,7 @@ jobs: ALI_SDK_STS_SECRET: ${{secrets.ALI_SDK_STS_SECRET}} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: test and report upload run: | npm install diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 49928c18c..2ad651bd1 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -28,7 +28,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/dev_node_test.yml b/.github/workflows/dev_node_test.yml index 701e8d71e..0d7f8081f 100644 --- a/.github/workflows/dev_node_test.yml +++ b/.github/workflows/dev_node_test.yml @@ -31,7 +31,7 @@ jobs: node-version: [14.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: browser build and test run: | curl cip.cc @@ -61,11 +61,12 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install - run: npm run test @@ -92,11 +93,12 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install - run: npm run test @@ -123,12 +125,13 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: ref: develop - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install - run: npm run test diff --git a/.github/workflows/master_node_test.yml b/.github/workflows/master_node_test.yml index 5ba8019a2..b9dfe0fae 100644 --- a/.github/workflows/master_node_test.yml +++ b/.github/workflows/master_node_test.yml @@ -31,7 +31,7 @@ jobs: node-version: [14.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: browser build and test run: | curl cip.cc @@ -61,11 +61,12 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install -D npm@6.14.12 - run: npm install - run: npm run test @@ -93,11 +94,12 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install -D npm@6.14.16 - run: npm install - run: npm run test @@ -125,11 +127,12 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install -D npm@6.14.18 - run: npm install - run: npm run test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 80695164f..c81c4f78c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,11 +17,12 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v4 with: node-version: 18 + architecture: x64 - name: Install Dependencies run: npm install - name: Snyk Protect diff --git a/.github/workflows/schedule.yml b/.github/workflows/schedule.yml index 8ae333faa..c55ed19ec 100644 --- a/.github/workflows/schedule.yml +++ b/.github/workflows/schedule.yml @@ -25,10 +25,11 @@ jobs: matrix: node-version: [14.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: clean all bucket - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: x64 - run: npm install - - run: node test/node/cleanAllBucket.js + - run: node test/node/fixtures/cleanAllBucket.js diff --git a/.github/workflows/snyk.yml b/.github/workflows/snyk.yml index eba299b4f..19700d90b 100644 --- a/.github/workflows/snyk.yml +++ b/.github/workflows/snyk.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - run: npm install - name: Run Snyk to check for vulnerabilities diff --git a/.gitignore b/.gitignore index ecab552ad..381eed2b1 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ package-lock.json es .eslintcache + +/test/node/fixtures/nodejs-processed-w200-latest.png +/test/node/fixtures/nodejs-processed-w200.png \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 04c01ba7b..df49b88d7 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,6 @@ node_modules/ -dist/ \ No newline at end of file +dist/ + +lib/common/utils/createRequest.js +lib/common/utils/encodeString.js +lib/common/utils/getStandardRegion.js \ No newline at end of file diff --git a/lib/common/multipart.js b/lib/common/multipart.js index b6cb1edb1..70ce16ef4 100644 --- a/lib/common/multipart.js +++ b/lib/common/multipart.js @@ -238,8 +238,8 @@ proto._uploadPart = async function _uploadPart(name, uploadId, partNo, data, opt opt.headers = opt.headers || {}; opt.headers['Content-Length'] = data.size; - // Uploading shards does not require x-oss server side encryption - opt.headers = omit(opt.headers, ['x-oss-server-side-encryption']); + // Uploading shards does not require x-oss headers. + opt.headers = omit(opt.headers, ['x-oss-server-side-encryption', 'x-oss-storage-class']); opt.subres = { partNumber: partNo, uploadId diff --git a/lib/common/utils/createRequest.js b/lib/common/utils/createRequest.js index 9c4428f1f..e1886c428 100644 --- a/lib/common/utils/createRequest.js +++ b/lib/common/utils/createRequest.js @@ -1,6 +1,5 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { value: true }); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); exports.createRequest = void 0; const crypto = require('crypto'); const debug = require('debug')('ali-oss'); @@ -16,130 +15,125 @@ 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()]; + return headers[name] || headers[name.toLowerCase()]; } function delHeader(headers, name) { - delete headers[name]; - delete headers[name.toLowerCase()]; + delete headers[name]; + delete headers[name.toLowerCase()]; } function createRequest(params) { - let date = new Date(); - if (this.options.amendTimeSkewed) { - date = +new Date() + this.options.amendTimeSkewed; - } - const headers = { - 'x-oss-date': dateFormat( - date, - this.options.authorizationV4 ? "UTC:yyyymmdd'T'HHMMss'Z'" : "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'" - ) - }; - if (this.options.authorizationV4) { - headers['x-oss-content-sha256'] = 'UNSIGNED-PAYLOAD'; - } - if (typeof window !== 'undefined') { - headers['x-oss-user-agent'] = this.userAgent; - } - if (this.userAgent.includes('nodejs')) { - headers['User-Agent'] = this.userAgent; - } - if (this.options.isRequestPay) { - Object.assign(headers, { 'x-oss-request-payer': 'requester' }); - } - if (this.options.stsToken) { - headers['x-oss-security-token'] = this.options.stsToken; - } - copy(params.headers).to(headers); - 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 || '')); - } - } - if (!getHeader(headers, 'Content-Type')) { - delHeader(headers, 'Content-Type'); - } - if (params.content) { - if (!params.disabledMD5) { - if (!params.headers || !params.headers['Content-MD5']) { - headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64'); - } else { - headers['Content-MD5'] = params.headers['Content-MD5']; - } - } - if (!headers['Content-Length']) { - headers['Content-Length'] = params.content.length; - } - } - const { hasOwnProperty } = Object.prototype; - for (const k in headers) { - if (headers[k] && hasOwnProperty.call(headers, k)) { - headers[k] = encoder(String(headers[k]), this.options.headerEncoding); - } - } - const queries = {}; - if (_isString(params.subres)) { - queries[params.subres] = null; - } else if (_isArray(params.subres)) { - params.subres.forEach(v => { - queries[v] = null; - }); - } else if (_isObject(params.subres)) { - Object.entries(params.subres).forEach(v => { - queries[v[0]] = v[1] === '' ? null : v[1]; - }); - } - if (_isObject(params.query)) { - Object.entries(params.query).forEach(v => { - queries[v[0]] = v[1]; - }); - } - headers.authorization = this.options.authorizationV4 - ? this.authorizationV4( - params.method, - { - headers, - queries - }, - params.bucket, - params.object, - params.additionalHeaders - ) - : this.authorization(params.method, this._getResource(params), params.subres, headers, this.options.headerEncoding); - // const url = this._getReqUrl(params); - if (isIP(this.options.endpoint.hostname)) { - const { region, internal, secure } = this.options; - const hostInfo = setRegion(region, internal, secure); - headers.host = `${params.bucket}.${hostInfo.host}`; - } - const url = getReqUrl.bind(this)(params); - debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream); - const timeout = params.timeout || this.options.timeout; - const reqParams = { - method: params.method, - content: params.content, - stream: params.stream, - headers, - timeout, - writeStream: params.writeStream, - customResponse: params.customResponse, - ctx: params.ctx || this.ctx - }; - if (this.agent) { - reqParams.agent = this.agent; - } - if (this.httpsAgent) { - reqParams.httpsAgent = this.httpsAgent; - } - reqParams.enableProxy = !!this.options.enableProxy; - reqParams.proxy = this.options.proxy ? this.options.proxy : null; - return { - url, - params: reqParams - }; + let date = new Date(); + if (this.options.amendTimeSkewed) { + date = +new Date() + this.options.amendTimeSkewed; + } + const headers = { + 'x-oss-date': dateFormat(date, this.options.authorizationV4 ? "UTC:yyyymmdd'T'HHMMss'Z'" : "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'") + }; + if (this.options.authorizationV4) { + headers['x-oss-content-sha256'] = 'UNSIGNED-PAYLOAD'; + } + if (typeof window !== 'undefined') { + headers['x-oss-user-agent'] = this.userAgent; + } + if (this.userAgent.includes('nodejs')) { + headers['User-Agent'] = this.userAgent; + } + if (this.options.isRequestPay) { + Object.assign(headers, { 'x-oss-request-payer': 'requester' }); + } + if (this.options.stsToken) { + headers['x-oss-security-token'] = this.options.stsToken; + } + copy(params.headers).to(headers); + 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 || '')); + } + } + if (!getHeader(headers, 'Content-Type')) { + delHeader(headers, 'Content-Type'); + } + if (params.content) { + if (!params.disabledMD5) { + if (!params.headers || !params.headers['Content-MD5']) { + headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64'); + } + else { + headers['Content-MD5'] = params.headers['Content-MD5']; + } + } + if (!headers['Content-Length']) { + headers['Content-Length'] = params.content.length; + } + } + const { hasOwnProperty } = Object.prototype; + for (const k in headers) { + if (headers[k] && hasOwnProperty.call(headers, k)) { + headers[k] = encoder(String(headers[k]), this.options.headerEncoding); + } + } + const queries = {}; + if (_isString(params.subres)) { + queries[params.subres] = null; + } + else if (_isArray(params.subres)) { + params.subres.forEach(v => { + queries[v] = null; + }); + } + else if (_isObject(params.subres)) { + Object.entries(params.subres).forEach(v => { + queries[v[0]] = v[1] === '' ? null : v[1]; + }); + } + if (_isObject(params.query)) { + Object.entries(params.query).forEach(v => { + queries[v[0]] = v[1]; + }); + } + headers.authorization = this.options.authorizationV4 + ? this.authorizationV4(params.method, { + headers, + queries + }, params.bucket, params.object, params.additionalHeaders) + : this.authorization(params.method, this._getResource(params), params.subres, headers, this.options.headerEncoding); + // const url = this._getReqUrl(params); + if (isIP(this.options.endpoint.hostname)) { + const { region, internal, secure } = this.options; + const hostInfo = setRegion(region, internal, secure); + headers.host = `${params.bucket}.${hostInfo.host}`; + } + const url = getReqUrl.bind(this)(params); + debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream); + const timeout = params.timeout || this.options.timeout; + const reqParams = { + method: params.method, + content: params.content, + stream: params.stream, + headers, + timeout, + writeStream: params.writeStream, + customResponse: params.customResponse, + ctx: params.ctx || this.ctx + }; + if (this.agent) { + reqParams.agent = this.agent; + } + if (this.httpsAgent) { + reqParams.httpsAgent = this.httpsAgent; + } + reqParams.enableProxy = !!this.options.enableProxy; + reqParams.proxy = this.options.proxy ? this.options.proxy : null; + return { + url, + params: reqParams + }; } exports.createRequest = createRequest; diff --git a/lib/common/utils/encodeString.js b/lib/common/utils/encodeString.js index 0d57a441a..81cdd9ce0 100644 --- a/lib/common/utils/encodeString.js +++ b/lib/common/utils/encodeString.js @@ -1,16 +1,12 @@ -'use strict'; - -const __importDefault = - (this && this.__importDefault) || - function (mod) { - return mod && mod.__esModule ? mod : { default: mod }; - }; -Object.defineProperty(exports, '__esModule', { value: true }); +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); exports.encodeString = void 0; -const toString_1 = __importDefault(require('lodash/toString')); - +const toString_1 = __importDefault(require("lodash/toString")); function encodeString(str) { - const tempStr = toString_1.default(str); - return encodeURIComponent(tempStr).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`); + const tempStr = toString_1.default(str); + return encodeURIComponent(tempStr).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`); } exports.encodeString = encodeString; diff --git a/lib/common/utils/getStandardRegion.js b/lib/common/utils/getStandardRegion.js index 518850730..14971fd65 100644 --- a/lib/common/utils/getStandardRegion.js +++ b/lib/common/utils/getStandardRegion.js @@ -1,8 +1,7 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { value: true }); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); exports.getStandardRegion = void 0; function getStandardRegion(str) { - return str.replace(/^oss-/g, ''); + return str.replace(/^oss-/g, ''); } exports.getStandardRegion = getStandardRegion; diff --git a/package.json b/package.json index 032c0e920..d55be2630 100644 --- a/package.json +++ b/package.json @@ -156,7 +156,7 @@ "sdk-base": "^2.0.1", "stream-http": "2.8.2", "stream-wormhole": "^1.0.4", - "urllib": "2.41.0", + "urllib": "2.42.0", "utility": "^1.18.0", "xml2js": "^0.6.2" }, diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 38880df1f..c11804adf 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -1099,23 +1099,6 @@ describe('browser', () => { ); }); - // it('should signature url with image processed and get object ok', function* () { - // var name = prefix + 'ali-sdk/oss/nodejs-test-signature-1024x768.png'; - // var originImagePath = path.join(__dirname, 'nodejs-1024x768.png'); - // var processedImagePath = path.join(__dirname, 'nodejs-processed-w200.png'); - // var object = yield this.store.put(name, originImagePath, { - // mime: 'image/png' - // }); - // - // var signUrl = this.store.signatureUrl(name, {expires: 3600, process: 'image/resize,w_200'}); - // var processedKeyword = "x-oss-process=image%2Fresize%2Cw_200"; - // assert.equal(signUrl.match(processedKeyword), processedKeyword); - // var urlRes = yield urllib.request(signUrl); - // assert.equal(urlRes.status, 200); - // // assert(urlRes.data.toString() == fs.readFileSync(processedImagePath, 'utf8'), - // // 'response content should be same as test/nodejs-processed-w200.png'); - // }); - // it('should signature url for PUT', async () => { const putString = 'Hello World'; const contentMd5 = crypto1.createHash('md5').update(Buffer.from(putString, 'utf8')).digest('base64'); @@ -1563,6 +1546,21 @@ describe('browser', () => { assert.equal(result.res.status, 200); }); + it('should set storage-class header', async () => { + const fileContent = Array(1024 * 1024) + .fill('a') + .join(''); + const file = new File([fileContent], 'multipart-fallback'); + const name = `${prefix}storage-class`; + let result = await store.multipartUpload(name, file, { + headers: { 'x-oss-storage-class': 'IA' } + }); + assert.equal(true, result.res && Object.keys(result.res).length !== 0); + assert.equal(result.res.status, 200); + result = await store.head(name); + assert.equal(result.res.headers['x-oss-storage-class'], 'IA'); + }); + it('should upload file using multipart upload with exception', async () => { // create a file with 1M random data const fileContent = Array(1024 * 1024) @@ -2530,7 +2528,7 @@ describe('browser', () => { const info = await store.restore(name); assert.equal(info.res.status, 202); - // in 1 minute verify RestoreAlreadyInProgressError + // in 1 minute verify RestoreAlreadyInProgressError. try { await store.restore(name); } catch (err) { diff --git a/test/config.js b/test/config.js index a58ca7611..47c8154dc 100644 --- a/test/config.js +++ b/test/config.js @@ -1,14 +1,14 @@ const { env } = process; const config = module.exports; -const USWEST = 'oss-us-west-1'; // ONCI=true Using the region of Silicon Valley in the United States would be faster +// const USWEST = 'oss-us-west-1'; // ONCI=true Using the region of Silicon Valley in the United States would be faster config.oss = { accessKeyId: env.ALI_SDK_OSS_ID, accessKeySecret: env.ALI_SDK_OSS_SECRET, accountId: env.ALI_SDK_STS_ROLE.match(/^acs:ram::(\d+):role/i)[1], // Obtain the main account ID through roleRan region: env.ALI_SDK_OSS_REGION, - endpoint: env.ONCI ? `https://${USWEST}.aliyuncs.com` : undefined, + // endpoint: env.ONCI ? `https://${USWEST}.aliyuncs.com` : undefined, maxSocket: 50 }; @@ -17,9 +17,9 @@ config.sts = { accessKeySecret: env.ALI_SDK_STS_SECRET, roleArn: env.ALI_SDK_STS_ROLE, bucket: env.ALI_SDK_STS_BUCKET, - endpoint: env.ONCI ? 'https://sts.aliyuncs.com/' : undefined, + // endpoint: env.ONCI ? 'https://sts.aliyuncs.com/' : undefined, maxSocket: 50 }; -config.metaSyncTime = env.ONCI ? '10s' : '1000ms'; +config.metaSyncTime = env.ONCI ? '1s' : '1000ms'; config.timeout = '120s'; diff --git a/test/node/cluster.test.js b/test/node/cluster.test.js index 830dc5557..e0deaa9c9 100644 --- a/test/node/cluster.test.js +++ b/test/node/cluster.test.js @@ -10,7 +10,7 @@ describe('test/cluster.test.js', () => { afterEach(mm.restore); before(async function () { - this.region = config.region; + // this.region = config.region; this.bucket1 = `ali-oss-test-cluster1-${prefix.replace(/[/.]/g, '')}`; this.bucket2 = `ali-oss-test-cluster2-${prefix.replace(/[/.]/g, '')}`; const client = oss(config); @@ -25,13 +25,13 @@ describe('test/cluster.test.js', () => { accessKeyId: config.accessKeyId, accessKeySecret: config.accessKeySecret, bucket: this.bucket1, - endpoint: config.endpoint + region: config.region }, { accessKeyId: config.accessKeyId, accessKeySecret: config.accessKeySecret, bucket: this.bucket2, - endpoint: config.endpoint + region: config.region } ] }; diff --git a/test/node/cleanAllBucket.js b/test/node/fixtures/cleanAllBucket.js similarity index 51% rename from test/node/cleanAllBucket.js rename to test/node/fixtures/cleanAllBucket.js index 88f9ae365..df7995a0b 100644 --- a/test/node/cleanAllBucket.js +++ b/test/node/fixtures/cleanAllBucket.js @@ -1,6 +1,6 @@ -const { cleanAllBucket } = require('./utils'); -const { oss: config } = require('../config'); -const OSS = require('../..'); +const { cleanAllBucket } = require('../utils'); +const { oss: config } = require('../../config'); +const OSS = require('../../../lib/client'); // eslint-disable-next-line no-console console.log(`cleanAllBucket...${new Date()}`); diff --git a/test/node/nodejs-1024x768.png b/test/node/fixtures/nodejs-1024x768.png similarity index 100% rename from test/node/nodejs-1024x768.png rename to test/node/fixtures/nodejs-1024x768.png diff --git a/test/node/multipart.test.js b/test/node/multipart.test.js index c2422ada7..a4d18adfd 100644 --- a/test/node/multipart.test.js +++ b/test/node/multipart.test.js @@ -1,4 +1,5 @@ const fs = require('fs'); +const path = require('path'); const assert = require('assert'); const utils = require('./utils'); const oss = require('../..'); @@ -7,6 +8,31 @@ const { md5 } = require('utility'); const mm = require('mm'); const sinon = require('sinon'); +const tmpdir = path.join(__dirname, '.tmp'); +if (!fs.existsSync(tmpdir)) { + fs.mkdirSync(tmpdir); +} + +const createFile = async (name, size) => { + size = size || 200 * 1024; + await new Promise((resolve, reject) => { + const rs = fs.createReadStream('/dev/random', { + start: 0, + end: size - 1 + }); + const ws = fs.createWriteStream(name); + rs.pipe(ws); + ws.on('finish', (err, res) => { + if (err) { + reject(err); + } else { + resolve(res); + } + }); + }); + return name; +}; + describe('test/multipart.test.js', () => { const { prefix } = utils; let store; @@ -1004,6 +1030,19 @@ describe('test/multipart.test.js', () => { assert.equal(header['x-oss-traffic-limit'], 645763); assert.equal(header['x-oss-server-side-encryption'], undefined); }); + + it('should set storage-class header', async () => { + const filepath = path.join(tmpdir, 'content-storage-class-file.jpg'); + await createFile(filepath); + const name = `${prefix}ali-sdk/oss/content-type-by-file.png`; + await store.multipartUpload(name, filepath, { + mime: 'text/plain', + headers: { 'x-oss-storage-class': 'IA' } + }); + const result = await store.head(name); + assert.equal(result.res.headers['content-type'], 'text/plain'); + assert.equal(result.res.headers['x-oss-storage-class'], 'IA'); + }); }); }); }); diff --git a/test/node/multiversion.test.js b/test/node/multiversion.test.js index 1936bca45..c6210b743 100644 --- a/test/node/multiversion.test.js +++ b/test/node/multiversion.test.js @@ -157,6 +157,7 @@ describe('test/multiversion.test.js', () => { } ]); assert.equal(putresult1.res.status, 200); + await utils.sleep(2000); const { rules } = await store.getBucketLifecycle(bucket); assert.strictEqual(rules[0].expiration.expiredObjectDeleteMarker, 'true'); }); @@ -174,6 +175,7 @@ describe('test/multiversion.test.js', () => { } ]); assert.equal(putresult1.res.status, 200); + await utils.sleep(1000); const { rules } = await store.getBucketLifecycle(bucket); const [ { @@ -246,6 +248,7 @@ describe('test/multiversion.test.js', () => { let versionId; before(async () => { await store.putBucketVersioning(bucket, enabled); + await utils.sleep(1000); const result = await store.put(name, __filename); store.delete(name); versionId = result.res.headers['x-oss-version-id']; @@ -257,7 +260,7 @@ describe('test/multiversion.test.js', () => { assert.strictEqual(result.res.headers['x-oss-version-id'], versionId); assert.strictEqual(result.res.status, 200); } catch (error) { - assert(false); + assert(false, error); } }); }); @@ -568,18 +571,22 @@ describe('test/multiversion.test.js', () => { // 暂停多版本后,当前版本为DELETE标记,指定version删除该DELETE标记(包括null version),则恢复上一个版本 it('should delete marker and restore lastest version when suspended ', async () => { await store.putBucketVersioning(bucket, enabled); + await utils.sleep(2000); try { const currentName = 'delete-marker-test'; const result = await store.put(currentName, Buffer.from(currentName)); const currentVersionId = result.res.headers['x-oss-version-id']; // 删除当前版本 产生标记 const delRes = await store.delete(currentName); + await utils.sleep(1000); const delVerionsId = delRes.res.headers['x-oss-version-id']; await store.putBucketVersioning(bucket, suspended); + await utils.sleep(1000); // 删除标记 await store.delete(currentName, { versionId: delVerionsId }); + await utils.sleep(1000); // 验证是否恢复上一个版本 const headInfo = await store.head(currentName); assert.strictEqual(headInfo.res.headers['x-oss-version-id'], currentVersionId); @@ -593,6 +600,7 @@ describe('test/multiversion.test.js', () => { it('should return bucket Versioning', async () => { try { await store.putBucketVersioning(bucket, enabled); + await utils.sleep(2000); const result = await store.getBucketInfo(bucket); assert.equal(result.res.status, 200); assert.equal(result.bucket.Versioning, enabled); diff --git a/test/node/nodejs-processed-w200-latest.png b/test/node/nodejs-processed-w200-latest.png deleted file mode 100644 index f4ae7e0b0..000000000 Binary files a/test/node/nodejs-processed-w200-latest.png and /dev/null differ diff --git a/test/node/nodejs-processed-w200.png b/test/node/nodejs-processed-w200.png deleted file mode 100644 index dd0200178..000000000 Binary files a/test/node/nodejs-processed-w200.png and /dev/null differ diff --git a/test/node/object.test.js b/test/node/object.test.js index 26336683e..873fa9eaa 100644 --- a/test/node/object.test.js +++ b/test/node/object.test.js @@ -108,7 +108,7 @@ describe('test/object.test.js', () => { it('should add image with file streaming way', async () => { const name = `${prefix}ali-sdk/oss/nodejs-1024x768.png`; - const imagepath = path.join(__dirname, 'nodejs-1024x768.png'); + const imagepath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); const object = await store.putStream(name, fs.createReadStream(imagepath), { mime: 'image/png' }); @@ -129,7 +129,7 @@ describe('test/object.test.js', () => { it('should put object with http streaming way', async () => { const name = `${prefix}ali-sdk/oss/nodejs-1024x768.png`; const nameCpy = `${prefix}ali-sdk/oss/nodejs-1024x768`; - const imagepath = path.join(__dirname, 'nodejs-1024x768.png'); + const imagepath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); await store.putStream(name, fs.createReadStream(imagepath), { mime: 'image/png' }); const signUrl = await store.signatureUrl(name, { expires: 3600 }); const stream = fs.createWriteStream(imagepath); @@ -179,7 +179,7 @@ describe('test/object.test.js', () => { describe('processObjectSave()', () => { const name = 'sourceObject.png'; before(async () => { - const imagepath = path.join(__dirname, 'nodejs-1024x768.png'); + const imagepath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); await store.putStream(name, fs.createReadStream(imagepath), { mime: 'image/png' }); @@ -832,8 +832,8 @@ describe('test/object.test.js', () => { it('should get object content buffer with image process', async () => { const imageName = `${prefix}ali-sdk/oss/nodejs-test-get-image-1024x768.png`; - const originImagePath = path.join(__dirname, 'nodejs-1024x768.png'); - path.join(__dirname, 'nodejs-processed-w200.png'); + const originImagePath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); + // path.join(__dirname, 'nodejs-processed-w200.png'); await store.put(imageName, originImagePath, { mime: 'image/png' }); @@ -1116,8 +1116,8 @@ describe('test/object.test.js', () => { testParameters: 'xxx' }; const imageName = `${prefix}ali-sdk/oss/nodejs-test-signature-1024x768.png`; - const originImagePath = path.join(__dirname, 'nodejs-1024x768.png'); - path.join(__dirname, 'nodejs-processed-w200.png'); + const originImagePath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); + // path.join(__dirname, 'nodejs-processed-w200.png'); await store.put(imageName, originImagePath, { mime: 'image/png' }); @@ -1152,8 +1152,8 @@ describe('test/object.test.js', () => { // todo 这个用例和上面的重复了 it('should signature url with image processed and get object ok', async () => { const imageName = `${prefix}ali-sdk/oss/nodejs-test-signature-1024x768.png`; - const originImagePath = path.join(__dirname, 'nodejs-1024x768.png'); - path.join(__dirname, 'nodejs-processed-w200.png'); + const originImagePath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); + // path.join(__dirname, 'nodejs-processed-w200.png'); await store.put(imageName, originImagePath, { mime: 'image/png' }); @@ -1398,36 +1398,41 @@ describe('test/object.test.js', () => { }); /** - * Image processing uses different compression algorithms, + * Image processing uses different compression algorithms * and the performance may be inconsistent * between different regions */ it('should get image stream with image process', async () => { const imageName = `${prefix}ali-sdk/oss/nodejs-test-getstream-image-1024x768.png`; - const originImagePath = path.join(__dirname, 'nodejs-1024x768.png'); - const processedImagePath = path.join(__dirname, 'nodejs-processed-w200.png'); - const processedImagePath2 = path.join(__dirname, 'nodejs-processed-w200-latest.png'); + const originImagePath = path.join(__dirname, './fixtures/nodejs-1024x768.png'); + const processedImagePath = path.join(__dirname, './fixtures/nodejs-processed-w200.png'); + const processedImagePath2 = path.join(__dirname, './fixtures/nodejs-processed-w200-latest.png'); await store.put(imageName, originImagePath, { mime: 'image/png' }); - let result = await store.getStream(imageName, { process: 'image/resize,w_200' }); - let result2 = await store.getStream(imageName, { process: 'image/resize,w_200' }); + let opts = { process: 'image/resize,w_200' }; + let result = await store.getStream(imageName, opts); + let result2 = await store.getStream(imageName, opts); assert.equal(result.res.status, 200); assert.equal(result2.res.status, 200); + await store.get(imageName, processedImagePath, opts); + await store.get(imageName, processedImagePath2, opts); + let isEqual = await streamEqual(result.stream, fs.createReadStream(processedImagePath)); let isEqual2 = await streamEqual(result2.stream, fs.createReadStream(processedImagePath2)); assert(isEqual || isEqual2); - result = await store.getStream(imageName, { - process: 'image/resize,w_200', - subres: { 'x-oss-process': 'image/resize,w_100' } - }); - result2 = await store.getStream(imageName, { + + opts = { process: 'image/resize,w_200', subres: { 'x-oss-process': 'image/resize,w_100' } - }); + }; + result = await store.getStream(imageName, opts); + result2 = await store.getStream(imageName, opts); assert.equal(result.res.status, 200); assert.equal(result2.res.status, 200); + await store.get(imageName, processedImagePath, opts); + await store.get(imageName, processedImagePath2, opts); isEqual = await streamEqual(result.stream, fs.createReadStream(processedImagePath)); isEqual2 = await streamEqual(result2.stream, fs.createReadStream(processedImagePath2)); assert(isEqual || isEqual2); @@ -1452,7 +1457,6 @@ describe('test/object.test.js', () => { await store.getStream(`${name}not-exists`); throw new Error('should not run this'); } catch (err) { - console.log('error is', err); assert.equal(err.name, 'NoSuchKeyError'); assert.equal(Object.keys(store.agent.freeSockets).length, 0); await utils.sleep(ms(metaSyncTime)); diff --git a/test/node/range-test.txt b/test/node/range-test.txt deleted file mode 100644 index e84a29b55..000000000 --- a/test/node/range-test.txt +++ /dev/null @@ -1 +0,0 @@ -aaaaaaaaaabbbbbbbbbb \ No newline at end of file