@fontmin-rs/wasm 0.1.0-beta.2 → 0.1.0-beta.3

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.
Binary file
package/dist/index.d.mts CHANGED
@@ -5,6 +5,14 @@ type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Modul
5
5
  declare function isWasmInitialized(): boolean;
6
6
  declare function initWasm(input?: InitInput): Promise<void>;
7
7
  //#endregion
8
+ //#region src/diagnostics.d.ts
9
+ type FontminDiagnosticCode = 'fontmin::config' | 'fontmin::convert_failed' | 'fontmin::invalid_font' | 'fontmin::io' | 'fontmin::missing_glyph' | 'fontmin::napi_bridge_failed' | 'fontmin::plugin_failed' | 'fontmin::unsupported_format';
10
+ declare class FontminDiagnosticError extends Error {
11
+ readonly name = "FontminDiagnosticError";
12
+ readonly code: FontminDiagnosticCode;
13
+ constructor(code: FontminDiagnosticCode, message: string, options?: ErrorOptions);
14
+ }
15
+ //#endregion
8
16
  //#region types.d.ts
9
17
  type FontFormat = 'ttf' | 'otf' | 'woff' | 'woff2' | 'eot' | 'svg' | 'css' | 'unknown';
10
18
  type OutputFormat = 'ttf' | 'woff' | 'woff2' | 'eot' | 'svg' | 'css';
@@ -204,4 +212,4 @@ interface BrowserOptimizeConfig {
204
212
  }
205
213
  declare function optimizeBrowser(config: BrowserOptimizeConfig): Promise<BrowserAsset[]>;
206
214
  //#endregion
207
- export { type BrowserAsset, type BrowserOptimizeConfig, type BrowserPlugin, type BrowserPluginContext, type CoverageOptions, type CoverageReport, type CssFontSource, type CssGlyph, type CssOptions, type DeliverySlice, type DeliverySlicesOptions, type FontFormat, type FontInfo, type FontMetadata, type FontminCompatPresetOptions, type GlyphOptions, type LayoutSubsetMode, type MaybePromise, type MissingGlyphPolicy, type ModernWebOptions, type Otf2TtfOptions, type Otf2TtfPluginOptions, type OutputFormat, type SubsetOptions, type Svg2TtfOptions, type Svg2TtfPluginOptions, type SvgIcon, type Svgs2TtfOptions, type Svgs2TtfPluginOptions, type Ttf2EotOptions, type Ttf2EotPluginOptions, type Ttf2SvgOptions, type Ttf2SvgPluginOptions, type Ttf2Woff2Options, type Ttf2Woff2PluginOptions, type Ttf2WoffPluginOptions, type WoffOptions, analyzeCoverage, css, deliverySlices, eotToTtf, fontminCompatPreset, generateFontFaceCss, glyph, initWasm, inspect, isWasmInitialized, modernWeb, optimizeBrowser, otf2ttf, otfToTtf, subsetTtf, svg2ttf, svgFontToTtf, svgs2ttf, svgsToTtf, ttf2eot, ttf2svg, ttf2woff, ttf2woff2, ttfToEot, ttfToSvg, ttfToWoff, ttfToWoff2, validateWoff2, woff2ToTtf, woffToTtf };
215
+ export { type BrowserAsset, type BrowserOptimizeConfig, type BrowserPlugin, type BrowserPluginContext, type CoverageOptions, type CoverageReport, type CssFontSource, type CssGlyph, type CssOptions, type DeliverySlice, type DeliverySlicesOptions, type FontFormat, type FontInfo, type FontMetadata, type FontminCompatPresetOptions, type FontminDiagnosticCode, FontminDiagnosticError, type GlyphOptions, type LayoutSubsetMode, type MaybePromise, type MissingGlyphPolicy, type ModernWebOptions, type Otf2TtfOptions, type Otf2TtfPluginOptions, type OutputFormat, type SubsetOptions, type Svg2TtfOptions, type Svg2TtfPluginOptions, type SvgIcon, type Svgs2TtfOptions, type Svgs2TtfPluginOptions, type Ttf2EotOptions, type Ttf2EotPluginOptions, type Ttf2SvgOptions, type Ttf2SvgPluginOptions, type Ttf2Woff2Options, type Ttf2Woff2PluginOptions, type Ttf2WoffPluginOptions, type WoffOptions, analyzeCoverage, css, deliverySlices, eotToTtf, fontminCompatPreset, generateFontFaceCss, glyph, initWasm, inspect, isWasmInitialized, modernWeb, optimizeBrowser, otf2ttf, otfToTtf, subsetTtf, svg2ttf, svgFontToTtf, svgs2ttf, svgsToTtf, ttf2eot, ttf2svg, ttf2woff, ttf2woff2, ttfToEot, ttfToSvg, ttfToWoff, ttfToWoff2, validateWoff2, woff2ToTtf, woffToTtf };
package/dist/index.mjs CHANGED
@@ -9,24 +9,62 @@ function assertWasmInitialized() {
9
9
  }
