|
| 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))) |
0 commit comments