@@ -9,6 +9,7 @@ Write JS code that you can run on servers, browsers or other clients.
99
1010<!-- toc -->
1111
12+ - [ Introduction] ( #introduction )
1213- [ Socket example] ( #socket-example )
1314 * [ Module] ( #module )
1415 * [ Server] ( #server )
@@ -25,6 +26,75 @@ Write JS code that you can run on servers, browsers or other clients.
2526
2627<!-- tocstop -->
2728
29+ ## Introduction
30+
31+ There has been interest in improving APIs by allowing aggregations in a
32+ single request. Examples include
33+
34+ - [ JSON-RPC] ( https://json-rpc.dev/ ) which allows you to do multiple requests
35+ but it does not allow you to compose the return value of one endpoint to be the
36+ input/arguments of another.
37+
38+ - [ GraphQL] ( https://graphql.org/ ) is very cool but also introduces a new languages and the
39+ tooling that is required to wield it.
40+
41+ What SendScript attempts is to allow for very expressive queries and mutations to be performed
42+ that read and write like ordinary JS. That means that the queries and complete programs
43+ that are sent to the server from a client can also just run on the server as is. The only
44+ limitation being the serialization which by default is limited by JSON and could be extended by
45+ using more advanced (de)serialization libraries.
46+
47+ SendScript produces an intermediate JSON representation of the program. Let's see what that looks like.
48+
49+ ``` js
50+ import stringify from ' sendscript/stringify.mjs'
51+ import module from ' sendscript/module.mjs'
52+
53+ const { add } = module ([' add' ])
54+
55+ console .log (stringify (add (1 ,2 )))
56+ ```
57+ ``` json
58+ [" call" ,[" ref" ," add" ],[1 ,2 ]]
59+ ```
60+
61+ We can then parse that JSON and it will evaluate down to a value.
62+
63+ ``` js
64+ import Parse from ' sendscript/parse.mjs'
65+
66+ const module = {
67+ add (a , b ) {
68+ return a + b
69+ }
70+ }
71+
72+ const parse = Parse (module )
73+
74+ const program = ' ["call",["ref","add"],[1,2]]'
75+
76+ console .log (parse (program))
77+ ```
78+ ``` json
79+ 3
80+ ```
81+
82+ SendScript does more than a simple function call. It supports function
83+ composition and even await.
84+
85+ This package is nothing more than the absolute core of sendscript. It
86+ includes:
87+
88+ - The ` module ` function to create stubs to write the programs.
89+ - ` stringify ` which takes the program and returns a JSON string.
90+ - ` parse ` which takes the ` stringify ` JSON string and a real module and returns the result.
91+
92+ The naming could use more love and there are many things to solve either in the core or around it.
93+ Things like supporting more complex (de)serializers, errors and maybe mixing client functions with
94+ sendscript programs. Contact me if I have piqued your interest.
95+
96+ ---
97+
2898SendScript leaves it up to you to choose HTTP, web-sockets or any other
2999method of communication between servers and clients that best fits your
30100needs.
@@ -33,17 +103,6 @@ needs.
33103
34104For this example we'll use [ socket.io] [ socket.io ] .
35105
36- ``` bash
37- set -e
38-
39- npm link
40- cd ./example
41- npm ci
42- npm link sendscript
43- ```
44-
45- > We use the ` --no-save ` option because it's only for demonstration purposes.
46-
47106### Module
48107
49108We write a simple module.
@@ -234,7 +293,7 @@ npm install --no-save \
234293 typedoc \
235294 typedoc-plugin-markdown
236295
237- typedoc --plugin typedoc-plugin-markdown --out ./example/typescript/docs ./example/typescript/math.ts
296+ npx typedoc --plugin typedoc-plugin-markdown --out ./example/typescript/docs ./example/typescript/math.ts
238297```
239298
240299You can see the docs [ here] ( ./example/typescript/docs/globals.md )
@@ -254,11 +313,11 @@ npm t -- report text-summary
254313```
255314```
256315
257- > sendscript@1.0.5 test
316+ > sendscript@1.0.6 test
258317> tap -R silent
259318
260319
261- > sendscript@1.0.5 test
320+ > sendscript@1.0.6 test
262321> tap report text-summary
263322
264323
0 commit comments