Files
NapCatQQ/.pnpm-store/v3/files/74/1371a3992711f29046c106502caa00522a923eb3c4e51889f4692e611150b87313329b88608608c8602b076526fa193b9d9e72b2bae4a4b90d7ca090dd8cf5
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

44 lines
1.3 KiB
Plaintext

import { Parser, type Handler, type ParserOptions } from "./Parser.js";
/*
* NOTE: If either of these two imports produces a type error,
* please update your @types/node dependency!
*/
import { Writable } from "node:stream";
import { StringDecoder } from "node:string_decoder";
// Following the example in https://nodejs.org/api/stream.html#stream_decoding_buffers_in_a_writable_stream
function isBuffer(_chunk: string | Buffer, encoding: string): _chunk is Buffer {
return encoding === "buffer";
}
/**
* WritableStream makes the `Parser` interface available as a NodeJS stream.
*
* @see Parser
*/
export class WritableStream extends Writable {
private readonly _parser: Parser;
private readonly _decoder = new StringDecoder();
constructor(cbs: Partial<Handler>, options?: ParserOptions) {
super({ decodeStrings: false });
this._parser = new Parser(cbs, options);
}
override _write(
chunk: string | Buffer,
encoding: string,
callback: () => void,
): void {
this._parser.write(
isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk,
);
callback();
}
override _final(callback: () => void): void {
this._parser.end(this._decoder.end());
callback();
}
}