Skip to content

Commit

Permalink
fix: checkBucketName bug (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie committed Mar 23, 2020
1 parent 8ed3228 commit 67275bd
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 70 deletions.
10 changes: 0 additions & 10 deletions lib/browser/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,6 @@ proto.getBucket = function getBucket() {
return this.options.bucket;
};

proto.putBucket = async function putBucket(name, options) {
const params = this._bucketRequestParams('PUT', name, '', options);
params.successStatuses = [200];
const result = await this.request(params);
return {
bucket: (result.headers.location && result.headers.location.substring(1)) || null,
res: result.res
};
};

proto.deleteBucket = async function deleteBucket(name, options) {
const params = this._bucketRequestParams('DELETE', name, '', options);
const result = await this.request(params);
Expand Down
9 changes: 0 additions & 9 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const dateFormat = require('dateformat');
const bowser = require('bowser');
const signUtils = require('../common/signUtils');
const _isIP = require('../common/utils/isIP');
const _checkBucketName = require('../common/utils/checkBucketName');
const _initOptions = require('../common/client/initOptions');

const globalHttpAgent = new AgentKeepalive();
Expand Down Expand Up @@ -102,15 +101,7 @@ merge(proto, require('./object'));
// /**
// * Bucket operations
// */
/**
* check Bucket Name
*/

proto._checkBucketName = function (name) {
if (!_checkBucketName(name)) {
throw new Error('The bucket must be conform to the specifications');
}
};
// merge(proto, require('./bucket'));


Expand Down
43 changes: 0 additions & 43 deletions lib/bucket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


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

const proto = exports;

Expand All @@ -17,16 +16,6 @@ function toArray(obj) {
return [obj];
}

/**
* check Bucket Name
*/

proto._checkBucketName = function (name) {
if (!_checkBucketName(name)) {
throw new Error('The bucket must be conform to the specifications');
}
};

/**
* Bucket opertaions
*/
Expand Down Expand Up @@ -113,38 +102,6 @@ proto.getBucketInfo = async function getBucketInfo(name, options) {
};
};

proto.putBucket = async function putBucket(name, options) {
this._checkBucketName(name);
options = options || {};
const params = this._bucketRequestParams('PUT', name, '', options);

const startTag = '<?xml version="1.0" encoding="UTF-8"?>\n<CreateBucketConfiguration>';
const endTag = '</CreateBucketConfiguration>';
let paramlXML = '';

// server not support
// if (region) {
// paramlXML += `<LocationConstraint>${region}</LocationConstraint>`;
// params.content = `${startTag}${paramlXML}${endTag}`;
// }

if (options.StorageClass) {
paramlXML += `<StorageClass>${options.StorageClass}</StorageClass>`;
}

if (paramlXML) {
params.mime = 'xml';
params.content = `${startTag}${paramlXML}${endTag}`;
}

params.successStatuses = [200];
const result = await this.request(params);
return {
bucket: (result.headers.location && result.headers.location.substring(1)) || null,
res: result.res
};
};

proto.deleteBucket = async function deleteBucket(name, options) {
this._checkBucketName(name);
const params = this._bucketRequestParams('DELETE', name, '', options);
Expand Down
9 changes: 9 additions & 0 deletions lib/common/bucket/_checkBucketName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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');
}
};
2 changes: 2 additions & 0 deletions lib/common/bucket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ merge(proto, require('./deleteBucketEncryption'));
merge(proto, require('./getBucketTags'));
merge(proto, require('./putBucketTags'));
merge(proto, require('./deleteBucketTags'));
merge(proto, require('./putBucket'));
merge(proto, require('./_checkBucketName'));
26 changes: 26 additions & 0 deletions lib/common/bucket/putBucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const proto = exports;
const obj2xml = require('../utils/obj2xml');

proto.putBucket = async function putBucket(name, options) {
this._checkBucketName(name, true);
options = options || {};
const params = this._bucketRequestParams('PUT', name, '', options);

const CreateBucketConfiguration = {};
const paramlXMLObJ = {
CreateBucketConfiguration
};

if (options.StorageClass) {
CreateBucketConfiguration.StorageClass = options.StorageClass;
params.mime = 'xml';
params.content = obj2xml(paramlXMLObJ, { headers: true });
}

params.successStatuses = [200];
const result = await this.request(params);
return {
bucket: (result.headers.location && result.headers.location.substring(1)) || null,
res: result.res
};
};
4 changes: 2 additions & 2 deletions lib/common/utils/checkBucketName.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* check Bucket Name
*/

module.exports = function (name) {
const bucketRegex = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
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;
};
6 changes: 0 additions & 6 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ describe('test/bucket.test.js', () => {
const result1 = await store.putBucket(name);
assert.equal(result1.bucket, name);
assert.equal(result1.res.status, 200);

// create a exists should work
const result2 = await store.putBucket(name);
assert.equal(result2.res.status, 200);
assert.equal(result2.bucket, name);
});

it('should create an archive bucket', async () => {
await utils.sleep(ms(metaSyncTime));
const result2 = await store.listBuckets();
const { buckets } = result2;
const m = buckets.some(item => item.name === archvieBucket);
console.log(buckets);
assert(m === true);
buckets.map((item) => {
if (item.name === archvieBucket) {
Expand Down

0 comments on commit 67275bd

Please sign in to comment.