itty-sockets 0.5.2 → 0.5.4
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 +38 -35
- package/connect.cjs +1 -0
- package/connect.d.ts +9 -4
- package/connect.mjs +1 -1
- package/package.json +12 -35
- package/connect.js +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/index.mjs +0 -1
package/README.md
CHANGED
|
@@ -16,43 +16,46 @@
|
|
|
16
16
|
|
|
17
17
|
---
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
# WebSockets : simplified and minified.
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
Zero-config. Pick a channel and go.
|
|
22
|
+
```ts
|
|
23
|
+
// CLIENT 1 (listens for messages)
|
|
24
|
+
connect('unique-channel-name')
|
|
25
|
+
// listen for all messages
|
|
26
|
+
.on('message', e => console.log(e.message))
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
// or just our custom messages
|
|
29
|
+
.on('my-chat-message', ({ user, text }) => console.log(user, 'says:', text))
|
|
30
|
+
```
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
```ts
|
|
33
|
+
// CLIENT 2 (sends messages)
|
|
34
|
+
const channel = connect('unique-channel-name')
|
|
35
|
+
.send({ foo: 'bar' })
|
|
36
|
+
.send({ type: 'my-chat-message', user: 'Halsey', text: 'Meow!' })
|
|
29
37
|
|
|
30
|
-
1. One or more parties connect to a channel (by name).
|
|
31
|
-
2. They send/receive messages (this can be anything) in the channel.
|
|
32
|
-
3. That's it!
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
```
|
|
36
|
-
import { connect } from 'itty-sockets'
|
|
39
|
+
channel.send('what else can this do?')
|
|
40
|
+
```
|
|
37
41
|
|
|
38
|
-
// connect to a channel
|
|
39
|
-
const foo = connect('my-secret-room-name')
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
// we can listen for messages
|
|
43
|
-
.on('message', e => console.log(e.message))
|
|
43
|
+
## Or simply use `connect` as a tiny WebSocket client that brings the following:
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
- JSON parsing/stringifying
|
|
46
|
+
- message queing - sending automatically connects and queue is flushed on open
|
|
47
|
+
- easy reconnection (listeners keep working)
|
|
48
|
+
- custom listeners/filters
|
|
49
|
+
- chainable syntax (it's just handy)
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
```ts
|
|
52
|
+
const ws = connect('wss://somewhere.else')
|
|
53
|
+
.on('message', console.log) // log all messages
|
|
54
|
+
.send({ foo: 'bar' }) // send immediately, no waiting
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
// optional - reconnect every second (no effect if open)
|
|
57
|
+
setInterval(ws.open, 1000)
|
|
58
|
+
```
|
|
56
59
|
|
|
57
60
|
<br />
|
|
58
61
|
|
|
@@ -66,7 +69,7 @@ import { connect } from 'itty-sockets'
|
|
|
66
69
|
...or simply paste this into your environment/console:
|
|
67
70
|
<!-- BEGIN SNIPPET -->
|
|
68
71
|
```ts
|
|
69
|
-
let connect=(e,s={})=>{let
|
|
72
|
+
let connect=(e,s={})=>{let t,a=0,n=[],p=[],o={},l=()=>(t||(t=new WebSocket((/^wss?:/.test(e)?e:"wss://ittysockets.io/c/"+e)+"?"+new URLSearchParams(s)),t.onmessage=(e,s=JSON.parse(e.data),t=s?.message,a={...null==t?.[0]&&t,...s,...s.date&&{date:new Date(s.date)}})=>{o[s?.type??t?.type]?.map(e=>e(a)),s?.type||o.message?.map(e=>e(a)),p.map(([e,s])=>e(a)&&s(a))},t.onopen=()=>(n.splice(0).map(e=>t?.send(e)),o.open?.map(e=>e()),a&&t?.close()),t.onclose=()=>(a=0,t=null,o.close?.map(e=>e()))),c),c=new Proxy(l,{get:(e,s)=>({open:l,close:()=>(1==t?.readyState?t.close():a=1,c),push:(e,s)=>(a=1,c.send(e,s)),send:(e,s)=>(e=JSON.stringify(e),e=s?"@@"+s+"@@"+e:e,1==t?.readyState?(t.send(e),c):(n.push(e),l())),on:(e,s)=>(s&&(e?.[0]?(o[e]??=[]).push(s):p.push([e,s])),l()),remove:(e,s,t=o[e],a=t?.indexOf(s)??-1)=>(~a&&t?.splice(a,1),l())}[s])});return c};
|
|
70
73
|
```
|
|
71
74
|
<!-- END SNIPPET -->
|
|
72
75
|
|
|
@@ -81,17 +84,17 @@ To start, simply connect to a channel based on a unique name (this can be anythi
|
|
|
81
84
|
import { connect } from 'itty-sockets'
|
|
82
85
|
|
|
83
86
|
// basic connection
|
|
84
|
-
const channel = connect('my-
|
|
87
|
+
const channel = connect('my-super-secret-channel')
|
|
85
88
|
|
|
86
89
|
// with options
|
|
87
|
-
const channel = connect('my-
|
|
88
|
-
|
|
89
|
-
announce: true,
|
|
90
|
-
echo: true
|
|
90
|
+
const channel = connect('my-super-secret-channel', {
|
|
91
|
+
alias: 'Kevin', // optional non-unique identifier, visible in messages
|
|
92
|
+
announce: true, // shares your uid/alias with the channel on joining
|
|
93
|
+
echo: true // echos your own messages back to you (for testing)
|
|
91
94
|
})
|
|
92
95
|
|
|
93
|
-
//
|
|
94
|
-
const channel = connect('wss://somewhere.else
|
|
96
|
+
// or any external JSON WebSocket server
|
|
97
|
+
const channel = connect('wss://somewhere.else.entirely')
|
|
95
98
|
```
|
|
96
99
|
|
|
97
100
|
#### Connection Options
|
package/connect.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";exports.connect=(e,s={})=>{let t,a=0,n=[],p=[],o={},c=()=>(t||(t=new WebSocket((/^wss?:/.test(e)?e:"wss://ittysockets.io/c/"+e)+"?"+new URLSearchParams(s)),t.onmessage=(e,s=JSON.parse(e.data),t=s?.message,a={...null==t?.[0]&&t,...s,...s.date&&{date:new Date(s.date)}})=>{o[s?.type??t?.type]?.map(e=>e(a)),s?.type||o.message?.map(e=>e(a)),p.map(([e,s])=>e(a)&&s(a))},t.onopen=()=>(n.splice(0).map(e=>t?.send(e)),o.open?.map(e=>e()),a&&t?.close()),t.onclose=()=>(a=0,t=null,o.close?.map(e=>e()))),l),l=new Proxy(c,{get:(e,s)=>({open:c,close:()=>(1==t?.readyState?t.close():a=1,l),push:(e,s)=>(a=1,l.send(e,s)),send:(e,s)=>(e=JSON.stringify(e),e=s?"@@"+s+"@@"+e:e,1==t?.readyState?(t.send(e),l):(n.push(e),c())),on:(e,s)=>(s&&(e?.[0]?(o[e]??=[]).push(s):p.push([e,s])),c()),remove:(e,s,t=o[e],a=t?.indexOf(s)??-1)=>(~a&&t?.splice(a,1),c())}[s])});return l};
|
package/connect.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ type OptionalUserDetails = {
|
|
|
12
12
|
};
|
|
13
13
|
export type MessageEvent<MessageType = any> = {
|
|
14
14
|
message: MessageType;
|
|
15
|
-
} & Date & UserDetails;
|
|
15
|
+
} & Date & UserDetails & MessageType;
|
|
16
16
|
export type JoinEvent = {
|
|
17
17
|
type: 'join';
|
|
18
18
|
users: number;
|
|
@@ -32,11 +32,16 @@ export type IttySocket = {
|
|
|
32
32
|
connected: boolean;
|
|
33
33
|
send: SendMessage;
|
|
34
34
|
push: SendMessage;
|
|
35
|
-
on<MessageFormat = any>(type: 'message', listener: (event: MessageEvent<MessageFormat>) => any): IttySocket;
|
|
36
35
|
on(type: 'join', listener: (event: JoinEvent) => any): IttySocket;
|
|
37
36
|
on(type: 'leave', listener: (event: LeaveEvent) => any): IttySocket;
|
|
38
37
|
on(type: 'error', listener: (event: ErrorEvent) => any): IttySocket;
|
|
39
|
-
on(type:
|
|
38
|
+
on<MessageFormat = any>(type: 'message', listener: (event: MessageEvent<MessageFormat>) => any): IttySocket;
|
|
39
|
+
on<MessageFormat = any>(type: string, listener: (event: MessageEvent<MessageFormat & {
|
|
40
|
+
type: string;
|
|
41
|
+
}>) => any): IttySocket;
|
|
42
|
+
on<MessageFormat = any>(type: (event?: any) => any, listener: (event: MessageEvent<MessageFormat & {
|
|
43
|
+
type: string;
|
|
44
|
+
}>) => any): IttySocket;
|
|
40
45
|
remove(type: IttySocketEvent, listener: () => any): IttySocket;
|
|
41
46
|
};
|
|
42
47
|
export type IttySocketOptions = {
|
|
@@ -45,5 +50,5 @@ export type IttySocketOptions = {
|
|
|
45
50
|
echo?: true;
|
|
46
51
|
announce?: true;
|
|
47
52
|
};
|
|
48
|
-
export declare
|
|
53
|
+
export declare let connect: (channelId: string, options?: IttySocketOptions) => IttySocket;
|
|
49
54
|
export {};
|
package/connect.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
let e=(e,s={})=>{let t,a=0,n=[],p=[],o={},l=()=>(t||(t=new WebSocket((/^wss?:/.test(e)?e:"wss://ittysockets.io/c/"+e)+"?"+new URLSearchParams(s)),t.onmessage=(e,s=JSON.parse(e.data),t=s?.message,a={...null==t?.[0]&&t,...s,...s.date&&{date:new Date(s.date)}})=>{o[s?.type??t?.type]?.map(e=>e(a)),s?.type||o.message?.map(e=>e(a)),p.map(([e,s])=>e(a)&&s(a))},t.onopen=()=>(n.splice(0).map(e=>t?.send(e)),o.open?.map(e=>e()),a&&t?.close()),t.onclose=()=>(a=0,t=null,o.close?.map(e=>e()))),c),c=new Proxy(l,{get:(e,s)=>({open:l,close:()=>(1==t?.readyState?t.close():a=1,c),push:(e,s)=>(a=1,c.send(e,s)),send:(e,s)=>(e=JSON.stringify(e),e=s?"@@"+s+"@@"+e:e,1==t?.readyState?(t.send(e),c):(n.push(e),l())),on:(e,s)=>(s&&(e?.[0]?(o[e]??=[]).push(s):p.push([e,s])),l()),remove:(e,s,t=o[e],a=t?.indexOf(s)??-1)=>(~a&&t?.splice(a,1),l())}[s])});return c};export{e as connect};
|
package/package.json
CHANGED
|
@@ -1,39 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-sockets",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.5.4",
|
|
4
|
+
"description": "WebSockets : simplified and minified.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
-
"import": "./index.mjs",
|
|
9
|
-
"require": "./index.js",
|
|
10
|
-
"types": "./index.d.ts"
|
|
11
|
-
},
|
|
12
|
-
"./connect": {
|
|
13
8
|
"import": "./connect.mjs",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
9
|
+
"types": "./connect.d.ts",
|
|
10
|
+
"require": "./connect.cjs"
|
|
16
11
|
}
|
|
17
12
|
},
|
|
18
13
|
"scripts": {
|
|
19
|
-
"dev": "bun test --watch",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"build": "rollup -c && bun readme-inject.mjs",
|
|
26
|
-
"release": "release --tag --push --patch --src=dist",
|
|
27
|
-
"release:next": "release --tag --push --type=next --src=dist"
|
|
14
|
+
"dev": "bun test --watch --coverage",
|
|
15
|
+
"test": "bun test --coverage",
|
|
16
|
+
"lint": "itty lint",
|
|
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"
|
|
28
20
|
},
|
|
29
21
|
"keywords": [
|
|
30
22
|
"websockets",
|
|
31
23
|
"realtime",
|
|
32
|
-
"
|
|
33
|
-
"messages",
|
|
34
|
-
"tiny",
|
|
35
|
-
"free",
|
|
36
|
-
"p2p"
|
|
24
|
+
"client"
|
|
37
25
|
],
|
|
38
26
|
"repository": {
|
|
39
27
|
"type": "git",
|
|
@@ -45,18 +33,7 @@
|
|
|
45
33
|
"url": "https://github.com/kwhitley/itty-sockets/issues"
|
|
46
34
|
},
|
|
47
35
|
"devDependencies": {
|
|
48
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
49
|
-
"@rollup/plugin-typescript": "^11.1.6",
|
|
50
36
|
"@types/bun": "^1.2.3",
|
|
51
|
-
"
|
|
52
|
-
"@typescript-eslint/parser": "^8.18.0",
|
|
53
|
-
"eslint": "^9.17.0",
|
|
54
|
-
"globby": "^14.1.0",
|
|
55
|
-
"rimraf": "^6.0.1",
|
|
56
|
-
"rollup": "^4.28.1",
|
|
57
|
-
"rollup-plugin-bundle-size": "^1.0.3",
|
|
58
|
-
"rollup-plugin-copy": "^3.5.0",
|
|
59
|
-
"typescript": "^5.7.2",
|
|
60
|
-
"yarn-release": "^1.10.6"
|
|
37
|
+
"itty-packager": "^1.6.7"
|
|
61
38
|
}
|
|
62
39
|
}
|
package/connect.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let t,o=0,n=[],r={},a=()=>(t||(t=new WebSocket((/^wss?:/.test(e)?e:"wss://ittysockets.io/c/"+e)+"?"+new URLSearchParams(s)),t.onclose=()=>{o=0,t=null;for(let e of r.close??[])e()},t.onopen=()=>{for(;n.length;)t?.send(n.shift());for(let e of r.open??[])e();o&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of r[s.type??"message"]??[])e({...s,date:new Date(s.date)})}),c);const c=new Proxy(a,{get:(e,s)=>({close:()=>(1==t?.readyState?t.close():o=1,c),open:a,send:(e,s)=>(e=JSON.stringify(e),e=s?"@@"+s+"@@"+e:e,1==t?.readyState?(t.send(e),c):(n.push(e),a())),push:(e,s)=>(o=1,c.send(e,s)),on:(e,s)=>((r[e]??=[]).push(s),a()),remove:(e,s,t=r[e],o=t?.indexOf(s)??-1)=>(~o&&t?.splice(o,1),a())}[s])});return c};
|
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './connect';
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let t,o=0,n=[],r={},a=()=>(t||(t=new WebSocket((/^wss?:/.test(e)?e:"wss://ittysockets.io/c/"+e)+"?"+new URLSearchParams(s)),t.onclose=()=>{o=0,t=null;for(let e of r.close??[])e()},t.onopen=()=>{for(;n.length;)t?.send(n.shift());for(let e of r.open??[])e();o&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of r[s.type??"message"]??[])e({...s,date:new Date(s.date)})}),c);const c=new Proxy(a,{get:(e,s)=>({close:()=>(1==t?.readyState?t.close():o=1,c),open:a,send:(e,s)=>(e=JSON.stringify(e),e=s?"@@"+s+"@@"+e:e,1==t?.readyState?(t.send(e),c):(n.push(e),a())),push:(e,s)=>(o=1,c.send(e,s)),on:(e,s)=>((r[e]??=[]).push(s),a()),remove:(e,s,t=r[e],o=t?.indexOf(s)??-1)=>(~o&&t?.splice(o,1),a())}[s])});return c};
|
package/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const e=(e,s={})=>{let o,t=0,n=[],a={},r=()=>(o||(o=new WebSocket((/^wss?:/.test(e)?e:"wss://ittysockets.io/c/"+e)+"?"+new URLSearchParams(s)),o.onclose=()=>{t=0,o=null;for(let e of a.close??[])e()},o.onopen=()=>{for(;n.length;)o?.send(n.shift());for(let e of a.open??[])e();t&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})}),l);const l=new Proxy(r,{get:(e,s)=>({close:()=>(1==o?.readyState?o.close():t=1,l),open:r,send:(e,s)=>(e=JSON.stringify(e),e=s?"@@"+s+"@@"+e:e,1==o?.readyState?(o.send(e),l):(n.push(e),r())),push:(e,s)=>(t=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};export{e as connect};
|