protobuf.js
Advanced tools
Comparing version 0.0.1 to 0.0.2
20
index.js
@@ -1,2 +0,3 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs'), | ||
path = require('path'); | ||
@@ -46,3 +47,3 @@ function Protobuf(schema) { | ||
function parseSchema(file) { | ||
function parseSchema(file, filepath) { | ||
var lines = file.split('\n'), | ||
@@ -67,3 +68,3 @@ new_lines = [], | ||
if (line.match(/^import/)) { | ||
var f = fs.readFileSync(line.replace(/import\s|\"|;/g, ''), 'utf8'); | ||
var f = fs.readFileSync(path.dirname(filepath) + '/' + line.replace(/import\s|\"|;/g, ''), 'utf8'); | ||
line = parseSchema(f); | ||
@@ -121,3 +122,7 @@ } | ||
default: | ||
new_schema[msg][this_msg[2]].type = 2; | ||
if (new_schema[msg][this_msg[1]] && new_schema[msg][this_msg[1]].raw_type === 'enum') { | ||
new_schema[msg][this_msg[2]].type = 0; | ||
} else { | ||
new_schema[msg][this_msg[2]].type = 2; | ||
} | ||
} | ||
@@ -143,3 +148,3 @@ new_schema[msg][this_msg[2]].raw_type = this_msg[1]; | ||
var this_enum = line.trim().split(' '); | ||
if (!new_schema[msg][line_enum]) new_schema[msg][line_enum] = {}; | ||
if (!new_schema[msg][line_enum]) new_schema[msg][line_enum] = { raw_type: 'enum' }; | ||
new_schema[msg][line_enum][this_enum[0]] = parseInt(this_enum[2], 10); | ||
@@ -155,3 +160,3 @@ } | ||
var f = fs.readFileSync(schema, 'utf8'); | ||
return new Protobuf(parseSchema(f)); | ||
return new Protobuf(parseSchema(f, schema)); | ||
}; | ||
@@ -202,3 +207,3 @@ | ||
Protobuf.prototype.encode = function (message, params) { | ||
if (!~Object.keys(this.schema).indexOf(message)) return false; | ||
if (!~Object.keys(this.schema).indexOf(message) || !params) return new Buffer([]); | ||
var schema = this.schema[message], | ||
@@ -215,2 +220,3 @@ bytes = []; | ||
} else { | ||
console.log(key, schema); | ||
bytes.push(Buffer.byteLength(params[key])); | ||
@@ -217,0 +223,0 @@ for (var i = 0; i < params[key].length; i++) { |
{ | ||
"name": "protobuf.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "a pure javascript protocol buffer encoding implementation, written specifically for riak", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git://github.com/nathan-lafreniere/node-protobuf.js" | ||
"url": "git://github.com/nathan-lafreniere/protobuf.js" | ||
}, | ||
@@ -11,0 +11,0 @@ "keywords": [ |
9410
209