Name: js-handler/node_modules/restify/node_modules/formidable/lib/json_parser.js
| 1: | if (global.GENTLY) require = GENTLY.hijack(require); |
| 2: | |
| 3: | var Buffer = require('buffer').Buffer |
| 4: | |
| 5: | function JSONParser() { |
| 6: | this.data = new Buffer(''); |
| 7: | this.bytesWritten = 0; |
| 8: | }; |
| 9: | exports.JSONParser = JSONParser; |
| 10: | |
| 11: | JSONParser.prototype.initWithLength = function(length) { |
| 12: | this.data = new Buffer(length); |
| 13: | } |
| 14: | |
| 15: | JSONParser.prototype.write = function(buffer) { |
| 16: | if (this.data.length >= this.bytesWritten + buffer.length) { |
| 17: | buffer.copy(this.data, this.bytesWritten); |
| 18: | } else { |
| 19: | this.data = Buffer.concat([this.data, buffer]); |
| 20: | } |
| 21: | this.bytesWritten += buffer.length; |
| 22: | return buffer.length; |
| 23: | } |
| 24: | |
| 25: | JSONParser.prototype.end = function() { |
| 26: | try { |
| 27: | var fields = JSON.parse(this.data.toString('utf8')) |
| 28: | for (var field in fields) { |
| 29: | this.onField(field, fields[field]); |
| 30: | } |
| 31: | } catch (e) {} |
| 32: | this.data = null; |
| 33: | |
| 34: | this.onEnd(); |
| 35: | } |
