Code Monkey home page Code Monkey logo

renraku's Introduction

連絡
れんらく
R·E·N·R·A·K·U

npm install renraku

🔆 make smart typescript apis
🛎️ simply expose async functions
🛡 you set auth policies for groups of functions
🎭 easy mocks for testing and development
🧠 designed for good typescript types
🌐 make http json-rpc apis
🔁 make bidirectional websocket json-rpc apis


⛩️ RENRAKU teaches by example

  1. let's make an example api

    import * as renraku from "renraku"
    
    export const exampleApi = renraku.api({
    
      // we organize functions into services.
      greeter: renraku.service()
    
        // each service has its own auth policy.
        // a policy processes a "meta" object into an "auth" object.
        // this is where you might process access tokens, or verify permissions.
        // here's our contrived example policy.
        .policy(async(meta: {token: string}) => {
          if (meta.token) return {doctorate: meta.token === "doctor"}
          else throw new Error("invalid token")
        })
    
        // here's where our functions get access to our "auth" object.
        .expose(auth => ({
    
          // here's our silly example function.
          async greet(name: string) {
            return auth.doctorate   // if the user's doctorate is valid,
              ? `hello dr. ${name}` // we greet them formally,
              : `hi ${name}`        // otherwise, we greet them differently.
          },
        })),
    })
  2. now let's run our api on a node server

    import * as renraku from "renraku"
    import {nodeServer} from "renraku/x/http/node-server.js"
    import {exampleApi} from "./example-api.js"
    
    const server = nodeServer({
      api: exampleApi,
      exposeErrors: false,
      maxPayloadSize: renraku.megabytes(10),
    })
    
    server.listen(8000)
  3. now let's call that function from a browser

    import * as renraku from "renraku"
    import type {exampleApi} from "./example-api.js"
    
    // let's start as a doctor
    let meta = {token: "doctor"}
    
    // we create a browser client
    const {greeter} = renraku.browserClient({
      url: "http://localhost:8000/",
      metaMap: {
        // on the service, we specify which meta to use for api calls
        calculator: async() => meta,
      },
    })
    
    // hey look, we're a doctor!
    const result1 = await greeter.greet("chase")
      //> "hello, dr. chase"
    
    // okay let's stop being a doctor
    meta = {token: "not a doctor"}
    const result2 = await greeter.greet("chase")
      //> "hi chase"
    
    // now let's just fail to provide a valid meta
    meta = {token: undefined}
    const result3 = await greeter.greet("chase")
      //> ERROR! "invalid token"

⛩️ RENRAKU mocks help you test your app

  1. let's test our example-api, locally, in our test suite

    import * as renraku from "renraku"
    import {exampleApi} from "./example-api.js"
    
    // okay, let's start with a valid doctor token
    let meta = {token: "doctor"}
    
    // create a mock remote of our api
    const {greeter} = renraku.mock()
      .forApi(exampleApi)
      .withMetaMap({
        greeter: async() => meta,
      })
    
    // now we can call and test our api's functionality,
    // without running any servers, or clients, or any of that.
    
    const result1 = await greeter.greet("chase")
      //> "hello, dr. chase"
    
    meta = {token: "not a doctor"}
    const result2 = await greeter.greet("chase")
      //> "hi chase"
    
    // not only are we testing our api's business logic,
    // but we are also testing the auth policies too!
  2. when making our mocks, we may choose to skip the auth policy logic

    const {greeter} = renraku.mock()
      .forApi(exampleApi)
    
      // 👇 an auth map overrides auth policies
      .withAuthMap({
    
        //          we're forcing this auth result
        //                    👇
        greeter: async() => ({doctorate: true}),
      })

⛩️ RENRAKU error handling

    ~ readme docs coming soon lol ~


⛩️ RENRAKU also lets you build two-way websocket systems

    ~ readme docs coming soon lol ~



      — RENRAKU means "contact" —

renraku's People

Contributors

chase-moskal avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

renraku's Issues

json-rpc spec compliance

renraku should properly comply with the json rpc spec, it currently doesn't, work should be trivial

rewrite renraku

it's finally hit me that we can eliminate the concept of shapes by embracing javascript proxies

💪 🧠 🧠 🧠 🔥

  • use proxies instead of shapes
  • built in rate limiting and denial-of-service mitigations
  • consider integrating validation based on darkvalley (must support i18n)

browser automated tests

we should have a test suite that runs in pupeteer or playwright or whatever the kids are using these days

  • run a node instance
  • test real cors policies
  • test websocket connections

socket error handling should be improved

  • we need a timeout mechanism for websocket requests
    • seems like ws does this automatically after 60 seconds
  • we need to make sure error responses with a request-id of -1 are handled gracefully
    this can happen when a request fails to parse to the extent the id cannot be recovered (including when the request body exceeds the maximum payload size)

rate limiting design and implementation

ideas

  • we need an ergonomic way to achieve rate-limiting
  • this work should be coordinated with xiome's needs

criteria

  • test driven development, so we can be sure this thing works
  • mechanism to limit total size of incoming requests
    • per api-context
    • at the buffer-level, so we can cut off evil connections before it's too late
  • cool-off or cool-down mechanism
    • customizable, should be able to work at the api-context level, or per async function
    • should be able to operate by client ip address (ip's need to be integrated into renraku httprequest)
    • should be able to operate by app-factors like access tokens
  • all rate-limiting should throw accurate errors like 413 and 429 appropriately

Read me docs when??

Hey chase,

I remembered that you made a library for webrtc and was thinking of incorporating it into my project

Can you please point me to the library that you used for webrtc

Thanks
lucifer

logging

  • for clientside
  • for serverside

perhaps differentiate spikes, spikeRequest, and spikeResponse

but requiring users to understand the spike concept to achieve logging is bad ergonomically, even if it's simplest system with the best capabilities 🤔

to merge or not to merge? with crosscall?

crosscall will have much cross-over with renraku

crosscall is for communication between browser windows

renraku is basically crosscall but for communication between node servers and browser clients

samenesses

  • callee/callable concept with topics and events
  • permissions system for whitelisting topics and events
  • client-server handshake provides mediates available topics and events (then clientside callable is ready)

differences

  • event systems are totally, totally different
    • renraku serverside events would require websockets
    • renraku just might skip events (for now at least)
    • for crosscall, events are just more postmessages

possible end states

  • renraku completely cannibalizes crosscall, taking over its role and use-cases
  • renraku becomes like a library that crosscall uses in its core
  • renraku remains totally separated from, albeit initially inspired by, crosscall

rework readme

after using renraku a lot, i'm finding it seems easier to co-locate policies together with exposed topic functionality — that is, having apiContext files instead of topic files

  • perhaps rename apiContext to apiService, which may be easier to understand
  • instead of giving a piecemeal tutorial in the readme, let's just show a whole demonstration in one piece

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.