formdata-polyfill
Advanced tools
Comparing version 3.0.15 to 3.0.17
231
FormData.js
@@ -1,5 +0,8 @@ | ||
if (typeof FormData === 'undefined' || !FormData.prototype.keys) { | ||
/* global FormData self Blob File */ | ||
/* eslint-disable no-inner-declarations */ | ||
if (typeof Blob === 'function' && (typeof FormData === 'undefined' || !FormData.prototype.keys)) { | ||
const global = typeof window === 'object' | ||
? window : typeof self === 'object' | ||
? self : this | ||
? window | ||
: typeof self === 'object' ? self : this | ||
@@ -12,2 +15,3 @@ // keep a reference to native implementation | ||
const _fetch = global.Request && global.fetch | ||
const _sendBeacon = global.navigator && global.navigator.sendBeacon | ||
@@ -20,5 +24,2 @@ // Unable to patch Request constructor correctly | ||
const stringTag = global.Symbol && Symbol.toStringTag | ||
const map = new WeakMap | ||
const wm = o => map.get(o) | ||
const arrayFrom = Array.from || (obj => [].slice.call(obj)) | ||
@@ -38,7 +39,7 @@ // Add missing stringTags to blob and files | ||
try { | ||
new File([], '') | ||
new File([], '') // eslint-disable-line | ||
} catch (a) { | ||
global.File = function(b, d, c) { | ||
global.File = function File (b, d, c) { | ||
const blob = new Blob(b, c) | ||
const t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date | ||
const t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date() | ||
@@ -56,3 +57,3 @@ Object.defineProperties(blob, { | ||
toString: { | ||
value() { | ||
value () { | ||
return '[object File]' | ||
@@ -73,4 +74,4 @@ } | ||
function normalizeValue([value, filename]) { | ||
if (value instanceof Blob) | ||
function normalizeValue ([value, filename]) { | ||
if (value instanceof Blob) { | ||
// Should always returns a new File instance | ||
@@ -82,2 +83,3 @@ // console.assert(fd.get(x) !== fd.get(x)) | ||
}) | ||
} | ||
@@ -87,18 +89,12 @@ return value | ||
function stringify(name) { | ||
if (!arguments.length) | ||
throw new TypeError('1 argument required, but only 0 present.') | ||
return [name + ''] | ||
function ensureArgs (args, expected) { | ||
if (args.length < expected) { | ||
throw new TypeError(`${expected} argument required, but only ${args.length} present.`) | ||
} | ||
} | ||
function normalizeArgs(name, value, filename) { | ||
if (arguments.length < 2) | ||
throw new TypeError( | ||
`2 arguments required, but only ${arguments.length} present.` | ||
) | ||
function normalizeArgs (name, value, filename) { | ||
return value instanceof Blob | ||
// normalize name and filename if adding an attachment | ||
? [name + '', value, filename !== undefined | ||
? [String(name), value, filename !== undefined | ||
? filename + '' // Cast filename to string if 3th arg isn't undefined | ||
@@ -110,3 +106,3 @@ : typeof value.name === 'string' // if name prop exist | ||
// If no attachment, just cast the args to strings | ||
: [name + '', value + ''] | ||
: [String(name), String(value)] | ||
} | ||
@@ -116,7 +112,4 @@ | ||
// https://html.spec.whatwg.org/multipage/form-elements.html#textarea-line-break-normalisation-transformation | ||
function normalizeLinefeeds(value) { | ||
if (typeof value === "string") { | ||
value = value.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") | ||
} | ||
return value | ||
function normalizeLinefeeds (value) { | ||
return value.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n') | ||
} | ||
@@ -134,3 +127,2 @@ | ||
class FormDataPolyfill { | ||
/** | ||
@@ -141,7 +133,6 @@ * FormData class | ||
*/ | ||
constructor(form) { | ||
map.set(this, Object.create(null)) | ||
constructor (form) { | ||
this._data = Object.create(null) | ||
if (!form) | ||
return this | ||
if (!form) return this | ||
@@ -154,3 +145,7 @@ const self = this | ||
if (elm.type === 'file') { | ||
each(elm.files || [], file => { | ||
const files = elm.files && elm.files.length | ||
? elm.files | ||
: [new File([], '', { type: 'application/octet-stream' })] // #78 | ||
each(files, file => { | ||
self.append(elm.name, file) | ||
@@ -171,16 +166,16 @@ }) | ||
/** | ||
* Append a field | ||
* | ||
* @param {String} name field name | ||
* @param {String|Blob|File} value string / blob / file | ||
* @param {String=} filename filename to use with blob | ||
* @return {Undefined} | ||
* @param {string} name field name | ||
* @param {string|Blob|File} value string / blob / file | ||
* @param {string=} filename filename to use with blob | ||
* @return {undefined} | ||
*/ | ||
append(name, value, filename) { | ||
const map = wm(this) | ||
append (name, value, filename) { | ||
ensureArgs(arguments, 2) | ||
;[name, value, filename] = normalizeArgs.apply(null, arguments) | ||
const map = this._data | ||
if (!map[name]) | ||
map[name] = [] | ||
if (!map[name]) map[name] = [] | ||
@@ -190,14 +185,13 @@ map[name].push([value, filename]) | ||
/** | ||
* Delete all fields values given name | ||
* | ||
* @param {String} name Field name | ||
* @return {Undefined} | ||
* @param {string} name Field name | ||
* @return {undefined} | ||
*/ | ||
delete(name) { | ||
delete wm(this)[name] | ||
delete (name) { | ||
ensureArgs(arguments, 1) | ||
delete this._data[String(name)] | ||
} | ||
/** | ||
@@ -208,8 +202,10 @@ * Iterate over all fields as [name, value] | ||
*/ | ||
*entries() { | ||
const map = wm(this) | ||
* entries () { | ||
const map = this._data | ||
for (let name in map) | ||
for (let value of map[name]) | ||
for (let name in map) { | ||
for (let value of map[name]) { | ||
yield [name, normalizeValue(value)] | ||
} | ||
} | ||
} | ||
@@ -222,10 +218,11 @@ | ||
* @param {Object=} thisArg `this` context for callback function | ||
* @return {Undefined} | ||
* @return {undefined} | ||
*/ | ||
forEach(callback, thisArg) { | ||
for (let [name, value] of this) | ||
forEach (callback, thisArg) { | ||
ensureArgs(arguments, 1) | ||
for (let [name, value] of this) { | ||
callback.call(thisArg, value, name, this) | ||
} | ||
} | ||
/** | ||
@@ -235,33 +232,34 @@ * Return first field value given name | ||
* | ||
* @param {String} name Field name | ||
* @return {String|File|null} value Fields value | ||
* @param {string} name Field name | ||
* @return {string|File|null} value Fields value | ||
*/ | ||
get(name) { | ||
const map = wm(this) | ||
get (name) { | ||
ensureArgs(arguments, 1) | ||
const map = this._data | ||
name = String(name) | ||
return map[name] ? normalizeValue(map[name][0]) : null | ||
} | ||
/** | ||
* Return all fields values given name | ||
* | ||
* @param {String} name Fields name | ||
* @param {string} name Fields name | ||
* @return {Array} [{String|File}] | ||
*/ | ||
getAll(name) { | ||
return (wm(this)[name] || []).map(normalizeValue) | ||
getAll (name) { | ||
ensureArgs(arguments, 1) | ||
return (this._data[String(name)] || []).map(normalizeValue) | ||
} | ||
/** | ||
* Check for field name existence | ||
* | ||
* @param {String} name Field name | ||
* @param {string} name Field name | ||
* @return {boolean} | ||
*/ | ||
has(name) { | ||
return name in wm(this) | ||
has (name) { | ||
ensureArgs(arguments, 1) | ||
return String(name) in this._data | ||
} | ||
/** | ||
@@ -272,21 +270,22 @@ * Iterate over all fields name | ||
*/ | ||
*keys() { | ||
for (let [name] of this) | ||
* keys () { | ||
for (let [name] of this) { | ||
yield name | ||
} | ||
} | ||
/** | ||
* Overwrite all values given name | ||
* | ||
* @param {String} name Filed name | ||
* @param {String} value Field value | ||
* @param {String=} filename Filename (optional) | ||
* @return {Undefined} | ||
* @param {string} name Filed name | ||
* @param {string} value Field value | ||
* @param {string=} filename Filename (optional) | ||
* @return {undefined} | ||
*/ | ||
set(name, value, filename) { | ||
wm(this)[name] = [[value, filename]] | ||
set (name, value, filename) { | ||
ensureArgs(arguments, 2) | ||
const args = normalizeArgs.apply(null, arguments) | ||
this._data[args[0]] = [[args[1], args[2]]] | ||
} | ||
/** | ||
@@ -297,8 +296,8 @@ * Iterate over all fields | ||
*/ | ||
*values() { | ||
for (let [name, value] of this) | ||
* values () { | ||
for (let [, value] of this) { | ||
yield value | ||
} | ||
} | ||
/** | ||
@@ -310,7 +309,8 @@ * Return a native (perhaps degraded) FormData with only a `append` method | ||
*/ | ||
['_asNative']() { | ||
const fd = new _FormData | ||
['_asNative'] () { | ||
const fd = new _FormData() | ||
for (let [name, value] of this) | ||
for (let [name, value] of this) { | ||
fd.append(name, value) | ||
} | ||
@@ -320,3 +320,2 @@ return fd | ||
/** | ||
@@ -327,3 +326,3 @@ * [_blob description] | ||
*/ | ||
['_blob']() { | ||
['_blob'] () { | ||
const boundary = '----formdata-polyfill-' + Math.random() | ||
@@ -351,6 +350,7 @@ const chunks = [] | ||
return new Blob(chunks, {type: 'multipart/form-data; boundary=' + boundary}) | ||
return new Blob(chunks, { | ||
type: 'multipart/form-data; boundary=' + boundary | ||
}) | ||
} | ||
/** | ||
@@ -362,13 +362,12 @@ * The class itself is iterable | ||
*/ | ||
[Symbol.iterator]() { | ||
[Symbol.iterator] () { | ||
return this.entries() | ||
} | ||
/** | ||
* Create the default string description. | ||
* | ||
* @return {String} [object FormData] | ||
* @return {string} [object FormData] | ||
*/ | ||
toString() { | ||
toString () { | ||
return '[object FormData]' | ||
@@ -378,3 +377,2 @@ } | ||
if (stringTag) { | ||
@@ -384,4 +382,2 @@ /** | ||
* It is accessed internally by the Object.prototype.toString(). | ||
* | ||
* @return {String} FormData | ||
*/ | ||
@@ -391,21 +387,10 @@ FormDataPolyfill.prototype[stringTag] = 'FormData' | ||
const decorations = [ | ||
['append', normalizeArgs], | ||
['delete', stringify], | ||
['get', stringify], | ||
['getAll', stringify], | ||
['has', stringify], | ||
['set', normalizeArgs] | ||
] | ||
decorations.forEach(arr => { | ||
const orig = FormDataPolyfill.prototype[arr[0]] | ||
FormDataPolyfill.prototype[arr[0]] = function() { | ||
return orig.apply(this, arr[1].apply(this, arrayFrom(arguments))) | ||
} | ||
}) | ||
// Patch xhr's send method to call _blob transparently | ||
if (_send) { | ||
XMLHttpRequest.prototype.send = function(data) { | ||
/** | ||
* @param {ArrayBuffer|ArrayBufferView|Blob|Document|FormData|string=} data | ||
* @return {undefined} | ||
* @see http://www.w3.org/TR/XMLHttpRequest/#the-send()-method | ||
*/ | ||
global.XMLHttpRequest.prototype.send = function (data) { | ||
// I would check if Content-Type isn't already set | ||
@@ -428,3 +413,3 @@ // But xhr lacks getRequestHeaders functionallity | ||
global.fetch = function(input, init) { | ||
global.fetch = function (input, init) { | ||
if (init && init.body && init.body instanceof FormDataPolyfill) { | ||
@@ -434,7 +419,17 @@ init.body = init.body['_blob']() | ||
return _fetch(input, init) | ||
return _fetch.call(this, input, init) | ||
} | ||
} | ||
// Patch navigator.sendBeacon to use native FormData | ||
if (_sendBeacon) { | ||
global.navigator.sendBeacon = function (url, data) { | ||
if (data instanceof FormDataPolyfill) { | ||
data = data['_asNative']() | ||
} | ||
return _sendBeacon.call(this, url, data) | ||
} | ||
} | ||
global['FormData'] = FormDataPolyfill | ||
} |
@@ -1,17 +0,17 @@ | ||
;(function(){var k;function l(a){var b=0;return function(){return b<a.length?{done:!1,value:a[b++]}:{done:!0}}}var m="function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,d){a!=Array.prototype&&a!=Object.prototype&&(a[b]=d.value)},n="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this;function p(){p=function(){};n.Symbol||(n.Symbol=r)}var r=function(){var a=0;return function(b){return"jscomp_symbol_"+(b||"")+a++}}(); | ||
;(function(){var k;function l(a){var b=0;return function(){return b<a.length?{done:!1,value:a[b++]}:{done:!0}}}var m="function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,e){a!=Array.prototype&&a!=Object.prototype&&(a[b]=e.value)},n="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this;function p(){p=function(){};n.Symbol||(n.Symbol=r)}var r=function(){var a=0;return function(b){return"jscomp_symbol_"+(b||"")+a++}}(); | ||
function u(){p();var a=n.Symbol.iterator;a||(a=n.Symbol.iterator=n.Symbol("iterator"));"function"!=typeof Array.prototype[a]&&m(Array.prototype,a,{configurable:!0,writable:!0,value:function(){return v(l(this))}});u=function(){}}function v(a){u();a={next:a};a[n.Symbol.iterator]=function(){return this};return a}function x(a){var b="undefined"!=typeof Symbol&&Symbol.iterator&&a[Symbol.iterator];return b?b.call(a):{next:l(a)}}var y; | ||
if("function"==typeof Object.setPrototypeOf)y=Object.setPrototypeOf;else{var z;a:{var A={o:!0},B={};try{B.__proto__=A;z=B.o;break a}catch(a){}z=!1}y=z?function(a,b){a.__proto__=b;if(a.__proto__!==b)throw new TypeError(a+" is not extensible");return a}:null}var C=y;function D(){this.g=!1;this.c=null;this.m=void 0;this.b=1;this.l=this.s=0;this.f=null}function E(a){if(a.g)throw new TypeError("Generator is already running");a.g=!0}D.prototype.h=function(a){this.m=a}; | ||
D.prototype.i=function(a){this.f={u:a,v:!0};this.b=this.s||this.l};D.prototype["return"]=function(a){this.f={"return":a};this.b=this.l};function F(a,b,d){a.b=d;return{value:b}}function G(a){this.w=a;this.j=[];for(var b in a)this.j.push(b);this.j.reverse()}function H(a){this.a=new D;this.A=a}H.prototype.h=function(a){E(this.a);if(this.a.c)return I(this,this.a.c.next,a,this.a.h);this.a.h(a);return J(this)}; | ||
function K(a,b){E(a.a);var d=a.a.c;if(d)return I(a,"return"in d?d["return"]:function(a){return{value:a,done:!0}},b,a.a["return"]);a.a["return"](b);return J(a)}H.prototype.i=function(a){E(this.a);if(this.a.c)return I(this,this.a.c["throw"],a,this.a.h);this.a.i(a);return J(this)}; | ||
function I(a,b,d,c){try{var e=b.call(a.a.c,d);if(!(e instanceof Object))throw new TypeError("Iterator result "+e+" is not an object");if(!e.done)return a.a.g=!1,e;var f=e.value}catch(g){return a.a.c=null,a.a.i(g),J(a)}a.a.c=null;c.call(a.a,f);return J(a)}function J(a){for(;a.a.b;)try{var b=a.A(a.a);if(b)return a.a.g=!1,{value:b.value,done:!1}}catch(d){a.a.m=void 0,a.a.i(d)}a.a.g=!1;if(a.a.f){b=a.a.f;a.a.f=null;if(b.v)throw b.u;return{value:b["return"],done:!0}}return{value:void 0,done:!0}} | ||
function L(a){this.next=function(b){return a.h(b)};this["throw"]=function(b){return a.i(b)};this["return"]=function(b){return K(a,b)};u();this[Symbol.iterator]=function(){return this}}function M(a,b){var d=new L(new H(b));C&&C(d,a.prototype);return d} | ||
if("undefined"===typeof FormData||!FormData.prototype.keys){var N=function(a,b){for(var d=0;d<a.length;d++)b(a[d])},O=function(a){"string"===typeof a&&(a=a.replace(/\r\n/g,"\n").replace(/\n/g,"\r\n"));return a},P=function(a,b,d){if(2>arguments.length)throw new TypeError("2 arguments required, but only "+arguments.length+" present.");return b instanceof Blob?[a+"",b,void 0!==d?d+"":"string"===typeof b.name?b.name:"blob"]:[a+"",b+""]},Q=function(a){if(!arguments.length)throw new TypeError("1 argument required, but only 0 present."); | ||
return[a+""]},R=function(a){var b=x(a);a=b.next().value;b=b.next().value;a instanceof Blob&&(a=new File([a],b,{type:a.type,lastModified:a.lastModified}));return a},S="object"===typeof window?window:"object"===typeof self?self:this,T=S.FormData,U=S.XMLHttpRequest&&S.XMLHttpRequest.prototype.send,V=S.Request&&S.fetch;p();var W=S.Symbol&&Symbol.toStringTag,X=new WeakMap,Y=Array.from||function(a){return[].slice.call(a)};W&&(Blob.prototype[W]||(Blob.prototype[W]="Blob"),"File"in S&&!File.prototype[W]&& | ||
(File.prototype[W]="File"));try{new File([],"")}catch(a){S.File=function(b,d,c){b=new Blob(b,c);c=c&&void 0!==c.lastModified?new Date(c.lastModified):new Date;Object.defineProperties(b,{name:{value:d},lastModifiedDate:{value:c},lastModified:{value:+c},toString:{value:function(){return"[object File]"}}});W&&Object.defineProperty(b,W,{value:"File"});return b}}p();u();var Z=function(a){X.set(this,Object.create(null));if(!a)return this;var b=this;N(a.elements,function(a){if(a.name&&!a.disabled&&"submit"!== | ||
a.type&&"button"!==a.type)if("file"===a.type)N(a.files||[],function(c){b.append(a.name,c)});else if("select-multiple"===a.type||"select-one"===a.type)N(a.options,function(c){!c.disabled&&c.selected&&b.append(a.name,c.value)});else if("checkbox"===a.type||"radio"===a.type)a.checked&&b.append(a.name,a.value);else{var c="textarea"===a.type?O(a.value):a.value;b.append(a.name,c)}})};k=Z.prototype;k.append=function(a,b,d){var c=X.get(this);c[a]||(c[a]=[]);c[a].push([b,d])};k["delete"]=function(a){delete X.get(this)[a]}; | ||
k.entries=function b(){var d=this,c,e,f,g,h,q;return M(b,function(b){switch(b.b){case 1:c=X.get(d),f=new G(c);case 2:var t;a:{for(t=f;0<t.j.length;){var w=t.j.pop();if(w in t.w){t=w;break a}}t=null}if(null==(e=t)){b.b=0;break}g=x(c[e]);h=g.next();case 5:if(h.done){b.b=2;break}q=h.value;return F(b,[e,R(q)],6);case 6:h=g.next(),b.b=5}})};k.forEach=function(b,d){for(var c=x(this),e=c.next();!e.done;e=c.next()){var f=x(e.value);e=f.next().value;f=f.next().value;b.call(d,f,e,this)}};k.get=function(b){var d= | ||
X.get(this);return d[b]?R(d[b][0]):null};k.getAll=function(b){return(X.get(this)[b]||[]).map(R)};k.has=function(b){return b in X.get(this)};k.keys=function d(){var c=this,e,f,g,h,q;return M(d,function(d){1==d.b&&(e=x(c),f=e.next());if(3!=d.b){if(f.done){d.b=0;return}g=f.value;h=x(g);q=h.next().value;return F(d,q,3)}f=e.next();d.b=2})};k.set=function(d,c,e){X.get(this)[d]=[[c,e]]};k.values=function c(){var e=this,f,g,h,q,w;return M(c,function(c){1==c.b&&(f=x(e),g=f.next());if(3!=c.b){if(g.done){c.b= | ||
0;return}h=g.value;q=x(h);q.next();w=q.next().value;return F(c,w,3)}g=f.next();c.b=2})};Z.prototype._asNative=function(){for(var c=new T,e=x(this),f=e.next();!f.done;f=e.next()){var g=x(f.value);f=g.next().value;g=g.next().value;c.append(f,g)}return c};Z.prototype._blob=function(){for(var c="----formdata-polyfill-"+Math.random(),e=[],f=x(this),g=f.next();!g.done;g=f.next()){var h=x(g.value);g=h.next().value;h=h.next().value;e.push("--"+c+"\r\n");h instanceof Blob?e.push('Content-Disposition: form-data; name="'+ | ||
g+'"; filename="'+h.name+'"\r\n',"Content-Type: "+(h.type||"application/octet-stream")+"\r\n\r\n",h,"\r\n"):e.push('Content-Disposition: form-data; name="'+g+'"\r\n\r\n'+h+"\r\n")}e.push("--"+c+"--");return new Blob(e,{type:"multipart/form-data; boundary="+c})};Z.prototype[Symbol.iterator]=function(){return this.entries()};Z.prototype.toString=function(){return"[object FormData]"};W&&(Z.prototype[W]="FormData");[["append",P],["delete",Q],["get",Q],["getAll",Q],["has",Q],["set",P]].forEach(function(c){var e= | ||
Z.prototype[c[0]];Z.prototype[c[0]]=function(){return e.apply(this,c[1].apply(this,Y(arguments)))}});U&&(XMLHttpRequest.prototype.send=function(c){c instanceof Z?(c=c._blob(),this.setRequestHeader("Content-Type",c.type),U.call(this,c)):U.call(this,c)});if(V){var aa=S.fetch;S.fetch=function(c,e){e&&e.body&&e.body instanceof Z&&(e.body=e.body._blob());return aa(c,e)}}S.FormData=Z}; | ||
if("function"==typeof Object.setPrototypeOf)y=Object.setPrototypeOf;else{var z;a:{var A={s:!0},B={};try{B.__proto__=A;z=B.s;break a}catch(a){}z=!1}y=z?function(a,b){a.__proto__=b;if(a.__proto__!==b)throw new TypeError(a+" is not extensible");return a}:null}var C=y;function D(){this.h=!1;this.c=null;this.o=void 0;this.b=1;this.m=this.u=0;this.g=null}function E(a){if(a.h)throw new TypeError("Generator is already running");a.h=!0}D.prototype.i=function(a){this.o=a}; | ||
D.prototype.j=function(a){this.g={v:a,w:!0};this.b=this.u||this.m};D.prototype["return"]=function(a){this.g={"return":a};this.b=this.m};function F(a,b,e){a.b=e;return{value:b}}function G(a){this.A=a;this.l=[];for(var b in a)this.l.push(b);this.l.reverse()}function H(a){this.a=new D;this.B=a}H.prototype.i=function(a){E(this.a);if(this.a.c)return I(this,this.a.c.next,a,this.a.i);this.a.i(a);return J(this)}; | ||
function K(a,b){E(a.a);var e=a.a.c;if(e)return I(a,"return"in e?e["return"]:function(a){return{value:a,done:!0}},b,a.a["return"]);a.a["return"](b);return J(a)}H.prototype.j=function(a){E(this.a);if(this.a.c)return I(this,this.a.c["throw"],a,this.a.i);this.a.j(a);return J(this)}; | ||
function I(a,b,e,c){try{var d=b.call(a.a.c,e);if(!(d instanceof Object))throw new TypeError("Iterator result "+d+" is not an object");if(!d.done)return a.a.h=!1,d;var f=d.value}catch(g){return a.a.c=null,a.a.j(g),J(a)}a.a.c=null;c.call(a.a,f);return J(a)}function J(a){for(;a.a.b;)try{var b=a.B(a.a);if(b)return a.a.h=!1,{value:b.value,done:!1}}catch(e){a.a.o=void 0,a.a.j(e)}a.a.h=!1;if(a.a.g){b=a.a.g;a.a.g=null;if(b.w)throw b.v;return{value:b["return"],done:!0}}return{value:void 0,done:!0}} | ||
function L(a){this.next=function(b){return a.i(b)};this["throw"]=function(b){return a.j(b)};this["return"]=function(b){return K(a,b)};u();this[Symbol.iterator]=function(){return this}}function M(a,b){var e=new L(new H(b));C&&C(e,a.prototype);return e} | ||
if("function"===typeof Blob&&("undefined"===typeof FormData||!FormData.prototype.keys)){var N=function(a,b){for(var e=0;e<a.length;e++)b(a[e])},O=function(a,b,e){return b instanceof Blob?[String(a),b,void 0!==e?e+"":"string"===typeof b.name?b.name:"blob"]:[String(a),String(b)]},P=function(a,b){if(a.length<b)throw new TypeError(b+" argument required, but only "+a.length+" present.");},Q=function(a){var b=x(a);a=b.next().value;b=b.next().value;a instanceof Blob&&(a=new File([a],b,{type:a.type,lastModified:a.lastModified})); | ||
return a},R="object"===typeof window?window:"object"===typeof self?self:this,S=R.FormData,T=R.XMLHttpRequest&&R.XMLHttpRequest.prototype.send,U=R.Request&&R.fetch,V=R.navigator&&R.navigator.sendBeacon;p();var W=R.Symbol&&Symbol.toStringTag;W&&(Blob.prototype[W]||(Blob.prototype[W]="Blob"),"File"in R&&!File.prototype[W]&&(File.prototype[W]="File"));try{new File([],"")}catch(a){R.File=function(b,e,c){b=new Blob(b,c);c=c&&void 0!==c.lastModified?new Date(c.lastModified):new Date;Object.defineProperties(b, | ||
{name:{value:e},lastModifiedDate:{value:c},lastModified:{value:+c},toString:{value:function(){return"[object File]"}}});W&&Object.defineProperty(b,W,{value:"File"});return b}}p();u();var X=function(a){this.f=Object.create(null);if(!a)return this;var b=this;N(a.elements,function(a){if(a.name&&!a.disabled&&"submit"!==a.type&&"button"!==a.type)if("file"===a.type){var c=a.files&&a.files.length?a.files:[new File([],"",{type:"application/octet-stream"})];N(c,function(c){b.append(a.name,c)})}else"select-multiple"=== | ||
a.type||"select-one"===a.type?N(a.options,function(c){!c.disabled&&c.selected&&b.append(a.name,c.value)}):"checkbox"===a.type||"radio"===a.type?a.checked&&b.append(a.name,a.value):(c="textarea"===a.type?a.value.replace(/\r\n/g,"\n").replace(/\n/g,"\r\n"):a.value,b.append(a.name,c))})};k=X.prototype;k.append=function(a,b,e){P(arguments,2);var c=x(O.apply(null,arguments));a=c.next().value;b=c.next().value;e=c.next().value;c=this.f;c[a]||(c[a]=[]);c[a].push([b,e])};k["delete"]=function(a){P(arguments, | ||
1);delete this.f[String(a)]};k.entries=function b(){var e=this,c,d,f,g,h,q;return M(b,function(b){switch(b.b){case 1:c=e.f,f=new G(c);case 2:var t;a:{for(t=f;0<t.l.length;){var w=t.l.pop();if(w in t.A){t=w;break a}}t=null}if(null==(d=t)){b.b=0;break}g=x(c[d]);h=g.next();case 5:if(h.done){b.b=2;break}q=h.value;return F(b,[d,Q(q)],6);case 6:h=g.next(),b.b=5}})};k.forEach=function(b,e){P(arguments,1);for(var c=x(this),d=c.next();!d.done;d=c.next()){var f=x(d.value);d=f.next().value;f=f.next().value; | ||
b.call(e,f,d,this)}};k.get=function(b){P(arguments,1);var e=this.f;b=String(b);return e[b]?Q(e[b][0]):null};k.getAll=function(b){P(arguments,1);return(this.f[String(b)]||[]).map(Q)};k.has=function(b){P(arguments,1);return String(b)in this.f};k.keys=function e(){var c=this,d,f,g,h,q;return M(e,function(e){1==e.b&&(d=x(c),f=d.next());if(3!=e.b){if(f.done){e.b=0;return}g=f.value;h=x(g);q=h.next().value;return F(e,q,3)}f=d.next();e.b=2})};k.set=function(e,c,d){P(arguments,2);var f=O.apply(null,arguments); | ||
this.f[f[0]]=[[f[1],f[2]]]};k.values=function c(){var d=this,f,g,h,q,w;return M(c,function(c){1==c.b&&(f=x(d),g=f.next());if(3!=c.b){if(g.done){c.b=0;return}h=g.value;q=x(h);q.next();w=q.next().value;return F(c,w,3)}g=f.next();c.b=2})};X.prototype._asNative=function(){for(var c=new S,d=x(this),f=d.next();!f.done;f=d.next()){var g=x(f.value);f=g.next().value;g=g.next().value;c.append(f,g)}return c};X.prototype._blob=function(){for(var c="----formdata-polyfill-"+Math.random(),d=[],f=x(this),g=f.next();!g.done;g= | ||
f.next()){var h=x(g.value);g=h.next().value;h=h.next().value;d.push("--"+c+"\r\n");h instanceof Blob?d.push('Content-Disposition: form-data; name="'+g+'"; filename="'+h.name+'"\r\n',"Content-Type: "+(h.type||"application/octet-stream")+"\r\n\r\n",h,"\r\n"):d.push('Content-Disposition: form-data; name="'+g+'"\r\n\r\n'+h+"\r\n")}d.push("--"+c+"--");return new Blob(d,{type:"multipart/form-data; boundary="+c})};X.prototype[Symbol.iterator]=function(){return this.entries()};X.prototype.toString=function(){return"[object FormData]"}; | ||
W&&(X.prototype[W]="FormData");T&&(R.XMLHttpRequest.prototype.send=function(c){c instanceof X?(c=c._blob(),this.setRequestHeader("Content-Type",c.type),T.call(this,c)):T.call(this,c)});if(U){var Y=R.fetch;R.fetch=function(c,d){d&&d.body&&d.body instanceof X&&(d.body=d.body._blob());return Y.call(this,c,d)}}V&&(R.navigator.sendBeacon=function(c,d){d instanceof X&&(d=d._asNative());return V.call(this,c,d)});R.FormData=X}; | ||
})(); |
{ | ||
"name": "formdata-polyfill", | ||
"version": "3.0.15", | ||
"version": "3.0.17", | ||
"description": "HTML5 `FormData` polyfill for Browsers.", | ||
@@ -8,4 +8,4 @@ "main": "formdata.min.js", | ||
"build": "node build", | ||
"prepare": "npm run build", | ||
"test": "karma start --single-run --browsers ChromeHeadless karma.conf.js" | ||
"prepare": "npm run build && npm run test", | ||
"test": "standard ./FormData.js && karma start --single-run --browsers ChromeHeadless karma.conf.js" | ||
}, | ||
@@ -39,4 +39,5 @@ "repository": { | ||
"karma-mocha": "^1.3.0", | ||
"mocha": "^5.2.0" | ||
"mocha": "^6.0.0", | ||
"standard": "^12.0.1" | ||
} | ||
} |
@@ -9,2 +9,4 @@ # FormData | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
```bash | ||
@@ -18,3 +20,4 @@ npm install formdata-polyfill | ||
since otherwise there is no way to get or delete existing values in the FormData object. | ||
Therefore this also patches `XMLHttpRequest.prototype.send` and `fetch` to send the FormData as a blob. | ||
Therefore this also patches `XMLHttpRequest.prototype.send` and `fetch` to send the `FormData` as a blob, | ||
and `navigator.sendBeacon` to send native `FormData`. | ||
@@ -34,5 +37,2 @@ I was unable to patch the Response/Request constructor | ||
The internal data is kept private to prevent unintentional access to it, | ||
therefore `WeakMap` is used and you may also need a polyfill for that. | ||
If you need to support IE <= 9 then I recommend you to include eligrey's [blob.js] | ||
@@ -39,0 +39,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22738
385
7