forked from ali-sdk/ali-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: common initOptions * doc: remove usefetch
- Loading branch information
1 parent
1f49ff3
commit a35c8b6
Showing
6 changed files
with
84 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const ms = require('humanize-ms'); | ||
const urlutil = require('url'); | ||
|
||
function setEndpoint(endpoint) { | ||
let url = urlutil.parse(endpoint); | ||
|
||
if (!url.protocol) { | ||
url = urlutil.parse(`http://${endpoint}`); | ||
} | ||
|
||
if (url.protocol !== 'http:' && url.protocol !== 'https:') { | ||
throw new Error('Endpoint protocol must be http or https.'); | ||
} | ||
|
||
return url; | ||
} | ||
|
||
function setRegion(region, internal, secure) { | ||
const protocol = secure ? 'https://' : 'http://'; | ||
let suffix = internal ? '-internal.aliyuncs.com' : '.aliyuncs.com'; | ||
const prefix = 'vpc100-oss-cn-'; | ||
// aliyun VPC region: https://help.aliyun.com/knowledge_detail/38740.html | ||
if (region.substr(0, prefix.length) === prefix) { | ||
suffix = '.aliyuncs.com'; | ||
} | ||
|
||
return urlutil.parse(protocol + region + suffix); | ||
} | ||
|
||
|
||
module.exports = function (options) { | ||
if (!options | ||
|| !options.accessKeyId | ||
|| !options.accessKeySecret) { | ||
throw new Error('require accessKeyId, accessKeySecret'); | ||
} | ||
const opts = Object.assign({ | ||
region: 'oss-cn-hangzhou', | ||
internal: false, | ||
secure: false, | ||
timeout: 60000, | ||
bucket: null, | ||
endpoint: null, | ||
cname: false, | ||
isRequestPay: false | ||
}, options); | ||
|
||
opts.accessKeyId = opts.accessKeyId.trim(); | ||
opts.accessKeySecret = opts.accessKeySecret.trim(); | ||
|
||
if (opts.timeout) { | ||
opts.timeout = ms(opts.timeout); | ||
} | ||
|
||
if (opts.endpoint) { | ||
opts.endpoint = setEndpoint(opts.endpoint, opts.secure); | ||
} else if (opts.region) { | ||
opts.endpoint = setRegion(opts.region, opts.internal, opts.secure); | ||
} else { | ||
throw new Error('require options.endpoint or options.region'); | ||
} | ||
|
||
opts.inited = true; | ||
return opts; | ||
}; |
4 changes: 2 additions & 2 deletions
4
lib/common/utils/checkBucketname.js → lib/common/utils/checkBucketName.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/** | ||
* check Bucket name | ||
* check Bucket Name | ||
*/ | ||
|
||
exports._checkBucketName = function (name) { | ||
const bucketRegex = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/; | ||
const checkBucket = bucketRegex.test(name); | ||
return checkBucket; | ||
}; | ||
}; |