index.js 2.1 KB

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