- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 748
 
Generic Body Parser #561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Phillip9587
  wants to merge
  27
  commits into
  expressjs:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Phillip9587:generic
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +268
        
        
          −404
        
        
          
        
      
    
  
  
     Open
                    Generic Body Parser #561
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            27 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6e3cbbd
              
                Added support for external parsers to bodyParser.json()
              
              
                 81bcdb8
              
                removed test dependency on json-bigint
              
              
                 c1ed1bc
              
                reworked doc to describe json parser() func better
              
              
                 36fdeee
              
                added parser() option and doc for .text()
              
              
                 e0c6d1a
              
                added parser() option and doc for .raw()
              
              
                 0007807
              
                added parser() option and doc for .urlencoded()
              
              
                 38c1c3f
              
                cleanup to satisfy linter
              
              
                 3057444
              
                added generic parser
              
              
                 825a78b
              
                converted json parser to use generic parser
              
              
                 bd0601b
              
                converted raw parser to use generic parser
              
              
                 149966b
              
                converted text parser to use generic parser
              
              
                 7d7ab1d
              
                converted urlencoded parser to use generic parser
              
              
                 eb881ef
              
                cleanup / fix linter warnings
              
              
                 07def41
              
                removed items from README
              
              
                 b4c3c52
              
                fixed tests after rebase
              
              
                sdellysse 1e59337
              
                satisfying linter
              
              
                sdellysse 3526aff
              
                Ref'd genParser via the bodyparser getter to signal how third party p…
              
              
                sdellysse 12dcaaf
              
                removed dep on object-assign, which didnt support node < 0.10
              
              
                sdellysse 6f5ff23
              
                minor text cleanup
              
              
                sdellysse 4978bab
              
                🔧 add debug script
              
              
                ctcpip e1e629b
              
                🐛 fix object merging
              
              
                ctcpip d574f61
              
                🔥 clean up
              
              
                ctcpip 6f3dae1
              
                Fix rebase
              
              
                Phillip9587 c166e6d
              
                Remove added npm script
              
              
                Phillip9587 debe0a3
              
                Fix rebase
              
              
                Phillip9587 96fada8
              
                Fix charset validation for urlencoded
              
              
                Phillip9587 b83814b
              
                Refactor
              
              
                Phillip9587 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||
| /*! | ||||||
| * body-parser | ||||||
| * Copyright(c) 2014 Jonathan Ong | ||||||
| * Copyright(c) 2014-2015 Douglas Christopher Wilson | ||||||
| * MIT Licensed | ||||||
| */ | ||||||
| 
     | 
||||||
| 'use strict' | ||||||
| 
     | 
||||||
| /** | ||||||
| * Module dependencies. | ||||||
| * @private | ||||||
| */ | ||||||
| 
     | 
||||||
| var bytes = require('bytes') | ||||||
| var contentType = require('content-type') | ||||||
| var createError = require('http-errors') | ||||||
| var debug = require('debug')('body-parser:generic') | ||||||
| var isFinished = require('on-finished').isFinished | ||||||
| var read = require('./read') | ||||||
| var typeis = require('type-is') | ||||||
| 
     | 
||||||
| /** | ||||||
| * Module exports. | ||||||
| */ | ||||||
| 
     | 
||||||
| module.exports = createBodyParser | ||||||
| 
     | 
||||||
| /** | ||||||
| * Use this to create a middleware that parses request bodies | ||||||
| * | ||||||
| * @param {function} parse | ||||||
| * @param {object} options | ||||||
| * @param {object} defaultOptions | ||||||
| * @return {function} | ||||||
| * @public | ||||||
| */ | ||||||
| 
     | 
