-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnreplClient.ts
167 lines (136 loc) · 4.71 KB
/
nreplClient.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
import * as vscode from 'vscode';
import * as net from 'net';
import { Buffer } from 'buffer';
import * as bencodeUtil from './bencodeUtil';
import { cljConnection, CljConnectionInformation } from './cljConnection';
interface nREPLCompleteMessage {
op: string;
symbol: string;
ns?: string
}
interface nREPLInfoMessage {
op: string;
symbol: string;
ns: string;
session?: string;
}
type TestMessage = {
op: "test" | "test-all" | "test-stacktrace" | "retest"
ns?: string,
'load?'?: any
}
interface nREPLEvalMessage {
op: string;
file: string;
'file-path'?: string;
session: string;
}
interface nREPLSingleEvalMessage {
op: string;
code: string;
session: string;
}
interface nREPLStacktraceMessage {
op: string;
session: string;
}
interface nREPLCloneMessage {
op: string;
session?: string;
}
interface nREPLCloseMessage {
op: string;
session?: string;
}
const complete = (symbol: string, ns: string): Promise<any> => {
const msg: nREPLCompleteMessage = { op: 'complete', symbol, ns };
return send(msg).then(respObjs => respObjs[0]);
};
const info = (symbol: string, ns: string, session?: string): Promise<any> => {
const msg: nREPLInfoMessage = { op: 'info', symbol, ns, session };
return send(msg).then(respObjs => respObjs[0]);
};
const evaluate = (code: string, session?: string): Promise<any[]> => clone(session).then((session_id) => {
const msg: nREPLSingleEvalMessage = { op: 'eval', code: code, session: session_id };
return send(msg);
});
const evaluateFile = (code: string, filepath: string, session?: string): Promise<any[]> => clone(session).then((session_id) => {
const msg: nREPLEvalMessage = { op: 'load-file', file: code, 'file-path': filepath, session: session_id };
return send(msg);
});
const stacktrace = (session: string): Promise<any> => send({ op: 'stacktrace', session: session });
const runTests = function (namespace: string | undefined): Promise<any[]> {
const message: TestMessage = {
op: (namespace ? "test" : "test-all"),
ns: namespace,
'load?': 1
}
return send(message);
}
const clone = (session?: string): Promise<string> => send({ op: 'clone', session: session }).then(respObjs => respObjs[0]['new-session']);
const test = (connectionInfo: CljConnectionInformation): Promise<any[]> => {
return send({ op: 'clone' }, connectionInfo)
.then(respObjs => respObjs[0])
.then(response => {
if (!('new-session' in response))
return Promise.reject(false);
else {
return Promise.resolve([]);
}
});
};
const close = (session?: string): Promise<any[]> => send({ op: 'close', session: session });
const listSessions = (): Promise<[string]> => {
return send({ op: 'ls-sessions' }).then(respObjs => {
const response = respObjs[0];
if (response.status[0] == "done") {
return Promise.resolve(response.sessions);
}
});
}
type Message = TestMessage | nREPLCompleteMessage | nREPLInfoMessage | nREPLEvalMessage | nREPLStacktraceMessage | nREPLCloneMessage | nREPLCloseMessage | nREPLSingleEvalMessage;
const send = (msg: Message, connection?: CljConnectionInformation): Promise<any[]> => {
console.log("nREPL: Sending op", msg);
return new Promise<any[]>((resolve, reject) => {
connection = connection || cljConnection.getConnection();
if (!connection)
return reject('No connection found.');
const client = net.createConnection(connection.port, connection.host);
Object.keys(msg).forEach(key => (msg as any)[key] === undefined && delete (msg as any)[key]);
client.write(bencodeUtil.encode(msg), 'binary');
client.on('error', error => {
client.end();
client.removeAllListeners();
if ((error as any)['code'] === 'ECONNREFUSED') {
vscode.window.showErrorMessage('Connection refused.');
cljConnection.disconnect();
}
reject(error);
});
let nreplResp = Buffer.from('');
const respObjects: any[] = [];
client.on('data', data => {
nreplResp = Buffer.concat([nreplResp, data]);
const { decodedObjects, rest, isDone } = bencodeUtil.decodeBuffer(nreplResp);
nreplResp = rest;
respObjects.push(...decodedObjects);
if (isDone) {
client.end();
client.removeAllListeners();
resolve(respObjects);
}
});
});
};
export const nreplClient = {
complete,
info,
evaluate,
evaluateFile,
stacktrace,
clone,
test,
runTests,
close,
listSessions
};