@stream-mdx/worker 0.0.2 → 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 +11 -0
- package/README.md +10 -0
- package/dist/hosted/markdown-worker.js +81868 -393
- 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/worker-client.d.cts +2 -0
- package/dist/worker-client.d.ts +2 -0
- package/package.json +11 -4
|
@@ -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.d.cts
CHANGED
package/dist/worker-client.d.ts
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": {
|
|
@@ -35,7 +35,14 @@
|
|
|
35
35
|
"types": "./dist/streaming/custom-matcher.d.ts",
|
|
36
36
|
"import": "./dist/streaming/custom-matcher.mjs",
|
|
37
37
|
"require": "./dist/streaming/custom-matcher.cjs"
|
|
38
|
-
}
|
|
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"
|
|
39
46
|
},
|
|
40
47
|
"files": [
|
|
41
48
|
"dist",
|
|
@@ -56,8 +63,8 @@
|
|
|
56
63
|
"@lezer/common": "^1.2.3",
|
|
57
64
|
"@lezer/lr": "^1.4.2",
|
|
58
65
|
"@lezer/markdown": "^1.3.0",
|
|
59
|
-
"@stream-mdx/core": "0.0.
|
|
60
|
-
"@stream-mdx/plugins": "0.0.
|
|
66
|
+
"@stream-mdx/core": "0.0.3",
|
|
67
|
+
"@stream-mdx/plugins": "0.0.3",
|
|
61
68
|
"@mdx-js/mdx": "^3.1.0",
|
|
62
69
|
"@shikijs/engine-javascript": "^1.29.2",
|
|
63
70
|
"@shikijs/engine-oniguruma": "^1.29.2",
|