octet-stream / form-data-encoder Goto Github PK
View Code? Open in Web Editor NEWEncode FormData content into the multipart/form-data format
Home Page: https://www.npmjs.com/package/form-data-encoder
License: MIT License
Encode FormData content into the multipart/form-data format
Home Page: https://www.npmjs.com/package/form-data-encoder
License: MIT License
In v2 the minimum required Node.js version is v14.18.0:
form-data-encoder/package.json
Lines 8 to 10 in 4975e93
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:
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 .
๐ป 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'
}
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?
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
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
This part of the type FormDataLike
:
form-data-encoder/src/FormDataLike.ts
Line 45 in f761231
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'.
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 :/
isFormDataLike
is no longer an exported function in v1.7.0. Please try to follow semver.
The following change is a breaking change:
https://github.com/octet-stream/form-data-encoder/blame/v1.8.0/lib/FormDataEncoder.ts#L77
It is possible that some other changes brought by this minor update are also breaking changes.
Hi,
I recently used your library on my web platform, but i've encountered an error for my Apple users, where the Lookbehind used on the regexp
is not currently supported by Apple devices.
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.
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.
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.
The iterator type for FormDataLike is incorrect / too restrictive (
form-data-encoder/src/FormDataLike.ts
Line 43 in 8c8085d
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.