angelia.io 4.0.88 → 4.0.90

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/client/worker.js CHANGED
@@ -70,6 +70,12 @@ export function ClientWebWorker() {
70
70
  * event.reason)
71
71
  */
72
72
  break
73
+ case 1001:
74
+ /**
75
+ * Freaking firefox code when reloading the page, must be
76
+ * blacklisted else it will connect twice in reloads.
77
+ */
78
+ break
73
79
  case 1005:
74
80
  /**
75
81
  * Closed by client - console.log( 'ws - we called
@@ -97,8 +103,12 @@ export function ClientWebWorker() {
97
103
  postMessage(['dispatch', 'disconnect'])
98
104
  }
99
105
 
100
- // do not reconnect when we call `socket.disconnect()`
101
- if (reconnect && code !== 1005) {
106
+ console.log(code)
107
+
108
+ // 1001: freaking firefox
109
+ // 1005: do not reconnect when we call `socket.disconnect()`
110
+ // 1006: DO NOT USE! THAT HAPPENS WHEN SERVER CONNECTION IS KILLED
111
+ if (reconnect && code !== 1005 && code !== 1001) {
102
112
  postMessage(['connect'])
103
113
  }
104
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angelia.io",
3
- "version": "4.0.88",
3
+ "version": "4.0.90",
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",
@@ -13,7 +13,8 @@
13
13
  "./client": {
14
14
  "types": "./types/client/index.js",
15
15
  "default": "./client/index.js"
16
- }
16
+ },
17
+ "./*": null
17
18
  },
18
19
  "dependencies": {
19
20
  "bufferutil": "^4.0.8",
package/readme.md CHANGED
@@ -261,7 +261,7 @@ Server.listen()
261
261
 
262
262
  ### `Room` Class
263
263
 
264
- Iterating `room` returns the sockets
264
+ Iterating returns the sockets
265
265
 
266
266
  | signature | kind | description |
267
267
  | ------------------------------------- | ------- | ------------------------------------------------ |
@@ -273,20 +273,37 @@ Iterating `room` returns the sockets
273
273
  | `connections` | number | number of sockets in the room |
274
274
  | `sockets` | Set | sockets in the room |
275
275
  | `emit(key, [value])` | method | emits to all sockets in the room |
276
- | `once(key, [value])` | method | emits once all sockets in the room |
276
+ | `once(key, [value])` | method | emits once to all sockets in the room |
277
277
  | `broadcast(socket, key, [value])` | method | emits to other sockets in the room |
278
278
  | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in the room |
279
279
  | `id` | any | the room id |
280
280
 
281
281
  ### `Rooms` Class
282
282
 
283
- Iterating `rooms` returns the rooms
284
-
285
- | signature | kind | description |
286
- | --------------------- | ------ | ------------------------------ |
287
- | `get(id)` | method | returns room with such an `id` |
288
- | `join(socket, [id])` | method | adds socket to room `id` |
289
- | `leave(socket, [id])` | method | removes socket from room `id` |
283
+ Iterating returns the rooms
284
+
285
+ | signature | kind | description |
286
+ | ------------------------------------- | -------- | --------------------------------------------- |
287
+ | `get(id)` | method | gets room with such an `id` |
288
+ | `join(socket, [id])` | method | adds socket to room `id` |
289
+ | `leave(socket, [id])` | method | removes socket from room `id` |
290
+ | `connections` | number | number of sockets in this room list |
291
+ | `sockets` | Iterator | sockets in this room list |
292
+ | `emit(key, [value])` | method | emits to all sockets in this room list |
293
+ | `once(key, [value])` | method | emits once to all sockets in this room list |
294
+ | `broadcast(socket, key, [value])` | method | emits to other sockets in this room list |
295
+ | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in this room list |
296
+ | `map(fn)` | method | returns mapped array |
297
+ | `filter(fn)` | method | returns mapped filtered array |
298
+
299
+ ### `sockets` Object
300
+
301
+ Like `Set` but with two aditional methods
302
+
303
+ | signature | kind | description |
304
+ | ------------ | ------ | ----------------------------- |
305
+ | `map(fn)` | method | returns mapped array |
306
+ | `filter(fn)` | method | returns mapped filtered array |
290
307
 
291
308
  ## Authors
292
309
 
package/server/Emitter.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { MapeableSet } from './utils.js'
2
+
1
3
  export class Emitter {
2
4
  constructor() {
3
- /** @type Set<import('./Socket.js').Socket> */
4
- this.sockets = new Set()
5
+ /** @type MapeableSet<import('./Socket.js').Socket> */
6
+ this.sockets = new MapeableSet()
5
7
  }
