@modern-js/prod-server 2.14.0 → 2.16.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.
Files changed (122) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/cjs/constants.js +15 -31
  3. package/dist/cjs/index.js +35 -37
  4. package/dist/cjs/libs/context/context.js +53 -66
  5. package/dist/cjs/libs/context/index.js +15 -28
  6. package/dist/cjs/libs/hook-api/index.js +59 -54
  7. package/dist/cjs/libs/hook-api/route.js +28 -30
  8. package/dist/cjs/libs/hook-api/template.js +30 -34
  9. package/dist/cjs/libs/loadConfig.js +74 -65
  10. package/dist/cjs/libs/logger.js +46 -37
  11. package/dist/cjs/libs/metrics.js +7 -25
  12. package/dist/cjs/libs/proxy.js +29 -46
  13. package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +48 -64
  14. package/dist/cjs/libs/render/cache/__tests__/cache.test.js +53 -37
  15. package/dist/cjs/libs/render/cache/__tests__/cacheable.js +38 -34
  16. package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +22 -30
  17. package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +86 -46
  18. package/dist/cjs/libs/render/cache/index.js +17 -37
  19. package/dist/cjs/libs/render/cache/page-caches/index.js +12 -28
  20. package/dist/cjs/libs/render/cache/page-caches/lru.js +32 -40
  21. package/dist/cjs/libs/render/cache/spr.js +104 -111
  22. package/dist/cjs/libs/render/cache/type.js +4 -15
  23. package/dist/cjs/libs/render/cache/util.js +29 -53
  24. package/dist/cjs/libs/render/index.js +75 -73
  25. package/dist/cjs/libs/render/measure.js +19 -34
  26. package/dist/cjs/libs/render/reader.js +52 -61
  27. package/dist/cjs/libs/render/ssr.js +34 -66
  28. package/dist/cjs/libs/render/static.js +22 -45
  29. package/dist/cjs/libs/render/type.js +10 -28
  30. package/dist/cjs/libs/render/utils.js +17 -38
  31. package/dist/cjs/libs/route/index.js +32 -31
  32. package/dist/cjs/libs/route/matcher.js +38 -38
  33. package/dist/cjs/libs/route/route.js +29 -26
  34. package/dist/cjs/libs/serveFile.js +23 -41
  35. package/dist/cjs/server/index.js +101 -107
  36. package/dist/cjs/server/modernServer.js +191 -171
  37. package/dist/cjs/server/modernServerSplit.js +12 -30
  38. package/dist/cjs/type.js +4 -15
  39. package/dist/cjs/utils.js +50 -50
  40. package/dist/cjs/workerServer.js +26 -36
  41. package/dist/esm/constants.js +24 -25
  42. package/dist/esm/index.js +11 -10
  43. package/dist/esm/libs/context/context.js +271 -264
  44. package/dist/esm/libs/context/index.js +3 -3
  45. package/dist/esm/libs/hook-api/index.js +237 -223
  46. package/dist/esm/libs/hook-api/route.js +62 -60
  47. package/dist/esm/libs/hook-api/template.js +117 -115
  48. package/dist/esm/libs/loadConfig.js +68 -69
  49. package/dist/esm/libs/logger.js +188 -174
  50. package/dist/esm/libs/metrics.js +6 -3
  51. package/dist/esm/libs/proxy.js +236 -221
  52. package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +286 -267
  53. package/dist/esm/libs/render/cache/__tests__/cache.test.js +765 -745
  54. package/dist/esm/libs/render/cache/__tests__/cacheable.js +62 -63
  55. package/dist/esm/libs/render/cache/__tests__/error-configuration.js +42 -43
  56. package/dist/esm/libs/render/cache/__tests__/matched-cache.js +134 -135
  57. package/dist/esm/libs/render/cache/index.js +337 -326
  58. package/dist/esm/libs/render/cache/page-caches/index.js +147 -141
  59. package/dist/esm/libs/render/cache/page-caches/lru.js +78 -76
  60. package/dist/esm/libs/render/cache/spr.js +465 -456
  61. package/dist/esm/libs/render/cache/type.js +1 -1
  62. package/dist/esm/libs/render/cache/util.js +266 -246
  63. package/dist/esm/libs/render/index.js +226 -218
  64. package/dist/esm/libs/render/measure.js +136 -128
  65. package/dist/esm/libs/render/reader.js +315 -304
  66. package/dist/esm/libs/render/ssr.js +214 -209
  67. package/dist/esm/libs/render/static.js +208 -198
  68. package/dist/esm/libs/render/type.js +6 -7
  69. package/dist/esm/libs/render/utils.js +8 -9
  70. package/dist/esm/libs/route/index.js +134 -123
  71. package/dist/esm/libs/route/matcher.js +140 -130
  72. package/dist/esm/libs/route/route.js +36 -37
  73. package/dist/esm/libs/serveFile.js +177 -168
  74. package/dist/esm/server/index.js +578 -549
  75. package/dist/esm/server/modernServer.js +1137 -1074
  76. package/dist/esm/server/modernServerSplit.js +344 -328
  77. package/dist/esm/type.js +1 -1
  78. package/dist/esm/utils.js +133 -124
  79. package/dist/esm/workerServer.js +226 -220
  80. package/dist/esm-node/constants.js +7 -14
  81. package/dist/esm-node/index.js +4 -9
  82. package/dist/esm-node/libs/context/context.js +32 -25
  83. package/dist/esm-node/libs/context/index.js +2 -5
  84. package/dist/esm-node/libs/hook-api/index.js +40 -21
  85. package/dist/esm-node/libs/hook-api/route.js +21 -8
  86. package/dist/esm-node/libs/hook-api/template.js +19 -14
  87. package/dist/esm-node/libs/loadConfig.js +11 -28
  88. package/dist/esm-node/libs/logger.js +37 -17
  89. package/dist/esm-node/libs/metrics.js +1 -3
  90. package/dist/esm-node/libs/proxy.js +14 -22
  91. package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +25 -36
  92. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +23 -11
  93. package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +32 -13
  94. package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +16 -9
  95. package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +80 -25
  96. package/dist/esm-node/libs/render/cache/index.js +3 -10
  97. package/dist/esm-node/libs/render/cache/page-caches/index.js +4 -5
  98. package/dist/esm-node/libs/render/cache/page-caches/lru.js +20 -8
  99. package/dist/esm-node/libs/render/cache/spr.js +79 -80
  100. package/dist/esm-node/libs/render/cache/type.js +1 -0
  101. package/dist/esm-node/libs/render/cache/util.js +13 -23
  102. package/dist/esm-node/libs/render/index.js +10 -28
  103. package/dist/esm-node/libs/render/measure.js +7 -13
  104. package/dist/esm-node/libs/render/reader.js +31 -26
  105. package/dist/esm-node/libs/render/ssr.js +5 -21
  106. package/dist/esm-node/libs/render/static.js +1 -4
  107. package/dist/esm-node/libs/render/type.js +3 -6
  108. package/dist/esm-node/libs/render/utils.js +6 -18
  109. package/dist/esm-node/libs/route/index.js +20 -9
  110. package/dist/esm-node/libs/route/matcher.js +28 -17
  111. package/dist/esm-node/libs/route/route.js +23 -5
  112. package/dist/esm-node/libs/serveFile.js +2 -6
  113. package/dist/esm-node/server/index.js +72 -75
  114. package/dist/esm-node/server/modernServer.js +102 -130
  115. package/dist/esm-node/server/modernServerSplit.js +1 -4
  116. package/dist/esm-node/type.js +1 -0
  117. package/dist/esm-node/utils.js +41 -32
  118. package/dist/esm-node/workerServer.js +9 -10
  119. package/dist/types/libs/context/context.d.ts +1 -1
  120. package/dist/types/server/modernServer.d.ts +1 -1
  121. package/dist/types/utils.d.ts +1 -1
  122. package/package.json +12 -8
