@live-change/server 0.1.11 → 0.1.15

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/lib/Renderer.js CHANGED
@@ -121,6 +121,13 @@ class Renderer {
121
121
  this.vite && this.vite.ssrFixStacktrace(e)
122
122
  }
123
123
 
124
+ async close() {
125
+ if(this.vite) {
126
+ console.log("VITE CLOSE!!!")
127
+ await this.vite.close()
128
+ }
129
+ }
130
+
124
131
  }
125
132
 
126
133
  module.exports = Renderer
package/lib/Services.js CHANGED
@@ -34,17 +34,26 @@ class Services {
34
34
  return this.config.services.find(s => s.name = serviceName)
35
35
  }
36
36
  async loadServices() {
37
- if(this.config.plugins)
38
- for(const plugin of this.config.plugins) {
39
- const entryFile = await this.getServiceEntryFile(plugin)
40
- console.log("PLUGIN", plugin, 'ENTRY FILE', entryFile)
41
- this.plugins.push(require(entryFile))
37
+ app.config.services = this.config.services
38
+ app.config.plugins = this.config.plugins
39
+ if(this.config.plugins) {
40
+ for(const plugin of this.config.plugins) {
41
+ const entryFile = await this.getServiceEntryFile(plugin)
42
+ console.log("PLUGIN", plugin, 'ENTRY FILE', entryFile)
43
+ this.plugins.push(require(entryFile))
44
+ }
42
45
  }
43
- if(this.config.services)
44
- for(const service of this.config.services) {
45
- const entryFile = await this.getServiceEntryFile(service)
46
- console.log("SERVICE", service, 'ENTRY FILE', entryFile)
47
- this.serviceDefinitions.push(require(entryFile))
46
+ if(this.config.services) {
47
+ for(const service of this.config.services) {
48
+ const entryFile = await this.getServiceEntryFile(service)
49
+ console.log("SERVICE", service, 'ENTRY FILE', entryFile)
50
+ const definition = require(entryFile)
51
+ if(definition.name != service.name) {
52
+ console.error("SERVICE", service, "NAME", service.name, "MISMATCH", definition.name)
53
+ process.exit(1)
54
+ }
55
+ this.serviceDefinitions.push(definition)
56
+ }
48
57
  }
49
58
 
50
59
  /// TODO: load dependent services!!!
package/lib/SsrServer.js CHANGED
@@ -2,10 +2,11 @@ const cookie = require('cookie')
2
2
  const path = require('path')
3
3
  const serveStatic = require('serve-static')
4
4
  const crypto = require('crypto')
5
+ const expressStaticGzip = require("express-static-gzip")
5
6
 
6
- const serverDao = require('@live-change/vue3-ssr/serverDao.js')
7
+ const serverDao = require('./serverDao.js')
7
8
  const { hashCode, encodeNumber, uidGenerator } = require('@live-change/uid')
8
- const getIp = require('@live-change/vue3-ssr/getIp.js')
9
+ const getIp = require('./getIp.js')
9
10
 
10
11
  const Renderer = require('./Renderer.js')
11
12
 
@@ -32,7 +33,21 @@ class SsrServer {
32
33
  this.express.use(this.renderer.vite.middlewares)
33
34
  } else {
34
35
  const staticPath = path.resolve(this.root, 'dist/client')
35
- this.express.use(serveStatic(staticPath, { index: false }))
36
+ this.express.use('/', expressStaticGzip(staticPath, {
37
+ //enableBrotli: true,
38
+ customCompressions: [{
39
+ encodingName: 'br',
40
+ fileExtension: 'br'
41
+ },{
42
+ encodingName: 'gzip',
43
+ fileExtension: 'gz'
44
+ },{
45
+ encodingName: 'deflate',
46
+ fileExtension: 'zz'
47
+ }],
48
+ orderPreference: ['br', 'gzip', 'deflate']
49
+ }))
50
+ //this.express.use(serveStatic(staticPath, { index: false }))
36
51
  }
37
52
 
38
53
  await this.setupSsr()
@@ -92,6 +107,9 @@ class SsrServer {
92
107
  })
93
108
  }
94
109
 
110
+ async close() {
111
+ await this.renderer.close()
112
+ }
95
113
  }
