sendscript
Advanced tools
Comparing version
@@ -7,4 +7,12 @@ ### Changelog | ||
#### [v1.0.0](https://github.com/bas080/sendscript/compare/v0.1.4...v1.0.0) | ||
- Add support for async await [`e949184`](https://github.com/bas080/sendscript/commit/e949184ab31a3bfdf4c6181463dcdd7250aca3a8) | ||
- Update tap to latest version on 2024-03-17 [`1164994`](https://github.com/bas080/sendscript/commit/11649947737ffd152ac46d1db01946ad0c1261e7) | ||
- Support nested objects and keywords as first item [`06da388`](https://github.com/bas080/sendscript/commit/06da3887c05ab0c545a53b6e796d2f7d4c53e6e6) | ||
#### [v0.1.4](https://github.com/bas080/sendscript/compare/v0.1.3...v0.1.4) | ||
> 8 March 2024 | ||
- Add missing index file that exports both modules [`38054ea`](https://github.com/bas080/sendscript/commit/38054ea8284a1626146bec42309b3c014527ff7d) | ||
@@ -11,0 +19,0 @@ - Prevent ref of non existant property on env object [`49520ad`](https://github.com/bas080/sendscript/commit/49520ad32604683c668d95e479de772a91f6ae2b) |
{ | ||
"name": "sendscript", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"description": "Blur the line between server and client code.", | ||
@@ -20,3 +20,3 @@ "module": true, | ||
"test": "tap", | ||
"version": "npm run docs && git add *.md", | ||
"version": "npm run docs && git add *.md example", | ||
"docs": "markatzea README.mz | tee README.md && npx markdown-toc -i README.md" | ||
@@ -27,7 +27,7 @@ }, | ||
"devDependencies": { | ||
"tap": "^18.5.2" | ||
"tap": "^21.0.1" | ||
}, | ||
"dependencies": { | ||
"debug": "^4.3.4" | ||
"debug": "^4.3.7" | ||
} | ||
} |
117
README.md
@@ -16,5 +16,2 @@ # SendScript | ||
* [Client](#client) | ||
- [Reference](#reference) | ||
* [`sendscript/api.mjs`](#sendscriptapimjs) | ||
* [`sendscript/exec.mjs`](#sendscriptexecmjs) | ||
- [TypeScript](#typescript) | ||
@@ -26,2 +23,3 @@ - [Tests](#tests) | ||
- [License](#license) | ||
- [Roadmap](#roadmap) | ||
@@ -63,5 +61,6 @@ <!-- tocstop --> | ||
import { Server } from 'socket.io' | ||
import exec from '../exec.mjs' | ||
import Parse from '../parse.mjs' | ||
import * as math from './math.mjs' | ||
const parse = Parse(math) | ||
const server = new Server() | ||
@@ -73,3 +72,3 @@ const port = process.env.PORT || 3000 | ||
try { | ||
const result = await exec(math, program) | ||
const result = parse(program) | ||
callback(null, result) // Pass null as the first argument to indicate success | ||
@@ -94,3 +93,6 @@ } catch (error) { | ||
import socketClient from 'socket.io-client' | ||
import api from '../api.mjs' | ||
import stringify from '../stringify.mjs' | ||
import module from '../module.mjs' | ||
import * as math from './math.mjs' | ||
import assert from 'node:assert' | ||
@@ -100,5 +102,5 @@ const port = process.env.PORT || 3000 | ||
const exec = program => { | ||
const send = program => { | ||
return new Promise((resolve, reject) => { | ||
client.emit('message', program, (error, result) => { | ||
client.emit('message', stringify(program), (error, result) => { | ||
error | ||
@@ -111,8 +113,13 @@ ? reject(error) | ||
const { add, square } = api(['add', 'square'], exec) | ||
const { add, square } = module(math) | ||
console.log( | ||
await square(add(1, add(add(2, 3), 4))) | ||
) | ||
// 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) | ||
@@ -124,2 +131,4 @@ ``` | ||
```bash | ||
set -e | ||
# Run the server | ||
@@ -134,67 +143,8 @@ node ./example/server.socket.io.mjs& | ||
``` | ||
100 | ||
Result: 100 | ||
``` | ||
## Reference | ||
SendScript is essentially a way to serialize a program to then send over the | ||
wire and execute it somewhere else. | ||
We only have two modules. One that helps you write programs that can be sent | ||
over the wire and another for running that program. | ||
### `sendscript/api.mjs` | ||
The api module exports a function that takes two arguments. | ||
1. The schema, which represents the values that are available. | ||
2. The function that will be called with the serializable version of the | ||
program. | ||
It returns an object that contains functions which are defined in the schema. | ||
These functions are a JavaScript API for writing programs that can be sent to | ||
a server. | ||
```js | ||
import api from './api.mjs' | ||
const { add, subtract } = api( | ||
['add', 'subtract'], | ||
serializableProgram => sendSomewhereToBeExecuted(serializableProgram) | ||
) | ||
await add(1, 2) // => 3 | ||
await subtract(1, 2) // => -1 | ||
await add(1, subtract(2, 3)) // => 0 | ||
``` | ||
The add and subtract functions are thennable. The execute function is called as | ||
soon as await or `.then` is used. | ||
> Notice that you do not have to await the subtract call. You only need to | ||
> await when you want to execute the program. | ||
This API is composable and wrappable. | ||
### `sendscript/exec.mjs` | ||
The exec function takes an environment object and any valid SendScript program. | ||
```js | ||
import exec from './exec.mjs' | ||
exec({ | ||
add: (a, b) => a + b, | ||
subtract: (a, b) => a - b | ||
}, ['add', 1, [subtract, 1, 2]]) | ||
``` | ||
The array you see here is the LISP that SendScript uses to represent programs. | ||
You could use SendScript without knowing the details of how the LISP works. It | ||
is an implementation detail and might change over time. | ||
## TypeScript | ||
There is a good use-case to write an environment module in TypeScript. | ||
There is a good use-case to write a module in TypeScript. | ||
@@ -206,3 +156,3 @@ 1. Obviously the module would have the benefits that TypeScript offers when | ||
3. You can use the types of the module to coerce your client to adopt the | ||
modules type. | ||
module's type. | ||
@@ -230,4 +180,3 @@ ```bash | ||
> experience, it does not represent the actual type. | ||
> Values are likely subject to serialization and deserialization, | ||
> particularly when interfacing with JSON formats. | ||
> Values are subject to serialization and deserialization. | ||
@@ -244,7 +193,7 @@ ## Tests | ||
> sendscript@0.1.4 test | ||
> sendscript@1.0.0 test | ||
> tap -R silent | ||
> sendscript@0.1.4 test | ||
> sendscript@1.0.0 test | ||
> tap report text-summary | ||
@@ -254,6 +203,6 @@ | ||
=============================== Coverage summary =============================== | ||
Statements : 100% ( 110/110 ) | ||
Branches : 100% ( 32/32 ) | ||
Functions : 100% ( 10/10 ) | ||
Lines : 100% ( 110/110 ) | ||
Statements : 100% ( 239/239 ) | ||
Branches : 100% ( 71/71 ) | ||
Functions : 100% ( 18/18 ) | ||
Lines : 100% ( 239/239 ) | ||
================================================================================ | ||
@@ -294,2 +243,6 @@ ``` | ||
## Roadmap | ||
- [ ] Support for simple lambdas to compose functions more easily. | ||
[license]:./LICENSE.txt | ||
@@ -296,0 +249,0 @@ [socket.io]:https://socket.io/ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
29886
17.59%20
11.11%492
92.19%1
-50%239
-16.43%1
Infinity%Updated