https://github.com/delight-rpc/child-process
🌱
https://github.com/delight-rpc/child-process
esm library nodejs npm-package typescript
Last synced: 2 months ago
JSON representation
🌱
- Host: GitHub
- URL: https://github.com/delight-rpc/child-process
- Owner: delight-rpc
- License: mit
- Created: 2021-12-03T16:53:39.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-04T03:02:49.000Z (over 2 years ago)
- Last Synced: 2025-02-14T15:50:26.779Z (over 1 year ago)
- Topics: esm, library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@delight-rpc/child-process
- Size: 1.05 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @delight-rpc/child-process
## Install
```sh
npm install --save @delight-rpc/child-process
# or
yarn add @delight-rpc/child-process
```
## Usage
### Main as Client, ChildProcess as Server
```ts
// api.d.ts
interface IAPI {
echo(message: string): string
}
// child-process.ts
import { createServer } from '@delight-rpc/child-process'
const api: IAPI = {
echo(message: string): string {
return message
}
}
createServer(api, process)
// main.ts
import { fork } from 'child_process'
import { createClient } from '@delight-rpc/child-process'
const childProcess = fork('./child-process.js', { serialization: 'advanced' })
const [client] = createClient(childProcess)
await client.echo('hello world')
```
### ChildProcess as Client, Main as Server
```ts
// api.d.ts
interface IAPI {
echo(message: string): string
}
// main.ts
import { fork } from 'child_process'
import { createServer } from '@delight-rpc/child-process'
const api: IAPI = {
echo(message: string): string {
return message
}
}
const childProcess = fork('./child-process.js', { serialization: 'advanced' })
createServer(api, childProcess)
// child-process.ts
import { createClient } from '@delight-rpc/child-process'
const [client] = createClient(process)
await client.echo('hello world')
```
## API
### createClient
```ts
function createClient(
process: ChildProcess | NodeJS.Process
, options?: {
parameterValidators?: DelightRPC.ParameterValidators
expectedVersion?: string
channel?: string
timeout?: number
}
): [client: DelightRPC.ClientProxy, close: () => void]
```
### createBatchClient
```ts
function createBatchClient(
process: ChildProcess | NodeJS.Process
, options?: {
expectedVersion?: string
channel?: string
timeout?: number
}
): [client: DelightRPC.BatchClient, close: () => void]
```
### createServer
```ts
function createServer(
api: DelightRPC.ImplementationOf
, process: ChildProcess | NodeJS.Process
, options?: {
parameterValidators?: DelightRPC.ParameterValidators
version?: `${number}.${number}.${number}`
channel?: string | RegExp | AnyChannel
ownPropsOnly?: boolean
}
): () => void
```