96
114
 
97
115
  module.exports = SsrServer
package/lib/TestServer.js CHANGED
@@ -4,10 +4,11 @@ const express = require('express')
4
4
 
5
5
  const app = require('@live-change/framework').app()
6
6
 
7
+ const { hashCode, encodeNumber, uidGenerator } = require('@live-change/uid')
8
+
7
9
  const setupApiServer = require('./setupApiServer.js')
8
10
  const setupApiSockJs = require('./setupApiSockJs.js')
9
11
  const setupApiWs = require('./setupApiWs.js')
10
- const setupApp = require('./setupApp.js')
11
12
  const setupDbServer = require('./setupDbServer.js')
12
13
  const createLoopbackDao = require('./createLoopbackDao.js')
13
14
  const SsrServer = require('./SsrServer.js')
@@ -24,10 +25,13 @@ class TestServer {
24
25
  path.resolve(this.config.ssrRoot, 'dist/client/ssr-manifest.json')
25
26
  )
26
27
 
27
- await setupApp({
28
- withDb: true,
29
- dbBackend: 'mem'
30
- })
28
+ app.instanceId = encodeNumber(hashCode(
29
+ `app${process.pid}${require("os").hostname()} ${process.cwd()}/${process.argv.join(' ')}`))
30
+ app.uidGenerator = uidGenerator(app.instanceId, 1)
31
+ this.dbServer = await setupDbServer({ dbBackend: 'mem' })
32
+ const loopbackDao = await createLoopbackDao('local', () => this.dbServer.createDao('local'))
33
+ app.dao = loopbackDao
34
+ app.databaseName = 'test'
31
35
 
32
36
  await app.dao.request(['database', 'createDatabase'], app.databaseName, { }).catch(err => 'exists')
33
37
 
@@ -66,11 +70,22 @@ class TestServer {
66
70
  return await createLoopbackDao(credentials, () => this.apiServer.daoFactory(credentials, ip))
67
71
  }
68
72
 
69
- dispose() {
70
- this.httpServer.close()
71
- this.dbServer.close()
72
- //this.wsServer.close()
73
- //this.sockJsServer.close()
73
+ async dispose() {
74
+ try {
75
+ console.log("CLOSE HTTP!")
76
+ await this.httpServer.close()
77
+ console.log("CLOSE APP")
78
+ await app.close()
79
+ console.log("CLOSE DB!")
80
+ await this.dbServer.close()
81
+ console.log("CLOSE SSR!")
82
+ await this.ssrServer.close()
83
+ console.log("CLOSED!")
84
+ //this.wsServer.close()
85
+ //this.sockJsServer.close()
86
+ } catch(error) {
87
+ console.error("CLOSE ERROR", error)
88
+ }
74
89
  }
75
90
  }
76
91
 
