Skip to content

Commit

Permalink
feat: support cname (#19)
Browse files Browse the repository at this point in the history
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 -->
  • Loading branch information
fengmk2 authored Jun 25, 2024
1 parent 8ac8953 commit 8c64904
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:

steps:
- name: Checkout Git Source
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

Expand Down
24 changes: 8 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const store = new OSSObject({
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name',
region: 'oss-cn-hangzhou'
region: 'oss-cn-hangzhou',
});
```

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

3. use custom domain

See https://help.aliyun.com/zh/oss/user-guide/map-custom-domain-names-5

```js
const { OSSObject } = require('oss-client');

const store = new OSSObject({
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
cname: true,
endpoint: 'your custome domain',
// your custom domain endpoint
endpoint: 'https://my-static.domain.com',
});
```

Expand Down Expand Up @@ -1629,19 +1632,8 @@ RequestError | -1 | network error | 网络出现中断或异常
ConnectionTimeoutError | -2 | request connect timeout | 请求连接超时
SecurityTokenExpired | 403 | sts Security Token Expired | sts Security Token 超时失效
<!-- GITCONTRIBUTOR_START -->
## Contributors
|[<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/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<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/>|
|[<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/>|
|[<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/>|
|[<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/>|
|[<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/>|
[<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/>

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`.
[![Contributors](https://contrib.rocks/image?repo=node-modules/oss-client)](https://github.com/node-modules/oss-client/graphs/contributors)
<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "oss-client",
"version": "2.1.0",
"engines": {
"node": ">= 16.0.0"
},
"description": "Aliyun OSS(Object Storage Service) Node.js Client",
"scripts": {
"lint": "eslint src test --ext .ts",
"test": "egg-bin test",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run lint && npm run cov && npm run prepublishOnly",
"contributor": "git-contributor",
"ci": "npm run lint && npm run cov && npm run prepublishOnly && attw --pack",
"prepublishOnly": "tshy && tshy-after"
},
"repository": {
Expand All @@ -25,9 +27,6 @@
"bugs": {
"url": "https://github.com/node-modules/oss-client/issues"
},
"engines": {
"node": ">= 16.0.0"
},
"homepage": "https://github.com/node-modules/oss-client",
"dependencies": {
"is-type-of": "^2.0.1",
Expand All @@ -40,6 +39,7 @@
"xml2js": "^0.6.2"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@eggjs/tsconfig": "^1.1.0",
"@types/mime": "^3.0.1",
"@types/mocha": "^10.0.1",
Expand All @@ -49,12 +49,10 @@
"egg-bin": "^6.4.1",
"eslint": "^8.25.0",
"eslint-config-egg": "^13.0.0",
"git-contributor": "^2.1.5",
"tshy": "^1.0.0",
"tshy-after": "^1.0.0",
"typescript": "^5.2.2"
},
"typings": "./dist/esm/index.d.ts",
"files": [
"dist",
"src"
Expand All @@ -70,14 +68,17 @@
"./package.json": "./package.json",
".": {
"import": {
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"types": "./dist/commonjs/index.d.ts"
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js"
}
34 changes: 26 additions & 8 deletions src/OSSObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,36 @@ import {
} from './util/index.js';

export interface OSSObjectClientInitOptions extends OSSBaseClientInitOptions {
bucket: string;
bucket?: string;
/**
* Enable cname
* @see https://help.aliyun.com/zh/oss/user-guide/map-custom-domain-names-5
*/
cname?: boolean;
}

export class OSSObject extends OSSBaseClient implements IObjectSimple {
#bucket: string;
#bucketEndpoint: string;

constructor(options: OSSObjectClientInitOptions) {
checkBucketName(options.bucket);
if (!options.cname) {
assert(options.bucket, 'bucket required');
}
if (options.bucket) {
checkBucketName(options.bucket);
}
super(options);
this.#bucket = options.bucket;
const urlObject = new URL(this.options.endpoint);
urlObject.hostname = `${this.#bucket}.${urlObject.hostname}`;
this.#bucketEndpoint = urlObject.toString();
if (options.cname) {
// ignore bucket on cname set to true
this.#bucket = '';
this.#bucketEndpoint = this.options.endpoint;
} else {
this.#bucket = options.bucket!;
const urlObject = new URL(this.options.endpoint);
urlObject.hostname = `${this.#bucket}.${urlObject.hostname}`;
this.#bucketEndpoint = urlObject.toString();
}
}

/** public methods */
Expand Down Expand Up @@ -882,9 +898,11 @@ export class OSSObject extends OSSBaseClient implements IObjectSimple {
bucketName = sourceName.replace(/\/(.+?)(\/.*)/, '$1');
sourceName = sourceName.replace(/(\/.+?\/)(.*)/, '$2');
}
checkBucketName(bucketName);
sourceName = encodeURIComponent(sourceName);
sourceName = `/${bucketName}/${sourceName}`;
if (bucketName) {
checkBucketName(bucketName);
sourceName = `/${bucketName}/${sourceName}`;
}
return sourceName;
}

Expand Down
21 changes: 21 additions & 0 deletions test/OSSObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2296,4 +2296,25 @@ describe('test/OSSObject.test.ts', () => {
assert.equal(result.status, 200);
});
});

describe('options.cname = true', () => {
it('should work', async () => {
const ossObject = new OSSObject({
...config.oss,
cname: true,
endpoint: 'https://foo.bar.com',
});
const url = ossObject.signatureUrl('foo.jpg');
assert.match(url, /https:\/\/foo\.bar\.com\/foo\.jpg\?OSSAccessKeyId=/);
});

it('should throw error when bucket empty', async () => {
assert.throws(() => {
new OSSObject({
...config.oss,
bucket: '',
});
}, /bucket required/);
});
});
});

0 comments on commit 8c64904

Please sign in to comment.