protons 2.0.2 → 3.0.1

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.
Files changed (59) hide show
  1. package/LICENSE +3 -20
  2. package/README.md +30 -122
  3. package/dist/bin/protons.js +34 -0
  4. package/dist/src/index.d.ts +2 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +206 -0
  7. package/dist/src/index.js.map +1 -0
  8. package/package.json +148 -70
  9. package/src/index.ts +282 -0
  10. package/.aegir.js +0 -11
  11. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  12. package/.github/ISSUE_TEMPLATE/open_an_issue.md +0 -19
  13. package/.github/config.yml +0 -68
  14. package/.travis.yml +0 -42
  15. package/CHANGELOG.md +0 -88
  16. package/bench/bench.proto.js +0 -30
  17. package/bench/index.js +0 -57
  18. package/example.js +0 -19
  19. package/example.proto +0 -4
  20. package/src/compile/decode.js +0 -330
  21. package/src/compile/encode.js +0 -133
  22. package/src/compile/encoding-length.js +0 -102
  23. package/src/compile/encodings/bool.js +0 -21
  24. package/src/compile/encodings/bytes.js +0 -42
  25. package/src/compile/encodings/double.js +0 -21
  26. package/src/compile/encodings/encoder.js +0 -14
  27. package/src/compile/encodings/fixed32.js +0 -21
  28. package/src/compile/encodings/fixed64.js +0 -25
  29. package/src/compile/encodings/float.js +0 -21
  30. package/src/compile/encodings/index.js +0 -22
  31. package/src/compile/encodings/int32.js +0 -22
  32. package/src/compile/encodings/int64.js +0 -49
  33. package/src/compile/encodings/sfixed32.js +0 -21
  34. package/src/compile/encodings/sint64.js +0 -19
  35. package/src/compile/encodings/string.js +0 -41
  36. package/src/compile/encodings/varint.js +0 -19
  37. package/src/compile/index.js +0 -165
  38. package/src/compile/utils.js +0 -5
  39. package/src/index.js +0 -39
  40. package/test/basic.spec.js +0 -109
  41. package/test/booleans.spec.js +0 -37
  42. package/test/bytes.spec.js +0 -36
  43. package/test/corrupted.spec.js +0 -50
  44. package/test/custom-types.spec.js +0 -55
  45. package/test/defaults.spec.js +0 -44
  46. package/test/enums.spec.js +0 -21
  47. package/test/float.spec.js +0 -28
  48. package/test/integers.spec.js +0 -72
  49. package/test/map.spec.js +0 -35
  50. package/test/nan.spec.js +0 -28
  51. package/test/nested.spec.js +0 -70
  52. package/test/notpacked.spec.js +0 -33
  53. package/test/oneof.spec.js +0 -59
  54. package/test/optional.spec.js +0 -65
  55. package/test/packed.spec.js +0 -61
  56. package/test/repeated.spec.js +0 -74
  57. package/test/strings.spec.js +0 -45
  58. package/test/test.proto.js +0 -134
  59. package/test/utf-8.spec.js +0 -21
