@smpx/koa-request 0.2.6 → 0.3.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/Request.js CHANGED
@@ -37,6 +37,7 @@ const COOKIEID_COOKIE = 'id';
37
37
  const COOKIE_PARAM_PREFIX = '_ck_';
38
38
  const USER_TOKEN_COOKIE = 'utok';
39
39
  const FLASH_COOKIE = 'flash';
40
+ const VIEWPORT_WIDTH_COOKIE = 'vw';
40
41
  // these cookies are httpOnly, should not be readable from js
41
42
  const SENSITIVE_COOKIES = [USER_TOKEN_COOKIE, COOKIEID_COOKIE, SESSIONID_COOKIE];
42
43
 
@@ -240,6 +241,18 @@ class Request {
240
241
  return paramValue;
241
242
  }
242
243
 
244
+ /**
245
+ * Get parameter as a string (from query or body)
246
+ * @param {string} key
247
+ * @param {number} defaultValue
248
+ * @returns {number}
249
+ */
250
+ paramStr(key, defaultValue = '') {
251
+ const value = this.param(key, null);
252
+ if (value === null) return defaultValue;
253
+ return String(value);
254
+ }
255
+
243
256
  /**
244
257
  * Get parameter as an integer (from query or body)
245
258
  * @param {string} key
@@ -272,6 +285,7 @@ class Request {
272
285
  if (value === null) return defaultValue;
273
286
 
274
287
  if (
288
+ value === false ||
275
289
  value === 0 ||
276
290
  value === '0' ||
277
291
  value === 'false' ||
@@ -564,6 +578,7 @@ class Request {
564
578
  case 'Firefox': return 'Firefox Mobile';
565
579
  case 'Safari': return 'Safari Mobile';
566
580
  case 'Mobile Safari': return 'Safari Mobile';
581
+ default: return browserName;
567
582
  }
568
583
  }
569
584
 
@@ -742,9 +757,21 @@ class Request {
742
757
  return false;
743
758
  }
744
759
 
745
- if (!this._isMobileWeb) {
746
- const ua = this.parseUserAgent();
747
- this._isMobileWeb = (ua && ua.device && ua.device.type === 'mobile') || false;
760
+ if (this._isMobileWeb == null) {
761
+ const secHeader = this.ctx.headers['sec-ch-ua-mobile'];
762
+ if (secHeader) {
763
+ this._isMobileWeb = secHeader === '?1';
764
+ }
765
+ else {
766
+ const vwCookie = this.cookie(VIEWPORT_WIDTH_COOKIE);
767
+ if (vwCookie) {
768
+ this._isMobileWeb = Number(vwCookie) <= 750;
769
+ }
770
+ else {
771
+ const ua = this.parseUserAgent();
772
+ this._isMobileWeb = (ua && ua.device && ua.device.type === 'mobile') || false;
773
+ }
774
+ }
748
775
  }
749
776
  return this._isMobileWeb;
750
777
  }
@@ -753,6 +780,10 @@ class Request {
753
780
  return this.isMobileApp() || this.isMobileWeb();
754
781
  }
755
782
 
783
+ isAPI() {
784
+ return false;
785
+ }
786
+
756
787
  platform() {
757
788
  if (!this._platform) {
758
789
  if (this.isMobileApp()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smpx/koa-request",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "description": "Handle basic tasks for koajs",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -30,12 +30,12 @@
30
30
  },
31
31
  "homepage": "https://github.com/smartprix/koa-request#readme",
32
32
  "dependencies": {
33
- "ip": "^1.1.5",
33
+ "ip": "^1.1.8",
34
34
  "koa-basic-auth": "^4.0.0",
35
- "koa-body": "^4.2.0",
35
+ "koa-body": "^5.0.0",
36
36
  "koa-send": "^5.0.1",
37
- "koa2-ratelimit": "^0.9.1",
38
- "maxmind": "^4.3.3",
37
+ "koa2-ratelimit": "^1.1.1",
38
+ "maxmind": "^4.3.6",
39
39
  "tar": "^6.1.11",
40
40
  "ua-parser-js": "^1.0.2"
41
41
  },
package/staticPaths.js CHANGED
@@ -13,19 +13,18 @@ function getMiddleware(options) {
13
13
  }
14
14
 
15
15
  return async (ctx, next) => {
16
+ if (options.disabled) {
17
+ await next();
18
+ return;
19
+ }
20
+
16
21
  if (options.path === '/') {
17
22
  // root requires special handling to check if extension denotes a static path
18
- const matches = ctx.path.match(/^\/(.*)\.(jpg|jpeg|gif|png|ico|css|js|json|ttf|otf|eot|woff|svg|svgz|xml|html|txt|ogg|ogv|mp4|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$/);
23
+ const matches = ctx.path.match(/^\/(.*)\.(jpg|jpeg|gif|png|webp|avif|jxl|ico|css|js|mjs|json|ttf|otf|eot|woff|woff2|svg|svgz|xml|html|txt|ogg|ogv|mp4|av1|webm|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$/);
19
24
  if (!matches) {
20
25
  await next();
21
26
  return;
22
27
  }
23
-
24
- // skip the current path
25
- if (options.skip && options.skip(ctx)) {
26
- await next();
27
- return;
28
- }
29
28
  }
30
29
 
31
30
  // return if request path is not static
@@ -34,6 +33,12 @@ function getMiddleware(options) {
34
33
  return;
35
34
  }
36
35
 
36
+ // skip the current path
37
+ if (options.skip && options.skip(ctx)) {
38
+ await next();
39
+ return;
40
+ }
41
+
37
42
  // only GET or GET like methods are allowed
38
43
  if (!['get', 'head', 'options'].includes(ctx.method.toLowerCase())) {
39
44
  ctx.body = 'Method Not Allowed';