10
10
  async function getWasmModule() {
11
11
  assertWasmInitialized();
12
- return initialization;
12
+ const module = initialization;
13
+ if (module === void 0) throw new Error("fontmin-rs WASM runtime initialization is unavailable");
14
+ return module;
13
15
  }
14
16
  async function initWasm(input) {
15
- initialization ??= import("./fontmin_wasm_core-Cn_EVAE9.mjs").then(async (module) => {
16
- await module.default(input === void 0 ? void 0 : { module_or_path: input });
17
- if (module.runtime_name() !== "fontmin-rs") throw new Error("fontmin-rs WASM runtime did not initialize correctly");
18
- initialized = true;
19
- return module;
20
- });
17
+ initialization ??= initializeWasm(input);
21
18
  await initialization;
22
19
  }
20
+ async function initializeWasm(input) {
21
+ const module = await import("./fontmin_wasm_core-Cn_EVAE9.mjs");
22
+ await module.default(input === void 0 ? void 0 : { module_or_path: input });
23
+ if (module.runtime_name() !== "fontmin-rs") throw new Error("fontmin-rs WASM runtime did not initialize correctly");
24
+ initialized = true;
25
+ return module;
26
+ }
27
+ //#endregion
28
+ //#region src/diagnostics.ts
29
+ const bridgeDiagnosticPattern = /^\[(?<code>fontmin::[a-z_]+)\] (?<message>[\s\S]+)$/u;
30
+ var FontminDiagnosticError = class extends Error {
31
+ name = "FontminDiagnosticError";
32
+ code;
33
+ constructor(code, message, options) {
34
+ super(message, options);
35
+ this.code = code;
36
+ }
37
+ };
38
+ function normalizeFontminDiagnostic(error) {
39
+ let message;
40
+ if (error instanceof Error) message = error.message;
41
+ else if (typeof error === "string") message = error;
42
+ const match = message?.match(bridgeDiagnosticPattern);
43
+ if (match === null || match === void 0) {
44
+ if (error instanceof Error) return error;
45
+ if (typeof error === "string") return new Error(error, { cause: error });
46
+ return new Error("fontmin-rs WASM operation failed", { cause: error });
47
+ }
48
+ const code = match.groups?.["code"];
49
+ const diagnosticMessage = match.groups?.["message"];
50
+ if (code === void 0 || diagnosticMessage === void 0) return new Error("fontmin-rs WASM operation returned an invalid diagnostic", { cause: error });
51
+ return new FontminDiagnosticError(code, diagnosticMessage, { cause: error });
52
+ }
53
+ function withFontminDiagnostics(operation) {
54
+ try {
55
+ return operation();
56
+ } catch (error) {
57
+ throw normalizeFontminDiagnostic(error);
58
+ }
59
+ }
23
60
  //#endregion
24
61
  //#region src/native.ts
25
62
  function bytes(value) {
26
63
  return new Uint8Array(value);
27
64
  }
28
65
  async function binary(operation, input, options = {}) {
29
- return bytes((await getWasmModule()).transform(operation, input, options));
66
+ const wasm = await getWasmModule();
67
+ return withFontminDiagnostics(() => bytes(wasm.transform(operation, input, options)));
30
68
  }
