diff --git a/package-lock.json b/package-lock.json index 445143ad..d74ad7ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,6 @@ "compression": "1.7.4", "copy-webpack-plugin": "11.0.0", "express": "4.19.2", - "express-fileupload": "1.4.0", "file-saver": "^2.0.5", "follow-redirects": "1.15.6", "fs-extra": "11.1.1", @@ -79,7 +78,6 @@ "@rollup/plugin-url": "^8.0.1", "@testing-library/react": "14.0.0", "@tsconfig/docusaurus": "^1.0.5", - "@types/express-fileupload": "1.5.0", "@types/file-saver": "^2.0.5", "@types/jest": "^29.4.0", "@types/node": "18.14.2", @@ -12481,15 +12479,6 @@ "@types/node": "*" } }, - "node_modules/@types/busboy": { - "version": "1.5.3", - "resolved": "https://registry.npmjs.org/@types/busboy/-/busboy-1.5.3.tgz", - "integrity": "sha512-YMBLFN/xBD8bnqywIlGyYqsNFXu6bsiY7h3Ae0kO17qEuTjsqeyYMRPSUDacIKIquws2Y6KjmxAyNx8xB3xQbw==", - "dev": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@types/chai": { "version": "4.3.5", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.5.tgz", @@ -12568,16 +12557,6 @@ "@types/serve-static": "*" } }, - "node_modules/@types/express-fileupload": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/@types/express-fileupload/-/express-fileupload-1.5.0.tgz", - "integrity": "sha512-Y9v88IC5ItAxkKwfnyIi1y0jSZwTMY4jqXUQLZ3jFhYJlLdRnN919bKBNM8jbVVD2cxywA/uEC1kNNpZQGwx7Q==", - "dev": true, - "dependencies": { - "@types/busboy": "*", - "@types/express": "*" - } - }, "node_modules/@types/express-serve-static-core": { "version": "4.17.35", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.35.tgz", @@ -19973,17 +19952,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/busboy": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", - "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", - "dependencies": { - "streamsearch": "^1.1.0" - }, - "engines": { - "node": ">=10.16.0" - } - }, "node_modules/byte-size": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-7.0.0.tgz", @@ -24951,17 +24919,6 @@ "node": ">= 0.10.0" } }, - "node_modules/express-fileupload": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.4.0.tgz", - "integrity": "sha512-RjzLCHxkv3umDeZKeFeMg8w7qe0V09w3B7oGZprr/oO2H/ISCgNzuqzn7gV3HRWb37GjRk429CCpSLS2KNTqMQ==", - "dependencies": { - "busboy": "^1.6.0" - }, - "engines": { - "node": ">=12.0.0" - } - }, "node_modules/express/node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -43458,14 +43415,6 @@ "node": ">= 0.4" } }, - "node_modules/streamsearch": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", - "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", - "engines": { - "node": ">=10.0.0" - } - }, "node_modules/streamx": { "version": "2.15.6", "resolved": "https://registry.npmjs.org/streamx/-/streamx-2.15.6.tgz", diff --git a/package.json b/package.json index 79566415..6a09e966 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "compression": "1.7.4", "copy-webpack-plugin": "11.0.0", "express": "4.19.2", - "express-fileupload": "1.4.0", "file-saver": "^2.0.5", "follow-redirects": "1.15.6", "fs-extra": "11.1.1", @@ -81,7 +80,6 @@ "@rollup/plugin-url": "^8.0.1", "@testing-library/react": "14.0.0", "@tsconfig/docusaurus": "^1.0.5", - "@types/express-fileupload": "1.5.0", "@types/file-saver": "^2.0.5", "@types/jest": "^29.4.0", "@types/node": "18.14.2", diff --git a/packages/wp-now/src/encode-as-multipart.ts b/packages/wp-now/src/encode-as-multipart.ts deleted file mode 100644 index 5917b573..00000000 --- a/packages/wp-now/src/encode-as-multipart.ts +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Encodes the Express request with files into multipart/form-data request body. - * Adaptation of https://github.com/WordPress/wordpress-playground/blob/45bf16970867cc6f23738224dacf201905adcab6/packages/php-wasm/universal/src/lib/encode-as-multipart.ts - */ -export async function encodeAsMultipart(req: Express.Request) { - const boundary = `----${Math.random().toString(36).slice(2)}`; - const contentType = `multipart/form-data; boundary=${boundary}`; - - const textEncoder = new TextEncoder(); - const parts: (string | Uint8Array)[] = []; - const data = (req as any).body as Record; - for (const [name, value] of Object.entries(data)) { - parts.push(`--${boundary}\r\n`); - parts.push(`Content-Disposition: form-data; name="${name}"`); - parts.push(`\r\n`); - parts.push(`\r\n`); - parts.push(value); - parts.push(`\r\n`); - } - const files = req.files; - for (const [name, value] of Object.entries(files)) { - if (!Array.isArray(value)) { - parts.push(`--${boundary}\r\n`); - parts.push(`Content-Disposition: form-data; name="${name}"`); - parts.push(`; filename="${value.name}"`); - parts.push(`\r\n`); - parts.push(`Content-Type: application/octet-stream`); - parts.push(`\r\n`); - parts.push(`\r\n`); - parts.push(value.data); - parts.push(`\r\n`); - } - } - parts.push(`--${boundary}--\r\n`); - - const length = parts.reduce((acc, part) => acc + part.length, 0); - const bytes = new Uint8Array(length); - let offset = 0; - for (const part of parts) { - bytes.set( - typeof part === 'string' ? textEncoder.encode(part) : part, - offset - ); - offset += part.length; - } - - return { bytes, contentType }; -} diff --git a/packages/wp-now/src/start-server.ts b/packages/wp-now/src/start-server.ts index 1bb89457..64cf0e46 100644 --- a/packages/wp-now/src/start-server.ts +++ b/packages/wp-now/src/start-server.ts @@ -4,14 +4,12 @@ import { HTTPMethod } from '@php-wasm/universal'; import express from 'express'; import compression from 'compression'; import compressible from 'compressible'; -import fileUpload from 'express-fileupload'; import { portFinder } from './port-finder'; import { NodePHP } from '@php-wasm/node'; import { isWebContainer } from '@webcontainer/env'; import startWPNow from './wp-now'; import { output } from './output'; import { addTrailingSlash } from './add-trailing-slash'; -import { encodeAsMultipart } from './encode-as-multipart'; const requestBodyToBytes = async (req): Promise => await new Promise((resolve) => { @@ -46,7 +44,6 @@ export async function startServer( ); } const app = express(); - app.use(fileUpload()); app.use(compression({ filter: shouldCompress })); app.use(addTrailingSlash('/wp-admin')); const port = await portFinder.getOpenPort(); @@ -62,23 +59,11 @@ export async function startServer( } } - let body: Uint8Array; - if ( - requestHeaders['content-type']?.startsWith( - 'multipart/form-data' - ) - ) { - const multipart = await encodeAsMultipart(req); - body = multipart.bytes; - requestHeaders['content-type'] = multipart.contentType; - } else { - body = await requestBodyToBytes(req); - } const data = { url: req.url, headers: requestHeaders, method: req.method as HTTPMethod, - body, + body: await requestBodyToBytes(req), }; if (isWebContainer()) {