Skip to content

Commit 67275bd

Browse files
authored
fix: checkBucketName bug (#749)
1 parent 8ed3228 commit 67275bd

File tree

8 files changed

+39
-70
lines changed

8 files changed

+39
-70
lines changed

lib/browser/bucket.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ proto.getBucket = function getBucket() {
7777
return this.options.bucket;
7878
};
7979

80-
proto.putBucket = async function putBucket(name, options) {
81-
const params = this._bucketRequestParams('PUT', name, '', options);
82-
params.successStatuses = [200];
83-
const result = await this.request(params);
84-
return {
85-
bucket: (result.headers.location && result.headers.location.substring(1)) || null,
86-
res: result.res
87-
};
88-
};
89-
9080
proto.deleteBucket = async function deleteBucket(name, options) {
9181
const params = this._bucketRequestParams('DELETE', name, '', options);
9282
const result = await this.request(params);

lib/browser/client.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const dateFormat = require('dateformat');
1616
const bowser = require('bowser');
1717
const signUtils = require('../common/signUtils');
1818
const _isIP = require('../common/utils/isIP');
19-
const _checkBucketName = require('../common/utils/checkBucketName');
2019
const _initOptions = require('../common/client/initOptions');
2120

2221
const globalHttpAgent = new AgentKeepalive();
@@ -102,15 +101,7 @@ merge(proto, require('./object'));
102101
// /**
103102
// * Bucket operations
104103
// */
105-
/**
106-
* check Bucket Name
107-
*/
108104

109-
proto._checkBucketName = function (name) {
110-
if (!_checkBucketName(name)) {
111-
throw new Error('The bucket must be conform to the specifications');
112-
}
113-
};
114105
// merge(proto, require('./bucket'));
115106

116107

lib/bucket.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22

33
const assert = require('assert');
4-
const _checkBucketName = require('./common/utils/checkBucketName');
54

65
const proto = exports;
76

@@ -17,16 +16,6 @@ function toArray(obj) {
1716
return [obj];
1817
}
1918

20-
/**
21-
* check Bucket Name
22-
*/
23-
24-
proto._checkBucketName = function (name) {
25-
if (!_checkBucketName(name)) {
26-
throw new Error('The bucket must be conform to the specifications');
27-
}
28-
};
29-
3019
/**
3120
* Bucket opertaions
3221
*/
@@ -113,38 +102,6 @@ proto.getBucketInfo = async function getBucketInfo(name, options) {
113102
};
114103
};
115104

116-
proto.putBucket = async function putBucket(name, options) {
117-
this._checkBucketName(name);
118-
options = options || {};
119-
const params = this._bucketRequestParams('PUT', name, '', options);
120-
121-
const startTag = '<?xml version="1.0" encoding="UTF-8"?>\n<CreateBucketConfiguration>';
122-
const endTag = '</CreateBucketConfiguration>';
123-
let paramlXML = '';
124-
125-
// server not support
126-
// if (region) {
127-
// paramlXML += `<LocationConstraint>${region}</LocationConstraint>`;
128-
// params.content = `${startTag}${paramlXML}${endTag}`;
129-
// }
130-
131-
if (options.StorageClass) {
132-
paramlXML += `<StorageClass>${options.StorageClass}</StorageClass>`;
133-
}
134-
135-
if (paramlXML) {
136-
params.mime = 'xml';
137-
params.content = `${startTag}${paramlXML}${endTag}`;
138-
}
139-
140-
params.successStatuses = [200];
141-
const result = await this.request(params);
142-
return {
143-
bucket: (result.headers.location && result.headers.location.substring(1)) || null,
144-
res: result.res
145-
};
146-
};
147-
148105
proto.deleteBucket = async function deleteBucket(name, options) {
149106
this._checkBucketName(name);
150107
const params = this._bucketRequestParams('DELETE', name, '', options);

lib/common/bucket/_checkBucketName.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const checkBucketName = require('../utils/checkBucketName');
2+
3+
const proto = exports;
4+
5+
proto._checkBucketName = function (name, createBucket) {
6+
if (!checkBucketName(name, createBucket)) {
7+
throw new Error('The bucket must be conform to the specifications');
8+
}
9+
};

lib/common/bucket/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ merge(proto, require('./deleteBucketEncryption'));
1010
merge(proto, require('./getBucketTags'));
1111
merge(proto, require('./putBucketTags'));
1212
merge(proto, require('./deleteBucketTags'));
13+
merge(proto, require('./putBucket'));
14+
merge(proto, require('./_checkBucketName'));

lib/common/bucket/putBucket.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const proto = exports;
2+
const obj2xml = require('../utils/obj2xml');
3+
4+
proto.putBucket = async function putBucket(name, options) {
5+
this._checkBucketName(name, true);
6+
options = options || {};
7+
const params = this._bucketRequestParams('PUT', name, '', options);
8+
9+
const CreateBucketConfiguration = {};
10+
const paramlXMLObJ = {
11+
CreateBucketConfiguration
12+
};
13+
14+
if (options.StorageClass) {
15+
CreateBucketConfiguration.StorageClass = options.StorageClass;
16+
params.mime = 'xml';
17+
params.content = obj2xml(paramlXMLObJ, { headers: true });
18+
}
19+
20+
params.successStatuses = [200];
21+
const result = await this.request(params);
22+
return {
23+
bucket: (result.headers.location && result.headers.location.substring(1)) || null,
24+
res: result.res
25+
};
26+
};

lib/common/utils/checkBucketName.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* check Bucket Name
33
*/
44

5-
module.exports = function (name) {
6-
const bucketRegex = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
5+
module.exports = function (name, createBucket) {
6+
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_]$/;
77
const checkBucket = bucketRegex.test(name);
88
return checkBucket;
99
};

test/node/bucket.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,13 @@ describe('test/bucket.test.js', () => {
8383
const result1 = await store.putBucket(name);
8484
assert.equal(result1.bucket, name);
8585
assert.equal(result1.res.status, 200);
86-
87-
// create a exists should work
88-
const result2 = await store.putBucket(name);
89-
assert.equal(result2.res.status, 200);
90-
assert.equal(result2.bucket, name);
9186
});
9287

9388
it('should create an archive bucket', async () => {
9489
await utils.sleep(ms(metaSyncTime));
9590
const result2 = await store.listBuckets();
9691
const { buckets } = result2;
9792
const m = buckets.some(item => item.name === archvieBucket);
98-
console.log(buckets);
9993
assert(m === true);
10094
buckets.map((item) => {
10195
if (item.name === archvieBucket) {

0 commit comments

Comments
 (0)