Skip to content

Commit

Permalink
feat(core): better choice between builds
Browse files Browse the repository at this point in the history
also allow to debug the version that is in use with the `DEBUG=ttf2woff2` env var.

fix #22
fix #46
  • Loading branch information
nfroidure committed Jul 20, 2024
1 parent 4319d41 commit ff54055
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@
"dependencies": {
"bindings": "^1.5.0",
"bufferstreams": "^4.0.0",
"debug": "^4.3.5",
"nan": "^2.20.0",
"node-gyp": "^10.2.0"
"node-gyp": "^10.2.0",
"yerror": "^8.0.0"
},
"devDependencies": {
"@eslint/js": "^9.7.0",
Expand Down Expand Up @@ -192,4 +194,4 @@
"overrides": {
"eslint": "^9.7.0"
}
}
}
44 changes: 38 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
let ttf2woff2: (input: Buffer) => Buffer;
import { YError, printStackTrace } from 'yerror';
import debug from 'debug';
import { env } from 'node:process';

try {
ttf2woff2 = (await import('bindings'))('addon.node').convert;
} catch (err) {
ttf2woff2 = (await import('../jssrc/index.js')).default;
const doDebug = debug('ttf2woff2');
let ttf2woff2: undefined | ((input: Buffer) => Buffer) = undefined;

if (
!env.TTF2WOFF2_VERSION ||
env.TTF2WOFF2_VERSION?.toLowerCase() === 'native'
) {
try {
ttf2woff2 = (await import('bindings'))('addon.node').convert;
doDebug('✅ Using native version.');
} catch (err) {
doDebug(
'❌ Could not load the native version.',
printStackTrace(err as Error),
);
}
}

if (!env.TTF2WOFF2_VERSION || env.TTF2WOFF2_VERSION?.toLowerCase() === 'wasm') {
if (!ttf2woff2) {
try {
ttf2woff2 = (await import('../jssrc/index.js')).default;
doDebug('✅ Using WASM version.');
} catch (err) {
doDebug(
'❌ Could not load the WASM version.',
printStackTrace(err as Error),
);
}
}
}

if (!ttf2woff2) {
throw new YError('E_UNABLE_TO_LOAD_TTF2WOFF2', env.TTF2WOFF2_VERSION);
}

export default ttf2woff2;
export default ttf2woff2 as NonNullable<typeof ttf2woff2>;

0 comments on commit ff54055

Please sign in to comment.