Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into v21
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Nov 14, 2024
2 parents 84d8ea0 + 399890f commit 0516b73
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 72 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ using (var browser = await PuppeteerSharp.Puppeteer.ConnectAsync(options))

## Sponsors

A massive thanks to [AWS](https://github.com/aws), who sponsored Puppeteer-sharp from Nov 2023 via the [.NET on AWS Open Source Software Fund](https://github.com/aws/dotnet-foss) and [JetBrains](https://www.jetbrains.com/?from=PuppeteerSharp) for a community Resharper and Rider license to use on this project.
A massive thanks to [JetBrains](https://www.jetbrains.com/?from=PuppeteerSharp) for a community Resharper and Rider license to use on this project.

<div style="display:inline">
<img src="https://raw.githubusercontent.com/aaubry/YamlDotNet/master/Sponsors/aws-logo-small.png" width="200" height="200"/>
Expand All @@ -172,6 +172,6 @@ A massive thanks to [AWS](https://github.com/aws), who sponsored Puppeteer-sharp

And a huge thanks to everyone who sponsors this project through [Github sponsors](https://github.com/sponsors/hardkoded):

<!-- sponsors --><a href="https://github.com/tolgabalci"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;2467618?u&#x3D;8ed50176b40b4e869c95c4c53137ab6a8a5fe1b1&amp;v&#x3D;4" width="60px" alt="Tolga Balci" /></a><a href="https://github.com/nogginbox"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;729381?v&#x3D;4" width="60px" alt="Richard Garside" /></a><a href="https://github.com/aws"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;2232217?v&#x3D;4" width="60px" alt="Amazon Web Services" /></a><!-- sponsors -->
<!-- sponsors --><a href="https://github.com/tolgabalci"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;2467618?u&#x3D;8ed50176b40b4e869c95c4c53137ab6a8a5fe1b1&amp;v&#x3D;4" width="60px" alt="Tolga Balci" /></a><a href="https://github.com/nogginbox"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;729381?v&#x3D;4" width="60px" alt="Richard Garside" /></a><!-- sponsors -->


2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/BrowserData/Chrome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class Chrome
/// <summary>
/// Default chrome build.
/// </summary>
public static string DefaultBuildId => "129.0.6668.100";
public static string DefaultBuildId => "130.0.6723.69";

internal static async Task<string> ResolveBuildIdAsync(ChromeReleaseChannel channel)
=> (await GetLastKnownGoodReleaseForChannel(channel).ConfigureAwait(false)).Version;
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Cdp/CdpHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal CdpHttpRequest(
Url = data.Request.Url;
ResourceType = data.Type ?? ResourceType.Other;
Method = data.Request.Method;
PostData = data.Request.PostData?.ToString();
PostData = data.Request.PostData;
HasPostData = data.Request.HasPostData ?? false;

Frame = frame;
Expand Down
56 changes: 0 additions & 56 deletions lib/PuppeteerSharp/Cdp/CdpPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,62 +418,6 @@ await Task.WhenAll(
return navigationTask.Result;
}

/// <inheritdoc/>
public override async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)
{
var timeout = options?.Timeout ?? DefaultTimeout;
var idleTime = options?.IdleTime ?? 500;

var networkIdleTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

var idleTimer = new Timer { Interval = idleTime, };

idleTimer.Elapsed += (_, _) => { networkIdleTcs.TrySetResult(true); };

var networkManager = FrameManager.NetworkManager;

void Evaluate()
{
idleTimer.Stop();

if (networkManager.NumRequestsInProgress == 0)
{
idleTimer.Start();
}
}

void RequestEventListener(object sender, RequestEventArgs e) => Evaluate();
void ResponseEventListener(object sender, ResponseCreatedEventArgs e) => Evaluate();

void Cleanup()
{
idleTimer.Stop();
idleTimer.Dispose();

networkManager.Request -= RequestEventListener;
networkManager.Response -= ResponseEventListener;
}

networkManager.Request += RequestEventListener;
networkManager.Response += ResponseEventListener;

Evaluate();

await Task.WhenAny(networkIdleTcs.Task, SessionClosedTask).WithTimeout(timeout, t =>
{
Cleanup();

return new TimeoutException($"Timeout of {t.TotalMilliseconds} ms exceeded");
}).ConfigureAwait(false);

Cleanup();

if (SessionClosedTask.IsFaulted)
{
await SessionClosedTask.ConfigureAwait(false);
}
}

/// <inheritdoc/>
public override async Task<IRequest> WaitForRequestAsync(Func<IRequest, bool> predicate, WaitForOptions options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Cdp/Messaging/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class Request
{
public HttpMethod Method { get; set; }

public object PostData { get; set; }
public string PostData { get; set; }

public Dictionary<string, string> Headers { get; set; } = [];

Expand Down
3 changes: 0 additions & 3 deletions lib/PuppeteerSharp/Cdp/NetworkEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ internal class NetworkEventManager
private readonly ConcurrentDictionary<string, List<RedirectInfo>> _queuedRedirectInfoMap = new();
private readonly ConcurrentDictionary<string, List<ResponseReceivedExtraInfoResponse>> _responseReceivedExtraInfoMap = new();

public int NumRequestsInProgress
=> _httpRequestsMap.Values.Count(r => r.Response == null);

internal void Forget(string requestId)
{
_requestWillBeSentMap.TryRemove(requestId, out _);
Expand Down
2 changes: 0 additions & 2 deletions lib/PuppeteerSharp/Cdp/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ internal NetworkManager(IFrameProvider frameManager, ILoggerFactory loggerFactor

internal Dictionary<string, string> ExtraHTTPHeaders => _extraHTTPHeaders?.Clone();

internal int NumRequestsInProgress => _networkEventManager.NumRequestsInProgress;

internal Task AddClientAsync(ICDPSession client)
{
if (_clients.ContainsKey(client))
Expand Down
68 changes: 66 additions & 2 deletions lib/PuppeteerSharp/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using PuppeteerSharp.Cdp;
using PuppeteerSharp.Cdp.Messaging;
using PuppeteerSharp.Helpers;
Expand Down Expand Up @@ -41,10 +43,14 @@ public abstract class Page : IPage

private readonly TaskQueue _screenshotTaskQueue;
private readonly ConcurrentSet<Func<IRequest, Task>> _requestInterceptionTask = [];
private readonly ConcurrentSet<IRequest> _requests = new();
private readonly TaskCompletionSource<bool> _closeTaskCompletionSource =
new(TaskCreationOptions.RunContinuationsAsynchronously);

internal Page(TaskQueue screenshotTaskQueue)
{
_screenshotTaskQueue = screenshotTaskQueue;
Request += (_, e) => _requests.Add(e.Request);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -228,6 +234,9 @@ public int DefaultTimeout
/// </summary>
protected ScreenshotOptions ScreenshotBurstModeOptions { get; set; }

private int NumRequestsInProgress
=> _requests.Count(r => r.Response == null);

/// <inheritdoc/>
public abstract Task SetGeolocationAsync(GeolocationOption options);

Expand Down Expand Up @@ -683,7 +692,58 @@ public Task<IResponse> WaitForNavigationAsync(NavigationOptions options = null)
=> MainFrame.WaitForNavigationAsync(options);

/// <inheritdoc/>
public abstract Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null);
public async Task WaitForNetworkIdleAsync(WaitForNetworkIdleOptions options = null)
{
var timeout = options?.Timeout ?? DefaultTimeout;
var idleTime = options?.IdleTime ?? 500;

var networkIdleTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

var idleTimer = new Timer { Interval = idleTime, };

idleTimer.Elapsed += (_, _) => { networkIdleTcs.TrySetResult(true); };

void Evaluate()
{
idleTimer.Stop();

if (NumRequestsInProgress <= (options?.Concurrency ?? 0))
{
idleTimer.Start();
}
}

void RequestEventListener(object sender, RequestEventArgs e) => Evaluate();
void ResponseEventListener(object sender, ResponseCreatedEventArgs e) => Evaluate();

void Cleanup()
{
idleTimer.Stop();
idleTimer.Dispose();

Request -= RequestEventListener;
Response -= ResponseEventListener;
}

Request += RequestEventListener;
Response += ResponseEventListener;

Evaluate();

await Task.WhenAny(networkIdleTcs.Task, _closeTaskCompletionSource.Task).WithTimeout(timeout, t =>
{
Cleanup();

return new TimeoutException($"Timeout of {t.TotalMilliseconds} ms exceeded");
}).ConfigureAwait(false);

Cleanup();

if (_closeTaskCompletionSource.Task.IsFaulted)
{
await _closeTaskCompletionSource.Task.ConfigureAwait(false);
}
}

/// <inheritdoc/>
public Task<IRequest> WaitForRequestAsync(string url, WaitForOptions options = null)
Expand Down Expand Up @@ -885,7 +945,11 @@ protected virtual void Dispose(bool disposing)
/// <summary>
/// Raises the <see cref="Close"/> event.
/// </summary>
protected void OnClose() => Close?.Invoke(this, EventArgs.Empty);
protected void OnClose()
{
_closeTaskCompletionSource?.TrySetException(new TargetClosedException("Target closed", "Session closed"));
Close?.Invoke(this, EventArgs.Empty);
}

/// <summary>
/// Raises the <see cref="Console"/> event.
Expand Down
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/PuppeteerSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<Description>Headless Browser .NET API</Description>
<PackageId>PuppeteerSharp</PackageId>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageVersion>20.0.3</PackageVersion>
<ReleaseVersion>20.0.3</ReleaseVersion>
<AssemblyVersion>20.0.3</AssemblyVersion>
<FileVersion>20.0.3</FileVersion>
<PackageVersion>20.0.5</PackageVersion>
<ReleaseVersion>20.0.5</ReleaseVersion>
<AssemblyVersion>20.0.5</AssemblyVersion>
<FileVersion>20.0.5</FileVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
<DebugType>embedded</DebugType>
Expand Down
21 changes: 21 additions & 0 deletions lib/PuppeteerSharp/SecurityDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,27 @@ public SecurityDetails()
/// <param name="validTo">Valid to.</param>
/// <param name="protocol">Protocol.</param>
public SecurityDetails(string subjectName, string issuer, long validFrom, long validTo, string protocol)
: this(subjectName, issuer, validFrom, validTo, protocol, [])
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityDetails"/> class.
/// </summary>
/// <param name="subjectName">Subject name.</param>
/// <param name="issuer">Issuer.</param>
/// <param name="validFrom">Valid from.</param>
/// <param name="validTo">Valid to.</param>
/// <param name="protocol">Protocol.</param>
/// <param name="subjectAlternativeNames">Subject alternative names.</param>
public SecurityDetails(string subjectName, string issuer, long validFrom, long validTo, string protocol, string[] subjectAlternativeNames)
{
SubjectName = subjectName;
Issuer = issuer;
ValidFrom = validFrom;
ValidTo = validTo;
Protocol = protocol;
SubjectAlternativeNames = subjectAlternativeNames;
}

/// <summary>
Expand Down Expand Up @@ -59,5 +74,11 @@ public SecurityDetails(string subjectName, string issuer, long validFrom, long v
/// </summary>
/// <value>The protocol.</value>
public string Protocol { get; set; }

/// <summary>
/// Gets the list of subject alternative names (SANs) of the certificate.
/// </summary>
/// <value>The list of subject alternative names (SANs) of the certificate.</value>
public string[] SubjectAlternativeNames { get; set; }
}
}
5 changes: 5 additions & 0 deletions lib/PuppeteerSharp/WaitForNetworkIdleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public class WaitForNetworkIdleOptions : WaitForOptions
/// How long to wait for no network requests in milliseconds, defaults to 500 milliseconds.
/// </summary>
public int? IdleTime { get; set; }

/// <summary>
/// Maximum number concurrent of network connections to be considered inactive.
/// </summary>
public int Concurrency { get; set; }
}
}

0 comments on commit 0516b73

Please sign in to comment.