tinybase 5.3.5 → 5.4.0-beta.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/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +145 -0
- package/@types/synchronizers/synchronizer-ws-server-simple/index.d.ts +145 -0
- package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +145 -0
- package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.ts +145 -0
- package/cjs/es6/min/synchronizers/synchronizer-ws-server-simple/index.cjs +1 -0
- package/cjs/es6/min/synchronizers/synchronizer-ws-server-simple/index.cjs.gz +0 -0
- package/cjs/es6/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.cjs +1 -0
- package/cjs/es6/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.cjs.gz +0 -0
- package/cjs/es6/synchronizers/synchronizer-ws-server-simple/index.cjs +130 -0
- package/cjs/es6/synchronizers/synchronizer-ws-server-simple/with-schemas/index.cjs +130 -0
- package/cjs/min/synchronizers/synchronizer-ws-server-simple/index.cjs +1 -0
- package/cjs/min/synchronizers/synchronizer-ws-server-simple/index.cjs.gz +0 -0
- package/cjs/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.cjs +1 -0
- package/cjs/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.cjs.gz +0 -0
- package/cjs/synchronizers/synchronizer-ws-server-simple/index.cjs +89 -0
- package/cjs/synchronizers/synchronizer-ws-server-simple/with-schemas/index.cjs +89 -0
- package/es6/min/synchronizers/synchronizer-ws-server-simple/index.js +1 -0
- package/es6/min/synchronizers/synchronizer-ws-server-simple/index.js.gz +0 -0
- package/es6/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +1 -0
- package/es6/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js.gz +0 -0
- package/es6/synchronizers/synchronizer-ws-server-simple/index.js +128 -0
- package/es6/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +128 -0
- package/min/synchronizers/synchronizer-ws-server-simple/index.js +1 -0
- package/min/synchronizers/synchronizer-ws-server-simple/index.js.gz +0 -0
- package/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +1 -0
- package/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js.gz +0 -0
- package/package.json +107 -3
- package/readme.md +14 -14
- package/releases.md +24 -24
- package/synchronizers/synchronizer-ws-server-simple/index.js +87 -0
- package/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +87 -0
- package/umd/es6/min/synchronizers/synchronizer-ws-server-simple/index.js +1 -0
- package/umd/es6/min/synchronizers/synchronizer-ws-server-simple/index.js.gz +0 -0
- package/umd/es6/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +1 -0
- package/umd/es6/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js.gz +0 -0
- package/umd/es6/synchronizers/synchronizer-ws-server-simple/index.js +144 -0
- package/umd/es6/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +144 -0
- package/umd/min/synchronizers/synchronizer-ws-server-simple/index.js +1 -0
- package/umd/min/synchronizers/synchronizer-ws-server-simple/index.js.gz +0 -0
- package/umd/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +1 -0
- package/umd/min/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js.gz +0 -0
- package/umd/synchronizers/synchronizer-ws-server-simple/index.js +102 -0
- package/umd/synchronizers/synchronizer-ws-server-simple/with-schemas/index.js +102 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The synchronizer-ws-server-simple module of the TinyBase project lets you
|
|
3
|
+
* create a server that facilitates synchronization between clients, without the
|
|
4
|
+
* complications of listeners, persistence, or statistics.
|
|
5
|
+
*
|
|
6
|
+
* This makes it more suitable to be used as a reference implementation for
|
|
7
|
+
* other server environments.
|
|
8
|
+
* @see Synchronization guide
|
|
9
|
+
* @see Todo App v6 (collaboration) demo
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
* @module synchronizer-ws-server-simple
|
|
12
|
+
* @since v5.4.0
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type {WebSocketServer} from 'ws';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The WsServerSimple interface represents an object that facilitates
|
|
19
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
20
|
+
*
|
|
21
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
22
|
+
* the complications of listeners, persistence, or statistics.
|
|
23
|
+
*
|
|
24
|
+
* You should use the createWsServerSimple function to create a WsServerSimple
|
|
25
|
+
* object.
|
|
26
|
+
* @category Server
|
|
27
|
+
* @since v5.4.0
|
|
28
|
+
*/
|
|
29
|
+
export interface WsServerSimple {
|
|
30
|
+
/**
|
|
31
|
+
* The getWebSocketServer method returns a reference to the WebSocketServer
|
|
32
|
+
* being used for this WsServerSimple.
|
|
33
|
+
* @returns The WebSocketServer reference.
|
|
34
|
+
* @example
|
|
35
|
+
* This example creates a WsServerSimple and then gets the WebSocketServer
|
|
36
|
+
* reference back out again.
|
|
37
|
+
*
|
|
38
|
+
* ```js
|
|
39
|
+
* import {WebSocketServer} from 'ws';
|
|
40
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
41
|
+
*
|
|
42
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
43
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
44
|
+
*
|
|
45
|
+
* console.log(server.getWebSocketServer() == webSocketServer);
|
|
46
|
+
* // -> true
|
|
47
|
+
*
|
|
48
|
+
* server.destroy();
|
|
49
|
+
* ```
|
|
50
|
+
* @category Getter
|
|
51
|
+
* @since v5.4.0
|
|
52
|
+
*/
|
|
53
|
+
getWebSocketServer(): WebSocketServer;
|
|
54
|
+
/**
|
|
55
|
+
* The destroy method provides a way to clean up the server at the end of its
|
|
56
|
+
* use.
|
|
57
|
+
*
|
|
58
|
+
* This closes the underlying WebSocketServer that was provided when the
|
|
59
|
+
* WsServerSimple was created.
|
|
60
|
+
* @example
|
|
61
|
+
* This example creates a WsServerSimple and then destroys it again, closing
|
|
62
|
+
* the underlying WebSocketServer.
|
|
63
|
+
*
|
|
64
|
+
* ```js
|
|
65
|
+
* import {WebSocketServer} from 'ws';
|
|
66
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
67
|
+
*
|
|
68
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
69
|
+
* webSocketServer.on('close', () => {
|
|
70
|
+
* console.log('WebSocketServer closed');
|
|
71
|
+
* });
|
|
72
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
73
|
+
*
|
|
74
|
+
* server.destroy();
|
|
75
|
+
* // ...
|
|
76
|
+
* // -> 'WebSocketServer closed'
|
|
77
|
+
* ```
|
|
78
|
+
* @category Getter
|
|
79
|
+
* @since v5.4.0
|
|
80
|
+
*/
|
|
81
|
+
destroy(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The createWsServerSimple function creates a WsServerSimple that facilitates
|
|
86
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
87
|
+
*
|
|
88
|
+
* This should be run in a server environment, and you must pass in a configured
|
|
89
|
+
* WebSocketServer object in order to create it.
|
|
90
|
+
*
|
|
91
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
92
|
+
* the complications of listeners, persistence, or statistics. This makes it
|
|
93
|
+
* more suitable to be used as a reference implementation for other server
|
|
94
|
+
* environments.
|
|
95
|
+
* @param webSocketServer A WebSocketServer object from your server environment.
|
|
96
|
+
* @returns A reference to the new WsServerSimple object.
|
|
97
|
+
* @example
|
|
98
|
+
* This example creates a WsServerSimple that synchronizes two clients on a
|
|
99
|
+
* shared path.
|
|
100
|
+
*
|
|
101
|
+
* ```js
|
|
102
|
+
* import {WebSocketServer} from 'ws';
|
|
103
|
+
* import {createMergeableStore} from 'tinybase';
|
|
104
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
105
|
+
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
106
|
+
*
|
|
107
|
+
* // Server
|
|
108
|
+
* const server = createWsServerSimple(new WebSocketServer({port: 8053}));
|
|
109
|
+
*
|
|
110
|
+
* // Client 1
|
|
111
|
+
* const clientStore1 = createMergeableStore();
|
|
112
|
+
* clientStore1.setCell('pets', 'fido', 'species', 'dog');
|
|
113
|
+
* const synchronizer1 = await createWsSynchronizer(
|
|
114
|
+
* clientStore1,
|
|
115
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
116
|
+
* );
|
|
117
|
+
* await synchronizer1.startSync();
|
|
118
|
+
* // ...
|
|
119
|
+
*
|
|
120
|
+
* // Client 2
|
|
121
|
+
* const clientStore2 = createMergeableStore();
|
|
122
|
+
* clientStore2.setCell('pets', 'felix', 'species', 'cat');
|
|
123
|
+
* const synchronizer2 = await createWsSynchronizer(
|
|
124
|
+
* clientStore2,
|
|
125
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
126
|
+
* );
|
|
127
|
+
* await synchronizer2.startSync();
|
|
128
|
+
* // ...
|
|
129
|
+
*
|
|
130
|
+
* console.log(clientStore1.getTables());
|
|
131
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
132
|
+
*
|
|
133
|
+
* console.log(clientStore2.getTables());
|
|
134
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
135
|
+
*
|
|
136
|
+
* synchronizer1.destroy();
|
|
137
|
+
* synchronizer2.destroy();
|
|
138
|
+
* server.destroy();
|
|
139
|
+
* ```
|
|
140
|
+
* @category Creation
|
|
141
|
+
* @since v5.4.0
|
|
142
|
+
*/
|
|
143
|
+
export function createWsServerSimple(
|
|
144
|
+
webSocketServer: WebSocketServer,
|
|
145
|
+
): WsServerSimple;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The synchronizer-ws-server-simple module of the TinyBase project lets you
|
|
3
|
+
* create a server that facilitates synchronization between clients, without the
|
|
4
|
+
* complications of listeners, persistence, or statistics.
|
|
5
|
+
*
|
|
6
|
+
* This makes it more suitable to be used as a reference implementation for
|
|
7
|
+
* other server environments.
|
|
8
|
+
* @see Synchronization guide
|
|
9
|
+
* @see Todo App v6 (collaboration) demo
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
* @module synchronizer-ws-server-simple
|
|
12
|
+
* @since v5.4.0
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type {WebSocketServer} from 'ws';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The WsServerSimple interface represents an object that facilitates
|
|
19
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
20
|
+
*
|
|
21
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
22
|
+
* the complications of listeners, persistence, or statistics.
|
|
23
|
+
*
|
|
24
|
+
* You should use the createWsServerSimple function to create a WsServerSimple
|
|
25
|
+
* object.
|
|
26
|
+
* @category Server
|
|
27
|
+
* @since v5.4.0
|
|
28
|
+
*/
|
|
29
|
+
export interface WsServerSimple {
|
|
30
|
+
/**
|
|
31
|
+
* The getWebSocketServer method returns a reference to the WebSocketServer
|
|
32
|
+
* being used for this WsServerSimple.
|
|
33
|
+
* @returns The WebSocketServer reference.
|
|
34
|
+
* @example
|
|
35
|
+
* This example creates a WsServerSimple and then gets the WebSocketServer
|
|
36
|
+
* reference back out again.
|
|
37
|
+
*
|
|
38
|
+
* ```js
|
|
39
|
+
* import {WebSocketServer} from 'ws';
|
|
40
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
41
|
+
*
|
|
42
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
43
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
44
|
+
*
|
|
45
|
+
* console.log(server.getWebSocketServer() == webSocketServer);
|
|
46
|
+
* // -> true
|
|
47
|
+
*
|
|
48
|
+
* server.destroy();
|
|
49
|
+
* ```
|
|
50
|
+
* @category Getter
|
|
51
|
+
* @since v5.4.0
|
|
52
|
+
*/
|
|
53
|
+
getWebSocketServer(): WebSocketServer;
|
|
54
|
+
/**
|
|
55
|
+
* The destroy method provides a way to clean up the server at the end of its
|
|
56
|
+
* use.
|
|
57
|
+
*
|
|
58
|
+
* This closes the underlying WebSocketServer that was provided when the
|
|
59
|
+
* WsServerSimple was created.
|
|
60
|
+
* @example
|
|
61
|
+
* This example creates a WsServerSimple and then destroys it again, closing
|
|
62
|
+
* the underlying WebSocketServer.
|
|
63
|
+
*
|
|
64
|
+
* ```js
|
|
65
|
+
* import {WebSocketServer} from 'ws';
|
|
66
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
67
|
+
*
|
|
68
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
69
|
+
* webSocketServer.on('close', () => {
|
|
70
|
+
* console.log('WebSocketServer closed');
|
|
71
|
+
* });
|
|
72
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
73
|
+
*
|
|
74
|
+
* server.destroy();
|
|
75
|
+
* // ...
|
|
76
|
+
* // -> 'WebSocketServer closed'
|
|
77
|
+
* ```
|
|
78
|
+
* @category Getter
|
|
79
|
+
* @since v5.4.0
|
|
80
|
+
*/
|
|
81
|
+
destroy(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The createWsServerSimple function creates a WsServerSimple that facilitates
|
|
86
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
87
|
+
*
|
|
88
|
+
* This should be run in a server environment, and you must pass in a configured
|
|
89
|
+
* WebSocketServer object in order to create it.
|
|
90
|
+
*
|
|
91
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
92
|
+
* the complications of listeners, persistence, or statistics. This makes it
|
|
93
|
+
* more suitable to be used as a reference implementation for other server
|
|
94
|
+
* environments.
|
|
95
|
+
* @param webSocketServer A WebSocketServer object from your server environment.
|
|
96
|
+
* @returns A reference to the new WsServerSimple object.
|
|
97
|
+
* @example
|
|
98
|
+
* This example creates a WsServerSimple that synchronizes two clients on a
|
|
99
|
+
* shared path.
|
|
100
|
+
*
|
|
101
|
+
* ```js
|
|
102
|
+
* import {WebSocketServer} from 'ws';
|
|
103
|
+
* import {createMergeableStore} from 'tinybase';
|
|
104
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
105
|
+
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
106
|
+
*
|
|
107
|
+
* // Server
|
|
108
|
+
* const server = createWsServerSimple(new WebSocketServer({port: 8053}));
|
|
109
|
+
*
|
|
110
|
+
* // Client 1
|
|
111
|
+
* const clientStore1 = createMergeableStore();
|
|
112
|
+
* clientStore1.setCell('pets', 'fido', 'species', 'dog');
|
|
113
|
+
* const synchronizer1 = await createWsSynchronizer(
|
|
114
|
+
* clientStore1,
|
|
115
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
116
|
+
* );
|
|
117
|
+
* await synchronizer1.startSync();
|
|
118
|
+
* // ...
|
|
119
|
+
*
|
|
120
|
+
* // Client 2
|
|
121
|
+
* const clientStore2 = createMergeableStore();
|
|
122
|
+
* clientStore2.setCell('pets', 'felix', 'species', 'cat');
|
|
123
|
+
* const synchronizer2 = await createWsSynchronizer(
|
|
124
|
+
* clientStore2,
|
|
125
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
126
|
+
* );
|
|
127
|
+
* await synchronizer2.startSync();
|
|
128
|
+
* // ...
|
|
129
|
+
*
|
|
130
|
+
* console.log(clientStore1.getTables());
|
|
131
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
132
|
+
*
|
|
133
|
+
* console.log(clientStore2.getTables());
|
|
134
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
135
|
+
*
|
|
136
|
+
* synchronizer1.destroy();
|
|
137
|
+
* synchronizer2.destroy();
|
|
138
|
+
* server.destroy();
|
|
139
|
+
* ```
|
|
140
|
+
* @category Creation
|
|
141
|
+
* @since v5.4.0
|
|
142
|
+
*/
|
|
143
|
+
export function createWsServerSimple(
|
|
144
|
+
webSocketServer: WebSocketServer,
|
|
145
|
+
): WsServerSimple;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The synchronizer-ws-server-simple module of the TinyBase project lets you
|
|
3
|
+
* create a server that facilitates synchronization between clients, without the
|
|
4
|
+
* complications of listeners, persistence, or statistics.
|
|
5
|
+
*
|
|
6
|
+
* This makes it more suitable to be used as a reference implementation for
|
|
7
|
+
* other server environments.
|
|
8
|
+
* @see Synchronization guide
|
|
9
|
+
* @see Todo App v6 (collaboration) demo
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
* @module synchronizer-ws-server-simple
|
|
12
|
+
* @since v5.4.0
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type {WebSocketServer} from 'ws';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The WsServerSimple interface represents an object that facilitates
|
|
19
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
20
|
+
*
|
|
21
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
22
|
+
* the complications of listeners, persistence, or statistics.
|
|
23
|
+
*
|
|
24
|
+
* You should use the createWsServerSimple function to create a WsServerSimple
|
|
25
|
+
* object.
|
|
26
|
+
* @category Server
|
|
27
|
+
* @since v5.4.0
|
|
28
|
+
*/
|
|
29
|
+
export interface WsServerSimple {
|
|
30
|
+
/**
|
|
31
|
+
* The getWebSocketServer method returns a reference to the WebSocketServer
|
|
32
|
+
* being used for this WsServerSimple.
|
|
33
|
+
* @returns The WebSocketServer reference.
|
|
34
|
+
* @example
|
|
35
|
+
* This example creates a WsServerSimple and then gets the WebSocketServer
|
|
36
|
+
* reference back out again.
|
|
37
|
+
*
|
|
38
|
+
* ```js
|
|
39
|
+
* import {WebSocketServer} from 'ws';
|
|
40
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
41
|
+
*
|
|
42
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
43
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
44
|
+
*
|
|
45
|
+
* console.log(server.getWebSocketServer() == webSocketServer);
|
|
46
|
+
* // -> true
|
|
47
|
+
*
|
|
48
|
+
* server.destroy();
|
|
49
|
+
* ```
|
|
50
|
+
* @category Getter
|
|
51
|
+
* @since v5.4.0
|
|
52
|
+
*/
|
|
53
|
+
getWebSocketServer(): WebSocketServer;
|
|
54
|
+
/**
|
|
55
|
+
* The destroy method provides a way to clean up the server at the end of its
|
|
56
|
+
* use.
|
|
57
|
+
*
|
|
58
|
+
* This closes the underlying WebSocketServer that was provided when the
|
|
59
|
+
* WsServerSimple was created.
|
|
60
|
+
* @example
|
|
61
|
+
* This example creates a WsServerSimple and then destroys it again, closing
|
|
62
|
+
* the underlying WebSocketServer.
|
|
63
|
+
*
|
|
64
|
+
* ```js
|
|
65
|
+
* import {WebSocketServer} from 'ws';
|
|
66
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
67
|
+
*
|
|
68
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
69
|
+
* webSocketServer.on('close', () => {
|
|
70
|
+
* console.log('WebSocketServer closed');
|
|
71
|
+
* });
|
|
72
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
73
|
+
*
|
|
74
|
+
* server.destroy();
|
|
75
|
+
* // ...
|
|
76
|
+
* // -> 'WebSocketServer closed'
|
|
77
|
+
* ```
|
|
78
|
+
* @category Getter
|
|
79
|
+
* @since v5.4.0
|
|
80
|
+
*/
|
|
81
|
+
destroy(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The createWsServerSimple function creates a WsServerSimple that facilitates
|
|
86
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
87
|
+
*
|
|
88
|
+
* This should be run in a server environment, and you must pass in a configured
|
|
89
|
+
* WebSocketServer object in order to create it.
|
|
90
|
+
*
|
|
91
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
92
|
+
* the complications of listeners, persistence, or statistics. This makes it
|
|
93
|
+
* more suitable to be used as a reference implementation for other server
|
|
94
|
+
* environments.
|
|
95
|
+
* @param webSocketServer A WebSocketServer object from your server environment.
|
|
96
|
+
* @returns A reference to the new WsServerSimple object.
|
|
97
|
+
* @example
|
|
98
|
+
* This example creates a WsServerSimple that synchronizes two clients on a
|
|
99
|
+
* shared path.
|
|
100
|
+
*
|
|
101
|
+
* ```js
|
|
102
|
+
* import {WebSocketServer} from 'ws';
|
|
103
|
+
* import {createMergeableStore} from 'tinybase';
|
|
104
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
105
|
+
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
106
|
+
*
|
|
107
|
+
* // Server
|
|
108
|
+
* const server = createWsServerSimple(new WebSocketServer({port: 8053}));
|
|
109
|
+
*
|
|
110
|
+
* // Client 1
|
|
111
|
+
* const clientStore1 = createMergeableStore();
|
|
112
|
+
* clientStore1.setCell('pets', 'fido', 'species', 'dog');
|
|
113
|
+
* const synchronizer1 = await createWsSynchronizer(
|
|
114
|
+
* clientStore1,
|
|
115
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
116
|
+
* );
|
|
117
|
+
* await synchronizer1.startSync();
|
|
118
|
+
* // ...
|
|
119
|
+
*
|
|
120
|
+
* // Client 2
|
|
121
|
+
* const clientStore2 = createMergeableStore();
|
|
122
|
+
* clientStore2.setCell('pets', 'felix', 'species', 'cat');
|
|
123
|
+
* const synchronizer2 = await createWsSynchronizer(
|
|
124
|
+
* clientStore2,
|
|
125
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
126
|
+
* );
|
|
127
|
+
* await synchronizer2.startSync();
|
|
128
|
+
* // ...
|
|
129
|
+
*
|
|
130
|
+
* console.log(clientStore1.getTables());
|
|
131
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
132
|
+
*
|
|
133
|
+
* console.log(clientStore2.getTables());
|
|
134
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
135
|
+
*
|
|
136
|
+
* synchronizer1.destroy();
|
|
137
|
+
* synchronizer2.destroy();
|
|
138
|
+
* server.destroy();
|
|
139
|
+
* ```
|
|
140
|
+
* @category Creation
|
|
141
|
+
* @since v5.4.0
|
|
142
|
+
*/
|
|
143
|
+
export function createWsServerSimple(
|
|
144
|
+
webSocketServer: WebSocketServer,
|
|
145
|
+
): WsServerSimple;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The synchronizer-ws-server-simple module of the TinyBase project lets you
|
|
3
|
+
* create a server that facilitates synchronization between clients, without the
|
|
4
|
+
* complications of listeners, persistence, or statistics.
|
|
5
|
+
*
|
|
6
|
+
* This makes it more suitable to be used as a reference implementation for
|
|
7
|
+
* other server environments.
|
|
8
|
+
* @see Synchronization guide
|
|
9
|
+
* @see Todo App v6 (collaboration) demo
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
* @module synchronizer-ws-server-simple
|
|
12
|
+
* @since v5.4.0
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type {WebSocketServer} from 'ws';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The WsServerSimple interface represents an object that facilitates
|
|
19
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
20
|
+
*
|
|
21
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
22
|
+
* the complications of listeners, persistence, or statistics.
|
|
23
|
+
*
|
|
24
|
+
* You should use the createWsServerSimple function to create a WsServerSimple
|
|
25
|
+
* object.
|
|
26
|
+
* @category Server
|
|
27
|
+
* @since v5.4.0
|
|
28
|
+
*/
|
|
29
|
+
export interface WsServerSimple {
|
|
30
|
+
/**
|
|
31
|
+
* The getWebSocketServer method returns a reference to the WebSocketServer
|
|
32
|
+
* being used for this WsServerSimple.
|
|
33
|
+
* @returns The WebSocketServer reference.
|
|
34
|
+
* @example
|
|
35
|
+
* This example creates a WsServerSimple and then gets the WebSocketServer
|
|
36
|
+
* reference back out again.
|
|
37
|
+
*
|
|
38
|
+
* ```js
|
|
39
|
+
* import {WebSocketServer} from 'ws';
|
|
40
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
41
|
+
*
|
|
42
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
43
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
44
|
+
*
|
|
45
|
+
* console.log(server.getWebSocketServer() == webSocketServer);
|
|
46
|
+
* // -> true
|
|
47
|
+
*
|
|
48
|
+
* server.destroy();
|
|
49
|
+
* ```
|
|
50
|
+
* @category Getter
|
|
51
|
+
* @since v5.4.0
|
|
52
|
+
*/
|
|
53
|
+
getWebSocketServer(): WebSocketServer;
|
|
54
|
+
/**
|
|
55
|
+
* The destroy method provides a way to clean up the server at the end of its
|
|
56
|
+
* use.
|
|
57
|
+
*
|
|
58
|
+
* This closes the underlying WebSocketServer that was provided when the
|
|
59
|
+
* WsServerSimple was created.
|
|
60
|
+
* @example
|
|
61
|
+
* This example creates a WsServerSimple and then destroys it again, closing
|
|
62
|
+
* the underlying WebSocketServer.
|
|
63
|
+
*
|
|
64
|
+
* ```js
|
|
65
|
+
* import {WebSocketServer} from 'ws';
|
|
66
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
67
|
+
*
|
|
68
|
+
* const webSocketServer = new WebSocketServer({port: 8053});
|
|
69
|
+
* webSocketServer.on('close', () => {
|
|
70
|
+
* console.log('WebSocketServer closed');
|
|
71
|
+
* });
|
|
72
|
+
* const server = createWsServerSimple(webSocketServer);
|
|
73
|
+
*
|
|
74
|
+
* server.destroy();
|
|
75
|
+
* // ...
|
|
76
|
+
* // -> 'WebSocketServer closed'
|
|
77
|
+
* ```
|
|
78
|
+
* @category Getter
|
|
79
|
+
* @since v5.4.0
|
|
80
|
+
*/
|
|
81
|
+
destroy(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The createWsServerSimple function creates a WsServerSimple that facilitates
|
|
86
|
+
* synchronization between clients that are using WsSynchronizer instances.
|
|
87
|
+
*
|
|
88
|
+
* This should be run in a server environment, and you must pass in a configured
|
|
89
|
+
* WebSocketServer object in order to create it.
|
|
90
|
+
*
|
|
91
|
+
* The core functionality is equivalent to the WsServer interface, but without
|
|
92
|
+
* the complications of listeners, persistence, or statistics. This makes it
|
|
93
|
+
* more suitable to be used as a reference implementation for other server
|
|
94
|
+
* environments.
|
|
95
|
+
* @param webSocketServer A WebSocketServer object from your server environment.
|
|
96
|
+
* @returns A reference to the new WsServerSimple object.
|
|
97
|
+
* @example
|
|
98
|
+
* This example creates a WsServerSimple that synchronizes two clients on a
|
|
99
|
+
* shared path.
|
|
100
|
+
*
|
|
101
|
+
* ```js
|
|
102
|
+
* import {WebSocketServer} from 'ws';
|
|
103
|
+
* import {createMergeableStore} from 'tinybase';
|
|
104
|
+
* import {createWsServerSimple} from 'tinybase/synchronizers/synchronizer-ws-server-simple';
|
|
105
|
+
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
106
|
+
*
|
|
107
|
+
* // Server
|
|
108
|
+
* const server = createWsServerSimple(new WebSocketServer({port: 8053}));
|
|
109
|
+
*
|
|
110
|
+
* // Client 1
|
|
111
|
+
* const clientStore1 = createMergeableStore();
|
|
112
|
+
* clientStore1.setCell('pets', 'fido', 'species', 'dog');
|
|
113
|
+
* const synchronizer1 = await createWsSynchronizer(
|
|
114
|
+
* clientStore1,
|
|
115
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
116
|
+
* );
|
|
117
|
+
* await synchronizer1.startSync();
|
|
118
|
+
* // ...
|
|
119
|
+
*
|
|
120
|
+
* // Client 2
|
|
121
|
+
* const clientStore2 = createMergeableStore();
|
|
122
|
+
* clientStore2.setCell('pets', 'felix', 'species', 'cat');
|
|
123
|
+
* const synchronizer2 = await createWsSynchronizer(
|
|
124
|
+
* clientStore2,
|
|
125
|
+
* new WebSocket('ws://localhost:8053/petShop'),
|
|
126
|
+
* );
|
|
127
|
+
* await synchronizer2.startSync();
|
|
128
|
+
* // ...
|
|
129
|
+
*
|
|
130
|
+
* console.log(clientStore1.getTables());
|
|
131
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
132
|
+
*
|
|
133
|
+
* console.log(clientStore2.getTables());
|
|
134
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
135
|
+
*
|
|
136
|
+
* synchronizer1.destroy();
|
|
137
|
+
* synchronizer2.destroy();
|
|
138
|
+
* server.destroy();
|
|
139
|
+
* ```
|
|
140
|
+
* @category Creation
|
|
141
|
+
* @since v5.4.0
|
|
142
|
+
*/
|
|
143
|
+
export function createWsServerSimple(
|
|
144
|
+
webSocketServer: WebSocketServer,
|
|
145
|
+
): WsServerSimple;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const e=e=>null==e,n=(n,l,r)=>e(n)?null==r?void 0:r():l(n),l=(e,n,l)=>e.slice(n,l),r=Object.freeze,t=(e,n)=>null==e?void 0:e.delete(n),o=e=>new Map(e),s=(e,n)=>null==e?void 0:e.get(n),c=(n,l,r)=>e(r)?(t(n,l),n):null==n?void 0:n.set(l,r),u=/\/([^?]*)/;exports.createWsServerSimple=a=>{const v=o();return a.on("connection",((r,a)=>{return n((i=a.url,d=u,null==i?void 0:i.match(d)),(([,u])=>n(a.headers["sec-websocket-key"],(n=>{return a=function*(){const a=(h=o,m=d=u,null!=(p=null==(f=i=v)?void 0:f.has(m))&&p||c(i,d,h()),s(i,d));var i,d,h,f,m,p;c(a,n,r),r.on("message",(e=>{(e=>{const r=e.indexOf("\n");-1!==r&&((e,l)=>{var r;const t=((e,n)=>e+"\n"+n)(n,l);""===e?(e=>{((e,n)=>{null==e||e.forEach(n)})(e,((e,l)=>{return r=e,l!==n?r.send(t):0;var r}))})(a):null==(r=s(a,e))||r.send(t)})(l(e,0,r),l(e,r+1))})(e.toString("utf8"))})),r.on("close",(()=>{t(a,n),(n=>e(n)||0==(e=>{var n;return null!=(n=null==e?void 0:e.size)?n:0})(n))(a)&&t(v,u)}))},new Promise(((e,n)=>{var l=e=>{try{t(a.next(e))}catch(e){n(e)}},r=e=>{try{t(a.throw(e))}catch(e){n(e)}},t=n=>n.done?e(n.value):Promise.resolve(n.value).then(l,r);t((a=a.apply(void 0,null)).next())}));var a}))));var i,d})),r({getWebSocketServer:()=>a,destroy:()=>{v.clear(),a.close()}})};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const e=e=>null==e,n=(n,l,r)=>e(n)?null==r?void 0:r():l(n),l=(e,n,l)=>e.slice(n,l),r=Object.freeze,t=(e,n)=>null==e?void 0:e.delete(n),o=e=>new Map(e),s=(e,n)=>null==e?void 0:e.get(n),c=(n,l,r)=>e(r)?(t(n,l),n):null==n?void 0:n.set(l,r),u=/\/([^?]*)/;exports.createWsServerSimple=a=>{const v=o();return a.on("connection",((r,a)=>{return n((i=a.url,d=u,null==i?void 0:i.match(d)),(([,u])=>n(a.headers["sec-websocket-key"],(n=>{return a=function*(){const a=(h=o,m=d=u,null!=(p=null==(f=i=v)?void 0:f.has(m))&&p||c(i,d,h()),s(i,d));var i,d,h,f,m,p;c(a,n,r),r.on("message",(e=>{(e=>{const r=e.indexOf("\n");-1!==r&&((e,l)=>{var r;const t=((e,n)=>e+"\n"+n)(n,l);""===e?(e=>{((e,n)=>{null==e||e.forEach(n)})(e,((e,l)=>{return r=e,l!==n?r.send(t):0;var r}))})(a):null==(r=s(a,e))||r.send(t)})(l(e,0,r),l(e,r+1))})(e.toString("utf8"))})),r.on("close",(()=>{t(a,n),(n=>e(n)||0==(e=>{var n;return null!=(n=null==e?void 0:e.size)?n:0})(n))(a)&&t(v,u)}))},new Promise(((e,n)=>{var l=e=>{try{t(a.next(e))}catch(e){n(e)}},r=e=>{try{t(a.throw(e))}catch(e){n(e)}},t=n=>n.done?e(n.value):Promise.resolve(n.value).then(l,r);t((a=a.apply(void 0,null)).next())}));var a}))));var i,d})),r({getWebSocketServer:()=>a,destroy:()=>{v.clear(),a.close()}})};
|