@smpx/koa-request 0.3.0 → 0.4.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
@@ -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');
@@ -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,19 +1381,25 @@ 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() {
@@ -1496,7 +1529,7 @@ class Request {
1496
1529
  }
1497
1530
  catch (e) {
1498
1531
  this._flash = '';
1499
- console.error('Error parsing flash message', e);
1532
+ logError('Error parsing flash message', e);
1500
1533
  }
1501
1534
 
1502
1535
  this.cookie(FLASH_COOKIE, null);
@@ -1578,4 +1611,5 @@ class Request {
1578
1611
  }
1579
1612
  }
1580
1613
 
1614
+ Request.setLogger = setLogger;
1581
1615
  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.0",
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
  }