nicevg 0.15.0 → 0.15.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/native/darwin-arm64/libnicevg_ffi.dylib +0 -0
- package/native/darwin-x64/libnicevg_ffi.dylib +0 -0
- package/native/linux-arm64/libnicevg_ffi.so +0 -0
- package/native/linux-x64/libnicevg_ffi.so +0 -0
- package/native/win32-x64/nicevg_ffi.dll +0 -0
- package/package.json +2 -2
- package/src/lib.rs +0 -81
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nicevg",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "Deterministic checks and repairs for diagrams, given as shapes or SVG, on Bun and Node through native FFI.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"diagram",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
30
|
"dist",
|
|
31
|
-
"src",
|
|
31
|
+
"src/*.ts",
|
|
32
32
|
"native"
|
|
33
33
|
],
|
|
34
34
|
"engines": {
|
package/src/lib.rs
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
//! A C interface to nicevg for runtimes that load native libraries, such as
|
|
2
|
-
//! Bun's FFI or koffi on Node: a diagram as JSON in, the fixed and arranged
|
|
3
|
-
//! diagram as JSON out.
|
|
4
|
-
//!
|
|
5
|
-
//! The request is either `{"svg": "..."}` or a sketch (`{"nodes": [...],
|
|
6
|
-
//! "connectors": [...]}`). The reply is `{"ok": ...}`, holding the fix
|
|
7
|
-
//! result, or `{"error": "..."}`.
|
|
8
|
-
|
|
9
|
-
use std::panic::{AssertUnwindSafe, catch_unwind};
|
|
10
|
-
|
|
11
|
-
use serde_json::{Value, json};
|
|
12
|
-
|
|
13
|
-
#[global_allocator]
|
|
14
|
-
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
|
15
|
-
|
|
16
|
-
enum Input {
|
|
17
|
-
Svg(String),
|
|
18
|
-
Sketch(nicevg::Sketch),
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
fn read(request: &[u8]) -> Result<Input, String> {
|
|
22
|
-
let value: Value = serde_json::from_slice(request).map_err(|error| format!("invalid JSON: {error}"))?;
|
|
23
|
-
if let Some(svg) = value.get("svg") {
|
|
24
|
-
return svg
|
|
25
|
-
.as_str()
|
|
26
|
-
.map(|svg| Input::Svg(svg.to_owned()))
|
|
27
|
-
.ok_or_else(|| "svg: expected a string".to_owned());
|
|
28
|
-
}
|
|
29
|
-
serde_path_to_error::deserialize(value)
|
|
30
|
-
.map(Input::Sketch)
|
|
31
|
-
.map_err(|error| format!("{}: {}", error.path(), error.inner()))
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
fn render(request: &[u8]) -> Result<Value, String> {
|
|
35
|
-
let result = match read(request)? {
|
|
36
|
-
Input::Svg(svg) => nicevg::arrange(&svg).map_err(|error| error.to_string())?,
|
|
37
|
-
Input::Sketch(sketch) => nicevg::arrange_sketch(&sketch).map_err(|error| error.to_string())?,
|
|
38
|
-
};
|
|
39
|
-
Ok(serde_json::to_value(result).expect("serializable result"))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/// Fixes and arranges the diagram in the JSON request of `length` bytes at
|
|
43
|
-
/// `request`, and returns the JSON reply, its length written to
|
|
44
|
-
/// `reply_length`. The caller frees the reply with [`nicevg_free`].
|
|
45
|
-
///
|
|
46
|
-
/// # Safety
|
|
47
|
-
///
|
|
48
|
-
/// `request` points to `length` readable bytes and `reply_length` to a
|
|
49
|
-
/// writable `usize`.
|
|
50
|
-
#[unsafe(no_mangle)]
|
|
51
|
-
pub unsafe extern "C" fn nicevg_render(request: *const u8, length: usize, reply_length: *mut usize) -> *mut u8 {
|
|
52
|
-
let request = if length == 0 {
|
|
53
|
-
&[][..]
|
|
54
|
-
} else {
|
|
55
|
-
// SAFETY: the caller passes `length` readable bytes.
|
|
56
|
-
unsafe { std::slice::from_raw_parts(request, length) }
|
|
57
|
-
};
|
|
58
|
-
let reply = match catch_unwind(AssertUnwindSafe(|| render(request))) {
|
|
59
|
-
Ok(Ok(value)) => json!({ "ok": value }),
|
|
60
|
-
Ok(Err(message)) => json!({ "error": message }),
|
|
61
|
-
Err(_) => json!({ "error": "nicevg stopped on an internal error" }),
|
|
62
|
-
};
|
|
63
|
-
let bytes = serde_json::to_vec(&reply)
|
|
64
|
-
.expect("serializable reply")
|
|
65
|
-
.into_boxed_slice();
|
|
66
|
-
// SAFETY: the caller passes a writable `usize`.
|
|
67
|
-
unsafe { *reply_length = bytes.len() };
|
|
68
|
-
Box::into_raw(bytes).cast::<u8>()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/// Frees a reply [`nicevg_render`] returned.
|
|
72
|
-
///
|
|
73
|
-
/// # Safety
|
|
74
|
-
///
|
|
75
|
-
/// `reply` and `length` are a reply and its length from [`nicevg_render`],
|
|
76
|
-
/// freed only once.
|
|
77
|
-
#[unsafe(no_mangle)]
|
|
78
|
-
pub unsafe extern "C" fn nicevg_free(reply: *mut u8, length: usize) {
|
|
79
|
-
// SAFETY: the reply was a boxed slice of `length` bytes.
|
|
80
|
-
drop(unsafe { Box::from_raw(std::ptr::slice_from_raw_parts_mut(reply, length)) });
|
|
81
|
-
}
|