Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

restana

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restana - npm Package Compare versions

Comparing version 4.4.1 to 4.5.0

libs/utils.js

108

libs/response-extensions.js
'use strict'
const { forEachObject } = require('./utils')
const CONTENT_TYPE_HEADER = 'content-type'
const TYPE_JSON = 'application/json; charset=utf-8'
const TYPE_PLAIN = 'text/plain; charset=utf-8'
const TYPE_OCTET = 'application/octet-stream'
const NOOP = () => {}
const stringify = (obj) => {
// ToDo: fast json stringify ?
return JSON.stringify(obj)
}
const preEnd = (res, contentType, statusCode) => {
if (contentType) {
res.setHeader(CONTENT_TYPE_HEADER, contentType)
}
res.statusCode = statusCode
}
const parseErr = (error) => {
const errorCode = error.status || error.code || error.statusCode
const statusCode = typeof errorCode === 'number' ? parseInt(errorCode) : 500
return {
statusCode,
data: stringify({
code: statusCode,
message: error.message,
data: error.data
})
}
}
/**

@@ -9,34 +42,53 @@ * The friendly 'res.send' method

*/
module.exports.send = (options, req, res) => (data = 200, code = 200, headers = null, cb = () => {}) => {
if (headers !== null) {
Object.keys(headers).forEach((key) => {
res.setHeader(key.toLowerCase(), headers[key])
})
}
module.exports.send = (options, req, res) => {
return (data = 200, code = 200, headers = null, cb = NOOP) => {
let contentType
if (typeof data === 'number') {
code = parseInt(data, 10)
data = res.body
} else if (data instanceof Error) {
const errorCode = data.status || data.code || data.statusCode
code = typeof errorCode === 'number' ? parseInt(errorCode) : 500
data = {
code,
message: data.message,
data: data.data
if (data instanceof Error) {
const err = parseErr(data)
contentType = TYPE_JSON
code = err.statusCode
data = err.data
} else {
if (headers && typeof headers === 'object') {
forEachObject(headers, (value, key) => {
res.setHeader(key.toLowerCase(), value)
})
}
// NOTE: only retrieve content-type after setting custom headers
contentType = res.getHeader(CONTENT_TYPE_HEADER)
if (typeof data === 'number') {
code = data
data = res.body
}
if (data) {
if (typeof data === 'string') {
if (!contentType) contentType = TYPE_PLAIN
} else if (typeof data === 'object') {
if (data instanceof Buffer) {
if (!contentType) contentType = TYPE_OCTET
} else if (typeof data.pipe === 'function') {
if (!contentType) contentType = TYPE_OCTET
// NOTE: we exceptionally handle the response termination for streams
preEnd(res, contentType, code)
data.pipe(res)
data.on('end', cb)
return
} else {
if (!contentType) contentType = TYPE_JSON
data = stringify(data)
}
}
}
}
res.setHeader(CONTENT_TYPE_HEADER, 'application/json')
}
if (typeof data === 'object' && data instanceof Buffer === false) {
if (!res.hasHeader(CONTENT_TYPE_HEADER)) {
res.setHeader(CONTENT_TYPE_HEADER, 'application/json')
}
data = JSON.stringify(data)
preEnd(res, contentType, code)
res.end(data, cb)
}
res.statusCode = code
// finally end request
res.end(data, cb)
}
{
"name": "restana",
"version": "4.4.1",
"version": "4.5.0",
"description": "Super fast and minimalist web framework for building REST micro-services.",

@@ -45,4 +45,4 @@ "main": "index.js",

"@hapi/hapi": "^19.1.1",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"connect-query": "^1.0.0",
"express": "^4.17.1",

@@ -54,2 +54,3 @@ "express-jwt": "^5.3.3",

"koa-router": "^8.0.8",
"microtime": "^3.0.0",
"mocha": "^7.1.2",

@@ -56,0 +57,0 @@ "morgan": "^1.10.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc