Advertisement
Guest User

Untitled

a guest
May 24th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const fs = require('fs');
  4. const Pbf = require('pbf')
  5. const csv = require('./csv')
  6.  
  7.  
  8. const kLinePbfFile = __dirname + '/line.pbf';
  9. const kLineCsvFile = __dirname + '/line.csv';
  10.  
  11. const kLineToWrite = { node_from: 1, node_to: 2, speed: 3 };
  12.  
  13.  
  14. function main() {
  15.  
  16. spawn(function* (resume) {
  17. const outPbf = new Pbf();
  18. csv.Line.write(kLineToWrite, outPbf);
  19. const outBuf = outPbf.finish();
  20. yield fs.writeFile(kLinePbfFile, outBuf, resume);
  21.  
  22. const inBuf = yield fs.readFile(kLinePbfFile, resume);
  23. const inPbf = new Pbf(inBuf);
  24.  
  25. const roundtrip = csv.Line.read(inPbf);
  26. console.log(roundtrip);
  27. });
  28.  
  29. spawn(function* (resume) {
  30. const pbf = new Pbf(new Buffer(0));
  31. pbf.writeVarint(42);
  32. const buf = pbf.finish();
  33. console.log(buf);
  34.  
  35. const rbuf = new Buffer([0x2a, 0xff, 0xff, 0xff, 0xff]);
  36. const rpbf = new Pbf(rbuf);
  37. const len = rpbf.readVarint();
  38. console.log(len);
  39. });
  40.  
  41. }
  42.  
  43.  
  44. function spawn(generator) {
  45. let it = generator(function (err, data) {
  46. if (err) it.throw(err);
  47. return it.next(data);
  48. });
  49. it.next();
  50. }
  51.  
  52. if (require.main === module) { main(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement