@contentauth/c2pa-node 0.9.8 → 0.9.9

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 CHANGED
@@ -35,5 +35,5 @@ toml = "0.8"
35
35
  thiserror = "1.0.61"
36
36
  tokio = { version = "1.43.0", features = ["rt-multi-thread"] }
37
37
  tokio-util = "0.7.13"
38
- trustmark = "0.2.2"
38
+ trustmark = "0.4.0"
39
39
  rand = "0.8.5"
package/dist/Builder.js CHANGED
@@ -200,6 +200,9 @@ export class Builder {
200
200
  updateActions(transform) {
201
201
  getNeonBinary().builderUpdateActions.call(this.builder, transform);
202
202
  }
203
+ updateAssertion(label, transform) {
204
+ getNeonBinary().builderUpdateAssertion.call(this.builder, label, transform);
205
+ }
203
206
  getHandle() {
204
207
  return this.builder;
205
208
  }
@@ -1053,6 +1053,102 @@ describe("Builder", () => {
1053
1053
  throw new Error("action predicate boom");
1054
1054
  }, () => false)).toThrow("action predicate boom");
1055
1055
  });
1056
+ it("updateAssertion updates exact label matches in order and preserves metadata", () => {
1057
+ const builder = Builder.withJson({
1058
+ ...actionsManifest(),
1059
+ assertions: [
1060
+ { label: "org.test.note", data: { value: 1 }, kind: "Cbor" },
1061
+ { label: "org.test.note.extra", data: { value: 9 } },
1062
+ { label: "org.test.note", data: { value: 2 }, kind: "Cbor" },
1063
+ ],
1064
+ });
1065
+ const seen = [];
1066
+ builder.updateAssertion("org.test.note", (data) => {
1067
+ const value = data.value;
1068
+ seen.push(value);
1069
+ return { value: value + 10 };
1070
+ });
1071
+ expect(seen).toEqual([1, 2]);
1072
+ expect(builder.getManifestDefinition().assertions).toEqual([
1073
+ { label: "org.test.note", data: { value: 11 }, kind: "Cbor" },
1074
+ { label: "org.test.note.extra", data: { value: 9 } },
1075
+ { label: "org.test.note", data: { value: 12 }, kind: "Cbor" },
1076
+ ]);
1077
+ });
1078
+ it("updateAssertion preserves JSON and CBOR kinds when signing", async () => {
1079
+ const builder = Builder.withJson({
1080
+ ...actionsManifest(),
1081
+ assertions: [
1082
+ ...actionsManifest().assertions,
1083
+ { label: "org.test.json", data: { value: 1 }, kind: "Json" },
1084
+ { label: "org.test.cbor", data: { value: 2 }, kind: "Cbor" },
1085
+ ],
1086
+ });
1087
+ builder.updateAssertion("org.test.json", () => ({ value: 11 }));
1088
+ builder.updateAssertion("org.test.cbor", () => ({ value: 22 }));
1089
+ expect(builder.getManifestDefinition().assertions).toEqual(expect.arrayContaining([
1090
+ { label: "org.test.json", data: { value: 11 }, kind: "Json" },
1091
+ { label: "org.test.cbor", data: { value: 22 }, kind: "Cbor" },
1092
+ ]));
1093
+ const dest = { path: path.join(tempDir, "updated_assertion_kinds.jpg") };
1094
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
1095
+ builder.sign(signer, source, dest);
1096
+ const reader = await Reader.fromAsset(dest);
1097
+ expect(reader).not.toBeNull();
1098
+ const assertions = reader.getActive().assertions;
1099
+ expect(assertions).toEqual(expect.arrayContaining([
1100
+ expect.objectContaining({
1101
+ label: "org.test.json",
1102
+ data: { value: 11 },
1103
+ }),
1104
+ expect.objectContaining({
1105
+ label: "org.test.cbor",
1106
+ data: { value: 22 },
1107
+ }),
1108
+ ]));
1109
+ });
1110
+ it("updateAssertion skips null and undefined results without dropping other updates", () => {
1111
+ const builder = Builder.withJson({
1112
+ ...actionsManifest(),
1113
+ assertions: [
1114
+ { label: "org.test.note", data: { value: 1 }, kind: "Json" },
1115
+ { label: "org.test.note", data: { value: 2 }, kind: "Json" },
1116
+ { label: "org.test.note", data: { value: 3 }, kind: "Json" },
1117
+ ],
1118
+ });
1119
+ builder.updateAssertion("org.test.note", (data) => {
1120
+ const value = data.value;
1121
+ return value === 1 ? null : value === 2 ? undefined : { value: 33 };
1122
+ });
1123
+ expect(builder.getManifestDefinition().assertions.map((a) => a.data)).toEqual([{ value: 1 }, { value: 2 }, { value: 33 }]);
1124
+ });
1125
+ it("updateAssertion is atomic on callback failure and ignores missing labels", () => {
1126
+ const builder = Builder.withJson({
1127
+ ...actionsManifest(),
1128
+ assertions: [
1129
+ { label: "org.test.note", data: { value: 1 } },
1130
+ { label: "org.test.note", data: { value: 2 } },
1131
+ ],
1132
+ });
1133
+ const before = builder.getManifestDefinition();
1134
+ expect(() => builder.updateAssertion("org.test.note", (data) => {
1135
+ if (data.value === 2) {
1136
+ throw new Error("transform failed");
1137
+ }
1138
+ return { value: 10 };
1139
+ })).toThrow("transform failed");
1140
+ expect(builder.getManifestDefinition()).toEqual(before);
1141
+ expect(() => builder.updateAssertion("org.test.note", (data) => data.value === 2
1142
+ ? () => undefined
1143
+ : { value: 10 })).toThrow();
1144
+ expect(builder.getManifestDefinition()).toEqual(before);
1145
+ let calls = 0;
1146
+ builder.updateAssertion("org.test.missing", () => {
1147
+ calls++;
1148
+ return {};
1149
+ });
1150
+ expect(calls).toBe(0);
1151
+ });
1056
1152
  it("updateActions patches a parameter on an existing action", () => {
1057
1153
  const builder = Builder.withJson(actionsManifest());
1058
1154
  builder.updateActions((actions) => actions.map((action) => action.action === "c2pa.created"
package/dist/index.node CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/dist/index.node
3
- size: 49482104 bytes
4
- sha256: 1fd4eec767e67c0888ae47f36b6ed5dfddeb93562309c39572483506b6b114a6
3
+ size: 52040488 bytes
4
+ sha256: 5f413ef149badf60fce3372fcd57b3d9a11683aaa13e15de7bd24257f171a125
@@ -65,5 +65,6 @@ export declare class Builder implements BuilderInterface {
65
65
  filterIngredients(rescue: (ingredient: Ingredient, provenance: ManifestStore | null) => boolean): void;
66
66
  filterActionsAndIngredients(keepAction: (action: Action) => boolean, rescueIngredient: (ingredient: Ingredient, provenance: ManifestStore | null) => boolean): void;
67
67
  updateActions(transform: (actions: Action[]) => Action[]): void;
68
+ updateAssertion(label: string, transform: (data: unknown) => unknown): void;
68
69
  getHandle(): NeonBuilderHandle;
69
70
  }
@@ -421,6 +421,19 @@ export interface BuilderInterface {
421
421
  */
422
422
  updateActions(transform: (actions: Action[]) => Action[]): void;
423
423
 
424
+ /**
425
+ * Replaces the data of every assertion with an exact matching label.
426
+ * `transform` runs once per matching assertion in manifest order. Other assertion
427
+ * fields and positions are preserved. A missing label is a no-op.
428
+ * Returning null or undefined leaves that assertion unchanged.
429
+ * The replacement data must be JSON-serializable and may fail validation at signing time.
430
+ * The callback must not call back into this builder.
431
+ *
432
+ * @param label The exact assertion label to update.
433
+ * @param transform Receives the current data and returns its replacement.
434
+ */
435
+ updateAssertion(label: string, transform: (data: unknown) => unknown): void;
436
+
424
437
  /**
425
438
  * Get the internal handle for use with Neon bindings
426
439
  */
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "url": "git+https://github.com/contentauth/c2pa-js.git",
6
6
  "directory": "packages/c2pa-node"
7
7
  },
8
- "version": "0.9.8",
8
+ "version": "0.9.9",
9
9
  "description": "Node.js bindings for C2PA",
10
10
  "main": "dist/index.js",
11
11
  "type": "module",
package/src/lib.rs CHANGED
@@ -101,6 +101,10 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
101
101
  "builderUpdateActions",
102
102
  neon_builder::NeonBuilder::update_actions,
103
103
  )?;
104
+ cx.export_function(
105
+ "builderUpdateAssertion",
106
+ neon_builder::NeonBuilder::update_assertion,
107
+ )?;
104
108
 
105
109
  // Reader
106
110
  cx.export_function("readerNew", neon_reader::NeonReader::new)?;
@@ -675,6 +675,53 @@ impl NeonBuilder {
675
675
  Ok(cx.undefined())
676
676
  }
677
677
 
678
+ /// Replaces the data of every assertion with an exact matching label.
679
+ /// A null or undefined result leaves that assertion unchanged.
680
+ /// The builder is left unchanged if any transform throws or returns invalid data.
681
+ /// The callback must not call back into this builder while its lock is held.
682
+ pub fn update_assertion(mut cx: FunctionContext) -> JsResult<JsUndefined> {
683
+ let rt = runtime();
684
+ let this = cx.this::<JsBox<Self>>()?;
685
+ let label = cx.argument::<JsString>(0)?.value(&mut cx);
686
+ let transform = cx.argument::<JsFunction>(1)?;
687
+ let mut builder = rt.block_on(async { this.builder.lock().await });
688
+
689
+ let positions: Vec<usize> = builder
690
+ .definition
691
+ .assertions
692
+ .iter()
693
+ .enumerate()
694
+ .filter(|(_, assertion)| assertion.label == label)
695
+ .map(|(index, _)| index)
696
+ .collect();
697
+
698
+ let mut replacements = Vec::with_capacity(positions.len());
699
+ for &position in &positions {
700
+ let data = serde_json::to_value(&builder.definition.assertions[position].data)
701
+ .or_else(|err| cx.throw_error(err.to_string()))?;
702
+ let js_data = neon_serde4::to_value(&mut cx, &data)
703
+ .or_else(|err| cx.throw_error(err.to_string()))?;
704
+ let undefined = cx.undefined();
705
+ let result = transform.call(&mut cx, undefined, [js_data])?;
706
+ if result.is_a::<JsNull, _>(&mut cx) || result.is_a::<JsUndefined, _>(&mut cx) {
707
+ replacements.push(None);
708
+ continue;
709
+ }
710
+ let replacement: serde_json::Value = neon_serde4::from_value(&mut cx, result)
711
+ .or_else(|err| cx.throw_error(err.to_string()))?;
712
+ let replacement = serde_json::from_value(replacement)
713
+ .or_else(|err| cx.throw_error(err.to_string()))?;
714
+ replacements.push(Some(replacement));
715
+ }
716
+
717
+ for (position, replacement) in positions.into_iter().zip(replacements) {
718
+ if let Some(replacement) = replacement {
719
+ builder.definition.assertions[position].data = replacement;
720
+ }
721
+ }
722
+ Ok(cx.undefined())
723
+ }
724
+
678
725
  /// Replaces the actions in the `c2pa.actions`/`c2pa.actions.v2` assertions.
679
726
  /// `softwareAgents`/`allActionsIncluded`/`templates`/`metadata` are preserved as-is.
680
727
  ///