protons 2.0.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -20
- package/README.md +31 -124
- package/dist/bin/protons.js +34 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +206 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +149 -71
- package/src/index.ts +282 -0
- package/.aegir.js +0 -11
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/open_an_issue.md +0 -19
- package/.github/config.yml +0 -68
- package/.github/workflows/main.yml +0 -79
- package/CHANGELOG.md +0 -93
- package/bench/bench.proto.js +0 -30
- package/bench/index.js +0 -57
- package/example.js +0 -19
- package/example.proto +0 -4
- package/src/compile/decode.js +0 -330
- package/src/compile/encode.js +0 -133
- package/src/compile/encoding-length.js +0 -102
- package/src/compile/encodings/bool.js +0 -21
- package/src/compile/encodings/bytes.js +0 -42
- package/src/compile/encodings/double.js +0 -21
- package/src/compile/encodings/encoder.js +0 -14
- package/src/compile/encodings/fixed32.js +0 -21
- package/src/compile/encodings/fixed64.js +0 -25
- package/src/compile/encodings/float.js +0 -21
- package/src/compile/encodings/index.js +0 -22
- package/src/compile/encodings/int32.js +0 -22
- package/src/compile/encodings/int64.js +0 -49
- package/src/compile/encodings/sfixed32.js +0 -21
- package/src/compile/encodings/sint64.js +0 -19
- package/src/compile/encodings/string.js +0 -41
- package/src/compile/encodings/varint.js +0 -19
- package/src/compile/index.js +0 -165
- package/src/compile/utils.js +0 -5
- package/src/index.js +0 -39
- package/test/basic.spec.js +0 -109
- package/test/booleans.spec.js +0 -37
- package/test/bytes.spec.js +0 -36
- package/test/corrupted.spec.js +0 -50
- package/test/custom-types.spec.js +0 -55
- package/test/defaults.spec.js +0 -44
- package/test/enums.spec.js +0 -21
- package/test/float.spec.js +0 -28
- package/test/integers.spec.js +0 -72
- package/test/map.spec.js +0 -35
- package/test/nan.spec.js +0 -28
- package/test/nested.spec.js +0 -70
- package/test/notpacked.spec.js +0 -33
- package/test/oneof.spec.js +0 -59
- package/test/optional.spec.js +0 -65
- package/test/packed.spec.js +0 -61
- package/test/repeated.spec.js +0 -74
- package/test/strings.spec.js +0 -45
- package/test/test.proto.js +0 -134
- package/test/utf-8.spec.js +0 -21
package/src/compile/index.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encodings = require('./encodings')
|
|
4
|
-
const compileDecode = require('./decode')
|
|
5
|
-
const compileEncode = require('./encode')
|
|
6
|
-
const compileEncodingLength = require('./encoding-length')
|
|
7
|
-
const varint = require('varint')
|
|
8
|
-
|
|
9
|
-
const flatten = function (values) {
|
|
10
|
-
if (!values) return null
|
|
11
|
-
const result = {}
|
|
12
|
-
Object.keys(values).forEach(function (k) {
|
|
13
|
-
result[k] = values[k].value
|
|
14
|
-
})
|
|
15
|
-
return result
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
module.exports = function (schema, extraEncodings) {
|
|
19
|
-
const messages = {}
|
|
20
|
-
const enums = {}
|
|
21
|
-
const cache = {}
|
|
22
|
-
|
|
23
|
-
const visit = function (schema, prefix) {
|
|
24
|
-
if (schema.enums) {
|
|
25
|
-
schema.enums.forEach(function (e) {
|
|
26
|
-
e.id = prefix + (prefix ? '.' : '') + e.name
|
|
27
|
-
enums[e.id] = e
|
|
28
|
-
visit(e, e.id)
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
if (schema.messages) {
|
|
32
|
-
schema.messages.forEach(function (m) {
|
|
33
|
-
m.id = prefix + (prefix ? '.' : '') + m.name
|
|
34
|
-
messages[m.id] = m
|
|
35
|
-
m.fields.forEach(function (f) {
|
|
36
|
-
if (!f.map) return
|
|
37
|
-
|
|
38
|
-
const name = 'Map_' + f.map.from + '_' + f.map.to
|
|
39
|
-
const map = {
|
|
40
|
-
name: name,
|
|
41
|
-
enums: [],
|
|
42
|
-
messages: [],
|
|
43
|
-
fields: [{
|
|
44
|
-
name: 'key',
|
|
45
|
-
type: f.map.from,
|
|
46
|
-
tag: 1,
|
|
47
|
-
repeated: false,
|
|
48
|
-
required: true
|
|
49
|
-
}, {
|
|
50
|
-
name: 'value',
|
|
51
|
-
type: f.map.to,
|
|
52
|
-
tag: 2,
|
|
53
|
-
repeated: false,
|
|
54
|
-
required: false
|
|
55
|
-
}],
|
|
56
|
-
extensions: null,
|
|
57
|
-
id: prefix + (prefix ? '.' : '') + name
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!messages[map.id]) {
|
|
61
|
-
messages[map.id] = map
|
|
62
|
-
schema.messages.push(map)
|
|
63
|
-
}
|
|
64
|
-
f.type = name
|
|
65
|
-
f.repeated = true
|
|
66
|
-
})
|
|
67
|
-
visit(m, m.id)
|
|
68
|
-
})
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
visit(schema, '')
|
|
73
|
-
|
|
74
|
-
const compileEnum = function (e) {
|
|
75
|
-
const values = Object.keys(e.values || []).map(function (k) {
|
|
76
|
-
return parseInt(e.values[k].value, 10)
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
const encode = function enumEncode (val, buf, view, offset) {
|
|
80
|
-
if (!values.length || values.indexOf(val) === -1) {
|
|
81
|
-
throw new Error('Invalid enum value: ' + val)
|
|
82
|
-
}
|
|
83
|
-
varint.encode(val, buf, offset)
|
|
84
|
-
enumEncode.bytes = varint.encode.bytes
|
|
85
|
-
return buf
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const decode = function enumDecode (buf, view, offset) {
|
|
89
|
-
var val = varint.decode(buf, offset)
|
|
90
|
-
if (!values.length || values.indexOf(val) === -1) {
|
|
91
|
-
throw new Error('Invalid enum value: ' + val)
|
|
92
|
-
}
|
|
93
|
-
enumDecode.bytes = varint.decode.bytes
|
|
94
|
-
return val
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return encodings.make(0, encode, decode, varint.encodingLength)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const compileMessage = function (m, exports) {
|
|
101
|
-
m.messages.forEach(function (nested) {
|
|
102
|
-
exports[nested.name] = resolve(nested.name, m.id)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
m.enums.forEach(function (val) {
|
|
106
|
-
exports[val.name] = flatten(val.values)
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
exports.type = 2
|
|
110
|
-
exports.message = true
|
|
111
|
-
exports.name = m.name
|
|
112
|
-
|
|
113
|
-
const oneofs = {}
|
|
114
|
-
|
|
115
|
-
m.fields.forEach(function (f) {
|
|
116
|
-
if (!f.oneof) return
|
|
117
|
-
if (!oneofs[f.oneof]) oneofs[f.oneof] = []
|
|
118
|
-
oneofs[f.oneof].push(f.name)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
const enc = m.fields.map(function (f) {
|
|
122
|
-
return resolve(f.type, m.id)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
const encodingLength = compileEncodingLength(m, enc, oneofs)
|
|
126
|
-
const encode = compileEncode(m, resolve, enc, oneofs, encodingLength)
|
|
127
|
-
const decode = compileDecode(m, resolve, enc)
|
|
128
|
-
|
|
129
|
-
// end of compilation - return all the things
|
|
130
|
-
|
|
131
|
-
encode.bytes = decode.bytes = 0
|
|
132
|
-
|
|
133
|
-
exports.buffer = true
|
|
134
|
-
exports.encode = encode
|
|
135
|
-
exports.decode = decode
|
|
136
|
-
exports.encodingLength = encodingLength
|
|
137
|
-
|
|
138
|
-
return exports
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const resolve = function (name, from, compile) {
|
|
142
|
-
if (extraEncodings && extraEncodings[name]) return extraEncodings[name]
|
|
143
|
-
if (encodings[name]) return encodings[name]
|
|
144
|
-
|
|
145
|
-
const m = (from ? from + '.' + name : name).split('.')
|
|
146
|
-
.map(function (part, i, list) {
|
|
147
|
-
return list.slice(0, i).concat(name).join('.')
|
|
148
|
-
})
|
|
149
|
-
.reverse()
|
|
150
|
-
.reduce(function (result, id) {
|
|
151
|
-
return result || messages[id] || enums[id]
|
|
152
|
-
}, null)
|
|
153
|
-
|
|
154
|
-
if (compile === false) return m
|
|
155
|
-
if (!m) throw new Error('Could not resolve ' + name)
|
|
156
|
-
|
|
157
|
-
if (m.values) return compileEnum(m)
|
|
158
|
-
const res = cache[m.id] || compileMessage(m, cache[m.id] = {})
|
|
159
|
-
return res
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return (schema.enums || []).concat((schema.messages || []).map(function (message) {
|
|
163
|
-
return resolve(message.id)
|
|
164
|
-
}))
|
|
165
|
-
}
|
package/src/compile/utils.js
DELETED
package/src/index.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var schema = require('protocol-buffers-schema')
|
|
4
|
-
var compile = require('./compile')
|
|
5
|
-
|
|
6
|
-
var flatten = function (values) {
|
|
7
|
-
if (!values) return null
|
|
8
|
-
var result = {}
|
|
9
|
-
Object.keys(values).forEach(function (k) {
|
|
10
|
-
result[k] = values[k].value
|
|
11
|
-
})
|
|
12
|
-
return result
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = function (proto, opts) {
|
|
16
|
-
if (!opts) opts = {}
|
|
17
|
-
if (!proto) throw new Error('Pass in a .proto string or a protobuf-schema parsed object')
|
|
18
|
-
|
|
19
|
-
var sch = (typeof proto === 'object' && !(proto instanceof Uint8Array)) ? proto : schema.parse(proto)
|
|
20
|
-
|
|
21
|
-
// to not make toString,toJSON enumarable we make a fire-and-forget prototype
|
|
22
|
-
var Messages = function () {
|
|
23
|
-
var self = this
|
|
24
|
-
|
|
25
|
-
compile(sch, opts.encodings || {}).forEach(function (m) {
|
|
26
|
-
self[m.name] = flatten(m.values) || m
|
|
27
|
-
})
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
Messages.prototype.toString = function () {
|
|
31
|
-
return schema.stringify(sch)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
Messages.prototype.toJSON = function () {
|
|
35
|
-
return sch
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return new Messages()
|
|
39
|
-
}
|
package/test/basic.spec.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobufNpm = require('protocol-buffers')
|
|
7
|
-
const protobuf = require('../src')
|
|
8
|
-
const proto = require('./test.proto')
|
|
9
|
-
const Basic = protobuf(proto).Basic
|
|
10
|
-
const BasicNpm = protobufNpm(proto).Basic
|
|
11
|
-
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
|
12
|
-
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
|
|
13
|
-
|
|
14
|
-
describe('basic', () => {
|
|
15
|
-
it('should encode basic object', () => {
|
|
16
|
-
const first = {
|
|
17
|
-
num: 1,
|
|
18
|
-
payload: uint8ArrayFromString('lol')
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const b1 = Basic.encode(first)
|
|
22
|
-
|
|
23
|
-
const bn1 = BasicNpm.encode({
|
|
24
|
-
...first,
|
|
25
|
-
payload: 'lol' // old version does not support Uint8Arrays
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
expect(uint8ArrayToString(b1, 'base16')).to.equal(uint8ArrayToString(bn1, 'base16'))
|
|
29
|
-
|
|
30
|
-
const b2 = Basic.encode({
|
|
31
|
-
num: 1,
|
|
32
|
-
payload: uint8ArrayFromString('lol'),
|
|
33
|
-
meeeh: 42
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
const b3 = Basic.encode({
|
|
37
|
-
num: 1,
|
|
38
|
-
payload: uint8ArrayFromString('lol'),
|
|
39
|
-
meeeh: 42
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
expect(b2).to.deep.equal(b1)
|
|
43
|
-
expect(b3).to.deep.equal(b1)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('should encode and decode basic object', () => {
|
|
47
|
-
const b1 = Basic.encode({
|
|
48
|
-
num: 1,
|
|
49
|
-
payload: uint8ArrayFromString('lol')
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
const o1 = Basic.decode(b1)
|
|
53
|
-
|
|
54
|
-
expect(o1).to.have.property('num', 1)
|
|
55
|
-
expect(o1).to.have.deep.property('payload', uint8ArrayFromString('lol'))
|
|
56
|
-
|
|
57
|
-
const b2 = Basic.encode({
|
|
58
|
-
num: 1,
|
|
59
|
-
payload: uint8ArrayFromString('lol'),
|
|
60
|
-
meeeh: 42
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
const o2 = Basic.decode(b2)
|
|
64
|
-
|
|
65
|
-
expect(o2).to.deep.equal(o1)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('should add basic accessors', () => {
|
|
69
|
-
const b1 = Basic.encode({
|
|
70
|
-
num: 1,
|
|
71
|
-
payload: uint8ArrayFromString('lol')
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
const o1 = Basic.decode(b1)
|
|
75
|
-
|
|
76
|
-
expect(o1).to.have.property('hasNum').that.is.a('function')
|
|
77
|
-
expect(o1.hasNum()).to.be.true()
|
|
78
|
-
|
|
79
|
-
expect(o1).to.have.property('setNum').that.is.a('function')
|
|
80
|
-
o1.setNum(5)
|
|
81
|
-
|
|
82
|
-
expect(o1).to.have.property('getNum').that.is.a('function')
|
|
83
|
-
expect(o1.getNum(5)).to.equal(5)
|
|
84
|
-
|
|
85
|
-
expect(o1).to.have.property('clearNum').that.is.a('function')
|
|
86
|
-
o1.clearNum()
|
|
87
|
-
|
|
88
|
-
expect(o1.getNum(5)).to.be.undefined()
|
|
89
|
-
|
|
90
|
-
const methods = Object.keys(o1)
|
|
91
|
-
|
|
92
|
-
expect(methods).to.not.include('getNum')
|
|
93
|
-
expect(methods).to.not.include('setNum')
|
|
94
|
-
expect(methods).to.not.include('hasNum')
|
|
95
|
-
expect(methods).to.not.include('clearNum')
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
it('should encode and decode floats in a basic object', () => {
|
|
99
|
-
const b1 = Basic.encode({
|
|
100
|
-
num: 1.1,
|
|
101
|
-
payload: uint8ArrayFromString('lol')
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
const o1 = Basic.decode(b1)
|
|
105
|
-
|
|
106
|
-
expect(o1).to.have.property('num', 1.1)
|
|
107
|
-
expect(o1).to.have.deep.property('payload', uint8ArrayFromString('lol'))
|
|
108
|
-
})
|
|
109
|
-
})
|
package/test/booleans.spec.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const proto = require('./test.proto')
|
|
8
|
-
const Booleans = protobuf(proto).Booleans
|
|
9
|
-
|
|
10
|
-
describe('booleans', () => {
|
|
11
|
-
it('should encode and decode booleans', () => {
|
|
12
|
-
const b1 = Booleans.encode({
|
|
13
|
-
bool1: true,
|
|
14
|
-
bool2: false
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const o1 = Booleans.decode(b1)
|
|
18
|
-
|
|
19
|
-
expect(o1).to.deep.equal({
|
|
20
|
-
bool1: true,
|
|
21
|
-
bool2: false
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should encode and decode optional booleans', () => {
|
|
26
|
-
const b1 = Booleans.encode({
|
|
27
|
-
bool1: true
|
|
28
|
-
})
|
|
29
|
-
const o1 = Booleans.decode(b1)
|
|
30
|
-
|
|
31
|
-
expect(o1).to.deep.equal({
|
|
32
|
-
bool1: true,
|
|
33
|
-
bool2: false
|
|
34
|
-
})
|
|
35
|
-
expect(o1.hasBool2()).to.be.false()
|
|
36
|
-
})
|
|
37
|
-
})
|
package/test/bytes.spec.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const Bytes = protobuf(require('./test.proto')).Bytes
|
|
8
|
-
|
|
9
|
-
describe('bytes', () => {
|
|
10
|
-
it('should encode and decode bytes', () => {
|
|
11
|
-
const b1 = Bytes.encode({
|
|
12
|
-
req: Uint8Array.from([0, 1, 2, 3]),
|
|
13
|
-
opt: Uint8Array.from([4, 5, 6, 7])
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
const o1 = Bytes.decode(b1)
|
|
17
|
-
|
|
18
|
-
expect(o1).to.deep.equal({
|
|
19
|
-
req: Uint8Array.from([0, 1, 2, 3]),
|
|
20
|
-
opt: Uint8Array.from([4, 5, 6, 7])
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('should encode and decode optional bytes', () => {
|
|
25
|
-
const b1 = Bytes.encode({
|
|
26
|
-
req: Uint8Array.from([0, 1, 2, 3])
|
|
27
|
-
})
|
|
28
|
-
const o1 = Bytes.decode(b1)
|
|
29
|
-
|
|
30
|
-
expect(o1).to.deep.equal({
|
|
31
|
-
req: Uint8Array.from([0, 1, 2, 3]),
|
|
32
|
-
opt: null
|
|
33
|
-
})
|
|
34
|
-
expect(o1.hasOpt()).to.be.false()
|
|
35
|
-
})
|
|
36
|
-
})
|
package/test/corrupted.spec.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
|
7
|
-
const protobuf = require('../src')
|
|
8
|
-
|
|
9
|
-
const protoStr = 'enum AbcType {\n' +
|
|
10
|
-
' IGNORE = 0;\n' +
|
|
11
|
-
' ACK_CONFIRMATION_TOKEN = 1;\n' +
|
|
12
|
-
'}\n' +
|
|
13
|
-
'message AbcAcknowledgeConfirmationToken { // 0x01\n' +
|
|
14
|
-
' optional uint64 confirmation_token = 1;\n' +
|
|
15
|
-
' extensions 1000 to max;\n' +
|
|
16
|
-
'}\n' +
|
|
17
|
-
'message ABC {\n' +
|
|
18
|
-
' required AbcType type = 9;\n' +
|
|
19
|
-
' required uint32 api_version = 8;\n' +
|
|
20
|
-
' optional AbcAcknowledgeConfirmationToken ack_confirmation_token = 1;\n' +
|
|
21
|
-
' extensions 1000 to max;\n' +
|
|
22
|
-
'}\n' +
|
|
23
|
-
'message Open {\n' +
|
|
24
|
-
' required bytes feed = 1;\n' +
|
|
25
|
-
' required bytes nonce = 2;\n' +
|
|
26
|
-
'}'
|
|
27
|
-
|
|
28
|
-
const messages = protobuf(protoStr)
|
|
29
|
-
|
|
30
|
-
describe('corrupted', () => {
|
|
31
|
-
it('should fail to decode an invalid message', () => {
|
|
32
|
-
expect(() => {
|
|
33
|
-
messages.ABC.decode(Uint8Array.from([8, 182, 168, 235, 144, 178, 41]))
|
|
34
|
-
}).to.throw(/not valid/)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('should fail to decode non-byte arrays', () => {
|
|
38
|
-
expect(() => {
|
|
39
|
-
messages.ABC.decode({})
|
|
40
|
-
}).to.throw(/not valid/)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should fail to decode a base16 message', () => {
|
|
44
|
-
const buf = uint8ArrayFromString('cec1', 'base16')
|
|
45
|
-
|
|
46
|
-
expect(() => {
|
|
47
|
-
messages.Open.decode(buf)
|
|
48
|
-
}).to.throw(/Could not decode varint/)
|
|
49
|
-
})
|
|
50
|
-
})
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const CustomType = protobuf(require('./test.proto')).CustomType
|
|
8
|
-
|
|
9
|
-
describe('custom types', () => {
|
|
10
|
-
it('should encode and decode custom types', () => {
|
|
11
|
-
var b1 = CustomType.encode({
|
|
12
|
-
req: {
|
|
13
|
-
num: 5,
|
|
14
|
-
payload: Uint8Array.from([])
|
|
15
|
-
},
|
|
16
|
-
opt: {
|
|
17
|
-
num: 6,
|
|
18
|
-
payload: Uint8Array.from([])
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
var o1 = CustomType.decode(b1)
|
|
23
|
-
|
|
24
|
-
expect(o1).to.deep.equal({
|
|
25
|
-
req: {
|
|
26
|
-
num: 5,
|
|
27
|
-
payload: Uint8Array.from([])
|
|
28
|
-
},
|
|
29
|
-
opt: {
|
|
30
|
-
num: 6,
|
|
31
|
-
payload: Uint8Array.from([])
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('should encode and decode custom types with optional fields', () => {
|
|
37
|
-
var b1 = CustomType.encode({
|
|
38
|
-
req: {
|
|
39
|
-
num: 5,
|
|
40
|
-
payload: Uint8Array.from([])
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
var o1 = CustomType.decode(b1)
|
|
45
|
-
|
|
46
|
-
expect(o1).to.deep.equal({
|
|
47
|
-
req: {
|
|
48
|
-
num: 5,
|
|
49
|
-
payload: Uint8Array.from([])
|
|
50
|
-
},
|
|
51
|
-
opt: null
|
|
52
|
-
})
|
|
53
|
-
expect(o1.hasOpt()).to.be.false()
|
|
54
|
-
})
|
|
55
|
-
})
|
package/test/defaults.spec.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const Defaults = protobuf(require('./test.proto')).Defaults
|
|
8
|
-
|
|
9
|
-
describe('default', () => {
|
|
10
|
-
it('should decode with defaults', () => {
|
|
11
|
-
const o1 = Defaults.decode(new Uint8Array()) // everything default
|
|
12
|
-
|
|
13
|
-
const b2 = Defaults.encode({
|
|
14
|
-
num: 10,
|
|
15
|
-
foos: [1]
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
const b3 = Defaults.encode({
|
|
19
|
-
num: 10,
|
|
20
|
-
foo2: 2
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
expect(Defaults.decode(b3)).to.deep.equal({
|
|
24
|
-
num: 10,
|
|
25
|
-
foo1: 2,
|
|
26
|
-
foo2: 2,
|
|
27
|
-
foos: []
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
expect(o1).to.deep.equal({
|
|
31
|
-
num: 42,
|
|
32
|
-
foo1: 2,
|
|
33
|
-
foo2: 1,
|
|
34
|
-
foos: []
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
expect(Defaults.decode(b2)).to.deep.equal({
|
|
38
|
-
num: 10,
|
|
39
|
-
foo1: 2,
|
|
40
|
-
foo2: 1,
|
|
41
|
-
foos: [1]
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
})
|
package/test/enums.spec.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const messages = protobuf(require('./test.proto'))
|
|
8
|
-
|
|
9
|
-
describe('enums', () => {
|
|
10
|
-
it('should encode and decode enums', () => {
|
|
11
|
-
const e = messages.FOO
|
|
12
|
-
|
|
13
|
-
expect(e).to.deep.equal({ A: 1, B: 2 })
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
it('should encode and decode hex enums', () => {
|
|
17
|
-
const e = messages.FOO_HEX
|
|
18
|
-
|
|
19
|
-
expect(e).to.deep.equal({ A: 1, B: 2 })
|
|
20
|
-
})
|
|
21
|
-
})
|
package/test/float.spec.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const Float = protobuf(require('./test.proto')).Float
|
|
8
|
-
|
|
9
|
-
describe('floats', () => {
|
|
10
|
-
it('should encode and decode floats', () => {
|
|
11
|
-
const arr = new Float32Array(3)
|
|
12
|
-
arr[0] = 1.1
|
|
13
|
-
arr[1] = 0
|
|
14
|
-
arr[2] = -2.3
|
|
15
|
-
|
|
16
|
-
const obj = {
|
|
17
|
-
float1: arr[0],
|
|
18
|
-
float2: arr[1],
|
|
19
|
-
float3: arr[2]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const b1 = Float.encode(obj)
|
|
23
|
-
|
|
24
|
-
const o1 = Float.decode(b1)
|
|
25
|
-
|
|
26
|
-
expect(o1).to.deep.equal(obj)
|
|
27
|
-
})
|
|
28
|
-
})
|
package/test/integers.spec.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const Integers = protobuf(require('./test.proto')).Integers
|
|
8
|
-
|
|
9
|
-
describe('integers', () => {
|
|
10
|
-
it('should encode and decode integers', () => {
|
|
11
|
-
const b1 = Integers.encode({
|
|
12
|
-
sint32: 1,
|
|
13
|
-
sint64: 2,
|
|
14
|
-
int32: 3,
|
|
15
|
-
uint32: 4,
|
|
16
|
-
int64: 5,
|
|
17
|
-
fixed32: 6
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const o1 = Integers.decode(b1)
|
|
21
|
-
|
|
22
|
-
expect(o1).to.deep.equal({
|
|
23
|
-
sint32: 1,
|
|
24
|
-
sint64: 2,
|
|
25
|
-
int32: 3,
|
|
26
|
-
uint32: 4,
|
|
27
|
-
int64: 5,
|
|
28
|
-
fixed32: 6
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('should encode and decode negative integers', () => {
|
|
33
|
-
const b1 = Integers.encode({
|
|
34
|
-
sint32: -1,
|
|
35
|
-
sint64: -2,
|
|
36
|
-
int32: -3,
|
|
37
|
-
uint32: 0,
|
|
38
|
-
int64: -1 * Math.pow(2, 52) - 5,
|
|
39
|
-
fixed32: 0
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
const o1 = Integers.decode(b1)
|
|
43
|
-
|
|
44
|
-
expect(o1).to.deep.equal({
|
|
45
|
-
sint32: -1,
|
|
46
|
-
sint64: -2,
|
|
47
|
-
int32: -3,
|
|
48
|
-
uint32: 0,
|
|
49
|
-
int64: -1 * Math.pow(2, 52) - 5,
|
|
50
|
-
fixed32: 0
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('should encode and decode optional integers', () => {
|
|
55
|
-
const b1 = Integers.encode({
|
|
56
|
-
sint32: null
|
|
57
|
-
})
|
|
58
|
-
const b2 = Integers.encode({
|
|
59
|
-
sint32: 0
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
// sint32 is optional, verify that setting it to null does not
|
|
63
|
-
// cause a value to be written into the encoded buffer
|
|
64
|
-
expect(b1.length).to.be.lessThan(b2.length)
|
|
65
|
-
|
|
66
|
-
const o1 = Integers.decode(b1)
|
|
67
|
-
expect(o1.hasSint32()).to.be.false()
|
|
68
|
-
|
|
69
|
-
const o2 = Integers.decode(b2)
|
|
70
|
-
expect(o2.sint32).to.equal(0)
|
|
71
|
-
})
|
|
72
|
-
})
|
package/test/map.spec.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
const { expect } = require('aegir/utils/chai')
|
|
6
|
-
const protobuf = require('../src')
|
|
7
|
-
const Map = protobuf(require('./test.proto')).Map
|
|
8
|
-
|
|
9
|
-
describe('maps', () => {
|
|
10
|
-
it('should encode and decode maps', () => {
|
|
11
|
-
const b1 = Map.encode({
|
|
12
|
-
foo: {
|
|
13
|
-
hello: 'world'
|
|
14
|
-
}
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const o1 = Map.decode(b1)
|
|
18
|
-
|
|
19
|
-
expect(o1).to.have.deep.property('foo', { hello: 'world' })
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('should encode and decode maps with multiple fields', () => {
|
|
23
|
-
const doc = {
|
|
24
|
-
foo: {
|
|
25
|
-
hello: 'world',
|
|
26
|
-
hi: 'verden'
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const b2 = Map.encode(doc)
|
|
31
|
-
const o2 = Map.decode(b2)
|
|
32
|
-
|
|
33
|
-
expect(o2).to.deep.equal(doc)
|
|
34
|
-
})
|
|
35
|
-
})
|