UNPKG

next-gravity-forms

Version:

A component to take GraphQl Gravity Forms query data from WordPress and return a usable form.

3 lines (2 loc) 4.49 kB
import"node:fs";import"node:path";import{F as e,a as t}from"./index-BI3UQJFe.js";import"node:http";import"node:https";import"node:zlib";import"node:stream";import"node:buffer";import"node:util";import"node:url";import"node:net";import"./query.js";let n=0;const r={START_BOUNDARY:n++,HEADER_FIELD_START:n++,HEADER_FIELD:n++,HEADER_VALUE_START:n++,HEADER_VALUE:n++,HEADER_VALUE_ALMOST_DONE:n++,HEADERS_ALMOST_DONE:n++,PART_DATA_START:n++,PART_DATA:n++,END:n++};let a=1;const o=a,i=a*=2,s=e=>32|e,d=()=>{};class E{constructor(e){this.index=0,this.flags=0,this.onHeaderEnd=d,this.onHeaderField=d,this.onHeadersEnd=d,this.onHeaderValue=d,this.onPartBegin=d,this.onPartData=d,this.onPartEnd=d,this.boundaryChars={},e="\r\n--"+e;const t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n),this.boundaryChars[t[n]]=!0;this.boundary=t,this.lookbehind=new Uint8Array(this.boundary.length+8),this.state=r.START_BOUNDARY}write(e){let t=0;const n=e.length;let a=this.index,{lookbehind:d,boundary:E,boundaryChars:A,index:h,state:l,flags:D}=this;const c=this.boundary.length,f=c-1,T=e.length;let _,R;const u=e=>{this[e+"Mark"]=t},H=e=>{delete this[e+"Mark"]},b=(e,t,n,r)=>{void 0!==t&&t===n||this[e](r&&r.subarray(t,n))},p=(n,r)=>{const a=n+"Mark";a in this&&(r?(b(n,this[a],t,e),delete this[a]):(b(n,this[a],e.length,e),this[a]=0))};for(t=0;t<n;t++)switch(_=e[t],l){case r.START_BOUNDARY:if(h===E.length-2){if(45===_)D|=i;else if(13!==_)return;h++;break}if(h-1==E.length-2){if(D&i&&45===_)l=r.END,D=0;else{if(D&i||10!==_)return;h=0,b("onPartBegin"),l=r.HEADER_FIELD_START}break}_!==E[h+2]&&(h=-2),_===E[h+2]&&h++;break;case r.HEADER_FIELD_START:l=r.HEADER_FIELD,u("onHeaderField"),h=0;case r.HEADER_FIELD:if(13===_){H("onHeaderField"),l=r.HEADERS_ALMOST_DONE;break}if(h++,45===_)break;if(58===_){if(1===h)return;p("onHeaderField",!0),l=r.HEADER_VALUE_START;break}if(R=s(_),R<97||R>122)return;break;case r.HEADER_VALUE_START:if(32===_)break;u("onHeaderValue"),l=r.HEADER_VALUE;case r.HEADER_VALUE:13===_&&(p("onHeaderValue",!0),b("onHeaderEnd"),l=r.HEADER_VALUE_ALMOST_DONE);break;case r.HEADER_VALUE_ALMOST_DONE:if(10!==_)return;l=r.HEADER_FIELD_START;break;case r.HEADERS_ALMOST_DONE:if(10!==_)return;b("onHeadersEnd"),l=r.PART_DATA_START;break;case r.PART_DATA_START:l=r.PART_DATA,u("onPartData");case r.PART_DATA:if(a=h,0===h){for(t+=f;t<T&&!(e[t]in A);)t+=c;t-=f,_=e[t]}if(h<E.length)E[h]===_?(0===h&&p("onPartData",!0),h++):h=0;else if(h===E.length)h++,13===_?D|=o:45===_?D|=i:h=0;else if(h-1===E.length)if(D&o){if(h=0,10===_){D&=~o,b("onPartEnd"),b("onPartBegin"),l=r.HEADER_FIELD_START;break}}else D&i&&45===_?(b("onPartEnd"),l=r.END,D=0):h=0;if(h>0)d[h-1]=_;else if(a>0){const e=new Uint8Array(d.buffer,d.byteOffset,d.byteLength);b("onPartData",0,a,e),a=0,u("onPartData"),t--}break;case r.END:break;default:throw new Error(`Unexpected state entered: ${l}`)}p("onHeaderField"),p("onHeaderValue"),p("onPartData"),this.index=h,this.state=l,this.flags=D}end(){if(this.state===r.HEADER_FIELD_START&&0===this.index||this.state===r.PART_DATA&&this.index===this.boundary.length)this.onPartEnd();else if(this.state!==r.END)throw new Error("MultipartParser.end(): stream ended unexpectedly")}}async function A(n,r){if(!/multipart/i.test(r))throw new TypeError("Failed to fetch");const a=r.match(/boundary=(?:"([^"]+)"|([^;]+))/i);if(!a)throw new TypeError("no or bad content-type header, no multipart boundary");const o=new E(a[1]||a[2]);let i,s,d,A,h,l;const D=[],c=new e,f=e=>{d+=u.decode(e,{stream:!0})},T=e=>{D.push(e)},_=()=>{const e=new t(D,l,{type:h});c.append(A,e)},R=()=>{c.append(A,d)},u=new TextDecoder("utf-8");u.decode(),o.onPartBegin=function(){o.onPartData=f,o.onPartEnd=R,i="",s="",d="",A="",h="",l=null,D.length=0},o.onHeaderField=function(e){i+=u.decode(e,{stream:!0})},o.onHeaderValue=function(e){s+=u.decode(e,{stream:!0})},o.onHeaderEnd=function(){if(s+=u.decode(),i=i.toLowerCase(),"content-disposition"===i){const e=s.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);e&&(A=e[2]||e[3]||""),l=function(e){const t=e.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);if(!t)return;const n=t[2]||t[3]||"";let r=n.slice(n.lastIndexOf("\\")+1);return r=r.replace(/%22/g,'"'),r=r.replace(/&#(\d{4});/g,((e,t)=>String.fromCharCode(t))),r}(s),l&&(o.onPartData=T,o.onPartEnd=_)}else"content-type"===i&&(h=s);s="",i=""};for await(const e of n)o.write(e);return o.end(),c}export{A as toFormData}; //# sourceMappingURL=multipart-parser-BSDixSoX.js.map