-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat(client, server): electron IPC #500
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
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b44b70f
playground
dinwwwh 33f8fa9
wip
dinwwwh a7cecc6
wip
dinwwwh ef8a7f2
improve
dinwwwh 5c3a50b
fix vercel build failed
dinwwwh 4de89af
docs
dinwwwh 6ac45be
improve scripts
dinwwwh eefb599
improve
dinwwwh 4a6fe07
improve
dinwwwh 848d606
improve
dinwwwh 04e7427
client tests
dinwwwh d262d1b
handler tests
dinwwwh 7f5c7ce
improve
dinwwwh ff0057f
fix docs
dinwwwh f9d3e34
Merge branch 'main' into feat/server/electron-ipc
dinwwwh 929d646
Merge branch 'main' into feat/server/electron-ipc
dinwwwh 6630b9a
update pnpm-lock.yaml
dinwwwh 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
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,16 @@ | ||
| --- | ||
| title: IPC | ||
| description: How to use oRPC over IPC? | ||
| --- | ||
|
|
||
| # IPC | ||
|
|
||
| oRPC includes built-in support for common IPC protocols, making it easy to expose RPC endpoints in any environment that supports IPC: | ||
|
|
||
| | Adapter | Target | | | ||
| | -------------- | ------------------------------------------------------------------- | --------------------------------------- | | ||
| | `electron-ipc` | [Electron IPC](https://www.electronjs.org/docs/latest/tutorial/ipc) | [docs](/docs/integrations/electron-ipc) | | ||
|
|
||
| ::: info | ||
| Each IPC protocol has its own setup requirements. Refer to the corresponding documentation for detailed instructions. | ||
| ::: |
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,45 @@ | ||
| --- | ||
| title: Electron IPC Integration | ||
| description: Integrate oRPC with Electron IPC | ||
| --- | ||
|
|
||
| # Electron IPC Integration | ||
|
|
||
| [Electron IPC](https://www.electronjs.org/docs/latest/tutorial/ipc) is a built-in IPC mechanism in Electron that allows you to communicate between the main process and renderer processes. For additional context, refer to the [IPC Adapter](/docs/adapters/ipc) guide. | ||
|
|
||
| ## Basic | ||
|
|
||
| 1. **main script**: Create and upgrade an oRPC handler. | ||
|
|
||
| ```ts | ||
| import { app } from 'electron' | ||
| import { experimental_RPCHandler as RPCHandler } from '@orpc/server/electron-ipc' | ||
|
|
||
| const handler = new RPCHandler(router) | ||
|
|
||
| app.whenReady().then(() => { | ||
| handler.upgrade({ | ||
| context: {}, // Provide initial context if needed | ||
| }) | ||
| }) | ||
| ``` | ||
|
|
||
| 2. **preload script**: Expose the oRPC handler channel to the renderer process. | ||
|
|
||
| ```ts | ||
| import { experimental_exposeORPCHandlerChannel as exposeORPCHandlerChannel } from '@orpc/server/electron-ipc' | ||
|
|
||
| exposeORPCHandlerChannel() | ||
| ``` | ||
|
|
||
| 3. **renderer script**: Create an oRPC link and use it to initialize the client. | ||
|
|
||
| ```ts | ||
| import { experimental_RPCLink as RPCLink } from '@orpc/client/electron-ipc' | ||
|
|
||
| const link = new RPCLink() | ||
| ``` | ||
|
|
||
| :::info | ||
| This only shows how to configure the link. For full client examples, see [Client-Side Clients](/docs/client/client-side). | ||
| ::: |
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
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 @@ | ||
| export const DEFAULT_ORPC_HANDLER_CHANNEL = 'orpc:default' |
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,4 @@ | ||
| export * from './consts' | ||
| export * from './link-client' | ||
| export * from './rpc-link' | ||
| export * from './types' |
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,42 @@ | ||
| import type { StandardLazyResponse, StandardRequest } from '@orpc/standard-server' | ||
| import type { ClientContext, ClientOptions } from '../../types' | ||
| import type { StandardLinkClient } from '../standard' | ||
| import type { experimental_ExposedORPCHandlerChannel as ExposedORPCHandlerChannel } from './types' | ||
| import { ClientPeer } from '@orpc/standard-server-peer' | ||
| import { DEFAULT_ORPC_HANDLER_CHANNEL } from './consts' | ||
|
|
||
| export interface experimental_LinkElectronIPCClientOptions { | ||
| /** | ||
| * The channel name exposed by the Electron IPC handler. | ||
| * | ||
| * @default 'orpc:default' | ||
| */ | ||
| channel?: string | ||
| } | ||
|
|
||
| export class experimental_LinkElectronIPCClient<T extends ClientContext> implements StandardLinkClient<T> { | ||
| private readonly peer: ClientPeer | ||
|
|
||
| constructor(options: experimental_LinkElectronIPCClientOptions = {}) { | ||
| const channel = options.channel ?? DEFAULT_ORPC_HANDLER_CHANNEL | ||
|
|
||
| const exposed: ExposedORPCHandlerChannel | undefined = (globalThis as any)[channel] | ||
|
|
||
| if (!exposed) { | ||
| throw new Error(`Cannot find exposed ORPC handler channel at globalThis['${channel}']`) | ||
| } | ||
|
|
||
| this.peer = new ClientPeer(async (message) => { | ||
| exposed.send(message instanceof Blob ? await message.arrayBuffer() : message) | ||
| }) | ||
|
dinwwwh marked this conversation as resolved.
|
||
|
|
||
| exposed.receive((message) => { | ||
| this.peer.message(message) | ||
| }) | ||
| } | ||
|
|
||
| async call(request: StandardRequest, _options: ClientOptions<T>, _path: readonly string[], _input: unknown): Promise<StandardLazyResponse> { | ||
| const response = await this.peer.request(request) | ||
| return { ...response, body: () => Promise.resolve(response.body) } | ||
| } | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
packages/client/src/adapters/electron-ipc/rpc-link.test.ts
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,72 @@ | ||
| import { decodeRequestMessage, encodeResponseMessage, MessageType } from '@orpc/standard-server-peer' | ||
| import { createORPCClient } from '../../client' | ||
| import { experimental_RPCLink as RPCLink } from './rpc-link' | ||
|
|
||
| beforeEach(() => { | ||
| delete (globalThis as any)['orpc:default'] | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('rpcLink', () => { | ||
| it('it throw if not find exposed channel', () => { | ||
| expect(() => new RPCLink()).toThrow() | ||
| }) | ||
|
|
||
| describe('on success', () => { | ||
| const send = vi.fn() | ||
| const receive = vi.fn() | ||
| ; (globalThis as any)['orpc:default'] = { | ||
| send, | ||
| receive, | ||
| } | ||
| const link = new RPCLink() | ||
| const orpc = createORPCClient(link) as any | ||
|
|
||
| const onMessage = receive.mock.calls[0]![0] | ||
|
|
||
| it('with text', async () => { | ||
| expect(orpc.ping('input')).resolves.toEqual('pong') | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
|
||
| const [id, , payload] = (await decodeRequestMessage(send.mock.calls[0]![0])) | ||
|
|
||
| expect(id).toBeTypeOf('number') | ||
| expect(payload).toEqual({ | ||
| url: new URL('orpc:/ping'), | ||
| body: { json: 'input' }, | ||
| headers: {}, | ||
| method: 'POST', | ||
| }) | ||
|
|
||
| onMessage(await encodeResponseMessage(id, MessageType.RESPONSE, { body: { json: 'pong' }, status: 200, headers: {} })) | ||
| }) | ||
|
|
||
| it('with blob', async () => { | ||
| const form = new FormData() | ||
| form.append('file', new Blob(['hello'], { type: 'text/plain' })) | ||
| form.append('text', 'world') | ||
|
|
||
| expect(orpc.ping({ | ||
| file: new Blob(['hello'], { type: 'text/plain' }), | ||
| text: 'world', | ||
| })).resolves.toEqual('pong') | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
|
||
| const [id, , payload] = (await decodeRequestMessage(send.mock.calls[0]![0])) | ||
|
|
||
| expect(id).toBeTypeOf('number') | ||
| expect(payload).toEqual({ | ||
| url: new URL('orpc:/ping'), | ||
| body: expect.toSatisfy(v => v instanceof FormData && v.get('0') instanceof Blob), | ||
| headers: { | ||
| 'content-type': expect.stringContaining('multipart/form-data'), | ||
| }, | ||
| method: 'POST', | ||
| }) | ||
|
|
||
| onMessage(await encodeResponseMessage(id, MessageType.RESPONSE, { body: { json: 'pong' }, status: 200, headers: {} })) | ||
| }) | ||
| }) | ||
| }) |
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,21 @@ | ||
| import type { ClientContext } from '../../types' | ||
| import type { StandardRPCLinkOptions } from '../standard' | ||
| import type { experimental_LinkElectronIPCClientOptions as LinkElectronIPCClientOptions } from './link-client' | ||
| import { StandardRPCLink } from '../standard' | ||
| import { experimental_LinkElectronIPCClient as LinkElectronIPCClient } from './link-client' | ||
|
|
||
| export interface experimental_RPCLinkOptions<T extends ClientContext> | ||
| extends Omit<StandardRPCLinkOptions<T>, 'url' | 'headers' | 'method' | 'fallbackMethod' | 'maxUrlLength'>, LinkElectronIPCClientOptions {} | ||
|
|
||
| /** | ||
| * The RPC Link communicates with the server using the RPC protocol over Electron IPC. | ||
| * | ||
| * @see {@link https://orpc.unnoq.com/docs/client/rpc-link RPC Link Docs} | ||
| * @see {@link https://orpc.unnoq.com/docs/integrations/electron-ipc Electron IPC Integration Docs} | ||
| */ | ||
| export class experimental_RPCLink<T extends ClientContext> extends StandardRPCLink<T> { | ||
| constructor(options: experimental_RPCLinkOptions<T> = {}) { | ||
| const linkClient = new LinkElectronIPCClient(options) | ||
| super(linkClient, { ...options, url: 'orpc:/' }) | ||
| } | ||
| } |
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,4 @@ | ||
| export interface experimental_ExposedORPCHandlerChannel { | ||
| send(message: string | ArrayBufferLike): void | ||
| receive(callback: (message: string | ArrayBufferLike) => void): void | ||
| } | ||
|
dinwwwh marked this conversation as resolved.
|
||
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,33 @@ | ||
| import { contextBridge, ipcRenderer } from 'electron' | ||
| import { experimental_exposeORPCHandlerChannel as exposeORPCHandlerChannel } from './expose' | ||
|
|
||
| vi.mock('electron', () => ({ | ||
| contextBridge: { | ||
| exposeInMainWorld: vi.fn(), | ||
| }, | ||
| ipcRenderer: { | ||
| on: vi.fn(), | ||
| send: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| beforeEach(() => { | ||
| delete (globalThis as any)['orpc:default'] | ||
| }) | ||
|
dinwwwh marked this conversation as resolved.
|
||
|
|
||
| it('exposeORPCHandlerChannel', () => { | ||
| exposeORPCHandlerChannel() | ||
|
|
||
| expect(contextBridge.exposeInMainWorld).toHaveBeenCalledWith('orpc:default', { | ||
| send: expect.any(Function), | ||
| receive: expect.any(Function), | ||
| }) | ||
|
|
||
| vi.mocked(contextBridge.exposeInMainWorld).mock.calls[0]![1].send('hello') | ||
| expect(ipcRenderer.send).toHaveBeenCalledWith('orpc:default', 'hello') | ||
|
|
||
| const messages: string[] = [] | ||
| vi.mocked(contextBridge.exposeInMainWorld).mock.calls[0]![1].receive((message: any) => messages.push(message)) | ||
| vi.mocked(ipcRenderer.on).mock.calls[0] | ||
| expect(messages).toEqual(['hello']) | ||
| }) | ||
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.