Skip to content

Commit 53cbbda

Browse files
authored
BigInt writing support, and support reading from buffer offset (#113)
* support writing BigInts * add buffer offset parameter to parsePacketBuffer * standard fix * lint fix, update doc * use BigIntExtended, update to node 10 * simplify BigIntExt * fix BigIntExt valueOf * simplify bigint further * valueOf -> instanceof * remove unnecessary instanceof BigIntExt * use typeof * cleanup
1 parent 6a625d6 commit 53cbbda

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

doc/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Size of the packet `value` defined by `_fieldInfo` with context `rootNode`
4242

4343
Returns a buffer of the `packet` for `type`.
4444

45-
### ProtoDef.parsePacketBuffer(type,buffer)
45+
### ProtoDef.parsePacketBuffer(type,buffer,offset = 0)
4646

47-
Returns a parsed packet of `buffer` for `type`.
47+
Returns a parsed packet of `buffer` for `type` starting at `offset`.
4848

4949
## Serializer(proto,mainType)
5050

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"readable-stream": "^3.0.3"
2222
},
2323
"engines": {
24-
"node": ">=6"
24+
"node": ">=10.4"
2525
},
2626
"bugs": {
2727
"url": "https://github.com/ProtoDef-io/node-protodef/issues"
@@ -31,6 +31,11 @@
3131
"type": "git",
3232
"url": "https://github.com/ProtoDef-io/node-protodef.git"
3333
},
34+
"standard": {
35+
"ignore": [
36+
"src/datatypes/numeric.js"
37+
]
38+
},
3439
"devDependencies": {
3540
"benchmark": "^2.1.4",
3641
"chai": "^4.1.2",

src/compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class CompiledProtodef {
9595
return buffer
9696
}
9797

98-
parsePacketBuffer (type, buffer) {
99-
const { value, size } = tryCatch(() => this.read(buffer, 0, type),
98+
parsePacketBuffer (type, buffer, offset = 0) {
99+
const { value, size } = tryCatch(() => this.read(buffer, offset, type),
100100
(e) => {
101101
e.message = `Read error for ${e.field} : ${e.message}`
102102
throw e

src/datatypes/numeric.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,78 @@
11
const { PartialReadError } = require('../utils')
22

3+
class BigIntExtended extends Array {
4+
valueOf () { return BigInt.asIntN(64, BigInt(this[0]) << 32n) | BigInt.asUintN(32, BigInt(this[1])) }
5+
}
6+
37
function readI64 (buffer, offset) {
48
if (offset + 8 > buffer.length) { throw new PartialReadError() }
59
return {
6-
value: [buffer.readInt32BE(offset), buffer.readInt32BE(offset + 4)],
10+
value: new BigIntExtended(buffer.readInt32BE(offset), buffer.readInt32BE(offset + 4)),
711
size: 8
812
}
913
}
1014

1115
function writeI64 (value, buffer, offset) {
12-
buffer.writeInt32BE(value[0], offset)
13-
buffer.writeInt32BE(value[1], offset + 4)
16+
if (typeof value === 'bigint') {
17+
buffer.writeBigInt64BE(value, offset)
18+
} else {
19+
buffer.writeInt32BE(value[0], offset)
20+
buffer.writeInt32BE(value[1], offset + 4)
21+
}
1422
return offset + 8
1523
}
1624

1725
function readLI64 (buffer, offset) {
1826
if (offset + 8 > buffer.length) { throw new PartialReadError() }
1927
return {
20-
value: [buffer.readInt32LE(offset + 4), buffer.readInt32LE(offset)],
28+
value: new BigIntExtended(buffer.readInt32LE(offset + 4), buffer.readInt32LE(offset)),
2129
size: 8
2230
}
2331
}
2432

2533
function writeLI64 (value, buffer, offset) {
26-
buffer.writeInt32LE(value[0], offset + 4)
27-
buffer.writeInt32LE(value[1], offset)
34+
if (typeof value === 'bigint') {
35+
buffer.writeBigInt64LE(value, offset)
36+
} else {
37+
buffer.writeInt32LE(value[0], offset + 4)
38+
buffer.writeInt32LE(value[1], offset)
39+
}
2840
return offset + 8
2941
}
3042

3143
function readU64 (buffer, offset) {
3244
if (offset + 8 > buffer.length) { throw new PartialReadError() }
3345
return {
34-
value: [buffer.readUInt32BE(offset), buffer.readUInt32BE(offset + 4)],
46+
value: new BigIntExtended(buffer.readUInt32BE(offset), buffer.readUInt32BE(offset + 4)),
3547
size: 8
3648
}
3749
}
3850

3951
function writeU64 (value, buffer, offset) {
40-
buffer.writeUInt32BE(value[0], offset)
41-
buffer.writeUInt32BE(value[1], offset + 4)
52+
if (typeof value === 'bigint') {
53+
buffer.writeBigUInt64BE(value, offset)
54+
} else {
55+
buffer.writeUInt32BE(value[0], offset)
56+
buffer.writeUInt32BE(value[1], offset + 4)
57+
}
4258
return offset + 8
4359
}
4460

4561
function readLU64 (buffer, offset) {
4662
if (offset + 8 > buffer.length) { throw new PartialReadError() }
4763
return {
48-
value: [buffer.readUInt32LE(offset + 4), buffer.readUInt32LE(offset)],
64+
value: new BigIntExtended(buffer.readUInt32LE(offset + 4), buffer.readUInt32LE(offset)),
4965
size: 8
5066
}
5167
}
5268

5369
function writeLU64 (value, buffer, offset) {
54-
buffer.writeUInt32LE(value[0], offset + 4)
55-
buffer.writeUInt32LE(value[1], offset)
70+
if (typeof value === 'bigint') {
71+
buffer.writeBigUInt64LE(value, offset)
72+
} else {
73+
buffer.writeUInt32LE(value[0], offset + 4)
74+
buffer.writeUInt32LE(value[1], offset)
75+
}
5676
return offset + 8
5777
}
5878

src/protodef.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ class ProtoDef {
145145
return buffer
146146
}
147147

148-
parsePacketBuffer (type, buffer) {
149-
const { value, size } = tryCatch(() => this.read(buffer, 0, type, {}),
148+
parsePacketBuffer (type, buffer, offset = 0) {
149+
const { value, size } = tryCatch(() => this.read(buffer, offset, type, {}),
150150
(e) => {
151151
e.message = `Read error for ${e.field} : ${e.message}`
152152
throw e

0 commit comments

Comments
 (0)