Skip to content

Commit 8c64904

Browse files
authored
feat: support cname (#19)
closes #18 https://help.aliyun.com/zh/oss/user-guide/map-custom-domain-names-5 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced support for custom domains with a new `cname` option for OSSObject. - Added contributors section with a dynamic image link in the README. - **Documentation** - Updated README to include instructions for using a custom domain and modified `endpoint` values. - **Chores** - Updated Node.js version requirement to `>= 16.0.0`. - Updated CI workflow scripts and dependencies. - Modified package.json to reflect updated dependencies and scripts. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8ac8953 commit 8c64904

File tree

5 files changed

+66
-34
lines changed

5 files changed

+66
-34
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ jobs:
2323

2424
steps:
2525
- name: Checkout Git Source
26-
uses: actions/checkout@v3
26+
uses: actions/checkout@v4
2727

2828
- name: Use Node.js ${{ matrix.node-version }}
29-
uses: actions/setup-node@v3
29+
uses: actions/setup-node@v4
3030
with:
3131
node-version: ${{ matrix.node-version }}
3232

README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const store = new OSSObject({
159159
accessKeyId: 'your access key',
160160
accessKeySecret: 'your access secret',
161161
bucket: 'your bucket name',
162-
region: 'oss-cn-hangzhou'
162+
region: 'oss-cn-hangzhou',
163163
});
164164
```
165165

@@ -175,20 +175,23 @@ const store = new OSSObject({
175175
accessKeyId: 'your access key',
176176
accessKeySecret: 'your access secret',
177177
bucket: 'your bucket name',
178-
endpoint: 'oss-accelerate.aliyuncs.com',
178+
endpoint: 'https://oss-accelerate.aliyuncs.com',
179179
});
180180
```
181181

182182
3. use custom domain
183183

184+
See https://help.aliyun.com/zh/oss/user-guide/map-custom-domain-names-5
185+
184186
```js
185187
const { OSSObject } = require('oss-client');
186188

187189
const store = new OSSObject({
188190
accessKeyId: 'your access key',
189191
accessKeySecret: 'your access secret',
190192
cname: true,
191-
endpoint: 'your custome domain',
193+
// your custom domain endpoint
194+
endpoint: 'https://my-static.domain.com',
192195
});
193196
```
194197

@@ -1629,19 +1632,8 @@ RequestError | -1 | network error | 网络出现中断或异常
16291632
ConnectionTimeoutError | -2 | request connect timeout | 请求连接超时
16301633
SecurityTokenExpired | 403 | sts Security Token Expired | sts Security Token 超时失效
16311634
1632-
<!-- GITCONTRIBUTOR_START -->
1633-
16341635
## Contributors
16351636
1636-
|[<img src="https://avatars.githubusercontent.com/u/1611647?v=4" width="100px;"/><br/><sub><b>PeterRao</b></sub>](https://github.com/PeterRao)<br/>|[<img src="https://avatars.githubusercontent.com/u/1209779?v=4" width="100px;"/><br/><sub><b>rockuw</b></sub>](https://github.com/rockuw)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/24582204?v=4" width="100px;"/><br/><sub><b>taotao7</b></sub>](https://github.com/taotao7)<br/>|[<img src="https://avatars.githubusercontent.com/u/19388725?v=4" width="100px;"/><br/><sub><b>weiyie</b></sub>](https://github.com/weiyie)<br/>|
1637-
| :---: | :---: | :---: | :---: | :---: | :---: |
1638-
|[<img src="https://avatars.githubusercontent.com/u/5886725?v=4" width="100px;"/><br/><sub><b>binghaiwang</b></sub>](https://github.com/binghaiwang)<br/>|[<img src="https://avatars.githubusercontent.com/u/14790466?v=4" width="100px;"/><br/><sub><b>greenkeeperio-bot</b></sub>](https://github.com/greenkeeperio-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/5653578?v=4" width="100px;"/><br/><sub><b>luozhang002</b></sub>](https://github.com/luozhang002)<br/>|[<img src="https://avatars.githubusercontent.com/u/17213005?v=4" width="100px;"/><br/><sub><b>beajer</b></sub>](https://github.com/beajer)<br/>|[<img src="https://avatars.githubusercontent.com/u/17476119?v=4" width="100px;"/><br/><sub><b>mars-coder</b></sub>](https://github.com/mars-coder)<br/>|[<img src="https://avatars.githubusercontent.com/u/4757625?v=4" width="100px;"/><br/><sub><b>duan007a</b></sub>](https://github.com/duan007a)<br/>|
1639-
|[<img src="https://avatars.githubusercontent.com/u/33024188?v=4" width="100px;"/><br/><sub><b>Ari1c</b></sub>](https://github.com/Ari1c)<br/>|[<img src="https://avatars.githubusercontent.com/u/20496894?v=4" width="100px;"/><br/><sub><b>Pedestrian93</b></sub>](https://github.com/Pedestrian93)<br/>|[<img src="https://avatars.githubusercontent.com/u/13463883?v=4" width="100px;"/><br/><sub><b>microJ</b></sub>](https://github.com/microJ)<br/>|[<img src="https://avatars.githubusercontent.com/u/19716675?v=4" width="100px;"/><br/><sub><b>aloisklink</b></sub>](https://github.com/aloisklink)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|
1640-
|[<img src="https://avatars.githubusercontent.com/u/12809715?v=4" width="100px;"/><br/><sub><b>1019272778</b></sub>](https://github.com/1019272778)<br/>|[<img src="https://avatars.githubusercontent.com/u/863754?v=4" width="100px;"/><br/><sub><b>zensh</b></sub>](https://github.com/zensh)<br/>|[<img src="https://avatars.githubusercontent.com/u/340282?v=4" width="100px;"/><br/><sub><b>fool2fish</b></sub>](https://github.com/fool2fish)<br/>|[<img src="https://avatars.githubusercontent.com/u/8952081?v=4" width="100px;"/><br/><sub><b>AviVahl</b></sub>](https://github.com/AviVahl)<br/>|[<img src="https://avatars.githubusercontent.com/u/5139554?v=4" width="100px;"/><br/><sub><b>danielwpz</b></sub>](https://github.com/danielwpz)<br/>|[<img src="https://avatars.githubusercontent.com/u/19678627?v=4" width="100px;"/><br/><sub><b>tianniu0106</b></sub>](https://github.com/tianniu0106)<br/>|
1641-
|[<img src="https://avatars.githubusercontent.com/u/327019?v=4" width="100px;"/><br/><sub><b>JacksonTian</b></sub>](https://github.com/JacksonTian)<br/>|[<img src="https://avatars.githubusercontent.com/u/440651?v=4" width="100px;"/><br/><sub><b>jackytck</b></sub>](https://github.com/jackytck)<br/>|[<img src="https://avatars.githubusercontent.com/u/3422918?v=4" width="100px;"/><br/><sub><b>leoliew</b></sub>](https://github.com/leoliew)<br/>|[<img src="https://avatars.githubusercontent.com/u/103457?v=4" width="100px;"/><br/><sub><b>lfeng</b></sub>](https://github.com/lfeng)<br/>|[<img src="https://avatars.githubusercontent.com/u/19733683?v=4" width="100px;"/><br/><sub><b>snyk-bot</b></sub>](https://github.com/snyk-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/30425185?v=4" width="100px;"/><br/><sub><b>yupeng-yuxiaoyu</b></sub>](https://github.com/yupeng-yuxiaoyu)<br/>|
1642-
|[<img src="https://avatars.githubusercontent.com/u/99770068?v=4" width="100px;"/><br/><sub><b>sartoshi-foot-dao</b></sub>](https://github.com/sartoshi-foot-dao)<br/>|[<img src="https://avatars.githubusercontent.com/u/5172747?v=4" width="100px;"/><br/><sub><b>chay-xu</b></sub>](https://github.com/chay-xu)<br/>|[<img src="https://avatars.githubusercontent.com/u/4565306?v=4" width="100px;"/><br/><sub><b>chunpu</b></sub>](https://github.com/chunpu)<br/>|[<img src="https://avatars.githubusercontent.com/in/29110?v=4" width="100px;"/><br/><sub><b>dependabot[bot]</b></sub>](https://github.com/apps/dependabot)<br/>|[<img src="https://avatars.githubusercontent.com/u/7284558?v=4" width="100px;"/><br/><sub><b>duncup</b></sub>](https://github.com/duncup)<br/>|[<img src="https://avatars.githubusercontent.com/u/416060?v=4" width="100px;"/><br/><sub><b>qin</b></sub>](https://github.com/qin)<br/>|
1643-
[<img src="https://avatars.githubusercontent.com/u/17328761?v=4" width="100px;"/><br/><sub><b>rdwh</b></sub>](https://github.com/rdwh)<br/>|[<img src="https://avatars.githubusercontent.com/u/25103518?v=4" width="100px;"/><br/><sub><b>richex-cn</b></sub>](https://github.com/richex-cn)<br/>|[<img src="https://avatars.githubusercontent.com/u/45714422?v=4" width="100px;"/><br/><sub><b>hengshanMWC</b></sub>](https://github.com/hengshanMWC)<br/>
1644-
1645-
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat Sep 16 2023 01:02:55 GMT+0800`.
1637+
[![Contributors](https://contrib.rocks/image?repo=node-modules/oss-client)](https://github.com/node-modules/oss-client/graphs/contributors)
16461638
1647-
<!-- GITCONTRIBUTOR_END -->
1639+
Made with [contributors-img](https://contrib.rocks).

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "oss-client",
33
"version": "2.1.0",
4+
"engines": {
5+
"node": ">= 16.0.0"
6+
},
47
"description": "Aliyun OSS(Object Storage Service) Node.js Client",
58
"scripts": {
69
"lint": "eslint src test --ext .ts",
710
"test": "egg-bin test",
811
"test-local": "egg-bin test",
912
"cov": "egg-bin cov",
10-
"ci": "npm run lint && npm run cov && npm run prepublishOnly",
11-
"contributor": "git-contributor",
13+
"ci": "npm run lint && npm run cov && npm run prepublishOnly && attw --pack",
1214
"prepublishOnly": "tshy && tshy-after"
1315
},
1416
"repository": {
@@ -25,9 +27,6 @@
2527
"bugs": {
2628
"url": "https://github.com/node-modules/oss-client/issues"
2729
},
28-
"engines": {
29-
"node": ">= 16.0.0"
30-
},
3130
"homepage": "https://github.com/node-modules/oss-client",
3231
"dependencies": {
3332
"is-type-of": "^2.0.1",
@@ -40,6 +39,7 @@
4039
"xml2js": "^0.6.2"
4140
},
4241
"devDependencies": {
42+
"@arethetypeswrong/cli": "^0.15.3",
4343
"@eggjs/tsconfig": "^1.1.0",
4444
"@types/mime": "^3.0.1",
4545
"@types/mocha": "^10.0.1",
@@ -49,12 +49,10 @@
4949
"egg-bin": "^6.4.1",
5050
"eslint": "^8.25.0",
5151
"eslint-config-egg": "^13.0.0",
52-
"git-contributor": "^2.1.5",
5352
"tshy": "^1.0.0",
5453
"tshy-after": "^1.0.0",
5554
"typescript": "^5.2.2"
5655
},
57-
"typings": "./dist/esm/index.d.ts",
5856
"files": [
5957
"dist",
6058
"src"
@@ -70,14 +68,17 @@
7068
"./package.json": "./package.json",
7169
".": {
7270
"import": {
71+
"source": "./src/index.ts",
7372
"types": "./dist/esm/index.d.ts",
7473
"default": "./dist/esm/index.js"
7574
},
7675
"require": {
76+
"source": "./src/index.ts",
7777
"types": "./dist/commonjs/index.d.ts",
7878
"default": "./dist/commonjs/index.js"
7979
}
8080
}
8181
},
82-
"types": "./dist/commonjs/index.d.ts"
82+
"types": "./dist/commonjs/index.d.ts",
83+
"main": "./dist/commonjs/index.js"
8384
}

src/OSSObject.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,36 @@ import {
6464
} from './util/index.js';
6565

6666
export interface OSSObjectClientInitOptions extends OSSBaseClientInitOptions {
67-
bucket: string;
67+
bucket?: string;
68+
/**
69+
* Enable cname
70+
* @see https://help.aliyun.com/zh/oss/user-guide/map-custom-domain-names-5
71+
*/
72+
cname?: boolean;
6873
}
6974

7075
export class OSSObject extends OSSBaseClient implements IObjectSimple {
7176
#bucket: string;
7277
#bucketEndpoint: string;
7378

7479
constructor(options: OSSObjectClientInitOptions) {
75-
checkBucketName(options.bucket);
80+
if (!options.cname) {
81+
assert(options.bucket, 'bucket required');
82+
}
83+
if (options.bucket) {
84+
checkBucketName(options.bucket);
85+
}
7686
super(options);
77-
this.#bucket = options.bucket;
78-
const urlObject = new URL(this.options.endpoint);
79-
urlObject.hostname = `${this.#bucket}.${urlObject.hostname}`;
80-
this.#bucketEndpoint = urlObject.toString();
87+
if (options.cname) {
88+
// ignore bucket on cname set to true
89+
this.#bucket = '';
90+
this.#bucketEndpoint = this.options.endpoint;
91+
} else {
92+
this.#bucket = options.bucket!;
93+
const urlObject = new URL(this.options.endpoint);
94+
urlObject.hostname = `${this.#bucket}.${urlObject.hostname}`;
95+
this.#bucketEndpoint = urlObject.toString();
96+
}
8197
}
8298

8399
/** public methods */
@@ -882,9 +898,11 @@ export class OSSObject extends OSSBaseClient implements IObjectSimple {
882898
bucketName = sourceName.replace(/\/(.+?)(\/.*)/, '$1');
883899
sourceName = sourceName.replace(/(\/.+?\/)(.*)/, '$2');
884900
}
885-
checkBucketName(bucketName);
886901
sourceName = encodeURIComponent(sourceName);
887-
sourceName = `/${bucketName}/${sourceName}`;
902+
if (bucketName) {
903+
checkBucketName(bucketName);
904+
sourceName = `/${bucketName}/${sourceName}`;
905+
}
888906
return sourceName;
889907
}
890908

test/OSSObject.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,4 +2296,25 @@ describe('test/OSSObject.test.ts', () => {
22962296
assert.equal(result.status, 200);
22972297
});
22982298
});
2299+
2300+
describe('options.cname = true', () => {
2301+
it('should work', async () => {
2302+
const ossObject = new OSSObject({
2303+
...config.oss,
2304+
cname: true,
2305+
endpoint: 'https://foo.bar.com',
2306+
});
2307+
const url = ossObject.signatureUrl('foo.jpg');
2308+
assert.match(url, /https:\/\/foo\.bar\.com\/foo\.jpg\?OSSAccessKeyId=/);
2309+
});
2310+
2311+
it('should throw error when bucket empty', async () => {
2312+
assert.throws(() => {
2313+
new OSSObject({
2314+
...config.oss,
2315+
bucket: '',
2316+
});
2317+
}, /bucket required/);
2318+
});
2319+
});
22992320
});

0 commit comments

Comments
 (0)