You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

sendscript

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sendscript

Blur the line between server and client code.

1.0.0
Source
npmnpm
Version published
Weekly downloads
9
800%
Maintainers
1
Weekly downloads
 
Created
Source

SendScript

Write JS code that you can run on servers, browsers or other clients.

NPM 100% Code Coverage Standard Code Style License

SendScript leaves it up to you to choose HTTP, web-sockets or any other method of communication between servers and clients that best fits your needs.

Socket example

For this example we'll use socket.io.

npm install --no-save socket.io socket.io-client

We use the --no-save option because it's only for demonstration purposes.

Module

We write a simple module.

// ./example/math.mjs

export const add = (a, b) => a + b
export const square = a => a * a

Server

Here a socket.io server that runs SendScript programs.

// ./example/server.socket.io.mjs

import { Server } from 'socket.io'
import Parse from '../parse.mjs'
import * as math from './math.mjs'

const parse = Parse(math)
const server = new Server()
const port = process.env.PORT || 3000

server.on('connection', (socket) => {
  socket.on('message', async (program, callback) => {
    try {
      const result = parse(program)
      callback(null, result) // Pass null as the first argument to indicate success
    } catch (error) {
      callback(error) // Pass the error to the callback
    }
  })
})

server.listen(port)
process.title = 'sendscript'

Client

Now for a client that sends a program to the server.

// ./example/client.socket.io.mjs

import socketClient from 'socket.io-client'
import stringify from '../stringify.mjs'
import module from '../module.mjs'
import * as math from './math.mjs'
import assert from 'node:assert'

const port = process.env.PORT || 3000
const client = socketClient(`http://localhost:${port}`)

const send = program => {
  return new Promise((resolve, reject) => {
    client.emit('message', stringify(program), (error, result) => {
      error
        ? reject(error)
        : resolve(result)
    })
  })
}

const { add, square } = module(math)

// The program to be sent over the wire
const program = square(add(1, add(add(2, 3), 4)))

const result = await send(program)

console.log('Result: ', result)

assert.equal(result, 100)

process.exit(0)

Now we run this server and a client script.

set -e

# Run the server
node ./example/server.socket.io.mjs&

# Run the client example
node ./example/client.socket.io.mjs

pkill sendscript
Result:  100

TypeScript

There is a good use-case to write a module in TypeScript.

  • Obviously the module would have the benefits that TypeScript offers when coding.
  • You can use tools like typedoc to generate docs from your types to share with consumers of your API.
  • You can use the types of the module to coerce your client to adopt the module's type.
# Create pretty docs for your module.
npx typedoc my-module.ts

Now we can use the my-module.ts file for the client API.

import type * as MyModule from './my-module'

import sendScriptApi from 'sendscript/api.mjs'

export default sendScriptApi([
  fnOne,
  fnTwo,
], /* perform websocket request */) as typeof MyModule

[!NOTE] Although type coercion on the client side can improve the development experience, it does not represent the actual type. Values are subject to serialization and deserialization.

Tests

Tests with 100% code coverage.

npm t -- -R silent
npm t -- report text-summary

> sendscript@1.0.0 test
> tap -R silent


> sendscript@1.0.0 test
> tap report text-summary


=============================== Coverage summary ===============================
Statements   : 100% ( 239/239 )
Branches     : 100% ( 71/71 )
Functions    : 100% ( 18/18 )
Lines        : 100% ( 239/239 )
================================================================================

Formatting

Standard because no config.

npx standard

Changelog

The changelog is generated using the useful auto-changelog project.

npx auto-changelog -p

Dependencies

Check if packages are up to date on release.

npm outdated && echo 'No outdated packages found'
No outdated packages found

License

See the LICENSE.txt file for details.

Roadmap

  • Support for simple lambdas to compose functions more easily.

Keywords

rpc

FAQs

Package last updated on 03 May 2025

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