@@ -153,9 +153,39 @@ async function handler(request: Request): Promise<Response> {
153
153
154
154
async function proxy ( request : Request , token : string ) : Promise < Response > {
155
155
const editor = tokenToEditor [ token ] ;
156
+ let shutdown = async function ( ) {
157
+ console . log ( "Shutting down port " + editor . port + " VSCode instance" ) ;
158
+ delete tokenToEditor [ token ] ;
159
+ editor . process . kill ( "SIGHUP" ) ;
160
+ await editor . process . status ( ) ;
161
+ nextDisplay . push ( editor . display ) ;
162
+ } ;
163
+ let decrRefCount = function ( ) {
164
+ -- editor . refCount ;
165
+ if ( editor . refCount == 0 ) {
166
+ console . log ( "Last client closed for port " + editor . port + " VSCode instance" ) ;
167
+ editor . shutdownHandle = setTimeout ( shutdown , SHUTDOWN_TIMEOUT_S * 1000 ) ;
168
+ }
169
+ } ;
170
+ let incrRefCount = function ( ) {
171
+ if ( editor . refCount == 0 && editor . shutdownHandle ) {
172
+ console . log ( "Client reopened for port " + editor . port + " VSCode instance" ) ;
173
+ clearTimeout ( editor . shutdownHandle ) ;
174
+ delete editor . shutdownHandle ;
175
+ }
176
+ ++ editor . refCount ;
177
+ } ;
178
+
156
179
const url = new URL ( request . url ) ;
157
180
url . hostname = "localhost" ;
158
- url . port = String ( editor . port ) ;
181
+ if ( url . pathname . startsWith ( "/vnc/" ) || url . pathname == "/websockify" ) {
182
+ url . port = String ( editor . displayPort ) ;
183
+ url . pathname = url . pathname . replace ( "/vnc/" , "/" ) ;
184
+ shutdown = nop ;
185
+ decrRefCount = nop ;
186
+ incrRefCount = nop ;
187
+ } else
188
+ url . port = String ( editor . port ) ;
159
189
160
190
if ( request . headers . get ( "upgrade" ) != "websocket" ) {
161
191
let response = await fetch ( new Request ( String ( url ) , request ) , { redirect : "manual" } ) ;
@@ -166,9 +196,7 @@ async function proxy(request: Request, token: string): Promise<Response> {
166
196
+ "location.search.startsWith(\"?folder=\")"
167
197
+ "|| location.search.startsWith(\"?workspace=\")"
168
198
+ ")"
169
- + "\nopen(\"http://\" + location.hostname + \":"
170
- + editor . displayPort
171
- + "/vnc_lite.html\", \"_blank\", \"popup\");"
199
+ + "\nopen(\"vnc/vnc_lite.html\", \"_blank\", \"popup\");"
172
200
,
173
201
response ,
174
202
) ;
@@ -178,28 +206,6 @@ async function proxy(request: Request, token: string): Promise<Response> {
178
206
179
207
const serverSocket = new WebSocket ( String ( url ) ) ;
180
208
const { response, socket} = Deno . upgradeWebSocket ( request ) ;
181
- const shutdown = async function ( ) {
182
- console . log ( "Shutting down port " + editor . port + " VSCode instance" ) ;
183
- delete tokenToEditor [ token ] ;
184
- editor . process . kill ( "SIGHUP" ) ;
185
- await editor . process . status ( ) ;
186
- nextDisplay . push ( editor . display ) ;
187
- } ;
188
- const decrRefCount = function ( ) {
189
- -- editor . refCount ;
190
- if ( editor . refCount == 0 ) {
191
- console . log ( "Last client closed for port " + editor . port + " VSCode instance" ) ;
192
- editor . shutdownHandle = setTimeout ( shutdown , SHUTDOWN_TIMEOUT_S * 1000 ) ;
193
- }
194
- } ;
195
- const incrRefCount = function ( ) {
196
- if ( editor . refCount == 0 && editor . shutdownHandle ) {
197
- console . log ( "Client reopened for port " + editor . port + " VSCode instance" ) ;
198
- clearTimeout ( editor . shutdownHandle ) ;
199
- delete editor . shutdownHandle ;
200
- }
201
- ++ editor . refCount ;
202
- } ;
203
209
204
210
const serverBootstrap = deferred ( ) ;
205
211
serverSocket . onopen = function ( ) {
@@ -259,6 +265,10 @@ async function parsePortsAndToken(
259
265
} ;
260
266
}
261
267
268
+ function nop ( ) : Promise < void > {
269
+ return Promise . resolve ( ) ;
270
+ }
271
+
262
272
const address : Partial < Deno . ListenOptions > = {
263
273
hostname : "localhost" ,
264
274
} ;
0 commit comments