@kne/fastify-aliyun 1.0.0
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/index.js +42 -0
- package/libs/controllers.js +33 -0
- package/libs/services.js +63 -0
- package/package.json +61 -0
package/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
const merge = require('lodash/merge');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const packageJson = require('./package.json');
|
|
5
|
+
const NodeCache = require('node-cache');
|
|
6
|
+
|
|
7
|
+
module.exports = fp(
|
|
8
|
+
async (fastify, options) => {
|
|
9
|
+
const cache = new NodeCache();
|
|
10
|
+
options = merge(
|
|
11
|
+
{
|
|
12
|
+
nls: {
|
|
13
|
+
token: {
|
|
14
|
+
endpoint: 'http://nls-meta.cn-shanghai.aliyuncs.com',
|
|
15
|
+
apiVersion: '2019-02-28'
|
|
16
|
+
},
|
|
17
|
+
tts: {
|
|
18
|
+
endpoint: 'http://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts'
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
prefix: `/api/v${packageJson.version.split('.')[0]}/aliyun`,
|
|
22
|
+
createAuthenticate: () => {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
options,
|
|
27
|
+
{ cache }
|
|
28
|
+
);
|
|
29
|
+
fastify.register(require('@kne/fastify-namespace'), {
|
|
30
|
+
name: 'aliyun',
|
|
31
|
+
options,
|
|
32
|
+
modules: [
|
|
33
|
+
['controllers', path.resolve(__dirname, './libs/controllers.js')],
|
|
34
|
+
['services', path.resolve(__dirname, './libs/services.js')]
|
|
35
|
+
]
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'fastify-aliyun',
|
|
40
|
+
dependencies: ['fastify-file-manager']
|
|
41
|
+
}
|
|
42
|
+
);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
module.exports = fp(async (fastify, options) => {
|
|
3
|
+
fastify.post(
|
|
4
|
+
`${options.prefix}/nls/getToken`,
|
|
5
|
+
{
|
|
6
|
+
onRequest: options.createAuthenticate('nls:getToken')
|
|
7
|
+
},
|
|
8
|
+
async () => {
|
|
9
|
+
const { services } = fastify.aliyun;
|
|
10
|
+
return services.getToken();
|
|
11
|
+
}
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
fastify.post(
|
|
15
|
+
`${options.prefix}/nls/tts`,
|
|
16
|
+
{
|
|
17
|
+
onRequest: options.createAuthenticate('nls:tts'),
|
|
18
|
+
schema: {
|
|
19
|
+
body: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
required: ['text'],
|
|
22
|
+
properties: {
|
|
23
|
+
text: { type: 'string' }
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
async request => {
|
|
29
|
+
const { services } = fastify.aliyun;
|
|
30
|
+
return await services.tts(request.body);
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
});
|
package/libs/services.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
const { RPCClient } = require('@alicloud/pop-core');
|
|
3
|
+
const fetch = require('node-fetch');
|
|
4
|
+
const fileType = require('file-type');
|
|
5
|
+
module.exports = fp(async (fastify, options) => {
|
|
6
|
+
const getToken = async () => {
|
|
7
|
+
if (options.cache.has('nls:token')) {
|
|
8
|
+
const Token = options.cache.get('nls:token');
|
|
9
|
+
return { token: Token.Id };
|
|
10
|
+
}
|
|
11
|
+
const client = new RPCClient({
|
|
12
|
+
accessKeyId: options.nls.token.accessKeyId,
|
|
13
|
+
accessKeySecret: options.nls.token.accessKeySecret,
|
|
14
|
+
endpoint: options.nls.token.endpoint,
|
|
15
|
+
apiVersion: options.nls.token.apiVersion
|
|
16
|
+
});
|
|
17
|
+
const { Token } = await client.request('CreateToken');
|
|
18
|
+
options.cache.set('nls:token', Token, Token.expireTime * 1000 - Date.now());
|
|
19
|
+
return { token: Token.Id };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const tts = async ({ text }) => {
|
|
23
|
+
const { token } = await getToken();
|
|
24
|
+
const response = await fetch(options.nls.tts.endpoint, {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'application/json'
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify({
|
|
30
|
+
appkey: options.nls.appKey,
|
|
31
|
+
text: text,
|
|
32
|
+
token: token,
|
|
33
|
+
format: 'wav'
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
console.log(
|
|
38
|
+
JSON.stringify({
|
|
39
|
+
appkey: options.nls.appkey,
|
|
40
|
+
text: text,
|
|
41
|
+
token: token,
|
|
42
|
+
format: 'wav'
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
const buffer = await response.buffer();
|
|
46
|
+
const type = await fileType.fromBuffer(buffer);
|
|
47
|
+
if (!type) {
|
|
48
|
+
const errorMsg = (() => {
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(buffer.toString())?.message || '请求发生错误';
|
|
51
|
+
} catch (e) {
|
|
52
|
+
console.error(e);
|
|
53
|
+
return '请求发生错误';
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
throw new Error(errorMsg);
|
|
57
|
+
}
|
|
58
|
+
return await fastify.fileManager.services.fileRecord.uploadToFileSystem({
|
|
59
|
+
file: { toBuffer: () => buffer, filename: `response.${type.ext}`, mimetype: type.mime }
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
fastify.aliyun.services = { getToken, tts };
|
|
63
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kne/fastify-aliyun",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "对接阿里云服务",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"init": "husky",
|
|
8
|
+
"dev": "nodemon ./index.js",
|
|
9
|
+
"start": "node ./index.js",
|
|
10
|
+
"doc": "node ./open-api.js",
|
|
11
|
+
"build:md": "npx @kne/md-doc",
|
|
12
|
+
"start:md": "npx @kne/md-doc --watch",
|
|
13
|
+
"prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
|
|
14
|
+
"lint-staged": "npx lint-staged"
|
|
15
|
+
},
|
|
16
|
+
"lint-staged": {
|
|
17
|
+
"**/*.md": [
|
|
18
|
+
"git add"
|
|
19
|
+
],
|
|
20
|
+
"{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}": [
|
|
21
|
+
"prettier --config .prettierrc --write",
|
|
22
|
+
"git add"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"index.js",
|
|
27
|
+
"libs"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/kne-union/fastify-aliyun.git"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [],
|
|
34
|
+
"author": "linzp",
|
|
35
|
+
"license": "ISC",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/kne-union/fastify-aliyun/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/kne-union/fastify-aliyun#readme",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@alicloud/pop-core": "^1.7.13",
|
|
42
|
+
"@kne/fastify-namespace": "^0.1.2",
|
|
43
|
+
"file-type": "^16.5.4",
|
|
44
|
+
"lodash": "^4.17.21",
|
|
45
|
+
"node-cache": "^5.1.2",
|
|
46
|
+
"node-fetch": "^2.7.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@fastify/env": "^4.4.0",
|
|
50
|
+
"@fastify/swagger": "^8.14.0",
|
|
51
|
+
"@kne/fastify-file-manager": "^1.1.3",
|
|
52
|
+
"@kne/fastify-response-data-format": "^0.1.2",
|
|
53
|
+
"@kne/fastify-sequelize": "^2.0.5",
|
|
54
|
+
"fastify": "^4.27.0",
|
|
55
|
+
"fastify-plugin": "^4.5.1",
|
|
56
|
+
"husky": "^9.0.11",
|
|
57
|
+
"nodemon": "^3.1.3",
|
|
58
|
+
"prettier": "^3.2.5",
|
|
59
|
+
"sqlite3": "^5.1.7"
|
|
60
|
+
}
|
|
61
|
+
}
|