@smpx/koa-request 0.3.0 → 0.4.2

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
@@ -10,6 +10,8 @@ const enableBasicAuth = require('./basicAuth');
10
10
  const enableStaticPaths = require('./staticPaths');
11
11
  const enableBotBanning = require('./botBanning');
12
12
 
13
+ const {setLogger, logError} = require('./logger');
14
+
13
15
  const uaParser = new UAParser();
14
16
 
15
17
  const ONE_HOUR = 3600 * 1000;
@@ -123,19 +125,25 @@ function getIntegerKey(key) {
123
125
  }
124
126
 
125
127
  function addQuery(url, query = {}) {
126
- const uri = new URL(url, 'http://localhost');
127
- if (typeof query === 'string') {
128
- query = new URLSearchParams(query);
129
- for (const [key, val] of query) {
130
- uri.searchParams.set(key, val);
128
+ try {
129
+ const uri = new URL(url, 'http://localhost');
130
+ if (typeof query === 'string') {
131
+ query = new URLSearchParams(query);
132
+ for (const [key, val] of query) {
133
+ uri.searchParams.set(key, val);
134
+ }
131
135
  }
132
- }
133
- else {
134
- for (const [key, val] of Object.entries(query)) {
135
- uri.searchParams.set(key, val);
136
+ else {
137
+ for (const [key, val] of Object.entries(query)) {
138
+ uri.searchParams.set(key, val);
139
+ }
136
140
  }
141
+ return `${uri.pathname}${uri.search}`;
142
+ }
143
+ catch (e) {
144
+ logError(e);
145
+ return url;
137
146
  }
138
- return `${uri.pathname}${uri.search}`;
139
147
  }
140
148
 
141
149
  const isProduction = (process.env.NODE_ENV === 'production');
@@ -391,8 +399,8 @@ class Request {
391
399
  this.ctx.set('Content-Security-Policy', `frame-ancestors https://*.${domain}`);
392
400
  }
393
401
 
402
+ this.handlePlatformModification();
394
403
  if (!this.isAjax()) {
395
- this.handlePlatformModification();
396
404
  this.setUTMCookie();
397
405
  this.setAffidCookie();
398
406
  this.handleFlashMessage();
@@ -1235,7 +1243,20 @@ class Request {
1235
1243
  };
1236
1244
  }
1237
1245
 
1238
- const refererUri = new URL(referer);
1246
+ let refererUri;
1247
+ try {
1248
+ refererUri = new URL(referer, 'http://localhost');
1249
+ }
1250
+ catch (e) {
1251
+ logError(e);
1252
+ return {
1253
+ name: '',
1254
+ source: 'direct',
1255
+ medium: 'direct',
1256
+ term: '',
1257
+ };
1258
+ }
1259
+
1239
1260
  let host = refererUri.hostname;
1240
1261
  const baseDomain = this.baseDomain();
1241
1262
 
@@ -1288,16 +1309,22 @@ class Request {
1288
1309
  * sets UTM cookies from a predefined url
1289
1310
  */
1290
1311
  setUTMCookieFromUrl(url) {
1291
- const uri = (url instanceof URL) ? url : new URL(url, 'http://localhost');
1292
- const params = uri.searchParams;
1293
- this.setUTMCookieFromQuery({
1294
- utm_source: params.get('utm_source'),
1295
- utm_medium: params.get('utm_medium'),
1296
- utm_campaign: params.get('utm_campaign'),
1297
- utm_term: params.get('utm_term'),
1298
- utm_content: params.get('utm_content'),
1299
- gclid: params.get('gclid'),
1300
- });
1312
+ try {
1313
+ const uri = (url instanceof URL) ? url : new URL(url, 'http://localhost');
1314
+ const params = uri.searchParams;
1315
+ this.setUTMCookieFromQuery({
1316
+ utm_source: params.get('utm_source'),
1317
+ utm_medium: params.get('utm_medium'),
1318
+ utm_campaign: params.get('utm_campaign'),
1319
+ utm_term: params.get('utm_term'),
1320
+ utm_content: params.get('utm_content'),
1321
+ gclid: params.get('gclid'),
1322
+ });
1323
+ }
1324
+ catch (e) {
1325
+ logError(e);
1326
+ // ignore error
1327
+ }
1301
1328
  }
1302
1329
 
1303
1330
  /**
@@ -1354,32 +1381,45 @@ class Request {
1354
1381
  }
1355
1382
 
1356
1383
  setAffidCookieFromUrl(url) {
1357
- const uri = (url instanceof URL) ? url : new URL(url, 'http://localhost');
1358
- const params = uri.searchParams;
1359
- const affid = params.get(AFFID_PARAM);
1360
- if (!affid) return;
1361
- const subaffid = params.get(SUBAFFID_PARAM);
1362
-
1363
- this.cookie(
1364
- AFFID_COOKIE,
1365
- joinCookieParts([affid, subaffid]), {
1366
- maxAge: AFFID_COOKIE_DURATION,
1367
- domain: '*',
1368
- },
1369
- );
1384
+ try {
1385
+ const uri = (url instanceof URL) ? url : new URL(url, 'http://localhost');
1386
+ const params = uri.searchParams;
1387
+ const affid = params.get(AFFID_PARAM);
1388
+ if (!affid) return;
1389
+ const subaffid = params.get(SUBAFFID_PARAM);
1390
+
1391
+ this.cookie(
1392
+ AFFID_COOKIE,
1393
+ joinCookieParts([affid, subaffid]), {
1394
+ maxAge: AFFID_COOKIE_DURATION,
1395
+ domain: '*',
1396
+ },
1397
+ );
1398
+ }
1399
+ catch (e) {
1400
+ logError(e);
1401
+ // ignore error
1402
+ }
1370
1403
  }
1371
1404
 
1372
1405
  handlePlatformModification() {
1373
- // don't change platform in mobile apps
1374
- if (this.isMobileApp()) return;
1375
-
1376
- const platform = this.ctx.query[PLATFORM_PARAM] || this.cookie(PLATFORM_COOKIE);
1377
- const setPlatformCookie = this.setPlatform(platform);
1378
- if (setPlatformCookie) {
1379
- this.cookie(PLATFORM_COOKIE, platform, {
1380
- maxAge: PLATFORM_COOKIE_DURATION,
1381
- domain: '*',
1382
- });
1406
+ let setPlatformCookie = false;
1407
+ if (!this.isMobileApp() && !this.isAjax()) {
1408
+ // don't change platform in mobile apps (and ajax requests) from query or cookie
1409
+ const platform = this.ctx.query[PLATFORM_PARAM] || this.cookie(PLATFORM_COOKIE);
1410
+ setPlatformCookie = this.setPlatform(platform);
1411
+ if (setPlatformCookie) {
1412
+ this.cookie(PLATFORM_COOKIE, platform, {
1413
+ maxAge: PLATFORM_COOKIE_DURATION,
1414
+ domain: '*',
1415
+ });
1416
+ }
1417
+ }
1418
+ if (!setPlatformCookie) {
1419
+ const smPlatform = this.header('x-sm-platform');
1420
+ if (smPlatform) {
1421
+ this._platform = smPlatform;
1422
+ }
1383
1423
  }
1384
1424
  }
1385
1425
 
@@ -1496,7 +1536,7 @@ class Request {
1496
1536
  }
1497
1537
  catch (e) {
1498
1538
  this._flash = '';
1499
- console.error('Error parsing flash message', e);
1539
+ logError('Error parsing flash message', e);
1500
1540
  }
1501
1541
 
1502
1542
  this.cookie(FLASH_COOKIE, null);
@@ -1578,4 +1618,5 @@ class Request {
1578
1618
  }
1579
1619
  }
1580
1620
 
1621
+ Request.setLogger = setLogger;
1581
1622
  module.exports = Request;
package/basicAuth.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const auth = require('koa-basic-auth');
2
2
 
3
- module.exports = function enableBasicAuth(app, options = {}) {
3
+ module.exports = function enableBasicAuth(app, options) {
4
4
  if (!options || options.enabled === false) return;
5
5
 
6
6
  app.use(async (ctx, next) => {
package/botBanning.js CHANGED
@@ -26,7 +26,7 @@ function banned(ctx, email) {
26
26
  ctx.body = `<pre>Our system has detected unusual traffic from your ip ${ip}. Hence your ip has been banned temporarily.\n${emailStr}</pre>`;
27
27
  }
28
28
 
29
- module.exports = function enableBotBanning(app, options = {}) {
29
+ module.exports = function enableBotBanning(app, options) {
30
30
  if (!options || options.enabled === false) return;
31
31
 
32
32
  let userAgents = [];
package/logger.js ADDED
@@ -0,0 +1,22 @@
1
+ let globalLogger;
2
+
3
+ /**
4
+ * set logger
5
+ */
6
+ function setLogger(logger) {
7
+ globalLogger = logger;
8
+ }
9
+
10
+ function logError(...args) {
11
+ if (globalLogger !== undefined) {
12
+ if (globalLogger) globalLogger.error(...args);
13
+ }
14
+ else {
15
+ console.error(...args);
16
+ }
17
+ }
18
+
19
+ module.exports = {
20
+ setLogger,
21
+ logError,
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smpx/koa-request",
3
- "version": "0.3.0",
3
+ "version": "0.4.2",
4
4
  "description": "Handle basic tasks for koajs",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/rateLimit.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const {RateLimit} = require('koa2-ratelimit');
2
2
 
3
- module.exports = function enableRateLimit(app, options = {}) {
3
+ module.exports = function enableRateLimit(app, options) {
4
4
  if (!options || options.enabled === false) return;
5
5
 
6
6
  const skip = options.skip;
package/staticPaths.js CHANGED
@@ -20,8 +20,8 @@ function getMiddleware(options) {
20
20
 
21
21
  if (options.path === '/') {
22
22
  // root requires special handling to check if extension denotes a static path
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)$/);
24
- if (!matches) {
23
+ const isStatic = /^\/(.*)\.(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)$/.test(ctx.path);
24
+ if (!isStatic) {
25
25
  await next();
26
26
  return;
27
27
  }
@@ -48,7 +48,7 @@ function getMiddleware(options) {
48
48
 
49
49
  if (options.immutable) {
50
50
  // return a 304 not modified response, as immutables can't be modified
51
- if (ctx.headers['if-modified-since']) {
51
+ if (ctx.headers['if-modified-since'] && !ctx.response.get('Cache-Control')) {
52
52
  ctx.status = 304;
53
53
  return;
54
54
  }