Skip to content

Commit

Permalink
fix(icons-to-woff.js): enforce deterministic output
Browse files Browse the repository at this point in the history
Ensure builds with the same set of inputs results in an identical font hash for deterministic
builds.

re jantimon#53
  • Loading branch information
kaidjohnson committed Jan 5, 2022
1 parent 0de5bd2 commit ad9e583
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lib/icons-to-woff.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ module.exports = function createIconFont (fs, icons, options) {
name: options.name,
normalize: true,
fontHeight: options.enforcedSvgHeight ? options.enforcedSvgHeight : undefined,
log: function () {},
error: /** @param {any} err */function (err) {
reject(err);
}
log: function () {}
});
let svgFont = '';
fontStream
Expand Down Expand Up @@ -58,7 +55,7 @@ module.exports = function createIconFont (fs, icons, options) {
});
fontStream.end();
})
.then((svgFont) => svg2ttf(svgFont, {}).buffer)
.then((svgFont) => svg2ttf(svgFont, { ts: 0 }).buffer)
.then((ttfFont) => ttf2woff(ttfFont).buffer)
.then((woffFont) => Buffer.from(woffFont).toString('base64'));
};
1 change: 1 addition & 0 deletions test/fixtures/malformed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions test/icons-to-woff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
/* eslint max-len: off, quotes:off, arrow-parens:off */
import createIconFont from '../lib/icons-to-woff.js';
import fs from 'fs';
import test from 'ava';

test('should throw an error when an svg cannot be found', async (t) => {
let error;
try {
await createIconFont(fs, [
'./test/fixtures/does-not-exist.svg'
], {
name: 'test'
});
} catch (e) {
error = e.message;
}

t.is(error.substring(0, 33), 'ENOENT: no such file or directory');
t.pass();
});

test('should throw an error when a malformed svg is provided', async (t) => {
let error;
try {
await createIconFont(fs, [
'./test/fixtures/malformed.svg'
], {
name: 'test'
});
} catch (e) {
error = e.message;
}

t.is(error.substring(0, 32), 'Non-whitespace before first tag.');
t.pass();
});

test('should produce the same output when receiving the same input and configuration', async (t) => {
const svgs = [
'./test/fixtures/account-494x512.svg',
'./test/fixtures/account-986x1024.svg'
];

const test1 = await createIconFont(fs, svgs, {
name: 'test'
});

await new Promise((resolve) => setTimeout(resolve, 1000));

const test2 = await createIconFont(fs, svgs, {
name: 'test'
});

t.is(test1, test2);
t.pass();
});

test('should produce different output when receiving the same input but different fontHeight', async (t) => {
const svgs = [
'./test/fixtures/account-494x512.svg',
'./test/fixtures/account-986x1024.svg'
];

const test1 = await createIconFont(fs, svgs, {
enforcedSvgHeight: 32
});

await new Promise((resolve) => setTimeout(resolve, 1000));

const test2 = await createIconFont(fs, svgs, {
enforcedSvgHeight: 16
});

t.not(test1, test2);
t.pass();
});

0 comments on commit ad9e583

Please sign in to comment.