-
Notifications
You must be signed in to change notification settings - Fork 9k
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(agnostification): agnostify web socket connections #6520
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All rights reserved. | ||
* | ||
* Licensed 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. | ||
*/ | ||
import { ConnectionTransport } from './ConnectionTransport.js'; | ||
|
||
export class BrowserWebSocketTransport implements ConnectionTransport { | ||
static create(url: string): Promise<BrowserWebSocketTransport> { | ||
return new Promise((resolve, reject) => { | ||
const ws = new WebSocket(url); | ||
|
||
ws.addEventListener('open', () => | ||
resolve(new BrowserWebSocketTransport(ws)) | ||
); | ||
ws.addEventListener('error', reject); | ||
}); | ||
} | ||
|
||
private _ws: WebSocket; | ||
onmessage?: (message: string) => void; | ||
onclose?: () => void; | ||
|
||
constructor(ws: WebSocket) { | ||
this._ws = ws; | ||
this._ws.addEventListener('message', (event) => { | ||
if (this.onmessage) this.onmessage.call(null, event.data); | ||
}); | ||
this._ws.addEventListener('close', () => { | ||
if (this.onclose) this.onclose.call(null); | ||
}); | ||
// Silently ignore all errors - we don't know what to do with them. | ||
this._ws.addEventListener('error', () => {}); | ||
this.onmessage = null; | ||
this.onclose = null; | ||
} | ||
|
||
send(message: string): void { | ||
this._ws.send(message); | ||
} | ||
|
||
close(): void { | ||
this._ws.close(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ import { assert } from '../common/assert.js'; | |
import { helper, debugError } from '../common/helper.js'; | ||
import { LaunchOptions } from './LaunchOptions.js'; | ||
import { Connection } from '../common/Connection.js'; | ||
import { WebSocketTransport } from '../common/WebSocketTransport.js'; | ||
import { NodeWebSocketTransport as WebSocketTransport } from '../node/NodeWebSocketTransport.js'; | ||
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. Do you want to rename the import? Might be better to refactor it to use the new name. 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. Yeah, I think so. Will rework in follow-up. |
||
import { PipeTransport } from './PipeTransport.js'; | ||
import * as readline from 'readline'; | ||
import { TimeoutError } from '../common/Errors.js'; | ||
|
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All rights reserved. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
/** | ||
* Returns the web socket endpoint for the backend of the browser the tests run | ||
* in. Used to create connections to that browser in Puppeteer for unit tests. | ||
* | ||
* It's available on window.__ENV__ because setup code in | ||
* web-test-runner.config.js puts it there. If you're changing this code (or | ||
* that code), make sure the other is updated accordingly. | ||
*/ | ||
export function getWebSocketEndpoint() { | ||
return window.__ENV__.wsEndpoint; | ||
} |
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
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.
Does there need to be a subdirectory for browser-centric code?
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.
Yeah, I think probably there does. But then it'll get a bit messy with node/browser/common where common will sometimes conditionally load in things. But maybe that's fine?
I might leave this for a follow up PR - let me know if you feel strongly to fix this now 👍
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 don't personally think it's a blocker if you're planning to do it anyway.