@blazediff/matcher 1.5.4 → 2.0.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 +0 -26
- package/dist/index.d.ts +0 -7
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- package/dist/worker.js +1 -1
- package/dist/worker.mjs +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -171,12 +171,6 @@ Main function for snapshot comparison.
|
|
|
171
171
|
<td>true</td>
|
|
172
172
|
<td>Run image I/O and comparison in a worker thread for better performance</td>
|
|
173
173
|
</tr>
|
|
174
|
-
<tr>
|
|
175
|
-
<td><code>outputFormat</code></td>
|
|
176
|
-
<td>'png' | 'html'</td>
|
|
177
|
-
<td>'png'</td>
|
|
178
|
-
<td>Diff output format (<code>core-native</code> only). Use <code>'html'</code> for an interactive interpret report with detected change regions, classification, and severity</td>
|
|
179
|
-
</tr>
|
|
180
174
|
</table>
|
|
181
175
|
|
|
182
176
|
### ComparisonResult
|
|
@@ -328,26 +322,6 @@ const result = await getOrCreateSnapshot(
|
|
|
328
322
|
);
|
|
329
323
|
```
|
|
330
324
|
|
|
331
|
-
### HTML Interpret Report
|
|
332
|
-
|
|
333
|
-
When using `core-native`, set `outputFormat: 'html'` to generate an interactive HTML report on failure instead of a plain diff image. The report includes detected change regions with classification, severity, and spatial analysis.
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
const result = await getOrCreateSnapshot(
|
|
337
|
-
imagePath,
|
|
338
|
-
{
|
|
339
|
-
method: 'core-native',
|
|
340
|
-
outputFormat: 'html',
|
|
341
|
-
failureThreshold: 0.01,
|
|
342
|
-
failureThresholdType: 'percent',
|
|
343
|
-
},
|
|
344
|
-
{ testPath: __filename, testName: 'test name' }
|
|
345
|
-
);
|
|
346
|
-
|
|
347
|
-
// On failure, result.diffPath points to an .html file instead of .png
|
|
348
|
-
console.log(result.diffPath); // __snapshots__/test-name.diff.html
|
|
349
|
-
```
|
|
350
|
-
|
|
351
325
|
### Different Comparison Methods
|
|
352
326
|
|
|
353
327
|
```typescript
|
package/dist/index.d.ts
CHANGED
|
@@ -99,13 +99,6 @@ interface MatcherOptions {
|
|
|
99
99
|
* @default true
|
|
100
100
|
*/
|
|
101
101
|
runInWorker?: boolean;
|
|
102
|
-
/**
|
|
103
|
-
* Output format for diff visualization (core-native method only)
|
|
104
|
-
* - "png": standard diff image (default)
|
|
105
|
-
* - "html": interactive HTML interpret report
|
|
106
|
-
* @default 'png'
|
|
107
|
-
*/
|
|
108
|
-
outputFormat?: "png" | "html";
|
|
109
102
|
}
|
|
110
103
|
/**
|
|
111
104
|
* Result of a comparison operation
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var fs=require('fs'),path=require('path'),codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim'),s=require('picocolors'),worker_threads=require('worker_threads');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var s__default=/*#__PURE__*/_interopDefault(s);var ye=Object.defineProperty;var b=(e,t)=>()=>(e&&(t=e(e=0)),t);var me=(e,t)=>{for(var r in t)ye(e,r,{get:t[r],enumerable:true});};var xe,I,u=b(()=>{xe=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href,I=xe();});var
|
|
2
|
-
`)}function Be(){return `${s__default.default.green(B.success)} ${s__default.default.green("Image matches snapshot")}`}function Ne(e){let{method:t,baselinePath:r,receivedPath:o,diffPath:n,diffCount:a=0,diffPercentage:i,score:f=0,threshold:
|
|
3
|
-
`)}u();H();q();O();u();var x=null,ze=0,ge=false,D=new Map;function
|
|
1
|
+
'use strict';var fs=require('fs'),path=require('path'),codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim'),s=require('picocolors'),worker_threads=require('worker_threads');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var s__default=/*#__PURE__*/_interopDefault(s);var ye=Object.defineProperty;var b=(e,t)=>()=>(e&&(t=e(e=0)),t);var me=(e,t)=>{for(var r in t)ye(e,r,{get:t[r],enumerable:true});};var xe,I,u=b(()=>{xe=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href,I=xe();});var ce={};me(ce,{ensureDir:()=>te,fileExists:()=>v,isFilePath:()=>C,isImageBuffer:()=>de,isImageData:()=>P,isRawPngBuffer:()=>W,loadPNG:()=>E,normalizeImageInput:()=>S,savePNG:()=>y,saveRawPNGBuffer:()=>R});function C(e){return typeof e=="string"}function W(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function de(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}function P(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function E(e){if(!fs.existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function y(e,t,r,o){let n=path.dirname(e);fs.existsSync(n)||fs.mkdirSync(n,{recursive:true}),await codecPngjs.codecPngjs.write({data:t instanceof Uint8Array?t:new Uint8Array(t),width:r,height:o},e);}function R(e,t){let r=path.dirname(e);fs.existsSync(r)||fs.mkdirSync(r,{recursive:true}),fs.writeFileSync(e,t);}async function S(e){if(C(e))return E(e);if(W(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function v(e){return fs.existsSync(e)}function te(e){fs.existsSync(e)||fs.mkdirSync(e,{recursive:true});}var O=b(()=>{u();});function z(e,t,r,o){let{width:n,height:a}=e,i=n*a;if(e.width!==t.width||e.height!==t.height)return {diffCount:i,diffPercentage:100};let f=r?new Uint8Array(i*4):void 0,c=core.diff(e.data,t.data,f,n,a,{threshold:o.threshold??.1,includeAA:o.includeAA??false});return {diffCount:c,diffPercentage:c/i*100,diffOutput:f}}var re=b(()=>{u();});async function G(e,t,r,o){if(!C(e))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!C(t))throw new Error("Method 'core-native' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let n=await coreNative.compare(e,t,r,{threshold:o.threshold,antialiasing:o.antialiasing});if(n.match)return {diffCount:0,diffPercentage:0};if(n.reason==="layout-diff")return {diffCount:Number.MAX_SAFE_INTEGER,diffPercentage:100};if(n.reason==="pixel-diff")return {diffCount:n.diffCount,diffPercentage:n.diffPercentage};if(n.reason==="file-not-exists")throw new Error(`Image file not found: ${n.file}`);return {diffCount:0,diffPercentage:0}}var ae=b(()=>{u();O();});function ke(){return oe||(oe=import('@blazediff/core-wasm').then(async e=>{let t=Oe.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await e.initBlazediff(await promises.readFile(t)),e})),oe}async function F(e,t,r,o){let{width:n,height:a}=e,i=n*a;if(e.width!==t.width||e.height!==t.height)return {diffCount:i,diffPercentage:100};let f=r?new Uint8Array(i*4):void 0,{diff:c}=await ke(),m=await c(e.data,t.data,n,a,f,{threshold:o.threshold??.1,includeAA:o.includeAA??false});return {diffCount:m,diffPercentage:m/i*100,diffOutput:f}}var Oe,oe,ne=b(()=>{u();Oe=module$1.createRequire(I);});function _(e,t,r,o){let{width:n,height:a}=e,i=n*a;if(e.width!==t.width||e.height!==t.height)return {score:1};let f=r?new Uint8Array(i*4):void 0;return {score:gmsd.gmsd(e.data,t.data,f,n,a,{downsample:o.downsample}),diffOutput:f}}var ie=b(()=>{u();});function j(e,t,r,o,n){let{width:a,height:i}=e,f=a*i;if(e.width!==t.width||e.height!==t.height)return {score:0};let c=o?new Uint8Array(f*4):void 0,m={windowSize:n.windowSize,k1:n.k1,k2:n.k2},M;switch(r){case "ssim":M=ssim.ssim(e.data,t.data,c,a,i,m);break;case "msssim":M=msssim.msssim(e.data,t.data,c,a,i,m);break;case "hitchhikers-ssim":M=hitchhikersSsim.hitchhikersSSIM(e.data,t.data,c,a,i,m);break;default:throw new Error(`Unknown SSIM method: ${r}`)}return {score:M,diffOutput:c}}function k(e){return e==="ssim"||e==="msssim"||e==="hitchhikers-ssim"}var q=b(()=>{u();});var pe={};me(pe,{compareCore:()=>z,compareCoreNative:()=>G,compareCoreWasm:()=>F,compareGmsd:()=>_,compareSsim:()=>j,isSsimMethod:()=>k,runComparison:()=>$,validateMethodSupportsInput:()=>L});function L(e,t){if(e==="core-native"&&!C(t))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.")}async function $(e,t,r,o,n){if(L(r,e),L(r,t),r==="core-native"){let m=await G(e,t,n,o);return {diffCount:m.diffCount,diffPercentage:m.diffPercentage}}let a=P(e)?e:await S(e),i=P(t)?t:await S(t),f=n!==void 0;if(k(r)){let m=j(a,i,r,f,o);return {score:m.score,diffOutput:m.diffOutput}}if(r==="gmsd"){let m=_(a,i,f,o);return {score:m.score,diffOutput:m.diffOutput}}if(r==="core-wasm"){let m=await F(a,i,f,o);return {diffCount:m.diffCount,diffPercentage:m.diffPercentage,diffOutput:m.diffOutput}}let c=z(a,i,f,o);return {diffCount:c.diffCount,diffPercentage:c.diffPercentage,diffOutput:c.diffOutput}}var H=b(()=>{u();O();re();ae();ne();ie();q();re();ae();ne();ie();q();});u();H();O();u();var B={success:s__default.default.isColorSupported?"\u2714":"\u221A",error:s__default.default.isColorSupported?"\u2716":"\xD7",info:s__default.default.isColorSupported?"\u2139":"i",arrow:s__default.default.isColorSupported?"\u2514\u2500":"'-"},We=new Set(["ssim","msssim","hitchhikers-ssim"]);function A(e){return e.snapshotCreated?Ee(e.baselinePath):e.pass?Be():Ne(e)}function Ee(e){return [`${s__default.default.green(B.success)} ${s__default.default.green("New snapshot created")}`,` ${s__default.default.dim(B.arrow)} ${s__default.default.dim(e)}`].join(`
|
|
2
|
+
`)}function Be(){return `${s__default.default.green(B.success)} ${s__default.default.green("Image matches snapshot")}`}function Ne(e){let{method:t,baselinePath:r,receivedPath:o,diffPath:n,diffCount:a=0,diffPercentage:i,score:f=0,threshold:c,thresholdType:m,updateCommand:M}=e,p=[`${s__default.default.red(B.error)} ${s__default.default.red(s__default.default.bold("Image snapshot mismatch"))}`,""],g=12;if(p.push(` ${s__default.default.dim("Method".padEnd(g))}${t}`),p.push(` ${s__default.default.dim("Baseline".padEnd(g))}${s__default.default.dim(r)}`),p.push(` ${s__default.default.dim("Received".padEnd(g))}${s__default.default.dim(o)}`),p.push(` ${s__default.default.dim("Diff".padEnd(g))}${s__default.default.dim(n)}`),p.push(""),We.has(t)){let T=((1-f)*100).toFixed(2);p.push(` ${s__default.default.dim("SSIM Score".padEnd(g))}${s__default.default.yellow(f.toFixed(4))} ${s__default.default.dim("(1.0 = identical)")}`),p.push(` ${s__default.default.dim("Difference".padEnd(g))}${s__default.default.yellow(`${T}%`)}`);}else if(t==="gmsd")p.push(` ${s__default.default.dim("GMSD Score".padEnd(g))}${s__default.default.yellow(f.toFixed(4))} ${s__default.default.dim("(0.0 = identical)")}`);else {let T=i?.toFixed(2)??"0.00";p.push(` ${s__default.default.dim("Difference".padEnd(g))}${s__default.default.yellow(a.toLocaleString())} pixels ${s__default.default.dim(`(${T}%)`)}`);}let K=m==="percent"?"%":"pixels";return p.push(` ${s__default.default.dim("Threshold".padEnd(g))}${c} ${K}`),p.push(""),p.push(` ${s__default.default.cyan(B.info)} ${s__default.default.cyan(`Run with ${M??"--update"} to update the snapshot`)}`),p.join(`
|
|
3
|
+
`)}u();H();q();O();u();var x=null,ze=0,ge=false,D=new Map;function Ge(){let e=path.join(__dirname,"worker.js");if(fs.existsSync(e))return e;let t=path.join(__dirname,"../dist/worker.js");return fs.existsSync(t)?t:null}function Fe(){if(ge)return null;if(!x){let e=Ge();if(!e)return ge=true,null;x=new worker_threads.Worker(e),x.on("message",t=>{let r=D.get(t.id);r&&(D.delete(t.id),t.success?r.resolve(t.result):r.reject(new Error(t.error)));}),x.on("error",t=>{for(let r of D.values())r.reject(t);D.clear(),x=null;}),x.on("exit",t=>{if(t!==0){let r=new Error(`Worker exited with code ${t}`);for(let o of D.values())o.reject(r);D.clear();}x=null;});}return x}async function se(e,t){let r=Fe();if(!r){let{runComparison:o}=await Promise.resolve().then(()=>(H(),pe)),{loadPNG:n,normalizeImageInput:a}=await Promise.resolve().then(()=>(O(),ce));switch(e){case "normalize":return a(t.input);case "loadPNG":return n(t.filePath);case "compare":{let i=t;return o(i.received,i.baseline,i.method,i.options,i.diffOutputPath)}default:throw new Error(`Unknown request type: ${e}`)}}return new Promise((o,n)=>{let a=++ze;D.set(a,{resolve:o,reject:n});let i={id:a,type:e,payload:t};r.postMessage(i);})}function fe(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function we(e){let t=await se("normalize",{input:e});return fe(t)}async function Ie(e){let t=await se("loadPNG",{filePath:e});return fe(t)}function Ce(e,t,r,o,n){let a=fe(e);return se("compare",{received:a,baseline:t,method:r,options:o,diffOutputPath:n})}function _e(){if(x){let e=x;return x=null,e.terminate()}return Promise.resolve(0)}function Le(e){return e.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function He(e,t){let r=path.dirname(e.testPath),o=t.snapshotsDir??"__snapshots__",n=path.isAbsolute(o)?o:path.join(r,o),a=t.snapshotIdentifier??Le(e),i=t.method==="core-native"?"html":"png";return {snapshotDir:n,baselinePath:path.join(n,`${a}.png`),receivedPath:path.join(n,`${a}.received.png`),diffPath:path.join(n,`${a}.diff.${i}`)}}function Pe(e,t,r){let o=r.failureThreshold??0,n=r.failureThresholdType??"pixel";if(k(e)){let a=t.score??0;return n==="percent"?(1-a)*100<=o:a>=1-o/100}if(e==="gmsd"){let a=t.score??0;return n==="percent"?a*100<=o:a<=o/100}return n==="percent"?(t.diffPercentage??0)<=o:(t.diffCount??0)<=o}async function Xe(e,t,r){let o=He(r,t),{snapshotDir:n,baselinePath:a,receivedPath:i,diffPath:f}=o,c=W(e),m=c?e:null,M=t.runInWorker!==false,p=M?we:S,g=M?Ie:E,K=M?(w,l,V)=>Ce(w,l,t.method,t,V):(w,l,V)=>$(w,l,t.method,t,V);te(n);let T=v(a),N=t.updateSnapshots===true?"all":t.updateSnapshots===false||t.updateSnapshots===void 0?"new":t.updateSnapshots;if(!T&&N!=="none"&&(N==="new"||N==="all")){if(m)R(a,m);else if(C(e)){let l=await g(e);await y(a,l.data,l.width,l.height);}else if(P(e))await y(a,e.data,e.width,e.height);else {let l=await p(e);await y(a,l.data,l.width,l.height);}return {pass:true,message:A({pass:true,method:t.method,snapshotCreated:true,baselinePath:a,receivedPath:i,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:a,snapshotStatus:"added"}}let h,d;t.method==="core-native"?(h=await $(e,a,t.method,t,f),d=e):(d=c?await p(e):e,h=await K(P(d)?d:await p(d),a,f));let Q=Pe(t.method,h,t);if(Q)return fs.existsSync(i)&&fs.unlinkSync(i),fs.existsSync(f)&&fs.unlinkSync(f),{pass:true,message:A({pass:Q,method:t.method,snapshotCreated:false,baselinePath:a,receivedPath:i,diffPath:f,diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,baselinePath:a,snapshotStatus:"matched"};if(N==="all"){if(m)R(a,m);else if(C(d)){let l=await g(d);await y(a,l.data,l.width,l.height);}else P(d)&&await y(a,d.data,d.width,d.height);return fs.existsSync(i)&&fs.unlinkSync(i),fs.existsSync(f)&&fs.unlinkSync(f),{pass:true,message:A({pass:true,method:t.method,snapshotCreated:true,baselinePath:a,receivedPath:i,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:a,snapshotStatus:"updated"}}if(m)R(i,m);else if(C(d)){let w=await g(d);await y(i,w.data,w.width,w.height);}else P(d)&&await y(i,d.data,d.width,d.height);if(h.diffOutput&&t.method!=="core-native"){let w=P(d)?d:await p(d);await y(f,h.diffOutput,w.width,w.height);}return {pass:false,message:A({pass:Q,method:t.method,snapshotCreated:false,baselinePath:a,receivedPath:i,diffPath:f,diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,baselinePath:a,receivedPath:i,diffPath:f,snapshotStatus:"failed"}}async function Ze(e,t,r){let o=await $(e,t,r.method,r),n=Pe(r.method,o,r);return {pass:n,message:n?"Images match.":`Images differ: ${o.diffCount??o.score} ${o.diffCount!==void 0?"pixels":"score"}`,diffCount:o.diffCount,diffPercentage:o.diffPercentage,score:o.score}}exports.compareImages=Ze;exports.fileExists=v;exports.formatReport=A;exports.getOrCreateSnapshot=Xe;exports.isFilePath=C;exports.isImageBuffer=de;exports.isImageData=P;exports.isRawPngBuffer=W;exports.loadPNG=E;exports.normalizeImageInput=S;exports.runComparison=$;exports.savePNG=y;exports.saveRawPNGBuffer=R;exports.terminateWorker=_e;exports.validateMethodSupportsInput=L;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import Me,{dirname,isAbsolute,join}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,unlinkSync}from'fs';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff}from'@blazediff/core';import {compare}from'@blazediff/core-native';import {readFile}from'fs/promises';import {createRequire}from'module';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';import s from'picocolors';import {Worker}from'worker_threads';var xe=Object.defineProperty;var D=(e,t)=>()=>(e&&(t=e(e=0)),t);var ue=(e,t)=>{for(var r in t)xe(e,r,{get:t[r],enumerable:true});};var De,Se,I,u=D(()=>{De=()=>fileURLToPath(import.meta.url),Se=()=>Me.dirname(De()),I=Se();});var ce={};ue(ce,{ensureDir:()=>re,fileExists:()=>z,isFilePath:()=>C,isImageBuffer:()=>pe,isImageData:()=>P,isRawPngBuffer:()=>E,loadPNG:()=>B,normalizeImageInput:()=>S,savePNG:()=>y,saveRawPNGBuffer:()=>O});function C(e){return typeof e=="string"}function E(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function pe(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}function P(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function B(e){if(!existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function y(e,t,r,o){let n=dirname(e);existsSync(n)||mkdirSync(n,{recursive:true}),await codecPngjs.write({data:t instanceof Uint8Array?t:new Uint8Array(t),width:r,height:o},e);}function O(e,t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true}),writeFileSync(e,t);}async function S(e){if(C(e))return B(e);if(E(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function z(e){return existsSync(e)}function re(e){existsSync(e)||mkdirSync(e,{recursive:true});}var k=D(()=>{u();});function
|
|
2
|
-
`)}function
|
|
3
|
-
`)}u();X();L();k();u();var x=null,_e=0,we=false,R=new Map;function qe(){let e=join(I,"worker.js");if(existsSync(e))return e;let t=join(I,"../dist/worker.js");return existsSync(t)?t:null}function Le(){if(we)return null;if(!x){let e=qe();if(!e)return we=true,null;x=new Worker(e),x.on("message",t=>{let r=R.get(t.id);r&&(R.delete(t.id),t.success?r.resolve(t.result):r.reject(new Error(t.error)));}),x.on("error",t=>{for(let r of R.values())r.reject(t);R.clear(),x=null;}),x.on("exit",t=>{if(t!==0){let r=new Error(`Worker exited with code ${t}`);for(let o of R.values())o.reject(r);R.clear();}x=null;});}return x}async function fe(e,t){let r=Le();if(!r){let{runComparison:o}=await Promise.resolve().then(()=>(X(),he)),{loadPNG:n,normalizeImageInput:a}=await Promise.resolve().then(()=>(k(),ce));switch(e){case "normalize":return a(t.input);case "loadPNG":return n(t.filePath);case "compare":{let i=t;return o(i.received,i.baseline,i.method,i.options,i.diffOutputPath)}default:throw new Error(`Unknown request type: ${e}`)}}return new Promise((o,n)=>{let a=++_e;R.set(a,{resolve:o,reject:n});let i={id:a,type:e,payload:t};r.postMessage(i);})}function me(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function Ie(e){let t=await fe("normalize",{input:e});return me(t)}async function Ce(e){let t=await fe("loadPNG",{filePath:e});return me(t)}function Pe(e,t,r,o,n){let a=me(e);return fe("compare",{received:a,baseline:t,method:r,options:o,diffOutputPath:n})}function He(){if(x){let e=x;return x=null,e.terminate()}return Promise.resolve(0)}function Je(e){return e.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function Ke(e,t){let r=dirname(e.testPath),o=t.snapshotsDir??"__snapshots__",n=isAbsolute(o)?o:join(r,o),a=t.snapshotIdentifier??Je(e),i=t.method==="core-native"
|
|
1
|
+
import Me,{dirname,isAbsolute,join}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,unlinkSync}from'fs';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff}from'@blazediff/core';import {compare}from'@blazediff/core-native';import {readFile}from'fs/promises';import {createRequire}from'module';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';import s from'picocolors';import {Worker}from'worker_threads';var xe=Object.defineProperty;var D=(e,t)=>()=>(e&&(t=e(e=0)),t);var ue=(e,t)=>{for(var r in t)xe(e,r,{get:t[r],enumerable:true});};var De,Se,I,u=D(()=>{De=()=>fileURLToPath(import.meta.url),Se=()=>Me.dirname(De()),I=Se();});var ce={};ue(ce,{ensureDir:()=>re,fileExists:()=>z,isFilePath:()=>C,isImageBuffer:()=>pe,isImageData:()=>P,isRawPngBuffer:()=>E,loadPNG:()=>B,normalizeImageInput:()=>S,savePNG:()=>y,saveRawPNGBuffer:()=>O});function C(e){return typeof e=="string"}function E(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function pe(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}function P(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function B(e){if(!existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function y(e,t,r,o){let n=dirname(e);existsSync(n)||mkdirSync(n,{recursive:true}),await codecPngjs.write({data:t instanceof Uint8Array?t:new Uint8Array(t),width:r,height:o},e);}function O(e,t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true}),writeFileSync(e,t);}async function S(e){if(C(e))return B(e);if(E(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function z(e){return existsSync(e)}function re(e){existsSync(e)||mkdirSync(e,{recursive:true});}var k=D(()=>{u();});function G(e,t,r,o){let{width:n,height:a}=e,i=n*a;if(e.width!==t.width||e.height!==t.height)return {diffCount:i,diffPercentage:100};let f=r?new Uint8Array(i*4):void 0,p=diff(e.data,t.data,f,n,a,{threshold:o.threshold??.1,includeAA:o.includeAA??false});return {diffCount:p,diffPercentage:p/i*100,diffOutput:f}}var ae=D(()=>{u();});async function F(e,t,r,o){if(!C(e))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!C(t))throw new Error("Method 'core-native' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let n=await compare(e,t,r,{threshold:o.threshold,antialiasing:o.antialiasing});if(n.match)return {diffCount:0,diffPercentage:0};if(n.reason==="layout-diff")return {diffCount:Number.MAX_SAFE_INTEGER,diffPercentage:100};if(n.reason==="pixel-diff")return {diffCount:n.diffCount,diffPercentage:n.diffPercentage};if(n.reason==="file-not-exists")throw new Error(`Image file not found: ${n.file}`);return {diffCount:0,diffPercentage:0}}var oe=D(()=>{u();k();});function Ue(){return ne||(ne=import('@blazediff/core-wasm').then(async e=>{let t=Ae.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await e.initBlazediff(await readFile(t)),e})),ne}async function j(e,t,r,o){let{width:n,height:a}=e,i=n*a;if(e.width!==t.width||e.height!==t.height)return {diffCount:i,diffPercentage:100};let f=r?new Uint8Array(i*4):void 0,{diff:p}=await Ue(),m=await p(e.data,t.data,n,a,f,{threshold:o.threshold??.1,includeAA:o.includeAA??false});return {diffCount:m,diffPercentage:m/i*100,diffOutput:f}}var Ae,ne,ie=D(()=>{u();Ae=createRequire(import.meta.url);});function _(e,t,r,o){let{width:n,height:a}=e,i=n*a;if(e.width!==t.width||e.height!==t.height)return {score:1};let f=r?new Uint8Array(i*4):void 0;return {score:gmsd(e.data,t.data,f,n,a,{downsample:o.downsample}),diffOutput:f}}var se=D(()=>{u();});function q(e,t,r,o,n){let{width:a,height:i}=e,f=a*i;if(e.width!==t.width||e.height!==t.height)return {score:0};let p=o?new Uint8Array(f*4):void 0,m={windowSize:n.windowSize,k1:n.k1,k2:n.k2},M;switch(r){case "ssim":M=ssim(e.data,t.data,p,a,i,m);break;case "msssim":M=msssim(e.data,t.data,p,a,i,m);break;case "hitchhikers-ssim":M=hitchhikersSSIM(e.data,t.data,p,a,i,m);break;default:throw new Error(`Unknown SSIM method: ${r}`)}return {score:M,diffOutput:p}}function $(e){return e==="ssim"||e==="msssim"||e==="hitchhikers-ssim"}var L=D(()=>{u();});var he={};ue(he,{compareCore:()=>G,compareCoreNative:()=>F,compareCoreWasm:()=>j,compareGmsd:()=>_,compareSsim:()=>q,isSsimMethod:()=>$,runComparison:()=>T,validateMethodSupportsInput:()=>H});function H(e,t){if(e==="core-native"&&!C(t))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.")}async function T(e,t,r,o,n){if(H(r,e),H(r,t),r==="core-native"){let m=await F(e,t,n,o);return {diffCount:m.diffCount,diffPercentage:m.diffPercentage}}let a=P(e)?e:await S(e),i=P(t)?t:await S(t),f=n!==void 0;if($(r)){let m=q(a,i,r,f,o);return {score:m.score,diffOutput:m.diffOutput}}if(r==="gmsd"){let m=_(a,i,f,o);return {score:m.score,diffOutput:m.diffOutput}}if(r==="core-wasm"){let m=await j(a,i,f,o);return {diffCount:m.diffCount,diffPercentage:m.diffPercentage,diffOutput:m.diffOutput}}let p=G(a,i,f,o);return {diffCount:p.diffCount,diffPercentage:p.diffPercentage,diffOutput:p.diffOutput}}var X=D(()=>{u();k();ae();oe();ie();se();L();ae();oe();ie();se();L();});u();X();k();u();var N={success:s.isColorSupported?"\u2714":"\u221A",error:s.isColorSupported?"\u2716":"\xD7",info:s.isColorSupported?"\u2139":"i",arrow:s.isColorSupported?"\u2514\u2500":"'-"},ve=new Set(["ssim","msssim","hitchhikers-ssim"]);function A(e){return e.snapshotCreated?ze(e.baselinePath):e.pass?Ge():Fe(e)}function ze(e){return [`${s.green(N.success)} ${s.green("New snapshot created")}`,` ${s.dim(N.arrow)} ${s.dim(e)}`].join(`
|
|
2
|
+
`)}function Ge(){return `${s.green(N.success)} ${s.green("Image matches snapshot")}`}function Fe(e){let{method:t,baselinePath:r,receivedPath:o,diffPath:n,diffCount:a=0,diffPercentage:i,score:f=0,threshold:p,thresholdType:m,updateCommand:M}=e,c=[`${s.red(N.error)} ${s.red(s.bold("Image snapshot mismatch"))}`,""],g=12;if(c.push(` ${s.dim("Method".padEnd(g))}${t}`),c.push(` ${s.dim("Baseline".padEnd(g))}${s.dim(r)}`),c.push(` ${s.dim("Received".padEnd(g))}${s.dim(o)}`),c.push(` ${s.dim("Diff".padEnd(g))}${s.dim(n)}`),c.push(""),ve.has(t)){let U=((1-f)*100).toFixed(2);c.push(` ${s.dim("SSIM Score".padEnd(g))}${s.yellow(f.toFixed(4))} ${s.dim("(1.0 = identical)")}`),c.push(` ${s.dim("Difference".padEnd(g))}${s.yellow(`${U}%`)}`);}else if(t==="gmsd")c.push(` ${s.dim("GMSD Score".padEnd(g))}${s.yellow(f.toFixed(4))} ${s.dim("(0.0 = identical)")}`);else {let U=i?.toFixed(2)??"0.00";c.push(` ${s.dim("Difference".padEnd(g))}${s.yellow(a.toLocaleString())} pixels ${s.dim(`(${U}%)`)}`);}let Q=m==="percent"?"%":"pixels";return c.push(` ${s.dim("Threshold".padEnd(g))}${p} ${Q}`),c.push(""),c.push(` ${s.cyan(N.info)} ${s.cyan(`Run with ${M??"--update"} to update the snapshot`)}`),c.join(`
|
|
3
|
+
`)}u();X();L();k();u();var x=null,_e=0,we=false,R=new Map;function qe(){let e=join(I,"worker.js");if(existsSync(e))return e;let t=join(I,"../dist/worker.js");return existsSync(t)?t:null}function Le(){if(we)return null;if(!x){let e=qe();if(!e)return we=true,null;x=new Worker(e),x.on("message",t=>{let r=R.get(t.id);r&&(R.delete(t.id),t.success?r.resolve(t.result):r.reject(new Error(t.error)));}),x.on("error",t=>{for(let r of R.values())r.reject(t);R.clear(),x=null;}),x.on("exit",t=>{if(t!==0){let r=new Error(`Worker exited with code ${t}`);for(let o of R.values())o.reject(r);R.clear();}x=null;});}return x}async function fe(e,t){let r=Le();if(!r){let{runComparison:o}=await Promise.resolve().then(()=>(X(),he)),{loadPNG:n,normalizeImageInput:a}=await Promise.resolve().then(()=>(k(),ce));switch(e){case "normalize":return a(t.input);case "loadPNG":return n(t.filePath);case "compare":{let i=t;return o(i.received,i.baseline,i.method,i.options,i.diffOutputPath)}default:throw new Error(`Unknown request type: ${e}`)}}return new Promise((o,n)=>{let a=++_e;R.set(a,{resolve:o,reject:n});let i={id:a,type:e,payload:t};r.postMessage(i);})}function me(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function Ie(e){let t=await fe("normalize",{input:e});return me(t)}async function Ce(e){let t=await fe("loadPNG",{filePath:e});return me(t)}function Pe(e,t,r,o,n){let a=me(e);return fe("compare",{received:a,baseline:t,method:r,options:o,diffOutputPath:n})}function He(){if(x){let e=x;return x=null,e.terminate()}return Promise.resolve(0)}function Je(e){return e.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function Ke(e,t){let r=dirname(e.testPath),o=t.snapshotsDir??"__snapshots__",n=isAbsolute(o)?o:join(r,o),a=t.snapshotIdentifier??Je(e),i=t.method==="core-native"?"html":"png";return {snapshotDir:n,baselinePath:join(n,`${a}.png`),receivedPath:join(n,`${a}.received.png`),diffPath:join(n,`${a}.diff.${i}`)}}function ye(e,t,r){let o=r.failureThreshold??0,n=r.failureThresholdType??"pixel";if($(e)){let a=t.score??0;return n==="percent"?(1-a)*100<=o:a>=1-o/100}if(e==="gmsd"){let a=t.score??0;return n==="percent"?a*100<=o:a<=o/100}return n==="percent"?(t.diffPercentage??0)<=o:(t.diffCount??0)<=o}async function Qe(e,t,r){let o=Ke(r,t),{snapshotDir:n,baselinePath:a,receivedPath:i,diffPath:f}=o,p=E(e),m=p?e:null,M=t.runInWorker!==false,c=M?Ie:S,g=M?Ce:B,Q=M?(w,l,Y)=>Pe(w,l,t.method,t,Y):(w,l,Y)=>T(w,l,t.method,t,Y);re(n);let U=z(a),v=t.updateSnapshots===true?"all":t.updateSnapshots===false||t.updateSnapshots===void 0?"new":t.updateSnapshots;if(!U&&v!=="none"&&(v==="new"||v==="all")){if(m)O(a,m);else if(C(e)){let l=await g(e);await y(a,l.data,l.width,l.height);}else if(P(e))await y(a,e.data,e.width,e.height);else {let l=await c(e);await y(a,l.data,l.width,l.height);}return {pass:true,message:A({pass:true,method:t.method,snapshotCreated:true,baselinePath:a,receivedPath:i,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:a,snapshotStatus:"added"}}let h,d;t.method==="core-native"?(h=await T(e,a,t.method,t,f),d=e):(d=p?await c(e):e,h=await Q(P(d)?d:await c(d),a,f));let V=ye(t.method,h,t);if(V)return existsSync(i)&&unlinkSync(i),existsSync(f)&&unlinkSync(f),{pass:true,message:A({pass:V,method:t.method,snapshotCreated:false,baselinePath:a,receivedPath:i,diffPath:f,diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,baselinePath:a,snapshotStatus:"matched"};if(v==="all"){if(m)O(a,m);else if(C(d)){let l=await g(d);await y(a,l.data,l.width,l.height);}else P(d)&&await y(a,d.data,d.width,d.height);return existsSync(i)&&unlinkSync(i),existsSync(f)&&unlinkSync(f),{pass:true,message:A({pass:true,method:t.method,snapshotCreated:true,baselinePath:a,receivedPath:i,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:a,snapshotStatus:"updated"}}if(m)O(i,m);else if(C(d)){let w=await g(d);await y(i,w.data,w.width,w.height);}else P(d)&&await y(i,d.data,d.width,d.height);if(h.diffOutput&&t.method!=="core-native"){let w=P(d)?d:await c(d);await y(f,h.diffOutput,w.width,w.height);}return {pass:false,message:A({pass:V,method:t.method,snapshotCreated:false,baselinePath:a,receivedPath:i,diffPath:f,diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:h.diffCount,diffPercentage:h.diffPercentage,score:h.score,baselinePath:a,receivedPath:i,diffPath:f,snapshotStatus:"failed"}}async function Ve(e,t,r){let o=await T(e,t,r.method,r),n=ye(r.method,o,r);return {pass:n,message:n?"Images match.":`Images differ: ${o.diffCount??o.score} ${o.diffCount!==void 0?"pixels":"score"}`,diffCount:o.diffCount,diffPercentage:o.diffPercentage,score:o.score}}export{Ve as compareImages,z as fileExists,A as formatReport,Qe as getOrCreateSnapshot,C as isFilePath,pe as isImageBuffer,P as isImageData,E as isRawPngBuffer,B as loadPNG,S as normalizeImageInput,T as runComparison,y as savePNG,O as saveRawPNGBuffer,He as terminateWorker,H as validateMethodSupportsInput};
|
package/dist/worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var worker_threads=require('worker_threads'),fs=require('fs');require('path');var codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim');var U=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href,u=U();function p(t){return typeof t=="string"}function R(t){return (Buffer.isBuffer(t)||t instanceof Uint8Array)&&!("width"in t)}function g(t){return typeof t=="object"&&t!==null&&"data"in t&&"width"in t&&"height"in t}async function l(t){if(!fs.existsSync(t))throw new Error(`Image file not found: ${t}`);let e=await codecPngjs.codecPngjs.read(t);return {data:new Uint8Array(e.data),width:e.width,height:e.height}}async function d(t){if(p(t))return l(t);if(R(t)){let e=Buffer.isBuffer(t)?t:Buffer.from(t),o=await codecPngjs.codecPngjs.read(e);return {data:new Uint8Array(o.data),width:o.width,height:o.height}}return t.data instanceof Uint8Array?t:{data:new Uint8Array(t.data),width:t.width,height:t.height}}function w(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:n,diffPercentage:100};let f=o?new Uint8Array(n*4):void 0,m=core.diff(t.data,e.data,f,r,i,{threshold:a.threshold??.1,includeAA:a.includeAA??false});return {diffCount:m,diffPercentage:m/n*100,diffOutput:f}}async function y(t,e,o,a){if(!p(t))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!p(e))throw new Error("Method 'core-native' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let r=await coreNative.compare(t,e,o,{threshold:a.threshold,antialiasing:a.antialiasing
|
|
1
|
+
'use strict';var worker_threads=require('worker_threads'),fs=require('fs');require('path');var codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim');var U=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href,u=U();function p(t){return typeof t=="string"}function R(t){return (Buffer.isBuffer(t)||t instanceof Uint8Array)&&!("width"in t)}function g(t){return typeof t=="object"&&t!==null&&"data"in t&&"width"in t&&"height"in t}async function l(t){if(!fs.existsSync(t))throw new Error(`Image file not found: ${t}`);let e=await codecPngjs.codecPngjs.read(t);return {data:new Uint8Array(e.data),width:e.width,height:e.height}}async function d(t){if(p(t))return l(t);if(R(t)){let e=Buffer.isBuffer(t)?t:Buffer.from(t),o=await codecPngjs.codecPngjs.read(e);return {data:new Uint8Array(o.data),width:o.width,height:o.height}}return t.data instanceof Uint8Array?t:{data:new Uint8Array(t.data),width:t.width,height:t.height}}function w(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:n,diffPercentage:100};let f=o?new Uint8Array(n*4):void 0,m=core.diff(t.data,e.data,f,r,i,{threshold:a.threshold??.1,includeAA:a.includeAA??false});return {diffCount:m,diffPercentage:m/n*100,diffOutput:f}}async function y(t,e,o,a){if(!p(t))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!p(e))throw new Error("Method 'core-native' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let r=await coreNative.compare(t,e,o,{threshold:a.threshold,antialiasing:a.antialiasing});if(r.match)return {diffCount:0,diffPercentage:0};if(r.reason==="layout-diff")return {diffCount:Number.MAX_SAFE_INTEGER,diffPercentage:100};if(r.reason==="pixel-diff")return {diffCount:r.diffCount,diffPercentage:r.diffPercentage};if(r.reason==="file-not-exists")throw new Error(`Image file not found: ${r.file}`);return {diffCount:0,diffPercentage:0}}var B=module$1.createRequire(u),I;function G(){return I||(I=import('@blazediff/core-wasm').then(async t=>{let e=B.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await t.initBlazediff(await promises.readFile(e)),t})),I}async function C(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:n,diffPercentage:100};let f=o?new Uint8Array(n*4):void 0,{diff:m}=await G(),s=await m(t.data,e.data,r,i,f,{threshold:a.threshold??.1,includeAA:a.includeAA??false});return {diffCount:s,diffPercentage:s/n*100,diffOutput:f}}function P(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {score:1};let f=o?new Uint8Array(n*4):void 0;return {score:gmsd.gmsd(t.data,e.data,f,r,i,{downsample:a.downsample}),diffOutput:f}}function x(t,e,o,a,r){let{width:i,height:n}=t,f=i*n;if(t.width!==e.width||t.height!==e.height)return {score:0};let m=a?new Uint8Array(f*4):void 0,s={windowSize:r.windowSize,k1:r.k1,k2:r.k2},c;switch(o){case "ssim":c=ssim.ssim(t.data,e.data,m,i,n,s);break;case "msssim":c=msssim.msssim(t.data,e.data,m,i,n,s);break;case "hitchhikers-ssim":c=hitchhikersSsim.hitchhikersSSIM(t.data,e.data,m,i,n,s);break;default:throw new Error(`Unknown SSIM method: ${o}`)}return {score:c,diffOutput:m}}function M(t){return t==="ssim"||t==="msssim"||t==="hitchhikers-ssim"}function A(t,e){if(t==="core-native"&&!p(e))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.")}async function O(t,e,o,a,r){if(A(o,t),A(o,e),o==="core-native"){let s=await y(t,e,r,a);return {diffCount:s.diffCount,diffPercentage:s.diffPercentage}}let i=g(t)?t:await d(t),n=g(e)?e:await d(e),f=r!==void 0;if(M(o)){let s=x(i,n,o,f,a);return {score:s.score,diffOutput:s.diffOutput}}if(o==="gmsd"){let s=P(i,n,f,a);return {score:s.score,diffOutput:s.diffOutput}}if(o==="core-wasm"){let s=await C(i,n,f,a);return {diffCount:s.diffCount,diffPercentage:s.diffPercentage,diffOutput:s.diffOutput}}let m=w(i,n,f,a);return {diffCount:m.diffCount,diffPercentage:m.diffPercentage,diffOutput:m.diffOutput}}function L(t){return t.data instanceof Uint8Array?t:{data:new Uint8Array(t.data),width:t.width,height:t.height}}async function _(t){switch(t.type){case "normalize":{let{input:e}=t.payload;return d(e)}case "loadPNG":{let{filePath:e}=t.payload;return l(e)}case "compare":{let{received:e,baseline:o,method:a,options:r,diffOutputPath:i}=t.payload;return O(L(e),o,a,r,i)}default:throw new Error(`Unknown request type: ${t.type}`)}}worker_threads.parentPort&&worker_threads.parentPort.on("message",async t=>{try{let e=await _(t);worker_threads.parentPort?.postMessage({id:t.id,success:!0,result:e});}catch(e){worker_threads.parentPort?.postMessage({id:t.id,success:false,error:e instanceof Error?e.message:String(e)});}});
|
package/dist/worker.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {parentPort}from'worker_threads';import {existsSync}from'fs';import'path';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff}from'@blazediff/core';import {compare}from'@blazediff/core-native';import {readFile}from'fs/promises';import {createRequire}from'module';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';function d(t){return typeof t=="string"}function R(t){return (Buffer.isBuffer(t)||t instanceof Uint8Array)&&!("width"in t)}function l(t){return typeof t=="object"&&t!==null&&"data"in t&&"width"in t&&"height"in t}async function w(t){if(!existsSync(t))throw new Error(`Image file not found: ${t}`);let e=await codecPngjs.read(t);return {data:new Uint8Array(e.data),width:e.width,height:e.height}}async function c(t){if(d(t))return w(t);if(R(t)){let e=Buffer.isBuffer(t)?t:Buffer.from(t),o=await codecPngjs.read(e);return {data:new Uint8Array(o.data),width:o.width,height:o.height}}return t.data instanceof Uint8Array?t:{data:new Uint8Array(t.data),width:t.width,height:t.height}}function y(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:n,diffPercentage:100};let f=o?new Uint8Array(n*4):void 0,m=diff(t.data,e.data,f,r,i,{threshold:a.threshold??.1,includeAA:a.includeAA??false});return {diffCount:m,diffPercentage:m/n*100,diffOutput:f}}async function I(t,e,o,a){if(!d(t))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!d(e))throw new Error("Method 'core-native' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let r=await compare(t,e,o,{threshold:a.threshold,antialiasing:a.antialiasing
|
|
1
|
+
import {parentPort}from'worker_threads';import {existsSync}from'fs';import'path';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff}from'@blazediff/core';import {compare}from'@blazediff/core-native';import {readFile}from'fs/promises';import {createRequire}from'module';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';function d(t){return typeof t=="string"}function R(t){return (Buffer.isBuffer(t)||t instanceof Uint8Array)&&!("width"in t)}function l(t){return typeof t=="object"&&t!==null&&"data"in t&&"width"in t&&"height"in t}async function w(t){if(!existsSync(t))throw new Error(`Image file not found: ${t}`);let e=await codecPngjs.read(t);return {data:new Uint8Array(e.data),width:e.width,height:e.height}}async function c(t){if(d(t))return w(t);if(R(t)){let e=Buffer.isBuffer(t)?t:Buffer.from(t),o=await codecPngjs.read(e);return {data:new Uint8Array(o.data),width:o.width,height:o.height}}return t.data instanceof Uint8Array?t:{data:new Uint8Array(t.data),width:t.width,height:t.height}}function y(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:n,diffPercentage:100};let f=o?new Uint8Array(n*4):void 0,m=diff(t.data,e.data,f,r,i,{threshold:a.threshold??.1,includeAA:a.includeAA??false});return {diffCount:m,diffPercentage:m/n*100,diffOutput:f}}async function I(t,e,o,a){if(!d(t))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");if(!d(e))throw new Error("Method 'core-native' only supports file paths for baseline, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.");let r=await compare(t,e,o,{threshold:a.threshold,antialiasing:a.antialiasing});if(r.match)return {diffCount:0,diffPercentage:0};if(r.reason==="layout-diff")return {diffCount:Number.MAX_SAFE_INTEGER,diffPercentage:100};if(r.reason==="pixel-diff")return {diffCount:r.diffCount,diffPercentage:r.diffPercentage};if(r.reason==="file-not-exists")throw new Error(`Image file not found: ${r.file}`);return {diffCount:0,diffPercentage:0}}var B=createRequire(import.meta.url),C;function G(){return C||(C=import('@blazediff/core-wasm').then(async t=>{let e=B.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await t.initBlazediff(await readFile(e)),t})),C}async function P(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {diffCount:n,diffPercentage:100};let f=o?new Uint8Array(n*4):void 0,{diff:m}=await G(),s=await m(t.data,e.data,r,i,f,{threshold:a.threshold??.1,includeAA:a.includeAA??false});return {diffCount:s,diffPercentage:s/n*100,diffOutput:f}}function x(t,e,o,a){let{width:r,height:i}=t,n=r*i;if(t.width!==e.width||t.height!==e.height)return {score:1};let f=o?new Uint8Array(n*4):void 0;return {score:gmsd(t.data,e.data,f,r,i,{downsample:a.downsample}),diffOutput:f}}function M(t,e,o,a,r){let{width:i,height:n}=t,f=i*n;if(t.width!==e.width||t.height!==e.height)return {score:0};let m=a?new Uint8Array(f*4):void 0,s={windowSize:r.windowSize,k1:r.k1,k2:r.k2},h;switch(o){case "ssim":h=ssim(t.data,e.data,m,i,n,s);break;case "msssim":h=msssim(t.data,e.data,m,i,n,s);break;case "hitchhikers-ssim":h=hitchhikersSSIM(t.data,e.data,m,i,n,s);break;default:throw new Error(`Unknown SSIM method: ${o}`)}return {score:h,diffOutput:m}}function b(t){return t==="ssim"||t==="msssim"||t==="hitchhikers-ssim"}function O(t,e){if(t==="core-native"&&!d(e))throw new Error("Method 'core-native' only supports file paths, but received a buffer. Use method 'core', 'ssim', or 'gmsd' for buffer inputs.")}async function D(t,e,o,a,r){if(O(o,t),O(o,e),o==="core-native"){let s=await I(t,e,r,a);return {diffCount:s.diffCount,diffPercentage:s.diffPercentage}}let i=l(t)?t:await c(t),n=l(e)?e:await c(e),f=r!==void 0;if(b(o)){let s=M(i,n,o,f,a);return {score:s.score,diffOutput:s.diffOutput}}if(o==="gmsd"){let s=x(i,n,f,a);return {score:s.score,diffOutput:s.diffOutput}}if(o==="core-wasm"){let s=await P(i,n,f,a);return {diffCount:s.diffCount,diffPercentage:s.diffPercentage,diffOutput:s.diffOutput}}let m=y(i,n,f,a);return {diffCount:m.diffCount,diffPercentage:m.diffPercentage,diffOutput:m.diffOutput}}function $(t){return t.data instanceof Uint8Array?t:{data:new Uint8Array(t.data),width:t.width,height:t.height}}async function j(t){switch(t.type){case "normalize":{let{input:e}=t.payload;return c(e)}case "loadPNG":{let{filePath:e}=t.payload;return w(e)}case "compare":{let{received:e,baseline:o,method:a,options:r,diffOutputPath:i}=t.payload;return D($(e),o,a,r,i)}default:throw new Error(`Unknown request type: ${t.type}`)}}parentPort&&parentPort.on("message",async t=>{try{let e=await j(t);parentPort?.postMessage({id:t.id,success:!0,result:e});}catch(e){parentPort?.postMessage({id:t.id,success:false,error:e instanceof Error?e.message:String(e)});}});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blazediff/matcher",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Core matcher logic for visual regression testing with blazediff",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"picocolors": "^1.1.1",
|
|
36
|
+
"@blazediff/core-native": "5.0.0",
|
|
36
37
|
"@blazediff/core": "1.9.2",
|
|
37
|
-
"@blazediff/core-wasm": "
|
|
38
|
-
"@blazediff/core-native": "4.3.4",
|
|
38
|
+
"@blazediff/core-wasm": "5.0.0",
|
|
39
39
|
"@blazediff/gmsd": "1.7.1",
|
|
40
40
|
"@blazediff/codec-pngjs": "4.0.0",
|
|
41
41
|
"@blazediff/ssim": "1.7.1"
|