@@ -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 uint8ArrayFromString = require('uint8arrays/from-string')
12
- const 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
- })
@@ -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
- })
@@ -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
- })
@@ -1,50 +0,0 @@
1
- /* eslint-env mocha */
2
-
3
- 'use strict'
4
-
5
- const { expect } = require('aegir/utils/chai')
6
- const 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
- })
@@ -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
- })
@@ -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
- })
@@ -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
- })
@@ -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
- })
package/test/nan.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
-
8
- const protoStr = 'message MyMessage {\n' +
9
- ' optional uint32 my_number = 1;\n' +
10
- ' required string my_other = 2;\n' +
11
- '}'
12
-
13
- const messages = protobuf(protoStr)
14
-
15
- describe('NaN', () => {
16
- it('should consider NaN as not defined', () => {
17
- const testString = 'hello!'
18
- const properResult = Uint8Array.from([0x12, 0x06, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21])
19
- const encoded = messages.MyMessage.encode({
20
- my_number: NaN,
21
- my_other: testString
22
- })
23
- const decoded = messages.MyMessage.decode(encoded)
24
-
25
- expect(decoded).to.have.property('my_other', testString)
26
- expect(encoded).to.deep.equal(properResult)
27
- })
28
- })
@@ -1,70 +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 uint8ArrayFromString = require('uint8arrays/from-string')
8
-
9
- const Nested = protobuf(require('./test.proto')).Nested
10
-
11
- describe('nested', () => {
12
- it('should encode nested objects', () => {
13
- const b1 = Nested.encode({
14
- num: 1,
15
- payload: uint8ArrayFromString('lol'),
16
- meh: {
17
- num: 2,
18
- payload: uint8ArrayFromString('bar')
19
- }
20
- })
21
-
22
- const b2 = Nested.encode({
23
- num: 1,
24
- payload: uint8ArrayFromString('lol'),
25
- meeeh: 42,
26
- meh: {
27
- num: 2,
28
- payload: uint8ArrayFromString('bar')
29
- }
30
- })
31
-
32
- expect(b2).to.deep.equal(b1)
33
- })
34
-
35
- it('should decode nested objects', () => {
36
- const b1 = Nested.encode({
37
- num: 1,
38
- payload: uint8ArrayFromString('lol'),
39
- meh: {
40
- num: 2,
41
- payload: uint8ArrayFromString('bar')
42
- }
43
- })
44
-
45
- const o1 = Nested.decode(b1)
46
-
47
- expect(o1).to.have.property('num', 1)
48
- expect(o1).to.have.deep.property('payload', uint8ArrayFromString('lol'))
49
- expect(o1).to.have.deep.property('meh')
50
-
51
- expect(o1).to.have.deep.property('meh', {
52
- num: 2,
53
- payload: uint8ArrayFromString('bar')
54
- })
55
-
56
- const b2 = Nested.encode({
57
- num: 1,
58
- payload: uint8ArrayFromString('lol'),
59
- meeeh: 42,
60
- meh: {
61
- num: 2,
62
- payload: uint8ArrayFromString('bar')
63
- }
64
- })
65
-
66
- const o2 = Nested.decode(b2)
67
-
68
- expect(o2).to.deep.equal(o1)
69
- })
70
- })
@@ -1,33 +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 NotPacked = protobuf(proto).NotPacked
9
- const FalsePacked = protobuf(proto).FalsePacked
10
-
11
- describe('not packed', () => {
12
- it('should encode NotPacked and decode FalsePacked', () => {
13
- const b1 = NotPacked.encode({
14
- id: [9847136125],
15
- value: 10000
16
- })
17
-
18
- const o1 = FalsePacked.decode(b1)
19
-
20
- expect(o1).to.have.deep.property('id', [9847136125])
21
- })
22
-
23
- it('should encode FalsePacked and decode NotPacked', () => {
24
- const b1 = FalsePacked.encode({
25
- id: [9847136125],
26
- value: 10000
27
- })
28
-
29
- const o1 = NotPacked.decode(b1)
30
-
31
- expect(o1).to.have.deep.property('id', [9847136125])
32
- })
33
- })
@@ -1,59 +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 = protobuf(require('./test.proto'))
8
- const Property = proto.Property
9
- const PropertyNoOneof = proto.PropertyNoOneof
10
-
11
- const data = {
12
- name: 'Foo',
13
- desc: 'optional description',
14
- int_value: 12345
15
- }
16
-
17
- describe('onof', () => {
18
- it('should encode oneof', () => {
19
- expect(Property.encode(data)).to.be.ok()
20
- })
21
-
22
- it('should encode and decode oneof', () => {
23
- const buf = Property.encode(data)
24
- const out = Property.decode(buf)
25
-
26
- expect(out).to.deep.equal(data)
27
- })
28
-
29
- it('should throw when encoding overloaded json', () => {
30
- expect(() => {
31
- Property.encode({
32
- name: 'Foo',
33
- desc: 'optional description',
34
- string_value: 'Bar', // ignored
35
- bool_value: true, // ignored
36
- int_value: 12345 // retained, was last entered
37
- })
38
- }).to.throw(/only one of the properties defined in oneof value can be set/)
39
- })
40
-
41
- it('should encode and decode overloaded oneof buffer', () => {
42
- const invalidData = {
43
- name: 'Foo',
44
- desc: 'optional description',
45
- string_value: 'Bar', // retained, has highest tag number
46
- bool_value: true, // ignored
47
- int_value: 12345 // ignored
48
- }
49
- const validData = {
50
- name: 'Foo',
51
- desc: 'optional description',
52
- string_value: 'Bar'
53
- }
54
-
55
- const buf = PropertyNoOneof.encode(invalidData)
56
- const out = Property.decode(buf)
57
- expect(validData).to.deep.equal(out)
58
- })
59
- })