-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathiosConnection.ts
236 lines (198 loc) · 8.87 KB
/
iosConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as net from 'net';
import * as stream from 'stream';
import {EventEmitter} from 'events';
import {INSDebugConnection} from './INSDebugConnection';
import * as utils from '../../common/utilities';
import {Services} from '../../services/debugAdapterServices';
interface IMessageWithId {
id: number;
method: string;
params?: any;
}
export class PacketStream extends stream.Transform {
private buffer: Buffer;
private offset: number;
constructor(opts?: stream.TransformOptions) {
super(opts);
}
public _transform(packet: any, encoding: string, done: Function): void {
while (packet.length > 0) {
if (!this.buffer) {
// read length
let length = packet.readInt32BE(0);
this.buffer = new Buffer(length);
this.offset = 0;
packet = packet.slice(4);
}
packet.copy(this.buffer, this.offset);
let copied = Math.min(this.buffer.length - this.offset, packet.length);
this.offset += copied;
packet = packet.slice(copied);
if (this.offset === this.buffer.length) {
this.push(this.buffer);
this.buffer = undefined;
}
}
done();
}
}
/**
* Implements a Request/Response API on top of a Unix domain socket for messages that are marked with an `id` property.
* Emits `message.method` for messages that don't have `id`.
*/
class ResReqTcpSocket extends EventEmitter {
private _pendingRequests = new Map<number, any>();
private _unixSocketAttached: Promise<net.Socket>;
/**
* Attach to the given filePath
*/
public attach(filePath: string): Promise<void> {
this._unixSocketAttached = new Promise((resolve, reject) => {
let unixSocket: net.Socket;
try {
unixSocket = net.createConnection(filePath);
unixSocket.on('connect', () => {
resolve(unixSocket);
});
unixSocket.on('error', (e) => {
reject(e);
});
unixSocket.on('close', () => {
Services.logger().log('Unix socket closed');
this.emit('close');
});
let packetsStream = new PacketStream();
unixSocket.pipe(packetsStream);
packetsStream.on('data', (buffer: Buffer) => {
let packet = buffer.toString('utf16le');
Services.logger().log('From target: ' + packet);
this.onMessage(JSON.parse(packet));
});
} catch (e) {
// invalid url e.g.
reject(e.message);
return;
}
});
return <Promise<void>><any>this._unixSocketAttached;
}
public close(): void {
if (this._unixSocketAttached) {
this._unixSocketAttached.then(socket => socket.destroy());
}
}
/**
* Send a message which must have an id. Ok to call immediately after attach. Messages will be queued until
* the websocket actually attaches.
*/
public sendMessage(message: IMessageWithId): Promise<any> {
return new Promise((resolve, reject) => {
this._pendingRequests.set(message.id, resolve);
this._unixSocketAttached.then(socket => {
const msgStr = JSON.stringify(message);
Services.logger().log('To target: ' + msgStr);
let encoding = "utf16le";
let length = Buffer.byteLength(msgStr, encoding);
let payload = new Buffer(length + 4);
payload.writeInt32BE(length, 0);
payload.write(msgStr, 4, length, encoding);
socket.write(payload);
});
});
}
private onMessage(message: any): void {
if (message.id) {
if (this._pendingRequests.has(message.id)) {
// Resolve the pending request with this response
this._pendingRequests.get(message.id)(message);
this._pendingRequests.delete(message.id);
} else {
console.error(`Got a response with id ${message.id} for which there is no pending request, weird.`);
}
} else if (message.method) {
this.emit(message.method, message.params);
}
}
}
/**
* Connects to a target supporting the webkit protocol and sends and receives messages
*/
export class IosConnection implements INSDebugConnection {
private _nextId = 1;
private _socket: ResReqTcpSocket;
constructor() {
this._socket = new ResReqTcpSocket();
}
public on(eventName: string, eventHandler: (eventParams: any) => void): void {
this._socket.on(eventName, eventHandler);
}
/**
* Attach the underlying Unix socket
*/
public attach(filePath: string): Promise<void> {
Services.logger().log('Attempting to attach to path ' + filePath);
return utils.retryAsync(() => this._attach(filePath), 6000)
.then(() => {
Promise.all<WebKitProtocol.Response>([
this.sendMessage('Debugger.enable'),
this.sendMessage('Console.enable'),
this.sendMessage('Debugger.setBreakpointsActive', {active: true})
]);
});
}
public _attach(filePath: string): Promise<void> {
return this._socket.attach(filePath);
}
public close(): void {
this._socket.close();
}
public debugger_setBreakpoint(location: WebKitProtocol.Debugger.Location, condition?: string): Promise<WebKitProtocol.Debugger.SetBreakpointResponse> {
return this.sendMessage('Debugger.setBreakpoint', <WebKitProtocol.Debugger.SetBreakpointParams>{ location, options: { condition: condition }});
}
public debugger_setBreakpointByUrl(url: string, lineNumber: number, columnNumber: number, condition: string, ignoreCount: number): Promise<WebKitProtocol.Debugger.SetBreakpointByUrlResponse> {
return this.sendMessage('Debugger.setBreakpointByUrl', <WebKitProtocol.Debugger.SetBreakpointByUrlParams>{ url: url, lineNumber: lineNumber, columnNumber: 0 /* a columnNumber different from 0 confuses the debugger */, options: { condition: condition, ignoreCount: ignoreCount }});
}
public debugger_removeBreakpoint(breakpointId: string): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.removeBreakpoint', <WebKitProtocol.Debugger.RemoveBreakpointParams>{ breakpointId });
}
public debugger_stepOver(): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.stepOver');
}
public debugger_stepIn(): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.stepInto');
}
public debugger_stepOut(): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.stepOut');
}
public debugger_resume(): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.resume');
}
public debugger_pause(): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.pause');
}
public debugger_evaluateOnCallFrame(callFrameId: string, expression: string, objectGroup = 'dummyObjectGroup', returnByValue?: boolean): Promise<WebKitProtocol.Debugger.EvaluateOnCallFrameResponse> {
return this.sendMessage('Debugger.evaluateOnCallFrame', <WebKitProtocol.Debugger.EvaluateOnCallFrameParams>{ callFrameId, expression, objectGroup, returnByValue });
}
public debugger_setPauseOnExceptions(state: string): Promise<WebKitProtocol.Response> {
return this.sendMessage('Debugger.setPauseOnExceptions', <WebKitProtocol.Debugger.SetPauseOnExceptionsParams>{ state });
}
public debugger_getScriptSource(scriptId: WebKitProtocol.Debugger.ScriptId): Promise<WebKitProtocol.Debugger.GetScriptSourceResponse> {
return this.sendMessage('Debugger.getScriptSource', <WebKitProtocol.Debugger.GetScriptSourceParams>{ scriptId });
}
public runtime_getProperties(objectId: string, ownProperties: boolean, accessorPropertiesOnly: boolean): Promise<WebKitProtocol.Runtime.GetPropertiesResponse> {
return this.sendMessage('Runtime.getProperties', <WebKitProtocol.Runtime.GetPropertiesParams>{ objectId, ownProperties, accessorPropertiesOnly });
}
public runtime_evaluate(expression: string, objectGroup = 'dummyObjectGroup', contextId?: number, returnByValue = false): Promise<WebKitProtocol.Runtime.EvaluateResponse> {
return this.sendMessage('Runtime.evaluate', <WebKitProtocol.Runtime.EvaluateParams>{ expression, objectGroup, contextId, returnByValue });
}
private sendMessage(method: string, params?: any): Promise<WebKitProtocol.Response> {
return this._socket.sendMessage({
id: this._nextId++,
method: method,
params: params
});
}
}