jimmywarting / FormData

HTML5 `FormData` polyfill for Browsers and nodejs
MIT License
360 stars 102 forks source link

undefined or null reference #37

Closed mmiklosik closed 6 years ago

mmiklosik commented 6 years ago

Hi, i using this polyfill, but i get error: Unable to get property 'apply' of undefined or null reference

screen shot 2017-11-27 at 17 09 16
jimmywarting commented 6 years ago

Sorry, I'm going to need something better then a image... there is two apply on that row.

a fiddle or stack trace would be good... what do you execute to make this error happen? and what version of IE are you using?

if you would beautify the script it would also help

mmiklosik commented 6 years ago

This is all i have :/ formdata.min.js is yours lib and undefined is located in the first apply (specifically f) IE version: 11.0.9600.18838 I think the error will happen, when i tried get data from FormData Object, like formdata.get("file").name

Aebrathia commented 6 years ago

My team and I just had the problem. We found that your google-closure-compiler command strips append, delete, getAll methods from the compiled code.

That's why on line 357 it throws "property 'apply' of undefined or null reference" because it can't find those methods.

I would love to create a PR but i do not know google-closure-compiler.

jimmywarting commented 6 years ago

I simplified a part of the es6 code to es5 and came out with this:

var g,k="function"==typeof Object.defineProperties?Object.defineProperty:function(b,a,d){b!=Array.prototype&&b!=Object.prototype&&(b[a]=d.value)},l="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this;function n(){n=function(){};l.Symbol||(l.Symbol=p)}var p=function(){var b=0;return function(a){return"jscomp_symbol_"+(a||"")+b++}}();
function q(){n();var b=l.Symbol.iterator;b||(b=l.Symbol.iterator=l.Symbol("iterator"));"function"!=typeof Array.prototype[b]&&k(Array.prototype,b,{configurable:!0,writable:!0,value:function(){return u(this)}});q=function(){}}function u(b){var a=0;return v(function(){return a<b.length?{done:!1,value:b[a++]}:{done:!0}})}function v(b){q();b={next:b};b[l.Symbol.iterator]=function(){return this};return b}function w(b){q();n();q();var a=b[Symbol.iterator];return a?a.call(b):u(b)}
if(!window.FormData||!window.FormData.prototype.keys){var x=function(b,a,d){if(2>arguments.length)throw new TypeError("2 arguments required, but only "+arguments.length+" present.");return a instanceof Blob?[b+"",a,void 0!==d?d+"":"File"===Object.prototype.toString.call(a).slice(8,-1)?a.name:"Blob"]:[b+"",a+""]},y=function(b){if(!arguments.length)throw new TypeError("1 argument required, but only 0 present.");return[b+""]},A=function(b){var a=w(b);b=a.next().value;a=a.next().value;b instanceof Blob&&
(b=new File([b],a,{type:b.type,lastModified:b.lastModified}));return b},B=window.FormData,C=window.XMLHttpRequest.prototype.send,D=window.Request&&window.fetch;n();var E=window.Symbol&&Symbol.toStringTag,F=new WeakMap,G=Array.from||function(b){return[].slice.call(b)};E&&(Blob.prototype[E]||(Blob.prototype[E]="Blob"),"File"in window&&!File.prototype[E]&&(File.prototype[E]="File"));try{new File([],"")}catch(b){window.File=function(a,d,c){a=new Blob(a,c);c=c&&void 0!==c.lastModified?new Date(c.lastModified):
new Date;Object.defineProperties(a,{name:{value:d},lastModifiedDate:{value:c},lastModified:{value:+c},toString:{value:function(){return"[object File]"}}});E&&Object.defineProperty(a,E,{value:"File"});return a}}var H=function(b){F.set(this,Object.create(null));if(!b)return this;b=w(G(b.elements));for(var a=b.next();!a.done;a=b.next())if(a=a.value,a.name&&!a.disabled)if("file"===a.type)for(var d=w(a.files),c=d.next();!c.done;c=d.next())this.append(a.name,c.value);else if("select-multiple"===a.type||
"select-one"===a.type)for(a=w(G(a.selectedOptions)),d=a.next();!d.done;d=a.next())d=d.value,this.append(d.name,d.value);else"checkbox"===a.type||"radio"===a.type?a.checked&&this.append(a.name,a.value):this.append(a.name,a.value)};g=H.prototype;g.append=function(b,a,d){var c=F.get(this);c[b]||(c[b]=[]);c[b].push([a,d])};g["delete"]=function(b){delete F.get(this)[b]};g.entries=function(){function b(b,t,I){for(;;)switch(a){case 0:z=F.get(J);h=[];m=z;for(f in m)h.push(f);r=0;case 1:if(!(r<h.length)){a=
3;break}f=h[r];if(f in m){a=4;break}a=2;break;case 4:e=w(z[f]),c=e.next();case 5:if(c.done){a=7;break}d=c.value;a=8;return{value:[f,A(d)],done:!1};case 8:if(1!=b){a=9;break}a=-1;throw I;case 9:case 6:c=e.next();a=5;break;case 7:case 2:r++;a=1;break;case 3:a=-1;default:return{value:void 0,done:!0}}}var a=0,d,c,e,f,m,r,h,z,J=this,t={next:function(a){return b(0,a,void 0)},"throw":function(a){return b(1,void 0,a)},"return":function(){throw Error("Not yet implemented");}};q();t[Symbol.iterator]=function(){return this};
return t};g.forEach=function(b,a){for(var d=w(this),c=d.next();!c.done;c=d.next()){var e=w(c.value);c=e.next().value;e=e.next().value;b.call(a,e,c,this)}};g.get=function(b){var a=F.get(this);return a[b]?A(a[b][0]):null};g.getAll=function(b){return(F.get(this)[b]||[]).map(A)};g.has=function(b){return b in F.get(this)};g.keys=function(){function b(b,h,t){for(;;)switch(a){case 0:m=w(r),f=m.next();case 1:if(f.done){a=3;break}e=f.value;c=w(e);d=c.next().value;a=4;return{value:d,done:!1};case 4:if(1!=b){a=
5;break}a=-1;throw t;case 5:case 2:f=m.next();a=1;break;case 3:a=-1;default:return{value:void 0,done:!0}}}var a=0,d,c,e,f,m,r=this,h={next:function(a){return b(0,a,void 0)},"throw":function(a){return b(1,void 0,a)},"return":function(){throw Error("Not yet implemented");}};q();h[Symbol.iterator]=function(){return this};return h};g.set=function(b,a,d){F.get(this)[b]=[[a,d]]};g.values=function(){function b(b,h,t){for(;;)switch(a){case 0:m=w(r),f=m.next();case 1:if(f.done){a=3;break}e=f.value;c=w(e);
c.next();d=c.next().value;a=4;return{value:d,done:!1};case 4:if(1!=b){a=5;break}a=-1;throw t;case 5:case 2:f=m.next();a=1;break;case 3:a=-1;default:return{value:void 0,done:!0}}}var a=0,d,c,e,f,m,r=this,h={next:function(a){return b(0,a,void 0)},"throw":function(a){return b(1,void 0,a)},"return":function(){throw Error("Not yet implemented");}};q();h[Symbol.iterator]=function(){return this};return h};H.prototype._asNative=function(){for(var b=new B,a=w(this),d=a.next();!d.done;d=a.next()){var c=w(d.value);
d=c.next().value;c=c.next().value;b.append(d,c)}return b};H.prototype._blob=function(){for(var b="----formdata-polyfill-"+Math.random(),a=[],d=w(this),c=d.next();!c.done;c=d.next()){var e=w(c.value);c=e.next().value;e=e.next().value;a.push("--"+b+"\r\n");e instanceof Blob?a.push('Content-Disposition: form-data; name="'+c+'"; filename="'+e.name+'"\r\n',"Content-Type: "+(e.type||"application/octet-stream")+"\r\n\r\n",e,"\r\n"):a.push('Content-Disposition: form-data; name="'+c+'"\r\n\r\n'+e+"\r\n")}a.push("--"+
b+"--");return new Blob(a,{type:"multipart/form-data; boundary="+b})};n();q();H.prototype[Symbol.iterator]=function(){return this.entries()};H.prototype.toString=function(){return"[object FormData]"};E&&(H.prototype[E]="FormData");[["append",x],["delete",y],["get",y],["getAll",y],["has",y],["set",x]].forEach(function(b){var a=H.prototype[b[0]];H.prototype[method]=function(){return a.apply(this,b[1](G(arguments)))}});XMLHttpRequest.prototype.send=function(b){C.call(this,b instanceof H?b._blob():b)};
if(D){var K=window.fetch;window.fetch=function(b,a){a&&a.body&&a.body instanceof H&&(a.body=a.body._blob());return K(b,a)}}window.FormData=H};

