@php-wasm/cli 0.1.10 → 0.1.16
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/main.js +4 -4
- package/package.json +3 -3
package/main.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import{existsSync as b,writeFileSync as O}from"fs";import{rootCertificates as g}from"tls";import*as
|
|
1
|
+
import{existsSync as b,writeFileSync as E,readdirSync as O,lstatSync as L}from"fs";import{rootCertificates as g}from"tls";import*as W from"dns";import*as k from"util";import*as h from"net";import{createServer as _}from"net";import*as A from"http";import{WebSocketServer as N,WebSocket as S}from"ws";import{getPHPLoaderModule as R,loadPHPRuntime as x,PHP as U}from"@php-wasm/node";const D="modulepreload",$=function(e){return"/"+e},w={},I=function(c,r,t){if(!r||r.length===0)return c();const s=document.getElementsByTagName("link");return Promise.all(r.map(l=>{if(l=$(l),l in w)return;w[l]=!0;const f=l.endsWith(".css"),i=f?'[rel="stylesheet"]':"";if(!!t)for(let a=s.length-1;a>=0;a--){const o=s[a];if(o.href===l&&(!f||o.rel==="stylesheet"))return}else if(document.querySelector(`link[href="${l}"]${i}`))return;const n=document.createElement("link");if(n.rel=f?"stylesheet":D,f||(n.as="script",n.crossOrigin=""),n.href=l,document.head.appendChild(n),f)return new Promise((a,o)=>{n.addEventListener("load",a),n.addEventListener("error",()=>o(new Error(`Unable to preload CSS for ${l}`)))})})).then(()=>c())};function C(...e){process.env.DEV&&console.log(...e)}async function H(e){const c=[];for(let s=0;s<e;s++)c.push(M());const r=await Promise.all(c),t=[];for(const s of r){const l=s.address();t.push(l.port),s.close()}return t}function M(){return new Promise(e=>{const c=h.createServer();c.listen(0,()=>{e(c)})})}function v(...e){C("[WS Server]",...e)}const B=k.promisify(W.lookup);function V(e,c){if(typeof e=="string")e=String.fromCharCode(c)+e;else if(e instanceof ArrayBuffer){const r=new Uint8Array(e.byteLength+1);r[0]=c,r.set(new Uint8Array(e),1),e=r.buffer}else throw new Error("Unsupported chunk type");return e}const y=1,T=2;function q(e){return class extends e{constructor(){super(...arguments),this.CONNECTING=0,this.OPEN=1,this.CLOSING=2,this.CLOSED=3}send(r,t){return this.sendCommand(y,r,t)}setSocketOpt(r,t,s){return this.sendCommand(T,new Uint8Array([r,t,s]).buffer,()=>{})}sendCommand(r,t,s){return e.prototype.send.call(this,V(t,r),s)}}}function K(e,c="127.0.0.1"){v(`Binding the WebSockets server to ${c}:${e}...`);const r=A.createServer((t,s)=>{s.writeHead(403,{"Content-Type":"text/plain"}),s.write(`403 Permission Denied
|
|
2
2
|
Only websockets are allowed here.
|
|
3
|
-
`),
|
|
4
|
-
`));async function
|
|
5
|
-
`)),p.unshift("-d",`openssl.cafile=${P}`),i.cli(["php",...p]).catch(
|
|
3
|
+
`),s.end()});return new Promise(t=>{r.listen(e,c,function(){new N({server:r}).on("connection",F),t(r)})})}async function F(e,c){const r=e._socket.remoteAddress,t=function(...o){v(" "+r+": ",...o)};t("WebSocket connection from : "+r+" at URL "+(c?c.url:e.upgradeReq.url)),t("Version "+e.protocolVersion+", subprotocol: "+e.protocol);const s=new URL("ws://0.0.0.0"+c.url),l=Number(s.searchParams.get("port")),f=s.searchParams.get("host");if(!l||!f){t("Missing host or port information"),e.close(3e3);return}let i;const u=[];function n(){for(;u.length>0;){const o=u.pop(),d=o[0];t("flushing",{commandType:d},o),d===y?i.write(o.slice(1)):d===T?o[1]===1&&o[2]===9?i.setKeepAlive(o[3]):o[1]===6&&o[2]===1&&i.setNoDelay(o[3]):(t("Unknown command type: "+d),process.exit())}}e.on("message",function(o){u.unshift(o),i&&n()}),e.on("close",function(o,d){t("WebSocket client disconnected: "+o+" ["+d+"]"),i.end()}),e.on("error",function(o){t("WebSocket client error: "+o),i.end()});let a;h.isIP(f)===0?(t("resolving "+f+"... "),a=(await B(f)).address,t("resolved "+f+" -> "+a)):a=f,t("Opening a socket connection to "+a+":"+l),i=h.createConnection(l,a,function(){t("Connected to target"),n()}),i.on("data",function(o){try{e.send(o)}catch{t("Client closed, cleaning up target"),i.end()}}),i.on("end",function(){t("target disconnected"),e.close()}),i.on("error",function(o){t("target connection error",o),i.end(),e.close(3e3)})}function m(...e){C("[TCP Server]",...e)}function j(e,c){return class extends c{constructor(t,s){const l=t.port;t.port=e,G({tcpListenPort:l,wsConnectPort:e}),super(t,s)}}}function G(e){e={wsConnectHost:"127.0.0.1",...e};const{tcpListenPort:c,wsConnectHost:r,wsConnectPort:t}=e,s=_();s.on("connection",function(f){const i=[],u=new S(`ws://${r}:${t}/`);u.binaryType="arraybuffer";function n(a){u.send(new Uint8Array(a))}u.addEventListener("open",function(){for(m("Outbound WebSocket connection established");i.length>0;)n(i.shift())}),u.addEventListener("message",a=>{m("WS->TCP message:",new TextDecoder().decode(a.data)),f.write(Buffer.from(a.data))}),u.addEventListener("close",()=>{m("WebSocket connection closed"),f.end()}),f.on("data",function(a){if(m("TCP->WS message:",a),u.readyState===S.OPEN){for(;i.length>0;)n(i.shift());n(a)}else i.push(a)}),f.once("close",function(){m("TCP connection closed"),u.close()}),f.on("error",function(){m("TCP connection error"),u.close()})}),s.listen(c,function(){m("TCP server listening")})}let p=process.argv.slice(2);p.length||(p=["--help"]);const P=new URL("ca-bundle.crt",(import.meta||{}).url).pathname;b(P)||E(P,g.join(`
|
|
4
|
+
`));async function Q(){const e=await I(()=>import("./assets/php-e1f9edd4.js"),[]),c=process.env.PHP||"8.2",[r,t]=await H(2);await K(t);const s=await R(c),l=await x(s,{ENV:{...process.env,TERM:"xterm"},websocket:{url:(n,a,o)=>{const d=new URLSearchParams({host:a,port:o}).toString();return`ws://127.0.0.1:${t}/?${d}`},subprotocol:"binary",decorator:q,serverDecorator:j.bind(null,r)}});p.some(n=>n.startsWith("-c"))||p.unshift("-c",e);const i=new U(l),u=O("/").map(n=>`/${n}`).filter(n=>L(n).isDirectory());for(const n of u)i.fileExists(n)||i.mkdirTree(n),i.mount({root:n},n);i.chdir(process.cwd()),i.writeFile(P,g.join(`
|
|
5
|
+
`)),p.unshift("-d",`openssl.cafile=${P}`),i.cli(["php",...p]).catch(n=>{throw n.name==="ExitStatus"&&process.exit(n.status===void 0?1:n.status),n})}Q();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "PHP.wasm CLI for node.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
"type": "module",
|
|
24
24
|
"main": "main.js",
|
|
25
25
|
"bin": "cli.js",
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "8de6da5b33182c74df8144316ea9a67aaa96973f",
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"comlink": "4.4.1",
|
|
29
29
|
"ws": "8.13.0",
|
|
30
|
-
"@php-wasm/node": "0.1.
|
|
30
|
+
"@php-wasm/node": "0.1.16"
|
|
31
31
|
}
|
|
32
32
|
}
|