Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/import assert from'assert';importtype*as http from'http';import*as net from'net';import{ ensureNoDisposablesAreLeakedInTestSuite }from'../../../../base/test/common/utils.js';import{ NullLogService }from'../../../log/common/log.js';import{
ILoopbackProxyRuntime,
IProxyInFlight,
LoopbackProxyServer,
readProxyRequestBody,}from'../../node/shared/loopbackProxyServer.js';// #region Test subclassinterfaceITestState{
value:string;
req httpIncomingMessage
res httpServerResponse
runtime ILoopbackProxyRuntimeITestState
createStateCalls
requestHandler _req res
res
res
internalErrorWriter res httpServerResponse
name
name
ITestState
createStateCalls
value
override
req httpIncomingMessage
res httpServerResponse
runtime ILoopbackProxyRuntimeITestState
req res runtime
override res httpServerResponse
internalErrorWriter
res
res
value ITestHandle
runtime release
value
runtimestatevalue value
baseUrl runtimebaseUrl
nonce runtimenonce
runtime
dispose release
baseUrl
nonce
runtime ILoopbackProxyRuntimeITestState
seeds
requestHandler _req res
res
res
name
name
seed ITestState
seedsseed
value seed
override
req httpIncomingMessage
res httpServerResponse
runtime ILoopbackProxyRuntimeITestState
req res runtime
seed ITestHandle
runtime release seed
baseUrl runtimebaseUrl
nonce runtimenonce
runtime
dispose release
_httpModule http
http
_httpModule
_httpModule
_httpModule
status
headers httpIncomingHttpHeaders
body
parsed
url
init method headers Record body
onResponse res httpIncomingMessage
IFetchResult
httpMod resolve reject
u url
req httpMod
hostname uhostname
port uport
path upathname usearch
method initmethod
headers initheaders
res
chunks Buffer
res c chunksBufferc c Bufferc
res
body Bufferchunks
parsed
parsed body body parsed
status resstatusCode headers resheaders body parsed
res reject
onResponseres req
req reject
initbody
reqinitbody
req
url
url
err
code err NodeJSErrnoExceptioncode
code code code
service
handle service
asserthandlebaseUrl
asserthandlenonce
handle
service
service
handle service
asserthandlebaseUrl
port handlebaseUrlport
refusedOnIpv6 resolve
socket net host port
socket socket
socket socket
assertrefusedOnIpv6
handle
service
service
service _req res
res
res hello
handle service
res
assertresstatus
assertresparsed hello
handle
service
service
seen ILoopbackProxyRuntimeITestState
service _req res runtime
seen runtime
res
res
handle service
assertseen handleruntime
assertseenbaseUrl handlebaseUrl
assertseennonce handlenonce
assertseenstatevalue
handle
service
service
h1 h2
service
service
asserth1baseUrl h2baseUrl
asserth1nonce h2nonce
asserth1runtimestate h2runtimestate
assertservicecreateStateCalls
h1
h2
service
service
h1 service
h2 service
h1
res
assertresstatus
h2
service
service
handle service
baseUrl handlebaseUrl
assert status
handle
assert
service
service
h1 service
nonce1 h1nonce
h1
h2 service
asserth2nonce nonce1
asserth2baseUrl
assertservicecreateStateCalls
h2
service
service
firstRequestValue
service _req res runtime
firstRequestValue runtimestatevalue
res
res
handle service
assert
seeds serviceseeds state handleruntimestatevalue firstRequestValue
seeds state firstRequestValue
handle
service
service
h1 h2
service
service
assert
seeds serviceseeds shared h1runtimestate h2runtimestate value h1runtimestatevalue
seeds shared value
h1
h2
service
service
h1 service
h1
h2 service
assert
seeds serviceseeds value h2runtimestatevalue
seeds value
h2
service
service
handle service
baseUrl handlebaseUrl
service
assert
handle
service
startPromise service
service
assert startPromise
service
service
assert service
service
handle service
handle
service
service
handle
service
service
assert service
service
service
handle service
res
assertresstatus
assertresparsed error type message
handle
service
service
service res
res
res custom
service
handle service
res
assertresstatus
assertresparsed custom
handle
service
service
service _req res
res
res
handle service
res
assertresstatus
assertresbody
handle
service
service
aborted
entered
handlerEntered resolve entered resolve
service _req res runtime
entry IProxyInFlight ac res clientGone
runtimeinFlightentry
res entryclientGone entryac
resolve
entryacsignal
aborted
entryclientGone reswritableEnded
res
runtimeinFlightentry
handle service
reqError err NodeJSErrnoException err
handlerEntered
service
result reqError
assertresult
assertaborted
handle
service
received
service req res
received req
res
res
handle service
payload greeting n
method body payload
assertreceived payload
handle
service
service
received
service req res
received req
res
res
handle service
method
assertreceived
handle
service
/**
* Minimal concrete proxy used to drive the shared {@link LoopbackProxyServer}
* lifecycle in isolation. The request handler and internal-error writer are
* swappable per test; `createState` is counted so we can assert one state per
* bind.
*/
class
TestProxyServer
extends
LoopbackProxyServer<ITestState>
{
=
0
;
:
RequestHandler
=
async
(
,
)
=>
{
.
writeHead
(
200
,
{
'Content-Type'
:
'text/plain'
}
)
;
.
end
(
'ok'
)
;
}
;
:
(
(
:
.
)
=>
void
)
|
undefined
;
constructor
(
=
'TestProxyServer'
)
{
super
(
,
new
NullLogService
(
)
)
;
}
protected
createState
(
)
:
{
this
.
++
;
return
{
:
''
}
;
}
protected
handleRequest
(
:
.
,
:
.
,
:
<
>
,
)
:
Promise
<
void
>
{
return
this
.
requestHandler
(
,
,
)
;
}
protected
writeInternalError
(
:
.
)
:
void
{
if
(
this
.
)
{
this
.
internalErrorWriter
(
)
;
return
;
}
super
.
writeInternalError
(
)
;
}
/** Test-only public wrapper around the protected {@link acquire}. */
async
startHandle
(
?
:
string
)
:
Promise
<
>
{
const
{
,
}
=
await
this
.
acquire
(
)
;
if
(
!==
undefined
)
{
.
.
=
;
}
return
{
:
.
,
:
.
,
,
:
,
}
;
}
}
interface
ITestHandle
{
readonly
:
string
;
readonly
:
string
;
readonly
:
<
>
;
dispose
(
)
:
void
;
}
/**
* Concrete proxy whose per-bind state is seeded at `acquire()` time, used to
* exercise the seed → {@link LoopbackProxyServer.createState} flow. Every seed
* threaded into `createState` is recorded so tests can assert when — and with
* which value — the state was built.
*/
class
SeededTestProxyServer
extends
LoopbackProxyServer<ITestState,string>
{
readonly
:
string
[
]
=
[
]
;
:
RequestHandler
=
async
(
,
)
=>
{
.
writeHead
(
200
,
{
'Content-Type'
:
'text/plain'
}
)
;
.
end
(
'ok'
)
;
}
;
constructor
(
=
'SeededTestProxyServer'
)
{
super
(
,
new
NullLogService
(
)
)
;
}
protected
createState
(
:
string
)
:
{
this
.
.
push
(
)
;
return
{
:
}
;
}
protected
handleRequest
(
:
.
,
:
.
,
:
<
>
,
)
:
Promise
<
void
>
{
return
this
.
requestHandler
(
,
,
)
;
}
/** Test-only public wrapper around the protected {@link acquire}. */
async
startHandle
(
:
string
)
:
Promise
<
>
{
const
{
,
}
=
await
this
.
acquire
(
)
;
return
{
:
.
,
:
.
,
,
:
,
}
;
}
}
// #endregion
// #region HTTP helpers
let
:
typeof
|
undefined
;
async
function
getHttp
(
)
:
Promise
<
typeof
>
{
if
(
!
)
{
=
await
import
(
'http'
)
;
}
return
;
}
interface
IFetchResult
{
:
number
;
:
.
;
:
string
;
:
unknown
;
}
function
fetchHttp
(
:
string
,
?
:
{
?
:
string
;
?
:
<
string
,
string
>
;
?
:
string
}
,
?
:
(
:
.
,
abort
:
(
)
=>
void
)
=>
void
,
)
:
Promise
<
>
{
return
getHttp
(
)
.
then
(
=>
new
Promise<IFetchResult>
(
(
,
)
=>
{
const
=
new
URL
(
)
;
const
=
.
request
(
{
:
.
,
:
.
,
:
.
+
.
,
:
?.
??
'GET'
,
:
?.
,
}
,
=>
{
const
:
[
]
=
[
]
;
.
on
(
'data'
,
=>
.
push
(
.
isBuffer
(
)
?
:
.
from
(
)
)
)
;
.
on
(
'end'
,
(
)
=>
{
const
=
.
concat
(
)
.
toString
(
'utf8'
)
;
let
:
unknown
;
try
{
=
?
JSON
.
parse
(
)
:
undefined
;
}
catch
{
=
undefined
;
}
resolve
(
{
:
.
??
0
,
:
.
,
,
}
)
;
}
)
;
.
on
(
'error'
,
)
;
?.
(
,
(
)
=>
.
destroy
(
)
)
;
}
)
;
.
on
(
'error'
,
)
;
if
(
?.
!==
undefined
)
{
.
write
(
.
)
;
}
.
end
(
)
;
}
)
)
;
}
/** Resolves `true` if the connection was refused (server torn down). */
async
function
isConnectionRefused
(
:
string
)
:
Promise
<
boolean
>
{
try
{
await
fetchHttp
(
)
;
return
false
;
}
catch
(
)
{
const
=
(
as
.
)
.
;
return
===
'ECONNREFUSED'
||
===
'ECONNRESET'
||
===
'ECONNABORTED'
;
}
}
// #endregion
suite
(
'LoopbackProxyServer'
,
(
)
=>
{
ensureNoDisposablesAreLeakedInTestSuite
(
)
;
// #region Lifecycle & binding
suite
(
'Lifecycle & binding'
,
(
)
=>
{
test
(
'startHandle() returns a loopback baseUrl and 256-bit hex nonce'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
try
{
.
match
(
.
,
/^http:\/\/127\.0\.0\.1:\d+$/
)
;
.
match
(
.
,
/^[0-9a-f]{64}$/
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'binds only on the IPv4 loopback interface'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
try
{
.
match
(
.
,
/^http:\/\/127\.0\.0\.1:\d+$/
)
;
// Binding to 127.0.0.1 must NOT also listen on the IPv6
// loopback (::1); a connection there should be refused.
const
=
Number
(
new
URL
(
.
)
.
)
;
const
=
await
new
Promise<boolean>
(
=>
{
const
=
.
connect
(
{
:
'::1'
,
}
)
;
.
once
(
'connect'
,
(
)
=>
{
.
destroy
(
)
;
resolve
(
false
)
;
}
)
;
.
once
(
'error'
,
(
)
=>
{
.
destroy
(
)
;
resolve
(
true
)
;
}
)
;
}
)
;
.
strictEqual
(
,
true
,
'server should not be reachable on ::1'
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'serves real requests via handleRequest'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
.
requestHandler
=
async
(
,
)
=>
{
.
writeHead
(
201
,
{
'Content-Type'
:
'application/json'
}
)
;
.
end
(
JSON
.
stringify
(
{
:
'world'
}
)
)
;
}
;
const
=
await
.
startHandle
(
)
;
try
{
const
=
await
fetchHttp
(
`${handle.baseUrl}/anything`
)
;
.
strictEqual
(
.
,
201
)
;
.
deepStrictEqual
(
.
,
{
:
'world'
}
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'handleRequest receives the runtime with baseUrl, nonce and state'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
let
:
<
>
|
undefined
;
.
requestHandler
=
async
(
,
,
)
=>
{
=
;
.
writeHead
(
200
)
;
.
end
(
)
;
}
;
const
=
await
.
startHandle
(
'payload'
)
;
try
{
await
fetchHttp
(
`${handle.baseUrl}/`
)
;
.
strictEqual
(
,
.
)
;
.
strictEqual
(
?.
,
.
)
;
.
strictEqual
(
?.
,
.
)
;
.
strictEqual
(
?.
.
,
'payload'
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
}
)
;
// #endregion
// #region Refcounting
suite
(
'Refcounting'
,
(
)
=>
{
test
(
'concurrent acquires share a single bind and one state object'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
// Issue both before the first bind resolves; they must share
// the runtime rather than each binding a server.
const
[
,
]
=
await
Promise
.
all
(
[
.
startHandle
(
'a'
)
,
.
startHandle
(
'b'
)
,
]
)
;
try
{
.
strictEqual
(
.
,
.
)
;
.
strictEqual
(
.
,
.
)
;
.
strictEqual
(
.
.
,
.
.
,
'state is shared by reference'
)
;
.
strictEqual
(
.
,
1
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'disposing one handle while another is alive keeps the server up'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
const
=
await
.
startHandle
(
)
;
.
dispose
(
)
;
try
{
const
=
await
fetchHttp
(
`${h2.baseUrl}/`
)
;
.
strictEqual
(
.
,
200
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'disposing the last handle tears the server down'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
const
=
.
;
// Reachable while held.
.
strictEqual
(
(
await
fetchHttp
(
`${baseUrl}/`
)
)
.
,
200
)
;
.
dispose
(
)
;
.
strictEqual
(
await
isConnectionRefused
(
`${baseUrl}/`
)
,
true
)
;
.
dispose
(
)
;
}
)
;
test
(
'startHandle() after refcount-0 teardown rebinds with a fresh nonce and new state'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
const
=
.
;
.
dispose
(
)
;
const
=
await
.
startHandle
(
)
;
try
{
.
notStrictEqual
(
.
,
)
;
.
match
(
.
,
/^http:\/\/127\.0\.0\.1:\d+$/
)
;
.
strictEqual
(
.
,
2
,
'state is rebuilt per bind'
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
}
)
;
// #endregion
// #region Seeding
suite
(
'Seeding'
,
(
)
=>
{
test
(
'acquire seeds createState so the state is born valid with no placeholder window'
,
async
(
)
=>
{
const
=
new
SeededTestProxyServer
(
)
;
// Capture the state the very first dispatched request observes to
// prove no empty/placeholder value is ever visible on the wire.
let
:
string
|
undefined
;
.
requestHandler
=
async
(
,
,
)
=>
{
=
.
.
;
.
writeHead
(
200
)
;
.
end
(
)
;
}
;
const
=
await
.
startHandle
(
'token-1'
)
;
try
{
await
fetchHttp
(
`${handle.baseUrl}/`
)
;
.
deepStrictEqual
(
{
:
.
,
:
.
.
.
,
}
,
{
:
[
'token-1'
]
,
:
'token-1'
,
:
'token-1'
}
,
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'concurrent acquires build state once from the seed that wins the bind'
,
async
(
)
=>
{
const
=
new
SeededTestProxyServer
(
)
;
// Both are issued before the first bind resolves; the first caller
// wins the bind race so createState runs once with its seed, while
// the second just joins the shared runtime.
const
[
,
]
=
await
Promise
.
all
(
[
.
startHandle
(
'token-1'
)
,
.
startHandle
(
'token-2'
)
,
]
)
;
try
{
.
deepStrictEqual
(
{
:
.
,
:
.
.
===
.
.
,
:
.
.
.
}
,
{
:
[
'token-1'
]
,
:
true
,
:
'token-1'
}
,
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'rebinding after refcount-0 teardown re-seeds createState with the new value'
,
async
(
)
=>
{
const
=
new
SeededTestProxyServer
(
)
;
const
=
await
.
startHandle
(
'token-1'
)
;
.
dispose
(
)
;
const
=
await
.
startHandle
(
'token-2'
)
;
try
{
.
deepStrictEqual
(
{
:
.
,
:
.
.
.
}
,
{
:
[
'token-1'
,
'token-2'
]
,
:
'token-2'
}
,
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
}
)
;
// #endregion
// #region Dispose semantics
suite
(
'Dispose semantics'
,
(
)
=>
{
test
(
'explicit dispose() tears down regardless of live handles'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
const
=
.
;
.
dispose
(
)
;
// Handle is still "held" by the caller, but the service is gone.
.
strictEqual
(
await
isConnectionRefused
(
`${baseUrl}/`
)
,
true
)
;
// Releasing the now-stale handle must be a safe no-op.
.
dispose
(
)
;
}
)
;
test
(
'dispose() while a bind is in flight rejects the pending acquire'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
.
startHandle
(
)
;
.
dispose
(
)
;
await
.
rejects
(
(
)
=>
,
/disposed/
)
;
}
)
;
test
(
'acquire after dispose() rejects'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
.
dispose
(
)
;
await
.
rejects
(
(
)
=>
.
startHandle
(
)
,
/disposed/
)
;
}
)
;
test
(
'dispose() is idempotent'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
const
=
await
.
startHandle
(
)
;
.
dispose
(
)
;
.
dispose
(
)
;
.
dispose
(
)
;
// Re-disposing the released handle is also a no-op.
.
dispose
(
)
;
}
)
;
test
(
'error message is prefixed with the proxy name'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
'MyCustomProxy'
)
;
.
dispose
(
)
;
await
.
rejects
(
(
)
=>
.
startHandle
(
)
,
/MyCustomProxy has been disposed/
)
;
}
)
;
}
)
;
// #endregion
// #region Unhandled errors
suite
(
'Unhandled errors'
,
(
)
=>
{
test
(
'throw before headers → default internal-error envelope (500)'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
.
requestHandler
=
async
(
)
=>
{
throw
new
Error
(
'boom'
)
;
}
;
const
=
await
.
startHandle
(
)
;
try
{
const
=
await
fetchHttp
(
`${handle.baseUrl}/`
)
;
.
strictEqual
(
.
,
500
)
;
.
deepStrictEqual
(
.
,
{
:
{
:
'api_error'
,
:
'Internal proxy error'
}
}
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'throw before headers → subclass writeInternalError override is used'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
.
internalErrorWriter
=
=>
{
.
writeHead
(
503
,
{
'Content-Type'
:
'application/json'
}
)
;
.
end
(
JSON
.
stringify
(
{
:
true
}
)
)
;
}
;
.
requestHandler
=
async
(
)
=>
{
throw
new
Error
(
'boom'
)
;
}
;
const
=
await
.
startHandle
(
)
;
try
{
const
=
await
fetchHttp
(
`${handle.baseUrl}/`
)
;
.
strictEqual
(
.
,
503
)
;
.
deepStrictEqual
(
.
,
{
:
true
}
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'throw after headers are sent → response is ended without crashing'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
.
requestHandler
=
async
(
,
)
=>
{
.
writeHead
(
200
,
{
'Content-Type'
:
'text/plain'
}
)
;
.
write
(
'partial'
)
;
throw
new
Error
(
'boom after headers'
)
;
}
;
const
=
await
.
startHandle
(
)
;
try
{
const
=
await
fetchHttp
(
`${handle.baseUrl}/`
)
;
.
strictEqual
(
.
,
200
)
;
.
strictEqual
(
.
,
'partial'
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
}
)
;
// #endregion
// #region In-flight abort
suite
(
'In-flight abort'
,
(
)
=>
{
test
(
'dispose() aborts in-flight requests and destroys their sockets'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
let
=
false
;
let
!
:
(
)
=>
void
;
const
=
new
Promise<void>
(
=>
{
=
;
}
)
;
.
requestHandler
=
async
(
,
,
)
=>
{
const
:
=
{
:
new
AbortController
(
)
,
,
:
false
}
;
.
.
add
(
)
;
.
on
(
'close'
,
(
)
=>
{
.
=
true
;
.
.
abort
(
)
;
}
)
;
try
{
entered
(
)
;
await
new
Promise<void>
(
=>
{
.
.
.
addEventListener
(
'abort'
,
(
)
=>
{
=
true
;
// Service-driven abort: socket still open → destroy.
if
(
!
.
&&
!
.
)
{
.
destroy
(
)
;
}
resolve
(
)
;
}
)
;
}
)
;
}
finally
{
.
.
delete
(
)
;
}
}
;
const
=
await
.
startHandle
(
)
;
const
=
fetchHttp
(
`${handle.baseUrl}/`
)
.
catch
(
(
:
.
)
=>
)
;
await
;
.
dispose
(
)
;
const
=
await
;
.
ok
(
instanceof
Error
,
'client request should error when the socket is destroyed'
)
;
.
strictEqual
(
,
true
,
'in-flight AbortController should have fired'
)
;
.
dispose
(
)
;
}
)
;
}
)
;
// #endregion
// #region readProxyRequestBody
suite
(
'readProxyRequestBody'
,
(
)
=>
{
test
(
'reads the full request body as UTF-8'
,
async
(
)
=>
{
const
=
new
TestProxyServer
(
)
;
let
:
string
|
undefined
;
.
requestHandler
=
async
(
,
)
=>
{
=
await
readProxyRequestBody
(
)
;
.
writeHead
(
200
)
;
.
end
(
)
;
}
;
const
=
await
.
startHandle
(
)
;
try
{
const
=
JSON
.
stringify
(
{
:
'héllo 🌍'
,
:
42
}
)
;
await
fetchHttp
(
`${handle.baseUrl}/`
,
{
:
'POST'
,
:
}
)
;
.
strictEqual
(
,
)
;
}
finally
{
.
dispose
(
)
;
.
dispose
(
)
;
}
}
)
;
test
(
'resolves to an empty string for a body-less request'