Digamos que tengo un ciclo simple como este:
for (const i=0;i<3;i++) { to(`This counter is ${i}`) }Quiero tener en mi archivo al final:
Intenté hacer eso haciendo esto:
export class S3Output extends Output { #stream: Readable | null = null async to(s: string): Promise<void> { const params = { Bucket: config.aws.bucketName, Body: this.#stream, Key: 'test.json' } this.#stream?.pipe(process.stdout) this.#stream?.push(s) await S3.upload(params).promise() return } init(): void { this.#stream = new Readable() this.#stream._read = function () {}; } finish(): void { this.#stream?.push(null) return } }Mi función init se llama al comienzo del ciclo, mi función to se llama cada vez que quiero insertar una cadena en el archivo y la función de finalización al final del ciclo. Este código no envía ningún dato, ¿por qué?
De hecho, encontré cuál era el problema. Tuve que enviar la transmisión una vez que terminó y no mientras lo estaba haciendo. Tampoco hay necesidad de tubería.
export class S3Output extends Output { #stream: Readable | null = null async to(s: string): Promise<void> { this.#stream?.push(s) return } init(): void { this.#stream = new Readable() this.#stream._read = function () {} } async finish(): Promise<void> { this.#stream?.push(null) const params = { Bucket: config.aws.bucketName, Body: this.#stream, Key: 'test.json' } await S3.upload(params).promise() return } }