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/README.md CHANGED
@@ -931,6 +931,19 @@ for await (const chunk of streamHtmlToMarkdown(response.body)) {
931
931
  }
932
932
  ```
933
933
 
934
+ Conversion errors are catchable. WASM has no unwinding, so an internal Rust panic aborts the call, but the message is preserved and re-thrown as a normal `Error` (with the raw WASM trap as `cause`), and the instance keeps working for later requests:
935
+
936
+ ```ts
937
+ try {
938
+ return new Response(htmlToMarkdown(html))
939
+ }
940
+ catch (error) {
941
+ // "mdream WASM panic, please report this at ...\npanicked at ..."
942
+ console.error(error)
943
+ return new Response('conversion failed', { status: 500 })
944
+ }
945
+ ```
946
+
934
947
  If your toolchain doesn't resolve the export conditions, the raw wasm-bindgen build (web target) is exposed at `mdream/wasm` for manual initialization:
935
948
 
936
949
  ```ts
package/dist/browser.mjs CHANGED
@@ -1,4 +1,15 @@
1
- import init, { MarkdownStream as MarkdownStream$1, htmlToMarkdownResult } from "../wasm/mdream_edge.js";
1
+ import init, { MarkdownStream as MarkdownStream$1, __mdreamTakePanicMessage, htmlToMarkdownResult } from "../wasm/mdream_edge.js";
2
+ function wasmPanicError(error, takeMessage = __mdreamTakePanicMessage) {
3
+ if (!(error instanceof WebAssembly.RuntimeError)) return error;
4
+ let message;
5
+ try {
6
+ message = takeMessage();
7
+ } catch {
8
+ return error;
9
+ }
10
+ if (!message) return error;
11
+ return new Error(`mdream WASM panic, please report this at https://github.com/harlan-zw/mdream/issues\n${message}`, { cause: error });
12
+ }
2
13
  let _initPromise;
3
14
  function ensureInit() {
4
15
  if (!_initPromise) _initPromise = init();
@@ -7,7 +18,11 @@ function ensureInit() {
7
18
  ensureInit();
8
19
  async function htmlToMarkdown(html, options) {
9
20
  await ensureInit();
10
- return htmlToMarkdownResult(html, options || {});
21
+ try {
22
+ return htmlToMarkdownResult(html, options || {});
23
+ } catch (error) {
24
+ throw wasmPanicError(error);
25
+ }
11
26
  }
12
27
  async function createMarkdownStream(options) {
13
28
  await ensureInit();
@@ -16,13 +31,25 @@ async function createMarkdownStream(options) {
16
31
  var MarkdownStream = class {
17
32
  _inner;
18
33
  constructor(options) {
19
- this._inner = new MarkdownStream$1(options || {});
34
+ try {
35
+ this._inner = new MarkdownStream$1(options || {});
36
+ } catch (error) {
37
+ throw wasmPanicError(error);
38
+ }
20
39
  }
21
40
  processChunk(chunk) {
22
- return this._inner.processChunk(chunk);
41
+ try {
42
+ return this._inner.processChunk(chunk);
43
+ } catch (error) {
44
+ throw wasmPanicError(error);
45
+ }
23
46
  }
24
47
  finish() {
25
- return this._inner.finish();
48
+ try {
49
+ return this._inner.finish();
50
+ } catch (error) {
51
+ throw wasmPanicError(error);
52
+ }
26
53
  }
27
54
  };
28
55
  async function* streamHtmlToMarkdown(htmlStream, options) {
@@ -46,6 +73,8 @@ async function* streamHtmlToMarkdown(htmlStream, options) {
46
73
  }
47
74
  const final_ = stream.finish();
48
75
  if (final_) yield final_;
76
+ } catch (error) {
77
+ throw wasmPanicError(error);
49
78
  } finally {
50
79
  reader.releaseLock();
51
80
  }
package/dist/edge.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { MarkdownStream as MarkdownStream$1, htmlToMarkdownResult, initSync } from "../wasm/mdream_edge.js";
1
+ import { MarkdownStream as MarkdownStream$1, __mdreamTakePanicMessage, htmlToMarkdownResult, initSync } from "../wasm/mdream_edge.js";
2
2
  import wasmModule from "../wasm/mdream_edge_bg.wasm";
3
3
  const MINIMAL_FILTER_DEFAULT = { exclude: [
4
4
  "form",
@@ -99,11 +99,29 @@ function resolveOptions(options) {
99
99
  frontmatterCallback
100
100
  };
101
101
  }
102
+ function wasmPanicError(error, takeMessage = __mdreamTakePanicMessage) {
103
+ if (!(error instanceof WebAssembly.RuntimeError)) return error;
104
+ let message;
105
+ try {
106
+ message = takeMessage();
107
+ } catch {
108
+ return error;
109
+ }
110
+ if (!message) return error;
111
+ return new Error(`mdream WASM panic, please report this at https://github.com/harlan-zw/mdream/issues\n${message}`, { cause: error });
112
+ }
102
113
  initSync({ module: wasmModule });
114
+ function convert(html, napiOpts) {
115
+ try {
116
+ return htmlToMarkdownResult(html, napiOpts);
117
+ } catch (error) {
118
+ throw wasmPanicError(error);
119
+ }
120
+ }
103
121
  function htmlToMarkdown(html, options) {
104
- if (!options) return htmlToMarkdownResult(html, void 0).markdown || "";
122
+ if (!options) return convert(html, void 0).markdown || "";
105
123
  const { napiOpts, extractionHandlers, frontmatterCallback } = resolveOptions(options);
106
- const result = htmlToMarkdownResult(html, napiOpts);
124
+ const result = convert(html, napiOpts);
107
125
  if (result.frontmatter && frontmatterCallback) frontmatterCallback(result.frontmatter);
108
126
  if (result.extracted?.length && extractionHandlers) for (const element of result.extracted) extractionHandlers[element.selector]?.(element);
109
127
  return result.markdown || "";
@@ -111,13 +129,25 @@ function htmlToMarkdown(html, options) {
111
129
  var MarkdownStream = class {
112
130
  _inner;
113
131
  constructor(options) {
114
- this._inner = new MarkdownStream$1(options);
132
+ try {
133
+ this._inner = new MarkdownStream$1(options);
134
+ } catch (error) {
135
+ throw wasmPanicError(error);
136
+ }
115
137
  }
116
138
  processChunk(chunk) {
117
- return this._inner.processChunk(chunk);
139
+ try {
140
+ return this._inner.processChunk(chunk);
141
+ } catch (error) {
142
+ throw wasmPanicError(error);
143
+ }
118
144
  }
119
145
  finish() {
120
- return this._inner.finish();
146
+ try {
147
+ return this._inner.finish();
148
+ } catch (error) {
149
+ throw wasmPanicError(error);
150
+ }
121
151
  }
122
152
  };
123
153
  async function* streamHtmlToMarkdown(htmlStream, options) {
@@ -140,6 +170,8 @@ async function* streamHtmlToMarkdown(htmlStream, options) {
140
170
  }
141
171
  const final_ = stream.finish();
142
172
  if (final_) yield final_;
173
+ } catch (error) {
174
+ throw wasmPanicError(error);
143
175
  } finally {
144
176
  reader.releaseLock();
145
177
  }