itty-sockets 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -29
- package/connect.js +1 -1
- package/connect.mjs +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -18,32 +18,19 @@
|
|
|
18
18
|
|
|
19
19
|
---
|
|
20
20
|
|
|
21
|
-
###
|
|
21
|
+
### Say goodbye to WebSocket boilerplate.
|
|
22
|
+
|
|
23
|
+
Your own wrapper is bigger, I promise.
|
|
24
|
+
|
|
25
|
+
Or [optionally] go a step further and use the integrated [itty.ws](https://itty.ws) connection to send messages (zero-config, zero-tracking, 100% free).
|
|
22
26
|
|
|
23
27
|
## Features ✨
|
|
24
|
-
1. **DX perks** - JSON-in/out, queued messages, easy-reconnect, chainable everything
|
|
25
|
-
1. **Works with any JSON-based WebSocket server**
|
|
26
|
-
1. **Powerful
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
-
- Only specific types
|
|
32
|
-
```ts
|
|
33
|
-
// matches any data with { type: 'chat' }
|
|
34
|
-
.on('chat', ({ text }) => console.log(text))
|
|
35
|
-
```
|
|
36
|
-
- or fully custom filters
|
|
37
|
-
```ts
|
|
38
|
-
// matches only messages where data.value > 20
|
|
39
|
-
.on(
|
|
40
|
-
({ value }) => value > 20), // filter function
|
|
41
|
-
({ value }) => console.log(value),
|
|
42
|
-
)
|
|
43
|
-
```
|
|
44
|
-
1. **Type-safe message handling**
|
|
45
|
-
1. **Tiny footprint** - 512 bytes, all-in.
|
|
46
|
-
1. **Optional usage with free/public/zero-config [itty.ws](https://itty.ws) service**
|
|
28
|
+
1. **DX perks** - JSON-in/out, queued messages, easy-reconnect, chainable everything
|
|
29
|
+
1. **Works with *any* JSON-based WebSocket server** - it's just a raw WebSocket wrapper, after all
|
|
30
|
+
1. **Powerful routing** - easily handle your own message formats
|
|
31
|
+
1. **Type-safe message handling** - so your app knows what to expect
|
|
32
|
+
1. **No socket server needed** - Use [itty.ws](https://itty.ws) public channels to get started even faster
|
|
33
|
+
1. **Tiny** - under 500 bytes total
|
|
47
34
|
|
|
48
35
|
## Basic Example
|
|
49
36
|
```ts
|
|
@@ -85,7 +72,7 @@ import { connect } from 'itty-sockets'
|
|
|
85
72
|
**Option 2: Just copy this snippet:**
|
|
86
73
|
<!-- BEGIN SNIPPET -->
|
|
87
74
|
```ts
|
|
88
|
-
let connect=(e,s={})=>{let
|
|
75
|
+
let connect=(e,s={})=>{let a,t,n=[],p={},o=()=>(a||(a=new WebSocket((/^wss?:/.test(e)?e:"wss://itty.ws/c/"+e)+"?"+new URLSearchParams(s)),a.onmessage=(e,s=JSON.parse(e.data),a=s?.message,t={...null==a?.[0]&&a,...s})=>[t.type,s.type?0:"message","*"].map(e=>p[e]?.map(e=>e(t))),a.onopen=()=>(n.splice(0).map(e=>a.send(e)),p.open?.map(e=>e(t)),t&&a?.close()),a.onclose=()=>(t=a=null,p.close?.map(e=>e(t)))),l),l={open:o,send:(e,s)=>(e=(s?`${s}`:"")+JSON.stringify(e),1&a?.readyState?a.send(e):n.push(e),o()),on:(e,s)=>((p[e?.[0]?e:"*"]??=[]).push(e?.[0]?s:a=>e?.(a)&&s(a)),o()),remove:(e,s)=>(p[e]=p[e]?.filter(e=>e!=s),l),close:()=>(1&a?.readyState?a.close():t=1,l),push:(e,s)=>(t=1,l.send(e,s))};return l};
|
|
89
76
|
```
|
|
90
77
|
<!-- END SNIPPET -->
|
|
91
78
|
*Note: This will lose TypeScript support.*
|
|
@@ -101,13 +88,14 @@ connect('wss://example.com')
|
|
|
101
88
|
.on('message', console.log)
|
|
102
89
|
|
|
103
90
|
// and just { type: 'chat' }
|
|
104
|
-
.on('chat',
|
|
91
|
+
.on('chat',
|
|
105
92
|
({ user, text }) => console.log(`${user} says: ${text}`)
|
|
106
93
|
)
|
|
107
94
|
```
|
|
108
95
|
|
|
109
96
|
Now let's assume the following 2 messages are sent:
|
|
110
97
|
```json
|
|
98
|
+
// message 1
|
|
111
99
|
{
|
|
112
100
|
"type": "chat",
|
|
113
101
|
"user": "Kevin",
|
|
@@ -116,6 +104,7 @@ Now let's assume the following 2 messages are sent:
|
|
|
116
104
|
```
|
|
117
105
|
|
|
118
106
|
```json
|
|
107
|
+
// message 2
|
|
119
108
|
{
|
|
120
109
|
"date": 1754659171196,
|
|
121
110
|
"items": [1, 2, 3],
|
|
@@ -123,12 +112,13 @@ Now let's assume the following 2 messages are sent:
|
|
|
123
112
|
```
|
|
124
113
|
|
|
125
114
|
This will output the following to the console:
|
|
126
|
-
```
|
|
115
|
+
```js
|
|
116
|
+
// message 1
|
|
127
117
|
{ type: "chat", user: "Kevin", text: "Hey!" }
|
|
118
|
+
"Kevin says: Hey!"
|
|
128
119
|
|
|
120
|
+
// message 2
|
|
129
121
|
{ date: 1754659171196, items: [1, 2, 3] }
|
|
130
|
-
|
|
131
|
-
"Kevin says: Hey!"
|
|
132
122
|
```
|
|
133
123
|
|
|
134
124
|
## Example 3 - Reconnection
|
package/connect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let
|
|
1
|
+
"use strict";exports.connect=(e,s={})=>{let t,n,a=[],o={},p=()=>(t||(t=new WebSocket((/^wss?:/.test(e)?e:"wss://itty.ws/c/"+e)+"?"+new URLSearchParams(s)),t.onmessage=(e,s=JSON.parse(e.data),t=s?.message,n={...null==t?.[0]&&t,...s})=>[n.type,s.type?0:"message","*"].map(e=>o[e]?.map(e=>e(n))),t.onopen=()=>(a.splice(0).map(e=>t.send(e)),o.open?.map(e=>e(n)),n&&t?.close()),t.onclose=()=>(n=t=null,o.close?.map(e=>e(n)))),c),c={open:p,send:(e,s)=>(e=(s?`${s}`:"")+JSON.stringify(e),1&t?.readyState?t.send(e):a.push(e),p()),on:(e,s)=>((o[e?.[0]?e:"*"]??=[]).push(e?.[0]?s:t=>e?.(t)&&s(t)),p()),remove:(e,s)=>(o[e]=o[e]?.filter(e=>e!=s),c),close:()=>(1&t?.readyState?t.close():n=1,c),push:(e,s)=>(n=1,c.send(e,s))};return c};
|
package/connect.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let e=(e,s={})=>{let
|
|
1
|
+
let e=(e,s={})=>{let a,t,n=[],p={},o=()=>(a||(a=new WebSocket((/^wss?:/.test(e)?e:"wss://itty.ws/c/"+e)+"?"+new URLSearchParams(s)),a.onmessage=(e,s=JSON.parse(e.data),a=s?.message,t={...null==a?.[0]&&a,...s})=>[t.type,s.type?0:"message","*"].map(e=>p[e]?.map(e=>e(t))),a.onopen=()=>(n.splice(0).map(e=>a.send(e)),p.open?.map(e=>e(t)),t&&a?.close()),a.onclose=()=>(t=a=null,p.close?.map(e=>e(t)))),l),l={open:o,send:(e,s)=>(e=(s?`${s}`:"")+JSON.stringify(e),1&a?.readyState?a.send(e):n.push(e),o()),on:(e,s)=>((p[e?.[0]?e:"*"]??=[]).push(e?.[0]?s:a=>e?.(a)&&s(a)),o()),remove:(e,s)=>(p[e]=p[e]?.filter(e=>e!=s),l),close:()=>(1&a?.readyState?a.close():t=1,l),push:(e,s)=>(t=1,l.send(e,s))};return l};export{e as connect};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-sockets",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "WebSockets : simplified and minified.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"test": "bun test --coverage",
|
|
16
16
|
"lint": "itty lint",
|
|
17
17
|
"build": "itty lint && itty build --snippet=connect --hybrid",
|
|
18
|
-
"release": "itty release --prepare --tag --push",
|
|
19
|
-
"release:next": "itty release --prepare --tag --push --type=next"
|
|
18
|
+
"release": "itty release --prepare --tag --push --otp",
|
|
19
|
+
"release:next": "itty release --prepare --tag --push --type=next --otp"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
22
|
"websockets",
|
|
@@ -34,6 +34,6 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/bun": "^1.2.3",
|
|
37
|
-
"itty-packager": "^1.
|
|
37
|
+
"itty-packager": "^1.7.0"
|
|
38
38
|
}
|
|
39
39
|
}
|