Giter VIP home page Giter VIP logo

form-data-encoder's People

Contributors

baune8d avatar curtdept avatar dependabot[bot] avatar github-actions[bot] avatar octet-stream avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

form-data-encoder's Issues

Minimum required Node.js version

In v2 the minimum required Node.js version is v14.18.0:

"engines": {
"node": ">= 14.18"
},

Is that necessary, or can it be lowered to v14.17.0? The reason I ask is because Iโ€™ve been rolling that out as the minimum for my packages and in particular would rather not increase this for the upcoming graphql-upload major version:

https://github.com/jaydenseric/graphql-upload/blob/6da4fb2cdf8e4610a251babb5632bf9b97ce9f71/package.json#L47-L49

I think a lot of packages in the ecosystem will settle on Node.js v14.17.0 as the minimum to support, including it looks like node-fetch v4: node-fetch/node-fetch#1452 .

`Content-Length` header is `NaN`

๐Ÿ’ป Terminal

npm init -y
npm install got form-data-encoder formdata-node

๐Ÿ“ file.txt

Hello, world!

๐Ÿ“ index.mjs

import { FormData } from 'formdata-node';
import { got } from 'got';
import { createServer } from 'http';
import { createReadStream } from 'fs';
import { FormDataEncoder } from 'form-data-encoder';
import { Readable } from 'stream';

const server = createServer((request, response) => {
  console.log('request headers', request.headers);
  request.pipe(response);
}).listen(0, async () => {
  const fd = new FormData();

  fd.set('file', {
    type: 'text/plain',
    name: 'hello.txt',
    [Symbol.toStringTag]: 'File',
    stream() {
      return createReadStream('file.txt');
    },
  });

  const encoder = new FormDataEncoder(fd);

  console.log('encoder headers', encoder.headers);

  try {
    const response = await got.post(
      'http://127.0.0.1:' + server.address().port,
      {
        body: Readable.from(encoder),
        timeout: {
          request: 1000,
        },
        retry: {
          limit: 0,
        },
        headers: encoder.headers,
      }
    );

    console.log(response.body);
  } catch (error) {
    console.error(error);
  }

  server.close();
});

๐Ÿ’ป Output

encoder headers {
  'Content-Type': 'multipart/form-data; boundary=form-data-boundary-3e3ywfsmy96fjzun',
  'Content-Length': 'NaN'
}
Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:160:15)
    at writeGeneric (node:internal/stream_base_commons:151:3)
    at Socket._writeGeneric (node:net:817:11)
    at Socket._write (node:net:829:8)
    at doWrite (node:internal/streams/writable:408:12)
    at clearBuffer (node:internal/streams/writable:569:7)
    at Socket.Writable.uncork (node:internal/streams/writable:348:7)
    at connectionCorkNT (node:_http_outgoing:797:8)
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

README outdated

Hey guys!!

The README tells to use the Encoder class but the implementation marks it as @deprecated and that we should use the FormDataEncoder class. Which I figured that the README might be outdated.

Which changes should be added in the README?

Part's headers should contain `Content-Length` header if size is known

Is your feature request related to a problem? Please describe.

Some server applications require to have known Content-Length for each part of the multipart request.

Describe the solution you'd like

Add Content-Length header in part's headers if the size is known (for example for Blob use the Blob.size) in the getHeader() function.

Describe alternatives you've considered

At the moment I have to use form-data package instead of the spec complaint FormData as it has support for this.

Additional context

Happy to create patch for this.

Discussion about part's Content-Length in multipart request: square/okhttp#2138

Lint error in LowercaseObjectKeys.d.t

Hi, I got a lint error in the types definition.

./node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:19 - error TS1005: ']' expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                    ~~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:22 - error TS1005: ';' expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                       ~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:24 - error TS1005: ';' expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                         ~~~~~~~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:54 - error TS1005: '(' expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                                                       ~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:57 - error TS1005: ',' expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                                                          ~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:58 - error TS1135: Argument expression expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                                                           ~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:3:64 - error TS1005: ')' expected.

3     [K in keyof T as K extends string ? Lowercase<K> : K]: T[K];
                                                                 ~

../node_modules/form-data-encoder/@type/util/LowercaseObjectKeys.d.ts:4:1 - error TS1128: Declaration or statement expected.

4 };
  ~


