Skip to content

Commit 76e0cd4

Browse files
committed
update tests
1 parent aa024ce commit 76e0cd4

3 files changed

Lines changed: 92 additions & 23 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,31 @@ See the language independent [ProtoDef](https://github.com/ProtoDef-io/ProtoDef)
4545
* [diablo2-protocol](https://github.com/MephisTools/diablo2-protocol) Diablo 2 network protocol
4646
* [dofus-protocol](https://github.com/AstrubTools/dofus-protocol) Network protocol for dofus : create client and servers for dofus 1.30
4747

48+
## Advanced Datatypes
49+
50+
ProtoDef includes advanced datatypes for handling complex data patterns:
51+
52+
### loop
53+
Reads a sequence of elements until a terminator or end of buffer:
54+
```javascript
55+
// Read numbers until encountering 0
56+
['loop', { type: 'i8', nt: 0 }] // [1, 2, 3] from buffer [1, 2, 3, 0, ...]
57+
58+
// Read until end of buffer
59+
['loop', { type: 'i16', nt: null }] // Read all remaining i16 values
60+
```
61+
62+
### restBuffer
63+
Captures all remaining bytes as a Buffer:
64+
```javascript
65+
// Protocol with header and payload
66+
['container', [
67+
{ name: 'header', type: 'u8' },
68+
{ name: 'payload', type: 'restBuffer' } // All remaining bytes
69+
]]
70+
```
71+
72+
See [examples/extras_demo.js](examples/extras_demo.js) for comprehensive examples and [ProtoDef/doc/datatypes/extras.md](ProtoDef/doc/datatypes/extras.md) for detailed documentation.
73+
74+
**Note:** When using the new `loop` and `restBuffer` types, you may need to disable schema validation by creating your ProtoDef instance with `new ProtoDef(false)` until the external validator is updated to recognize these types.
75+

examples/extras.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Quick test and demonstration of the new loop and restBuffer types
3+
*/
4+
5+
const { ProtoDef } = require('../src/index')
6+
7+
const proto = new ProtoDef(false)
8+
9+
console.log('=== Testing loop and restBuffer types ===\n')
10+
11+
// Test 1: restBuffer - reads all remaining bytes
12+
console.log('1. Testing restBuffer:')
13+
const testBuffer1 = Buffer.from([1, 2, 3, 4, 5])
14+
const restResult = proto.read(testBuffer1, 2, 'restBuffer')
15+
console.log(' Buffer:', Array.from(testBuffer1))
16+
console.log(' Reading from offset 2:', restResult)
17+
console.log(' Remaining bytes:', Array.from(restResult.value))
18+
console.log()
19+
20+
// Test 2: loop without terminator - reads until EOF
21+
console.log('2. Testing loop (no terminator):')
22+
const loopResult1 = proto.read(Buffer.from([10, 20, 30]), 0, ['loop', { type: 'i8', nt: null }])
23+
console.log(' Buffer: [10, 20, 30]')
24+
console.log(' Result:', loopResult1)
25+
console.log(' Values:', loopResult1.value)
26+
console.log()
27+
28+
// Test 3: loop with terminator
29+
console.log('3. Testing loop (with terminator):')
30+
const loopResult2 = proto.read(Buffer.from([1, 2, 3, 0, 99]), 0, ['loop', { type: 'i8', nt: 0 }])
31+
console.log(' Buffer: [1, 2, 3, 0, 99] (0 is terminator)')
32+
console.log(' Result:', loopResult2)
33+
console.log(' Values:', loopResult2.value, '(99 not included)')
34+
console.log()
35+
36+
// Test 4: Writing with loop
37+
console.log('4. Testing loop writing:')
38+
const writeBuffer1 = proto.createPacketBuffer(['loop', { type: 'i8', nt: 0 }], [5, 10, 15])
39+
console.log(' Writing [5, 10, 15] with null terminator:')
40+
console.log(' Buffer:', Array.from(writeBuffer1))
41+
console.log()
42+
43+
// Test 5: Complex example with both types
44+
console.log('5. Complex example - message with header and payload:')
45+
proto.addType('message', ['container', [
46+
{ name: 'type', type: 'u8' },
47+
{ name: 'flags', type: 'u8' },
48+
{ name: 'data', type: 'restBuffer' }
49+
]])
50+
51+
const complexBuffer = Buffer.concat([
52+
Buffer.from([42, 0x80]), // type=42, flags=0x80
53+
Buffer.from('Hello World!') // payload
54+
])
55+
56+
const complexResult = proto.parsePacketBuffer('message', complexBuffer)
57+
console.log(' Message parsed:')
58+
console.log(' Type:', complexResult.data.type)
59+
console.log(' Flags:', '0x' + complexResult.data.flags.toString(16))
60+
console.log(' Data:', complexResult.data.data.toString())
61+
console.log()
62+
63+
console.log('✅ All tests completed successfully!')
64+
console.log('\nAvailable types:', Object.keys(proto.types).filter(t => ['loop', 'restBuffer'].includes(t)))

test_extras.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)