Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support cname #19

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify the type of region in the documentation.

The region parameter is used in the initialization of OSSObject, but its type isn't specified in the documentation. This could lead to confusion about what values are expected (string, object, etc.).

-  region: '<oss region>',
+  region: '<oss region>', // Type: string
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
region: 'oss-cn-hangzhou',
region: 'oss-cn-hangzhou', // Type: string

});
```

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',
Comment on lines +184 to +194
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the cname property is adequately explained.

The cname property is used in the example for custom domains, but there's no explanation of what it does or how it affects the behavior of the OSSObject. This could confuse users unfamiliar with the concept of CNAME in DNS settings.

+ // Set cname to true to use a custom domain name for bucket access.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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',
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, // Set cname to true to use a custom domain name for bucket access.
// your custom domain endpoint
endpoint: 'https://my-static.domain.com',
Tools
Markdownlint

184-184: null (MD034, no-bare-urls)
Bare URL used

});
```

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",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

No usage of @arethetypeswrong/cli found in npm scripts.

The dependency @arethetypeswrong/cli is listed in package.json, but it is not referenced in the npm scripts. Please ensure that this tool is integrated into the project's workflow if it is intended to be used.

  • package.json: Dependency listed but not used in scripts.
Analysis chain

Add new dependency @arethetypeswrong/cli.

The addition of @arethetypeswrong/cli to devDependencies suggests improvements in type checking or code quality tools. Confirm that this tool integrates well with existing workflows and that it is used in the scripts where necessary.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the usage of `@arethetypeswrong/cli` in npm scripts.

# Test: Search for usage in npm scripts.
grep -R '@arethetypeswrong/cli' package.json

Length of output: 84



Script:

#!/bin/bash
# Description: Verify the usage of `@arethetypeswrong/cli` in npm scripts.

# Extract and check the scripts section in package.json for any usage of @arethetypeswrong/cli.
jq '.scripts' package.json | grep '@arethetypeswrong/cli'

Length of output: 55

"@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/);
});
});
});
Loading