All files / common serializer.js

73.91% Statements 17/23
50% Branches 5/10
100% Functions 6/6
88.89% Lines 16/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 422x     2x 2x                   2x       2x 2x 2x 2x 2x         2x       2x 2x 2x 2x 2x       2x    
((that, register) => {
  'use strict'
 
  Eif (typeof (exports) === 'object') {
    register(module.exports)
  } else {
    that.PapanProto = {}
    register(that.PapanProto)
  }
})(global, that => {
  'use strict'
 
  class Serializer {
    constructor (proto) {
      this.proto = proto
    }
 
    deserialize (event, data) {
      const MessageType = this.proto.lookupType(event)
      Iif (!MessageType) throw Error('Message type ' + event + ' not found')
      const message = MessageType.decode(data)
      Iif (!message) throw Error('Unable to decode message ' + event)
      const obj = MessageType.toObject(message, {
        enums: String,
        defaults: true,
        oneofs: true
      })
      return obj
    }
 
    serialize (event, message) {
      const MessageType = this.proto.lookupType(event)
      Iif (!MessageType) throw Error('Message type ' + event + ' not found')
      const obj = MessageType.fromObject(message)
      Iif (!obj) throw Error('Unable to create message ' + event)
      return MessageType.encode(obj).finish()
    }
  }
 
  that.createSerializer = proto => new Serializer(proto)
})