@casual-simulation/websocket 3.5.3-alpha.16326443512 → 3.10.2

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.
Files changed (2) hide show
  1. package/README.md +58 -92
  2. package/package.json +41 -42
package/README.md CHANGED
@@ -1,121 +1,87 @@
1
- # undom
1
+ # @casual-simulation/websocket
2
2
 
3
- [![NPM](https://img.shields.io/npm/v/undom.svg?style=flat)](https://www.npmjs.org/package/undom)
4
- [![travis-ci](https://travis-ci.org/developit/undom.svg?branch=master)](https://travis-ci.org/developit/undom)
3
+ A lightweight WebSocket management library that provides automatic reconnection capabilities and connection lifecycle management using RxJS observables.
5
4
 
6
- ### **Minimally viable DOM Document implementation**
5
+ ## Overview
7
6
 
8
- > A bare-bones HTML DOM in a box. If you want the DOM but not a parser, this might be for you.
9
- >
10
- > `1kB`, works in Node and browsers, plugins coming soon!
7
+ This package provides two main classes for managing WebSocket connections with built-in reconnection logic and observable-based event handling:
11
8
 
12
- [**JSFiddle Demo:**](https://jsfiddle.net/developit/4qv3v6r3/) Rendering [preact](https://github.com/developit/preact/) components into an undom Document.
9
+ ### `ReconnectableSocket`
13
10
 
14
- [![preview](https://i.gyazo.com/7fcca9dd3e562b076293ef2cf3979d23.gif)](https://jsfiddle.net/developit/4qv3v6r3/)
11
+ A WebSocket wrapper that provides automatic reconnection capabilities and exposes connection events through RxJS observables.
15
12
 
16
- ---
13
+ **Features:**
17
14
 
18
- ## Project Goals
15
+ - Wraps native WebSocket API with observable-based event handling
16
+ - Distinguishes between intentional disconnections and unexpected connection losses
17
+ - Provides `onOpen`, `onClose`, `onMessage`, and `onError` observables
18
+ - Manual control over opening and closing connections
19
+ - Supports WebSocket protocols
19
20
 
20
- Undom aims to find a sweet spot between size/performance and utility. The goal is to provide the simplest possible implementation of a DOM Document, such that libraries relying on the DOM can run in places where there isn't one available.
21
+ **Usage:**
21
22
 
22
- The intent to keep things as simple as possible means undom lacks some DOM features like HTML parsing & serialization, Web Components, etc. These features can be added through additional libraries.
23
+ ```typescript
24
+ const socket = new ReconnectableSocket('wss://example.com', 'protocol');
23
25
 
24
- #### Looking to 1.0.0
26
+ socket.onOpen.subscribe(() => {
27
+ console.log('Connected!');
28
+ });
25
29
 
26
- As of version 1.0.0, the DOM constructors and their prototypes will be shared for all instances of a document, as is the case with [JSDOM](https://github.com/jsdom/jsdom#shared-constructors-and-prototypes). Once merged, [PR #25](https://github.com/developit/undom/pull/25) will address this by adding an `undom.env()` function, which returns a fresh document factory with a new set of constructors & prototypes.
30
+ socket.onMessage.subscribe((event) => {
31
+ console.log('Message received:', event.data);
32
+ });
27
33
 
28
- ---
29
-
30
- ## Installation
34
+ socket.onClose.subscribe((reason) => {
35
+ if (reason.type === 'closed') {
36
+ console.log('Intentionally disconnected');
37
+ } else {
38
+ console.log('Connection lost:', reason.reason);
39
+ }
40
+ });
31
41
 
32
- Via npm:
42
+ socket.open();
43
+ socket.send('Hello, server!');
44
+ ```
33
45
 
34
- `npm install --save undom`
46
+ ### `SocketManager`
35
47
 
36
- ---
48
+ A higher-level WebSocket connection manager that automatically handles reconnection attempts after unexpected disconnections.
37
49
 
38
- ## Require Hook
50
+ **Features:**
39
51
 
40
- In CommonJS environments, simply import `undom/register` to patch the global object with a singleton Document.
52
+ - Manages `ReconnectableSocket` lifecycle
53
+ - Automatic reconnection with configurable delay (default: 5 seconds)
54
+ - Connection state tracking via observable
55
+ - Manual offline mode control
56
+ - Debounced reconnection to prevent rapid retry attempts
41
57
 
42
- ```js
43
- require('undom/register');
58
+ **Usage:**
44
59
 
45
- // now you have a DOM.
46
- document.createElement('div');
47
- ```
60
+ ```typescript
61
+ const manager = new SocketManager('wss://example.com', 'protocol');
48
62
 
49
- ---
63
+ manager.connectionStateChanged.subscribe((connected) => {
64
+ console.log(connected ? 'Online' : 'Offline');
65
+ });
50
66
 
51
- ## Usage
67
+ manager.init();
52
68
 
53
- ```js
54
- // import the library:
55
- import undom from 'undom';
69
+ // Access underlying socket
70
+ manager.socket.send('Hello!');
56
71
 
57
- let document = undom();
72
+ // Force offline mode
73
+ manager.forcedOffline = true;
58
74
 
59
- let foo = document.createElement('foo');
60
- foo.appendChild(document.createTextNode('Hello, World!'));
61
- document.body.appendChild(foo);
75
+ // Toggle offline mode
76
+ manager.toggleForceOffline();
62
77
  ```
63
78
 
64
- ---
65
-
66
- ## Recipe: Serialize to HTML
67
-
68
- One task `undom` doesn't handle for you by default is HTML serialization. A proper implementation of this would be cumbersome to maintain and would rely heavily on getters and setters, which limits browser support. Below is a simple recipe for serializing an `undom` Element (Document, etc) to HTML.
69
-
70
- #### Small & in ES2015:
71
-
72
- ```js
73
- Element.prototype.toString = function () {
74
- return serialize(this);
75
- };
76
-
77
- function serialize(el) {
78
- return el.nodeType == 3
79
- ? enc(el.nodeValue)
80
- : '<' +
81
- this.nodeName.toLowerCase() +
82
- this.attributes.map(attr).join('') +
83
- '>' +
84
- this.childNodes.map(serialize).join('') +
85
- '</' +
86
- this.nodeName.toLowerCase() +
87
- '>';
88
- }
89
- let attr = (a) => ` ${a.name}="${enc(a.value)}"`;
90
- let enc = (s) => s.replace(/[&'"<>]/g, (a) => `&#${a.codePointAt(0)};`);
91
- ```
79
+ ## Installation
92
80
 
93
- #### ES3 Version
81
+ ```bash
82
+ npm install @casual-simulation/websocket
83
+ ```
94
84
 
95
- This also does pretty-printing.
85
+ ## Dependencies
96
86
 
97
- ```js
98
- function serialize(el) {
99
- if (el.nodeType === 3) return el.textContent;
100
- var name = String(el.nodeName).toLowerCase(),
101
- str = '<' + name,
102
- c,
103
- i;
104
- for (i = 0; i < el.attributes.length; i++) {
105
- str +=
106
- ' ' + el.attributes[i].name + '="' + el.attributes[i].value + '"';
107
- }
108
- str += '>';
109
- for (i = 0; i < el.childNodes.length; i++) {
110
- c = serialize(el.childNodes[i]);
111
- if (c) str += '\n\t' + c.replace(/\n/g, '\n\t');
112
- }
113
- return str + (c ? '\n' : '') + '</' + name + '>';
114
- }
115
-
116
- function enc(s) {
117
- return s.replace(/[&'"<>]/g, function (a) {
118
- return `&#${a};`;
119
- });
120
- }
121
- ```
87
+ - **rxjs**: For observable-based event handling and stream operators
package/package.json CHANGED
@@ -1,43 +1,42 @@
1
1
  {
2
- "name": "@casual-simulation/websocket",
3
- "version": "3.5.3-alpha.16326443512",
4
- "description": "Minimal WebSocket implementation",
5
- "keywords": [],
6
- "author": "Casual Simulation, Inc.",
7
- "homepage": "https://github.com/casual-simulation/casualos",
8
- "license": "AGPL-3.0-only",
9
- "main": "index.js",
10
- "types": "index.d.ts",
11
- "module": "index",
12
- "directories": {
13
- "lib": "."
14
- },
15
- "files": [
16
- "/README.md",
17
- "/LICENSE.txt",
18
- "**/*.js",
19
- "**/*.js.map",
20
- "**/*.d.ts"
21
- ],
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/casual-simulation/casualos.git"
25
- },
26
- "scripts": {
27
- "watch": "tsc --watch",
28
- "watch:player": "npm run watch",
29
- "build": "echo \"Nothing to do.\"",
30
- "test": "jest",
31
- "test:watch": "jest --watchAll"
32
- },
33
- "bugs": {
34
- "url": "https://github.com/casual-simulation/casualos/issues"
35
- },
36
- "publishConfig": {
37
- "access": "public"
38
- },
39
- "dependencies": {
40
- "rxjs": "8.0.0-alpha.14"
41
- },
42
- "gitHead": "a2f91bd2715625eb0bf3febfa4a2380f0f868dd5"
43
- }
2
+ "name": "@casual-simulation/websocket",
3
+ "version": "3.10.2",
4
+ "description": "Minimal WebSocket implementation",
5
+ "keywords": [],
6
+ "author": "Casual Simulation, Inc.",
7
+ "homepage": "https://github.com/casual-simulation/casualos",
8
+ "license": "AGPL-3.0-only",
9
+ "main": "index.js",
10
+ "types": "index.d.ts",
11
+ "module": "index",
12
+ "directories": {
13
+ "lib": "."
14
+ },
15
+ "files": [
16
+ "/README.md",
17
+ "/LICENSE.txt",
18
+ "**/*.js",
19
+ "**/*.js.map",
20
+ "**/*.d.ts"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/casual-simulation/casualos.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/casual-simulation/casualos/issues"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "dependencies": {
33
+ "rxjs": "8.0.0-alpha.14"
34
+ },
35
+ "scripts": {
36
+ "watch": "tsc --watch",
37
+ "watch:player": "npm run watch",
38
+ "build": "echo \"Nothing to do.\"",
39
+ "test": "jest",
40
+ "test:watch": "jest --watchAll"
41
+ }
42
+ }