Upgrade to interprocess 2.0.0 (#12729)

# Description

This fixes #12724. NetBSD confirmed to work with this change.

The update also behaves a bit better in some ways - it automatically
unlinks and reclaims sockets on Unix, and doesn't try to flush/sync the
socket on Windows, so I was able to remove that platform-specific logic.

They also have a way to split the socket so I could just use one socket
now, but I haven't tried to do that yet. That would be more of a
breaking change but I think it's more straightforward.

# User-Facing Changes

- Hopefully more platforms work

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
This commit is contained in:
Devyn Cairns
2024-05-02 22:31:33 -07:00
committed by GitHub
parent bc6d934fa1
commit 72f3942c37
5 changed files with 79 additions and 260 deletions

View File

@ -1,9 +1,12 @@
use std::{
error::Error,
ffi::OsStr,
io::{BufRead, BufReader, Write},
};
use interprocess::local_socket::LocalSocketStream;
use interprocess::local_socket::{
self, traits::Stream, GenericFilePath, GenericNamespaced, ToFsName, ToNsName,
};
use serde::Deserialize;
use serde_json::{json, Value};
@ -35,9 +38,6 @@ pub fn main() -> Result<(), Box<dyn Error>> {
local_socket_path: None,
};
#[allow(unused_mut)]
let mut should_flush = true;
let (mut input, mut output): (Box<dyn BufRead>, Box<dyn Write>) =
match args.get(1).map(|s| s.as_str()) {
Some("--stdio") => (
@ -49,14 +49,13 @@ pub fn main() -> Result<(), Box<dyn Error>> {
if opts.refuse_local_socket {
std::process::exit(1)
} else {
let in_socket = LocalSocketStream::connect(args[2].as_str())?;
let out_socket = LocalSocketStream::connect(args[2].as_str())?;
#[cfg(windows)]
{
// Flushing on a socket on Windows is weird and waits for the other side
should_flush = false;
}
let name = if cfg!(windows) {
OsStr::new(&args[2]).to_ns_name::<GenericNamespaced>()?
} else {
OsStr::new(&args[2]).to_fs_name::<GenericFilePath>()?
};
let in_socket = local_socket::Stream::connect(name.clone())?;
let out_socket = local_socket::Stream::connect(name)?;
(Box::new(BufReader::new(in_socket)), Box::new(out_socket))
}
@ -73,9 +72,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
// Send encoding format
output.write_all(b"\x04json")?;
if should_flush {
output.flush()?;
}
output.flush()?;
// Test exiting without `Hello`
if opts.exit_before_hello {
@ -91,7 +88,6 @@ pub fn main() -> Result<(), Box<dyn Error>> {
// Send `Hello` message
write(
&mut output,
should_flush,
&json!({
"Hello": {
"protocol": "nu-plugin",
@ -117,7 +113,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
// Parse incoming messages
loop {
match Value::deserialize(&mut de) {
Ok(message) => handle_message(&mut output, should_flush, &opts, &message)?,
Ok(message) => handle_message(&mut output, &opts, &message)?,
Err(err) => {
if err.is_eof() {
break;
@ -135,7 +131,6 @@ pub fn main() -> Result<(), Box<dyn Error>> {
fn handle_message(
output: &mut impl Write,
should_flush: bool,
opts: &Options,
message: &Value,
) -> Result<(), Box<dyn Error>> {
@ -144,7 +139,6 @@ fn handle_message(
if plugin_call.as_str() == Some("Signature") {
write(
output,
should_flush,
&json!({
"CallResponse": [
id,
@ -165,7 +159,6 @@ fn handle_message(
});
write(
output,
should_flush,
&json!({
"CallResponse": [
id,
@ -212,11 +205,9 @@ fn signatures() -> Vec<Value> {
})]
}
fn write(output: &mut impl Write, should_flush: bool, value: &Value) -> Result<(), Box<dyn Error>> {
fn write(output: &mut impl Write, value: &Value) -> Result<(), Box<dyn Error>> {
serde_json::to_writer(&mut *output, value)?;
output.write_all(b"\n")?;
if should_flush {
output.flush()?;
}
output.flush()?;
Ok(())
}