-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.mz
More file actions
576 lines (409 loc) · 15.5 KB
/
Copy pathREADME.mz
File metadata and controls
576 lines (409 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# SendScript
Serialize and execute composable JavaScript function calls with JSON.
[](https://www.npmjs.com/package/sendscript)
[](#tests)
[](https://standardjs.com)
[](./LICENSE.txt)
[](https://www.islamic-relief.org.uk/giving/appeals/palestine/)
## Features
- **Composable function calls** - Combine multiple functions into a single
request
- **Write ordinary JavaScript** - Programs serialize to JSON and execute as
written
- **Type-safe** - Works great with TypeScript for client-side type checking
- **Zero dependencies** - Lightweight core library
- **Async/await support** - Seamless async operations within a single payload
- **Custom serializers** - Support for Date, Map, Set, BigInt, and more
- **Transport agnostic** - Use HTTP, WebSockets, or any other communication
method
<!-- toc -->
## Installation
Install SendScript from npm:
```bash
npm install sendscript
```
## Quick Start
Here's the simplest example to get started with SendScript:
```js
import Stringify from 'sendscript/stringify.mjs'
import Parse from 'sendscript/parse.mjs'
import references from 'sendscript/references.mjs'
// Define your functions
const functions = {
greet: (name) => `Hello, ${name}!`,
}
// Create client-side stubs
const { greet } = references(['greet'])
// Serialize a program on the client
const stringify = Stringify()
const program = stringify(greet('World'))
// Parse and execute on the server
const parse = Parse(['greet'], functions)
const result = parse(program) // "Hello, World!"
```
This demonstrates the core concept: write a program on the client, serialize it
to JSON, send it to the server, and execute it exactly as written.
## Introduction
There has been interest in improving APIs by allowing aggregations in a single
request. Examples include
- [JSON-RPC](https://json-rpc.dev/) which allows you to do multiple requests but
it does not allow you to compose the return value of one endpoint to be the
input/arguments of another.
- [GraphQL](https://graphql.org/) is very cool but also introduces a new
language and the tooling that is required to wield it.
## Why SendScript?
Unlike JSON-RPC (which doesn't support function composition) or GraphQL (which
requires learning a new language), SendScript lets you write ordinary
JavaScript/TypeScript that gets serialized and executed on the server exactly as
written.
## How It Works
What SendScript attempts is to allow for very expressive queries and mutations
to be performed that read and write like ordinary JS. That means that the
queries and complete programs that are sent to the server from a client can also
just run on the server as is. The only limitation being the serialization which
by default is limited by JSON and could be extended by using more advanced
(de)serialization libraries.
SendScript produces an intermediate JSON representation of the program. Let's
see what that looks like.
```js|json node --input-type=module | tee /tmp/sendscript.json
import Stringify from 'sendscript/stringify.mjs'
import references from 'sendscript/references.mjs'
const { add } = references(['add'])
const stringify = Stringify()
console.log(stringify(add(1,2)))
```
We can then parse that JSON and it will evaluate down to a value.
```js|json node --input-type=module
import Parse from 'sendscript/parse.mjs'
const module = {
add(a, b) {
return a + b
}
}
const parse = Parse(['add'], module)
const program = '["call",["ref","add"],[1,2]]'
console.log(parse(program))
```
SendScript does more than a simple function call. It supports function
composition and even await.
This package is nothing more than the absolute core of sendscript. It includes:
- The `references` function to create stubs to write the programs.
- `stringify` which takes the program and returns a JSON string.
- `parse` which takes the `stringify` JSON string and a real module and returns
the result.
> **Note:** SendScript works in Node.js and browsers. The core library is
> framework-agnostic and transport-agnostic — use HTTP, WebSockets, or any other
> communication method that suits your needs.
Future enhancements may include support for more complex (de)serializers,
improved error handling, and deeper integration of client functions with
SendScript programs. Contributions and feedback are welcome.
## Socket example
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.
For this example we'll use [socket.io][socket.io].
### Module
We write a simple module.
```js cat - > ./example/math.mjs
// ./example/math.mjs
export const add = (a, b) => a + b
export const square = (a) => a * a
```
### Server
Here's a socket.io server that runs SendScript programs.
```js cat - > ./example/server.socket.io.mjs
// ./example/server.socket.io.mjs
import { Server } from 'socket.io'
import Parse from 'sendscript/parse.mjs'
import * as math from './math.mjs'
const schema = Object.keys(math)
const parse = Parse(schema, 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.
```js cat - > ./example/client.socket.io.mjs
// ./example/client.socket.io.mjs
import socketClient from 'socket.io-client'
import Stringify from 'sendscript/stringify.mjs'
import references from 'sendscript/references.mjs'
import assert from 'node:assert'
const { add, square } = references(['add', 'square'])
const stringify = Stringify()
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)
})
})
}
// 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.
```bash bash
set -e
# Run the server
node ./example/server.socket.io.mjs&
# Run the client example
node ./example/client.socket.io.mjs
pkill sendscript
```
## Repl
Sendscript ships with a bare-bones (no-dependencies) node-repl script. One can
run it by simply typing `sendscript` in their console.
> Use the `DEBUG='*'` to enable all logs or `DEBUG='sendscript:*'` for printing
> only sendscript logs.
## Promises
### .then / .catch
Supported since `v2.3`.
```js
const getOrCreatePost = send(createPost(title).catch(createPost(title)))
```
You will likely need to define better helpers that makes it safer to handle
rejections and work with promises. It is however sensible to have this basic
behavior for the sendscript DSL and parser.
### await
By default, `await api.someMethod(...)` still creates a SendScript await stub
and keeps the result serializable with `stringify()`. It does not automatically
send anything over the network.
```js
const api = references(['add'])
const program = api.add(1, 2)
Stringify()(program)
// => "[\"call\",[\"ref\",\"add\"],[1,2]]"
```
If you want native `await` to cross the transport boundary, pass an `onAwait`
handler when creating the references:
```js
import Stringify from 'sendscript/stringify.mjs'
import references from 'sendscript/references.mjs'
const stringify = Stringify()
const api = references(['add'], (program) => {
return fetch('/api', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: stringify(program),
}).then((response) => response.json())
})
const result = await api.add(1, 2)
```
The callback receives the generated SendScript program, and you decide how it is
transported or executed.
## TypeScript
Using SendScript with TypeScript enables **type-safe client-side code**. Your
client can have full IDE autocomplete and compile-time type checking when
calling server functions.
### Server-Side Module
Define your API as a TypeScript module on the server:
```bash|ts bash
cat ./example/typescript/math.ts
```
### Client-Side Type Stub
Create a client-side file that mirrors your server types using the `as typeof`
casting pattern:
```bash|ts bash
cat ./example/typescript/math.client.ts
```
The `as typeof mathTypes` type assertion gives your client-side references the
exact same types as your server module. This means:
- Full IDE autocomplete for function names and parameters
- Compile-time type checking - catch errors before runtime
- Your client code looks identical to regular JavaScript calls
### Using Typed References
Now on your client, you have complete type safety:
```bash|ts bash
cat ./example/typescript/client.ts
```
TypeScript knows the exact parameter types and return types for every function
call.
### Generating API Documentation
You can use [typedoc][typedoc] to automatically generate documentation from your
TypeScript types:
```bash bash
npx typedoc --plugin typedoc-plugin-markdown --out ./docs ./example/typescript/math.ts
```
This generates markdown docs that can be shared with API consumers. See the
[generated docs](./example/typescript/docs/globals.md).
> [!IMPORTANT] **Type vs. Runtime Values**
>
> Type casting on the client side improves the development experience, but
> remember:
>
> - The actual serialized JSON may differ from the static types
> - Runtime values depend on serialization/deserialization
> - Always validate user input on the server using schema validation (e.g., Zod)
> - Use the types as a contract, not a guarantee
## Schema and Nested Modules
Sendscript allows you to define your API as a **nested object of functions**,
making it easy to organize your DSL into modules and submodules. Each function
is instrumented so that when serialized, it produces a structured reference that
can be safely sent and executed elsewhere.
### Defining a Nested Module
You can define a schema as **an array of function names**
```js
const schema = [
'help',
'version',
['math', [
'add',
'sub'
]],
['vector', [
'add',
'multiply'
]],
['utils', [
'identity',
'always'
]],
})
```
Functions are referenced via their **path in the module tree**:
```js
const { math, vector } = references(schema)
math.add(1, vector.length(vector.multiply([1, 2], 3)))
```
## Validation (using Zod)
SendScript focuses on program serialization and execution. For runtime input
validation, you can use [Zod](https://zod.dev).
### Validating structured input
```js
const userSchema = z.object({
id: z.string().uuid(),
name: z.string(),
roles: z.array(z.string()),
})
export function createUser(user) {
userSchema.parse(user)
return { success: true }
}
```
**Benefits**:
- Ensures arguments match expected types and shapes.
- Throws structured errors that can be propagated to clients.
- Works with TypeScript for automatic type inference.
## Leaf Serializer
By default, SendScript uses JSON for serialization, which limits support to
primitives and plain objects/arrays. To support richer JavaScript types like
`Date`, `RegExp`, `BigInt`, `Map`, `Set`, and `undefined`, you can provide
custom serialization functions.
The `stringify` function accepts an optional `leafSerializer` parameter, and
`parse` accepts an optional `leafDeserializer` parameter. These functions
control how non-SendScript values (leaves) are encoded and decoded.
### Example with superjson
Here's how to use [superjson](https://github.com/blitz-js/superjson) to support
extended types:
```js
import SuperJSON from 'superjson'
import Stringify from 'sendscript/stringify.mjs'
import references from 'sendscript/references.mjs'
import Parse from 'sendscript/parse.mjs'
const leafSerializer = (value) => {
if (value === undefined) return JSON.stringify({ __undefined__: true })
return JSON.stringify(SuperJSON.serialize(value))
}
const leafDeserializer = (text) => {
const parsed = JSON.parse(text)
if (parsed && parsed.__undefined__ === true) return undefined
return SuperJSON.deserialize(parsed)
}
const schema = ['processData']
const { processData } = references(schema)
const stringify = Stringify(leafSerializer)
// Program with Date, RegExp, and other types
const program = {
createdAt: new Date('2020-01-01T00:00:00.000Z'),
pattern: /foo/gi,
count: BigInt('9007199254740992'),
items: new Set([1, 2, 3]),
mapping: new Map([
['a', 1],
['b', 2],
]),
}
// Serialize with custom leaf serializer
const json = stringify(processData(program))
// The environment
const env = {
processData: (data) => ({
success: true,
received: data,
}),
}
// Parse with custom leaf deserializer
const parse = Parse(schema, env, leafDeserializer)
const result = parse(json)
```
The leaf wrapper format is `['leaf', serializedPayload]`, making it unambiguous
and safe from colliding with SendScript operators.
## Limitations
The stubs the `references` function returns are limited in the way they can be
used. This has to do with the fact that they are to be serialized.
### Chaining APIs
Currently you cannot do chaining APIs. `lib().doThis().doThat()`. It likely is
possible to implement but will require more extensive schemas and obviously more
code to create the AST.
### Callbacks
SendScript supports basic callback-style usage, such as passing a function as an
argument or using a callback to flip arguments into the generated program.
```js
const api = references(['map', 'add'])
const result = api.map((value) => api.add(value, 1))([1, 2, 3])
```
This is meant for composing SendScript programs, not for executing arbitrary
client-side logic at runtime.
> [!WARNING] Mixing client and server functions can be confusing, because the
> callback may be used to build a program rather than execute immediately. Keep
> callback logic simple and deterministic.
### Error handling
Sendscript does not have builtin tools to handle errors. You can write your own
utilities for that or use things like [Ramda's tryCatch][tryCatch]. This will
likely never be supported as JS try catch does not have a return value. It might
make sense to wrap everything that can throw in promises which is easy to do
with async functions.
## Tests
Tests with 100% code coverage.
```bash bash
npm t -- -R silent
npm t -- report text-summary
```
## Formatting
Standard because no config.
```bash bash
npx standard
```
## Changelog
The [changelog][changelog] is generated using the useful
[auto-changelog][auto-changelog] project.
```bash bash > /dev/null
npx auto-changelog -p
```
## License
See the [LICENSE.txt][license] file for details.
## Issues
See [issues][issues] for roadmap and known bugs.
[license]: ./LICENSE.txt
[socket.io]: https://socket.io/
[changelog]: ./CHANGELOG.md
[auto-changelog]: https://www.npmjs.com/package/auto-changelog
[typedoc]: https://github.com/TypeStrong/typedoc
[issues]: https://github.com/bas080/sendscript/issues