Skip to content

Commit dc01f89

Browse files
authored
Merge pull request #111 from kinode-dao/develop
0.9.7
2 parents 11daa54 + d09c01f commit dc01f89

28 files changed

+326
-192
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kinode_process_lib"
33
description = "A library for writing Kinode processes in Rust."
4-
version = "0.9.6"
4+
version = "0.9.7"
55
edition = "2021"
66
license-file = "LICENSE"
77
homepage = "https://kinode.org"

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# `kinode_process_lib`
22

33
Library of functions for more ergonomic [Kinode](https://github.com/kinode-dao/kinode) Rust process development.
4-
Documentation can be found [here](https://docs.rs/kinode_process_lib).
4+
5+
[Documentation can be found here](https://docs.rs/kinode_process_lib).
6+
7+
[Crate can be found here](https://crates.io/crates/kinode_process_lib).
8+
59
See the [Kinode Book](https://book.kinode.org) for a guide on how to use this library to write Kinode apps in Rust.
610

711
The major version of `kinode_process_lib` will always match the major version of Kinode OS.

src/eth.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum EthAction {
3434
},
3535
}
3636

37-
/// Incoming `Request` containing subscription updates or errors that processes will receive.
37+
/// Incoming [`crate::Request`] containing subscription updates or errors that processes will receive.
3838
/// Can deserialize all incoming requests from eth:distro:sys to this type.
3939
///
4040
/// Will be serialized and deserialized using `serde_json::to_vec` and `serde_json::from_slice`.
@@ -54,7 +54,7 @@ pub struct EthSubError {
5454
pub error: String,
5555
}
5656

57-
/// The Response type which a process will get from requesting with an [`EthAction`] will be
57+
/// The [`crate::Response`] type which a process will get from requesting with an [`EthAction`] will be
5858
/// of this type, serialized and deserialized using `serde_json::to_vec`
5959
/// and `serde_json::from_slice`.
6060
///
@@ -90,7 +90,7 @@ pub enum EthError {
9090
}
9191

9292
/// The action type used for configuring eth:distro:sys. Only processes which have the "root"
93-
/// capability from eth:distro:sys can successfully send this action.
93+
/// [`crate::Capability`] from eth:distro:sys can successfully send this action.
9494
#[derive(Debug, Serialize, Deserialize)]
9595
pub enum EthConfigAction {
9696
/// Add a new provider to the list of providers.
@@ -126,12 +126,12 @@ pub enum EthConfigAction {
126126
pub enum EthConfigResponse {
127127
Ok,
128128
/// Response from a GetProviders request.
129-
/// Note the [`crate::kernel_types::KnsUpdate`] will only have the correct `name` field.
129+
/// Note the [`crate::net::KnsUpdate`] will only have the correct `name` field.
130130
/// The rest of the Update is not saved in this module.
131131
Providers(SavedConfigs),
132132
/// Response from a GetAccessSettings request.
133133
AccessSettings(AccessSettings),
134-
/// Permission denied due to missing capability
134+
/// Permission denied due to missing [`crate::Capability`]
135135
PermissionDenied,
136136
/// Response from a GetState request
137137
State {
@@ -194,11 +194,11 @@ impl Provider {
194194
request_timeout,
195195
}
196196
}
197-
/// Sends a request based on the specified `EthAction` and parses the response.
197+
/// Sends a request based on the specified [`EthAction`] and parses the response.
198198
///
199-
/// This function constructs a request targeting the Ethereum distribution system, serializes the provided `EthAction`,
199+
/// This function constructs a request targeting the Ethereum distribution system, serializes the provided [`EthAction`],
200200
/// and sends it. It awaits a response with a specified timeout, then attempts to parse the response into the expected
201-
/// type `T`. This method is generic and can be used for various Ethereum actions by specifying the appropriate `EthAction`
201+
/// type `T`. This method is generic and can be used for various Ethereum actions by specifying the appropriate [`EthAction`]
202202
/// and return type `T`.
203203
pub fn send_request_and_parse_response<T: serde::de::DeserializeOwned>(
204204
&self,

src/homepage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::Request;
22

33
/// Add a new icon and/or widget to the Kinode homepage. Note that the process calling this
4-
/// function must have the `homepage:homepage:sys` messaging capability.
4+
/// function must have the `homepage:homepage:sys` messaging [`crate::Capability`].
55
///
66
/// This should be called upon process startup to ensure that the process is added to the homepage.
77
///
@@ -30,7 +30,7 @@ pub fn add_to_homepage(label: &str, icon: Option<&str>, path: Option<&str>, widg
3030
}
3131

3232
/// Remove the caller process from the Kinode homepage. Note that the process calling this function
33-
/// must have the `homepage:homepage:sys` messaging capability.
33+
/// must have the `homepage:homepage:sys` messaging [`crate::Capability`].
3434
///
3535
/// This usually isn't necessary as processes are not persisted on homepage between boots.
3636
pub fn remove_from_homepage() {

src/http/client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::collections::HashMap;
66
use std::str::FromStr;
77
use thiserror::Error;
88

9-
/// Request type sent to the `http_client:distro:sys` service in order to open a
9+
/// [`crate::Request`] type sent to the `http_client:distro:sys` service in order to open a
1010
/// WebSocket connection, send a WebSocket message on an existing connection, or
1111
/// send an HTTP request.
1212
///
13-
/// You will receive a Response with the format `Result<HttpClientResponse, HttpClientError>`.
13+
/// You will receive a [`crate::Response`] with the format `Result<HttpClientResponse, HttpClientError>`.
1414
///
1515
/// Always serialized/deserialized as JSON.
1616
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -47,7 +47,7 @@ pub struct OutgoingHttpRequest {
4747
pub headers: HashMap<String, String>,
4848
}
4949

50-
/// Request that comes from an open WebSocket client connection in the
50+
/// [`crate::Request`] that comes from an open WebSocket client connection in the
5151
/// `http_client:distro:sys` service. Be prepared to receive these after
5252
/// using a [`HttpClientAction::WebSocketOpen`] to open a connection.
5353
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
@@ -61,7 +61,7 @@ pub enum HttpClientRequest {
6161
},
6262
}
6363

64-
/// Response type received from the `http_client:distro:sys` service after
64+
/// [`crate::Response`] type received from the `http_client:distro:sys` service after
6565
/// sending a successful [`HttpClientAction`] to it.
6666
#[derive(Debug, Serialize, Deserialize)]
6767
pub enum HttpClientResponse {
@@ -125,7 +125,7 @@ pub fn send_request(
125125

126126
/// Make an HTTP request using http_client and await its response.
127127
///
128-
/// Returns [`Response`] from the `http` crate if successful, with the body type as bytes.
128+
/// Returns HTTP response from the `http` crate if successful, with the body type as bytes.
129129
pub fn send_request_await_response(
130130
method: Method,
131131
url: url::Url,

src/http/server.rs

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ use crate::{
33
get_blob, Address, LazyLoadBlob as KiBlob, Message, Request as KiRequest,
44
Response as KiResponse,
55
};
6-
use http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
6+
pub use http::StatusCode;
7+
use http::{HeaderMap, HeaderName, HeaderValue};
78
use serde::{Deserialize, Serialize};
89
use std::collections::{HashMap, HashSet};
910
use thiserror::Error;
1011

11-
/// HTTP Request received from the `http_server:distro:sys` service as a
12+
/// [`crate::Request`] received from the `http_server:distro:sys` service as a
1213
/// result of either an HTTP or WebSocket binding, created via [`HttpServerAction`].
1314
#[derive(Clone, Debug, Serialize, Deserialize)]
1415
pub enum HttpServerRequest {
1516
Http(IncomingHttpRequest),
1617
/// Processes will receive this kind of request when a client connects to them.
17-
/// If a process does not want this websocket open, they should issue a *request*
18+
/// If a process does not want this websocket open, they should issue a [`crate::Request`]
1819
/// containing a [`HttpServerAction::WebSocketClose`] message and this channel ID.
1920
WebSocketOpen {
2021
path: String,
2122
channel_id: u32,
2223
},
23-
/// Processes can both SEND and RECEIVE this kind of request
24+
/// Processes can both SEND and RECEIVE this kind of [`crate::Request`]
2425
/// (send as [`HttpServerAction::WebSocketPush`]).
25-
/// When received, will contain the message bytes as lazy_load_blob.
26+
/// When received, will contain the message bytes as [`crate::LazyLoadBlob`].
2627
WebSocketPush {
2728
channel_id: u32,
2829
message_type: WsMessageType,
@@ -33,7 +34,7 @@ pub enum HttpServerRequest {
3334
}
3435

3536
impl HttpServerRequest {
36-
/// Parse a byte slice into an HttpServerRequest.
37+
/// Parse a byte slice into an [`HttpServerRequest`].
3738
pub fn from_bytes(bytes: &[u8]) -> serde_json::Result<Self> {
3839
serde_json::from_slice(bytes)
3940
}
@@ -132,9 +133,9 @@ impl IncomingHttpRequest {
132133

133134
/// The possible message types for [`HttpServerRequest::WebSocketPush`].
134135
/// Ping and Pong are limited to 125 bytes by the WebSockets protocol.
135-
/// Text will be sent as a Text frame, with the lazy_load_blob bytes
136+
/// Text will be sent as a Text frame, with the [`crate::LazyLoadBlob`] bytes
136137
/// being the UTF-8 encoding of the string. Binary will be sent as a
137-
/// Binary frame containing the unmodified lazy_load_blob bytes.
138+
/// Binary frame containing the unmodified [`crate::LazyLoadBlob`] bytes.
138139
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
139140
pub enum WsMessageType {
140141
Text,
@@ -144,26 +145,26 @@ pub enum WsMessageType {
144145
Close,
145146
}
146147

147-
/// Request type sent to `http_server:distro:sys` in order to configure it.
148+
/// [`crate::Request`] type sent to `http_server:distro:sys` in order to configure it.
148149
///
149-
/// If a response is expected, all actions will return a Response
150+
/// If a [`crate::Response`] is expected, all actions will return a [`crate::Response`]
150151
/// with the shape `Result<(), HttpServerActionError>` serialized to JSON.
151152
#[derive(Clone, Debug, Serialize, Deserialize)]
152153
pub enum HttpServerAction {
153-
/// Bind expects a lazy_load_blob if and only if `cache` is TRUE. The lazy_load_blob should
154-
/// be the static file to serve at this path.
154+
/// Bind expects a [`crate::LazyLoadBlob`] if and only if `cache` is TRUE.
155+
/// The [`crate::LazyLoadBlob`] should be the static file to serve at this path.
155156
Bind {
156157
path: String,
157158
/// Set whether the HTTP request needs a valid login cookie, AKA, whether
158159
/// the user needs to be logged in to access this path.
159160
authenticated: bool,
160-
/// Set whether requests can be fielded from anywhere, or only the loopback address.
161+
/// Set whether [`crate::Request`]s can be fielded from anywhere, or only the loopback address.
161162
local_only: bool,
162-
/// Set whether to bind the lazy_load_blob statically to this path. That is, take the
163-
/// lazy_load_blob bytes and serve them as the response to any request to this path.
163+
/// Set whether to bind the [`crate::LazyLoadBlob`] statically to this path. That is, take the
164+
/// [`crate::LazyLoadBlob`] bytes and serve them as the response to any request to this path.
164165
cache: bool,
165166
},
166-
/// SecureBind expects a lazy_load_blob if and only if `cache` is TRUE. The lazy_load_blob should
167+
/// SecureBind expects a [`crate::LazyLoadBlob`] if and only if `cache` is TRUE. The [`crate::LazyLoadBlob`] should
167168
/// be the static file to serve at this path.
168169
///
169170
/// SecureBind is the same as Bind, except that it forces requests to be made from
@@ -174,8 +175,8 @@ pub enum HttpServerAction {
174175
/// will require the user to be logged in separately to the general domain authentication.
175176
SecureBind {
176177
path: String,
177-
/// Set whether to bind the lazy_load_blob statically to this path. That is, take the
178-
/// lazy_load_blob bytes and serve them as the response to any request to this path.
178+
/// Set whether to bind the [`crate::LazyLoadBlob`] statically to this path. That is, take the
179+
/// [`crate::LazyLoadBlob`] bytes and serve them as the response to any request to this path.
179180
cache: bool,
180181
},
181182
/// Unbind a previously-bound HTTP path
@@ -199,26 +200,26 @@ pub enum HttpServerAction {
199200
},
200201
/// Unbind a previously-bound WebSocket path
201202
WebSocketUnbind { path: String },
202-
/// When sent, expects a lazy_load_blob containing the WebSocket message bytes to send.
203+
/// When sent, expects a [`crate::LazyLoadBlob`] containing the WebSocket message bytes to send.
203204
WebSocketPush {
204205
channel_id: u32,
205206
message_type: WsMessageType,
206207
},
207-
/// When sent, expects a `lazy_load_blob` containing the WebSocket message bytes to send.
208-
/// Modifies the `lazy_load_blob` by placing into `WebSocketExtPushData` with id taken from
209-
/// this `KernelMessage` and `kinode_message_type` set to `desired_reply_type`.
208+
/// When sent, expects a [`crate::LazyLoadBlob`] containing the WebSocket message bytes to send.
209+
/// Modifies the [`crate::LazyLoadBlob`] by placing into [`HttpServerAction::WebSocketExtPushData`]` with id taken from
210+
/// this [`KernelMessage`]` and `kinode_message_type` set to `desired_reply_type`.
210211
WebSocketExtPushOutgoing {
211212
channel_id: u32,
212213
message_type: WsMessageType,
213214
desired_reply_type: MessageType,
214215
},
215216
/// For communicating with the ext.
216-
/// Kinode's http_server sends this to the ext after receiving `WebSocketExtPushOutgoing`.
217+
/// Kinode's http_server sends this to the ext after receiving [`HttpServerAction::WebSocketExtPushOutgoing`].
217218
/// Upon receiving reply with this type from ext, http_server parses, setting:
218219
/// * id as given,
219-
/// * message type as given (Request or Response),
220-
/// * body as HttpServerRequest::WebSocketPush,
221-
/// * blob as given.
220+
/// * message type as given ([`crate::Request`] or [`crate::Response`]),
221+
/// * body as [`HttpServerRequest::WebSocketPush`],
222+
/// * [`crate::LazyLoadBlob`] as given.
222223
WebSocketExtPushData {
223224
id: u64,
224225
kinode_message_type: MessageType,
@@ -230,11 +231,12 @@ pub enum HttpServerAction {
230231

231232
/// HTTP Response type that can be shared over Wasm boundary to apps.
232233
/// Respond to [`IncomingHttpRequest`] with this type.
234+
///
235+
/// BODY is stored in the [`crate::LazyLoadBlob`] as bytes
233236
#[derive(Clone, Debug, Serialize, Deserialize)]
234237
pub struct HttpResponse {
235238
pub status: u16,
236239
pub headers: HashMap<String, String>,
237-
// BODY is stored in the lazy_load_blob, as bytes
238240
}
239241

240242
impl HttpResponse {
@@ -268,7 +270,7 @@ impl HttpResponse {
268270
}
269271
}
270272

271-
/// Part of the Response type issued by http_server
273+
/// Part of the [`crate::Response`] type issued by http_server
272274
#[derive(Clone, Debug, Error, Serialize, Deserialize)]
273275
pub enum HttpServerError {
274276
#[error("request could not be parsed to HttpServerAction: {req}.")]
@@ -287,7 +289,7 @@ pub enum HttpServerError {
287289
UnexpectedResponse,
288290
}
289291

290-
/// Whether the WebSocketPush is a request or a response.
292+
/// Whether the [`HttpServerAction::WebSocketPush`] is [`crate::Request`] or [`crate::Response`].
291293
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
292294
pub enum MessageType {
293295
Request,
@@ -1040,11 +1042,11 @@ impl HttpServer {
10401042

10411043
/// Push a WebSocket message to all channels on a given path.
10421044
pub fn ws_push_all_channels(&self, path: &str, message_type: WsMessageType, blob: KiBlob) {
1043-
if let Some(channels) = self.ws_channels.get(path) {
1044-
channels.iter().for_each(|channel_id| {
1045-
send_ws_push(*channel_id, message_type, blob.clone());
1046-
});
1047-
}
1045+
ws_push_all_channels(&self.ws_channels, path, message_type, blob);
1046+
}
1047+
1048+
pub fn get_ws_channels(&self) -> HashMap<String, HashSet<u32>> {
1049+
self.ws_channels.clone()
10481050
}
10491051
}
10501052

@@ -1078,6 +1080,19 @@ pub fn send_ws_push(channel_id: u32, message_type: WsMessageType, blob: KiBlob)
10781080
.unwrap()
10791081
}
10801082

1083+
pub fn ws_push_all_channels(
1084+
ws_channels: &HashMap<String, HashSet<u32>>,
1085+
path: &str,
1086+
message_type: WsMessageType,
1087+
blob: KiBlob,
1088+
) {
1089+
if let Some(channels) = ws_channels.get(path) {
1090+
channels.iter().for_each(|channel_id| {
1091+
send_ws_push(*channel_id, message_type, blob.clone());
1092+
});
1093+
}
1094+
}
1095+
10811096
/// Guess the MIME type of a file from its extension.
10821097
pub fn get_mime_type(filename: &str) -> String {
10831098
let file_path = std::path::Path::new(filename);

src/kernel_types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub enum KernelCommand {
147147
/// The process that sends this command will be given messaging capabilities
148148
/// for the new process if `public` is false.
149149
///
150-
/// All capabilities passed into initial_capabilities must be held by the source
150+
/// All capabilities passed into `initial_capabilities` must be held by the source
151151
/// of this message, or the kernel will discard them (silently for now).
152152
InitializeProcess {
153153
id: ProcessId,
@@ -273,7 +273,7 @@ impl StateError {
273273
/// - `image`: An optional field containing a URL to an image representing the package.
274274
/// - `external_url`: An optional field containing a URL for more information about the package. For example, a link to the github repository.
275275
/// - `animation_url`: An optional field containing a URL to an animation or video representing the package.
276-
/// - `properties`: A requried field containing important information about the package.
276+
/// - `properties`: A required field containing important information about the package.
277277
#[derive(Clone, Debug, Serialize, Deserialize)]
278278
pub struct Erc721Metadata {
279279
pub name: Option<String>,
@@ -288,15 +288,15 @@ pub struct Erc721Metadata {
288288
/// This follows the [ERC1155](https://github.com/ethereum/ercs/blob/master/ERCS/erc-1155.md#erc-1155-metadata-uri-json-schema) metadata standard.
289289
///
290290
/// Fields:
291-
/// - `package_name`: The unique name of the package, used in the `PackageId`, e.g. `package_name:publisher`.
292-
/// - `publisher`: The KNS identity of the package publisher used in the `PackageId`, e.g. `package_name:publisher`
291+
/// - `package_name`: The unique name of the package, used in the [`crate::PackageId`], e.g. `package_name:publisher`.
292+
/// - `publisher`: The KNS identity of the package publisher used in the [`crate::PackageId`], e.g. `package_name:publisher`
293293
/// - `current_version`: A string representing the current version of the package, e.g. `1.0.0`.
294294
/// - `mirrors`: A list of NodeIds where the package can be found, providing redundancy.
295295
/// - `code_hashes`: A map from version names to their respective SHA-256 hashes.
296296
/// - `license`: An optional field containing the license of the package.
297297
/// - `screenshots`: An optional field containing a list of URLs to screenshots of the package.
298298
/// - `wit_version`: An optional field containing the version of the WIT standard that the package adheres to.
299-
/// - `dependencies`: An optional field containing a list of `PackageId`s: API dependencies.
299+
/// - `dependencies`: An optional field containing a list of [`crate::PackageId`]s: API dependencies.
300300
/// - `api_includes`: An optional field containing a list of `PathBuf`s: additional files to include in the `api.zip`.
301301
#[derive(Clone, Debug, Serialize, Deserialize)]
302302
pub struct Erc721Properties {

0 commit comments

Comments
 (0)