Found 8 errors.

tsc version = Version 4.7.4

Type error using Node.js global `FormData` instance with `FormDataEncoder` constructor

This part of the type FormDataLike:

readonly [Symbol.toStringTag]: string

Is causing this error when you attempt to use a Node.js global FormData instance with the FormDataEncoder constructor:

Argument of type 'FormData' is not assignable to parameter of type 'FormDataLike'.
Property '[Symbol.toStringTag]' is missing in type 'FormData' but required in type 'FormDataLike'.

Screenshot 2023-08-10 at 12 40 17 pm

`formdata-node` `FormData` is not assignable to `new FormDataEncoder(formData)`

Hey guys!

I'm using Typescript and Node v12.

Using the following example written in the jsDocs notations give me an compilation error

import {FormDataEncoder} from "form-data-encoder" // 1.5.0
import {FormData} from "formdata-node" // 4.0.1

const fd = new FormData()

fd.set("greeting", "Hello, World!")

const encoder = new FormDataEncoder(fd) // compilation error
/*
Argument of type 'FormData' is not assignable to parameter of type 'FormDataLike'.
  The types returned by 'getAll(...)' are incompatible between these types.
    Type 'FormDataFieldValue[]' is not assignable to type 'FormDataEntryValue[]'.
      Type 'FormDataFieldValue' is not assignable to type 'FormDataEntryValue'.
        Type 'File' is not assignable to type 'FormDataEntryValue'.
          Type 'File' is missing the following properties from type 'FileLike': type, size, stream, [Symbol.toStringTag]
*/

Feels like the formdata-node File implementation is not compliant to the this lib FileLike.

I didn't investigated much... but I'm stuck here :/

Appending files is not working after `[email protected]`

Hello! I'm getting a weird error when trying to add files to a form.

I've created a library similar to supertest called tepper, and the other day the bot tried to upgrade formdata-node from 4.3.2 to 4.4.1. The CI of the PR was failing, and I've been trying to narrow up the error.

Finally, I've managed to reproduce the error in this separate repository: DanielRamosAcosta/form-data-issue. The CI is passing on the main branch, but it is failing after upgrade, as you can see in this PR.

Output at 4.3.2:

--form-data-boundary-xxxxxxxxxxxxxxxx
Content-Disposition: form-data; name="stream"; filename="file.txt"
Content-Type: text/plain

Stream content
--form-data-boundary-xxxxxxxxxxxxxxxx--

Output at 4.4.1

--form-data-boundary-xxxxxxxxxxxxxxxx
Content-Disposition: form-data; name="stream"

[object File]
--form-data-boundary-xxxxxxxxxxxxxxxx--

There seems to be a problem with the File detection at 4.4.1! No more time today, but I could give a try reproducing the error at this repo and providing a fix.

Support for `multipart/mixed` and other multipart content types

Could this library theoretically support multipart/mixed and related content types defined in https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa493937(v=exchg.140)?

According to RFC1341, all 'multipart/* content types have identical syntax, they just differ in semantics. Basically all multipart/* content types are identical with multipart/form-data except that within the single boundary, the Content-Disposition header must be omitted.

error TS2304: Cannot find name 'ReadableStream'

Compiling [email protected] using TypeScript 5.3.3 on Node.js 20.x, I get the following error:

node_modules/form-data-encoder/lib/index.d.ts:30:15 - error TS2304: Cannot find name 'ReadableStream'.

30     stream(): ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>;
                 ~~~~~~~~~~~~~~

My application is not using this package directly. Instead, it is referred from Got 14.0.0.

`FormDataLike` iterator type is incorrect

The iterator type for FormDataLike is incorrect / too restrictive (

[Symbol.iterator](): Generator<[string, FormDataEntryValue]>
). It uses Generator which makes methods throw and return required. But based on the spec, those two are optional for iterators (see here in MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#throwexception) which are linked to in the source of FormDataLike itself.

I believe the types in undici are more accurate (https://github.com/nodejs/undici/blob/8050ec0224a51d44f776364820e6a16112fb4781/types/formdata.d.ts#L105 and https://github.com/nodejs/undici/blob/8050ec0224a51d44f776364820e6a16112fb4781/types/fetch.d.ts#L41-L47).

This is actually how I found this issue. I was trying to use the FormData object from Undici with the FormDataEncoder from this library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.