@kne/fastify-aliyun 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/libs/services/oss.js +6 -5
- package/package.json +8 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 linzp
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
|
|
2
|
+
# fastify-aliyun
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### 描述
|
|
6
|
+
|
|
7
|
+
对接阿里云服务
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 安装
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm i --save @kne/fastify-aliyun
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### 概述
|
|
18
|
+
|
|
19
|
+
### 项目简介
|
|
20
|
+
|
|
21
|
+
fastify-aliyun 是一个用于集成阿里云服务的 Fastify 插件,目前支持阿里云智能语音交互(NLS)和对象存储(OSS)服务。该插件提供了简单易用的API接口,帮助开发者快速集成阿里云服务到 Fastify 应用中。
|
|
22
|
+
|
|
23
|
+
### 功能特性
|
|
24
|
+
|
|
25
|
+
- **阿里云NLS服务集成**
|
|
26
|
+
- 自动管理和缓存访问令牌
|
|
27
|
+
- 支持文本转语音(TTS)功能
|
|
28
|
+
- 使用LRU缓存优化性能
|
|
29
|
+
|
|
30
|
+
- **阿里云OSS服务集成**
|
|
31
|
+
- 文件上传(支持文件和流)
|
|
32
|
+
- 文件下载
|
|
33
|
+
- 文件流获取
|
|
34
|
+
- 文件链接生成
|
|
35
|
+
|
|
36
|
+
### 安装方法
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install fastify-aliyun
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 配置说明
|
|
43
|
+
|
|
44
|
+
插件需要以下配置:
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
fastify.register(require('fastify-aliyun'), {
|
|
48
|
+
nls: {
|
|
49
|
+
appKey: 'your-nls-app-key',
|
|
50
|
+
token: {
|
|
51
|
+
accessKeyId: 'your-access-key-id',
|
|
52
|
+
accessKeySecret: 'your-access-key-secret'
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
oss: {
|
|
56
|
+
// OSS配置(可选)
|
|
57
|
+
region: 'oss-region',
|
|
58
|
+
accessKeyId: 'your-access-key-id',
|
|
59
|
+
accessKeySecret: 'your-access-key-secret',
|
|
60
|
+
bucket: 'your-bucket-name'
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 依赖插件
|
|
66
|
+
|
|
67
|
+
使用本插件前,需要安装和配置以下Fastify插件:
|
|
68
|
+
|
|
69
|
+
- @fastify/env - 环境变量管理
|
|
70
|
+
- @kne/fastify-sequelize - 数据库操作
|
|
71
|
+
- @kne/fastify-file-manager - 文件管理
|
|
72
|
+
- @kne/fastify-response-data-format - 响应数据格式化
|
|
73
|
+
|
|
74
|
+
### 使用示例
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
const fastify = require('fastify')({ logger: true });
|
|
78
|
+
|
|
79
|
+
// 配置环境变量
|
|
80
|
+
fastify.register(fastifyEnv, {
|
|
81
|
+
dotenv: true,
|
|
82
|
+
schema: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
required: ['NLS_APP_KEY', 'NLS_ACCESS_KEY_ID', 'NLS_ACCESS_KEY_SECRET'],
|
|
85
|
+
properties: {
|
|
86
|
+
NLS_APP_KEY: { type: 'string' },
|
|
87
|
+
NLS_ACCESS_KEY_ID: { type: 'string' },
|
|
88
|
+
NLS_ACCESS_KEY_SECRET: { type: 'string' }
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// 注册插件
|
|
94
|
+
fastify.register(require('fastify-plugin')(async (fastify) => {
|
|
95
|
+
await fastify.sequelize.sync();
|
|
96
|
+
fastify.register(require('fastify-aliyun'), {
|
|
97
|
+
nls: {
|
|
98
|
+
appKey: fastify.config.NLS_APP_KEY,
|
|
99
|
+
token: {
|
|
100
|
+
accessKeyId: fastify.config.NLS_ACCESS_KEY_ID,
|
|
101
|
+
accessKeySecret: fastify.config.NLS_ACCESS_KEY_SECRET
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}));
|
|
106
|
+
|
|
107
|
+
// 启动服务器
|
|
108
|
+
fastify.listen({ port: 3000 }, (err, address) => {
|
|
109
|
+
if (err) throw err;
|
|
110
|
+
// Server is now listening on ${address}
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
### 示例
|
|
116
|
+
|
|
117
|
+
#### 示例代码
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
### API
|
|
122
|
+
|
|
123
|
+
### API文档
|
|
124
|
+
|
|
125
|
+
本文档描述了fastify-aliyun插件提供的API接口。
|
|
126
|
+
|
|
127
|
+
### HTTP接口
|
|
128
|
+
|
|
129
|
+
#### NLS服务接口
|
|
130
|
+
|
|
131
|
+
| 接口路径 | 方法 | 说明 | 权限要求 |
|
|
132
|
+
|---------|-----|------|---------|
|
|
133
|
+
| /nls/getToken | GET | 获取NLS访问令牌 | 无 |
|
|
134
|
+
| /nls/tts | POST | 文本转语音 | 无 |
|
|
135
|
+
|
|
136
|
+
##### 获取NLS访问令牌
|
|
137
|
+
|
|
138
|
+
| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|
|
139
|
+
|-------|-----|------|-----|-------|
|
|
140
|
+
| 无参数 | - | - | - | - |
|
|
141
|
+
|
|
142
|
+
**响应示例**:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{
|
|
146
|
+
"code": 0,
|
|
147
|
+
"data": {
|
|
148
|
+
"token": "xxxxxxxx",
|
|
149
|
+
"expireTime": 1234567890
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
##### 文本转语音
|
|
155
|
+
|
|
156
|
+
| 参数名 | 说明 | 类型 | 必填 | 默认值 |
|
|
157
|
+
|-------|-----|------|-----|-------|
|
|
158
|
+
| text | 要转换的文本内容 | string | 是 | - |
|
|
159
|
+
| format | 音频格式 | string | 否 | mp3 |
|
|
160
|
+
| voice | 发音人 | string | 否 | xiaoyun |
|
|
161
|
+
| volume | 音量 | number | 否 | 50 |
|
|
162
|
+
| speech_rate | 语速 | number | 否 | 0 |
|
|
163
|
+
| pitch_rate | 语调 | number | 否 | 0 |
|
|
164
|
+
|
|
165
|
+
**响应示例**:
|
|
166
|
+
|
|
167
|
+
```json
|
|
168
|
+
{
|
|
169
|
+
"code": 0,
|
|
170
|
+
"data": {
|
|
171
|
+
"audio_file": "/path/to/audio/file.mp3"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 服务API
|
|
177
|
+
|
|
178
|
+
fastify-aliyun插件在Fastify实例上注册了`aliyun`命名空间,提供以下服务API:
|
|
179
|
+
|
|
180
|
+
#### NLS服务 (fastify.aliyun.nls)
|
|
181
|
+
|
|
182
|
+
| 方法名 | 说明 | 参数 | 返回值 |
|
|
183
|
+
|-------|------|------|-------|
|
|
184
|
+
| getToken | 获取NLS访问令牌 | 无 | Promise<{token, expireTime}> |
|
|
185
|
+
| tts | 文本转语音 | options: {text, format, voice, volume, speech_rate, pitch_rate} | Promise<{audio_file}> |
|
|
186
|
+
|
|
187
|
+
#### OSS服务 (fastify.aliyun.oss)
|
|
188
|
+
|
|
189
|
+
| 方法名 | 说明 | 参数 | 返回值 |
|
|
190
|
+
|-------|------|------|-------|
|
|
191
|
+
| createClient | 创建OSS客户端 | options: {region, accessKeyId, accessKeySecret, bucket} | OSS客户端实例 |
|
|
192
|
+
| uploadFile | 上传文件 | filePath, objectName, options | Promise<{url, name}> |
|
|
193
|
+
| uploadFileStream | 上传文件流 | fileStream, objectName, options | Promise<{url, name}> |
|
|
194
|
+
| downloadFile | 下载文件 | objectName, localPath | Promise<void> |
|
|
195
|
+
| getFileStream | 获取文件流 | objectName | Promise<Readable> |
|
|
196
|
+
| getFileLink | 获取文件链接 | objectName, options | string |
|
|
197
|
+
|
|
198
|
+
### 使用示例
|
|
199
|
+
|
|
200
|
+
#### 获取NLS令牌
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
fastify.get('/api/token', async (request, reply) => {
|
|
204
|
+
const tokenData = await fastify.aliyun.nls.getToken();
|
|
205
|
+
return { token: tokenData.token };
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### 文本转语音
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
fastify.post('/api/speak', async (request, reply) => {
|
|
213
|
+
const { text } = request.body;
|
|
214
|
+
const result = await fastify.aliyun.nls.tts({
|
|
215
|
+
text,
|
|
216
|
+
format: 'mp3',
|
|
217
|
+
voice: 'xiaoyun'
|
|
218
|
+
});
|
|
219
|
+
return { audioFile: result.audio_file };
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### 上传文件到OSS
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
fastify.post('/api/upload', async (request, reply) => {
|
|
227
|
+
const data = await request.file();
|
|
228
|
+
const result = await fastify.aliyun.oss.uploadFileStream(
|
|
229
|
+
data.file,
|
|
230
|
+
`uploads/${data.filename}`,
|
|
231
|
+
{ contentType: data.mimetype }
|
|
232
|
+
);
|
|
233
|
+
return { url: result.url };
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
#### 获取OSS文件链接
|
|
238
|
+
|
|
239
|
+
```javascript
|
|
240
|
+
fastify.get('/api/files/:filename', async (request, reply) => {
|
|
241
|
+
const { filename } = request.params;
|
|
242
|
+
const url = fastify.aliyun.oss.getFileLink(`uploads/${filename}`);
|
|
243
|
+
return { url };
|
|
244
|
+
});
|
|
245
|
+
```
|
|
246
|
+
|
package/libs/services/oss.js
CHANGED
|
@@ -29,16 +29,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
29
29
|
return result.content;
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
+
const getFileStream = async ({ filename }) => {
|
|
33
|
+
const client = createClient();
|
|
34
|
+
return client.getStream(`${options.oss.baseDir}/${filename}`);
|
|
35
|
+
};
|
|
36
|
+
|
|
32
37
|
const getFileLink = ({ filename, expires }) => {
|
|
33
38
|
const client = createClient();
|
|
34
39
|
return client.signatureUrl(`${options.oss.baseDir}/${filename}`, { expires: expires || 3600 });
|
|
35
40
|
};
|
|
36
41
|
|
|
37
42
|
services.oss = {
|
|
38
|
-
createClient,
|
|
39
|
-
uploadFile,
|
|
40
|
-
uploadFileStream,
|
|
41
|
-
getFileLink,
|
|
42
|
-
downloadFile
|
|
43
|
+
createClient, uploadFile, uploadFileStream, getFileLink, downloadFile, getFileStream
|
|
43
44
|
};
|
|
44
45
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-aliyun",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "对接阿里云服务",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -39,21 +39,26 @@
|
|
|
39
39
|
"homepage": "https://github.com/kne-union/fastify-aliyun#readme",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@alicloud/pop-core": "^1.7.13",
|
|
42
|
-
"@kne/fastify-namespace": "^0.1.2",
|
|
43
42
|
"ali-oss": "^6.20.0",
|
|
44
43
|
"file-type": "^16.5.4",
|
|
45
44
|
"lodash": "^4.17.21",
|
|
46
45
|
"lru-cache": "^11.0.0",
|
|
47
46
|
"node-fetch": "^2.7.0"
|
|
48
47
|
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@kne/fastify-namespace": "*",
|
|
50
|
+
"@kne/fastify-sequelize": "*",
|
|
51
|
+
"fastify-plugin": ">=5"
|
|
52
|
+
},
|
|
49
53
|
"devDependencies": {
|
|
50
54
|
"@fastify/env": "^4.4.0",
|
|
51
55
|
"@fastify/swagger": "^8.14.0",
|
|
52
56
|
"@kne/fastify-file-manager": "^1.1.3",
|
|
57
|
+
"@kne/fastify-namespace": "^0.1.2",
|
|
53
58
|
"@kne/fastify-response-data-format": "^0.1.2",
|
|
54
59
|
"@kne/fastify-sequelize": "^2.0.5",
|
|
55
60
|
"fastify": "^4.27.0",
|
|
56
|
-
"fastify-plugin": "^
|
|
61
|
+
"fastify-plugin": "^5.0.1",
|
|
57
62
|
"husky": "^9.0.11",
|
|
58
63
|
"nodemon": "^3.1.3",
|
|
59
64
|
"prettier": "^3.2.5",
|