@@ -1,14 +1,20 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
1
14
  import cookie from "cookie";
2
15
  import { RouteAPI } from "./route";
3
16
  import { TemplateAPI } from "./template";
4
17
  class Response {
5
- constructor(res) {
6
- this.res = res;
7
- this.cookies = {
8
- set: this.setCookie.bind(this),
9
- clear: this.clearCookie.bind(this)
10
- };
11
- }
12
18
  get(key) {
13
19
  return this.res.getHeader(key);
14
20
  }
@@ -20,7 +26,9 @@ class Response {
20
26
  }
21
27
  setCookie(key, value, options) {
22
28
  const cookieValue = this.res.getHeader("set-cookie");
23
- const fmt = Array.isArray(cookieValue) ? cookieValue : [cookieValue].filter(Boolean);
29
+ const fmt = Array.isArray(cookieValue) ? cookieValue : [
30
+ cookieValue
31
+ ].filter(Boolean);
24
32
  fmt.push(cookie.serialize(key, value, options));
25
33
  this.res.setHeader("set-cookie", fmt.length === 1 ? fmt[0] : fmt);
26
34
  }
@@ -37,9 +45,29 @@ class Response {
37
45
  }
38
46
  this.res.end(body);
39
47
  }
48
+ constructor(res) {
49
+ _define_property(this, "cookies", void 0);
50
+ _define_property(this, "res", void 0);
51
+ this.res = res;
52
+ this.cookies = {
53
+ set: this.setCookie.bind(this),
54
+ clear: this.clearCookie.bind(this)
55
+ };
56
+ }
40
57
  }
