Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

protocol-buffers

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protocol-buffers - npm Package Compare versions

Comparing version 2.7.0 to 3.0.0

test/map.js

162

compile.js

@@ -43,2 +43,3 @@ var encodings = require('./encodings')

var defaultValue = function(f, def) {
if (f.map) return '{}'
if (f.repeated) return '[]'

@@ -90,2 +91,34 @@

messages[m.id] = m
m.fields.forEach(function (f) {
if (!f.map) return
var name = 'Map_' + f.map.from + '_' + f.map.to
var map = {
name: name,
enums: [],
messages: [],
fields: [{
name: 'key',
type: f.map.from,
tag: 1,
repeated: false,
required: true
}, {
name: 'value',
type: f.map.to,
tag: 2,
repeated: false,
required: false
}],
extensions: null,
id: prefix + (prefix ? '.' : '')+name
}
if (!messages[map.id]) {
messages[map.id] = map
schema.messages.push(map)
}
f.type = name
f.repeated = true
})
visit(m, m.id)

@@ -145,19 +178,11 @@ })

var oneofMap = {}
var oneofs = []
m.oneof.forEach(function(o) {
o.fields.forEach(function(f) {
oneofMap[f.name] = o.name
oneofs.push({
name: f.name,
type: f.type,
tag: f.tag,
required: false,
oneof: o.name
})
})
var oneofs = {}
m.fields.forEach(function (f) {
if (!f.oneof) return
if (!oneofs[f.oneof]) oneofs[f.oneof] = []
oneofs[f.oneof].push(f.name)
})
var fields = m.fields.concat(oneofs)
var enc = fields.map(function(f) {
var enc = m.fields.map(function(f) {
return resolve(f.type, m.id)

@@ -167,3 +192,3 @@ })

var forEach = function(fn) {
for (var i = 0; i < enc.length; i++) fn(enc[i], fields[i], genobj('obj', fields[i].name), i)
for (var i = 0; i < enc.length; i++) fn(enc[i], m.fields[i], genobj('obj', m.fields[i].name), i)
}

@@ -177,2 +202,13 @@

Object.keys(oneofs).forEach(function (name) {
var msg = JSON.stringify('only one of the properties defined in oneof ' + name + ' can be set')
var cnt = oneofs[name]
.map(function (prop) {
return '+defined(' + genobj('obj', prop) + ')'
})
.join(' + ')
encodingLength('if ((%s) > 1) throw new Error(%s)', cnt, msg)
})
forEach(function(e, f, val, i) {

@@ -185,2 +221,11 @@ var packed = f.repeated && f.options && f.options.packed

if (f.map) {
encodingLength()
('var tmp = Object.keys(%s)', val)
('for (var i = 0; i < tmp.length; i++) {')
('tmp[i] = {key: tmp[i], value: %s[tmp[i]]}', val)
('}')
val = 'tmp'
}
if (packed) {

@@ -193,4 +238,6 @@ encodingLength()

('packedLen += len')
if (e.message) encodingLength('packedLen += varint.encodingLength(len)')
encodingLength('}')
if (e.message) encodingLength('packedLen += varint.encodingLength(len)')
encodingLength('}')
('if (packedLen) {')

@@ -229,19 +276,2 @@ ('length += %d + packedLen + varint.encodingLength(packedLen)', hl)

('function encode(obj, buf, offset) {')
if (oneofs.length) {
// build up clean object with only the last field set for each oneof
encode()
('var cleanObj={}, foundOneofs = {}')
('var oneofs = %s', JSON.stringify(oneofMap))
('for (var f in obj) {')
('cleanObj[f] = obj[f]')
('if (oneofs[f]) {')
('if (foundOneofs[oneofs[f]]) {')
('delete cleanObj[foundOneofs[oneofs[f]]]')
('}')
('foundOneofs[oneofs[f]] = f')
('}')
('}')
('obj = cleanObj')
}
encode()
('if (!offset) offset = 0')

@@ -251,2 +281,14 @@ ('if (!buf) buf = new Buffer(encodingLength(obj))')

Object.keys(oneofs).forEach(function (name) {
var msg = JSON.stringify('only one of the properties defined in oneof ' + name + ' can be set')
var cnt = oneofs[name]
.map(function (prop) {
return '+defined(' + genobj('obj', prop) + ')'
})
.join(' + ')
encode('if ((%s) > 1) throw new Error(%s)', cnt, msg)
})
forEach(function(e, f, val, i) {

@@ -260,2 +302,11 @@ if (f.required) encode('if (!defined(%s)) throw new Error(%s)', val, JSON.stringify(f.name+' is required'))

if (f.map) {
encode()
('var tmp = Object.keys(%s)', val)
('for (var i = 0; i < tmp.length; i++) {')
('tmp[i] = {key: tmp[i], value: %s[tmp[i]]}', val)
('}')
val = 'tmp'
}
if (packed) {

@@ -329,4 +380,3 @@ encode()

// only set defaults if not a oneof
if (f.oneof == null) {
if (!f.oneof) {
Message('%s = %s', genobj('this', f.name), defaultValue(f, def))

@@ -359,7 +409,2 @@ }

('var obj = new Message()')
if (oneofs.length) {
decode()
('var oneofs = %s', JSON.stringify(oneofMap))
('var foundOneofs = {}')
}

@@ -386,2 +431,10 @@ forEach(function(e, f, val, i) {

if (f.oneof) {
m.fields.forEach(function (otherField) {
if (otherField.oneof === f.oneof && f.name !== otherField.name) {
decode('delete %s', genobj('obj', otherField.name))
}
})
}
if (packed) {

@@ -398,18 +451,15 @@ decode()

decode('offset += varint.decode.bytes')
if (f.repeated) decode('%s.push(enc[%d].decode(buf, offset, offset + len))', val, i)
else decode('%s = enc[%d].decode(buf, offset, offset + len)', val, i)
if (f.map) {
decode('var tmp = enc[%d].decode(buf, offset, offset + len)', i)
decode('%s[tmp.key] = tmp.value', val)
} else if (f.repeated) {
decode('%s.push(enc[%d].decode(buf, offset, offset + len))', val, i)
} else {
decode('%s = enc[%d].decode(buf, offset, offset + len)', val, i)
}
} else {
if (f.repeated) decode('%s.push(enc[%d].decode(buf, offset))', val, i)
else {
if (f.repeated) {
decode('%s.push(enc[%d].decode(buf, offset))', val, i)
} else {
decode('%s = enc[%d].decode(buf, offset)', val, i)
// if a oneof, only keep the last value in the buffer
if (oneofs.length && f.oneof) {
decode('var f = "%s"', f.name)
('if (oneofs[f]) {')
('if (foundOneofs[oneofs[f]]) {')
('delete obj[foundOneofs[oneofs[f]]]')
('}')
('foundOneofs[oneofs[f]] = f')
('}')
}
}

@@ -416,0 +466,0 @@ }

@@ -1,4 +0,5 @@

var protobuf = require('./require')
var protobuf = require('./')
var fs = require('fs')
var messages = protobuf('./example.proto')
var messages = protobuf(fs.readFileSync(__dirname + '/example.proto'))

@@ -5,0 +6,0 @@ var ex = {

@@ -1,5 +0,4 @@

var schema = require('protobuf-schema')
var schema = require('protocol-buffers-schema')
var compile = require('./compile')
module.exports = function(proto, opts) {

@@ -6,0 +5,0 @@ if (!opts) opts = {}

{
"name": "protocol-buffers",
"version": "2.7.0",
"version": "3.0.0",
"description": "Protocol Buffers for Node.js",

@@ -12,4 +12,3 @@ "repository": {

"generate-object-property": "^1.1.0",
"protobuf-schema": "^1.5.0",
"resolve-protobuf-schema": "^1.0.2",
"protocol-buffers-schema": "^2.0.1",
"signed-varint": "^2.0.0",

@@ -16,0 +15,0 @@ "varint": "^4.0.0"

@@ -16,3 +16,3 @@ # protocol-buffers

```
```proto
enum FOO {

@@ -55,10 +55,2 @@ BAR = 1;

You can also use `protocol-buffers/require` to require .proto files from disk instead of
passing them as buffers.
``` js
var protobuf = require('protocol-buffers/require')
var messages = protobuf('test.proto') // will load and parse __dirname/test.proto
```
Enums are accessed in the same way as messages

@@ -65,0 +57,0 @@

@@ -27,3 +27,3 @@ var tape = require('tape')

tape('oneof encode + decode of overloaded oneof json', function(t) {
tape('oneof encode of overloaded json throws', function(t) {
var invalidData = {

@@ -36,6 +36,8 @@ name: 'Foo',

}
var buf = Property.encode(invalidData);
var out = Property.decode(buf)
t.deepEqual(data, out)
t.end()
try {
Property.encode(invalidData);
} catch (err) {
t.ok(true, 'should throw')
t.end()
}
})

@@ -42,0 +44,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc