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

parsed-body

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parsed-body - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

33

json.js
'use strict';
function parseJsonBody(req) {
throw new Error('Not implemented');
const getRawBody = require('raw-body');
function withOptions(options) {
options = options || {};
const limit = options.limit || '1mb';
function parse(raw) {
const str = raw.toString('utf8');
try {
return JSON.parse(str);
} catch (parseError) {
parseError.raw = str;
return Promise.reject(parseError);
}
}
function parseJsonBody(req) {
const opts = { limit: limit };
const length = req.headers['content-length'] | 0;
if (length) options.length = length;
return getRawBody(req, opts).then(parse);
}
return parseJsonBody;
}
module.exports = parseJsonBody;
const withDefaultOpions = withOptions();
module.exports = withDefaultOpions;
withDefaultOpions['default'] = withDefaultOpions;
withDefaultOpions.withOptions = withOptions;
{
"name": "parsed-body",
"version": "0.0.0",
"version": "1.0.0",
"description": "Get the parsed body of a request",

@@ -29,4 +29,10 @@ "main": "index.js",

"devDependencies": {
"bluebird": "^2.9.25",
"gofer": "^2.3.7",
"quinn": "^3.2.0",
"tape": "~4.0.0"
},
"dependencies": {
"raw-body": "^2.0.1"
}
}
'use strict';
const http = require('http');
const test = require('tape');
const Gofer = require('gofer');
const Bluebird = require('bluebird'),
async = Bluebird.coroutine;
const createApp = require('quinn'),
respond = createApp.respond;
test('Parses valid json', function(t) {
const parsedBody = require('../json');
let __handler = function(req) { throw new Error('No handler'); };
const testAppUrl = new Bluebird(function(resolve, reject) {
const server = http.createServer(function(req, res) {
(createApp(__handler)(req, res)).then(null, function(error) {
console.error(error.stack);
});
}).listen(0, function() {
resolve('http://127.0.0.1:' + server.address().port);
}).on('error', reject);
server.unref();
});
const getTestApp = async(function *(handler) {
__handler = handler;
return new Gofer({ globalDefaults: { baseUrl: yield testAppUrl } });
});
test('Parses valid json', async(function *(t) {
const client = yield getTestApp(async(function *(req) {
const body = yield parsedBody(req);
return respond.json({ ok: true, body: body });
}));
const data = yield client.post('/', { json: { a: 'b' } });
t.deepEqual(data, { ok: true, body: { a: 'b' } });
t.end();
});
}));
test('Rejects invalid json', async(function *(t) {
const client = yield getTestApp(async(function *(req) {
try {
yield parsedBody(req);
} catch (err) {
return respond.json({
type: err.name,
message: err.message,
raw: err.raw
});
}
}));
const data = yield client.post('/', { body: '{"not json' });
t.deepEqual(data, {
type: 'SyntaxError',
message: 'Unexpected token n',
raw: '{"not json'
});
t.end();
}));
test('Honors content-length', async(function *(t) {
let parseError;
const client = yield getTestApp(async(function *(req) {
try {
yield parsedBody(req);
} catch (err) {
parseError = err;
}
}));
try {
yield client.post('/', {
body: '{}',
headers: { 'Content-Length': 1 }
});
} catch (e) {
t.equal(e.code, 'ECONNRESET',
'Connection reset because body could not be written');
}
t.equal(parseError.name, 'SyntaxError');
t.equal(parseError.message, 'Unexpected end of input');
t.end();
}));

Sorry, the diff of this file is not supported yet

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