index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getEncoding = exports.DecodeStream = void 0;
  7. exports.decodeBuffer = decodeBuffer;
  8. const node_stream_1 = require("node:stream");
  9. const iconv_lite_1 = __importDefault(require("iconv-lite"));
  10. const sniffer_js_1 = require("./sniffer.js");
  11. /**
  12. * Sniff the encoding of a buffer, then decode it.
  13. *
  14. * @param buffer Buffer to be decoded
  15. * @param options Options for the sniffer
  16. * @returns The decoded buffer
  17. */
  18. function decodeBuffer(buffer, options = {}) {
  19. return iconv_lite_1.default.decode(buffer, (0, sniffer_js_1.getEncoding)(buffer, options));
  20. }
  21. /**
  22. * Decodes a stream of buffers into a stream of strings.
  23. *
  24. * Reads the first 1024 bytes and passes them to the sniffer. Once an encoding
  25. * has been determined, it passes all data to iconv-lite's stream and outputs
  26. * the results.
  27. */
  28. class DecodeStream extends node_stream_1.Transform {
  29. constructor(options) {
  30. var _a;
  31. super({ decodeStrings: false, encoding: "utf-8" });
  32. this.buffers = [];
  33. /** The iconv decode stream. If it is set, we have read more than `options.maxBytes` bytes. */
  34. this.iconv = null;
  35. this.readBytes = 0;
  36. this.sniffer = new sniffer_js_1.Sniffer(options);
  37. this.maxBytes = (_a = options === null || options === void 0 ? void 0 : options.maxBytes) !== null && _a !== void 0 ? _a : 1024;
  38. }
  39. _transform(chunk, _encoding, callback) {
  40. if (this.readBytes < this.maxBytes) {
  41. this.sniffer.write(chunk);
  42. this.readBytes += chunk.length;
  43. if (this.readBytes < this.maxBytes) {
  44. this.buffers.push(chunk);
  45. callback();
  46. return;
  47. }
  48. }
  49. this.getIconvStream().write(chunk, callback);
  50. }
  51. getIconvStream() {
  52. if (this.iconv) {
  53. return this.iconv;
  54. }
  55. const stream = iconv_lite_1.default.decodeStream(this.sniffer.encoding);
  56. stream.on("data", (chunk) => this.push(chunk, "utf-8"));
  57. stream.on("end", () => this.push(null));
  58. this.iconv = stream;
  59. for (const buffer of this.buffers) {
  60. stream.write(buffer);
  61. }
  62. this.buffers.length = 0;
  63. return stream;
  64. }
  65. _flush(callback) {
  66. this.getIconvStream().end(callback);
  67. }
  68. }
  69. exports.DecodeStream = DecodeStream;
  70. var sniffer_js_2 = require("./sniffer.js");
  71. Object.defineProperty(exports, "getEncoding", { enumerable: true, get: function () { return sniffer_js_2.getEncoding; } });
  72. //# sourceMappingURL=index.js.map