mdream 1.5.8 → 1.5.11

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/dist/worker.mjs CHANGED
@@ -5,7 +5,9 @@ const _pending = /* @__PURE__ */ new Map();
5
5
  const WASM_RE = /\.wasm$/;
6
6
  function getWorkerBlob(wasmUrl) {
7
7
  const code = `
8
- import init, { htmlToMarkdownResult } from '${wasmUrl.replace(WASM_RE, ".js")}';
8
+ // namespace import: the URL is caller-supplied and may point at an older build
9
+ import init, * as engine from '${wasmUrl.replace(WASM_RE, ".js")}';
10
+ const { htmlToMarkdownResult } = engine;
9
11
 
10
12
  let ready = false;
11
13
 
@@ -30,7 +32,9 @@ self.onmessage = function(e) {
30
32
  const result = htmlToMarkdownResult(msg.html, msg.options || {});
31
33
  self.postMessage({ id: msg.id, type: 'result', data: result.markdown || '' });
32
34
  } catch (err) {
33
- self.postMessage({ id: msg.id, type: 'error', message: err.message });
35
+ // WASM aborts on panic (#195): the message is stashed by the panic hook
36
+ const panic = engine.__mdreamTakePanicMessage?.();
37
+ self.postMessage({ id: msg.id, type: 'error', message: panic || err.message });
34
38
  }
35
39
  }
36
40
  };`;
package/napi/index.mjs CHANGED
@@ -527,47 +527,163 @@ function requireNative() {
527
527
  }
528
528
  }
529
529
 
530
- nativeBinding = requireNative()
530
+ function createLoadErrorChain(errors) {
531
+ return errors.reduce((previous, current) => {
532
+ let message
533
+ try {
534
+ message =
535
+ current && typeof current.message === 'string'
536
+ ? current.message
537
+ : String(current)
538
+ } catch {
539
+ message = 'Unknown error'
540
+ }
541
+ const error = new Error(message)
542
+ error.cause = previous
543
+ return error
544
+ }, null)
545
+ }
531
546
 
532
547
  // NAPI_RS_FORCE_WASI is a tri-state flag:
533
548
  // unset / any other value → native binding preferred, WASI is only a fallback
534
- // 'true' → force WASI fallback even if native loaded
535
- // 'error' → force WASI and throw if no WASI binding is found
549
+ // 'true' → prefer WASI, but retain native as a lazy fallback
550
+ // 'error' → require WASI without initializing a native fallback
536
551
  // Treating any non-empty string as truthy (the historical behavior) meant
537
552
  // NAPI_RS_FORCE_WASI=false, NAPI_RS_FORCE_WASI=0, etc. inadvertently triggered
538
553
  // the WASI path, causing ENOENT for packages shipped without a .wasi.cjs file.
554
+ //
555
+ // NAPI_RS_WASI_FLAVOR selects one exact generated flavor and implies strict
556
+ // WASI loading. It never crosses into another flavor or falls back to native.
557
+ const __napiWasiFlavors = ["wasm32-wasi"]
558
+ const __napiWasiFlavor = process.env.NAPI_RS_WASI_FLAVOR
559
+ const __napiWasiFlavorRequested =
560
+ typeof __napiWasiFlavor === 'string' && __napiWasiFlavor.length > 0
561
+ if (
562
+ __napiWasiFlavorRequested &&
563
+ __napiWasiFlavors.indexOf(__napiWasiFlavor) === -1
564
+ ) {
565
+ throw new Error(
566
+ 'Unsupported WASI flavor "' +
567
+ __napiWasiFlavor +
568
+ '". Available flavors: ' +
569
+ __napiWasiFlavors.join(', '),
570
+ )
571
+ }
572
+ const forceWasiError = process.env.NAPI_RS_FORCE_WASI === 'error'
539
573
  const forceWasi =
540
- process.env.NAPI_RS_FORCE_WASI === 'true' || process.env.NAPI_RS_FORCE_WASI === 'error'
574
+ process.env.NAPI_RS_FORCE_WASI === 'true' ||
575
+ forceWasiError ||
576
+ __napiWasiFlavorRequested
577
+
578
+ if (!forceWasi) {
579
+ nativeBinding = requireNative()
580
+ }
541
581
 
