Vercel Blob CSV file stream to CSVTOJSON

28 views Asked by At

Building an app in NextJS.

Using FilePond to upload CSV to an API route.

I have saved my uploaded CSV file to Vercel Blob storage.

Now I want to stream it into CSVTOJSON.

Error: "readStream.pipe is not a function"

Tried creating a stream - see note code.

I cant figure out how to create the stream. Its all new to me.

TIA

import { put } from '@vercel/blob'
const csv = require("csvtojson/v2");
import { customAlphabet } from 'nanoid'

const nanoid = customAlphabet(
    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
    7
)

/*
Experiments ....
const createStream = async (url)=>{
    return await fetch(url)
    // Retrieve its body as ReadableStream
    .then((response) => {
      const reader = response.body.getReader();
      return new ReadableStream({
        start(controller) {
          return pump();
          function pump() {
            return reader.read().then(({ done, value }) => {
              // When no more data needs to be consumed, close the stream
              if (done) {
                controller.close();
                return;
              }
              // Enqueue the next data chunk into our target stream
              controller.enqueue(value);
              return pump();
            });
          }
        },
      });
    })
    // Create a new response out of the stream
    .then((stream) => {
        return new Response(stream)
    })
}

*/

export default async function POST(req, res) {

    const contentType = 'text/csv'
    const filename = `${nanoid()}.${contentType.split('/')[1]}`

    const blob = await put(filename, req.body, {
        contentType,
        access: 'public',
    })

    const jsonArray = await csv().fromStream(await fetch(blob.url))

....

}

To create a stream.

0

There are 0 answers