@contentauth/c2pa-node 0.5.4 → 0.6.0
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/Cargo.toml +2 -1
- package/README.md +145 -7
- package/dist/Builder.js +63 -80
- package/dist/Builder.spec.js +251 -202
- package/dist/IdentityAssertion.js +8 -18
- package/dist/IdentityAssertion.spec.js +28 -35
- package/dist/Reader.js +13 -28
- package/dist/Reader.spec.js +53 -64
- package/dist/Settings.js +16 -29
- package/dist/Settings.spec.js +47 -62
- package/dist/Signer.js +4 -13
- package/dist/Signer.spec.js +35 -43
- package/dist/Trustmark.js +8 -22
- package/dist/Trustmark.spec.js +50 -65
- package/dist/binary.js +2 -1
- package/dist/index.node +0 -0
- package/dist/types/Builder.d.ts +2 -1
- package/dist/types/types.d.ts +11 -1
- package/package.json +54 -51
- package/scripts/postinstall.cjs +18 -1
- package/src/lib.rs +4 -0
- package/src/neon_builder.rs +30 -6
- package/src/neon_reader.rs +4 -2
- package/src/neon_trustmark.rs +74 -30
- package/src/utils.rs +8 -6
- package/Cargo.lock +0 -5050
- package/LICENSE +0 -21
package/src/neon_trustmark.rs
CHANGED
|
@@ -184,21 +184,27 @@ fn check_strength(strength: f32) -> Result<()> {
|
|
|
184
184
|
Ok(())
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
+
/// Minimum expected size for a valid ONNX model file (10 MB).
|
|
188
|
+
const MIN_MODEL_SIZE: usize = 10 * 1024 * 1024;
|
|
189
|
+
|
|
190
|
+
/// Maximum number of download attempts per model file.
|
|
191
|
+
const MAX_RETRIES: u32 = 3;
|
|
192
|
+
|
|
187
193
|
// Taken from Trustmark xtask.
|
|
188
194
|
// Will not overwrite models if they already exist.
|
|
189
195
|
pub fn fetch_model(variant: Variant, dir_path: &std::path::Path) -> Result<PathBuf> {
|
|
190
196
|
let rt = runtime();
|
|
191
197
|
let client = Client::builder()
|
|
192
|
-
.timeout(Duration::from_secs(
|
|
193
|
-
.connect_timeout(Duration::from_secs(
|
|
194
|
-
.read_timeout(Duration::from_secs(
|
|
198
|
+
.timeout(Duration::from_secs(120))
|
|
199
|
+
.connect_timeout(Duration::from_secs(10))
|
|
200
|
+
.read_timeout(Duration::from_secs(60))
|
|
195
201
|
.build()
|
|
196
202
|
.map_err(|e| Error::ModelDownload(format!("Failed to build HTTP client: {e}")))?;
|
|
197
203
|
|
|
198
204
|
// Ensure directory exists
|
|
199
205
|
std::fs::create_dir_all(dir_path)?;
|
|
200
206
|
|
|
201
|
-
let root = "https://
|
|
207
|
+
let root = "https://cai-watermark.adobe.net/watermarking/trustmark-models";
|
|
202
208
|
|
|
203
209
|
// Construct filenames manually since encoder_filename/decoder_filename are private
|
|
204
210
|
let encoder_filename = format!("encoder_{variant}.onnx");
|
|
@@ -210,32 +216,70 @@ pub fn fetch_model(variant: Variant, dir_path: &std::path::Path) -> Result<PathB
|
|
|
210
216
|
continue;
|
|
211
217
|
}
|
|
212
218
|
let model_url = format!("{root}/{filename}");
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
219
|
+
|
|
220
|
+
let mut last_err = None;
|
|
221
|
+
for attempt in 1..=MAX_RETRIES {
|
|
222
|
+
match download_model(&rt, &client, &model_url) {
|
|
223
|
+
Ok(model_bytes) => {
|
|
224
|
+
if model_bytes.len() < MIN_MODEL_SIZE {
|
|
225
|
+
last_err = Some(Error::ModelDownload(format!(
|
|
226
|
+
"Downloaded model {filename} is too small ({} bytes), expected at least {MIN_MODEL_SIZE} bytes",
|
|
227
|
+
model_bytes.len()
|
|
228
|
+
)));
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Write to a temporary file first, then rename for atomicity.
|
|
233
|
+
let tmp_path = dir_path.join(format!("{filename}.tmp"));
|
|
234
|
+
let mut file = OpenOptions::new()
|
|
235
|
+
.write(true)
|
|
236
|
+
.create(true)
|
|
237
|
+
.truncate(true)
|
|
238
|
+
.open(&tmp_path)
|
|
239
|
+
.map_err(|e| {
|
|
240
|
+
Error::ModelDownload(format!(
|
|
241
|
+
"Failed to create temp file {tmp_path:?}: {e}"
|
|
242
|
+
))
|
|
243
|
+
})?;
|
|
244
|
+
file.write_all(&model_bytes).map_err(|e| {
|
|
245
|
+
Error::ModelDownload(format!("Failed to write model to {tmp_path:?}: {e}"))
|
|
246
|
+
})?;
|
|
247
|
+
std::fs::rename(&tmp_path, &model_path).map_err(|e| {
|
|
248
|
+
Error::ModelDownload(format!(
|
|
249
|
+
"Failed to rename {tmp_path:?} to {model_path:?}: {e}"
|
|
250
|
+
))
|
|
251
|
+
})?;
|
|
252
|
+
|
|
253
|
+
last_err = None;
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
Err(e) => {
|
|
257
|
+
last_err = Some(e);
|
|
258
|
+
if attempt < MAX_RETRIES {
|
|
259
|
+
std::thread::sleep(Duration::from_secs(u64::from(attempt)));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if let Some(err) = last_err {
|
|
266
|
+
return Err(err);
|
|
267
|
+
}
|
|
239
268
|
}
|
|
240
269
|
Ok(dir_path.to_path_buf())
|
|
241
270
|
}
|
|
271
|
+
|
|
272
|
+
fn download_model(rt: &tokio::runtime::Runtime, client: &Client, url: &str) -> Result<Vec<u8>> {
|
|
273
|
+
rt.block_on(async {
|
|
274
|
+
let response = client
|
|
275
|
+
.get(url)
|
|
276
|
+
.send()
|
|
277
|
+
.await
|
|
278
|
+
.map_err(|e| Error::ModelDownload(format!("Failed to GET model: {e}")))?;
|
|
279
|
+
response
|
|
280
|
+
.bytes()
|
|
281
|
+
.await
|
|
282
|
+
.map_err(|e| Error::ModelDownload(format!("Failed to read model bytes: {e}")))
|
|
283
|
+
.map(|b| b.to_vec())
|
|
284
|
+
})
|
|
285
|
+
}
|
package/src/utils.rs
CHANGED
|
@@ -46,25 +46,27 @@ pub fn parse_settings(
|
|
|
46
46
|
Some(js_value) => {
|
|
47
47
|
if js_value.is_a::<JsString, _>(cx) {
|
|
48
48
|
let settings_string = js_value
|
|
49
|
-
.downcast::<JsString, _>(cx)
|
|
49
|
+
.downcast::<JsString, _>(cx)
|
|
50
|
+
.map_err(|_| {
|
|
51
|
+
Error::Signing(format!("{error_prefix}: Expected settings string"))
|
|
52
|
+
})?
|
|
50
53
|
.value(cx);
|
|
51
54
|
|
|
52
55
|
// Create context with settings
|
|
53
56
|
let context = Context::new()
|
|
54
57
|
.with_settings(settings_string.as_str())
|
|
55
|
-
.map_err(|e|
|
|
56
|
-
|
|
58
|
+
.map_err(|e| {
|
|
59
|
+
Error::Signing(format!("{error_prefix}: Invalid settings: {e}"))
|
|
60
|
+
})?;
|
|
57
61
|
Ok(Some(context))
|
|
58
62
|
} else if js_value.is_a::<JsNull, _>(cx) || js_value.is_a::<JsUndefined, _>(cx) {
|
|
59
63
|
Ok(None)
|
|
60
64
|
} else {
|
|
61
65
|
Err(Error::Signing(format!(
|
|
62
|
-
"{}: Settings must be a string, null, or undefined",
|
|
63
|
-
error_prefix
|
|
66
|
+
"{error_prefix}: Settings must be a string, null, or undefined",
|
|
64
67
|
)))
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
None => Ok(None),
|
|
68
71
|
}
|
|
69
72
|
}
|
|
70
|
-
|