Giter Site home page Giter Site logo

ryanl-29 / srpc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yziti/srpc

0.0 0.0 0.0 30 KB

Customized & Minimal RPC Protocol based on HTTP for JavaScript

Home Page: https://yziti.github.io/srpc/

License: Apache License 2.0

JavaScript 92.69% HTML 7.31%

srpc's Introduction

SRPC

A super simple RPC, connect client and server with the least possible code!

NO dependency, NO schema, NO config, just define functions and CALL!

// On Server
const srpc = require('./server-node.js')
srpc() // start server
srpc.add = (x, y) => x + y

// -------------------------------------

// On Client
import srpc from './client-es.js'
srpc('http://localhost:11111/') // server endpoint
console.log(await srpc.add(1, 2)) // 3

Server

export functions to be called by clients

Nodejs

const srpc = require('./server-node.js')

srpc() // listen on port 11111 by default

// the following methods are exported
srpc.test = () => 'Hello, world!'
srpc.add = (x, y) => x + y
// function can be nested!
srpc.calc = {}
srpc.calc.sqrt = x => Math.sqrt(x)

Aliyun Function Compute

const srpc = require('./server-fc.js')

// the following methods are exported
srpc.test = () => 'Hello, world!'
srpc.add = (x, y) => x + y
srpc.calc = {}
srpc.calc.sqrt = x => Math.sqrt(x)

// entrance
exports.handler = srpc()

Client

call functions on server and get the return value

Online Client Demo/Debug

Browser

Add script to <head>

<script src="https://cdn.jsdelivr.net/gh/yzITI/srpc@main/client.js"></script>

OR copy client-es.js into your ESM project:

import srpc from './client-es.js'
// initialize with endpoint
srpc('http://localhost:11111/')

// just call the functions!
srpc.test() // Promise -> 'Hello, world!'
srpc.add(1, 2) // Promise -> 3
srpc.calc.sqrt(2) // Promise -> 1.4142135623730951

Nodejs

const srpc = require('./client-node.js')

// initialize with endpoint
srpc('https://matrix.yzzx.org/srpc')

srpc.test() // Promise -> 'Hello, world!'
srpc.add(1, 2) // Promise -> 3
srpc.calc.sqrt(2) // Promise -> 1.4142135623730951

Protocol Model

The following request and response model are used with http POST method and 'Content-Type': 'application/json'.

Request {
  N: 'function.name', // function name
  A: [1, 2, 3] // args in order
}
Response {
  R: {} // return value
}
// Context data model used in hooks
Context {
  N, A, R,
  IP: String, // request IP
  F: Function
}

Server Config

// node-srpc
srpc(hooks = {
  before: Context => {}, // abort if assign Context.R
  after: Context => {}
}, port = 11111)

// fc-srpc
srpc(hooks = {
  before: Context => {}, // abort if assign Context.R
  after: Context => {}
})

srpc's People

Contributors

phantomlsh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.