Skip to content

Commit

Permalink
fix: checkBucketName bug (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie authored Apr 3, 2020
1 parent 0ef0fe2 commit 0a3c64c
Show file tree
Hide file tree
Showing 26 changed files with 74 additions and 54 deletions.
5 changes: 3 additions & 2 deletions lib/browser/bucket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

const assert = require('assert');
const _checkBucketName = require('../common/utils/checkBucketName');

const proto = exports;

Expand Down Expand Up @@ -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;
};
Expand Down
33 changes: 17 additions & 16 deletions lib/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const isArray = require('./common/utils/isArray');
const _checkBucketName = require('../lib/common/utils/checkBucketName');

const proto = exports;

Expand Down Expand Up @@ -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;
};
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 = `${'<?xml version="1.0" encoding="UTF-8"?>\n<BucketLoggingStatus>\n' +
'<LoggingEnabled>\n<TargetBucket>'}${name}</TargetBucket>\n`;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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) => {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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 = '<?xml version="1.0" encoding="UTF-8"?>\n<RefererConfiguration>\n';
xml += ` <AllowEmptyReferer>${allowEmpty ? 'true' : 'false'}</AllowEmptyReferer>\n`;
Expand All @@ -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;
Expand All @@ -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);
};

Expand Down
9 changes: 0 additions & 9 deletions lib/common/bucket/_checkBucketName.js

This file was deleted.

4 changes: 3 additions & 1 deletion lib/common/bucket/deleteBucketEncryption.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');

const proto = exports;
// const jstoxml = require('jstoxml');
/**
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/deleteBucketLifecycle.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/deleteBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');

const proto = exports;
/**
* deleteBucketPolicy
Expand All @@ -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];
Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/deleteBucketTags.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');

const proto = exports;
/**
* deleteBucketTags
Expand All @@ -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];
Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/deleteBucketWebsite.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/getBucketEncryption.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const _checkBucketName = require('../utils/checkBucketName');

const proto = exports;
/**
* getBucketEncryption
* @param {String} bucketName - bucket name
*/

proto.getBucketEncryption = async function getBucketEncryption(bucketName) {
this._checkBucketName(bucketName);
_checkBucketName(bucketName);
const params = this._bucketRequestParams('GET', bucketName, 'encryption');
params.successStatuses = [200];
params.xmlResponse = true;
Expand Down
3 changes: 2 additions & 1 deletion lib/common/bucket/getBucketLifecycle.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/getBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');

const proto = exports;
/**
* getBucketPolicy
Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion lib/common/bucket/getBucketRequestPayment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _checkBucketName = require('../utils/checkBucketName');

const proto = exports;
/**
* getBucketRequestPayment
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/common/bucket/getBucketTags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const proto = exports;
const _checkBucketName = require('../utils/checkBucketName');
const isObject = require('../utils/isObject');
/**
* getBucketTags
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/common/bucket/getBucketWebsite.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/common/bucket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
3 changes: 2 additions & 1 deletion lib/common/bucket/putBucket.js
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
3 changes: 2 additions & 1 deletion lib/common/bucket/putBucketEncryption.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const proto = exports;
// const jstoxml = require('jstoxml');
const _checkBucketName = require('../utils/checkBucketName');
const obj2xml = require('../utils/obj2xml');
/**
* putBucketEncryption
Expand All @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/putBucketLifecycle.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions lib/common/bucket/putBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

const _checkBucketName = require('../utils/checkBucketName');
const policy2Str = require('../utils/policy2Str');
const isObject = require('../utils/isObject');

Expand All @@ -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');
Expand Down
Loading

0 comments on commit 0a3c64c

Please sign in to comment.