6
8
 
7
9
  get connections() {
@@ -20,6 +22,7 @@ export class Emitter {
20
22
  socket.once(d)
21
23
  }
22
24
  }
25
+ /** @param me {import('./Socket.js').Socket} */
23
26
  broadcast(me, k, v) {
24
27
  const d = [k, v]
25
28
  for (const socket of this.sockets) {
@@ -28,6 +31,7 @@ export class Emitter {
28
31
  }
29
32
  }
30
33
  }
34
+ /** @param me {import('./Socket.js').Socket} */
31
35
  broadcastOnce(me, k, v) {
32
36
  const d = [k, v]
33
37
  for (const socket of this.sockets) {
package/server/Room.js CHANGED
@@ -6,11 +6,16 @@ export class Room extends Emitter {
6
6
  id
7
7
  persistent
8
8
 
9
+ /** @param socket {import('./Socket.js').Socket} */
9
10
  onCreate(socket) {}
11
+ /** @param socket {import('./Socket.js').Socket} */
10
12
  onDelete(socket) {}
13
+ /** @param socket {import('./Socket.js').Socket} */
11
14
  onJoin(socket) {}
15
+ /** @param socket {import('./Socket.js').Socket} */
12
16
  onLeave(socket) {}
13
17
 
18
+ /** @param socket {import('./Socket.js').Socket} */
14
19
  [join](socket) {
15
20
  this.sockets.add(socket)
16
21
 
@@ -18,6 +23,7 @@ export class Room extends Emitter {
18
23
 
19
24
  this.onJoin(socket)
20
25
  }
26
+ /** @param socket {import('./Socket.js').Socket} */
21
27
  [leave](socket) {
22
28
  this.sockets.delete(socket)
23
29
 
package/server/Rooms.js CHANGED
@@ -1,38 +1,74 @@
1
- import { join, leave } from './constants.js'
1
+ import { join, leave, parent } from './constants.js'
2
+ import { Emitter } from './Emitter.js'
3
+ import { arrayFrom } from './utils.js'
2
4
 
3
- export class Rooms {
5
+ export class Rooms extends Emitter {
4
6
  /** @param {Object} RoomClass */
5
7
  constructor(RoomClass) {
8
+ super()
9
+
6
10
  this.RoomClass = RoomClass
7
11
  this.rooms = new Map()
12
+
13
+ Object.defineProperty(this, 'sockets', {
14
+ get: this.Sockets.bind(this),
15
+ enumerable: true,
16
+ })
8
17
  }
18
+
9
19
  get(id) {
10
20
  return this.rooms.get(id)
11
21
  }
22
+
12
23
  join(socket, id = undefined) {
13
24
  let room = this.rooms.get(id)
14
25
  if (!room) {
15
26
  room = new this.RoomClass()
16
- room.id = room.id || id
27
+ room[parent] = this
28
+ room.id = id
17
29
  this.rooms.set(id, room)
18
30
 
19
31
  room.onCreate(socket)
20
32
  }
21
-
22
33
  room[join](socket)
23
34
  }
35
+
24
36
  leave(socket, id = undefined) {
25
37
  const room = this.rooms.get(id)
26
38
  if (room) {
27
39
  room[leave](socket)
28
-
29
- if (!room.persistent && room.size === 0) {
30
- room.onDelete(socket)
40
+ if (!room.persistent && room.sockets.size === 0) {
31
41
  this.rooms.delete(id)
42
+ room.onDelete(socket)
32
43
  }
33
44
  }
34
45
  }
35
46
 
47
+ get connections() {
48
+ let count = 0
49
+ for (const room of this.rooms.values()) {
50
+ count += room.sockets.size
51
+ }
52
+ return count
53
+ }
54
+
55
+ *Sockets() {
56
+ for (const room of this.rooms.values()) {
57
+ for (const socket of room.sockets) {
58
+ yield socket
59
+ }
60
+ }
61
+ }
62
+
63
+ map(fn) {
64
+ return arrayFrom(this.rooms.values(), fn)
65
+ }
66
+ filter(fn) {
67
+ return this.map().filter(fn)
68
+ }
69
+ toJSON() {
70
+ return this.map()
71
+ }
36
72
  [Symbol.iterator]() {
37
73
  return this.rooms.values()
38
74
  }
package/server/Server.js CHANGED
@@ -158,7 +158,7 @@ export default new (class Server extends Emitter {
158
158
  on(listener) {
159
159
  const instance = new listener()
160
160
 
161
- const methods = [
161
+ const methods = listener.listeners || [
162
162
  // todo maybe use getPrototypeOf ?
163
163
  ...Object.getOwnPropertyNames(instance.__proto__),
164
164
  ...Object.getOwnPropertyNames(instance),
@@ -175,6 +175,8 @@ export default new (class Server extends Emitter {
175
175
  }
176
176
  }
177
177
  }
178
+
179
+ return instance
178
180
  }
179
181
 
180
182
  // private
package/server/Socket.js CHANGED
@@ -1,6 +1,12 @@
1
- import { leave } from './constants.js'
1
+ import { parent } from './constants.js'
2
2
 
3
- import { fromEntries, inspect, isArray, parse } from './utils.js'
3
+ import {
4
+ fromEntries,
5
+ inspect,
6
+ isArray,
7
+ MapeableSet,
8
+ parse,
9
+ } from './utils.js'
4
10
 
5
11
  export class Socket {
6
12
  constructor(socket, server, request) {
@@ -34,7 +40,7 @@ export class Socket {
34
40
  this.ping = 0
35
41
  this.timedout = false
36
42
 
37
- this.rooms = new Set()
43
+ this.rooms = new MapeableSet()
38
44
  }
39
45
  emit(k, v, cb) {
40
46
  if (!this.messages.length) {
@@ -91,7 +97,7 @@ export class Socket {
91
97
  this.server.sockets.delete(this)
92
98
 
93
99
  for (const room of this.rooms) {
94
- room[leave](this)
100
+ room[parent].leave(this, room.id)
95
101
  }
96
102
 
97
103
  this.server.events.disconnect &&
@@ -155,7 +161,7 @@ export class Socket {
155
161
  this.server.events[m[0]](
156
162
  this,
157
163
  m[1],
158
- m[2] && this.doreply.bind(null, m[2]),
164
+ m[2] && this.doreply.bind(this, m[2]),
159
165
  )
160
166
  } else {
161
167
  this.server.messagesGarbage++
@@ -1,2 +1,3 @@
1
1
  export const join = Symbol('join')
2
2
  export const leave = Symbol('leave')
3
+ export const parent = Symbol('parent')
package/server/utils.js CHANGED
@@ -1,5 +1,3 @@
1
- // utils
2
-
3
1
  export const empty = () => Object.create(null)
4
2
 
5
3
  export const now = Date.now
@@ -16,6 +14,8 @@ export const isArray = Array.isArray
16
14
 
17
15
  export const fromEntries = Object.fromEntries
18
16
 
17
+ export const arrayFrom = Array.from
18
+
19
19
  export function ListenerTemplate() {
20
20
  const fns = []
21
21
  function Listener(...args) {
@@ -45,3 +45,12 @@ export const frame = function (
45
45
  opcode: 1,
46
46
  fin: true,
47
47
  })
48
+
49
+ export class MapeableSet extends Set {
50
+ map(fn) {
51
+ return arrayFrom(this, fn)
52
+ }
53
+ filter(fn) {
54
+ return this.map().filter(fn)
55
+ }
56
+ }
@@ -1,9 +1,12 @@
1
1
  export class Emitter {
2
- /** @type Set<import('./Socket.js').Socket> */
3
- sockets: Set<import("./Socket.js").Socket>;
4
- get connections(): number;
2
+ /** @type MapeableSet<import('./Socket.js').Socket> */
3
+ sockets: MapeableSet<import("./Socket.js").Socket>;
4
+ get connections(): any;
5
5
  emit(k: any, v: any): void;
6
6
  once(k: any, v: any): void;
7
- broadcast(me: any, k: any, v: any): void;
8
- broadcastOnce(me: any, k: any, v: any): void;
7
+ /** @param me {import('./Socket.js').Socket} */
8
+ broadcast(me: import("./Socket.js").Socket, k: any, v: any): void;
9
+ /** @param me {import('./Socket.js').Socket} */
10
+ broadcastOnce(me: import("./Socket.js").Socket, k: any, v: any): void;
9
11
  }
12
+ import { MapeableSet } from './utils.js';
@@ -1,13 +1,19 @@
1
1
  export class Room extends Emitter {
2
2
  id: any;
3
3
  persistent: any;
4
- onCreate(socket: any): void;
5
- onDelete(socket: any): void;
6
- onJoin(socket: any): void;
7
- onLeave(socket: any): void;
8
- [join](socket: any): void;
9
- [leave](socket: any): void;
10
- [Symbol.iterator](): Set<import("./Socket.js").Socket>;
4
+ /** @param socket {import('./Socket.js').Socket} */
5
+ onCreate(socket: import("./Socket.js").Socket): void;
6
+ /** @param socket {import('./Socket.js').Socket} */
7
+ onDelete(socket: import("./Socket.js").Socket): void;
8
+ /** @param socket {import('./Socket.js').Socket} */
9
+ onJoin(socket: import("./Socket.js").Socket): void;
10
+ /** @param socket {import('./Socket.js').Socket} */
11
+ onLeave(socket: import("./Socket.js").Socket): void;
12
+ /** @param socket {import('./Socket.js').Socket} */
13
+ [join](socket: import("./Socket.js").Socket): void;
14
+ /** @param socket {import('./Socket.js').Socket} */
15
+ [leave](socket: import("./Socket.js").Socket): void;
16
+ [Symbol.iterator](): any;
11
17
  }
12
18
  import { Emitter } from './Emitter.js';
13
19
  import { join } from './constants.js';
@@ -1,8 +1,15 @@
1
- export class Rooms {
2
- constructor(Room: any);
3
- Room: any;
1
+ export class Rooms extends Emitter {
2
+ /** @param {Object} RoomClass */
3
+ constructor(RoomClass: any);
4
+ RoomClass: any;
4
5
  rooms: Map<any, any>;
6
+ get(id: any): any;
5
7
  join(socket: any, id?: undefined): void;
6
8
  leave(socket: any, id?: undefined): void;
9
+ Sockets(): Generator<any, void, unknown>;
10
+ map(fn: any): any[];
11
+ filter(fn: any): any[];
12
+ toJSON(): any[];
7
13
  [Symbol.iterator](): MapIterator<any>;
8
14
  }
15
+ import { Emitter } from './Emitter.js';
@@ -49,7 +49,7 @@ declare const _default: {
49
49
  * @template T
50
50
  * @param {T} listener
51
51
  */
52
- on<T>(listener: T): void;
52
+ on<T>(listener: T): any;
53
53
  onconnect: (socket: any, request: any) => void;
54
54
  onerror: (err: any) => void;
55
55
  nextQueue(socket: any): void;
@@ -63,12 +63,12 @@ declare const _default: {
63
63
  [inspect](): any & {
64
64
  sockets: string;
65
65
  };
66
- sockets: Set<import("./Socket.js").Socket>;
67
- readonly connections: number;
66
+ sockets: import("./utils.js").MapeableSet<import("./Socket.js").Socket>;
67
+ readonly connections: any;
68
68
  emit(k: any, v: any): void;
69
69
  once(k: any, v: any): void;
70
- broadcast(me: any, k: any, v: any): void;
71
- broadcastOnce(me: any, k: any, v: any): void;
70
+ broadcast(me: import("./Socket.js").Socket, k: any, v: any): void;
71
+ broadcastOnce(me: import("./Socket.js").Socket, k: any, v: any): void;
72
72
  };
73
73
  export default _default;
74
74
  import { inspect } from './utils.js';
@@ -20,7 +20,7 @@ export class Socket {
20
20
  contacted: any;
21
21
  ping: number;
22
22
  timedout: boolean;
23
- rooms: Set<any>;
23
+ rooms: MapeableSet;
24
24
  emit(k: any, v: any, cb: any): void;
25
25
  once(k: any, v: any): void;
26
26
  disconnect(noReconnect: any): void;
@@ -44,4 +44,5 @@ export class Socket {
44
44
  readyState: any;
45
45
  };
46
46
  }
47
+ import { MapeableSet } from './utils.js';
47
48
  import { inspect } from './utils.js';
@@ -1,2 +1,3 @@
1
1
  export const join: unique symbol;
2
2
  export const leave: unique symbol;
3
+ export const parent: unique symbol;
@@ -23,4 +23,16 @@ export const fromEntries: {
23
23
  };
24
24
  (entries: Iterable<readonly any[]>): any;
25
25
  };
26
+ export const arrayFrom: {
27
+ <T>(arrayLike: ArrayLike<T>): T[];
28
+ <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[];
29
+ <T>(iterable: Iterable<T> | ArrayLike<T>): T[];
30
+ <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[];
31
+ };
26
32
  export const frame: (data?: any) => any;
33
+ export class MapeableSet extends Set<any> {
34
+ constructor(values?: readonly any[] | null | undefined);
35
+ constructor(iterable?: Iterable<any> | null | undefined);
36
+ map(fn: any): any[];
37
+ filter(fn: any): any[];
38
+ }