-
-
Notifications
You must be signed in to change notification settings - Fork 118
fix: Add CSpellRPCServer and CSpellRPCClient classes #8366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 26 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
1976ddc
fix: CSpellRPCServer and CSpellRPCClient classes
Jason3S be0746e
Make sure settings can be transfered.
Jason3S 11ead68
implement worker
Jason3S b56fec7
get it working
Jason3S 2a25366
fix build
Jason3S f09efef
Update cspellWorker.ts
Jason3S 33f4cd0
Update cleanValidationIssue.ts
Jason3S 8861200
refactor RPC client / server
Jason3S 334cbc7
more tests
Jason3S 8b83b39
more tests
Jason3S 725bbc4
Update clientServer.test.ts
Jason3S cb01191
Update packages/cspell-lib/src/lib/util/clone.ts
Jason3S aae32d2
Update sanitizeSettings.ts
Jason3S 316b56f
Update packages/cspell/src/worker/cspellWorker.ts
Jason3S 9f6da5e
Create a cspell-worker package.
Jason3S 1e085c4
Add a simple server rpc worker.
Jason3S e3154a9
Add tests
Jason3S 2db4092
Update index.test.ts.snap
Jason3S 87174ca
Update packages/cspell-lib/src/lib/spellCheckFile.ts
Jason3S d26f509
fix spelliing
Jason3S 98baf8d
fix package dir
Jason3S 7ec62ed
Make sure the object is cloned
Jason3S 0ba2ad5
rename memorizer to memoizer
Jason3S 0f45cc7
Update LanguageSettings.ts
Jason3S 55bcfc1
move cspellRPC out of lib
Jason3S 532670b
fix build
Jason3S 8f3dd14
Apply suggestion from @Jason3S
Jason3S 41d9785
Update cspellWorker.ts
Jason3S 3354c2c
Update index.test.ts
Jason3S d76c5ae
Update vitest.config.mjs
Jason3S b832a5f
fix coverage
Jason3S e5466a6
Update index.test.ts
Jason3S 534dfc1
Use forks when testing worker.
Jason3S d2ef5b2
Add RPC to the API
Jason3S 36be43f
separate the RPC client and server
Jason3S 450cd6f
Update packages/cspell-lib/src/cspell-rpc/server.ts
Jason3S 47e821b
Update packages/cspell-lib/src/lib/util/memoizeLastCall.ts
Jason3S 13dd126
Update tsdown.config.ts
Jason3S 353b513
Add a listener to the port as soon as possible.
Jason3S f33ebdb
try waiting before checking the worker.
Jason3S 1360dfd
Merge branch 'main' into dev-worker
Jason3S 6fdc701
Update client.ts
Jason3S ef6bcb3
Update cspellWorker.test.ts
Jason3S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ licia | |
| linkcode | ||
| liriliri | ||
| megistos | ||
| memoizer | ||
| mkdir | ||
| monkeyc | ||
| mshick | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { MessageChannel } from 'node:worker_threads'; | ||
|
|
||
| import { describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import type { MessagePortLike } from './cspellRPC.js'; | ||
| import { CSpellRPCClient, CSpellRPCServer } from './cspellRPC.js'; | ||
|
|
||
| const oc = (...params: Parameters<typeof expect.objectContaining>) => expect.objectContaining(...params); | ||
|
|
||
| describe('Validate Client / Server communications', () => { | ||
| test('Check creation', async () => { | ||
| const { client, server, portClient, portServer } = createClientServerPair(); | ||
|
|
||
| expect(client).toBeDefined(); | ||
| expect(server).toBeDefined(); | ||
|
|
||
| expect(portClient.addListener).toHaveBeenCalled(); | ||
| expect(portServer.addListener).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('spell checking a document.', async () => { | ||
| const { client } = createClientServerPair(); | ||
|
|
||
| const api = client.getApi(); | ||
| expect(api).toBeDefined(); | ||
| expect(api.spellCheckDocument).toBeDefined(); | ||
|
|
||
| const doc = { uri: import.meta.url }; | ||
| const result = await api.spellCheckDocument(doc, {}, {}); | ||
| expect(result).toBeDefined(); | ||
| expect(result).toEqual(oc({ issues: [], errors: undefined })); | ||
|
|
||
| const result2 = await api.spellCheckDocument(doc, {}, {}); | ||
| expect(result2).toBeDefined(); | ||
| expect(result2).toEqual(oc({ issues: [], errors: undefined })); | ||
| }); | ||
| }); | ||
|
|
||
| interface ClientServerPair { | ||
| client: CSpellRPCClient; | ||
| server: CSpellRPCServer; | ||
| portClient: MessagePortLike; | ||
| portServer: MessagePortLike; | ||
| } | ||
|
|
||
| function createClientServerPair(): ClientServerPair { | ||
| const channel = new MessageChannel(); | ||
| const portClient = channel.port1; | ||
| const portServer = channel.port2; | ||
| spyOnPort(portServer); | ||
| spyOnPort(portClient); | ||
|
|
||
| const server = new CSpellRPCServer(portServer); | ||
| const client = new CSpellRPCClient(portClient); | ||
|
|
||
| return { client, server, portClient, portServer }; | ||
| } | ||
|
|
||
| function spyOnPort(port: MessagePortLike) { | ||
| const spyOnAddListener = vi.spyOn(port, 'addListener'); | ||
| const spyOnStart = vi.spyOn(port, 'start'); | ||
| const spyOnClose = vi.spyOn(port, 'close'); | ||
| return { spyOnAddListener, spyOnStart, spyOnClose }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { randomUUID } from 'node:crypto'; | ||
|
|
||
| import type { MessagePortLike, RPCClientOptions, RPCProtocol, RPCServerOptions } from '../rpc/index.js'; | ||
| import { RPCClient, RPCServer } from '../rpc/index.js'; | ||
| import type { spellCheckDocumentRPC } from './spellCheckFile.js'; | ||
|
|
||
| export type { MessagePortLike } from '../rpc/index.js'; | ||
|
|
||
| export interface CSpellRPCApi { | ||
| spellCheckDocument: typeof spellCheckDocumentRPC; | ||
| } | ||
|
|
||
| export type CSpellRPCServerOptions = RPCServerOptions; | ||
|
|
||
| export class CSpellRPCServer extends RPCServer<CSpellRPCApi> { | ||
| constructor(port: MessagePortLike, options?: CSpellRPCServerOptions) { | ||
| super(port, getCSpellRPCApi(), options); | ||
| } | ||
| } | ||
|
|
||
| export function createCSpellRPCServer(port: MessagePortLike, options?: CSpellRPCServerOptions): CSpellRPCServer { | ||
| return new CSpellRPCServer(port, options); | ||
| } | ||
|
|
||
| export type CSpellRPCClientOptions = RPCClientOptions; | ||
|
|
||
| export class CSpellRPCClient extends RPCClient<CSpellRPCApi> { | ||
| constructor(port: MessagePortLike, options?: CSpellRPCClientOptions) { | ||
| super(port, { randomUUID, ...options }); | ||
| } | ||
|
|
||
| getApi(): RPCProtocol<CSpellRPCApi> { | ||
| return super.getApi(Object.keys(getCSpellRPCApi()) as Array<keyof CSpellRPCApi>); | ||
| } | ||
| } | ||
|
|
||
| export function createCSpellRPCClient(port: MessagePortLike, options?: CSpellRPCClientOptions): CSpellRPCClient { | ||
| return new CSpellRPCClient(port, options); | ||
| } | ||
|
|
||
| let pSpellCheckFileJs: Promise<{ spellCheckDocumentRPC: typeof spellCheckDocumentRPC }> | undefined = undefined; | ||
|
|
||
| /** | ||
| * Get the CSpell RPC API. | ||
| * | ||
| * NOTE: This function lazy loads the implementation to avoid loading unnecessary during initialization of workers. | ||
| * | ||
| * @returns the api implementation. | ||
| */ | ||
| function getCSpellRPCApi(): CSpellRPCApi { | ||
| return { | ||
| spellCheckDocument: async (...params) => { | ||
| const { spellCheckDocumentRPC } = await getSpellCheckFileJs(); | ||
| return spellCheckDocumentRPC(...params); | ||
| }, | ||
| }; | ||
|
|
||
| function getSpellCheckFileJs() { | ||
| pSpellCheckFileJs ??= import('./spellCheckFile.js'); | ||
| return pSpellCheckFileJs; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export type { CSpellRPCApi } from './cspellRPC.js'; | ||
| export { | ||
| createCSpellRPCClient, | ||
| createCSpellRPCServer, | ||
| CSpellRPCClient, | ||
| CSpellRPCServer, | ||
| CSpellRPCServerOptions, | ||
| } from './cspellRPC.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { spellCheckDocumentRPC } from '../lib/spellCheckFile.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.