Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

formdata-polyfill

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

formdata-polyfill - npm Package Compare versions

Comparing version 4.0.3 to 4.0.4

24

esm.min.js

@@ -1,2 +0,2 @@

import B from 'fetch-blob'
import C from 'fetch-blob'
import F from 'fetch-blob/file.js'

@@ -6,3 +6,4 @@

m='append,set,get,getAll,delete,keys,values,entries,forEach,constructor'.split(','),
f=(a,b,c)=>(a+='',/^(Blob|File)$/.test(b?.[t])?[(c=c!==void 0?c+'':b[t]=='File'?b.name:'blob',a),b.name!==c||b[t]=='blob'?new F([b],c):b]:[a,b+''])
f=(a,b,c)=>(a+='',/^(Blob|File)$/.test(b?.[t])?[(c=c!==void 0?c+'':b[t]=='File'?b.name:'blob',a),b.name!==c||b[t]=='blob'?new F([b],c):b]:[a,b+'']),
e=c=>c.replace(/\n/g,'%0A').replace(/\r/g,'%0D').replace(/"/g,'%22')

@@ -24,13 +25,10 @@ export const File = F

*keys(){for(var[a]of this)yield a}
*values(){for(var[,a]of this)yield a}
}
*values(){for(var[,a]of this)yield a}}
export function formDataToBlob(formData){
var b='----formdata-'+Math.random(),c=[],d=`--${b}\r\nContent-Disposition: form-data; name="`
formData.forEach((v,n)=>typeof v==='string'
?c.push(`${d}${n}"\r\n\r\n${v}\r\n--${b}--`)
:c.push(`${d}${n}"; filename="${v.name}"\r\nContent-Type: ${v.type || 'application/octet-stream'}\r\n\r\n`,v,`\r\n--${b}--`))
return new B(c,{type:'multipart/form-data; boundary='+b})
}
export function formDataToBlob (F,B=C){
var b='----formdata-'+Math.random(),c=[],p=`--${b}\r\nContent-Disposition: form-data; name="`
F.forEach((v,n)=>typeof v=='string'
?c.push(p+e(n)+`"\r\n\r\n${v}\r\n`)
:c.push(p+e(n)+`"; filename="${e(v.name)}"\r\nContent-Type: ${v.type||"application/octet-stream"}\r\n\r\n`, v, '\r\n'))
c.push(`--${b}--`)
return new B(c,{type:"multipart/form-data; boundary="+b})}

@@ -0,1 +1,3 @@

