@php-wasm/universal 3.0.21 → 3.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -1
- package/index.cjs.map +1 -1
- package/index.js +8 -1
- package/index.js.map +1 -1
- package/package.json +7 -7
package/index.cjs
CHANGED
|
@@ -86,7 +86,7 @@ ${JSON.stringify(t.headers,null,2)}
|
|
|
86
86
|
`)),n.push(`\r
|
|
87
87
|
`),l instanceof File?n.push(await fileToUint8Array(l)):n.push(l),n.push(`\r
|
|
88
88
|
`);n.push(`--${e}--\r
|
|
89
|
-
`);const i=n.reduce((c,l)=>c+l.length,0),o=new Uint8Array(i);let a=0;for(const c of n)o.set(typeof c=="string"?s.encode(c):c,a),a+=c.length;return{bytes:o,contentType:r}}function fileToUint8Array(t){return t.arrayBuffer().then(e=>new Uint8Array(e))}const _default="application/octet-stream",asx="video/x-ms-asf",atom="application/atom+xml",avi="video/x-msvideo",avif="image/avif",bin="application/octet-stream",bmp="image/x-ms-bmp",cco="application/x-cocoa",css="text/css",data="application/octet-stream",deb="application/octet-stream",der="application/x-x509-ca-cert",dmg="application/octet-stream",doc="application/msword",docx="application/vnd.openxmlformats-officedocument.wordprocessingml.document",eot="application/vnd.ms-fontobject",flv="video/x-flv",gif="image/gif",gz="application/gzip",hqx="application/mac-binhex40",htc="text/x-component",html="text/html",ico="image/x-icon",iso="application/octet-stream",jad="text/vnd.sun.j2me.app-descriptor",jar="application/java-archive",jardiff="application/x-java-archive-diff",jng="image/x-jng",jnlp="application/x-java-jnlp-file",jpg="image/jpeg",jpeg="image/jpeg",js="application/javascript",json="application/json",kml="application/vnd.google-earth.kml+xml",kmz="application/vnd.google-earth.kmz",m3u8="application/vnd.apple.mpegurl",m4a="audio/x-m4a",m4v="video/x-m4v",md="text/plain",mid="audio/midi",mml="text/mathml",mng="video/x-mng",mov="video/quicktime",mp3="audio/mpeg",mp4="video/mp4",mpeg="video/mpeg",msi="application/octet-stream",odg="application/vnd.oasis.opendocument.graphics",odp="application/vnd.oasis.opendocument.presentation",ods="application/vnd.oasis.opendocument.spreadsheet",odt="application/vnd.oasis.opendocument.text",ogg="audio/ogg",otf="font/otf",pdf="application/pdf",pl="application/x-perl",png="image/png",ppt="application/vnd.ms-powerpoint",pptx="application/vnd.openxmlformats-officedocument.presentationml.presentation",prc="application/x-pilot",ps="application/postscript",ra="audio/x-realaudio",rar="application/x-rar-compressed",rpm="application/x-redhat-package-manager",rss="application/rss+xml",rtf="application/rtf",run="application/x-makeself",sea="application/x-sea",sit="application/x-stuffit",svg="image/svg+xml",swf="application/x-shockwave-flash",tcl="application/x-tcl",tar="application/x-tar",tif="image/tiff",ts="video/mp2t",ttf="font/ttf",txt="text/plain",wasm="application/wasm",wbmp="image/vnd.wap.wbmp",webm="video/webm",webp="image/webp",wml="text/vnd.wap.wml",wmlc="application/vnd.wap.wmlc",wmv="video/x-ms-wmv",woff="font/woff",woff2="font/woff2",xhtml="application/xhtml+xml",xls="application/vnd.ms-excel",xlsx="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",xml="text/xml",xpi="application/x-xpinstall",xspf="application/xspf+xml",zip="application/zip",mimeTypes={_default,"3gpp":"video/3gpp","7z":"application/x-7z-compressed",asx,atom,avi,avif,bin,bmp,cco,css,data,deb,der,dmg,doc,docx,eot,flv,gif,gz,hqx,htc,html,ico,iso,jad,jar,jardiff,jng,jnlp,jpg,jpeg,js,json,kml,kmz,m3u8,m4a,m4v,md,mid,mml,mng,mov,mp3,mp4,mpeg,msi,odg,odp,ods,odt,ogg,otf,pdf,pl,png,ppt,pptx,prc,ps,ra,rar,rpm,rss,rtf,run,sea,sit,svg,swf,tcl,tar,tif,ts,ttf,txt,wasm,wbmp,webm,webp,wml,wmlc,wmv,woff,woff2,xhtml,xls,xlsx,xml,xpi,xspf,zip};var P,M,L,C,A,x,N,R,E,ee,te,re,se;class PHPRequestHandler{constructor(e){y(this,E);y(this,P);y(this,M);y(this,L);y(this,C);y(this,A);y(this,x);y(this,N);y(this,R);const{documentRoot:r="/www/",absoluteUrl:s=typeof location=="object"?location.href:DEFAULT_BASE_URL,rewriteRules:n=[],getFileNotFoundAction:i=()=>({type:"404"})}=e;"processManager"in e?this.processManager=e.processManager:this.processManager=new PHPProcessManager({phpFactory:async c=>{const l=await e.phpFactory({...c,requestHandler:this});return l.isDir(r)||l.mkdir(r),l.chdir(r),l.requestHandler=this,l},maxPhpInstances:e.maxPhpInstances}),w(this,R,e.cookieStore===void 0?new HttpCookieStore:e.cookieStore),w(this,P,r);const o=new URL(s);w(this,L,o.hostname),w(this,C,o.port?Number(o.port):o.protocol==="https:"?443:80),w(this,M,(o.protocol||"").replace(":",""));const a=u(this,C)!==443&&u(this,C)!==80;w(this,A,[u(this,L),a?`:${u(this,C)}`:""].join("")),w(this,x,o.pathname.replace(/\/+$/,"")),w(this,N,[`${u(this,M)}://`,u(this,A),u(this,x)].join("")),this.rewriteRules=n,this.getFileNotFoundAction=i}async getPrimaryPhp(){return await this.processManager.getPrimaryPhp()}pathToInternalUrl(e){return e.startsWith("/")||(e=`/${e}`),`${this.absoluteUrl}${e}`}internalUrlToPath(e){const r=new URL(e,"https://playground.internal");return r.pathname.startsWith(u(this,x))&&(r.pathname=r.pathname.slice(u(this,x).length)),toRelativeUrl(r)}get absoluteUrl(){return u(this,N)}get documentRoot(){return u(this,P)}async request(e){const r=URL.canParse(e.url),s=new URL(e.url.split("#")[0],r?void 0:DEFAULT_BASE_URL),n=f(this,E,ee).call(this,s),i=await this.getPrimaryPhp();let o=util.joinPaths(u(this,P),removePathPrefix(decodeURIComponent(n.pathname),u(this,x)));if(i.isDir(o)){if(!o.endsWith("/"))return new PHPResponse(301,{Location:[`${n.pathname}/`]},new Uint8Array(0));for(const a of["index.php","index.html"]){const c=util.joinPaths(o,a);if(i.isFile(c)){o=c,n.pathname=util.joinPaths(n.pathname,a);break}}}if(!i.isFile(o)){let a=n.pathname;for(;a.startsWith("/")&&a!==util.dirname(a);){a=util.dirname(a);const c=util.joinPaths(u(this,P),a);if(i.isFile(c)&&c.endsWith(".php")){o=util.joinPaths(u(this,P),a);break}}}if(!i.isFile(o)){const a=this.getFileNotFoundAction(n.pathname);switch(a.type){case"response":return a.response;case"internal-redirect":o=util.joinPaths(u(this,P),a.uri);break;case"404":return PHPResponse.forHttpCode(404);default:throw new Error(`Unsupported file-not-found action type: '${a.type}'`)}}if(i.isFile(o))if(o.endsWith(".php")){const a=await f(this,E,re).call(this,e,s,n,o);return a.ok()&&a.exitCode!==0?new PHPResponse(500,a.headers,a.bytes,a.errors,a.exitCode):a}else return f(this,E,te).call(this,i,o);else return PHPResponse.forHttpCode(404)}prepare_$_SERVER_superglobal(e,r,s){const n={REMOTE_ADDR:"127.0.0.1",DOCUMENT_ROOT:u(this,P),HTTPS:u(this,N).startsWith("https://")?"on":""};return n.REQUEST_URI=e.pathname+e.search,s.startsWith(u(this,P))&&(n.SCRIPT_NAME=s.substring(u(this,P).length),n.PHP_SELF=r.pathname,n.REQUEST_URI.startsWith(n.SCRIPT_NAME)&&(n.PATH_INFO=n.REQUEST_URI.substring(n.SCRIPT_NAME.length),n.PATH_INFO.includes("?")&&(n.PATH_INFO=n.PATH_INFO.substring(0,n.PATH_INFO.indexOf("?"))))),n.QUERY_STRING=r.search.substring(1),n}async[Symbol.asyncDispose](){await this.processManager[Symbol.asyncDispose]()}}P=new WeakMap,M=new WeakMap,L=new WeakMap,C=new WeakMap,A=new WeakMap,x=new WeakMap,N=new WeakMap,R=new WeakMap,E=new WeakSet,ee=function(e){const r=removePathPrefix(decodeURIComponent(e.pathname),u(this,x)),s=applyRewriteRules(r,this.rewriteRules),n=new URL(util.joinPaths(u(this,x),s),e.toString());for(const[i,o]of e.searchParams.entries())n.searchParams.append(i,o);return n},te=function(e,r){const s=e.readFileAsBuffer(r);return new PHPResponse(200,{"content-length":[`${s.byteLength}`],"content-type":[inferMimeType(r)],"accept-ranges":["bytes"],"cache-control":["public, max-age=0"]},s)},re=async function(e,r,s,n){let i;try{i=await this.processManager.acquirePHPInstance({considerPrimary:!0})}catch(o){return o instanceof MaxPhpInstancesError?PHPResponse.forHttpCode(502):PHPResponse.forHttpCode(500)}try{return await f(this,E,se).call(this,i.php,e,r,s,n)}finally{i.reap()}},se=async function(e,r,s,n,i){let o="GET";const a={host:u(this,A),...normalizeHeaders(r.headers||{})};u(this,R)&&(a.cookie=u(this,R).getCookieRequestHeader());let c=r.body;if(typeof c=="object"&&!(c instanceof Uint8Array)){o="POST";const{bytes:l,contentType:p}=await encodeAsMultipart(c);c=l,a["content-type"]=p}try{const l=await e.run({relativeUri:ensurePathPrefix(toRelativeUrl(new URL(n.toString())),u(this,x)),protocol:u(this,M),method:r.method||o,$_SERVER:this.prepare_$_SERVER_superglobal(s,n,i),body:c,scriptPath:i,headers:a});return u(this,R)&&u(this,R).rememberCookiesFromResponseHeaders(l.headers),l}catch(l){const p=l;if(p!=null&&p.response)return p.response;throw l}};function inferMimeType(t){const e=t.split(".").pop();return mimeTypes[e]||mimeTypes._default}function applyRewriteRules(t,e){for(const r of e)if(new RegExp(r.match).test(t)){t=t.replace(r.match,r.replacement);break}return t}function rotatePHPRuntime({php:t,recreateRuntime:e,maxRequests:r=400}){return t.enableRuntimeRotation({recreateRuntime:e,maxRequests:r})}async function writeFiles(t,e,r,{rmRoot:s=!1}={}){s&&await t.isDir(e)&&await t.rmdir(e,{recursive:!0});for(const[n,i]of Object.entries(r)){const o=util.joinPaths(e,n);await t.fileExists(util.dirname(o))||await t.mkdir(util.dirname(o)),i instanceof Uint8Array||typeof i=="string"?await t.writeFile(o,i):await writeFiles(t,o,i)}}function proxyFileSystem(t,e,r){const s=Object.getOwnPropertySymbols(t)[0];for(const n of r)e.fileExists(n)||e.mkdir(n),t.fileExists(n)||t.mkdir(n),e[s].FS.mount(e[s].PROXYFS,{root:n,fs:t[s].FS},n)}function isPathToSharedFS(t,e){var i;const r=Object.getOwnPropertySymbols(t)[0],n=t[r].FS.lookupPath(e,{noent_okay:!0});return((i=n==null?void 0:n.node)==null?void 0:i.isSharedFS)??!1}function sandboxedSpawnHandlerFactory(t){return util.createSpawnHandler(async function(e,r,s){r.notifySpawn(),e[0]==="exec"&&e.shift(),(e[0].endsWith(".php")||e[0].endsWith(".phar"))&&e.unshift("php");const n=e[0].split("/").pop();if(e[0]==="/usr/bin/env"&&e[1]==="stty"&&e[2]==="size")r.stdout("18 140"),r.exit(0);else if(n==="tput"&&e[1]==="cols")r.stdout("140"),r.exit(0);else if(n==="less"){r.on("stdin",a=>{r.stdout(a)}),await new Promise(a=>{r.childProcess.stdin.on("finish",()=>{a(!0)})}),r.exit(0);return}if(!["php","ls","pwd"].includes(n??"")){r.exit(127);return}const{php:i,reap:o}=await t.acquirePHPInstance({considerPrimary:!1});try{s.cwd&&i.chdir(s.cwd);const a=i.cwd();switch(n){case"php":{const c=await i.cli(e,{env:{...s.env,SCRIPT_PATH:e[1],SHELL_PIPE:"0"}});c.stdout.pipeTo(new WritableStream({write(l){r.stdout(l)}})),c.stderr.pipeTo(new WritableStream({write(l){r.stderr(l)}})),r.exit(await c.exitCode);break}case"ls":{const c=i.listFiles(e[1]??a);for(const l of c)r.stdout(l+`
|
|
89
|
+
`);const i=n.reduce((c,l)=>c+l.length,0),o=new Uint8Array(i);let a=0;for(const c of n)o.set(typeof c=="string"?s.encode(c):c,a),a+=c.length;return{bytes:o,contentType:r}}function fileToUint8Array(t){return t.arrayBuffer().then(e=>new Uint8Array(e))}const _default="application/octet-stream",asx="video/x-ms-asf",atom="application/atom+xml",avi="video/x-msvideo",avif="image/avif",bin="application/octet-stream",bmp="image/x-ms-bmp",cco="application/x-cocoa",css="text/css",data="application/octet-stream",deb="application/octet-stream",der="application/x-x509-ca-cert",dmg="application/octet-stream",doc="application/msword",docx="application/vnd.openxmlformats-officedocument.wordprocessingml.document",eot="application/vnd.ms-fontobject",flv="video/x-flv",gif="image/gif",gz="application/gzip",hqx="application/mac-binhex40",htc="text/x-component",html="text/html",ico="image/x-icon",iso="application/octet-stream",jad="text/vnd.sun.j2me.app-descriptor",jar="application/java-archive",jardiff="application/x-java-archive-diff",jng="image/x-jng",jnlp="application/x-java-jnlp-file",jpg="image/jpeg",jpeg="image/jpeg",js="application/javascript",json="application/json",kml="application/vnd.google-earth.kml+xml",kmz="application/vnd.google-earth.kmz",m3u8="application/vnd.apple.mpegurl",m4a="audio/x-m4a",m4v="video/x-m4v",md="text/plain",mid="audio/midi",mml="text/mathml",mng="video/x-mng",mov="video/quicktime",mp3="audio/mpeg",mp4="video/mp4",mpeg="video/mpeg",msi="application/octet-stream",odg="application/vnd.oasis.opendocument.graphics",odp="application/vnd.oasis.opendocument.presentation",ods="application/vnd.oasis.opendocument.spreadsheet",odt="application/vnd.oasis.opendocument.text",ogg="audio/ogg",otf="font/otf",pdf="application/pdf",pl="application/x-perl",png="image/png",ppt="application/vnd.ms-powerpoint",pptx="application/vnd.openxmlformats-officedocument.presentationml.presentation",prc="application/x-pilot",ps="application/postscript",ra="audio/x-realaudio",rar="application/x-rar-compressed",rpm="application/x-redhat-package-manager",rss="application/rss+xml",rtf="application/rtf",run="application/x-makeself",sea="application/x-sea",sit="application/x-stuffit",svg="image/svg+xml",swf="application/x-shockwave-flash",tcl="application/x-tcl",tar="application/x-tar",tif="image/tiff",ts="video/mp2t",ttf="font/ttf",txt="text/plain",wasm="application/wasm",wbmp="image/vnd.wap.wbmp",webm="video/webm",webp="image/webp",wml="text/vnd.wap.wml",wmlc="application/vnd.wap.wmlc",wmv="video/x-ms-wmv",woff="font/woff",woff2="font/woff2",xhtml="application/xhtml+xml",xls="application/vnd.ms-excel",xlsx="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",xml="text/xml",xpi="application/x-xpinstall",xspf="application/xspf+xml",zip="application/zip",mimeTypes={_default,"3gpp":"video/3gpp","7z":"application/x-7z-compressed",asx,atom,avi,avif,bin,bmp,cco,css,data,deb,der,dmg,doc,docx,eot,flv,gif,gz,hqx,htc,html,ico,iso,jad,jar,jardiff,jng,jnlp,jpg,jpeg,js,json,kml,kmz,m3u8,m4a,m4v,md,mid,mml,mng,mov,mp3,mp4,mpeg,msi,odg,odp,ods,odt,ogg,otf,pdf,pl,png,ppt,pptx,prc,ps,ra,rar,rpm,rss,rtf,run,sea,sit,svg,swf,tcl,tar,tif,ts,ttf,txt,wasm,wbmp,webm,webp,wml,wmlc,wmv,woff,woff2,xhtml,xls,xlsx,xml,xpi,xspf,zip};var P,M,L,C,A,x,N,R,E,ee,te,re,se;class PHPRequestHandler{constructor(e){y(this,E);y(this,P);y(this,M);y(this,L);y(this,C);y(this,A);y(this,x);y(this,N);y(this,R);const{documentRoot:r="/www/",absoluteUrl:s=typeof location=="object"?location.href:DEFAULT_BASE_URL,rewriteRules:n=[],getFileNotFoundAction:i=()=>({type:"404"})}=e;"processManager"in e?this.processManager=e.processManager:this.processManager=new PHPProcessManager({phpFactory:async c=>{const l=await e.phpFactory({...c,requestHandler:this});return l.isDir(r)||l.mkdir(r),l.chdir(r),l.requestHandler=this,l},maxPhpInstances:e.maxPhpInstances}),w(this,R,e.cookieStore===void 0?new HttpCookieStore:e.cookieStore),w(this,P,r);const o=new URL(s);w(this,L,o.hostname),w(this,C,o.port?Number(o.port):o.protocol==="https:"?443:80),w(this,M,(o.protocol||"").replace(":",""));const a=u(this,C)!==443&&u(this,C)!==80;w(this,A,[u(this,L),a?`:${u(this,C)}`:""].join("")),w(this,x,o.pathname.replace(/\/+$/,"")),w(this,N,[`${u(this,M)}://`,u(this,A),u(this,x)].join("")),this.rewriteRules=n,this.getFileNotFoundAction=i}async getPrimaryPhp(){return await this.processManager.getPrimaryPhp()}pathToInternalUrl(e){return e.startsWith("/")||(e=`/${e}`),`${this.absoluteUrl}${e}`}internalUrlToPath(e){const r=new URL(e,"https://playground.internal");return r.pathname.startsWith(u(this,x))&&(r.pathname=r.pathname.slice(u(this,x).length)),toRelativeUrl(r)}get absoluteUrl(){return u(this,N)}get documentRoot(){return u(this,P)}async request(e){const r=looksLikeAbsoluteUrl(e.url),s=new URL(e.url.split("#")[0],r?void 0:DEFAULT_BASE_URL),n=f(this,E,ee).call(this,s),i=await this.getPrimaryPhp();let o=util.joinPaths(u(this,P),removePathPrefix(decodeURIComponent(n.pathname),u(this,x)));if(i.isDir(o)){if(!o.endsWith("/"))return new PHPResponse(301,{Location:[`${n.pathname}/`]},new Uint8Array(0));for(const a of["index.php","index.html"]){const c=util.joinPaths(o,a);if(i.isFile(c)){o=c,n.pathname=util.joinPaths(n.pathname,a);break}}}if(!i.isFile(o)){let a=n.pathname;for(;a.startsWith("/")&&a!==util.dirname(a);){a=util.dirname(a);const c=util.joinPaths(u(this,P),a);if(i.isFile(c)&&c.endsWith(".php")){o=util.joinPaths(u(this,P),a);break}}}if(!i.isFile(o)){const a=this.getFileNotFoundAction(n.pathname);switch(a.type){case"response":return a.response;case"internal-redirect":o=util.joinPaths(u(this,P),a.uri);break;case"404":return PHPResponse.forHttpCode(404);default:throw new Error(`Unsupported file-not-found action type: '${a.type}'`)}}if(i.isFile(o))if(o.endsWith(".php")){const a=await f(this,E,re).call(this,e,s,n,o);return a.ok()&&a.exitCode!==0?new PHPResponse(500,a.headers,a.bytes,a.errors,a.exitCode):a}else return f(this,E,te).call(this,i,o);else return PHPResponse.forHttpCode(404)}prepare_$_SERVER_superglobal(e,r,s){const n={REMOTE_ADDR:"127.0.0.1",DOCUMENT_ROOT:u(this,P),HTTPS:u(this,N).startsWith("https://")?"on":""};return n.REQUEST_URI=e.pathname+e.search,s.startsWith(u(this,P))&&(n.SCRIPT_NAME=s.substring(u(this,P).length),n.PHP_SELF=r.pathname,n.REQUEST_URI.startsWith(n.SCRIPT_NAME)&&(n.PATH_INFO=n.REQUEST_URI.substring(n.SCRIPT_NAME.length),n.PATH_INFO.includes("?")&&(n.PATH_INFO=n.PATH_INFO.substring(0,n.PATH_INFO.indexOf("?"))))),n.QUERY_STRING=r.search.substring(1),n}async[Symbol.asyncDispose](){await this.processManager[Symbol.asyncDispose]()}}P=new WeakMap,M=new WeakMap,L=new WeakMap,C=new WeakMap,A=new WeakMap,x=new WeakMap,N=new WeakMap,R=new WeakMap,E=new WeakSet,ee=function(e){const r=removePathPrefix(decodeURIComponent(e.pathname),u(this,x)),s=applyRewriteRules(r,this.rewriteRules),n=new URL(util.joinPaths(u(this,x),s),e.toString());for(const[i,o]of e.searchParams.entries())n.searchParams.append(i,o);return n},te=function(e,r){const s=e.readFileAsBuffer(r);return new PHPResponse(200,{"content-length":[`${s.byteLength}`],"content-type":[inferMimeType(r)],"accept-ranges":["bytes"],"cache-control":["public, max-age=0"]},s)},re=async function(e,r,s,n){let i;try{i=await this.processManager.acquirePHPInstance({considerPrimary:!0})}catch(o){return o instanceof MaxPhpInstancesError?PHPResponse.forHttpCode(502):PHPResponse.forHttpCode(500)}try{return await f(this,E,se).call(this,i.php,e,r,s,n)}finally{i.reap()}},se=async function(e,r,s,n,i){let o="GET";const a={host:u(this,A),...normalizeHeaders(r.headers||{})};u(this,R)&&(a.cookie=u(this,R).getCookieRequestHeader());let c=r.body;if(typeof c=="object"&&!(c instanceof Uint8Array)){o="POST";const{bytes:l,contentType:p}=await encodeAsMultipart(c);c=l,a["content-type"]=p}try{const l=await e.run({relativeUri:ensurePathPrefix(toRelativeUrl(new URL(n.toString())),u(this,x)),protocol:u(this,M),method:r.method||o,$_SERVER:this.prepare_$_SERVER_superglobal(s,n,i),body:c,scriptPath:i,headers:a});return u(this,R)&&u(this,R).rememberCookiesFromResponseHeaders(l.headers),l}catch(l){const p=l;if(p!=null&&p.response)return p.response;throw l}};function inferMimeType(t){const e=t.split(".").pop();return mimeTypes[e]||mimeTypes._default}function applyRewriteRules(t,e){for(const r of e)if(new RegExp(r.match).test(t)){t=t.replace(r.match,r.replacement);break}return t}function looksLikeAbsoluteUrl(t){try{return new URL(t),!0}catch{return!1}}function rotatePHPRuntime({php:t,recreateRuntime:e,maxRequests:r=400}){return t.enableRuntimeRotation({recreateRuntime:e,maxRequests:r})}async function writeFiles(t,e,r,{rmRoot:s=!1}={}){s&&await t.isDir(e)&&await t.rmdir(e,{recursive:!0});for(const[n,i]of Object.entries(r)){const o=util.joinPaths(e,n);await t.fileExists(util.dirname(o))||await t.mkdir(util.dirname(o)),i instanceof Uint8Array||typeof i=="string"?await t.writeFile(o,i):await writeFiles(t,o,i)}}function proxyFileSystem(t,e,r){const s=Object.getOwnPropertySymbols(t)[0];for(const n of r)e.fileExists(n)||e.mkdir(n),t.fileExists(n)||t.mkdir(n),e[s].FS.mount(e[s].PROXYFS,{root:n,fs:t[s].FS},n)}function isPathToSharedFS(t,e){var i;const r=Object.getOwnPropertySymbols(t)[0],n=t[r].FS.lookupPath(e,{noent_okay:!0});return((i=n==null?void 0:n.node)==null?void 0:i.isSharedFS)??!1}function sandboxedSpawnHandlerFactory(t){return util.createSpawnHandler(async function(e,r,s){r.notifySpawn(),e[0]==="exec"&&e.shift(),(e[0].endsWith(".php")||e[0].endsWith(".phar"))&&e.unshift("php");const n=e[0].split("/").pop();if(e[0]==="/usr/bin/env"&&e[1]==="stty"&&e[2]==="size")r.stdout("18 140"),r.exit(0);else if(n==="tput"&&e[1]==="cols")r.stdout("140"),r.exit(0);else if(n==="less"){r.on("stdin",a=>{r.stdout(a)}),await new Promise(a=>{r.childProcess.stdin.on("finish",()=>{a(!0)})}),r.exit(0);return}if(!["php","ls","pwd"].includes(n??"")){r.exit(127);return}const{php:i,reap:o}=await t.acquirePHPInstance({considerPrimary:!1});try{s.cwd&&i.chdir(s.cwd);const a=i.cwd();switch(n){case"php":{const c=await i.cli(e,{env:{...s.env,SCRIPT_PATH:e[1],SHELL_PIPE:"0"}});c.stdout.pipeTo(new WritableStream({write(l){r.stdout(l)}})),c.stderr.pipeTo(new WritableStream({write(l){r.stderr(l)}})),r.exit(await c.exitCode);break}case"ls":{const c=i.listFiles(e[1]??a);for(const l of c)r.stdout(l+`
|
|
90
90
|
`);await new Promise(l=>setTimeout(l,10)),r.exit(0);break}case"pwd":{r.stdout(a+`
|
|
91
91
|
`),await new Promise(c=>setTimeout(c,10)),r.exit(0);break}}}catch(a){throw r.exit(1),a}finally{o()}})}function exposeSync(t,e,r,s=["*"]){return expose(t,e,s,r.afterResponseSent)}function createSyncProxy(t,e=[],r){return new Proxy(()=>{},{get(s,n){return n==="then"&&!e.length?{then:(i,o)=>o(createSyncProxy(t,[],r))}:createSyncProxy(t,[...e,n],r)},set(s,n,i){const[o,a]=toWireValue(i);return r.send(t,{type:MessageType.SET,path:[...e,n].map(String),value:o},a),!0},apply(s,n,i){if(e.at(-1)==="bind")return createSyncProxy(t,e.slice(0,-1),r);const[a,c]=processArguments(i),l=r.send(t,{type:MessageType.APPLY,path:e.map(String),argumentList:a},c);return fromWireValue(l)},construct(s,n){const[i,o]=processArguments(n),a=r.send(t,{type:MessageType.CONSTRUCT,path:e.map(String),argumentList:i},o);return fromWireValue(a)}})}function wrapSync(t,e){return createSyncProxy(t,[],e)}class NodeSABSyncReceiveMessageTransport{static async create(){if(!NodeSABSyncReceiveMessageTransport.receiveMessageOnPort)try{NodeSABSyncReceiveMessageTransport.receiveMessageOnPort=require("worker_threads").receiveMessageOnPort}catch{NodeSABSyncReceiveMessageTransport.receiveMessageOnPort=await import("worker_threads").then(e=>e.receiveMessageOnPort)}return new NodeSABSyncReceiveMessageTransport}constructor(){}afterResponseSent(e){const{notifyBuffer:r}=e.data;if(r){const s=new Int32Array(r);s[0]=1,Atomics.notify(s,0)}}send(e,r,s){var l;const n=new SharedArrayBuffer(4),i=new Int32Array(n);i[0]=0;const o=generateUUID();if(e.postMessage({...r,id:o,notifyBuffer:n},s),Atomics.wait(i,0,0,5e3)==="timed-out")throw new Error("Timeout waiting for response");for(;;){const p=NodeSABSyncReceiveMessageTransport.receiveMessageOnPort(e);if(((l=p.message)==null?void 0:l.id)===o)return p.message;if(!p)throw new Error("No response received")}}}/**
|
|
92
92
|
* Original, unmodified Comlink library from Google:
|