@contentauth/c2pa-node 0.4.2 → 0.5.1

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/dist/Settings.js CHANGED
@@ -19,190 +19,112 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19
19
  // specific language governing permissions and limitations under
20
20
  // each license.
21
21
  import * as fs from "fs-extra";
22
- import path from "path";
23
22
  import fetch from "node-fetch";
24
- import { getNeonBinary } from "./binary.js";
25
23
  /**
26
- * Load settings from a JSON string and apply them globally.
27
- * @param json The JSON string containing the settings configuration
24
+ * Create a Settings object with trust configuration.
25
+ * @param trustConfig The trust configuration
26
+ * @returns Settings object that can be passed to Reader/Builder
28
27
  */
29
- export function loadC2paSettings(json) {
30
- getNeonBinary().loadSettings(json);
31
- }
32
- /**
33
- * Load settings from a TOML string and apply them globally.
34
- * @param toml The TOML string containing the settings configuration
35
- */
36
- export function loadC2paSettingsToml(toml) {
37
- getNeonBinary().loadSettingsToml(toml);
38
- }
39
- /**
40
- * Load settings from a TOML or JSON file path and apply them globally.
41
- * The file format is determined by the file extension (.toml for TOML, otherwise JSON).
42
- * @param filePath The path to the settings file
43
- */
44
- export function loadSettingsFromFile(filePath) {
45
- return __awaiter(this, void 0, void 0, function* () {
46
- const ext = path.extname(filePath).toLowerCase();
47
- const content = yield fs.readFile(filePath, "utf8");
48
- if (ext === ".toml") {
49
- loadC2paSettingsToml(content);
50
- return;
51
- }
52
- // Assume JSON (or JSON5 that is valid JSON) otherwise
53
- // If needed, callers can ensure the content is valid JSON.
54
- loadC2paSettings(content);
55
- });
56
- }
57
- /**
58
- * Load settings from a URL and apply them globally.
59
- * The format is determined by the content-type header or file extension (.toml for TOML, otherwise JSON).
60
- * @param url The URL to fetch the settings from
61
- */
62
- export function loadSettingsFromUrl(url) {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- const res = yield fetch(url);
65
- if (!res.ok) {
66
- throw new Error(`Failed to fetch settings from URL: ${res.status} ${res.statusText}`);
67
- }
68
- const contentType = res.headers.get("content-type") || "";
69
- const text = yield res.text();
70
- const isToml = contentType.includes("toml") || url.toLowerCase().endsWith(".toml");
71
- if (isToml) {
72
- loadC2paSettingsToml(text);
73
- return;
74
- }
75
- // Treat as JSON by default
76
- loadC2paSettings(text);
77
- });
78
- }
79
- /**
80
- * Get the current settings as a JSON string.
81
- * @returns The JSON representation of the current settings
82
- */
83
- export function getSettingsJson() {
84
- return getNeonBinary().getSettingsJson();
85
- }
86
- /**
87
- * Load trust configuration into the main trust settings.
88
- * @param trustConfig The trust configuration object
89
- */
90
- export function loadTrustConfig(trustConfig) {
91
- var _a, _b, _c, _d;
92
- // Convert settings to snake_case for the neon Rust code.
93
- // Explicitly set undefined fields to null to ensure they are cleared
94
- const config = {
95
- verify_trust_list: trustConfig.verifyTrustList,
96
- user_anchors: (_a = trustConfig.userAnchors) !== null && _a !== void 0 ? _a : null,
97
- trust_anchors: (_b = trustConfig.trustAnchors) !== null && _b !== void 0 ? _b : null,
98
- trust_config: (_c = trustConfig.trustConfig) !== null && _c !== void 0 ? _c : null,
99
- allowed_list: (_d = trustConfig.allowedList) !== null && _d !== void 0 ? _d : null,
100
- };
101
- getNeonBinary().loadTrustConfig(JSON.stringify(config));
102
- }
103
- /**
104
- * Load trust configuration into the CAWG trust settings.
105
- * @param trustConfig The trust configuration object
106
- */
107
- export function loadCawgTrustConfig(trustConfig) {
108
- var _a, _b, _c, _d;
109
- // Convert settings to snake_case for the neon Rust code
110
- // Explicitly set undefined fields to null to ensure they are cleared
111
- const config = {
112
- verify_trust_list: trustConfig.verifyTrustList,
113
- user_anchors: (_a = trustConfig.userAnchors) !== null && _a !== void 0 ? _a : null,
114
- trust_anchors: (_b = trustConfig.trustAnchors) !== null && _b !== void 0 ? _b : null,
115
- trust_config: (_c = trustConfig.trustConfig) !== null && _c !== void 0 ? _c : null,
116
- allowed_list: (_d = trustConfig.allowedList) !== null && _d !== void 0 ? _d : null,
28
+ export function createTrustSettings(trustConfig) {
29
+ return {
30
+ trust: {
31
+ verify_trust_list: trustConfig.verifyTrustList,
32
+ user_anchors: trustConfig.userAnchors,
33
+ trust_anchors: trustConfig.trustAnchors,
34
+ trust_config: trustConfig.trustConfig,
35
+ allowed_list: trustConfig.allowedList,
36
+ },
117
37
  };
118
- getNeonBinary().loadCawgTrustConfig(JSON.stringify(config));
119
38
  }
120
39
  /**
121
- * Get the current trust configuration.
122
- * @returns The current trust configuration object
40
+ * Create a settings object with CAWG trust configuration.
41
+ * @param trustConfig The CAWG trust configuration
42
+ * @returns Settings object that can be passed to Reader/Builder
123
43
  */
124
- export function getTrustConfig() {
125
- var _a, _b, _c, _d;
126
- const json = getNeonBinary().getTrustConfig();
127
- const parsed = JSON.parse(json);
128
- // Convert snake_case to camelCase to match TrustConfig interface
129
- // Handle undefined values by converting them to null
44
+ export function createCawgTrustSettings(trustConfig) {
130
45
  return {
131
- verifyTrustList: parsed.verify_trust_list,
132
- userAnchors: (_a = parsed.user_anchors) !== null && _a !== void 0 ? _a : null,
133
- trustAnchors: (_b = parsed.trust_anchors) !== null && _b !== void 0 ? _b : null,
134
- trustConfig: (_c = parsed.trust_config) !== null && _c !== void 0 ? _c : null,
135
- allowedList: (_d = parsed.allowed_list) !== null && _d !== void 0 ? _d : null,
46
+ cawg_trust: {
47
+ verify_trust_list: trustConfig.verifyTrustList,
48
+ user_anchors: trustConfig.userAnchors,
49
+ trust_anchors: trustConfig.trustAnchors,
50
+ trust_config: trustConfig.trustConfig,
51
+ allowed_list: trustConfig.allowedList,
52
+ },
136
53
  };
137
54
  }
138
55
  /**
139
- * Get the current CAWG trust configuration.
140
- * @returns The current CAWG trust configuration object
56
+ * Create a settings object with verify configuration.
57
+ * @param verifyConfig The verify configuration
58
+ * @returns Settings object that can be passed to Reader/Builder
141
59
  */
142
- export function getCawgTrustConfig() {
143
- var _a, _b, _c, _d;
144
- const json = getNeonBinary().getCawgTrustConfig();
145
- const parsed = JSON.parse(json);
146
- // Convert snake_case to camelCase to match TrustConfig interface
147
- // Handle undefined values by converting them to null
60
+ export function createVerifySettings(verifyConfig) {
148
61
  return {
149
- verifyTrustList: parsed.verify_trust_list,
150
- userAnchors: (_a = parsed.user_anchors) !== null && _a !== void 0 ? _a : null,
151
- trustAnchors: (_b = parsed.trust_anchors) !== null && _b !== void 0 ? _b : null,
152
- trustConfig: (_c = parsed.trust_config) !== null && _c !== void 0 ? _c : null,
153
- allowedList: (_d = parsed.allowed_list) !== null && _d !== void 0 ? _d : null,
62
+ verify: {
63
+ verify_after_reading: verifyConfig.verifyAfterReading,
64
+ verify_after_sign: verifyConfig.verifyAfterSign,
65
+ verify_trust: verifyConfig.verifyTrust,
66
+ verify_timestamp_trust: verifyConfig.verifyTimestampTrust,
67
+ ocsp_fetch: verifyConfig.ocspFetch,
68
+ remote_manifest_fetch: verifyConfig.remoteManifestFetch,
69
+ skip_ingredient_conflict_resolution: verifyConfig.skipIngredientConflictResolution,
70
+ strict_v1_validation: verifyConfig.strictV1Validation,
71
+ },
154
72
  };
155
73
  }
156
74
  /**
157
- * Load verify configuration into the verify settings.
158
- * @param verifyConfig The verify configuration object
75
+ * Merge multiple settings objects into one.
76
+ * Later settings override earlier ones.
77
+ * @param settings Settings objects to merge
78
+ * @returns Merged settings object
159
79
  */
160
- export function loadVerifyConfig(verifyConfig) {
161
- // Convert camelCase to snake_case for the neon Rust code
162
- const config = {
163
- verify_after_reading: verifyConfig.verifyAfterReading,
164
- verify_after_sign: verifyConfig.verifyAfterSign,
165
- verify_trust: verifyConfig.verifyTrust,
166
- verify_timestamp_trust: verifyConfig.verifyTimestampTrust,
167
- ocsp_fetch: verifyConfig.ocspFetch,
168
- remote_manifest_fetch: verifyConfig.remoteManifestFetch,
169
- skip_ingredient_conflict_resolution: verifyConfig.skipIngredientConflictResolution,
170
- strict_v1_validation: verifyConfig.strictV1Validation,
171
- };
172
- getNeonBinary().loadVerifyConfig(JSON.stringify(config));
80
+ export function mergeSettings(...settings) {
81
+ const merged = {};
82
+ for (const setting of settings) {
83
+ if (setting.trust) {
84
+ merged.trust = Object.assign(Object.assign({}, merged.trust), setting.trust);
85
+ }
86
+ if (setting.cawg_trust) {
87
+ merged.cawg_trust = Object.assign(Object.assign({}, merged.cawg_trust), setting.cawg_trust);
88
+ }
89
+ if (setting.verify) {
90
+ merged.verify = Object.assign(Object.assign({}, merged.verify), setting.verify);
91
+ }
92
+ if (setting.builder) {
93
+ merged.builder = Object.assign(Object.assign({}, merged.builder), setting.builder);
94
+ }
95
+ }
96
+ return merged;
173
97
  }
174
98
  /**
175
- * Patch the verify configuration with any subset of fields.
176
- * @param patch Partial verify config fields to update
99
+ * Convert a settings object to a JSON string.
100
+ * @param settings The settings object
101
+ * @returns JSON string representation
177
102
  */
178
- export function patchVerifyConfig(patch) {
179
- const current = getVerifyConfig();
180
- const updated = Object.assign(Object.assign({}, current), patch);
181
- loadVerifyConfig(updated);
103
+ export function settingsToJson(settings) {
104
+ return JSON.stringify(settings);
182
105
  }
183
106
  /**
184
- * Get the current verify configuration.
185
- * @returns The current verify configuration object
107
+ * Load settings from a TOML or JSON file.
108
+ * @param filePath The path to the settings file
109
+ * @returns Settings as a string (TOML or JSON depending on file extension)
186
110
  */
187
- export function getVerifyConfig() {
188
- const json = getNeonBinary().getVerifyConfig();
189
- const parsed = JSON.parse(json);
190
- // Convert snake_case to camelCase to match VerifyConfig interface
191
- return {
192
- verifyAfterReading: parsed.verify_after_reading,
193
- verifyAfterSign: parsed.verify_after_sign,
194
- verifyTrust: parsed.verify_trust,
195
- verifyTimestampTrust: parsed.verify_timestamp_trust,
196
- ocspFetch: parsed.ocsp_fetch,
197
- remoteManifestFetch: parsed.remote_manifest_fetch,
198
- skipIngredientConflictResolution: parsed.skip_ingredient_conflict_resolution,
199
- strictV1Validation: parsed.strict_v1_validation,
200
- };
111
+ export function loadSettingsFromFile(filePath) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const content = yield fs.readFile(filePath, "utf8");
114
+ return content;
115
+ });
201
116
  }
202
117
  /**
203
- * Reset settings to their default values.
204
- * This clears any custom settings and restores the default configuration.
118
+ * Load settings from a URL.
119
+ * @param url The URL to fetch the settings from
120
+ * @returns Settings as a string
205
121
  */
206
- export function resetSettings() {
207
- getNeonBinary().resetSettings();
122
+ export function loadSettingsFromUrl(url) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ const res = yield fetch(url);
125
+ if (!res.ok) {
126
+ throw new Error(`Failed to fetch settings from URL: ${res.status} ${res.statusText}`);
127
+ }
128
+ return yield res.text();
129
+ });
208
130
  }