wok-server 0.3.0 → 0.3.2

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/dist/mvc/index.js CHANGED
@@ -1,12 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stopWebServer = exports.startWebServer = void 0;
3
+ exports.stopWebServer = exports.startWebServer = exports.removeServerStaticCache = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const server_1 = require("./server");
6
6
  /**
7
7
  * 服务实例.
8
8
  */
9
9
  let SERVER;
10
+ /**
11
+ * 删除服务器静态缓存
12
+ * @param path 路径,必须以斜杠开头,如:/assets/index.js
13
+ */
14
+ async function removeServerStaticCache(path) {
15
+ if (SERVER) {
16
+ SERVER.removeServerStaticCache(path);
17
+ }
18
+ }
19
+ exports.removeServerStaticCache = removeServerStaticCache;
10
20
  /**
11
21
  * 启动 web服务
12
22
  * @param opts
@@ -62,9 +62,7 @@ class WokServer {
62
62
  }
63
63
  // 主服务
64
64
  if (!tls) {
65
- this.server = (0, http_1.createServer)({
66
- requestTimeout: this.config.timeout
67
- }, (req, res) => {
65
+ this.server = (0, http_1.createServer)((req, res) => {
68
66
  res.setHeader('Server', 'Wok Server');
69
67
  res.on('error', error => {
70
68
  // 如果响应流发生错误,只能把信息记录下来
@@ -81,7 +79,6 @@ class WokServer {
81
79
  }
82
80
  else {
83
81
  this.server = (0, https_1.createServer)({
84
- requestTimeout: this.config.timeout,
85
82
  key: tls.key,
86
83
  cert: tls.cert
87
84
  }, (req, res) => {
@@ -99,8 +96,9 @@ class WokServer {
99
96
  });
100
97
  });
101
98
  }
99
+ this.server.setTimeout(this.config.timeout);
102
100
  this.server.on('timeout', (socket) => {
103
- socket.end('HTTP/1.1 408 Timeout\ncontent-type: application/json; charset=utf-8\n\n{"message":"Request timeout"}');
101
+ socket.end('HTTP/1.1 408 Timeout\r\ncontent-type: application/json; charset=utf-8\r\n\r\n{"message":"Request timeout"}');
104
102
  });
105
103
  }
106
104
  /**
@@ -243,6 +241,15 @@ class WokServer {
243
241
  }
244
242
  return res;
245
243
  }
244
+ /**
245
+ * 删除服务器端静态资源缓存
246
+ * @param path
247
+ */
248
+ removeServerStaticCache(path) {
249
+ if (this.staticHandler) {
250
+ this.staticHandler.removeServerCache(path);
251
+ }
252
+ }
246
253
  /**
247
254
  * 停止
248
255
  */
@@ -351,5 +351,15 @@ class StaticHandler {
351
351
  }
352
352
  return { filePath, stats: fileStat, maxAge: matchedRule.cacheAge };
353
353
  }
354
+ /**
355
+ * 删除服务器端静态资源缓存
356
+ * @param path
357
+ */
358
+ removeServerCache(path) {
359
+ if (this.cache) {
360
+ this.cache.remove(path);
361
+ this.cache.remove(`gzip-${path}`);
362
+ }
363
+ }
354
364
  }
355
365
  exports.StaticHandler = StaticHandler;
@@ -577,10 +577,15 @@ dir 参数是映射文件目录的地址,可以是绝对路径,也可以是
577
577
  | SERVER_STATIC_CACHE_ENABLE | 是否启用服务器缓存,默认 false |
578
578
  | SERVER_STATIC_CACHE_MAX_AGE | 服务器缓存时间,单位秒,默认 600 |
579
579
  | SERVER_STATIC_CACHE_MAX_FILE_SIZE | 最大可缓存的文件大小,支持语义化格式,如 10m 和 100k 等,默认 10m |
580
- | SERVER_STATIC_CACHE_MAX_SIZE | 缓存最大空间,一旦超出将执行清理,同上支持语义化格式,默认 100, |
580
+ | SERVER_STATIC_CACHE_MAX_SIZE | 缓存最大空间,一旦超出将执行清理,同上支持语义化格式,默认 100m |
581
581
 
582
- 静态文件的服务器缓存不支持清除,只能等待过期,或者空间满被清理掉。所以,只适合缓存长期不需要改变的文件,
583
- 目前还没有支持按规则来缓存,后续的版本有可能会考虑。
582
+ 0.3.2 版本新增加了 removeServerStaticCache 函数,可以主动删除指定的服务器端静态缓存。
583
+
584
+ ```ts
585
+ import {removeServerStaticCache} from 'wok-server'
586
+
587
+ removeServerStaticCache('/assets/index.js')
588
+ ```
584
589
 
585
590
  ### 请求日志
586
591
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wok-server",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "packageManager": "pnpm@8.9.0",
5
5
  "description": "一个基于 NodeJs 和 TypeScript 的后端框架,轻量级、克制、简洁。A lightweight, restrained, and concise backend framework based on Node.js and TypeScript.",
6
6
  "scripts": {
@@ -1,4 +1,9 @@
1
1
  import { WokServerOpts } from './server';
2
+ /**
3
+ * 删除服务器静态缓存
4
+ * @param path 路径,必须以斜杠开头,如:/assets/index.js
5
+ */
6
+ export declare function removeServerStaticCache(path: string): Promise<void>;
2
7
  /**
3
8
  * 启动 web服务
4
9
  * @param opts
@@ -78,6 +78,11 @@ export declare class WokServer {
78
78
  * @returns
79
79
  */
80
80
  private getIpv4List;
81
+ /**
82
+ * 删除服务器端静态资源缓存
83
+ * @param path
84
+ */
85
+ removeServerStaticCache(path: string): void;
81
86
  /**
82
87
  * 停止
83
88
  */
@@ -69,4 +69,9 @@ export declare class StaticHandler {
69
69
  * @returns 返回被查询到的文件信息,如果找不到则返回 null
70
70
  */
71
71
  private findFile;
72
+ /**
73
+ * 删除服务器端静态资源缓存
74
+ * @param path
75
+ */
76
+ removeServerCache(path: string): void;
72
77
  }