Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
xprezzo-json-parser
Advanced tools
A middleware that only parses json
and only looks at requests where
the Content-Type
header matches the type
option. This parser accepts any
Unicode encoding of the body and supports automatic inflation of gzip
and
deflate
encodings.
A new body
object containing the parsed data is populated on the request
object after the middleware (i.e. req.body
).
$ npm install xprezzo-json-parser
var JsonParser = require('xprezzo-json-parser')
The JsonParser
object exposes various factories to create middlewares. All
middlewares will populate the req.body
property with the parsed body when
the Content-Type
request header matches the type
option, or an empty
object ({}
) if there was no body to parse, the Content-Type
was not matched,
or an error occurred.
The various errors returned by this module are described in the errors section.
The json
function takes an optional options
object that may contain any of
the following keys:
When set to true
, then deflated (compressed) bodies will be inflated; when
false
, deflated bodies are rejected. Defaults to true
.
Controls the maximum request body size. If this is a number, then the value
specifies the number of bytes; if it is a string, the value is passed to the
bytes library for parsing. Defaults
to '100kb'
.
The reviver
option is passed directly to JSON.parse
as the second
argument. You can find more information on this argument
in the MDN documentation about JSON.parse.
When set to true
, will only accept arrays and objects; when false
will
accept anything JSON.parse
accepts. Defaults to true
.
The type
option is used to determine what media type the middleware will
parse. This option can be a string, array of strings, or a function. If not a
function, type
option is passed directly to the
type-is library and this can
be an extension name (like json
), a mime type (like application/json
), or
a mime type with a wildcard (like */*
or */json
). If a function, the type
option is called as fn(req)
and the request is parsed if it returns a truthy
value. Defaults to application/json
.
The verify
option, if supplied, is called as verify(req, res, buf, encoding)
,
where buf
is a Buffer
of the raw request body and encoding
is the
encoding of the request. The parsing can be aborted by throwing an error.
The middlewares provided by this module create errors using the
xprezzo-http-errors
module. The errors
will typically have a status
/statusCode
property that contains the suggested
HTTP response code, an expose
property to determine if the message
property
should be displayed to the client, a type
property to determine the type of
error without matching against the message
, and a body
property containing
the read body, if available.
The following are the common errors created, though any error can come through for various reasons.
This error will occur when the request had a Content-Encoding
header that
contained an encoding but the "inflation" option was set to false
. The
status
property is set to 415
, the type
property is set to
'encoding.unsupported'
, and the charset
property will be set to the
encoding that is unsupported.
This error will occur when the request contained an entity that could not be
parsed by the middleware. The status
property is set to 400
, the type
property is set to 'entity.parse.failed'
, and the body
property is set to
the entity value that failed parsing.
This error will occur when the request contained an entity that could not be
failed verification by the defined verify
option. The status
property is
set to 403
, the type
property is set to 'entity.verify.failed'
, and the
body
property is set to the entity value that failed verification.
This error will occur when the request is aborted by the client before reading
the body has finished. The received
property will be set to the number of
bytes received before the request was aborted and the expected
property is
set to the number of expected bytes. The status
property is set to 400
and type
property is set to 'request.aborted'
.
This error will occur when the request body's size is larger than the "limit"
option. The limit
property will be set to the byte limit and the length
property will be set to the request body's length. The status
property is
set to 413
and the type
property is set to 'entity.too.large'
.
This error will occur when the request's length did not match the length from
the Content-Length
header. This typically occurs when the request is malformed,
typically when the Content-Length
header was calculated based on characters
instead of bytes. The status
property is set to 400
and the type
property
is set to 'request.size.invalid'
.
This error will occur when something called the req.setEncoding
method prior
to this middleware. This module operates directly on bytes only and you cannot
call req.setEncoding
when using this module. The status
property is set to
500
and the type
property is set to 'stream.encoding.set'
.
This error will occur when the content of the request exceeds the configured
parameterLimit
for the urlencoded
parser. The status
property is set to
413
and the type
property is set to 'parameters.too.many'
.
This error will occur when the request had a charset parameter in the
Content-Type
header, but the xprezzo-iconv-lite
module does not support it OR the
parser does not support it. The charset is contained in the message as well
as in the charset
property. The status
property is set to 415
, the
type
property is set to 'charset.unsupported'
, and the charset
property
is set to the charset that is unsupported.
This error will occur when the request had a Content-Encoding
header that
contained an unsupported encoding. The encoding is contained in the message
as well as in the encoding
property. The status
property is set to 415
,
the type
property is set to 'encoding.unsupported'
, and the encoding
property is set to the encoding that is unsupported.
This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.
var Xprezzo = require('xprezzo')
var JsonParser = require('xprezzo-json-parser')
var app = Xprezzo()
// parse application/json
app.use(JsonParser())
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
})
This example demonstrates adding body parsers specifically to the routes that need them. In general, this is the most recommended way to use body-parser with Express.
var Xprezzo = require('xprezzo')
var JsonParser = require('xprezzo-json-parser')
var app = Xprezzo()
// POST /api/users gets JSON bodies
app.post('/api/users', JsonParser, function (req, res) {
// create user in req.body
})
All the parsers accept a type
option which allows you to change the
Content-Type
that the middleware will parse.
var Xprezzo = require('xprezzo')
var JsonParser = require('xprezzo-json-parser')
var app = Xprezzo()
// parse various different custom JSON types as JSON
app.use(JsonParser({ type: 'application/*+json' }))
FAQs
Xprezzo json parser middleware
The npm package xprezzo-json-parser receives a total of 17 weekly downloads. As such, xprezzo-json-parser popularity was classified as not popular.
We found that xprezzo-json-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.