542
582
  if (!nativeBinding || forceWasi) {
543
583
  let wasiBinding = null
544
- let wasiBindingError = null
545
- try {
546
- wasiBinding = require('./rust.wasi.cjs')
547
- nativeBinding = wasiBinding
548
- } catch (err) {
549
- if (forceWasi) {
550
- wasiBindingError = err
584
+ let wasiBindingLoaded = false
585
+ const wasiBindingErrors = []
586
+ const __napiWasiResolveCandidate = (specifier, isPackage, localArtifacts) => {
587
+ try {
588
+ require.resolve(specifier)
589
+ } catch (resolveError) {
590
+ if (!resolveError || resolveError.code !== 'MODULE_NOT_FOUND') {
591
+ throw resolveError
592
+ }
593
+ if (isPackage) {
594
+ try {
595
+ require.resolve(specifier + '/package.json')
596
+ } catch (packageError) {
597
+ if (packageError && packageError.code === 'MODULE_NOT_FOUND') {
598
+ return resolveError
599
+ }
600
+ // An exports restriction proves the package exists even when its
601
+ // package.json is not public. Preserve the root resolution failure.
602
+ throw resolveError
603
+ }
604
+ // The package exists but its main/export target is broken.
605
+ throw resolveError
606
+ }
607
+ return resolveError
608
+ }
609
+ if (localArtifacts) {
610
+ let artifactError = null
611
+ for (let i = 0; i < localArtifacts.length; i++) {
612
+ try {
613
+ require.resolve(localArtifacts[i])
614
+ return null
615
+ } catch (resolveError) {
616
+ if (!resolveError || resolveError.code !== 'MODULE_NOT_FOUND') {
617
+ throw resolveError
618
+ }
619
+ artifactError = resolveError
620
+ }
621
+ }
622
+ return artifactError
551
623
  }
624
+ return null
552
625
  }
553
- if (!nativeBinding || forceWasi) {
626
+ if (!wasiBindingLoaded && (!__napiWasiFlavorRequested || __napiWasiFlavor === "wasm32-wasi")) {
627
+ let candidateError = null
628
+ let candidateFailed = false
554
629
  try {
555
- wasiBinding = require('@mdream/rust-wasm32-wasi')
556
- nativeBinding = wasiBinding
630
+ candidateError = __napiWasiResolveCandidate('./rust.wasi.cjs', false, ["./rust.wasm32-wasi.debug.wasm","./rust.wasm32-wasi.wasm"])
631
+ candidateFailed = candidateError !== null
632
+ if (!candidateFailed) {
633
+ wasiBinding = require('./rust.wasi.cjs')
634
+ nativeBinding = wasiBinding
635
+ wasiBindingLoaded = true
636
+ }
557
637
  } catch (err) {
558
- if (forceWasi) {
559
- if (!wasiBindingError) {
560
- wasiBindingError = err
561
- } else {
562
- wasiBindingError.cause = err
638
+ candidateError = err
639
+ candidateFailed = true
640
+ }
641
+ if (candidateFailed) {
642
+ wasiBindingErrors.push(candidateError)
643
+ loadErrors.push(candidateError)
644
+ }
645
+ }
646
+ if (!wasiBindingLoaded && (!__napiWasiFlavorRequested || __napiWasiFlavor === "wasm32-wasi")) {
647
+ let candidateError = null
648
+ let candidateFailed = false
649
+ try {
650
+ candidateError = __napiWasiResolveCandidate('@mdream/rust-wasm32-wasi', true, undefined)
651
+ candidateFailed = candidateError !== null
652
+ if (!candidateFailed) {
653
+ if (process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
654
+ const bindingPackageVersion = require('@mdream/rust-wasm32-wasi/package.json').version
655
+ if (bindingPackageVersion !== '0.17.0') {
656
+ throw new Error(`WASI binding package version mismatch, expected 0.17.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
657
+ }
563
658
  }
564
- loadErrors.push(err)
659
+ wasiBinding = require('@mdream/rust-wasm32-wasi')
660
+ nativeBinding = wasiBinding
661
+ wasiBindingLoaded = true
565
662
  }
663
+ } catch (err) {
664
+ candidateError = err
665
+ candidateFailed = true
666
+ }
667
+ if (candidateFailed) {
668
+ wasiBindingErrors.push(candidateError)
669
+ loadErrors.push(candidateError)
566
670
  }
567
671
  }
568
- if (process.env.NAPI_RS_FORCE_WASI === 'error' && !wasiBinding) {
569
- const error = new Error('WASI binding not found and NAPI_RS_FORCE_WASI is set to error')
570
- error.cause = wasiBindingError
672
+ if (
673
+ !wasiBindingLoaded &&
674
+ forceWasi &&
675
+ !forceWasiError &&
676
+ !__napiWasiFlavorRequested
677
+ ) {
678
+ nativeBinding = requireNative()
679
+ }
680
+ if ((forceWasiError || __napiWasiFlavorRequested) && !wasiBindingLoaded) {
681
+ const error = new Error(
682
+ __napiWasiFlavorRequested
683
+ ? 'WASI binding for flavor "' + __napiWasiFlavor + '" not found'
684
+ : 'WASI binding not found and NAPI_RS_FORCE_WASI is set to error',
685
+ )
686
+ error.cause = createLoadErrorChain(wasiBindingErrors)
571
687
  throw error
572
688
  }
573
689
  }
@@ -581,10 +697,7 @@ if (!nativeBinding) {
581
697
  )
582
698
  // assign instead of the `new Error(message, { cause })` options form,
583
699
  // which Node < 16.9 silently ignores
584
- error.cause = loadErrors.reduce((err, cur) => {
585
- cur.cause = err
586
- return cur
587
- })
700
+ error.cause = createLoadErrorChain(loadErrors)
588
701
  throw error
589
702
  }
590
703
  throw new Error(`Failed to load native binding`)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mdream",
3
3
  "type": "module",
4
- "version": "1.5.8",
4
+ "version": "1.5.11",
5
5
  "description": "Ultra-performant HTML to Markdown Convertor Optimized for LLMs and llm.txt artifacts.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -57,22 +57,22 @@
57
57
  "browser": "./dist/browser.mjs",
58
58
  "dependencies": {},
59
59
  "optionalDependencies": {
60
- "@mdream/rust-android-arm-eabi": "1.5.8",
61
- "@mdream/rust-android-arm64": "1.5.8",
62
- "@mdream/rust-darwin-arm64": "1.5.8",
63
- "@mdream/rust-darwin-x64": "1.5.8",
64
- "@mdream/rust-freebsd-x64": "1.5.8",
65
- "@mdream/rust-linux-arm64-musl": "1.5.8",
66
- "@mdream/rust-linux-arm64-gnu": "1.5.8",
67
- "@mdream/rust-linux-arm-gnueabihf": "1.5.8",
68
- "@mdream/rust-linux-x64-gnu": "1.5.8",
69
- "@mdream/rust-linux-x64-musl": "1.5.8",
70
- "@mdream/rust-wasm32-wasi": "1.5.8",
71
- "@mdream/rust-win32-arm64-msvc": "1.5.8",
72
- "@mdream/rust-win32-x64-msvc": "1.5.8"
60
+ "@mdream/rust-android-arm64": "1.5.11",
61
+ "@mdream/rust-android-arm-eabi": "1.5.11",
62
+ "@mdream/rust-freebsd-x64": "1.5.11",
63
+ "@mdream/rust-darwin-x64": "1.5.11",
64
+ "@mdream/rust-darwin-arm64": "1.5.11",
65
+ "@mdream/rust-linux-arm-gnueabihf": "1.5.11",
66
+ "@mdream/rust-linux-arm64-gnu": "1.5.11",
67
+ "@mdream/rust-linux-x64-gnu": "1.5.11",
68
+ "@mdream/rust-linux-arm64-musl": "1.5.11",
69
+ "@mdream/rust-linux-x64-musl": "1.5.11",
70
+ "@mdream/rust-wasm32-wasi": "1.5.11",
71
+ "@mdream/rust-win32-arm64-msvc": "1.5.11",
72
+ "@mdream/rust-win32-x64-msvc": "1.5.11"
73
73
  },
74
74
  "devDependencies": {
75
- "@mdream/js": "1.5.8"
75
+ "@mdream/js": "1.5.11"
76
76
  },
77
77
  "scripts": {
78
78
  "flame": "pnpm build && unbuild bench/bundle && clinic flame -- node bench/bundle/dist/string.mjs 10",
@@ -9,17 +9,26 @@ export class MarkdownStream {
9
9
  processChunk(chunk: string): string;
10
10
  }
11
11
 
12
+ /**
13
+ * Consumes the message of the panic that aborted the last export call.
14
+ */
15
+ export function __mdreamTakePanicMessage(): string | undefined;
16
+
12
17
  export function htmlToMarkdown(html: string, options: any): string;
13
18
 
14
19
  export function htmlToMarkdownResult(html: string, options: any): any;
15
20
 
21
+ export function install_panic_hook(): void;
22
+
16
23
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
17
24
 
18
25
  export interface InitOutput {
19
26
  readonly memory: WebAssembly.Memory;
27
+ readonly __mdreamTakePanicMessage: (a: number) => void;
20
28
  readonly __wbg_markdownstream_free: (a: number, b: number) => void;
21
29
  readonly htmlToMarkdown: (a: number, b: number, c: number, d: number) => void;
22
30
  readonly htmlToMarkdownResult: (a: number, b: number, c: number) => number;
31
+ readonly install_panic_hook: () => void;
23
32
  readonly markdownstream_finish: (a: number, b: number) => void;
24
33
  readonly markdownstream_new: (a: number) => number;
25
34
  readonly markdownstream_processChunk: (a: number, b: number, c: number, d: number) => void;
@@ -28,6 +37,7 @@ export interface InitOutput {
28
37
  readonly __wbindgen_export3: (a: number) => void;
29
38
  readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
30
39
  readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
40
+ readonly __wbindgen_start: () => void;
31
41
  }
32
42
 
33
43
  export type SyncInitInput = BufferSource | WebAssembly.Module;
@@ -64,6 +64,27 @@ export class MarkdownStream {
64
64
  }
65
65
  if (Symbol.dispose) MarkdownStream.prototype[Symbol.dispose] = MarkdownStream.prototype.free;
66
66
 
67
+ /**
68
+ * Consumes the message of the panic that aborted the last export call.
69
+ * @returns {string | undefined}
70
+ */
71
+ export function __mdreamTakePanicMessage() {
72
+ try {
73
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
74
+ wasm.__mdreamTakePanicMessage(retptr);
75
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
76
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
77
+ let v1;
78
+ if (r0 !== 0) {
79
+ v1 = getStringFromWasm0(r0, r1).slice();
80
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
81
+ }
82
+ return v1;
83
+ } finally {
84
+ wasm.__wbindgen_add_to_stack_pointer(16);
85
+ }
86
+ }
87
+
67
88
  /**
68
89
  * @param {string} html
69
90
  * @param {any} options
@@ -99,6 +120,10 @@ export function htmlToMarkdownResult(html, options) {
99
120
  const ret = wasm.htmlToMarkdownResult(ptr0, len0, addHeapObject(options));
100
121
  return takeObject(ret);
101
122
  }
123
+
124
+ export function install_panic_hook() {
125
+ wasm.install_panic_hook();
126
+ }
102
127
  function __wbg_get_imports() {
103
128
  const import0 = {
104
129
  __proto__: null,
@@ -328,6 +353,7 @@ function __wbg_finalize_init(instance, module) {
328
353
  wasmModule = module;
329
354
  cachedDataViewMemory0 = null;
330
355
  cachedUint8ArrayMemory0 = null;
356
+ wasm.__wbindgen_start();
331
357
  return wasm;
332
358
  }
333
359
 
Binary file
@@ -1,9 +1,11 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
+ export const __mdreamTakePanicMessage: (a: number) => void;
4
5
  export const __wbg_markdownstream_free: (a: number, b: number) => void;
5
6
  export const htmlToMarkdown: (a: number, b: number, c: number, d: number) => void;
6
7
  export const htmlToMarkdownResult: (a: number, b: number, c: number) => number;
8
+ export const install_panic_hook: () => void;
7
9
  export const markdownstream_finish: (a: number, b: number) => void;
8
10
  export const markdownstream_new: (a: number) => number;
9
11
  export const markdownstream_processChunk: (a: number, b: number, c: number, d: number) => void;
@@ -12,3 +14,4 @@ export const __wbindgen_export2: (a: number, b: number, c: number, d: number) =>
12
14
  export const __wbindgen_export3: (a: number) => void;
13
15
  export const __wbindgen_add_to_stack_pointer: (a: number) => number;
14
16
  export const __wbindgen_export4: (a: number, b: number, c: number) => void;
17
+ export const __wbindgen_start: () => void;
package/wasm/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mdream-edge",
3
3
  "type": "module",
4
4
  "description": "WebAssembly edge runtime bindings for mdream HTML-to-Markdown converter",
5
- "version": "1.5.8",
5
+ "version": "1.5.11",
6
6
  "license": "MIT",
7
7
  "files": [
8
8
  "mdream_edge_bg.wasm",
@@ -9,6 +9,13 @@ export class MarkdownStream {
9
9
  processChunk(chunk: string): string;
10
10
  }
11
11
 
12
+ /**
13
+ * Consumes the message of the panic that aborted the last export call.
14
+ */
15
+ export function __mdreamTakePanicMessage(): string | undefined;
16
+
12
17
  export function htmlToMarkdown(html: string, options: any): string;
13
18
 
14
19
  export function htmlToMarkdownResult(html: string, options: any): any;
20
+
21
+ export function install_panic_hook(): void;
@@ -3,7 +3,7 @@ import * as wasm from "./mdream_edge_bg.wasm";
3
3
  import { __wbg_set_wasm } from "./mdream_edge_bg.js";
4
4
 
5
5
  __wbg_set_wasm(wasm);
6
-
6
+ wasm.__wbindgen_start();
7
7
  export {
8
- MarkdownStream, htmlToMarkdown, htmlToMarkdownResult
8
+ MarkdownStream, __mdreamTakePanicMessage, htmlToMarkdown, htmlToMarkdownResult, install_panic_hook
9
9
  } from "./mdream_edge_bg.js";
@@ -62,6 +62,27 @@ export class MarkdownStream {
62
62
  }
63
63
  if (Symbol.dispose) MarkdownStream.prototype[Symbol.dispose] = MarkdownStream.prototype.free;
64
64
 
65
+ /**
66
+ * Consumes the message of the panic that aborted the last export call.
67
+ * @returns {string | undefined}
68
+ */
69
+ export function __mdreamTakePanicMessage() {
70
+ try {
71
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
72
+ wasm.__mdreamTakePanicMessage(retptr);
73
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
74
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
75
+ let v1;
76
+ if (r0 !== 0) {
77
+ v1 = getStringFromWasm0(r0, r1).slice();
78
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
79
+ }
80
+ return v1;
81
+ } finally {
82
+ wasm.__wbindgen_add_to_stack_pointer(16);
83
+ }
84
+ }
85
+
65
86
  /**
66
87
  * @param {string} html
67
88
  * @param {any} options
@@ -97,6 +118,10 @@ export function htmlToMarkdownResult(html, options) {
97
118
  const ret = wasm.htmlToMarkdownResult(ptr0, len0, addHeapObject(options));
98
119
  return takeObject(ret);
99
120
  }
121
+
122
+ export function install_panic_hook() {
123
+ wasm.install_panic_hook();
124
+ }
100
125
  export function __wbg___wbindgen_boolean_get_b131b2f36d6b2f55(arg0) {
101
126
  const v = getObject(arg0);
102
127
  const ret = typeof(v) === 'boolean' ? v : undefined;
Binary file
@@ -1,9 +1,11 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
+ export const __mdreamTakePanicMessage: (a: number) => void;
4
5
  export const __wbg_markdownstream_free: (a: number, b: number) => void;
5
6
  export const htmlToMarkdown: (a: number, b: number, c: number, d: number) => void;
6
7
  export const htmlToMarkdownResult: (a: number, b: number, c: number) => number;
8
+ export const install_panic_hook: () => void;
7
9
  export const markdownstream_finish: (a: number, b: number) => void;
8
10
  export const markdownstream_new: (a: number) => number;
9
11
  export const markdownstream_processChunk: (a: number, b: number, c: number, d: number) => void;
@@ -12,3 +14,4 @@ export const __wbindgen_export2: (a: number, b: number, c: number, d: number) =>
12
14
  export const __wbindgen_export3: (a: number) => void;
13
15
  export const __wbindgen_add_to_stack_pointer: (a: number) => number;
14
16
  export const __wbindgen_export4: (a: number, b: number, c: number) => void;
17
+ export const __wbindgen_start: () => void;
@@ -2,7 +2,7 @@
2
2
  "name": "mdream-edge",
3
3
  "type": "module",
4
4
  "description": "WebAssembly edge runtime bindings for mdream HTML-to-Markdown converter",
5
- "version": "1.5.8",
5
+ "version": "1.5.11",
6
6
  "license": "MIT",
7
7
  "files": [
8
8
  "mdream_edge_bg.wasm",