forked from mafintosh/protocol-buffers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (21 loc) · 749 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var schema = require('protocol-buffers-schema')
var compile = require('./compile')
module.exports = function(proto, opts) {
if (!opts) opts = {}
if (!proto) throw new Error('Pass in a .proto string or a protobuf-schema parsed object')
var sch = (typeof proto === 'object' && !Buffer.isBuffer(proto)) ? proto : schema.parse(proto)
// to not make toString,toJSON enumarable we make a fire-and-forget prototype
var Messages = function() {
var self = this
compile(sch, opts.encodings || {}).forEach(function(m) {
self[m.name] = m.values || m
})
}
Messages.prototype.toString = function() {
return schema.stringify(sch)
}
Messages.prototype.toJSON = function() {
return sch
}
return new Messages()
}