protons 2.0.1 → 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 -122
- 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 -70
- 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/.travis.yml +0 -42
- package/CHANGELOG.md +0 -83
- 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
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var defined = require('./utils').defined
|
|
4
|
-
var varint = require('varint')
|
|
5
|
-
|
|
6
|
-
function compileEncodingLength (m, enc, oneofs) {
|
|
7
|
-
const oneofsKeys = Object.keys(oneofs)
|
|
8
|
-
const encLength = enc.length
|
|
9
|
-
|
|
10
|
-
const hls = new Array(encLength)
|
|
11
|
-
|
|
12
|
-
for (let i = 0; i < m.fields.length; i++) {
|
|
13
|
-
hls[i] = varint.encodingLength(m.fields[i].tag << 3 | enc[i].type)
|
|
14
|
-
|
|
15
|
-
const field = m.fields[i]
|
|
16
|
-
m.fields[i].packed = field.repeated && field.options && field.options.packed && field.options.packed !== 'false'
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return function encodingLength (obj) {
|
|
20
|
-
let length = 0
|
|
21
|
-
let i = 0
|
|
22
|
-
let j = 0
|
|
23
|
-
|
|
24
|
-
for (i = 0; i < oneofsKeys.length; i++) {
|
|
25
|
-
const name = oneofsKeys[i]
|
|
26
|
-
const props = oneofs[name]
|
|
27
|
-
|
|
28
|
-
let match = false
|
|
29
|
-
for (j = 0; j < props.length; j++) {
|
|
30
|
-
if (defined(obj[props[j]])) {
|
|
31
|
-
if (match) {
|
|
32
|
-
throw new Error('only one of the properties defined in oneof ' + name + ' can be set')
|
|
33
|
-
}
|
|
34
|
-
match = true
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
for (i = 0; i < encLength; i++) {
|
|
40
|
-
const e = enc[i]
|
|
41
|
-
const field = m.fields[i]
|
|
42
|
-
let val = obj[field.name]
|
|
43
|
-
const hl = hls[i]
|
|
44
|
-
let len
|
|
45
|
-
|
|
46
|
-
if (!defined(val)) {
|
|
47
|
-
if (field.required) {
|
|
48
|
-
throw new Error(field.name + ' is required')
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
continue
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (field.map) {
|
|
55
|
-
const tmp = Object.keys(val)
|
|
56
|
-
for (j = 0; j < tmp.length; j++) {
|
|
57
|
-
tmp[j] = {
|
|
58
|
-
key: tmp[j],
|
|
59
|
-
value: val[tmp[j]]
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
val = tmp
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (field.packed) {
|
|
67
|
-
let packedLen = 0
|
|
68
|
-
for (j = 0; j < val.length; j++) {
|
|
69
|
-
if (!defined(val[j])) {
|
|
70
|
-
continue
|
|
71
|
-
}
|
|
72
|
-
len = e.encodingLength(val[j])
|
|
73
|
-
packedLen += len
|
|
74
|
-
|
|
75
|
-
if (e.message) {
|
|
76
|
-
packedLen += varint.encodingLength(len)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (packedLen) {
|
|
81
|
-
length += hl + packedLen + varint.encodingLength(packedLen)
|
|
82
|
-
}
|
|
83
|
-
} else if (field.repeated) {
|
|
84
|
-
for (j = 0; j < val.length; j++) {
|
|
85
|
-
if (!defined(val[j])) {
|
|
86
|
-
continue
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
len = e.encodingLength(val[j])
|
|
90
|
-
length += hl + len + (e.message ? varint.encodingLength(len) : 0)
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
len = e.encodingLength(val)
|
|
94
|
-
length += hl + len + (e.message ? varint.encodingLength(len) : 0)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return length
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
module.exports = compileEncodingLength
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encoder = require('./encoder')
|
|
4
|
-
|
|
5
|
-
function boolEncodingLength () {
|
|
6
|
-
return 1
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function boolEncode (value, buffer, dataView, offset) {
|
|
10
|
-
buffer[offset] = value ? 1 : 0
|
|
11
|
-
boolEncode.bytes = 1
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function boolDecode (buffer, dataView, offset) {
|
|
15
|
-
const bool = buffer[offset] > 0
|
|
16
|
-
boolDecode.bytes = 1
|
|
17
|
-
|
|
18
|
-
return bool
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = encoder(0, boolEncode, boolDecode, boolEncodingLength)
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const varint = require('varint')
|
|
4
|
-
const encoder = require('./encoder')
|
|
5
|
-
|
|
6
|
-
function bytesBufferLength (val) {
|
|
7
|
-
return val.byteLength
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function bytesEncodingLength (val) {
|
|
11
|
-
const len = bytesBufferLength(val)
|
|
12
|
-
return varint.encodingLength(len) + len
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function bytesEncode (val, buffer, dataView, offset) {
|
|
16
|
-
const oldOffset = offset
|
|
17
|
-
const len = bytesBufferLength(val)
|
|
18
|
-
|
|
19
|
-
varint.encode(len, buffer, offset)
|
|
20
|
-
offset += varint.encode.bytes
|
|
21
|
-
|
|
22
|
-
buffer.set(val, offset)
|
|
23
|
-
offset += len
|
|
24
|
-
|
|
25
|
-
bytesEncode.bytes = offset - oldOffset
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function bytesDecode (buffer, dataView, offset) {
|
|
29
|
-
const oldOffset = offset
|
|
30
|
-
|
|
31
|
-
const len = varint.decode(buffer, offset)
|
|
32
|
-
offset += varint.decode.bytes
|
|
33
|
-
|
|
34
|
-
const val = buffer.slice(offset, offset + len)
|
|
35
|
-
offset += val.length
|
|
36
|
-
|
|
37
|
-
bytesDecode.bytes = offset - oldOffset
|
|
38
|
-
|
|
39
|
-
return val
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
module.exports = encoder(2, bytesEncode, bytesDecode, bytesEncodingLength)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encoder = require('./encoder')
|
|
4
|
-
|
|
5
|
-
function doubleEncodingLength () {
|
|
6
|
-
return 8
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function doubleEncode (val, buffer, dataView, offset) {
|
|
10
|
-
dataView.setFloat64(offset, val, true)
|
|
11
|
-
doubleEncode.bytes = 8
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function doubleDecode (buffer, dataView, offset) {
|
|
15
|
-
const val = dataView.getFloat64(offset, true)
|
|
16
|
-
doubleDecode.bytes = 8
|
|
17
|
-
|
|
18
|
-
return val
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = encoder(1, doubleEncode, doubleDecode, doubleEncodingLength)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encoder = require('./encoder')
|
|
4
|
-
|
|
5
|
-
function fixed32EncodingLength (val) {
|
|
6
|
-
return 4
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function fixed32Encode (val, buffer, dataView, offset) {
|
|
10
|
-
dataView.setUint32(offset, val, true)
|
|
11
|
-
fixed32Encode.bytes = 4
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function fixed32Decode (buffer, dataView, offset) {
|
|
15
|
-
const val = dataView.getUint32(offset, true)
|
|
16
|
-
fixed32Decode.bytes = 4
|
|
17
|
-
|
|
18
|
-
return val
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = encoder(5, fixed32Encode, fixed32Decode, fixed32EncodingLength)
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encoder = require('./encoder')
|
|
4
|
-
|
|
5
|
-
function fixed64EncodingLength () {
|
|
6
|
-
return 8
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function fixed64Encode (val, buffer, dataView, offset) {
|
|
10
|
-
for (const byte of val) {
|
|
11
|
-
buffer[offset] = byte
|
|
12
|
-
offset++
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
fixed64Encode.bytes = 8
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function fixed64Decode (buffer, dataView, offset) {
|
|
19
|
-
const val = buffer.slice(offset, offset + 8)
|
|
20
|
-
fixed64Decode.bytes = 8
|
|
21
|
-
|
|
22
|
-
return val
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = encoder(1, fixed64Encode, fixed64Decode, fixed64EncodingLength)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encoder = require('./encoder')
|
|
4
|
-
|
|
5
|
-
function floatEncodingLength () {
|
|
6
|
-
return 4
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function floatEncode (val, buffer, dataView, offset) {
|
|
10
|
-
dataView.setFloat32(offset, val, true)
|
|
11
|
-
floatEncode.bytes = 4
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function floatDecode (buffer, dataView, offset) {
|
|
15
|
-
const val = dataView.getFloat32(offset, true)
|
|
16
|
-
floatDecode.bytes = 4
|
|
17
|
-
|
|
18
|
-
return val
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = encoder(5, floatEncode, floatDecode, floatEncodingLength)
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.make = require('./encoder')
|
|
4
|
-
exports.bytes = require('./bytes')
|
|
5
|
-
exports.string = require('./string')
|
|
6
|
-
exports.bool = require('./bool')
|
|
7
|
-
exports.int32 = require('./int32')
|
|
8
|
-
exports.int64 = require('./int64')
|
|
9
|
-
exports.sint32 =
|
|
10
|
-
exports.sint64 = require('./sint64')
|
|
11
|
-
exports.uint32 =
|
|
12
|
-
exports.uint64 =
|
|
13
|
-
exports.enum =
|
|
14
|
-
exports.varint = require('./varint')
|
|
15
|
-
|
|
16
|
-
// we cannot represent these in javascript so we just use buffers
|
|
17
|
-
exports.fixed64 =
|
|
18
|
-
exports.sfixed64 = require('./fixed64')
|
|
19
|
-
exports.double = require('./double')
|
|
20
|
-
exports.fixed32 = require('./fixed32')
|
|
21
|
-
exports.sfixed32 = require('./sfixed32')
|
|
22
|
-
exports.float = require('./float')
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const varint = require('varint')
|
|
4
|
-
const encoder = require('./encoder')
|
|
5
|
-
|
|
6
|
-
function in32Encode (val, buffer, dataView, offset) {
|
|
7
|
-
varint.encode(val < 0 ? val + 4294967296 : val, buffer, offset)
|
|
8
|
-
in32Encode.bytes = varint.encode.bytes
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function int32Decode (buffer, dataView, offset) {
|
|
12
|
-
const val = varint.decode(buffer, offset)
|
|
13
|
-
int32Decode.bytes = varint.decode.bytes
|
|
14
|
-
|
|
15
|
-
return val > 2147483647 ? val - 4294967296 : val
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function int32EncodingLength (val) {
|
|
19
|
-
return varint.encodingLength(val < 0 ? val + 4294967296 : val)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
module.exports = encoder(0, in32Encode, int32Decode, int32EncodingLength)
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const varint = require('varint')
|
|
4
|
-
const encoder = require('./encoder')
|
|
5
|
-
|
|
6
|
-
function int64Encode (val, buffer, dataView, offset) {
|
|
7
|
-
if (val < 0) {
|
|
8
|
-
const last = offset + 9
|
|
9
|
-
varint.encode(val * -1, buffer, offset)
|
|
10
|
-
|
|
11
|
-
offset += varint.encode.bytes - 1
|
|
12
|
-
buffer[offset] = buffer[offset] | 0x80
|
|
13
|
-
|
|
14
|
-
while (offset < last - 1) {
|
|
15
|
-
offset++
|
|
16
|
-
buffer[offset] = 0xff
|
|
17
|
-
}
|
|
18
|
-
buffer[last] = 0x01
|
|
19
|
-
|
|
20
|
-
int64Encode.bytes = 10
|
|
21
|
-
} else {
|
|
22
|
-
varint.encode(val, buffer, offset)
|
|
23
|
-
int64Encode.bytes = varint.encode.bytes
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function int64Decode (buffer, dataView, offset) {
|
|
28
|
-
let val = varint.decode(buffer, offset)
|
|
29
|
-
|
|
30
|
-
if (val >= Math.pow(2, 63)) {
|
|
31
|
-
let limit = 9
|
|
32
|
-
while (buffer[offset + limit - 1] === 0xff) limit--
|
|
33
|
-
limit = limit || 9
|
|
34
|
-
const subset = buffer.subarray(offset, offset + limit)
|
|
35
|
-
subset[limit - 1] = subset[limit - 1] & 0x7f
|
|
36
|
-
val = -1 * varint.decode(subset, 0)
|
|
37
|
-
int64Decode.bytes = 10
|
|
38
|
-
} else {
|
|
39
|
-
int64Decode.bytes = varint.decode.bytes
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return val
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function int64EncodingLength (val) {
|
|
46
|
-
return val < 0 ? 10 : varint.encodingLength(val)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
module.exports = encoder(0, int64Encode, int64Decode, int64EncodingLength)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const encoder = require('./encoder')
|
|
4
|
-
|
|
5
|
-
function sfixed32EncodingLength (val) {
|
|
6
|
-
return 4
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function sfixed32Encode (val, buffer, dataView, offset) {
|
|
10
|
-
dataView.setInt32(offset, val, true)
|
|
11
|
-
sfixed32Encode.bytes = 4
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function sfixed32Decode (buffer, dataView, offset) {
|
|
15
|
-
const val = dataView.getInt32(offset, true)
|
|
16
|
-
sfixed32Decode.bytes = 4
|
|
17
|
-
|
|
18
|
-
return val
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = encoder(5, sfixed32Encode, sfixed32Decode, sfixed32EncodingLength)
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const svarint = require('signed-varint')
|
|
4
|
-
const encoder = require('./encoder')
|
|
5
|
-
|
|
6
|
-
function svarintEncode (val, buffer, dataView, offset) {
|
|
7
|
-
svarint.encode(val, buffer, offset)
|
|
8
|
-
|
|
9
|
-
svarintEncode.bytes = svarint.encode.bytes
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function svarintDecode (buffer, dataView, offset) {
|
|
13
|
-
const val = svarint.decode(buffer, offset)
|
|
14
|
-
svarintDecode.bytes = svarint.decode.bytes
|
|
15
|
-
|
|
16
|
-
return val
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = encoder(0, svarintEncode, svarintDecode, svarint.encodingLength)
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const varint = require('varint')
|
|
4
|
-
const uint8ArrayFromString = require('uint8arrays/from-string')
|
|
5
|
-
const uint8ArrayToString = require('uint8arrays/to-string')
|
|
6
|
-
const encoder = require('./encoder')
|
|
7
|
-
|
|
8
|
-
function stringEncodingLength (val) {
|
|
9
|
-
const len = uint8ArrayFromString(val).byteLength
|
|
10
|
-
return varint.encodingLength(len) + len
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function stringEncode (val, buffer, dataView, offset) {
|
|
14
|
-
const oldOffset = offset
|
|
15
|
-
const len = uint8ArrayFromString(val).byteLength
|
|
16
|
-
|
|
17
|
-
varint.encode(len, buffer, offset, 'utf-8')
|
|
18
|
-
offset += varint.encode.bytes
|
|
19
|
-
|
|
20
|
-
const arr = uint8ArrayFromString(val)
|
|
21
|
-
buffer.set(arr, offset)
|
|
22
|
-
offset += arr.length
|
|
23
|
-
|
|
24
|
-
stringEncode.bytes = offset - oldOffset
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function stringDecode (buffer, dataView, offset) {
|
|
28
|
-
const oldOffset = offset
|
|
29
|
-
|
|
30
|
-
const len = varint.decode(buffer, offset)
|
|
31
|
-
offset += varint.decode.bytes
|
|
32
|
-
|
|
33
|
-
const val = uint8ArrayToString(buffer.subarray(offset, offset + len))
|
|
34
|
-
offset += len
|
|
35
|
-
|
|
36
|
-
stringDecode.bytes = offset - oldOffset
|
|
37
|
-
|
|
38
|
-
return val
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
module.exports = encoder(2, stringEncode, stringDecode, stringEncodingLength)
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const varint = require('varint')
|
|
4
|
-
const encoder = require('./encoder')
|
|
5
|
-
|
|
6
|
-
function varintEncode (val, buffer, dataView, offset) {
|
|
7
|
-
varint.encode(val, buffer, offset)
|
|
8
|
-
|
|
9
|
-
varintEncode.bytes = varint.encode.bytes
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function varintDecode (buffer, dataView, offset) {
|
|
13
|
-
const val = varint.decode(buffer, offset)
|
|
14
|
-
varintDecode.bytes = varint.decode.bytes
|
|
15
|
-
|
|
16
|
-
return val
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = encoder(0, varintEncode, varintDecode, varint.encodingLength)
|
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
|
-
}
|