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.
Files changed (59) hide show
  1. package/LICENSE +3 -20
  2. package/README.md +31 -124
  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 +149 -71
  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/.github/workflows/main.yml +0 -79
  15. package/CHANGELOG.md +0 -93
  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,79 +0,0 @@
1
- name: ci
2
- on:
3
- push:
4
- branches:
5
- - master
6
- pull_request:
7
- branches:
8
- - master
9
-
10
- jobs:
11
- check:
12
- runs-on: ubuntu-latest
13
- steps:
14
- - uses: actions/checkout@v2
15
- - uses: actions/setup-node@v2
16
- with:
17
- node-version: 14
18
- - run: npm install
19
- - run: npx aegir lint
20
- - run: npx aegir build
21
- - run: npx aegir dep-check
22
- - uses: ipfs/aegir/actions/bundle-size@master
23
- with:
24
- github_token: ${{ secrets.GITHUB_TOKEN }}
25
- test-node:
26
- needs: check
27
- runs-on: ${{ matrix.os }}
28
- strategy:
29
- matrix:
30
- os: [windows-latest, ubuntu-latest, macos-latest]
31
- node: [14, 15]
32
- fail-fast: true
33
- steps:
34
- - uses: actions/checkout@v2
35
- - uses: actions/setup-node@v1
36
- with:
37
- node-version: ${{ matrix.node }}
38
- - run: npm install
39
- - run: npx aegir test -t node --bail --cov
40
- - uses: codecov/codecov-action@v1
41
- test-chrome:
42
- needs: check
43
- runs-on: ubuntu-latest
44
- steps:
45
- - uses: actions/checkout@v2
46
- - uses: microsoft/playwright-github-action@v1
47
- - run: npm install
48
- - run: npx aegir test -t browser -t webworker --bail
49
- - uses: codecov/codecov-action@v1
50
- test-firefox:
51
- needs: check
52
- runs-on: ubuntu-latest
53
- steps:
54
- - uses: actions/checkout@v2
55
- - uses: microsoft/playwright-github-action@v1
56
- - run: npm install
57
- - run: npx aegir test -t browser -t webworker --bail -- --browser firefox
58
- test-webkit:
59
- needs: check
60
- runs-on: ubuntu-latest
61
- steps:
62
- - uses: actions/checkout@v2
63
- - uses: microsoft/playwright-github-action@v1
64
- - run: npm install
65
- - run: npx aegir test -t browser -t webworker --bail -- --browser webkit
66
- test-electron-main:
67
- needs: check
68
- runs-on: ubuntu-latest
69
- steps:
70
- - uses: actions/checkout@v2
71
- - run: npm install
72
- - run: npx xvfb-maybe aegir test -t electron-main --bail
73
- test-electron-renderer:
74
- needs: check
75
- runs-on: ubuntu-latest
76
- steps:
77
- - uses: actions/checkout@v2
78
- - run: npm install
79
- - run: npx xvfb-maybe aegir test -t electron-renderer --bail
package/CHANGELOG.md DELETED
@@ -1,93 +0,0 @@
1
- <a name="2.0.3"></a>
2
- ## [2.0.3](https://github.com/ipfs/protons/compare/v2.0.2...v2.0.3) (2021-08-24)
3
-
4
-
5
-
6
- <a name="2.0.2"></a>
7
- ## [2.0.2](https://github.com/ipfs/protons/compare/v2.0.1...v2.0.2) (2021-08-11)
8
-
9
-
10
-
11
- <a name="2.0.1"></a>
12
- ## [2.0.1](https://github.com/ipfs/protons/compare/v2.0.0...v2.0.1) (2021-04-16)
13
-
14
-
15
- ### Bug Fixes
16
-
17
- * update uint8arrays ([#15](https://github.com/ipfs/protons/issues/15)) ([8a9dbf9](https://github.com/ipfs/protons/commit/8a9dbf9))
18
-
19
-
20
-
21
- <a name="2.0.0"></a>
22
- # [2.0.0](https://github.com/ipfs/protons/compare/v1.2.1...v2.0.0) (2020-08-04)
23
-
24
-
25
- ### Bug Fixes
26
-
27
- * replace node buffers with uint8arrays ([#14](https://github.com/ipfs/protons/issues/14)) ([d3547cb](https://github.com/ipfs/protons/commit/d3547cb))
28
-
29
-
30
- ### BREAKING CHANGES
31
-
32
- * - Byte arrays returned from this module will now be UintArrays and not Node Buffers
33
-
34
-
35
-
36
- <a name="1.2.1"></a>
37
- ## [1.2.1](https://github.com/ipfs/protons/compare/v1.2.0...v1.2.1) (2020-06-24)
38
-
39
-
40
- ### Bug Fixes
41
-
42
- * **ci:** add empty commit to fix lint checks on master ([3b5276f](https://github.com/ipfs/protons/commit/3b5276f))
43
-
44
-
45
- ### Features
46
-
47
- * 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)
48
-
49
-
50
-
51
- <a name="1.2.0"></a>
52
- # [1.2.0](https://github.com/ipfs/protons/compare/v1.1.0...v1.2.0) (2020-04-24)
53
-
54
-
55
-
56
- <a name="1.1.0"></a>
57
- # [1.1.0](https://github.com/ipfs/protons/compare/v1.0.2...v1.1.0) (2020-04-24)
58
-
59
-
60
-
61
- <a name="1.0.2"></a>
62
- ## [1.0.2](https://github.com/ipfs/protons/compare/v1.0.0...v1.0.2) (2020-03-18)
63
-
64
-
65
- ### Bug Fixes
66
-
67
- * 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)
68
- * make default value only return default value ([fce4635](https://github.com/ipfs/protons/commit/fce4635))
69
-
70
-
71
- ### Features
72
-
73
- * adds accessor methods for object properties ([3beeb49](https://github.com/ipfs/protons/commit/3beeb49))
74
-
75
-
76
-
77
- <a name="1.0.0"></a>
78
- # [1.0.0](https://github.com/ipfs/protons/compare/122d5c0...v1.0.0) (2017-09-06)
79
-
80
-
81
- ### Bug Fixes
82
-
83
- * updated for packed fixes ([122d5c0](https://github.com/ipfs/protons/commit/122d5c0))
84
-
85
-
86
- ### Features
87
-
88
- * make benchmarks browser compatible ([9645adf](https://github.com/ipfs/protons/commit/9645adf))
89
- * rename to protons ([db6d2fb](https://github.com/ipfs/protons/commit/db6d2fb))
90
- * upgrade dependencies ([fd66b86](https://github.com/ipfs/protons/commit/fd66b86))
91
-
92
-
93
-
@@ -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 { fromString: 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