@yumerijs/core 2.0.7 → 2.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/session.d.ts +12 -0
- package/dist/session.js +48 -1
- package/package.json +1 -1
package/dist/session.d.ts
CHANGED
|
@@ -19,6 +19,12 @@ export interface CookieOptions {
|
|
|
19
19
|
httpOnly?: boolean;
|
|
20
20
|
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
21
21
|
}
|
|
22
|
+
export interface CacheOptions {
|
|
23
|
+
etag?: string;
|
|
24
|
+
modified?: Date;
|
|
25
|
+
maxAge?: number;
|
|
26
|
+
expires?: Date;
|
|
27
|
+
}
|
|
22
28
|
export declare class Session {
|
|
23
29
|
ip: string;
|
|
24
30
|
cookie: Record<string, string>;
|
|
@@ -84,5 +90,11 @@ export declare class Session {
|
|
|
84
90
|
* @param name 文本点名称
|
|
85
91
|
*/
|
|
86
92
|
text(name: string): any;
|
|
93
|
+
/**
|
|
94
|
+
* 设置缓存标头
|
|
95
|
+
* @param option 选项
|
|
96
|
+
*/
|
|
97
|
+
setCache(option: CacheOptions): void;
|
|
98
|
+
static(path: string, option: CacheOptions): void;
|
|
87
99
|
}
|
|
88
100
|
export {};
|
package/dist/session.js
CHANGED
|
@@ -44,6 +44,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
44
44
|
exports.Session = void 0;
|
|
45
45
|
const crypto_1 = __importDefault(require("crypto"));
|
|
46
46
|
const formidable = __importStar(require("formidable"));
|
|
47
|
+
const fs_1 = __importDefault(require("fs"));
|
|
47
48
|
class Session {
|
|
48
49
|
ip;
|
|
49
50
|
cookie;
|
|
@@ -73,9 +74,23 @@ class Session {
|
|
|
73
74
|
this.query = query;
|
|
74
75
|
this.server = server;
|
|
75
76
|
this.pathname = pathname;
|
|
77
|
+
let header = {};
|
|
78
|
+
for (let key in req.headers) {
|
|
79
|
+
const value = req.headers[key];
|
|
80
|
+
if (typeof value === 'string') {
|
|
81
|
+
header[key] = value;
|
|
82
|
+
}
|
|
83
|
+
else if (Array.isArray(value)) {
|
|
84
|
+
header[key] = value.join(', ');
|
|
85
|
+
}
|
|
86
|
+
else if (value !== undefined) {
|
|
87
|
+
header[key] = String(value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
76
90
|
this.client = {
|
|
77
91
|
req: req,
|
|
78
|
-
res: res
|
|
92
|
+
res: res,
|
|
93
|
+
headers: header
|
|
79
94
|
};
|
|
80
95
|
this.head['Content-Type'] = 'text/plain';
|
|
81
96
|
if (cookie.sessionid) {
|
|
@@ -272,5 +287,37 @@ class Session {
|
|
|
272
287
|
text(name) {
|
|
273
288
|
return this.server.core.i18n.get(name, this.languages);
|
|
274
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* 设置缓存标头
|
|
292
|
+
* @param option 选项
|
|
293
|
+
*/
|
|
294
|
+
setCache(option) {
|
|
295
|
+
if (option.etag)
|
|
296
|
+
this.head['ETag'] = `"${option.etag}"`;
|
|
297
|
+
if (option.modified)
|
|
298
|
+
this.head['Last-Modified'] = option.modified.toUTCString();
|
|
299
|
+
if (option.maxAge !== undefined)
|
|
300
|
+
this.head['Cache-Control'] = `max-age=${option.maxAge}`;
|
|
301
|
+
if (option.expires)
|
|
302
|
+
this.head['Expires'] = option.expires.toUTCString();
|
|
303
|
+
}
|
|
304
|
+
static(path, option) {
|
|
305
|
+
// 先查看用户是否在询问文件修改情况
|
|
306
|
+
if (this.client.headers['If-Modified-Since']) {
|
|
307
|
+
const modified = new Date(this.client.headers['If-Modified-Since']);
|
|
308
|
+
if (modified.getTime() === option.modified.getTime()) {
|
|
309
|
+
this.status = 304;
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (this.client.headers['If-None-Match']) {
|
|
314
|
+
if (this.client.headers['If-None-Match'] === option.etag) {
|
|
315
|
+
this.status = 304;
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
this.setCache(option);
|
|
320
|
+
this.body = fs_1.default.readFileSync(path);
|
|
321
|
+
}
|
|
275
322
|
}
|
|
276
323
|
exports.Session = Session;
|