iosignal-cli 1.7.0 → 2.0.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.
package/bin/io-client.js CHANGED
@@ -1,396 +1,396 @@
1
- #!/usr/bin/env node
2
-
3
- import { IO, IOCongSocket, ENC_MODE, serverOption } from 'iosignal'
4
- import { EventEmitter } from 'events'
5
- import readline from 'readline'
6
- import tty from 'tty'
7
- import { program } from 'commander'
8
- import { version } from './getVersion.js'
9
-
10
- class Console extends EventEmitter {
11
- constructor() {
12
- super()
13
-
14
- this.showIncommingMessage = true
15
- this.stdin = process.stdin
16
- this.stdout = process.stdout
17
- this.stderr = process.stderr
18
-
19
- this.readlineInterface = readline.createInterface(this.stdin, this.stdout)
20
-
21
- this.readlineInterface
22
- .on('line', (data) => {
23
- this.emit('line', data)
24
- })
25
- .on('close', () => {
26
- this.emit('close')
27
- })
28
-
29
- this._resetInput = () => {
30
- this.clear()
31
- }
32
- }
33
-
34
- static get Colors() {
35
- return {
36
- Red: '\u001b[31m',
37
- Green: '\u001b[32m',
38
- Yellow: '\u001b[33m',
39
- Blue: '\u001b[34m',
40
- Default: '\u001b[39m'
41
- }
42
- }
43
-
44
- static get Types() {
45
- return {
46
- Incoming: '< ',
47
- Control: '',
48
- Error: 'error: ',
49
- Put: '.'
50
- }
51
- }
52
-
53
- prompt() {
54
- this.readlineInterface.prompt(true)
55
- }
56
-
57
- print(type, msg, color) {
58
- if (tty.isatty(1)) {
59
- this.clear()
60
- this.stdout.write(color + type + msg + Console.Colors.Default + '\n')
61
- this.prompt()
62
- } else if (type === Console.Types.Incoming) {
63
- this.stdout.write(msg + '\n')
64
- } else if (type === Console.Types.Error) {
65
- this.stderr.write(type + msg + '\n')
66
- } else if (type === Console.Types.Put) {
67
- // this.stdout.write( '.' )
68
- } else {
69
- // is a control message and we're not in a tty... drop it.
70
- }
71
- }
72
-
73
- clear() {
74
- if (tty.isatty(1)) {
75
- this.stdout.write('\r\u001b[2K\u001b[3D')
76
- }
77
- }
78
-
79
- pause() {
80
- this.stdin.on('keypress', this._resetInput)
81
- }
82
-
83
- resume() {
84
- this.stdin.removeListener('keypress', this._resetInput)
85
- }
86
- }
87
-
88
- function noop() { }
89
-
90
- program
91
- .version(version)
92
- .usage('[options] (--connect <url> )')
93
- .option('-t, --timeout <milliseconds>', 'ping period & timeout')
94
- .option('-c, --connect <url>', 'connect to a server')
95
- .option('-i, --id <id>', 'userId')
96
- .option('-k, --key <key>', 'userKey')
97
- .option('-a, --auth-idKey <idkey>', 'auth id.key')
98
- .option('-j, --join-channel <channelName>', 'join to channel')
99
- .parse(process.argv)
100
-
101
- const options = program.opts()
102
- const defaultWebSocketPort = 7777;
103
-
104
- console.log(options)
105
-
106
- if (!options.connect) {
107
- options.connect = 'localhost:' + defaultWebSocketPort;
108
- }
109
-
110
-
111
- const wsConsole = new Console()
112
-
113
- let connectUrl = options.connect
114
- // console.log('connectUrl raw', connectUrl )
115
-
116
- let io;
117
-
118
-
119
- if (connectUrl.indexOf('cong') === 0) {
120
- //use TCP connection
121
- io = new IOCongSocket(connectUrl)
122
- } else { // ws connection
123
- if (!connectUrl.match(/\w+:\/\/.*$/i)) {
124
- connectUrl = `ws://${connectUrl}`
125
- }
126
- io = new IO(connectUrl)
127
- }
128
-
129
- if (options.id && options.key) {
130
- io.auth(options.id, options.key)
131
- } else if (options.authIdKey) {
132
- io.auth(options.authIdKey)
133
- }
134
-
135
-
136
- io.listen('@', (...args) => {
137
- console.log('rcv @', args)
138
- })
139
-
140
- if (options.joinChannel) {
141
- console.log('options joinchannel', options.joinChannel)
142
- io.channels.add(options.joinChannel)
143
-
144
- }
145
-
146
-
147
- wsConsole.print(Console.Types.Control, `Connecting to ${connectUrl}`, Console.Colors.Yellow)
148
-
149
- io.on('@pong', (...data) => {
150
- wsConsole.print(
151
- Console.Types.Control,
152
- `>> receive pong from ${data[0]}`,
153
- Console.Colors.Yellow)
154
- })
155
-
156
- io.on('ready', () => {
157
- wsConsole.print(
158
- Console.Types.Control,
159
- `ready: cid: ${io.cid}`,
160
- Console.Colors.Green)
161
- })
162
-
163
- io.on('close', () => {
164
- wsConsole.print(Console.Types.Control, `closed`, Console.Colors.Yellow)
165
- })
166
-
167
-
168
- io.on('authorized', () => {
169
- wsConsole.print(
170
- Console.Types.Control,
171
- `Boho authorized. TLS: ${io.TLS}`,
172
- Console.Colors.Yellow)
173
- })
174
-
175
- io.on('auth_fail', () => {
176
- wsConsole.print(
177
- Console.Types.Control,
178
- `Boho auth_fail.`,
179
- Console.Colors.Yellow)
180
- })
181
- io.on('over_size', () => {
182
- wsConsole.print(
183
- Console.Types.Control,
184
- `OVER_SIZE: your signalSize limit is: ${io.quota.signalSize}`,
185
- Console.Colors.Yellow)
186
- })
187
-
188
-
189
- io.on('message', (data, isBinary) => {
190
-
191
- let moreBytesIndicator = ""
192
- if (isBinary) {
193
- let buffer
194
- const displayBufferLimit = 20
195
- if (data.byteLength > displayBufferLimit) {
196
- buffer = Buffer.from(data, 0, displayBufferLimit);
197
- moreBytesIndicator = "..."
198
- } else {
199
- buffer = Buffer.from(data);
200
- }
201
-
202
- let prn = `${buffer.toString('hex')}${moreBytesIndicator} [${data.byteLength} bytes total]`;
203
- if (wsConsole.showIncommingMessage) wsConsole.print(Console.Types.Incoming, `rx: bin [hex] ${prn}`, Console.Colors.Green)
204
-
205
- } else {
206
- if (wsConsole.showIncommingMessage) wsConsole.print(Console.Types.Incoming, `rx: text: ${data}`, Console.Colors.Green)
207
- }
208
- })
209
-
210
- // to send frame message.
211
- wsConsole.on('line', (data) => {
212
- if (data[0] === '.') {
213
- const toks = data.split(/\s+/)
214
- const cmd = toks[0].substring(1)
215
- let ch = ""
216
- switch (cmd) {
217
- case 'login':
218
- toks.shift()
219
- io.login(...toks)
220
- break;
221
- case 'auth':
222
- toks.shift()
223
- io.auth(...toks)
224
- break;
225
-
226
- case 'encNo':
227
- io.encMode = ENC_MODE.NO
228
- wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
229
- break;
230
- case 'encYes':
231
- io.encMode = ENC_MODE.YES
232
- wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
233
- break;
234
- case 'encAuto':
235
- io.encMode = ENC_MODE.AUTO
236
- wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
237
- break;
238
- case 'encMode':
239
- wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
240
- break;
241
-
242
- case 'echo':
243
- io.echo(toks[1])
244
- break;
245
-
246
- case 'iam':
247
- io.iam(toks[1])
248
- break;
249
-
250
- case 'id':
251
- console.log(`cid: ${io.cid} level: ${io.level}`)
252
- break;
253
-
254
- case 'sudo':
255
- toks.shift()
256
- console.log('sudo toks', toks)
257
- io.req('sudo', ...toks).then(res => {
258
- if (res.ok) {
259
- console.log('>> sudo response:', res.body)
260
- } else {
261
- console.log('>> sudo response:', res.body)
262
- }
263
- }).catch(err => {
264
- console.log('sudo call err', err)
265
- })
266
-
267
- break;
268
-
269
-
270
- case 'quota':
271
- console.log(`quota: ${JSON.stringify(io.quota)}`)
272
- break;
273
-
274
- case 'ch':
275
- let chList = []
276
- io.channels.forEach(v => {
277
- chList.push(v)
278
- })
279
- console.log(`channels: ${chList.toString()}`)
280
- break;
281
-
282
- case 'sig':
283
- case 'signal':
284
- toks.shift()
285
- io.signal(...toks)
286
- break;
287
-
288
- case 'sig_bin':
289
- toks.shift()
290
- let to = toks[0]
291
- let size = parseInt(toks[1])
292
- console.log(`signal tag: ${to} size: ${size}`)
293
- io.signal(to, new Uint8Array(size))
294
- break;
295
-
296
- case 'pub':
297
- case 'publish':
298
- toks.shift()
299
- io.publish(...toks)
300
- break;
301
-
302
- case 'set':
303
- io.set(toks[1])
304
- break;
305
-
306
- case 'api':
307
- case 'req':
308
- toks.shift()
309
- if (toks.length < 2) {
310
- wsConsole.print(
311
- Console.Types.Error,
312
- '[.req need at least two params] req target command [, ..args]',
313
- Console.Colors.Yellow
314
- )
315
- return
316
- }
317
- io.req(...toks).then(result => {
318
- console.log('>> response:', result)
319
- }).catch(e => {
320
- console.log(e)
321
- })
322
- break;
323
-
324
- case 'join':
325
- case 'sub':
326
- case 'subscribe':
327
- case 'listen':
328
- toks.shift()
329
- let tag = toks[0]
330
- console.log('listen tag', tag)
331
- io.listen(tag, (...args) => {
332
- console.log(`subscribe & listen tag: ${tag} args:`, args)
333
-
334
- })
335
- io.subscribe_memory_channels()
336
- break;
337
-
338
-
339
- case 'unsub':
340
- io.unsubscribe(toks[1]);
341
- break;
342
-
343
- case 'pping':
344
- io.signal(toks[1] + "@ping", io.cid)
345
- break;
346
- case 'ping':
347
- io.ping()
348
- break;
349
-
350
- case 'pong':
351
- io.pong()
352
- break
353
-
354
- case 'close':
355
- io.close()
356
- break
357
-
358
- case 'open':
359
- case 'connect':
360
- if (toks[1]) {
361
- let url = toks[1]
362
- if (!url.match(/\w+:\/\/.*$/i)) {
363
- url = `ws://${url}`
364
- }
365
- io.open(url) // new url
366
- } else {
367
- io.open() // last url
368
- }
369
- break
370
- case 'show':
371
- wsConsole.showIncommingMessage = true
372
- break;
373
- case 'hide':
374
- wsConsole.showIncommingMessage = false
375
- break;
376
- case 'quit':
377
- case 'exit':
378
- process.exit();
379
- default:
380
- wsConsole.print(
381
- Console.Types.Error,
382
- 'command list: .signal .sig .listen .publish .pub .subscribe .sub .unsub .ping .pong .id .iam .open .connect .close .login .auth .quit .exit',
383
- Console.Colors.Yellow
384
- )
385
- }
386
- } else {
387
- io.send( data )
388
- }
389
- wsConsole.prompt()
390
- })
391
-
392
-
393
-
394
-
395
-
396
-
1
+ #!/usr/bin/env node
2
+
3
+ import { IOWS, IOCongSocket, ENC_MODE, serverOption } from 'iosignal'
4
+ import { EventEmitter } from 'events'
5
+ import readline from 'readline'
6
+ import tty from 'tty'
7
+ import { program } from 'commander'
8
+ import { version } from './getVersion.js'
9
+
10
+ class Console extends EventEmitter {
11
+ constructor() {
12
+ super()
13
+
14
+ this.showIncommingMessage = true
15
+ this.stdin = process.stdin
16
+ this.stdout = process.stdout
17
+ this.stderr = process.stderr
18
+
19
+ this.readlineInterface = readline.createInterface(this.stdin, this.stdout)
20
+
21
+ this.readlineInterface
22
+ .on('line', (data) => {
23
+ this.emit('line', data)
24
+ })
25
+ .on('close', () => {
26
+ this.emit('close')
27
+ })
28
+
29
+ this._resetInput = () => {
30
+ this.clear()
31
+ }
32
+ }
33
+
34
+ static get Colors() {
35
+ return {
36
+ Red: '\u001b[31m',
37
+ Green: '\u001b[32m',
38
+ Yellow: '\u001b[33m',
39
+ Blue: '\u001b[34m',
40
+ Default: '\u001b[39m'
41
+ }
42
+ }
43
+
44
+ static get Types() {
45
+ return {
46
+ Incoming: '< ',
47
+ Control: '',
48
+ Error: 'error: ',
49
+ Put: '.'
50
+ }
51
+ }
52
+
53
+ prompt() {
54
+ this.readlineInterface.prompt(true)
55
+ }
56
+
57
+ print(type, msg, color) {
58
+ if (tty.isatty(1)) {
59
+ this.clear()
60
+ this.stdout.write(color + type + msg + Console.Colors.Default + '\n')
61
+ this.prompt()
62
+ } else if (type === Console.Types.Incoming) {
63
+ this.stdout.write(msg + '\n')
64
+ } else if (type === Console.Types.Error) {
65
+ this.stderr.write(type + msg + '\n')
66
+ } else if (type === Console.Types.Put) {
67
+ // this.stdout.write( '.' )
68
+ } else {
69
+ // is a control message and we're not in a tty... drop it.
70
+ }
71
+ }
72
+
73
+ clear() {
74
+ if (tty.isatty(1)) {
75
+ this.stdout.write('\r\u001b[2K\u001b[3D')
76
+ }
77
+ }
78
+
79
+ pause() {
80
+ this.stdin.on('keypress', this._resetInput)
81
+ }
82
+
83
+ resume() {
84
+ this.stdin.removeListener('keypress', this._resetInput)
85
+ }
86
+ }
87
+
88
+ function noop() { }
89
+
90
+ program
91
+ .version(version)
92
+ .usage('[options] (--connect <url> )')
93
+ .option('-t, --timeout <milliseconds>', 'ping period & timeout')
94
+ .option('-c, --connect <url>', 'connect to a server')
95
+ .option('-i, --id <id>', 'userId')
96
+ .option('-k, --key <key>', 'userKey')
97
+ .option('-a, --auth-idKey <idkey>', 'auth id.key')
98
+ .option('-j, --join-channel <channelName>', 'join to channel')
99
+ .parse(process.argv)
100
+
101
+ const options = program.opts()
102
+ const defaultWebSocketPort = 7777;
103
+
104
+ console.log(options)
105
+
106
+ if (!options.connect) {
107
+ options.connect = 'localhost:' + defaultWebSocketPort;
108
+ }
109
+
110
+
111
+ const wsConsole = new Console()
112
+
113
+ let connectUrl = options.connect
114
+ // console.log('connectUrl raw', connectUrl )
115
+
116
+ let io;
117
+
118
+
119
+ if (connectUrl.indexOf('cong') === 0) {
120
+ //use TCP connection
121
+ io = new IOCongSocket(connectUrl)
122
+ } else { // ws connection
123
+ if (!connectUrl.match(/\w+:\/\/.*$/i)) {
124
+ connectUrl = `ws://${connectUrl}`
125
+ }
126
+ io = new IOWS(connectUrl)
127
+ }
128
+
129
+ if (options.id && options.key) {
130
+ io.auth(options.id, options.key)
131
+ } else if (options.authIdKey) {
132
+ io.auth(options.authIdKey)
133
+ }
134
+
135
+
136
+ io.listen('@', (...args) => {
137
+ console.log('rcv @', args)
138
+ })
139
+
140
+ if (options.joinChannel) {
141
+ console.log('options joinchannel', options.joinChannel)
142
+ io.channels.add(options.joinChannel)
143
+
144
+ }
145
+
146
+
147
+ wsConsole.print(Console.Types.Control, `Connecting to ${connectUrl}`, Console.Colors.Yellow)
148
+
149
+ io.on('@pong', (...data) => {
150
+ wsConsole.print(
151
+ Console.Types.Control,
152
+ `>> receive pong from ${data[0]}`,
153
+ Console.Colors.Yellow)
154
+ })
155
+
156
+ io.on('ready', () => {
157
+ wsConsole.print(
158
+ Console.Types.Control,
159
+ `ready: cid: ${io.cid}`,
160
+ Console.Colors.Green)
161
+ })
162
+
163
+ io.on('close', () => {
164
+ wsConsole.print(Console.Types.Control, `closed`, Console.Colors.Yellow)
165
+ })
166
+
167
+
168
+ io.on('authorized', () => {
169
+ wsConsole.print(
170
+ Console.Types.Control,
171
+ `Boho authorized. TLS: ${io.TLS}`,
172
+ Console.Colors.Yellow)
173
+ })
174
+
175
+ io.on('auth_fail', () => {
176
+ wsConsole.print(
177
+ Console.Types.Control,
178
+ `Boho auth_fail.`,
179
+ Console.Colors.Yellow)
180
+ })
181
+ io.on('over_size', () => {
182
+ wsConsole.print(
183
+ Console.Types.Control,
184
+ `OVER_SIZE: your signalSize limit is: ${io.quota.signalSize}`,
185
+ Console.Colors.Yellow)
186
+ })
187
+
188
+
189
+ io.on('message', (data, isBinary) => {
190
+
191
+ let moreBytesIndicator = ""
192
+ if (isBinary) {
193
+ let buffer
194
+ const displayBufferLimit = 20
195
+ if (data.byteLength > displayBufferLimit) {
196
+ buffer = Buffer.from(data, 0, displayBufferLimit);
197
+ moreBytesIndicator = "..."
198
+ } else {
199
+ buffer = Buffer.from(data);
200
+ }
201
+
202
+ let prn = `${buffer.toString('hex')}${moreBytesIndicator} [${data.byteLength} bytes total]`;
203
+ if (wsConsole.showIncommingMessage) wsConsole.print(Console.Types.Incoming, `rx: bin [hex] ${prn}`, Console.Colors.Green)
204
+
205
+ } else {
206
+ if (wsConsole.showIncommingMessage) wsConsole.print(Console.Types.Incoming, `rx: text: ${data}`, Console.Colors.Green)
207
+ }
208
+ })
209
+
210
+ // to send frame message.
211
+ wsConsole.on('line', (data) => {
212
+ if (data[0] === '.') {
213
+ const toks = data.split(/\s+/)
214
+ const cmd = toks[0].substring(1)
215
+ let ch = ""
216
+ switch (cmd) {
217
+ case 'login':
218
+ toks.shift()
219
+ io.login(...toks)
220
+ break;
221
+ case 'auth':
222
+ toks.shift()
223
+ io.auth(...toks)
224
+ break;
225
+
226
+ case 'encNo':
227
+ io.encMode = ENC_MODE.NO
228
+ wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
229
+ break;
230
+ case 'encYes':
231
+ io.encMode = ENC_MODE.YES
232
+ wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
233
+ break;
234
+ case 'encAuto':
235
+ io.encMode = ENC_MODE.AUTO
236
+ wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
237
+ break;
238
+ case 'encMode':
239
+ wsConsole.print(Console.Types.Incoming, `encMode: ${ENC_MODE[io.encMode]}`, Console.Colors.Green)
240
+ break;
241
+
242
+ case 'echo':
243
+ io.echo(toks[1])
244
+ break;
245
+
246
+ case 'iam':
247
+ io.iam(toks[1])
248
+ break;
249
+
250
+ case 'id':
251
+ console.log(`cid: ${io.cid} level: ${io.level}`)
252
+ break;
253
+
254
+ case 'sudo':
255
+ toks.shift()
256
+ console.log('sudo toks', toks)
257
+ io.req('sudo', ...toks).then(res => {
258
+ if (res.ok) {
259
+ console.log('>> sudo response:', res.body)
260
+ } else {
261
+ console.log('>> sudo response:', res.body)
262
+ }
263
+ }).catch(err => {
264
+ console.log('sudo call err', err)
265
+ })
266
+
267
+ break;
268
+
269
+
270
+ case 'quota':
271
+ console.log(`quota: ${JSON.stringify(io.quota)}`)
272
+ break;
273
+
274
+ case 'ch':
275
+ let chList = []
276
+ io.channels.forEach(v => {
277
+ chList.push(v)
278
+ })
279
+ console.log(`channels: ${chList.toString()}`)
280
+ break;
281
+
282
+ case 'sig':
283
+ case 'signal':
284
+ toks.shift()
285
+ io.signal(...toks)
286
+ break;
287
+
288
+ case 'sig_bin':
289
+ toks.shift()
290
+ let to = toks[0]
291
+ let size = parseInt(toks[1])
292
+ console.log(`signal tag: ${to} size: ${size}`)
293
+ io.signal(to, new Uint8Array(size))
294
+ break;
295
+
296
+ case 'pub':
297
+ case 'publish':
298
+ toks.shift()
299
+ io.publish(...toks)
300
+ break;
301
+
302
+ case 'set':
303
+ io.set(toks[1])
304
+ break;
305
+
306
+ case 'api':
307
+ case 'req':
308
+ toks.shift()
309
+ if (toks.length < 2) {
310
+ wsConsole.print(
311
+ Console.Types.Error,
312
+ '[.req need at least two params] req target command [, ..args]',
313
+ Console.Colors.Yellow
314
+ )
315
+ return
316
+ }
317
+ io.req(...toks).then(result => {
318
+ console.log('>> response:', result)
319
+ }).catch(e => {
320
+ console.log(e)
321
+ })
322
+ break;
323
+
324
+ case 'join':
325
+ case 'sub':
326
+ case 'subscribe':
327
+ case 'listen':
328
+ toks.shift()
329
+ let tag = toks[0]
330
+ console.log('listen tag', tag)
331
+ io.listen(tag, (...args) => {
332
+ console.log(`subscribe & listen tag: ${tag} args:`, args)
333
+
334
+ })
335
+ io.subscribe_memory_channels()
336
+ break;
337
+
338
+
339
+ case 'unsub':
340
+ io.unsubscribe(toks[1]);
341
+ break;
342
+
343
+ case 'pping':
344
+ io.signal(toks[1] + "@ping", io.cid)
345
+ break;
346
+ case 'ping':
347
+ io.ping()
348
+ break;
349
+
350
+ case 'pong':
351
+ io.pong()
352
+ break
353
+
354
+ case 'close':
355
+ io.close()
356
+ break
357
+
358
+ case 'open':
359
+ case 'connect':
360
+ if (toks[1]) {
361
+ let url = toks[1]
362
+ if (!url.match(/\w+:\/\/.*$/i)) {
363
+ url = `ws://${url}`
364
+ }
365
+ io.open(url) // new url
366
+ } else {
367
+ io.open() // last url
368
+ }
369
+ break
370
+ case 'show':
371
+ wsConsole.showIncommingMessage = true
372
+ break;
373
+ case 'hide':
374
+ wsConsole.showIncommingMessage = false
375
+ break;
376
+ case 'quit':
377
+ case 'exit':
378
+ process.exit();
379
+ default:
380
+ wsConsole.print(
381
+ Console.Types.Error,
382
+ 'command list: .signal .sig .listen .publish .pub .subscribe .sub .unsub .ping .pong .id .iam .open .connect .close .login .auth .quit .exit',
383
+ Console.Colors.Yellow
384
+ )
385
+ }
386
+ } else {
387
+ io.send( data )
388
+ }
389
+ wsConsole.prompt()
390
+ })
391
+
392
+
393
+
394
+
395
+
396
+