@contentauth/c2pa-node 0.3.0 → 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.
- package/Cargo.lock +139 -98
- package/Cargo.toml +1 -1
- package/dist/Builder.js +9 -1
- package/dist/Builder.spec.js +102 -0
- package/dist/IdentityAssertion.spec.js +1 -2
- package/dist/Reader.js +1 -6
- package/dist/Reader.spec.js +25 -4
- package/dist/Settings.js +7 -2
- package/dist/Settings.spec.js +5 -10
- package/dist/Signer.spec.js +2 -2
- package/dist/index.node +0 -0
- package/dist/types/Builder.d.ts +2 -1
- package/dist/types/Reader.d.ts +1 -2
- package/dist/types/Settings.d.ts +5 -0
- package/dist/types/types.d.ts +26 -10
- package/package.json +2 -2
- package/src/lib.rs +8 -4
- package/src/neon_builder.rs +68 -46
- package/src/neon_reader.rs +40 -39
- package/src/settings.rs +24 -7
package/src/neon_builder.rs
CHANGED
|
@@ -16,7 +16,7 @@ use crate::error::{as_js_error, Error};
|
|
|
16
16
|
use crate::neon_identity_assertion_signer::NeonIdentityAssertionSigner;
|
|
17
17
|
use crate::neon_signer::{CallbackSignerConfig, NeonCallbackSigner, NeonLocalSigner};
|
|
18
18
|
use crate::runtime::runtime;
|
|
19
|
-
use c2pa::{Builder, BuilderIntent};
|
|
19
|
+
use c2pa::{Builder, BuilderIntent, Ingredient};
|
|
20
20
|
use neon::prelude::*;
|
|
21
21
|
use neon_serde4;
|
|
22
22
|
use serde::{Deserialize, Serialize};
|
|
@@ -88,6 +88,19 @@ impl NeonBuilder {
|
|
|
88
88
|
Ok(cx.undefined())
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
pub fn add_action(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
92
|
+
let rt = runtime();
|
|
93
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
94
|
+
let action_json = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
95
|
+
let action: c2pa::assertions::Action = serde_json::from_str(&action_json)
|
|
96
|
+
.or_else(|err| cx.throw_error(err.to_string()))?;
|
|
97
|
+
let mut builder = rt.block_on(async { this.builder.lock().await });
|
|
98
|
+
builder
|
|
99
|
+
.add_action(action)
|
|
100
|
+
.or_else(|err| cx.throw_error(err.to_string()))?;
|
|
101
|
+
Ok(cx.undefined())
|
|
102
|
+
}
|
|
103
|
+
|
|
91
104
|
pub fn add_assertion(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
92
105
|
let rt = runtime();
|
|
93
106
|
let this = cx.this::<JsBox<Self>>()?;
|
|
@@ -123,7 +136,6 @@ impl NeonBuilder {
|
|
|
123
136
|
}
|
|
124
137
|
|
|
125
138
|
pub fn add_resource(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
126
|
-
let rt = runtime();
|
|
127
139
|
let this = cx.this::<JsBox<Self>>()?;
|
|
128
140
|
let uri = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
129
141
|
let resource = cx
|
|
@@ -131,27 +143,37 @@ impl NeonBuilder {
|
|
|
131
143
|
.and_then(|obj| parse_asset(&mut cx, obj))?;
|
|
132
144
|
let builder = Arc::clone(&this.builder);
|
|
133
145
|
|
|
134
|
-
let
|
|
135
|
-
|
|
146
|
+
let promise = cx
|
|
147
|
+
.task(move || {
|
|
148
|
+
// Block on acquiring the async mutex lock
|
|
149
|
+
let rt = runtime();
|
|
150
|
+
let mut builder = rt.block_on(async { builder.lock().await });
|
|
136
151
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
deferred.settle_with(&channel, |mut cx| match result {
|
|
152
|
+
resource.into_read_stream().and_then(|mut resource_stream| {
|
|
153
|
+
builder.add_resource(&uri, &mut resource_stream)?;
|
|
154
|
+
Ok(())
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
.promise(move |mut cx, result: Result<(), Error>| match result {
|
|
146
158
|
Ok(_) => Ok(cx.undefined()),
|
|
147
159
|
Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
|
|
148
160
|
});
|
|
149
|
-
});
|
|
150
161
|
|
|
151
162
|
Ok(promise)
|
|
152
163
|
}
|
|
164
|
+
pub fn add_ingredient(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
165
|
+
let rt = runtime();
|
|
166
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
167
|
+
let ingredient_json = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
168
|
+
let ingredient = Ingredient::from_json(&ingredient_json)
|
|
169
|
+
.or_else(|err| cx.throw_error(err.to_string()))?;
|
|
170
|
+
|
|
171
|
+
let mut builder = rt.block_on(async { this.builder.lock().await });
|
|
172
|
+
builder.add_ingredient(ingredient);
|
|
173
|
+
Ok(cx.undefined())
|
|
174
|
+
}
|
|
153
175
|
|
|
154
|
-
pub fn
|
|
176
|
+
pub fn add_ingredient_from_asset(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
155
177
|
let rt = runtime();
|
|
156
178
|
let this = cx.this::<JsBox<Self>>()?;
|
|
157
179
|
let ingredient_json = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
@@ -167,18 +189,24 @@ impl NeonBuilder {
|
|
|
167
189
|
rt.spawn(async move {
|
|
168
190
|
let mut builder = builder.lock().await;
|
|
169
191
|
|
|
170
|
-
let result =
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
192
|
+
let result = async {
|
|
193
|
+
let format = ingredient
|
|
194
|
+
.mime_type()
|
|
195
|
+
.ok_or_else(|| {
|
|
196
|
+
Error::Signing("Ingredient asset must have a mime type".to_string())
|
|
197
|
+
})?
|
|
198
|
+
.to_owned();
|
|
199
|
+
let mut ingredient_stream = ingredient.into_read_stream()?;
|
|
200
|
+
builder
|
|
201
|
+
.add_ingredient_from_stream_async(
|
|
176
202
|
&ingredient_json,
|
|
177
203
|
&format,
|
|
178
204
|
&mut ingredient_stream,
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
|
|
205
|
+
)
|
|
206
|
+
.await?;
|
|
207
|
+
Ok(())
|
|
208
|
+
}
|
|
209
|
+
.await;
|
|
182
210
|
|
|
183
211
|
deferred.settle_with(&channel, move |mut cx| match result {
|
|
184
212
|
Ok(_) => Ok(cx.undefined()),
|
|
@@ -190,53 +218,47 @@ impl NeonBuilder {
|
|
|
190
218
|
}
|
|
191
219
|
|
|
192
220
|
pub fn to_archive(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
193
|
-
let rt = runtime();
|
|
194
221
|
let this = cx.this::<JsBox<Self>>()?;
|
|
195
222
|
let dest = cx
|
|
196
223
|
.argument::<JsObject>(0)
|
|
197
224
|
.and_then(|obj| parse_asset(&mut cx, obj))?;
|
|
198
225
|
let builder = Arc::clone(&this.builder);
|
|
199
226
|
|
|
200
|
-
let
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
let result = dest.write_stream().and_then(|dest_stream| {
|
|
206
|
-
builder.to_archive(dest_stream)?;
|
|
207
|
-
Ok(())
|
|
208
|
-
});
|
|
227
|
+
let promise = cx
|
|
228
|
+
.task(move || {
|
|
229
|
+
// Block on acquiring the async mutex lock
|
|
230
|
+
let rt = runtime();
|
|
231
|
+
let mut builder = rt.block_on(async { builder.lock().await });
|
|
209
232
|
|
|
210
|
-
|
|
233
|
+
dest.write_stream().and_then(|dest_stream| {
|
|
234
|
+
builder.to_archive(dest_stream)?;
|
|
235
|
+
Ok(())
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
.promise(move |mut cx, result: Result<(), Error>| match result {
|
|
211
239
|
Ok(_) => Ok(cx.undefined()),
|
|
212
240
|
Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
|
|
213
241
|
});
|
|
214
|
-
});
|
|
215
242
|
Ok(promise)
|
|
216
243
|
}
|
|
217
244
|
|
|
218
245
|
pub fn from_archive(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
219
|
-
let rt = runtime();
|
|
220
246
|
let source = cx
|
|
221
247
|
.argument::<JsObject>(0)
|
|
222
248
|
.and_then(|obj| parse_asset(&mut cx, obj))?;
|
|
223
249
|
|
|
224
|
-
let
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
rt.spawn(async move {
|
|
228
|
-
let result = source.into_read_stream().and_then(|source_stream| {
|
|
250
|
+
let promise = cx
|
|
251
|
+
.task(move || {
|
|
252
|
+
let source_stream = source.into_read_stream()?;
|
|
229
253
|
let builder = Builder::from_archive(source_stream)?;
|
|
230
254
|
Ok(builder)
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
deferred.settle_with(&channel, move |mut cx| match result {
|
|
255
|
+
})
|
|
256
|
+
.promise(move |mut cx, result: Result<Builder, Error>| match result {
|
|
234
257
|
Ok(builder) => Ok(cx.boxed(Self {
|
|
235
258
|
builder: Arc::new(Mutex::new(builder)),
|
|
236
259
|
})),
|
|
237
260
|
Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
|
|
238
261
|
});
|
|
239
|
-
});
|
|
240
262
|
Ok(promise)
|
|
241
263
|
}
|
|
242
264
|
|
package/src/neon_reader.rs
CHANGED
|
@@ -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::
|
|
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
|
-
|
|
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::
|
|
55
|
+
let reader = Reader::from_stream_async(&format, stream).await?;
|
|
52
56
|
Ok(reader)
|
|
53
|
-
}
|
|
54
|
-
.
|
|
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) =>
|
|
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
|
-
|
|
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
|
|
103
|
+
let stream = asset.into_read_stream()?;
|
|
82
104
|
let reader =
|
|
83
|
-
Reader::
|
|
105
|
+
Reader::from_manifest_data_and_stream_async(&c2pa_data, &format, stream)
|
|
106
|
+
.await?;
|
|
84
107
|
Ok(reader)
|
|
85
|
-
}
|
|
86
|
-
.
|
|
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
|
}
|
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(
|
|
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
|
+
}
|