0

This script is how I always uploaded files to server, but I would like to remove dependecy on node-fetch library and it has confilicting types of streams.

import {createReadStream} from 'fs';


declare const url: string;
declare cosnt file: {
  size: number;
  filePath: string;
};

await fetch(url, {
  method: 'post',
  headers: {
    "Content-length": `${file.size}`,
  },
  body: createReadStream(file.filepath), // These streams are not compatible
});

How to transform native Node stream to native Web stream used by native fetch in Node v18 (and above)?

Akxe
  • 9,694
  • 3
  • 36
  • 71
  • How would I transform the `fs` `ReadStream` to the native `ReadableStream`? Simple `new ReadableStream(createReadStream(file.filepath))` will not work... – Akxe May 10 '23 at 09:49
  • You could try `Readable.toWeb()`, see [this](https://exploringjs.com/nodejs-shell-scripting/ch_web-streams.html#example-reading-a-file-via-a-readablestream) for more info – Fraction May 10 '23 at 12:14

0 Answers0