@soundworks/core 3.1.0-beta.4 → 3.1.0-beta.5

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.1.0-beta.4",
3
+ "version": "3.1.0-beta.5",
4
4
  "description": "full-stack javascript framework for distributed audio visual experiences on the web",
5
5
  "authors": [
6
6
  "Benjamin Matuszewski",
package/server/Server.js CHANGED
@@ -264,7 +264,37 @@ class Server {
264
264
  }
265
265
  }
266
266
 
267
- this._clientConfigFunction = clientConfigFunction;
267
+ this._clientConfigFunction = clientConfigFunction; // basic http authentication
268
+
269
+ if (this.config.env.auth) {
270
+ this.router.use((req, res, next) => {
271
+ const isProtected = this.config.env.auth.clients.map(type => req.path.endsWith(`/${type}`)).reduce((acc, value) => acc || value, false);
272
+
273
+ if (isProtected) {
274
+ // authentication middleware
275
+ const auth = this.config.env.auth; // parse login and password from headers
276
+
277
+ const b64auth = (req.headers.authorization || '').split(' ')[1] || '';
278
+ const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':'); // verify login and password are set and correct
279
+
280
+ if (login && password && login === auth.login && password === auth.password) {
281
+ // -> access granted...
282
+ return next();
283
+ } // -> access denied...
284
+
285
+
286
+ res.writeHead(401, {
287
+ 'WWW-Authenticate': 'Basic',
288
+ 'Content-Type': 'text/plain'
289
+ });
290
+ res.end('Authentication required.');
291
+ } else {
292
+ // route not protected
293
+ return next();
294
+ }
295
+ });
296
+ }
297
+
268
298
  return Promise.resolve();
269
299
  }
270
300
  /**
@@ -414,7 +444,7 @@ class Server {
414
444
  }
415
445
  }
416
446
 
417
- _logger.default.clientConfigAndRouting(routes, this.config.app.clients, this.config.env.serverIp);
447
+ _logger.default.clientConfigAndRouting(routes, this.config);
418
448
 
419
449
  return Promise.resolve();
420
450
  }).then(() => {
@@ -512,8 +542,16 @@ class Server {
512
542
 
513
543
  if (this.config.env.subpath && !data.env.subpath) {
514
544
  data.env.subpath = this.config.env.subpath;
515
- }
545
+ } // cors / coop / coep headers for `crossOriginIsolated pages, enables
546
+ // sharedArrayBuffers and high precision timers
547
+ // cf. https://web.dev/why-coop-coep/
548
+
516
549
 
550
+ res.writeHead(200, {
551
+ 'Cross-Origin-Resource-Policy': 'same-origin',
552
+ 'Cross-Origin-Embedder-Policy': 'require-corp',
553
+ 'Cross-Origin-Opener-Policy': 'same-origin'
554
+ });
517
555
  const appIndex = tmpl(data);
518
556
  res.end(appIndex);
519
557
  }); // return route infos for logging on server start
@@ -19,7 +19,10 @@ const logger = {
19
19
  console.log(_chalk.default.cyan(`+ ${msg}`));
20
20
  },
21
21
 
22
- clientConfigAndRouting(routes, clientsConfig, serverIp) {
22
+ clientConfigAndRouting(routes, config) {
23
+ const clientsConfig = config.app.clients;
24
+ const servIp = config.env.serverIp;
25
+ const auth = config.env.auth;
23
26
  const table = [];
24
27
 
25
28
  for (let clientType in clientsConfig) {
@@ -27,18 +30,20 @@ const logger = {
27
30
 
28
31
  if (client.target === 'node') {
29
32
  const line = {
30
- clientType: `${clientType}`,
33
+ client_type: `> ${clientType}`,
31
34
  target: _chalk.default.red(client.target),
32
35
  path: `server ip: ${_chalk.default.green(serverIp)}`,
33
- default: undefined
36
+ default: undefined,
37
+ auth: undefined
34
38
  };
35
39
  table.push(line);
36
40
  } else if (client.target === 'browser') {
37
41
  const line = {
38
- clientType: `${clientType}`,
42
+ client_type: `> ${clientType}`,
39
43
  target: _chalk.default.red(client.target),
40
44
  path: routes.find(r => r.clientType === clientType) ? _chalk.default.green(routes.find(r => r.clientType === clientType).path) : _chalk.default.red('no route defined'),
41
- default: client.default ? 'default' : undefined
45
+ default: client.default ? 'x' : undefined,
46
+ auth: auth && auth.clients.indexOf(clientType) !== -1 ? 'x' : undefined
42
47
  };
43
48
  table.push(line);
44
49
  } else {
@@ -46,10 +51,21 @@ const logger = {
46
51
  }
47
52
  }
48
53
 
54
+ console.log(``);
49
55
  console.log((0, _columnify.default)(table, {
50
- showHeaders: false,
51
- minWidth: 14
52
- })); // check if a route is defined but not in config
56
+ showHeaders: true,
57
+ minWidth: 6,
58
+ columnSplitter: ' | ',
59
+ config: {
60
+ default: {
61
+ align: 'center'
62
+ },
63
+ auth: {
64
+ align: 'center'
65
+ }
66
+ }
67
+ }));
68
+ console.log(``); // check if a route is defined but not in config
53
69
 
54
70
  const configClientTypes = Object.keys(clientsConfig);
55
71
  routes.forEach(r => {