@yumerijs/core 2.0.3 → 2.0.5

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/core.js CHANGED
@@ -107,15 +107,24 @@ class Core {
107
107
  const route = this.routes[routePath];
108
108
  const result = route.match(pathname);
109
109
  if (result) {
110
- const middlewares = [...Object.values(this.globalMiddlewares), ...(route.middlewares || [])];
111
- let index = 0;
112
- const runner = async () => {
113
- if (index < middlewares.length)
114
- await middlewares[index++](session, runner);
115
- else
116
- await route.executeHandler(session, queryParams, result.pathParams);
117
- };
118
- await runner();
110
+ try {
111
+ const middlewares = [...Object.values(this.globalMiddlewares), ...(route.middlewares || [])];
112
+ let index = 0;
113
+ const runner = async () => {
114
+ if (index < middlewares.length)
115
+ await middlewares[index++](session, runner);
116
+ else
117
+ await route.executeHandler(session, queryParams, result.pathParams);
118
+ };
119
+ await runner();
120
+ }
121
+ catch (error) {
122
+ this.logger.error(`Unhandled error in route execution for path "${pathname}":`, error);
123
+ if (session && session.client.res && !session.client.res.writableEnded) {
124
+ session.client.res.statusCode = 500;
125
+ session.client.res.end('Internal Server Error');
126
+ }
127
+ }
119
128
  return true;
120
129
  }
121
130
  }
package/dist/server.js CHANGED
@@ -119,7 +119,28 @@ class Server {
119
119
  }
120
120
  else {
121
121
  let head = session.head;
122
- head['Set-Cookie'] = Object.entries(session.newCookie).map(([k, v]) => `${k}=${v}`);
122
+ head['Set-Cookie'] = Object.entries(session.newCookie).map(([name, cookie]) => {
123
+ let cookieString = `${name}=${cookie.value}`;
124
+ if (cookie.options.expires) {
125
+ cookieString += `; Expires=${cookie.options.expires.toUTCString()}`;
126
+ }
127
+ if (cookie.options.path) {
128
+ cookieString += `; Path=${cookie.options.path}`;
129
+ }
130
+ if (cookie.options.domain) {
131
+ cookieString += `; Domain=${cookie.options.domain}`;
132
+ }
133
+ if (cookie.options.secure) {
134
+ cookieString += `; Secure`;
135
+ }
136
+ if (cookie.options.httpOnly) {
137
+ cookieString += `; HttpOnly`;
138
+ }
139
+ if (cookie.options.sameSite) {
140
+ cookieString += `; SameSite=${cookie.options.sameSite}`;
141
+ }
142
+ return cookieString;
143
+ });
123
144
  if (this.enableCors) {
124
145
  head['Access-Control-Allow-Origin'] = '*';
125
146
  }
@@ -134,7 +155,28 @@ class Server {
134
155
  }
135
156
  else {
136
157
  let head = session.head;
137
- head['Set-Cookie'] = Object.entries(session.newCookie).map(([k, v]) => `${k}=${v}`);
158
+ head['Set-Cookie'] = Object.entries(session.newCookie).map(([name, cookie]) => {
159
+ let cookieString = `${name}=${cookie.value}`;
160
+ if (cookie.options.expires) {
161
+ cookieString += `; Expires=${cookie.options.expires.toUTCString()}`;
162
+ }
163
+ if (cookie.options.path) {
164
+ cookieString += `; Path=${cookie.options.path}`;
165
+ }
166
+ if (cookie.options.domain) {
167
+ cookieString += `; Domain=${cookie.options.domain}`;
168
+ }
169
+ if (cookie.options.secure) {
170
+ cookieString += `; Secure`;
171
+ }
172
+ if (cookie.options.httpOnly) {
173
+ cookieString += `; HttpOnly`;
174
+ }
175
+ if (cookie.options.sameSite) {
176
+ cookieString += `; SameSite=${cookie.options.sameSite}`;
177
+ }
178
+ return cookieString;
179
+ });
138
180
  if (this.enableCors) {
139
181
  head['Access-Control-Allow-Origin'] = '*';
140
182
  }
package/dist/session.d.ts CHANGED
@@ -11,13 +11,24 @@ export interface Client {
11
11
  res: ServerResponse;
12
12
  headers?: Record<string, string>;
13
13
  }
14
+ export interface CookieOptions {
15
+ expires?: Date;
16
+ path?: string;
17
+ domain?: string;
18
+ secure?: boolean;
19
+ httpOnly?: boolean;
20
+ sameSite?: 'Strict' | 'Lax' | 'None';
21
+ }
14
22
  export declare class Session {
15
23
  ip: string;
16
24
  cookie: Record<string, string>;
17
25
  query: Record<string, string> | undefined;
18
26
  sessionid: string;
19
27
  data: Record<string, any>;
20
- newCookie: Record<string, string>;
28
+ newCookie: Record<string, {
29
+ value: string;
30
+ options: CookieOptions;
31
+ }>;
21
32
  head: Record<string, any>;
22
33
  status: number;
23
34
  body: any;
@@ -34,6 +45,7 @@ export declare class Session {
34
45
  * @param query 请求字符串
35
46
  */
36
47
  constructor(ip: string, cookie: Record<string, string>, server: Server, req?: IncomingMessage, res?: ServerResponse, pathname?: string, query?: Record<string, string>);
48
+ setCookie(name: string, value: string, options?: CookieOptions): void;
37
49
  /**
38
50
  * 解析 Accept-Language 字符串为排序后的语言数组
39
51
  * @param header Accept-Language 头,比如 "zh-CN,zh-TW;q=0.8,en-US;q=0.6"
package/dist/session.js CHANGED
@@ -82,16 +82,22 @@ class Session {
82
82
  }
83
83
  else {
84
84
  this.sessionid = this.generateId(this.ip);
85
- this.newCookie.sessionid = this.sessionid;
85
+ this.setCookie('sessionid', this.sessionid);
86
86
  }
87
87
  if (cookie.lang) {
88
88
  this.languages = cookie.lang.split(',');
89
89
  }
90
90
  else if (req.headers['accept-language']) {
91
91
  this.languages = this.parseAcceptLanguages(req.headers['accept-language']);
92
- this.newCookie.lang = this.languages.join(',');
92
+ this.setCookie('lang', this.languages.join(','));
93
93
  }
94
94
  }
95
+ setCookie(name, value, options = {}) {
96
+ if (options.path === undefined) {
97
+ options.path = '/';
98
+ }
99
+ this.newCookie[name] = { value, options };
100
+ }
95
101
  /**
96
102
  * 解析 Accept-Language 字符串为排序后的语言数组
97
103
  * @param header Accept-Language 头,比如 "zh-CN,zh-TW;q=0.8,en-US;q=0.6"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/core",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Core module for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",