index.js 3.3 KB

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