-
Notifications
You must be signed in to change notification settings - Fork 941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(roll): roll Playwright to 1.40.0 #2170
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,15 @@ | |
import os | ||
import sys | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, List, Optional, Union | ||
from typing import TYPE_CHECKING, Dict, List, Optional, Union, cast | ||
|
||
if sys.version_info >= (3, 8): # pragma: no cover | ||
from typing import TypedDict | ||
else: # pragma: no cover | ||
from typing_extensions import TypedDict | ||
|
||
from playwright._impl._connection import Channel, from_channel | ||
from playwright._impl._helper import Error, async_readfile | ||
from playwright._impl._helper import Error | ||
from playwright._impl._writable_stream import WritableStream | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
|
@@ -34,81 +34,61 @@ | |
SIZE_LIMIT_IN_BYTES = 50 * 1024 * 1024 | ||
|
||
|
||
class InputFilesList(TypedDict): | ||
class InputFilesList(TypedDict, total=False): | ||
streams: Optional[List[Channel]] | ||
localPaths: Optional[List[str]] | ||
files: Optional[List[FilePayload]] | ||
payloads: Optional[List[Dict[str, Union[str, bytes]]]] | ||
|
||
|
||
async def convert_input_files( | ||
files: Union[str, Path, FilePayload, List[Union[str, Path]], List[FilePayload]], | ||
context: "BrowserContext", | ||
) -> InputFilesList: | ||
file_list = files if isinstance(files, list) else [files] | ||
items = files if isinstance(files, list) else [files] | ||
|
||
total_buffer_size_exceeds_limit = ( | ||
sum( | ||
[ | ||
len(f.get("buffer", "")) | ||
for f in file_list | ||
if not isinstance(f, (str, Path)) | ||
] | ||
) | ||
> SIZE_LIMIT_IN_BYTES | ||
) | ||
if total_buffer_size_exceeds_limit: | ||
raise Error( | ||
"Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead." | ||
) | ||
|
||
total_file_size_exceeds_limit = ( | ||
sum([os.stat(f).st_size for f in file_list if isinstance(f, (str, Path))]) | ||
> SIZE_LIMIT_IN_BYTES | ||
) | ||
if total_file_size_exceeds_limit: | ||
if any([isinstance(item, (str, Path)) for item in items]): | ||
if not all([isinstance(item, (str, Path)) for item in items]): | ||
raise Error("File paths cannot be mixed with buffers") | ||
if context._channel._connection.is_remote: | ||
streams = [] | ||
for file in file_list: | ||
assert isinstance(file, (str, Path)) | ||
for item in items: | ||
assert isinstance(item, (str, Path)) | ||
last_modified_ms = int(os.path.getmtime(item) * 1000) | ||
stream: WritableStream = from_channel( | ||
await context._channel.send( | ||
"createTempFile", {"name": os.path.basename(file)} | ||
"createTempFile", | ||
{ | ||
"name": os.path.basename(item), | ||
"lastModifiedMs": last_modified_ms, | ||
}, | ||
) | ||
) | ||
await stream.copy(file) | ||
await stream.copy(item) | ||
streams.append(stream._channel) | ||
return InputFilesList(streams=streams, localPaths=None, files=None) | ||
local_paths = [] | ||
for p in file_list: | ||
assert isinstance(p, (str, Path)) | ||
local_paths.append(str(Path(p).absolute().resolve())) | ||
return InputFilesList(streams=None, localPaths=local_paths, files=None) | ||
return InputFilesList(streams=streams) | ||
return InputFilesList( | ||
localPaths=[ | ||
str(Path(cast(Union[str, Path], item)).absolute().resolve()) | ||
for item in items | ||
] | ||
) | ||
|
||
return InputFilesList( | ||
streams=None, localPaths=None, files=await _normalize_file_payloads(files) | ||
file_payload_exceeds_size_limit = ( | ||
sum([len(f.get("buffer", "")) for f in items if not isinstance(f, (str, Path))]) | ||
> SIZE_LIMIT_IN_BYTES | ||
) | ||
if file_payload_exceeds_size_limit: | ||
raise Error( | ||
"Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead." | ||
) | ||
|
||
|
||
async def _normalize_file_payloads( | ||
files: Union[str, Path, FilePayload, List[Union[str, Path]], List[FilePayload]] | ||
) -> List: | ||
file_list = files if isinstance(files, list) else [files] | ||
file_payloads: List = [] | ||
for item in file_list: | ||
if isinstance(item, (str, Path)): | ||
file_payloads.append( | ||
{ | ||
"name": os.path.basename(item), | ||
"buffer": base64.b64encode(await async_readfile(item)).decode(), | ||
} | ||
) | ||
else: | ||
file_payloads.append( | ||
{ | ||
"name": item["name"], | ||
"mimeType": item["mimeType"], | ||
"buffer": base64.b64encode(item["buffer"]).decode(), | ||
} | ||
) | ||
|
||
return file_payloads | ||
return InputFilesList( | ||
payloads=[ | ||
{ | ||
"name": item["name"], | ||
"mimeType": item["mimeType"], | ||
"buffer": base64.b64encode(item["buffer"]).decode(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is it encode than decode, is it just the python way to do base64 encoding? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. b64encode returns bytes which we need to decode to a str. |
||
} | ||
for item in cast(List[FilePayload], items) | ||
] | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might follow up and do it like in .NET that we remove None by default in send().