@contentauth/c2pa-node 0.5.5 → 0.6.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/Cargo.toml +2 -1
- package/README.md +11 -15
- package/dist/Builder.js +60 -80
- package/dist/Builder.spec.js +140 -155
- package/dist/IdentityAssertion.js +8 -18
- package/dist/IdentityAssertion.spec.js +28 -35
- package/dist/Reader.js +13 -29
- package/dist/Reader.spec.js +52 -63
- package/dist/Settings.js +16 -29
- package/dist/Settings.spec.js +47 -62
- package/dist/Signer.js +4 -13
- package/dist/Signer.spec.js +35 -43
- package/dist/Trustmark.js +8 -22
- package/dist/Trustmark.spec.js +50 -61
- package/dist/binary.js +1 -1
- package/dist/index.node +0 -0
- package/package.json +33 -30
- package/scripts/postinstall.cjs +18 -1
- package/Cargo.lock +0 -5079
- package/LICENSE +0 -21
package/dist/Trustmark.spec.js
CHANGED
|
@@ -3,15 +3,6 @@
|
|
|
3
3
|
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
4
4
|
// or the MIT license (http://opensource.org/licenses/MIT),
|
|
5
5
|
// at your option.
|
|
6
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
6
|
// Unless required by applicable law or agreed to in writing,
|
|
16
7
|
// this software is distributed on an "AS IS" BASIS, WITHOUT
|
|
17
8
|
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
|
|
@@ -24,19 +15,17 @@ import sharp from "sharp";
|
|
|
24
15
|
import { Trustmark } from "./Trustmark.js";
|
|
25
16
|
const tempDir = path.join(__dirname, "..", "tmp");
|
|
26
17
|
// Helper function to convert raw RGB pixel data to JPEG format
|
|
27
|
-
function rawRgbToJpeg(rawRgbData, width, height) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
// Convert to JPEG format
|
|
38
|
-
return yield image.jpeg().toBuffer();
|
|
18
|
+
async function rawRgbToJpeg(rawRgbData, width, height) {
|
|
19
|
+
// Create a sharp image from raw RGB data
|
|
20
|
+
const image = sharp(rawRgbData, {
|
|
21
|
+
raw: {
|
|
22
|
+
width,
|
|
23
|
+
height,
|
|
24
|
+
channels: 3, // RGB
|
|
25
|
+
},
|
|
39
26
|
});
|
|
27
|
+
// Convert to JPEG format
|
|
28
|
+
return await image.jpeg().toBuffer();
|
|
40
29
|
}
|
|
41
30
|
describe("Trustmark", () => {
|
|
42
31
|
let trustmark;
|
|
@@ -48,95 +37,95 @@ describe("Trustmark", () => {
|
|
|
48
37
|
version: "BCH_3", // Tolerates 3 bit flips
|
|
49
38
|
modelPath: path.join(tempDir, "trustmark_models"), // Use temporary directory for model path
|
|
50
39
|
};
|
|
51
|
-
beforeAll(() =>
|
|
52
|
-
|
|
40
|
+
beforeAll(async () => {
|
|
41
|
+
await fs.ensureDir(tempDir);
|
|
53
42
|
// Use an actual test image from fixtures directory
|
|
54
|
-
testImage =
|
|
43
|
+
testImage = await fs.readFile("./tests/fixtures/A.jpg");
|
|
55
44
|
// Get image dimensions for converting raw pixel data
|
|
56
|
-
const imageInfo =
|
|
45
|
+
const imageInfo = await sharp(testImage).metadata();
|
|
57
46
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
58
47
|
testImageWidth = imageInfo.width;
|
|
59
48
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
60
49
|
testImageHeight = imageInfo.height;
|
|
61
|
-
trustmark =
|
|
62
|
-
})
|
|
50
|
+
trustmark = await Trustmark.newTrustmark(trustmarkConfig);
|
|
51
|
+
});
|
|
63
52
|
describe("newTrustmark", () => {
|
|
64
|
-
it("should create a new trustmark instance with valid config", () =>
|
|
53
|
+
it("should create a new trustmark instance with valid config", async () => {
|
|
65
54
|
// This test will be skipped if the model file doesn't exist
|
|
66
55
|
expect(trustmark).toBeDefined();
|
|
67
56
|
expect(typeof trustmark.encode).toBe("function");
|
|
68
57
|
expect(typeof trustmark.decode).toBe("function");
|
|
69
|
-
})
|
|
70
|
-
it("should throw error with invalid config", () =>
|
|
58
|
+
});
|
|
59
|
+
it("should throw error with invalid config", async () => {
|
|
71
60
|
const invalidConfig = {
|
|
72
61
|
variant: "X", // Invalid variant
|
|
73
62
|
version: "BCH_3", // Valid version
|
|
74
63
|
};
|
|
75
|
-
|
|
76
|
-
})
|
|
64
|
+
await expect(Trustmark.newTrustmark(invalidConfig)).rejects.toThrow("Watermark configuration error: invalid variant");
|
|
65
|
+
});
|
|
77
66
|
});
|
|
78
67
|
describe("encode", () => {
|
|
79
|
-
it("should encode a watermark into an image with default watermark", () =>
|
|
68
|
+
it("should encode a watermark into an image with default watermark", async () => {
|
|
80
69
|
const strength = 0.9;
|
|
81
|
-
const encodedImage =
|
|
70
|
+
const encodedImage = await trustmark.encode(testImage, strength);
|
|
82
71
|
expect(encodedImage).toBeDefined();
|
|
83
72
|
expect(Buffer.isBuffer(encodedImage)).toBe(true);
|
|
84
73
|
expect(encodedImage.length).toBeGreaterThan(0);
|
|
85
74
|
// The encoded image should be different from the original
|
|
86
75
|
expect(encodedImage).not.toEqual(testImage);
|
|
87
|
-
})
|
|
88
|
-
it("should encode a watermark into an image with custom watermark", () =>
|
|
76
|
+
});
|
|
77
|
+
it("should encode a watermark into an image with custom watermark", async () => {
|
|
89
78
|
const strength = 0.95;
|
|
90
79
|
const customWatermark = "0101000011001";
|
|
91
|
-
const encodedImage =
|
|
80
|
+
const encodedImage = await trustmark.encode(testImage, strength, customWatermark);
|
|
92
81
|
expect(encodedImage).toBeDefined();
|
|
93
82
|
expect(Buffer.isBuffer(encodedImage)).toBe(true);
|
|
94
83
|
expect(encodedImage.length).toBeGreaterThan(0);
|
|
95
|
-
})
|
|
96
|
-
it("should throw error with invalid strength values", () =>
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
})
|
|
100
|
-
it("should throw error with empty image buffer", () =>
|
|
84
|
+
});
|
|
85
|
+
it("should throw error with invalid strength values", async () => {
|
|
86
|
+
await expect(trustmark.encode(testImage, -0.1)).rejects.toThrow("Watermark configuration error: strength must be between 0.0 and 1.0");
|
|
87
|
+
await expect(trustmark.encode(testImage, 1.1)).rejects.toThrow("Watermark configuration error: strength must be between 0.0 and 1.0");
|
|
88
|
+
});
|
|
89
|
+
it("should throw error with empty image buffer", async () => {
|
|
101
90
|
const emptyBuffer = Buffer.alloc(0);
|
|
102
|
-
|
|
103
|
-
})
|
|
91
|
+
await expect(trustmark.encode(emptyBuffer, 0.5)).rejects.toThrow("The image format could not be determined");
|
|
92
|
+
});
|
|
104
93
|
});
|
|
105
94
|
describe("decode", () => {
|
|
106
|
-
it("should decode a watermark from an encoded image", () =>
|
|
95
|
+
it("should decode a watermark from an encoded image", async () => {
|
|
107
96
|
const strength = 0.8;
|
|
108
97
|
const customWatermark = "0101000011001";
|
|
109
98
|
// First encode a watermark
|
|
110
|
-
const rawPixelData =
|
|
99
|
+
const rawPixelData = await trustmark.encode(testImage, strength, customWatermark);
|
|
111
100
|
// Convert raw pixel data to JPEG format for decoding
|
|
112
|
-
const encodedImage =
|
|
101
|
+
const encodedImage = await rawRgbToJpeg(rawPixelData, testImageWidth, testImageHeight);
|
|
113
102
|
// Then decode it
|
|
114
|
-
const decodedWatermark =
|
|
103
|
+
const decodedWatermark = await trustmark.decode(encodedImage);
|
|
115
104
|
expect(decodedWatermark).toBeDefined();
|
|
116
105
|
expect(typeof decodedWatermark).toBe("string");
|
|
117
106
|
expect(decodedWatermark.length).toBeGreaterThan(0);
|
|
118
107
|
// The decoded watermark should match the original
|
|
119
108
|
expect(decodedWatermark.startsWith(customWatermark)).toBeTruthy();
|
|
120
|
-
})
|
|
121
|
-
it("should decode watermark from image with generated watermark", () =>
|
|
109
|
+
});
|
|
110
|
+
it("should decode watermark from image with generated watermark", async () => {
|
|
122
111
|
const strength = 0.75;
|
|
123
112
|
// Encode with default watermark
|
|
124
|
-
const rawPixelData =
|
|
113
|
+
const rawPixelData = await trustmark.encode(testImage, strength);
|
|
125
114
|
// Convert raw pixel data to JPEG format for decoding
|
|
126
|
-
const encodedImage =
|
|
115
|
+
const encodedImage = await rawRgbToJpeg(rawPixelData, testImageWidth, testImageHeight);
|
|
127
116
|
// Decode the watermark
|
|
128
|
-
const decodedWatermark =
|
|
117
|
+
const decodedWatermark = await trustmark.decode(encodedImage);
|
|
129
118
|
expect(decodedWatermark).toBeDefined();
|
|
130
119
|
expect(typeof decodedWatermark).toBe("string");
|
|
131
120
|
expect(decodedWatermark.length).toBeGreaterThan(0);
|
|
132
|
-
})
|
|
133
|
-
it("should throw error decoding from original image (no watermark)", () =>
|
|
121
|
+
});
|
|
122
|
+
it("should throw error decoding from original image (no watermark)", async () => {
|
|
134
123
|
// Try to decode from an image that hasn't been watermarked
|
|
135
|
-
|
|
136
|
-
})
|
|
137
|
-
it("should throw error with empty image buffer", () =>
|
|
124
|
+
await expect(trustmark.decode(testImage)).rejects.toThrow("watermark is corrupt or missing");
|
|
125
|
+
});
|
|
126
|
+
it("should throw error with empty image buffer", async () => {
|
|
138
127
|
const emptyBuffer = Buffer.alloc(0);
|
|
139
|
-
|
|
140
|
-
})
|
|
128
|
+
await expect(trustmark.decode(emptyBuffer)).rejects.toThrow("The image format could not be determined");
|
|
129
|
+
});
|
|
141
130
|
});
|
|
142
131
|
});
|
package/dist/binary.js
CHANGED
|
@@ -19,7 +19,7 @@ let neon = null;
|
|
|
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
|
|
22
|
+
neon = require(C2PA_LIBRARY_PATH ?? "./index.node");
|
|
23
23
|
}
|
|
24
24
|
return neon;
|
|
25
25
|
}
|
package/dist/index.node
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
"name": "@contentauth/c2pa-node",
|
|
3
3
|
"repository": {
|
|
4
4
|
"type": "git",
|
|
5
|
-
"url": "git+https://github.com/contentauth/c2pa-
|
|
5
|
+
"url": "git+https://github.com/contentauth/c2pa-js.git",
|
|
6
|
+
"directory": "packages/c2pa-node"
|
|
6
7
|
},
|
|
7
|
-
"version": "0.
|
|
8
|
+
"version": "0.6.0",
|
|
8
9
|
"description": "Node.js bindings for C2PA",
|
|
9
10
|
"main": "dist/index.js",
|
|
10
11
|
"type": "module",
|
|
@@ -17,13 +18,32 @@
|
|
|
17
18
|
"Cargo.lock"
|
|
18
19
|
],
|
|
19
20
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
21
|
+
"node": ">=22"
|
|
21
22
|
},
|
|
22
|
-
"
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "run-s build:rust build:ts",
|
|
25
|
+
"build:debug": "run-s build:rust-debug build:ts",
|
|
26
|
+
"build:rust": "pnpm run build:rust-debug --release",
|
|
27
|
+
"build:rust-debug": "cargo build --message-format=json-render-diagnostics > cargo.log",
|
|
28
|
+
"build:ts": "tsc -b && cpy index.node dist && cpy index.node js-src && cpy --flat js-src/types.d.ts dist/types",
|
|
29
|
+
"ci": "run-s build:rust-debug build:ts test",
|
|
30
|
+
"clean": "rimraf dist target",
|
|
31
|
+
"cross": "pnpm run cross-build --release",
|
|
32
|
+
"cross-build": "cross build --message-format=json-render-diagnostics > cross.log",
|
|
33
|
+
"debug": "pnpm run cargo-build --",
|
|
34
|
+
"lint": "cargo clippy && pnpm eslint",
|
|
35
|
+
"postbuild:rust-debug": "neon dist < cargo.log",
|
|
36
|
+
"postcross-build": "neon dist -m /target < cross.log",
|
|
37
|
+
"postinstall": "node scripts/postinstall.cjs",
|
|
38
|
+
"test": "pnpm build:debug && vitest run --config ./vitest.config.ts",
|
|
39
|
+
"test:watch": "pnpm build:debug && vitest --config ./vitest.config.ts",
|
|
40
|
+
"test:ui": "pnpm build:debug && vitest --ui --config ./vitest.config.ts"
|
|
41
|
+
},
|
|
42
|
+
"author": "Colin Murphy <colmurph@adobe.com>, Austin Le <ale@adobe.com>",
|
|
23
43
|
"license": "MIT",
|
|
24
44
|
"devDependencies": {
|
|
25
45
|
"@changesets/cli": "^2.31.0",
|
|
26
|
-
"@contentauth/c2pa-types": "
|
|
46
|
+
"@contentauth/c2pa-types": "workspace:*",
|
|
27
47
|
"@eslint/js": "^9.39.4",
|
|
28
48
|
"@neon-rs/cli": "0.1.82",
|
|
29
49
|
"@types/cli-progress": "^3.11.6",
|
|
@@ -51,9 +71,9 @@
|
|
|
51
71
|
"c2pa"
|
|
52
72
|
],
|
|
53
73
|
"bugs": {
|
|
54
|
-
"url": "https://github.com/contentauth/c2pa-
|
|
74
|
+
"url": "https://github.com/contentauth/c2pa-js/issues"
|
|
55
75
|
},
|
|
56
|
-
"homepage": "https://github.com/contentauth/c2pa-node
|
|
76
|
+
"homepage": "https://github.com/contentauth/c2pa-js/tree/main/packages/c2pa-node#readme",
|
|
57
77
|
"dependencies": {
|
|
58
78
|
"cargo-cp-artifact": "^0.1.9",
|
|
59
79
|
"cli-progress": "^3.12.0",
|
|
@@ -65,27 +85,10 @@
|
|
|
65
85
|
"pretty-bytes": "^6.1.1",
|
|
66
86
|
"unzipper": "^0.10.14"
|
|
67
87
|
},
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"build:rust-debug": "cargo build --message-format=json-render-diagnostics > cargo.log",
|
|
74
|
-
"build:ts": "tsc -b && cpy index.node dist && cpy index.node js-src && cpy --flat js-src/types.d.ts dist/types",
|
|
75
|
-
"changeset:publish": "changeset publish",
|
|
76
|
-
"ci": "run-s build:rust-debug build:ts test",
|
|
77
|
-
"ci:release": "pnpm run build && changeset tag && pnpm publish --access=public --no-git-checks",
|
|
78
|
-
"clean": "rimraf dist target",
|
|
79
|
-
"cross": "pnpm run cross-build --release",
|
|
80
|
-
"cross-build": "cross build --message-format=json-render-diagnostics > cross.log",
|
|
81
|
-
"debug": "pnpm run cargo-build --",
|
|
82
|
-
"lint": "cargo clippy && pnpm eslint",
|
|
83
|
-
"postbuild:rust-debug": "neon dist < cargo.log",
|
|
84
|
-
"postcross-build": "neon dist -m /target < cross.log",
|
|
85
|
-
"postinstall": "node scripts/postinstall.cjs",
|
|
86
|
-
"release": "run-s build changeset:publish",
|
|
87
|
-
"test": "pnpm build:debug && vitest run",
|
|
88
|
-
"test:watch": "pnpm build:debug && vitest",
|
|
89
|
-
"test:ui": "pnpm build:debug && vitest --ui"
|
|
88
|
+
"packageManager": "pnpm@10.15.0",
|
|
89
|
+
"nx": {
|
|
90
|
+
"tags": [
|
|
91
|
+
"lib"
|
|
92
|
+
]
|
|
90
93
|
}
|
|
91
|
-
}
|
|
94
|
+
}
|
package/scripts/postinstall.cjs
CHANGED
|
@@ -14,7 +14,8 @@ const cliProgress = require('cli-progress');
|
|
|
14
14
|
const unzipper = require('unzipper');
|
|
15
15
|
const os = require('node:os');
|
|
16
16
|
const { mkdirp } = require('mkdirp');
|
|
17
|
-
const
|
|
17
|
+
const path = require('node:path');
|
|
18
|
+
const { resolve } = path;
|
|
18
19
|
const { exec } = require('node:child_process');
|
|
19
20
|
const { promisify } = require('node:util');
|
|
20
21
|
// node-fetch v3 is ES module only, so we'll import it dynamically
|
|
@@ -23,6 +24,16 @@ const { promisify } = require('node:util');
|
|
|
23
24
|
|
|
24
25
|
const pExec = promisify(exec);
|
|
25
26
|
|
|
27
|
+
async function isInPnpmWorkspace(startDir) {
|
|
28
|
+
let dir = path.dirname(startDir);
|
|
29
|
+
while (true) {
|
|
30
|
+
const parent = path.dirname(dir);
|
|
31
|
+
if (parent === dir) return false;
|
|
32
|
+
if (await fileExists(path.join(parent, 'pnpm-workspace.yaml'))) return true;
|
|
33
|
+
dir = parent;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
const execCallback = (err, stdout, stderr) => {
|
|
27
38
|
if (err) {
|
|
28
39
|
console.error(`ERROR: ${err}`);
|
|
@@ -175,6 +186,12 @@ async function buildRust(root) {
|
|
|
175
186
|
async function main() {
|
|
176
187
|
const { packageDirectory } = await import('pkg-dir');
|
|
177
188
|
const appRoot = await packageDirectory(__filename);
|
|
189
|
+
|
|
190
|
+
if (await isInPnpmWorkspace(appRoot)) {
|
|
191
|
+
console.log('Skipping Rust build: package is installed inside a pnpm workspace');
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
178
195
|
const distRoot = resolve(appRoot, 'dist');
|
|
179
196
|
const cargoDistPath = resolve(distRoot, 'Cargo.toml');
|
|
180
197
|
const libraryOverridePath = process.env.C2PA_LIBRARY_PATH;
|