@modern-js/prod-server 1.22.1 → 2.0.0-alpha.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/CHANGELOG.md +271 -17
- package/dist/js/modern/constants.js +32 -27
- package/dist/js/modern/index.js +11 -7
- package/dist/js/modern/libs/context/context.js +54 -132
- package/dist/js/modern/libs/context/index.js +5 -2
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +13 -37
- package/dist/js/modern/libs/hook-api/template.js +41 -32
- package/dist/js/modern/libs/loadConfig.js +46 -32
- package/dist/js/modern/libs/metrics.js +6 -7
- package/dist/js/modern/libs/proxy.js +70 -44
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
- package/dist/js/modern/libs/render/cache/index.js +93 -50
- package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -16
- package/dist/js/modern/libs/render/cache/spr.js +133 -167
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/render/cache/util.js +71 -45
- package/dist/js/modern/libs/render/index.js +76 -61
- package/dist/js/modern/libs/render/measure.js +42 -34
- package/dist/js/modern/libs/render/reader.js +68 -76
- package/dist/js/modern/libs/render/ssr.js +63 -33
- package/dist/js/modern/libs/render/static.js +51 -37
- package/dist/js/modern/libs/render/type.js +9 -7
- package/dist/js/modern/libs/route/index.js +12 -30
- package/dist/js/modern/libs/route/matcher.js +28 -50
- package/dist/js/modern/libs/route/route.js +9 -31
- package/dist/js/modern/libs/serve-file.js +40 -20
- package/dist/js/modern/server/index.js +152 -168
- package/dist/js/modern/server/modern-server-split.js +44 -60
- package/dist/js/modern/server/modern-server.js +416 -526
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +62 -58
- package/dist/js/modern/worker-server.js +54 -0
- package/dist/js/node/constants.js +53 -32
- package/dist/js/node/index.js +37 -67
- package/dist/js/node/libs/context/context.js +84 -150
- package/dist/js/node/libs/context/index.js +28 -16
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +35 -45
- package/dist/js/node/libs/hook-api/template.js +64 -40
- package/dist/js/node/libs/loadConfig.js +74 -55
- package/dist/js/node/libs/metrics.js +28 -12
- package/dist/js/node/libs/proxy.js +90 -55
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
- package/dist/js/node/libs/render/cache/index.js +115 -65
- package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
- package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
- package/dist/js/node/libs/render/cache/spr.js +161 -188
- package/dist/js/node/libs/render/cache/type.js +15 -5
- package/dist/js/node/libs/render/cache/util.js +99 -63
- package/dist/js/node/libs/render/index.js +106 -87
- package/dist/js/node/libs/render/measure.js +61 -44
- package/dist/js/node/libs/render/reader.js +98 -100
- package/dist/js/node/libs/render/ssr.js +92 -58
- package/dist/js/node/libs/render/static.js +80 -53
- package/dist/js/node/libs/render/type.js +31 -13
- package/dist/js/node/libs/route/index.js +35 -44
- package/dist/js/node/libs/route/matcher.js +48 -65
- package/dist/js/node/libs/route/route.js +29 -37
- package/dist/js/node/libs/serve-file.js +74 -37
- package/dist/js/node/server/index.js +170 -203
- package/dist/js/node/server/modern-server-split.js +70 -73
- package/dist/js/node/server/modern-server.js +432 -577
- package/dist/js/node/type.js +15 -3
- package/dist/js/node/utils.js +87 -93
- package/dist/js/node/worker-server.js +77 -0
- package/dist/js/treeshaking/constants.js +29 -0
- package/dist/js/treeshaking/index.js +13 -0
- package/dist/js/treeshaking/libs/context/context.js +274 -0
- package/dist/js/treeshaking/libs/context/index.js +5 -0
- package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
- package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
- package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
- package/dist/js/treeshaking/libs/loadConfig.js +82 -0
- package/dist/js/treeshaking/libs/metrics.js +6 -0
- package/dist/js/treeshaking/libs/proxy.js +244 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
- package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
- package/dist/js/treeshaking/libs/render/index.js +233 -0
- package/dist/js/treeshaking/libs/render/measure.js +146 -0
- package/dist/js/treeshaking/libs/render/reader.js +339 -0
- package/dist/js/treeshaking/libs/render/ssr.js +223 -0
- package/dist/js/treeshaking/libs/render/static.js +216 -0
- package/dist/js/treeshaking/libs/render/type.js +7 -0
- package/dist/js/treeshaking/libs/route/index.js +130 -0
- package/dist/js/treeshaking/libs/route/matcher.js +138 -0
- package/dist/js/treeshaking/libs/route/route.js +40 -0
- package/dist/js/treeshaking/libs/serve-file.js +184 -0
- package/dist/js/treeshaking/server/index.js +505 -0
- package/dist/js/treeshaking/server/modern-server-split.js +360 -0
- package/dist/js/treeshaking/server/modern-server.js +1083 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +177 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/hook-api/index.d.ts +5 -0
- package/dist/types/libs/hook-api/route.d.ts +9 -14
- package/dist/types/libs/hook-api/template.d.ts +19 -9
- package/dist/types/libs/render/cache/index.d.ts +4 -2
- package/dist/types/libs/render/type.d.ts +3 -1
- package/dist/types/libs/route/route.d.ts +0 -1
- package/dist/types/libs/serve-file.d.ts +2 -1
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/server/modern-server.d.ts +11 -11
- package/dist/types/type.d.ts +8 -10
- package/dist/types/utils.d.ts +3 -4
- package/dist/types/worker-server.d.ts +15 -0
- package/package.json +31 -45
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -48
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -64
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
|
@@ -1,70 +1,114 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
expect(ins1 === ins2).toBe(true);
|
|
14
|
-
destroyCache();
|
|
15
|
-
const ins3 = createCache();
|
|
16
|
-
expect(ins1 === ins3).toBe(false);
|
|
17
|
-
expect(ins2 === ins3).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
it('should return function correctly', () => {
|
|
20
|
-
const urlParams = (() => new url.URLSearchParams())();
|
|
21
|
-
|
|
22
|
-
urlParams.set('name', 'modern');
|
|
23
|
-
const getParam = valueFactory(urlParams);
|
|
24
|
-
expect(getParam('name')).toBe('modern');
|
|
25
|
-
const headers = {
|
|
26
|
-
age: '12345'
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
+
};
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
27
13
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const htmlWithHead = cacheAddition(contentWithHead, hash);
|
|
38
|
-
expect(htmlWithHead).toBe(`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`);
|
|
39
|
-
});
|
|
40
|
-
it('should only invoke func one time', async () => {
|
|
41
|
-
let index = 0;
|
|
42
|
-
const fn = withCoalescedInvoke(async () => new Promise(resolve => {
|
|
43
|
-
setTimeout(() => {
|
|
44
|
-
index += 1;
|
|
45
|
-
resolve(index);
|
|
46
|
-
}, 500);
|
|
47
|
-
}));
|
|
48
|
-
const key = 'test';
|
|
49
|
-
const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
|
|
50
|
-
expect(res1.isOrigin && res2.isOrigin).toBe(false);
|
|
51
|
-
expect(res1.isOrigin || res2.isOrigin).toBe(true);
|
|
52
|
-
expect(res1.value).toBe(1);
|
|
53
|
-
expect(res2.value).toBe(1);
|
|
54
|
-
});
|
|
55
|
-
it('should invoke sync or async', async () => {
|
|
56
|
-
const foo = '';
|
|
57
|
-
const async = await maybeSync(() => new Promise(resolve => {
|
|
58
|
-
setTimeout(() => {
|
|
59
|
-
resolve(foo);
|
|
60
|
-
}, 100);
|
|
61
|
-
}))(false);
|
|
62
|
-
expect(async).toBeUndefined();
|
|
63
|
-
const sync = await maybeSync(() => new Promise(resolve => {
|
|
64
|
-
setTimeout(() => {
|
|
65
|
-
resolve(foo);
|
|
66
|
-
}, 100);
|
|
67
|
-
}))(true);
|
|
68
|
-
expect(sync).toBe(foo);
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
69
23
|
});
|
|
70
|
-
}
|
|
24
|
+
};
|
|
25
|
+
import url from "url";
|
|
26
|
+
import { createCache, destroyCache } from "../spr";
|
|
27
|
+
import {
|
|
28
|
+
cacheAddition,
|
|
29
|
+
connectFactor,
|
|
30
|
+
fname,
|
|
31
|
+
maybeSync,
|
|
32
|
+
namespaceHash,
|
|
33
|
+
valueFactory,
|
|
34
|
+
withCoalescedInvoke
|
|
35
|
+
} from "../util";
|
|
36
|
+
var require_cache_fun_test = __commonJS({
|
|
37
|
+
"src/libs/render/cache/__tests__/cache.fun.test.ts"(exports) {
|
|
38
|
+
describe("test spr util functions", () => {
|
|
39
|
+
it("should return value correctly", () => {
|
|
40
|
+
expect(connectFactor("bar", "foo")).toBe("bar-foo");
|
|
41
|
+
expect(fname(1)).toBe("f1");
|
|
42
|
+
expect(namespaceHash("modern", "!@#$%^&")).toBe("modern/!@#$%^&");
|
|
43
|
+
});
|
|
44
|
+
it("should create or destroy instance correctly", () => {
|
|
45
|
+
const ins1 = createCache();
|
|
46
|
+
const ins2 = createCache();
|
|
47
|
+
expect(ins1 === ins2).toBe(true);
|
|
48
|
+
destroyCache();
|
|
49
|
+
const ins3 = createCache();
|
|
50
|
+
expect(ins1 === ins3).toBe(false);
|
|
51
|
+
expect(ins2 === ins3).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
it("should return function correctly", () => {
|
|
54
|
+
const urlParams = (() => new url.URLSearchParams())();
|
|
55
|
+
urlParams.set("name", "modern");
|
|
56
|
+
const getParam = valueFactory(urlParams);
|
|
57
|
+
expect(getParam("name")).toBe("modern");
|
|
58
|
+
const headers = { age: "12345" };
|
|
59
|
+
const getHeader = valueFactory(headers);
|
|
60
|
+
expect(getHeader("age")).toBe("12345");
|
|
61
|
+
});
|
|
62
|
+
it("should add target html content", () => {
|
|
63
|
+
const contentNoHead = "<div>123</div>";
|
|
64
|
+
const html = cacheAddition(contentNoHead, Math.random().toString());
|
|
65
|
+
expect(html).toBe(contentNoHead);
|
|
66
|
+
const contentWithHead = "<head></head><div>123</div>";
|
|
67
|
+
const hash = Math.random().toString();
|
|
68
|
+
const htmlWithHead = cacheAddition(contentWithHead, hash);
|
|
69
|
+
expect(htmlWithHead).toBe(
|
|
70
|
+
`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
it("should only invoke func one time", () => __async(exports, null, function* () {
|
|
74
|
+
let index = 0;
|
|
75
|
+
const fn = withCoalescedInvoke(
|
|
76
|
+
() => __async(exports, null, function* () {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
index += 1;
|
|
80
|
+
resolve(index);
|
|
81
|
+
}, 500);
|
|
82
|
+
});
|
|
83
|
+
})
|
|
84
|
+
);
|
|
85
|
+
const key = "test";
|
|
86
|
+
const [res1, res2] = yield Promise.all([fn(key, []), fn(key, [])]);
|
|
87
|
+
expect(res1.isOrigin && res2.isOrigin).toBe(false);
|
|
88
|
+
expect(res1.isOrigin || res2.isOrigin).toBe(true);
|
|
89
|
+
expect(res1.value).toBe(1);
|
|
90
|
+
expect(res2.value).toBe(1);
|
|
91
|
+
}));
|
|
92
|
+
it("should invoke sync or async", () => __async(exports, null, function* () {
|
|
93
|
+
const foo = "";
|
|
94
|
+
const async = yield maybeSync(
|
|
95
|
+
() => new Promise((resolve) => {
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
resolve(foo);
|
|
98
|
+
}, 100);
|
|
99
|
+
})
|
|
100
|
+
)(false);
|
|
101
|
+
expect(async).toBeUndefined();
|
|
102
|
+
const sync = yield maybeSync(
|
|
103
|
+
() => new Promise((resolve) => {
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
resolve(foo);
|
|
106
|
+
}, 100);
|
|
107
|
+
})
|
|
108
|
+
)(true);
|
|
109
|
+
expect(sync).toBe(foo);
|
|
110
|
+
}));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
export default require_cache_fun_test();
|
|
@@ -1,233 +1,254 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
interval: 10,
|
|
16
|
-
staleLimit: false,
|
|
17
|
-
level: 0,
|
|
18
|
-
fallback: false,
|
|
19
|
-
matches: null
|
|
20
|
-
}, config);
|
|
21
|
-
|
|
22
|
-
jest.setTimeout(60000);
|
|
23
|
-
describe('cache', () => {
|
|
24
|
-
it('should cache correctly', async () => {
|
|
25
|
-
destroyCache();
|
|
26
|
-
const cache = createCache();
|
|
27
|
-
const context = {
|
|
28
|
-
entry: '',
|
|
29
|
-
pathname: '',
|
|
30
|
-
query: {},
|
|
31
|
-
headers: {}
|
|
32
|
-
};
|
|
33
|
-
const content = 'hello';
|
|
34
|
-
const cacheConfig = createCacheConfig();
|
|
35
|
-
await cache.set(context, content, cacheConfig, true);
|
|
36
|
-
const cacheResult = await cache.get(context);
|
|
37
|
-
expect(cacheResult).not.toBe(null);
|
|
38
|
-
expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe('hello');
|
|
39
|
-
});
|
|
40
|
-
it('should ignore cache set when cache config not exist', async () => {
|
|
41
|
-
destroyCache();
|
|
42
|
-
destroyCache();
|
|
43
|
-
const cache = createCache();
|
|
44
|
-
const context = {
|
|
45
|
-
entry: '',
|
|
46
|
-
pathname: '',
|
|
47
|
-
query: {},
|
|
48
|
-
headers: {}
|
|
49
|
-
};
|
|
50
|
-
const content = 'hello';
|
|
51
|
-
const shouldCache = await cache.set(context, content, null, true);
|
|
52
|
-
expect(shouldCache).toBe(false);
|
|
53
|
-
});
|
|
54
|
-
it('should calcual cache key error', async () => {
|
|
55
|
-
destroyCache();
|
|
56
|
-
const cache = createCache();
|
|
57
|
-
const content = 'hello';
|
|
58
|
-
|
|
59
|
-
for (const config of errorConfiguration) {
|
|
60
|
-
const cacheConfig = createCacheConfig(config);
|
|
61
|
-
const tmpEntry = Math.random().toString();
|
|
62
|
-
const context = {
|
|
63
|
-
entry: tmpEntry,
|
|
64
|
-
pathname: '',
|
|
65
|
-
query: {},
|
|
66
|
-
headers: {}
|
|
67
|
-
};
|
|
68
|
-
const shouldCache = await cache.set(context, content, cacheConfig);
|
|
69
|
-
expect(shouldCache).toBe(false);
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __spreadValues = (a, b) => {
|
|
8
|
+
for (var prop in b || (b = {}))
|
|
9
|
+
if (__hasOwnProp.call(b, prop))
|
|
10
|
+
__defNormalProp(a, prop, b[prop]);
|
|
11
|
+
if (__getOwnPropSymbols)
|
|
12
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
13
|
+
if (__propIsEnum.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
70
15
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
level: 1,
|
|
84
|
-
includes: {
|
|
85
|
-
query: ['name']
|
|
16
|
+
return a;
|
|
17
|
+
};
|
|
18
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
19
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
20
|
+
};
|
|
21
|
+
var __async = (__this, __arguments, generator) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
var fulfilled = (value) => {
|
|
24
|
+
try {
|
|
25
|
+
step(generator.next(value));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
86
28
|
}
|
|
87
|
-
});
|
|
88
|
-
await cache.set(context, content, cacheConfig, true);
|
|
89
|
-
const context_req = {
|
|
90
|
-
entry: '',
|
|
91
|
-
pathname: '/home',
|
|
92
|
-
query: {},
|
|
93
|
-
headers: {}
|
|
94
|
-
};
|
|
95
|
-
const cacheResult = await cache.get(context_req);
|
|
96
|
-
expect(cacheResult).toBe(null);
|
|
97
|
-
});
|
|
98
|
-
it('should get nothing for diff cacheHash', async () => {
|
|
99
|
-
destroyCache();
|
|
100
|
-
const cache = createCache();
|
|
101
|
-
const context = {
|
|
102
|
-
entry: '',
|
|
103
|
-
pathname: '',
|
|
104
|
-
query: {},
|
|
105
|
-
headers: {}
|
|
106
29
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
30
|
+
var rejected = (value) => {
|
|
31
|
+
try {
|
|
32
|
+
step(generator.throw(value));
|
|
33
|
+
} catch (e) {
|
|
34
|
+
reject(e);
|
|
112
35
|
}
|
|
113
|
-
});
|
|
114
|
-
await cache.set(context, content, cacheConfig, true);
|
|
115
|
-
const context_req = {
|
|
116
|
-
entry: '',
|
|
117
|
-
pathname: '',
|
|
118
|
-
query: {
|
|
119
|
-
name: 'zll'
|
|
120
|
-
},
|
|
121
|
-
headers: {}
|
|
122
36
|
};
|
|
123
|
-
|
|
124
|
-
|
|
37
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
38
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
125
39
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
content
|
|
153
|
-
} = baseCacheable;
|
|
154
|
-
const context = {
|
|
155
|
-
entry: '',
|
|
156
|
-
pathname: requestOpt.url,
|
|
157
|
-
query: requestOpt.query,
|
|
158
|
-
headers: requestOpt.headers
|
|
159
|
-
};
|
|
160
|
-
await cache.set(context, content, createCacheConfig(cacheConfig), true);
|
|
161
|
-
const matchContext = {
|
|
162
|
-
entry: '',
|
|
163
|
-
pathname: matchOne.url,
|
|
164
|
-
query: matchOne.query,
|
|
165
|
-
headers: matchOne.headers
|
|
166
|
-
};
|
|
167
|
-
const cacheResult = await cache.get(matchContext);
|
|
168
|
-
expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
|
|
169
|
-
|
|
170
|
-
for (const notMatch of other) {
|
|
171
|
-
const notMatchContext = {
|
|
172
|
-
entry: '',
|
|
173
|
-
pathname: notMatch.url,
|
|
174
|
-
query: notMatch.query,
|
|
175
|
-
headers: notMatch.headers
|
|
40
|
+
};
|
|
41
|
+
import { createCache, destroyCache } from "../spr";
|
|
42
|
+
import { errorConfiguration } from "./error-configuration";
|
|
43
|
+
import { cacheabelAry } from "./cacheable";
|
|
44
|
+
import { matchedCacheableAry } from "./matched-cache";
|
|
45
|
+
var require_cache_test = __commonJS({
|
|
46
|
+
"src/libs/render/cache/__tests__/cache.test.ts"(exports) {
|
|
47
|
+
const createCacheConfig = (config = {}) => __spreadValues({
|
|
48
|
+
excludes: null,
|
|
49
|
+
includes: null,
|
|
50
|
+
interval: 10,
|
|
51
|
+
staleLimit: false,
|
|
52
|
+
level: 0,
|
|
53
|
+
fallback: false,
|
|
54
|
+
matches: null
|
|
55
|
+
}, config);
|
|
56
|
+
jest.setTimeout(6e4);
|
|
57
|
+
describe("cache", () => {
|
|
58
|
+
it("should cache correctly", () => __async(exports, null, function* () {
|
|
59
|
+
destroyCache();
|
|
60
|
+
const cache = createCache();
|
|
61
|
+
const context = {
|
|
62
|
+
entry: "",
|
|
63
|
+
pathname: "",
|
|
64
|
+
query: {},
|
|
65
|
+
headers: {}
|
|
176
66
|
};
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
67
|
+
const content = "hello";
|
|
68
|
+
const cacheConfig = createCacheConfig();
|
|
69
|
+
yield cache.set(context, content, cacheConfig, true);
|
|
70
|
+
const cacheResult = yield cache.get(context);
|
|
71
|
+
expect(cacheResult).not.toBe(null);
|
|
72
|
+
expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
|
|
73
|
+
}));
|
|
74
|
+
it("should ignore cache set when cache config not exist", () => __async(exports, null, function* () {
|
|
75
|
+
destroyCache();
|
|
76
|
+
destroyCache();
|
|
77
|
+
const cache = createCache();
|
|
78
|
+
const context = {
|
|
79
|
+
entry: "",
|
|
80
|
+
pathname: "",
|
|
81
|
+
query: {},
|
|
82
|
+
headers: {}
|
|
83
|
+
};
|
|
84
|
+
const content = "hello";
|
|
85
|
+
const shouldCache = yield cache.set(context, content, null, true);
|
|
86
|
+
expect(shouldCache).toBe(false);
|
|
87
|
+
}));
|
|
88
|
+
it("should calcual cache key error", () => __async(exports, null, function* () {
|
|
89
|
+
destroyCache();
|
|
90
|
+
const cache = createCache();
|
|
91
|
+
const content = "hello";
|
|
92
|
+
for (const config of errorConfiguration) {
|
|
93
|
+
const cacheConfig = createCacheConfig(config);
|
|
94
|
+
const tmpEntry = Math.random().toString();
|
|
95
|
+
const context = {
|
|
96
|
+
entry: tmpEntry,
|
|
97
|
+
pathname: "",
|
|
98
|
+
query: {},
|
|
99
|
+
headers: {}
|
|
100
|
+
};
|
|
101
|
+
const shouldCache = yield cache.set(context, content, cacheConfig);
|
|
102
|
+
expect(shouldCache).toBe(false);
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
105
|
+
it("should get nothing for diff requestKey", () => __async(exports, null, function* () {
|
|
106
|
+
destroyCache();
|
|
107
|
+
const cache = createCache();
|
|
108
|
+
const context = {
|
|
109
|
+
entry: "",
|
|
110
|
+
pathname: "",
|
|
111
|
+
query: {},
|
|
112
|
+
headers: {}
|
|
113
|
+
};
|
|
114
|
+
const content = "hello";
|
|
115
|
+
const cacheConfig = createCacheConfig({
|
|
116
|
+
level: 1,
|
|
117
|
+
includes: { query: ["name"] }
|
|
118
|
+
});
|
|
119
|
+
yield cache.set(context, content, cacheConfig, true);
|
|
120
|
+
const context_req = {
|
|
121
|
+
entry: "",
|
|
122
|
+
pathname: "/home",
|
|
123
|
+
query: {},
|
|
124
|
+
headers: {}
|
|
125
|
+
};
|
|
126
|
+
const cacheResult = yield cache.get(context_req);
|
|
127
|
+
expect(cacheResult).toBe(null);
|
|
128
|
+
}));
|
|
129
|
+
it("should get nothing for diff cacheHash", () => __async(exports, null, function* () {
|
|
130
|
+
destroyCache();
|
|
131
|
+
const cache = createCache();
|
|
132
|
+
const context = {
|
|
133
|
+
entry: "",
|
|
134
|
+
pathname: "",
|
|
135
|
+
query: {},
|
|
136
|
+
headers: {}
|
|
137
|
+
};
|
|
138
|
+
const content = "hello";
|
|
139
|
+
const cacheConfig = createCacheConfig({
|
|
140
|
+
level: 1,
|
|
141
|
+
includes: { query: ["name"] }
|
|
142
|
+
});
|
|
143
|
+
yield cache.set(context, content, cacheConfig, true);
|
|
144
|
+
const context_req = {
|
|
145
|
+
entry: "",
|
|
146
|
+
pathname: "",
|
|
147
|
+
query: { name: "zll" },
|
|
148
|
+
headers: {}
|
|
149
|
+
};
|
|
150
|
+
const cacheResult = yield cache.get(context_req);
|
|
151
|
+
expect(cacheResult).toBe(null);
|
|
152
|
+
}));
|
|
153
|
+
it("should get cache correctly", () => __async(exports, null, function* () {
|
|
154
|
+
destroyCache();
|
|
155
|
+
const cache = createCache();
|
|
156
|
+
for (const cacheable of cacheabelAry) {
|
|
157
|
+
const context = {
|
|
158
|
+
entry: "",
|
|
159
|
+
pathname: cacheable.requestOpt.url,
|
|
160
|
+
query: cacheable.requestOpt.query || {},
|
|
161
|
+
headers: cacheable.requestOpt.headers || {}
|
|
162
|
+
};
|
|
163
|
+
const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
|
|
164
|
+
yield cache.set(context, cacheable.content, cacheConfig, true);
|
|
165
|
+
const cacheResult = yield cache.get(context);
|
|
166
|
+
expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
|
|
167
|
+
}
|
|
168
|
+
}));
|
|
169
|
+
it("should match cache correctly", () => __async(exports, null, function* () {
|
|
170
|
+
destroyCache();
|
|
171
|
+
const cache = createCache();
|
|
172
|
+
for (const cacheable of matchedCacheableAry) {
|
|
173
|
+
const [baseCacheable, matchOne, ...other] = cacheable;
|
|
174
|
+
const { requestOpt = {}, cacheConfig, content } = baseCacheable;
|
|
175
|
+
const context = {
|
|
176
|
+
entry: "",
|
|
177
|
+
pathname: requestOpt.url,
|
|
178
|
+
query: requestOpt.query,
|
|
179
|
+
headers: requestOpt.headers
|
|
180
|
+
};
|
|
181
|
+
yield cache.set(context, content, createCacheConfig(cacheConfig), true);
|
|
182
|
+
const matchContext = {
|
|
183
|
+
entry: "",
|
|
184
|
+
pathname: matchOne.url,
|
|
185
|
+
query: matchOne.query,
|
|
186
|
+
headers: matchOne.headers
|
|
187
|
+
};
|
|
188
|
+
const cacheResult = yield cache.get(matchContext);
|
|
189
|
+
expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
|
|
190
|
+
for (const notMatch of other) {
|
|
191
|
+
const notMatchContext = {
|
|
192
|
+
entry: "",
|
|
193
|
+
pathname: notMatch.url,
|
|
194
|
+
query: notMatch.query,
|
|
195
|
+
headers: notMatch.headers
|
|
196
|
+
};
|
|
197
|
+
const nothing = yield cache.get(notMatchContext);
|
|
198
|
+
expect(nothing).toBe(null);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}));
|
|
202
|
+
it("should stale cache correctly", () => __async(exports, null, function* () {
|
|
203
|
+
destroyCache();
|
|
204
|
+
const cache = createCache();
|
|
205
|
+
const context = {
|
|
206
|
+
entry: "",
|
|
207
|
+
pathname: "",
|
|
208
|
+
query: {},
|
|
209
|
+
headers: {}
|
|
210
|
+
};
|
|
211
|
+
const config = createCacheConfig({ interval: 5 });
|
|
212
|
+
const content = "hello";
|
|
213
|
+
const shouldCache = yield cache.set(context, content, config, true);
|
|
214
|
+
expect(shouldCache.value).toBe(true);
|
|
215
|
+
const freshResult = yield cache.get(context);
|
|
216
|
+
expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
|
|
217
|
+
yield new Promise((resolve) => {
|
|
218
|
+
setTimeout(() => {
|
|
219
|
+
resolve();
|
|
220
|
+
}, 6e3);
|
|
221
|
+
});
|
|
222
|
+
const staleResult = yield cache.get(context);
|
|
223
|
+
expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
|
|
224
|
+
}));
|
|
225
|
+
it("should garbage cache correctly", () => __async(exports, null, function* () {
|
|
226
|
+
destroyCache();
|
|
227
|
+
const cache = createCache();
|
|
228
|
+
const context = {
|
|
229
|
+
entry: "",
|
|
230
|
+
pathname: "",
|
|
231
|
+
query: {},
|
|
232
|
+
headers: {}
|
|
233
|
+
};
|
|
234
|
+
const config = createCacheConfig({
|
|
235
|
+
interval: 3,
|
|
236
|
+
staleLimit: 8
|
|
237
|
+
});
|
|
238
|
+
const content = "hello";
|
|
239
|
+
const shouldCache = yield cache.set(context, content, config, true);
|
|
240
|
+
expect(shouldCache.value).toBe(true);
|
|
241
|
+
const freshResult = yield cache.get(context);
|
|
242
|
+
expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
|
|
243
|
+
yield new Promise((resolve) => {
|
|
244
|
+
setTimeout(() => {
|
|
245
|
+
resolve();
|
|
246
|
+
}, 1e4);
|
|
247
|
+
});
|
|
248
|
+
const staleResult = yield cache.get(context);
|
|
249
|
+
expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
|
|
250
|
+
}));
|
|
229
251
|
});
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
export default require_cache_test();
|