1
1
import * as vscode from 'vscode'
2
- import axios from 'axios'
2
+ import * as http from 'http'
3
+ import * as https from 'https'
4
+ import { URL } from 'url'
3
5
import { getUIStorageField , setUIStorageField } from './lib'
4
6
5
7
/*
@@ -56,13 +58,76 @@ async function getBackendBaseUrl(): Promise<string> {
56
58
return `http://localhost:${ appPort } `
57
59
}
58
60
61
+ // Helper function to make HTTP requests using Node.js built-in modules
62
+ async function makeHttpRequest (
63
+ url : string ,
64
+ method : 'GET' | 'POST' | 'DELETE' = 'GET' ,
65
+ data ?: any ,
66
+ ) : Promise < any > {
67
+ return new Promise ( ( resolve , reject ) => {
68
+ const parsedUrl = new URL ( url )
69
+ const isHttps = parsedUrl . protocol === 'https:'
70
+ const httpModule = isHttps ? https : http
71
+
72
+ const headers : { [ key : string ] : string | number } = {
73
+ 'Content-Type' : 'application/json' ,
74
+ }
75
+
76
+ if ( data && method === 'POST' ) {
77
+ const postData = JSON . stringify ( data )
78
+ headers [ 'Content-Length' ] = Buffer . byteLength ( postData )
79
+ }
80
+
81
+ const options = {
82
+ hostname : parsedUrl . hostname ,
83
+ port : parsedUrl . port || ( isHttps ? 443 : 80 ) ,
84
+ path : parsedUrl . pathname + parsedUrl . search ,
85
+ method,
86
+ headers,
87
+ }
88
+
89
+ const req = httpModule . request ( options , ( res ) => {
90
+ let responseData = ''
91
+
92
+ res . on ( 'data' , ( chunk ) => {
93
+ responseData += chunk
94
+ } )
95
+
96
+ res . on ( 'end' , ( ) => {
97
+ try {
98
+ const parsedData = responseData ? JSON . parse ( responseData ) : { }
99
+ resolve ( {
100
+ status : res . statusCode ,
101
+ data : parsedData ,
102
+ } )
103
+ } catch ( error ) {
104
+ resolve ( {
105
+ status : res . statusCode ,
106
+ data : responseData ,
107
+ } )
108
+ }
109
+ } )
110
+ } )
111
+
112
+ req . on ( 'error' , ( error ) => {
113
+ reject ( error )
114
+ } )
115
+
116
+ if ( data && method === 'POST' ) {
117
+ req . write ( JSON . stringify ( data ) )
118
+ }
119
+
120
+ req . end ( )
121
+ } )
122
+ }
123
+
59
124
// Helper function to execute Redis CLI command on a database
60
125
async function executeCliCommand ( databaseId : string , command : string ) : Promise < CliCommandResponse | null > {
61
126
try {
62
127
const baseUrl = await getBackendBaseUrl ( )
63
128
64
129
// First create a CLI client for the database
65
- const createResponse = await axios . post ( `${ baseUrl } /databases/${ databaseId } /cli` )
130
+ const createResponse = await makeHttpRequest ( `${ baseUrl } /databases/${ databaseId } /cli` , 'POST' )
66
131
67
132
if ( createResponse . status !== 201 ) {
68
133
console . error ( 'Failed to create CLI client' )
@@ -78,16 +143,17 @@ async function executeCliCommand(databaseId: string, command: string): Promise<C
78
143
79
144
try {
80
145
// Execute the command
81
- const commandResponse = await axios . post (
146
+ const commandResponse = await makeHttpRequest (
82
147
`${ baseUrl } /databases/${ databaseId } /cli/${ cliClientUuid } /send-command` ,
148
+ 'POST' ,
83
149
{
84
150
command,
85
151
outputFormat : 'RAW' ,
86
152
} ,
87
153
)
88
154
89
155
// Clean up: delete the CLI client
90
- await axios . delete ( `${ baseUrl } /databases/${ databaseId } /cli/${ cliClientUuid } ` )
156
+ await makeHttpRequest ( `${ baseUrl } /databases/${ databaseId } /cli/${ cliClientUuid } ` , 'DELETE' )
91
157
92
158
if ( commandResponse . status === 200 ) {
93
159
return {
@@ -100,7 +166,7 @@ async function executeCliCommand(databaseId: string, command: string): Promise<C
100
166
} catch ( error ) {
101
167
// Clean up: delete the CLI client even if command execution failed
102
168
try {
103
- await axios . delete ( `${ baseUrl } /databases/${ databaseId } /cli/${ cliClientUuid } ` )
169
+ await makeHttpRequest ( `${ baseUrl } /databases/${ databaseId } /cli/${ cliClientUuid } ` , 'DELETE' )
104
170
} catch ( cleanupError ) {
105
171
console . error ( 'Failed to cleanup CLI client:' , cleanupError )
106
172
}
@@ -119,7 +185,7 @@ async function executeCliCommand(databaseId: string, command: string): Promise<C
119
185
export async function getAllDatabases ( ) : Promise < DatabaseInstance [ ] > {
120
186
try {
121
187
const baseUrl = await getBackendBaseUrl ( )
122
- const response = await axios . get ( `${ baseUrl } /databases` )
188
+ const response = await makeHttpRequest ( `${ baseUrl } /databases` )
123
189
124
190
if ( response . status === 200 && Array . isArray ( response . data ) ) {
125
191
return response . data . map ( ( db : any ) => ( {
0 commit comments