diff --git a/packages/playwright-core/src/client/elementHandle.ts b/packages/playwright-core/src/client/elementHandle.ts index 3d0ddfd7fee024..9ca79fc1f57ca6 100644 --- a/packages/playwright-core/src/client/elementHandle.ts +++ b/packages/playwright-core/src/client/elementHandle.ts @@ -267,12 +267,11 @@ export async function convertInputFiles(files: string | FilePayload | string[] | throw new Error('File paths must be all files or a single directory'); if (context._connection.isRemote()) { - let streams: channels.WritableStreamChannel[] | undefined; - let localPaths: string[] | undefined; + const streams: channels.WritableStreamChannel[] = []; await Promise.all((items as string[]).map(async item => { const isDirectory = (await fs.promises.stat(item)).isDirectory(); - const files = isDirectory ? (await fs.promises.readdir(item, { withFileTypes: true, recursive: true })).filter(f => f.isFile()).map(f => path.join(item, f.name)) : [item]; - const { writableStreams, remoteDir } = await context._wrapApiCall(async () => context._channel.createTempFiles({ + const files = isDirectory ? (await fs.promises.readdir(item, { withFileTypes: true, recursive: true })).filter(f => f.isFile()).map(f => path.join(f.path, f.name)) : [item]; + const { writableStreams } = await context._wrapApiCall(async () => context._channel.createTempFiles({ rootDirName: isDirectory ? item : undefined, items: await Promise.all(files.map(f => fileToTempFileParams(f))), }), true); @@ -280,15 +279,9 @@ export async function convertInputFiles(files: string | FilePayload | string[] | const writable = WritableStream.from(writableStreams[i]); await pipelineAsync(fs.createReadStream(files[i]), writable.stream()); } - if (isDirectory) { - localPaths ??= []; - localPaths.push(remoteDir); - } else { - streams ??= []; - streams.push(...writableStreams); - } + streams.push(...writableStreams); })); - return { streams, localPaths }; + return { streams }; } return { localPaths: items.map(f => path.resolve(f as string)) as string[] }; } diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts index d80f331ae41cb1..ef0d85c5f2ed1a 100644 --- a/packages/playwright-core/src/protocol/validator.ts +++ b/packages/playwright-core/src/protocol/validator.ts @@ -959,7 +959,6 @@ scheme.BrowserContextCreateTempFilesParams = tObject({ })), }); scheme.BrowserContextCreateTempFilesResult = tObject({ - remoteDir: tString, writableStreams: tArray(tChannel(['WritableStream'])), }); scheme.BrowserContextUpdateSubscriptionParams = tObject({ diff --git a/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts b/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts index 458cfefa1d7471..6fc312bc2475c5 100644 --- a/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts @@ -184,11 +184,10 @@ export class BrowserContextDispatcher extends Dispatcher { await fs.promises.mkdir(path.dirname(path.join(tempDirWithRootName, item.name)), { recursive: true }); const file = fs.createWriteStream(path.join(tempDirWithRootName, item.name)); - return new WritableStreamDispatcher(this, file, item.lastModifiedMs); + return new WritableStreamDispatcher(this, file, item.lastModifiedMs, params.rootDirName ? tempDirWithRootName : undefined); })) }; } diff --git a/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts b/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts index 70d480aab0640b..f2ab6400365d12 100644 --- a/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts +++ b/packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts @@ -23,10 +23,12 @@ import type { BrowserContextDispatcher } from './browserContextDispatcher'; export class WritableStreamDispatcher extends Dispatcher<{ guid: string, stream: fs.WriteStream }, channels.WritableStreamChannel, BrowserContextDispatcher> implements channels.WritableStreamChannel { _type_WritableStream = true; private _lastModifiedMs: number | undefined; + private _rootDir: string | undefined; - constructor(scope: BrowserContextDispatcher, stream: fs.WriteStream, lastModifiedMs?: number) { + constructor(scope: BrowserContextDispatcher, stream: fs.WriteStream, lastModifiedMs?: number, rootDir?: string) { super(scope, { guid: 'writableStream@' + createGuid(), stream }, 'WritableStream', {}); this._lastModifiedMs = lastModifiedMs; + this._rootDir = rootDir; } async write(params: channels.WritableStreamWriteParams): Promise { @@ -51,4 +53,8 @@ export class WritableStreamDispatcher extends Dispatcher<{ guid: string, stream: path(): string { return this._object.stream.path as string; } + + rootDir(): string | undefined { + return this._rootDir; + } } diff --git a/packages/playwright-core/src/server/fileUploadUtils.ts b/packages/playwright-core/src/server/fileUploadUtils.ts index 89fc0cf7e18e99..402fe2fcd61f25 100644 --- a/packages/playwright-core/src/server/fileUploadUtils.ts +++ b/packages/playwright-core/src/server/fileUploadUtils.ts @@ -36,8 +36,13 @@ export async function prepareFilesForUpload(frame: Frame, params: channels.Eleme if ([payloads, localPaths, streams].filter(Boolean).length !== 1) throw new Error('Exactly one of payloads, localPaths and streams must be provided'); - if (streams) - localPaths = streams.map(c => (c as WritableStreamDispatcher).path()); + if (streams) { + const directoryMode = streams.every(c => (c as WritableStreamDispatcher).rootDir()); + if (directoryMode) + localPaths = Array.from(new Set(streams.map(c => (c as WritableStreamDispatcher).rootDir()!))); + else + localPaths = streams.map(c => (c as WritableStreamDispatcher).path()); + } if (localPaths) { for (const p of localPaths) assert(path.isAbsolute(p) && path.resolve(p) === p, 'Paths provided to localPaths must be absolute and fully resolved.'); diff --git a/packages/protocol/src/channels.ts b/packages/protocol/src/channels.ts index ecec62b35bbf26..2f6dc635315891 100644 --- a/packages/protocol/src/channels.ts +++ b/packages/protocol/src/channels.ts @@ -1748,7 +1748,6 @@ export type BrowserContextCreateTempFilesOptions = { rootDirName?: string, }; export type BrowserContextCreateTempFilesResult = { - remoteDir: string, writableStreams: WritableStreamChannel[], }; export type BrowserContextUpdateSubscriptionParams = { diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml index d36a7bad22b503..db0afbb9d7ebea 100644 --- a/packages/protocol/src/protocol.yml +++ b/packages/protocol/src/protocol.yml @@ -1195,7 +1195,6 @@ BrowserContext: name: string lastModifiedMs: number? returns: - remoteDir: string writableStreams: type: array items: WritableStream