@stream-mdx/worker 0.0.1 → 0.0.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.
- package/CHANGELOG.md +30 -0
- package/README.md +28 -36
- package/dist/hosted/markdown-worker.js +81868 -394
- package/dist/index.cjs +0 -1
- package/dist/index.mjs +0 -1
- package/dist/mdx-compile.cjs +81 -0
- package/dist/mdx-compile.d.cts +13 -0
- package/dist/mdx-compile.d.ts +13 -0
- package/dist/mdx-compile.mjs +46 -0
- package/dist/node/index.cjs +75 -0
- package/dist/node/index.d.cts +29 -0
- package/dist/node/index.d.ts +29 -0
- package/dist/node/index.mjs +38 -0
- package/dist/node/worker-thread-entry.cjs +75 -0
- package/dist/node/worker-thread-entry.d.cts +1 -0
- package/dist/node/worker-thread-entry.d.ts +1 -0
- package/dist/node/worker-thread-entry.mjs +72 -0
- package/dist/streaming/custom-matcher.cjs +0 -1
- package/dist/streaming/custom-matcher.mjs +0 -1
- package/dist/worker-client.cjs +0 -1
- package/dist/worker-client.d.cts +3 -0
- package/dist/worker-client.d.ts +3 -0
- package/dist/worker-client.mjs +0 -1
- package/package.json +19 -5
- package/dist/hosted/markdown-worker.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/streaming/custom-matcher.cjs.map +0 -1
- package/dist/streaming/custom-matcher.mjs.map +0 -1
- package/dist/worker-client.cjs.map +0 -1
- package/dist/worker-client.mjs.map +0 -1
package/dist/index.cjs
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/mdx-compile.ts
|
|
31
|
+
var mdx_compile_exports = {};
|
|
32
|
+
__export(mdx_compile_exports, {
|
|
33
|
+
compileMdxContent: () => compileMdxContent
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(mdx_compile_exports);
|
|
36
|
+
var import_mdx = require("@mdx-js/mdx");
|
|
37
|
+
var import_rehype_katex = __toESM(require("rehype-katex"), 1);
|
|
38
|
+
var import_rehype_slug = __toESM(require("rehype-slug"), 1);
|
|
39
|
+
var import_remark_gfm = __toESM(require("remark-gfm"), 1);
|
|
40
|
+
var import_remark_math = __toESM(require("remark-math"), 1);
|
|
41
|
+
function collectMdxDependencies(source) {
|
|
42
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
43
|
+
const importRegex = /(?:import|export)\s+[^'"]*?\sfrom\s+['"]([^'"]+)['"]/g;
|
|
44
|
+
let match;
|
|
45
|
+
while (true) {
|
|
46
|
+
match = importRegex.exec(source);
|
|
47
|
+
if (match === null) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
dependencies.add(match[1]);
|
|
51
|
+
}
|
|
52
|
+
return Array.from(dependencies);
|
|
53
|
+
}
|
|
54
|
+
async function compileMdxContent(source) {
|
|
55
|
+
const isDev = typeof process !== "undefined" && typeof process.env !== "undefined" && process.env?.NODE_ENV === "development";
|
|
56
|
+
const compiled = await (0, import_mdx.compile)(source, {
|
|
57
|
+
outputFormat: "function-body",
|
|
58
|
+
development: isDev,
|
|
59
|
+
remarkPlugins: [import_remark_gfm.default, import_remark_math.default],
|
|
60
|
+
rehypePlugins: [
|
|
61
|
+
import_rehype_slug.default,
|
|
62
|
+
[
|
|
63
|
+
import_rehype_katex.default,
|
|
64
|
+
{
|
|
65
|
+
throwOnError: false,
|
|
66
|
+
errorColor: "#cc0000",
|
|
67
|
+
strict: false
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
],
|
|
71
|
+
jsxImportSource: "react"
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
code: String(compiled),
|
|
75
|
+
dependencies: collectMdxDependencies(source)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
compileMdxContent
|
|
81
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared MDX compilation helper used by both the worker (mdxCompileMode="worker")
|
|
3
|
+
* and the server-side MDX compile endpoint.
|
|
4
|
+
*
|
|
5
|
+
* The goal is to keep the remark/rehype pipeline identical so that server and
|
|
6
|
+
* worker compilation strategies produce equivalent component code and HTML.
|
|
7
|
+
*/
|
|
8
|
+
declare function compileMdxContent(source: string): Promise<{
|
|
9
|
+
code: string;
|
|
10
|
+
dependencies: string[];
|
|
11
|
+
}>;
|
|
12
|
+
|
|
13
|
+
export { compileMdxContent };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared MDX compilation helper used by both the worker (mdxCompileMode="worker")
|
|
3
|
+
* and the server-side MDX compile endpoint.
|
|
4
|
+
*
|
|
5
|
+
* The goal is to keep the remark/rehype pipeline identical so that server and
|
|
6
|
+
* worker compilation strategies produce equivalent component code and HTML.
|
|
7
|
+
*/
|
|
8
|
+
declare function compileMdxContent(source: string): Promise<{
|
|
9
|
+
code: string;
|
|
10
|
+
dependencies: string[];
|
|
11
|
+
}>;
|
|
12
|
+
|
|
13
|
+
export { compileMdxContent };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/mdx-compile.ts
|
|
2
|
+
import { compile as compileMdx } from "@mdx-js/mdx";
|
|
3
|
+
import rehypeKatex from "rehype-katex";
|
|
4
|
+
import rehypeSlug from "rehype-slug";
|
|
5
|
+
import remarkGfm from "remark-gfm";
|
|
6
|
+
import remarkMath from "remark-math";
|
|
7
|
+
function collectMdxDependencies(source) {
|
|
8
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
9
|
+
const importRegex = /(?:import|export)\s+[^'"]*?\sfrom\s+['"]([^'"]+)['"]/g;
|
|
10
|
+
let match;
|
|
11
|
+
while (true) {
|
|
12
|
+
match = importRegex.exec(source);
|
|
13
|
+
if (match === null) {
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
dependencies.add(match[1]);
|
|
17
|
+
}
|
|
18
|
+
return Array.from(dependencies);
|
|
19
|
+
}
|
|
20
|
+
async function compileMdxContent(source) {
|
|
21
|
+
const isDev = typeof process !== "undefined" && typeof process.env !== "undefined" && process.env?.NODE_ENV === "development";
|
|
22
|
+
const compiled = await compileMdx(source, {
|
|
23
|
+
outputFormat: "function-body",
|
|
24
|
+
development: isDev,
|
|
25
|
+
remarkPlugins: [remarkGfm, remarkMath],
|
|
26
|
+
rehypePlugins: [
|
|
27
|
+
rehypeSlug,
|
|
28
|
+
[
|
|
29
|
+
rehypeKatex,
|
|
30
|
+
{
|
|
31
|
+
throwOnError: false,
|
|
32
|
+
errorColor: "#cc0000",
|
|
33
|
+
strict: false
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
],
|
|
37
|
+
jsxImportSource: "react"
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
code: String(compiled),
|
|
41
|
+
dependencies: collectMdxDependencies(source)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
compileMdxContent
|
|
46
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/node/index.ts
|
|
31
|
+
var node_exports = {};
|
|
32
|
+
__export(node_exports, {
|
|
33
|
+
createWorkerThread: () => createWorkerThread,
|
|
34
|
+
getHostedWorkerBundleUrl: () => getHostedWorkerBundleUrl
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(node_exports);
|
|
37
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
38
|
+
var import_node_url = require("url");
|
|
39
|
+
var import_node_worker_threads = require("worker_threads");
|
|
40
|
+
var import_meta = {};
|
|
41
|
+
function getHostedWorkerBundleUrl() {
|
|
42
|
+
return new URL("../hosted/markdown-worker.js", getModuleUrl());
|
|
43
|
+
}
|
|
44
|
+
function createWorkerThread(options = {}) {
|
|
45
|
+
const { workerBundle, workerData, ...workerOptions } = options;
|
|
46
|
+
const runnerUrl = new URL("./worker-thread-entry.mjs", getModuleUrl());
|
|
47
|
+
const bundleUrl = normalizeWorkerBundleUrl(workerBundle) ?? getHostedWorkerBundleUrl();
|
|
48
|
+
return new import_node_worker_threads.Worker(runnerUrl, {
|
|
49
|
+
...workerOptions,
|
|
50
|
+
workerData: {
|
|
51
|
+
...workerData ?? {},
|
|
52
|
+
bundleUrl: bundleUrl.href
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function normalizeWorkerBundleUrl(value) {
|
|
57
|
+
if (!value) return void 0;
|
|
58
|
+
if (value instanceof URL) return value;
|
|
59
|
+
try {
|
|
60
|
+
return new URL(value);
|
|
61
|
+
} catch {
|
|
62
|
+
return (0, import_node_url.pathToFileURL)(import_node_path.default.resolve(value));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function getModuleUrl() {
|
|
66
|
+
if (typeof __filename === "string" && __filename.length > 0) {
|
|
67
|
+
return (0, import_node_url.pathToFileURL)(__filename).href;
|
|
68
|
+
}
|
|
69
|
+
return import_meta.url;
|
|
70
|
+
}
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
createWorkerThread,
|
|
74
|
+
getHostedWorkerBundleUrl
|
|
75
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { WorkerOptions, Worker } from 'node:worker_threads';
|
|
2
|
+
|
|
3
|
+
interface CreateWorkerThreadOptions extends Omit<WorkerOptions, "type" | "workerData"> {
|
|
4
|
+
/**
|
|
5
|
+
* Override the worker bundle module that should be executed inside the thread.
|
|
6
|
+
*
|
|
7
|
+
* Defaults to the hosted worker shipped with `@stream-mdx/worker` at
|
|
8
|
+
* `dist/hosted/markdown-worker.js`.
|
|
9
|
+
*/
|
|
10
|
+
workerBundle?: string | URL;
|
|
11
|
+
/**
|
|
12
|
+
* Extra data passed to the thread. This will be merged with the internal
|
|
13
|
+
* `bundleUrl` field used by the bootstrap.
|
|
14
|
+
*/
|
|
15
|
+
workerData?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns the file URL for the hosted worker bundle shipped with `@stream-mdx/worker`.
|
|
19
|
+
*/
|
|
20
|
+
declare function getHostedWorkerBundleUrl(): URL;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a Node `worker_threads` Worker running the StreamMDX hosted worker bundle.
|
|
23
|
+
*
|
|
24
|
+
* The thread bootstrap installs WebWorker-like shims (`self`, `postMessage`, `onmessage`)
|
|
25
|
+
* so the same hosted bundle used in browsers can run under Node.
|
|
26
|
+
*/
|
|
27
|
+
declare function createWorkerThread(options?: CreateWorkerThreadOptions): Worker;
|
|
28
|
+
|
|
29
|
+
export { type CreateWorkerThreadOptions, createWorkerThread, getHostedWorkerBundleUrl };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { WorkerOptions, Worker } from 'node:worker_threads';
|
|
2
|
+
|
|
3
|
+
interface CreateWorkerThreadOptions extends Omit<WorkerOptions, "type" | "workerData"> {
|
|
4
|
+
/**
|
|
5
|
+
* Override the worker bundle module that should be executed inside the thread.
|
|
6
|
+
*
|
|
7
|
+
* Defaults to the hosted worker shipped with `@stream-mdx/worker` at
|
|
8
|
+
* `dist/hosted/markdown-worker.js`.
|
|
9
|
+
*/
|
|
10
|
+
workerBundle?: string | URL;
|
|
11
|
+
/**
|
|
12
|
+
* Extra data passed to the thread. This will be merged with the internal
|
|
13
|
+
* `bundleUrl` field used by the bootstrap.
|
|
14
|
+
*/
|
|
15
|
+
workerData?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns the file URL for the hosted worker bundle shipped with `@stream-mdx/worker`.
|
|
19
|
+
*/
|
|
20
|
+
declare function getHostedWorkerBundleUrl(): URL;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a Node `worker_threads` Worker running the StreamMDX hosted worker bundle.
|
|
23
|
+
*
|
|
24
|
+
* The thread bootstrap installs WebWorker-like shims (`self`, `postMessage`, `onmessage`)
|
|
25
|
+
* so the same hosted bundle used in browsers can run under Node.
|
|
26
|
+
*/
|
|
27
|
+
declare function createWorkerThread(options?: CreateWorkerThreadOptions): Worker;
|
|
28
|
+
|
|
29
|
+
export { type CreateWorkerThreadOptions, createWorkerThread, getHostedWorkerBundleUrl };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/node/index.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { pathToFileURL } from "url";
|
|
4
|
+
import { Worker } from "worker_threads";
|
|
5
|
+
function getHostedWorkerBundleUrl() {
|
|
6
|
+
return new URL("../hosted/markdown-worker.js", getModuleUrl());
|
|
7
|
+
}
|
|
8
|
+
function createWorkerThread(options = {}) {
|
|
9
|
+
const { workerBundle, workerData, ...workerOptions } = options;
|
|
10
|
+
const runnerUrl = new URL("./worker-thread-entry.mjs", getModuleUrl());
|
|
11
|
+
const bundleUrl = normalizeWorkerBundleUrl(workerBundle) ?? getHostedWorkerBundleUrl();
|
|
12
|
+
return new Worker(runnerUrl, {
|
|
13
|
+
...workerOptions,
|
|
14
|
+
workerData: {
|
|
15
|
+
...workerData ?? {},
|
|
16
|
+
bundleUrl: bundleUrl.href
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function normalizeWorkerBundleUrl(value) {
|
|
21
|
+
if (!value) return void 0;
|
|
22
|
+
if (value instanceof URL) return value;
|
|
23
|
+
try {
|
|
24
|
+
return new URL(value);
|
|
25
|
+
} catch {
|
|
26
|
+
return pathToFileURL(path.resolve(value));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function getModuleUrl() {
|
|
30
|
+
if (typeof __filename === "string" && __filename.length > 0) {
|
|
31
|
+
return pathToFileURL(__filename).href;
|
|
32
|
+
}
|
|
33
|
+
return import.meta.url;
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
createWorkerThread,
|
|
37
|
+
getHostedWorkerBundleUrl
|
|
38
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/node/worker-thread-entry.ts
|
|
4
|
+
var import_node_url = require("url");
|
|
5
|
+
var import_node_worker_threads = require("worker_threads");
|
|
6
|
+
var import_meta = {};
|
|
7
|
+
var port = import_node_worker_threads.parentPort;
|
|
8
|
+
if (!port) {
|
|
9
|
+
throw new Error("[stream-mdx] worker thread bootstrap missing parentPort.");
|
|
10
|
+
}
|
|
11
|
+
var globalAny = globalThis;
|
|
12
|
+
if (!globalAny.self) {
|
|
13
|
+
globalAny.self = globalThis;
|
|
14
|
+
}
|
|
15
|
+
globalAny.postMessage = (value) => {
|
|
16
|
+
port.postMessage(value);
|
|
17
|
+
};
|
|
18
|
+
var messageListeners = /* @__PURE__ */ new Set();
|
|
19
|
+
globalAny.addEventListener = (type, listener) => {
|
|
20
|
+
if (type !== "message") return;
|
|
21
|
+
messageListeners.add(listener);
|
|
22
|
+
};
|
|
23
|
+
globalAny.removeEventListener = (type, listener) => {
|
|
24
|
+
if (type !== "message") return;
|
|
25
|
+
messageListeners.delete(listener);
|
|
26
|
+
};
|
|
27
|
+
var ready = false;
|
|
28
|
+
var buffered = [];
|
|
29
|
+
function dispatchMessage(data) {
|
|
30
|
+
const event = { data };
|
|
31
|
+
const handler = globalAny.onmessage;
|
|
32
|
+
if (typeof handler === "function") {
|
|
33
|
+
try {
|
|
34
|
+
handler(event);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.error("[stream-mdx] worker thread onmessage threw:", error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (messageListeners.size > 0) {
|
|
40
|
+
for (const listener of messageListeners) {
|
|
41
|
+
try {
|
|
42
|
+
listener(event);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error("[stream-mdx] worker thread message listener threw:", error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
port.on("message", (data) => {
|
|
50
|
+
if (!ready) {
|
|
51
|
+
buffered.push(data);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
dispatchMessage(data);
|
|
55
|
+
});
|
|
56
|
+
var bootstrap = import_node_worker_threads.workerData ?? {};
|
|
57
|
+
var bundleUrl = typeof bootstrap.bundleUrl === "string" && bootstrap.bundleUrl.length > 0 ? bootstrap.bundleUrl : new URL("../hosted/markdown-worker.js", getModuleUrl()).href;
|
|
58
|
+
void (async () => {
|
|
59
|
+
await import(bundleUrl);
|
|
60
|
+
ready = true;
|
|
61
|
+
if (buffered.length > 0) {
|
|
62
|
+
for (const data of buffered.splice(0, buffered.length)) {
|
|
63
|
+
dispatchMessage(data);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
})().catch((error) => {
|
|
67
|
+
console.error("[stream-mdx] Failed to load hosted worker bundle in worker thread:", error);
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
function getModuleUrl() {
|
|
71
|
+
if (typeof __filename === "string" && __filename.length > 0) {
|
|
72
|
+
return (0, import_node_url.pathToFileURL)(__filename).href;
|
|
73
|
+
}
|
|
74
|
+
return import_meta.url;
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@stream-mdx/core/streaming/custom-matcher';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@stream-mdx/core/streaming/custom-matcher';
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/node/worker-thread-entry.ts
|
|
2
|
+
import { pathToFileURL } from "url";
|
|
3
|
+
import { parentPort, workerData } from "worker_threads";
|
|
4
|
+
var port = parentPort;
|
|
5
|
+
if (!port) {
|
|
6
|
+
throw new Error("[stream-mdx] worker thread bootstrap missing parentPort.");
|
|
7
|
+
}
|
|
8
|
+
var globalAny = globalThis;
|
|
9
|
+
if (!globalAny.self) {
|
|
10
|
+
globalAny.self = globalThis;
|
|
11
|
+
}
|
|
12
|
+
globalAny.postMessage = (value) => {
|
|
13
|
+
port.postMessage(value);
|
|
14
|
+
};
|
|
15
|
+
var messageListeners = /* @__PURE__ */ new Set();
|
|
16
|
+
globalAny.addEventListener = (type, listener) => {
|
|
17
|
+
if (type !== "message") return;
|
|
18
|
+
messageListeners.add(listener);
|
|
19
|
+
};
|
|
20
|
+
globalAny.removeEventListener = (type, listener) => {
|
|
21
|
+
if (type !== "message") return;
|
|
22
|
+
messageListeners.delete(listener);
|
|
23
|
+
};
|
|
24
|
+
var ready = false;
|
|
25
|
+
var buffered = [];
|
|
26
|
+
function dispatchMessage(data) {
|
|
27
|
+
const event = { data };
|
|
28
|
+
const handler = globalAny.onmessage;
|
|
29
|
+
if (typeof handler === "function") {
|
|
30
|
+
try {
|
|
31
|
+
handler(event);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error("[stream-mdx] worker thread onmessage threw:", error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (messageListeners.size > 0) {
|
|
37
|
+
for (const listener of messageListeners) {
|
|
38
|
+
try {
|
|
39
|
+
listener(event);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("[stream-mdx] worker thread message listener threw:", error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
port.on("message", (data) => {
|
|
47
|
+
if (!ready) {
|
|
48
|
+
buffered.push(data);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
dispatchMessage(data);
|
|
52
|
+
});
|
|
53
|
+
var bootstrap = workerData ?? {};
|
|
54
|
+
var bundleUrl = typeof bootstrap.bundleUrl === "string" && bootstrap.bundleUrl.length > 0 ? bootstrap.bundleUrl : new URL("../hosted/markdown-worker.js", getModuleUrl()).href;
|
|
55
|
+
void (async () => {
|
|
56
|
+
await import(bundleUrl);
|
|
57
|
+
ready = true;
|
|
58
|
+
if (buffered.length > 0) {
|
|
59
|
+
for (const data of buffered.splice(0, buffered.length)) {
|
|
60
|
+
dispatchMessage(data);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})().catch((error) => {
|
|
64
|
+
console.error("[stream-mdx] Failed to load hosted worker bundle in worker thread:", error);
|
|
65
|
+
throw error;
|
|
66
|
+
});
|
|
67
|
+
function getModuleUrl() {
|
|
68
|
+
if (typeof __filename === "string" && __filename.length > 0) {
|
|
69
|
+
return pathToFileURL(__filename).href;
|
|
70
|
+
}
|
|
71
|
+
return import.meta.url;
|
|
72
|
+
}
|
package/dist/worker-client.cjs
CHANGED
package/dist/worker-client.d.cts
CHANGED
package/dist/worker-client.d.ts
CHANGED
package/dist/worker-client.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stream-mdx/worker",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Worker client utilities and shared worker helpers for the Streaming Markdown V2 pipeline",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -26,14 +26,28 @@
|
|
|
26
26
|
"import": "./dist/worker-client.mjs",
|
|
27
27
|
"require": "./dist/worker-client.cjs"
|
|
28
28
|
},
|
|
29
|
+
"./mdx-compile": {
|
|
30
|
+
"types": "./dist/mdx-compile.d.ts",
|
|
31
|
+
"import": "./dist/mdx-compile.mjs",
|
|
32
|
+
"require": "./dist/mdx-compile.cjs"
|
|
33
|
+
},
|
|
29
34
|
"./streaming/custom-matcher": {
|
|
30
35
|
"types": "./dist/streaming/custom-matcher.d.ts",
|
|
31
36
|
"import": "./dist/streaming/custom-matcher.mjs",
|
|
32
37
|
"require": "./dist/streaming/custom-matcher.cjs"
|
|
33
|
-
}
|
|
38
|
+
},
|
|
39
|
+
"./node": {
|
|
40
|
+
"types": "./dist/node/index.d.ts",
|
|
41
|
+
"import": "./dist/node/index.mjs",
|
|
42
|
+
"require": "./dist/node/index.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./hosted/markdown-worker.js": "./dist/hosted/markdown-worker.js",
|
|
45
|
+
"./dist/hosted/markdown-worker.js": "./dist/hosted/markdown-worker.js"
|
|
34
46
|
},
|
|
35
47
|
"files": [
|
|
36
|
-
"dist"
|
|
48
|
+
"dist",
|
|
49
|
+
"README.md",
|
|
50
|
+
"CHANGELOG.md"
|
|
37
51
|
],
|
|
38
52
|
"sideEffects": [
|
|
39
53
|
"src/worker-dom-stub.ts"
|
|
@@ -49,8 +63,8 @@
|
|
|
49
63
|
"@lezer/common": "^1.2.3",
|
|
50
64
|
"@lezer/lr": "^1.4.2",
|
|
51
65
|
"@lezer/markdown": "^1.3.0",
|
|
52
|
-
"@stream-mdx/core": "0.0.
|
|
53
|
-
"@stream-mdx/plugins": "0.0.
|
|
66
|
+
"@stream-mdx/core": "0.0.3",
|
|
67
|
+
"@stream-mdx/plugins": "0.0.3",
|
|
54
68
|
"@mdx-js/mdx": "^3.1.0",
|
|
55
69
|
"@shikijs/engine-javascript": "^1.29.2",
|
|
56
70
|
"@shikijs/engine-oniguruma": "^1.29.2",
|