microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4adeb45d4a2f292842d38deb2126dd90bb71bd20

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/cdp-proxy/CDPMessageHandlers/directCDPMessageHandler.ts

100lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import { Protocol as Cdp } from "devtools-protocol/types/protocol";
5import { ICDPMessageHandler, ProcessedCDPMessage } from "./ICDPMessageHandler";
6import { CDP_API_NAMES } from "./CDPAPINames";
7
8export class DirectCDPMessageHandler implements ICDPMessageHandler {
9
10 /**
11 * @description The Hermes native functions calls mark in call stack
12 * @type {string}
13 */
14 private readonly HERMES_NATIVE_FUNCTION_NAME: string = "(native)";
15
16 /**
17 * @description Equals to 0xfffffff - the scriptId returned by Hermes debugger, that means "invalid script ID"
18 * @type {string}
19 */
20 private readonly HERMES_NATIVE_FUNCTION_SCRIPT_ID: string = "4294967295";
21
22 private readonly ARRAY_REQUEST_PHRASE_MARKER: string = "Object.getOwnPropertyDescriptor";
23
24 constructor() { }
25
26 public processDebuggerCDPMessage(event: any): ProcessedCDPMessage {
27 let sendBack = false;
28 if (event.method === CDP_API_NAMES.DEBUGGER_SET_BREAKPOINT) {
29 event = this.handleBreakpointSetting(event);
30 } else if (event.method === CDP_API_NAMES.RUNTIME_CALL_FUNCTION_ON) {
31 if (event.params.functionDeclaration.includes(this.ARRAY_REQUEST_PHRASE_MARKER)) {
32 event = this.handleCallFunctionOnEvent(event);
33 sendBack = true;
34 }
35 }
36
37 return {
38 event,
39 sendBack,
40 };
41 }
42
43 public processApplicationCDPMessage(event: any): ProcessedCDPMessage {
44 let sendBack = false;
45 if (event.method === CDP_API_NAMES.DEBUGGER_PAUSED) {
46 event = this.handlePausedEvent(event);
47 } else if (event.result && event.result.result) {
48 event = this.handleFunctionTypeResult(event);
49 }
50
51 return {
52 event,
53 sendBack,
54 };
55 }
56
57 private handleCallFunctionOnEvent(event: any): any {
58 return {
59 result: {
60 result: {
61 objectId: event.params.objectId,
62 },
63 },
64 id: event.id,
65 };
66 }
67
68 private handleFunctionTypeResult(event: any): any {
69 if (Array.isArray(event.result.result)) {
70 let results: Cdp.Runtime.PropertyDescriptor[] = event.result.result;
71 results.forEach((resultObj) => {
72 if (resultObj.value && resultObj.value.type === "function" && !resultObj.value.description) {
73 resultObj.value.description = "function() { … }";
74 }
75 });
76
77 event.result.result = results;
78 }
79 return event;
80 }
81
82 private handlePausedEvent(event: any): any {
83 let callFrames: Cdp.Debugger.CallFrame[] = event.params.callFrames;
84
85 callFrames = callFrames.filter(callFrame =>
86 callFrame.functionName !== this.HERMES_NATIVE_FUNCTION_NAME &&
87 callFrame.location.scriptId !== this.HERMES_NATIVE_FUNCTION_SCRIPT_ID
88 );
89 event.params.callFrames = callFrames;
90
91 return event;
92 }
93
94 private handleBreakpointSetting(event: any): any {
95 if (event.params) {
96 delete event.params.location.columnNumber;
97 }
98 return event;
99 }
100}
101