comunicationLeap/node_modules/formidable/lib/json_parser.js
node_modules/formidable/lib/json_parser.jsRaw
1if (global.GENTLY) require = GENTLY.hijack(require);
2
3var Buffer = require('buffer').Buffer
4
5function JSONParser() {
6 this.data = new Buffer('');
7 this.bytesWritten = 0;
8};
9exports.JSONParser = JSONParser;
10
11JSONParser.prototype.initWithLength = function(length) {
12 this.data = new Buffer(length);
13}
14
15JSONParser.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
25JSONParser.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}