@cspell/cspell-worker 9.6.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jason Dent
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # CSpell Worker
2
+
3
+ Package: `@cspell/cspell-worker`
4
+
5
+ Designed to enable spelling checking documents on a NodeJS worker thread.
6
+
7
+ > [!CAUTION]
8
+ >
9
+ > This package is currently experimental and its exports and APIs will change.
@@ -0,0 +1,58 @@
1
+ import { CSpellRPCClient } from "cspell-lib/cspell-rpc";
2
+
3
+ //#region src/cspellWorker.d.ts
4
+ declare function startCSpellWorker(): CSpellWorker;
5
+ interface CSpellWorker {
6
+ ready: Promise<boolean>;
7
+ isReadyNow: boolean;
8
+ numberOfPendingRequests: number;
9
+ ok: (timeoutMs?: number) => Promise<boolean>;
10
+ api: CSpellRPCClient["getApi"];
11
+ client: CSpellRPCClient;
12
+ [Symbol.asyncDispose](): Promise<void>;
13
+ }
14
+ //#endregion
15
+ //#region src/cspellWorkerPool.d.ts
16
+ interface CSpellWorkerPoolOptions {
17
+ /**
18
+ * The maximum number of workers to create in the pool.
19
+ */
20
+ maxWorkers?: number;
21
+ /**
22
+ * The minimum number of workers to create in the pool.
23
+ */
24
+ minWorkers?: number;
25
+ /**
26
+ * The maximum number of pending tasks allowed per worker before the worker is considered unavailable.
27
+ */
28
+ maxPendingTasksPerWorker?: number;
29
+ }
30
+ interface GetAvailableWorkerOptions {
31
+ /**
32
+ * If true, only return ready workers.
33
+ */
34
+ onlyReady?: boolean;
35
+ /**
36
+ * If true, only return workers that do not have pending requests.
37
+ */
38
+ onlyIdle?: boolean;
39
+ /**
40
+ * If true, start a new worker if no available worker is found.
41
+ */
42
+ autostart?: boolean;
43
+ }
44
+ declare class CSpellWorkerPool {
45
+ #private;
46
+ constructor(options?: CSpellWorkerPoolOptions);
47
+ get size(): number;
48
+ get maxWorkers(): number;
49
+ get minWorkers(): number;
50
+ get maxPendingTasksPerWorker(): number;
51
+ set maxPendingTasksPerWorker(value: number);
52
+ getAvailableWorker(options?: GetAvailableWorkerOptions): CSpellWorker | undefined;
53
+ stopWorker(worker: CSpellWorker): Promise<void>;
54
+ [Symbol.asyncDispose](): Promise<void>;
55
+ }
56
+ //#endregion
57
+ export { type CSpellWorker, CSpellWorkerPool, type CSpellWorkerPoolOptions, type GetAvailableWorkerOptions, startCSpellWorker };
58
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,128 @@
1
+ import { MessageChannel, Worker } from "node:worker_threads";
2
+ import { createCSpellRPCClient } from "cspell-lib/cspell-rpc";
3
+ import { cpus } from "node:os";
4
+
5
+ //#region src/cspellWorker.ts
6
+ function startCSpellWorker() {
7
+ const { port1, port2 } = new MessageChannel();
8
+ return new CSpellWorkerImpl({
9
+ worker: new Worker(new URL("worker.js", import.meta.url), {
10
+ workerData: { port: port1 },
11
+ transferList: [port1],
12
+ stderr: true,
13
+ stdout: true
14
+ }),
15
+ port: port2
16
+ });
17
+ }
18
+ var CSpellWorkerImpl = class {
19
+ #terminated = false;
20
+ #worker;
21
+ #client;
22
+ constructor(instance) {
23
+ this.#worker = instance.worker;
24
+ this.#client = createCSpellRPCClient({ port: instance.port });
25
+ }
26
+ get ready() {
27
+ return this.#client.ready();
28
+ }
29
+ get isReadyNow() {
30
+ return this.#client.isReady;
31
+ }
32
+ get numberOfPendingRequests() {
33
+ return this.#client.length;
34
+ }
35
+ get client() {
36
+ return this.#client;
37
+ }
38
+ get isTerminated() {
39
+ return this.#terminated;
40
+ }
41
+ ok(timeoutMs) {
42
+ return this.#client.isOK({ timeoutMs });
43
+ }
44
+ terminate() {
45
+ return this[Symbol.asyncDispose]();
46
+ }
47
+ api() {
48
+ return this.#client.getApi();
49
+ }
50
+ async [Symbol.asyncDispose]() {
51
+ if (this.#terminated) return;
52
+ this.#terminated = true;
53
+ this.#client[Symbol.dispose]();
54
+ await this.#worker.terminate();
55
+ }
56
+ };
57
+
58
+ //#endregion
59
+ //#region src/cspellWorkerPool.ts
60
+ const MAX_WORKERS_TO_CORES_RATIO = .75;
61
+ const DEFAULT_WORKERS_TO_CORES_RATIO = .5;
62
+ var CSpellWorkerPool = class {
63
+ #workers;
64
+ #options;
65
+ #maxWorkers;
66
+ #minWorkers;
67
+ #maxPendingTasksPerWorker;
68
+ constructor(options) {
69
+ this.#workers = /* @__PURE__ */ new Set();
70
+ this.#options = options || {};
71
+ this.#maxPendingTasksPerWorker = this.#options.maxPendingTasksPerWorker ?? 1;
72
+ this.#maxPendingTasksPerWorker = Math.max(1, this.#maxPendingTasksPerWorker);
73
+ const numCores = cpus().length;
74
+ this.#maxWorkers = this.#options.maxWorkers ?? Math.ceil(numCores * DEFAULT_WORKERS_TO_CORES_RATIO);
75
+ this.#maxWorkers = Math.min(this.#maxWorkers, Math.ceil(numCores * MAX_WORKERS_TO_CORES_RATIO));
76
+ this.#maxWorkers = Math.max(1, this.#maxWorkers);
77
+ this.#minWorkers = Math.min(this.#options.minWorkers ?? 0, this.#maxWorkers);
78
+ for (let i = 0; i < this.#minWorkers; i++) this.#createWorker();
79
+ }
80
+ #createWorker() {
81
+ const w = startCSpellWorker();
82
+ this.#workers.add(w);
83
+ return w;
84
+ }
85
+ get size() {
86
+ return this.#workers.size;
87
+ }
88
+ get maxWorkers() {
89
+ return this.#maxWorkers;
90
+ }
91
+ get minWorkers() {
92
+ return this.#minWorkers;
93
+ }
94
+ get maxPendingTasksPerWorker() {
95
+ return this.#maxPendingTasksPerWorker;
96
+ }
97
+ set maxPendingTasksPerWorker(value) {
98
+ this.#maxPendingTasksPerWorker = Math.max(1, value);
99
+ }
100
+ getAvailableWorker(options) {
101
+ let workers = [...this.#workers];
102
+ workers = workers.filter((worker) => worker.numberOfPendingRequests < this.#maxPendingTasksPerWorker);
103
+ workers = options?.onlyReady ? workers.filter((worker) => worker.isReadyNow) : workers;
104
+ workers = options?.onlyIdle ? workers.filter((worker) => worker.numberOfPendingRequests === 0) : workers;
105
+ workers.sort((a, b) => {
106
+ let v = 0;
107
+ v = (a.isReadyNow ? 0 : 1) - (b.isReadyNow ? 0 : 1);
108
+ if (v !== 0) return v;
109
+ v = a.numberOfPendingRequests - b.numberOfPendingRequests;
110
+ return v;
111
+ });
112
+ if (workers.length > 0) return workers[0];
113
+ if (options?.autostart && this.#workers.size < this.#maxWorkers) return this.#createWorker();
114
+ }
115
+ stopWorker(worker) {
116
+ this.#workers.delete(worker);
117
+ return worker[Symbol.asyncDispose]();
118
+ }
119
+ [Symbol.asyncDispose]() {
120
+ const workers = [...this.#workers];
121
+ this.#workers.clear();
122
+ return Promise.all(workers.map((w) => this.stopWorker(w).catch(() => void 0))).then(() => void 0);
123
+ }
124
+ };
125
+
126
+ //#endregion
127
+ export { CSpellWorkerPool, startCSpellWorker };
128
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ export { };
package/dist/worker.js ADDED
@@ -0,0 +1,9 @@
1
+ import { parentPort, workerData } from "node:worker_threads";
2
+ import { createCSpellRPCServer } from "cspell-lib/cspell-rpc/server";
3
+
4
+ //#region src/worker.ts
5
+ if (parentPort) createCSpellRPCServer({ port: workerData?.port || parentPort });
6
+
7
+ //#endregion
8
+ export { };
9
+ //# sourceMappingURL=worker.js.map
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@cspell/cspell-worker",
3
+ "publishConfig": {
4
+ "access": "public",
5
+ "provenance": true
6
+ },
7
+ "version": "9.6.1",
8
+ "description": "Client Server RPC worker for CSpell.",
9
+ "keywords": [
10
+ "cspell",
11
+ "rpc",
12
+ "worker"
13
+ ],
14
+ "author": "Jason Dent <jason@streetsidesoftware.nl>",
15
+ "homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-worker#readme",
16
+ "license": "MIT",
17
+ "type": "module",
18
+ "sideEffects": true,
19
+ "exports": {
20
+ ".": "./dist/index.js"
21
+ },
22
+ "directories": {
23
+ "dist": "dist"
24
+ },
25
+ "typings": "dist/index.d.ts",
26
+ "files": [
27
+ "dist",
28
+ "!**/*.tsbuildInfo",
29
+ "!**/__mocks__",
30
+ "!**/*.test.*",
31
+ "!**/*.spec.*",
32
+ "!**/*.map"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsdown && tsc -p .",
36
+ "watch": "tsdown --watch",
37
+ "clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
38
+ "clean-build": "pnpm run clean && pnpm run build",
39
+ "coverage": "vitest run --coverage --pool=forks",
40
+ "test": "vitest run --pool=forks"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/streetsidesoftware/cspell.git",
45
+ "directory": "packages/cspell-worker"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/streetsidesoftware/cspell"
49
+ },
50
+ "engines": {
51
+ "node": ">=20.18"
52
+ },
53
+ "dependencies": {
54
+ "cspell-lib": "9.6.1"
55
+ },
56
+ "gitHead": "666fb79096d25c53af9519cad07030e7aca597e1"
57
+ }