@cuecue/http 0.0.42 → 0.0.44

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/dist/index.js +52 -120
  2. package/package.json +11 -7
package/dist/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var express = require('express');
6
4
  var lodash = require('lodash');
7
5
  var json2csv = require('json2csv');
@@ -15,29 +13,15 @@ var axios = require('axios');
15
13
  var StateMachine = require('javascript-state-machine');
16
14
  var EventEmitter = require('wolfy87-eventemitter');
17
15
 
18
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
-
20
- var express__default = /*#__PURE__*/_interopDefaultLegacy(express);
21
- var bodyParser__default = /*#__PURE__*/_interopDefaultLegacy(bodyParser);
22
- var cors__default = /*#__PURE__*/_interopDefaultLegacy(cors);
23
- var createSession__default = /*#__PURE__*/_interopDefaultLegacy(createSession);
24
- var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
25
- var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
26
- var StateMachine__default = /*#__PURE__*/_interopDefaultLegacy(StateMachine);
27
- var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
28
-
29
16
  const createApi = (app, input, externalRouter = null) => {
30
17
  const router = externalRouter === null ? new express.Router() : externalRouter;
31
-
32
18
  const sendNotFound = res => {
33
19
  res.status(404).json({
34
20
  success: false,
35
21
  error: 'not_found'
36
22
  });
37
23
  };
38
-
39
24
  const getUserId = req => req.header('X-User-Id') || null;
40
-
41
25
  const downloadCSV = (res, fileName, data) => {
42
26
  const json2csv$1 = new json2csv.Parser({
43
27
  transforms: [json2csv.transforms.flatten('.')]
@@ -47,12 +31,37 @@ const createApi = (app, input, externalRouter = null) => {
47
31
  res.attachment(fileName);
48
32
  return res.send(csv);
49
33
  };
50
-
51
34
  router.get('/', async (req, res) => {
52
35
  res.json({
53
36
  message: 'Welcome to CueCue'
54
37
  });
55
38
  });
39
+
40
+ // Any command the app accepts (start, end, reset, cue, define, interact…), as
41
+ // { "command": "cue", "args": ["intro"] }. The app ignores the commands it doesn't accept.
42
+ router.post('/commands', async (req, res) => {
43
+ const {
44
+ command = null,
45
+ args = []
46
+ } = req.body || {};
47
+ if (typeof command !== 'string' || command === '' || !lodash.isArray(args)) {
48
+ res.status(422).json({
49
+ success: false,
50
+ error: 'invalid_command'
51
+ });
52
+ return;
53
+ }
54
+ try {
55
+ await input.command(command, ...args);
56
+ res.json({
57
+ success: true
58
+ });
59
+ } catch {
60
+ res.json({
61
+ success: false
62
+ });
63
+ }
64
+ });
56
65
  router.get('/cues', async (req, res) => {
57
66
  const cues = await app.getCues();
58
67
  res.json(cues);
@@ -65,18 +74,16 @@ const createApi = (app, input, externalRouter = null) => {
65
74
  const {
66
75
  body = null
67
76
  } = req || {};
68
-
69
77
  if (body === null) {
70
78
  sendNotFound(res);
71
79
  return;
72
80
  }
73
-
74
81
  try {
75
82
  await app.define(body);
76
83
  res.json({
77
84
  success: true
78
85
  });
79
- } catch (e) {
86
+ } catch {
80
87
  res.json({
81
88
  success: false
82
89
  });
@@ -85,29 +92,25 @@ const createApi = (app, input, externalRouter = null) => {
85
92
  router.get('/cues/:id', async (req, res) => {
86
93
  const cues = await app.getCues();
87
94
  const cue = cues.find(it => it.id === req.params.id) || null;
88
-
89
95
  if (cue === null) {
90
96
  sendNotFound(res);
91
97
  return;
92
98
  }
93
-
94
99
  res.json(cue);
95
100
  });
96
101
  router.post('/cues/:id', async (req, res) => {
97
102
  const cues = await app.getCues();
98
103
  const cue = cues.find(it => it.id === req.params.id) || null;
99
-
100
104
  if (cue === null) {
101
105
  sendNotFound(res);
102
106
  return;
103
107
  }
104
-
105
108
  try {
106
109
  await input.cue(cue.id, req.body || null);
107
110
  res.json({
108
111
  success: true
109
112
  });
110
- } catch (e) {
113
+ } catch {
111
114
  res.json({
112
115
  success: false
113
116
  });
@@ -116,12 +119,10 @@ const createApi = (app, input, externalRouter = null) => {
116
119
  router.get('/cues/:id/interactions', async (req, res) => {
117
120
  const cues = await app.getCues();
118
121
  const cue = cues.find(it => it.id === req.params.id) || null;
119
-
120
122
  if (cue === null) {
121
123
  sendNotFound(res);
122
124
  return;
123
125
  }
124
-
125
126
  const interactions = await app.getInteractionsByCue(cue.id);
126
127
  res.json(interactions);
127
128
  });
@@ -131,7 +132,7 @@ const createApi = (app, input, externalRouter = null) => {
131
132
  res.json({
132
133
  success: true
133
134
  });
134
- } catch (e) {
135
+ } catch {
135
136
  res.json({
136
137
  success: false
137
138
  });
@@ -149,7 +150,7 @@ const createApi = (app, input, externalRouter = null) => {
149
150
  res.json({
150
151
  success: true
151
152
  });
152
- } catch (e) {
153
+ } catch {
153
154
  res.json({
154
155
  success: false
155
156
  });
@@ -159,84 +160,70 @@ const createApi = (app, input, externalRouter = null) => {
159
160
  const {
160
161
  session
161
162
  } = app;
162
-
163
163
  if (session === null) {
164
164
  sendNotFound(res);
165
165
  return;
166
166
  }
167
-
168
167
  res.json(session.id);
169
168
  });
170
169
  router.get('/session/:id', async (req, res) => {
171
170
  const {
172
171
  session
173
- } = app; // TODO: Find a session?
174
-
172
+ } = app;
173
+ // TODO: Find a session?
175
174
  if (session === null) {
176
175
  sendNotFound(res);
177
176
  return;
178
177
  }
179
-
180
178
  res.json(session);
181
179
  });
182
180
  router.get('/app/state', async (req, res) => {
183
181
  const {
184
182
  state
185
183
  } = app.state;
186
-
187
184
  if (state === null) {
188
185
  sendNotFound(res);
189
186
  return;
190
187
  }
191
-
192
188
  res.json(state);
193
189
  });
194
190
  router.get('/export/sessions.:format', async (req, res) => {
195
191
  const format = req.params.format || null;
196
192
  const items = await app.store.getItems('sessions', {}, true);
197
-
198
193
  if (items === null || !lodash.isArray(items)) {
199
194
  sendNotFound(res);
200
195
  return;
201
196
  }
202
-
203
197
  if (format === 'csv') {
204
198
  downloadCSV(res, 'sessions.csv', items);
205
199
  return;
206
200
  }
207
-
208
201
  res.json(items);
209
202
  });
210
203
  router.get('/export/interactions.:format', async (req, res) => {
211
204
  const format = req.params.format || null;
212
205
  const items = await app.store.getItems('interactions', {}, true);
213
-
214
206
  if (items === null || !lodash.isArray(items)) {
215
207
  sendNotFound(res);
216
208
  return;
217
209
  }
218
-
219
210
  if (format === 'csv') {
220
211
  downloadCSV(res, 'interactions.csv', items);
221
212
  return;
222
213
  }
223
-
224
214
  res.json(items);
225
215
  });
226
216
  router.get('/export/cues.:format', async (req, res) => {
227
217
  const format = req.params.format || null;
228
218
  const items = await app.store.getItems('cues', {}, true);
229
-
230
219
  if (items === null || !lodash.isArray(items)) {
231
220
  sendNotFound(res);
232
221
  return;
233
222
  }
234
-
235
223
  if (format === 'csv') {
236
224
  downloadCSV(res, 'cues.csv', items);
237
225
  return;
238
226
  }
239
-
240
227
  res.json(items);
241
228
  });
242
229
  return router;
@@ -256,71 +243,60 @@ class HttpInput extends core.BaseInput {
256
243
  this.http = null;
257
244
  this.path = '/';
258
245
  this.router = new express.Router();
259
- this.debug = createDebug__default["default"]('cuecue:input:http');
246
+ this.debug = createDebug('cuecue:input:http');
260
247
  }
261
-
262
248
  cue(cue, extraData = null) {
263
249
  this.emit('command', 'cue', cue, extraData);
264
250
  }
265
-
266
251
  interact(data, interactionId = null) {
267
252
  this.emit('command', 'interact', data, interactionId);
268
253
  }
269
-
270
254
  getRouter() {
271
255
  return this.router;
272
256
  }
273
-
274
257
  setRouter(router) {
275
258
  this.router = router;
276
259
  }
277
-
278
260
  use(...args) {
279
261
  return this.express.use(...args);
280
262
  }
281
-
282
263
  async onInit() {
283
264
  this.emit('init');
284
265
  const {
285
266
  path
286
267
  } = this.options;
287
- this.express = express__default["default"]();
268
+ this.express = express();
288
269
  this.http = http.createServer(this.express);
289
- this.session = createSession__default["default"]({
270
+ this.session = createSession({
290
271
  secret: 'my-secret-session',
291
272
  resave: true,
292
273
  saveUninitialized: true
293
274
  });
294
- this.express.use(cors__default["default"]());
295
- this.express.use(bodyParser__default["default"].json());
296
- this.express.use(bodyParser__default["default"].urlencoded({
275
+ this.express.use(cors());
276
+ this.express.use(bodyParser.json());
277
+ this.express.use(bodyParser.urlencoded({
297
278
  extended: false
298
- })); // Use express-session middleware for express
279
+ }));
299
280
 
281
+ // Use express-session middleware for express
300
282
  this.express.use(this.session);
301
-
302
283
  if (this.router !== null) {
303
284
  this.use(path, this.router);
304
285
  }
305
-
306
286
  await this.startServer();
307
287
  }
308
-
309
288
  async onDestroy() {
310
289
  this.debug('destroying...');
311
290
  this.emit('destroy');
312
-
313
291
  try {
314
292
  await this.stopServer();
315
293
  } catch (e) {
316
294
  this.debug('destroy error %O', e);
317
295
  }
318
-
319
296
  this.express = null;
320
297
  this.http = null;
321
298
  this.session = null;
322
299
  }
323
-
324
300
  onStarted() {
325
301
  const {
326
302
  port
@@ -328,7 +304,6 @@ class HttpInput extends core.BaseInput {
328
304
  this.debug(`started on *:${port}`);
329
305
  return process.nextTick(() => this.emit('started'));
330
306
  }
331
-
332
307
  startServer() {
333
308
  const {
334
309
  port
@@ -339,12 +314,10 @@ class HttpInput extends core.BaseInput {
339
314
  reject(err);
340
315
  return;
341
316
  }
342
-
343
317
  resolve();
344
318
  });
345
319
  });
346
320
  }
347
-
348
321
  stopServer() {
349
322
  this.debug('Stopping...');
350
323
  return new Promise((resolve, reject) => {
@@ -353,12 +326,10 @@ class HttpInput extends core.BaseInput {
353
326
  reject(err);
354
327
  return;
355
328
  }
356
-
357
329
  resolve();
358
330
  });
359
331
  });
360
332
  }
361
-
362
333
  }
363
334
 
364
335
  class ApiInput extends HttpInput {
@@ -370,7 +341,6 @@ class ApiInput extends HttpInput {
370
341
  super(options);
371
342
  this.router = createApi(app, this);
372
343
  }
373
-
374
344
  }
375
345
 
376
346
  class HttpOutput extends core.BaseOutput {
@@ -379,33 +349,26 @@ class HttpOutput extends core.BaseOutput {
379
349
  method: 'POST',
380
350
  ...opts
381
351
  });
382
- this.debug = createDebug__default["default"]('cuecue:output:http');
352
+ this.debug = createDebug('cuecue:output:http');
383
353
  }
384
-
385
354
  cue(cue, extraData = null) {
386
355
  this.command('cue', cue, extraData);
387
356
  }
388
-
389
357
  cues(cues) {
390
358
  this.command('cues', cues);
391
359
  }
392
-
393
360
  interact(data, interactionId = null) {
394
361
  this.command('interact', data, interactionId);
395
362
  }
396
-
397
363
  interaction(interaction) {
398
364
  this.command('interaction', interaction);
399
365
  }
400
-
401
366
  uninteraction(interactionId) {
402
367
  this.command('uninteract', interactionId);
403
368
  }
404
-
405
369
  uninteractions(interactionIds) {
406
370
  this.command('uninteractions', interactionIds);
407
371
  }
408
-
409
372
  async command(command, ...args) {
410
373
  const {
411
374
  transformCommand = null,
@@ -416,22 +379,18 @@ class HttpOutput extends core.BaseOutput {
416
379
  command,
417
380
  args
418
381
  };
419
-
420
382
  if (value === false) {
421
383
  this.debug('command canceled: %s %o', command, args);
422
384
  return Promise.resolve();
423
385
  }
424
-
425
386
  const {
426
387
  command: finalCommand = command,
427
388
  args: finalArgs = args
428
389
  } = value || {};
429
-
430
390
  if (acceptCommand !== null && !acceptCommand(finalCommand, finalArgs)) {
431
391
  this.debug('command refused: %s %o', finalCommand, finalArgs);
432
392
  return Promise.resolve();
433
393
  }
434
-
435
394
  this.debug('command: %s message: %o', finalCommand, finalArgs);
436
395
  const message = {
437
396
  command: finalCommand,
@@ -440,24 +399,22 @@ class HttpOutput extends core.BaseOutput {
440
399
  const payload = transformMessage !== null ? await transformMessage(message) : message;
441
400
  return this.request(payload);
442
401
  }
443
-
444
402
  async request(payload) {
445
403
  const {
446
404
  url,
447
405
  method,
448
406
  headers
449
407
  } = this.options;
450
- return axios__default["default"]({
408
+ return axios({
451
409
  url,
452
410
  method,
453
411
  headers,
454
412
  data: payload
455
413
  });
456
414
  }
457
-
458
415
  }
459
416
 
460
- class Server extends EventEmitter__default["default"] {
417
+ class Server extends EventEmitter {
461
418
  constructor(opts = {}) {
462
419
  super();
463
420
  this.options = {
@@ -476,8 +433,8 @@ class Server extends EventEmitter__default["default"] {
476
433
  this.app = null;
477
434
  this.session = null;
478
435
  this.http = null;
479
- this.debug = createDebug__default["default"]('cuecue:server');
480
- this.state = new StateMachine__default["default"]({
436
+ this.debug = createDebug('cuecue:server');
437
+ this.state = new StateMachine({
481
438
  transitions: [{
482
439
  name: 'init',
483
440
  from: 'none',
@@ -508,99 +465,81 @@ class Server extends EventEmitter__default["default"] {
508
465
  });
509
466
  this.init();
510
467
  }
511
-
512
468
  init() {
513
469
  return this.state.init();
514
470
  }
515
-
516
471
  destroy() {
517
472
  return this.state.destroy();
518
473
  }
519
-
520
474
  async start() {
521
475
  if (!this.initialized()) {
522
476
  await this.init();
523
477
  }
524
-
525
478
  return this.state.start();
526
479
  }
527
-
528
480
  stop() {
529
481
  return this.state.stop();
530
482
  }
531
-
532
483
  initialized() {
533
484
  return this.state.is('initialized');
534
485
  }
535
-
536
486
  started() {
537
487
  return this.state.is('started');
538
488
  }
539
-
540
489
  stopped() {
541
490
  return this.state.is('stopped');
542
491
  }
543
-
544
492
  destroyed() {
545
493
  return this.state.is('destroyed');
546
494
  }
547
-
548
495
  use(...args) {
549
496
  return this.app.use(...args);
550
497
  }
551
-
552
498
  async onInit() {
553
499
  this.emit('init');
554
- this.app = express__default["default"]();
500
+ this.app = express();
555
501
  this.http = http.createServer(this.app);
556
- this.session = createSession__default["default"]({
502
+ this.session = createSession({
557
503
  secret: 'my-secret-session',
558
504
  resave: true,
559
505
  saveUninitialized: true
560
506
  });
561
- this.app.use(cors__default["default"]());
562
- this.app.use(bodyParser__default["default"].json());
563
- this.app.use(bodyParser__default["default"].urlencoded({
507
+ this.app.use(cors());
508
+ this.app.use(bodyParser.json());
509
+ this.app.use(bodyParser.urlencoded({
564
510
  extended: false
565
- })); // Use express-session middleware for express
511
+ }));
566
512
 
513
+ // Use express-session middleware for express
567
514
  this.app.use(this.session);
568
515
  await this.startServer();
569
516
  }
570
-
571
517
  onInitialized() {
572
518
  this.debug('initialized');
573
519
  return process.nextTick(() => this.emit('initialized'));
574
520
  }
575
-
576
521
  async onDestroy() {
577
522
  this.debug('destroying...');
578
523
  this.emit('destroy');
579
-
580
524
  try {
581
525
  await this.stopServer();
582
-
583
526
  if (this.session !== null && typeof this.session.close !== 'undefined') {
584
527
  this.session.close();
585
528
  }
586
529
  } catch (e) {
587
530
  this.debug('onDestroy error %O', e);
588
531
  }
589
-
590
532
  this.app = null;
591
533
  this.http = null;
592
534
  }
593
-
594
535
  onDestroyed() {
595
536
  this.debug('destroyed');
596
537
  return process.nextTick(() => this.emit('destroyed'));
597
538
  }
598
-
599
539
  onStart() {
600
540
  this.debug('starting...');
601
541
  this.emit('start');
602
542
  }
603
-
604
543
  onStarted() {
605
544
  const {
606
545
  port
@@ -608,16 +547,13 @@ class Server extends EventEmitter__default["default"] {
608
547
  this.debug(`started on *:${port}`);
609
548
  return process.nextTick(() => this.emit('started'));
610
549
  }
611
-
612
550
  onStop() {
613
551
  this.emit('stop');
614
552
  }
615
-
616
553
  onStopped() {
617
554
  this.debug('Stopped.');
618
555
  return process.nextTick(() => this.emit('stopped'));
619
556
  }
620
-
621
557
  startServer() {
622
558
  const {
623
559
  port
@@ -628,12 +564,10 @@ class Server extends EventEmitter__default["default"] {
628
564
  reject(err);
629
565
  return;
630
566
  }
631
-
632
567
  resolve();
633
568
  });
634
569
  });
635
570
  }
636
-
637
571
  stopServer() {
638
572
  this.debug('Stopping...');
639
573
  return new Promise((resolve, reject) => {
@@ -642,12 +576,10 @@ class Server extends EventEmitter__default["default"] {
642
576
  reject(err);
643
577
  return;
644
578
  }
645
-
646
579
  resolve();
647
580
  });
648
581
  });
649
582
  }
650
-
651
583
  }
652
584
 
653
585
  exports.ApiInput = ApiInput;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuecue/http",
3
- "version": "0.0.42",
3
+ "version": "0.0.44",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "javascript"
@@ -29,19 +29,21 @@
29
29
  }
30
30
  ],
31
31
  "license": "ISC",
32
+ "engines": {
33
+ "node": ">=22"
34
+ },
32
35
  "main": "dist/index.js",
33
36
  "files": [
34
37
  "dist"
35
38
  ],
36
39
  "scripts": {
37
40
  "clean": "rm -rf dist",
38
- "build": "rollup --config ../../rollup.config.js",
39
- "prepare": "npm run clean && npm run build"
41
+ "build": "rollup --config ../../rollup.config.mjs",
42
+ "prepack": "npm run clean && npm run build"
40
43
  },
41
44
  "dependencies": {
42
- "@babel/runtime": "^7.12.5",
43
- "@cuecue/core": "^0.0.42",
44
- "axios": "^0.24.0",
45
+ "@cuecue/core": "^0.0.44",
46
+ "axios": "^1.21.1",
45
47
  "body-parser": "^1.19.0",
46
48
  "commander": "^9.4.0",
47
49
  "cors": "^2.8.5",
@@ -56,5 +58,7 @@
56
58
  "publishConfig": {
57
59
  "access": "public"
58
60
  },
59
- "gitHead": "ce63758c17a7b670e6684e3dee3091a07b132522"
61
+ "devDependencies": {
62
+ "@cuecue/app": "^0.0.44"
63
+ }
60
64
  }