feat(c2pa-web): Use Context to configure Readers/Builders - #202
feat(c2pa-web): Use Context to configure Readers/Builders#202ale-adobe wants to merge 16 commits into
Conversation
…ttings createC2pa's Config gains context?: Context, attached once at SDK-creation time and shared by every Reader/Builder the SDK subsequently creates. Settings are resolved to JSON exactly once per SDK instance instead of on every single Reader/Builder call. Config.settings and per-call settings overrides on reader.fromBlob/builder.new/etc. are deprecated but still fully functional, merging over the base Context via mergeSettings(), to avoid breaking existing callers before a major version bump. Also removes c2pa-wasm's dead thread-local settings path (loadSettings/Settings::from_string): nothing ever read the thread-local state it wrote, since every Reader/Builder already built its own explicit per-call Context. The one caller (c2pa-web's worker init) is removed along with it. Builds on the Context introduced for c2pa-utilities in refactor/context-settings-utilities. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: aa2a181 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…entauth/c2pa-js into refactor/context-settings-web
…entauth/c2pa-js into refactor/context-settings-web
…entauth/c2pa-js into refactor/context-settings-web
| /** | ||
| * @param worker - Worker (via WorkerManager) to be associated with this reader factory. | ||
| * @param settings - Optional settings to be used for all builders. | ||
| * @deprecated Use `Builder.new`/`Builder.fromDefinition`/`Builder.fromArchive` with a `Context` |
There was a problem hiding this comment.
Existing BuilderFactory is still here, but now marked as deprecated.
| } | ||
|
|
||
| /** | ||
| * @deprecated Use `Reader.fromBlob`/`Reader.fromBlobFragment` with a `Context` instead. |
There was a problem hiding this comment.
Existing ReaderFactory is still here, but now marked as deprecated.
…entauth/c2pa-js into refactor/context-settings-web
| /** | ||
| * NOTE: we can only return Err(JsString) or Err(JsValue) as error types here, because for some as-of-yet unknown | ||
| * reason, wasm-bindgen appears to mishandle JsErrors when created in a Firefox web worker. | ||
| * | ||
| * See: https://github.com/wasm-bindgen/wasm-bindgen/issues/4961 | ||
| */ | ||
|
|
||
| /// Accepts a JSON-serialized string to be loaded as c2pa-rs settings. | ||
| #[wasm_bindgen(js_name = loadSettings)] | ||
| pub fn load_settings(settings: &str) -> Result<(), JsString> { | ||
| c2pa::settings::Settings::from_string(settings, "json").map_err(WasmError::other)?; | ||
|
|
||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
Gone, since we now pass settings through Context.
| @@ -7,7 +7,7 @@ | |||
| * it. | |||
There was a problem hiding this comment.
The diff in this file is a bit scary, but not much has actually changed at a high level.
- Helper functions are pulled out to the top-level as free functions.
- Functions that used to be in the
createBuilderfactory are now static functions on theBuilderclass. - The old
BuilderFactoryis maintained at the bottom, but marked deprecated. - New: Functions for constructing a new
Builderaccept theC2paobject and the optionalContext.
There was a problem hiding this comment.
Can we summarize the prose a little bit maybe, to make it less scary?
There was a problem hiding this comment.
I can try to trim some of the docs and comments, but most of that was existing from previous AI compliance work that Colin did.
| @@ -10,88 +10,164 @@ | |||
| import { Manifest, ManifestStore } from '@contentauth/c2pa-types'; | |||
There was a problem hiding this comment.
The diff in this file is a bit scary, but not much has actually changed at a high level.
- Helper functions are pulled out to the top-level as free functions.
- Functions that used to be in the
createReaderfactory are now static functions on theReaderclass. - The old
ReaderFactoryis maintained at the bottom, but marked deprecated. - New: Functions for constructing a new
Readeraccept theC2paobject and the optionalContext.
| builder: createBuilderFactory(worker, settings), | ||
| const c2pa: C2pa = { | ||
| worker, | ||
| reader: undefined as unknown as ReaderFactory, |
There was a problem hiding this comment.
A workaround to satisfy the shape of the object before creating the factories below, since we are still trying to keep around the factories as deprecated paths to avoid breaking changes.
tmathern
left a comment
There was a problem hiding this comment.
Double-check with the native SDK, but I think you can simplify the Reader format check.
| */ | ||
|
|
||
| export type * from './lib/c2pa.js'; | ||
| export type { Config, C2pa } from './lib/c2pa.js'; |
There was a problem hiding this comment.
export changes may make this a breaking change if exposed types are different
There was a problem hiding this comment.
Yes, correct, alongside the rename to just C2pa.
| // Define browser-to-worker RPC interface | ||
| const { createTx, rx } = channel<{ | ||
| initWorker: (module: WebAssembly.Module, settings?: string) => void; | ||
| initWorker: (module: WebAssembly.Module) => void; |
There was a problem hiding this comment.
Yes. Changesets are marked as minor for the breaking change, but willing to make this the major version bump to go to 1.0.
| @@ -7,7 +7,7 @@ | |||
| * it. | |||
There was a problem hiding this comment.
Can we summarize the prose a little bit maybe, to make it less scary?
| */ | ||
| new: (settings?: Settings) => Promise<Builder>; | ||
| static async new(c2pa: C2pa, context: Context = new Context()): Promise<Builder> { | ||
| const settingsJson = await context.toJson(); |
There was a problem hiding this comment.
Can a context be returned as null if there was an error in context.toJson?
There was a problem hiding this comment.
Not anymore, since I made some changes in #201. It will either resolve into a string or throw if something goes wrong. If an error is thrown, that error will propagate through the constructor here.
| ) => Promise<Reader | null>; | ||
| context: Context = new Context() | ||
| ): Promise<Reader | null> { | ||
| if (!isSupportedReaderFormat(format)) { |
There was a problem hiding this comment.
Is this still needed, now that the Rust SDK attempts auto-detect?
There was a problem hiding this comment.
Maybe not. But I think that's out-of-scope for this PR, so let me revisit that separately.
(I'm inclined to believe that you're right, since this is something we flagged in my proposal originally. Node doesn't do this, and Rust is doing the heavy lifting there as far as I can tell. So I don't see why Web shouldn't do the same thing.)
Regardless, filed https://jira.corp.adobe.com/browse/CAI-13611 this in the epic for me to follow up on later.
…entauth/c2pa-js into refactor/context-settings-web
Summary
This PR brings in the
Contextclass introduced inc2pa-utilities(see #201) inc2pa-web, replacing the SDK-wideSettingsobject and theReader/Builderfactory pattern with static constructors and per-instanceContextobjects. This mirrors howc2pa-nodeworks (see #204).In addition, we remove the dead thread-local
Settingspath usingloadSettingsfromc2pa-wasm, sinceSettingsare now passed explicitly via theContextobject.Changes
C2paclass (c2pa.ts)C2paSdkto justC2pa.C2paclass: now functions as a way to start up a web worker instance and load the Wasm binary. The returned handle fromcreateC2pa()is passed in directly when creating aReaderorBuilder.Reader(reader.ts) andBuilder(builder.ts)ReaderFactory/createReaderFactoryandBuilderFactory/createBuilderFactorypatterns, but still retain them for now in order to not introduce breaking changes just yet.ReaderandBuilderfor constructing new instances, which accept theC2pahandle above, plus an optionalContextto configure their respective behaviors. This is the new, recommended way to create them.New example usage:
Thread-local
Settingsc2pa-wasm's dead thread-local settings path (loadSettings/Settings::from_string). The one caller (c2pa-web's worker init) is removed along with it.