@php-wasm/universal 0.1.43 → 0.1.46
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 +4 -4
- package/index.js +79 -74
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=Symbol("error"),S=Symbol("message");class v extends Event{constructor(e,t={}){super(e),this[E]=t.error===void 0?null:t.error,this[S]=t.message===void 0?"":t.message}get error(){return this[E]}get message(){return this[S]}}Object.defineProperty(v.prototype,"error",{enumerable:!0});Object.defineProperty(v.prototype,"message",{enumerable:!0});const B=typeof globalThis.ErrorEvent=="function"?globalThis.ErrorEvent:v;class C extends EventTarget{constructor(){super(...arguments),this.listenersCount=0}addEventListener(e,t){++this.listenersCount,super.addEventListener(e,t)}removeEventListener(e,t){--this.listenersCount,super.removeEventListener(e,t)}hasListeners(){return this.listenersCount>0}}function M(s){s.asm={...s.asm};const e=new C;for(const t in s.asm)if(typeof s.asm[t]=="function"){const r=s.asm[t];s.asm[t]=function(...n){try{return r(...n)}catch(o){if(!(o instanceof Error))throw o;if("exitCode"in o&&o?.exitCode===0)return;const i=q(o,s.lastAsyncifyStackSource?.stack);if(s.lastAsyncifyStackSource&&(o.cause=s.lastAsyncifyStackSource),!e.hasListeners())throw z(i),o;e.dispatchEvent(new B("error",{error:o,message:i}))}}}return e}let w=[];function D(){return w}function q(s,e){if(s.message==="unreachable"){let t=W;e||(t+=`
|
|
2
2
|
|
|
3
3
|
This stack trace is lacking. For a better one initialize
|
|
4
4
|
the PHP runtime with { debug: true }, e.g. PHPNode.load('8.1', { debug: true }).
|
|
@@ -32,9 +32,9 @@ CLI option:
|
|
|
32
32
|
${T}
|
|
33
33
|
${j} WASM ERROR${H}${x}`);for(const e of s.split(`
|
|
34
34
|
`))console.log(`${T} ${e} `);console.log(`${H}`)}}function G(s){try{const e=s.split(`
|
|
35
|
-
`).slice(1).map(t=>{const r=t.trim().substring(3).split(" ");return{fn:r.length>=2?r[0]:"<unknown>",isWasm:t.includes("wasm://")}}).filter(({fn:t,isWasm:r})=>r&&!t.startsWith("dynCall_")&&!t.startsWith("invoke_")).map(({fn:t})=>t);return Array.from(new Set(e))}catch{return[]}}class y{constructor(e,t,r,n="",o=0){this.httpStatusCode=e,this.headers=t,this.bytes=r,this.exitCode=o,this.errors=n}static fromRawData(e){return new y(e.httpStatusCode,e.headers,e.bytes,e.errors,e.exitCode)}toRawData(){return{headers:this.headers,bytes:this.bytes,errors:this.errors,exitCode:this.exitCode,httpStatusCode:this.httpStatusCode}}get json(){return JSON.parse(this.text)}get text(){return new TextDecoder().decode(this.bytes)}}const R=["8.2","8.1","8.0","7.4","7.3","7.2","7.1","7.0","5.6"],V=R[0],Y=R;class U{#e;#t;constructor(e,t={}){this.requestHandler=e,this.#e={},this.#t={handleRedirects:!1,maxRedirects:4,...t}}async request(e,t=0){const r=await this.requestHandler.request({...e,headers:{...e.headers,cookie:this.#r()}});if(r.headers["set-cookie"]&&this.#s(r.headers["set-cookie"]),this.#t.handleRedirects&&r.headers.location&&t<this.#t.maxRedirects){const n=new URL(r.headers.location[0],this.requestHandler.absoluteUrl);return this.request({url:n.toString(),method:"GET",headers:{}},t+1)}return r}pathToInternalUrl(e){return this.requestHandler.pathToInternalUrl(e)}internalUrlToPath(e){return this.requestHandler.internalUrlToPath(e)}get absoluteUrl(){return this.requestHandler.absoluteUrl}get documentRoot(){return this.requestHandler.documentRoot}#s(e){for(const t of e)try{if(!t.includes("="))continue;const r=t.indexOf("="),n=t.substring(0,r),o=t.substring(r+1).split(";")[0];this.#e[n]=o}catch(r){console.error(r)}}#r(){const e=[];for(const t in this.#e)e.push(`${t}=${this.#e[t]}`);return e.join("; ")}}class J{constructor({concurrency:e}){this._running=0,this.concurrency=e,this.queue=[]}get running(){return this._running}async acquire(){for(;;)if(this._running>=this.concurrency)await new Promise(e=>this.queue.push(e));else return this._running++,()=>{this._running--,this.queue.length>0&&this.queue.shift()()}}async run(e){const t=await this.acquire();try{return await e()}finally{t()}}}const A="http://example.com";function P(s){return s.toString().substring(s.origin.length)}function b(s,e){return!e||!s.startsWith(e)?s:s.substring(e.length)}function O(s,e){return!e||s.startsWith(e)?s:e+s}class I{#e;#t;#s;#r;#o;#n;#i;#a;#l;constructor(e,t={}){this.#a=new J({concurrency:1});const{documentRoot:r="/www/",absoluteUrl:n=typeof location=="object"?location?.href:"",isStaticFilePath:o=()=>!1}=t;this.php=e,this.#e=r,this.#l=o;const i=new URL(n);this.#s=i.hostname,this.#r=i.port?Number(i.port):i.protocol==="https:"?443:80,this.#t=(i.protocol||"").replace(":","");const l=this.#r!==443&&this.#r!==80;this.#o=[this.#s,l?`:${this.#r}`:""].join(""),this.#n=i.pathname.replace(/\/+$/,""),this.#i=[`${this.#t}://`,this.#o,this.#n].join("")}pathToInternalUrl(e){return`${this.absoluteUrl}${e}`}internalUrlToPath(e){const t=new URL(e);return t.pathname.startsWith(this.#n)&&(t.pathname=t.pathname.slice(this.#n.length)),P(t)}get isRequestRunning(){return this.#a.running>0}get absoluteUrl(){return this.#i}get documentRoot(){return this.#e}async request(e){const t=e.url.startsWith("http://")||e.url.startsWith("https://"),r=new URL(e.url,t?void 0:A),n=b(r.pathname,this.#n);return this.#l(n)?this.#c(n):await this.#h(e,r)}#c(e){const t=`${this.#e}${e}`;if(!this.php.fileExists(t))return new y(404,{},new TextEncoder().encode("404 File not found"));const r=this.php.readFileAsBuffer(t);return new y(200,{"content-length":[`${r.byteLength}`],"content-type":[Q(t)],"accept-ranges":["bytes"],"cache-control":["public, max-age=0"]},r)}async#h(e,t){const r=await this.#a.acquire();try{this.php.addServerGlobalEntry("DOCUMENT_ROOT",this.#e),this.php.addServerGlobalEntry("HTTPS",this.#i.startsWith("https://")?"on":"");let n="GET";const o={host:this.#o,...N(e.headers||{})},i=[];if(e.files&&Object.keys(e.files).length){n="POST";for(const
|
|
35
|
+
`).slice(1).map(t=>{const r=t.trim().substring(3).split(" ");return{fn:r.length>=2?r[0]:"<unknown>",isWasm:t.includes("wasm://")}}).filter(({fn:t,isWasm:r})=>r&&!t.startsWith("dynCall_")&&!t.startsWith("invoke_")).map(({fn:t})=>t);return Array.from(new Set(e))}catch{return[]}}class y{constructor(e,t,r,n="",o=0){this.httpStatusCode=e,this.headers=t,this.bytes=r,this.exitCode=o,this.errors=n}static fromRawData(e){return new y(e.httpStatusCode,e.headers,e.bytes,e.errors,e.exitCode)}toRawData(){return{headers:this.headers,bytes:this.bytes,errors:this.errors,exitCode:this.exitCode,httpStatusCode:this.httpStatusCode}}get json(){return JSON.parse(this.text)}get text(){return new TextDecoder().decode(this.bytes)}}const R=["8.2","8.1","8.0","7.4","7.3","7.2","7.1","7.0","5.6"],V=R[0],Y=R;class U{#e;#t;constructor(e,t={}){this.requestHandler=e,this.#e={},this.#t={handleRedirects:!1,maxRedirects:4,...t}}async request(e,t=0){const r=await this.requestHandler.request({...e,headers:{...e.headers,cookie:this.#r()}});if(r.headers["set-cookie"]&&this.#s(r.headers["set-cookie"]),this.#t.handleRedirects&&r.headers.location&&t<this.#t.maxRedirects){const n=new URL(r.headers.location[0],this.requestHandler.absoluteUrl);return this.request({url:n.toString(),method:"GET",headers:{}},t+1)}return r}pathToInternalUrl(e){return this.requestHandler.pathToInternalUrl(e)}internalUrlToPath(e){return this.requestHandler.internalUrlToPath(e)}get absoluteUrl(){return this.requestHandler.absoluteUrl}get documentRoot(){return this.requestHandler.documentRoot}#s(e){for(const t of e)try{if(!t.includes("="))continue;const r=t.indexOf("="),n=t.substring(0,r),o=t.substring(r+1).split(";")[0];this.#e[n]=o}catch(r){console.error(r)}}#r(){const e=[];for(const t in this.#e)e.push(`${t}=${this.#e[t]}`);return e.join("; ")}}class J{constructor({concurrency:e}){this._running=0,this.concurrency=e,this.queue=[]}get running(){return this._running}async acquire(){for(;;)if(this._running>=this.concurrency)await new Promise(e=>this.queue.push(e));else return this._running++,()=>{this._running--,this.queue.length>0&&this.queue.shift()()}}async run(e){const t=await this.acquire();try{return await e()}finally{t()}}}const A="http://example.com";function P(s){return s.toString().substring(s.origin.length)}function b(s,e){return!e||!s.startsWith(e)?s:s.substring(e.length)}function O(s,e){return!e||s.startsWith(e)?s:e+s}class I{#e;#t;#s;#r;#o;#n;#i;#a;#l;constructor(e,t={}){this.#a=new J({concurrency:1});const{documentRoot:r="/www/",absoluteUrl:n=typeof location=="object"?location?.href:"",isStaticFilePath:o=()=>!1}=t;this.php=e,this.#e=r,this.#l=o;const i=new URL(n);this.#s=i.hostname,this.#r=i.port?Number(i.port):i.protocol==="https:"?443:80,this.#t=(i.protocol||"").replace(":","");const l=this.#r!==443&&this.#r!==80;this.#o=[this.#s,l?`:${this.#r}`:""].join(""),this.#n=i.pathname.replace(/\/+$/,""),this.#i=[`${this.#t}://`,this.#o,this.#n].join("")}pathToInternalUrl(e){return`${this.absoluteUrl}${e}`}internalUrlToPath(e){const t=new URL(e);return t.pathname.startsWith(this.#n)&&(t.pathname=t.pathname.slice(this.#n.length)),P(t)}get isRequestRunning(){return this.#a.running>0}get absoluteUrl(){return this.#i}get documentRoot(){return this.#e}async request(e){const t=e.url.startsWith("http://")||e.url.startsWith("https://"),r=new URL(e.url,t?void 0:A),n=b(r.pathname,this.#n);return this.#l(n)?this.#c(n):await this.#h(e,r)}#c(e){const t=`${this.#e}${e}`;if(!this.php.fileExists(t))return new y(404,{},new TextEncoder().encode("404 File not found"));const r=this.php.readFileAsBuffer(t);return new y(200,{"content-length":[`${r.byteLength}`],"content-type":[Q(t)],"accept-ranges":["bytes"],"cache-control":["public, max-age=0"]},r)}async#h(e,t){const r=await this.#a.acquire();try{this.php.addServerGlobalEntry("DOCUMENT_ROOT",this.#e),this.php.addServerGlobalEntry("HTTPS",this.#i.startsWith("https://")?"on":"");let n="GET";const o={host:this.#o,...N(e.headers||{})},i=[];if(e.files&&Object.keys(e.files).length){n="POST";for(const u in e.files){const m=e.files[u];i.push({key:u,name:m.name,type:m.type,data:new Uint8Array(await m.arrayBuffer())})}o["content-type"]?.startsWith("multipart/form-data")&&(e.formData=K(e.body||""),o["content-type"]="application/x-www-form-urlencoded",delete e.body)}let l;e.formData!==void 0?(n="POST",o["content-type"]=o["content-type"]||"application/x-www-form-urlencoded",l=new URLSearchParams(e.formData).toString()):l=e.body;let c;try{c=this.#u(t.pathname)}catch{return new y(404,{},new TextEncoder().encode("404 File not found"))}return await this.php.run({relativeUri:O(P(t),this.#n),protocol:this.#t,method:e.method||n,body:l,fileInfos:i,scriptPath:c,headers:o})}finally{r()}}#u(e){let t=b(e,this.#n);t.includes(".php")?t=t.split(".php")[0]+".php":(t.endsWith("/")||(t+="/"),t.endsWith("index.php")||(t+="index.php"));const r=`${this.#e}${t}`;if(this.php.fileExists(r))return r;if(!this.php.fileExists(`${this.#e}/index.php`))throw new Error(`File not found: ${r}`);return`${this.#e}/index.php`}}function K(s){const e={},t=s.match(/--(.*)\r\n/);if(!t)return e;const r=t[1],n=s.split(`--${r}`);return n.shift(),n.pop(),n.forEach(o=>{const i=o.indexOf(`\r
|
|
36
36
|
\r
|
|
37
|
-
`),l=o.substring(0,i).trim(),c=o.substring(i+4).trim(),
|
|
37
|
+
`),l=o.substring(0,i).trim(),c=o.substring(i+4).trim(),u=l.match(/name="([^"]+)"/);if(u){const m=u[1];e[m]=c}}),e}function Q(s){switch(s.split(".").pop()){case"css":return"text/css";case"js":return"application/javascript";case"png":return"image/png";case"jpg":case"jpeg":return"image/jpeg";case"gif":return"image/gif";case"svg":return"image/svg+xml";case"woff":return"font/woff";case"woff2":return"font/woff2";case"ttf":return"font/ttf";case"otf":return"font/otf";case"eot":return"font/eot";case"ico":return"image/x-icon";case"html":return"text/html";case"json":return"application/json";case"xml":return"application/xml";case"txt":case"md":return"text/plain";default:return"application-octet-stream"}}const F={0:"No error occurred. System call completed successfully.",1:"Argument list too long.",2:"Permission denied.",3:"Address in use.",4:"Address not available.",5:"Address family not supported.",6:"Resource unavailable, or operation would block.",7:"Connection already in progress.",8:"Bad file descriptor.",9:"Bad message.",10:"Device or resource busy.",11:"Operation canceled.",12:"No child processes.",13:"Connection aborted.",14:"Connection refused.",15:"Connection reset.",16:"Resource deadlock would occur.",17:"Destination address required.",18:"Mathematics argument out of domain of function.",19:"Reserved.",20:"File exists.",21:"Bad address.",22:"File too large.",23:"Host is unreachable.",24:"Identifier removed.",25:"Illegal byte sequence.",26:"Operation in progress.",27:"Interrupted function.",28:"Invalid argument.",29:"I/O error.",30:"Socket is connected.",31:"There is a directory under that path.",32:"Too many levels of symbolic links.",33:"File descriptor value too large.",34:"Too many links.",35:"Message too large.",36:"Reserved.",37:"Filename too long.",38:"Network is down.",39:"Connection aborted by network.",40:"Network unreachable.",41:"Too many files open in system.",42:"No buffer space available.",43:"No such device.",44:"There is no such file or directory OR the parent directory does not exist.",45:"Executable file format error.",46:"No locks available.",47:"Reserved.",48:"Not enough space.",49:"No message of the desired type.",50:"Protocol not available.",51:"No space left on device.",52:"Function not supported.",53:"The socket is not connected.",54:"Not a directory or a symbolic link to a directory.",55:"Directory not empty.",56:"State not recoverable.",57:"Not a socket.",58:"Not supported, or operation not supported on socket.",59:"Inappropriate I/O control operation.",60:"No such device or address.",61:"Value too large to be stored in data type.",62:"Previous owner died.",63:"Operation not permitted.",64:"Broken pipe.",65:"Protocol error.",66:"Protocol not supported.",67:"Protocol wrong type for socket.",68:"Result too large.",69:"Read-only file system.",70:"Invalid seek.",71:"No such process.",72:"Reserved.",73:"Connection timed out.",74:"Text file busy.",75:"Cross-device link.",76:"Extension: Capabilities insufficient."};function p(s=""){return function(t,r,n){const o=n.value;n.value=function(...i){try{return o.apply(this,i)}catch(l){const c=typeof l=="object"?l?.errno:null;if(c in F){const u=F[c],m=typeof i[0]=="string"?i[0]:null,$=m!==null?s.replaceAll("{path}",m):s;throw new Error(`${$}: ${u}`,{cause:l})}throw l}}}}async function X(s,e={},t=[]){let r,n;const o=new Promise(c=>{n=c}),i=new Promise(c=>{r=c}),l=s.init(ee,{onAbort(c){console.error("WASM aborted: "),console.error(c)},ENV:{},locateFile:c=>c,...e,noInitialRun:!0,onRuntimeInitialized(){e.onRuntimeInitialized&&e.onRuntimeInitialized(),r()},monitorRunDependencies(c){c===0&&(delete l.monitorRunDependencies,n())}});for(const{default:c}of t)c(l);return t.length||n(),await o,await i,_.push(l),_.length-1}const _=[];function Z(s){return _[s]}const ee=function(){return typeof process<"u"&&process.release?.name==="node"?"NODE":typeof window<"u"?"WEB":typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope?"WORKER":"NODE"}();var te=Object.defineProperty,re=Object.getOwnPropertyDescriptor,f=(s,e,t,r)=>{for(var n=r>1?void 0:r?re(e,t):e,o=s.length-1,i;o>=0;o--)(i=s[o])&&(n=(r?i(e,t,n):i(n))||n);return r&&n&&te(e,t,n),n};const h="string",g="number",a=Symbol("__private__dont__use");class d{constructor(e,t){this.#e=[],this.#t=!1,this.#s=null,this.#r={},e!==void 0&&this.initializeRuntime(e),t&&(this.requestHandler=new U(new I(this,t)))}#e;#t;#s;#r;get absoluteUrl(){return this.requestHandler.requestHandler.absoluteUrl}get documentRoot(){return this.requestHandler.requestHandler.documentRoot}pathToInternalUrl(e){return this.requestHandler.requestHandler.pathToInternalUrl(e)}internalUrlToPath(e){return this.requestHandler.requestHandler.internalUrlToPath(e)}initializeRuntime(e){if(this[a])throw new Error("PHP runtime already initialized.");const t=Z(e);if(!t)throw new Error("Invalid PHP runtime id.");this[a]=t,this.#s=M(t)}setPhpIniPath(e){if(this.#t)throw new Error("Cannot set PHP ini path after calling run().");this[a].ccall("wasm_set_phpini_path",null,["string"],[e])}setPhpIniEntry(e,t){if(this.#t)throw new Error("Cannot set PHP ini entries after calling run().");this.#e.push([e,t])}chdir(e){this[a].FS.chdir(e)}async request(e,t){if(!this.requestHandler)throw new Error("No request handler available.");return this.requestHandler.request(e,t)}async run(e){this.#t||(this.#o(),this.#t=!0),this.#u(e.scriptPath||""),this.#i(e.relativeUri||""),this.#l(e.method||"GET");const{host:t,...r}={host:"example.com:443",...N(e.headers||{})};if(this.#a(t,e.protocol||"http"),this.#c(r),e.body&&this.#h(e.body),e.fileInfos)for(const n of e.fileInfos)this.#p(n);return e.code&&this.#f(" ?>"+e.code),this.#d(),await this.#m()}#o(){if(this.#e.length>0){const e=this.#e.map(([t,r])=>`${t}=${r}`).join(`
|
|
38
38
|
`)+`
|
|
39
39
|
|
|
40
|
-
`;this[a].ccall("wasm_set_phpini_entries",null,[h],[e])}this[a].ccall("php_wasm_init",null,[],[])}#n(){const e="/tmp/headers.json";if(!this.fileExists(e))throw new Error("SAPI Error: Could not find response headers file.");const t=JSON.parse(this.readFileAsText(e)),r={};for(const n of t.headers){if(!n.includes(": "))continue;const o=n.indexOf(": "),i=n.substring(0,o).toLowerCase(),l=n.substring(o+2);i in r||(r[i]=[]),r[i].push(l)}return{headers:r,httpStatusCode:t.status}}#i(e){if(this[a].ccall("wasm_set_request_uri",null,[h],[e]),e.includes("?")){const t=e.substring(e.indexOf("?")+1);this[a].ccall("wasm_set_query_string",null,[h],[t])}}#a(e,t){this[a].ccall("wasm_set_request_host",null,[h],[e]);let r;try{r=parseInt(new URL(e).port,10)}catch{}(!r||isNaN(r)||r===80)&&(r=t==="https"?443:80),this[a].ccall("wasm_set_request_port",null,[
|
|
40
|
+
`;this[a].ccall("wasm_set_phpini_entries",null,[h],[e])}this[a].ccall("php_wasm_init",null,[],[])}#n(){const e="/tmp/headers.json";if(!this.fileExists(e))throw new Error("SAPI Error: Could not find response headers file.");const t=JSON.parse(this.readFileAsText(e)),r={};for(const n of t.headers){if(!n.includes(": "))continue;const o=n.indexOf(": "),i=n.substring(0,o).toLowerCase(),l=n.substring(o+2);i in r||(r[i]=[]),r[i].push(l)}return{headers:r,httpStatusCode:t.status}}#i(e){if(this[a].ccall("wasm_set_request_uri",null,[h],[e]),e.includes("?")){const t=e.substring(e.indexOf("?")+1);this[a].ccall("wasm_set_query_string",null,[h],[t])}}#a(e,t){this[a].ccall("wasm_set_request_host",null,[h],[e]);let r;try{r=parseInt(new URL(e).port,10)}catch{}(!r||isNaN(r)||r===80)&&(r=t==="https"?443:80),this[a].ccall("wasm_set_request_port",null,[g],[r]),(t==="https"||!t&&r===443)&&this.addServerGlobalEntry("HTTPS","on")}#l(e){this[a].ccall("wasm_set_request_method",null,[h],[e])}#c(e){e.cookie&&this[a].ccall("wasm_set_cookies",null,[h],[e.cookie]),e["content-type"]&&this[a].ccall("wasm_set_content_type",null,[h],[e["content-type"]]),e["content-length"]&&this[a].ccall("wasm_set_content_length",null,[g],[parseInt(e["content-length"],10)]);for(const t in e)this.addServerGlobalEntry(`HTTP_${t.toUpperCase().replace(/-/g,"_")}`,e[t])}#h(e){this[a].ccall("wasm_set_request_body",null,[h],[e]),this[a].ccall("wasm_set_content_length",null,[g],[new TextEncoder().encode(e).length])}#u(e){this[a].ccall("wasm_set_path_translated",null,[h],[e])}addServerGlobalEntry(e,t){this.#r[e]=t}#d(){for(const e in this.#r)this[a].ccall("wasm_add_SERVER_entry",null,[h,h],[e,this.#r[e]])}#p(e){const{key:t,name:r,type:n,data:o}=e,i=`/tmp/${Math.random().toFixed(20)}`;this.writeFile(i,o);const l=0;this[a].ccall("wasm_add_uploaded_file",null,[h,h,h,h,g,g],[t,r,n,i,l,o.byteLength])}#f(e){this[a].ccall("wasm_set_php_code",null,[h],[e])}async#m(){let e,t;try{e=await new Promise((o,i)=>{t=c=>{const u=new Error("Rethrown");u.cause=c.error,u.betterMessage=c.message,i(u)},this.#s?.addEventListener("error",t);const l=this[a].ccall("wasm_sapi_handle_request",g,[],[]);return l instanceof Promise?l.then(o,i):o(l)})}catch(o){for(const u in this)typeof this[u]=="function"&&(this[u]=()=>{throw new Error("PHP runtime has crashed – see the earlier error for details.")});this.functionsMaybeMissingFromAsyncify=D();const i=o,l="betterMessage"in i?i.betterMessage:i.message,c=new Error(l);throw c.cause=i,c}finally{this.#s?.removeEventListener("error",t),this.#r={}}const{headers:r,httpStatusCode:n}=this.#n();return new y(n,r,this.readFileAsBuffer("/tmp/stdout"),this.readFileAsText("/tmp/stderr"),e)}mkdir(e){this[a].FS.mkdirTree(e)}mkdirTree(e){this.mkdir(e)}readFileAsText(e){return new TextDecoder().decode(this.readFileAsBuffer(e))}readFileAsBuffer(e){return this[a].FS.readFile(e)}writeFile(e,t){this[a].FS.writeFile(e,t)}unlink(e){this[a].FS.unlink(e)}mv(e,t){this[a].FS.mv(e,t)}rmdir(e,t={recursive:!0}){t?.recursive&&this.listFiles(e).forEach(r=>{const n=`${e}/${r}`;this.isDir(n)?this.rmdir(n,t):this.unlink(n)}),this[a].FS.rmdir(e)}listFiles(e){if(!this.fileExists(e))return[];try{return this[a].FS.readdir(e).filter(t=>t!=="."&&t!=="..")}catch(t){return console.error(t,{path:e}),[]}}isDir(e){return this.fileExists(e)?this[a].FS.isDir(this[a].FS.lookupPath(e).node.mode):!1}fileExists(e){try{return this[a].FS.lookupPath(e),!0}catch{return!1}}}f([p('Could not create directory "{path}"')],d.prototype,"mkdir",1);f([p('Could not create directory "{path}"')],d.prototype,"mkdirTree",1);f([p('Could not read "{path}"')],d.prototype,"readFileAsText",1);f([p('Could not read "{path}"')],d.prototype,"readFileAsBuffer",1);f([p('Could not write to "{path}"')],d.prototype,"writeFile",1);f([p('Could not unlink "{path}"')],d.prototype,"unlink",1);f([p('Could not move "{path}"')],d.prototype,"mv",1);f([p('Could not remove directory "{path}"')],d.prototype,"rmdir",1);f([p('Could not list files in "{path}"')],d.prototype,"listFiles",1);f([p('Could not stat "{path}"')],d.prototype,"isDir",1);f([p('Could not stat "{path}"')],d.prototype,"fileExists",1);function N(s){const e={};for(const t in s)e[t.toLowerCase()]=s[t];return e}function L(s){return!(s instanceof d)}function se(s){return!L(s)}exports.BasePHP=d;exports.DEFAULT_BASE_URL=A;exports.LatestSupportedPHPVersion=V;exports.PHPBrowser=U;exports.PHPRequestHandler=I;exports.PHPResponse=y;exports.SupportedPHPVersions=R;exports.SupportedPHPVersionsList=Y;exports.UnhandledRejectionsTarget=C;exports.__private__dont__use=a;exports.ensurePathPrefix=O;exports.isLocalPHP=L;exports.isRemotePHP=se;exports.loadPHPRuntime=X;exports.removePathPrefix=b;exports.rethrowFileSystemError=p;exports.toRelativeUrl=P;
|
package/index.js
CHANGED
|
@@ -50,7 +50,7 @@ function I(s) {
|
|
|
50
50
|
throw o;
|
|
51
51
|
if ("exitCode" in o && o?.exitCode === 0)
|
|
52
52
|
return;
|
|
53
|
-
const i =
|
|
53
|
+
const i = $(
|
|
54
54
|
o,
|
|
55
55
|
s.lastAsyncifyStackSource?.stack
|
|
56
56
|
);
|
|
@@ -71,9 +71,9 @@ let w = [];
|
|
|
71
71
|
function N() {
|
|
72
72
|
return w;
|
|
73
73
|
}
|
|
74
|
-
function
|
|
74
|
+
function $(s, e) {
|
|
75
75
|
if (s.message === "unreachable") {
|
|
76
|
-
let t =
|
|
76
|
+
let t = L;
|
|
77
77
|
e || (t += `
|
|
78
78
|
|
|
79
79
|
This stack trace is lacking. For a better one initialize
|
|
@@ -89,7 +89,7 @@ the PHP runtime with { debug: true }, e.g. PHPNode.load('8.1', { debug: true }).
|
|
|
89
89
|
}
|
|
90
90
|
return s.message;
|
|
91
91
|
}
|
|
92
|
-
const
|
|
92
|
+
const L = `
|
|
93
93
|
"unreachable" WASM instruction executed.
|
|
94
94
|
|
|
95
95
|
The typical reason is a PHP function missing from the ASYNCIFY_ONLY
|
|
@@ -143,12 +143,12 @@ function B(s) {
|
|
|
143
143
|
return [];
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
-
class
|
|
146
|
+
class g {
|
|
147
147
|
constructor(e, t, r, n = "", o = 0) {
|
|
148
148
|
this.httpStatusCode = e, this.headers = t, this.bytes = r, this.exitCode = o, this.errors = n;
|
|
149
149
|
}
|
|
150
150
|
static fromRawData(e) {
|
|
151
|
-
return new
|
|
151
|
+
return new g(
|
|
152
152
|
e.httpStatusCode,
|
|
153
153
|
e.headers,
|
|
154
154
|
e.bytes,
|
|
@@ -178,7 +178,7 @@ class y {
|
|
|
178
178
|
return new TextDecoder().decode(this.bytes);
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
|
-
const
|
|
181
|
+
const C = [
|
|
182
182
|
"8.2",
|
|
183
183
|
"8.1",
|
|
184
184
|
"8.0",
|
|
@@ -188,7 +188,7 @@ const F = [
|
|
|
188
188
|
"7.1",
|
|
189
189
|
"7.0",
|
|
190
190
|
"5.6"
|
|
191
|
-
], ee =
|
|
191
|
+
], ee = C[0], te = C;
|
|
192
192
|
class q {
|
|
193
193
|
#e;
|
|
194
194
|
#t;
|
|
@@ -384,13 +384,13 @@ class G {
|
|
|
384
384
|
#c(e) {
|
|
385
385
|
const t = `${this.#e}${e}`;
|
|
386
386
|
if (!this.php.fileExists(t))
|
|
387
|
-
return new
|
|
387
|
+
return new g(
|
|
388
388
|
404,
|
|
389
389
|
{},
|
|
390
390
|
new TextEncoder().encode("404 File not found")
|
|
391
391
|
);
|
|
392
392
|
const r = this.php.readFileAsBuffer(t);
|
|
393
|
-
return new
|
|
393
|
+
return new g(
|
|
394
394
|
200,
|
|
395
395
|
{
|
|
396
396
|
"content-length": [`${r.byteLength}`],
|
|
@@ -425,13 +425,13 @@ class G {
|
|
|
425
425
|
}, i = [];
|
|
426
426
|
if (e.files && Object.keys(e.files).length) {
|
|
427
427
|
n = "POST";
|
|
428
|
-
for (const
|
|
429
|
-
const
|
|
428
|
+
for (const u in e.files) {
|
|
429
|
+
const m = e.files[u];
|
|
430
430
|
i.push({
|
|
431
|
-
key:
|
|
432
|
-
name:
|
|
433
|
-
type:
|
|
434
|
-
data: new Uint8Array(await
|
|
431
|
+
key: u,
|
|
432
|
+
name: m.name,
|
|
433
|
+
type: m.type,
|
|
434
|
+
data: new Uint8Array(await m.arrayBuffer())
|
|
435
435
|
});
|
|
436
436
|
}
|
|
437
437
|
o["content-type"]?.startsWith("multipart/form-data") && (e.formData = V(
|
|
@@ -439,9 +439,20 @@ class G {
|
|
|
439
439
|
), o["content-type"] = "application/x-www-form-urlencoded", delete e.body);
|
|
440
440
|
}
|
|
441
441
|
let l;
|
|
442
|
-
|
|
442
|
+
e.formData !== void 0 ? (n = "POST", o["content-type"] = o["content-type"] || "application/x-www-form-urlencoded", l = new URLSearchParams(
|
|
443
443
|
e.formData
|
|
444
|
-
).toString()) : l = e.body
|
|
444
|
+
).toString()) : l = e.body;
|
|
445
|
+
let c;
|
|
446
|
+
try {
|
|
447
|
+
c = this.#u(t.pathname);
|
|
448
|
+
} catch {
|
|
449
|
+
return new g(
|
|
450
|
+
404,
|
|
451
|
+
{},
|
|
452
|
+
new TextEncoder().encode("404 File not found")
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
return await this.php.run({
|
|
445
456
|
relativeUri: z(
|
|
446
457
|
k(t),
|
|
447
458
|
this.#n
|
|
@@ -450,7 +461,7 @@ class G {
|
|
|
450
461
|
method: e.method || n,
|
|
451
462
|
body: l,
|
|
452
463
|
fileInfos: i,
|
|
453
|
-
scriptPath:
|
|
464
|
+
scriptPath: c,
|
|
454
465
|
headers: o
|
|
455
466
|
});
|
|
456
467
|
} finally {
|
|
@@ -463,13 +474,18 @@ class G {
|
|
|
463
474
|
* Fall back to index.php as if there was a url rewriting rule in place.
|
|
464
475
|
*
|
|
465
476
|
* @param requestedPath - The requested pathname.
|
|
477
|
+
* @throws {Error} If the requested path doesn't exist.
|
|
466
478
|
* @returns The resolved filesystem path.
|
|
467
479
|
*/
|
|
468
480
|
#u(e) {
|
|
469
481
|
let t = T(e, this.#n);
|
|
470
482
|
t.includes(".php") ? t = t.split(".php")[0] + ".php" : (t.endsWith("/") || (t += "/"), t.endsWith("index.php") || (t += "index.php"));
|
|
471
483
|
const r = `${this.#e}${t}`;
|
|
472
|
-
|
|
484
|
+
if (this.php.fileExists(r))
|
|
485
|
+
return r;
|
|
486
|
+
if (!this.php.fileExists(`${this.#e}/index.php`))
|
|
487
|
+
throw new Error(`File not found: ${r}`);
|
|
488
|
+
return `${this.#e}/index.php`;
|
|
473
489
|
}
|
|
474
490
|
}
|
|
475
491
|
function V(s) {
|
|
@@ -480,10 +496,10 @@ function V(s) {
|
|
|
480
496
|
return n.shift(), n.pop(), n.forEach((o) => {
|
|
481
497
|
const i = o.indexOf(`\r
|
|
482
498
|
\r
|
|
483
|
-
`), l = o.substring(0, i).trim(), c = o.substring(i + 4).trim(),
|
|
484
|
-
if (
|
|
485
|
-
const
|
|
486
|
-
e[
|
|
499
|
+
`), l = o.substring(0, i).trim(), c = o.substring(i + 4).trim(), u = l.match(/name="([^"]+)"/);
|
|
500
|
+
if (u) {
|
|
501
|
+
const m = u[1];
|
|
502
|
+
e[m] = c;
|
|
487
503
|
}
|
|
488
504
|
}), e;
|
|
489
505
|
}
|
|
@@ -527,7 +543,7 @@ function Y(s) {
|
|
|
527
543
|
return "application-octet-stream";
|
|
528
544
|
}
|
|
529
545
|
}
|
|
530
|
-
const
|
|
546
|
+
const F = {
|
|
531
547
|
0: "No error occurred. System call completed successfully.",
|
|
532
548
|
1: "Argument list too long.",
|
|
533
549
|
2: "Permission denied.",
|
|
@@ -614,9 +630,9 @@ function p(s = "") {
|
|
|
614
630
|
return o.apply(this, i);
|
|
615
631
|
} catch (l) {
|
|
616
632
|
const c = typeof l == "object" ? l?.errno : null;
|
|
617
|
-
if (c in
|
|
618
|
-
const
|
|
619
|
-
throw new Error(`${U}: ${
|
|
633
|
+
if (c in F) {
|
|
634
|
+
const u = F[c], m = typeof i[0] == "string" ? i[0] : null, U = m !== null ? s.replaceAll("{path}", m) : s;
|
|
635
|
+
throw new Error(`${U}: ${u}`, {
|
|
620
636
|
cause: l
|
|
621
637
|
});
|
|
622
638
|
}
|
|
@@ -665,8 +681,8 @@ var Q = Object.defineProperty, X = Object.getOwnPropertyDescriptor, f = (s, e, t
|
|
|
665
681
|
(i = s[o]) && (n = (r ? i(e, t, n) : i(n)) || n);
|
|
666
682
|
return r && n && Q(e, t, n), n;
|
|
667
683
|
};
|
|
668
|
-
const h = "string",
|
|
669
|
-
class
|
|
684
|
+
const h = "string", y = "number", a = Symbol("__private__dont__use");
|
|
685
|
+
class d {
|
|
670
686
|
/**
|
|
671
687
|
* Initializes a PHP runtime.
|
|
672
688
|
*
|
|
@@ -812,7 +828,7 @@ class u {
|
|
|
812
828
|
(!r || isNaN(r) || r === 80) && (r = t === "https" ? 443 : 80), this[a].ccall(
|
|
813
829
|
"wasm_set_request_port",
|
|
814
830
|
null,
|
|
815
|
-
[
|
|
831
|
+
[y],
|
|
816
832
|
[r]
|
|
817
833
|
), (t === "https" || !t && r === 443) && this.addServerGlobalEntry("HTTPS", "on");
|
|
818
834
|
}
|
|
@@ -838,7 +854,7 @@ class u {
|
|
|
838
854
|
), e["content-length"] && this[a].ccall(
|
|
839
855
|
"wasm_set_content_length",
|
|
840
856
|
null,
|
|
841
|
-
[
|
|
857
|
+
[y],
|
|
842
858
|
[parseInt(e["content-length"], 10)]
|
|
843
859
|
);
|
|
844
860
|
for (const t in e)
|
|
@@ -856,7 +872,7 @@ class u {
|
|
|
856
872
|
), this[a].ccall(
|
|
857
873
|
"wasm_set_content_length",
|
|
858
874
|
null,
|
|
859
|
-
[
|
|
875
|
+
[y],
|
|
860
876
|
[new TextEncoder().encode(e).length]
|
|
861
877
|
);
|
|
862
878
|
}
|
|
@@ -896,7 +912,7 @@ class u {
|
|
|
896
912
|
this[a].ccall(
|
|
897
913
|
"wasm_add_uploaded_file",
|
|
898
914
|
null,
|
|
899
|
-
[h, h, h, h,
|
|
915
|
+
[h, h, h, h, y, y],
|
|
900
916
|
[t, r, n, i, l, o.byteLength]
|
|
901
917
|
);
|
|
902
918
|
}
|
|
@@ -911,36 +927,25 @@ class u {
|
|
|
911
927
|
async #m() {
|
|
912
928
|
let e, t;
|
|
913
929
|
try {
|
|
914
|
-
e = await new Promise(
|
|
915
|
-
t = (
|
|
916
|
-
const
|
|
917
|
-
|
|
930
|
+
e = await new Promise((o, i) => {
|
|
931
|
+
t = (c) => {
|
|
932
|
+
const u = new Error("Rethrown");
|
|
933
|
+
u.cause = c.error, u.betterMessage = c.message, i(u);
|
|
918
934
|
}, this.#s?.addEventListener(
|
|
919
935
|
"error",
|
|
920
936
|
t
|
|
921
937
|
);
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
*/
|
|
930
|
-
await await this[a].ccall(
|
|
931
|
-
"wasm_sapi_handle_request",
|
|
932
|
-
m,
|
|
933
|
-
[],
|
|
934
|
-
[]
|
|
935
|
-
)
|
|
936
|
-
);
|
|
937
|
-
} catch (l) {
|
|
938
|
-
i(l);
|
|
939
|
-
}
|
|
938
|
+
const l = this[a].ccall(
|
|
939
|
+
"wasm_sapi_handle_request",
|
|
940
|
+
y,
|
|
941
|
+
[],
|
|
942
|
+
[]
|
|
943
|
+
);
|
|
944
|
+
return l instanceof Promise ? l.then(o, i) : o(l);
|
|
940
945
|
});
|
|
941
946
|
} catch (o) {
|
|
942
|
-
for (const
|
|
943
|
-
typeof this[
|
|
947
|
+
for (const u in this)
|
|
948
|
+
typeof this[u] == "function" && (this[u] = () => {
|
|
944
949
|
throw new Error(
|
|
945
950
|
"PHP runtime has crashed – see the earlier error for details."
|
|
946
951
|
);
|
|
@@ -952,7 +957,7 @@ class u {
|
|
|
952
957
|
this.#s?.removeEventListener("error", t), this.#r = {};
|
|
953
958
|
}
|
|
954
959
|
const { headers: r, httpStatusCode: n } = this.#n();
|
|
955
|
-
return new
|
|
960
|
+
return new g(
|
|
956
961
|
n,
|
|
957
962
|
r,
|
|
958
963
|
this.readFileAsBuffer("/tmp/stdout"),
|
|
@@ -1013,37 +1018,37 @@ class u {
|
|
|
1013
1018
|
}
|
|
1014
1019
|
f([
|
|
1015
1020
|
p('Could not create directory "{path}"')
|
|
1016
|
-
],
|
|
1021
|
+
], d.prototype, "mkdir", 1);
|
|
1017
1022
|
f([
|
|
1018
1023
|
p('Could not create directory "{path}"')
|
|
1019
|
-
],
|
|
1024
|
+
], d.prototype, "mkdirTree", 1);
|
|
1020
1025
|
f([
|
|
1021
1026
|
p('Could not read "{path}"')
|
|
1022
|
-
],
|
|
1027
|
+
], d.prototype, "readFileAsText", 1);
|
|
1023
1028
|
f([
|
|
1024
1029
|
p('Could not read "{path}"')
|
|
1025
|
-
],
|
|
1030
|
+
], d.prototype, "readFileAsBuffer", 1);
|
|
1026
1031
|
f([
|
|
1027
1032
|
p('Could not write to "{path}"')
|
|
1028
|
-
],
|
|
1033
|
+
], d.prototype, "writeFile", 1);
|
|
1029
1034
|
f([
|
|
1030
1035
|
p('Could not unlink "{path}"')
|
|
1031
|
-
],
|
|
1036
|
+
], d.prototype, "unlink", 1);
|
|
1032
1037
|
f([
|
|
1033
1038
|
p('Could not move "{path}"')
|
|
1034
|
-
],
|
|
1039
|
+
], d.prototype, "mv", 1);
|
|
1035
1040
|
f([
|
|
1036
1041
|
p('Could not remove directory "{path}"')
|
|
1037
|
-
],
|
|
1042
|
+
], d.prototype, "rmdir", 1);
|
|
1038
1043
|
f([
|
|
1039
1044
|
p('Could not list files in "{path}"')
|
|
1040
|
-
],
|
|
1045
|
+
], d.prototype, "listFiles", 1);
|
|
1041
1046
|
f([
|
|
1042
1047
|
p('Could not stat "{path}"')
|
|
1043
|
-
],
|
|
1048
|
+
], d.prototype, "isDir", 1);
|
|
1044
1049
|
f([
|
|
1045
1050
|
p('Could not stat "{path}"')
|
|
1046
|
-
],
|
|
1051
|
+
], d.prototype, "fileExists", 1);
|
|
1047
1052
|
function H(s) {
|
|
1048
1053
|
const e = {};
|
|
1049
1054
|
for (const t in s)
|
|
@@ -1051,19 +1056,19 @@ function H(s) {
|
|
|
1051
1056
|
return e;
|
|
1052
1057
|
}
|
|
1053
1058
|
function Z(s) {
|
|
1054
|
-
return !(s instanceof
|
|
1059
|
+
return !(s instanceof d);
|
|
1055
1060
|
}
|
|
1056
1061
|
function se(s) {
|
|
1057
1062
|
return !Z(s);
|
|
1058
1063
|
}
|
|
1059
1064
|
export {
|
|
1060
|
-
|
|
1065
|
+
d as BasePHP,
|
|
1061
1066
|
j as DEFAULT_BASE_URL,
|
|
1062
1067
|
ee as LatestSupportedPHPVersion,
|
|
1063
1068
|
q as PHPBrowser,
|
|
1064
1069
|
G as PHPRequestHandler,
|
|
1065
|
-
|
|
1066
|
-
|
|
1070
|
+
g as PHPResponse,
|
|
1071
|
+
C as SupportedPHPVersions,
|
|
1067
1072
|
te as SupportedPHPVersionsList,
|
|
1068
1073
|
O as UnhandledRejectionsTarget,
|
|
1069
1074
|
a as __private__dont__use,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/universal",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.46",
|
|
4
4
|
"description": "PHP.wasm – emscripten bindings for PHP",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"main": "./index.cjs",
|
|
37
37
|
"module": "./index.js",
|
|
38
38
|
"license": "GPL-2.0-or-later",
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "b1e1b4b57b00fb52429f0490af4ccb943f496f74"
|
|
40
40
|
}
|