From 1a6b9a1cbae34beba44a7d40b8b7bfef5b014ff1 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 25 Jun 2024 21:42:02 +0800 Subject: [PATCH 1/2] feat: support cname closes https://github.com/node-modules/oss-client/issues/18 https://help.aliyun.com/zh/oss/user-guide/map-custom-domain-names-5 --- .github/workflows/nodejs.yml | 6 +++--- README.md | 24 ++++++++---------------- package.json | 17 +++++++++-------- src/OSSObject.ts | 34 ++++++++++++++++++++++++++-------- test/OSSObject.test.ts | 21 +++++++++++++++++++++ 5 files changed, 67 insertions(+), 35 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 54c18c13b..72d597e94 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -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 }} @@ -48,6 +48,6 @@ jobs: ALI_SDK_STS_ENDPOINT: ${{ secrets.ALI_SDK_STS_ENDPOINT }} - name: Code Coverage - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/README.md b/README.md index f457077a0..cdde01791 100644 --- a/README.md +++ b/README.md @@ -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', }); ``` @@ -175,12 +175,14 @@ 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'); @@ -188,7 +190,8 @@ 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', }); ``` @@ -1629,19 +1632,8 @@ RequestError | -1 | network error | 网络出现中断或异常 ConnectionTimeoutError | -2 | request connect timeout | 请求连接超时 SecurityTokenExpired | 403 | sts Security Token Expired | sts Security Token 超时失效 - - ## Contributors -|[
PeterRao](https://github.com/PeterRao)
|[
rockuw](https://github.com/rockuw)
|[
fengmk2](https://github.com/fengmk2)
|[
dead-horse](https://github.com/dead-horse)
|[
taotao7](https://github.com/taotao7)
|[
weiyie](https://github.com/weiyie)
| -| :---: | :---: | :---: | :---: | :---: | :---: | -|[
binghaiwang](https://github.com/binghaiwang)
|[
greenkeeperio-bot](https://github.com/greenkeeperio-bot)
|[
luozhang002](https://github.com/luozhang002)
|[
beajer](https://github.com/beajer)
|[
mars-coder](https://github.com/mars-coder)
|[
duan007a](https://github.com/duan007a)
| -|[
Ari1c](https://github.com/Ari1c)
|[
Pedestrian93](https://github.com/Pedestrian93)
|[
microJ](https://github.com/microJ)
|[
aloisklink](https://github.com/aloisklink)
|[
popomore](https://github.com/popomore)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
| -|[
1019272778](https://github.com/1019272778)
|[
zensh](https://github.com/zensh)
|[
fool2fish](https://github.com/fool2fish)
|[
AviVahl](https://github.com/AviVahl)
|[
danielwpz](https://github.com/danielwpz)
|[
tianniu0106](https://github.com/tianniu0106)
| -|[
JacksonTian](https://github.com/JacksonTian)
|[
jackytck](https://github.com/jackytck)
|[
leoliew](https://github.com/leoliew)
|[
lfeng](https://github.com/lfeng)
|[
snyk-bot](https://github.com/snyk-bot)
|[
yupeng-yuxiaoyu](https://github.com/yupeng-yuxiaoyu)
| -|[
sartoshi-foot-dao](https://github.com/sartoshi-foot-dao)
|[
chay-xu](https://github.com/chay-xu)
|[
chunpu](https://github.com/chunpu)
|[
dependabot[bot]](https://github.com/apps/dependabot)
|[
duncup](https://github.com/duncup)
|[
qin](https://github.com/qin)
| -[
rdwh](https://github.com/rdwh)
|[
richex-cn](https://github.com/richex-cn)
|[
hengshanMWC](https://github.com/hengshanMWC)
- -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) - +Made with [contributors-img](https://contrib.rocks). diff --git a/package.json b/package.json index e98453e44..0e8f19ac4 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", @@ -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", @@ -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" @@ -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" } diff --git a/src/OSSObject.ts b/src/OSSObject.ts index a6af03b25..1996dcb23 100644 --- a/src/OSSObject.ts +++ b/src/OSSObject.ts @@ -64,7 +64,12 @@ 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 { @@ -72,12 +77,23 @@ export class OSSObject extends OSSBaseClient implements IObjectSimple { #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 */ @@ -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; } diff --git a/test/OSSObject.test.ts b/test/OSSObject.test.ts index 5aa25dda7..d185010f3 100644 --- a/test/OSSObject.test.ts +++ b/test/OSSObject.test.ts @@ -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/); + }); + }); }); From 0ceb0d63206c07ab28328938df11d636e8f68a87 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 25 Jun 2024 21:52:06 +0800 Subject: [PATCH 2/2] f --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 72d597e94..930d2f23b 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -48,6 +48,6 @@ jobs: ALI_SDK_STS_ENDPOINT: ${{ secrets.ALI_SDK_STS_ENDPOINT }} - name: Code Coverage - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }}