big-token.js 708 B

123456789101112131415161718192021222324
  1. var stream = require('stream');
  2. var JsonParse = require('../jsonparse');
  3. var test = require('tape');
  4. test('can handle large tokens without running out of memory', function (t) {
  5. var parser = new JsonParse();
  6. var chunkSize = 1024;
  7. var chunks = 1024 * 200; // 200mb
  8. var quote = Buffer.from ? Buffer.from('"') : new Buffer('"');
  9. t.plan(1);
  10. parser.onToken = function (type, value) {
  11. t.equal(value.length, chunkSize * chunks, 'token should be size of input json');
  12. t.end();
  13. };
  14. parser.write(quote);
  15. for (var i = 0; i < chunks; ++i) {
  16. var buf = Buffer.alloc ? Buffer.alloc(chunkSize) : new Buffer(chunkSize);
  17. buf.fill('a');
  18. parser.write(buf);
  19. }
  20. parser.write(quote);
  21. });