@r_masseater/masseater-utils 0.9.0 → 0.9.1
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 +1 -1
- package/native/desktop-shell/Cargo.toml +38 -0
- package/native/desktop-shell/src/app/builder.rs +39 -0
- package/native/desktop-shell/src/app/mod.rs +3 -0
- package/native/desktop-shell/src/entities/access_token.rs +77 -0
- package/native/desktop-shell/src/entities/mod.rs +1 -0
- package/native/desktop-shell/src/features/mod.rs +4 -0
- package/native/desktop-shell/src/features/record_telemetry/appender.rs +127 -0
- package/native/desktop-shell/src/features/record_telemetry/forwarder.rs +183 -0
- package/native/desktop-shell/src/features/record_telemetry/invoke.rs +80 -0
- package/native/desktop-shell/src/features/record_telemetry/mod.rs +231 -0
- package/native/desktop-shell/src/features/serve_screen/mod.rs +135 -0
- package/native/desktop-shell/src/features/serve_tools/mod.rs +37 -0
- package/native/desktop-shell/src/features/update_app/mod.rs +53 -0
- package/native/desktop-shell/src/lib.rs +17 -0
- package/native/desktop-shell/src/shared/lib/bearer.rs +49 -0
- package/native/desktop-shell/src/shared/lib/host.rs +1 -0
- package/native/desktop-shell/src/shared/lib/mod.rs +5 -0
- package/native/desktop-shell/src/shared/mod.rs +2 -0
- package/native/desktop-shell/src/shared/ui/log.rs +15 -0
- package/native/desktop-shell/src/shared/ui/menu.rs +37 -0
- package/native/desktop-shell/src/shared/ui/mod.rs +7 -0
- package/native/desktop-shell/src/shared/ui/window.rs +49 -0
- package/native/media-notes/Cargo.toml +36 -0
- package/native/media-notes/src/entities/media_file.rs +89 -0
- package/native/media-notes/src/entities/media_notes.rs +31 -0
- package/native/media-notes/src/entities/mod.rs +3 -0
- package/native/media-notes/src/entities/source/credit.rs +129 -0
- package/native/media-notes/src/entities/source/link.rs +194 -0
- package/native/media-notes/src/entities/source/mod.rs +5 -0
- package/native/media-notes/src/features/mod.rs +2 -0
- package/native/media-notes/src/features/open_media/mod.rs +88 -0
- package/native/media-notes/src/features/share_mcp_access.rs +15 -0
- package/native/media-notes/src/lib.rs +12 -0
- package/native/media-notes/src/shared/db/mod.rs +3 -0
- package/native/media-notes/src/shared/db/store.rs +276 -0
- package/native/media-notes/src/shared/lib/failure.rs +55 -0
- package/native/media-notes/src/shared/lib/mcp.rs +34 -0
- package/native/media-notes/src/shared/lib/mod.rs +5 -0
- package/native/media-notes/src/shared/mod.rs +3 -0
- package/native/media-notes/src/shared/protocol.rs +69 -0
- package/package.json +5 -1
- package/skills/build-tauri-app/SKILL.md +4 -2
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ The `peerDependencies` of `package.json` are the exact list with ranges.
|
|
|
60
60
|
|
|
61
61
|
## Tauri desktop apps
|
|
62
62
|
|
|
63
|
-
The release commands run through `tauriReleaseTasks` and `bundleCollect` of `desktop-shell/vite`: `bundle-collect <kind>` copies a Tauri bundle and its signature into `release-assets/`, `updater-feed` writes the updater feed of a released version to the `<name>-updater` GitHub Release, and `updater-key` makes the updater signing key in the `<name>-release` environment and prints the public key. The Rust crates `desktop-shell` and `media-notes` are in `native/`;
|
|
63
|
+
The release commands run through `tauriReleaseTasks` and `bundleCollect` of `desktop-shell/vite`: `bundle-collect <kind>` copies a Tauri bundle and its signature into `release-assets/`, `updater-feed` writes the updater feed of a released version to the `<name>-updater` GitHub Release, and `updater-key` makes the updater signing key in the `<name>-release` environment and prints the public key. The Rust crates `desktop-shell` and `media-notes` are in `native/`; they ship in this package, so an app depends on them with a path dependency into its `node_modules`.
|
|
64
64
|
|
|
65
65
|
## Examples
|
|
66
66
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "desktop-shell"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
license = "MIT"
|
|
6
|
+
repository.workspace = true
|
|
7
|
+
rust-version.workspace = true
|
|
8
|
+
publish = false
|
|
9
|
+
|
|
10
|
+
[dependencies]
|
|
11
|
+
axum = "=0.8.9"
|
|
12
|
+
constant_time_eq = "=0.6.2"
|
|
13
|
+
getrandom = "=0.4.3"
|
|
14
|
+
opentelemetry = "=0.33.1"
|
|
15
|
+
opentelemetry_sdk = "=0.33.1"
|
|
16
|
+
otlp-file-exporter = "=0.2.1"
|
|
17
|
+
rmcp = { version = "=3.12.0", features = ["macros", "server", "transport-streamable-http-server"] }
|
|
18
|
+
tauri = { version = "=2.12.7", features = ["tracing"] }
|
|
19
|
+
tauri-plugin-dialog = "=2.8.4"
|
|
20
|
+
tauri-plugin-localhost = "=2.4.0"
|
|
21
|
+
tauri-plugin-log = "=2.10.2"
|
|
22
|
+
tauri-plugin-prevent-default = "=6.0.1"
|
|
23
|
+
tauri-plugin-single-instance = "=2.5.5"
|
|
24
|
+
tauri-plugin-updater = "=2.14.1"
|
|
25
|
+
tauri-plugin-window-state = "=2.5.0"
|
|
26
|
+
tokio = { version = "=1.54.1", features = ["net"] }
|
|
27
|
+
serde_json = "=1.0.154"
|
|
28
|
+
tracing = "=0.1.44"
|
|
29
|
+
tracing-opentelemetry = "=0.34.0"
|
|
30
|
+
tracing-subscriber = { version = "=0.3.23", features = ["registry"] }
|
|
31
|
+
url = "=2.5.9"
|
|
32
|
+
ureq = { version = "=3.5.2", features = ["platform-verifier"] }
|
|
33
|
+
|
|
34
|
+
[dev-dependencies]
|
|
35
|
+
tempfile = "=3.27.0"
|
|
36
|
+
|
|
37
|
+
[lints]
|
|
38
|
+
workspace = true
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
use tauri::{Builder, Wry};
|
|
2
|
+
|
|
3
|
+
use crate::features::record_telemetry::telemetry_plugin;
|
|
4
|
+
use crate::features::serve_screen::served;
|
|
5
|
+
use crate::features::update_app::updates_plugin;
|
|
6
|
+
use crate::shared::ui::{browser_keys_plugin, japanese_menu, log_plugin, show, state_plugin};
|
|
7
|
+
|
|
8
|
+
pub struct Shell {
|
|
9
|
+
pub name: &'static str,
|
|
10
|
+
pub window: &'static str,
|
|
11
|
+
pub port: u16,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#[must_use]
|
|
15
|
+
pub fn builder(shell: &Shell) -> Builder<Wry> {
|
|
16
|
+
let window = shell.window;
|
|
17
|
+
let name = shell.name;
|
|
18
|
+
let app = Builder::default();
|
|
19
|
+
let app = if cfg!(target_os = "macos") {
|
|
20
|
+
app.menu(move |handle| japanese_menu(handle, name))
|
|
21
|
+
} else {
|
|
22
|
+
app
|
|
23
|
+
};
|
|
24
|
+
served(
|
|
25
|
+
app.plugin(tauri_plugin_single_instance::init(
|
|
26
|
+
move |handle, _arguments, _cwd| {
|
|
27
|
+
show(handle, window);
|
|
28
|
+
},
|
|
29
|
+
))
|
|
30
|
+
.plugin(telemetry_plugin())
|
|
31
|
+
.plugin(log_plugin())
|
|
32
|
+
.plugin(tauri_plugin_dialog::init())
|
|
33
|
+
.plugin(tauri_plugin_updater::Builder::new().build())
|
|
34
|
+
.plugin(updates_plugin())
|
|
35
|
+
.plugin(state_plugin())
|
|
36
|
+
.plugin(browser_keys_plugin()),
|
|
37
|
+
shell.port,
|
|
38
|
+
)
|
|
39
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
use std::fmt::Write as _;
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::io::{self, Write as _};
|
|
4
|
+
use std::path::Path;
|
|
5
|
+
|
|
6
|
+
const TOKEN_FILE: &str = "mcp-token";
|
|
7
|
+
const TOKEN_BYTES: usize = 32;
|
|
8
|
+
|
|
9
|
+
fn hex(bytes: &[u8]) -> String {
|
|
10
|
+
bytes.iter().fold(String::new(), |mut text, byte| {
|
|
11
|
+
let _ = write!(text, "{byte:02x}");
|
|
12
|
+
text
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
fn write_private(file: &Path, text: &str) -> io::Result<()> {
|
|
17
|
+
let mut options = fs::OpenOptions::new();
|
|
18
|
+
options.write(true).create(true).truncate(true);
|
|
19
|
+
#[cfg(unix)]
|
|
20
|
+
{
|
|
21
|
+
use std::os::unix::fs::OpenOptionsExt;
|
|
22
|
+
options.mode(0o600);
|
|
23
|
+
}
|
|
24
|
+
let mut file = options.open(file)?;
|
|
25
|
+
#[cfg(unix)]
|
|
26
|
+
{
|
|
27
|
+
use std::os::unix::fs::PermissionsExt;
|
|
28
|
+
file.set_permissions(fs::Permissions::from_mode(0o600))?;
|
|
29
|
+
}
|
|
30
|
+
file.write_all(text.as_bytes())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pub fn token(directory: &Path) -> io::Result<String> {
|
|
34
|
+
let file = directory.join(TOKEN_FILE);
|
|
35
|
+
let stored = fs::read_to_string(&file).unwrap_or_default();
|
|
36
|
+
if !stored.trim().is_empty() {
|
|
37
|
+
return Ok(stored.trim().to_owned());
|
|
38
|
+
}
|
|
39
|
+
let mut bytes = [0_u8; TOKEN_BYTES];
|
|
40
|
+
getrandom::fill(&mut bytes).map_err(io::Error::other)?;
|
|
41
|
+
let token = hex(&bytes);
|
|
42
|
+
fs::create_dir_all(directory)?;
|
|
43
|
+
write_private(&file, &token)?;
|
|
44
|
+
Ok(token)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#[cfg(test)]
|
|
48
|
+
mod tests {
|
|
49
|
+
use super::token;
|
|
50
|
+
|
|
51
|
+
#[test]
|
|
52
|
+
fn keeps_the_token_it_made() {
|
|
53
|
+
let directory = tempfile::tempdir().expect("temporary directory");
|
|
54
|
+
let first = token(directory.path()).expect("first token");
|
|
55
|
+
let second = token(directory.path()).expect("second token");
|
|
56
|
+
assert_eq!(first.len(), 64);
|
|
57
|
+
assert_eq!(first, second);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#[cfg(unix)]
|
|
61
|
+
#[test]
|
|
62
|
+
fn only_the_owner_can_read_the_token_file() {
|
|
63
|
+
use std::os::unix::fs::PermissionsExt;
|
|
64
|
+
|
|
65
|
+
let directory = tempfile::tempdir().expect("temporary directory");
|
|
66
|
+
let file = directory.path().join(super::TOKEN_FILE);
|
|
67
|
+
std::fs::write(&file, "").expect("empty token file");
|
|
68
|
+
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644))
|
|
69
|
+
.expect("loose permissions");
|
|
70
|
+
token(directory.path()).expect("token");
|
|
71
|
+
let mode = std::fs::metadata(&file)
|
|
72
|
+
.expect("token file")
|
|
73
|
+
.permissions()
|
|
74
|
+
.mode();
|
|
75
|
+
assert_eq!(mode & 0o777, 0o600);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pub mod access_token;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
use std::fs::{self, File, OpenOptions};
|
|
2
|
+
use std::io::{self, Write};
|
|
3
|
+
use std::path::Path;
|
|
4
|
+
use std::sync::{Arc, Mutex, PoisonError};
|
|
5
|
+
|
|
6
|
+
use super::forwarder::Forwarder;
|
|
7
|
+
|
|
8
|
+
const LIMIT: u64 = 64 * 1024 * 1024;
|
|
9
|
+
const NEWLINE: u8 = b'\n';
|
|
10
|
+
|
|
11
|
+
#[derive(Debug)]
|
|
12
|
+
pub struct Appender {
|
|
13
|
+
file: Arc<Mutex<File>>,
|
|
14
|
+
pending: Vec<u8>,
|
|
15
|
+
forwarder: Option<Forwarder>,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
impl Clone for Appender {
|
|
19
|
+
fn clone(&self) -> Self {
|
|
20
|
+
Self {
|
|
21
|
+
file: Arc::clone(&self.file),
|
|
22
|
+
pending: Vec::new(),
|
|
23
|
+
forwarder: self.forwarder.clone(),
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl Appender {
|
|
29
|
+
pub fn open(dir: &Path, signal: &str, forwarder: Option<Forwarder>) -> io::Result<Self> {
|
|
30
|
+
fs::create_dir_all(dir)?;
|
|
31
|
+
let current = dir.join(format!("{signal}.jsonl"));
|
|
32
|
+
if fs::metadata(¤t).is_ok_and(|meta| meta.len() > LIMIT) {
|
|
33
|
+
fs::rename(¤t, dir.join(format!("{signal}.1.jsonl")))?;
|
|
34
|
+
}
|
|
35
|
+
let file = OpenOptions::new().create(true).append(true).open(current)?;
|
|
36
|
+
Ok(Self {
|
|
37
|
+
file: Arc::new(Mutex::new(file)),
|
|
38
|
+
pending: Vec::new(),
|
|
39
|
+
forwarder,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fn forward(&self, lines: &[u8]) {
|
|
44
|
+
if let Some(forwarder) = &self.forwarder {
|
|
45
|
+
lines
|
|
46
|
+
.split(|byte| *byte == NEWLINE)
|
|
47
|
+
.filter(|line| !line.is_empty())
|
|
48
|
+
.for_each(|line| forwarder.send(line));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fn append(&self, lines: &[u8]) -> io::Result<()> {
|
|
53
|
+
self.forward(lines);
|
|
54
|
+
self.file
|
|
55
|
+
.lock()
|
|
56
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
57
|
+
.write_all(lines)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
pub fn line(&self, json: &serde_json::Value) -> io::Result<()> {
|
|
61
|
+
let mut bytes = serde_json::to_vec(json)?;
|
|
62
|
+
bytes.push(NEWLINE);
|
|
63
|
+
let mut file = self.file.lock().unwrap_or_else(PoisonError::into_inner);
|
|
64
|
+
if file.metadata()?.len() > LIMIT {
|
|
65
|
+
return Err(io::ErrorKind::FileTooLarge.into());
|
|
66
|
+
}
|
|
67
|
+
self.forward(&bytes);
|
|
68
|
+
file.write_all(&bytes)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
impl Write for Appender {
|
|
73
|
+
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
|
74
|
+
self.pending.extend_from_slice(bytes);
|
|
75
|
+
if let Some(end) = self.pending.iter().rposition(|byte| *byte == NEWLINE) {
|
|
76
|
+
let complete: Vec<u8> = self.pending.drain(..=end).collect();
|
|
77
|
+
self.append(&complete)?;
|
|
78
|
+
}
|
|
79
|
+
Ok(bytes.len())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fn flush(&mut self) -> io::Result<()> {
|
|
83
|
+
self.file
|
|
84
|
+
.lock()
|
|
85
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
86
|
+
.flush()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[cfg(test)]
|
|
91
|
+
mod tests {
|
|
92
|
+
use std::fs;
|
|
93
|
+
use std::io::Write;
|
|
94
|
+
|
|
95
|
+
use super::{Appender, LIMIT};
|
|
96
|
+
|
|
97
|
+
#[test]
|
|
98
|
+
fn keeps_lines_whole_when_two_writers_share_the_file() {
|
|
99
|
+
let dir = tempfile::tempdir().unwrap();
|
|
100
|
+
let mut spans = Appender::open(dir.path(), "traces", None).unwrap();
|
|
101
|
+
let screen = spans.clone();
|
|
102
|
+
spans.write_all(b"{\"resourceSpans\":").unwrap();
|
|
103
|
+
screen
|
|
104
|
+
.line(&serde_json::json!({ "resourceSpans": [] }))
|
|
105
|
+
.unwrap();
|
|
106
|
+
spans.write_all(b"[]}\n").unwrap();
|
|
107
|
+
let written = fs::read_to_string(dir.path().join("traces.jsonl")).unwrap();
|
|
108
|
+
assert_eq!(written, "{\"resourceSpans\":[]}\n{\"resourceSpans\":[]}\n");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[test]
|
|
112
|
+
fn refuses_screen_lines_once_the_file_has_passed_the_limit() {
|
|
113
|
+
let dir = tempfile::tempdir().unwrap();
|
|
114
|
+
let screen = Appender::open(dir.path(), "logs", None).unwrap();
|
|
115
|
+
let file = dir.path().join("logs.jsonl");
|
|
116
|
+
let batch = serde_json::json!({ "resourceLogs": [] });
|
|
117
|
+
screen.line(&batch).unwrap();
|
|
118
|
+
fs::OpenOptions::new()
|
|
119
|
+
.write(true)
|
|
120
|
+
.open(&file)
|
|
121
|
+
.unwrap()
|
|
122
|
+
.set_len(LIMIT + 1)
|
|
123
|
+
.unwrap();
|
|
124
|
+
assert!(screen.line(&batch).is_err());
|
|
125
|
+
assert_eq!(fs::metadata(&file).unwrap().len(), LIMIT + 1);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
use std::sync::mpsc::{RecvTimeoutError, Sender, channel};
|
|
2
|
+
use std::thread;
|
|
3
|
+
use std::time::{Duration, Instant};
|
|
4
|
+
|
|
5
|
+
const URL: &str = "TELEMETRY_URL";
|
|
6
|
+
const KEY: &str = "TELEMETRY_API_KEY";
|
|
7
|
+
const DEFAULT_URL: &str = "https://telemetry.masseater.dev";
|
|
8
|
+
const TIMEOUT: Duration = Duration::from_secs(10);
|
|
9
|
+
const INTERVAL: Duration = Duration::from_mins(1);
|
|
10
|
+
const FULL: usize = 1024 * 1024;
|
|
11
|
+
|
|
12
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
13
|
+
pub struct Destination {
|
|
14
|
+
pub url: String,
|
|
15
|
+
pub key: Option<String>,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fn filled(value: Option<String>) -> Option<String> {
|
|
19
|
+
value
|
|
20
|
+
.map(|value| value.trim().to_owned())
|
|
21
|
+
.filter(|value| !value.is_empty())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#[must_use]
|
|
25
|
+
pub fn destination_of(
|
|
26
|
+
runtime: impl Fn(&str) -> Option<String>,
|
|
27
|
+
built: (Option<&str>, Option<&str>),
|
|
28
|
+
) -> Option<Destination> {
|
|
29
|
+
let url = filled(runtime(URL)).or_else(|| filled(built.0.map(str::to_owned)))?;
|
|
30
|
+
let key = filled(runtime(KEY)).or_else(|| filled(built.1.map(str::to_owned)));
|
|
31
|
+
Some(Destination {
|
|
32
|
+
url: url.trim_end_matches('/').to_owned(),
|
|
33
|
+
key,
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#[must_use]
|
|
38
|
+
pub fn configured() -> Option<Destination> {
|
|
39
|
+
destination_of(
|
|
40
|
+
|name| std::env::var(name).ok(),
|
|
41
|
+
(
|
|
42
|
+
option_env!("TELEMETRY_URL").or(Some(DEFAULT_URL)),
|
|
43
|
+
option_env!("TELEMETRY_API_KEY"),
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#[derive(Debug, Clone)]
|
|
49
|
+
pub struct Forwarder {
|
|
50
|
+
lines: Sender<Vec<u8>>,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
impl Forwarder {
|
|
54
|
+
#[must_use]
|
|
55
|
+
pub fn start(destination: Destination, signal: &str) -> Self {
|
|
56
|
+
Self::every(destination, signal, INTERVAL)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fn every(destination: Destination, signal: &str, interval: Duration) -> Self {
|
|
60
|
+
let (lines, received) = channel::<Vec<u8>>();
|
|
61
|
+
let endpoint = format!("{}/v1/{signal}", destination.url);
|
|
62
|
+
let agent: ureq::Agent = ureq::Agent::config_builder()
|
|
63
|
+
.timeout_global(Some(TIMEOUT))
|
|
64
|
+
.http_status_as_error(false)
|
|
65
|
+
.build()
|
|
66
|
+
.into();
|
|
67
|
+
let post = move |body: &[u8]| {
|
|
68
|
+
let mut request = agent
|
|
69
|
+
.post(&endpoint)
|
|
70
|
+
.header("content-type", "application/x-ndjson");
|
|
71
|
+
if let Some(key) = &destination.key {
|
|
72
|
+
request = request.header("authorization", &format!("Bearer {key}"));
|
|
73
|
+
}
|
|
74
|
+
let _ = request.send(body);
|
|
75
|
+
};
|
|
76
|
+
thread::spawn(move || {
|
|
77
|
+
let mut pending = Vec::new();
|
|
78
|
+
let mut due = Instant::now() + interval;
|
|
79
|
+
loop {
|
|
80
|
+
let wait = due.saturating_duration_since(Instant::now());
|
|
81
|
+
let closed = match received.recv_timeout(wait) {
|
|
82
|
+
Ok(line) => {
|
|
83
|
+
pending.extend_from_slice(&line);
|
|
84
|
+
pending.push(b'\n');
|
|
85
|
+
false
|
|
86
|
+
}
|
|
87
|
+
Err(RecvTimeoutError::Timeout) => false,
|
|
88
|
+
Err(RecvTimeoutError::Disconnected) => true,
|
|
89
|
+
};
|
|
90
|
+
if !pending.is_empty() && (closed || pending.len() >= FULL || Instant::now() >= due)
|
|
91
|
+
{
|
|
92
|
+
post(&pending);
|
|
93
|
+
pending.clear();
|
|
94
|
+
}
|
|
95
|
+
if closed {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if Instant::now() >= due {
|
|
99
|
+
due = Instant::now() + interval;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
Self { lines }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
pub fn send(&self, line: &[u8]) {
|
|
107
|
+
let _ = self.lines.send(line.to_vec());
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[cfg(test)]
|
|
112
|
+
mod tests {
|
|
113
|
+
use std::io::{BufRead, BufReader, Read, Write};
|
|
114
|
+
use std::net::TcpListener;
|
|
115
|
+
|
|
116
|
+
use std::time::Duration;
|
|
117
|
+
|
|
118
|
+
use super::{Destination, Forwarder, destination_of};
|
|
119
|
+
|
|
120
|
+
#[test]
|
|
121
|
+
fn posts_the_lines_of_an_interval_together_with_the_key() {
|
|
122
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
123
|
+
let url = format!("http://{}", listener.local_addr().unwrap());
|
|
124
|
+
let forwarder = Forwarder::every(
|
|
125
|
+
Destination {
|
|
126
|
+
url,
|
|
127
|
+
key: Some("k".to_owned()),
|
|
128
|
+
},
|
|
129
|
+
"logs",
|
|
130
|
+
Duration::from_millis(50),
|
|
131
|
+
);
|
|
132
|
+
forwarder.send(b"{\"resourceLogs\":[]}");
|
|
133
|
+
forwarder.send(b"{\"resourceLogs\":[1]}");
|
|
134
|
+
let (stream, _) = listener.accept().unwrap();
|
|
135
|
+
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
|
136
|
+
let mut head = Vec::new();
|
|
137
|
+
let mut length = 0;
|
|
138
|
+
loop {
|
|
139
|
+
let mut line = String::new();
|
|
140
|
+
reader.read_line(&mut line).unwrap();
|
|
141
|
+
if line == "\r\n" {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
if let Some(value) = line.to_lowercase().strip_prefix("content-length:") {
|
|
145
|
+
length = value.trim().parse().unwrap();
|
|
146
|
+
}
|
|
147
|
+
head.push(line);
|
|
148
|
+
}
|
|
149
|
+
let mut body = vec![0; length];
|
|
150
|
+
reader.read_exact(&mut body).unwrap();
|
|
151
|
+
(&stream)
|
|
152
|
+
.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n")
|
|
153
|
+
.unwrap();
|
|
154
|
+
assert_eq!(head[0], "POST /v1/logs HTTP/1.1\r\n");
|
|
155
|
+
assert!(
|
|
156
|
+
head.iter()
|
|
157
|
+
.any(|line| line == "authorization: Bearer k\r\n")
|
|
158
|
+
);
|
|
159
|
+
assert_eq!(body, b"{\"resourceLogs\":[]}\n{\"resourceLogs\":[1]}\n");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
#[test]
|
|
163
|
+
fn prefers_the_runtime_settings_and_needs_a_url() {
|
|
164
|
+
assert_eq!(destination_of(|_| None, (None, Some("k"))), None);
|
|
165
|
+
assert_eq!(
|
|
166
|
+
destination_of(|_| None, (Some("https://built.example/"), Some("k"))),
|
|
167
|
+
Some(Destination {
|
|
168
|
+
url: "https://built.example".to_owned(),
|
|
169
|
+
key: Some("k".to_owned())
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
assert_eq!(
|
|
173
|
+
destination_of(
|
|
174
|
+
|name| (name == "TELEMETRY_URL").then(|| "https://run.example".to_owned()),
|
|
175
|
+
(Some("https://built.example"), Some(" "))
|
|
176
|
+
),
|
|
177
|
+
Some(Destination {
|
|
178
|
+
url: "https://run.example".to_owned(),
|
|
179
|
+
key: None
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
use opentelemetry::propagation::{Extractor, TextMapPropagator};
|
|
2
|
+
use opentelemetry::trace::TraceContextExt;
|
|
3
|
+
use opentelemetry_sdk::propagation::TraceContextPropagator;
|
|
4
|
+
use tauri::Runtime;
|
|
5
|
+
use tauri::http::HeaderMap;
|
|
6
|
+
use tauri::ipc::Invoke;
|
|
7
|
+
use tracing_opentelemetry::OpenTelemetrySpanExt;
|
|
8
|
+
|
|
9
|
+
struct Headers<'a>(&'a HeaderMap);
|
|
10
|
+
|
|
11
|
+
impl Extractor for Headers<'_> {
|
|
12
|
+
fn get(&self, key: &str) -> Option<&str> {
|
|
13
|
+
self.0.get(key).and_then(|value| value.to_str().ok())
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
fn keys(&self) -> Vec<&str> {
|
|
17
|
+
self.0.keys().map(tauri::http::HeaderName::as_str).collect()
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
pub fn invoked(headers: &HeaderMap, command: &str) -> tracing::Span {
|
|
22
|
+
let runtime = tracing::Span::current()
|
|
23
|
+
.context()
|
|
24
|
+
.span()
|
|
25
|
+
.span_context()
|
|
26
|
+
.clone();
|
|
27
|
+
let span = tracing::info_span!("ipc.invoke", cmd = command);
|
|
28
|
+
let _ = span.set_parent(TraceContextPropagator::new().extract(&Headers(headers)));
|
|
29
|
+
if runtime.is_valid() {
|
|
30
|
+
span.add_link(runtime);
|
|
31
|
+
}
|
|
32
|
+
span
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
pub fn traced_commands<R, F>(handler: F) -> impl Fn(Invoke<R>) -> bool + Send + Sync + 'static
|
|
36
|
+
where
|
|
37
|
+
R: Runtime,
|
|
38
|
+
F: Fn(Invoke<R>) -> bool + Send + Sync + 'static,
|
|
39
|
+
{
|
|
40
|
+
move |invoke| {
|
|
41
|
+
let span = invoked(invoke.message.headers(), invoke.message.command());
|
|
42
|
+
span.in_scope(|| handler(invoke))
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
#[cfg(test)]
|
|
47
|
+
mod tests {
|
|
48
|
+
use std::fs;
|
|
49
|
+
|
|
50
|
+
use tauri::http::{HeaderMap, HeaderValue};
|
|
51
|
+
use tracing_subscriber::layer::SubscriberExt;
|
|
52
|
+
|
|
53
|
+
use super::super::{recording, spans};
|
|
54
|
+
use super::invoked;
|
|
55
|
+
|
|
56
|
+
const TRACE: &str = "4bf92f3577b34da6a3ce929d0e0e4736";
|
|
57
|
+
const SCREEN_SPAN: &str = "00f067aa0ba902b7";
|
|
58
|
+
|
|
59
|
+
#[test]
|
|
60
|
+
fn continues_the_trace_the_screen_sent_with_the_command() {
|
|
61
|
+
let dir = tempfile::tempdir().unwrap();
|
|
62
|
+
let recording = recording(dir.path(), "app.test", None).unwrap();
|
|
63
|
+
let subscriber = tracing_subscriber::registry().with(spans(&recording.provider));
|
|
64
|
+
let mut headers = HeaderMap::new();
|
|
65
|
+
headers.insert(
|
|
66
|
+
"traceparent",
|
|
67
|
+
HeaderValue::from_str(&format!("00-{TRACE}-{SCREEN_SPAN}-01")).unwrap(),
|
|
68
|
+
);
|
|
69
|
+
tracing::subscriber::with_default(subscriber, || {
|
|
70
|
+
invoked(&headers, "list_songs").in_scope(|| {
|
|
71
|
+
tracing::info_span!("list").in_scope(|| {});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
recording.provider.force_flush().unwrap();
|
|
75
|
+
let written = fs::read_to_string(dir.path().join("traces.jsonl")).unwrap();
|
|
76
|
+
assert!(written.contains("ipc.invoke") && written.contains("\"list\""));
|
|
77
|
+
assert_eq!(written.matches(TRACE).count(), 2);
|
|
78
|
+
assert!(written.contains(SCREEN_SPAN));
|
|
79
|
+
}
|
|
80
|
+
}
|