@php-wasm/universal 3.1.38 → 3.1.40
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 +123 -81
- package/index.js.map +1 -1
- package/package.json +7 -7
package/index.cjs
CHANGED
|
@@ -54,7 +54,7 @@ ${bold} WASM ERROR${reset}${redBg}`);for(const e of r.split(`
|
|
|
54
54
|
${t.text}
|
|
55
55
|
|
|
56
56
|
=== Stderr ===
|
|
57
|
-
${t.errors}`,t,"request");return t}async runStream(r){const e=await this.semaphore.acquire();let t;const s=_(this,g,me).call(this,async()=>{if(p(this,V)||(await this[__private__dont__use].ccall("php_wasm_init",null,[],[],{isAsync:!0}),E(this,V,!0)),r.scriptPath&&!this.fileExists(r.scriptPath))throw new Error(`The script path "${r.scriptPath}" does not exist.`);_(this,g,_e).call(this,r.relativeUri||""),_(this,g,Se).call(this,r.method||"GET");const o=normalizeHeaders(r.headers||{}),i=o.host||"example.com:443",a=_(this,g,Ee).call(this,i,r.protocol||"http");if(_(this,g,ve).call(this,i),_(this,g,xe).call(this,a),_(this,g,Fe).call(this,o),r.body&&(t=_(this,g,ke).call(this,r.body)),typeof r.code=="string")this.writeFile("/internal/eval.php",r.code),_(this,g,fe).call(this,"/internal/eval.php");else if(typeof r.scriptPath=="string")_(this,g,fe).call(this,r.scriptPath||"");else throw new TypeError("The request object must have either a `code` or a `scriptPath` property.");const l=_(this,g,we).call(this,r.$_SERVER,o,a);for(const u in l)_(this,g,Re).call(this,u,l[u]);const c=r.env||{};for(const u in c)_(this,g,he).call(this,u,c[u]);return await this[__private__dont__use].ccall("wasm_sapi_handle_request",NUMBER,[],[],{async:!0})}),n=()=>{if(t)try{this[__private__dont__use].free(t)}catch(o){logger.logger.error(o)}e(),this.dispatchEvent({type:"request.end"})};return s.then(o=>(o.finished.finally(n),o),o=>{try{n()}catch{}finally{throw o}})}defineConstant(r,e){let t={};try{t=JSON.parse(this.fileExists("/internal/shared/consts.json")&&this.readFileAsText("/internal/shared/consts.json")||"{}")}catch{}this.writeFile("/internal/shared/consts.json",JSON.stringify({...t,[r]:e}))}mkdir(r){const e=FSHelpers.mkdir(this[__private__dont__use].FS,r);return this.dispatchEvent({type:"filesystem.write"}),e}mkdirTree(r){return FSHelpers.mkdir(this[__private__dont__use].FS,r)}readFileAsText(r){return FSHelpers.readFileAsText(this[__private__dont__use].FS,r)}readFileAsBuffer(r){return FSHelpers.readFileAsBuffer(this[__private__dont__use].FS,r)}writeFile(r,e){const t=FSHelpers.writeFile(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}unlink(r){const e=FSHelpers.unlink(this[__private__dont__use].FS,r);return this.dispatchEvent({type:"filesystem.write"}),e}mv(r,e){const t=FSHelpers.mv(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}cp(r,e){const t=FSHelpers.copyRecursive(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}rmdir(r,e={recursive:!0}){const t=FSHelpers.rmdir(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}listFiles(r,e={prependPath:!1}){return FSHelpers.listFiles(this[__private__dont__use].FS,r,e)}isDir(r){return FSHelpers.isDir(this[__private__dont__use].FS,r)}isFile(r){return FSHelpers.isFile(this[__private__dont__use].FS,r)}symlink(r,e){return FSHelpers.symlink(this[__private__dont__use].FS,r,e)}isSymlink(r){return FSHelpers.isSymlink(this[__private__dont__use].FS,r)}readlink(r){return FSHelpers.readlink(this[__private__dont__use].FS,r)}realpath(r){return FSHelpers.realpath(this[__private__dont__use].FS,r)}fileExists(r){return FSHelpers.fileExists(this[__private__dont__use].FS,r)}enableRuntimeRotation(r){E(this,F,{...p(this,F),enabled:!0,recreateRuntime:r.recreateRuntime,maxRequests:r.maxRequests??400})}async rotateRuntime(){if(!p(this,F).enabled)throw new Error("Runtime rotation is not enabled. Call enableRuntimeRotation() first.");await this.hotSwapPHPRuntime(await p(this,F).recreateRuntime()),p(this,F).requestsMade=0,p(this,F).needsRotating=!1}async hotSwapPHPRuntime(r){const e=this[__private__dont__use].FS,t=this.listFiles("/").map(l=>`/${l}`),s=this[__private__dont__use].spawnProcess,n=e.cwd();e.chdir("/");const o=Object.entries(p(this,X)).map(([l,c])=>({mountHandler:c.mountHandler,vfsPath:l})),i=Object.values(p(this,X)).reverse();for(const l of i)await l.unmount();try{this.exit()}catch{}this.initializeRuntime(r),s&&(this[__private__dont__use].spawnProcess=s),p(this,J)&&this.setSapiName(p(this,J));const a=this[__private__dont__use].FS;for(const l of t)l&&l!=="/request"&©MEMFSNodes(e,a,l);for(const{mountHandler:l,vfsPath:c}of o)this.mkdir(c),await this.mount(c,l);try{a.chdir(n)}catch(l){throw new Error(`Failed to restore CWD to ${n} after PHP runtime rotation.`,{cause:l})}}async mount(r,e){const t=await e(this,this[__private__dont__use].FS,r),s={mountHandler:e,unmount:async()=>{try{await t()}finally{delete p(this,X)[r]}}};return p(this,X)[r]=s,()=>s.unmount()}async cli(r,e={}){if(util.basename(r[0]??"")!=="php")return this.subProcess(r,e);p(this,V)&&(p(this,F).needsRotating=!0);const t=await this.semaphore.acquire();return await _(this,g,me).call(this,()=>{const s=e.env||{};for(const[n,o]of Object.entries(s))_(this,g,he).call(this,n,o);r=[r[0],"-c",PHP_INI_PATH,...r.slice(1)];for(const n of r)this[__private__dont__use].ccall("wasm_add_cli_arg",null,[STRING],[n]);return this[__private__dont__use].ccall("run_cli",null,[],[],{async:!0})}).then(s=>(s.exitCode.finally(t),s)).finally(()=>{p(this,F).needsRotating=!0})}async subProcess(r,e={}){const t=this[__private__dont__use].spawnProcess(r[0],r.slice(1),{env:e.env,cwd:e.cwd??this.cwd()}),s=await createInvertedReadableStream();t.on("error",a=>{safeStreamError$1(s.controller,a)});const n=a=>{try{s.controller.enqueue(a)}catch{t.stderr.off("data",n)}};t.stderr.on("data",n);const o=await createInvertedReadableStream(),i=a=>{try{o.controller.enqueue(a)}catch{t.stdout.off("data",i)}};return t.stdout.on("data",i),t.on("exit",()=>{setTimeout(()=>{try{s.controller.close()}catch{}try{o.controller.close()}catch{}},0)}),new StreamedPHPResponse(new ReadableStream({start(a){a.close()}}),o.stream,s.stream,new Promise(a=>{t.on("exit",l=>{a(l)})}))}setSkipShebang(r){this[__private__dont__use].ccall("wasm_set_skip_shebang",null,[NUMBER],[r?1:0])}exit(r=0){this.dispatchEvent({type:"runtime.beforeExit"});try{this[__private__dont__use]._exit(r)}catch{}E(this,V,!1),E(this,z,null),this[__private__dont__use]&&(delete this[__private__dont__use].onMessage,delete this[__private__dont__use])}[Symbol.dispose](){this.exit(0)}}J=new WeakMap,V=new WeakMap,z=new WeakMap,j=new WeakMap,G=new WeakMap,X=new WeakMap,F=new WeakMap,g=new WeakSet,we=function(r,e,t){const s={...r||{}};s.HTTPS=s.HTTPS||t===443?"on":"off";for(const n in e){let o="HTTP_";["content-type","content-length"].includes(n.toLowerCase())&&(o=""),s[`${o}${n.toUpperCase().replace(/-/g,"_")}`]=e[n]}return s},_e=function(r){this[__private__dont__use].ccall("wasm_set_request_uri",null,[STRING],[r]);let e="";r.includes("?")&&(e=r.substring(r.indexOf("?")+1)),this[__private__dont__use].ccall("wasm_set_query_string",null,[STRING],[e])},ve=function(r){this[__private__dont__use].ccall("wasm_set_request_host",null,[STRING],[r])},xe=function(r){this[__private__dont__use].ccall("wasm_set_request_port",null,[NUMBER],[r])},Ee=function(r,e){let t;try{t=parseInt(new URL(r).port,10)}catch{}return(!t||isNaN(t)||t===80)&&(t=e==="https"?443:80),t},Se=function(r){this[__private__dont__use].ccall("wasm_set_request_method",null,[STRING],[r])},Fe=function(r){r.cookie&&this[__private__dont__use].ccall("wasm_set_cookies",null,[STRING],[r.cookie]),r["content-type"]&&this[__private__dont__use].ccall("wasm_set_content_type",null,[STRING],[r["content-type"]]),r["content-length"]&&this[__private__dont__use].ccall("wasm_set_content_length",null,[NUMBER],[parseInt(r["content-length"],10)])},ke=function(r){let e,t;typeof r=="string"?(logger.logger.warn("Passing a string as the request body is deprecated. Please use a Uint8Array instead. See https://github.com/WordPress/wordpress-playground/issues/997 for more details"),t=this[__private__dont__use].lengthBytesUTF8(r),e=t+1):(t=r.byteLength,e=r.byteLength);const s=this[__private__dont__use].malloc(e);if(!s)throw new Error("Could not allocate memory for the request body.");return typeof r=="string"?this[__private__dont__use].stringToUTF8(r,s,e+1):this[__private__dont__use].HEAPU8.set(r,s),this[__private__dont__use].ccall("wasm_set_request_body",null,[NUMBER],[s]),this[__private__dont__use].ccall("wasm_set_content_length",null,[NUMBER],[t]),s},fe=function(r){this[__private__dont__use].ccall("wasm_set_path_translated",null,[STRING],[r])},Re=function(r,e){this[__private__dont__use].ccall("wasm_add_SERVER_entry",null,[STRING,STRING],[r,e])},he=function(r,e){this[__private__dont__use].ccall("wasm_add_ENV_entry",null,[STRING,STRING],[r,e])},me=async function(r){p(this,F).enabled&&p(this,F).needsRotating&&await this.rotateRuntime(),++p(this,F).requestsMade,p(this,F).requestsMade>=p(this,F).maxRequests&&(p(this,F).needsRotating=!0);const e=this[__private__dont__use],t=await createInvertedReadableStream();e.onHeaders=d=>{a||s||t.controller.enqueue(d.slice())};let s=!1;const n=()=>{s||(s=!0,t.controller.close())},o=await createInvertedReadableStream();e.onStdout=d=>{n(),!a&&o.controller.enqueue(d.slice())};const i=await createInvertedReadableStream();e.onStderr=d=>{a||i.controller.enqueue(d.slice())};let a=!1,l;const u=(async()=>{var d;try{return await Promise.race([r(),new Promise((P,S)=>{var N;l=B=>{isExitCode(B.error)||S(B.error)},(N=p(this,z))==null||N.addEventListener("error",l,{once:!0})})])}catch(h){if(isExitCode(h))return h.status;safeStreamError$1(o.controller,h),safeStreamError$1(i.controller,h),safeStreamError$1(t.controller,h),a=!0;for(const P in this)typeof this[P]=="function"&&(this[P]=()=>{throw new Error("PHP runtime has crashed – see the earlier error for details.")});throw this.functionsMaybeMissingFromAsyncify=getFunctionsMaybeMissingFromAsyncify(),h}finally{a||(safeStreamClose$1(o.controller),safeStreamClose$1(i.controller),n(),a=!0),(d=p(this,z))==null||d.removeEventListener("error",l)}})().then(d=>(d!==0&&this.dispatchEvent({type:"request.error",error:new Error(`PHP.run() failed with exit code ${d}.`),source:"php-wasm"}),d),d=>{const h=d.source??"php-wasm";throw this.dispatchEvent({type:"request.error",error:d,source:h}),d});return new StreamedPHPResponse(t.stream,o.stream,i.stream,u)};function normalizeHeaders(r){const e={};for(const t in r)e[t.toLowerCase()]=r[t];return e}function copyMEMFSNodes(r,e,t){if(getNodeType(r,t)!=="memfs"||!["memfs","missing"].includes(getNodeType(e,t)))return;const s=r.lookupPath(t,{follow:!1});if(r.isLink(s.node.mode)){const o=r.readlink(t);e.symlink(o,t);return}if(!r.isDir(s.node.mode)){e.writeFile(t,r.readFile(t));return}e.mkdirTree(t);const n=r.readdir(t).filter(o=>o!=="."&&o!=="..");for(const o of n)copyMEMFSNodes(r,e,util.joinPaths(t,o))}async function createInvertedReadableStream(r={}){let e;const t=new Promise(o=>{e=o}),s=new ReadableStream({...r,start(o){if(e(o),r.start)return r.start(o)}}),n=await t;return{stream:s,controller:n}}function safeStreamError$1(r,e){try{r.error(e)}catch{}}function safeStreamClose$1(r){try{r.close()}catch{}}const getNodeType=(r,e)=>{try{return"contents"in r.lookupPath(e,{follow:!0}).node?"memfs":"not-memfs"}catch{return"missing"}};async function getPhpIniEntries(r,e){const t=ini.parse(await r.readFileAsText(PHP_INI_PATH));if(e===void 0)return t;const s={};for(const n of e)s[n]=t[n];return s}async function setPhpIniEntries(r,e){const t=ini.parse(await r.readFileAsText(PHP_INI_PATH));for(const[s,n]of Object.entries(e))n==null?delete t[s]:t[s]=n;await r.writeFile(PHP_INI_PATH,ini.stringify(t))}async function withPHPIniValues(r,e,t){const s=await r.readFileAsText(PHP_INI_PATH);try{return await setPhpIniEntries(r,e),await t()}finally{await r.writeFile(PHP_INI_PATH,s)}}async function printDebugDetails(r,e){e&&printResponseDebugDetails(await PHPResponse.fromStreamedResponse(e)),await prettyPrintFullStackTrace(r)}async function prettyPrintFullStackTrace(r){let e=r,t=!0;for(;e;)t||process.stderr.write(`
|
|
57
|
+
${t.errors}`,t,"request");return t}async runStream(r){const e=await this.semaphore.acquire();let t;const s=_(this,g,me).call(this,async()=>{if(p(this,V)||(await this[__private__dont__use].ccall("php_wasm_init",null,[],[],{isAsync:!0}),E(this,V,!0)),r.scriptPath&&!this.fileExists(r.scriptPath))throw new Error(`The script path "${r.scriptPath}" does not exist.`);_(this,g,_e).call(this,r.relativeUri||""),_(this,g,Se).call(this,r.method||"GET");const o=normalizeHeaders(r.headers||{}),i=o.host||"example.com:443",a=_(this,g,Ee).call(this,i,r.protocol||"http");if(_(this,g,ve).call(this,i),_(this,g,xe).call(this,a),_(this,g,Fe).call(this,o),r.body&&(t=_(this,g,ke).call(this,r.body)),typeof r.code=="string")this.writeFile("/internal/eval.php",r.code),_(this,g,fe).call(this,"/internal/eval.php");else if(typeof r.scriptPath=="string")_(this,g,fe).call(this,r.scriptPath||"");else throw new TypeError("The request object must have either a `code` or a `scriptPath` property.");const l=_(this,g,we).call(this,r.$_SERVER,o,a);for(const u in l)_(this,g,Re).call(this,u,l[u]);const c=r.env||{};for(const u in c)_(this,g,he).call(this,u,c[u]);return await this[__private__dont__use].ccall("wasm_sapi_handle_request",NUMBER,[],[],{async:!0})}),n=()=>{if(t)try{this[__private__dont__use].free(t)}catch(o){logger.logger.error(o)}e(),this.dispatchEvent({type:"request.end"})};return s.then(o=>(o.finished.finally(n),o),o=>{try{n()}catch{}finally{throw o}})}defineConstant(r,e){let t={};try{t=JSON.parse(this.fileExists("/internal/shared/consts.json")&&this.readFileAsText("/internal/shared/consts.json")||"{}")}catch{}this.writeFile("/internal/shared/consts.json",JSON.stringify({...t,[r]:e}))}mkdir(r){const e=FSHelpers.mkdir(this[__private__dont__use].FS,r);return this.dispatchEvent({type:"filesystem.write"}),e}mkdirTree(r){return FSHelpers.mkdir(this[__private__dont__use].FS,r)}readFileAsText(r){return FSHelpers.readFileAsText(this[__private__dont__use].FS,r)}readFileAsBuffer(r){return FSHelpers.readFileAsBuffer(this[__private__dont__use].FS,r)}writeFile(r,e){const t=FSHelpers.writeFile(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}unlink(r){const e=FSHelpers.unlink(this[__private__dont__use].FS,r);return this.dispatchEvent({type:"filesystem.write"}),e}mv(r,e){const t=FSHelpers.mv(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}cp(r,e){const t=FSHelpers.copyRecursive(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}rmdir(r,e={recursive:!0}){const t=FSHelpers.rmdir(this[__private__dont__use].FS,r,e);return this.dispatchEvent({type:"filesystem.write"}),t}listFiles(r,e={prependPath:!1}){return FSHelpers.listFiles(this[__private__dont__use].FS,r,e)}isDir(r){return FSHelpers.isDir(this[__private__dont__use].FS,r)}isFile(r){return FSHelpers.isFile(this[__private__dont__use].FS,r)}symlink(r,e){return FSHelpers.symlink(this[__private__dont__use].FS,r,e)}isSymlink(r){return FSHelpers.isSymlink(this[__private__dont__use].FS,r)}readlink(r){return FSHelpers.readlink(this[__private__dont__use].FS,r)}realpath(r){return FSHelpers.realpath(this[__private__dont__use].FS,r)}fileExists(r){return FSHelpers.fileExists(this[__private__dont__use].FS,r)}enableRuntimeRotation(r){E(this,F,{...p(this,F),enabled:!0,recreateRuntime:r.recreateRuntime,maxRequests:r.maxRequests??400})}async rotateRuntime(){if(!p(this,F).enabled)throw new Error("Runtime rotation is not enabled. Call enableRuntimeRotation() first.");await this.hotSwapPHPRuntime(await p(this,F).recreateRuntime()),p(this,F).requestsMade=0,p(this,F).needsRotating=!1}async hotSwapPHPRuntime(r){const e=this[__private__dont__use].FS,t=this.listFiles("/").map(l=>`/${l}`),s=this[__private__dont__use].spawnProcess,n=e.cwd();e.chdir("/");const o=Object.entries(p(this,X)).map(([l,c])=>({mountHandler:c.mountHandler,mountPointSnapshot:snapshotMountPoint(e,l),vfsPath:l})),i=Object.values(p(this,X)).reverse();for(const l of i)await l.unmount();try{this.exit()}catch{}this.initializeRuntime(r),s&&(this[__private__dont__use].spawnProcess=s),p(this,J)&&this.setSapiName(p(this,J));const a=this[__private__dont__use].FS;for(const l of t)l&&l!=="/request"&©MEMFSNodes(e,a,l);for(const{mountHandler:l,mountPointSnapshot:c,vfsPath:u}of o)try{await this.mount(u,l)}catch(d){if(isMissingMountSourceError(d)){restoreMountPointSnapshot(a,u,c);continue}if(!isMissingMountTargetPathError(d))throw d;this.mkdir(u),await this.mount(u,l)}try{a.chdir(n)}catch(l){throw new Error(`Failed to restore CWD to ${n} after PHP runtime rotation.`,{cause:l})}}async mount(r,e){const t=await e(this,this[__private__dont__use].FS,r),s={mountHandler:e,unmount:async()=>{try{await t()}finally{delete p(this,X)[r]}}};return p(this,X)[r]=s,()=>s.unmount()}async cli(r,e={}){if(util.basename(r[0]??"")!=="php")return this.subProcess(r,e);p(this,V)&&(p(this,F).needsRotating=!0);const t=await this.semaphore.acquire();return await _(this,g,me).call(this,()=>{const s=e.env||{};for(const[n,o]of Object.entries(s))_(this,g,he).call(this,n,o);r=[r[0],"-c",PHP_INI_PATH,...r.slice(1)];for(const n of r)this[__private__dont__use].ccall("wasm_add_cli_arg",null,[STRING],[n]);return this[__private__dont__use].ccall("run_cli",null,[],[],{async:!0})}).then(s=>(s.exitCode.finally(t),s)).finally(()=>{p(this,F).needsRotating=!0})}async subProcess(r,e={}){const t=this[__private__dont__use].spawnProcess(r[0],r.slice(1),{env:e.env,cwd:e.cwd??this.cwd()}),s=await createInvertedReadableStream();t.on("error",a=>{safeStreamError$1(s.controller,a)});const n=a=>{try{s.controller.enqueue(a)}catch{t.stderr.off("data",n)}};t.stderr.on("data",n);const o=await createInvertedReadableStream(),i=a=>{try{o.controller.enqueue(a)}catch{t.stdout.off("data",i)}};return t.stdout.on("data",i),t.on("exit",()=>{setTimeout(()=>{try{s.controller.close()}catch{}try{o.controller.close()}catch{}},0)}),new StreamedPHPResponse(new ReadableStream({start(a){a.close()}}),o.stream,s.stream,new Promise(a=>{t.on("exit",l=>{a(l)})}))}setSkipShebang(r){this[__private__dont__use].ccall("wasm_set_skip_shebang",null,[NUMBER],[r?1:0])}exit(r=0){this.dispatchEvent({type:"runtime.beforeExit"});try{this[__private__dont__use]._exit(r)}catch{}E(this,V,!1),E(this,z,null),this[__private__dont__use]&&(delete this[__private__dont__use].onMessage,delete this[__private__dont__use])}[Symbol.dispose](){this.exit(0)}}J=new WeakMap,V=new WeakMap,z=new WeakMap,j=new WeakMap,G=new WeakMap,X=new WeakMap,F=new WeakMap,g=new WeakSet,we=function(r,e,t){const s={...r||{}};s.HTTPS=s.HTTPS||t===443?"on":"off";for(const n in e){let o="HTTP_";["content-type","content-length"].includes(n.toLowerCase())&&(o=""),s[`${o}${n.toUpperCase().replace(/-/g,"_")}`]=e[n]}return s},_e=function(r){this[__private__dont__use].ccall("wasm_set_request_uri",null,[STRING],[r]);let e="";r.includes("?")&&(e=r.substring(r.indexOf("?")+1)),this[__private__dont__use].ccall("wasm_set_query_string",null,[STRING],[e])},ve=function(r){this[__private__dont__use].ccall("wasm_set_request_host",null,[STRING],[r])},xe=function(r){this[__private__dont__use].ccall("wasm_set_request_port",null,[NUMBER],[r])},Ee=function(r,e){let t;try{t=parseInt(new URL(r).port,10)}catch{}return(!t||isNaN(t)||t===80)&&(t=e==="https"?443:80),t},Se=function(r){this[__private__dont__use].ccall("wasm_set_request_method",null,[STRING],[r])},Fe=function(r){r.cookie&&this[__private__dont__use].ccall("wasm_set_cookies",null,[STRING],[r.cookie]),r["content-type"]&&this[__private__dont__use].ccall("wasm_set_content_type",null,[STRING],[r["content-type"]]),r["content-length"]&&this[__private__dont__use].ccall("wasm_set_content_length",null,[NUMBER],[parseInt(r["content-length"],10)])},ke=function(r){let e,t;typeof r=="string"?(logger.logger.warn("Passing a string as the request body is deprecated. Please use a Uint8Array instead. See https://github.com/WordPress/wordpress-playground/issues/997 for more details"),t=this[__private__dont__use].lengthBytesUTF8(r),e=t+1):(t=r.byteLength,e=r.byteLength);const s=this[__private__dont__use].malloc(e);if(!s)throw new Error("Could not allocate memory for the request body.");return typeof r=="string"?this[__private__dont__use].stringToUTF8(r,s,e+1):this[__private__dont__use].HEAPU8.set(r,s),this[__private__dont__use].ccall("wasm_set_request_body",null,[NUMBER],[s]),this[__private__dont__use].ccall("wasm_set_content_length",null,[NUMBER],[t]),s},fe=function(r){this[__private__dont__use].ccall("wasm_set_path_translated",null,[STRING],[r])},Re=function(r,e){this[__private__dont__use].ccall("wasm_add_SERVER_entry",null,[STRING,STRING],[r,e])},he=function(r,e){this[__private__dont__use].ccall("wasm_add_ENV_entry",null,[STRING,STRING],[r,e])},me=async function(r){p(this,F).enabled&&p(this,F).needsRotating&&await this.rotateRuntime(),++p(this,F).requestsMade,p(this,F).requestsMade>=p(this,F).maxRequests&&(p(this,F).needsRotating=!0);const e=this[__private__dont__use],t=await createInvertedReadableStream();e.onHeaders=d=>{a||s||t.controller.enqueue(d.slice())};let s=!1;const n=()=>{s||(s=!0,t.controller.close())},o=await createInvertedReadableStream();e.onStdout=d=>{n(),!a&&o.controller.enqueue(d.slice())};const i=await createInvertedReadableStream();e.onStderr=d=>{a||i.controller.enqueue(d.slice())};let a=!1,l;const u=(async()=>{var d;try{return await Promise.race([r(),new Promise((P,S)=>{var N;l=B=>{isExitCode(B.error)||S(B.error)},(N=p(this,z))==null||N.addEventListener("error",l,{once:!0})})])}catch(h){if(isExitCode(h))return h.status;safeStreamError$1(o.controller,h),safeStreamError$1(i.controller,h),safeStreamError$1(t.controller,h),a=!0;for(const P in this)typeof this[P]=="function"&&(this[P]=()=>{throw new Error("PHP runtime has crashed – see the earlier error for details.")});throw this.functionsMaybeMissingFromAsyncify=getFunctionsMaybeMissingFromAsyncify(),h}finally{a||(safeStreamClose$1(o.controller),safeStreamClose$1(i.controller),n(),a=!0),(d=p(this,z))==null||d.removeEventListener("error",l)}})().then(d=>(d!==0&&this.dispatchEvent({type:"request.error",error:new Error(`PHP.run() failed with exit code ${d}.`),source:"php-wasm"}),d),d=>{const h=d.source??"php-wasm";throw this.dispatchEvent({type:"request.error",error:d,source:h}),d});return new StreamedPHPResponse(t.stream,o.stream,i.stream,u)};function normalizeHeaders(r){const e={};for(const t in r)e[t.toLowerCase()]=r[t];return e}function copyMEMFSNodes(r,e,t){if(getNodeType(r,t)!=="memfs"||!["memfs","missing"].includes(getNodeType(e,t)))return;const s=r.lookupPath(t,{follow:!1});if(r.isLink(s.node.mode)){const o=r.readlink(t);e.symlink(o,t);return}if(!r.isDir(s.node.mode)){e.writeFile(t,r.readFile(t));return}e.mkdirTree(t);const n=r.readdir(t).filter(o=>o!=="."&&o!=="..");for(const o of n)copyMEMFSNodes(r,e,util.joinPaths(t,o))}function snapshotMountPoint(r,e){try{const t=r.lookupPath(e,{follow:!1});return r.isLink(t.node.mode)?{kind:"symlink",target:r.readlink(e)}:r.isDir(t.node.mode)?{kind:"directory"}:{kind:"file"}}catch{return}}function restoreMountPointSnapshot(r,e,t){if(!(!t||getNodeType(r,e)!=="missing")){if(t.kind==="directory"){r.mkdirTree(e);return}r.mkdirTree(util.dirname(e)),t.kind==="symlink"?r.symlink(t.target,e):r.writeFile(e,new Uint8Array)}}function isMissingMountSourceError(r){return r.phpWasmMountSourceMissing===!0}function isMissingMountTargetPathError(r){return r.errno===44}async function createInvertedReadableStream(r={}){let e;const t=new Promise(o=>{e=o}),s=new ReadableStream({...r,start(o){if(e(o),r.start)return r.start(o)}}),n=await t;return{stream:s,controller:n}}function safeStreamError$1(r,e){try{r.error(e)}catch{}}function safeStreamClose$1(r){try{r.close()}catch{}}const getNodeType=(r,e)=>{try{return"contents"in r.lookupPath(e,{follow:!0}).node?"memfs":"not-memfs"}catch{return"missing"}};async function getPhpIniEntries(r,e){const t=ini.parse(await r.readFileAsText(PHP_INI_PATH));if(e===void 0)return t;const s={};for(const n of e)s[n]=t[n];return s}async function setPhpIniEntries(r,e){const t=ini.parse(await r.readFileAsText(PHP_INI_PATH));for(const[s,n]of Object.entries(e))n==null?delete t[s]:t[s]=n;await r.writeFile(PHP_INI_PATH,ini.stringify(t))}async function withPHPIniValues(r,e,t){const s=await r.readFileAsText(PHP_INI_PATH);try{return await setPhpIniEntries(r,e),await t()}finally{await r.writeFile(PHP_INI_PATH,s)}}async function printDebugDetails(r,e){e&&printResponseDebugDetails(await PHPResponse.fromStreamedResponse(e)),await prettyPrintFullStackTrace(r)}async function prettyPrintFullStackTrace(r){let e=r,t=!0;for(;e;)t||process.stderr.write(`
|
|
58
58
|
Caused by:
|
|
59
59
|
|
|
60
60
|
`),process.stderr.write(e.originalErrorClassName??e.name),process.stderr.write(": "+e.message+`
|