Guest User

Untitled

a guest
Oct 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. const msgpack = {
  2. append(dest, src) {
  3. return Buffer.concat([dest, src])
  4. },
  5.  
  6. encode(obj) {
  7. if (typeof obj === 'number') {
  8. let buf = Buffer.alloc(9)
  9. buf.writeUInt8(0xce, 0)
  10. buf.writeUInt32BE(obj, 1)
  11. return buf
  12. } else if (typeof obj === 'string') {
  13. const str = Buffer.from(obj)
  14. let buf = Buffer.alloc(5 + obj.length)
  15. buf.writeUInt8(0xdb, 0)
  16. buf.writeUInt32BE(obj.length, 1)
  17. str.copy(buf, 5)
  18. return buf
  19. }
  20.  
  21. throw new Error(`msgpack: unsupported object: ${obj}`)
  22. },
  23.  
  24. pack(obj) {
  25. console.log(obj)
  26. let payload = Buffer.from([0x80 | Object.keys(obj).length])
  27.  
  28. // We cannot use Object.keys() because
  29. // `k' must be a Number.
  30. for (const k in obj) {
  31. if (k > 0x7f) {
  32. throw new Error(`msgpack: too large key`)
  33. }
  34.  
  35. payload = this.append(payload, Buffer.from([k]))
  36. payload = this.append(payload, this.encode(obj[k]))
  37. console.log(k)
  38. }
  39. },
  40.  
  41. unpack(obj) {
  42. console.log(obj)
  43. }
  44. }
Add Comment
Please, Sign In to add comment