{"version":3,"file":"index.mjs","names":["#emit","#decoder","#pending"],"sources":["../src/linewise.ts","../src/index.ts"],"sourcesContent":["import { Transform } from \"node:stream\";\nimport type { TransformCallback } from \"node:stream\";\nimport { StringDecoder } from \"node:string_decoder\";\n\nexport default class LinewiseStream extends Transform {\n  #decoder = new StringDecoder(\"utf8\");\n  #pending = \"\";\n\n  override _transform(chunk: Buffer, _encoding: BufferEncoding, callback: TransformCallback): void {\n    this.#emit(this.#decoder.write(chunk));\n    callback();\n  }\n\n  override _flush(callback: TransformCallback): void {\n    this.#emit(this.#decoder.end());\n    if (this.#pending) this.push(this.#pending);\n    callback();\n  }\n\n  #emit(text: string) {\n    this.#pending += text;\n    const lines = this.#pending.split(\"\\n\");\n    this.#pending = lines.pop() ?? \"\";\n    for (const line of lines) this.push(line);\n  }\n}\n","import { Writable } from \"node:stream\";\nimport util from \"node:util\";\n\nimport debugModule from \"debug\";\n\nimport LinewiseStream from \"./linewise\";\n\nexport default class Parser extends Writable {\n  static debug: ReturnType<typeof debugModule> = debugModule(\"slate-irc-parser\");\n  private readonly nlstream: LinewiseStream;\n\n  /**\n   * Initialize IRC parser.\n   */\n  constructor() {\n    super();\n    this.nlstream = new LinewiseStream();\n    this.nlstream.setEncoding(\"utf8\");\n    this.nlstream.on(\"data\", this.online.bind(this));\n    this.nlstream.resume();\n  }\n\n  /**\n   * Write `chunk`.\n   *\n   * @param {Buffer} chunk\n   */\n  override write(chunk: Buffer): boolean {\n    return this.nlstream.write(chunk);\n  }\n\n  /**\n   * Parse lines and emit \"message\" events.\n   *\n   * @param {String} line\n   */\n  private online(line: string): void {\n    // Remove a single CR at the end of the line if it does exist\n    line = line.replace(/\\r$/, \"\");\n\n    Parser.debug(\"line %s\", util.inspect(line));\n    const orig = line;\n\n    // prefix\n    let prefix;\n    if (\":\" == line[0]) {\n      const i = line.indexOf(\" \");\n      prefix = line.slice(1, i);\n      line = line.slice(i + 1);\n    }\n\n    // command\n    let i = line.indexOf(\" \");\n    if (-1 == i) i = line.length;\n    const command = line.slice(0, i);\n    line = line.slice(i);\n\n    // params\n    i = line.indexOf(\" :\");\n    if (-1 == i) i = line.length;\n    const params = line.slice(1, i);\n    line = line.slice(i + 2);\n\n    const msg = {\n      prefix: prefix || \"\",\n      command: command,\n      params: params || \"\",\n      trailing: line || \"\",\n      string: orig,\n    };\n\n    Parser.debug(\"message %j\", msg);\n    this.emit(\"message\", msg);\n  }\n\n  /**\n   * Emit \"end\".\n   */\n  override end(): this {\n    this.emit(\"end\");\n    return this;\n  }\n}\n"],"mappings":"wJAIA,IAAqB,EAArB,cAA4C,CAAU,CACpD,GAAW,IAAI,EAAc,OAAO,CACpC,GAAW,GAEX,WAAoB,EAAe,EAA2B,EAAmC,CAC/F,MAAA,EAAW,MAAA,EAAc,MAAM,EAAM,CAAC,CACtC,GAAU,CAGZ,OAAgB,EAAmC,CACjD,MAAA,EAAW,MAAA,EAAc,KAAK,CAAC,CAC3B,MAAA,GAAe,KAAK,KAAK,MAAA,EAAc,CAC3C,GAAU,CAGZ,GAAM,EAAc,CAClB,MAAA,GAAiB,EACjB,IAAM,EAAQ,MAAA,EAAc,MAAM;EAAK,CACvC,MAAA,EAAgB,EAAM,KAAK,EAAI,GAC/B,IAAK,IAAM,KAAQ,EAAO,KAAK,KAAK,EAAK,GChBxB,EAArB,MAAqB,UAAe,CAAS,CAC3C,OAAO,MAAwC,EAAY,mBAAmB,CAC9E,SAKA,aAAc,CACZ,OAAO,CACP,KAAK,SAAW,IAAI,EACpB,KAAK,SAAS,YAAY,OAAO,CACjC,KAAK,SAAS,GAAG,OAAQ,KAAK,OAAO,KAAK,KAAK,CAAC,CAChD,KAAK,SAAS,QAAQ,CAQxB,MAAe,EAAwB,CACrC,OAAO,KAAK,SAAS,MAAM,EAAM,CAQnC,OAAe,EAAoB,CAEjC,EAAO,EAAK,QAAQ,MAAO,GAAG,CAE9B,EAAO,MAAM,UAAW,EAAK,QAAQ,EAAK,CAAC,CAC3C,IAAM,EAAO,EAG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