@geastack/host 0.1.15 → 0.1.17
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/README.md +6 -0
- package/host/websocket.cpp +7 -5
- package/include/gea/embedded-host.h +1 -0
- package/include/host/uart.h +69 -0
- package/include/host/websocket.h +43 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,12 @@ headers it cannot satisfy with the portable sources.
|
|
|
20
20
|
|
|
21
21
|
There is no JavaScript entry point; the package exists to be compiled.
|
|
22
22
|
|
|
23
|
+
Version 0.1.16 adds native ESP32 WebSocket connection headers, paired with the
|
|
24
|
+
public constructor options in `@geastack/core` 0.1.41. Headers are applied to both
|
|
25
|
+
the ESP-IDF WebSocket client and its owned transport, so bearer authentication
|
|
26
|
+
works on the actual HTTP upgrade request. Existing URL and subprotocol-only
|
|
27
|
+
connections retain their defaults.
|
|
28
|
+
|
|
23
29
|
## License
|
|
24
30
|
|
|
25
31
|
Apache-2.0. See `LICENSE`.
|
package/host/websocket.cpp
CHANGED
|
@@ -97,7 +97,7 @@ std::unordered_map<NativeWebSocketHandle, CallbackTable> &callbackTable() {
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
#if defined(ESP_PLATFORM) && !defined(GEA_EMBEDDED_WIFI_DISABLED)
|
|
100
|
-
void platform_open(NativeWebSocketHandle, const std::string &, const std::string &);
|
|
100
|
+
void platform_open(NativeWebSocketHandle, const std::string &, const std::string &, const std::string &);
|
|
101
101
|
std::size_t platform_buffered(NativeWebSocketHandle);
|
|
102
102
|
void platform_send(NativeWebSocketHandle, const std::string &);
|
|
103
103
|
void platform_close(NativeWebSocketHandle);
|
|
@@ -105,7 +105,7 @@ void platform_destroy(NativeWebSocketHandle);
|
|
|
105
105
|
void platform_reap();
|
|
106
106
|
void platform_set_producer(NativeWebSocketHandle, TextProducer);
|
|
107
107
|
#else
|
|
108
|
-
__attribute__((weak)) void platform_open(NativeWebSocketHandle, const std::string &, const std::string &) {}
|
|
108
|
+
__attribute__((weak)) void platform_open(NativeWebSocketHandle, const std::string &, const std::string &, const std::string &) {}
|
|
109
109
|
__attribute__((weak)) std::size_t platform_buffered(NativeWebSocketHandle) { return 0; }
|
|
110
110
|
__attribute__((weak)) void platform_send(NativeWebSocketHandle, const std::string &) {}
|
|
111
111
|
__attribute__((weak)) void platform_close(NativeWebSocketHandle) {}
|
|
@@ -128,7 +128,7 @@ void setTextConsumer(NativeWebSocketHandle handle, TextConsumer consumer) {
|
|
|
128
128
|
it->second.consumer = std::move(consumer);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols) {
|
|
131
|
+
NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols, const std::string &headers) {
|
|
132
132
|
auto handle = nextHandle()++;
|
|
133
133
|
{
|
|
134
134
|
std::lock_guard<std::mutex> lock(instancesMutex());
|
|
@@ -138,7 +138,7 @@ NativeWebSocketHandle create_handle(const std::string &url, const std::string &p
|
|
|
138
138
|
(void)callbackTable();
|
|
139
139
|
{ std::lock_guard lock(ownerMutex); owners[handle] = owner; }
|
|
140
140
|
if (!owner->isMain()) owner->addCleanup([handle] { destroy_handle(handle); });
|
|
141
|
-
platform_open(handle, url, protocols);
|
|
141
|
+
platform_open(handle, url, protocols, headers);
|
|
142
142
|
return handle;
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -679,11 +679,12 @@ void sendWorker(void *arg) {
|
|
|
679
679
|
// The scope below is provided by the task trampoline in platform_open.
|
|
680
680
|
}
|
|
681
681
|
|
|
682
|
-
void platform_open(NativeWebSocketHandle handle, const std::string &url, const std::string &protocols) {
|
|
682
|
+
void platform_open(NativeWebSocketHandle handle, const std::string &url, const std::string &protocols, const std::string &headers) {
|
|
683
683
|
std::lock_guard platformLock(platformMutex);
|
|
684
684
|
esp_websocket_client_config_t config = {};
|
|
685
685
|
config.uri = url.c_str();
|
|
686
686
|
config.subprotocol = protocols.empty() ? nullptr : protocols.c_str();
|
|
687
|
+
config.headers = headers.empty() ? nullptr : headers.c_str();
|
|
687
688
|
config.task_stack = 8192;
|
|
688
689
|
config.buffer_size = 4096;
|
|
689
690
|
config.disable_auto_reconnect = true;
|
|
@@ -727,6 +728,7 @@ void platform_open(NativeWebSocketHandle handle, const std::string &url, const s
|
|
|
727
728
|
esp_transport_ws_config_t transportConfig{};
|
|
728
729
|
transportConfig.ws_path = path.c_str();
|
|
729
730
|
transportConfig.sub_protocol = config.subprotocol;
|
|
731
|
+
transportConfig.headers = config.headers;
|
|
730
732
|
transportConfig.propagate_control_frames = true;
|
|
731
733
|
if (esp_transport_ws_set_config(state->transport, &transportConfig) != ESP_OK) {
|
|
732
734
|
enqueue(PendingEvent{handle, EventKind::Error, "WebSocket transport configuration failed", 0});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "platform/uart.h"
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <limits>
|
|
6
|
+
#include <type_traits>
|
|
7
|
+
|
|
8
|
+
namespace gea::host {
|
|
9
|
+
struct UartFacade {
|
|
10
|
+
static int integer(double value) {
|
|
11
|
+
return std::isfinite(value) && value >= 0 &&
|
|
12
|
+
value <= std::numeric_limits<int>::max() &&
|
|
13
|
+
std::trunc(value) == value
|
|
14
|
+
? static_cast<int>(value)
|
|
15
|
+
: -1;
|
|
16
|
+
}
|
|
17
|
+
bool open(double port, double tx, double rx, double baud, double rxBytes,
|
|
18
|
+
double txBytes) const {
|
|
19
|
+
return gea::platform::uart::Uart::open(
|
|
20
|
+
integer(port), {integer(tx), integer(rx), integer(baud),
|
|
21
|
+
integer(rxBytes), integer(txBytes)});
|
|
22
|
+
}
|
|
23
|
+
bool isOpen(double port) const {
|
|
24
|
+
return gea::platform::uart::Uart::isOpen(integer(port));
|
|
25
|
+
}
|
|
26
|
+
void close(double port) const {
|
|
27
|
+
gea::platform::uart::Uart::close(integer(port));
|
|
28
|
+
}
|
|
29
|
+
template <typename Bytes> double read(double port, Bytes &&bytes) const {
|
|
30
|
+
static_assert(std::is_same_v<std::remove_cvref_t<decltype(*bytes.data())>,
|
|
31
|
+
std::uint8_t>);
|
|
32
|
+
if constexpr (requires {
|
|
33
|
+
bytes.buffer();
|
|
34
|
+
bytes.byteOffset();
|
|
35
|
+
}) {
|
|
36
|
+
// The compiler passes a const TypedArray VIEW. Its ArrayBuffer remains
|
|
37
|
+
// mutable: receive into that shared backing store, respecting subarrays.
|
|
38
|
+
// data() preserves the runtime's detached-buffer check before accessing
|
|
39
|
+
// it.
|
|
40
|
+
(void)bytes.data();
|
|
41
|
+
const auto count = bytes.size();
|
|
42
|
+
auto *destination = count
|
|
43
|
+
? bytes.buffer()->data() +
|
|
44
|
+
static_cast<std::size_t>(bytes.byteOffset())
|
|
45
|
+
: nullptr;
|
|
46
|
+
return gea::platform::uart::Uart::read(integer(port), destination, count);
|
|
47
|
+
} else {
|
|
48
|
+
static_assert(
|
|
49
|
+
!std::is_const_v<std::remove_reference_t<decltype(*bytes.data())>>);
|
|
50
|
+
return gea::platform::uart::Uart::read(integer(port), bytes.data(),
|
|
51
|
+
bytes.size());
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
template <typename Bytes>
|
|
55
|
+
double write(double port, const Bytes &bytes) const {
|
|
56
|
+
static_assert(std::is_same_v<std::remove_cvref_t<decltype(*bytes.data())>,
|
|
57
|
+
std::uint8_t>);
|
|
58
|
+
return gea::platform::uart::Uart::write(integer(port), bytes.data(),
|
|
59
|
+
bytes.size());
|
|
60
|
+
}
|
|
61
|
+
double available(double port) const {
|
|
62
|
+
return gea::platform::uart::Uart::available(integer(port));
|
|
63
|
+
}
|
|
64
|
+
double writable(double port) const {
|
|
65
|
+
return gea::platform::uart::Uart::writable(integer(port));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
inline constexpr UartFacade Uart{};
|
|
69
|
+
} // namespace gea::host
|
package/include/host/websocket.h
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
#include <cstdint>
|
|
5
5
|
#include <functional>
|
|
6
6
|
#include <memory>
|
|
7
|
+
#include <stdexcept>
|
|
7
8
|
#include <string>
|
|
9
|
+
#include <type_traits>
|
|
8
10
|
#include <unordered_map>
|
|
9
11
|
#include <utility>
|
|
10
12
|
#include <vector>
|
|
@@ -95,16 +97,54 @@ struct CallbackTable {
|
|
|
95
97
|
|
|
96
98
|
std::unordered_map<NativeWebSocketHandle, CallbackTable> &callbackTable();
|
|
97
99
|
|
|
98
|
-
NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols = {}
|
|
100
|
+
NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols = {},
|
|
101
|
+
const std::string &headers = {});
|
|
102
|
+
// Typed records and arrays may be native values or identity-preserving Refs.
|
|
103
|
+
template <typename T>
|
|
104
|
+
decltype(auto) connectionValue(const T &value) {
|
|
105
|
+
if constexpr (requires { value.operator->(); }) return *value;
|
|
106
|
+
else return (value);
|
|
107
|
+
}
|
|
108
|
+
template <typename Options>
|
|
109
|
+
NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols,
|
|
110
|
+
const Options &options) {
|
|
111
|
+
std::string headers;
|
|
112
|
+
const auto &entries = connectionValue(connectionValue(options).headers);
|
|
113
|
+
for (std::size_t i = 0; i < entries.size(); ++i) {
|
|
114
|
+
const auto &header = connectionValue(entries.at(i));
|
|
115
|
+
if (header.name.empty() || header.name.find_first_not_of(
|
|
116
|
+
"!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") != std::string::npos ||
|
|
117
|
+
header.value.find_first_of("\r\n") != std::string::npos ||
|
|
118
|
+
header.value.find('\0') != std::string::npos) {
|
|
119
|
+
throw std::invalid_argument("Invalid WebSocket connection header");
|
|
120
|
+
}
|
|
121
|
+
headers += header.name + ": " + header.value + "\r\n";
|
|
122
|
+
}
|
|
123
|
+
return create_handle(url, protocols, headers);
|
|
124
|
+
}
|
|
99
125
|
template <typename Protocols>
|
|
126
|
+
requires (!std::is_convertible_v<Protocols, std::string>)
|
|
100
127
|
NativeWebSocketHandle create_handle(const std::string &url, const Protocols &protocols) {
|
|
101
128
|
std::string joined;
|
|
102
|
-
|
|
129
|
+
const auto &entries = connectionValue(protocols);
|
|
130
|
+
for (std::size_t i = 0; i < entries.size(); ++i) {
|
|
103
131
|
if (i) joined += ",";
|
|
104
|
-
joined +=
|
|
132
|
+
joined += entries.at(i);
|
|
105
133
|
}
|
|
106
134
|
return create_handle(url, joined);
|
|
107
135
|
}
|
|
136
|
+
template <typename Protocols, typename Options>
|
|
137
|
+
requires (!std::is_convertible_v<Protocols, std::string>)
|
|
138
|
+
NativeWebSocketHandle create_handle(const std::string &url, const Protocols &protocols,
|
|
139
|
+
const Options &options) {
|
|
140
|
+
std::string joined;
|
|
141
|
+
const auto &entries = connectionValue(protocols);
|
|
142
|
+
for (std::size_t i = 0; i < entries.size(); ++i) {
|
|
143
|
+
if (i) joined += ",";
|
|
144
|
+
joined += entries.at(i);
|
|
145
|
+
}
|
|
146
|
+
return create_handle(url, joined, options);
|
|
147
|
+
}
|
|
108
148
|
void destroy_handle(NativeWebSocketHandle handle);
|
|
109
149
|
|
|
110
150
|
// Native streaming sources run on the socket's sender, never on the UI loop.
|