+--`+a;const n=new Uint8Array(a.length);for(let r=0;r<a.length;r++)n[r]=a.charCodeAt(r),this.boundaryChars[n[r]]=!0;this.boundary=n,this.lookbehind=new Uint8Array(this.boundary.length+8),this.state=t.START_BOUNDARY}write(a){let n=0;const r=a.length;let E=this.index,{lookbehind:l,boundary:d,boundaryChars:p,index:e,state:i,flags:A}=this;const m=this.boundary.length,S=m-1,N=a.length;let o,b;const h=c=>{this[c+"Mark"]=n},s=c=>{delete this[c+"Mark"]},D=(c,L,T,y)=>{(L===void 0||L!==T)&&this[c](y&&y.subarray(L,T))},H=(c,L)=>{const T=c+"Mark";T in this&&(L?(D(c,this[T],n,a),delete this[T]):(D(c,this[T],a.length,a),this[T]=0))};for(n=0;n<r;n++)switch(o=a[n],i){case t.START_BOUNDARY:if(e===d.length-2){if(o===P)A|=_.LAST_BOUNDARY;else if(o!==g)return;e++;break}else if(e-1===d.length-2){if(A&_.LAST_BOUNDARY&&o===P)i=t.END,A=0;else if(!(A&_.LAST_BOUNDARY)&&o===O)e=0,D("onPartBegin"),i=t.HEADER_FIELD_START;else return;break}o!==d[e+2]&&(e=-2),o===d[e+2]&&e++;break;case t.HEADER_FIELD_START:i=t.HEADER_FIELD,h("onHeaderField"),e=0;case t.HEADER_FIELD:if(o===g){s("onHeaderField"),i=t.HEADERS_ALMOST_DONE;break}if(e++,o===P)break;if(o===B){if(e===1)return;H("onHeaderField",!0),i=t.HEADER_VALUE_START;break}if(b=x(o),b<V||b>Y)return;break;case t.HEADER_VALUE_START:if(o===w)break;h("onHeaderValue"),i=t.HEADER_VALUE;case t.HEADER_VALUE:o===g&&(H("onHeaderValue",!0),D("onHeaderEnd"),i=t.HEADER_VALUE_ALMOST_DONE);break;case t.HEADER_VALUE_ALMOST_DONE:if(o!==O)return;i=t.HEADER_FIELD_START;break;case t.HEADERS_ALMOST_DONE:if(o!==O)return;D("onHeadersEnd"),i=t.PART_DATA_START;break;case t.PART_DATA_START:i=t.PART_DATA,h("onPartData");case t.PART_DATA:if(E=e,e===0){for(n+=S;n<N&&!(a[n]in p);)n+=m;n-=S,o=a[n]}if(e<d.length)d[e]===o?(e===0&&H("onPartData",!0),e++):e=0;else if(e===d.length)e++,o===g?A|=_.PART_BOUNDARY:o===P?A|=_.LAST_BOUNDARY:e=0;else if(e-1===d.length)if(A&_.PART_BOUNDARY){if(e=0,o===O){A&=~_.PART_BOUNDARY,D("onPartEnd"),D("onPartBegin"),i=t.HEADER_FIELD_START;break}}else A&_.LAST_BOUNDARY&&o===P?(D("onPartEnd"),i=t.END,A=0):e=0;if(e>0)l[e-1]=o;else if(E>0){const c=new Uint8Array(l.buffer,l.byteOffset,l.byteLength);D("onPartData",0,E,c),E=0,h("onPartData"),n--}break;case t.END:break;default:throw new Error(`Unexpected state entered: ${i}`)}H("onHeaderField"),H("onHeaderValue"),H("onPartData"),this.index=e,this.state=i,this.flags=A}end(){if(this.state===t.HEADER_FIELD_START&&this.index===0||this.state===t.PART_DATA&&this.index===this.boundary.length)this.onPartEnd();else if(this.state!==t.END)throw new Error("MultipartParser.end(): stream ended unexpectedly")}}function I(u){const a=u.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);if(!a)return;const n=a[2]||a[3]||"";let r=n.slice(n.lastIndexOf("\\")+1);return r=r.replace(/%22/g,'"'),r=r.replace(/&#(\d{4});/g,(E,l)=>String.fromCharCode(l)),r}async function M(u,a){if(!/multipart/i.test(a))throw new TypeError("Failed to fetch");const n=a.match(/boundary=(?:"([^"]+)"|([^;]+))/i);if(!n)throw new TypeError("no or bad content-type header, no multipart boundary");const r=new C(n[1]||n[2]);let E,l,d,p,e,i;const A=[],m=new k,S=s=>{d+=h.decode(s,{stream:!0})},N=s=>{A.push(s)},o=()=>{const s=new U(A,i,{type:e});m.append(p,s)},b=()=>{m.append(p,d)},h=new TextDecoder("utf-8");h.decode(),r.onPartBegin=function(){r.onPartData=S,r.onPartEnd=b,E="",l="",d="",p="",e="",i=null,A.length=0},r.onHeaderField=function(s){E+=h.decode(s,{stream:!0})},r.onHeaderValue=function(s){l+=h.decode(s,{stream:!0})},r.onHeaderEnd=function(){if(l+=h.decode(),E=E.toLowerCase(),E==="content-disposition"){const s=l.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);s&&(p=s[2]||s[3]||""),i=I(l),i&&(r.onPartData=N,r.onPartEnd=o)}else E==="content-type"&&(e=l);l="",E=""};for await(const s of u)r.write(s);return r.end(),m}export{M as toFormData};
0 commit comments