Skip to content

Commit

Permalink
merge of pull request #36 of org. repository
Browse files Browse the repository at this point in the history
  • Loading branch information
przedmiot committed Mar 13, 2023
2 parents df726e9 + 26265f0 commit 90bac38
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/ChromeDevtoolsProtocol/Instance/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,11 @@ class Launcher
private $input;

/**
* @param int $port If port <= 0, random port number is generated.
* @throws \Exception
* @param int $port If port <= 0, random available port is used.
*/
public function __construct($port = 0)
{
if ($port <= 0) {
$port = random_int(1024 + 1, 65535);
}

$this->port = $port;
$this->port = max(0, $port);
}

/**
Expand Down Expand Up @@ -213,6 +208,10 @@ private function launchWithExecutable(ContextInterface $ctx, string $executable,
$temporaryUserDataDir = null;
if (!$foundUserDataDir) {
$temporaryUserDataDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "chrome-profile-" . $this->port;
if ($this->port === 0) {
$temporaryUserDataDir .= "-" . bin2hex(random_bytes(8));
}

$fs->mkdir($temporaryUserDataDir);
$args[] = "--user-data-dir=" . $temporaryUserDataDir;
}
Expand All @@ -227,6 +226,16 @@ private function launchWithExecutable(ContextInterface $ctx, string $executable,
);
$process->start();

if ($this->port === 0) {
$process->waitUntil(function ($type, $buffer) {
if (preg_match('~DevTools listening on ws://.+:(\d+)/devtools~', $buffer, $m)) {
$this->port = (int)$m[1];
return true;
}
return false;
});
}

$instance = new ProcessInstance($process, $temporaryUserDataDir, $this->port);

while (true) {
Expand Down

0 comments on commit 90bac38

Please sign in to comment.