An open API service indexing awesome lists of open source software.

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

🌱

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
```