@@ -6,28 +6,10 @@ import { GlobalArgs } from "../../../globalArgs";
66
77const LOGIN_REPONSE_HTML_FILE = "login_response.html" ;
88
9- const createServer = (
10- port : number ,
11- onRequest : (
12- req : http . IncomingMessage ,
13- res : http . ServerResponse ,
14- ) => Promise < void > ,
15- ) => {
16- return new http . Server ( ( req , res ) => {
17- onRequest ( req , res ) . catch ( ( err ) => {
18- res . writeHead ( 500 ) ;
19- res . end ( "Internal Server Error" ) ;
20- console . error ( err ) ;
21- } ) ;
22- } ) . listen ( port , ( ) =>
23- console . info (
24- `Listening for OAuth callback on http://127.0.0.1:${ port } /callback` ,
25- ) ) ;
26- } ;
27-
28- export const getAvailablePort = async ( ) : Promise < number > => {
29- const tempServer = http . createServer ( ) ;
9+ export function getAvailablePort ( ) : Promise < number > {
3010 return new Promise ( ( resolve , reject ) => {
11+ const tempServer = http . createServer ( ) ;
12+
3113 tempServer . listen ( 0 , ( ) => {
3214 const address = tempServer . address ( ) ;
3315 if ( address && typeof address === "object" ) {
@@ -38,9 +20,11 @@ export const getAvailablePort = async (): Promise<number> => {
3820 }
3921 } ) ;
4022
41- tempServer . on ( "error" , ( error : any ) => reject ( new Error ( `Error finding available port: ${ error . message } ` ) ) ) ;
23+ tempServer . on ( "error" , ( error : any ) => {
24+ reject ( new Error ( `Error finding available port: ${ error . message } ` ) ) ;
25+ } ) ;
4226 } ) ;
43- } ;
27+ }
4428
4529const loadHtmlFile = async ( filePath : string ) : Promise < string > => {
4630 try {
@@ -50,47 +34,50 @@ const loadHtmlFile = async (filePath: string): Promise<string> => {
5034 }
5135} ;
5236
53- const handleCallbackRequest = async (
54- req : http . IncomingMessage ,
55- res : http . ServerResponse ,
56- ) : Promise < void > => {
57- if ( ! req . url ?. startsWith ( "/callback" ) ) {
58- res . writeHead ( 404 ) ;
59- res . end ( "Not Found" ) ;
60- return ;
61- }
62-
63- const urlParams = new URLSearchParams ( req . url . split ( "?" ) [ 1 ] ) ;
64- const authorizationCode = urlParams . get ( "code" ) ;
65-
66- if ( ! authorizationCode ) {
67- res . writeHead ( 400 ) ;
68- res . end ( "Invalid callback request" ) ;
69- throw new Error ( "No authorization code found" ) ;
70- }
37+ export function startLocalServerForCallback ( port : number ) : Promise < string > {
38+ return new Promise ( ( resolve , reject ) => {
39+ const server = http . createServer ( async ( req , res ) => {
40+ if ( req . url ?. startsWith ( "/callback" ) ) {
41+ const urlParams = new URLSearchParams ( req . url . split ( "?" ) [ 1 ] ) ;
42+ const authorizationCode = urlParams . get ( "code" ) ;
7143
72- const htmlFilePath = vscode . Uri . file (
73- path . join ( GlobalArgs . getExtensionPath ( ) , "public" , LOGIN_REPONSE_HTML_FILE ) ,
74- ) ;
75- const htmlContent = await loadHtmlFile ( htmlFilePath . path ) ;
44+ if ( ! authorizationCode ) {
45+ res . writeHead ( 400 ) ;
46+ res . end ( "Invalid callback request" ) ;
47+ reject ( new Error ( "No authorization code found" ) ) ;
48+ return ;
49+ }
50+ const htmlFilePath = vscode . Uri . file (
51+ path . join (
52+ GlobalArgs . getExtensionPath ( ) ,
53+ "public" ,
54+ LOGIN_REPONSE_HTML_FILE ,
55+ ) ,
56+ ) ;
57+ const htmlContent = await loadHtmlFile ( htmlFilePath . path ) ;
7658
77- res . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
78- res . end ( htmlContent ) ;
79- } ;
59+ res . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
60+ res . end ( htmlContent ) ;
61+ server . close ( ) ;
8062
81- export const startLocalServerForCallback = async (
82- port : number ,
83- onClose ?: ( ) => void ,
84- ) : Promise < string | undefined > => {
85- const server = createServer ( port , handleCallbackRequest ) . close ( onClose ) ;
63+ resolve ( authorizationCode ) ;
64+ server . close ( ) ;
65+ } else {
66+ res . writeHead ( 404 ) ;
67+ res . end ( "Not Found" ) ;
68+ }
69+ } ) ;
8670
87- return new Promise ( ( resolve , reject ) => {
88- server . on ( "close" , ( ) => {
89- resolve ( undefined ) ;
71+ server . listen ( port , ( ) => {
72+ console . log (
73+ `Listening for OAuth callback on ${ server . address ( ) } :/callback` ,
74+ ) ;
9075 } ) ;
9176
92- server . on ( "error" , ( err ) => {
93- reject ( new Error ( `Failed to start server: ${ err . message } ` ) ) ;
77+ server . on ( "error" , ( error : any ) => {
78+ reject ( new Error ( `Failed to start server: ${ error . message } ` ) ) ;
9479 } ) ;
80+
81+ server . on ( "close" , ( ) => console . info ( "Server closed" ) ) ;
9582 } ) ;
96- } ;
83+ }
0 commit comments