1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#include "root.h"
#include <uws/src/App.h>
#include <JavaScriptCore/InspectorFrontendChannel.h>
#include <JavaScriptCore/JSGlobalObjectDebuggable.h>
#include <JavaScriptCore/JSGlobalObjectDebugger.h>
#include <JavaScriptCore/Debugger.h>
extern "C" void Bun__tickWhilePaused(bool*);
namespace Bun {
using namespace JSC;
template<bool isSSL>
class BunInspectorConnection : public Inspector::FrontendChannel {
public:
using BunInspectorSocket = uWS::WebSocket<isSSL, true, BunInspectorConnection*>;
BunInspectorConnection(BunInspectorSocket* ws, JSC::JSGlobalObject* globalObject)
: ws(ws)
, globalObject(globalObject)
, pendingMessages()
{
}
~BunInspectorConnection()
{
}
Inspector::FrontendChannel::ConnectionType connectionType() const override
{
return Inspector::FrontendChannel::ConnectionType::Remote;
}
void onOpen(JSC::JSGlobalObject* globalObject)
{
this->globalObject = globalObject;
this->globalObject->inspectorDebuggable().connect(*this);
Inspector::JSGlobalObjectDebugger* debugger = reinterpret_cast<Inspector::JSGlobalObjectDebugger*>(this->globalObject->debugger());
if (debugger) {
debugger->runWhilePausedCallback = [](JSC::JSGlobalObject& globalObject, bool& isPaused) -> void {
Bun__tickWhilePaused(&isPaused);
};
}
}
void onClose()
{
this->globalObject->inspectorDebuggable().disconnect(*this);
this->pendingMessages.clear();
}
void sendMessageToFrontend(const String& message) override
{
send(message);
}
void send(const WTF::String& message)
{
if (ws->getBufferedAmount() == 0) {
WTF::CString messageCString = message.utf8();
ws->send(std::string_view { messageCString.data(), messageCString.length() }, uWS::OpCode::TEXT);
} else {
pendingMessages.append(message);
}
}
void onMessage(std::string_view message)
{
WTF::String messageString = WTF::String::fromUTF8(message.data(), message.length());
Inspector::JSGlobalObjectDebugger* debugger = reinterpret_cast<Inspector::JSGlobalObjectDebugger*>(this->globalObject->debugger());
if (debugger) {
debugger->runWhilePausedCallback = [](JSC::JSGlobalObject& globalObject, bool& done) -> void {
Inspector::JSGlobalObjectDebugger* debugger = reinterpret_cast<Inspector::JSGlobalObjectDebugger*>(globalObject.debugger());
Bun__tickWhilePaused(&done);
};
}
this->globalObject->inspectorDebuggable().dispatchMessageFromRemote(WTFMove(messageString));
}
void drain()
{
if (pendingMessages.size() == 0)
return;
if (ws->getBufferedAmount() == 0) {
ws->cork([&]() {
for (auto& message : pendingMessages) {
WTF::CString messageCString = message.utf8();
ws->send(std::string_view { messageCString.data(), messageCString.length() }, uWS::OpCode::TEXT);
}
pendingMessages.clear();
});
}
}
WTF::Vector<WTF::String> pendingMessages;
JSC::JSGlobalObject* globalObject;
BunInspectorSocket* ws;
};
using BunInspectorConnectionNoSSL = BunInspectorConnection<false>;
using SSLBunInspectorConnection = BunInspectorConnection<true>;
template<bool isSSL>
static void addInspector(void* app, JSC::JSGlobalObject* globalObject)
{
if constexpr (isSSL) {
auto handler = uWS::SSLApp::WebSocketBehavior<SSLBunInspectorConnection*> {
/* Settings */
.compression = uWS::DISABLED,
.maxPayloadLength = 16 * 1024 * 1024,
.idleTimeout = 960,
.maxBackpressure = 16 * 1024 * 1024,
.closeOnBackpressureLimit = false,
.resetIdleTimeoutOnSend = true,
.sendPingsAutomatically = true,
/* Handlers */
.upgrade = nullptr,
.open = [globalObject](auto* ws) {
globalObject->setInspectable(true);
*ws->getUserData() = new SSLBunInspectorConnection(ws, globalObject);
SSLBunInspectorConnection* inspector = *ws->getUserData();
inspector->onOpen(globalObject);
//
},
.message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
SSLBunInspectorConnection* inspector = *(SSLBunInspectorConnection**)ws->getUserData();
inspector->onMessage(message);
//
},
.drain = [](auto* ws) {
SSLBunInspectorConnection* inspector = *(SSLBunInspectorConnection**)ws->getUserData();
inspector->drain();
//
},
.ping = [](auto* /*ws*/, std::string_view) {
/* Not implemented yet */ },
.pong = [](auto* /*ws*/, std::string_view) {
/* Not implemented yet */ },
.close = [](auto* ws, int /*code*/, std::string_view /*message*/) {
SSLBunInspectorConnection* inspector = *(SSLBunInspectorConnection**)ws->getUserData();
inspector->onClose();
delete inspector; }
};
((uWS::SSLApp*)app)->ws<SSLBunInspectorConnection*>("/bun:inspect", std::move(handler));
} else {
auto handler = uWS::App::WebSocketBehavior<BunInspectorConnectionNoSSL*> {
/* Settings */
.compression = uWS::DISABLED,
.maxPayloadLength = 16 * 1024 * 1024,
.idleTimeout = 960,
.maxBackpressure = 16 * 1024 * 1024,
.closeOnBackpressureLimit = false,
.resetIdleTimeoutOnSend = true,
.sendPingsAutomatically = true,
/* Handlers */
.upgrade = nullptr,
.open = [globalObject](auto* ws) {
globalObject->setInspectable(true);
*ws->getUserData() = new BunInspectorConnectionNoSSL(ws, globalObject);
BunInspectorConnectionNoSSL* inspector = *ws->getUserData();
inspector->onOpen(globalObject);
//
},
.message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
BunInspectorConnectionNoSSL* inspector = *(BunInspectorConnectionNoSSL**)ws->getUserData();
inspector->onMessage(message);
//
},
.drain = [](auto* ws) {
BunInspectorConnectionNoSSL* inspector = *(BunInspectorConnectionNoSSL**)ws->getUserData();
inspector->drain();
//
},
.ping = [](auto* /*ws*/, std::string_view) {
/* Not implemented yet */ },
.pong = [](auto* /*ws*/, std::string_view) {
/* Not implemented yet */ },
.close = [](auto* ws, int /*code*/, std::string_view /*message*/) {
BunInspectorConnectionNoSSL* inspector = *(BunInspectorConnectionNoSSL**)ws->getUserData();
inspector->onClose();
delete inspector; }
};
((uWS::App*)app)->ws<BunInspectorConnectionNoSSL*>("/bun:inspect", std::move(handler));
}
}
extern "C" void Bun__addInspector(bool isSSL, void* app, JSC::JSGlobalObject* globalObject)
{
if (isSSL) {
addInspector<true>((uWS::TemplatedApp<true>*)app, globalObject);
} else {
addInspector<false>((uWS::TemplatedApp<false>*)app, globalObject);
}
};
}
|