@radatek/microserver 2.3.9 → 2.3.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/microserver.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.9
3
+ * @version 2.3.11
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -120,7 +120,7 @@ export declare class ServerResponse<T = any> extends http.ServerResponse {
120
120
  redirect(code: number | string, url?: string): void;
121
121
  /** Set status code */
122
122
  status(code: number): this;
123
- download(path: string, filename?: string): void;
123
+ file(path: string, filename?: string): void;
124
124
  }
125
125
  /** WebSocket options */
126
126
  export interface WebSocketOptions {
@@ -551,6 +551,8 @@ export interface AuthOptionsInternal extends AuthOptions {
551
551
  };
552
552
  /** Expire time in seconds */
553
553
  expire: number;
554
+ /** Use object token instead of user id */
555
+ objectToken: boolean;
554
556
  /** Authentication mode */
555
557
  mode: 'cookie' | 'token';
556
558
  /** Authentication realm for basic authentication */
package/microserver.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.9
3
+ * @version 2.3.11
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -438,7 +438,7 @@ export class ServerResponse extends http.ServerResponse {
438
438
  this.statusCode = code;
439
439
  return this;
440
440
  }
441
- download(path, filename) {
441
+ file(path, filename) {
442
442
  StaticPlugin.serveFile(this.req, this, {
443
443
  path: path,
444
444
  filename: filename || basename(path),
@@ -987,7 +987,7 @@ export class Router extends EventEmitter {
987
987
  idx = 5;
988
988
  }
989
989
  if (name === 'json')
990
- return (req, res) => res.isJson = true;
990
+ return (req, res, next) => { res.isJson = true; return next(); };
991
991
  if (idx >= 0) {
992
992
  const v = name.slice(idx + 1);
993
993
  const type = name.slice(0, idx);
@@ -1114,6 +1114,7 @@ export class Router extends EventEmitter {
1114
1114
  return rnext();
1115
1115
  }
1116
1116
  _walkTree(item, req, res, next) {
1117
+ // TODO: walk recursively and add to stack all possibilities: /api/user/:id, /api/:last*. set params and paramsList pro stack record
1117
1118
  req.params = {};
1118
1119
  req.paramsList = [];
1119
1120
  const rstack = [];
@@ -1139,7 +1140,7 @@ export class Router extends EventEmitter {
1139
1140
  done = true;
1140
1141
  }
1141
1142
  else {
1142
- item = item.last || item.tree[name] || item.param;
1143
+ item = item.tree[name] || item.param || item.last;
1143
1144
  if (item && item.name) {
1144
1145
  req.params[item.name] = name;
1145
1146
  req.paramsList.push(name);
@@ -2109,7 +2110,7 @@ export class Auth {
2109
2110
  if (usrInfo?.id || usrInfo?._id) {
2110
2111
  const expire = Math.min(34560000, options?.expire || this.options.expire || defaultExpire);
2111
2112
  const expireTime = new Date().getTime() + expire * 1000;
2112
- const token = await this.token((usrInfo?.id || usrInfo?._id), undefined, expire);
2113
+ const token = await this.token(this.options.objectToken ? JSON.stringify(usrInfo) : (usrInfo?.id || usrInfo?._id), undefined, expire);
2113
2114
  if (token && this.res && this.req) {
2114
2115
  const oldToken = this.req.tokenId;
2115
2116
  if (oldToken)
@@ -2313,14 +2314,14 @@ class AuthPlugin extends Plugin {
2313
2314
  }
2314
2315
  return next();
2315
2316
  }
2316
- const cookie = req.headers.cookie, cookies = cookie ? cookie.split(/;\s+/g) : [];
2317
+ const cookie = req.headers.cookie, cookies = cookie ? cookie.split(/;\s*/g) : [];
2317
2318
  const sid = cookies.find(s => s.startsWith('token='));
2318
2319
  let token = '';
2319
2320
  if (authorization.startsWith('Bearer '))
2320
2321
  token = authorization.slice(7);
2321
2322
  if (sid)
2322
2323
  token = sid.slice(sid.indexOf('=') + 1);
2323
- if (!token)
2324
+ if (req.query.token)
2324
2325
  token = req.query.token;
2325
2326
  if (token) {
2326
2327
  const now = new Date().getTime();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radatek/microserver",
3
- "version": "2.3.9",
3
+ "version": "2.3.11",
4
4
  "description": "HTTP MicroServer",
5
5
  "author": "Darius Kisonas",
6
6
  "license": "MIT",