Firebase函数Node.js转换流

问题描述

我正在创建一个Firebase HTTP函数,该函数进行BigQuery查询并返回查询结果的修改版本。该查询可能返回数百万行,因此在响应HTTP客户端之前,我无法将整个查询结果存储在内存中。我试图使用Node.js流,并且由于需要在将结果发送到客户端之前修改结果,因此我试图使用转换流。但是,当我尝试通过转换流传输查询流时,Firebase函数崩溃并显示以下错误消息:finished with status: 'response error'

我的最小可重复示例如下。我正在使用缓冲区,因为我不想一次处理一行(块),因为我需要进行异步网络调用来转换数据。

return new Promise((resolve,reject) => {
    const buffer = new Array(5000)
    let bufferIndex = 0
    const [job] = await bigQuery.createQueryJob(options)
    const bqStream = job.getQueryResultsStream()

    const transformer = new Transform({
        writableObjectMode: true,readableObjectMode: false,transform(chunk,enc,callback) {
            buffer[bufferIndex] = chunk
            if (bufferIndex < buffer.length - 1) {
                bufferIndex++
            }
            else {
                this.push(JSON.stringify(buffer).slice(1,-1)) // Transformation should happen here.
                bufferIndex = 0
            }
            callback()
        },flush(callback) {
            if (bufferIndex > 0) {
                this.push(JSON.stringify(buffer.slice(0,bufferIndex)).slice(1,-1))
            }
            this.push("]")
            callback()
        },})

    bqStream
        .pipe(transform)
        .pipe(response)

    bqStream.on("end",() => {
        resolve()
    })
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)