@celljs/web 4.5.1 → 4.6.1
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/esm/common/utils/url-util.d.ts +0 -2
- package/esm/common/utils/url-util.js +0 -14
- package/esm/common/utils/url-util.js.map +1 -1
- package/esm/node/application/dev-application-entry.d.ts +0 -1
- package/esm/node/application/dev-application-entry.js +0 -1
- package/esm/node/application/dev-application-entry.js.map +1 -1
- package/lib/common/utils/url-util.d.ts +0 -2
- package/lib/common/utils/url-util.d.ts.map +1 -1
- package/lib/common/utils/url-util.js +0 -14
- package/lib/common/utils/url-util.js.map +1 -1
- package/lib/node/application/dev-application-entry.d.ts +0 -1
- package/lib/node/application/dev-application-entry.d.ts.map +1 -1
- package/lib/node/application/dev-application-entry.js +0 -1
- package/lib/node/application/dev-application-entry.js.map +1 -1
- package/lib/node/web-http.spec.d.ts +2 -0
- package/lib/node/web-http.spec.d.ts.map +1 -0
- package/lib/node/web-http.spec.js +200 -0
- package/lib/node/web-http.spec.js.map +1 -0
- package/lib/package.spec.js +17 -2
- package/lib/package.spec.js.map +1 -1
- package/package.json +3 -3
- package/src/common/utils/url-util.ts +0 -15
- package/src/node/application/dev-application-entry.ts +0 -1
- package/src/node/web-http.spec.ts +192 -0
- package/src/package.spec.ts +16 -3
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { ConfigUtil, ContainerUtil } from '@celljs/core';
|
|
2
|
-
import { ENDPOINT } from '../constants.js';
|
|
3
|
-
import { PathResolver } from '../resolver/index.js';
|
|
4
1
|
export var UrlUtil;
|
|
5
2
|
(function (UrlUtil) {
|
|
6
3
|
function isValidRedirectUrl(url) {
|
|
@@ -18,16 +15,5 @@ export var UrlUtil;
|
|
|
18
15
|
return /^[a-z0-9.+-]+:\/\/.*/i.test(url);
|
|
19
16
|
}
|
|
20
17
|
UrlUtil.isAbsoluteUrl = isAbsoluteUrl;
|
|
21
|
-
async function getUrl(...paths) {
|
|
22
|
-
const endpoint = ConfigUtil.get(ENDPOINT);
|
|
23
|
-
const pathResolver = ContainerUtil.get(PathResolver);
|
|
24
|
-
return pathResolver.resolve(endpoint, await pathResolver.resolve(...paths));
|
|
25
|
-
}
|
|
26
|
-
UrlUtil.getUrl = getUrl;
|
|
27
|
-
async function getPath(...paths) {
|
|
28
|
-
const pathResolver = ContainerUtil.get(PathResolver);
|
|
29
|
-
return pathResolver.resolve(...paths);
|
|
30
|
-
}
|
|
31
|
-
UrlUtil.getPath = getPath;
|
|
32
18
|
})(UrlUtil || (UrlUtil = {}));
|
|
33
19
|
//# sourceMappingURL=url-util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url-util.js","sourceRoot":"","sources":["../../../src/common/utils/url-util.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"url-util.js","sourceRoot":"","sources":["../../../src/common/utils/url-util.ts"],"names":[],"mappings":"AAAA,MAAM,KAAW,OAAO,CAiBvB;AAjBD,WAAiB,OAAO;IAEpB,SAAgB,kBAAkB,CAAC,GAAY;QAC3C,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;IAC9D,CAAC;IAFe,QAAA,kBAAkB,qBAEjC,CAAA;IAED;;;OAGG;IACH,SAAgB,aAAa,CAAC,GAAY;QACtC,IAAI,CAAC,GAAG,EAAE,CAAC;YACP,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,OAAO,uBAAuB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7C,CAAC;IALe,QAAA,aAAa,gBAK5B,CAAA;AAEL,CAAC,EAjBgB,OAAO,KAAP,OAAO,QAiBvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev-application-entry.js","sourceRoot":"","sources":["../../../src/node/application/dev-application-entry.ts"],"names":[],"mappings":"AAAA,cAAc,qDAAqD,CAAC;AACpE,cAAc,
|
|
1
|
+
{"version":3,"file":"dev-application-entry.js","sourceRoot":"","sources":["../../../src/node/application/dev-application-entry.ts"],"names":[],"mappings":"AAAA,cAAc,qDAAqD,CAAC;AACpE,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,qCAAqC,CAAC;AACpD,cAAc,uBAAuB,CAAC"}
|
|
@@ -5,7 +5,5 @@ export declare namespace UrlUtil {
|
|
|
5
5
|
* defined in RFC 1738.
|
|
6
6
|
*/
|
|
7
7
|
function isAbsoluteUrl(url?: string): boolean;
|
|
8
|
-
function getUrl(...paths: string[]): Promise<string>;
|
|
9
|
-
function getPath(...paths: string[]): Promise<string>;
|
|
10
8
|
}
|
|
11
9
|
//# sourceMappingURL=url-util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url-util.d.ts","sourceRoot":"","sources":["../../../src/common/utils/url-util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"url-util.d.ts","sourceRoot":"","sources":["../../../src/common/utils/url-util.ts"],"names":[],"mappings":"AAAA,yBAAiB,OAAO,CAAC;IAErB,SAAgB,kBAAkB,CAAC,GAAG,CAAC,EAAE,MAAM,4BAE9C;IAED;;;OAGG;IACH,SAAgB,aAAa,CAAC,GAAG,CAAC,EAAE,MAAM,WAKzC;CAEJ"}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UrlUtil = void 0;
|
|
4
|
-
const core_1 = require("@celljs/core");
|
|
5
|
-
const constants_1 = require("../constants");
|
|
6
|
-
const resolver_1 = require("../resolver");
|
|
7
4
|
var UrlUtil;
|
|
8
5
|
(function (UrlUtil) {
|
|
9
6
|
function isValidRedirectUrl(url) {
|
|
@@ -21,16 +18,5 @@ var UrlUtil;
|
|
|
21
18
|
return /^[a-z0-9.+-]+:\/\/.*/i.test(url);
|
|
22
19
|
}
|
|
23
20
|
UrlUtil.isAbsoluteUrl = isAbsoluteUrl;
|
|
24
|
-
async function getUrl(...paths) {
|
|
25
|
-
const endpoint = core_1.ConfigUtil.get(constants_1.ENDPOINT);
|
|
26
|
-
const pathResolver = core_1.ContainerUtil.get(resolver_1.PathResolver);
|
|
27
|
-
return pathResolver.resolve(endpoint, await pathResolver.resolve(...paths));
|
|
28
|
-
}
|
|
29
|
-
UrlUtil.getUrl = getUrl;
|
|
30
|
-
async function getPath(...paths) {
|
|
31
|
-
const pathResolver = core_1.ContainerUtil.get(resolver_1.PathResolver);
|
|
32
|
-
return pathResolver.resolve(...paths);
|
|
33
|
-
}
|
|
34
|
-
UrlUtil.getPath = getPath;
|
|
35
21
|
})(UrlUtil || (exports.UrlUtil = UrlUtil = {}));
|
|
36
22
|
//# sourceMappingURL=url-util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url-util.js","sourceRoot":"","sources":["../../../src/common/utils/url-util.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"url-util.js","sourceRoot":"","sources":["../../../src/common/utils/url-util.ts"],"names":[],"mappings":";;;AAAA,IAAiB,OAAO,CAiBvB;AAjBD,WAAiB,OAAO;IAEpB,SAAgB,kBAAkB,CAAC,GAAY;QAC3C,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;IAC9D,CAAC;IAFe,QAAA,kBAAkB,qBAEjC,CAAA;IAED;;;OAGG;IACH,SAAgB,aAAa,CAAC,GAAY;QACtC,IAAI,CAAC,GAAG,EAAE,CAAC;YACP,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,OAAO,uBAAuB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7C,CAAC;IALe,QAAA,aAAa,gBAK5B,CAAA;AAEL,CAAC,EAjBgB,OAAO,aAAP,OAAO,GAAP,OAAO,QAiBvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev-application-entry.d.ts","sourceRoot":"","sources":["../../../src/node/application/dev-application-entry.ts"],"names":[],"mappings":"AAAA,cAAc,qDAAqD,CAAC;AACpE,cAAc,
|
|
1
|
+
{"version":3,"file":"dev-application-entry.d.ts","sourceRoot":"","sources":["../../../src/node/application/dev-application-entry.ts"],"names":[],"mappings":"AAAA,cAAc,qDAAqD,CAAC;AACpE,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,qCAAqC,CAAC;AACpD,cAAc,uBAAuB,CAAC"}
|
|
@@ -15,7 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("@celljs/core/lib/common/container/dynamic-container"), exports);
|
|
18
|
-
__exportStar(require("@celljs/core/lib/common/container/container-provider"), exports);
|
|
19
18
|
__exportStar(require("../context"), exports);
|
|
20
19
|
__exportStar(require("../dispatcher"), exports);
|
|
21
20
|
__exportStar(require("@celljs/core/lib/common/application"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev-application-entry.js","sourceRoot":"","sources":["../../../src/node/application/dev-application-entry.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sFAAoE;AACpE,
|
|
1
|
+
{"version":3,"file":"dev-application-entry.js","sourceRoot":"","sources":["../../../src/node/application/dev-application-entry.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sFAAoE;AACpE,6CAA2B;AAC3B,gDAA8B;AAC9B,sEAAoD;AACpD,wDAAsC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-http.spec.d.ts","sourceRoot":"","sources":["../../src/node/web-http.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const bun_test_1 = require("bun:test");
|
|
13
|
+
const chai_1 = require("chai");
|
|
14
|
+
const static_module_1 = __importDefault(require("@celljs/core/lib/common/static-module"));
|
|
15
|
+
const static_module_2 = __importDefault(require("@celljs/core/lib/node/static-module"));
|
|
16
|
+
const module_1 = __importDefault(require("@celljs/http/lib/common/module"));
|
|
17
|
+
const core_1 = require("@celljs/core");
|
|
18
|
+
const module_2 = __importDefault(require("../common/module"));
|
|
19
|
+
const module_3 = __importDefault(require("./module"));
|
|
20
|
+
const context_1 = require("./context");
|
|
21
|
+
const handler_1 = require("./handler");
|
|
22
|
+
const middleware_1 = require("./middleware");
|
|
23
|
+
const compose_1 = require("./middleware/compose");
|
|
24
|
+
const express_middleware_converter_1 = require("./middleware/express-middleware-converter");
|
|
25
|
+
const server_1 = require("./server");
|
|
26
|
+
/**
|
|
27
|
+
* web 的 HTTP 行为约定(阶段 1.E 行为测试):经真实 HTTP 请求断言中间件顺序、会话、健康检查和没有处理器时的 404。
|
|
28
|
+
* 配置是 core / web / http 的 cell.yml 合并后的默认值。
|
|
29
|
+
*/
|
|
30
|
+
const DEFAULT_CONFIG = {
|
|
31
|
+
component: { proxy: false }, aop: { enabled: false }, logger: { level: 'error' }, tenant: { enabled: false },
|
|
32
|
+
server: { path: '/', port: 3000, host: '127.0.0.1' },
|
|
33
|
+
web: { session: { autoCommit: true, maxAge: 86400000, sessionIdKey: 'cell:sessionId', sessionKey: 'cell:session' }, trace: { requestField: 'X-Cell-Trace-ID', responseField: 'X-Cell-Trace-ID' }, health: { url: '/health', method: 'GET' }, cookies: { keys: ['abcdef'] }, route: { cacheSize: 500 } },
|
|
34
|
+
http: { client: { headers: {} } },
|
|
35
|
+
};
|
|
36
|
+
let server;
|
|
37
|
+
let app;
|
|
38
|
+
(0, bun_test_1.afterEach)(async () => {
|
|
39
|
+
await new Promise(r => (server ? server.close(() => r()) : r()));
|
|
40
|
+
await (app === null || app === void 0 ? void 0 : app.stop());
|
|
41
|
+
server = undefined;
|
|
42
|
+
app = undefined;
|
|
43
|
+
});
|
|
44
|
+
/** Starts an application with the components defined before the call; a fetch to it. */
|
|
45
|
+
async function serve() {
|
|
46
|
+
core_1.currentThis.cellProps = structuredClone(DEFAULT_CONFIG);
|
|
47
|
+
const container = new core_1.Container(static_module_1.default, static_module_2.default, module_1.default, module_2.default, module_3.default, (0, core_1.autoBind)());
|
|
48
|
+
app = container.get(core_1.Application);
|
|
49
|
+
await app.start();
|
|
50
|
+
server = server_1.HttpServerUtil.createHttpServer(server_1.HttpServerUtil.createHandler(container));
|
|
51
|
+
await new Promise(r => server.listen(0, '127.0.0.1', r));
|
|
52
|
+
const base = `http://127.0.0.1:${server.address().port}`;
|
|
53
|
+
return (path, init) => fetch(base + path, init);
|
|
54
|
+
}
|
|
55
|
+
/** A handler for every request below `prefix`, answering what `answer` returns. */
|
|
56
|
+
function handler(prefix, answer) {
|
|
57
|
+
let Answering = class Answering {
|
|
58
|
+
constructor() {
|
|
59
|
+
this.priority = 100;
|
|
60
|
+
}
|
|
61
|
+
async canHandle() {
|
|
62
|
+
return context_1.Context.getRequest().path.startsWith(prefix);
|
|
63
|
+
}
|
|
64
|
+
async handle() {
|
|
65
|
+
const response = context_1.Context.getResponse();
|
|
66
|
+
response.setHeader('content-type', 'application/json');
|
|
67
|
+
response.body = JSON.stringify(await answer(context_1.Context.getCurrent()));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
Answering = __decorate([
|
|
71
|
+
(0, core_1.Component)(handler_1.HandlerAdapter)
|
|
72
|
+
], Answering);
|
|
73
|
+
return Answering;
|
|
74
|
+
}
|
|
75
|
+
(0, bun_test_1.describe)('web 中间件', () => {
|
|
76
|
+
(0, bun_test_1.it)('按 priority 从高到低进入,出来的顺序相反;处理器在最里层', async () => {
|
|
77
|
+
const order = [];
|
|
78
|
+
for (const [name, priority] of [['low', 10], ['high', 9000], ['mid', 500]]) {
|
|
79
|
+
let Recording = class Recording {
|
|
80
|
+
constructor() {
|
|
81
|
+
this.priority = priority;
|
|
82
|
+
}
|
|
83
|
+
async handle(ctx, next) {
|
|
84
|
+
order.push(`>${name}`);
|
|
85
|
+
await next();
|
|
86
|
+
order.push(`<${name}`);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
Recording = __decorate([
|
|
90
|
+
(0, core_1.Component)(middleware_1.Middleware)
|
|
91
|
+
], Recording);
|
|
92
|
+
void Recording;
|
|
93
|
+
}
|
|
94
|
+
handler('/order', () => order.push('handler'));
|
|
95
|
+
const f = await serve();
|
|
96
|
+
(0, chai_1.expect)((await f('/order')).status).to.equal(200);
|
|
97
|
+
const mine = order.filter(step => /low|mid|high|handler/.test(step));
|
|
98
|
+
(0, chai_1.expect)(mine).to.deep.equal(['>high', '>mid', '>low', 'handler', '<low', '<mid', '<high']);
|
|
99
|
+
});
|
|
100
|
+
(0, bun_test_1.it)('中间件不调用 next 时请求在它这里结束', async () => {
|
|
101
|
+
let Gate = class Gate {
|
|
102
|
+
constructor() {
|
|
103
|
+
this.priority = 9000;
|
|
104
|
+
}
|
|
105
|
+
async handle(ctx, next) {
|
|
106
|
+
if (ctx.request.headers['x-pass'] !== 'yes') {
|
|
107
|
+
ctx.response.statusCode = 403;
|
|
108
|
+
ctx.response.end('closed');
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
await next();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
Gate = __decorate([
|
|
115
|
+
(0, core_1.Component)(middleware_1.Middleware)
|
|
116
|
+
], Gate);
|
|
117
|
+
let handled = 0;
|
|
118
|
+
handler('/gate', () => ++handled);
|
|
119
|
+
const f = await serve();
|
|
120
|
+
const closed = await f('/gate');
|
|
121
|
+
(0, chai_1.expect)(closed.status).to.equal(403);
|
|
122
|
+
(0, chai_1.expect)(await closed.text()).to.equal('closed');
|
|
123
|
+
(0, chai_1.expect)(handled).to.equal(0);
|
|
124
|
+
(0, chai_1.expect)((await f('/gate', { headers: { 'x-pass': 'yes' } })).status).to.equal(200);
|
|
125
|
+
(0, chai_1.expect)([Gate]).to.have.length(1);
|
|
126
|
+
});
|
|
127
|
+
(0, bun_test_1.it)('没有处理器的请求回答 404', async () => {
|
|
128
|
+
const f = await serve();
|
|
129
|
+
(0, chai_1.expect)((await f('/nothing-here')).status).to.equal(404);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
(0, bun_test_1.describe)('web 会话', () => {
|
|
133
|
+
(0, bun_test_1.it)('会话写入后自动提交,下一次请求带上 cookie 就拿到同一份会话', async () => {
|
|
134
|
+
handler('/count', () => {
|
|
135
|
+
var _a;
|
|
136
|
+
const session = context_1.Context.getSession();
|
|
137
|
+
session.count = ((_a = session.count) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
138
|
+
return { count: session.count };
|
|
139
|
+
});
|
|
140
|
+
const f = await serve();
|
|
141
|
+
const first = await f('/count');
|
|
142
|
+
(0, chai_1.expect)(await first.json()).to.deep.equal({ count: 1 });
|
|
143
|
+
const cookies = first.headers.getSetCookie().map(c => c.split(';')[0]).join('; ');
|
|
144
|
+
(0, chai_1.expect)(cookies).to.contain('cell:sessionId=');
|
|
145
|
+
(0, chai_1.expect)(await (await f('/count', { headers: { cookie: cookies } })).json()).to.deep.equal({ count: 2 });
|
|
146
|
+
// 没有 cookie:新会话
|
|
147
|
+
(0, chai_1.expect)(await (await f('/count')).json()).to.deep.equal({ count: 1 });
|
|
148
|
+
});
|
|
149
|
+
(0, bun_test_1.it)('cookie 被篡改(签名不对)时不认,按新会话处理', async () => {
|
|
150
|
+
handler('/visit', () => {
|
|
151
|
+
var _a;
|
|
152
|
+
const session = context_1.Context.getSession();
|
|
153
|
+
session.visits = ((_a = session.visits) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
154
|
+
return { visits: session.visits };
|
|
155
|
+
});
|
|
156
|
+
const f = await serve();
|
|
157
|
+
const first = await f('/visit');
|
|
158
|
+
const cookies = first.headers.getSetCookie().map(c => c.split(';')[0]);
|
|
159
|
+
(0, chai_1.expect)(cookies.some(c => c.startsWith('cell:session.sig='))).to.equal(true);
|
|
160
|
+
// The data of the session, with another value and the signature of the old one.
|
|
161
|
+
const forgedData = Buffer.from(JSON.stringify({ id: 'x', visits: 41 })).toString('base64');
|
|
162
|
+
const forged = cookies.map(c => (c.startsWith('cell:session=') ? `cell:session=${forgedData}` : c)).join('; ');
|
|
163
|
+
(0, chai_1.expect)(await (await f('/visit', { headers: { cookie: forged } })).json()).to.deep.equal({ visits: 1 });
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
(0, bun_test_1.describe)('web 健康检查', () => {
|
|
167
|
+
(0, bun_test_1.it)('GET /health 回答 UP 的 JSON', async () => {
|
|
168
|
+
const f = await serve();
|
|
169
|
+
const res = await f('/health');
|
|
170
|
+
(0, chai_1.expect)(res.status).to.equal(200);
|
|
171
|
+
(0, chai_1.expect)(res.headers.get('content-type')).to.contain('application/json');
|
|
172
|
+
(0, chai_1.expect)((await res.json()).status).to.equal('UP');
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
(0, bun_test_1.describe)('compose 与 express 中间件', () => {
|
|
176
|
+
(0, bun_test_1.it)('compose 按顺序调用,最后调用 next;同一个中间件调用两次 next 会被拒绝', async () => {
|
|
177
|
+
const steps = [];
|
|
178
|
+
const mw = (name) => ({ priority: 0, handle: async (ctx, next) => { steps.push(name); await next(); } });
|
|
179
|
+
await (0, compose_1.compose)([mw('a'), mw('b')])({}, { priority: 0, handle: async () => { steps.push('end'); } });
|
|
180
|
+
(0, chai_1.expect)(steps).to.deep.equal(['a', 'b', 'end']);
|
|
181
|
+
const twice = { priority: 0, handle: async (ctx, next) => { await next(); await next(); } };
|
|
182
|
+
const error = await (0, compose_1.compose)([twice])({}, { priority: 0, handle: async () => undefined }).then(() => undefined, e => e);
|
|
183
|
+
(0, chai_1.expect)(error === null || error === void 0 ? void 0 : error.message).to.equal('next() called multiple times');
|
|
184
|
+
});
|
|
185
|
+
(0, bun_test_1.it)('express 风格的中间件:next() 继续,next(err) 让请求失败', async () => {
|
|
186
|
+
const ctx = new context_1.Context({ headers: {} }, {});
|
|
187
|
+
await context_1.Context.run(async () => {
|
|
188
|
+
context_1.Context.setCurrent(ctx);
|
|
189
|
+
const seen = [];
|
|
190
|
+
const passing = (0, express_middleware_converter_1.convertExpressMiddleware)((req, res, next) => next());
|
|
191
|
+
await passing.handle(ctx, async () => { seen.push('next'); });
|
|
192
|
+
(0, chai_1.expect)(seen).to.deep.equal(['next']);
|
|
193
|
+
const failing = (0, express_middleware_converter_1.convertExpressMiddleware)((req, res, next) => next(new Error('denied')));
|
|
194
|
+
const error = await failing.handle(ctx, async () => { seen.push('never'); }).then(() => undefined, e => e);
|
|
195
|
+
(0, chai_1.expect)(error === null || error === void 0 ? void 0 : error.message).to.equal('denied');
|
|
196
|
+
(0, chai_1.expect)(seen).to.deep.equal(['next']);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
//# sourceMappingURL=web-http.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-http.spec.js","sourceRoot":"","sources":["../../src/node/web-http.spec.ts"],"names":[],"mappings":";;;;;;;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
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
const bun_test_1 = require("bun:test");
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
const core_1 = require("@celljs/core");
|
|
8
|
+
const assembly_test_util_1 = require("@celljs/core/lib/common/test/assembly-test-util");
|
|
9
|
+
const static_module_1 = __importDefault(require("@celljs/core/lib/common/static-module"));
|
|
10
|
+
const static_module_2 = __importDefault(require("@celljs/core/lib/node/static-module"));
|
|
11
|
+
const module_1 = __importDefault(require("@celljs/http/lib/common/module"));
|
|
12
|
+
const module_2 = __importDefault(require("./common/module"));
|
|
13
|
+
const module_3 = __importDefault(require("./node/module"));
|
|
14
|
+
core_1.currentThis.cellProps = {};
|
|
15
|
+
(0, bun_test_1.describe)('@celljs/web', () => {
|
|
16
|
+
(0, bun_test_1.it)('its DI module loads after those of its dependencies and every binding resolves', () => {
|
|
17
|
+
const { changed, errors } = assembly_test_util_1.AssemblyTestUtil.check([static_module_1.default, static_module_2.default, module_1.default, module_2.default], module_3.default);
|
|
18
|
+
(0, bun_test_1.expect)(errors).toEqual([]);
|
|
19
|
+
(0, bun_test_1.expect)(changed).toBeGreaterThan(0);
|
|
20
|
+
});
|
|
6
21
|
});
|
|
7
22
|
//# sourceMappingURL=package.spec.js.map
|
package/lib/package.spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":";;;;;AAAA,uCAAgD;AAChD,uCAA2C;AAC3C,wFAAmF;AACnF,0FAAuD;AACvD,wFAAqD;AACrD,4EAAgD;AAChD,6DAAmC;AACnC,2DAAiC;AAEjC,kBAAW,CAAC,SAAS,GAAG,EAAE,CAAC;AAE3B,IAAA,mBAAQ,EAAC,aAAa,EAAE,GAAG,EAAE;IACzB,IAAA,aAAE,EAAC,gFAAgF,EAAE,GAAG,EAAE;QACtF,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,qCAAgB,CAAC,KAAK,CAAC,CAAC,uBAAE,EAAE,uBAAE,EAAE,gBAAE,EAAE,gBAAI,CAAC,EAAE,gBAAI,CAAC,CAAC;QAC7E,IAAA,iBAAM,EAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC3B,IAAA,iBAAM,EAAC,OAAO,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/web",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@celljs/core": "4.
|
|
37
|
-
"@celljs/http": "4.
|
|
36
|
+
"@celljs/core": "4.6.1",
|
|
37
|
+
"@celljs/http": "4.6.1"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { ConfigUtil, ContainerUtil } from '@celljs/core';
|
|
2
|
-
import { ENDPOINT } from '../constants';
|
|
3
|
-
import { PathResolver } from '../resolver';
|
|
4
|
-
|
|
5
1
|
export namespace UrlUtil {
|
|
6
2
|
|
|
7
3
|
export function isValidRedirectUrl(url?: string) {
|
|
@@ -19,15 +15,4 @@ export namespace UrlUtil {
|
|
|
19
15
|
return /^[a-z0-9.+-]+:\/\/.*/i.test(url);
|
|
20
16
|
}
|
|
21
17
|
|
|
22
|
-
export async function getUrl(...paths: string[]) {
|
|
23
|
-
const endpoint = ConfigUtil.get<string>(ENDPOINT);
|
|
24
|
-
const pathResolver = ContainerUtil.get<PathResolver>(PathResolver);
|
|
25
|
-
return pathResolver.resolve(endpoint, await pathResolver.resolve(...paths));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function getPath(...paths: string[]) {
|
|
29
|
-
const pathResolver = ContainerUtil.get<PathResolver>(PathResolver);
|
|
30
|
-
return pathResolver.resolve(...paths);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
18
|
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { afterEach, describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { Server } from 'node:http';
|
|
4
|
+
import { AddressInfo } from 'node:net';
|
|
5
|
+
import coreCommonStatic from '@celljs/core/lib/common/static-module';
|
|
6
|
+
import coreNodeStatic from '@celljs/core/lib/node/static-module';
|
|
7
|
+
import httpCommonModule from '@celljs/http/lib/common/module';
|
|
8
|
+
import { Application, autoBind, Component, Container, currentThis } from '@celljs/core';
|
|
9
|
+
import webCommonModule from '../common/module';
|
|
10
|
+
import webNodeModule from './module';
|
|
11
|
+
import { Context } from './context';
|
|
12
|
+
import { HandlerAdapter } from './handler';
|
|
13
|
+
import { Middleware } from './middleware';
|
|
14
|
+
import { compose } from './middleware/compose';
|
|
15
|
+
import { convertExpressMiddleware } from './middleware/express-middleware-converter';
|
|
16
|
+
import { HttpServerUtil } from './server';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* web 的 HTTP 行为约定(阶段 1.E 行为测试):经真实 HTTP 请求断言中间件顺序、会话、健康检查和没有处理器时的 404。
|
|
20
|
+
* 配置是 core / web / http 的 cell.yml 合并后的默认值。
|
|
21
|
+
*/
|
|
22
|
+
const DEFAULT_CONFIG = {
|
|
23
|
+
component: { proxy: false }, aop: { enabled: false }, logger: { level: 'error' }, tenant: { enabled: false },
|
|
24
|
+
server: { path: '/', port: 3000, host: '127.0.0.1' },
|
|
25
|
+
web: { session: { autoCommit: true, maxAge: 86400000, sessionIdKey: 'cell:sessionId', sessionKey: 'cell:session' }, trace: { requestField: 'X-Cell-Trace-ID', responseField: 'X-Cell-Trace-ID' }, health: { url: '/health', method: 'GET' }, cookies: { keys: ['abcdef'] }, route: { cacheSize: 500 } },
|
|
26
|
+
http: { client: { headers: {} } },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
let server: Server | undefined;
|
|
30
|
+
let app: Application | undefined;
|
|
31
|
+
afterEach(async () => {
|
|
32
|
+
await new Promise<void>(r => (server ? server.close(() => r()) : r()));
|
|
33
|
+
await app?.stop();
|
|
34
|
+
server = undefined;
|
|
35
|
+
app = undefined;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
/** Starts an application with the components defined before the call; a fetch to it. */
|
|
39
|
+
async function serve(): Promise<(path: string, init?: RequestInit) => Promise<Response>> {
|
|
40
|
+
currentThis.cellProps = structuredClone(DEFAULT_CONFIG);
|
|
41
|
+
const container = new Container(coreCommonStatic, coreNodeStatic, httpCommonModule, webCommonModule, webNodeModule, autoBind());
|
|
42
|
+
app = container.get<Application>(Application);
|
|
43
|
+
await app.start();
|
|
44
|
+
server = HttpServerUtil.createHttpServer(HttpServerUtil.createHandler(container));
|
|
45
|
+
await new Promise<void>(r => server!.listen(0, '127.0.0.1', r));
|
|
46
|
+
const base = `http://127.0.0.1:${(server!.address() as AddressInfo).port}`;
|
|
47
|
+
return (path, init) => fetch(base + path, init);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A handler for every request below `prefix`, answering what `answer` returns. */
|
|
51
|
+
function handler(prefix: string, answer: (ctx: Context) => unknown) {
|
|
52
|
+
@Component(HandlerAdapter)
|
|
53
|
+
class Answering implements HandlerAdapter {
|
|
54
|
+
readonly priority = 100;
|
|
55
|
+
async canHandle() {
|
|
56
|
+
return Context.getRequest().path.startsWith(prefix);
|
|
57
|
+
}
|
|
58
|
+
async handle() {
|
|
59
|
+
const response = Context.getResponse();
|
|
60
|
+
response.setHeader('content-type', 'application/json');
|
|
61
|
+
response.body = JSON.stringify(await answer(Context.getCurrent()));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return Answering;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe('web 中间件', () => {
|
|
68
|
+
|
|
69
|
+
it('按 priority 从高到低进入,出来的顺序相反;处理器在最里层', async () => {
|
|
70
|
+
const order: string[] = [];
|
|
71
|
+
for (const [name, priority] of [['low', 10], ['high', 9000], ['mid', 500]] as const) {
|
|
72
|
+
@Component(Middleware)
|
|
73
|
+
class Recording implements Middleware {
|
|
74
|
+
readonly priority = priority;
|
|
75
|
+
async handle(ctx: Context, next: () => Promise<void>) {
|
|
76
|
+
order.push(`>${name}`);
|
|
77
|
+
await next();
|
|
78
|
+
order.push(`<${name}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
void Recording;
|
|
82
|
+
}
|
|
83
|
+
handler('/order', () => order.push('handler'));
|
|
84
|
+
const f = await serve();
|
|
85
|
+
expect((await f('/order')).status).to.equal(200);
|
|
86
|
+
const mine = order.filter(step => /low|mid|high|handler/.test(step));
|
|
87
|
+
expect(mine).to.deep.equal(['>high', '>mid', '>low', 'handler', '<low', '<mid', '<high']);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('中间件不调用 next 时请求在它这里结束', async () => {
|
|
91
|
+
@Component(Middleware)
|
|
92
|
+
class Gate implements Middleware {
|
|
93
|
+
readonly priority = 9000;
|
|
94
|
+
async handle(ctx: Context, next: () => Promise<void>) {
|
|
95
|
+
if (ctx.request.headers['x-pass'] !== 'yes') {
|
|
96
|
+
ctx.response.statusCode = 403;
|
|
97
|
+
ctx.response.end('closed');
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
await next();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let handled = 0;
|
|
104
|
+
handler('/gate', () => ++handled);
|
|
105
|
+
const f = await serve();
|
|
106
|
+
const closed = await f('/gate');
|
|
107
|
+
expect(closed.status).to.equal(403);
|
|
108
|
+
expect(await closed.text()).to.equal('closed');
|
|
109
|
+
expect(handled).to.equal(0);
|
|
110
|
+
expect((await f('/gate', { headers: { 'x-pass': 'yes' } })).status).to.equal(200);
|
|
111
|
+
expect([Gate]).to.have.length(1);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('没有处理器的请求回答 404', async () => {
|
|
115
|
+
const f = await serve();
|
|
116
|
+
expect((await f('/nothing-here')).status).to.equal(404);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('web 会话', () => {
|
|
121
|
+
|
|
122
|
+
it('会话写入后自动提交,下一次请求带上 cookie 就拿到同一份会话', async () => {
|
|
123
|
+
handler('/count', () => {
|
|
124
|
+
const session = Context.getSession();
|
|
125
|
+
session.count = (session.count ?? 0) + 1;
|
|
126
|
+
return { count: session.count };
|
|
127
|
+
});
|
|
128
|
+
const f = await serve();
|
|
129
|
+
const first = await f('/count');
|
|
130
|
+
expect(await first.json()).to.deep.equal({ count: 1 });
|
|
131
|
+
const cookies = first.headers.getSetCookie().map(c => c.split(';')[0]).join('; ');
|
|
132
|
+
expect(cookies).to.contain('cell:sessionId=');
|
|
133
|
+
expect(await (await f('/count', { headers: { cookie: cookies } })).json()).to.deep.equal({ count: 2 });
|
|
134
|
+
// 没有 cookie:新会话
|
|
135
|
+
expect(await (await f('/count')).json()).to.deep.equal({ count: 1 });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('cookie 被篡改(签名不对)时不认,按新会话处理', async () => {
|
|
139
|
+
handler('/visit', () => {
|
|
140
|
+
const session = Context.getSession();
|
|
141
|
+
session.visits = (session.visits ?? 0) + 1;
|
|
142
|
+
return { visits: session.visits };
|
|
143
|
+
});
|
|
144
|
+
const f = await serve();
|
|
145
|
+
const first = await f('/visit');
|
|
146
|
+
const cookies = first.headers.getSetCookie().map(c => c.split(';')[0]);
|
|
147
|
+
expect(cookies.some(c => c.startsWith('cell:session.sig='))).to.equal(true);
|
|
148
|
+
// The data of the session, with another value and the signature of the old one.
|
|
149
|
+
const forgedData = Buffer.from(JSON.stringify({ id: 'x', visits: 41 })).toString('base64');
|
|
150
|
+
const forged = cookies.map(c => (c.startsWith('cell:session=') ? `cell:session=${forgedData}` : c)).join('; ');
|
|
151
|
+
expect(await (await f('/visit', { headers: { cookie: forged } })).json()).to.deep.equal({ visits: 1 });
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('web 健康检查', () => {
|
|
156
|
+
|
|
157
|
+
it('GET /health 回答 UP 的 JSON', async () => {
|
|
158
|
+
const f = await serve();
|
|
159
|
+
const res = await f('/health');
|
|
160
|
+
expect(res.status).to.equal(200);
|
|
161
|
+
expect(res.headers.get('content-type')).to.contain('application/json');
|
|
162
|
+
expect((await res.json()).status).to.equal('UP');
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('compose 与 express 中间件', () => {
|
|
167
|
+
|
|
168
|
+
it('compose 按顺序调用,最后调用 next;同一个中间件调用两次 next 会被拒绝', async () => {
|
|
169
|
+
const steps: string[] = [];
|
|
170
|
+
const mw = (name: string): Middleware => ({ priority: 0, handle: async (ctx, next) => { steps.push(name); await next(); } });
|
|
171
|
+
await compose([mw('a'), mw('b')])({} as Context, { priority: 0, handle: async () => { steps.push('end'); } });
|
|
172
|
+
expect(steps).to.deep.equal(['a', 'b', 'end']);
|
|
173
|
+
const twice: Middleware = { priority: 0, handle: async (ctx, next) => { await next(); await next(); } };
|
|
174
|
+
const error = await compose([twice])({} as Context, { priority: 0, handle: async () => undefined }).then(() => undefined, e => e);
|
|
175
|
+
expect(error?.message).to.equal('next() called multiple times');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('express 风格的中间件:next() 继续,next(err) 让请求失败', async () => {
|
|
179
|
+
const ctx = new Context({ headers: {} } as any, {} as any);
|
|
180
|
+
await Context.run(async () => {
|
|
181
|
+
Context.setCurrent(ctx);
|
|
182
|
+
const seen: string[] = [];
|
|
183
|
+
const passing = convertExpressMiddleware((req: unknown, res: unknown, next: (e?: unknown) => void) => next());
|
|
184
|
+
await passing.handle(ctx, async () => { seen.push('next'); });
|
|
185
|
+
expect(seen).to.deep.equal(['next']);
|
|
186
|
+
const failing = convertExpressMiddleware((req: unknown, res: unknown, next: (e?: unknown) => void) => next(new Error('denied')));
|
|
187
|
+
const error = await failing.handle(ctx, async () => { seen.push('never'); }).then(() => undefined, e => e);
|
|
188
|
+
expect(error?.message).to.equal('denied');
|
|
189
|
+
expect(seen).to.deep.equal(['next']);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
});
|
package/src/package.spec.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
import { describe, it } from 'bun:test';
|
|
2
|
-
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { currentThis } from '@celljs/core';
|
|
3
|
+
import { AssemblyTestUtil } from '@celljs/core/lib/common/test/assembly-test-util';
|
|
4
|
+
import m0 from '@celljs/core/lib/common/static-module';
|
|
5
|
+
import m1 from '@celljs/core/lib/node/static-module';
|
|
6
|
+
import m2 from '@celljs/http/lib/common/module';
|
|
7
|
+
import own0 from './common/module';
|
|
8
|
+
import own1 from './node/module';
|
|
3
9
|
|
|
4
|
-
|
|
10
|
+
currentThis.cellProps = {};
|
|
11
|
+
|
|
12
|
+
describe('@celljs/web', () => {
|
|
13
|
+
it('its DI module loads after those of its dependencies and every binding resolves', () => {
|
|
14
|
+
const { changed, errors } = AssemblyTestUtil.check([m0, m1, m2, own0], own1);
|
|
15
|
+
expect(errors).toEqual([]);
|
|
16
|
+
expect(changed).toBeGreaterThan(0);
|
|
17
|
+
});
|
|
5
18
|
});
|