@contentauth/c2pa-web 0.14.6 → 0.15.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/README.md CHANGED
@@ -57,6 +57,57 @@ import { createC2pa } from '@contentauth/c2pa-web/inline';
57
57
  const c2pa = await createC2pa();
58
58
  ```
59
59
 
60
+ ## Configuring behavior with `Context`
61
+
62
+ `createC2pa` sets up the worker and Wasm binary; `Reader` and `Builder`'s creation methods each accept an optional `Context` (see [`c2pa-utilities`'s README](../c2pa-utilities/README.md#context-and-settings) for the full API) directly, so the same running worker can create as many `Reader`s/`Builder`s as needed, each configured independently:
63
+
64
+ ```typescript
65
+ import { createC2pa, Reader, Builder, Context } from '@contentauth/c2pa-web';
66
+
67
+ import wasmSrc from '@contentauth/c2pa-web/resources/c2pa.wasm?url';
68
+
69
+ const c2pa = await createC2pa({ wasmSrc });
70
+
71
+ const context = new Context({
72
+ verify: {
73
+ verifyTrust: true
74
+ },
75
+ trust: {
76
+ trustAnchors: 'https://example.com/trust-anchors.pem'
77
+ }
78
+ });
79
+
80
+ const reader = await Reader.fromBlob(c2pa, blob.type, blob, context);
81
+ const builder = await Builder.new(c2pa, context);
82
+
83
+ // A second Reader can use a completely different Context on the same c2pa instance.
84
+ // No need to call createC2pa() again just to change settings.
85
+ const reader2 = await Reader.fromBlob(
86
+ c2pa,
87
+ blob2.type,
88
+ blob2,
89
+ new Context({ verify: { verifyTrust: false } })
90
+ );
91
+ ```
92
+
93
+ Each `Context` holds whatever single `Settings` object was passed to its constructor and does not handle any merging of settings. To combine more than one `Settings` source, merge them first with `mergeSettings()`, then construct a `Context` from the single, merged result. See [`c2pa-utilities`'s README](../c2pa-utilities/README.md#combining-settings) for details.
94
+
95
+ A `Context`'s settings are resolved once upon creation and then memoized for re-use. Passing the same `Context` instance to multiple `Reader`/`Builder` calls doesn't re-fetch trust-anchor URLs on every call.
96
+
97
+ ### Factory API (deprecated)
98
+
99
+ Earlier versions of `c2pa-web` configured the SDK once via `createC2pa({ settings })`, and created `Reader`/`Builder` objects through `c2pa.reader`/`c2pa.builder` factory methods that took a raw `Settings` object per call, merged over those top-level settings. This is still supported, but deprecated in favor of `Reader`/`Builder`'s static methods with an explicit `Context`, shown above, and will be removed in a future version:
100
+
101
+ ```typescript
102
+ const c2pa = await createC2pa({ wasmSrc, settings: { verify: { verifyTrust: true } } });
103
+
104
+ // Settings passed here are merged over, and take precedence over, the top-level settings above.
105
+ const reader = await c2pa.reader.fromBlob(blob.type, blob, {
106
+ trust: { trustAnchors: 'https://example.com/trust-anchors.pem' }
107
+ });
108
+ const builder = await c2pa.builder.new();
109
+ ```
110
+
60
111
  ## Using the library
61
112
 
62
113
  See also the [API reference documentation](https://contentauth.github.io/c2pa-js/modules/_contentauth_c2pa-web.html).
@@ -66,12 +117,14 @@ See also the [API reference documentation](https://contentauth.github.io/c2pa-js
66
117
  Fetch an image and provide it to the `Reader`:
67
118
 
68
119
  ```typescript
120
+ import { Reader } from '@contentauth/c2pa-web';
121
+
69
122
  const response = await fetch(
70
123
  'https://spec.c2pa.org/public-testfiles/image/jpeg/adobe-20220124-C.jpg'
71
124
  );
