@naturalcycles/backend-lib 4.15.0 → 4.17.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/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export * from './server/serverStatsMiddleware';
18
18
  export * from './server/methodOverrideMiddleware';
19
19
  export * from './server/notFoundMiddleware';
20
20
  export * from './server/okMiddleware';
21
+ export * from './server/basicAuthMiddleware';
21
22
  export * from './server/requestTimeoutMiddleware';
22
23
  export * from './server/reqValidationMiddleware';
23
24
  export * from './server/simpleRequestLoggerMiddleware';
package/dist/index.js CHANGED
@@ -23,6 +23,7 @@ tslib_1.__exportStar(require("./server/serverStatsMiddleware"), exports);
23
23
  tslib_1.__exportStar(require("./server/methodOverrideMiddleware"), exports);
24
24
  tslib_1.__exportStar(require("./server/notFoundMiddleware"), exports);
25
25
  tslib_1.__exportStar(require("./server/okMiddleware"), exports);
26
+ tslib_1.__exportStar(require("./server/basicAuthMiddleware"), exports);
26
27
  tslib_1.__exportStar(require("./server/requestTimeoutMiddleware"), exports);
27
28
  tslib_1.__exportStar(require("./server/reqValidationMiddleware"), exports);
28
29
  tslib_1.__exportStar(require("./server/simpleRequestLoggerMiddleware"), exports);
@@ -76,6 +76,12 @@ class SentrySharedService {
76
76
  // Skip reporting the error
77
77
  return;
78
78
  }
79
+ if (err?.data?.reportRate) {
80
+ const reportRate = err.data.reportRate;
81
+ // E.g rate of 0.1 means 10% of errors are reported
82
+ if (Math.random() > reportRate)
83
+ return;
84
+ }
79
85
  // This is to avoid Sentry cutting err.message to 253 characters
80
86
  // It will log additional "breadcrumb object" before the error
81
87
  // It's a Breadcrumb, not a console.log, because console.log are NOT automatically attached as Breadcrumbs in cron-job environments (outside of Express)
@@ -0,0 +1,13 @@
1
+ import { StringMap } from '@naturalcycles/js-lib';
2
+ import { BackendRequestHandler } from './server.model';
3
+ export interface BasicAuthMiddlewareCfg {
4
+ /**
5
+ * Map from login (Sting) to password (String).
6
+ */
7
+ loginPasswordMap: StringMap;
8
+ /**
9
+ *
10
+ */
11
+ realm?: string;
12
+ }
13
+ export declare function basicAuthMiddleware(cfg: BasicAuthMiddlewareCfg): BackendRequestHandler;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.basicAuthMiddleware = void 0;
4
+ const js_lib_1 = require("@naturalcycles/js-lib");
5
+ const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
+ function basicAuthMiddleware(cfg) {
7
+ const { realm } = cfg;
8
+ return function basicAuthMiddlewareHandler(req, res, next) {
9
+ const hash = (req.headers.authorization || '').split(' ')[1];
10
+ if (hash) {
11
+ const [login, password] = (0, js_lib_1._split)((0, nodejs_lib_1.base64ToString)(hash), ':', 2);
12
+ if (login && password && cfg.loginPasswordMap[login] === password) {
13
+ return next();
14
+ }
15
+ }
16
+ // Unauthorized
17
+ res
18
+ .set('WWW-Authenticate', `Basic${realm ? ` realm="${realm}"` : ''}`)
19
+ .status(401)
20
+ .send('Unauthorized');
21
+ };
22
+ }
23
+ exports.basicAuthMiddleware = basicAuthMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.15.0",
3
+ "version": "4.17.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install && patch-package",
6
6
  "serve": "APP_ENV=dev nodemon",
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ export * from './server/serverStatsMiddleware'
18
18
  export * from './server/methodOverrideMiddleware'
19
19
  export * from './server/notFoundMiddleware'
20
20
  export * from './server/okMiddleware'
21
+ export * from './server/basicAuthMiddleware'
21
22
  export * from './server/requestTimeoutMiddleware'
22
23
  export * from './server/reqValidationMiddleware'
23
24
  export * from './server/simpleRequestLoggerMiddleware'
@@ -1,4 +1,4 @@
1
- import { _anyToError, _Memo, CommonLogger, CommonLogLevel } from '@naturalcycles/js-lib'
1
+ import { _anyToError, _Memo, AppError, CommonLogger, CommonLogLevel } from '@naturalcycles/js-lib'
2
2
  import { inspectAny } from '@naturalcycles/nodejs-lib'
3
3
  import type { Breadcrumb, NodeOptions, SeverityLevel } from '@sentry/node'
4
4
  import type * as SentryLib from '@sentry/node'
@@ -88,6 +88,12 @@ export class SentrySharedService {
88
88
  return
89
89
  }
90
90
 
91
+ if (err?.data?.reportRate) {
92
+ const reportRate = (err as AppError).data.reportRate!
93
+ // E.g rate of 0.1 means 10% of errors are reported
94
+ if (Math.random() > reportRate) return
95
+ }
96
+
91
97
  // This is to avoid Sentry cutting err.message to 253 characters
92
98
  // It will log additional "breadcrumb object" before the error
93
99
  // It's a Breadcrumb, not a console.log, because console.log are NOT automatically attached as Breadcrumbs in cron-job environments (outside of Express)
@@ -0,0 +1,35 @@
1
+ import { _split, StringMap } from '@naturalcycles/js-lib'
2
+ import { base64ToString } from '@naturalcycles/nodejs-lib'
3
+ import { BackendRequestHandler } from './server.model'
4
+
5
+ export interface BasicAuthMiddlewareCfg {
6
+ /**
7
+ * Map from login (Sting) to password (String).
8
+ */
9
+ loginPasswordMap: StringMap
10
+
11
+ /**
12
+ *
13
+ */
14
+ realm?: string
15
+ }
16
+
17
+ export function basicAuthMiddleware(cfg: BasicAuthMiddlewareCfg): BackendRequestHandler {
18
+ const { realm } = cfg
19
+
20
+ return function basicAuthMiddlewareHandler(req, res, next) {
21
+ const hash = (req.headers.authorization || '').split(' ')[1]
22
+ if (hash) {
23
+ const [login, password] = _split(base64ToString(hash), ':', 2)
24
+ if (login && password && cfg.loginPasswordMap[login] === password) {
25
+ return next()
26
+ }
27
+ }
28
+
29
+ // Unauthorized
30
+ res
31
+ .set('WWW-Authenticate', `Basic${realm ? ` realm="${realm}"` : ''}`)
32
+ .status(401)
33
+ .send('Unauthorized')
34
+ }
35
+ }