@likecoin/epubcheck-ts 0.6.1 → 0.6.2

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 CHANGED
@@ -114,17 +114,22 @@ if (result.valid) {
114
114
  const fs = require('node:fs');
115
115
 
116
116
  async function validate() {
117
- const { EpubCheck } = await import('epubcheck-ts');
118
-
117
+ const { EpubCheck } = require('@likecoin/epubcheck-ts');
118
+
119
119
  const epubData = fs.readFileSync('book.epub');
120
120
  const result = await EpubCheck.validate(epubData);
121
-
121
+
122
122
  console.log(result.valid ? 'Valid!' : 'Invalid');
123
123
  }
124
124
 
125
125
  validate();
126
126
  ```
127
127
 
128
+ > The XML engine (`libxml2-wasm`, ESM-only with top-level await) is lazy-loaded
129
+ > inside `EpubCheck.validate()`, so the package stays `require()`-able from
130
+ > CommonJS and importable without forcing top-level-await support on your
131
+ > bundler.
132
+
128
133
  ### Browser
129
134
 
130
135
  ```typescript
package/bin/epubcheck.js CHANGED
@@ -3,7 +3,7 @@ import { readFile, readdir, stat, writeFile } from "node:fs/promises";
3
3
  import { parseArgs } from "node:util";
4
4
  import { basename, join, relative, sep } from "node:path";
5
5
  const { EpubCheck, EPUB_VERSIONS, MessageId, toJSONReport } = await import("../dist/index.js");
6
- const VERSION = "0.6.1";
6
+ const VERSION = "0.6.2";
7
7
  const VALID_MODES = /* @__PURE__ */ new Set([
8
8
  "exp",
9
9
  "opf",
package/bin/epubcheck.ts CHANGED
@@ -22,7 +22,7 @@ import type {
22
22
  // Dynamic import to support both ESM and CJS builds
23
23
  const { EpubCheck, EPUB_VERSIONS, MessageId, toJSONReport } = await import('../dist/index.js');
24
24
 
25
- const VERSION = '0.6.1';
25
+ const VERSION = '0.6.2';
26
26
  const VALID_MODES: ReadonlySet<ValidationMode> = new Set([
27
27
  'exp',
28
28
  'opf',
package/dist/index.cjs CHANGED
@@ -1,10 +1,25 @@
1
1
  'use strict';
2
2
 
3
- var libxml2Wasm = require('libxml2-wasm');
4
3
  var cssTree = require('css-tree');
5
4
  var fflate = require('fflate');
6
5
 
7
- // src/content/validator.ts
6
+ // src/util/xml-engine.ts
7
+ var engine;
8
+ async function loadXmlEngine() {
9
+ engine ??= await import('libxml2-wasm');
10
+ }
11
+ function getXmlDocument() {
12
+ if (!engine) {
13
+ throw new Error("libxml2-wasm not initialized \u2014 call loadXmlEngine() first");
14
+ }
15
+ return engine.XmlDocument;
16
+ }
17
+ function getXmlElement() {
18
+ if (!engine) {
19
+ throw new Error("libxml2-wasm not initialized \u2014 call loadXmlEngine() first");
20
+ }
21
+ return engine.XmlElement;
22
+ }
8
23
 
9
24
  // src/messages/messages.ts
10
25
  var severityOverrides = /* @__PURE__ */ new Map();
@@ -5084,7 +5099,7 @@ var SKMValidator = class {
5084
5099
  const content = typeof data === "string" ? data : new TextDecoder().decode(data);
5085
5100
  let doc = null;
5086
5101
  try {
5087
- doc = libxml2Wasm.XmlDocument.fromString(content);
5102
+ doc = getXmlDocument().fromString(content);
5088
5103
  } catch {
5089
5104
  pushMessage(context.messages, {
5090
5105
  id: MessageId.RSC_016,
@@ -5321,7 +5336,7 @@ var SMILValidator = class {
5321
5336
  const content = typeof data === "string" ? data : new TextDecoder().decode(data);
5322
5337
  let doc = null;
5323
5338
  try {
5324
- doc = libxml2Wasm.XmlDocument.fromString(content);
5339
+ doc = getXmlDocument().fromString(content);
5325
5340
  } catch {
5326
5341
  pushMessage(context.messages, {
5327
5342
  id: MessageId.RSC_016,
@@ -6231,7 +6246,7 @@ var ContentValidator = class {
6231
6246
  const svgContent = new TextDecoder().decode(svgData);
6232
6247
  let doc;
6233
6248
  try {
6234
- doc = libxml2Wasm.XmlDocument.fromString(svgContent);
6249
+ doc = getXmlDocument().fromString(svgContent);
6235
6250
  this.extractAndRegisterIDs(path, doc.root, registry);
6236
6251
  } catch (e) {
6237
6252
  pushMessage(context.messages, {
@@ -6249,7 +6264,7 @@ var ContentValidator = class {
6249
6264
  const svgContent = new TextDecoder().decode(svgData);
6250
6265
  let doc;
6251
6266
  try {
6252
- doc = libxml2Wasm.XmlDocument.fromString(svgContent);
6267
+ doc = getXmlDocument().fromString(svgContent);
6253
6268
  } catch {
6254
6269
  return;
6255
6270
  }
@@ -6295,7 +6310,7 @@ var ContentValidator = class {
6295
6310
  const svgContent = new TextDecoder().decode(svgData);
6296
6311
  let doc;
6297
6312
  try {
6298
- doc = libxml2Wasm.XmlDocument.fromString(svgContent);
6313
+ doc = getXmlDocument().fromString(svgContent);
6299
6314
  } catch {
6300
6315
  return;
6301
6316
  }
@@ -6645,7 +6660,7 @@ var ContentValidator = class {
6645
6660
  }
6646
6661
  let doc = null;
6647
6662
  try {
6648
- doc = libxml2Wasm.XmlDocument.fromString(content);
6663
+ doc = getXmlDocument().fromString(content);
6649
6664
  } catch (error) {
6650
6665
  if (error instanceof Error) {
6651
6666
  const { message, line, column } = this.parseLibxmlError(error.message);
@@ -8730,11 +8745,12 @@ var ContentValidator = class {
8730
8745
  }
8731
8746
  return Number.parseInt(el.name.substring(1), 10);
8732
8747
  };
8748
+ const XmlElement = getXmlElement();
8733
8749
  const directElementChildren = (parent) => {
8734
8750
  const out = [];
8735
8751
  let n = parent.firstChild;
8736
8752
  while (n) {
8737
- if (n instanceof libxml2Wasm.XmlElement) out.push(n);
8753
+ if (n instanceof XmlElement) out.push(n);
8738
8754
  n = n.next;
8739
8755
  }
8740
8756
  return out;
@@ -8888,7 +8904,7 @@ var ContentValidator = class {
8888
8904
  let prev = p.prev;
8889
8905
  let hasHeadingBefore = false;
8890
8906
  while (prev) {
8891
- if (prev instanceof libxml2Wasm.XmlElement && /^h[1-6]$/.test(prev.name)) {
8907
+ if (prev instanceof XmlElement && /^h[1-6]$/.test(prev.name)) {
8892
8908
  hasHeadingBefore = true;
8893
8909
  break;
8894
8910
  }
@@ -10338,11 +10354,13 @@ function toJSONReport(result) {
10338
10354
  2
10339
10355
  );
10340
10356
  }
10357
+
10358
+ // src/ncx/validator.ts
10341
10359
  var NCXValidator = class {
10342
10360
  validate(context, ncxContent, ncxPath, registry) {
10343
10361
  let doc = null;
10344
10362
  try {
10345
- doc = libxml2Wasm.XmlDocument.fromString(ncxContent);
10363
+ doc = getXmlDocument().fromString(ncxContent);
10346
10364
  } catch (error) {
10347
10365
  if (error instanceof Error) {
10348
10366
  pushMessage(context.messages, {
@@ -12099,11 +12117,11 @@ var RelaxNGValidator = class extends BaseSchemaValidator {
12099
12117
  try {
12100
12118
  const libxml2 = await import('libxml2-wasm');
12101
12119
  const LibRelaxNGValidator = libxml2.RelaxNGValidator;
12102
- const { XmlDocument: XmlDocument5 } = libxml2;
12103
- const doc = XmlDocument5.fromString(xml);
12120
+ const { XmlDocument } = libxml2;
12121
+ const doc = XmlDocument.fromString(xml);
12104
12122
  try {
12105
12123
  const schemaContent = await loadSchema(schemaPath);
12106
- const schemaDoc = XmlDocument5.fromString(schemaContent);
12124
+ const schemaDoc = XmlDocument.fromString(schemaContent);
12107
12125
  try {
12108
12126
  const validator = LibRelaxNGValidator.fromDoc(schemaDoc);
12109
12127
  try {
@@ -12313,6 +12331,7 @@ var EpubCheck = class _EpubCheck {
12313
12331
  */
12314
12332
  async check(data, filename) {
12315
12333
  const startTime = performance.now();
12334
+ await loadXmlEngine();
12316
12335
  const context = {
12317
12336
  data,
12318
12337
  options: this.options,
@@ -12354,6 +12373,7 @@ var EpubCheck = class _EpubCheck {
12354
12373
  */
12355
12374
  async checkExpanded(files) {
12356
12375
  const startTime = performance.now();
12376
+ await loadXmlEngine();
12357
12377
  const context = {
12358
12378
  data: new Uint8Array(0),
12359
12379
  options: this.options,
@@ -12398,6 +12418,7 @@ var EpubCheck = class _EpubCheck {
12398
12418
  async checkSingleFile(data, filename) {
12399
12419
  const startTime = performance.now();
12400
12420
  const mode = this.options.mode;
12421
+ await loadXmlEngine();
12401
12422
  const context = {
12402
12423
  data: new Uint8Array(0),
12403
12424
  options: this.options,