Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ Create a parser of `mainType` defined in `proto`. This is a Transform stream.

Returns a parsed packet of `buffer`.

## FullPacketParser(proto,mainType,noErrorLogging)

Create a parser of `mainType` defined in `proto` that reads one whole packet per chunk. This is a Transform stream.

A chunk the definitions cannot read is dropped and the stream continues. Its stack is logged unless `noErrorLogging` is set.

### FullPacketParser.parsePacketBuffer(buffer)

Returns a parsed packet of `buffer`.

### Event: 'partialReadError' (error)

Emitted for each dropped chunk, before it is logged. `error.buffer` is the chunk.

## types

An object mapping the default type names to the corresponding `[read,write,sizeOf]` functions.
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ declare class CompiledProtoDef extends AbstractProtoDefInterface {

declare class ProtodefPartialError extends Error {
partialReadError: true
// The chunk a FullPacketParser could not read; set when it emits 'partialReadError'.
buffer?: Buffer
constructor(message?: string)
}

Expand All @@ -134,6 +136,8 @@ declare module 'protodef' {
noErrorLogging: boolean
constructor(proto: ProtoDef, mainType: string, noErrorLogging = false)
parsePacketBuffer(packet: any): Buffer
on(event: 'partialReadError', listener: (error: ProtodefPartialError) => void): this
on(event: string | symbol, listener: (...args: any[]) => void): this
}
export const Compiler: {
ReadCompiler: typeof ProtodefReadCompiler
Expand Down
2 changes: 2 additions & 0 deletions src/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class FullPacketParser extends Transform {
}
} catch (e) {
if (e.partialReadError) {
e.buffer = chunk
this.emit('partialReadError', e)
if (!this.noErrorLogging) {
console.log(e.stack)
}
Expand Down
28 changes: 27 additions & 1 deletion test/misc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */

const assert = require('assert')
const { ProtoDef } = require('../')
const { ProtoDef, FullPacketParser } = require('../')
const { ProtoDefCompiler } = require('../').Compiler

it('example works', () => {
Expand All @@ -25,3 +25,29 @@ describe('mapper', () => {
})
}
})

describe('FullPacketParser', () => {
const packet = ['container', [{ name: 'a', type: 'i32' }]]
const proto = new ProtoDef()
proto.addType('packet', packet)
const compiler = new ProtoDefCompiler()
compiler.addTypesToCompile({ packet })
const compiled = compiler.compileProtoDefSync()

for (const [label, p] of [['interpreted', proto], ['compiled', compiled]]) {
it(`emits partialReadError with the chunk it could not read, and keeps parsing (${label})`, async () => {
const parser = new FullPacketParser(p, 'packet', true)
const errors = []
const packets = []
parser.on('partialReadError', e => errors.push(e))
parser.on('data', d => packets.push(d.data))
parser.write(Buffer.from([0, 0]))
parser.write(Buffer.from([0, 0, 0, 7]))
await new Promise(resolve => parser.end(resolve))
assert.strictEqual(errors.length, 1)
assert.strictEqual(errors[0].partialReadError, true)
assert.deepStrictEqual(errors[0].buffer, Buffer.from([0, 0]))
assert.deepStrictEqual(packets, [{ a: 7 }])
})
}
})
Loading