Socket
Book a DemoInstallSign in
Socket

@saghen/hermes

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@saghen/hermes

Generic interface for one and two way communication layer

2.0.0-alpha.5
latest
Source
npmnpm
Version published
Weekly downloads
712
5.33%
Maintainers
1
Weekly downloads
 
Created
Source

hermes

Hermes provides a generic interface for one way (Endpoints) and two way (Sockets) communication. It requires a transport for sending data between the server and client. The library provides transport implementations for HTTP (Endpoints only), Websocket, Web postMessage, Browser Extension browser.runtime.sendMessage/port and a loopback.

Usage

import { createRouter, createEndpointClient, createSocketClient, Socket } from './src'
import { createLoopback, LoopbackRequestMetadata } from './src/transports/loopback'

const loopback = createLoopback()

// Setup router
const endpoints = {
  add: async (a: number, b: number) => a + b,
  deep: {
    echo: async (message: string) => message,
  },
  fail: () => Promise.reject('This endpoint fails'),
  metadata: async (hello: 'world', metadata: LoopbackRequestMetadata) => hello,
}
const sockets = {
  echo: async (socket: Socket<any, any>) => {
    for await (const message of socket.receiveIter()) {
      await socket.send(message)
    }
  },
  test: async () => {},
}
const router = createRouter(endpoints, sockets)
loopback.listen(router)

// Setup clients
const endpointClient = createEndpointClient<typeof router.endpoints>(loopback.endpointTransport)
const socketClient = createSocketClient<typeof router.sockets>(loopback.socketTransport)

// Test endpoint client
await endpointClient.add(1, 2).then(console.log) // '3'
await endpointClient.deep.echo('foo').then(console.log) // 'foo'
await endpointClient.fail().catch(console.error) // 'This endpoint fails'
await endpointClient.metadata('world') // the second argument metadata passed by loopback transport

// Test socket client
const socket = await socketClient.echo()
await socket.send('Hello world!')
await socket.receive().then(console.log) // 'Hello world'

FAQs

Package last updated on 28 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.