generated from ulivz/ts-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
657 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module.exports = { | ||
extends: "eslint-config-typescript-library", | ||
extends: 'eslint-config-typescript-library', | ||
rules: { | ||
camelcase: 'off', | ||
}, | ||
}; |
This file contains 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 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 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,152 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { ChannelMessage, Unport, Unrpc, UnrpcExecutionErrorError, UnrpcNotImplementationError } from '../src'; | ||
|
||
export type Definition = { | ||
parent2child: { | ||
syn: { | ||
pid: string; | ||
}; | ||
getInfo__callback: { | ||
user: string; | ||
}; | ||
getChildInfo: { | ||
name: string; | ||
} | ||
}; | ||
child2parent: { | ||
getInfo: { | ||
id: string; | ||
}; | ||
getChildInfo__callback: { | ||
clientKey: string; | ||
}; | ||
ack: { | ||
pid: string; | ||
}; | ||
}; | ||
}; | ||
|
||
describe('Unrpc', () => { | ||
let childPort: Unport<Definition, 'child'>; | ||
let parentPort: Unport<Definition, 'parent'>; | ||
let child: Unrpc<Definition, 'child'>; | ||
let parent: Unrpc<Definition, 'parent'>; | ||
|
||
beforeEach(() => { | ||
const messageChannel = new MessageChannel(); | ||
if (childPort) childPort.destroy(); | ||
childPort = new Unport(); | ||
childPort.implementChannel({ | ||
send(message) { | ||
messageChannel.port1.postMessage(message); | ||
}, | ||
accept(pipe) { | ||
messageChannel.port1.onmessage = (message: MessageEvent<ChannelMessage>) => pipe(message.data); | ||
}, | ||
destroy() { | ||
messageChannel.port1.close(); | ||
}, | ||
}); | ||
child = new Unrpc(childPort); | ||
|
||
parentPort = new Unport(); | ||
parentPort.implementChannel({ | ||
send(message) { | ||
console.log(message); | ||
messageChannel.port2.postMessage(message); | ||
}, | ||
accept(pipe) { | ||
messageChannel.port2.onmessage = (message: MessageEvent<ChannelMessage>) => pipe(message.data); | ||
}, | ||
destroy() { | ||
messageChannel.port2.close(); | ||
}, | ||
}); | ||
|
||
parent = new Unrpc(parentPort); | ||
}); | ||
|
||
it('implemented method - asynchronous implementation', async () => { | ||
parent.implement('getInfo', async ({ id }) => ({ user: id })); | ||
const response = child.call('getInfo', { id: 'name' }); | ||
expect(response).resolves.toMatchObject({ user: 'name' }); | ||
}); | ||
|
||
it('implemented method - synchronous implementation', async () => { | ||
parent.implement('getInfo', ({ id }) => ({ user: id })); | ||
const response = child.call('getInfo', { id: 'name' }); | ||
expect(response).resolves.toMatchObject({ user: 'name' }); | ||
}); | ||
|
||
it('Error: UnrpcNotImplementationError', async () => { | ||
expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( | ||
new UnrpcNotImplementationError('Method getInfo is not implemented'), | ||
); | ||
}); | ||
|
||
it('Error: UnrpcExecutionErrorError - script error - asynchronous implementation', async () => { | ||
parent.implement('getInfo', async () => { | ||
// @ts-expect-error mock execution error here. | ||
const result = foo; | ||
return result; | ||
}); | ||
expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( | ||
new UnrpcExecutionErrorError('foo is not defined'), | ||
); | ||
}); | ||
|
||
it('Error: UnrpcExecutionErrorError - script error - synchronous implementation', async () => { | ||
parent.implement('getInfo', () => { | ||
// @ts-expect-error mock execution error here. | ||
const result = foo; | ||
return result; | ||
}); | ||
expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( | ||
new UnrpcExecutionErrorError('foo is not defined'), | ||
); | ||
}); | ||
|
||
it('Error: UnrpcExecutionErrorError - user throws error', async () => { | ||
parent.implement('getInfo', () => { | ||
throw new Error('mock error'); | ||
}); | ||
expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( | ||
new UnrpcExecutionErrorError('mock error'), | ||
); | ||
}); | ||
|
||
it('complicated case', async () => { | ||
parent.implement('getInfo', async ({ id }) => ({ user: id })); | ||
child.implement('getChildInfo', async ({ name }) => ({ clientKey: name })); | ||
|
||
let finishHandshake: (value?: unknown) => void; | ||
const handshakePromise = new Promise(resolve => { | ||
finishHandshake = resolve; | ||
}); | ||
|
||
/** | ||
* Simulates a handshake | ||
*/ | ||
parent.port.postMessage('syn', { pid: 'parent' }); | ||
parent.port.onMessage('ack', async payload => { | ||
expect(payload.pid).toBe('child'); | ||
finishHandshake(); | ||
}); | ||
child.port.onMessage('syn', async payload => { | ||
expect(payload.pid).toBe('parent'); | ||
child.port.postMessage('ack', { pid: 'child' }); | ||
}); | ||
|
||
/** | ||
* Wait handshake finished | ||
*/ | ||
await handshakePromise; | ||
|
||
const [response1, response2] = await Promise.all([ | ||
child.call('getInfo', { id: 'child' }), | ||
parent.call('getChildInfo', { name: 'parent' }), | ||
]); | ||
expect(response1).toMatchObject({ user: 'child' }); | ||
expect(response2).toMatchObject({ clientKey: 'parent' }); | ||
}); | ||
}); |
This file contains 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,29 @@ | ||
import { Unport, Unrpc, ChannelMessage } from '../../lib'; | ||
import { ChildPort } from './port'; | ||
|
||
// 1. Initialize a port | ||
const childPort: ChildPort = new Unport(); | ||
|
||
// 2. Implement a Channel based on underlying IPC capabilities | ||
childPort.implementChannel({ | ||
send(message) { | ||
process.send && process.send(message); | ||
}, | ||
accept(pipe) { | ||
process.on('message', (message: ChannelMessage) => { | ||
pipe(message); | ||
}); | ||
}, | ||
}); | ||
|
||
// 3. Initialize a rpc client | ||
const childRpcClient = new Unrpc(childPort); | ||
childRpcClient.implement('getChildInfo', request => ({ | ||
clientKey: `[child] ${request.name}`, | ||
})); | ||
childRpcClient.port.onMessage('syn', async payload => { | ||
console.log('[child] [event] [syn] [result]', payload); | ||
const response = await childRpcClient.call('getInfo', { id: '<child>' }); | ||
console.log('[child] [rpc] [getInfo] [response]', response); | ||
childPort.postMessage('ack', { pid: 'child' }); | ||
}); |
This file contains 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,43 @@ | ||
import { join } from 'path'; | ||
import { fork } from 'child_process'; | ||
import { Unport, Unrpc, ChannelMessage } from '../../lib'; | ||
import { ParentPort } from './port'; | ||
|
||
// 1. Initialize a port | ||
const parentPort: ParentPort = new Unport(); | ||
|
||
// 2. Implement a Channel based on underlying IPC capabilities | ||
const childProcess = fork(join(__dirname, './child.js')); | ||
parentPort.implementChannel({ | ||
send(message) { | ||
childProcess.send(message); | ||
}, | ||
accept(pipe) { | ||
childProcess.on('message', (message: ChannelMessage) => { | ||
pipe(message); | ||
}); | ||
}, | ||
destroy() { | ||
childProcess.removeAllListeners('message'); | ||
childProcess.kill(); | ||
}, | ||
}); | ||
|
||
// 3. Initialize a rpc client from port. | ||
const parentRpcClient = new Unrpc(parentPort); | ||
|
||
parentRpcClient.implement('getInfo', request => ({ | ||
user: `parent (${request.id})`, | ||
})); | ||
parentRpcClient.port.postMessage('syn', { pid: 'parent' }); | ||
parentRpcClient.port.onMessage('ack', async payload => { | ||
console.log('[parent] [event] [ack] [result]', payload); | ||
const response = await parentRpcClient.call('getChildInfo', { | ||
name: 'parent', | ||
}); | ||
console.log('[parent] [rpc] [getChildInfo] [response]', response); | ||
setTimeout(() => { | ||
console.log('destroy'); | ||
parentPort.destroy(); | ||
}, 1000); | ||
}); |
Oops, something went wrong.