Skip to content

Chrome DevTools for coding agents

refactor: re-use Puppteer Mutex (#2444)

Nikolay Vitkov775ea2d3a32d
diff --git a/src/ToolHandler.ts b/src/ToolHandler.ts
index bf4cffa6..5ac8a558 100644
--- a/src/ToolHandler.ts
+++ b/src/ToolHandler.ts
@@ -18,7 +18,7 @@ import {labels, OFF_BY_DEFAULT_CATEGORIES} from './tools/categories.js';
import type {DefinedPageTool, ToolDefinition} from './tools/ToolDefinition.js';
import {pageIdSchema} from './tools/ToolDefinition.js';
import {logger} from './utils/logger.js';
-import type {Mutex} from './utils/Mutex.js';
+import type {Mutex} from './third_party/index.js';
export function buildFlag(category: ToolCategory) {
return `category${category.charAt(0).toUpperCase() + category.slice(1)}`;
@@ -310,7 +310,7 @@ export class ToolHandler {
success,
latencyMs: bucketizeLatency(Date.now() - startTime),
});
- guard.dispose();
+ guard[Symbol.dispose]();
}
}
}
diff --git a/src/index.ts b/src/index.ts
index 84b71b76..7c61218e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -24,7 +24,7 @@ import {ToolHandler} from './ToolHandler.js';
import type {DefinedPageTool, ToolDefinition} from './tools/ToolDefinition.js';
import {createTools} from './tools/tools.js';
import {logger} from './utils/logger.js';
-import {Mutex} from './utils/Mutex.js';
+import {Mutex} from './third_party/index.js';
import {VERSION} from './version.js';
export {buildFlag} from './ToolHandler.js';
diff --git a/src/third_party/index.ts b/src/third_party/index.ts
index 41772e33..5aa12c85 100644
--- a/src/third_party/index.ts
+++ b/src/third_party/index.ts
@@ -49,7 +49,7 @@ export type {CdpPage} from 'puppeteer-core/internal/cdp/Page.js';
export type {CdpWebWorker} from 'puppeteer-core/internal/cdp/WebWorker.js';
export type {Realm} from 'puppeteer-core/internal/api/Realm.js';
export type {JSONSchema7, JSONSchema7Definition} from 'json-schema';
-
+export {Mutex} from 'puppeteer-core/internal/util/Mutex.js';
export {
resolveDefaultUserDataDir,
detectBrowserPlatform,
diff --git a/src/utils/Mutex.ts b/src/utils/Mutex.ts
deleted file mode 100644
index b66e0cd2..00000000
--- a/src/utils/Mutex.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * @license
- * Copyright 2025 Google Inc.
- * SPDX-License-Identifier: Apache-2.0
- */
-
-export class Mutex {
- static Guard = class Guard {
- #mutex: Mutex;
- constructor(mutex: Mutex) {
- this.#mutex = mutex;
- }
- dispose(): void {
- return this.#mutex.release();
- }
- };
-
- #locked = false;
- #acquirers: Array<() => void> = [];
-
- // This is FIFO.
- async acquire(): Promise<InstanceType<typeof Mutex.Guard>> {
- if (!this.#locked) {
- this.#locked = true;
- return new Mutex.Guard(this);
- }
- const {resolve, promise} = Promise.withResolvers<void>();
- this.#acquirers.push(resolve);
- await promise;
- return new Mutex.Guard(this);
- }
-
- release(): void {
- const resolve = this.#acquirers.shift();
- if (!resolve) {
- this.#locked = false;
- return;
- }
- resolve();
- }
-}
diff --git a/tests/ToolHandler.test.ts b/tests/ToolHandler.test.ts
index fe5ffebe..53c97590 100644
--- a/tests/ToolHandler.test.ts
+++ b/tests/ToolHandler.test.ts
@@ -19,7 +19,7 @@ import type {
DefinedPageTool,
ToolDefinition,
} from '../src/tools/ToolDefinition.js';
-import {Mutex} from '../src/utils/Mutex.js';
+import {Mutex} from '../src/third_party/index.js';
describe('ToolHandler', () => {
afterEach(() => {