remove deprecated ctx.rawBody usage
[squeep-api-dingus] / lib / dingus.js
index 79cee2c448e6d61e7d641b45072c532bc52b8237..5b4643c6b2e79c641c3604910c0a3397be56da6b 100644 (file)
@@ -27,7 +27,7 @@ const defaultOptions = {
   strictAccept: true,
   selfBaseUrl: '',
   staticMetadata: true,
-  staticPath: undefined, // no reasonable default
+  staticPath: undefined, // No reasonable default
   trustProxy: true,
   querystring,
 };
@@ -44,7 +44,7 @@ class Dingus {
    * @param {Boolean} options.trustProxy trust some header data to be provided by proxy
    * @param {Object} options.querystring alternate qs parser to use
    */
-  constructor(logger = common.nullLogger, options = {}) {
+  constructor(logger = console, options = {}) {
     common.setOptions(this, defaultOptions, options);
 
     this.router = new Router(options);
@@ -60,7 +60,6 @@ class Dingus {
     ];
 
     this.logger = logger;
-    common.ensureLoggerLevels(this.logger);
   }
 
 
@@ -139,9 +138,9 @@ class Dingus {
    */
   _getAddress(req) {
     // TODO: RFC7239 Forwarded support
-    const address = (this.trustProxy && req && req.getHeader(Enum.Header.XForwardedFor)) ||
-      (this.trustProxy && req && req.getHeader(Enum.Header.XRealIP)) ||
-      (req && req.connection && req.connection.remoteAddress) ||
+    const address = (this.trustProxy && req?.getHeader(Enum.Header.XForwardedFor)) ||
+      (this.trustProxy && req?.getHeader(Enum.Header.XRealIP)) ||
+      (req?.connection?.remoteAddress) ||
       '';
     return address.split(/\s*,\s*/u)[0];
   }
@@ -153,8 +152,8 @@ class Dingus {
    */
   _getProtocol(req) {
     // TODO: RFC7239 Forwarded support
-    const protocol = (this.trustProxy && req && req.getHeader(Enum.Header.XForwardedProto)) ||
-      ((req && req.connection && req.connection.encrypted) ? 'https' : 'http');
+    const protocol = (this.trustProxy && req?.getHeader(Enum.Header.XForwardedProto)) ||
+      ((req?.connection?.encrypted) ? 'https' : 'http');
     return protocol.split(/\s*,\s*/u)[0];
   }
 
@@ -314,16 +313,11 @@ class Dingus {
    * Parse rawBody as contentType into ctx.parsedBody.
    * @param {string} contentType
    * @param {object} ctx
-   * @param {string|buffer}
+   * @param {string|buffer} rawBody
   */
   parseBody(contentType, ctx, rawBody) {
     const _scope = _fileScope('parseBody');
 
-    if (!rawBody) {
-      // 1.2.4 and earlier expected rawBody on context
-      rawBody = ctx.rawBody;
-    }
-
     switch (contentType) {
       case Enum.ContentType.ApplicationForm:
         ctx.parsedBody = this.querystring.parse(rawBody);
@@ -539,7 +533,7 @@ class Dingus {
     // We will not deal with any subdirs, nor any dot-files.
     // (Note that we could not deal with subdirs even if we wanted, due to simple router matching scheme.)
     if (fileName.indexOf(path.sep) >= 0
-    ||  fileName.charAt(0) === '.') {
+    ||  fileName.startsWith('.')) {
       this.logger.debug(_scope, 'rejected filename', { fileName });
       return this.handlerNotFound(req, res, ctx);
     }
@@ -652,7 +646,7 @@ class Dingus {
       res.setHeader(Enum.Header.ContentType, Enum.ContentType.TextPlain);
     }
 
-    if (err && err.statusCode) {
+    if (err?.statusCode) {
       res.statusCode = err.statusCode;
       body = this.renderError(res.getHeader(Enum.Header.ContentType), err);
       this.logger.debug(_scope, 'handler error', { err, req, res, ctx });