parsed-body
Advanced tools
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5036
101
0
1
4
2
+ Addedraw-body@^2.0.1
+ Addedbytes@3.1.2(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedunpipe@1.0.0(transitive)