From 0ca10da166efe41ba08d03db0b565b8f727fc16a Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 17 Nov 2021 18:12:26 -0800 Subject: [PATCH] fix: compute file field mime type on the server (#10394) --- packages/playwright-core/src/client/fetch.ts | 8 +------- packages/playwright-core/src/protocol/channels.ts | 2 +- packages/playwright-core/src/protocol/protocol.yml | 2 +- packages/playwright-core/src/protocol/validator.ts | 2 +- packages/playwright-core/src/server/formData.ts | 7 ++++--- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts index a94ba2eedf..3419abae6f 100644 --- a/packages/playwright-core/src/client/fetch.ts +++ b/packages/playwright-core/src/client/fetch.ts @@ -16,7 +16,6 @@ import fs from 'fs'; import path from 'path'; -import * as mime from 'mime'; import * as util from 'util'; import { Serializable } from '../../types/structs'; import * as api from '../../types/types'; @@ -283,11 +282,7 @@ export class APIResponse implements api.APIResponse { } } -type ServerFilePayload = { - name: string, - mimeType: string, - buffer: string, -}; +type ServerFilePayload = NonNullable; function filePayloadToJson(payload: FilePayload): ServerFilePayload { return { @@ -307,7 +302,6 @@ async function readStreamToJson(stream: fs.ReadStream): Promise Validator): Scheme { value: tOptional(tString), file: tOptional(tObject({ name: tString, - mimeType: tString, + mimeType: tOptional(tString), buffer: tBinary, })), }); diff --git a/packages/playwright-core/src/server/formData.ts b/packages/playwright-core/src/server/formData.ts index b58d31a860..4aa88a8289 100644 --- a/packages/playwright-core/src/server/formData.ts +++ b/packages/playwright-core/src/server/formData.ts @@ -14,7 +14,8 @@ * limitations under the License. */ -import * as types from './types'; +import mime from 'mime'; +import * as channels from '../protocol/channels'; export class MultipartFormData { private readonly _boundary: string; @@ -35,10 +36,10 @@ export class MultipartFormData { this._finishMultiPartField(); } - addFileField(name: string, value: types.FilePayload) { + addFileField(name: string, value: NonNullable) { this._beginMultiPartHeader(name); this._chunks.push(Buffer.from(`; filename="${value.name}"`)); - this._chunks.push(Buffer.from(`\r\ncontent-type: ${value.mimeType || 'application/octet-stream'}`)); + this._chunks.push(Buffer.from(`\r\ncontent-type: ${value.mimeType || mime.getType(value.name) || 'application/octet-stream'}`)); this._finishMultiPartHeader(); this._chunks.push(Buffer.from(value.buffer, 'base64')); this._finishMultiPartField();