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.54 kB
"use strict";require("node:fs"),require("node:path");var e=require("./index-vqBVR3Ze.js");require("node:http"),require("node:https"),require("node:zlib"),require("node:stream"),require("node:buffer"),require("node:util"),require("node:url"),require("node:net"),require("./query.js");let t=0;const n={START_BOUNDARY:t++,HEADER_FIELD_START:t++,HEADER_FIELD:t++,HEADER_VALUE_START:t++,HEADER_VALUE:t++,HEADER_VALUE_ALMOST_DONE:t++,HEADERS_ALMOST_DONE:t++,PART_DATA_START:t++,PART_DATA:t++,END:t++};let r=1;const a=r,i=r*=2,o=e=>32|e,s=()=>{};class d{constructor(e){this.index=0,this.flags=0,this.onHeaderEnd=s,this.onHeaderField=s,this.onHeadersEnd=s,this.onHeaderValue=s,this.onPartBegin=s,this.onPartData=s,this.onPartEnd=s,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=n.START_BOUNDARY}write(e){let t=0;const r=e.length;let s=this.index,{lookbehind:d,boundary:E,boundaryChars:A,index:h,state:l,flags:D}=this;const u=this.boundary.length,c=u-1,f=e.length;let T,_;const R=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))},L=(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<r;t++)switch(T=e[t],l){case n.START_BOUNDARY:if(h===E.length-2){if(45===T)D|=i;else if(13!==T)return;h++;break}if(h-1==E.length-2){if(D&i&&45===T)l=n.END,D=0;else{if(D&i||10!==T)return;h=0,b("onPartBegin"),l=n.HEADER_FIELD_START}break}T!==E[h+2]&&(h=-2),T===E[h+2]&&h++;break;case n.HEADER_FIELD_START:l=n.HEADER_FIELD,R("onHeaderField"),h=0;case n.HEADER_FIELD:if(13===T){H("onHeaderField"),l=n.HEADERS_ALMOST_DONE;break}if(h++,45===T)break;if(58===T){if(1===h)return;L("onHeaderField",!0),l=n.HEADER_VALUE_START;break}if(_=o(T),_<97||_>122)return;break;case n.HEADER_VALUE_START:if(32===T)break;R("onHeaderValue"),l=n.HEADER_VALUE;case n.HEADER_VALUE:13===T&&(L("onHeaderValue",!0),b("onHeaderEnd"),l=n.HEADER_VALUE_ALMOST_DONE);break;case n.HEADER_VALUE_ALMOST_DONE:if(10!==T)return;l=n.HEADER_FIELD_START;break;case n.HEADERS_ALMOST_DONE:if(10!==T)return;b("onHeadersEnd"),l=n.PART_DATA_START;break;case n.PART_DATA_START:l=n.PART_DATA,R("onPartData");case n.PART_DATA:if(s=h,0===h){for(t+=c;t<f&&!(e[t]in A);)t+=u;t-=c,T=e[t]}if(h<E.length)E[h]===T?(0===h&&L("onPartData",!0),h++):h=0;else if(h===E.length)h++,13===T?D|=a:45===T?D|=i:h=0;else if(h-1===E.length)if(D&a){if(h=0,10===T){D&=~a,b("onPartEnd"),b("onPartBegin"),l=n.HEADER_FIELD_START;break}}else D&i&&45===T?(b("onPartEnd"),l=n.END,D=0):h=0;if(h>0)d[h-1]=T;else if(s>0){const e=new Uint8Array(d.buffer,d.byteOffset,d.byteLength);b("onPartData",0,s,e),s=0,R("onPartData"),t--}break;case n.END:break;default:throw new Error(`Unexpected state entered: ${l}`)}L("onHeaderField"),L("onHeaderValue"),L("onPartData"),this.index=h,this.state=l,this.flags=D}end(){if(this.state===n.HEADER_FIELD_START&&0===this.index||this.state===n.PART_DATA&&this.index===this.boundary.length)this.onPartEnd();else if(this.state!==n.END)throw new Error("MultipartParser.end(): stream ended unexpectedly")}}exports.toFormData=async function(t,n){if(!/multipart/i.test(n))throw new TypeError("Failed to fetch");const r=n.match(/boundary=(?:"([^"]+)"|([^;]+))/i);if(!r)throw new TypeError("no or bad content-type header, no multipart boundary");const a=new d(r[1]||r[2]);let i,o,s,E,A,h;const l=[],D=new e.FormData,u=e=>{s+=_.decode(e,{stream:!0})},c=e=>{l.push(e)},f=()=>{const t=new e.File(l,h,{type:A});D.append(E,t)},T=()=>{D.append(E,s)},_=new TextDecoder("utf-8");_.decode(),a.onPartBegin=function(){a.onPartData=u,a.onPartEnd=T,i="",o="",s="",E="",A="",h=null,l.length=0},a.onHeaderField=function(e){i+=_.decode(e,{stream:!0})},a.onHeaderValue=function(e){o+=_.decode(e,{stream:!0})},a.onHeaderEnd=function(){if(o+=_.decode(),i=i.toLowerCase(),"content-disposition"===i){const e=o.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);e&&(E=e[2]||e[3]||""),h=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}(o),h&&(a.onPartData=c,a.onPartEnd=f)}else"content-type"===i&&(A=o);o="",i=""};for await(const e of t)a.write(e);return a.end(),D}; //# sourceMappingURL=multipart-parser-CTwrziuO.js.map