index.js 3.9 KB

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