package/lib/getIp.js ADDED
@@ -0,0 +1,12 @@
1
+ function getIp(connection) {
2
+ let ip =
3
+ connection.headers['x-real-ip'] ||
4
+ connection.headers['x-forwarded-for'] ||
5
+ connection.remoteAddress ||
6
+ (connection.connection && connection.connection.remoteAddress)
7
+ ip = ip.split(',')[0]
8
+ ip = ip.split(':').slice(-1)[0] //in case the ip returned in a format: "::ffff:146.xxx.xxx.xxx"
9
+ return ip
10
+ }
11
+
12
+ module.exports = getIp
@@ -0,0 +1,56 @@
1
+ const { Dao } = require("@live-change/dao")
2
+ const DaoWebsocket = require("@live-change/dao-websocket")
3
+
4
+ function reactiveObservableListConstructor(reactive) {
5
+ class ReactiveObservableList extends Dao.ObservableList {
6
+ constructor(value, what, dispose) {
7
+ super(value, what, dispose, (data) => {
8
+ if(data && typeof data == 'object') {
9
+ const activated = reactive(data)
10
+ return activated
11
+ }
12
+ return data
13
+ })
14
+ }
15
+ }
16
+ return ReactiveObservableList
17
+ }
18
+
19
+ function serverDao(credentials, ip, settings) {
20
+ const serverHost = settings.remoteUrl || process.env.API_SERVER || "localhost:" + (process.env.API_PORT || 8002)
21
+ const wsServer = `ws://${serverHost}/api/ws`
22
+
23
+ return new Dao(credentials, {
24
+ remoteUrl: wsServer,
25
+ protocols: {
26
+ 'ws': DaoWebsocket.client
27
+ },
28
+
29
+ ...settings,
30
+
31
+ connectionSettings: {
32
+ headers: {
33
+ 'X-real-ip': ip,
34
+ 'X-forwarded-for': ip
35
+ },
36
+ queueRequestsWhenDisconnected: true,
37
+ requestSendTimeout: 2300,
38
+ requestTimeout: 10000,
39
+ queueActiveRequestsOnDisconnect: false,
40
+ autoReconnectDelay: 200,
41
+ logLevel: 1,
42
+ /*connectionMonitorFactory: (connection) =>
43
+ new ReactiveDao.ConnectionMonitorPinger(connection, {
44
+ pingInterval: 50,
45
+ pongInterval: 200
46
+ })*/
47
+ ...(settings && settings.connectionSettings)
48
+ },
49
+ defaultRoute: {
50
+ type: "remote",
51
+ generator: settings.reactive ? reactiveObservableListConstructor(settings.reactive) : Dao.ObservableList
52
+ }
53
+ })
54
+ }
55
+
56
+ module.exports = serverDao
@@ -4,7 +4,7 @@ const app = require("@live-change/framework").app()
4
4
 
5
5
 
6
6
  async function setupApiServer(settings) {
7
- const { services: config, withServices, updateServices, enableSessions } = settings
7
+ const { services: config, withServices, updateServices } = settings
8
8
 
9
9
  const services = new Services(config)
10
10
 
@@ -41,13 +41,11 @@ async function setupApiServer(settings) {
41
41
  },
42
42
  shareDefinition: true,
43
43
  logErrors: true,
44
- createSessionOnUpdate: true
44
+ createSessionOnUpdate: true /// deprecated - moved to session-service settings
45
45
  }
46
46
 
47
- const apiServer = enableSessions
48
- ? await app.createSessionApiServer(apiServerConfig)
49
- : await app.createApiServer(apiServerConfig)
50
-
47
+ const apiServer = await app.createLiveApiServer(apiServerConfig)
48
+
51
49
  apiServer.services = services
52
50
 
53
51
  return apiServer
package/lib/setupApp.js CHANGED
@@ -13,11 +13,8 @@ async function setupApp(settings, env = process.env) {
13
13
  let dbServer
14
14
  if(settings.withDb) {
15
15
  dbServer = await setupDbServer(settings)
16
- //app.dao.dispose()
17
16
  const loopbackDao = await createLoopbackDao('local', () => dbServer.createDao('local'))
18
17
  app.dao = loopbackDao
19
- // loopbackDao.prepareSource(app.dao.definition.database)
20
- // loopbackDao.prepareSource(app.dao.definition.store)
21
18
  } else {
22
19
  app.dao = setupDbClient(settings)
23
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/server",
3
- "version": "0.1.11",
3
+ "version": "0.1.15",
4
4
  "description": "Live Change Framework - server",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -35,6 +35,7 @@
35
35
  "serialize-javascript": "^5.0.1",
36
36
  "sockjs": "^0.3.21",
37
37
  "websocket": "^1.0.34",
38
- "yargs": "^17.0.1"
38
+ "yargs": "^17.0.1",
39
+ "express-static-gzip": "2.1.1"
39
40
  }
40
41
  }