@kne/fastify-aliyun 1.1.1 → 1.1.3

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 CHANGED
@@ -2,11 +2,11 @@ const fp = require('fastify-plugin');
2
2
  const merge = require('lodash/merge');
3
3
  const path = require('path');
4
4
  const packageJson = require('./package.json');
5
- const NodeCache = require('node-cache');
5
+ const { LRUCache } = require('lru-cache');
6
6
 
7
7
  module.exports = fp(
8
8
  async (fastify, options) => {
9
- const cache = new NodeCache();
9
+ const cache = new LRUCache({ max: 1000 });
10
10
  options = merge(
11
11
  {
12
12
  nls: {
@@ -2,12 +2,13 @@ const fp = require('fastify-plugin');
2
2
  const { RPCClient } = require('@alicloud/pop-core');
3
3
  const fetch = require('node-fetch');
4
4
  const fileType = require('file-type');
5
+ const { parse } = require('ali-oss/shims/url');
5
6
  module.exports = fp(async (fastify, options) => {
6
7
  const { services } = fastify.aliyun;
7
8
  const getToken = async () => {
8
9
  if (options.cache.has('nls:token')) {
9
10
  const Token = options.cache.get('nls:token');
10
- return { token: Token.Id };
11
+ return { token: Token.Id, appKey: options.nls.appKey };
11
12
  }
12
13
  const client = new RPCClient({
13
14
  accessKeyId: options.nls.token.accessKeyId,
@@ -16,8 +17,8 @@ module.exports = fp(async (fastify, options) => {
16
17
  apiVersion: options.nls.token.apiVersion
17
18
  });
18
19
  const { Token } = await client.request('CreateToken');
19
- options.cache.set('nls:token', Token, Token.expireTime * 1000 - Date.now());
20
- return { token: Token.Id };
20
+ options.cache.set('nls:token', Token, { ttl: parseInt(Token.ExpireTime * 1000 - Date.now()) - 1 });
21
+ return { token: Token.Id, appKey: options.nls.appKey };
21
22
  };
22
23
  const tts = async ({ text, audioName }) => {
23
24
  const { token } = await getToken();
@@ -3,32 +3,39 @@ const fp = require('fastify-plugin');
3
3
 
4
4
  module.exports = fp(async (fastify, options) => {
5
5
  const { services } = fastify.aliyun;
6
- const client = new OSS({
7
- region: options.oss.region,
8
- accessKeyId: options.oss.accessKeyId,
9
- accessKeySecret: options.oss.accessKeySecret,
10
- bucket: options.oss.bucket
11
- });
6
+ const createClient = () => {
7
+ return new OSS({
8
+ region: options.oss.region,
9
+ accessKeyId: options.oss.accessKeyId,
10
+ accessKeySecret: options.oss.accessKeySecret,
11
+ bucket: options.oss.bucket,
12
+ secure: true
13
+ });
14
+ };
12
15
 
13
16
  const uploadFile = async ({ file, filename }) => {
17
+ const client = createClient();
14
18
  await client.put(`${options.oss.baseDir}/${filename}`, file);
15
19
  };
16
20
 
17
21
  const uploadFileStream = async ({ stream, filename }) => {
22
+ const client = createClient();
18
23
  await client.putStream(`${options.oss.baseDir}/${filename}`, stream);
19
24
  };
20
25
 
21
26
  const downloadFile = async ({ filename }) => {
27
+ const client = createClient();
22
28
  const result = await client.get(`${options.oss.baseDir}/${filename}`);
23
29
  return result.content;
24
30
  };
25
31
 
26
32
  const getFileLink = ({ filename, expires }) => {
33
+ const client = createClient();
27
34
  return client.signatureUrl(`${options.oss.baseDir}/${filename}`, { expires: expires || 3600 });
28
35
  };
29
36
 
30
37
  services.oss = {
31
- client,
38
+ createClient,
32
39
  uploadFile,
33
40
  uploadFileStream,
34
41
  getFileLink,
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@kne/fastify-aliyun",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "对接阿里云服务",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "init": "husky",
8
- "dev": "nodemon ./index.js",
8
+ "dev": "cd tests && nodemon ./server.js",
9
9
  "start": "node ./index.js",
10
10
  "doc": "node ./open-api.js",
11
11
  "build:md": "npx @kne/md-doc",
@@ -43,7 +43,7 @@
43
43
  "ali-oss": "^6.20.0",
44
44
  "file-type": "^16.5.4",
45
45
  "lodash": "^4.17.21",
46
- "node-cache": "^5.1.2",
46
+ "lru-cache": "^11.0.0",
47
47
  "node-fetch": "^2.7.0"
48
48
  },
49
49
  "devDependencies": {