SafetyCulture / grpc-web-devtools

Chrome & Firefox Browser extension to aid gRPC-Web development
MIT License
402 stars 51 forks source link

WB-530: Add support for connect-web and connect-web protocol #141

Closed matt-lewandowski closed 1 year ago

matt-lewandowski commented 1 year ago

Support for connect-web

Create a new script that adds a connect web interceptor to the window. This can be used when implementing connect-web clients.

Gotchas

In order to upgrade this plugin to manifest V3, we will need to make some breaking changes. This includes not injecting scripts into browsers as a string. V3 guards against this, so we will need to inject it as a shared js file. I have done so with this new script, but this causes an issue where sometimes the script loads after the app. We don't have control over the order that these scripts load.

The solution is to attempt to use the interceptor if it exists on the client, and if it doens't then watch for our custom event to see if it is ready again. For connect web this can be done like this.

// __CONNECT_WEB_DEVTOOLS__ is loaded in as a script, so it is not guaranteed to be loaded before your code.
const interceptors: Interceptor[] = window.__CONNECT_WEB_DEVTOOLS__ !== "undefined" ?
  [window.__CONNECT_WEB_DEVTOOLS__]
  : [];
// To get around the fact that __CONNECT_WEB_DEVTOOLS__ might not be loaded, we can listen for a custom event,
// and then push the interceptor to our array once loaded.
window.addEventListener("connect-web-dev-tools-ready", () => {
  if (typeof window.__CONNECT_WEB_DEVTOOLS__ !== "undefined") {
    interceptors.push(window.__CONNECT_WEB_DEVTOOLS__);
  }
});
// Now we can use the interceptors in our transport
const transport: Transport = createGrpcWebTransport({
  baseUrl: getApiHostname(),
  interceptors,
});

We will have to migrate the grpc-web script to do the same thing, later down the line, otherwise, chrome will take the extension down.

gtiminskis-sc commented 1 year ago

We will have to migrate the grpc-web script to do the same thing, later down the line, otherwise, chrome will take the extension down. do we know when that will happen?

matt-lewandowski commented 1 year ago

We will have to migrate the grpc-web script to do the same thing, later down the line, otherwise, chrome will take the extension down. do we know when that will happen?

https://developer.chrome.com/docs/extensions/mv3/mv2-sunset/

It looks like they are going to start making progress at the start of 2023, and will totally remove all v2 by 2024. We should start making progress towards this sooner rather then later though.