microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a9d77c8b0667ef4b6618231d585f8465dae8b0df

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/extensionMessaging.ts

74lines · 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 * as Q from "q";
5import * as net from "net";
6
7/**
8 * Pipe path used for communicating with the server.
9 */
10let WIN_ServerPipePath = "\\\\?\\pipe\\vscodereactnative";
11let UNIX_ServerPipePath = "/tmp/vscodereactnative.sock";
12
13export let ErrorMarker = "vscodereactnative-error-marker";
14
15export let getPipePath = (): string => {
16 return (process.platform === "win32" ? WIN_ServerPipePath : UNIX_ServerPipePath);
17};
18
19/**
20 * Defines the messages sent to the extension.
21 * Add new messages to this enum.
22 */
23export enum ExtensionMessage {
24 START_PACKAGER,
25 STOP_PACKAGER,
26 PREWARM_BUNDLE_CACHE,
27 START_MONITORING_LOGCAT,
28 STOP_MONITORING_LOGCAT
29}
30
31export interface MessageWithArguments {
32 message: ExtensionMessage;
33 args: any[];
34}
35
36/**
37 * Sends messages to the extension.
38 */
39export class ExtensionMessageSender {
40
41 public sendMessage(message: ExtensionMessage, args?: any[]): Q.Promise<any> {
42 let deferred = Q.defer<any>();
43 let messageWithArguments: MessageWithArguments = { message: message, args: args };
44 let body = "";
45
46 let socket = net.connect(getPipePath(), function() {
47 let messageJson = JSON.stringify(messageWithArguments);
48 socket.write(messageJson);
49 });
50
51 socket.on("data", function(data: any) {
52 body += data;
53 });
54
55 socket.on("error", function(data: any) {
56 deferred.reject(new Error("An error ocurred while handling message: " + ExtensionMessage[message]));
57 });
58
59 socket.on("end", function() {
60 try {
61 if (body === ErrorMarker) {
62 deferred.reject(new Error("An error ocurred while handling message: " + ExtensionMessage[message]));
63 } else {
64 let responseBody: any = body ? JSON.parse(body) : null;
65 deferred.resolve(responseBody);
66 }
67 } catch (e) {
68 deferred.reject(e);
69 }
70 });
71
72 return deferred.promise;
73 }
74}