31
69
  async function subsetTtf(input, options = {}) {
32
70
  if ((options.missingGlyphs ?? "warn") === "warn") {
@@ -36,7 +74,8 @@ async function subsetTtf(input, options = {}) {
36
74
  return binary("subsetTtf", input, options);
37
75
  }
38
76
  async function analyzeCoverage(input, options = {}) {
39
- return (await getWasmModule()).transform("analyzeCoverage", input, options);
77
+ const wasm = await getWasmModule();
78
+ return withFontminDiagnostics(() => wasm.transform("analyzeCoverage", input, options));
40
79
  }
41
80
  async function ttfToWoff(input, options = {}) {
42
81
  return binary("ttfToWoff", input, options);
@@ -51,7 +90,8 @@ async function woff2ToTtf(input) {
51
90
  return binary("woff2ToTtf", input);
52
91
  }
53
92
  async function validateWoff2(input) {
54
- (await getWasmModule()).transform("validateWoff2", input, {});
93
+ const wasm = await getWasmModule();
94
+ withFontminDiagnostics(() => wasm.transform("validateWoff2", input, {}));
55
95
  }
56
96
  async function ttfToEot(input, options = {}) {
57
97
  return binary("ttfToEot", input, options);
@@ -60,22 +100,27 @@ async function eotToTtf(input) {
60
100
  return binary("eotToTtf", input);
61
101
  }
62
102
  async function ttfToSvg(input, options = {}) {
63
- return (await getWasmModule()).transform("ttfToSvg", input, options);
103
+ const wasm = await getWasmModule();
104
+ return withFontminDiagnostics(() => wasm.transform("ttfToSvg", input, options));
64
105
  }
65
106
  async function svgFontToTtf(input, options = {}) {
66
- return bytes((await getWasmModule()).transform_text("svgFontToTtf", input, options));
107
+ const wasm = await getWasmModule();
108
+ return withFontminDiagnostics(() => bytes(wasm.transform_text("svgFontToTtf", input, options)));
67
109
  }
68
110
  async function svgsToTtf(inputs, options = {}) {
69
- return bytes((await getWasmModule()).transform_icons(inputs, options));
111
+ const wasm = await getWasmModule();
112
+ return withFontminDiagnostics(() => bytes(wasm.transform_icons(inputs, options)));
70
113
  }
71
114
  async function otfToTtf(input, options = {}) {
72
115
  return binary("otfToTtf", input, options);
73
116
  }
74
117
  async function inspect(input) {
75
- return (await getWasmModule()).transform("inspect", input, {});
118
+ const wasm = await getWasmModule();
119
+ return withFontminDiagnostics(() => wasm.transform("inspect", input, {}));
76
120
  }
77
121
  async function generateFontFaceCss(sources, options = {}) {
78
- return (await getWasmModule()).generate_css(sources, options);
122
+ const wasm = await getWasmModule();
123
+ return withFontminDiagnostics(() => wasm.generate_css(sources, options));
79
124
  }
80
125
  function coverageOptions(options) {
81
126
  const coverage = {};
@@ -298,9 +343,9 @@ async function optimizeBrowser(config) {
298
343
  const result = await plugin.transform(asset, context);
299
344
  if (result === null) continue;
300
345
  if (result === void 0) transformed.push(asset);
301
- else transformed.push(...(Array.isArray(result) ? result : [result]).map(formatAsset));
346
+ else transformed.push(...(Array.isArray(result) ? result : [result]).map((asset) => formatAsset(asset)));
302
347
  }
303
- assets = transformed.concat(emitted);
348
+ assets = [...transformed, ...emitted];
304
349
  continue;
305
350
  }
306
351
  if (plugin.name === "otf2ttf" && optionsOf(plugin).clone === false) {
@@ -312,7 +357,7 @@ async function optimizeBrowser(config) {
312
357
  const converted = await convert(asset, plugin);
313
358
  if (converted !== void 0) additions.push(converted);
314
359
  }
315
- assets = assets.concat(additions);
360
+ assets = [...assets, ...additions];
316
361
  }
317
362
  return assets;
318
363
  }
@@ -348,11 +393,11 @@ function replaceExtension(fileName, extension) {
348
393
  return `${fileName.replace(/\.[^.]+$/u, "")}.${extension}`;
349
394
  }
350
395
  function appendFileNameSuffix(fileName, suffix) {
351
- const extension = fileName.match(/(\.[^.]+)$/u)?.[1] ?? "";
396
+ const extension = fileName.match(/(?<extension>\.[^.]+)$/u)?.groups?.["extension"] ?? "";
352
397
  return `${extension === "" ? fileName : fileName.slice(0, -extension.length)}-${suffix}${extension}`;
353
398
  }
354
399
  function toKebabCase(value) {
355
- return value.trim().replaceAll(/([a-z])([A-Z])/gu, "$1-$2").replaceAll(/\s+/gu, "-").toLowerCase();
400
+ return value.trim().replaceAll(/(?<lower>[a-z])(?<upper>[A-Z])/gu, "$<lower>-$<upper>").replaceAll(/\s+/gu, "-").toLowerCase();
356
401
  }
357
402
  //#endregion
358
- export { analyzeCoverage, css, deliverySlices, eotToTtf, fontminCompatPreset, generateFontFaceCss, glyph, initWasm, inspect, isWasmInitialized, modernWeb, optimizeBrowser, otf2ttf, otfToTtf, subsetTtf, svg2ttf, svgFontToTtf, svgs2ttf, svgsToTtf, ttf2eot, ttf2svg, ttf2woff, ttf2woff2, ttfToEot, ttfToSvg, ttfToWoff, ttfToWoff2, validateWoff2, woff2ToTtf, woffToTtf };
403
+ export { FontminDiagnosticError, analyzeCoverage, css, deliverySlices, eotToTtf, fontminCompatPreset, generateFontFaceCss, glyph, initWasm, inspect, isWasmInitialized, modernWeb, optimizeBrowser, otf2ttf, otfToTtf, subsetTtf, svg2ttf, svgFontToTtf, svgs2ttf, svgsToTtf, ttf2eot, ttf2svg, ttf2woff, ttf2woff2, ttfToEot, ttfToSvg, ttfToWoff, ttfToWoff2, validateWoff2, woff2ToTtf, woffToTtf };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fontmin-rs/wasm",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.3",
4
4
  "description": "Browser WASM runtime for fontmin-rs.",
5
5
  "license": "MIT",
6
6
  "repository": {