Uploaded the base of the project the working demo of CH1 Multiplayer Game Development with HTML5
This commit is contained in:
+145
@@ -0,0 +1,145 @@
|
||||
/*!
|
||||
* body-parser
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var getBody = require('raw-body')
|
||||
var iconv = require('iconv-lite')
|
||||
var onFinished = require('on-finished')
|
||||
var typer = require('media-typer')
|
||||
var zlib = require('zlib')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = read
|
||||
|
||||
/**
|
||||
* Read a request into a buffer and parse.
|
||||
*
|
||||
* @param {object} req
|
||||
* @param {object} res
|
||||
* @param {function} next
|
||||
* @param {function} parse
|
||||
* @param {object} options
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function read(req, res, next, parse, options) {
|
||||
var length
|
||||
var stream
|
||||
|
||||
// flag as parsed
|
||||
req._body = true
|
||||
|
||||
try {
|
||||
stream = contentstream(req, options.inflate)
|
||||
length = stream.length
|
||||
delete stream.length
|
||||
} catch (err) {
|
||||
return next(err)
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
options.length = length
|
||||
|
||||
var encoding = options.encoding !== null
|
||||
? options.encoding || 'utf-8'
|
||||
: null
|
||||
var verify = options.verify
|
||||
|
||||
options.encoding = verify
|
||||
? null
|
||||
: encoding
|
||||
|
||||
// read body
|
||||
getBody(stream, options, function (err, body) {
|
||||
if (err) {
|
||||
if (!err.status) {
|
||||
err.status = 400
|
||||
}
|
||||
|
||||
// read off entire request
|
||||
stream.resume()
|
||||
onFinished(req, function onfinished() {
|
||||
next(err)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// verify
|
||||
if (verify) {
|
||||
try {
|
||||
verify(req, res, body, encoding)
|
||||
} catch (err) {
|
||||
if (!err.status) err.status = 403
|
||||
return next(err)
|
||||
}
|
||||
}
|
||||
|
||||
// parse
|
||||
try {
|
||||
body = typeof body !== 'string' && encoding !== null
|
||||
? iconv.decode(body, encoding)
|
||||
: body
|
||||
req.body = parse(body)
|
||||
} catch (err) {
|
||||
if (!err.status) {
|
||||
err.body = body
|
||||
err.status = 400
|
||||
}
|
||||
return next(err)
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content stream of the request.
|
||||
*
|
||||
* @param {object} req
|
||||
* @param {boolean} [inflate=true]
|
||||
* @return {object}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function contentstream(req, inflate) {
|
||||
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
|
||||
var err
|
||||
var length = req.headers['content-length']
|
||||
var stream
|
||||
|
||||
if (inflate === false && encoding !== 'identity') {
|
||||
err = new Error('content encoding unsupported')
|
||||
err.status = 415
|
||||
throw err
|
||||
}
|
||||
|
||||
switch (encoding) {
|
||||
case 'deflate':
|
||||
stream = zlib.createInflate()
|
||||
req.pipe(stream)
|
||||
break
|
||||
case 'gzip':
|
||||
stream = zlib.createGunzip()
|
||||
req.pipe(stream)
|
||||
break
|
||||
case 'identity':
|
||||
stream = req
|
||||
stream.length = length
|
||||
break
|
||||
default:
|
||||
err = new Error('unsupported content encoding')
|
||||
err.status = 415
|
||||
throw err
|
||||
}
|
||||
|
||||
return stream
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
/*!
|
||||
* body-parser
|
||||
* Copyright(c) 2014 Jonathan Ong
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var bytes = require('bytes')
|
||||
var read = require('../read')
|
||||
var typer = require('media-typer')
|
||||
var typeis = require('type-is')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = json
|
||||
|
||||
/**
|
||||
* RegExp to match the first non-space in a string.
|
||||
*/
|
||||
|
||||
var firstcharRegExp = /^\s*(.)/
|
||||
|
||||
/**
|
||||
* Create a middleware to parse JSON bodies.
|
||||
*
|
||||
* @param {object} [options]
|
||||
* @return {function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function json(options) {
|
||||
options = options || {}
|
||||
|
||||
var limit = typeof options.limit !== 'number'
|
||||
? bytes(options.limit || '100kb')
|
||||
: options.limit
|
||||
var inflate = options.inflate !== false
|
||||
var reviver = options.reviver
|
||||
var strict = options.strict !== false
|
||||
var type = options.type || 'json'
|
||||
var verify = options.verify || false
|
||||
|
||||
if (verify !== false && typeof verify !== 'function') {
|
||||
throw new TypeError('option verify must be function')
|
||||
}
|
||||
|
||||
function parse(body) {
|
||||
if (body.length === 0) {
|
||||
// special-case empty json body, as it's a common client-side mistake
|
||||
// TODO: maybe make this configurable or part of "strict" option
|
||||
return {}
|
||||
}
|
||||
|
||||
if (strict) {
|
||||
var first = firstchar(body)
|
||||
|
||||
if (first !== '{' && first !== '[') {
|
||||
throw new Error('invalid json')
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.parse(body, reviver)
|
||||
}
|
||||
|
||||
return function jsonParser(req, res, next) {
|
||||
if (req._body) return next()
|
||||
req.body = req.body || {}
|
||||
|
||||
if (!typeis(req, type)) return next()
|
||||
|
||||
// RFC 7159 sec 8.1
|
||||
var charset = typer.parse(req).parameters.charset || 'utf-8'
|
||||
if (charset.substr(0, 4).toLowerCase() !== 'utf-') {
|
||||
var err = new Error('unsupported charset')
|
||||
err.status = 415
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
|
||||
// read
|
||||
read(req, res, next, parse, {
|
||||
encoding: charset,
|
||||
inflate: inflate,
|
||||
limit: limit,
|
||||
verify: verify
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first non-whitespace character in a string.
|
||||
*
|
||||
* @param {string} str
|
||||
* @return {function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
function firstchar(str) {
|
||||
var match = firstcharRegExp.exec(str)
|
||||
return match ? match[1] : ''
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*!
|
||||
* body-parser
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var bytes = require('bytes')
|
||||
var read = require('../read')
|
||||
var typeis = require('type-is')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = raw
|
||||
|
||||
/**
|
||||
* Create a middleware to parse raw bodies.
|
||||
*
|
||||
* @param {object} [options]
|
||||
* @return {function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function raw(options) {
|
||||
options = options || {};
|
||||
|
||||
var inflate = options.inflate !== false
|
||||
var limit = typeof options.limit !== 'number'
|
||||
? bytes(options.limit || '100kb')
|
||||
: options.limit
|
||||
var type = options.type || 'application/octet-stream'
|
||||
var verify = options.verify || false
|
||||
|
||||
if (verify !== false && typeof verify !== 'function') {
|
||||
throw new TypeError('option verify must be function')
|
||||
}
|
||||
|
||||
function parse(buf) {
|
||||
return buf
|
||||
}
|
||||
|
||||
return function rawParser(req, res, next) {
|
||||
if (req._body) return next()
|
||||
req.body = req.body || {}
|
||||
|
||||
if (!typeis(req, type)) return next()
|
||||
|
||||
// read
|
||||
read(req, res, next, parse, {
|
||||
encoding: null,
|
||||
inflate: inflate,
|
||||
limit: limit,
|
||||
verify: verify
|
||||
})
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*!
|
||||
* body-parser
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var bytes = require('bytes')
|
||||
var read = require('../read')
|
||||
var typeis = require('type-is')
|
||||
var typer = require('media-typer')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = text
|
||||
|
||||
/**
|
||||
* Create a middleware to parse text bodies.
|
||||
*
|
||||
* @param {object} [options]
|
||||
* @return {function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function text(options) {
|
||||
options = options || {};
|
||||
|
||||
var defaultCharset = options.defaultCharset || 'utf-8'
|
||||
var inflate = options.inflate !== false
|
||||
var limit = typeof options.limit !== 'number'
|
||||
? bytes(options.limit || '100kb')
|
||||
: options.limit
|
||||
var type = options.type || 'text/plain'
|
||||
var verify = options.verify || false
|
||||
|
||||
if (verify !== false && typeof verify !== 'function') {
|
||||
throw new TypeError('option verify must be function')
|
||||
}
|
||||
|
||||
function parse(buf) {
|
||||
return buf
|
||||
}
|
||||
|
||||
return function textParser(req, res, next) {
|
||||
if (req._body) return next()
|
||||
req.body = req.body || {}
|
||||
|
||||
if (!typeis(req, type)) return next()
|
||||
|
||||
// get charset
|
||||
var charset = typer.parse(req).parameters.charset || defaultCharset
|
||||
|
||||
// read
|
||||
read(req, res, next, parse, {
|
||||
encoding: charset,
|
||||
inflate: inflate,
|
||||
limit: limit,
|
||||
verify: verify
|
||||
})
|
||||
}
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
/*!
|
||||
* body-parser
|
||||
* Copyright(c) 2014 Jonathan Ong
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var bytes = require('bytes')
|
||||
var deprecate = require('depd')('body-parser')
|
||||
var read = require('../read')
|
||||
var typer = require('media-typer')
|
||||
var typeis = require('type-is')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = urlencoded
|
||||
|
||||
/**
|
||||
* Cache of parser modules.
|
||||
*/
|
||||
|
||||
var parsers = Object.create(null)
|
||||
|
||||
/**
|
||||
* Create a middleware to parse urlencoded bodies.
|
||||
*
|
||||
* @param {object} [options]
|
||||
* @return {function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function urlencoded(options){
|
||||
options = options || {};
|
||||
|
||||
// notice because option default will flip in next major
|
||||
if (options.extended === undefined) {
|
||||
deprecate('undefined extended: provide extended option')
|
||||
}
|
||||
|
||||
var extended = options.extended !== false
|
||||
var inflate = options.inflate !== false
|
||||
var limit = typeof options.limit !== 'number'
|
||||
? bytes(options.limit || '100kb')
|
||||
: options.limit
|
||||
var type = options.type || 'urlencoded'
|
||||
var verify = options.verify || false
|
||||
|
||||
if (verify !== false && typeof verify !== 'function') {
|
||||
throw new TypeError('option verify must be function')
|
||||
}
|
||||
|
||||
var queryparse = extended
|
||||
? extendedparser(options)
|
||||
: simpleparser(options)
|
||||
|
||||
function parse(body) {
|
||||
return body.length
|
||||
? queryparse(body)
|
||||
: {}
|
||||
}
|
||||
|
||||
return function urlencodedParser(req, res, next) {
|
||||
if (req._body) return next();
|
||||
req.body = req.body || {}
|
||||
|
||||
if (!typeis(req, type)) return next();
|
||||
|
||||
var charset = typer.parse(req).parameters.charset || 'utf-8'
|
||||
if (charset.toLowerCase() !== 'utf-8') {
|
||||
var err = new Error('unsupported charset')
|
||||
err.status = 415
|
||||
next(err)
|
||||
return
|
||||
}
|
||||
|
||||
// read
|
||||
read(req, res, next, parse, {
|
||||
encoding: charset,
|
||||
inflate: inflate,
|
||||
limit: limit,
|
||||
verify: verify
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extended query parser.
|
||||
*
|
||||
* @param {object} options
|
||||
*/
|
||||
|
||||
function extendedparser(options) {
|
||||
var parameterLimit = options.parameterLimit !== undefined
|
||||
? options.parameterLimit
|
||||
: 1000
|
||||
var parse = parser('qs')
|
||||
|
||||
if (isNaN(parameterLimit) || parameterLimit < 1) {
|
||||
throw new TypeError('option parameterLimit must be a positive number')
|
||||
}
|
||||
|
||||
if (isFinite(parameterLimit)) {
|
||||
parameterLimit = parameterLimit | 0
|
||||
}
|
||||
|
||||
var opts = {
|
||||
arrayLimit: 100,
|
||||
parameterLimit: parameterLimit
|
||||
}
|
||||
|
||||
return function queryparse(body) {
|
||||
if (overlimit(body, parameterLimit)) {
|
||||
var err = new Error('too many parameters')
|
||||
err.status = 413
|
||||
throw err
|
||||
}
|
||||
|
||||
return parse(body, opts)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the parameter count is over the limit.
|
||||
*
|
||||
* @param {string} body
|
||||
* @param {number} limit
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function overlimit(body, limit) {
|
||||
if (limit === Infinity) {
|
||||
return false
|
||||
}
|
||||
|
||||
var count = 0
|
||||
var index = 0
|
||||
|
||||
while ((index = body.indexOf('&', index)) !== -1) {
|
||||
count++
|
||||
index++
|
||||
|
||||
if (count === limit) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parser for module name dynamically.
|
||||
*
|
||||
* @param {string} name
|
||||
* @return {function}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function parser(name) {
|
||||
var mod = parsers[name]
|
||||
|
||||
if (mod) {
|
||||
return mod.parse
|
||||
}
|
||||
|
||||
// load module
|
||||
mod = parsers[name] = require(name)
|
||||
|
||||
return mod.parse
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the simple query parser.
|
||||
*
|
||||
* @param {object} options
|
||||
*/
|
||||
|
||||
function simpleparser(options) {
|
||||
var parameterLimit = options.parameterLimit !== undefined
|
||||
? options.parameterLimit
|
||||
: 1000
|
||||
var parse = parser('querystring')
|
||||
|
||||
if (isNaN(parameterLimit) || parameterLimit < 1) {
|
||||
throw new TypeError('option parameterLimit must be a positive number')
|
||||
}
|
||||
|
||||
if (isFinite(parameterLimit)) {
|
||||
parameterLimit = parameterLimit | 0
|
||||
}
|
||||
|
||||
return function queryparse(body) {
|
||||
if (overlimit(body, parameterLimit)) {
|
||||
var err = new Error('too many parameters')
|
||||
err.status = 413
|
||||
throw err
|
||||
}
|
||||
|
||||
return parse(body, undefined, undefined, {maxKeys: parameterLimit})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user