@aptre/bldr-saucer 0.2.5 → 0.2.6
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 +6 -6
- package/src/fetch_proto.cpp +27 -0
- package/src/fetch_proto.h +17 -0
- package/src/main.cpp +130 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aptre/bldr-saucer",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
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.
|
|
37
|
-
"@aptre/bldr-saucer-darwin-x64": "0.2.
|
|
38
|
-
"@aptre/bldr-saucer-linux-x64": "0.2.
|
|
39
|
-
"@aptre/bldr-saucer-linux-arm64": "0.2.
|
|
40
|
-
"@aptre/bldr-saucer-win32-x64": "0.2.
|
|
36
|
+
"@aptre/bldr-saucer-darwin-arm64": "0.2.6",
|
|
37
|
+
"@aptre/bldr-saucer-darwin-x64": "0.2.6",
|
|
38
|
+
"@aptre/bldr-saucer-linux-x64": "0.2.6",
|
|
39
|
+
"@aptre/bldr-saucer-linux-arm64": "0.2.6",
|
|
40
|
+
"@aptre/bldr-saucer-win32-x64": "0.2.6"
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"index.js",
|
package/src/fetch_proto.cpp
CHANGED
|
@@ -345,6 +345,33 @@ bool DecodeFetchResponse(const uint8_t* buf, size_t len, FetchResponse& out) {
|
|
|
345
345
|
return true;
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
+
bool DecodeEvalJSRequest(const uint8_t* buf, size_t len, EvalJSRequest& out) {
|
|
349
|
+
size_t offset = 0;
|
|
350
|
+
while (offset < len) {
|
|
351
|
+
uint32_t field;
|
|
352
|
+
uint8_t wire;
|
|
353
|
+
if (!decodeTag(buf, len, offset, field, wire)) return false;
|
|
354
|
+
switch (field) {
|
|
355
|
+
case 1: { // code
|
|
356
|
+
if (wire != kLengthDelimited) return false;
|
|
357
|
+
if (!decodeString(buf, len, offset, out.code)) return false;
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
default:
|
|
361
|
+
if (!skipField(buf, len, offset, wire)) return false;
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
std::vector<uint8_t> EncodeEvalJSResponse(const EvalJSResponse& resp) {
|
|
369
|
+
std::vector<uint8_t> buf;
|
|
370
|
+
encodeString(buf, 1, resp.result);
|
|
371
|
+
encodeString(buf, 2, resp.error);
|
|
372
|
+
return buf;
|
|
373
|
+
}
|
|
374
|
+
|
|
348
375
|
bool DecodeSaucerInit(const uint8_t* buf, size_t len, SaucerInit& out) {
|
|
349
376
|
size_t offset = 0;
|
|
350
377
|
while (offset < len) {
|
package/src/fetch_proto.h
CHANGED
|
@@ -57,6 +57,23 @@ struct FetchResponse {
|
|
|
57
57
|
ResponseData data;
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
+
// EvalJSRequest corresponds to saucer.EvalJSRequest.
|
|
61
|
+
struct EvalJSRequest {
|
|
62
|
+
std::string code; // field 1
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// EvalJSResponse corresponds to saucer.EvalJSResponse.
|
|
66
|
+
struct EvalJSResponse {
|
|
67
|
+
std::string result; // field 1
|
|
68
|
+
std::string error; // field 2
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// DecodeEvalJSRequest decodes an EvalJSRequest protobuf message.
|
|
72
|
+
bool DecodeEvalJSRequest(const uint8_t* buf, size_t len, EvalJSRequest& out);
|
|
73
|
+
|
|
74
|
+
// EncodeEvalJSResponse encodes an EvalJSResponse protobuf message.
|
|
75
|
+
std::vector<uint8_t> EncodeEvalJSResponse(const EvalJSResponse& resp);
|
|
76
|
+
|
|
60
77
|
// EncodeFetchRequest_Info serializes a FetchRequest with request_info (field 1).
|
|
61
78
|
std::vector<uint8_t> EncodeFetchRequest_Info(const FetchRequestInfo& info);
|
|
62
79
|
|
package/src/main.cpp
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
#include "scheme_forwarder.h"
|
|
6
6
|
|
|
7
7
|
#include <atomic>
|
|
8
|
+
#include <condition_variable>
|
|
8
9
|
#include <cstdlib>
|
|
9
10
|
#include <cstring>
|
|
10
11
|
#include <iostream>
|
|
@@ -12,8 +13,64 @@
|
|
|
12
13
|
#include <mutex>
|
|
13
14
|
#include <string>
|
|
14
15
|
#include <thread>
|
|
16
|
+
#include <unordered_map>
|
|
15
17
|
#include <vector>
|
|
16
18
|
|
|
19
|
+
// EvalRegistry tracks pending eval requests and their results.
|
|
20
|
+
// Worker threads register a request ID, execute JS that posts results via
|
|
21
|
+
// the saucer message channel, then wait on a condition variable for the
|
|
22
|
+
// message handler to deliver the result.
|
|
23
|
+
struct EvalRegistry {
|
|
24
|
+
struct Pending {
|
|
25
|
+
bool ready = false;
|
|
26
|
+
std::string result;
|
|
27
|
+
std::string error;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
std::mutex mtx;
|
|
31
|
+
std::condition_variable cv;
|
|
32
|
+
std::unordered_map<std::string, Pending> pending;
|
|
33
|
+
|
|
34
|
+
// Register registers a new eval request and returns the ID.
|
|
35
|
+
void Register(const std::string& id) {
|
|
36
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
37
|
+
pending[id] = Pending{};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Deliver delivers a result for a pending eval request.
|
|
41
|
+
// Returns true if the ID was found.
|
|
42
|
+
bool Deliver(const std::string& id, const std::string& result, const std::string& error) {
|
|
43
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
44
|
+
auto it = pending.find(id);
|
|
45
|
+
if (it == pending.end()) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
it->second.ready = true;
|
|
49
|
+
it->second.result = result;
|
|
50
|
+
it->second.error = error;
|
|
51
|
+
cv.notify_all();
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Wait waits for a result for the given eval ID (up to timeout_ms).
|
|
56
|
+
// Returns the response, with empty fields on timeout.
|
|
57
|
+
bldr::proto::EvalJSResponse Wait(const std::string& id, int timeout_ms) {
|
|
58
|
+
std::unique_lock<std::mutex> lock(mtx);
|
|
59
|
+
cv.wait_for(lock, std::chrono::milliseconds(timeout_ms), [this, &id] {
|
|
60
|
+
auto it = pending.find(id);
|
|
61
|
+
return it != pending.end() && it->second.ready;
|
|
62
|
+
});
|
|
63
|
+
bldr::proto::EvalJSResponse resp;
|
|
64
|
+
auto it = pending.find(id);
|
|
65
|
+
if (it != pending.end()) {
|
|
66
|
+
resp.result = std::move(it->second.result);
|
|
67
|
+
resp.error = std::move(it->second.error);
|
|
68
|
+
pending.erase(it);
|
|
69
|
+
}
|
|
70
|
+
return resp;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
17
74
|
coco::stray start(saucer::application* app) {
|
|
18
75
|
const char* runtime_id_env = std::getenv("BLDR_RUNTIME_ID");
|
|
19
76
|
if (!runtime_id_env) {
|
|
@@ -90,10 +147,49 @@ coco::stray start(saucer::application* app) {
|
|
|
90
147
|
auto webview_mtx = std::make_shared<std::mutex>();
|
|
91
148
|
auto webview_alive = std::make_shared<std::atomic<bool>>(true);
|
|
92
149
|
|
|
150
|
+
// Eval result registry: worker threads register pending evals, the message
|
|
151
|
+
// handler delivers results from JavaScript back to the waiting thread.
|
|
152
|
+
auto eval_registry = std::make_shared<EvalRegistry>();
|
|
153
|
+
|
|
154
|
+
// Register a message handler to intercept eval results from JavaScript.
|
|
155
|
+
// The Go side wraps JS code so it posts the result via postMessage with a
|
|
156
|
+
// prefix format: __bldr_eval:<eval_id>:r:<result> or __bldr_eval:<eval_id>:e:<error>.
|
|
157
|
+
// The smartview's own handler returns unhandled for unrecognized messages,
|
|
158
|
+
// so this handler sees them next.
|
|
159
|
+
constexpr std::string_view eval_prefix = "__bldr_eval:";
|
|
160
|
+
webview->on<saucer::webview::event::message>({{.func = [eval_registry, eval_prefix](std::string_view message) -> saucer::status {
|
|
161
|
+
if (!message.starts_with(eval_prefix)) {
|
|
162
|
+
return saucer::status::unhandled;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Parse prefix format: __bldr_eval:<eval_id>:<type>:<data>
|
|
166
|
+
auto rest = message.substr(eval_prefix.size());
|
|
167
|
+
auto sep1 = rest.find(':');
|
|
168
|
+
if (sep1 == std::string_view::npos || sep1 + 2 >= rest.size()) {
|
|
169
|
+
return saucer::status::unhandled;
|
|
170
|
+
}
|
|
171
|
+
auto sep2 = rest.find(':', sep1 + 1);
|
|
172
|
+
if (sep2 == std::string_view::npos) {
|
|
173
|
+
return saucer::status::unhandled;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
std::string eval_id(rest.substr(0, sep1));
|
|
177
|
+
char type = rest[sep1 + 1];
|
|
178
|
+
std::string data(rest.substr(sep2 + 1));
|
|
179
|
+
|
|
180
|
+
if (type == 'r') {
|
|
181
|
+
eval_registry->Deliver(eval_id, data, "");
|
|
182
|
+
} else {
|
|
183
|
+
eval_registry->Deliver(eval_id, "", data);
|
|
184
|
+
}
|
|
185
|
+
return saucer::status::handled;
|
|
186
|
+
}}});
|
|
187
|
+
|
|
93
188
|
// Start accept loop for Go-initiated streams (debug eval).
|
|
94
189
|
// webview is a std::expected; use &(*webview) to get a pointer to the contained value.
|
|
95
190
|
auto* webview_ptr = &(*webview);
|
|
96
|
-
|
|
191
|
+
auto eval_counter = std::make_shared<std::atomic<uint64_t>>(0);
|
|
192
|
+
std::thread accept_thread([session, webview_ptr, webview_mtx, webview_alive, eval_registry, eval_counter]() {
|
|
97
193
|
while (true) {
|
|
98
194
|
auto [stream, err] = session->Accept();
|
|
99
195
|
if (err != yamux::Error::OK || !stream) {
|
|
@@ -101,7 +197,7 @@ coco::stray start(saucer::application* app) {
|
|
|
101
197
|
}
|
|
102
198
|
|
|
103
199
|
// Handle each stream in a detached thread so accept loop continues.
|
|
104
|
-
std::thread([stream, webview_ptr, webview_mtx, webview_alive]() {
|
|
200
|
+
std::thread([stream, webview_ptr, webview_mtx, webview_alive, eval_registry, eval_counter]() {
|
|
105
201
|
// Read length-prefixed command frame.
|
|
106
202
|
uint8_t len_buf[4];
|
|
107
203
|
size_t total = 0;
|
|
@@ -132,7 +228,28 @@ coco::stray start(saucer::application* app) {
|
|
|
132
228
|
return;
|
|
133
229
|
}
|
|
134
230
|
|
|
135
|
-
|
|
231
|
+
// Decode the EvalJSRequest protobuf from Go.
|
|
232
|
+
bldr::proto::EvalJSRequest req;
|
|
233
|
+
if (!bldr::proto::DecodeEvalJSRequest(data.data(), data.size(), req)) {
|
|
234
|
+
stream->Close();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// The code from Go is already wrapped in an async IIFE that posts
|
|
239
|
+
// the result via postMessage. It contains a placeholder __EVAL_ID__
|
|
240
|
+
// that we replace with a unique ID for result correlation.
|
|
241
|
+
std::string code = std::move(req.code);
|
|
242
|
+
std::string eval_id = "e" + std::to_string(eval_counter->fetch_add(1));
|
|
243
|
+
|
|
244
|
+
// Replace the __EVAL_ID__ placeholder with the actual eval ID.
|
|
245
|
+
const std::string placeholder = "__EVAL_ID__";
|
|
246
|
+
auto pos = code.find(placeholder);
|
|
247
|
+
if (pos != std::string::npos) {
|
|
248
|
+
code.replace(pos, placeholder.size(), eval_id);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Register the eval request before executing the code.
|
|
252
|
+
eval_registry->Register(eval_id);
|
|
136
253
|
|
|
137
254
|
// Execute the JavaScript code in the webview (guarded against shutdown).
|
|
138
255
|
// Cast to webview* to call webview::execute(cstring_view) instead of
|
|
@@ -145,13 +262,19 @@ coco::stray start(saucer::application* app) {
|
|
|
145
262
|
}
|
|
146
263
|
}
|
|
147
264
|
|
|
148
|
-
//
|
|
149
|
-
|
|
265
|
+
// Wait for the JavaScript result (30 second timeout).
|
|
266
|
+
auto resp = eval_registry->Wait(eval_id, 30000);
|
|
267
|
+
if (resp.result.empty() && resp.error.empty()) {
|
|
268
|
+
resp.error = "eval timeout";
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Encode the EvalJSResponse protobuf and send it back.
|
|
272
|
+
auto resp_buf = bldr::proto::EncodeEvalJSResponse(resp);
|
|
150
273
|
uint8_t resp_len_buf[4];
|
|
151
|
-
uint32_t resp_len = static_cast<uint32_t>(
|
|
274
|
+
uint32_t resp_len = static_cast<uint32_t>(resp_buf.size());
|
|
152
275
|
std::memcpy(resp_len_buf, &resp_len, 4);
|
|
153
276
|
stream->Write(resp_len_buf, 4);
|
|
154
|
-
stream->Write(
|
|
277
|
+
stream->Write(resp_buf.data(), resp_buf.size());
|
|
155
278
|
stream->Close();
|
|
156
279
|
}).detach();
|
|
157
280
|
}
|