Repository navigation
[js] Ensure BiDi is not exposed on Driver #17926
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cb38f24
[js] Ensure BiDi is not exposed on driver
pujagani e653359
[js] Ensure BiDi is not exposed on driver
pujagani 2487694
Merge branch 'bidi-access-js' of https://github.com/pujagani/selenium…
pujagani 0979dd4
[js] Ensure BiDi is not exposed on driver
pujagani 705971e
Merge branch 'bidi-access-js' of https://github.com/pujagani/selenium…
pujagani 22d4fb3
[js] Ensure BiDi is not exposed on driver
pujagani eb11d1c
Merge branch 'bidi-access-js' of https://github.com/pujagani/selenium…
pujagani 324f9d7
[js] Extract bidi connection in a new class
pujagani 0d432ca
[js] Extract bidi connection in a new class
pujagani 6288b65
Merge branch 'bidi-access-js' of https://github.com/pujagani/selenium…
pujagani 330507e
[js] Ensure logging is as per JS norms
pujagani 20c8e70
Address comments and fix tests
pujagani d9bd8d0
[js] Address comments
pujagani e78fa56
Merge branch 'trunk' into bidi-access-js
pujagani 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| const BiDi = require('../bidi') | ||
|
|
||
| /** | ||
| * One BiDi connection per driver, kept outside the WebDriver class itself so | ||
| * it isn't a discoverable method on it — see | ||
| * docs/decisions/17670-bidi-implementation-boundaries.md. Not re-exported from | ||
| * the package's public entry point. | ||
| * | ||
| * Stores the in-flight promise, not the resolved connection, so a second | ||
| * concurrent call for the same driver awaits the same creation instead of | ||
| * racing its own and leaking whichever connection loses. | ||
| * @type {WeakMap<object, Promise<BiDi>>} | ||
| */ | ||
| const connections = new WeakMap() | ||
|
|
||
| /** | ||
| * Returns the BiDi connection for `driver`, creating it on first access. | ||
| * @param {object} driver | ||
| * @returns {Promise<BiDi>} | ||
| */ | ||
| function getBidiConnection(driver) { | ||
| if (!connections.has(driver)) { | ||
| connections.set(driver, createConnection(driver)) | ||
| } | ||
| return connections.get(driver) | ||
| } | ||
|
|
||
| /** | ||
| * @param {object} driver | ||
| * @returns {Promise<BiDi>} | ||
| */ | ||
| async function createConnection(driver) { | ||
| const caps = await driver.getCapabilities() | ||
| const webSocketUrl = caps['map_'].get('webSocketUrl') | ||
| if (!webSocketUrl) { | ||
| throw new Error('WebDriver instance must support BiDi protocol') | ||
| } | ||
| return new BiDi(webSocketUrl.replace('localhost', '127.0.0.1')) | ||
| } | ||
|
|
||
| /** | ||
| * Closes `driver`'s BiDi connection, if one was ever opened. A no-op | ||
| * otherwise — must not lazily create a connection just to close it. | ||
| * | ||
| * Callers (e.g. quit()) invoke this fire-and-forget, so it must never reject: | ||
| * if the original connection attempt itself had failed, `pending` is already | ||
| * rejected and there is nothing live to close. | ||
| * @param {object} driver | ||
| * @returns {Promise<void>} | ||
|
qodo-code-review[bot] marked this conversation as resolved.
Outdated
|
||
| */ | ||
| async function closeBidiConnection(driver) { | ||
| const pending = connections.get(driver) | ||
| if (pending === undefined) { | ||
| return | ||
| } | ||
| connections.delete(driver) | ||
| try { | ||
| const connection = await pending | ||
| await connection.close() | ||
| } catch { | ||
| // Nothing to close — the original connection attempt failed. | ||
| } | ||
| } | ||
|
|
||
| module.exports = { getBidiConnection, closeBidiConnection } | ||
This file contains hidden or 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
96 changes: 96 additions & 0 deletions
96
javascript/selenium-webdriver/test/lib/bidi_connection_test.js
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| 'use strict' | ||
|
|
||
| const assert = require('node:assert') | ||
| const { WebSocketServer } = require('ws') | ||
| const { getBidiConnection, closeBidiConnection } = require('../../lib/bidi_connection') | ||
|
|
||
| function startEchoServer() { | ||
| return new Promise((resolve) => { | ||
| const server = new WebSocketServer({ port: 0 }, () => { | ||
| const { port } = server.address() | ||
| resolve({ server, url: `ws://127.0.0.1:${port}` }) | ||
| }) | ||
| server.on('connection', (ws) => { | ||
| ws.on('message', (data) => { | ||
| const { id } = JSON.parse(data.toString()) | ||
| ws.send(JSON.stringify({ id, result: {} })) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| describe('bidi_connection', function () { | ||
| let server | ||
|
|
||
| afterEach(async function () { | ||
| if (server !== undefined) { | ||
| await new Promise((resolve) => server.close(resolve)) | ||
| server = undefined | ||
| } | ||
| }) | ||
|
|
||
| it('creates exactly one BiDi connection under concurrent first access', async function () { | ||
| const started = await startEchoServer() | ||
| server = started.server | ||
|
|
||
| let capabilitiesCalls = 0 | ||
| const driver = { | ||
| async getCapabilities() { | ||
| capabilitiesCalls++ | ||
| // Yield the event loop before resolving, so concurrent callers race. | ||
| await new Promise((resolve) => setTimeout(resolve, 20)) | ||
| return { map_: new Map([['webSocketUrl', started.url]]) } | ||
| }, | ||
| } | ||
|
|
||
| const [a, b, c] = await Promise.all([ | ||
| getBidiConnection(driver), | ||
| getBidiConnection(driver), | ||
| getBidiConnection(driver), | ||
| ]) | ||
|
|
||
| assert.strictEqual(capabilitiesCalls, 1, 'getCapabilities() should only be called once') | ||
| assert.strictEqual(a, b) | ||
| assert.strictEqual(b, c) | ||
|
|
||
| await closeBidiConnection(driver) | ||
| }) | ||
|
|
||
| it('closeBidiConnection() is a no-op when no connection was ever opened', async function () { | ||
| const driver = { | ||
| async getCapabilities() { | ||
| throw new Error('should not be called') | ||
| }, | ||
| } | ||
| await assert.doesNotReject(closeBidiConnection(driver)) | ||
| }) | ||
|
|
||
| it('closeBidiConnection() does not reject when the original connection attempt failed', async function () { | ||
| const driver = { | ||
| async getCapabilities() { | ||
| throw new Error('driver does not support BiDi') | ||
| }, | ||
| } | ||
| await assert.rejects(getBidiConnection(driver), /does not support BiDi/) | ||
| // quit() calls this fire-and-forget (no await/catch at the call site), so | ||
| // it must swallow the earlier failure rather than re-throwing it. | ||
| await assert.doesNotReject(closeBidiConnection(driver)) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.