diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fc6dff2b..32e7299c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,10 +4,30 @@ Fork [minio-js upstream](https://github.com/minio/minio-js/fork) source reposito ```bash $ git clone https://github.com/$USER_ID/minio-js $ cd minio-js +``` + +### Install npm dependencies + +```bash $ npm install -$ npm test -$ npm build -... +``` + +### Format code (with prettier) + +```shell +$ npm run format +``` + +### Check Code Style + +```shell +$ npm run lint +``` + +### Tests + +```shell +$ npm run test ``` ### Developer Guidelines diff --git a/README.md b/README.md index 6acaba26..955fb08d 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,7 @@ npm install -g ## Using with TypeScript -```sh -npm install --save-dev @types/minio -``` +`minio>7.1.0` is shipped with builtin type definition, `@types/minio` is no longer needed. ## Initialize MinIO Client @@ -244,5 +242,4 @@ The full API Reference is available here. [Contributors Guide](https://github.com/minio/minio-js/blob/master/CONTRIBUTING.md) -[![Build Status](https://travis-ci.org/minio/minio-js.svg)](https://travis-ci.org/minio/minio-js) -[![Build status](https://ci.appveyor.com/api/projects/status/1d05e6nvxcelmrak?svg=true)](https://ci.appveyor.com/project/harshavardhana/minio-js) +![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/minio/minio-js/nodejs.yml) diff --git a/README_zh_CN.md b/README_zh_CN.md index 6ed8d65d..f560b1cd 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -10,9 +10,7 @@ MinIO JavaScript Client SDK提供简单的API来访问任何Amazon S3兼容的 ## 使用NPM下载 -```sh -npm install --save minio -``` +`minio>7.1.0` 拥有自带的类型定义,不再需要安装 `@types/minio`。 ## 下载并安装源码 diff --git a/docs/zh_CN/CONTRIBUTING.md b/docs/zh_CN/CONTRIBUTING.md index 02a3e772..dc29d413 100644 --- a/docs/zh_CN/CONTRIBUTING.md +++ b/docs/zh_CN/CONTRIBUTING.md @@ -1,14 +1,35 @@ ### 设置你的minio-js Github仓库 Fork [minio-js upstream](https://github.com/minio/minio-js/fork) 源码仓库到你的个人仓库。 -MinIO Javascript使用[gulp](http://gulpjs.com/)来管理它的依赖。 +### 克隆代码 ```bash $ git clone https://github.com/$USER_ID/minio-js $ cd minio-js +``` + +### 安装依赖 + +```bash $ npm install -$ gulp -... +``` + +### 格式化代码 + +```shell +$ npm run format +``` + +### 检查代码风格 + +```shell +$ npm run lint +``` + +### 运行测试 + +```shell +$ npm run test ``` ### 开发者指南 diff --git a/src/AssumeRoleProvider.ts b/src/AssumeRoleProvider.ts index 11c474f4..71a59525 100644 --- a/src/AssumeRoleProvider.ts +++ b/src/AssumeRoleProvider.ts @@ -50,7 +50,7 @@ export interface AssumeRoleProviderOptions { transportAgent?: http.Agent } -const defaultExpiry = 900 +const defaultExpirySeconds = 900 export class AssumeRoleProvider extends CredentialProvider { private readonly stsEndpoint: URL @@ -77,7 +77,7 @@ export class AssumeRoleProvider extends CredentialProvider { stsEndpoint, accessKey, secretKey, - durationSeconds = defaultExpiry, + durationSeconds = defaultExpirySeconds, sessionToken, policy, region = '', @@ -106,8 +106,8 @@ export class AssumeRoleProvider extends CredentialProvider { this.durationSeconds = parseInt(durationSeconds as unknown as string) let expirySeconds = this.durationSeconds - if (this.durationSeconds < defaultExpiry) { - expirySeconds = defaultExpiry + if (this.durationSeconds < defaultExpirySeconds) { + expirySeconds = defaultExpirySeconds } this.expirySeconds = expirySeconds // for calculating refresh of credentials. @@ -241,13 +241,12 @@ export class AssumeRoleProvider extends CredentialProvider { } async getCredentials(): Promise { - let credConfig: Credentials | null - if (!this._credentials || (this._credentials && this.isAboutToExpire())) { - credConfig = await this.refreshCredentials() - } else { - credConfig = this._credentials + if (this._credentials && !this.isAboutToExpire()) { + return this._credentials } - return credConfig + + this._credentials = await this.refreshCredentials() + return this._credentials } isAboutToExpire() { diff --git a/src/internal/request.ts b/src/internal/request.ts index cb4c6190..ceb071c0 100644 --- a/src/internal/request.ts +++ b/src/internal/request.ts @@ -1,9 +1,12 @@ import type * as http from 'node:http' import type * as https from 'node:https' import type * as stream from 'node:stream' +import { pipeline } from 'node:stream' + +import type { Transport } from './type.ts' export async function request( - transport: typeof http | typeof https, + transport: Transport, opt: https.RequestOptions, body: Buffer | string | stream.Readable | null = null, ): Promise { @@ -12,25 +15,21 @@ export async function request( resolve(resp) }) - requestObj.on('error', (e: unknown) => { - reject(e) - }) - - if (!body) { - requestObj.end(null) - return - } + if (!body || Buffer.isBuffer(body) || typeof body === 'string') { + requestObj + .on('error', (e: unknown) => { + reject(e) + }) + .end(body) - if (Buffer.isBuffer(body) || typeof body === 'string') { - requestObj.end(body) return } - // stream.Readable - body.pipe(requestObj) - body.on('error', (err) => { - requestObj.destroy(err) - reject(err) + // pump readable stream + pipeline(body, requestObj, (err) => { + if (err) { + reject(err) + } }) }) } diff --git a/src/internal/response.ts b/src/internal/response.ts index 22ff15a3..bb3a0b15 100644 --- a/src/internal/response.ts +++ b/src/internal/response.ts @@ -1,3 +1,4 @@ +import type http from 'node:http' import type stream from 'node:stream' export async function readAsBuffer(res: stream.Readable): Promise { @@ -10,7 +11,7 @@ export async function readAsBuffer(res: stream.Readable): Promise { }) } -export async function readAsString(res: stream.Readable): Promise { +export async function readAsString(res: http.IncomingMessage): Promise { const body = await readAsBuffer(res) return body.toString() } diff --git a/src/signing.ts b/src/signing.ts index bacd0158..a4bb7015 100644 --- a/src/signing.ts +++ b/src/signing.ts @@ -255,7 +255,7 @@ export function presignSignatureV4( request: IRequest, accessKey: string, secretKey: string, - sessionToken: string, + sessionToken: string | undefined, region: string, requestDate: Date, expires: number,