@contentauth/c2pa-node 0.5.4 → 0.5.5
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 +35 -6
- package/Cargo.toml +1 -1
- package/README.md +142 -0
- package/dist/Builder.js +3 -0
- package/dist/Builder.spec.js +131 -67
- package/dist/Reader.js +2 -1
- package/dist/Reader.spec.js +1 -1
- package/dist/Trustmark.spec.js +0 -4
- 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 +24 -24
- 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
CHANGED
|
@@ -213,7 +213,7 @@ version = "0.4.27"
|
|
|
213
213
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
214
214
|
checksum = "ddb939d66e4ae03cee6091612804ba446b12878410cfa17f785f4dd67d4014e8"
|
|
215
215
|
dependencies = [
|
|
216
|
-
"brotli",
|
|
216
|
+
"brotli 8.0.1",
|
|
217
217
|
"flate2",
|
|
218
218
|
"futures-core",
|
|
219
219
|
"memchr",
|
|
@@ -257,9 +257,9 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
|
|
257
257
|
|
|
258
258
|
[[package]]
|
|
259
259
|
name = "atree"
|
|
260
|
-
version = "0.5.
|
|
260
|
+
version = "0.5.4"
|
|
261
261
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
262
|
-
checksum = "
|
|
262
|
+
checksum = "239d25181cb40f1955529367ee495e35d03aab4e578028f41e7abc8b21c367a8"
|
|
263
263
|
|
|
264
264
|
[[package]]
|
|
265
265
|
name = "autocfg"
|
|
@@ -397,6 +397,17 @@ dependencies = [
|
|
|
397
397
|
"generic-array",
|
|
398
398
|
]
|
|
399
399
|
|
|
400
|
+
[[package]]
|
|
401
|
+
name = "brotli"
|
|
402
|
+
version = "7.0.0"
|
|
403
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
404
|
+
checksum = "cc97b8f16f944bba54f0433f07e30be199b6dc2bd25937444bbad560bcea29bd"
|
|
405
|
+
dependencies = [
|
|
406
|
+
"alloc-no-stdlib",
|
|
407
|
+
"alloc-stdlib",
|
|
408
|
+
"brotli-decompressor 4.0.3",
|
|
409
|
+
]
|
|
410
|
+
|
|
400
411
|
[[package]]
|
|
401
412
|
name = "brotli"
|
|
402
413
|
version = "8.0.1"
|
|
@@ -405,7 +416,17 @@ checksum = "9991eea70ea4f293524138648e41ee89b0b2b12ddef3b255effa43c8056e0e0d"
|
|
|
405
416
|
dependencies = [
|
|
406
417
|
"alloc-no-stdlib",
|
|
407
418
|
"alloc-stdlib",
|
|
408
|
-
"brotli-decompressor",
|
|
419
|
+
"brotli-decompressor 5.0.0",
|
|
420
|
+
]
|
|
421
|
+
|
|
422
|
+
[[package]]
|
|
423
|
+
name = "brotli-decompressor"
|
|
424
|
+
version = "4.0.3"
|
|
425
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
426
|
+
checksum = "a334ef7c9e23abf0ce748e8cd309037da93e606ad52eb372e4ce327a0dcfbdfd"
|
|
427
|
+
dependencies = [
|
|
428
|
+
"alloc-no-stdlib",
|
|
429
|
+
"alloc-stdlib",
|
|
409
430
|
]
|
|
410
431
|
|
|
411
432
|
[[package]]
|
|
@@ -495,9 +516,9 @@ checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
|
|
|
495
516
|
|
|
496
517
|
[[package]]
|
|
497
518
|
name = "c2pa"
|
|
498
|
-
version = "0.
|
|
519
|
+
version = "0.82.1"
|
|
499
520
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
500
|
-
checksum = "
|
|
521
|
+
checksum = "8e96917bbbabdb4dd15030b67e4694b6751d618768fe88ad57f98aad7fd8e751"
|
|
501
522
|
dependencies = [
|
|
502
523
|
"asn1-rs",
|
|
503
524
|
"async-generic",
|
|
@@ -505,6 +526,7 @@ dependencies = [
|
|
|
505
526
|
"atree",
|
|
506
527
|
"base64",
|
|
507
528
|
"bcder",
|
|
529
|
+
"brotli 7.0.0",
|
|
508
530
|
"byteorder",
|
|
509
531
|
"byteordered",
|
|
510
532
|
"bytes",
|
|
@@ -522,6 +544,7 @@ dependencies = [
|
|
|
522
544
|
"extfmt",
|
|
523
545
|
"getrandom 0.2.16",
|
|
524
546
|
"getrandom 0.3.4",
|
|
547
|
+
"glob",
|
|
525
548
|
"hex",
|
|
526
549
|
"hex-literal",
|
|
527
550
|
"http",
|
|
@@ -1426,6 +1449,12 @@ version = "0.31.1"
|
|
|
1426
1449
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1427
1450
|
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
|
1428
1451
|
|
|
1452
|
+
[[package]]
|
|
1453
|
+
name = "glob"
|
|
1454
|
+
version = "0.3.3"
|
|
1455
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1456
|
+
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
|
|
1457
|
+
|
|
1429
1458
|
[[package]]
|
|
1430
1459
|
name = "group"
|
|
1431
1460
|
version = "0.13.0"
|
package/Cargo.toml
CHANGED
|
@@ -11,7 +11,7 @@ crate-type = ["cdylib"]
|
|
|
11
11
|
[dependencies]
|
|
12
12
|
async-trait = "0.1.77"
|
|
13
13
|
ciborium = "0.2.2"
|
|
14
|
-
c2pa = { version = "0.
|
|
14
|
+
c2pa = { version = "0.82.1", default-features = false, features = ["file_io", "pdf", "fetch_remote_manifests", "add_thumbnails", "rust_native_crypto", "default_http"] }
|
|
15
15
|
futures = "0.3"
|
|
16
16
|
image = "0.25.6"
|
|
17
17
|
neon = { version = "1.0.0", default-features = false, features = [
|
package/README.md
CHANGED
|
@@ -45,6 +45,18 @@ This command will download precompiled binaries for the following systems:
|
|
|
45
45
|
- Windows x86
|
|
46
46
|
- Windows ARM
|
|
47
47
|
|
|
48
|
+
## Documentation
|
|
49
|
+
|
|
50
|
+
Complete API documentation is generated from TypeScript source using [TypeDoc](https://typedoc.org/) and published to GitHub Pages at [https://contentauth.github.io/c2pa-node-v2/](https://contentauth.github.io/c2pa-node-v2/).
|
|
51
|
+
|
|
52
|
+
To generate documentation locally:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pnpm build:docs
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
This generates HTML documentation in the `docs/` directory. The `docs/` directory is not checked into version control. It's automatically generated and published to GitHub Pages whenever a new release is published.
|
|
59
|
+
|
|
48
60
|
## Components
|
|
49
61
|
|
|
50
62
|
### Reader
|
|
@@ -241,6 +253,125 @@ const ingredient = builder.addIngredientFromReader(sourceReader);
|
|
|
241
253
|
console.log(ingredient.title); // Contains ingredient metadata
|
|
242
254
|
```
|
|
243
255
|
|
|
256
|
+
#### Adding Ingredients from Archives (.c2pa files)
|
|
257
|
+
|
|
258
|
+
You can add ingredients from `.c2pa` archive files. Archives are binary files that contain a manifest store with ingredients and their associated resources (thumbnails, manifest data, etc.). To work with them, read the archive with `Reader` using the `application/c2pa` MIME type, then extract the ingredients and transfer their binary resources to a new `Builder`.
|
|
259
|
+
|
|
260
|
+
There are two types of archives sharing the same binary format:
|
|
261
|
+
|
|
262
|
+
- **Builder archives** (working store archives): Serialized snapshots of a `Builder`, created by `builder.toArchive()`. They can contain multiple ingredients.
|
|
263
|
+
- **Ingredient archives**: Contain exactly one ingredient from a source asset. They carry the provenance history for reuse in other manifests.
|
|
264
|
+
|
|
265
|
+
##### Reading an archive and adding its ingredients
|
|
266
|
+
|
|
267
|
+
```javascript
|
|
268
|
+
import { Reader, Builder } from '@contentauth/c2pa-node';
|
|
269
|
+
import * as fs from 'node:fs/promises';
|
|
270
|
+
|
|
271
|
+
// Read the archive using the application/c2pa MIME type
|
|
272
|
+
const archiveBuffer = await fs.readFile('ingredients.c2pa');
|
|
273
|
+
const reader = await Reader.fromAsset(
|
|
274
|
+
{ buffer: archiveBuffer, mimeType: 'application/c2pa' },
|
|
275
|
+
{ verify: { verify_after_reading: false } }
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
// Get the ingredients from the active manifest
|
|
279
|
+
const activeManifest = reader.getActive();
|
|
280
|
+
const ingredients = activeManifest.ingredients;
|
|
281
|
+
|
|
282
|
+
// Create a new builder with the ingredients from the archive
|
|
283
|
+
const builder = Builder.withJson({
|
|
284
|
+
claim_generator_info: [{ name: 'my-app', version: '1.0.0' }],
|
|
285
|
+
ingredients: ingredients,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// Transfer binary resources (thumbnails, manifest_data) for each ingredient
|
|
289
|
+
for (const ingredient of ingredients) {
|
|
290
|
+
if (ingredient.thumbnail) {
|
|
291
|
+
const resource = await reader.resourceToAsset(ingredient.thumbnail.identifier, { buffer: null });
|
|
292
|
+
await builder.addResource(ingredient.thumbnail.identifier, {
|
|
293
|
+
buffer: resource.buffer,
|
|
294
|
+
mimeType: ingredient.thumbnail.format,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
if (ingredient.manifest_data) {
|
|
298
|
+
const resource = await reader.resourceToAsset(ingredient.manifest_data.identifier, { buffer: null });
|
|
299
|
+
await builder.addResource(ingredient.manifest_data.identifier, {
|
|
300
|
+
buffer: resource.buffer,
|
|
301
|
+
mimeType: 'application/c2pa',
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Sign the manifest
|
|
307
|
+
const signer = LocalSigner.newSigner(cert, key, 'es256');
|
|
308
|
+
builder.sign(signer, inputAsset, outputAsset);
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
##### Selecting specific ingredients from an archive
|
|
312
|
+
|
|
313
|
+
When an archive contains multiple ingredients, you can filter to include only the ones you need:
|
|
314
|
+
|
|
315
|
+
```javascript
|
|
316
|
+
const reader = await Reader.fromAsset(
|
|
317
|
+
{ buffer: archiveBuffer, mimeType: 'application/c2pa' },
|
|
318
|
+
{ verify: { verify_after_reading: false } }
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const activeManifest = reader.getActive();
|
|
322
|
+
const allIngredients = activeManifest.ingredients;
|
|
323
|
+
|
|
324
|
+
// Select only the ingredients you want (e.g., by title or instance_id)
|
|
325
|
+
const selected = allIngredients.filter(
|
|
326
|
+
(ing) => ing.title === 'photo_1.jpg' || ing.instance_id === 'catalog:logo'
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
// Build with only the selected ingredients
|
|
330
|
+
const builder = Builder.withJson({
|
|
331
|
+
claim_generator_info: [{ name: 'my-app', version: '1.0.0' }],
|
|
332
|
+
ingredients: selected,
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
// Transfer resources only for selected ingredients
|
|
336
|
+
for (const ingredient of selected) {
|
|
337
|
+
if (ingredient.thumbnail) {
|
|
338
|
+
const resource = await reader.resourceToAsset(ingredient.thumbnail.identifier, { buffer: null });
|
|
339
|
+
await builder.addResource(ingredient.thumbnail.identifier, {
|
|
340
|
+
buffer: resource.buffer,
|
|
341
|
+
mimeType: ingredient.thumbnail.format,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
if (ingredient.manifest_data) {
|
|
345
|
+
const resource = await reader.resourceToAsset(ingredient.manifest_data.identifier, { buffer: null });
|
|
346
|
+
await builder.addResource(ingredient.manifest_data.identifier, {
|
|
347
|
+
buffer: resource.buffer,
|
|
348
|
+
mimeType: 'application/c2pa',
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
##### Building an ingredient archive
|
|
355
|
+
|
|
356
|
+
To create an ingredient archive, add ingredients to a `Builder` and save it as an archive:
|
|
357
|
+
|
|
358
|
+
```javascript
|
|
359
|
+
const builder = Builder.new();
|
|
360
|
+
|
|
361
|
+
// Add ingredients with stable instance_id for later catalog lookups
|
|
362
|
+
await builder.addIngredient(
|
|
363
|
+
JSON.stringify({
|
|
364
|
+
title: 'photo-A.jpg',
|
|
365
|
+
relationship: 'componentOf',
|
|
366
|
+
instance_id: 'catalog:photo-A',
|
|
367
|
+
}),
|
|
368
|
+
{ path: 'photo-A.jpg' }
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
// Save as a .c2pa archive
|
|
372
|
+
await builder.toArchive({ path: 'ingredient-catalog.c2pa' });
|
|
373
|
+
```
|
|
374
|
+
|
|
244
375
|
#### Creating and Reusing Builder Archives
|
|
245
376
|
|
|
246
377
|
Builder archives allow you to save a builder's state (including ingredients) and reuse it later:
|
|
@@ -506,3 +637,14 @@ const builder = Builder.new(urlSettings);
|
|
|
506
637
|
- `generate_c2pa_archive` - Whether to generate C2PA archive format
|
|
507
638
|
|
|
508
639
|
**Note:** Settings are passed as per-instance configuration. There are no global settings that affect all Readers and Builders.
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
### Build and use custom binary
|
|
643
|
+
**Build rust binary:**
|
|
644
|
+
```
|
|
645
|
+
npm run build:rust
|
|
646
|
+
```
|
|
647
|
+
**Use custom rust binary:**
|
|
648
|
+
```bash
|
|
649
|
+
export C2PA_LIBRARY_PATH=<path-to-binary-folder>/index.node
|
|
650
|
+
```
|
package/dist/Builder.js
CHANGED
|
@@ -166,6 +166,9 @@ export class Builder {
|
|
|
166
166
|
updateManifestProperty(property, value) {
|
|
167
167
|
getNeonBinary().builderUpdateManifestProperty.call(this.builder, property, value);
|
|
168
168
|
}
|
|
169
|
+
addRedaction(uri, reason) {
|
|
170
|
+
getNeonBinary().builderAddRedaction.call(this.builder, uri, reason);
|
|
171
|
+
}
|
|
169
172
|
getHandle() {
|
|
170
173
|
return this.builder;
|
|
171
174
|
}
|
package/dist/Builder.spec.js
CHANGED
|
@@ -467,60 +467,77 @@ describe("Builder", () => {
|
|
|
467
467
|
customBool: true,
|
|
468
468
|
customObject: {
|
|
469
469
|
nested: "value",
|
|
470
|
-
count: 123
|
|
470
|
+
count: 123,
|
|
471
471
|
},
|
|
472
|
-
customArray: ["item1", "item2", "item3"]
|
|
473
|
-
}
|
|
472
|
+
customArray: ["item1", "item2", "item3"],
|
|
473
|
+
},
|
|
474
474
|
};
|
|
475
475
|
yield builder.addIngredient(JSON.stringify(ingredient));
|
|
476
476
|
const definition = builder.getManifestDefinition();
|
|
477
477
|
expect(definition.ingredients).toHaveLength(1);
|
|
478
478
|
expect(definition.ingredients[0]).toMatchObject(ingredient);
|
|
479
479
|
}));
|
|
480
|
-
it("should
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
// Using a common assertion label that might exist
|
|
489
|
-
const assertionLabel = "stds.schema-org.CreativeWork";
|
|
490
|
-
const redactedUri = `contentauth:urn:uuid:${parentManifestLabel}/c2pa.assertions/${assertionLabel}`;
|
|
491
|
-
// Create a builder with edit intent and redactions
|
|
492
|
-
const redactionManifestDefinition = {
|
|
493
|
-
claim_generator: "test-generator",
|
|
494
|
-
claim_generator_info: [
|
|
495
|
-
{
|
|
496
|
-
name: "c2pa_test",
|
|
497
|
-
version: "1.0.0",
|
|
498
|
-
},
|
|
499
|
-
],
|
|
500
|
-
title: "Test_Redaction_Manifest",
|
|
501
|
-
format: "image/jpeg",
|
|
502
|
-
instance_id: "1234",
|
|
503
|
-
intent: "edit",
|
|
504
|
-
redactions: [redactedUri],
|
|
505
|
-
assertions: [
|
|
506
|
-
{
|
|
507
|
-
label: "org.test.assertion",
|
|
508
|
-
data: {},
|
|
509
|
-
},
|
|
510
|
-
],
|
|
511
|
-
resources: { resources: {} },
|
|
480
|
+
it("should redact a thumbnail from an ingredient manifest", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
481
|
+
var _a;
|
|
482
|
+
const signerConfig = {
|
|
483
|
+
alg: "es256",
|
|
484
|
+
certs: [publicKey],
|
|
485
|
+
reserveSize: 10000,
|
|
486
|
+
tsaUrl: undefined,
|
|
487
|
+
directCoseHandling: false,
|
|
512
488
|
};
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
489
|
+
const testSigner = new TestSigner(privateKey);
|
|
490
|
+
const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
|
|
491
|
+
// Sign source asset with a thumbnail
|
|
492
|
+
const step1Builder = Builder.withJson({
|
|
493
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
494
|
+
title: "Asset With Thumbnail",
|
|
495
|
+
format: "image/jpeg",
|
|
496
|
+
instance_id: "thumb-step1-1234",
|
|
497
|
+
thumbnail: { format: "image/jpeg", identifier: "thumbnail.jpg" },
|
|
498
|
+
});
|
|
499
|
+
yield step1Builder.addResource("thumbnail.jpg", {
|
|
500
|
+
mimeType: "image/jpeg",
|
|
501
|
+
buffer: testThumbnail,
|
|
502
|
+
});
|
|
503
|
+
const step1Dest = { buffer: null };
|
|
504
|
+
yield step1Builder.signAsync(signer, source, step1Dest);
|
|
505
|
+
const step1Asset = {
|
|
506
|
+
buffer: step1Dest.buffer,
|
|
507
|
+
mimeType: "image/jpeg",
|
|
521
508
|
};
|
|
522
|
-
|
|
523
|
-
|
|
509
|
+
// Verify thumbnail exists in original manifest
|
|
510
|
+
const originalReader = yield Reader.fromAsset(step1Asset);
|
|
511
|
+
expect(originalReader).not.toBeNull();
|
|
512
|
+
const parentLabel = originalReader.activeLabel();
|
|
513
|
+
expect(parentLabel).toBeDefined();
|
|
514
|
+
const originalManifest = originalReader.getActive();
|
|
515
|
+
expect(originalManifest === null || originalManifest === void 0 ? void 0 : originalManifest.thumbnail).toBeDefined();
|
|
516
|
+
expect(originalManifest === null || originalManifest === void 0 ? void 0 : originalManifest.thumbnail).not.toBeNull();
|
|
517
|
+
// Redact the thumbnail
|
|
518
|
+
const thumbnailUri = `self#jumbf=/c2pa/${parentLabel}/c2pa.assertions/c2pa.thumbnail.claim`;
|
|
519
|
+
const redactionBuilder = Builder.withJson({
|
|
520
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
521
|
+
title: "Redacted Thumbnail Manifest",
|
|
522
|
+
format: "image/jpeg",
|
|
523
|
+
instance_id: "thumb-step2-1234",
|
|
524
|
+
});
|
|
525
|
+
redactionBuilder.setIntent("update");
|
|
526
|
+
redactionBuilder.addRedaction(thumbnailUri, "c2pa.PII.present");
|
|
527
|
+
const step2Dest = { buffer: null };
|
|
528
|
+
yield redactionBuilder.signAsync(signer, step1Asset, step2Dest);
|
|
529
|
+
const finalReader = yield Reader.fromAsset({
|
|
530
|
+
buffer: step2Dest.buffer,
|
|
531
|
+
mimeType: "image/jpeg",
|
|
532
|
+
});
|
|
533
|
+
expect(finalReader).not.toBeNull();
|
|
534
|
+
const store = finalReader.json();
|
|
535
|
+
const parentManifest = (_a = store.manifests) === null || _a === void 0 ? void 0 : _a[parentLabel];
|
|
536
|
+
expect(parentManifest).toBeDefined();
|
|
537
|
+
expect(parentManifest === null || parentManifest === void 0 ? void 0 : parentManifest.thumbnail).toBeUndefined();
|
|
538
|
+
}));
|
|
539
|
+
it("should redact an assertion from an ingredient manifest", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
540
|
+
var _a, _b;
|
|
524
541
|
const signerConfig = {
|
|
525
542
|
alg: "es256",
|
|
526
543
|
certs: [publicKey],
|
|
@@ -530,31 +547,78 @@ describe("Builder", () => {
|
|
|
530
547
|
};
|
|
531
548
|
const testSigner = new TestSigner(privateKey);
|
|
532
549
|
const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
|
|
533
|
-
// Sign
|
|
534
|
-
const
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
550
|
+
// Sign source asset with multiple distinct assertions
|
|
551
|
+
const piiLabel = "stds.schema-org.CreativeWork";
|
|
552
|
+
const retainedLabel = "org.contentauth.retained";
|
|
553
|
+
const step1Builder = Builder.withJson({
|
|
554
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
555
|
+
title: "Asset With Multiple Assertions",
|
|
556
|
+
format: "image/jpeg",
|
|
557
|
+
instance_id: "assert-step1-1234",
|
|
558
|
+
assertions: [
|
|
559
|
+
{
|
|
560
|
+
label: piiLabel,
|
|
561
|
+
data: {
|
|
562
|
+
"@context": "http://schema.org/",
|
|
563
|
+
"@type": "CreativeWork",
|
|
564
|
+
author: [{ "@type": "Person", name: "John Doe" }],
|
|
565
|
+
},
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
label: retainedLabel,
|
|
569
|
+
data: { keep: true },
|
|
570
|
+
},
|
|
571
|
+
],
|
|
572
|
+
});
|
|
573
|
+
const step1Dest = { buffer: null };
|
|
574
|
+
yield step1Builder.signAsync(signer, source, step1Dest);
|
|
575
|
+
const step1Asset = {
|
|
576
|
+
buffer: step1Dest.buffer,
|
|
540
577
|
mimeType: "image/jpeg",
|
|
578
|
+
};
|
|
579
|
+
// Verify both assertions exist in original manifest
|
|
580
|
+
const originalReader = yield Reader.fromAsset(step1Asset);
|
|
581
|
+
expect(originalReader).not.toBeNull();
|
|
582
|
+
const parentLabel = originalReader.activeLabel();
|
|
583
|
+
expect(parentLabel).toBeDefined();
|
|
584
|
+
const originalStore = originalReader.json();
|
|
585
|
+
const originalLabels = originalStore.manifests[parentLabel].assertions.map((a) => a.label);
|
|
586
|
+
expect(originalLabels).toContain(piiLabel);
|
|
587
|
+
expect(originalLabels).toContain(retainedLabel);
|
|
588
|
+
// Redact only the PII assertion
|
|
589
|
+
const redactionUri = `self#jumbf=/c2pa/${parentLabel}/c2pa.assertions/${piiLabel}`;
|
|
590
|
+
const redactionBuilder = Builder.withJson({
|
|
591
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
592
|
+
title: "Redacted Assertion Manifest",
|
|
593
|
+
format: "image/jpeg",
|
|
594
|
+
instance_id: "assert-step2-1234",
|
|
541
595
|
});
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
expect(
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
596
|
+
redactionBuilder.setIntent("update");
|
|
597
|
+
redactionBuilder.addRedaction(redactionUri, "c2pa.PII.present");
|
|
598
|
+
const step2Dest = { buffer: null };
|
|
599
|
+
yield redactionBuilder.signAsync(signer, step1Asset, step2Dest);
|
|
600
|
+
const finalReader = yield Reader.fromAsset({
|
|
601
|
+
buffer: step2Dest.buffer,
|
|
602
|
+
mimeType: "image/jpeg",
|
|
603
|
+
});
|
|
604
|
+
expect(finalReader).not.toBeNull();
|
|
605
|
+
const store = finalReader.json();
|
|
606
|
+
const parentManifest = (_a = store.manifests) === null || _a === void 0 ? void 0 : _a[parentLabel];
|
|
607
|
+
expect(parentManifest).toBeDefined();
|
|
608
|
+
const assertionLabels = (_b = parentManifest === null || parentManifest === void 0 ? void 0 : parentManifest.assertions) === null || _b === void 0 ? void 0 : _b.map((a) => a.label);
|
|
609
|
+
// PII assertion removed, retained assertion still present
|
|
610
|
+
expect(assertionLabels).not.toContain(piiLabel);
|
|
611
|
+
expect(assertionLabels).toContain(retainedLabel);
|
|
557
612
|
}));
|
|
613
|
+
it("should add redactions via addRedaction method", () => {
|
|
614
|
+
const uri1 = "self#jumbf=/c2pa/test-label/c2pa.assertions/cawg.identity";
|
|
615
|
+
const uri2 = "self#jumbf=/c2pa/test-label/c2pa.assertions/stds.schema-org.CreativeWork";
|
|
616
|
+
const builder = Builder.new();
|
|
617
|
+
builder.addRedaction(uri1, "c2pa.PII.present");
|
|
618
|
+
builder.addRedaction(uri2, "c2pa.PII.present");
|
|
619
|
+
const definition = builder.getManifestDefinition();
|
|
620
|
+
expect(definition.redactions).toEqual([uri1, uri2]);
|
|
621
|
+
});
|
|
558
622
|
it("should test builder remote url", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
559
623
|
// This test mirrors the Rust test_builder_remote_url test
|
|
560
624
|
// Create a simple manifest definition similar to simple_manifest_json()
|
package/dist/Reader.js
CHANGED
|
@@ -52,12 +52,13 @@ export class Reader {
|
|
|
52
52
|
return (_a = manifestStore.active_manifest) !== null && _a !== void 0 ? _a : undefined;
|
|
53
53
|
}
|
|
54
54
|
getActive() {
|
|
55
|
+
var _a;
|
|
55
56
|
const manifestStore = this.json();
|
|
56
57
|
const activeManifest = manifestStore.active_manifest;
|
|
57
58
|
if (!activeManifest) {
|
|
58
59
|
return undefined;
|
|
59
60
|
}
|
|
60
|
-
return manifestStore.manifests[activeManifest];
|
|
61
|
+
return (_a = manifestStore.manifests) === null || _a === void 0 ? void 0 : _a[activeManifest];
|
|
61
62
|
}
|
|
62
63
|
getHandle() {
|
|
63
64
|
return this.reader;
|
package/dist/Reader.spec.js
CHANGED
|
@@ -48,7 +48,7 @@ describe("Reader", () => {
|
|
|
48
48
|
"instance_id": "xmp.iid:813ee422-9736-4cdc-9be6-4e35ed8e41cb",
|
|
49
49
|
"thumbnail": {
|
|
50
50
|
"format": "image/jpeg",
|
|
51
|
-
"identifier": "self#jumbf
|
|
51
|
+
"identifier": "self#jumbf=/c2pa/contentauth:urn:uuid:c2677d4b-0a93-4444-876f-ed2f2d40b8cf/c2pa.assertions/c2pa.thumbnail.ingredient.jpeg"
|
|
52
52
|
},
|
|
53
53
|
"relationship": "parentOf",
|
|
54
54
|
"label": "c2pa.ingredient"
|
package/dist/Trustmark.spec.js
CHANGED
|
@@ -58,10 +58,6 @@ describe("Trustmark", () => {
|
|
|
58
58
|
testImageWidth = imageInfo.width;
|
|
59
59
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
60
60
|
testImageHeight = imageInfo.height;
|
|
61
|
-
}));
|
|
62
|
-
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
-
// Note: In a real test environment, you would need a valid model file
|
|
64
|
-
// For now, we'll mock the trustmark creation or handle the case where model doesn't exist
|
|
65
61
|
trustmark = yield Trustmark.newTrustmark(trustmarkConfig);
|
|
66
62
|
}));
|
|
67
63
|
describe("newTrustmark", () => {
|
package/dist/binary.js
CHANGED
|
@@ -18,7 +18,8 @@ let neon = null;
|
|
|
18
18
|
// Get the binary module (loads synchronously on first access)
|
|
19
19
|
export function getNeonBinary() {
|
|
20
20
|
if (neon === null) {
|
|
21
|
-
|
|
21
|
+
const C2PA_LIBRARY_PATH = process.env.C2PA_LIBRARY_PATH;
|
|
22
|
+
neon = require(C2PA_LIBRARY_PATH !== null && C2PA_LIBRARY_PATH !== void 0 ? C2PA_LIBRARY_PATH : "./index.node");
|
|
22
23
|
}
|
|
23
24
|
return neon;
|
|
24
25
|
}
|
package/dist/index.node
CHANGED
|
Binary file
|
package/dist/types/Builder.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BuilderIntent, Ingredient, Manifest } from "@contentauth/c2pa-types";
|
|
1
|
+
import type { BuilderIntent, C2paReason, Ingredient, Manifest } from "@contentauth/c2pa-types";
|
|
2
2
|
import type { BuilderInterface, C2paSettings, CallbackSignerInterface, ClaimVersion, DestinationAsset, IdentityAssertionSignerInterface, JsCallbackSignerConfig, LocalSignerInterface, ManifestAssertionKind, ReaderInterface, SourceAsset, NeonBuilderHandle } from "./types.d.ts";
|
|
3
3
|
export declare class Builder implements BuilderInterface {
|
|
4
4
|
private builder;
|
|
@@ -21,5 +21,6 @@ export declare class Builder implements BuilderInterface {
|
|
|
21
21
|
signAsync(signer: CallbackSignerInterface | IdentityAssertionSignerInterface, input: SourceAsset, output: DestinationAsset): Promise<Buffer>;
|
|
22
22
|
getManifestDefinition(): Manifest;
|
|
23
23
|
updateManifestProperty(property: string, value: ClaimVersion): void;
|
|
24
|
+
addRedaction(uri: string, reason: C2paReason): void;
|
|
24
25
|
getHandle(): NeonBuilderHandle;
|
|
25
26
|
}
|
package/dist/types/types.d.ts
CHANGED
|
@@ -15,12 +15,13 @@
|
|
|
15
15
|
import { Buffer } from "buffer";
|
|
16
16
|
import type {
|
|
17
17
|
BuilderIntent,
|
|
18
|
+
C2paReason,
|
|
18
19
|
Ingredient,
|
|
19
20
|
Manifest,
|
|
20
21
|
ManifestStore,
|
|
21
22
|
} from "@contentauth/c2pa-types";
|
|
22
23
|
|
|
23
|
-
export type { Ingredient } from "@contentauth/c2pa-types";
|
|
24
|
+
export type { C2paReason, Ingredient } from "@contentauth/c2pa-types";
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* Describes the digital signature algorithms allowed by the C2PA spec
|
|
@@ -332,6 +333,15 @@ export interface BuilderInterface {
|
|
|
332
333
|
*/
|
|
333
334
|
updateManifestProperty(property: string, value: string | ClaimVersion): void;
|
|
334
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Redact an assertion from an ingredient manifest and record the reason.
|
|
338
|
+
* Adds the URI to `definition.redactions` and appends a `c2pa.redacted` action
|
|
339
|
+
* with `parameters.redacted` pointing to the same URI.
|
|
340
|
+
* @param uri JUMBF URI of the assertion to redact (e.g. `self#jumbf=/c2pa/{label}/c2pa.assertions/{name}`)
|
|
341
|
+
* @param reason Why the assertion is being redacted. Use `"c2pa.PII.present"` for PII removal.
|
|
342
|
+
*/
|
|
343
|
+
addRedaction(uri: string, reason: C2paReason): void;
|
|
344
|
+
|
|
335
345
|
/**
|
|
336
346
|
* Get the internal handle for use with Neon bindings
|
|
337
347
|
*/
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"type": "git",
|
|
5
5
|
"url": "git+https://github.com/contentauth/c2pa-node-v2.git"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.5.
|
|
7
|
+
"version": "0.5.5",
|
|
8
8
|
"description": "Node.js bindings for C2PA",
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"type": "module",
|
|
@@ -22,29 +22,29 @@
|
|
|
22
22
|
"author": "Colin Murphy <colmurph@adobe.com>",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@changesets/cli": "^2.
|
|
26
|
-
"@contentauth/c2pa-types": "^0.4.
|
|
27
|
-
"@eslint/js": "^9.
|
|
25
|
+
"@changesets/cli": "^2.31.0",
|
|
26
|
+
"@contentauth/c2pa-types": "^0.4.4",
|
|
27
|
+
"@eslint/js": "^9.39.4",
|
|
28
28
|
"@neon-rs/cli": "0.1.82",
|
|
29
29
|
"@types/cli-progress": "^3.11.6",
|
|
30
|
-
"@types/debug": "^4.1.
|
|
30
|
+
"@types/debug": "^4.1.13",
|
|
31
31
|
"@types/fs-extra": "^11.0.4",
|
|
32
32
|
"@types/jest": "^29.5.14",
|
|
33
|
-
"@types/node": "^22.17
|
|
33
|
+
"@types/node": "^22.19.17",
|
|
34
34
|
"@types/unzipper": "^0.10.11",
|
|
35
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
36
|
-
"@typescript-eslint/parser": "^8.
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^8.59.1",
|
|
36
|
+
"@typescript-eslint/parser": "^8.59.1",
|
|
37
37
|
"@vitest/ui": "^3.2.4",
|
|
38
|
-
"cbor2": "^2.0
|
|
38
|
+
"cbor2": "^2.3.0",
|
|
39
39
|
"cpy-cli": "^5.0.0",
|
|
40
|
-
"eslint": "^9.
|
|
40
|
+
"eslint": "^9.39.4",
|
|
41
41
|
"npm-run-all": "^4.1.5",
|
|
42
|
-
"rimraf": "^6.
|
|
43
|
-
"sharp": "^0.34.
|
|
44
|
-
"ts-jest": "^29.4.
|
|
45
|
-
"typedoc": "^0.28.
|
|
46
|
-
"typedoc-plugin-markdown": "^4.
|
|
47
|
-
"typescript": "^5.9.
|
|
42
|
+
"rimraf": "^6.1.3",
|
|
43
|
+
"sharp": "^0.34.5",
|
|
44
|
+
"ts-jest": "^29.4.9",
|
|
45
|
+
"typedoc": "^0.28.19",
|
|
46
|
+
"typedoc-plugin-markdown": "^4.11.0",
|
|
47
|
+
"typescript": "^5.9.3",
|
|
48
48
|
"vitest": "^3.2.4"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
@@ -55,20 +55,20 @@
|
|
|
55
55
|
},
|
|
56
56
|
"homepage": "https://github.com/contentauth/c2pa-node-v2#readme",
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"cargo-cp-artifact": "^0.1.
|
|
59
|
-
"cli-progress": "^3.
|
|
60
|
-
"debug": "^4.4.
|
|
61
|
-
"fs-extra": "^11.3.
|
|
62
|
-
"mkdirp": "^3.0.
|
|
58
|
+
"cargo-cp-artifact": "^0.1.9",
|
|
59
|
+
"cli-progress": "^3.12.0",
|
|
60
|
+
"debug": "^4.4.3",
|
|
61
|
+
"fs-extra": "^11.3.4",
|
|
62
|
+
"mkdirp": "^3.0.1",
|
|
63
63
|
"node-fetch": "^3.3.2",
|
|
64
64
|
"pkg-dir": "^8.0.0",
|
|
65
|
-
"pretty-bytes": "^6.
|
|
66
|
-
"unzipper": "^0.10.
|
|
65
|
+
"pretty-bytes": "^6.1.1",
|
|
66
|
+
"unzipper": "^0.10.14"
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
69
|
"build": "run-s build:rust build:ts",
|
|
70
70
|
"build:debug": "run-s build:rust-debug build:ts",
|
|
71
|
-
"build:docs": "
|
|
71
|
+
"build:docs": "typedoc",
|
|
72
72
|
"build:rust": "pnpm run build:rust-debug --release",
|
|
73
73
|
"build:rust-debug": "cargo build --message-format=json-render-diagnostics > cargo.log",
|
|
74
74
|
"build:ts": "tsc -b && cpy index.node dist && cpy index.node js-src && cpy --flat js-src/types.d.ts dist/types",
|
package/src/lib.rs
CHANGED
|
@@ -81,6 +81,10 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
|
|
81
81
|
"builderUpdateManifestProperty",
|
|
82
82
|
neon_builder::NeonBuilder::update_manifest_property,
|
|
83
83
|
)?;
|
|
84
|
+
cx.export_function(
|
|
85
|
+
"builderAddRedaction",
|
|
86
|
+
neon_builder::NeonBuilder::add_redaction,
|
|
87
|
+
)?;
|
|
84
88
|
|
|
85
89
|
// Reader
|
|
86
90
|
cx.export_function("readerNew", neon_reader::NeonReader::new)?;
|
package/src/neon_builder.rs
CHANGED
|
@@ -18,7 +18,7 @@ use crate::neon_reader::NeonReader;
|
|
|
18
18
|
use crate::neon_signer::{CallbackSignerConfig, NeonCallbackSigner, NeonLocalSigner};
|
|
19
19
|
use crate::runtime::runtime;
|
|
20
20
|
use crate::utils::parse_settings;
|
|
21
|
-
use c2pa::{Builder, BuilderIntent, Ingredient};
|
|
21
|
+
use c2pa::{assertions::Action, Builder, BuilderIntent, Ingredient};
|
|
22
22
|
use neon::context::Context as NeonContext;
|
|
23
23
|
use neon::prelude::*;
|
|
24
24
|
use neon_serde4;
|
|
@@ -60,7 +60,9 @@ impl NeonBuilder {
|
|
|
60
60
|
.with_definition(json.as_str())
|
|
61
61
|
.or_else(|err| cx.throw_error(err.to_string()))?
|
|
62
62
|
} else {
|
|
63
|
-
Builder::
|
|
63
|
+
Builder::default()
|
|
64
|
+
.with_definition(&json)
|
|
65
|
+
.or_else(|err| cx.throw_error(err.to_string()))?
|
|
64
66
|
};
|
|
65
67
|
|
|
66
68
|
Ok(cx.boxed(Self {
|
|
@@ -73,7 +75,7 @@ impl NeonBuilder {
|
|
|
73
75
|
let this = cx.this::<JsBox<Self>>()?;
|
|
74
76
|
let intent_str = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
75
77
|
let intent: BuilderIntent = serde_json::from_str(&intent_str)
|
|
76
|
-
.or_else(|_| cx.throw_error(format!("Invalid intent: {}"
|
|
78
|
+
.or_else(|_| cx.throw_error(format!("Invalid intent: {intent_str}")))?;
|
|
77
79
|
let mut builder = rt.block_on(async { this.builder.lock().await });
|
|
78
80
|
builder.set_intent(intent);
|
|
79
81
|
Ok(cx.undefined())
|
|
@@ -127,7 +129,7 @@ impl NeonBuilder {
|
|
|
127
129
|
// For Json, expect the assertion as a string (JSON) and parse it
|
|
128
130
|
let assertion_str = cx.argument::<JsString>(1)?.value(&mut cx);
|
|
129
131
|
let assertion: serde_json::Value = serde_json::from_str(&assertion_str)
|
|
130
|
-
.or_else(|err| cx.throw_error(format!("Invalid JSON: {}"
|
|
132
|
+
.or_else(|err| cx.throw_error(format!("Invalid JSON: {err}")))?;
|
|
131
133
|
builder
|
|
132
134
|
.add_assertion(&label, &assertion)
|
|
133
135
|
.or_else(|err| cx.throw_error(err.to_string()))?;
|
|
@@ -170,6 +172,28 @@ impl NeonBuilder {
|
|
|
170
172
|
|
|
171
173
|
Ok(promise)
|
|
172
174
|
}
|
|
175
|
+
|
|
176
|
+
pub fn add_redaction(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
177
|
+
let rt = runtime();
|
|
178
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
179
|
+
let uri = cx.argument::<JsString>(0)?.value(&mut cx);
|
|
180
|
+
let reason = cx.argument::<JsString>(1)?.value(&mut cx);
|
|
181
|
+
let mut builder = rt.block_on(async { this.builder.lock().await });
|
|
182
|
+
builder
|
|
183
|
+
.definition
|
|
184
|
+
.redactions
|
|
185
|
+
.get_or_insert_with(Vec::new)
|
|
186
|
+
.push(uri.clone());
|
|
187
|
+
let action = Action::new("c2pa.redacted")
|
|
188
|
+
.set_reason(reason)
|
|
189
|
+
.set_parameter("redacted".to_owned(), uri)
|
|
190
|
+
.or_else(|err| cx.throw_error(err.to_string()))?;
|
|
191
|
+
builder
|
|
192
|
+
.add_action(action)
|
|
193
|
+
.or_else(|err| cx.throw_error(err.to_string()))?;
|
|
194
|
+
Ok(cx.undefined())
|
|
195
|
+
}
|
|
196
|
+
|
|
173
197
|
pub fn add_ingredient(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|
174
198
|
let rt = runtime();
|
|
175
199
|
let this = cx.this::<JsBox<Self>>()?;
|
|
@@ -254,7 +278,7 @@ impl NeonBuilder {
|
|
|
254
278
|
// Block on acquiring the async mutex lock
|
|
255
279
|
// Settings are automatically applied when runtime() is called
|
|
256
280
|
let rt = runtime();
|
|
257
|
-
let
|
|
281
|
+
let builder = rt.block_on(async { builder.lock().await });
|
|
258
282
|
|
|
259
283
|
dest.write_stream().and_then(|mut dest_stream| {
|
|
260
284
|
builder.to_archive(&mut dest_stream)?;
|
|
@@ -302,7 +326,7 @@ impl NeonBuilder {
|
|
|
302
326
|
let builder = if let Some(context) = context_opt {
|
|
303
327
|
Builder::from_context(context).with_archive(source_stream)?
|
|
304
328
|
} else {
|
|
305
|
-
Builder::
|
|
329
|
+
Builder::default().with_archive(source_stream)?
|
|
306
330
|
};
|
|
307
331
|
Ok(builder)
|
|
308
332
|
})
|
package/src/neon_reader.rs
CHANGED
|
@@ -70,7 +70,7 @@ impl NeonReader {
|
|
|
70
70
|
.with_stream_async(&format, stream)
|
|
71
71
|
.await?
|
|
72
72
|
} else {
|
|
73
|
-
Reader::
|
|
73
|
+
Reader::default().with_stream_async(&format, stream).await?
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
Ok(reader)
|
|
@@ -130,7 +130,9 @@ impl NeonReader {
|
|
|
130
130
|
.with_manifest_data_and_stream_async(&c2pa_data, &format, stream)
|
|
131
131
|
.await?
|
|
132
132
|
} else {
|
|
133
|
-
Reader::
|
|
133
|
+
Reader::default()
|
|
134
|
+
.with_manifest_data_and_stream_async(&c2pa_data, &format, stream)
|
|
135
|
+
.await?
|
|
134
136
|
};
|
|
135
137
|
|
|
136
138
|
Ok(reader)
|
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
|
-
|