protocol-buffers
Advanced tools
Comparing version 4.0.0 to 4.0.1
99
bin.js
#!/usr/bin/env node | ||
var protobuf = require('./') | ||
@@ -12,2 +11,13 @@ var encodings = require('protocol-buffers-encodings') | ||
var RESERVED = { | ||
type: true, | ||
message: true, | ||
name: true, | ||
buffer: true, | ||
encode: true, | ||
decode: true, | ||
encodingLength: true, | ||
dependencies: true | ||
} | ||
// handrolled parser to not introduce minimist as this is used a bunch of prod places | ||
@@ -53,5 +63,9 @@ // TODO: if this becomes more complicated / has bugs, move to minimist | ||
function isEncoder (m) { | ||
return typeof m.encode === 'function' | ||
} | ||
function compile () { | ||
var out = '' | ||
var messages = protobuf(fs.readFileSync(filename)) | ||
var out = '' | ||
@@ -70,39 +84,60 @@ out += '// This file is auto generated by the protocol-buffers cli tool' + os.EOL | ||
Object.keys(messages).forEach(function (name) { | ||
out += 'var ' + name + ' = exports.' + name + ' = {' + os.EOL | ||
out += ' buffer: true,' + os.EOL | ||
out += ' encodingLength: null,' + os.EOL | ||
out += ' encode: null,' + os.EOL | ||
out += ' decode: null' + os.EOL | ||
out += '}' + os.EOL | ||
out += os.EOL | ||
}) | ||
visit(messages, 'exports', '') | ||
Object.keys(messages).forEach(function (name) { | ||
out += 'define' + name + '()' + os.EOL | ||
}) | ||
function visit (messages, exports, spaces) { | ||
var encoders = Object.keys(messages).filter(function (name) { | ||
if (RESERVED[name]) return false | ||
return isEncoder(messages[name]) | ||
}) | ||
if (Object.keys(messages).length) out += os.EOL | ||
var enums = Object.keys(messages).filter(function (name) { | ||
if (RESERVED[name]) return false | ||
return !isEncoder(messages[name]) | ||
}) | ||
Object.keys(messages).forEach(function (name) { | ||
out += 'function define' + name + ' () {' + os.EOL | ||
out += ' var enc = [' + os.EOL | ||
enums.forEach(function (name) { | ||
out += spaces + 'var ' + name + ' = ' + exports + '.' + name + ' = ' + | ||
JSON.stringify(messages[name], null, 2).replace(/\n/g, os.EOL) + os.EOL + os.EOL | ||
}) | ||
messages[name].dependencies.forEach(function (e, i, enc) { | ||
var name = encodings.name(e) | ||
if (name) name = 'encodings.' + name | ||
else name = e.name | ||
out += ' ' + name + (i < enc.length - 1 ? ',' : '') + os.EOL | ||
encoders.forEach(function (name) { | ||
out += spaces + 'var ' + name + ' = ' + exports + '.' + name + ' = {' + os.EOL | ||
out += spaces + ' buffer: true,' + os.EOL | ||
out += spaces + ' encodingLength: null,' + os.EOL | ||
out += spaces + ' encode: null,' + os.EOL | ||
out += spaces + ' decode: null' + os.EOL | ||
out += spaces + '}' + os.EOL | ||
out += os.EOL | ||
}) | ||
out += ' ]' + os.EOL + os.EOL | ||
out += ' ' + name + '.encodingLength = encodingLength' + os.EOL | ||
out += ' ' + name + '.encode = encode' + os.EOL | ||
out += ' ' + name + '.decode = decode' + os.EOL + os.EOL | ||
out += ' ' + funToString(messages[name].encodingLength, ' ') + os.EOL + os.EOL | ||
out += ' ' + funToString(messages[name].encode, ' ') + os.EOL + os.EOL | ||
out += ' ' + funToString(messages[name].decode, ' ') + os.EOL | ||
out += '}' + os.EOL + os.EOL | ||
}) | ||
encoders.forEach(function (name) { | ||
out += spaces + 'define' + name + '()' + os.EOL | ||
}) | ||
if (encoders.length) out += os.EOL | ||
encoders.forEach(function (name) { | ||
out += spaces + 'function define' + name + ' () {' + os.EOL | ||
visit(messages[name], name, spaces + ' ') | ||
out += spaces + ' var enc = [' + os.EOL | ||
messages[name].dependencies.forEach(function (e, i, enc) { | ||
var name = encodings.name(e) | ||
if (name) name = 'encodings.' + name | ||
else name = e.name | ||
out += spaces + ' ' + name + (i < enc.length - 1 ? ',' : '') + os.EOL | ||
}) | ||
out += spaces + ' ]' + os.EOL + os.EOL | ||
out += spaces + ' ' + name + '.encodingLength = encodingLength' + os.EOL | ||
out += spaces + ' ' + name + '.encode = encode' + os.EOL | ||
out += spaces + ' ' + name + '.decode = decode' + os.EOL + os.EOL | ||
out += spaces + ' ' + funToString(messages[name].encodingLength, spaces + ' ') + os.EOL + os.EOL | ||
out += spaces + ' ' + funToString(messages[name].encode, spaces + ' ') + os.EOL + os.EOL | ||
out += spaces + ' ' + funToString(messages[name].decode, spaces + ' ') + os.EOL | ||
out += spaces + '}' + os.EOL + os.EOL | ||
}) | ||
} | ||
out += funToString(require('./compile').defined, '') + os.EOL | ||
@@ -109,0 +144,0 @@ |
{ | ||
"name": "protocol-buffers", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Protocol Buffers for Node.js", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43205
1207