@yumerijs/core 2.0.9 → 2.0.11

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 CHANGED
@@ -23,7 +23,16 @@ export interface CacheOptions {
23
23
  etag?: string;
24
24
  modified?: Date;
25
25
  maxAge?: number;
26
+ smaxAge?: number;
27
+ isImmutable?: boolean;
26
28
  expires?: Date;
29
+ cacheControl?: 'public' | 'private' | 'no-cache' | 'no-store' | 'must-revalidate' | 'proxy-revalidate';
30
+ }
31
+ export interface StaticCacheOptions {
32
+ etag?: boolean;
33
+ maxAge?: number;
34
+ smaxAge?: number;
35
+ etagType?: 'md5' | 'sha1' | 'sha256' | 'sha512';
27
36
  }
28
37
  export declare class Session {
29
38
  ip: string;
@@ -95,6 +104,7 @@ export declare class Session {
95
104
  * @param option 选项
96
105
  */
97
106
  setCache(option: CacheOptions): void;
98
- static(path: string, option: CacheOptions): void;
107
+ static(content: string, option: CacheOptions): void;
108
+ file(path: string, option: StaticCacheOptions): void;
99
109
  }
100
110
  export {};
package/dist/session.js CHANGED
@@ -296,16 +296,25 @@ class Session {
296
296
  this.head['ETag'] = `"${option.etag}"`;
297
297
  if (option.modified)
298
298
  this.head['Last-Modified'] = option.modified.toUTCString();
299
+ let control = [];
300
+ if (option.cacheControl !== undefined)
301
+ control.push(option.cacheControl);
299
302
  if (option.maxAge !== undefined)
300
- this.head['Cache-Control'] = `max-age=${option.maxAge}`;
303
+ control.push[`max-age=${option.maxAge}`];
304
+ if (option.smaxAge !== undefined)
305
+ control.push(`s-maxage=${option.smaxAge}`);
306
+ if (option.isImmutable)
307
+ control.push('immutable');
308
+ if (control.length > 0)
309
+ this.head['Cache-Control'] = control.join(', ');
301
310
  if (option.expires)
302
311
  this.head['Expires'] = option.expires.toUTCString();
303
312
  }
304
- static(path, option) {
313
+ static(content, option) {
305
314
  // 先查看用户是否在询问文件修改情况
306
315
  if (this.client.headers['If-Modified-Since']) {
307
316
  const modified = new Date(this.client.headers['If-Modified-Since']);
308
- if (modified.getTime() === option.modified.getTime()) {
317
+ if (modified.getTime() === option.modified?.getTime()) {
309
318
  this.status = 304;
310
319
  return;
311
320
  }
@@ -317,7 +326,34 @@ class Session {
317
326
  }
318
327
  }
319
328
  this.setCache(option);
320
- this.body = fs_1.default.readFileSync(path);
329
+ this.body = content;
330
+ }
331
+ file(path, option) {
332
+ if (this.client.headers['If-Modified-Since']) {
333
+ const modified = new Date(this.client.headers['If-Modified-Since']);
334
+ const moditime = fs_1.default.statSync(path).mtime;
335
+ if (modified.getTime() === moditime.getTime()) {
336
+ this.status = 304;
337
+ return;
338
+ }
339
+ }
340
+ const etag = crypto_1.default.createHash(option.etagType || 'md5').update(fs_1.default.readFileSync(path)).digest('hex');
341
+ if (this.client.headers['If-None-Match']) {
342
+ if (option.etag) {
343
+ if (this.client.headers['If-None-Match'] === etag) {
344
+ this.status = 304;
345
+ return;
346
+ }
347
+ }
348
+ }
349
+ this.setCache({
350
+ modified: fs_1.default.statSync(path).mtime,
351
+ etag,
352
+ maxAge: option.maxAge,
353
+ smaxAge: option.smaxAge,
354
+ cacheControl: 'public'
355
+ });
356
+ this.body = fs_1.default.readFileSync(path, 'utf-8');
321
357
  }
322
358
  }
323
359
  exports.Session = Session;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/core",
3
- "version": "2.0.9",
3
+ "version": "2.0.11",
4
4
  "description": "Core module for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",