diff --git a/lib/browser/bucket.js b/lib/browser/bucket.js index 42045b571..9b8a557be 100644 --- a/lib/browser/bucket.js +++ b/lib/browser/bucket.js @@ -1,5 +1,6 @@ const assert = require('assert'); +const _checkBucketName = require('../common/utils/checkBucketName'); const proto = exports; @@ -62,13 +63,13 @@ function toArray(obj) { // }; proto.useBucket = function useBucket(name) { - this._checkBucketName(name); + _checkBucketName(name); this.options.bucket = name; return this; }; proto.setBucket = function useBucket(name) { - this._checkBucketName(name); + _checkBucketName(name); this.options.bucket = name; return this; }; diff --git a/lib/bucket.js b/lib/bucket.js index 2f6ebb9fe..b81d51b3d 100644 --- a/lib/bucket.js +++ b/lib/bucket.js @@ -2,6 +2,7 @@ const assert = require('assert'); const isArray = require('./common/utils/isArray'); +const _checkBucketName = require('../lib/common/utils/checkBucketName'); const proto = exports; @@ -58,12 +59,12 @@ proto.listBuckets = async function listBuckets(query, options) { }; proto.useBucket = function useBucket(name) { - this._checkBucketName(name); + _checkBucketName(name); return this.setBucket(name); }; proto.setBucket = function useBucket(name) { - this._checkBucketName(name); + _checkBucketName(name); this.options.bucket = name; return this; }; @@ -73,7 +74,7 @@ proto.getBucket = function getBucket() { }; proto.getBucketLocation = async function getBucketLocation(name, options) { - this._checkBucketName(name); + _checkBucketName(name); name = name || this.getBucket(); const params = this._bucketRequestParams('GET', name, 'location', options); params.successStatuses = [200]; @@ -86,7 +87,7 @@ proto.getBucketLocation = async function getBucketLocation(name, options) { }; proto.getBucketInfo = async function getBucketInfo(name, options) { - this._checkBucketName(name); + _checkBucketName(name); name = name || this.getBucket(); const params = this._bucketRequestParams('GET', name, 'bucketInfo', options); params.successStatuses = [200]; @@ -99,7 +100,7 @@ proto.getBucketInfo = async function getBucketInfo(name, options) { }; proto.deleteBucket = async function deleteBucket(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, '', options); const result = await this.request(params); if (result.status === 200 || result.status === 204) { @@ -113,7 +114,7 @@ proto.deleteBucket = async function deleteBucket(name, options) { // acl proto.putBucketACL = async function putBucketACL(name, acl, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('PUT', name, 'acl', options); params.headers = { 'x-oss-acl': acl @@ -127,7 +128,7 @@ proto.putBucketACL = async function putBucketACL(name, acl, options) { }; proto.getBucketACL = async function getBucketACL(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'acl', options); params.successStatuses = [200]; params.xmlResponse = true; @@ -145,7 +146,7 @@ proto.getBucketACL = async function getBucketACL(name, options) { // logging proto.putBucketLogging = async function putBucketLogging(name, prefix, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('PUT', name, 'logging', options); let xml = `${'\n\n' + '\n'}${name}\n`; @@ -163,7 +164,7 @@ proto.putBucketLogging = async function putBucketLogging(name, prefix, options) }; proto.getBucketLogging = async function getBucketLogging(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'logging', options); params.successStatuses = [200]; params.xmlResponse = true; @@ -177,7 +178,7 @@ proto.getBucketLogging = async function getBucketLogging(name, options) { }; proto.deleteBucketLogging = async function deleteBucketLogging(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, 'logging', options); params.successStatuses = [204, 200]; const result = await this.request(params); @@ -187,7 +188,7 @@ proto.deleteBucketLogging = async function deleteBucketLogging(name, options) { }; proto.putBucketCORS = async function putBucketCORS(name, rules, options) { - this._checkBucketName(name); + _checkBucketName(name); rules = rules || []; assert(rules.length, 'rules is required'); rules.forEach((rule) => { @@ -233,7 +234,7 @@ proto.putBucketCORS = async function putBucketCORS(name, rules, options) { }; proto.getBucketCORS = async function getBucketCORS(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'cors', options); params.successStatuses = [200]; params.xmlResponse = true; @@ -257,7 +258,7 @@ proto.getBucketCORS = async function getBucketCORS(name, options) { }; proto.deleteBucketCORS = async function deleteBucketCORS(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, 'cors', options); params.successStatuses = [204]; const result = await this.request(params); @@ -269,7 +270,7 @@ proto.deleteBucketCORS = async function deleteBucketCORS(name, options) { // referer proto.putBucketReferer = async function putBucketReferer(name, allowEmpty, referers, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('PUT', name, 'referer', options); let xml = '\n\n'; xml += ` ${allowEmpty ? 'true' : 'false'}\n`; @@ -293,7 +294,7 @@ proto.putBucketReferer = async function putBucketReferer(name, allowEmpty, refer }; proto.getBucketReferer = async function getBucketReferer(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'referer', options); params.successStatuses = [200]; params.xmlResponse = true; @@ -312,7 +313,7 @@ proto.getBucketReferer = async function getBucketReferer(name, options) { }; proto.deleteBucketReferer = async function deleteBucketReferer(name, options) { - this._checkBucketName(name); + _checkBucketName(name); return await this.putBucketReferer(name, true, null, options); }; diff --git a/lib/common/bucket/_checkBucketName.js b/lib/common/bucket/_checkBucketName.js deleted file mode 100644 index bddbbc622..000000000 --- a/lib/common/bucket/_checkBucketName.js +++ /dev/null @@ -1,9 +0,0 @@ -const checkBucketName = require('../utils/checkBucketName'); - -const proto = exports; - -proto._checkBucketName = function (name, createBucket) { - if (!checkBucketName(name, createBucket)) { - throw new Error('The bucket must be conform to the specifications'); - } -}; diff --git a/lib/common/bucket/deleteBucketEncryption.js b/lib/common/bucket/deleteBucketEncryption.js index 1b9b28efc..1df352906 100644 --- a/lib/common/bucket/deleteBucketEncryption.js +++ b/lib/common/bucket/deleteBucketEncryption.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; // const jstoxml = require('jstoxml'); /** @@ -6,7 +8,7 @@ const proto = exports; */ proto.deleteBucketEncryption = async function deleteBucketEncryption(bucketName) { - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams('DELETE', bucketName, 'encryption'); params.successStatuses = [204]; params.xmlResponse = true; diff --git a/lib/common/bucket/deleteBucketLifecycle.js b/lib/common/bucket/deleteBucketLifecycle.js index f4c1d955a..9f49845e5 100644 --- a/lib/common/bucket/deleteBucketLifecycle.js +++ b/lib/common/bucket/deleteBucketLifecycle.js @@ -1,7 +1,9 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; proto.deleteBucketLifecycle = async function deleteBucketLifecycle(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, 'lifecycle', options); params.successStatuses = [204]; const result = await this.request(params); diff --git a/lib/common/bucket/deleteBucketPolicy.js b/lib/common/bucket/deleteBucketPolicy.js index 2e9f5e2d6..de2218430 100644 --- a/lib/common/bucket/deleteBucketPolicy.js +++ b/lib/common/bucket/deleteBucketPolicy.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; /** * deleteBucketPolicy @@ -6,7 +8,7 @@ const proto = exports; */ proto.deleteBucketPolicy = async function deleteBucketPolicy(bucketName, options = {}) { - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams('DELETE', bucketName, 'policy', options); params.successStatuses = [204]; diff --git a/lib/common/bucket/deleteBucketTags.js b/lib/common/bucket/deleteBucketTags.js index 623d7768e..127c8a576 100644 --- a/lib/common/bucket/deleteBucketTags.js +++ b/lib/common/bucket/deleteBucketTags.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; /** * deleteBucketTags @@ -6,7 +8,7 @@ const proto = exports; */ proto.deleteBucketTags = async function deleteBucketTags(name, options = {}) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, 'tagging', options); params.successStatuses = [204]; diff --git a/lib/common/bucket/deleteBucketWebsite.js b/lib/common/bucket/deleteBucketWebsite.js index 4defbc6fe..f607432a5 100644 --- a/lib/common/bucket/deleteBucketWebsite.js +++ b/lib/common/bucket/deleteBucketWebsite.js @@ -1,7 +1,9 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; proto.deleteBucketWebsite = async function deleteBucketWebsite(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, 'website', options); params.successStatuses = [204]; const result = await this.request(params); diff --git a/lib/common/bucket/getBucketEncryption.js b/lib/common/bucket/getBucketEncryption.js index 2f004cd2a..e689dd65f 100644 --- a/lib/common/bucket/getBucketEncryption.js +++ b/lib/common/bucket/getBucketEncryption.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; /** * getBucketEncryption @@ -5,7 +7,7 @@ const proto = exports; */ proto.getBucketEncryption = async function getBucketEncryption(bucketName) { - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams('GET', bucketName, 'encryption'); params.successStatuses = [200]; params.xmlResponse = true; diff --git a/lib/common/bucket/getBucketLifecycle.js b/lib/common/bucket/getBucketLifecycle.js index 72509a3e9..0d12ca133 100644 --- a/lib/common/bucket/getBucketLifecycle.js +++ b/lib/common/bucket/getBucketLifecycle.js @@ -1,10 +1,11 @@ +const _checkBucketName = require('../utils/checkBucketName'); const isArray = require('../utils/isArray'); const formatObjKey = require('../utils/formatObjKey'); const proto = exports; proto.getBucketLifecycle = async function getBucketLifecycle(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'lifecycle', options); params.successStatuses = [200]; params.xmlResponse = true; diff --git a/lib/common/bucket/getBucketPolicy.js b/lib/common/bucket/getBucketPolicy.js index 0d46b5720..42c3b4e3d 100644 --- a/lib/common/bucket/getBucketPolicy.js +++ b/lib/common/bucket/getBucketPolicy.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; /** * getBucketPolicy @@ -6,7 +8,7 @@ const proto = exports; */ proto.getBucketPolicy = async function getBucketPolicy(bucketName, options = {}) { - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams('GET', bucketName, 'policy', options); diff --git a/lib/common/bucket/getBucketRequestPayment.js b/lib/common/bucket/getBucketRequestPayment.js index 4f62a3f84..6e070ab57 100644 --- a/lib/common/bucket/getBucketRequestPayment.js +++ b/lib/common/bucket/getBucketRequestPayment.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; /** * getBucketRequestPayment @@ -8,7 +10,7 @@ const proto = exports; proto.getBucketRequestPayment = async function getBucketRequestPayment(bucketName, options) { options = options || {}; - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams('GET', bucketName, 'requestPayment', options); params.successStatuses = [200]; params.xmlResponse = true; diff --git a/lib/common/bucket/getBucketTags.js b/lib/common/bucket/getBucketTags.js index 017adb6a5..08e98371c 100644 --- a/lib/common/bucket/getBucketTags.js +++ b/lib/common/bucket/getBucketTags.js @@ -1,4 +1,5 @@ const proto = exports; +const _checkBucketName = require('../utils/checkBucketName'); const isObject = require('../utils/isObject'); /** * getBucketTags @@ -8,7 +9,7 @@ const isObject = require('../utils/isObject'); */ proto.getBucketTags = async function getBucketTags(name, options = {}) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'tagging', options); params.successStatuses = [200]; const result = await this.request(params); diff --git a/lib/common/bucket/getBucketWebsite.js b/lib/common/bucket/getBucketWebsite.js index fe6be62f7..d18549a88 100644 --- a/lib/common/bucket/getBucketWebsite.js +++ b/lib/common/bucket/getBucketWebsite.js @@ -1,9 +1,10 @@ +const _checkBucketName = require('../utils/checkBucketName'); const isObject = require('../utils/isObject'); const proto = exports; proto.getBucketWebsite = async function getBucketWebsite(name, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('GET', name, 'website', options); params.successStatuses = [200]; params.xmlResponse = true; diff --git a/lib/common/bucket/index.js b/lib/common/bucket/index.js index 29d7017b9..4f7622c8b 100644 --- a/lib/common/bucket/index.js +++ b/lib/common/bucket/index.js @@ -11,7 +11,6 @@ merge(proto, require('./getBucketTags')); merge(proto, require('./putBucketTags')); merge(proto, require('./deleteBucketTags')); merge(proto, require('./putBucket')); -merge(proto, require('./_checkBucketName')); merge(proto, require('./getBucketWebsite')); merge(proto, require('./putBucketWebsite')); merge(proto, require('./deleteBucketWebsite')); diff --git a/lib/common/bucket/putBucket.js b/lib/common/bucket/putBucket.js index 86f54251d..4471dde3a 100644 --- a/lib/common/bucket/putBucket.js +++ b/lib/common/bucket/putBucket.js @@ -1,8 +1,9 @@ const proto = exports; +const _checkBucketName = require('../utils/checkBucketName'); const obj2xml = require('../utils/obj2xml'); proto.putBucket = async function putBucket(name, options) { - this._checkBucketName(name, true); + _checkBucketName(name, true); options = options || {}; const params = this._bucketRequestParams('PUT', name, '', options); diff --git a/lib/common/bucket/putBucketEncryption.js b/lib/common/bucket/putBucketEncryption.js index 9f2859470..eb0e76211 100644 --- a/lib/common/bucket/putBucketEncryption.js +++ b/lib/common/bucket/putBucketEncryption.js @@ -1,5 +1,6 @@ const proto = exports; // const jstoxml = require('jstoxml'); +const _checkBucketName = require('../utils/checkBucketName'); const obj2xml = require('../utils/obj2xml'); /** * putBucketEncryption @@ -9,7 +10,7 @@ const obj2xml = require('../utils/obj2xml'); proto.putBucketEncryption = async function putBucketEncryption(bucketName, options) { options = options || {}; - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams('PUT', bucketName, 'encryption', options); params.successStatuses = [200]; const paramXMLObj = { diff --git a/lib/common/bucket/putBucketLifecycle.js b/lib/common/bucket/putBucketLifecycle.js index 7f8bdf211..e9f4e47de 100644 --- a/lib/common/bucket/putBucketLifecycle.js +++ b/lib/common/bucket/putBucketLifecycle.js @@ -1,5 +1,5 @@ /* eslint-disable no-use-before-define */ - +const _checkBucketName = require('../utils/checkBucketName'); const isArray = require('../utils/isArray'); const deepCopy = require('../utils/deepCopy'); const isObject = require('../utils/isObject'); @@ -11,7 +11,7 @@ const proto = exports; proto.putBucketLifecycle = async function putBucketLifecycle(name, rules, options) { - this._checkBucketName(name); + _checkBucketName(name); if (!isArray(rules)) { throw new Error('rules must be Array'); diff --git a/lib/common/bucket/putBucketPolicy.js b/lib/common/bucket/putBucketPolicy.js index 8c88d2ef1..d8d8c8b9a 100644 --- a/lib/common/bucket/putBucketPolicy.js +++ b/lib/common/bucket/putBucketPolicy.js @@ -1,4 +1,4 @@ - +const _checkBucketName = require('../utils/checkBucketName'); const policy2Str = require('../utils/policy2Str'); const isObject = require('../utils/isObject'); @@ -11,7 +11,7 @@ const proto = exports; */ proto.putBucketPolicy = async function putBucketPolicy(bucketName, policy, options = {}) { - this._checkBucketName(bucketName); + _checkBucketName(bucketName); if (!isObject(policy)) { throw new Error('policy is not Object'); diff --git a/lib/common/bucket/putBucketRequestPayment.js b/lib/common/bucket/putBucketRequestPayment.js index 743546001..59416e6e0 100644 --- a/lib/common/bucket/putBucketRequestPayment.js +++ b/lib/common/bucket/putBucketRequestPayment.js @@ -1,3 +1,4 @@ +const _checkBucketName = require('../utils/checkBucketName'); const obj2xml = require('../utils/obj2xml'); const proto = exports; @@ -19,7 +20,7 @@ proto.putBucketRequestPayment = async function putBucketRequestPayment( throw new Error('payer must be BucketOwner or Requester'); } - this._checkBucketName(bucketName); + _checkBucketName(bucketName); const params = this._bucketRequestParams( 'PUT', bucketName, diff --git a/lib/common/bucket/putBucketTags.js b/lib/common/bucket/putBucketTags.js index ff3b18313..16b02eeb7 100644 --- a/lib/common/bucket/putBucketTags.js +++ b/lib/common/bucket/putBucketTags.js @@ -1,3 +1,4 @@ +const _checkBucketName = require('../utils/checkBucketName'); const obj2xml = require('../utils/obj2xml'); const checkTag = require('../utils/checkBucketTag'); @@ -10,7 +11,7 @@ const proto = exports; */ proto.putBucketTags = async function putBucketTags(name, tag, options = {}) { - this._checkBucketName(name); + _checkBucketName(name); checkTag(tag); const params = this._bucketRequestParams('PUT', name, 'tagging', options); params.successStatuses = [200]; diff --git a/lib/common/bucket/putBucketWebsite.js b/lib/common/bucket/putBucketWebsite.js index 87b5e2293..a52e3b594 100644 --- a/lib/common/bucket/putBucketWebsite.js +++ b/lib/common/bucket/putBucketWebsite.js @@ -1,9 +1,10 @@ +const _checkBucketName = require('../utils/checkBucketName'); const obj2xml = require('../utils/obj2xml'); const isArray = require('../utils/isArray'); const proto = exports; proto.putBucketWebsite = async function putBucketWebsite(name, config = {}, options) { - this._checkBucketName(name); + _checkBucketName(name); const params = this._bucketRequestParams('PUT', name, 'website', options); const IndexDocument = { Suffix: config.index || 'index.html' diff --git a/lib/common/client/initOptions.js b/lib/common/client/initOptions.js index b7a665d62..e1290c3cb 100644 --- a/lib/common/client/initOptions.js +++ b/lib/common/client/initOptions.js @@ -35,8 +35,8 @@ module.exports = function (options) { || !options.accessKeySecret) { throw new Error('require accessKeyId, accessKeySecret'); } - if (options.bucket && !_checkBucketName(options.bucket)) { - throw new Error('The bucket must be conform to the specifications'); + if (options.bucket) { + _checkBucketName(options.bucket); } const opts = Object.assign({ region: 'oss-cn-hangzhou', diff --git a/lib/common/image/processObjectSave.js b/lib/common/image/processObjectSave.js index 320864f2d..3f4a26d26 100644 --- a/lib/common/image/processObjectSave.js +++ b/lib/common/image/processObjectSave.js @@ -1,4 +1,5 @@ /* eslint-disable no-use-before-define */ +const _checkBucketName = require('../utils/checkBucketName'); const querystring = require('querystring'); const { Base64: { encode: str2Base64 } } = require('js-base64'); @@ -10,7 +11,7 @@ proto.processObjectSave = async function processObjectSave(sourceObject, targetO checkArgs(process, 'process'); targetObject = this._objectName(targetObject); if (targetBucket) { - this._checkBucketName(targetBucket); + _checkBucketName(targetBucket); } const params = this._objectRequestParams('POST', sourceObject, { diff --git a/lib/common/object/copyObject.js b/lib/common/object/copyObject.js index 400790ebf..553a2a925 100644 --- a/lib/common/object/copyObject.js +++ b/lib/common/object/copyObject.js @@ -1,3 +1,5 @@ +const _checkBucketName = require('../utils/checkBucketName'); + const proto = exports; proto.copy = async function copy(name, sourceName, bucketName, options) { @@ -50,7 +52,7 @@ proto._getSourceName = function _getSourceName(sourceName, bucketName) { sourceName = sourceName.replace(/(\/.+?\/)(.*)/, '$2'); } - this._checkBucketName(bucketName); + _checkBucketName(bucketName); sourceName = `/${bucketName}/${encodeURIComponent(sourceName)}`; return sourceName; diff --git a/lib/common/utils/checkBucketName.js b/lib/common/utils/checkBucketName.js index 531ff21fc..b1d736afc 100644 --- a/lib/common/utils/checkBucketName.js +++ b/lib/common/utils/checkBucketName.js @@ -4,6 +4,7 @@ module.exports = function (name, createBucket) { const bucketRegex = createBucket ? /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/ : /^[a-z0-9_][a-z0-9-_]{1,61}[a-z0-9_]$/; - const checkBucket = bucketRegex.test(name); - return checkBucket; + if (!bucketRegex.test(name)) { + throw new Error('The bucket must be conform to the specifications'); + } };