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
package/CHANGELOG.md DELETED
@@ -1,88 +0,0 @@
1
- <a name="2.0.2"></a>
2
- ## [2.0.2](https://github.com/ipfs/protons/compare/v2.0.1...v2.0.2) (2021-08-11)
3
-
4
-
5
-
6
- <a name="2.0.1"></a>
7
- ## [2.0.1](https://github.com/ipfs/protons/compare/v2.0.0...v2.0.1) (2021-04-16)
8
-
9
-
10
- ### Bug Fixes
11
-
12
- * update uint8arrays ([#15](https://github.com/ipfs/protons/issues/15)) ([8a9dbf9](https://github.com/ipfs/protons/commit/8a9dbf9))
13
-
14
-
15
-
16
- <a name="2.0.0"></a>
17
- # [2.0.0](https://github.com/ipfs/protons/compare/v1.2.1...v2.0.0) (2020-08-04)
18
-
19
-
20
- ### Bug Fixes
21
-
22
- * replace node buffers with uint8arrays ([#14](https://github.com/ipfs/protons/issues/14)) ([d3547cb](https://github.com/ipfs/protons/commit/d3547cb))
23
-
24
-
25
- ### BREAKING CHANGES
26
-
27
- * - Byte arrays returned from this module will now be UintArrays and not Node Buffers
28
-
29
-
30
-
31
- <a name="1.2.1"></a>
32
- ## [1.2.1](https://github.com/ipfs/protons/compare/v1.2.0...v1.2.1) (2020-06-24)
33
-
34
-
35
- ### Bug Fixes
36
-
37
- * **ci:** add empty commit to fix lint checks on master ([3b5276f](https://github.com/ipfs/protons/commit/3b5276f))
38
-
39
-
40
- ### Features
41
-
42
- * support Uint8Array input in place of Buffer ([#13](https://github.com/ipfs/protons/issues/13)) ([6c7add4](https://github.com/ipfs/protons/commit/6c7add4)), closes [/github.com/ipfs/protons/blob/3b5276f052f2e17c2d806d27cd9a88e156588977/src/compile/encodings.js#L30-L43](https://github.com//github.com/ipfs/protons/blob/3b5276f052f2e17c2d806d27cd9a88e156588977/src/compile/encodings.js/issues/L30-L43) [#12](https://github.com/ipfs/protons/issues/12)
43
-
44
-
45
-
46
- <a name="1.2.0"></a>
47
- # [1.2.0](https://github.com/ipfs/protons/compare/v1.1.0...v1.2.0) (2020-04-24)
48
-
49
-
50
-
51
- <a name="1.1.0"></a>
52
- # [1.1.0](https://github.com/ipfs/protons/compare/v1.0.2...v1.1.0) (2020-04-24)
53
-
54
-
55
-
56
- <a name="1.0.2"></a>
57
- ## [1.0.2](https://github.com/ipfs/protons/compare/v1.0.0...v1.0.2) (2020-03-18)
58
-
59
-
60
- ### Bug Fixes
61
-
62
- * add buffer and allow browser tests ([#11](https://github.com/ipfs/protons/issues/11)) ([c1d3d7e](https://github.com/ipfs/protons/commit/c1d3d7e)), closes [ipfs/js-ipfs#2924](https://github.com/ipfs/js-ipfs/issues/2924)
63
- * make default value only return default value ([fce4635](https://github.com/ipfs/protons/commit/fce4635))
64
-
65
-
66
- ### Features
67
-
68
- * adds accessor methods for object properties ([3beeb49](https://github.com/ipfs/protons/commit/3beeb49))
69
-
70
-
71
-
72
- <a name="1.0.0"></a>
73
- # [1.0.0](https://github.com/ipfs/protons/compare/122d5c0...v1.0.0) (2017-09-06)
74
-
75
-
76
- ### Bug Fixes
77
-
78
- * updated for packed fixes ([122d5c0](https://github.com/ipfs/protons/commit/122d5c0))
79
-
80
-
81
- ### Features
82
-
83
- * make benchmarks browser compatible ([9645adf](https://github.com/ipfs/protons/commit/9645adf))
84
- * rename to protons ([db6d2fb](https://github.com/ipfs/protons/commit/db6d2fb))
85
- * upgrade dependencies ([fd66b86](https://github.com/ipfs/protons/commit/fd66b86))
86
-
87
-
88
-
@@ -1,30 +0,0 @@
1
- 'use strict'
2
-
3
- module.exports = `message Bar {
4
- message Foo {
5
- optional uint32 baz = 1;
6
- }
7
-
8
- optional Foo tmp = 1;
9
- }
10
-
11
- enum FOO {
12
- LOL=1;
13
- ABE=3;
14
- }
15
-
16
- message Yo {
17
- repeated FOO lol = 1;
18
- }
19
-
20
- message Lol {
21
- optional string lol = 1;
22
- required Bar b = 2;
23
- }
24
-
25
- message Test {
26
- optional Lol meh = 6;
27
- optional uint32 hello = 3;
28
- optional string foo = 1;
29
- optional bytes payload = 7;
30
- }`
package/bench/index.js DELETED
@@ -1,57 +0,0 @@
1
- 'use strict'
2
-
3
- const Benchmark = require('benchmark')
4
- if (typeof window !== 'undefined') {
5
- window.Benchmark = Benchmark
6
- }
7
-
8
- const protobuf = require('protocol-buffers')
9
- const protonsNpm = require('protons')
10
- const protons = require('../')
11
- const proto = require('./bench.proto')
12
- const messages = protobuf(proto)
13
- const messagesBuf = protons(proto)
14
- const messagesNpm = protonsNpm(proto)
15
- const uint8ArrayFromString = require('uint8arrays/from-string')
16
-
17
- const EXAMPLE = {
18
- foo: 'hello',
19
- hello: 42,
20
- payload: uint8ArrayFromString('a'),
21
- meh: {
22
- b: {
23
- tmp: {
24
- baz: 1000
25
- }
26
- },
27
- lol: 'lol'
28
- }
29
- }
30
-
31
- const suite = new Benchmark.Suite()
32
-
33
- function add (name, encode, decode) {
34
- const EXAMPLE_BUFFER = encode(EXAMPLE)
35
-
36
- suite
37
- .add(name + ' (encode)', function () {
38
- return encode(EXAMPLE)
39
- })
40
- .add(name + ' (decode)', function () {
41
- return decode(EXAMPLE_BUFFER)
42
- })
43
- .add(name + ' (encode + decode)', function () {
44
- return decode(encode(EXAMPLE))
45
- })
46
- }
47
-
48
- add('JSON', JSON.stringify, JSON.parse)
49
- add(`protocol-buffers@${require('protocol-buffers/package.json').version}`, messages.Test.encode, messages.Test.decode)
50
- add(`protons@${require('protons/package.json').version}`, messagesNpm.Test.encode, messagesNpm.Test.decode)
51
- add('local', messagesBuf.Test.encode, messagesBuf.Test.decode)
52
-
53
- suite
54
- .on('cycle', (e) => {
55
- console.log(String(e.target))
56
- })
57
- .run()
package/example.js DELETED
@@ -1,19 +0,0 @@
1
- /* eslint no-console: "off" */
2
- 'use strict'
3
-
4
- var protobuf = require('./')
5
- var path = require('path')
6
- var fs = require('fs')
7
-
8
- var messages = protobuf(fs.readFileSync(path.join(__dirname, '/example.proto')))
9
-
10
- var ex = {
11
- foo: 'hello world',
12
- num: 42
13
- }
14
-
15
- var buf = messages.Test.encode(ex)
16
-
17
- console.log('test message', ex)
18
- console.log('encoded test message', buf)
19
- console.log('encoded test message decoded', messages.Test.decode(buf))
package/example.proto DELETED
@@ -1,4 +0,0 @@
1
- message Test {
2
- optional string foo = 1;
3
- required int32 num = 2;
4
- }
@@ -1,330 +0,0 @@
1
- /* eslint max-depth: 1 */
2
- 'use strict'
3
-
4
- const varint = require('varint')
5
- const defined = require('./utils').defined
6
-
7
- function toSentenceCase (string) {
8
- return `${string.substring(0, 1).toUpperCase()}${string.substring(1)}`
9
- }
10
-
11
- function addPropertyAccessors (obj, name, value, defaultValue) {
12
- if (Object.prototype.hasOwnProperty.call(obj, name)) {
13
- // have already added this property
14
- return
15
- }
16
-
17
- const sentenceCaseName = toSentenceCase(name)
18
-
19
- Object.defineProperties(obj, {
20
- [name]: {
21
- enumerable: true,
22
- configurable: true,
23
- set: (val) => {
24
- value = val
25
- },
26
- get: () => {
27
- if (value === undefined) {
28
- return defaultValue
29
- }
30
-
31
- return value
32
- }
33
- },
34
- [`has${sentenceCaseName}`]: {
35
- configurable: true,
36
- value: () => {
37
- return value !== undefined
38
- }
39
- },
40
- [`set${sentenceCaseName}`]: {
41
- configurable: true,
42
- value: (val) => {
43
- value = val
44
- }
45
- },
46
- [`get${sentenceCaseName}`]: {
47
- configurable: true,
48
- value: () => {
49
- return value
50
- }
51
- },
52
- [`clear${sentenceCaseName}`]: {
53
- configurable: true,
54
- value: () => {
55
- value = undefined
56
- obj[name] = undefined
57
- }
58
- }
59
- })
60
- }
61
-
62
- function compileDecode (m, resolve, enc) {
63
- const requiredFields = []
64
- const fields = {}
65
- const oneofFields = []
66
- const vals = []
67
-
68
- for (var i = 0; i < enc.length; i++) {
69
- const field = m.fields[i]
70
-
71
- fields[field.tag] = i
72
-
73
- const def = field.options && field.options.default
74
- const resolved = resolve(field.type, m.id, false)
75
- vals[i] = [def, resolved && resolved.values]
76
-
77
- m.fields[i].packed = field.repeated && field.options && field.options.packed && field.options.packed !== 'false'
78
-
79
- if (field.required) {
80
- requiredFields.push(field.name)
81
- }
82
-
83
- if (field.oneof) {
84
- oneofFields.push(field.name)
85
- }
86
- }
87
-
88
- function decodeField (e, field, obj, buf, dataView, offset, i) {
89
- const name = field.name
90
-
91
- if (field.oneof) {
92
- // clear already defined oneof fields
93
- const props = Object.keys(obj)
94
- for (var j = 0; j < props.length; j++) {
95
- if (oneofFields.indexOf(props[j]) > -1) {
96
- const sentenceCase = toSentenceCase(props[j])
97
- delete obj[`has${sentenceCase}`]
98
- delete obj[`get${sentenceCase}`]
99
- delete obj[`set${sentenceCase}`]
100
- delete obj[`clear${sentenceCase}`]
101
- delete obj[props[j]]
102
- }
103
- }
104
- }
105
-
106
- let value
107
-
108
- if (e.message) {
109
- const len = varint.decode(buf, offset)
110
- offset += varint.decode.bytes
111
-
112
- const decoded = e.decode(buf, dataView, offset, offset + len)
113
-
114
- if (field.map) {
115
- value = obj[name] || {}
116
- value[decoded.key] = decoded.value
117
- } else if (field.repeated) {
118
- value = obj[name] || []
119
- value.push(decoded)
120
- } else {
121
- value = decoded
122
- }
123
- } else {
124
- if (field.repeated) {
125
- value = obj[name] || []
126
- value.push(e.decode(buf, dataView, offset))
127
- } else {
128
- value = e.decode(buf, dataView, offset)
129
- }
130
- }
131
-
132
- addPropertyAccessors(obj, name, value)
133
-
134
- offset += e.decode.bytes
135
-
136
- return offset
137
- }
138
-
139
- return function decode (buf, view, offset, end) {
140
- if (offset == null) {
141
- offset = 0
142
- }
143
-
144
- if (end == null) {
145
- end = buf.length
146
- }
147
-
148
- if (!(end <= buf.length && offset <= buf.length)) {
149
- throw new Error('Decoded message is not valid')
150
- }
151
-
152
- if (!view) {
153
- view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
154
- }
155
-
156
- var oldOffset = offset
157
- var obj = {}
158
- var field
159
-
160
- while (true) {
161
- if (end <= offset) {
162
- // finished
163
-
164
- // check required methods
165
- var name = ''
166
- var j = 0
167
- for (j = 0; j < requiredFields.length; j++) {
168
- name = requiredFields[j]
169
- if (!defined(obj[name])) {
170
- throw new Error('Decoded message is not valid, missing required field: ' + name)
171
- }
172
- }
173
-
174
- // fill out missing defaults
175
- var val
176
- var def
177
- for (j = 0; j < enc.length; j++) {
178
- field = m.fields[j]
179
- def = vals[j][0]
180
- val = vals[j][1]
181
- name = field.name
182
- let defaultVal
183
-
184
- if (Object.prototype.hasOwnProperty.call(obj, name)) {
185
- continue
186
- }
187
-
188
- var done = false
189
-
190
- if (field.oneof) {
191
- var props = Object.keys(obj)
192
-
193
- for (var k = 0; k < props.length; k++) {
194
- if (oneofFields.indexOf(props[k]) > -1) {
195
- done = true
196
- break
197
- }
198
- }
199
- }
200
-
201
- if (done) {
202
- continue
203
- }
204
-
205
- if (val) { // is enum
206
- if (field.repeated) {
207
- def = []
208
- } else {
209
- def = (def && val[def]) ? val[def].value : val[Object.keys(val)[0]].value
210
- def = parseInt(def || 0, 10)
211
- }
212
- } else {
213
- defaultVal = defaultValue(field)
214
- def = coerceValue(field, def)
215
- }
216
-
217
- addPropertyAccessors(obj, name, def, defaultVal)
218
- }
219
-
220
- decode.bytes = offset - oldOffset
221
- return obj
222
- }
223
-
224
- var prefix = varint.decode(buf, offset)
225
- offset += varint.decode.bytes
226
- var tag = prefix >> 3
227
-
228
- var i = fields[tag]
229
-
230
- if (i == null) {
231
- offset = skip(prefix & 7, buf, view, offset)
232
- continue
233
- }
234
-
235
- var e = enc[i]
236
- field = m.fields[i]
237
-
238
- if (field.packed) {
239
- var packedEnd = varint.decode(buf, offset)
240
- offset += varint.decode.bytes
241
- packedEnd += offset
242
-
243
- while (offset < packedEnd) {
244
- offset = decodeField(e, field, obj, buf, view, offset, i)
245
- }
246
- } else {
247
- offset = decodeField(e, field, obj, buf, view, offset, i)
248
- }
249
- }
250
- }
251
- }
252
-
253
- var skip = function (type, buffer, view, offset) {
254
- switch (type) {
255
- case 0:
256
- varint.decode(buffer, offset)
257
- return offset + varint.decode.bytes
258
-
259
- case 1:
260
- return offset + 8
261
-
262
- case 2:
263
- var len = varint.decode(buffer, offset)
264
- return offset + varint.decode.bytes + len
265
-
266
- case 3:
267
- case 4:
268
- throw new Error('Groups are not supported')
269
-
270
- case 5:
271
- return offset + 4
272
- default:
273
- throw new Error('Unknown wire type: ' + type)
274
- }
275
- }
276
-
277
- var defaultValue = function (f) {
278
- if (f.map) return {}
279
- if (f.repeated) return []
280
-
281
- switch (f.type) {
282
- case 'string':
283
- return ''
284
- case 'bool':
285
- return false
286
- case 'float':
287
- case 'double':
288
- case 'sfixed32':
289
- case 'fixed32':
290
- case 'varint':
291
- case 'enum':
292
- case 'uint64':
293
- case 'uint32':
294
- case 'int64':
295
- case 'int32':
296
- case 'sint64':
297
- case 'sint32':
298
- return 0
299
- default:
300
- return null
301
- }
302
- }
303
-
304
- var coerceValue = function (f, def) {
305
- if (def === undefined) {
306
- return def
307
- }
308
-
309
- switch (f.type) {
310
- case 'bool':
311
- return def === 'true'
312
- case 'float':
313
- case 'double':
314
- case 'sfixed32':
315
- case 'fixed32':
316
- case 'varint':
317
- case 'enum':
318
- case 'uint64':
319
- case 'uint32':
320
- case 'int64':
321
- case 'int32':
322
- case 'sint64':
323
- case 'sint32':
324
- return parseInt(def, 10)
325
- default:
326
- return def
327
- }
328
- }
329
-
330
- module.exports = compileDecode
@@ -1,133 +0,0 @@
1
- 'use strict'
2
-
3
- var defined = require('./utils').defined
4
- var varint = require('varint')
5
-
6
- function compileEncode (m, resolve, enc, oneofs, encodingLength) {
7
- const oneofsKeys = Object.keys(oneofs)
8
- const encLength = enc.length
9
- const ints = {}
10
- for (let i = 0; i < encLength; i++) {
11
- ints[i] = {
12
- p: varint.encode(m.fields[i].tag << 3 | 2),
13
- h: varint.encode(m.fields[i].tag << 3 | enc[i].type)
14
- }
15
-
16
- const field = m.fields[i]
17
- m.fields[i].packed = field.repeated && field.options && field.options.packed && field.options.packed !== 'false'
18
- }
19
-
20
- function encodeField (buf, view, offset, h, e, packed, innerVal) {
21
- let j = 0
22
- if (!packed) {
23
- for (j = 0; j < h.length; j++) {
24
- buf[offset++] = h[j]
25
- }
26
- }
27
-
28
- if (e.message) {
29
- varint.encode(e.encodingLength(innerVal), buf, offset)
30
- offset += varint.encode.bytes
31
- }
32
-
33
- e.encode(innerVal, buf, view, offset)
34
-
35
- return offset + e.encode.bytes
36
- }
37
-
38
- return function encode (obj, buf, view, offset = 0) {
39
- if (buf == null) {
40
- buf = new Uint8Array(encodingLength(obj))
41
- }
42
-
43
- if (view == null) {
44
- view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
45
- }
46
-
47
- const oldOffset = offset
48
- const objKeys = Object.keys(obj)
49
- let i = 0
50
-
51
- // oneof checks
52
-
53
- let match = false
54
- for (i = 0; i < oneofsKeys.length; i++) {
55
- const name = oneofsKeys[i]
56
- const prop = oneofs[i]
57
- if (objKeys.indexOf(prop) > -1) {
58
- if (match) {
59
- throw new Error('only one of the properties defined in oneof ' + name + ' can be set')
60
- }
61
-
62
- match = true
63
- }
64
- }
65
-
66
- for (i = 0; i < encLength; i++) {
67
- const e = enc[i]
68
- const field = m.fields[i] // was f
69
- let val = obj[field.name]
70
- let j = 0
71
-
72
- if (!defined(val)) {
73
- if (field.required) {
74
- throw new Error(field.name + ' is required')
75
- }
76
- continue
77
- }
78
- const p = ints[i].p
79
- const h = ints[i].h
80
-
81
- const packed = field.packed
82
-
83
- if (field.map) {
84
- const tmp = Object.keys(val)
85
- for (j = 0; j < tmp.length; j++) {
86
- tmp[j] = {
87
- key: tmp[j],
88
- value: val[tmp[j]]
89
- }
90
- }
91
- val = tmp
92
- }
93
-
94
- if (packed) {
95
- let packedLen = 0
96
- for (j = 0; j < val.length; j++) {
97
- if (!Object.prototype.hasOwnProperty.call(val, j)) {
98
- continue
99
- }
100
-
101
- packedLen += e.encodingLength(val[j])
102
- }
103
-
104
- if (packedLen) {
105
- for (j = 0; j < h.length; j++) {
106
- buf[offset++] = p[j]
107
- }
108
- varint.encode(packedLen, buf, offset)
109
- offset += varint.encode.bytes
110
- }
111
- }
112
-
113
- if (field.repeated) {
114
- let innerVal
115
- for (j = 0; j < val.length; j++) {
116
- innerVal = val[j]
117
- if (!defined(innerVal)) {
118
- continue
119
- }
120
-
121
- offset = encodeField(buf, view, offset, h, e, packed, innerVal)
122
- }
123
- } else {
124
- offset = encodeField(buf, view, offset, h, e, packed, val)
125
- }
126
- }
127
-
128
- encode.bytes = offset - oldOffset
129
- return buf
130
- }
131
- }
132
-
133
- module.exports = compileEncode