psionic 0.2.0 → 0.3.0

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 (70) hide show
  1. package/adapters/reconnect.d.mts +10 -0
  2. package/adapters/reconnect.mjs +42 -0
  3. package/adapters/reconnect.mjs.map +1 -0
  4. package/adapters/resend.d.mts +13 -0
  5. package/adapters/resend.mjs +29 -0
  6. package/adapters/resend.mjs.map +1 -0
  7. package/adapters/socket.d.mts +14 -0
  8. package/adapters/socket.mjs +160 -0
  9. package/adapters/socket.mjs.map +1 -0
  10. package/adapters/websocket.d.mts +20 -0
  11. package/adapters/websocket.mjs +124 -0
  12. package/adapters/websocket.mjs.map +1 -0
  13. package/adapters/webworker.d.mts +8 -0
  14. package/adapters/webworker.mjs +42 -0
  15. package/adapters/webworker.mjs.map +1 -0
  16. package/codecs/cbor.d.mts +12 -0
  17. package/codecs/cbor.mjs +198 -0
  18. package/codecs/cbor.mjs.map +1 -0
  19. package/codecs/psionic.d.mts +12 -0
  20. package/codecs/psionic.mjs +57 -0
  21. package/codecs/psionic.mjs.map +1 -0
  22. package/package.json +44 -19
  23. package/psionic.d.mts +203 -0
  24. package/psionic.mjs +671 -0
  25. package/psionic.mjs.map +1 -0
  26. package/session.d.mts +63 -0
  27. package/session.mjs +92 -0
  28. package/session.mjs.map +1 -0
  29. package/utils/react-hooks.d.mts +24 -0
  30. package/utils/react-hooks.mjs +37 -0
  31. package/utils/react-hooks.mjs.map +1 -0
  32. package/README.md +0 -331
  33. package/examples/.babelrc +0 -4
  34. package/examples/browser/.babelrc +0 -4
  35. package/examples/browser/README.md +0 -14
  36. package/examples/browser/client.js +0 -73
  37. package/examples/browser/index.html +0 -10
  38. package/examples/browser/package.json +0 -28
  39. package/examples/browser/server.js +0 -33
  40. package/examples/browser/testdataset.json +0 -1
  41. package/examples/browser/webpack.config.js +0 -16
  42. package/examples/browser/webpack.devserver.js +0 -12
  43. package/examples/simple-socket/.babelrc +0 -3
  44. package/examples/simple-socket/README.md +0 -8
  45. package/examples/simple-socket/client.js +0 -12
  46. package/examples/simple-socket/package.json +0 -17
  47. package/examples/simple-socket/server.js +0 -15
  48. package/examples/socket-server/.babelrc +0 -3
  49. package/examples/socket-server/README.md +0 -8
  50. package/examples/socket-server/client.js +0 -34
  51. package/examples/socket-server/package.json +0 -17
  52. package/examples/socket-server/server.js +0 -41
  53. package/lib/browser.js +0 -7
  54. package/lib/index.js +0 -7
  55. package/lib/shared.js +0 -155
  56. package/lib/socket/client.js +0 -46
  57. package/lib/socket/index.js +0 -3
  58. package/lib/socket/server.js +0 -51
  59. package/lib/socket.io/browser.client.js +0 -63
  60. package/lib/socket.io/browser.index.js +0 -2
  61. package/lib/socket.io/index.js +0 -2
  62. package/lib/socket.io/package.json +0 -4
  63. package/lib/socket.io/server.js +0 -42
  64. package/lib/websocket/browser.client.js +0 -43
  65. package/lib/websocket/browser.index.js +0 -2
  66. package/lib/websocket/client.js +0 -57
  67. package/lib/websocket/index.js +0 -3
  68. package/lib/websocket/package.json +0 -4
  69. package/lib/websocket/server.js +0 -41
  70. package/webpack.config.js +0 -15
