@contentauth/c2pa-node 0.2.3 → 0.4.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.
@@ -12,9 +12,9 @@
12
12
  // each license.
13
13
 
14
14
  use crate::asset::parse_asset;
15
- use crate::error::{as_js_error, Error};
15
+ use crate::error::{as_js_error, Error, Result};
16
16
  use crate::runtime::runtime;
17
- use c2pa::{identity::validator::CawgValidator, Reader};
17
+ use c2pa::Reader;
18
18
  use neon::prelude::*;
19
19
  use neon::types::buffer::TypedArray;
20
20
  use std::sync::Arc;
@@ -35,55 +35,80 @@ impl NeonReader {
35
35
  }
36
36
 
37
37
  pub fn from_stream(mut cx: FunctionContext) -> JsResult<JsPromise> {
38
+ let rt = runtime();
39
+ let channel = cx.channel();
38
40
  let source = cx
39
41
  .argument::<JsObject>(0)
40
42
  .and_then(|obj| parse_asset(&mut cx, obj))?;
41
43
 
42
- let promise = cx
43
- .task(move || {
44
+ let (deferred, promise) = cx.promise();
45
+ rt.spawn(async move {
46
+ let result: Result<Reader> = async {
44
47
  let format = source
45
48
  .mime_type()
46
49
  .ok_or_else(|| {
47
50
  Error::Signing("Ingredient asset must have a mime type".to_string())
48
51
  })?
49
52
  .to_owned();
53
+
50
54
  let stream = source.into_read_stream()?;
51
- let reader = Reader::from_stream(&format, stream)?;
55
+ let reader = Reader::from_stream_async(&format, stream).await?;
52
56
  Ok(reader)
53
- })
54
- .promise(move |mut cx, result: Result<Reader, Error>| match result {
57
+ }
58
+ .await;
59
+
60
+ deferred.settle_with(&channel, move |mut cx| match result {
55
61
  Ok(reader) => {
56
62
  let boxed_reader = cx.boxed(Self {
57
63
  reader: Arc::new(Mutex::new(reader)),
58
64
  });
59
65
  Ok(boxed_reader.upcast::<JsValue>())
60
66
  }
61
- Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
67
+ Err(err) => {
68
+ // Check if the error is due to missing C2PA data
69
+ // Return null instead of throwing for these specific cases
70
+ match &err {
71
+ Error::C2pa(c2pa_err) => match c2pa_err {
72
+ c2pa::Error::JumbfNotFound => {
73
+ Ok(cx.null().upcast::<JsValue>())
74
+ }
75
+ _ => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
76
+ },
77
+ _ => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
78
+ }
79
+ }
62
80
  });
81
+ });
63
82
  Ok(promise)
64
83
  }
65
84
 
66
85
  pub fn from_manifest_data_and_asset(mut cx: FunctionContext) -> JsResult<JsPromise> {
86
+ let rt = runtime();
87
+ let channel = cx.channel();
67
88
  let manifest_data = cx.argument::<JsBuffer>(0)?;
68
89
  let asset = cx
69
90
  .argument::<JsObject>(1)
70
91
  .and_then(|obj| parse_asset(&mut cx, obj))?;
71
92
 
72
93
  let c2pa_data = manifest_data.as_slice(&cx).to_vec();
73
- let promise = cx
74
- .task(move || {
94
+ let (deferred, promise) = cx.promise();
95
+ rt.spawn(async move {
96
+ let result = async {
75
97
  let format = asset
76
98
  .mime_type()
77
99
  .ok_or_else(|| {
78
100
  Error::Signing("Ingredient asset must have a mime type".to_string())
79
101
  })?
80
102
  .to_owned();
81
- let mut stream = asset.into_read_stream()?;
103
+ let stream = asset.into_read_stream()?;
82
104
  let reader =
83
- Reader::from_manifest_data_and_stream(&c2pa_data, &format, &mut stream)?;
105
+ Reader::from_manifest_data_and_stream_async(&c2pa_data, &format, stream)
106
+ .await?;
84
107
  Ok(reader)
85
- })
86
- .promise(move |mut cx, result: Result<Reader, Error>| match result {
108
+ }
109
+ .await;
110
+
111
+ deferred.settle_with(&channel, move |mut cx| match result {
87
112
  Ok(reader) => {
88
113
  let boxed_reader = cx.boxed(Self {
89
114
  reader: Arc::new(Mutex::new(reader)),
@@ -92,7 +117,7 @@ impl NeonReader {
92
117
  }
93
118
  Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
94
119
  });
95
-
120
+ });
96
121
  Ok(promise)
97
122
  }
98
123
 
@@ -170,28 +195,4 @@ impl NeonReader {
170
195
  });
171
196
  Ok(promise)
172
197
  }
173
-
174
- pub fn post_validate_cawg(mut cx: FunctionContext) -> JsResult<JsPromise> {
175
- let rt = runtime();
176
- let this = cx.this::<JsBox<Self>>()?;
177
- let reader = Arc::clone(&this.reader);
178
-
179
- let channel = cx.channel();
180
- let (deferred, promise) = cx.promise();
181
-
182
- rt.spawn(async move {
183
- let result = reader
184
- .lock()
185
- .await
186
- .post_validate_async(&CawgValidator {})
187
- .await
188
- .map_err(Error::from);
189
-
190
- deferred.settle_with(&channel, move |mut cx| match result {
191
- Ok(_) => Ok(cx.undefined()),
192
- Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
193
- });
194
- });
195
- Ok(promise)
196
- }
197
198
  }
@@ -201,6 +201,11 @@ impl NeonCallbackSigner {
201
201
  }
202
202
  }
203
203
 
204
+ pub fn direct_cose_handling(mut cx: FunctionContext) -> JsResult<JsBoolean> {
205
+ let this = cx.this::<JsBox<Self>>()?;
206
+ Ok(cx.boolean(this.config.direct_cose_handling))
207
+ }
208
+
204
209
  pub fn sign(mut cx: FunctionContext) -> JsResult<JsPromise> {
205
210
  let rt = runtime();
206
211
  let this = cx.this::<JsBox<Self>>()?;
@@ -321,12 +326,12 @@ impl AsyncSigner for NeonCallbackSigner {
321
326
  }
322
327
 
323
328
  fn direct_cose_handling(&self) -> bool {
324
- // TODO: The direct_cose_handling field is currently not used in the signing logic.
325
- // The field is read from config and exposed via this getter, but the actual signing
326
- // implementation does not differentiate between directCoseHandling: true/false.
327
- // Both cases pass the same data to the JavaScript callback regardless of this setting.
328
329
  self.config.direct_cose_handling
329
330
  }
331
+
332
+ fn async_raw_signer(&self) -> Option<Box<&dyn AsyncRawSigner>> {
333
+ Some(Box::new(self))
334
+ }
330
335
  }
331
336
 
332
337
  impl TimeStampProvider for NeonCallbackSigner {
package/src/settings.rs CHANGED
@@ -356,7 +356,6 @@ pub fn load_verify_config(mut cx: FunctionContext) -> JsResult<JsUndefined> {
356
356
  "verify_timestamp_trust": true,
357
357
  "ocsp_fetch": false,
358
358
  "remote_manifest_fetch": true,
359
- "check_ingredient_trust": true,
360
359
  "skip_ingredient_conflict_resolution": false,
361
360
  "strict_v1_validation": false
362
361
  },
@@ -399,11 +398,11 @@ pub fn load_verify_config(mut cx: FunctionContext) -> JsResult<JsUndefined> {
399
398
  if let Some(val) = verify_config.get("remote_manifest_fetch") {
400
399
  obj.insert("remote_manifest_fetch".to_string(), val.clone());
401
400
  }
402
- if let Some(val) = verify_config.get("check_ingredient_trust") {
403
- obj.insert("check_ingredient_trust".to_string(), val.clone());
404
- }
405
401
  if let Some(val) = verify_config.get("skip_ingredient_conflict_resolution") {
406
- obj.insert("skip_ingredient_conflict_resolution".to_string(), val.clone());
402
+ obj.insert(
403
+ "skip_ingredient_conflict_resolution".to_string(),
404
+ val.clone(),
405
+ );
407
406
  }
408
407
  if let Some(val) = verify_config.get("strict_v1_validation") {
409
408
  obj.insert("strict_v1_validation".to_string(), val.clone());
@@ -454,7 +453,6 @@ pub fn get_verify_config(mut cx: FunctionContext) -> JsResult<JsValue> {
454
453
  "verify_timestamp_trust": true,
455
454
  "ocsp_fetch": false,
456
455
  "remote_manifest_fetch": true,
457
- "check_ingredient_trust": true,
458
456
  "skip_ingredient_conflict_resolution": false,
459
457
  "strict_v1_validation": false
460
458
  });
@@ -477,7 +475,6 @@ pub fn get_verify_config(mut cx: FunctionContext) -> JsResult<JsValue> {
477
475
  "verify_timestamp_trust": true,
478
476
  "ocsp_fetch": false,
479
477
  "remote_manifest_fetch": true,
480
- "check_ingredient_trust": true,
481
478
  "skip_ingredient_conflict_resolution": false,
482
479
  "strict_v1_validation": false
483
480
  });
@@ -485,3 +482,23 @@ pub fn get_verify_config(mut cx: FunctionContext) -> JsResult<JsValue> {
485
482
  }
486
483
  }
487
484
  }
485
+
486
+ /// Reset settings to their default values.
487
+ /// This replicates the private reset() function from c2pa-rs Settings.
488
+ pub fn reset_settings(mut cx: FunctionContext) -> JsResult<JsUndefined> {
489
+ // Create default settings and get their TOML representation
490
+ let default_settings = Settings::default();
491
+ let default_toml = toml::to_string(&default_settings)
492
+ .or_else(|e| cx.throw_error(format!("Failed to serialize default settings: {}", e)))?;
493
+
494
+ // Apply the default settings
495
+ match Settings::from_toml(&default_toml) {
496
+ Ok(_) => {
497
+ // Store the default TOML for worker threads
498
+ set_global_settings_toml(Some(default_toml));
499
+ reload_runtime();
500
+ Ok(cx.undefined())
501
+ }
502
+ Err(e) => cx.throw_error(format!("Failed to apply default settings: {}", e)),
503
+ }
504
+ }