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

stubborn-ws

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stubborn-ws - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

4

package.json
{
"name": "stubborn-ws",
"version": "1.1.0",
"version": "1.1.1",
"description": "",

@@ -38,5 +38,7 @@ "homepage": "https://github.com/ybonnefond/stubborn#stubborn",

"dependencies": {
"accept": "^3.1.3",
"body-parser": "^1.18.3",
"content-type": "^1.0.4",
"lodash": "^4.17.10"
}
}

@@ -513,3 +513,3 @@ 'use strict';

it('should respond with provided string body', async() => {
it('should respond with a json encoded body if accept header is json', async() => {
sb

@@ -522,2 +522,19 @@ .get('/')

it('should encode JSON is response content-type header is provided', async() => {
sb
.get('/')
.setResponseBody('toto')
.setResponseHeader('Content-Type', 'application/json; charset=utf-8');
await expect(request('/', { json: false })).toReplyWith(STATUS_CODES.SUCCESS, JSON.stringify('toto'));
});
it('should respond with no encodding if accept and content-type is not provided', async() => {
sb
.get('/')
.setResponseBody('toto');
await expect(request('/', { json: false })).toReplyWith(STATUS_CODES.SUCCESS, 'toto');
});
it('should respond with provided object body', async() => {

@@ -549,3 +566,3 @@ sb

describe('Doc ehandle basic usage', () => {
describe('Doc handle basic usage', () => {
it('should respond to query', async() => {

@@ -552,0 +569,0 @@ const body = { some: 'body' };

@@ -29,5 +29,3 @@ 'use strict';

statusCode: STATUS_CODES.SUCCESS,
headers: {
'Content-Type': '{{headers.accept}}'
},
headers: {},
body: null

@@ -34,0 +32,0 @@ };

@@ -9,2 +9,5 @@ 'use strict';

const accept = require('accept');
const contentType = require('content-type');
class Router {

@@ -119,8 +122,39 @@ constructor(options) {

function reply(route, res, req) {
res.writeHead(route.response.statusCode, applyTemplate(route.response.headers, req));
const headers = applyTemplate(route.response.headers, req);
const body = applyTemplate(route.response.body, req);
res.write(JSON.stringify(body, null, 2)); // eslint-disable-line no-magic-numbers
res.writeHead(route.response.statusCode, headers);
res.write(encodeBody(req, headers, body)); // eslint-disable-line no-magic-numbers
res.end();
}
function encodeBody(req, headers, body) {
const mediaType = findContentType(headers);
const mediaTypes = null === mediaType ? findAcceptTypes(req.headers) : [mediaType];
if (mediaTypes.includes('application/json')) {
return JSON.stringify(body, null, 2); // eslint-disable-line no-magic-numbers
}
return body;
}
function findAcceptTypes(headers) {
const headerKey = Object.keys(headers).find((name) => new RegExp('^accept$', 'i').test(name));
return 'undefined' === typeof headerKey ? [] : accept.mediaTypes(headers[headerKey]);
}
function findContentType(headers) {
const headerKey = Object.keys(headers).find((name) => new RegExp('^content-type$', 'i').test(name));
try {
return contentType.parse(headers[headerKey]).type;
} catch (e) {
return null;
}
}
function applyTemplate(template, req, scope) {

@@ -127,0 +161,0 @@ if (null === template || 'undefined' === typeof template) {

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