@@ -24,11 +24,11 @@ export interface EditorSessionEntry {
24
24
socketPath : string
25
25
}
26
26
27
- // interface DeleteSessionRequest {
28
- // socketPath: string
29
- // }
27
+ interface DeleteSessionRequest {
28
+ socketPath : string
29
+ }
30
30
31
- interface RegisterSessionRequest {
31
+ interface AddSessionRequest {
32
32
entry : EditorSessionEntry
33
33
}
34
34
@@ -37,52 +37,15 @@ interface GetSessionResponse {
37
37
socketPath ?: string
38
38
}
39
39
40
- export class EditorSessionManagerClient {
41
- constructor ( private codeServerSocketPath : string ) { }
42
-
43
- async canConnect ( ) {
44
- return canConnect ( this . codeServerSocketPath )
45
- }
46
-
47
- async getConnectedSocketPath ( filePath : string ) : Promise < string | undefined > {
48
- const response = await new Promise < GetSessionResponse > ( ( resolve , reject ) => {
49
- const opts = {
50
- path : "/session?filePath=" + encodeURIComponent ( filePath ) ,
51
- socketPath : this . codeServerSocketPath ,
52
- }
53
- const req = http . get ( opts , ( res ) => {
54
- let rawData = ""
55
- res . setEncoding ( "utf8" )
56
- res . on ( "data" , ( chunk ) => {
57
- rawData += chunk
58
- } )
59
- res . on ( "end" , ( ) => {
60
- try {
61
- const obj = JSON . parse ( rawData )
62
- if ( res . statusCode === 200 ) {
63
- resolve ( obj )
64
- } else {
65
- reject ( new Error ( "Unexpected status code: " + res . statusCode ) )
66
- }
67
- } catch ( e : unknown ) {
68
- reject ( e )
69
- }
70
- } )
71
- } )
72
- req . on ( "error" , reject )
73
- req . end ( )
74
- } )
75
- return response . socketPath
76
- }
77
- }
78
-
79
40
export async function makeEditorSessionManagerServer (
80
41
codeServerSocketPath : string ,
81
42
editorSessionManager : EditorSessionManager ,
82
43
) : Promise < http . Server > {
83
44
const router = express ( )
45
+
84
46
// eslint-disable-next-line import/no-named-as-default-member
85
47
router . use ( express . json ( ) )
48
+
86
49
router . get ( "/session" , async ( req , res ) => {
87
50
const filePath = req . query . filePath as string
88
51
if ( ! filePath ) {
@@ -97,17 +60,25 @@ export async function makeEditorSessionManagerServer(
97
60
res . status ( 500 ) . send ( error )
98
61
}
99
62
} )
100
- router . post ( "/session" , async ( req , res ) => {
101
- const request = req . body as RegisterSessionRequest
63
+
64
+ router . post ( "/add-session" , async ( req , res ) => {
65
+ const request = req . body as AddSessionRequest
102
66
if ( ! request . entry ) {
103
67
res . status ( 400 ) . send ( "entry is required" )
104
68
}
105
- editorSessionManager . registerSession ( request . entry )
69
+ editorSessionManager . addSession ( request . entry )
106
70
res . status ( 200 ) . send ( )
107
71
} )
108
- router . delete ( "/session" , async ( req , res ) => {
109
- throw new Error ( "Not implemented" )
72
+
73
+ router . post ( "/delete-session" , async ( req , res ) => {
74
+ const request = req . body as DeleteSessionRequest
75
+ if ( ! request . socketPath ) {
76
+ res . status ( 400 ) . send ( "socketPath is required" )
77
+ }
78
+ editorSessionManager . deleteSession ( request . socketPath )
79
+ res . status ( 200 ) . send ( )
110
80
} )
81
+
111
82
const server = http . createServer ( router )
112
83
try {
113
84
await fs . unlink ( codeServerSocketPath )
@@ -133,18 +104,19 @@ export class EditorSessionManager {
133
104
// Map from socket path to EditorSessionEntry.
134
105
private entries = new Map < string , EditorSessionEntry > ( )
135
106
136
- registerSession ( entry : EditorSessionEntry ) : void {
107
+ addSession ( entry : EditorSessionEntry ) : void {
108
+ logger . debug ( `Adding session to session registry: ${ entry . socketPath } ` )
137
109
this . entries . set ( entry . socketPath , entry )
138
110
}
139
111
140
- getCandidatesForFile ( path : string ) : EditorSessionEntry [ ] {
112
+ getCandidatesForFile ( filePath : string ) : EditorSessionEntry [ ] {
141
113
const matchCheckResults = new Map < string , boolean > ( )
142
114
143
115
const checkMatch = ( entry : EditorSessionEntry ) : boolean => {
144
116
if ( matchCheckResults . has ( entry . socketPath ) ) {
145
117
return matchCheckResults . get ( entry . socketPath ) !
146
118
}
147
- const result = this . containsPath ( entry , path )
119
+ const result = entry . workspace . folders . some ( ( folder ) => filePath . startsWith ( folder . uri . path + path . sep ) )
148
120
matchCheckResults . set ( entry . socketPath , result )
149
121
return result
150
122
}
@@ -165,17 +137,17 @@ export class EditorSessionManager {
165
137
} )
166
138
}
167
139
168
- private containsPath ( entry : EditorSessionEntry , path : string ) : boolean {
169
- return entry . workspace . folders . some ( ( folder ) => path . startsWith ( folder . uri . path ) )
140
+ async deleteSession ( socketPath : string ) : Promise < void > {
141
+ logger . debug ( `Deleting session from session registry: ${ socketPath } ` )
142
+ this . entries . delete ( socketPath )
170
143
}
171
144
172
145
/**
173
146
* Returns the best socket path that we can connect to.
174
- * See getSocketPaths for the order of preference.
175
147
* We also delete any sockets that we can't connect to.
176
148
*/
177
- async getConnectedSocketPath ( path : string ) : Promise < string | undefined > {
178
- const candidates = this . getCandidatesForFile ( path )
149
+ async getConnectedSocketPath ( filePath : string ) : Promise < string | undefined > {
150
+ const candidates = this . getCandidatesForFile ( filePath )
179
151
let match : EditorSessionEntry | undefined = undefined
180
152
181
153
const toDelete = new Set < EditorSessionEntry > ( )
@@ -189,11 +161,49 @@ export class EditorSessionManager {
189
161
190
162
if ( toDelete . size > 0 ) {
191
163
for ( const candidate of toDelete ) {
192
- logger . debug ( `Deleting stale socket from socket registry: ${ candidate . socketPath } ` )
193
- this . entries . delete ( candidate . socketPath )
164
+ this . deleteSession ( candidate . socketPath )
194
165
}
195
166
}
196
167
197
168
return match ?. socketPath
198
169
}
199
170
}
171
+
172
+ export class EditorSessionManagerClient {
173
+ constructor ( private codeServerSocketPath : string ) { }
174
+
175
+ async canConnect ( ) {
176
+ return canConnect ( this . codeServerSocketPath )
177
+ }
178
+
179
+ async getConnectedSocketPath ( filePath : string ) : Promise < string | undefined > {
180
+ const response = await new Promise < GetSessionResponse > ( ( resolve , reject ) => {
181
+ const opts = {
182
+ path : "/session?filePath=" + encodeURIComponent ( filePath ) ,
183
+ socketPath : this . codeServerSocketPath ,
184
+ }
185
+ const req = http . get ( opts , ( res ) => {
186
+ let rawData = ""
187
+ res . setEncoding ( "utf8" )
188
+ res . on ( "data" , ( chunk ) => {
189
+ rawData += chunk
190
+ } )
191
+ res . on ( "end" , ( ) => {
192
+ try {
193
+ const obj = JSON . parse ( rawData )
194
+ if ( res . statusCode === 200 ) {
195
+ resolve ( obj )
196
+ } else {
197
+ reject ( new Error ( "Unexpected status code: " + res . statusCode ) )
198
+ }
199
+ } catch ( e : unknown ) {
200
+ reject ( e )
201
+ }
202
+ } )
203
+ } )
204
+ req . on ( "error" , reject )
205
+ req . end ( )
206
+ } )
207
+ return response . socketPath
208
+ }
209
+ }
0 commit comments