Skip to content

Commit

Permalink
fix: don't crash when browser windows don't open
Browse files Browse the repository at this point in the history
We open browser windows for a few things; during `wrangler dev`, and logging in. There are environments where this doesn't work as expected (like codespaces, stackblitz, etc). This fix simply logs an error instead of breaking the flow. This is the same fix as #263, now applied to the rest of wrangler.
  • Loading branch information
threepointone committed Feb 8, 2022
1 parent 14098af commit 217fa0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/healthy-monkeys-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: don't crash when browser windows don't open

We open browser windows for a few things; during `wrangler dev`, and logging in. There are environments where this doesn't work as expected (like codespaces, stackblitz, etc). This fix simply logs an error instead of breaking the flow. This is the same fix as https://github.com/cloudflare/wrangler2/pull/263, now applied to the rest of wrangler.
20 changes: 14 additions & 6 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -789,15 +789,23 @@ function useHotkeys(initial: useHotkeysInitialState, port: number) {
) => {
switch (input.toLowerCase()) {
// open browser
case "b":
await open(`http://localhost:${port}/`);
case "b": {
const urlToOpen = `http://localhost:${port}`;
const childProcess = await open(urlToOpen);
childProcess.on("error", () => {
console.warn(`failed to open ${urlToOpen}`);
});
break;
}
// toggle inspector
case "d":
await open(
`https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:9229/ws`
);
case "d": {
const urlToOpen = `https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:9229/ws`;
const childProcess = await open(urlToOpen);
childProcess.on("error", () => {
console.warn(`failed to open ${urlToOpen}`);
});
break;
}
// toggle tunnel
case "s":
setToggles((previousToggles) => ({
Expand Down
13 changes: 8 additions & 5 deletions packages/wrangler/src/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -804,18 +804,21 @@ export async function loginOrRefreshIfRequired(): Promise<boolean> {

export async function login(props?: LoginProps): Promise<boolean> {
const urlToOpen = await getAuthURL(props?.scopes);
await open(urlToOpen);
// TODO: log url only if on system where it's unreliable/unavailable
// console.log(`💁 Opened ${urlToOpen}`);
const childProcess = await open(urlToOpen);
childProcess.on("error", async () => {
console.warn(`failed to open ${urlToOpen}`);
});
let server;
let loginTimeoutHandle;
const timerPromise = new Promise<boolean>((resolve) => {
loginTimeoutHandle = setTimeout(() => {
console.error("Timed out waiting for authorization code.");
console.error(
"Timed out waiting for authorization code, please try again."
);
server.close();
clearTimeout(loginTimeoutHandle);
resolve(false);
}, 60000); // wait for 30 seconds for the user to authorize
}, 60000); // wait for 60 seconds for the user to authorize
});

const loginPromise = new Promise<boolean>((resolve, reject) => {
Expand Down

0 comments on commit 217fa0e

Please sign in to comment.