drupflare/worker - v1.0.4
    Preparing search index...

    Variable CAPABILITY_CHECK_PHPConst

    CAPABILITY_CHECK_PHP: "\nuse Drupal\\Core\\DrupalKernel;\nuse Drupal\\Core\\Site\\Settings;\nuse Drupal\\drupflare\\Host;\nuse Drupal\\drupflare\\Logger\\CfwLogger;\nuse Drupal\\drupflare\\Plugin\\ImageToolkit\\CfwImageToolkit;\nuse Drupal\\drupflare\\Plugin\\Mail\\CfwMail;\nuse Drupal\\drupflare\\Queue\\CfwDeferredHttp;\nuse Drupal\\drupflare\\StreamWrapper\\HttpsStreamWrapper;\nuse GuzzleHttp\\Psr7\\Request as PsrRequest;\nuse Symfony\\Component\\HttpFoundation\\Request;\n\n// __CFW_FIBER_SHIM__\n// __CFW_HOST_HELPERS__\nchdir('/drupal');\n\n$checks = [];\n$assert = function (string $label, bool $ok, $detail = null) use (&$checks) {\n\t$checks[] = ['label' => $label, 'ok' => $ok, 'detail' => $detail];\n};\n\ntry {\n\tif (!isset($GLOBALS['__pw_autoloader']) || !is_object($GLOBALS['__pw_autoloader'])) {\n\t\t$GLOBALS['__pw_autoloader'] = require '/drupal/autoload.php';\n\t}\n\t$autoloader = $GLOBALS['__pw_autoloader'];\n\t// the pack does not enable this module, so nothing else registers its namespace\n\t$autoloader->addPsr4('Drupal\\\\drupflare\\\\', '/drupal/modules/custom/drupflare/src/');\n\t// its HttpsStreamWrapper extends the packaged one; composer never runs here, so this is the\n\t// autoloader entry composer would have written\n\t$autoloader->addPsr4('Drupflare\\\\StreamHttp\\\\', '/drupal/libraries/drupflare-stream-http/src/');\n\n\tif (!isset($GLOBALS['__pw_kernel'])) {\n\t\t$request = Request::create('/', 'GET');\n\t\t$kernel = new DrupalKernel('prod', $autoloader);\n\t\tDrupalKernel::bootEnvironment();\n\t\t$sitePath = DrupalKernel::findSitePath($request);\n\t\t$kernel->setSitePath($sitePath);\n\t\tSettings::initialize('/drupal', $sitePath, $autoloader);\n\t\t$kernel->boot();\n\t\t$GLOBALS['__pw_kernel'] = $kernel;\n\t}\n\n\t// #region Host, the single seam\n\t$assert('Host class loads', class_exists(Host::class));\n\tforeach (\n\t\t['cfwLog', 'cfwFetch', 'cfwMail', 'cfwImageUrl', 'cfwQueueFetch', 'cfwHttpCacheGet']\n\t\tas $fn\n\t) {\n\t\t$assert('Host::has(' . $fn . ')', Host::has($fn));\n\t}\n\t$assert(\n\t\t'Host::has() is FALSE for a capability the runtime did not install',\n\t\t!Host::has('cfwNotInstalled'),\n\t\t'the control: if this passed for everything, the six above would prove nothing',\n\t);\n\t$absent = Host::call('cfwNotInstalled', ['x' => 1]);\n\t$assert(\n\t\t'Host::call() on a missing capability returns a named refusal, not a throw',\n\t\t($absent['ok'] ?? null) === false &&\n\t\t\tstr_contains((string) ($absent['error'] ?? ''), 'not installed'),\n\t\t$absent['error'] ?? null,\n\t);\n\t// #endregion\n\n\t// #region the logger\n\t$parser = Drupal::service('logger.log_message_parser');\n\t$logger = new CfwLogger($parser);\n\t$marker = 'cfw-logger-' . bin2hex(random_bytes(4));\n\t$logger->log(3, 'placeholder @who saw @marker', [\n\t\t'@who' => 'the-test',\n\t\t'@marker' => $marker,\n\t\t'channel' => 'cfw-check',\n\t]);\n\t$assert('CfwLogger::log() executes without throwing', true, $marker);\n\t$logger->log(6, 'info level', ['channel' => 'cfw-check']);\n\n\t// and through Drupal's own channel, which is how it would actually be reached\n\tDrupal::service('logger.factory')->addLogger($logger);\n\t$channelMarker = 'cfw-channel-' . bin2hex(random_bytes(4));\n\tDrupal::logger('cfw-check')->warning('channel reached @m', ['@m' => $channelMarker]);\n\t$assert('CfwLogger receives entries through Drupal::logger()', true, $channelMarker);\n\t$GLOBALS['__cfw_markers'] = ['direct' => $marker, 'channel' => $channelMarker];\n\t// #endregion\n\n\t// #region the stream wrapper\n\t$before = stream_get_wrappers();\n\t// HttpsStreamWrapper's docblock says this runtime has no http/https wrapper,\n\t// citing a measured list of compress.zlib/php/file/glob/data. On static-free-v1\n\t// that is WRONG -- both are registered -- and the truth is worse than absence.\n\t// Reading through the native one throws 'ReferenceError: Asyncify is not defined'\n\t// out of the wasm import: a JS exception, so @ does not suppress it, a PHP catch\n\t// never sees it, and the whole invocation dies. Measured, and NOT reproduced here --\n\t// it would take this suite down with it. Route /__nativefetch reproduces it.\n\t$assert(\n\t\t'the runtime DOES register http/https, contradicting the class docblock',\n\t\tin_array('https', $before, true),\n\t\timplode(',', $before),\n\t);\n\t$registered = HttpsStreamWrapper::register();\n\t$after = stream_get_wrappers();\n\t$assert(\n\t\t'HttpsStreamWrapper registers both schemes',\n\t\t$registered === ['http', 'https'],\n\t\timplode(',', $registered),\n\t);\n\t$assert(\n\t\t'https is now a registered wrapper',\n\t\tin_array('https', $after, true),\n\t\timplode(',', $after),\n\t);\n\n\t$cachedUrl = getenv('CFW_TEST_URL') ?: 'https://example.com/';\n\t$body = @file_get_contents($cachedUrl);\n\t$assert(\n\t\t'file_get_contents() over the wrapper returns the prefetched body',\n\t\tis_string($body) && strlen($body) > 0,\n\t\tis_string($body) ? substr($body, 0, 80) : 'false',\n\t);\n\t$GLOBALS['__cfw_body_len'] = is_string($body) ? strlen($body) : -1;\n\n\t// fopen/fread/fseek, because file_get_contents() alone would not exercise them\n\t$fh = @fopen($cachedUrl, 'r');\n\t$assert('fopen() over the wrapper succeeds', is_resource($fh));\n\tif (is_resource($fh)) {\n\t\t$first = fread($fh, 8);\n\t\tfseek($fh, 0);\n\t\t$again = fread($fh, 8);\n\t\t$assert(\n\t\t\t'fread() returns bytes and fseek() rewinds',\n\t\t\t$first !== '' && $first === $again,\n\t\t\t$first,\n\t\t);\n\t\t$assert('feof() is false before the end', !feof($fh) || strlen((string) $body) <= 8);\n\t\t$stat = fstat($fh);\n\t\t$assert(\n\t\t\t'fstat() reports the fetched size',\n\t\t\tisset($stat['size']) && (int) $stat['size'] === strlen((string) $body),\n\t\t\t($stat['size'] ?? null) . ' vs ' . strlen((string) $body),\n\t\t);\n\t\tfclose($fh);\n\t}\n\n\t// the negative case, and the important one: a URL the host has not\n\t// prefetched must fail, because a Worker cannot fetch synchronously without JSPI\n\t$missing = 'https://example.invalid/never-prefetched-' . bin2hex(random_bytes(3));\n\t$missingBody = @file_get_contents($missing);\n\t$assert(\n\t\t'an unprefetched URL FAILS rather than returning something plausible',\n\t\t$missingBody === false,\n\t\tvar_export($missingBody, true),\n\t);\n\t// #endregion\n\n\t// #region mail\n\t$mailer = new CfwMail();\n\t$message = $mailer->format([\n\t\t'to' => '[email protected]',\n\t\t'subject' => 'Capability check',\n\t\t'body' => ['line one', 'line two'],\n\t\t'headers' => [\n\t\t\t'From' => '[email protected]',\n\t\t\t'Cc' => '[email protected]',\n\t\t\t'X-Ignored' => 'drop me',\n\t\t],\n\t\t'params' => [],\n\t]);\n\t$assert(\n\t\t'CfwMail::format() joins the body parts and wraps',\n\t\tis_string($message['body']) &&\n\t\t\tstr_contains($message['body'], 'line one') &&\n\t\t\tstr_contains($message['body'], 'line two'),\n\t\tsubstr((string) $message['body'], 0, 60),\n\t);\n\t$sent = $mailer->mail($message);\n\t$assert(\n\t\t'CfwMail::mail() returns a boolean rather than throwing when there is no binding',\n\t\tis_bool($sent),\n\t\tvar_export($sent, true),\n\t);\n\t$GLOBALS['__cfw_mail_result'] = $sent;\n\t// #endregion\n\n\t// #region deferred HTTP, the whole cached -> deferred layering\n\tif (class_exists(PsrRequest::class)) {\n\t\t$handler = new CfwDeferredHttp();\n\t\t$deferUrl = 'https://example.com/cfw-deferred-' . bin2hex(random_bytes(3));\n\t\t$response = $handler(new PsrRequest('GET', $deferUrl), [])->wait();\n\t\t$assert(\n\t\t\t'an uncached GET is DEFERRED with a 202 rather than blocking',\n\t\t\t$response->getStatusCode() === 202 &&\n\t\t\t\t$response->getHeaderLine('x-cfw-deferred') === 'queued',\n\t\t\t$response->getStatusCode() . ' ' . $response->getHeaderLine('x-cfw-deferred'),\n\t\t);\n\t\t$cachedResponse = $handler(new PsrRequest('GET', $cachedUrl), [])->wait();\n\t\t$assert(\n\t\t\t'a cached GET is answered from the cache with a real body',\n\t\t\t$cachedResponse->getStatusCode() === 200 &&\n\t\t\t\tstrlen((string) $cachedResponse->getBody()) > 0,\n\t\t\t$cachedResponse->getStatusCode() .\n\t\t\t\t' ' .\n\t\t\t\tstrlen((string) $cachedResponse->getBody()) .\n\t\t\t\t' bytes',\n\t\t);\n\t\t$GLOBALS['__cfw_deferred_url'] = $deferUrl;\n\t} else {\n\t\t$assert('GuzzleHttp is available for the deferred handler', false, 'class absent');\n\t}\n\t// #endregion\n\n\t// #region the image toolkit\n\t$assert(\n\t\t'CfwImageToolkit class loads against real Drupal',\n\t\tclass_exists(CfwImageToolkit::class),\n\t);\n\t$imageUrl = Host::call('cfwImageUrl', ['url' => '/sites/default/files/a.png', 'width' => 300]);\n\t$assert(\n\t\t'cfwImageUrl returns a delivery-time resizing URL',\n\t\t($imageUrl['ok'] ?? false) === true &&\n\t\t\tstr_contains((string) ($imageUrl['url'] ?? ''), 'width=300'),\n\t\t$imageUrl['url'] ?? null,\n\t);\n\t// #endregion\n} catch (Throwable $e) {\n\t$assert(\n\t\t'no exception escaped the capability check',\n\t\tfalse,\n\t\tget_class($e) . ': ' . $e->getMessage() . ' @ ' . $e->getFile() . ':' . $e->getLine(),\n\t);\n}\n\n$passed = count(array_filter($checks, fn($c) => $c['ok']));\necho json_encode([\n\t'passed' => $passed,\n\t'failed' => count($checks) - $passed,\n\t'markers' => $GLOBALS['__cfw_markers'] ?? null,\n\t'bodyLen' => $GLOBALS['__cfw_body_len'] ?? null,\n\t'mailResult' => $GLOBALS['__cfw_mail_result'] ?? null,\n\t'deferredUrl' => $GLOBALS['__cfw_deferred_url'] ?? null,\n\t'checks' => $checks,\n]);\n"