1import Protocol from 'devtools-protocol';
2
3import { CdpMessage, DebuggerRequest, InspectorHandler } from './types';
4import { ExpoDebuggerInfo } from '../device';
5
6/**
7 * Hermes and vscode have trouble setting breakpoints by `urlRegex` through `Debugger.setBreakpointByUrl`.
8 * Vscode adds `file://` to a URL containing `http://`, which confuses Hermes and sets it to the wrong location.
9 * Hermes needs to create the breakpoint to get the proper ID, but it must be unbounded.
10 * Once the sourcemap is loaded, vscode will rebind the unbounded breakpoint to the correct location (using `Debugger.setBreakpoint`).
11 */
12export class VscodeDebuggerSetBreakpointByUrlHandler implements InspectorHandler {
13  onDebuggerMessage(
14    message: DebuggerRequest<DebuggerSetBreakpointByUrl>,
15    { debuggerType }: ExpoDebuggerInfo
16  ): boolean {
17    if (
18      debuggerType === 'vscode' &&
19      message.method === 'Debugger.setBreakpointByUrl' &&
20      message.params.urlRegex
21    ) {
22      // Explicitly force the breakpoint to be unbounded
23      message.params.url = 'file://__invalid_url__';
24      delete message.params.urlRegex;
25    }
26
27    return false;
28  }
29}
30
31/** @see https://chromedevtools.github.io/devtools-protocol/v8/Debugger/#method-setBreakpointByUrl */
32export type DebuggerSetBreakpointByUrl = CdpMessage<
33  'Debugger.setBreakpointByUrl',
34  Protocol.Debugger.SetBreakpointByUrlRequest,
35  Protocol.Debugger.SetBreakpointByUrlResponse
36>;
37