||||||
| function createBodyParser (parse, options, defaultOptions) { | ||||||
| // Squash the options and the overrides down into one object | ||||||
| var opts = { ...defaultOptions || {}, ...options } | ||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
  | 
||||||
| 
     | 
||||||
| var limit = typeof opts.limit !== 'number' | ||||||
| ? bytes.parse(opts.limit || '100kb') | ||||||
| : opts.limit | ||||||
| var charset = opts.charset | ||||||
| var inflate = opts.inflate !== false | ||||||
| var verify = opts.verify || false | ||||||
| var defaultReqCharset = opts.defaultCharset || 'utf-8' | ||||||
| var type = opts.type | ||||||
| 
     | 
||||||
| if (verify !== false && typeof verify !== 'function') { | ||||||
| throw new TypeError('option verify must be function') | ||||||
| } | ||||||
| 
     | 
||||||
| // create the appropriate type checking function | ||||||
| var shouldParse = typeof type !== 'function' | ||||||
| ? typeChecker(type) | ||||||
| : type | ||||||
| 
     | 
||||||
| // create the appropriate charset validating function | ||||||
| var validCharset = typeof charset !== 'function' | ||||||
| ? charsetValidator(charset) | ||||||
| : charset | ||||||
| 
     | 
||||||
| return function (req, res, next) { | ||||||
| if (isFinished(req)) { | ||||||
| debug('body already parsed') | ||||||
| next() | ||||||
| return | ||||||
| } | ||||||
| 
     | 
||||||
| if (!('body' in req)) { | ||||||
| req.body = undefined | ||||||
| } | ||||||
| 
     | 
||||||
| // skip requests without bodies | ||||||
| if (!typeis.hasBody(req)) { | ||||||
| debug('skip empty body') | ||||||
| next() | ||||||
| return | ||||||
| } | ||||||
| 
     | 
||||||
| debug('content-type %j', req.headers['content-type']) | ||||||
| 
     | 
||||||
| // determine if request should be parsed | ||||||
| if (!shouldParse(req)) { | ||||||
| debug('skip parsing') | ||||||
| next() | ||||||
| return | ||||||
| } | ||||||
| 
     | 
||||||
| // assert charset per RFC 7159 sec 8.1 | ||||||
| var reqCharset = null | ||||||
| if (charset !== undefined) { | ||||||
| reqCharset = getCharset(req) || defaultReqCharset | ||||||
| if (!validCharset(reqCharset)) { | ||||||
| debug('invalid charset') | ||||||
| next(createError(415, 'unsupported charset "' + reqCharset.toUpperCase() + '"', { | ||||||
| charset: reqCharset, | ||||||
| type: 'charset.unsupported' | ||||||
| })) | ||||||
| return | ||||||
| } | ||||||
| } | ||||||
| 
     | 
||||||
| // read | ||||||
| read(req, res, next, parse, debug, { | ||||||
| encoding: reqCharset, | ||||||
| inflate: inflate, | ||||||
| limit: limit, | ||||||
| verify: verify | ||||||
| }) | ||||||
| } | ||||||
| } | ||||||
| 
     | 
||||||
| /** | ||||||
| * Get the charset of a request. | ||||||
| * | ||||||
| * @param {object} req | ||||||
| * @api private | ||||||
| */ | ||||||
| 
     | 
||||||
| function getCharset (req) { | ||||||
| try { | ||||||
| return (contentType.parse(req).parameters.charset || '').toLowerCase() | ||||||
| } catch (e) { | ||||||
| return undefined | ||||||
| } | ||||||
| } | ||||||
| 
     | 
||||||
| /** | ||||||
| * Get the simple type checker. | ||||||
| * | ||||||
| * @param {string} type | ||||||
| * @return {function} | ||||||
| */ | ||||||
| 
     | 
||||||
| function typeChecker (type) { | ||||||
| return function checkType (req) { | ||||||
| return Boolean(typeis(req, type)) | ||||||
| } | ||||||
| } | ||||||
| 
     | 
||||||
| /** | ||||||
| * Get the simple charset validator. | ||||||
| * | ||||||
| * @param {string} type | ||||||
| * @return {function} | ||||||
| */ | ||||||
| 
     | 
||||||
| function charsetValidator (charset) { | ||||||
| return function validateCharset (reqCharset) { | ||||||
| return charset === reqCharset | ||||||
| } | ||||||
| } | ||||||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest passing the
debugfunction as a parameter tocreateBodyParser(). This will allows the created middlewares to use their namespaces, such asbody-parser:json, for debug messages.