72
125
  const blob = await response.blob();
73
126
 
74
- const reader = await c2pa.reader.fromBlob(blob.type, blob);
127
+ const reader = await Reader.fromBlob(c2pa, blob.type, blob);
75
128
 
76
129
  const manifestStore = await reader.manifestStore();
77
130
 
@@ -96,7 +149,9 @@ The builder intent describes the type of operation being performed on the asset.
96
149
  - `update`: A restricted version of `edit` for non-editorial changes. There must be only one ingredient, as a parent. No changes can be made to the hashed content of the parent. There are additional restrictions on the types of changes that can be made.
97
150
 
98
151
  ```typescript
99
- const builder = await c2pa.builder.new();
152
+ import { Builder } from '@contentauth/c2pa-web';
153
+
154
+ const builder = await Builder.new(c2pa);
100
155
 
101
156
  await builder.setIntent({
102
157
  create:
@@ -128,7 +183,7 @@ const ingredientResponse = await fetch('path/to/source-image.jpg');
128
183
  const ingredientBlob = await ingredientResponse.blob();
129
184
 
130
185
  // Create a builder
131
- const builder = await c2pa.builder.new();
186
+ const builder = await Builder.new(c2pa);
132
187
 
133
188
  // Add the ingredient with its blob
134
189
  await builder.addIngredientFromBlob(
@@ -153,7 +208,7 @@ await builder.free();
153
208
  You can add multiple ingredients to document complex provenance chains:
154
209
 
155
210
  ```typescript
156
- const builder = await c2pa.builder.new();
211
+ const builder = await Builder.new(c2pa);
157
212
 
158
213
  // Fetch ingredient assets
159
214
  const background = await fetch('background.jpg').then((r) => r.blob());
@@ -195,7 +250,7 @@ Use a builder archive to save a builder's state (including ingredients) and reus
195
250
 
196
251
  ```typescript
197
252
  // Create a builder with ingredients
198
- const builder = await c2pa.builder.new();
253
+ const builder = await Builder.new(c2pa);
199
254
 
200
255
  const ingredientBlob = await fetch('source.jpg').then((r) => r.blob());
201
256
  await builder.addIngredientFromBlob(
@@ -213,7 +268,7 @@ const archive = await builder.toArchive();
213
268
  await builder.free();
214
269
 
215
270
  // Later, recreate the builder from the archive
216
- const restoredBuilder = await c2pa.builder.fromArchive(new Blob([archive]));
271
+ const restoredBuilder = await Builder.fromArchive(c2pa, new Blob([archive]));
217
272
 
218
273
  // The ingredients are preserved
219
274
  const definition = await restoredBuilder.getDefinition();
@@ -228,12 +283,12 @@ A `.c2pa` archive (created via `toArchive`) can be added as an ingredient to ano
228
283
 
229
284
  ```typescript
230
285
  // Step 1: Create a builder and archive it
231
- const originalBuilder = await c2pa.builder.new();
286
+ const originalBuilder = await Builder.new(c2pa);
232
287
  const archive = await originalBuilder.toArchive();
233
288
  await originalBuilder.free();
234
289
 
235
290
  // Step 2: Create a new builder and add the archive as an ingredient
236
- const newBuilder = await c2pa.builder.new();
291
+ const newBuilder = await Builder.new(c2pa);
237
292
  await newBuilder.setIntent('edit');
238
293
 
239
294
  await newBuilder.addIngredientFromBlob(
@@ -259,7 +314,7 @@ You can also add a `.c2pa` file loaded from a URL or file input:
259
314
  const response = await fetch('path/to/ingredient.c2pa');
260
315
  const archiveBlob = await response.blob();
261
316
 
262
- const builder = await c2pa.builder.new();
317
+ const builder = await Builder.new(c2pa);
263
318
  await builder.setIntent('edit');
264
319
 
265
320
  await builder.addIngredientFromBlob(