angelia.io 3.4.83 → 4.0.85

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.
@@ -0,0 +1,217 @@
1
+ export function ClientWebWorker() {
2
+ const id = () =>
3
+ crypto.getRandomValues(new BigUint64Array(1))[0].toString(36)
4
+
5
+ const stringify = JSON.stringify
6
+ const parse = JSON.parse
7
+
8
+ Blob.prototype.toJSON = function () {
9
+ return {
10
+ b: new FileReaderSync().readAsDataURL(this),
11
+ }
12
+ }
13
+ ArrayBuffer.prototype.toJSON = function () {
14
+ return {
15
+ a: new FileReaderSync().readAsDataURL(this),
16
+ }
17
+ }
18
+
19
+ const OPEN = 1
20
+ const CLOSING = 2
21
+ const CLOSED = 3
22
+
23
+ const connid = id()
24
+
25
+ let connected = true
26
+ let reconnect = true
27
+ let isReconnect = false
28
+
29
+ let messages = []
30
+ let buffered = []
31
+
32
+ let io
33
+
34
+ self.onmessage = function (e) {
35
+ switch (e.data[0]) {
36
+ case 'emit': {
37
+ emit(e.data[1])
38
+ break
39
+ }
40
+ case 'connect': {
41
+ connect(e.data[1])
42
+ break
43
+ }
44
+ case 'disconnect': {
45
+ disconnect(e.data[1])
46
+ break
47
+ }
48
+ }
49
+ }
50
+
51
+ function onopen() {
52
+ connected = true
53
+ postMessage(['connected', true])
54
+
55
+ if (isReconnect) {
56
+ postMessage(['dispatch', 'reconnect'])
57
+ } else {
58
+ isReconnect = true
59
+ postMessage(['dispatch', 'connect'])
60
+ }
61
+
62
+ send()
63
+ }
64
+ function onclose(event) {
65
+ const { code, reason } = event
66
+ switch (code) {
67
+ case 1000:
68
+ /**
69
+ * Normal close - console.log('ws - normal close', event.code,
70
+ * event.reason)
71
+ */
72
+ break
73
+ case 1005:
74
+ /**
75
+ * Closed by client - console.log( 'ws - we called
76
+ * socket.disconnect()', event.code, event.reason, )
77
+ */
78
+ break
79
+ case 1006: {
80
+ /**
81
+ * Closed by server or connection never opened and failed to
82
+ * connect - console.log( 'ws - server killed the connection,
83
+ * or we failed to connect to server', event.code,
84
+ * event.reason, )
85
+ */
86
+ break
87
+ }
88
+ default: {
89
+ console.warn('ws - unknown close', code, reason)
90
+ break
91
+ }
92
+ }
93
+
94
+ if (connected) {
95
+ connected = false
96
+ postMessage(['connected', false])
97
+ postMessage(['dispatch', 'disconnect'])
98
+ }
99
+
100
+ // do not reconnect when we call `socket.disconnect()`
101
+ if (reconnect && code !== 1005) {
102
+ postMessage(['connect'])
103
+ }
104
+ }
105
+
106
+ /**
107
+ * This happens when trying to connect while the server or the
108
+ * internet connection is down
109
+ */
110
+ function onerror() {
111
+ if (connected) {
112
+ connected = false
113
+ postMessage(['connected', false])
114
+ postMessage(['dispatch', 'disconnect'])
115
+ }
116
+
117
+ if (reconnect) {
118
+ postMessage(['connect'])
119
+ }
120
+ }
121
+
122
+ function disconnect(noReconnect) {
123
+ if (noReconnect) {
124
+ reconnect = false
125
+ }
126
+
127
+ if (io && io.readyState !== CLOSING && io.readyState !== CLOSED) {
128
+ io.close()
129
+ }
130
+ }
131
+
132
+ function connect(options) {
133
+ if (
134
+ reconnect &&
135
+ (!io || io.readyState === CLOSED || io.readyState === CLOSING)
136
+ ) {
137
+ const url = new URL(options.url)
138
+ url.pathname = 'angelia'
139
+
140
+ const { searchParams } = url
141
+ // params
142
+ const params = options.params
143
+ params.connid = connid
144
+
145
+ for (const [k, v] of Object.entries(params)) {
146
+ searchParams.append(k, v)
147
+ }
148
+
149
+ // append buffered messages
150
+ if (messages.length) {
151
+ if (!isReconnect) {
152
+ let length = url.href.length
153
+ const queue = []
154
+ while (messages.length && length < 2048) {
155
+ const message = messages.shift()
156
+ const string = stringify(message)
157
+ if (length + string.length < 2048) {
158
+ length += string.length
159
+ queue.push(message)
160
+ } else {
161
+ messages.unshift(message)
162
+ break
163
+ }
164
+ }
165
+ if (queue.length) {
166
+ searchParams.append('angelia', stringify(queue))
167
+ }
168
+ } else {
169
+ for (const m of messages) {
170
+ // dont buffer callbacks, they are gone for new connections
171
+ if (m[0] !== '') {
172
+ buffered.push(m)
173
+ }
174
+ }
175
+ messages = []
176
+ }
177
+ }
178
+
179
+ io = new WebSocket(url)
180
+ io.onopen = onopen
181
+ io.onclose = onclose
182
+ io.onerror = onerror
183
+ io.onmessage = onmessage
184
+ }
185
+ }
186
+
187
+ function emit(msgs) {
188
+ for (const m of msgs) {
189
+ messages.push(m)
190
+ }
191
+ send()
192
+ }
193
+ function send() {
194
+ if (io?.readyState === OPEN) {
195
+ let m
196
+ if (messages.length) {
197
+ m = messages
198
+ messages = []
199
+ io.send(stringify(m))
200
+ }
201
+ if (buffered.length) {
202
+ m = buffered
203
+ buffered = []
204
+ io.send(stringify(m))
205
+ }
206
+ }
207
+ }
208
+ function pong() {
209
+ if (io?.readyState === OPEN) {
210
+ io.send('')
211
+ }
212
+ }
213
+
214
+ function onmessage(e) {
215
+ e.data === '' ? pong() : postMessage(['messages', parse(e.data)])
216
+ }
217
+ }
package/package.json CHANGED
@@ -1,32 +1,38 @@
1
1
  {
2
2
  "name": "angelia.io",
3
- "version": "3.4.83",
3
+ "version": "4.0.85",
4
4
  "description": "WebSockets Server and Client API for node.js and the browser, with rooms support.",
5
5
  "author": "@titoBouzout @boredofnames",
6
6
  "license": "GPL",
7
- "homepage": "https://github.com/titoBouzout/angelia.io",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/titoBouzout/angelia.io"
11
- },
12
7
  "type": "module",