Could you test if it works before i publish?

Aebrathia commented 6 years ago

Edge throws "Exception thrown and not caught" in the code below. I cleaned and extracted the obfuscated code to show you:

function q() {
    n();
    var b = l.Symbol.iterator;
    b || (b = l.Symbol.iterator = l.Symbol("iterator"));
    /* line below throws error */
    "function" != typeof Array.prototype[b] && k(Array.prototype, b, {
        configurable: !0, writable: !0, value: function () {
            return u(this);
        }
    });
    q = function () {
    };
}

I'm trying other compilation settings on google-closure-compiler --compilation_level ADVANCED --js_output_file formdata.min.js FormData.js. I believe --compilation_level SIMPLE might work, but the output might be larger.

Also I found annotations available for google-closure-compiler which might tell him to keep the methods in the output.

I will keep trying tomorrow.

jimmywarting commented 6 years ago

I switched back to using closure-compiler.appspot.com using theres api

mmiklosik commented 6 years ago

When i updated formdata.min.js, i have now error "method" is undefined

E && (H.prototype[E] = "FormData");
  [
    ["append", x],
    ["delete", y],
    ["get", y],
    ["getAll", y],
    ["has", y],
    ["set", x]
  ].forEach(function(b) {
    var a = H.prototype[b[0]];
    H.prototype[method] = function() {
      return a.apply(this, b[1](G(arguments)));
    };
  });
jimmywarting commented 6 years ago

Doh my mistake. Forgot that method. Should be b[0]

jimmywarting commented 6 years ago

also forgot that it should be apply, think it's fixed now, published v3.0.4

Aebrathia commented 6 years ago

Yay all good, thanks