@celljs/web 3.9.0 → 4.1.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/cell.yml +5 -3
- package/esm/common/pipe/pipe-transtorm.d.ts +7 -0
- package/esm/common/pipe/pipe-transtorm.js +17 -1
- package/esm/common/pipe/pipe-transtorm.js.map +1 -1
- package/esm/node/body/body-middleware.d.ts +3 -0
- package/esm/node/body/body-middleware.js +13 -4
- package/esm/node/body/body-middleware.js.map +1 -1
- package/esm/node/context.js +1 -1
- package/esm/node/context.js.map +1 -1
- package/esm/node/http/http-middleware.d.ts +1 -0
- package/esm/node/http/http-middleware.js +10 -5
- package/esm/node/http/http-middleware.js.map +1 -1
- package/esm/node/session/session-store.js +4 -3
- package/esm/node/session/session-store.js.map +1 -1
- package/esm/node/session/session.d.ts +1 -1
- package/esm/node/session/session.js +4 -4
- package/esm/node/session/session.js.map +1 -1
- package/lib/common/pipe/pipe-transtorm.d.ts +7 -0
- package/lib/common/pipe/pipe-transtorm.d.ts.map +1 -1
- package/lib/common/pipe/pipe-transtorm.js +17 -1
- package/lib/common/pipe/pipe-transtorm.js.map +1 -1
- package/lib/common/pipe/pipe-transtorm.spec.d.ts +2 -0
- package/lib/common/pipe/pipe-transtorm.spec.d.ts.map +1 -0
- package/lib/common/pipe/pipe-transtorm.spec.js +69 -0
- package/lib/common/pipe/pipe-transtorm.spec.js.map +1 -0
- package/lib/node/body/body-middleware.d.ts +3 -0
- package/lib/node/body/body-middleware.d.ts.map +1 -1
- package/lib/node/body/body-middleware.js +12 -3
- package/lib/node/body/body-middleware.js.map +1 -1
- package/lib/node/body/body-middleware.spec.d.ts +2 -0
- package/lib/node/body/body-middleware.spec.d.ts.map +1 -0
- package/lib/node/body/body-middleware.spec.js +19 -0
- package/lib/node/body/body-middleware.spec.js.map +1 -0
- package/lib/node/context.js +1 -1
- package/lib/node/context.js.map +1 -1
- package/lib/node/http/http-middleware.d.ts +1 -0
- package/lib/node/http/http-middleware.d.ts.map +1 -1
- package/lib/node/http/http-middleware.js +10 -5
- package/lib/node/http/http-middleware.js.map +1 -1
- package/lib/node/session/session-store.d.ts.map +1 -1
- package/lib/node/session/session-store.js +4 -1
- package/lib/node/session/session-store.js.map +1 -1
- package/lib/node/session/session.d.ts +1 -1
- package/lib/node/session/session.d.ts.map +1 -1
- package/lib/node/session/session.js +37 -2
- package/lib/node/session/session.js.map +1 -1
- package/lib/node/session/session.spec.d.ts +2 -0
- package/lib/node/session/session.spec.d.ts.map +1 -0
- package/lib/node/session/session.spec.js +78 -0
- package/lib/node/session/session.spec.js.map +1 -0
- package/package.json +19 -32
- package/src/common/pipe/pipe-transtorm.spec.ts +62 -0
- package/src/common/pipe/pipe-transtorm.ts +18 -1
- package/src/node/body/body-middleware.spec.ts +17 -0
- package/src/node/body/body-middleware.ts +12 -4
- package/src/node/context.ts +1 -1
- package/src/node/http/http-middleware.ts +10 -5
- package/src/node/session/session-store.ts +5 -1
- package/src/node/session/session.spec.ts +81 -0
- package/src/node/session/session.ts +5 -2
|
@@ -18,11 +18,14 @@ let HttpMiddleware = class HttpMiddleware {
|
|
|
18
18
|
constructor() {
|
|
19
19
|
this.priority = http_protocol_1.HTTP_MIDDLEWARE_PRIORITY;
|
|
20
20
|
}
|
|
21
|
+
/** The access log: one line before and one after each request, at the debug level. */
|
|
21
22
|
async handle(ctx, next) {
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
// Checked once per request: the lines are only built when they are written.
|
|
24
|
+
const log = this.logger.isEnabled('debug');
|
|
25
|
+
const start = log ? Date.now() : 0;
|
|
26
|
+
if (log) {
|
|
27
|
+
this.logger.debug(`starting ${ctx.request.method} ${ctx.request.path}`);
|
|
28
|
+
}
|
|
26
29
|
try {
|
|
27
30
|
await next();
|
|
28
31
|
const response = ctx.response;
|
|
@@ -31,7 +34,9 @@ let HttpMiddleware = class HttpMiddleware {
|
|
|
31
34
|
}
|
|
32
35
|
}
|
|
33
36
|
finally {
|
|
34
|
-
|
|
37
|
+
if (log) {
|
|
38
|
+
this.logger.debug(`ending ${ctx.request.method} ${ctx.request.path} [${Date.now() - start}ms]`);
|
|
39
|
+
}
|
|
35
40
|
}
|
|
36
41
|
}
|
|
37
42
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-middleware.js","sourceRoot":"","sources":["../../../src/node/http/http-middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,8CAA2C;AAC3C,wCAAqC;AACrC,uCAA4D;AAC5D,mDAA2D;AAGpD,IAAM,cAAc,GAApB,MAAM,cAAc;IAApB;
|
|
1
|
+
{"version":3,"file":"http-middleware.js","sourceRoot":"","sources":["../../../src/node/http/http-middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,8CAA2C;AAC3C,wCAAqC;AACrC,uCAA4D;AAC5D,mDAA2D;AAGpD,IAAM,cAAc,GAApB,MAAM,cAAc;IAApB;QA0BM,aAAQ,GAAG,wCAAwB,CAAC;IAEjD,CAAC;IAvBG,sFAAsF;IACtF,KAAK,CAAC,MAAM,CAAC,GAAY,EAAE,IAAyB;QAChD,4EAA4E;QAC5E,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAC3C,MAAM,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACnC,IAAI,GAAG,EAAE,CAAC;YACN,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QAC5E,CAAC;QACD,IAAI,CAAC;YACD,MAAM,IAAI,EAAE,CAAC;YACb,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;YAC9B,IAAI,CAAC,iBAAO,CAAC,aAAa,EAAE,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAC;gBACtD,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAChC,CAAC;QACL,CAAC;gBAAS,CAAC;YACP,IAAI,GAAG,EAAE,CAAC;gBACN,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,KAAK,CAAC,CAAC;YACpG,CAAC;QACL,CAAC;IACL,CAAC;CAIJ,CAAA;;AAzBsB;IADlB,IAAA,gBAAS,EAAC,aAAM,CAAC;;8CACgB;yBAHzB,cAAc;IAD1B,IAAA,gBAAS,EAAC,uBAAU,CAAC;GACT,cAAc,CA4B1B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-store.d.ts","sourceRoot":"","sources":["../../../src/node/session/session-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,YAAY,EAAmB,MAAM,oBAAoB,CAAC;AAC7F,OAAO,EAA+B,MAAM,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"session-store.d.ts","sourceRoot":"","sources":["../../../src/node/session/session-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,YAAY,EAAmB,MAAM,oBAAoB,CAAC;AAC7F,OAAO,EAA+B,MAAM,EAAE,MAAM,cAAc,CAAC;AAQnE,qBACa,kBAAmB,YAAW,YAAY;IAEnD,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,CAAC;IAGvC,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,GAAG,CAAC;IAGvC,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;IAGpD,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAE5B,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,CAWlD;IAEK,GAAG,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAGzC;IAEK,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAKtC;CAEJ"}
|
|
@@ -13,7 +13,10 @@ exports.CookieSessionStore = void 0;
|
|
|
13
13
|
const session_protocol_1 = require("./session-protocol");
|
|
14
14
|
const core_1 = require("@celljs/core");
|
|
15
15
|
const context_1 = require("../context");
|
|
16
|
-
const
|
|
16
|
+
const node_util_1 = require("node:util");
|
|
17
|
+
const node_zlib_1 = require("node:zlib");
|
|
18
|
+
const gzip = (0, node_util_1.promisify)(node_zlib_1.gzip);
|
|
19
|
+
const ungzip = (0, node_util_1.promisify)(node_zlib_1.gunzip);
|
|
17
20
|
let CookieSessionStore = class CookieSessionStore {
|
|
18
21
|
async get(id) {
|
|
19
22
|
const value = context_1.Context.getCookies().get(this.sessionOptions.sessionKey, this.sessionOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-store.js","sourceRoot":"","sources":["../../../src/node/session/session-store.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yDAA6F;AAC7F,uCAAmE;AACnE,wCAAqC;AACrC,MAAM,
|
|
1
|
+
{"version":3,"file":"session-store.js","sourceRoot":"","sources":["../../../src/node/session/session-store.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yDAA6F;AAC7F,uCAAmE;AACnE,wCAAqC;AACrC,yCAAsC;AACtC,yCAAyD;AAEzD,MAAM,IAAI,GAAG,IAAA,qBAAS,EAAC,gBAAY,CAAC,CAAC;AACrC,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,kBAAM,CAAC,CAAC;AAG1B,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAa3B,KAAK,CAAC,GAAG,CAAC,EAAU;QAChB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DACgB;6BAXzB,kBAAkB;IAD9B,IAAA,gBAAS,EAAC,+BAAY,CAAC;GACX,kBAAkB,CAsC9B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/node/session/session.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/node/session/session.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAM7C,qBAAa,WAAY,YAAW,OAAO;IACvC,QAAQ,CAAC,EAAE,SAAmB;IAC9B,KAAK,UAAQ;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC3B,YAAY,cAAc,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,GAAG,EAWzC;IAED,SAAS,CAAC,IAAI,WAEb;IAED,MAAM,IAAI,GAAG,CAQZ;IAED,IAAI,OAAO,YAKV;CAEJ"}
|
|
@@ -1,8 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.SessionImpl = void 0;
|
|
4
37
|
const uuid_1 = require("@celljs/core/lib/common/utils/uuid");
|
|
5
|
-
const
|
|
38
|
+
const zlib = __importStar(require("node:zlib"));
|
|
39
|
+
// zlib.crc32 exists from Node.js 20.15 (and in Bun); @types/node is pinned to 18 in this repository.
|
|
40
|
+
const crc32 = zlib.crc32;
|
|
6
41
|
class SessionImpl {
|
|
7
42
|
constructor(sessionOptions, obj) {
|
|
8
43
|
this.id = (0, uuid_1.generateUUUID)();
|
|
@@ -20,7 +55,7 @@ class SessionImpl {
|
|
|
20
55
|
this._preHash = this.hash();
|
|
21
56
|
}
|
|
22
57
|
hash() {
|
|
23
|
-
return crc32(JSON.stringify(this.toJSON()));
|
|
58
|
+
return String(crc32(JSON.stringify(this.toJSON())));
|
|
24
59
|
}
|
|
25
60
|
toJSON() {
|
|
26
61
|
const obj = {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/node/session/session.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/node/session/session.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6DAAmE;AAEnE,MAAY,IAAI,sCAAkB;AAElC,qGAAqG;AACrG,MAAM,KAAK,GAAI,IAAmD,CAAC,KAAK,CAAC;AAEzE;IAMI,YAAY,cAAmB,EAAE,GAAS;QALjC,OAAE,GAAG,IAAA,oBAAa,GAAE,CAAC;QAC9B,UAAK,GAAG,IAAI,CAAC;QAKT,IAAI,GAAG,EAAE,CAAC;YACN,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YACnB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBAChC,IAAY,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;YAClC,CAAC;QACL,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,MAAM,GAAG,cAAc,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACjD,IAAI,CAAC,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC;QACxC,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;IAChC,CAAC;IAES,IAAI;QACV,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,MAAM;QACF,MAAM,GAAG,GAAQ,EAAE,CAAC;QACpB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAClC,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;gBACpC,GAAG,CAAC,GAAG,CAAC,GAAI,IAAY,CAAC,GAAG,CAAC,CAAC;YAClC,CAAC;QACL,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED,IAAI,OAAO;QACP,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;CAEJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.spec.d.ts","sourceRoot":"","sources":["../../../src/node/session/session.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bun_test_1 = require("bun:test");
|
|
4
|
+
const chai_1 = require("chai");
|
|
5
|
+
const context_1 = require("../context");
|
|
6
|
+
const session_1 = require("./session");
|
|
7
|
+
const session_store_1 = require("./session-store");
|
|
8
|
+
const session_strategy_1 = require("./session-strategy");
|
|
9
|
+
const options = { maxAge: 1000, sessionKey: 'cell:session' };
|
|
10
|
+
/** Runs fn in a request context and waits for it (Context.run does not return its promise with cls-hooked). */
|
|
11
|
+
function inContext(fn) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
context_1.Context.run(async () => {
|
|
14
|
+
try {
|
|
15
|
+
context_1.Context.setCurrent(new context_1.Context({}, {}));
|
|
16
|
+
await fn();
|
|
17
|
+
resolve();
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
reject(error);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
(0, bun_test_1.describe)('SessionImpl', () => {
|
|
26
|
+
(0, bun_test_1.it)('is unchanged until a value changes', () => {
|
|
27
|
+
const session = new session_1.SessionImpl(options);
|
|
28
|
+
(0, chai_1.expect)(session.isNew).to.equal(true);
|
|
29
|
+
(0, chai_1.expect)(session.changed).to.equal(false);
|
|
30
|
+
session.user = 'tom';
|
|
31
|
+
(0, chai_1.expect)(session.changed).to.equal(true);
|
|
32
|
+
});
|
|
33
|
+
(0, bun_test_1.it)('serializes its values without isNew and private fields', () => {
|
|
34
|
+
const session = new session_1.SessionImpl(options);
|
|
35
|
+
session.user = 'tom';
|
|
36
|
+
const json = session.toJSON();
|
|
37
|
+
(0, chai_1.expect)(json).to.include({ user: 'tom', maxAge: 1000 });
|
|
38
|
+
(0, chai_1.expect)(Object.keys(json)).to.not.include.members(['isNew', '_preHash']);
|
|
39
|
+
});
|
|
40
|
+
(0, bun_test_1.it)('restored from its values it is not new and unchanged', () => {
|
|
41
|
+
const session = new session_1.SessionImpl(options, { id: 'x', user: 'tom', expire: 5, maxAge: 1000 });
|
|
42
|
+
(0, chai_1.expect)(session.isNew).to.equal(false);
|
|
43
|
+
(0, chai_1.expect)(session.changed).to.equal(false);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
(0, bun_test_1.describe)('CookieSessionStore', () => {
|
|
47
|
+
(0, bun_test_1.it)('stores a session compressed in a cookie and reads it back', async () => {
|
|
48
|
+
const jar = new Map();
|
|
49
|
+
const store = new session_store_1.CookieSessionStore();
|
|
50
|
+
const strategy = new session_strategy_1.SessionStrategyImpl();
|
|
51
|
+
Object.assign(strategy, { sessionOptions: options });
|
|
52
|
+
Object.assign(store, { sessionOptions: options, sessionStrategy: strategy, logger: { error: () => undefined } });
|
|
53
|
+
await inContext(async () => {
|
|
54
|
+
context_1.Context.setCookies({ get: (k) => jar.get(k), set: (k, v) => { jar.set(k, v); } });
|
|
55
|
+
const session = new session_1.SessionImpl(options);
|
|
56
|
+
session.user = '中文 user';
|
|
57
|
+
await store.set(session);
|
|
58
|
+
const value = jar.get('cell:session');
|
|
59
|
+
(0, chai_1.expect)(value).to.match(/^[A-Za-z0-9+/]+=*$/);
|
|
60
|
+
(0, chai_1.expect)(Buffer.from(value, 'base64').subarray(0, 2)).to.deep.equal(Buffer.from([0x1f, 0x8b]));
|
|
61
|
+
const restored = await store.get('ignored');
|
|
62
|
+
(0, chai_1.expect)(restored.user).to.equal('中文 user');
|
|
63
|
+
(0, chai_1.expect)(restored.id).to.equal(session.id);
|
|
64
|
+
(0, chai_1.expect)(restored.changed).to.equal(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
(0, bun_test_1.it)('ignores a cookie it cannot read', async () => {
|
|
68
|
+
const errors = [];
|
|
69
|
+
const store = new session_store_1.CookieSessionStore();
|
|
70
|
+
Object.assign(store, { sessionOptions: options, sessionStrategy: new session_strategy_1.SessionStrategyImpl(), logger: { error: (e) => errors.push(e) } });
|
|
71
|
+
await inContext(async () => {
|
|
72
|
+
context_1.Context.setCookies({ get: () => 'bm90IGd6aXA=' });
|
|
73
|
+
(0, chai_1.expect)(await store.get('x')).to.equal(undefined);
|
|
74
|
+
});
|
|
75
|
+
(0, chai_1.expect)(errors).to.have.length(1);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
//# sourceMappingURL=session.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.spec.js","sourceRoot":"","sources":["../../../src/node/session/session.spec.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AACxC,+BAA8B;AAC9B,wCAAqC;AACrC,uCAAwC;AACxC,mDAAqD;AACrD,yDAAyD;AAEzD,MAAM,OAAO,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC;AAE7D,+GAA+G;AAC/G,SAAS,SAAS,CAAC,EAAuB;IACtC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnC,iBAAO,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE;YACnB,IAAI,CAAC;gBACD,iBAAO,CAAC,UAAU,CAAC,IAAI,iBAAO,CAAC,EAAS,EAAE,EAAS,CAAC,CAAC,CAAC;gBACtD,MAAM,EAAE,EAAE,CAAC;gB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O,EAAE,eAAe,EAAE,IAAI,sCAAmB,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACjJ,MAAM,SAAS,CAAC,KAAK,IAAI,EAAE;YACvB,iBAAO,CAAC,UAAU,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,cAAc,EAAS,CAAC,CAAC;YACzD,IAAA,aAAM,EAAC,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;QACH,IAAA,aAAM,EAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/web",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -29,23 +29,19 @@
|
|
|
29
29
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@celljs/core": "
|
|
33
|
-
"@celljs/http": "
|
|
34
|
-
"@types/cookies": "^0.7.7",
|
|
32
|
+
"@celljs/core": "4.1.0",
|
|
33
|
+
"@celljs/http": "4.1.0",
|
|
35
34
|
"@types/express": "^4.17.9",
|
|
36
|
-
"body-parser": "^1.20.
|
|
35
|
+
"body-parser": "^1.20.9",
|
|
37
36
|
"class-transformer": "^0.5.1",
|
|
38
|
-
"class-validator": "^0.14.
|
|
37
|
+
"class-validator": "^0.14.4",
|
|
39
38
|
"cookies": "^0.8.0",
|
|
40
39
|
"cors": "^2.8.5",
|
|
41
|
-
"
|
|
42
|
-
"express": "^4.17.1",
|
|
43
|
-
"node-gzip": "^1.1.2",
|
|
44
|
-
"tslib": "^2.8.0",
|
|
40
|
+
"express": "^4.22.3",
|
|
45
41
|
"url-pattern": "^1.0.3"
|
|
46
42
|
},
|
|
47
43
|
"devDependencies": {
|
|
48
|
-
"@
|
|
44
|
+
"@types/cookies": "^0.7.7",
|
|
49
45
|
"@types/cors": "^2.8.6"
|
|
50
46
|
},
|
|
51
47
|
"publishConfig": {
|
|
@@ -53,111 +49,102 @@
|
|
|
53
49
|
},
|
|
54
50
|
"exports": {
|
|
55
51
|
".": {
|
|
56
|
-
"webpack": "./lib/common/index.js",
|
|
57
52
|
"import": "./esm/common/index.js",
|
|
58
53
|
"default": "./lib/common/index.js"
|
|
59
54
|
},
|
|
60
55
|
"./package.json": "./package.json",
|
|
61
56
|
"./lib/common": {
|
|
62
|
-
"webpack": "./lib/common/index.js",
|
|
63
57
|
"import": "./esm/common/index.js",
|
|
64
58
|
"default": "./lib/common/index.js"
|
|
65
59
|
},
|
|
66
60
|
"./lib/common/pipe": {
|
|
67
|
-
"webpack": "./lib/common/pipe/index.js",
|
|
68
61
|
"import": "./esm/common/pipe/index.js",
|
|
69
62
|
"default": "./lib/common/pipe/index.js"
|
|
70
63
|
},
|
|
71
64
|
"./lib/common/resolver": {
|
|
72
|
-
"webpack": "./lib/common/resolver/index.js",
|
|
73
65
|
"import": "./esm/common/resolver/index.js",
|
|
74
66
|
"default": "./lib/common/resolver/index.js"
|
|
75
67
|
},
|
|
76
68
|
"./lib/common/utils": {
|
|
77
|
-
"webpack": "./lib/common/utils/index.js",
|
|
78
69
|
"import": "./esm/common/utils/index.js",
|
|
79
70
|
"default": "./lib/common/utils/index.js"
|
|
80
71
|
},
|
|
81
72
|
"./lib/node": {
|
|
82
|
-
"webpack": "./lib/node/index.js",
|
|
83
73
|
"import": "./esm/node/index.js",
|
|
84
74
|
"default": "./lib/node/index.js"
|
|
85
75
|
},
|
|
86
76
|
"./lib/node/body": {
|
|
87
|
-
"webpack": "./lib/node/body/index.js",
|
|
88
77
|
"import": "./esm/node/body/index.js",
|
|
89
78
|
"default": "./lib/node/body/index.js"
|
|
90
79
|
},
|
|
91
80
|
"./lib/node/cookies": {
|
|
92
|
-
"webpack": "./lib/node/cookies/index.js",
|
|
93
81
|
"import": "./esm/node/cookies/index.js",
|
|
94
82
|
"default": "./lib/node/cookies/index.js"
|
|
95
83
|
},
|
|
96
84
|
"./lib/node/cors": {
|
|
97
|
-
"webpack": "./lib/node/cors/index.js",
|
|
98
85
|
"import": "./esm/node/cors/index.js",
|
|
99
86
|
"default": "./lib/node/cors/index.js"
|
|
100
87
|
},
|
|
101
88
|
"./lib/node/dispatcher": {
|
|
102
|
-
"webpack": "./lib/node/dispatcher/index.js",
|
|
103
89
|
"import": "./esm/node/dispatcher/index.js",
|
|
104
90
|
"default": "./lib/node/dispatcher/index.js"
|
|
105
91
|
},
|
|
106
92
|
"./lib/node/error": {
|
|
107
|
-
"webpack": "./lib/node/error/index.js",
|
|
108
93
|
"import": "./esm/node/error/index.js",
|
|
109
94
|
"default": "./lib/node/error/index.js"
|
|
110
95
|
},
|
|
111
96
|
"./lib/node/handler": {
|
|
112
|
-
"webpack": "./lib/node/handler/index.js",
|
|
113
97
|
"import": "./esm/node/handler/index.js",
|
|
114
98
|
"default": "./lib/node/handler/index.js"
|
|
115
99
|
},
|
|
116
100
|
"./lib/node/health": {
|
|
117
|
-
"webpack": "./lib/node/health/index.js",
|
|
118
101
|
"import": "./esm/node/health/index.js",
|
|
119
102
|
"default": "./lib/node/health/index.js"
|
|
120
103
|
},
|
|
121
104
|
"./lib/node/http": {
|
|
122
|
-
"webpack": "./lib/node/http/index.js",
|
|
123
105
|
"import": "./esm/node/http/index.js",
|
|
124
106
|
"default": "./lib/node/http/index.js"
|
|
125
107
|
},
|
|
126
108
|
"./lib/node/matcher": {
|
|
127
|
-
"webpack": "./lib/node/matcher/index.js",
|
|
128
109
|
"import": "./esm/node/matcher/index.js",
|
|
129
110
|
"default": "./lib/node/matcher/index.js"
|
|
130
111
|
},
|
|
131
112
|
"./lib/node/middleware": {
|
|
132
|
-
"webpack": "./lib/node/middleware/index.js",
|
|
133
113
|
"import": "./esm/node/middleware/index.js",
|
|
134
114
|
"default": "./lib/node/middleware/index.js"
|
|
135
115
|
},
|
|
136
116
|
"./lib/node/redirect": {
|
|
137
|
-
"webpack": "./lib/node/redirect/index.js",
|
|
138
117
|
"import": "./esm/node/redirect/index.js",
|
|
139
118
|
"default": "./lib/node/redirect/index.js"
|
|
140
119
|
},
|
|
141
120
|
"./lib/node/session": {
|
|
142
|
-
"webpack": "./lib/node/session/index.js",
|
|
143
121
|
"import": "./esm/node/session/index.js",
|
|
144
122
|
"default": "./lib/node/session/index.js"
|
|
145
123
|
},
|
|
146
124
|
"./lib/node/trace": {
|
|
147
|
-
"webpack": "./lib/node/trace/index.js",
|
|
148
125
|
"import": "./esm/node/trace/index.js",
|
|
149
126
|
"default": "./lib/node/trace/index.js"
|
|
150
127
|
},
|
|
151
128
|
"./lib/*.js": {
|
|
152
|
-
"webpack": "./lib/*.js",
|
|
153
129
|
"import": "./esm/*.js",
|
|
154
130
|
"default": "./lib/*.js"
|
|
155
131
|
},
|
|
156
132
|
"./lib/*": {
|
|
157
|
-
"webpack": "./lib/*.js",
|
|
158
133
|
"import": "./esm/*.js",
|
|
159
134
|
"default": "./lib/*.js"
|
|
160
135
|
},
|
|
161
136
|
"./*": "./*"
|
|
137
|
+
},
|
|
138
|
+
"cell": {
|
|
139
|
+
"modules": {
|
|
140
|
+
"frontend": [
|
|
141
|
+
"lib/common/module",
|
|
142
|
+
"lib/browser/module"
|
|
143
|
+
],
|
|
144
|
+
"backend": [
|
|
145
|
+
"lib/common/module",
|
|
146
|
+
"lib/node/module"
|
|
147
|
+
]
|
|
148
|
+
}
|
|
162
149
|
}
|
|
163
150
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { IsString } from 'class-validator';
|
|
4
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
+
import { Socket } from 'node:net';
|
|
6
|
+
import { ValidationPipe } from './pipe-transtorm';
|
|
7
|
+
import { ValidationErrors } from './validation-errors';
|
|
8
|
+
|
|
9
|
+
class CreateUser {
|
|
10
|
+
@IsString()
|
|
11
|
+
name: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class Cookies {
|
|
15
|
+
constructor(protected readonly values: Record<string, string>) { }
|
|
16
|
+
|
|
17
|
+
get(name: string): string | undefined {
|
|
18
|
+
return this.values[name];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function createPipe(): ValidationPipe {
|
|
23
|
+
const pipe = new ValidationPipe();
|
|
24
|
+
(pipe as any).options = { detailedOutputDisabled: false, transformEnabled: true };
|
|
25
|
+
return pipe;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('ValidationPipe', () => {
|
|
29
|
+
|
|
30
|
+
it('passes the request and response through, whatever the parameter type', async () => {
|
|
31
|
+
const pipe = createPipe();
|
|
32
|
+
const request = new IncomingMessage(new Socket());
|
|
33
|
+
const response = new ServerResponse(request);
|
|
34
|
+
expect(await pipe.transform(response, { argType: Object })).to.equal(response);
|
|
35
|
+
expect(await pipe.transform(response, { argType: ServerResponse })).to.equal(response);
|
|
36
|
+
expect(await pipe.transform(request, { argType: Object })).to.equal(request);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('keeps other framework objects with their methods', async () => {
|
|
40
|
+
const cookies = new Cookies({ token: 'abc' });
|
|
41
|
+
const result = await createPipe().transform(cookies, { argType: Object });
|
|
42
|
+
expect(result).to.equal(cookies);
|
|
43
|
+
expect(result.get('token')).to.equal('abc');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('turns request data into the parameter class and validates it', async () => {
|
|
47
|
+
const pipe = createPipe();
|
|
48
|
+
const user = await pipe.transform({ name: 'Tom' }, { argType: CreateUser });
|
|
49
|
+
expect(user).to.be.instanceOf(CreateUser);
|
|
50
|
+
expect(user.name).to.equal('Tom');
|
|
51
|
+
let error: unknown;
|
|
52
|
+
await pipe.transform({ name: 1 }, { argType: CreateUser }).catch(e => error = e);
|
|
53
|
+
expect(error).to.be.instanceOf(ValidationErrors);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('converts plain objects, arrays and primitives as before', async () => {
|
|
57
|
+
const pipe = createPipe();
|
|
58
|
+
expect(await pipe.transform({ a: { b: 1 } }, { argType: Object })).to.deep.equal({ a: { b: 1 } });
|
|
59
|
+
expect(await pipe.transform([ 1, 2 ], { argType: Array })).to.deep.equal([ 1, 2 ]);
|
|
60
|
+
expect(await pipe.transform('5', { argType: String })).to.equal('5');
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -15,7 +15,7 @@ export class ValidationPipe implements PipeTransform<any> {
|
|
|
15
15
|
public async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
|
|
16
16
|
const opts = this.options || {};
|
|
17
17
|
const { argType } = metadata;
|
|
18
|
-
if (!argType) {
|
|
18
|
+
if (!argType || !this.isData(value)) {
|
|
19
19
|
return value;
|
|
20
20
|
}
|
|
21
21
|
if (!this.toValidate(metadata)) {
|
|
@@ -90,6 +90,23 @@ export class ValidationPipe implements PipeTransform<any> {
|
|
|
90
90
|
.forEach(key => this.stripProtoKeys(value[key]));
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Request data: primitives, arrays and plain objects, as parsed from JSON, query strings or RPC
|
|
95
|
+
* messages. Objects the framework passes in (request, response, session, cookies, errors) are
|
|
96
|
+
* class instances and pass through: converting them would copy them property by property, which
|
|
97
|
+
* never ends for the circular request and response.
|
|
98
|
+
*/
|
|
99
|
+
private isData(value: unknown): boolean {
|
|
100
|
+
if (typeof value === 'function') {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
const proto = Object.getPrototypeOf(value);
|
|
107
|
+
return proto === Object.prototype || proto === null;
|
|
108
|
+
}
|
|
109
|
+
|
|
93
110
|
private isPrimitive(value: unknown): boolean {
|
|
94
111
|
return ['number', 'boolean', 'string'].indexOf(typeof value) !== -1;
|
|
95
112
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { BodyMiddleware } from './body-middleware';
|
|
4
|
+
|
|
5
|
+
describe('BodyMiddleware', () => {
|
|
6
|
+
it('makes its parsers once, not per request', async () => {
|
|
7
|
+
const middleware = new BodyMiddleware();
|
|
8
|
+
const seen: unknown[] = [];
|
|
9
|
+
Object.assign(middleware, { options: { limit: '1kb' }, handlerExecutionChain: { execute: async (parsers: unknown[]) => { seen.push(parsers); } } });
|
|
10
|
+
(middleware as unknown as { init(): void }).init();
|
|
11
|
+
await middleware.handle({} as any, async () => undefined);
|
|
12
|
+
await middleware.handle({} as any, async () => undefined);
|
|
13
|
+
expect(seen).to.have.length(2);
|
|
14
|
+
expect(seen[0]).to.have.length(4);
|
|
15
|
+
expect(seen[1]).to.equal(seen[0]);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { convertExpressMiddleware, Middleware } from '../middleware';
|
|
2
2
|
import { Context } from '../context';
|
|
3
|
-
import { Component, Autowired, Value } from '@celljs/core';
|
|
3
|
+
import { Component, Autowired, Value, PostConstruct } from '@celljs/core';
|
|
4
4
|
import { json, text, raw, urlencoded, Options } from 'body-parser';
|
|
5
5
|
import { BODY_MIDDLEWARE_PRIORITY } from './body-protocol';
|
|
6
6
|
import { HandlerExecutionChain } from '../handler';
|
|
@@ -14,13 +14,21 @@ export class BodyMiddleware implements Middleware {
|
|
|
14
14
|
@Value('cell.web.body')
|
|
15
15
|
protected readonly options: Options;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
/** The four parsers, made once: making them per request cost a noticeable share of each request. */
|
|
18
|
+
protected parsers: Middleware[];
|
|
19
|
+
|
|
20
|
+
@PostConstruct()
|
|
21
|
+
protected init(): void {
|
|
22
|
+
this.parsers = [
|
|
19
23
|
convertExpressMiddleware(json({ ...this.options })),
|
|
20
24
|
convertExpressMiddleware(text({ ...this.options })),
|
|
21
25
|
convertExpressMiddleware(raw({ ...this.options })),
|
|
22
26
|
convertExpressMiddleware(urlencoded({ ...this.options, ...{ extended: true } }))
|
|
23
|
-
]
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
|
|
31
|
+
return this.handlerExecutionChain.execute(this.parsers, next);
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
readonly priority = BODY_MIDDLEWARE_PRIORITY;
|
package/src/node/context.ts
CHANGED
|
@@ -80,7 +80,7 @@ export class Context extends BaseContext {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
static override getAttr<T>(key: string, scope?: AttributeScope): T {
|
|
83
|
-
if (scope) {
|
|
83
|
+
if (scope !== undefined) {
|
|
84
84
|
if (scope === AttributeScope.Session) {
|
|
85
85
|
return Context.getSession()[key];
|
|
86
86
|
} else {
|
|
@@ -9,11 +9,14 @@ export class HttpMiddleware implements Middleware {
|
|
|
9
9
|
@Autowired(Logger)
|
|
10
10
|
protected readonly logger: Logger;
|
|
11
11
|
|
|
12
|
+
/** The access log: one line before and one after each request, at the debug level. */
|
|
12
13
|
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
// Checked once per request: the lines are only built when they are written.
|
|
15
|
+
const log = this.logger.isEnabled('debug');
|
|
16
|
+
const start = log ? Date.now() : 0;
|
|
17
|
+
if (log) {
|
|
18
|
+
this.logger.debug(`starting ${ctx.request.method} ${ctx.request.path}`);
|
|
19
|
+
}
|
|
17
20
|
try {
|
|
18
21
|
await next();
|
|
19
22
|
const response = ctx.response;
|
|
@@ -21,7 +24,9 @@ export class HttpMiddleware implements Middleware {
|
|
|
21
24
|
response.end(response.body);
|
|
22
25
|
}
|
|
23
26
|
} finally {
|
|
24
|
-
|
|
27
|
+
if (log) {
|
|
28
|
+
this.logger.debug(`ending ${ctx.request.method} ${ctx.request.path} [${Date.now() - start}ms]`);
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { Session, SessionStrategy, SessionStore, COOKIE_EXP_DATE } from './session-protocol';
|
|
2
2
|
import { Autowired, Value, Component, Logger } from '@celljs/core';
|
|
3
3
|
import { Context } from '../context';
|
|
4
|
-
|
|
4
|
+
import { promisify } from 'node:util';
|
|
5
|
+
import { gzip as gzipCallback, gunzip } from 'node:zlib';
|
|
6
|
+
|
|
7
|
+
const gzip = promisify(gzipCallback);
|
|
8
|
+
const ungzip = promisify(gunzip);
|
|
5
9
|
|
|
6
10
|
@Component(SessionStore)
|
|
7
11
|
export class CookieSessionStore implements SessionStore {
|