const escape = str => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22')
/**

@@ -9,18 +11,21 @@ * pure function to convert any formData instance to a Blob

export function formDataToBlob (formData, BlobClass = Blob) {
const boundary = '----formdata-' + Math.random()
const boundary = ('----formdata-polyfill-' + Math.random())
const chunks = []
const prefix = `--${boundary}\r\nContent-Disposition: form-data; name="`
for (const [name, value] of formData) {
chunks.push(`--${boundary}\r\nContent-Disposition: form-data; name="${name}"`)
typeof value === 'string'
? chunks.push(`\r\n\r\n${value}\r\n--${boundary}--`)
: chunks.push(
`; filename="${value.name}"\r\n` +
for (let [name, value] of formData) {
if (typeof value === 'string') {
chunks.push(prefix + escape(name) + `"\r\n\r\n${value}\r\n`)
} else {
chunks.push(
prefix + escape(name) + `"; filename="${escape(value.name)}"\r\n` +
`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`,
value,
`\r\n--${boundary}--`
'\r\n'
)
}
}
chunks.push(`--${boundary}--`)
return new BlobClass(chunks, {

@@ -27,0 +32,0 @@ type: 'multipart/form-data; boundary=' + boundary

@@ -50,5 +50,2 @@ /* global FormData self Blob File */

},
lastModifiedDate: {
value: t
},
lastModified: {

@@ -108,2 +105,4 @@ value: +t

const escape = str => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22')
/**

@@ -335,27 +334,13 @@ * @implements {Iterable}

['_blob'] () {
const boundary = '----formdata-polyfill-' + Math.random()
const chunks = []
const boundary = '----formdata-polyfill-' + Math.random(),
chunks = [],
p = `--${boundary}\r\nContent-Disposition: form-data; name="`
this.forEach((value, name) => typeof value == 'string'
? chunks.push(p + escape(name) + `"\r\n\r\n${value}\r\n`)
: chunks.push(p + escape(name) + `"; filename="${escape(value.name)}"\r\nContent-Type: ${value.type||"application/octet-stream"}\r\n\r\n`, value, `\r\n`))
chunks.push(`--${boundary}--`)
return new Blob(chunks, {
type: "multipart/form-data; boundary=" + boundary
})
for (const [name, value] of this) {
chunks.push(`--${boundary}\r\n`)
if (value instanceof Blob) {
chunks.push(
`Content-Disposition: form-data; name="${name}"; filename="${value.name}"\r\n` +
`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`,
value,
'\r\n'
)
} else {
chunks.push(
`Content-Disposition: form-data; name="${name}"\r\n\r\n${value}\r\n`
)
}
}
chunks.push(`--${boundary}--`)
return new Blob(chunks, {
type: 'multipart/form-data; boundary=' + boundary
})
}

@@ -362,0 +347,0 @@

;(function(){var h;function l(a){var c=0;return function(){return c<a.length?{done:!1,value:a[c++]}:{done:!0}}}var m="function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,b){if(a==Array.prototype||a==Object.prototype)return a;a[c]=b.value;return a};
function n(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var c=0;c<a.length;++c){var b=a[c];if(b&&b.Math==Math)return b}throw Error("Cannot find global object");}var p=n(this);function r(a,c){if(c)a:{for(var b=p,d=a.split("."),e=0;e<d.length-1;e++){var f=d[e];if(!(f in b))break a;b=b[f]}d=d[d.length-1];e=b[d];f=c(e);f!=e&&null!=f&&m(b,d,{configurable:!0,writable:!0,value:f})}}
function n(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var c=0;c<a.length;++c){var b=a[c];if(b&&b.Math==Math)return b}throw Error("Cannot find global object");}var q=n(this);function r(a,c){if(c)a:{for(var b=q,d=a.split("."),e=0;e<d.length-1;e++){var f=d[e];if(!(f in b))break a;b=b[f]}d=d[d.length-1];e=b[d];f=c(e);f!=e&&null!=f&&m(b,d,{configurable:!0,writable:!0,value:f})}}
r("Symbol",function(a){function c(f){if(this instanceof c)throw new TypeError("Symbol is not a constructor");return new b(d+(f||"")+"_"+e++,f)}function b(f,g){this.A=f;m(this,"description",{configurable:!0,writable:!0,value:g})}if(a)return a;b.prototype.toString=function(){return this.A};var d="jscomp_symbol_"+(1E9*Math.random()>>>0)+"_",e=0;return c});
r("Symbol.iterator",function(a){if(a)return a;a=Symbol("Symbol.iterator");for(var c="Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "),b=0;b<c.length;b++){var d=p[c[b]];"function"===typeof d&&"function"!=typeof d.prototype[a]&&m(d.prototype,a,{configurable:!0,writable:!0,value:function(){return u(l(this))}})}return a});function u(a){a={next:a};a[Symbol.iterator]=function(){return this};return a}
r("Symbol.iterator",function(a){if(a)return a;a=Symbol("Symbol.iterator");for(var c="Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "),b=0;b<c.length;b++){var d=q[c[b]];"function"===typeof d&&"function"!=typeof d.prototype[a]&&m(d.prototype,a,{configurable:!0,writable:!0,value:function(){return u(l(this))}})}return a});function u(a){a={next:a};a[Symbol.iterator]=function(){return this};return a}
function v(a){var c="undefined"!=typeof Symbol&&Symbol.iterator&&a[Symbol.iterator];return c?c.call(a):{next:l(a)}}var w;if("function"==typeof Object.setPrototypeOf)w=Object.setPrototypeOf;else{var y;a:{var z={a:!0},A={};try{A.__proto__=z;y=A.a;break a}catch(a){}y=!1}w=y?function(a,c){a.__proto__=c;if(a.__proto__!==c)throw new TypeError(a+" is not extensible");return a}:null}var B=w;function C(){this.m=!1;this.j=null;this.v=void 0;this.h=1;this.u=this.C=0;this.l=null}

@@ -11,11 +11,11 @@ function D(a){if(a.m)throw new TypeError("Generator is already running");a.m=!0}C.prototype.o=function(a){this.v=a};C.prototype.s=function(a){this.l={D:a,F:!0};this.h=this.C||this.u};C.prototype["return"]=function(a){this.l={"return":a};this.h=this.u};function E(a,c){a.h=3;return{value:c}}function F(a){this.g=new C;this.G=a}F.prototype.o=function(a){D(this.g);if(this.g.j)return G(this,this.g.j.next,a,this.g.o);this.g.o(a);return H(this)};

if("undefined"!==typeof Blob&&("undefined"===typeof FormData||!FormData.prototype.keys)){var L=function(a,c){for(var b=0;b<a.length;b++)c(a[b])},M=function(a,c,b){if(c instanceof Blob){b=void 0!==b?String(b+""):"string"===typeof c.name?c.name:"blob";if(c.name!==b||"[object Blob]"===Object.prototype.toString.call(c))c=new File([c],b);return[String(a),c]}return[String(a),String(c)]},N=function(a,c){if(a.length<c)throw new TypeError(c+" argument required, but only "+a.length+" present.");},O="object"===
typeof globalThis?globalThis:"object"===typeof window?window:"object"===typeof self?self:this,P=O.FormData,Q=O.XMLHttpRequest&&O.XMLHttpRequest.prototype.send,R=O.Request&&O.fetch,S=O.navigator&&O.navigator.sendBeacon,T=O.Element&&O.Element.prototype,U=O.Symbol&&Symbol.toStringTag;U&&(Blob.prototype[U]||(Blob.prototype[U]="Blob"),"File"in O&&!File.prototype[U]&&(File.prototype[U]="File"));try{new File([],"")}catch(a){O.File=function(c,b,d){c=new Blob(c,d||{});d=d&&void 0!==d.lastModified?new Date(d.lastModified):
new Date;Object.defineProperties(c,{name:{value:b},lastModifiedDate:{value:d},lastModified:{value:+d},toString:{value:function(){return"[object File]"}}});U&&Object.defineProperty(c,U,{value:"File"});return c}}var V=function(a){this.i=[];var c=this;a&&L(a.elements,function(b){if(b.name&&!b.disabled&&"submit"!==b.type&&"button"!==b.type&&!b.matches("form fieldset[disabled] *"))if("file"===b.type){var d=b.files&&b.files.length?b.files:[new File([],"",{type:"application/octet-stream"})];L(d,function(e){c.append(b.name,
e)})}else"select-multiple"===b.type||"select-one"===b.type?L(b.options,function(e){!e.disabled&&e.selected&&c.append(b.name,e.value)}):"checkbox"===b.type||"radio"===b.type?b.checked&&c.append(b.name,b.value):(d="textarea"===b.type?b.value.replace(/\r\n/g,"\n").replace(/\n/g,"\r\n"):b.value,c.append(b.name,d))})};h=V.prototype;h.append=function(a,c,b){N(arguments,2);this.i.push(M(a,c,b))};h["delete"]=function(a){N(arguments,1);var c=[];a=String(a);L(this.i,function(b){b[0]!==a&&c.push(b)});this.i=
c};h.entries=function c(){var b,d=this;return K(c,function(e){1==e.h&&(b=0);if(3!=e.h)return b<d.i.length?e=E(e,d.i[b]):(e.h=0,e=void 0),e;b++;e.h=2})};h.forEach=function(c,b){N(arguments,1);for(var d=v(this),e=d.next();!e.done;e=d.next()){var f=v(e.value);e=f.next().value;f=f.next().value;c.call(b,f,e,this)}};h.get=function(c){N(arguments,1);var b=this.i;c=String(c);for(var d=0;d<b.length;d++)if(b[d][0]===c)return b[d][1];return null};h.getAll=function(c){N(arguments,1);var b=[];c=String(c);L(this.i,
function(d){d[0]===c&&b.push(d[1])});return b};h.has=function(c){N(arguments,1);c=String(c);for(var b=0;b<this.i.length;b++)if(this.i[b][0]===c)return!0;return!1};h.keys=function b(){var d=this,e,f,g,k,q;return K(b,function(t){1==t.h&&(e=v(d),f=e.next());if(3!=t.h){if(f.done){t.h=0;return}g=f.value;k=v(g);q=k.next().value;return E(t,q)}f=e.next();t.h=2})};h.set=function(b,d,e){N(arguments,2);b=String(b);var f=[],g=M(b,d,e),k=!0;L(this.i,function(q){q[0]===b?k&&(k=!f.push(g)):f.push(q)});k&&f.push(g);
this.i=f};h.values=function d(){var e=this,f,g,k,q,t;return K(d,function(x){1==x.h&&(f=v(e),g=f.next());if(3!=x.h){if(g.done){x.h=0;return}k=g.value;q=v(k);q.next();t=q.next().value;return E(x,t)}g=f.next();x.h=2})};V.prototype._asNative=function(){for(var d=new P,e=v(this),f=e.next();!f.done;f=e.next()){var g=v(f.value);f=g.next().value;g=g.next().value;d.append(f,g)}return d};V.prototype._blob=function(){for(var d="----formdata-polyfill-"+Math.random(),e=[],f=v(this),g=f.next();!g.done;g=f.next()){var k=
v(g.value);g=k.next().value;k=k.next().value;e.push("--"+d+"\r\n");k instanceof Blob?e.push('Content-Disposition: form-data; name="'+g+'"; filename="'+k.name+'"\r\nContent-Type: '+((k.type||"application/octet-stream")+"\r\n\r\n"),k,"\r\n"):e.push('Content-Disposition: form-data; name="'+g+'"\r\n\r\n'+k+"\r\n")}e.push("--"+d+"--");return new Blob(e,{type:"multipart/form-data; boundary="+d})};V.prototype[Symbol.iterator]=function(){return this.entries()};V.prototype.toString=function(){return"[object FormData]"};
typeof globalThis?globalThis:"object"===typeof window?window:"object"===typeof self?self:this,P=O.FormData,Q=O.XMLHttpRequest&&O.XMLHttpRequest.prototype.send,R=O.Request&&O.fetch,S=O.navigator&&O.navigator.sendBeacon,T=O.Element&&O.Element.prototype,U=O.Symbol&&Symbol.toStringTag;U&&(Blob.prototype[U]||(Blob.prototype[U]="Blob"),"File"in O&&!File.prototype[U]&&(File.prototype[U]="File"));try{new File([],"")}catch(a){O.File=function(c,b,d){c=new Blob(c,d||{});Object.defineProperties(c,{name:{value:b},
lastModified:{value:+(d&&void 0!==d.lastModified?new Date(d.lastModified):new Date)},toString:{value:function(){return"[object File]"}}});U&&Object.defineProperty(c,U,{value:"File"});return c}}var escape=function(a){return a.replace(/\n/g,"%0A").replace(/\r/g,"%0D").replace(/"/g,"%22")},V=function(a){this.i=[];var c=this;a&&L(a.elements,function(b){if(b.name&&!b.disabled&&"submit"!==b.type&&"button"!==b.type&&!b.matches("form fieldset[disabled] *"))if("file"===b.type){var d=b.files&&b.files.length?
b.files:[new File([],"",{type:"application/octet-stream"})];L(d,function(e){c.append(b.name,e)})}else"select-multiple"===b.type||"select-one"===b.type?L(b.options,function(e){!e.disabled&&e.selected&&c.append(b.name,e.value)}):"checkbox"===b.type||"radio"===b.type?b.checked&&c.append(b.name,b.value):(d="textarea"===b.type?b.value.replace(/\r\n/g,"\n").replace(/\n/g,"\r\n"):b.value,c.append(b.name,d))})};h=V.prototype;h.append=function(a,c,b){N(arguments,2);this.i.push(M(a,c,b))};h["delete"]=function(a){N(arguments,
1);var c=[];a=String(a);L(this.i,function(b){b[0]!==a&&c.push(b)});this.i=c};h.entries=function c(){var b,d=this;return K(c,function(e){1==e.h&&(b=0);if(3!=e.h)return b<d.i.length?e=E(e,d.i[b]):(e.h=0,e=void 0),e;b++;e.h=2})};h.forEach=function(c,b){N(arguments,1);for(var d=v(this),e=d.next();!e.done;e=d.next()){var f=v(e.value);e=f.next().value;f=f.next().value;c.call(b,f,e,this)}};h.get=function(c){N(arguments,1);var b=this.i;c=String(c);for(var d=0;d<b.length;d++)if(b[d][0]===c)return b[d][1];
return null};h.getAll=function(c){N(arguments,1);var b=[];c=String(c);L(this.i,function(d){d[0]===c&&b.push(d[1])});return b};h.has=function(c){N(arguments,1);c=String(c);for(var b=0;b<this.i.length;b++)if(this.i[b][0]===c)return!0;return!1};h.keys=function b(){var d=this,e,f,g,k,p;return K(b,function(t){1==t.h&&(e=v(d),f=e.next());if(3!=t.h){if(f.done){t.h=0;return}g=f.value;k=v(g);p=k.next().value;return E(t,p)}f=e.next();t.h=2})};h.set=function(b,d,e){N(arguments,2);b=String(b);var f=[],g=M(b,
d,e),k=!0;L(this.i,function(p){p[0]===b?k&&(k=!f.push(g)):f.push(p)});k&&f.push(g);this.i=f};h.values=function d(){var e=this,f,g,k,p,t;return K(d,function(x){1==x.h&&(f=v(e),g=f.next());if(3!=x.h){if(g.done){x.h=0;return}k=g.value;p=v(k);p.next();t=p.next().value;return E(x,t)}g=f.next();x.h=2})};V.prototype._asNative=function(){for(var d=new P,e=v(this),f=e.next();!f.done;f=e.next()){var g=v(f.value);f=g.next().value;g=g.next().value;d.append(f,g)}return d};V.prototype._blob=function(){var d="----formdata-polyfill-"+
Math.random(),e=[],f="--"+d+'\r\nContent-Disposition: form-data; name="';this.forEach(function(g,k){return"string"==typeof g?e.push(f+escape(k)+('"\r\n\r\n'+g+"\r\n")):e.push(f+escape(k)+('"; filename="'+escape(g.name)+'"\r\nContent-Type: '+(g.type||"application/octet-stream")+"\r\n\r\n"),g,"\r\n")});e.push("--"+d+"--");return new Blob(e,{type:"multipart/form-data; boundary="+d})};V.prototype[Symbol.iterator]=function(){return this.entries()};V.prototype.toString=function(){return"[object FormData]"};
T&&!T.matches&&(T.matches=T.matchesSelector||T.mozMatchesSelector||T.msMatchesSelector||T.oMatchesSelector||T.webkitMatchesSelector||function(d){d=(this.document||this.ownerDocument).querySelectorAll(d);for(var e=d.length;0<=--e&&d.item(e)!==this;);return-1<e});U&&(V.prototype[U]="FormData");if(Q){var W=O.XMLHttpRequest.prototype.setRequestHeader;O.XMLHttpRequest.prototype.setRequestHeader=function(d,e){W.call(this,d,e);"content-type"===d.toLowerCase()&&(this.B=!0)};O.XMLHttpRequest.prototype.send=
function(d){d instanceof V?(d=d._blob(),this.B||this.setRequestHeader("Content-Type",d.type),Q.call(this,d)):Q.call(this,d)}}R&&(O.fetch=function(d,e){e&&e.body&&e.body instanceof V&&(e.body=e.body._blob());return R.call(this,d,e)});S&&(O.navigator.sendBeacon=function(d,e){e instanceof V&&(e=e._asNative());return S.call(this,d,e)});O.FormData=V};
})();
{
"name": "formdata-polyfill",
"version": "4.0.3",
"version": "4.0.4",
"description": "HTML5 `FormData` for Browsers and Node.",

@@ -9,4 +9,4 @@ "type": "module",

"build": "node build",
"_prepare": "npm run build && npm run test",
"_test": "karma start --single-run --browsers ChromeHeadless karma.conf.js"
"prepare": "npm run build && npm run test",
"test": "node test/test-esm.js"
},

@@ -13,0 +13,0 @@ "repository": {

@@ -37,3 +37,3 @@ ### A `FormData` polyfill for the browser ...and a module for NodeJS (`New!`)

stream() { return stream },
[symbol.toStringTag]: 'File'
[Symbol.toStringTag]: 'File'
})

@@ -48,13 +48,11 @@

It's mainly for [node-fetch](https://github.com/node-fetch/node-fetch) and other http library to ease the process of serializing a FormData into a blob and just wish to deal with Blobs instead
<details>
<summary>See code example</summary>
```js
import { formDataToBlob } from 'formdata-polyfill/esm.min.js'
import { Readable } from 'node:stream'
import { FormData, formDataToBlob } from 'formdata-polyfill/esm.min.js'
const blob = formDataToBlob(formData)
const blob = formDataToBlob(new FormData())
fetch('https://httpbin.org/post', { method: 'POST', body: blob })
// node built in http and other similar http library have to do:
const stream = stream.Readable.from(blob.stream())
const stream = Readable.from(blob.stream())
const req = http.request('http://httpbin.org/post', {

@@ -69,3 +67,3 @@ method: 'post',

```
</details>
PS: blob & file that are appended to the FormData will not be read until any of the serialized blob read-methods gets called

@@ -72,0 +70,0 @@ ...so uploading very large files is no biggie

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc