@aptre/bldr-saucer 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aptre/bldr-saucer",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Native webview bridge for Bldr using Saucer",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -33,11 +33,11 @@
33
33
  "release:publish": "git push && git push --tags"
34
34
  },
35
35
  "optionalDependencies": {
36
- "@aptre/bldr-saucer-darwin-arm64": "0.2.3",
37
- "@aptre/bldr-saucer-darwin-x64": "0.2.3",
38
- "@aptre/bldr-saucer-linux-x64": "0.2.3",
39
- "@aptre/bldr-saucer-linux-arm64": "0.2.3",
40
- "@aptre/bldr-saucer-win32-x64": "0.2.3"
36
+ "@aptre/bldr-saucer-darwin-arm64": "0.2.5",
37
+ "@aptre/bldr-saucer-darwin-x64": "0.2.5",
38
+ "@aptre/bldr-saucer-linux-x64": "0.2.5",
39
+ "@aptre/bldr-saucer-linux-arm64": "0.2.5",
40
+ "@aptre/bldr-saucer-win32-x64": "0.2.5"
41
41
  },
42
42
  "files": [
43
43
  "index.js",
@@ -46,7 +46,7 @@
46
46
  "CMakeLists.txt"
47
47
  ],
48
48
  "devDependencies": {
49
- "@aptre/common": "^0.30.1"
49
+ "@aptre/common": "^0.30.3"
50
50
  },
51
51
  "dependencies": {
52
52
  "@aptre/protobuf-es-lite": "^0.5.2"
package/src/main.cpp CHANGED
@@ -4,15 +4,20 @@
4
4
  #include "pipe_connection.h"
5
5
  #include "scheme_forwarder.h"
6
6
 
7
+ #include <atomic>
7
8
  #include <cstdlib>
9
+ #include <cstring>
8
10
  #include <iostream>
11
+ #include <memory>
12
+ #include <mutex>
9
13
  #include <string>
10
14
  #include <thread>
15
+ #include <vector>
11
16
 
12
17
  coco::stray start(saucer::application* app) {
13
18
  const char* runtime_id_env = std::getenv("BLDR_RUNTIME_ID");
14
19
  if (!runtime_id_env) {
15
- std::cerr << "BLDR_RUNTIME_ID not set" << std::endl;
20
+ std::cerr << "[bldr-saucer] BLDR_RUNTIME_ID not set" << std::endl;
16
21
  co_return;
17
22
  }
18
23
  std::string runtime_id = runtime_id_env;
@@ -22,7 +27,7 @@ coco::stray start(saucer::application* app) {
22
27
  if (init_b64) {
23
28
  auto data = bldr::proto::Base64Decode(init_b64);
24
29
  if (!data.empty() && !bldr::proto::DecodeSaucerInit(data.data(), data.size(), saucer_init)) {
25
- std::cerr << "Failed to decode BLDR_SAUCER_INIT" << std::endl;
30
+ std::cerr << "[bldr-saucer] failed to decode BLDR_SAUCER_INIT" << std::endl;
26
31
  }
27
32
  }
28
33
 
@@ -30,7 +35,7 @@ coco::stray start(saucer::application* app) {
30
35
  bldr::PipeClient pipe;
31
36
  std::string pipe_path = ".pipe-" + runtime_id;
32
37
  if (!pipe.connect(pipe_path)) {
33
- std::cerr << "Failed to connect to pipe: " << pipe_path << std::endl;
38
+ std::cerr << "[bldr-saucer] failed to connect to pipe: " << pipe_path << std::endl;
34
39
  co_return;
35
40
  }
36
41
 
@@ -41,12 +46,12 @@ coco::stray start(saucer::application* app) {
41
46
  config.enable_keepalive = false;
42
47
  auto session = yamux::Session::Client(std::move(conn), config);
43
48
  if (!session) {
44
- std::cerr << "Failed to create yamux session" << std::endl;
49
+ std::cerr << "[bldr-saucer] failed to create yamux session" << std::endl;
45
50
  co_return;
46
51
  }
47
52
 
48
- // Create the scheme forwarder.
49
- bldr::SchemeForwarder forwarder(session.get());
53
+ // Create the scheme forwarder (shared_ptr to avoid use-after-free in detached threads).
54
+ auto forwarder = std::make_shared<bldr::SchemeForwarder>(session.get());
50
55
 
51
56
  // Register bldr:// scheme BEFORE creating the webview.
52
57
  saucer::webview::register_scheme("bldr");
@@ -58,15 +63,13 @@ coco::stray start(saucer::application* app) {
58
63
  window->set_size({1024, 768});
59
64
 
60
65
  // Handle bldr:// scheme: forward all requests to Go over yamux.
61
- webview->handle_stream_scheme("bldr", [&forwarder](saucer::scheme::request req, saucer::scheme::stream_writer writer) {
62
- // Forward in background thread to not block the scheme handler.
63
- std::thread([&forwarder, req = std::move(req), writer = std::move(writer)]() mutable {
64
- forwarder.forward(req, writer);
66
+ webview->handle_stream_scheme("bldr", [forwarder](saucer::scheme::request req, saucer::scheme::stream_writer writer) {
67
+ std::thread([forwarder, req = std::move(req), writer = std::move(writer)]() mutable {
68
+ forwarder->forward(req, writer);
65
69
  }).detach();
66
70
  });
67
71
 
68
- // Use set_html with JavaScript redirect to work around WebKit's
69
- // loadFileURL being called incorrectly for custom schemes.
72
+ // Navigate via HTML redirect (works around WebKit's loadFileURL issue with custom schemes).
70
73
  std::string nav_url = "bldr:///index.html";
71
74
  const char* doc_id_env = std::getenv("BLDR_WEB_DOCUMENT_ID");
72
75
  if (doc_id_env && doc_id_env[0] != '\0') {
@@ -82,18 +85,96 @@ coco::stray start(saucer::application* app) {
82
85
  webview->set_dev_tools(true);
83
86
  }
84
87
 
88
+ // Shutdown guard: prevents webview->execute() calls after webview destruction.
89
+ // The mutex ensures no thread is inside execute() when we set the flag.
90
+ auto webview_mtx = std::make_shared<std::mutex>();
91
+ auto webview_alive = std::make_shared<std::atomic<bool>>(true);
92
+
93
+ // Start accept loop for Go-initiated streams (debug eval).
94
+ // webview is a std::expected; use &(*webview) to get a pointer to the contained value.
95
+ auto* webview_ptr = &(*webview);
96
+ std::thread accept_thread([session, webview_ptr, webview_mtx, webview_alive]() {
97
+ while (true) {
98
+ auto [stream, err] = session->Accept();
99
+ if (err != yamux::Error::OK || !stream) {
100
+ break;
101
+ }
102
+
103
+ // Handle each stream in a detached thread so accept loop continues.
104
+ std::thread([stream, webview_ptr, webview_mtx, webview_alive]() {
105
+ // Read length-prefixed command frame.
106
+ uint8_t len_buf[4];
107
+ size_t total = 0;
108
+ while (total < 4) {
109
+ auto [n, rerr] = stream->Read(len_buf + total, 4 - total);
110
+ if (rerr != yamux::Error::OK || n == 0) {
111
+ stream->Close();
112
+ return;
113
+ }
114
+ total += n;
115
+ }
116
+ uint32_t msg_len;
117
+ std::memcpy(&msg_len, len_buf, 4);
118
+ if (msg_len > 10 * 1024 * 1024) {
119
+ stream->Close();
120
+ return;
121
+ }
122
+
123
+ std::vector<uint8_t> data(msg_len);
124
+ total = 0;
125
+ while (total < msg_len) {
126
+ auto [n, rerr] = stream->Read(data.data() + total, msg_len - total);
127
+ if (rerr != yamux::Error::OK || n == 0) break;
128
+ total += n;
129
+ }
130
+ if (total < msg_len) {
131
+ stream->Close();
132
+ return;
133
+ }
134
+
135
+ std::string code(data.begin(), data.end());
136
+
137
+ // Execute the JavaScript code in the webview (guarded against shutdown).
138
+ // Cast to webview* to call webview::execute(cstring_view) instead of
139
+ // smartview::execute(format_string) which has a consteval constructor
140
+ // that breaks std::thread lambdas in C++23.
141
+ {
142
+ std::lock_guard<std::mutex> lock(*webview_mtx);
143
+ if (webview_alive->load()) {
144
+ static_cast<saucer::webview*>(webview_ptr)->execute(code);
145
+ }
146
+ }
147
+
148
+ // Write a simple "ok" response.
149
+ std::string resp = "ok";
150
+ uint8_t resp_len_buf[4];
151
+ uint32_t resp_len = static_cast<uint32_t>(resp.size());
152
+ std::memcpy(resp_len_buf, &resp_len, 4);
153
+ stream->Write(resp_len_buf, 4);
154
+ stream->Write(reinterpret_cast<const uint8_t*>(resp.data()), resp.size());
155
+ stream->Close();
156
+ }).detach();
157
+ }
158
+ });
159
+ accept_thread.detach();
160
+
85
161
  window->show();
86
162
  co_await app->finish();
87
163
 
88
- // Cleanup.
164
+ // Shutdown: close session first (causes Accept/Read/Write to return errors,
165
+ // winding down detached threads), then mark webview as dead.
89
166
  session->Close();
167
+ {
168
+ std::lock_guard<std::mutex> lock(*webview_mtx);
169
+ webview_alive->store(false);
170
+ }
90
171
  pipe.close();
91
172
  }
92
173
 
93
174
  int main() {
94
175
  auto app_result = saucer::application::create({.id = "bldr"});
95
176
  if (!app_result) {
96
- std::cerr << "Failed to create application" << std::endl;
177
+ std::cerr << "[bldr-saucer] failed to create application" << std::endl;
97
178
  return 1;
98
179
  }
99
180
  return app_result->run(start);
@@ -20,7 +20,8 @@ PipeClient::~PipeClient() {
20
20
  }
21
21
 
22
22
  bool PipeClient::connect(const std::string& pipe_path) {
23
- std::lock_guard<std::mutex> lock(mutex_);
23
+ std::lock_guard<std::mutex> rlock(read_mtx_);
24
+ std::lock_guard<std::mutex> wlock(write_mtx_);
24
25
 
25
26
  #ifdef _WIN32
26
27
  // Windows named pipe connection
@@ -82,9 +83,22 @@ bool PipeClient::connect(const std::string& pipe_path) {
82
83
  }
83
84
 
84
85
  void PipeClient::close() {
85
- std::lock_guard<std::mutex> lock(mutex_);
86
+ // Set disconnected first so blocked reads/writes return.
86
87
  connected_ = false;
87
88
 
89
+ #ifndef _WIN32
90
+ // Shut down the socket to unblock any thread in a blocking read.
91
+ // This is safe to call without holding locks since shutdown on a
92
+ // valid fd is thread-safe and causes blocked read/write to return.
93
+ int fd = fd_;
94
+ if (fd >= 0) {
95
+ ::shutdown(fd, SHUT_RDWR);
96
+ }
97
+ #endif
98
+
99
+ std::lock_guard<std::mutex> rlock(read_mtx_);
100
+ std::lock_guard<std::mutex> wlock(write_mtx_);
101
+
88
102
  #ifdef _WIN32
89
103
  if (handle_ != INVALID_HANDLE_VALUE) {
90
104
  CloseHandle(handle_);
@@ -107,7 +121,7 @@ std::vector<uint8_t> PipeClient::read() {
107
121
  }
108
122
 
109
123
  std::vector<uint8_t> PipeClient::read_with_timeout(int timeout_ms) {
110
- std::lock_guard<std::mutex> lock(mutex_);
124
+ std::lock_guard<std::mutex> lock(read_mtx_);
111
125
  std::vector<uint8_t> result;
112
126
 
113
127
  if (!connected_) {
@@ -186,7 +200,7 @@ std::vector<uint8_t> PipeClient::read_with_timeout(int timeout_ms) {
186
200
  }
187
201
 
188
202
  bool PipeClient::write(const uint8_t* data, size_t length) {
189
- std::lock_guard<std::mutex> lock(mutex_);
203
+ std::lock_guard<std::mutex> lock(write_mtx_);
190
204
 
191
205
  if (!connected_ || data == nullptr || length == 0) {
192
206
  return false;
package/src/pipe_client.h CHANGED
@@ -64,7 +64,8 @@ private:
64
64
  int fd_ = -1;
65
65
  #endif
66
66
  std::atomic<bool> connected_{false};
67
- std::mutex mutex_;
67
+ std::mutex read_mtx_;
68
+ std::mutex write_mtx_;
68
69
  };
69
70
 
70
71
  } // namespace bldr
@@ -3,7 +3,6 @@
3
3
  #include <algorithm>
4
4
  #include <cctype>
5
5
  #include <cstring>
6
- #include <iostream>
7
6
 
8
7
  namespace bldr {
9
8
 
@@ -26,7 +25,6 @@ void SchemeForwarder::forward(const saucer::scheme::request& req,
26
25
  // Open a new yamux stream.
27
26
  auto [stream, err] = session_->OpenStream();
28
27
  if (err != yamux::Error::OK || !stream) {
29
- std::cerr << "[forwarder] failed to open yamux stream" << std::endl;
30
28
  sendError(writer, 502);
31
29
  return;
32
30
  }
@@ -48,7 +46,6 @@ void SchemeForwarder::forward(const saucer::scheme::request& req,
48
46
  // Serialize and send FetchRequestInfo frame.
49
47
  auto reqInfoMsg = proto::EncodeFetchRequest_Info(info);
50
48
  if (!writeFrame(stream.get(), reqInfoMsg)) {
51
- std::cerr << "[forwarder] failed to write request info" << std::endl;
52
49
  stream->Close();
53
50
  sendError(writer, 502);
54
51
  return;
@@ -65,7 +62,6 @@ void SchemeForwarder::forward(const saucer::scheme::request& req,
65
62
 
66
63
  auto reqDataMsg = proto::EncodeFetchRequest_Data(bodyData);
67
64
  if (!writeFrame(stream.get(), reqDataMsg)) {
68
- std::cerr << "[forwarder] failed to write request body" << std::endl;
69
65
  stream->Close();
70
66
  sendError(writer, 502);
71
67
  return;
@@ -87,7 +83,6 @@ void SchemeForwarder::forward(const saucer::scheme::request& req,
87
83
 
88
84
  proto::FetchResponse resp;
89
85
  if (!proto::DecodeFetchResponse(frame.data(), frame.size(), resp)) {
90
- std::cerr << "[forwarder] failed to decode response" << std::endl;
91
86
  if (!started) {
92
87
  sendError(writer, 502);
93
88
  }
@@ -133,7 +128,9 @@ void SchemeForwarder::forward(const saucer::scheme::request& req,
133
128
  }
134
129
  }
135
130
 
136
- writer.finish();
131
+ if (started) {
132
+ writer.finish();
133
+ }
137
134
  stream->Close();
138
135
  }
139
136