13
- "main": "index.js",
14
- "dependencies": {
15
- "bufferutil": "^4.0.3",
16
- "is-class": "^0.0.9",
17
- "isobject": "^4.0.0",
18
- "to-fast-properties": "^3.0.1",
19
- "utf-8-validate": "^6.x.x",
20
- "ws": "^8.x.x"
21
- },
22
8
  "exports": {
23
9
  "./server": {
24
- "default": "./server/Server.js"
10
+ "types": "./types/server/index.js",
11
+ "default": "./server/index.js"
25
12
  },
26
13
  "./client": {
14
+ "types": "./types/client/index.js",
27
15
  "default": "./client/index.js"
28
16
  }
29
17
  },
18
+ "dependencies": {
19
+ "bufferutil": "^4.0.8",
20
+ "formidable": "^3.5.2",
21
+ "utf-8-validate": "^6.0.5",
22
+ "ws": "^8.18.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "^22.9.1",
26
+ "typescript": "^5.6.3"
27
+ },
28
+ "scripts": {
29
+ "dev": "tsc -w"
30
+ },
31
+ "homepage": "https://github.com/titoBouzout/angelia.io",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/titoBouzout/angelia.io"
35
+ },
30
36
  "keywords": [
31
37
  "Client",
32
38
  "Server",