package/README.md DELETED
@@ -1,331 +0,0 @@
1
- # psionic rpc
2
-
3
- not stable.
4
-
5
- psionic is a bidirectional rpc system for node, glued together with json-rpc and a promise-based workflow.
6
-
7
- ## getting started
8
-
9
- to create a websocket-server (using [ws](https://github.com/websockets/ws)):
10
-
11
- > all these examples use `babel-node`, [see the examples folder for config.](https://github.com/examples/simple-socket)
12
- > using babel is optional, but it does make working with promises nicer.
13
-
14
- ``` js
15
- // server
16
- import psionic from 'psionic';
17
-
18
- psionic.webSocket.createServer({ port: 3000 }, function (client) {
19
- // you have to call 'describe' once for the client to start.
20
- client.describe({
21
- factor: 'doubling'
22
- // functions can return promises if needed,
23
- // they'll always be promises for the caller.
24
- multiply(x) { return x * 2; }
25
- });
26
- });
27
- ```
28
-
29
- and a corresponding client:
30
-
31
- ``` js
32
- // client
33
- import psionic from 'psionic';
34
-
35
- (async function () {
36
- let client = await psionic.webSocket.connect('ws://localhost:3000');
37
- let doubled = await client.multiply(5);
38
- console.log(client.factor + '5 gives ' + doubled + '! amazing!');
39
- // doubling 5 gives 10! amazing!
40
- })().catch(ex => console.error(ex.stack));
41
- ```
42
-
43
- ## server to client communication
44
-
45
- the client can also call describe to send state to the server.
46
-
47
- ``` js
48
- // client
49
- import psionic from 'psionic';
50
-
51
- (async function () {
52
- let client = await psionic.webSocket.connect('ws://localhost:3000', {
53
- // using connect options, your state will be sent right as the server socket is created.
54
- describe: { name: "joe" }
55
- });
56
-
57
- let result = await client.test("jdp");
58
- if (!result) {
59
- // you can also call describe later to send state whenever needed,
60
- // which gives a promise to be sure it makes it to the server.
61
- await client.describe({
62
- name: "jdp",
63
- welcome(x) { console.log('Server says: ' + x); }
64
- });
65
- }
66
-
67
- console.log(await client.test("jdp"));
68
- })().catch(ex => console.error(ex.stack));
69
- ```
70
-
71
- corresponding server:
72
-
73
- ``` js
74
- // server
75
- import psionic from 'psionic';
76
-
77
- psionic.webSocket.createServer({ port: 3000 }, function (client) {
78
- // the 'client' objects data is sent from the client,
79
- // it is user input and can't be trusted.
80
- let name = client.name; // joe
81
-
82
- client.describe({
83
- async test(testName) {
84
- if (client.welcome instanceof Function) {
85
- await client.welcome('Welcome, ' + client.name);
86
- }
87
- return testName === client.name;
88
- }
89
- });
90
- });
91
- ```
92
-
93
- ## emitting events
94
-
95
- if you need ad-hoc message passing, you can use the event emitter:
96
-
97
- ``` js
98
- // server
99
- import psionic from 'psionic';
100
-
101
- psionic.webSocket.createServer({ port: 3000 }, function (client) {
102
- client.describe({}); // describe still has to be called.
103
- var pings = 0;
104
-
105
- client.events.on('pong', function (i) {
106
- console.log('client sent a pong: ' + i);
107
- });
108
-
109
- setInterval(function () {
110
- client.emit('ping', ++pings);
111
- }, 1000);
112
- });
113
- ```
114
-
115
- and a corresponding client:
116
-
117
- ``` js
118
- // client
119
- import psionic from 'psionic';
120
-
121
- (async function () {
122
- let client = await psionic.webSocket.connect('ws://localhost:3000');
123
-
124
- client.events.on('ping', function (i) {
125
- console.log(i + ' pings since we connected');
126
- // the client can trigger events on the server, as well.
127
- client.emit('pong', i);
128
- });
129
- })().catch(ex => console.error(ex.stack));
130
- ```
131
-
132
- # communication protocols
133
-
134
- So far there is no implementation with fallbacks (using say, socket.io or primus,) so each server/client pair is specific to a single protocol.
135
-
136
- ## socket
137
-
138
- Plain [Node.js net sockets](https://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener).
139
-
140
- It can be accessed using `require('psionic').socket`, or `require('psionic/lib/socket')`
141
-
142
- ``` js
143
- // server
144
- import psionic from 'psionic';
145
-
146
- // all server options are passed to net.createServer
147
- // if a `port` is passed, net.listen will be called during construction.
148
- var opts = { port: 9000 };
149
-
150
- var server = psionic.socket.createServer(opts, function (client) { });
151
-
152
- // createServer returns the underlying net server
153
- server.listen(9000);
154
-
155
- // client
156
- var promise = psionic.socket.connect(
157
- // the first argument is passed to net.connect
158
- { port: 9000 },
159
-
160
- // the second argument is used to configure the psionic client.
161
- { describe: {} }
162
- );
163
-
164
- promise.then(function (client) {
165
- // you can get the underlying socket after connecting.
166
- // note that the client auto-reconnects, so this can change.
167
- var underlyingSocket = client.state.socket;
168
- });
169
- ```
170
-
171
- ## websocket
172
-
173
- Websockets are created using the [websockets/ws](https://github.com/websockets/ws) library, or natively on the browser.
174
-
175
- It can be accessed using `require('psionic').webSocket`, or `require('psionic/lib/websocket')`
176
-
177
- ``` js
178
- // server
179
- import psionic from 'psionic';
180
-
181
- // all server options are passed to the ws.Server constructor
182
- var opts = { port: 9000 };
183
-
184
- var server = psionic.webSocket.createServer(opts, function (client) { });
185
-
186
- // createServer returns the underlying ws server
187
- server.listen(9000);
188
-
189
- // client
190
- var promise = psionic.webSocket.connect(
191
- // the first argument is passed to the ws WebSocket constructor
192
- 'ws://127.0.0.1:9000',
193
-
194
- // the second argument is used to configure the psionic client.
195
- { describe: {} }
196
- );
197
-
198
- promise.then(function (client) {
199
- // you can get the underlying WebSocket after connecting.
200
- // note that the client auto-reconnects, so this can change.
201
- var underlyingSocket = client.state.socket;
202
- });
203
- ```
204
-
205
- ### browser support
206
-
207
- The WebSocket protocol does not use any polyfills at this time, so it's limited to IE10+. If that's not a problem, you can `require('psionic')` with browserify or webpack and use `psionic.webSocket.connect`.
208
-
209
-
210
- # client object
211
-
212
- The client object is used on both the server- and client-side to send and receive messages.
213
-
214
- ``` js
215
- // Default client structure
216
- client = {
217
- events: EventEmitter,
218
- state: EventEmitter + {
219
- connected: true,
220
-
221
- // callId stores the id of the previous rpc-call,
222
- // this is incremented for every call.
223
- callId: 0,
224
-
225
- // describe is a reference to the functions that
226
- // can be called from the remote. it is replaced
227
- // by calling `client.describe`.
228
- describe: { ... },
229
-
230
- // @emit is called when the remote triggers an event.
231
- "@emit": Function
232
-
233
- // @describe is called when the remote is replacing its description.
234
- "@describe": Function
235
- },
236
-
237
- // the describe function calls @describe on the remote,
238
- // to tell it which functions are available on this client.
239
- describe: Function,
240
-
241
- // emit calls @emit on the remote, which triggers an event.
242
- emit: Function
243
-
244
- // any other functions that are added using the describe function
245
- // on the remote are found on this object as well.
246
- }
247
- ```
248
-
249
- ### client.state events
250
-
251
- Client.state has a few events that can be used to detect changes in the connection.
252
-
253
- - `open` - connected, but no service description has been receieved
254
- - `describe` - an updated service description has been received
255
- - `connect` - connected, and a service description has been received
256
- - `send` - a message is ready to be sent to the remote
257
- - `message` - a message has been received from the remote
258
- - `result:{id}` - a remote procedure call has returned a value
259
- - `disconnect` - the transport has disconnect, but might reconnect
260
- - `close` - the transport is shutting down, and will not reconnect
261
-
262
- ### client.events events
263
-
264
- Client.events is only triggered by calling `Client.emit(name, args)`, and can have any event names.
265
-
266
- ### creation options
267
-
268
- When connecting to a server using `socket.connect` or `webSocket.connect`,
269
- you can supply options (well, option..) to configure the client.
270
-
271
- - `describe` - state object that is sent to the server when connecting, can be used to pass state needed for initializing the remote service.
272
-
273
- # messaging protocol
274
-
275
- Psionic uses [JSON-RPC](http://json-rpc.org/wiki/specification) messages, with single line JSON. It does not support notification requests at this time, all requests must be responded to.
276
-
277
- #### example messages
278
-
279
- In order to begin communicating, both client and server must send their description.
280
- The client starts this, by calling the "@describe" function.
281
-
282
- <div style="font-size:10px;margin-bottom:-8px;height:10px;">(client to server)</div>
283
-
284
- ```json
285
- {"id":1,"name":"@describe","args":[{"test":"psionic!function","example":"value"}]}
286
- ````
287
-
288
- This tells the server to create an rpc-function called "test", and an extra value to add to the `client` object. The object can be arbitrarily nested.. When the server is ready, it responds to the message and sends its own description.
289
-
290
- <div style="font-size:10px;margin-bottom:-8px;height:10px;">(server to client)</div>
291
-
292
- ```json
293
- {"id":1}
294
- {"id":5,"name":"@describe","args":[{"login":"psionic!function"}]}
295
- ```
296
-
297
- The client then responds to the message, and calls its login function.
298
-
299
- <div style="font-size:10px;margin-bottom:-8px;height:10px;">(client to server)</div>
300
-
301
- ```json
302
- {"id":5}
303
- {"id":2,"name":"logim","args":["username","password"]}
304
- ```
305
-
306
- After the server is done processing the request, it will respond with its result:
307
-
308
- <div style="font-size:10px;margin-bottom:-8px;height:10px;">(server to client)</div>
309
-
310
- ```json
311
- {"id":2,"error":{"code":-32601,"message":"Function not found: logim"}}
312
- ```
313
-
314
- Different error codes are sent based on the JSON-RPC spec. `code` and `friendlyMessage` are used from any thrown Error objects. If no friendlyMessage is found, "Unhandled error" will be used. Let's try that again..
315
-
316
- <div style="font-size:10px;margin-bottom:-8px;height:10px;">(client to server)</div>
317
-
318
- ```json
319
- {"id":3,"name":"login","args":["username","password"]}
320
- ```
321
-
322
- There we go, everything's spelled right, now we'll get a response:
323
-
324
- <div style="font-size:10px;margin-bottom:-8px;height:10px;">(server to client)</div>
325
-
326
- ```json
327
- {"id":3,"result":true}
328
- ```
329
-
330
- And that's it!
331
-
package/examples/.babelrc DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "presets": ["es2015", "react", "stage-1"],
3
- "plugins": ["transform-runtime"]
4
- }
@@ -1,4 +0,0 @@
1
- {
2
- "presets": ["es2015", "react", "stage-1"],
3
- "plugins": ["transform-runtime"]
4
- }
@@ -1,14 +0,0 @@
1
- # Websocket example
2
-
3
- This example sets up a small webserver, and connects a react component to a psionic backend.
4
-
5
- ## Running
6
-
7
- babel-node server.js
8
-
9
- Then navigate a browser to http://localhost:3000/
10
-
11
- ## Data
12
-
13
- The dataset used can be found on [data.gov](http://catalog.data.gov/dataset/the-white-house-nominations-appointments-new).
14
-
@@ -1,73 +0,0 @@
1
- // importing the browser bundle exposes a 'Psionic' global
2
- // import '../../dist/psionic.min.js';
3
-
4
- import psionic from '../..'; // import psionic from 'psionic';
5
- import React from 'react';
6
- import ReactDOM from 'react-dom';
7
-
8
- class App extends React.Component {
9
- async componentWillMount() { await this.search(); }
10
- setPage(page) { this.setState({ page }); }
11
- setSort(sort) { this.setState({ sort, page: 0 }); }
12
-
13
- async search() {
14
- let state = this.state || { };
15
- let results = await this.props.client.Nominations.query({
16
- page: state.page,
17
- sort: state.sort
18
- });
19
- this.setState({ results });
20
- }
21
-
22
- async componentDidUpdate(prevProps, prevState) {
23
- if (prevState && (
24
- (prevState.page !== this.state.page) ||
25
- (prevState.sort !== this.state.sort)
26
- )) {
27
- await this.search();
28
- }
29
- }
30
-
31
- render() {
32
- if (!this.state) return <div />;
33
- let page = this.state.results.page, max = this.state.results.totalPages;
34
- let prevPage = <li onClick={e => this.setPage(page - 1)} className="previous"><a>Back</a></li>;
35
- let nextPage = <li onClick={e => this.setPage(page + 1)} className="next"><a>Next</a></li>;
36
-
37
- return <div className="container">
38
- <ul className="pager">
39
- { page > 0 && prevPage }
40
- <span>Page {page + 1} of {max + 1}</span>
41
- { page < max && nextPage }
42
- </ul>
43
- <table className="table table-bordered table-striped">
44
- <thead>
45
- <tr>
46
- <th onClick={e => this.setSort('name')}>Name</th>
47
- <th onClick={e => this.setSort('position_title')}>Position</th>
48
- <th onClick={e => this.setSort('agency_name')}>Agency</th>
49
- </tr>
50
- </thead>
51
- <tbody>
52
- {this.state.results.items.map(p => (
53
- <tr key={p[':sid']}>
54
- <td className="col-sm-4">{p.name}</td>
55
- <td className="col-sm-4">{p.position_title}</td>
56
- <td className="col-sm-4">{p.agency_name}</td>
57
- </tr>
58
- ))}
59
- </tbody>
60
- </table>
61
- </div>
62
- }
63
- }
64
-
65
- (async function run() {
66
- let client = await psionic.webSocket.connect('ws://' + location.host, {
67
- describe: { token: 'hello' }
68
- });
69
-
70
- window.client = client;
71
- ReactDOM.render(<App client={client} />, document.getElementById('root'));
72
- })()
73
- .catch(ex => console.error(ex.stack));
@@ -1,10 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" />
5
- </head>
6
- <body>
7
- <div id="root"></div>
8
- <script src="/client.js"></script>
9
- </body>
10
- </html>
@@ -1,28 +0,0 @@
1
- {
2
- "name": "examples",
3
- "version": "1.0.0",
4
- "private": true,
5
- "description": "",
6
- "main": "index.js",
7
- "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1"
9
- },
10
- "author": "",
11
- "license": "ISC",
12
- "devDependencies": {
13
- "babel-core": "^6.4.0",
14
- "babel-loader": "^6.2.1",
15
- "babel-plugin-transform-runtime": "^6.4.3",
16
- "babel-preset-es2015": "^6.3.13",
17
- "babel-preset-react": "^6.3.13",
18
- "babel-preset-stage-1": "^6.3.13",
19
- "es6-promise": "^3.0.2",
20
- "express": "^4.13.3",
21
- "lodash": "^4.0.0",
22
- "react": "^0.14.6",
23
- "react-dom": "^0.14.6",
24
- "webpack": "^1.12.11",
25
- "webpack-dev-middleware": "^1.4.0"
26
- }
27
- }
28
-
@@ -1,33 +0,0 @@
1
- // to run the examples, you need babel-cli:
2
- // $ npm i -g babel-cli
3
- // $ babel-node server.js
4
-
5
- var httpd = require('./webpack.devserver');
6
- var psionic = require('../../lib/websocket');
7
- var _ = require('lodash');
8
-
9
- var dataset = require('./testdataset.json');
10
-
11
- let Nominations = {
12
- query: function (opts) {
13
- opts = Object.assign({ page: 0, pageSize: 25, sort: 'name' }, opts);
14
- var items = _.sortBy(dataset, opts.sort);
15
- var page = {
16
- page: opts.page,
17
- pageSize: opts.pageSize,
18
- totalPages: Math.floor(dataset.length / opts.pageSize)
19
- };
20
- page.items = items.slice(opts.page * opts.pageSize, (opts.page + 1) * opts.pageSize);
21
- return page;
22
- }
23
- };
24
-
25
- // WS.createServer accepts an http server, or port/host options to
26
- // start a standalone server.
27
- psionic.createServer({ server: httpd }, function (client) {
28
- if (!client.token || client.token.length !== 5) {
29
- return client.describe({ failed: true });
30
- }
31
-
32
- client.describe({ Nominations });
33
- });