Skip to content
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

Method names not available #284

Open
slhck opened this issue Aug 22, 2022 · 2 comments
Open

Method names not available #284

slhck opened this issue Aug 22, 2022 · 2 comments

Comments

@slhck
Copy link

slhck commented Aug 22, 2022

While I can use TypeScript to type-check method parameters and response objects, I cannot actually ensure that the method I am calling exists, let alone that the parameters and response match what I am trying to call.

Consider this (simplified) function:

let baseId = 0;

async function _sendAndGetResponse<
  ResponseType,
  RequestParams = Record<string, unknown>
>(method: string, params?: RequestParams): Promise<ResponseType> {
  const id = baseId++;
  const request = { id, method, params };

  webSocket.send(JSON.stringify(request));

  return new Promise((resolve, _) => {
    webSocket?.on("message", (data: string) => {
      const response = JSON.parse(data);
      if (response.id === id) {
        resolve(response.result as ResponseType);
      }
    });
  });
}

I can invoke it with

this._sendAndGetResponse<Protocol.Runtime.EvaluateResponse, Protocol.Runtime.EvaluateRequest>(
        'Runtime.evaluate',
        {
          expression: "...",
        },
      );

But I could easily pass Runtime.foo instead of Runtime.evaluate and it'd still pass.

What I think would be required is:

  • A list of methods that can be called, and, optionally
  • A mapping type that maps the correct request parameters and response objects to that method
@paulirish
Copy link
Member

paulirish commented Aug 22, 2022

Kinda sidestepping your question.. but you may want to look at leveraging https://www.npmjs.com/package/noice-json-rpc

I only found it recently but a few excellent tools use it. And the expression of CDP in the API is quite attractive.

Edit: Oh, you already found it. :)

@slhck
Copy link
Author

slhck commented Aug 22, 2022

Yes, I did! :)

I actually also found https://github.com/cyrus-and/chrome-remote-interface which is actively maintained and provides a similarly expressive API.

I also found that the file node_modules/devtools-protocol/types/protocol-proxy-api.d.ts does contain the individual methods, albeit in a slightly different manner.

Feel free to close this issue if it's not worth implementing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants