From 6f7c5dba247b2110bad28e91737688d87af14f79 Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Thu, 21 Mar 2024 17:26:15 +0000 Subject: [PATCH] WP-NOW: Use rotatePHPRuntime to avoid memory error (#152) * use the new function rotatePHPRuntime to mitigate the memory error. --- packages/wp-now/src/wp-now.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/wp-now/src/wp-now.ts b/packages/wp-now/src/wp-now.ts index d95ac898..4dd02db5 100644 --- a/packages/wp-now/src/wp-now.ts +++ b/packages/wp-now/src/wp-now.ts @@ -1,5 +1,6 @@ import fs from 'fs-extra'; -import { NodePHP, PHPLoaderOptions } from '@php-wasm/node'; +import { NodePHP, PHPRequestHandlerConfiguration } from '@php-wasm/node'; +import { rotatePHPRuntime } from '@php-wasm/universal'; import path from 'path'; import { SQLITE_FILENAME } from './constants'; import { @@ -42,20 +43,23 @@ export default async function startWPNow( options: Partial = {} ): Promise<{ php: NodePHP; phpInstances: NodePHP[]; options: WPNowOptions }> { const { documentRoot } = options; - const nodePHPOptions: PHPLoaderOptions = { - requestHandler: { - documentRoot, - absoluteUrl: options.absoluteUrl, - }, + const nodePHPOptions: PHPRequestHandlerConfiguration = { + documentRoot, + absoluteUrl: options.absoluteUrl, }; const phpInstances = []; + const loadRuntime = async () => + await NodePHP.loadRuntime(options.phpVersion); for (let i = 0; i < Math.max(options.numberOfPhpInstances, 1); i++) { - phpInstances.push( - await NodePHP.load(options.phpVersion, nodePHPOptions) - ); + phpInstances.push(new NodePHP(await loadRuntime(), nodePHPOptions)); } const php = phpInstances[0]; + await rotatePHPRuntime({ + php, + recreateRuntime: loadRuntime, + maxRequests: 100, + }); phpInstances.forEach((_php) => { _php.mkdirTree(documentRoot);