@soundworks/core 3.2.2 → 3.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soundworks/core",
3
- "version": "3.2.2",
3
+ "version": "3.3.1",
4
4
  "description": "full-stack javascript framework for distributed audio visual experiences on the web",
5
5
  "authors": [
6
6
  "Benjamin Matuszewski",
@@ -29,7 +29,8 @@
29
29
  "dev:client": "chokidar src/client/ -c \"npm run build:client\"",
30
30
  "dev:server": "chokidar src/server/ -c \"npm run build:server\"",
31
31
  "dev:common": "chokidar src/common/ -c \"npm run build:common\"",
32
- "dev": "npm run build && (npm run dev:client & npm run dev:server & npm run dev:common)"
32
+ "dev": "npm run build && (npm run dev:client & npm run dev:server & npm run dev:common)",
33
+ "test": "mocha"
33
34
  },
34
35
  "dependencies": {
35
36
  "chalk": "^4.1.2",
@@ -38,6 +39,7 @@
38
39
  "debug": "^4.3.4",
39
40
  "fast-deep-equal": "^3.1.3",
40
41
  "fast-text-encoding": "^1.0.0",
42
+ "http-terminator": "^3.2.0",
41
43
  "isomorphic-ws": "^4.0.1",
42
44
  "keyv": "^4.2.2",
43
45
  "keyv-file": "^0.2.0",
@@ -20,7 +20,7 @@ const log = (0, _debug.default)('soundworks:lifecycle');
20
20
  */
21
21
 
22
22
  class AbstractExperience {
23
- constructor(server, clientTypes = null) {
23
+ constructor(server, clientTypes = []) {
24
24
  // @todo - check that it's a soundworks instance
25
25
  if (!server) {
26
26
  throw new Error(`${this.constructor.name} should receive the "Server" instance as first argument`);
package/server/Server.js CHANGED
@@ -41,6 +41,8 @@ var _keyv = _interopRequireDefault(require("keyv"));
41
41
 
42
42
  var _keyvFile = _interopRequireDefault(require("keyv-file"));
43
43
 
44
+ var _httpTerminator = require("http-terminator");
45
+
44
46
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
45
47
 
46
48
  let _dbNamespaces = new Set();
@@ -401,12 +403,23 @@ class Server {
401
403
  this.router.server = httpServer;
402
404
  return Promise.resolve();
403
405
  }).then(() => {
404
- if (this.templateEngine === null) {
405
- throw new Error('Undefined "server.templateEngine": please provide a valid template engine');
406
+ let nodeOnly = true; // do not throw if no browser clients are defined, very usefull for
407
+ // cleaning tests in particular
408
+
409
+ for (let clientType in this.config.app.clients) {
410
+ if (this.config.app.clients[clientType].target === 'browser') {
411
+ nodeOnly = false;
412
+ }
406
413
  }
407
414
 
408
- if (this.templateDirectory === null) {
409
- throw new Error('Undefined "server.templateDirectory": please provide a valid template directory');
415
+ if (!nodeOnly) {
416
+ if (this.templateEngine === null) {
417
+ throw new Error('Undefined "server.templateEngine": please provide a valid template engine');
418
+ }
419
+
420
+ if (this.templateDirectory === null) {
421
+ throw new Error('Undefined "server.templateDirectory": please provide a valid template directory');
422
+ }
410
423
  } // ------------------------------------------------------------
411
424
  // INIT ROUTING
412
425
  // ------------------------------------------------------------
@@ -425,8 +438,15 @@ class Server {
425
438
 
426
439
 
427
440
  for (let clientType in this._clientTypeActivitiesMap) {
441
+ // do nothing if client type is not registered in config
442
+ if (!(clientType in this.config.app.clients)) {
443
+ continue;
444
+ }
445
+
428
446
  if (clientType !== defaultClientType) {
429
- const path = this._openClientRoute(clientType, this.router);
447
+ const clientTarget = this.config.app.clients[clientType].target;
448
+
449
+ const path = this._openClientRoute(clientType, target, this.router);
430
450
 
431
451
  routes.push({
432
452
  clientType,
@@ -437,8 +457,15 @@ class Server {
437
457
 
438
458
 
439
459
  for (let clientType in this._clientTypeActivitiesMap) {
460
+ // do nothing if client type is not registered in config
461
+ if (!(clientType in this.config.app.clients)) {
462
+ continue;
463
+ }
464
+
440
465
  if (clientType === defaultClientType) {
441
- const path = this._openClientRoute(clientType, this.router, true);
466
+ const clientTarget = this.config.app.clients[clientType].target;
467
+
468
+ const path = this._openClientRoute(clientType, target, this.router, true);
442
469
 
443
470
  routes.unshift({
444
471
  clientType,
@@ -492,18 +519,31 @@ class Server {
492
519
  console.error(err);
493
520
  }
494
521
  }
522
+
523
+ async stop() {
524
+ const httpTerminator = (0, _httpTerminator.createHttpTerminator)({
525
+ server: this.httpServer
526
+ });
527
+ await httpTerminator.terminate();
528
+ }
495
529
  /**
496
530
  * Open the route for the given client.
497
531
  * @private
498
532
  */
499
533
 
500
534
 
501
- _openClientRoute(clientType, router, isDefault = false) {
535
+ _openClientRoute(clientType, target, router, isDefault = false) {
536
+ // only browser targets need a route
537
+ if (target === 'node') {
538
+ return;
539
+ }
540
+
502
541
  let route = '/';
503
542
 
504
543
  if (!isDefault) {
505
544
  route += `${clientType}`;
506
- }
545
+ } // @todo - define what is this... looks completely not used and not usable...
546
+
507
547
 
508
548
  if (this._routes[clientType]) {
509
549
  route += this._routes[clientType];