41
58
  class Request {
59
+ getCookie(key) {
60
+ return this._cookie[key];
61
+ }
42
62
  constructor(ctx) {
63
+ _define_property(this, "url", void 0);
64
+ _define_property(this, "host", void 0);
65
+ _define_property(this, "pathname", void 0);
66
+ _define_property(this, "query", void 0);
67
+ _define_property(this, "headers", void 0);
68
+ _define_property(this, "cookie", void 0);
69
+ _define_property(this, "cookies", void 0);
70
+ _define_property(this, "_cookie", void 0);
43
71
  this.url = ctx.url;
44
72
  this.host = ctx.host;
45
73
  this.pathname = ctx.path;
@@ -51,11 +79,8 @@ class Request {
51
79
  get: this.getCookie.bind(this)
52
80
  };
53
81
  }
54
- getCookie(key) {
55
- return this._cookie[key];
56
- }
57
82
  }
58
- const base = (context) => {
83
+ export const base = (context) => {
59
84
  const { res } = context;
60
85
  return {
61
86
  response: new Response(res),
@@ -64,21 +89,21 @@ const base = (context) => {
64
89
  metrics: context.metrics
65
90
  };
66
91
  };
67
- const createAfterMatchContext = (context, entryName) => {
92
+ export const createAfterMatchContext = (context, entryName) => {
68
93
  const baseContext = base(context);
69
94
  return {
70
95
  ...baseContext,
71
96
  router: new RouteAPI(entryName)
72
97
  };
73
98
  };
74
- const createAfterRenderContext = (context, content) => {
99
+ export const createAfterRenderContext = (context, content) => {
75
100
  const baseContext = base(context);
76
101
  return {
77
102
  ...baseContext,
78
103
  template: new TemplateAPI(content)
79
104
  };
80
105
  };
81
- const createMiddlewareContext = (context) => {
106
+ export const createMiddlewareContext = (context) => {
82
107
  const baseContext = base(context);
83
108
  return {
84
109
  ...baseContext,
@@ -92,9 +117,3 @@ const createMiddlewareContext = (context) => {
92
117
  }
93
118
  };
94
119
  };
95
- export {
96
- base,
97
- createAfterMatchContext,
98
- createAfterRenderContext,
99
- createMiddlewareContext
100
- };
@@ -1,9 +1,17 @@
1
- class RouteAPI {
2
- constructor(entryName) {
3
- this.current = entryName;
4
- this.status = 200;
5
- this.url = "";
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
6
11
  }
12
+ return obj;
13
+ }
14
+ export class RouteAPI {
7
15
  redirect(url, status = 302) {
8
16
  this.url = url;
9
17
  this.status = status;
@@ -14,7 +22,12 @@ class RouteAPI {
14
22
  use(entryName) {
15
23
  this.rewrite(entryName);
16
24
  }
25
+ constructor(entryName) {
26
+ _define_property(this, "current", void 0);
27
+ _define_property(this, "status", void 0);
28
+ _define_property(this, "url", void 0);
29
+ this.current = entryName;
30
+ this.status = 200;
31
+ this.url = "";
32
+ }
17
33
  }
18
- export {
19
- RouteAPI
20
- };
@@ -1,3 +1,16 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
1
14
  import { Transform } from "stream";
2
15
  const RegList = {
3
16
  before: {
@@ -9,10 +22,7 @@ const RegList = {
9
22
  body: "</body>"
10
23
  }
11
24
  };
12
- class TemplateAPI {
13
- constructor(content) {
14
- this.content = content;
15
- }
25
+ export class TemplateAPI {
16
26
  get() {
17
27
  return this.content;
18
28
  }
@@ -38,13 +48,12 @@ class TemplateAPI {
38
48
  replace(reg, text) {
39
49
  this.content = this.content.replace(reg, text);
40
50
  }
51
+ constructor(content) {
52
+ _define_property(this, "content", void 0);
53
+ this.content = content;
54
+ }
41
55
  }
42
- const templateInjectableStream = ({
43
- prependHead,
44
- appendHead,
45
- prependBody,
46
- appendBody
47
- }) => new Transform({
56
+ export const templateInjectableStream = ({ prependHead, appendHead, prependBody, appendBody }) => new Transform({
48
57
  write(chunk, _, callback) {
49
58
  let chunk_str = chunk.toString();
50
59
  if (prependHead) {
@@ -67,7 +76,3 @@ const templateInjectableStream = ({
67
76
  callback();
68
77
  }
69
78
  });
70
- export {
71
- TemplateAPI,
72
- templateInjectableStream
73
- };
@@ -1,46 +1,29 @@
1
1
  import * as path from "path";
2
2
  import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
3
3
  import mergeDeep from "merge-deep";
4
- const getServerConfigPath = (distDirectory, serverConfigFile = DEFAULT_SERVER_CONFIG) => {
4
+ export const getServerConfigPath = (distDirectory, serverConfigFile = DEFAULT_SERVER_CONFIG) => {
5
5
  const serverConfigPath = path.join(distDirectory, serverConfigFile);
6
6
  return `${serverConfigPath}.js`;
7
7
  };
8
- const requireConfig = (serverConfigPath) => {
8
+ export const requireConfig = (serverConfigPath) => {
9
9
  if (fs.pathExistsSync(serverConfigPath)) {
10
10
  return compatRequire(serverConfigPath);
11
11
  }
12
12
  return {};
13
13
  };
14
- const loadConfig = ({
15
- cliConfig,
16
- serverConfig,
17
- resolvedConfigPath
18
- }) => {
14
+ export const loadConfig = ({ cliConfig, serverConfig, resolvedConfigPath }) => {
19
15
  let config = null;
20
16
  if (process.env.NODE_ENV === "production") {
21
17
  const resolvedConfig = requireConfig(resolvedConfigPath);
22
- config = mergeDeep(
23
- {
24
- ...resolvedConfig,
25
- plugins: []
26
- // filter cli plugins
27
- },
28
- serverConfig,
29
- cliConfig
30
- );
18
+ config = mergeDeep({
19
+ ...resolvedConfig,
20
+ plugins: []
21
+ }, serverConfig, cliConfig);
31
22
  } else {
32
- config = mergeDeep(
33
- {
34
- ...cliConfig,
35
- plugins: []
36
- },
37
- serverConfig
38
- );
23
+ config = mergeDeep({
24
+ ...cliConfig,
25
+ plugins: []
26
+ }, serverConfig);
39
27
  }
40
28
  return config;
41
29
  };
42
- export {
43
- getServerConfigPath,
44
- loadConfig,
45
- requireConfig
46
- };
@@ -1,3 +1,16 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
1
14
  const LOG_LEVEL = {
2
15
  error: 0,
3
16
  warn: 1,
@@ -31,25 +44,15 @@ const LOG_TYPES = {
31
44
  label: "debug",
32
45
  level: "debug"
33
46
  },
34
- log: { level: "log" }
47
+ log: {
48
+ level: "log"
49
+ }
35
50
  };
36
51
  const DEFAULT_CONFIG = {
37
52
  displayLabel: true,
38
53
  uppercaseLabel: false
39
54
  };
40
55
  class Logger {
41
- constructor(options = {}) {
42
- this.level = options.level || LOG_TYPES.log.level;
43
- this.config = { ...DEFAULT_CONFIG, ...options.config || {} };
44
- this.types = {
45
- ...LOG_TYPES,
46
- ...options.types || {}
47
- };
48
- this.longestLabel = this.getLongestLabel();
49
- Object.keys(this.types).forEach((type) => {
50
- this[type] = this._log.bind(this, type);
51
- });
52
- }
53
56
  _log(type, message, ...args) {
54
57
  if (message === void 0 || message === null) {
55
58
  console.log();
@@ -89,10 +92,27 @@ ${rest.join("\n")}`;
89
92
  });
90
93
  return longestLabel;
91
94
  }
95
+ constructor(options = {}) {
96
+ _define_property(this, "level", void 0);
97
+ _define_property(this, "config", void 0);
98
+ _define_property(this, "types", void 0);
99
+ _define_property(this, "longestLabel", void 0);
100
+ this.level = options.level || LOG_TYPES.log.level;
101
+ this.config = {
102
+ ...DEFAULT_CONFIG,
103
+ ...options.config || {}
104
+ };
105
+ this.types = {
106
+ ...LOG_TYPES,
107
+ ...options.types || {}
108
+ };
109
+ this.longestLabel = this.getLongestLabel();
110
+ Object.keys(this.types).forEach((type) => {
111
+ this[type] = this._log.bind(this, type);
112
+ });
113
+ }
92
114
  }
93
115
  const logger = new Logger();
94
116
  logger.Logger = Logger;
95
- export {
96
- Logger,
97
- logger
98
- };
117
+ export { Logger };
118
+ export { logger };
@@ -6,6 +6,4 @@ const metrics = {
6
6
  emitTimer() {
7
7
  }
8
8
  };
9
- export {
10
- metrics
11
- };
9
+ export { metrics };
@@ -1,34 +1,30 @@
1
1
  import { createProxyMiddleware } from "http-proxy-middleware";
2
2
  import { debug } from "../utils";
3
- function formatProxyOptions(proxyOptions) {
3
+ export function formatProxyOptions(proxyOptions) {
4
4
  const formattedProxy = [];
5
5
  if (!Array.isArray(proxyOptions)) {
6
6
  if ("target" in proxyOptions) {
7
7
  formattedProxy.push(proxyOptions);
8
8
  } else {
9
- Array.prototype.push.apply(
10
- formattedProxy,
11
- Object.keys(proxyOptions).reduce(
12
- (total, source) => {
13
- const option = proxyOptions[source];
14
- total.push({
15
- context: source,
16
- changeOrigin: true,
17
- logLevel: "warn",
18
- ...typeof option === "string" ? { target: option } : option
19
- });
20
- return total;
21
- },
22
- []
23
- )
24
- );
9
+ Array.prototype.push.apply(formattedProxy, Object.keys(proxyOptions).reduce((total, source) => {
10
+ const option = proxyOptions[source];
11
+ total.push({
12
+ context: source,
13
+ changeOrigin: true,
14
+ logLevel: "warn",
15
+ ...typeof option === "string" ? {
16
+ target: option
17
+ } : option
18
+ });
19
+ return total;
20
+ }, []));
25
21
  }
26
22
  } else {
27
23
  formattedProxy.push(...proxyOptions);
28
24
  }
29
25
  return formattedProxy;
30
26
  }
31
- const createProxyHandler = (proxyOptions) => {
27
+ export const createProxyHandler = (proxyOptions) => {
32
28
  debug("createProxyHandler", proxyOptions);
33
29
  if (!proxyOptions) {
34
30
  return null;
@@ -51,7 +47,3 @@ const createProxyHandler = (proxyOptions) => {
51
47
  });
52
48
  return middlewares;
53
49
  };
54
- export {
55
- createProxyHandler,
56
- formatProxyOptions
57
- };
@@ -1,14 +1,6 @@
1
1
  import url from "url";
2
2
  import { createCache, destroyCache } from "../spr";
3
- import {
4
- cacheAddition,
5
- connectFactor,
6
- fname,
7
- maybeSync,
8
- namespaceHash,
9
- valueFactory,
10
- withCoalescedInvoke
11
- } from "../util";
3
+ import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "../util";
12
4
  describe("test spr util functions", () => {
13
5
  it("should return value correctly", () => {
14
6
  expect(connectFactor("bar", "foo")).toBe("bar-foo");
@@ -29,7 +21,9 @@ describe("test spr util functions", () => {
29
21
  urlParams.set("name", "modern");
30
22
  const getParam = valueFactory(urlParams);
31
23
  expect(getParam("name")).toBe("modern");
32
- const headers = { age: "12345" };
24
+ const headers = {
25
+ age: "12345"
26
+ };
33
27
  const getHeader = valueFactory(headers);
34
28
  expect(getHeader("age")).toBe("12345");
35
29
  });
@@ -40,22 +34,21 @@ describe("test spr util functions", () => {
40
34
  const contentWithHead = "<head></head><div>123</div>";
41
35
  const hash = Math.random().toString();
42
36
  const htmlWithHead = cacheAddition(contentWithHead, hash);
43
- expect(htmlWithHead).toBe(
44
- `<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`
45
- );
37
+ expect(htmlWithHead).toBe(`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`);
46
38
  });
47
39
  it("should only invoke func one time", async () => {
48
40
  let index = 0;
49
- const fn = withCoalescedInvoke(
50
- async () => new Promise((resolve) => {
51
- setTimeout(() => {
52
- index += 1;
53
- resolve(index);
54
- }, 500);
55
- })
56
- );
41
+ const fn = withCoalescedInvoke(async () => new Promise((resolve) => {
42
+ setTimeout(() => {
43
+ index += 1;
44
+ resolve(index);
45
+ }, 500);
46
+ }));
57
47
  const key = "test";
58
- const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
48
+ const [res1, res2] = await Promise.all([
49
+ fn(key, []),
50
+ fn(key, [])
51
+ ]);
59
52
  expect(res1.isOrigin && res2.isOrigin).toBe(false);
60
53
  expect(res1.isOrigin || res2.isOrigin).toBe(true);
61
54
  expect(res1.value).toBe(1);
@@ -63,21 +56,17 @@ describe("test spr util functions", () => {
63
56
  });
64
57
  it("should invoke sync or async", async () => {
65
58
  const foo = "";
66
- const async = await maybeSync(
67
- () => new Promise((resolve) => {
68
- setTimeout(() => {
69
- resolve(foo);
70
- }, 100);
71
- })
72
- )(false);
59
+ const async = await maybeSync(() => new Promise((resolve) => {
60
+ setTimeout(() => {
61
+ resolve(foo);
62
+ }, 100);
63
+ }))(false);
73
64
  expect(async).toBeUndefined();
74
- const sync = await maybeSync(
75
- () => new Promise((resolve) => {
76
- setTimeout(() => {
77
- resolve(foo);
78
- }, 100);
79
- })
80
- )(true);
65
+ const sync = await maybeSync(() => new Promise((resolve) => {
66
+ setTimeout(() => {
67
+ resolve(foo);
68
+ }, 100);
69
+ }))(true);
81
70
  expect(sync).toBe(foo);
82
71
  });
83
72
  });
@@ -28,7 +28,7 @@ describe("cache", () => {
28
28
  await cache.set(context, content, cacheConfig, true);
29
29
  const cacheResult = await cache.get(context);
30
30
  expect(cacheResult).not.toBe(null);
31
- expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
31
+ expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe("hello");
32
32
  });
33
33
  it("should ignore cache set when cache config not exist", async () => {
34
34
  destroyCache();
@@ -73,7 +73,11 @@ describe("cache", () => {
73
73
  const content = "hello";
74
74
  const cacheConfig = createCacheConfig({
75
75
  level: 1,
76
- includes: { query: ["name"] }
76
+ includes: {
77
+ query: [
78
+ "name"
79
+ ]
80
+ }
77
81
  });
78
82
  await cache.set(context, content, cacheConfig, true);
79
83
  const context_req = {
@@ -97,13 +101,19 @@ describe("cache", () => {
97
101
  const content = "hello";
98
102
  const cacheConfig = createCacheConfig({
99
103
  level: 1,
100
- includes: { query: ["name"] }
104
+ includes: {
105
+ query: [
106
+ "name"
107
+ ]
108
+ }
101
109
  });
102
110
  await cache.set(context, content, cacheConfig, true);
103
111
  const context_req = {
104
112
  entry: "",
105
113
  pathname: "",
106
- query: { name: "zll" },
114
+ query: {
115
+ name: "zll"
116
+ },
107
117
  headers: {}
108
118
  };
109
119
  const cacheResult = await cache.get(context_req);
@@ -122,7 +132,7 @@ describe("cache", () => {
122
132
  const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
123
133
  await cache.set(context, cacheable.content, cacheConfig, true);
124
134
  const cacheResult = await cache.get(context);
125
- expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
135
+ expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(cacheable.content);
126
136
  }
127
137
  });
128
138
  it("should match cache correctly", async () => {
@@ -145,7 +155,7 @@ describe("cache", () => {
145
155
  headers: matchOne.headers
146
156
  };
147
157
  const cacheResult = await cache.get(matchContext);
148
- expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
158
+ expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
149
159
  for (const notMatch of other) {
150
160
  const notMatchContext = {
151
161
  entry: "",
@@ -167,19 +177,21 @@ describe("cache", () => {
167
177
  query: {},
168
178
  headers: {}
169
179
  };
170
- const config = createCacheConfig({ interval: 5 });
180
+ const config = createCacheConfig({
181
+ interval: 5
182
+ });
171
183
  const content = "hello";
172
184
  const shouldCache = await cache.set(context, content, config, true);
173
185
  expect(shouldCache.value).toBe(true);
174
186
  const freshResult = await cache.get(context);
175
- expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
187
+ expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isStale).toBe(false);
176
188
  await new Promise((resolve) => {
177
189
  setTimeout(() => {
178
190
  resolve();
179
191
  }, 6e3);
180
192
  });
181
193
  const staleResult = await cache.get(context);
182
- expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
194
+ expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
183
195
  });
184
196
  it("should garbage cache correctly", async () => {
185
197
  destroyCache();
@@ -198,13 +210,13 @@ describe("cache", () => {
198
210
  const shouldCache = await cache.set(context, content, config, true);
199
211
  expect(shouldCache.value).toBe(true);
200
212
  const freshResult = await cache.get(context);
201
- expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
213
+ expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isGarbage).toBe(false);
202
214
  await new Promise((resolve) => {
203
215
  setTimeout(() => {
204
216
  resolve();
205
217
  }, 1e4);
206
218
  });
207
219
  const staleResult = await cache.get(context);
208
- expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
220
+ expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
209
221
  });
210
222
  });
@@ -1,47 +1,66 @@
1
- const cacheabelAry = [
1
+ export const cacheabelAry = [
2
2
  {
3
- requestOpt: { url: "/" },
3
+ requestOpt: {
4
+ url: "/"
5
+ },
4
6
  cacheConfig: {},
5
7
  content: "level0"
6
8
  },
7
9
  {
8
10
  requestOpt: {
9
11
  url: "/level-one",
10
- query: { name: "modern" }
12
+ query: {
13
+ name: "modern"
14
+ }
11
15
  },
12
16
  cacheConfig: {
13
17
  level: 1,
14
- includes: { query: ["name"] }
18
+ includes: {
19
+ query: [
20
+ "name"
21
+ ]
22
+ }
15
23
  },
16
24
  content: "level1"
17
25
  },
18
26
  {
19
27
  requestOpt: {
20
28
  url: "/level-two",
21
- headers: { age: "18" }
29
+ headers: {
30
+ age: "18"
31
+ }
22
32
  },
23
33
  cacheConfig: {
24
34
  level: 2,
25
- includes: { header: ["age"] }
35
+ includes: {
36
+ header: [
37
+ "age"
38
+ ]
39
+ }
26
40
  },
27
41
  content: "level2"
28
42
  },
29
43
  {
30
44
  requestOpt: {
31
45
  url: "/level-three",
32
- query: { name: "modern" },
33
- headers: { age: "18" }
46
+ query: {
47
+ name: "modern"
48
+ },
49
+ headers: {
50
+ age: "18"
51
+ }
34
52
  },
35
53
  cacheConfig: {
36
54
  level: 3,
37
55
  includes: {
38
- query: ["name"],
39
- header: ["age"]
56
+ query: [
57
+ "name"
58
+ ],
59
+ header: [
60
+ "age"
61
+ ]
40
62
  }
41
63
  },
42
64
  content: "level3"
43
65
  }
44
66
  ];
45
- export {
46
- cacheabelAry
47
- };