php-wasm icon indicating copy to clipboard operation
php-wasm copied to clipboard

Issues with PHP CGI Server.

Open MarketingPip opened this issue 1 year ago • 14 comments

Get a vague error message saying the request was denied etc using the demo / example..

MarketingPip avatar Aug 30 '24 00:08 MarketingPip

hi, how do you setup PHP CGI? i was curious about it. I can see the demo of laravel etc, but dont know how to setup. thanks

blackjyn avatar Sep 10 '24 05:09 blackjyn

@blackjyn - seek the documentation it should help you out.... If no luck - lmk.

MarketingPip avatar Sep 10 '24 07:09 MarketingPip

on this one right? https://github.com/seanmorris/php-wasm/blob/master/demo-node

blackjyn avatar Sep 10 '24 23:09 blackjyn

Assuming you are trying to run it in the web. This is the one you need - here

MarketingPip avatar Sep 11 '24 00:09 MarketingPip

@MarketingPip Are you running the CGI servier in a browser or in nodejs?

seanmorris avatar Oct 11 '24 08:10 seanmorris

@seanmorris - browser on incognito environment (storage issue?)

MarketingPip avatar Oct 11 '24 08:10 MarketingPip

@seanmorris - browser on incognito environment (storage issue?)

What's the error you're getting?

seanmorris avatar Oct 11 '24 15:10 seanmorris

@seanmorris - afk this weekend but my apologizes for not noting error earlier.

I do believe it was a "No response" or something error. I can confirm this by Monday etc.

MarketingPip avatar Oct 12 '24 00:10 MarketingPip

I am getting these errors with the following js and webpack config

Image

Register service worker:

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/build/sw.js') 
    .then((registration) => {
        console.log('Service Worker registered:', registration);
    })
    .catch((error) => {
        console.error('Service Worker registration failed:', error);
    });
}

sw.ts

/// <reference lib="WebWorker" />
import { PhpCgiWorker } from 'php-cgi-wasm/PhpCgiWorker';

declare const self: ServiceWorkerGlobalScope;

// Spawn the PHP-CGI binary
const php = new PhpCgiWorker({
  prefix: '/php-wasm',
  docroot: '/persist/www',
  types: {
    jpeg: 'image/jpeg',
    jpg: 'image/jpeg',
    gif: 'image/gif',
    png: 'image/png',
    svg: 'image/svg+xml',
  },
});

// Set up the event handlers
self.addEventListener('install', (event) => php.handleInstallEvent(event));
self.addEventListener('activate', (event) => php.handleActivateEvent(event));
self.addEventListener('fetch', (event) => php.handleFetchEvent(event));
self.addEventListener('message', (event) => php.handleMessageEvent(event));

webpack config:

import path from 'path';
import { Configuration } from 'webpack';

const config: Configuration = {
  entry: {
    sw: './resources/js/sw.ts',
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'public/build'),
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
    alias: {
      '@': path.resolve(__dirname, 'resources/js'),
    },
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env', '@babel/preset-typescript'],
          },
        },
      },
    ],
  },
  target: 'webworker',
  mode: 'development',
  devtool: 'source-map',
};

export default config;

ZebTheWizard avatar Jan 23 '25 01:01 ZebTheWizard

@ZebTheWizard - refer to this here

MarketingPip avatar Jan 23 '25 01:01 MarketingPip

I'm confused. The docs say I should be able to run php-cgi-wasm in the browser, but php-web-cgi.mjs clearly has a few references to XMLHttpRequest, and web workers do not have access to DOM APIs. My webpack config is based off of the webpack configuration here

Having a hard time compiling the example service worker.

ZebTheWizard avatar Jan 23 '25 15:01 ZebTheWizard

I'm confused. The docs say I should be able to run php-cgi-wasm in the browser, but php-web-cgi.mjs clearly has a few references to XMLHttpRequest, and web workers do not have access to DOM APIs. My webpack config is based off of the webpack configuration here

Having a hard time compiling the example service worker.

It seems like I need to include webpack-dev-server in my webpack config to remove the errors.

However, now I cannot get a response when sending a message to the worker.

import { sendMessageFor, onMessage } from 'php-cgi-wasm/msg-bus';

navigator.serviceWorker.addEventListener('message', onMessage);
const sendMessage = sendMessageFor(location.origin + '/build/sw.js')

        
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/build/sw.js') 
    .then((registration) => {
        console.log('Service Worker registered:', registration.active);
        
        sendMessage('readdir', ['/persist']).then(console.log).catch(console.error)
    })
    .catch((error) => {
        console.error('Service Worker registration failed:', error);
    });
}

ZebTheWizard avatar Jan 23 '25 17:01 ZebTheWizard

@ZebTheWizard - I am not sure. Open a separate issue and maybe the creator of this repo will help you.

MarketingPip avatar Jan 23 '25 21:01 MarketingPip

@ZebTheWizard - I am not sure. Open a separate issue and maybe the creator of this repo will help you.

Alright thanks, I've added a separate issue https://github.com/seanmorris/php-wasm/issues/77 since I'm trying to test a simple implementation of CGI web worker.

Seems like webpack-dev-server is required and I'm currently on version 0.0.9-alpha-27

ZebTheWizard avatar Jan 23 '25 22:01 ZebTheWizard