1.1.26 • Published 6 years ago

@diginet/noice-json-rpc v1.1.26

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Noice Json Rpc (forked from nojvek/noice-json-rpc)

Build Status Coverage Status issues open npm total downloads licence npm version

Client and Server helpers to implement a clean function based Api for Json Rpc.

Noice Json Rpc takes in a websocket like object. It calls send(msg:str) function and expects messages to come from on('message', handler). It works out of the box with WebSockets but it can also work with stdin/stdout, worker threads, iframes or any other mechanism in which strings can be sent or received.

Its only dependency is events.EventEmitter.

Using ES6 proxies it exposes a clean client-server api. Since its written in TypeScript, the api object can be cast to work off an interface specific to the domain. e.g chrome-remote-debug-protocol

Example

import * as WebSocket from 'ws'
import WebSocketServer = WebSocket.Server
import * as rpc from 'noice-json-rpc'

async function setupClient() {
    try {
        const api = new rpc.Client(new WebSocket("ws://localhost:8080"), {logConsole: true}).api()

        await Promise.all([
            api.Runtime.enable(),
            api.Debugger.enable(),
            api.Profiler.enable(),
            api.Runtime.run(),
        ])

        await api.Profiler.start()
        await new Promise((resolve) => api.Runtime.onExecutionContextDestroyed(resolve)); // Wait for event
        await api.Profiler.stop()

    } catch (e) {
        console.error(e)
    }
}

function setupServer() {
    const wssServer = new WebSocketServer({port: 8080});
    const api = new rpc.Server(wssServer).api();

    const enable = () => {}

    api.Debugger.expose({enable})
    api.Profiler.expose({enable})
    api.Runtime.expose({
        enable,
        run() {}
    })
    api.Profiler.expose({
        enable,
        start() {
            setTimeout(() => {
                api.Runtime.emitExecutionContextDestroyed()
            }, 1000)
        },
        stop() {
            return {data: "noice!"}
        }
    })

}

setupServer()
setupClient()

Output

Client > {"id":1,"method":"Runtime.enable"}
Client > {"id":2,"method":"Debugger.enable"}
Client > {"id":3,"method":"Profiler.enable"}
Client > {"id":4,"method":"Runtime.run"}
Client < {"id":1,"result":{}}
Client < {"id":2,"result":{}}
Client < {"id":3,"result":{}}
Client < {"id":4,"result":{}}
Client > {"id":5,"method":"Profiler.start"}
Client < {"id":5,"result":{}}
Client < {"method":"Runtime.executionContextDestroyed"}
Client > {"id":6,"method":"Profiler.stop"}
Client < {"id":6,"result":{"data":"noice!"}}
1.1.26

6 years ago

1.1.25

6 years ago

1.1.24

6 years ago

1.1.23

6 years ago

1.1.22

6 years ago

1.1.21

6 years ago

1.1.20

6 years ago

1.1.19

6 years ago

1.1.18

6 years ago

1.1.17

6 years ago

1.1.16

6 years ago

1.1.15

6 years ago

1.1.14

6 years ago

1.1.13

6 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago