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