JSON.cjs 739 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var node_stream = require('node:stream');
  4. /* eslint-disable no-underscore-dangle */
  5. class JSONParser extends node_stream.Transform {
  6. constructor(options = {}) {
  7. super({ readableObjectMode: true });
  8. this.chunks = [];
  9. this.globalOptions = { ...options };
  10. }
  11. _transform(chunk, encoding, callback) {
  12. this.chunks.push(String(chunk)); // todo consider using a string decoder
  13. callback();
  14. }
  15. _flush(callback) {
  16. try {
  17. const fields = JSON.parse(this.chunks.join(''));
  18. this.push(fields);
  19. } catch (e) {
  20. callback(e);
  21. return;
  22. }
  23. this.chunks = null;
  24. callback();
  25. }
  26. }
  27. exports.default = JSONParser;