@wp-playground/common 3.0.15 → 3.0.17

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.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../../../packages/playground/common/src/create-memoized-fetch.ts","../../../../packages/playground/common/src/index.ts"],"sourcesContent":["export interface CacheEntry {\n\tresponsePromise: Promise<Response>;\n\tunlockedBodyStream?: ReadableStream<Uint8Array>;\n\tnextResponse: () => Promise<Response>;\n}\n\n/**\n * Creates a fetch function that memoizes the response stream.\n * Calling it twice will return a response with the same status,\n * headers, and the body stream.\n * Memoization is keyed by URL. Method, headers etc are ignored.\n *\n * @param originalFetch The fetch function to memoize. Defaults to the global fetch.\n */\nexport function createMemoizedFetch(\n\toriginalFetch: (\n\t\tinput: RequestInfo | URL,\n\t\tinit?: RequestInit\n\t) => Promise<Response> = fetch\n) {\n\tconst fetches: Record<string, CacheEntry> = {};\n\n\treturn async function memoizedFetch(url: string, options?: RequestInit) {\n\t\tif (!fetches[url]) {\n\t\t\tfetches[url] = {\n\t\t\t\tresponsePromise: originalFetch(url, options),\n\t\t\t\tasync nextResponse() {\n\t\t\t\t\t// Wait for \"result\" to be set.\n\t\t\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\t\t\tconst [left, right] =\n\t\t\t\t\t\tfetches[url].unlockedBodyStream!.tee();\n\t\t\t\t\tfetches[url].unlockedBodyStream = left;\n\t\t\t\t\treturn new Response(right, {\n\t\t\t\t\t\tstatus: response.status,\n\t\t\t\t\t\tstatusText: response.statusText,\n\t\t\t\t\t\theaders: response.headers,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\tfetches[url].unlockedBodyStream = response.body!;\n\t\t}\n\n\t\treturn fetches[url].nextResponse();\n\t};\n}\n","/**\n * Avoid adding new code here. @wp-playground/common should remain\n * as lean as possible.\n *\n * This package exists to avoid circular dependencies. Let's not\n * use it as a default place to add code that doesn't seem to fit\n * anywhere else. If there's no good place for your code, perhaps\n * it needs to be restructured? Or maybe there's a need for a new package?\n * Let's always consider these questions before adding new code here.\n */\n\nimport type { UniversalPHP } from '@php-wasm/universal';\nimport { phpVars } from '@php-wasm/util';\n\nexport { createMemoizedFetch } from './create-memoized-fetch';\n\nexport const RecommendedPHPVersion = '8.3';\n\n/**\n * Unzip a zip file inside Playground.\n */\nexport const unzipFile = async (\n\tphp: UniversalPHP,\n\tzipPath: string | File,\n\textractToPath: string,\n\toverwriteFiles = true\n) => {\n\t/**\n\t * Use a random file name to avoid conflicts across concurrent unzipFile()\n\t * calls.\n\t */\n\tconst tmpPath = `/tmp/file-${Math.random()}.zip`;\n\tif (zipPath instanceof File) {\n\t\tconst zipFile = zipPath;\n\t\tzipPath = tmpPath;\n\t\tawait php.writeFile(\n\t\t\tzipPath,\n\t\t\tnew Uint8Array(await zipFile.arrayBuffer())\n\t\t);\n\t}\n\tconst js = phpVars({\n\t\tzipPath,\n\t\textractToPath,\n\t\toverwriteFiles,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n function unzip($zipPath, $extractTo, $overwriteFiles = true)\n {\n if (!is_dir($extractTo)) {\n mkdir($extractTo, 0777, true);\n }\n $zip = new ZipArchive;\n $res = $zip->open($zipPath);\n if ($res === TRUE) {\n\t\t\t\tfor ($i = 0; $i < $zip->numFiles; $i++) {\n\t\t\t\t\t$filename = $zip->getNameIndex($i);\n\t\t\t\t\t$fileinfo = pathinfo($filename);\n\t\t\t\t\t$extractFilePath = rtrim($extractTo, '/') . '/' . $filename;\n\t\t\t\t\t// Check if file exists and $overwriteFiles is false\n\t\t\t\t\tif (!file_exists($extractFilePath) || $overwriteFiles) {\n\t\t\t\t\t\t// Extract file\n\t\t\t\t\t\t$zip->extractTo($extractTo, $filename);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$zip->close();\n\t\t\t\tchmod($extractTo, 0777);\n } else {\n throw new Exception(\"Could not unzip file: \" . $zip->getStatusString());\n }\n }\n unzip(${js.zipPath}, ${js.extractToPath}, ${js.overwriteFiles});\n `,\n\t});\n\tif (await php.fileExists(tmpPath)) {\n\t\tawait php.unlink(tmpPath);\n\t}\n};\n\nexport const zipDirectory = async (\n\tphp: UniversalPHP,\n\tdirectoryPath: string\n) => {\n\tconst outputPath = `/tmp/file${Math.random()}.zip`;\n\tconst js = phpVars({\n\t\tdirectoryPath,\n\t\toutputPath,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n\t\tfunction zipDirectory($directoryPath, $outputPath) {\n\t\t\t$zip = new ZipArchive;\n\t\t\t$res = $zip->open($outputPath, ZipArchive::CREATE);\n\t\t\tif ($res !== TRUE) {\n\t\t\t\tthrow new Exception('Failed to create ZIP');\n\t\t\t}\n\t\t\t$files = new RecursiveIteratorIterator(\n\t\t\t\tnew RecursiveDirectoryIterator($directoryPath)\n\t\t\t);\n\t\t\tforeach ($files as $file) {\n\t\t\t\t$file = strval($file);\n\t\t\t\tif (is_dir($file)) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t$zip->addFile($file, substr($file, strlen($directoryPath)));\n\t\t\t}\n\t\t\t$zip->close();\n\t\t\tchmod($outputPath, 0777);\n\t\t}\n\t\tzipDirectory(${js.directoryPath}, ${js.outputPath});\n\t\t`,\n\t});\n\n\tconst fileBuffer = await php.readFileAsBuffer(outputPath);\n\tphp.unlink(outputPath);\n\treturn fileBuffer;\n};\n"],"names":["createMemoizedFetch","originalFetch","fetches","url","options","response","left","right","RecommendedPHPVersion","unzipFile","php","zipPath","extractToPath","overwriteFiles","tmpPath","zipFile","js","phpVars","zipDirectory","directoryPath","outputPath","fileBuffer"],"mappings":"kHAcgB,SAAAA,EACfC,EAGyB,MACxB,CACD,MAAMC,EAAsC,CAAC,EAEtC,OAAA,eAA6BC,EAAaC,EAAuB,CACnE,GAAA,CAACF,EAAQC,CAAG,EAAG,CAClBD,EAAQC,CAAG,EAAI,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,EAAKC,EAAAA,QAAQ,CAClB,cAAAE,EACA,WAAAC,CAAA,CACA,EACD,MAAMV,EAAI,IAAI,CACb,KAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAoBSM,EAAG,aAAa,KAAKA,EAAG,UAAU;AAAA,GAAA,CAEjD,EAED,MAAMK,EAAa,MAAMX,EAAI,iBAAiBU,CAAU,EACxD,OAAAV,EAAI,OAAOU,CAAU,EACdC,CACR"}
1
+ {"version":3,"file":"index.cjs","sources":["../../../../packages/playground/common/src/create-memoized-fetch.ts","../../../../packages/playground/common/src/index.ts"],"sourcesContent":["export interface CacheEntry {\n\tresponsePromise: Promise<Response>;\n\tunlockedBodyStream?: ReadableStream<Uint8Array>;\n\tnextResponse: () => Promise<Response>;\n}\n\n/**\n * Creates a fetch function that memoizes the response stream.\n * Calling it twice will return a response with the same status,\n * headers, and the body stream.\n * Memoization is keyed by URL. Method, headers etc are ignored.\n *\n * @param originalFetch The fetch function to memoize. Defaults to the global fetch.\n */\nexport function createMemoizedFetch(\n\toriginalFetch: (\n\t\tinput: RequestInfo | URL,\n\t\tinit?: RequestInit\n\t) => Promise<Response> = fetch\n) {\n\tconst fetches: Record<string, CacheEntry> = {};\n\n\treturn async function memoizedFetch(url: string, options?: RequestInit) {\n\t\tif (!fetches[url]) {\n\t\t\tfetches[url] = {\n\t\t\t\tresponsePromise: originalFetch(url, options),\n\t\t\t\tasync nextResponse() {\n\t\t\t\t\t// Wait for \"result\" to be set.\n\t\t\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\t\t\tconst [left, right] =\n\t\t\t\t\t\tfetches[url].unlockedBodyStream!.tee();\n\t\t\t\t\tfetches[url].unlockedBodyStream = left;\n\t\t\t\t\treturn new Response(right, {\n\t\t\t\t\t\tstatus: response.status,\n\t\t\t\t\t\tstatusText: response.statusText,\n\t\t\t\t\t\theaders: response.headers,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\tfetches[url].unlockedBodyStream = response.body!;\n\t\t}\n\n\t\treturn fetches[url].nextResponse();\n\t};\n}\n","/**\n * Avoid adding new code here. @wp-playground/common should remain\n * as lean as possible.\n *\n * This package exists to avoid circular dependencies. Let's not\n * use it as a default place to add code that doesn't seem to fit\n * anywhere else. If there's no good place for your code, perhaps\n * it needs to be restructured? Or maybe there's a need for a new package?\n * Let's always consider these questions before adding new code here.\n */\n\nimport type { UniversalPHP } from '@php-wasm/universal';\nimport { phpVars } from '@php-wasm/util';\n\nexport { createMemoizedFetch } from './create-memoized-fetch';\n\nexport const RecommendedPHPVersion = '8.3';\n\n/**\n * Unzip a zip file inside Playground.\n */\nexport const unzipFile = async (\n\tphp: UniversalPHP,\n\tzipPath: string | File,\n\textractToPath: string,\n\toverwriteFiles = true\n) => {\n\t/**\n\t * Use a random file name to avoid conflicts across concurrent unzipFile()\n\t * calls.\n\t */\n\tconst tmpPath = `/tmp/file-${Math.random()}.zip`;\n\tif (zipPath instanceof File) {\n\t\tconst zipFile = zipPath;\n\t\tzipPath = tmpPath;\n\t\tawait php.writeFile(\n\t\t\tzipPath,\n\t\t\tnew Uint8Array(await zipFile.arrayBuffer())\n\t\t);\n\t}\n\tconst js = phpVars({\n\t\tzipPath,\n\t\textractToPath,\n\t\toverwriteFiles,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n function unzip($zipPath, $extractTo, $overwriteFiles = true)\n {\n if (!is_dir($extractTo)) {\n mkdir($extractTo, 0777, true);\n }\n $zip = new ZipArchive;\n $res = $zip->open($zipPath);\n if ($res === TRUE) {\n\t\t\t\tfor ($i = 0; $i < $zip->numFiles; $i++) {\n\t\t\t\t\t$filename = $zip->getNameIndex($i);\n\t\t\t\t\t$fileinfo = pathinfo($filename);\n\t\t\t\t\t$extractFilePath = rtrim($extractTo, '/') . '/' . $filename;\n\t\t\t\t\t// Check if file exists and $overwriteFiles is false\n\t\t\t\t\tif (!file_exists($extractFilePath) || $overwriteFiles) {\n\t\t\t\t\t\t// Extract file\n\t\t\t\t\t\t$zip->extractTo($extractTo, $filename);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$zip->close();\n\t\t\t\tchmod($extractTo, 0777);\n } else {\n throw new Exception(\"Could not unzip file: \" . $zip->getStatusString());\n }\n }\n unzip(${js.zipPath}, ${js.extractToPath}, ${js.overwriteFiles});\n `,\n\t});\n\tif (await php.fileExists(tmpPath)) {\n\t\tawait php.unlink(tmpPath);\n\t}\n};\n\nexport const zipDirectory = async (\n\tphp: UniversalPHP,\n\tdirectoryPath: string\n) => {\n\tconst outputPath = `/tmp/file${Math.random()}.zip`;\n\tconst js = phpVars({\n\t\tdirectoryPath,\n\t\toutputPath,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n\t\tfunction zipDirectory($directoryPath, $outputPath) {\n\t\t\t$zip = new ZipArchive;\n\t\t\t$res = $zip->open($outputPath, ZipArchive::CREATE);\n\t\t\tif ($res !== TRUE) {\n\t\t\t\tthrow new Exception('Failed to create ZIP');\n\t\t\t}\n\t\t\t$files = new RecursiveIteratorIterator(\n\t\t\t\tnew RecursiveDirectoryIterator($directoryPath)\n\t\t\t);\n\t\t\tforeach ($files as $file) {\n\t\t\t\t$file = strval($file);\n\t\t\t\tif (is_dir($file)) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t$zip->addFile($file, substr($file, strlen($directoryPath)));\n\t\t\t}\n\t\t\t$zip->close();\n\t\t\tchmod($outputPath, 0777);\n\t\t}\n\t\tzipDirectory(${js.directoryPath}, ${js.outputPath});\n\t\t`,\n\t});\n\n\tconst fileBuffer = await php.readFileAsBuffer(outputPath);\n\tphp.unlink(outputPath);\n\treturn fileBuffer;\n};\n"],"names":["createMemoizedFetch","originalFetch","fetches","url","options","response","left","right","RecommendedPHPVersion","unzipFile","php","zipPath","extractToPath","overwri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O,KAAKA,EAAG,aAAa,KAAKA,EAAG,cAAc;AAAA,SAAA,CAEnE,EACG,MAAMN,EAAI,WAAWI,CAAO,GAC/B,MAAMJ,EAAI,OAAOI,CAAO,CAE1B,EAEaI,EAAe,MAC3BR,EACAS,IACI,CACJ,MAAMC,EAAa,YAAY,KAAK,OAAA,CAAQ,OACtCJ,EAAKC,EAAAA,QAAQ,CAClB,cAAAE,EACA,WAAAC,CAAA,CACA,EACD,MAAMV,EAAI,IAAI,CACb,KAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAoBSM,EAAG,aAAa,KAAKA,EAAG,UAAU;AAAA,GAAA,CAEjD,EAED,MAAMK,EAAa,MAAMX,EAAI,iBAAiBU,CAAU,EACxD,OAAAV,EAAI,OAAOU,CAAU,EACdC,CACR"}
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../../packages/playground/common/src/create-memoized-fetch.ts","../../../../packages/playground/common/src/index.ts"],"sourcesContent":["export interface CacheEntry {\n\tresponsePromise: Promise<Response>;\n\tunlockedBodyStream?: ReadableStream<Uint8Array>;\n\tnextResponse: () => Promise<Response>;\n}\n\n/**\n * Creates a fetch function that memoizes the response stream.\n * Calling it twice will return a response with the same status,\n * headers, and the body stream.\n * Memoization is keyed by URL. Method, headers etc are ignored.\n *\n * @param originalFetch The fetch function to memoize. Defaults to the global fetch.\n */\nexport function createMemoizedFetch(\n\toriginalFetch: (\n\t\tinput: RequestInfo | URL,\n\t\tinit?: RequestInit\n\t) => Promise<Response> = fetch\n) {\n\tconst fetches: Record<string, CacheEntry> = {};\n\n\treturn async function memoizedFetch(url: string, options?: RequestInit) {\n\t\tif (!fetches[url]) {\n\t\t\tfetches[url] = {\n\t\t\t\tresponsePromise: originalFetch(url, options),\n\t\t\t\tasync nextResponse() {\n\t\t\t\t\t// Wait for \"result\" to be set.\n\t\t\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\t\t\tconst [left, right] =\n\t\t\t\t\t\tfetches[url].unlockedBodyStream!.tee();\n\t\t\t\t\tfetches[url].unlockedBodyStream = left;\n\t\t\t\t\treturn new Response(right, {\n\t\t\t\t\t\tstatus: response.status,\n\t\t\t\t\t\tstatusText: response.statusText,\n\t\t\t\t\t\theaders: response.headers,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\tfetches[url].unlockedBodyStream = response.body!;\n\t\t}\n\n\t\treturn fetches[url].nextResponse();\n\t};\n}\n","/**\n * Avoid adding new code here. @wp-playground/common should remain\n * as lean as possible.\n *\n * This package exists to avoid circular dependencies. Let's not\n * use it as a default place to add code that doesn't seem to fit\n * anywhere else. If there's no good place for your code, perhaps\n * it needs to be restructured? Or maybe there's a need for a new package?\n * Let's always consider these questions before adding new code here.\n */\n\nimport type { UniversalPHP } from '@php-wasm/universal';\nimport { phpVars } from '@php-wasm/util';\n\nexport { createMemoizedFetch } from './create-memoized-fetch';\n\nexport const RecommendedPHPVersion = '8.3';\n\n/**\n * Unzip a zip file inside Playground.\n */\nexport const unzipFile = async (\n\tphp: UniversalPHP,\n\tzipPath: string | File,\n\textractToPath: string,\n\toverwriteFiles = true\n) => {\n\t/**\n\t * Use a random file name to avoid conflicts across concurrent unzipFile()\n\t * calls.\n\t */\n\tconst tmpPath = `/tmp/file-${Math.random()}.zip`;\n\tif (zipPath instanceof File) {\n\t\tconst zipFile = zipPath;\n\t\tzipPath = tmpPath;\n\t\tawait php.writeFile(\n\t\t\tzipPath,\n\t\t\tnew Uint8Array(await zipFile.arrayBuffer())\n\t\t);\n\t}\n\tconst js = phpVars({\n\t\tzipPath,\n\t\textractToPath,\n\t\toverwriteFiles,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n function unzip($zipPath, $extractTo, $overwriteFiles = true)\n {\n if (!is_dir($extractTo)) {\n mkdir($extractTo, 0777, true);\n }\n $zip = new ZipArchive;\n $res = $zip->open($zipPath);\n if ($res === TRUE) {\n\t\t\t\tfor ($i = 0; $i < $zip->numFiles; $i++) {\n\t\t\t\t\t$filename = $zip->getNameIndex($i);\n\t\t\t\t\t$fileinfo = pathinfo($filename);\n\t\t\t\t\t$extractFilePath = rtrim($extractTo, '/') . '/' . $filename;\n\t\t\t\t\t// Check if file exists and $overwriteFiles is false\n\t\t\t\t\tif (!file_exists($extractFilePath) || $overwriteFiles) {\n\t\t\t\t\t\t// Extract file\n\t\t\t\t\t\t$zip->extractTo($extractTo, $filename);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$zip->close();\n\t\t\t\tchmod($extractTo, 0777);\n } else {\n throw new Exception(\"Could not unzip file: \" . $zip->getStatusString());\n }\n }\n unzip(${js.zipPath}, ${js.extractToPath}, ${js.overwriteFiles});\n `,\n\t});\n\tif (await php.fileExists(tmpPath)) {\n\t\tawait php.unlink(tmpPath);\n\t}\n};\n\nexport const zipDirectory = async (\n\tphp: UniversalPHP,\n\tdirectoryPath: string\n) => {\n\tconst outputPath = `/tmp/file${Math.random()}.zip`;\n\tconst js = phpVars({\n\t\tdirectoryPath,\n\t\toutputPath,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n\t\tfunction zipDirectory($directoryPath, $outputPath) {\n\t\t\t$zip = new ZipArchive;\n\t\t\t$res = $zip->open($outputPath, ZipArchive::CREATE);\n\t\t\tif ($res !== TRUE) {\n\t\t\t\tthrow new Exception('Failed to create ZIP');\n\t\t\t}\n\t\t\t$files = new RecursiveIteratorIterator(\n\t\t\t\tnew RecursiveDirectoryIterator($directoryPath)\n\t\t\t);\n\t\t\tforeach ($files as $file) {\n\t\t\t\t$file = strval($file);\n\t\t\t\tif (is_dir($file)) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t$zip->addFile($file, substr($file, strlen($directoryPath)));\n\t\t\t}\n\t\t\t$zip->close();\n\t\t\tchmod($outputPath, 0777);\n\t\t}\n\t\tzipDirectory(${js.directoryPath}, ${js.outputPath});\n\t\t`,\n\t});\n\n\tconst fileBuffer = await php.readFileAsBuffer(outputPath);\n\tphp.unlink(outputPath);\n\treturn fileBuffer;\n};\n"],"names":["createMemoizedFetch","originalFetch","fetches","url","options","response","left","right","RecommendedPHPVersion","unzipFile","php","zipPath","extractToPath","overwriteFiles","tmpPath","zipFile","js","phpVars","zipDirectory","directoryPath","outputPath","fileBuffer"],"mappings":";AAcgB,SAAAA,EACfC,IAGyB,OACxB;AACD,QAAMC,IAAsC,CAAC;AAEtC,SAAA,eAA6BC,GAAaC,GAAuB;AACnE,QAAA,CAACF,EAAQC,CAAG,GAAG;AAClB,MAAAD,EAAQC,CAAG,IAAI;AAAA,QACd,iBAAiBF,EAAcE,GAAKC,CAAO;AAAA,QAC3C,MAAM,eAAe;AAEpB,gBAAMC,IAAW,MAAMH,EAAQC,CAAG,EAAE,iBAC9B,CAACG,GAAMC,CAAK,IACjBL,EAAQC,CAAG,EAAE,mBAAoB,IAAI;AAC9B,iBAAAD,EAAAC,CAAG,EAAE,qBAAqBG,GAC3B,IAAI,SAASC,GAAO;AAAA,YAC1B,QAAQF,EAAS;AAAA,YACjB,YAAYA,EAAS;AAAA,YACrB,SAASA,EAAS;AAAA,UAAA,CAClB;AAAA,QAAA;AAAA,MAEH;AACA,YAAMA,IAAW,MAAMH,EAAQC,CAAG,EAAE;AAC5B,MAAAD,EAAAC,CAAG,EAAE,qBAAqBE,EAAS;AAAA,IAAA;AAGrC,WAAAH,EAAQC,CAAG,EAAE,aAAa;AAAA,EAClC;AACD;AC7BO,MAAMK,IAAwB,OAKxBC,IAAY,OACxBC,GACAC,GACAC,GACAC,IAAiB,OACb;AAKJ,QAAMC,IAAU,aAAa,KAAK,OAAA,CAAQ;AAC1C,MAAIH,aAAmB,MAAM;AAC5B,UAAMI,IAAUJ;AACN,IAAAA,IAAAG,GACV,MAAMJ,EAAI;AAAA,MACTC;AAAA,MACA,IAAI,WAAW,MAAMI,EAAQ,YAAa,CAAA;AAAA,IAC3C;AAAA,EAAA;AAED,QAAMC,IAAKC,EAAQ;AAAA,IAClB,SAAAN;AAAA,IACA,eAAAC;AAAA,IACA,gBAAAC;AAAA,EAAA,CACA;AACD,QAAMH,EAAI,IAAI;AAAA,IACb,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAyBQM,EAAG,OAAO,KAAKA,EAAG,aAAa,KAAKA,EAAG,cAAc;AAAA;AAAA,EAAA,CAEnE,GACG,MAAMN,EAAI,WAAWI,CAAO,KACzB,MAAAJ,EAAI,OAAOI,CAAO;AAE1B,GAEaI,IAAe,OAC3BR,GACAS,MACI;AACJ,QAAMC,IAAa,YAAY,KAAK,OAAA,CAAQ,QACtCJ,IAAKC,EAAQ;AAAA,IAClB,eAAAE;AAAA,IACA,YAAAC;AAAA,EAAA,CACA;AACD,QAAMV,EAAI,IAAI;AAAA,IACb,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAoBSM,EAAG,aAAa,KAAKA,EAAG,UAAU;AAAA;AAAA,EAAA,CAEjD;AAED,QAAMK,IAAa,MAAMX,EAAI,iBAAiBU,CAAU;AACxD,SAAAV,EAAI,OAAOU,CAAU,GACdC;AACR;"}
1
+ {"version":3,"file":"index.js","sources":["../../../../packages/playground/common/src/create-memoized-fetch.ts","../../../../packages/playground/common/src/index.ts"],"sourcesContent":["export interface CacheEntry {\n\tresponsePromise: Promise<Response>;\n\tunlockedBodyStream?: ReadableStream<Uint8Array>;\n\tnextResponse: () => Promise<Response>;\n}\n\n/**\n * Creates a fetch function that memoizes the response stream.\n * Calling it twice will return a response with the same status,\n * headers, and the body stream.\n * Memoization is keyed by URL. Method, headers etc are ignored.\n *\n * @param originalFetch The fetch function to memoize. Defaults to the global fetch.\n */\nexport function createMemoizedFetch(\n\toriginalFetch: (\n\t\tinput: RequestInfo | URL,\n\t\tinit?: RequestInit\n\t) => Promise<Response> = fetch\n) {\n\tconst fetches: Record<string, CacheEntry> = {};\n\n\treturn async function memoizedFetch(url: string, options?: RequestInit) {\n\t\tif (!fetches[url]) {\n\t\t\tfetches[url] = {\n\t\t\t\tresponsePromise: originalFetch(url, options),\n\t\t\t\tasync nextResponse() {\n\t\t\t\t\t// Wait for \"result\" to be set.\n\t\t\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\t\t\tconst [left, right] =\n\t\t\t\t\t\tfetches[url].unlockedBodyStream!.tee();\n\t\t\t\t\tfetches[url].unlockedBodyStream = left;\n\t\t\t\t\treturn new Response(right, {\n\t\t\t\t\t\tstatus: response.status,\n\t\t\t\t\t\tstatusText: response.statusText,\n\t\t\t\t\t\theaders: response.headers,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst response = await fetches[url].responsePromise;\n\t\t\tfetches[url].unlockedBodyStream = response.body!;\n\t\t}\n\n\t\treturn fetches[url].nextResponse();\n\t};\n}\n","/**\n * Avoid adding new code here. @wp-playground/common should remain\n * as lean as possible.\n *\n * This package exists to avoid circular dependencies. Let's not\n * use it as a default place to add code that doesn't seem to fit\n * anywhere else. If there's no good place for your code, perhaps\n * it needs to be restructured? Or maybe there's a need for a new package?\n * Let's always consider these questions before adding new code here.\n */\n\nimport type { UniversalPHP } from '@php-wasm/universal';\nimport { phpVars } from '@php-wasm/util';\n\nexport { createMemoizedFetch } from './create-memoized-fetch';\n\nexport const RecommendedPHPVersion = '8.3';\n\n/**\n * Unzip a zip file inside Playground.\n */\nexport const unzipFile = async (\n\tphp: UniversalPHP,\n\tzipPath: string | File,\n\textractToPath: string,\n\toverwriteFiles = true\n) => {\n\t/**\n\t * Use a random file name to avoid conflicts across concurrent unzipFile()\n\t * calls.\n\t */\n\tconst tmpPath = `/tmp/file-${Math.random()}.zip`;\n\tif (zipPath instanceof File) {\n\t\tconst zipFile = zipPath;\n\t\tzipPath = tmpPath;\n\t\tawait php.writeFile(\n\t\t\tzipPath,\n\t\t\tnew Uint8Array(await zipFile.arrayBuffer())\n\t\t);\n\t}\n\tconst js = phpVars({\n\t\tzipPath,\n\t\textractToPath,\n\t\toverwriteFiles,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n function unzip($zipPath, $extractTo, $overwriteFiles = true)\n {\n if (!is_dir($extractTo)) {\n mkdir($extractTo, 0777, true);\n }\n $zip = new ZipArchive;\n $res = $zip->open($zipPath);\n if ($res === TRUE) {\n\t\t\t\tfor ($i = 0; $i < $zip->numFiles; $i++) {\n\t\t\t\t\t$filename = $zip->getNameIndex($i);\n\t\t\t\t\t$fileinfo = pathinfo($filename);\n\t\t\t\t\t$extractFilePath = rtrim($extractTo, '/') . '/' . $filename;\n\t\t\t\t\t// Check if file exists and $overwriteFiles is false\n\t\t\t\t\tif (!file_exists($extractFilePath) || $overwriteFiles) {\n\t\t\t\t\t\t// Extract file\n\t\t\t\t\t\t$zip->extractTo($extractTo, $filename);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$zip->close();\n\t\t\t\tchmod($extractTo, 0777);\n } else {\n throw new Exception(\"Could not unzip file: \" . $zip->getStatusString());\n }\n }\n unzip(${js.zipPath}, ${js.extractToPath}, ${js.overwriteFiles});\n `,\n\t});\n\tif (await php.fileExists(tmpPath)) {\n\t\tawait php.unlink(tmpPath);\n\t}\n};\n\nexport const zipDirectory = async (\n\tphp: UniversalPHP,\n\tdirectoryPath: string\n) => {\n\tconst outputPath = `/tmp/file${Math.random()}.zip`;\n\tconst js = phpVars({\n\t\tdirectoryPath,\n\t\toutputPath,\n\t});\n\tawait php.run({\n\t\tcode: `<?php\n\t\tfunction zipDirectory($directoryPath, $outputPath) {\n\t\t\t$zip = new ZipArchive;\n\t\t\t$res = $zip->open($outputPath, ZipArchive::CREATE);\n\t\t\tif ($res !== TRUE) {\n\t\t\t\tthrow new Exception('Failed to create ZIP');\n\t\t\t}\n\t\t\t$files = new RecursiveIteratorIterator(\n\t\t\t\tnew RecursiveDirectoryIterator($directoryPath)\n\t\t\t);\n\t\t\tforeach ($files as $file) {\n\t\t\t\t$file = strval($file);\n\t\t\t\tif (is_dir($file)) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t$zip->addFile($file, substr($file, strlen($directoryPath)));\n\t\t\t}\n\t\t\t$zip->close();\n\t\t\tchmod($outputPath, 0777);\n\t\t}\n\t\tzipDirectory(${js.directoryPath}, ${js.outputPath});\n\t\t`,\n\t});\n\n\tconst fileBuffer = await php.readFileAsBuffer(outputPath);\n\tphp.unlink(outputPath);\n\treturn fileBuffer;\n};\n"],"names":["createMemoizedFetch","originalFetch","fetches","url","options","response","left","right","RecommendedPHPVersion","unzipFile","php","zipPath","extractToPath","overwriteFiles","tmpPath","zipFile","js","phpVars","zipDirectory","directoryPath","outputPath","fileBuffer"],"mappings":";AAcO,SAASA,EACfC,IAGyB,OACxB;AACD,QAAMC,IAAsC,CAAA;AAE5C,SAAO,eAA6BC,GAAaC,GAAuB;AACvE,QAAI,CAACF,EAAQC,CAAG,GAAG;AAClB,MAAAD,EAAQC,CAAG,IAAI;AAAA,QACd,iBAAiBF,EAAcE,GAAKC,CAAO;AAAA,QAC3C,MAAM,eAAe;AAEpB,gBAAMC,IAAW,MAAMH,EAAQC,CAAG,EAAE,iBAC9B,CAACG,GAAMC,CAAK,IACjBL,EAAQC,CAAG,EAAE,mBAAoB,IAAA;AAClC,iBAAAD,EAAQC,CAAG,EAAE,qBAAqBG,GAC3B,IAAI,SAASC,GAAO;AAAA,YAC1B,QAAQF,EAAS;AAAA,YACjB,YAAYA,EAAS;AAAA,YACrB,SAASA,EAAS;AAAA,UAAA,CAClB;AAAA,QACF;AAAA,MAAA;AAED,YAAMA,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,EAAG,UAAU;AAAA;AAAA,EAAA,CAEjD;AAED,QAAMK,IAAa,MAAMX,EAAI,iBAAiBU,CAAU;AACxD,SAAAV,EAAI,OAAOU,CAAU,GACdC;AACR;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-playground/common",
3
- "version": "3.0.15",
3
+ "version": "3.0.17",
4
4
  "description": "Common exports and utilities for WordPress Playground",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,11 +35,11 @@
35
35
  "access": "public",
36
36
  "directory": "../../../dist/packages/playground/common"
37
37
  },
38
- "gitHead": "4e8addef4a0fa0e76f26785689a1a676bbb823e1",
38
+ "gitHead": "11e92b1431bd01ee27049678aec2932b8b4d9f44",
39
39
  "dependencies": {
40
40
  "ini": "4.1.2",
41
- "@php-wasm/universal": "3.0.15",
42
- "@php-wasm/util": "3.0.15"
41
+ "@php-wasm/universal": "3.0.17",
42
+ "@php-wasm/util": "3.0.17"
43
43
  },
44
44
  "packageManager": "npm@10.9.2",
45
45
  "overrides": {