| 424 | * @deprecated Use {@link createEngineStreamV2} instead. |
| 425 | */ |
| 426 | export function createEngineStream(opts: EngineStreamOptions): Duplex { |
| 427 | if (!opts || !opts.engine) { |
| 428 | throw new Error("Missing engine parameter!"); |
| 429 | } |
| 430 | |
| 431 | const { engine } = opts; |
| 432 | // eslint-disable-next-line prefer-const |
| 433 | let stream: Duplex; |
| 434 | |
| 435 | function read(): undefined { |
| 436 | return undefined; |
| 437 | } |
| 438 | |
| 439 | function write(req: JRPCRequest, _encoding: unknown, cb: (error?: Error | null) => void) { |
| 440 | engine.handle(req, (_err, res) => { |
| 441 | stream.push(res); |
| 442 | }); |
| 443 | cb(); |
| 444 | } |
| 445 | |
| 446 | stream = new Duplex({ objectMode: true, read, write }); |
| 447 | |
| 448 | // forward notifications |
| 449 | if (engine.on) { |
| 450 | const onNotification = (message: unknown) => { |
| 451 | stream.push(message); |
| 452 | }; |
| 453 | |
| 454 | // cleanup listener on stream close |
| 455 | const cleanup = () => { |
| 456 | engine.removeListener("notification", onNotification); |
| 457 | }; |
| 458 | |
| 459 | engine.on("notification", onNotification); |
| 460 | stream.once("close", cleanup); |
| 461 | } |
| 462 | return stream; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @deprecated Use {@link providerFromEngineV2} instead. |