Skip to content

Commit 00cd19b

Browse files
committed
2.0.0
1 parent ffa4fea commit 00cd19b

8 files changed

Lines changed: 77 additions & 80 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ All notable changes to this project will be documented in this file. Dates are d
44

55
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
66

7+
#### [v2.0.0](https://github.com/bas080/sendscript/compare/v1.1.0...v2.0.0)
8+
9+
- Rewrite to have parse take schema to prevent accessing refs [`df16f85`](https://github.com/bas080/sendscript/commit/df16f85c79299aa8bcd3e44b6cc127bd7198eace)
10+
- Run tests before release and publish on release [`10442d6`](https://github.com/bas080/sendscript/commit/10442d6766a098926ee98daee08dba23eea0fb14)
11+
- Create release on tag with github workflows [`4ec222b`](https://github.com/bas080/sendscript/commit/4ec222bf51fc1d3cce9fc54fdd1e9639b5d259cd)
12+
713
#### [v1.1.0](https://github.com/bas080/sendscript/compare/v1.0.6...v1.1.0)
814

15+
> 7 April 2026
16+
917
- Support custom serialization of leaf nodes [`d56f1b7`](https://github.com/bas080/sendscript/commit/d56f1b750e2d7faf72d69f9ba8e9bb62ef5ebce7)
1018
- Support nested modules [`88e69e5`](https://github.com/bas080/sendscript/commit/88e69e5c05ddb85aac1797a020e92519c986e89f)
1119
- Document new nesting and leaf serialization feature [`b3ee9f6`](https://github.com/bas080/sendscript/commit/b3ee9f693dc6137b01c9f0072aa2dd1e940f07c9)

README.md

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ using more advanced (de)serialization libraries.
5353
SendScript produces an intermediate JSON representation of the program. Let's see what that looks like.
5454

5555
```js
56-
import stringify from 'sendscript/stringify.mjs'
57-
import module from 'sendscript/module.mjs'
56+
import Stringify from 'sendscript/stringify.mjs'
57+
import references from 'sendscript/references.mjs'
5858

59-
const { add } = module(['add'])
59+
const { add } = references(['add'])
60+
const stringify = Stringify()
6061

6162
console.log(stringify(add(1,2)))
6263
```
@@ -75,7 +76,7 @@ const module = {
7576
}
7677
}
7778

78-
const parse = Parse(module)
79+
const parse = Parse(['add'], module)
7980

8081
const program = '["call",["ref","add"],[1,2]]'
8182

@@ -131,7 +132,8 @@ import { Server } from 'socket.io'
131132
import Parse from 'sendscript/parse.mjs'
132133
import * as math from './math.mjs'
133134

134-
const parse = Parse(math)
135+
const schema = Object.keys(math)
136+
const parse = Parse(schema, math)
135137
const server = new Server()
136138
const port = process.env.PORT || 3000
137139

@@ -158,11 +160,13 @@ Now for a client that sends a program to the server.
158160
// ./example/client.socket.io.mjs
159161

160162
import socketClient from 'socket.io-client'
161-
import stringify from 'sendscript/stringify.mjs'
162-
import module from 'sendscript/module.mjs'
163-
import * as math from './math.mjs'
163+
import Stringify from 'sendscript/stringify.mjs'
164+
import references from 'sendscript/references.mjs'
164165
import assert from 'node:assert'
165166

167+
const { add, square } = references(['add', 'square'])
168+
const stringify = Stringify()
169+
166170
const port = process.env.PORT || 3000
167171
const client = socketClient(`http://localhost:${port}`)
168172

@@ -176,8 +180,6 @@ const send = program => {
176180
})
177181
}
178182

179-
const { add, square } = module(math)
180-
181183
// The program to be sent over the wire
182184
const program = square(add(1, add(add(2, 3), 4)))
183185

@@ -253,52 +255,31 @@ export const add = (a: number, b: number) => a + b
253255
export const square = (a: number) => a * a
254256
```
255257

256-
We want to use this module on the client. We create a client version of that module and coerce the types to match those of the server.
257-
258-
```bash
259-
cat ./example/typescript/math.client.ts
260-
```
261-
```ts
262-
import module from 'sendscript/module.mjs'
263-
import type * as mathTypes from './math.ts'
264-
265-
const math = module([
266-
'add',
267-
'square'
268-
]) as typeof mathTypes
269-
270-
export default math
271-
```
272-
273-
We now use the client version of this module.
258+
We can then coerce the types of the instrumented stubs.
274259

275260
```bash
276261
cat ./example/typescript/client.ts
277262
```
278263
```ts
279-
import stringify from 'sendscript/stringify.mjs'
264+
import math from './math.client.ts'
265+
import Stringify from 'sendscript/stringify.mjs'
266+
267+
const stringify = Stringify()
280268

281-
async function send<T>(program: T): Promise<T>{
269+
// The return type of this function matches the type passed as the return of the program.
270+
async function send<T>(program: T): Promise<T> {
282271
return (await fetch('/api', {
283272
method: 'POST',
284273
body: stringify(program)
285274
})).json()
286275
}
287276

288-
import math from './math.client.ts'
289-
290-
const { add, square } = math
291-
292277
send(square(add(1, 2)))
293278
```
294279

295280
We'll also generate the docs for this module.
296281

297282
```bash
298-
npm install --no-save \
299-
typedoc \
300-
typedoc-plugin-markdown
301-
302283
npx typedoc --plugin typedoc-plugin-markdown --out ./example/typescript/docs ./example/typescript/math.ts
303284
```
304285

@@ -316,32 +297,33 @@ Sendscript allows you to define your API as a **nested object of functions**, ma
316297

317298
### Defining a Nested Module
318299

319-
You can define a schema as either:
320-
321-
1. **An object with nested objects** – submodules.
322-
2. **An array of function names** – automatically instrumented.
300+
You can define a schema as **an array of function names**
323301

324302
```js
325-
import module from 'sendscript/module.mjs'
326-
327-
const myModule = module({
328-
math: ['add', 'sub'],
329-
330-
// Use an object with keys and true value
331-
vector: {
332-
add: true,
333-
multiply: true
334-
},
335303

336-
// or use an array.
337-
utils: ['identity', 'always'],
304+
const schema = [
305+
'help',
306+
'version',
307+
308+
['math', [
309+
'add',
310+
'sub'
311+
]],
312+
['vector', [
313+
'add',
314+
'multiply'
315+
]],
316+
['utils', [
317+
'identity',
318+
'always'
319+
]],
338320
})
339321
```
340322

341323
Functions are referenced via their **path in the module tree**:
342324

343325
```js
344-
const { math, vector } = myModule
326+
const { math, vector } = references(schema)
345327

346328
math.add(
347329
1,
@@ -389,9 +371,9 @@ Here's how to use [superjson](https://github.com/blitz-js/superjson) to support
389371

390372
```js
391373
import SuperJSON from 'superjson'
392-
import stringify from 'sendscript/stringify.mjs'
374+
import Stringify from 'sendscript/stringify.mjs'
375+
import references from 'sendscript/references.mjs'
393376
import Parse from 'sendscript/parse.mjs'
394-
import module from 'sendscript/module.mjs'
395377

396378
const leafSerializer = (value) => {
397379
if (value === undefined) return JSON.stringify({ __undefined__: true })
@@ -404,7 +386,10 @@ const leafDeserializer = (text) => {
404386
return SuperJSON.deserialize(parsed)
405387
}
406388

407-
const { processData } = module(['processData'])
389+
const schema = ['processData']
390+
391+
const { processData } = references(schema)
392+
const stringify = Stringify(leafSerializer)
408393

409394
// Program with Date, RegExp, and other types
410395
const program = {
@@ -416,17 +401,20 @@ const program = {
416401
}
417402

418403
// Serialize with custom leaf serializer
419-
const json = stringify(processData(program), leafSerializer)
404+
const json = stringify(processData(program))
420405

421-
// Parse with custom leaf deserializer
422-
const parse = Parse({
406+
// The environment
407+
const env = {
423408
processData: (data) => ({
424409
success: true,
425410
received: data
426411
})
427-
})
412+
}
413+
414+
// Parse with custom leaf deserializer
415+
const parse = Parse(schema, env, leadDeserializer)
428416

429-
const result = parse(json, leafDeserializer)
417+
const result = parse(json)
430418
```
431419

432420
The leaf wrapper format is `['leaf', serializedPayload]`, making it unambiguous and safe from colliding with SendScript operators.
@@ -441,19 +429,19 @@ npm t -- report text-summary
441429
```
442430
```
443431
444-
> sendscript@1.1.0 test
432+
> sendscript@2.0.0 test
445433
> tap -R silent
446434
447435
448-
> sendscript@1.1.0 test
436+
> sendscript@2.0.0 test
449437
> tap report text-summary
450438
451439
452440
=============================== Coverage summary ===============================
453-
Statements : 100% ( 328/328 )
454-
Branches : 100% ( 138/138 )
455-
Functions : 100% ( 23/23 )
456-
Lines : 100% ( 328/328 )
441+
Statements : 100% ( 348/348 )
442+
Branches : 100% ( 145/145 )
443+
Functions : 100% ( 24/24 )
444+
Lines : 100% ( 348/348 )
457445
================================================================================
458446
```
459447

example/client.socket.io.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// ./example/client.socket.io.mjs
22

33
import socketClient from 'socket.io-client'
4-
import stringify from 'sendscript/stringify.mjs'
5-
import module from 'sendscript/module.mjs'
6-
import * as math from './math.mjs'
4+
import Stringify from 'sendscript/stringify.mjs'
5+
import references from 'sendscript/references.mjs'
76
import assert from 'node:assert'
87

8+
const { add, square } = references(['add', 'square'])
9+
const stringify = Stringify()
10+
911
const port = process.env.PORT || 3000
1012
const client = socketClient(`http://localhost:${port}`)
1113

@@ -19,8 +21,6 @@ const send = program => {
1921
})
2022
}
2123

22-
const { add, square } = module(math)
23-
2424
// The program to be sent over the wire
2525
const program = square(add(1, add(add(2, 3), 4)))
2626

example/server.socket.io.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Server } from 'socket.io'
44
import Parse from 'sendscript/parse.mjs'
55
import * as math from './math.mjs'
66

7-
const parse = Parse(math)
7+
const schema = Object.keys(math)
8+
const parse = Parse(schema, math)
89
const server = new Server()
910
const port = process.env.PORT || 3000
1011

example/typescript/docs/functions/add.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **add**(`a`, `b`): `number`
1010
11-
Defined in: [math.ts:1](https://github.com/bas080/sendscript/blob/68bfab27a561a994381cdcf5a7cf367e29a2d07d/example/typescript/math.ts#L1)
11+
Defined in: [math.ts:1](https://github.com/bas080/sendscript/blob/ffa4feaeee11b6652b89c5412b2af937139edac5/example/typescript/math.ts#L1)
1212

1313
## Parameters
1414

example/typescript/docs/functions/square.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **square**(`a`): `number`
1010
11-
Defined in: [math.ts:2](https://github.com/bas080/sendscript/blob/68bfab27a561a994381cdcf5a7cf367e29a2d07d/example/typescript/math.ts#L2)
11+
Defined in: [math.ts:2](https://github.com/bas080/sendscript/blob/ffa4feaeee11b6652b89c5412b2af937139edac5/example/typescript/math.ts#L2)
1212

1313
## Parameters
1414

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sendscript",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "Blur the line between server and client code.",
55
"module": true,
66
"main": "index.mjs",

0 commit comments

Comments
 (0)