6.1.0 • Published 5 months ago

@delight-rpc/browser v6.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@delight-rpc/browser

Install

npm install --save @delight-rpc/browser
# or
yarn add @delight-rpc/browser

Usage

Web Worker

Main as Client, Web Worker as Server

// api.d.ts
interface IAPI {
  echo(message: string): string
}

// main.ts
import { createClient } from '@delight-rpc/browser'

const worker = new Worker('./worker.js')
const [client] = createClient<IAPI>(worker)
await client.echo('hello world')

// worker.ts
import { createServer } from '@delight-rpc/browser'

const api: IAPI = {
  echo(message) {
    return message
  }
}

createServer(api, self)

Web Worker as Client, Main as Server

// api.d.ts
interface IAPI {
  echo(message: string): string
}

// main.ts
import { createServer } from '@delight-rpc/browser'

const api: IAPI = {
  echo(message) {
    return message
  }
}

const worker = new Worker('./worker.js')
createServer(api, worker)

// worker.ts
import { createClient } from '@delight-rpc/browser'

const [client] = createClient<IAPI>(self)
await client.echo('hello world')

MessageChannel

interface IAPI {
  echo(message: string): string
}

const api: IAPI = {
  echo(message) {
    return message
  }
}

const channel = new MessageChannel()
channel.port1.start()
channel.port2.start()

createServer<IAPI>(api, channel.port1)

const [client] = createClient<IAPI>(channel.port2)
await client.echo('hello world')

API

createClient

function createClient<IAPI extends object>(
  port: Window | MessagePort | Worker
, options?: {
    parameterValidators?: DelightRPC.ParameterValidators<IAPI>
    expectedVersion?: string
    channel?: string
  }
): [client: DelightRPC.ClientProxy<IAPI>, close: () => void]

createBatchClient

function createBatchClient<DataType>(
  port: Window | MessagePort | Worker
, options?: {
    expectedVersion?: string
    channel?: string
  }
): [client: DelightRPC.BatchClient<DataType>, close: () => void]

createServer

function createServer<IAPI extends object>(
  api: DelightRPC.ImplementationOf<IAPI>
, port: Window | MessagePort | Worker
, options?: {
    parameterValidators?: DelightRPC.ParameterValidators<IAPI>
    version?: `${number}.${number}.${number}`
    channel?: string | RegExp | AnyChannel
    ownPropsOnly?: boolean
  }
): () => void

createHTTPHandler

function createHTTPHandler<IAPI>(
  api: DelightRPC.ImplementationOf<IAPI>
, options?: {
    basicAuth?: (username: string, password: string) => Awaitable<boolean>
    parameterValidators?: DelightRPC.ParameterValidators<IAPI>
    version?: `${number}.${number}.${number}`
    ownPropsOnly?: boolean
    channel?: string | RegExp | typeof DelightRPC.AnyChannel
  }
): (req: Request) => Promise<Response>
6.1.0

5 months ago

6.0.2

11 months ago

6.0.1

1 year ago

6.0.0

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.1.6

2 years ago

4.1.5

2 years ago

4.1.4

2 years ago

4.1.3

2 years ago

4.1.0

2 years ago

4.0.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago