Skip to content

How to combine stream with async await? #1787

@phen0menon

Description

@phen0menon

I'm looking for a way to combine WritableStream with async await. I saw some old issues in this repository related to this problem and didn't find any relevant solution.

What am I trying to do:

  1. Get file stream from S3
  2. Parse file with streams using htmlparser2
  3. During parsing, insert chunks of data to Kafka

Example of code is following:

const readable = getFileStreamFromS3()

let buffer = []

const parser = new WStream(
    {
        onopentag(name, attributes) {
            // ... collect data on open tag
        },

        ontext(text) {
            // collect text on text
        },

        async onclosetag(tagname) {
            buffer.push(data)
            if (buffer.length >= 100) {
                await pushToKafka(buffer)
                buffer = []
            }
        },
    },
    { xmlMode: true },
)

return new Promise(resolve => readable.pipe(writableStream).on('finish', resolve))

However, it doesn't seem to work. Any ideas on how to implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions