octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

This will throw error when send two request #54

Closed Val-istar-Guo closed 2 years ago

Val-istar-Guo commented 2 years ago

https://github.com/octet-stream/form-data/blob/b925ce9c541f11a6be1b87cfc159334a3a573087/lib/isFile.ts#L8

import fetch from 'node-fetch'
import { FormData, Blob } from "formdata-node"

const form = new FormData()
const blob = new Blob(["Some content"], {type: "text/plain"})

form.set("blob", blob)

const encoder = new FormDataEncoder(form)

const options = {
  method: "post",
  headers: encoder.headers,
  body: Readable.from(encoder)
}

const res = await fetch("https://httpbin.org/post", options)

const imgBlob = res.blob()

const nextForm = new FormData()
nextForm.set("nextRequestBlob", imgBlob)
const nextEncoder = new FormDataEncoder(nextForm)
const nextOptions = {
  method: "post",
  headers: nextEncoder.headers,
  body: Readable.from(nextEncoder)
}

// will error
await fetch("https://httpbin.org/post", nextOptions)

The way to fix the problem is not cumbersome, but not elegant

const imgBuf = res.buffer()

const nextForm = new FormData()
nextForm.set("nextRequestBlob", imgBuf)

The reason of the problem is due to the fact that node-fetch also provides a Blob implementation:

https://github.com/node-fetch/node-fetch/blob/2.x/src/blob.js

Are there plans to fix this?

Val-istar-Guo commented 2 years ago

I have the error of using