@live-change/server 0.1.10 → 0.1.14
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 +7 -0
- package/lib/Services.js +19 -10
- package/lib/SsrServer.js +5 -2
- package/lib/TestServer.js +26 -9
- package/lib/getIp.js +12 -0
- package/lib/serverDao.js +56 -0
- package/lib/setupApiServer.js +4 -6
- package/lib/setupApp.js +0 -3
- package/package.json +1 -1
package/lib/Renderer.js
CHANGED
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
@@ -3,9 +3,9 @@ const path = require('path')
|
|
|
3
3
|
const serveStatic = require('serve-static')
|
|
4
4
|
const crypto = require('crypto')
|
|
5
5
|
|
|
6
|
-
const serverDao = require('
|
|
6
|
+
const serverDao = require('./serverDao.js')
|
|
7
7
|
const { hashCode, encodeNumber, uidGenerator } = require('@live-change/uid')
|
|
8
|
-
const getIp = require('
|
|
8
|
+
const getIp = require('./getIp.js')
|
|
9
9
|
|
|
10
10
|
const Renderer = require('./Renderer.js')
|
|
11
11
|
|
|
@@ -92,6 +92,9 @@ class SsrServer {
|
|
|
92
92
|
})
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
async close() {
|
|
96
|
+
await this.renderer.close()
|
|
97
|
+
}
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
module.exports = SsrServer
|
package/lib/TestServer.js
CHANGED
|
@@ -4,6 +4,8 @@ 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')
|
|
@@ -23,9 +25,13 @@ class TestServer {
|
|
|
23
25
|
path.resolve(this.config.ssrRoot, 'dist/client/ssr-manifest.json')
|
|
24
26
|
)
|
|
25
27
|
|
|
28
|
+
app.instanceId = encodeNumber(hashCode(
|
|
29
|
+
`app${process.pid}${require("os").hostname()} ${process.cwd()}/${process.argv.join(' ')}`))
|
|
30
|
+
app.uidGenerator = uidGenerator(app.instanceId, 1)
|
|
26
31
|
this.dbServer = await setupDbServer({ dbBackend: 'mem' })
|
|
27
|
-
|
|
28
|
-
app.dao =
|
|
32
|
+
const loopbackDao = await createLoopbackDao('local', () => this.dbServer.createDao('local'))
|
|
33
|
+
app.dao = loopbackDao
|
|
34
|
+
app.databaseName = 'test'
|
|
29
35
|
|
|
30
36
|
await app.dao.request(['database', 'createDatabase'], app.databaseName, { }).catch(err => 'exists')
|
|
31
37
|
|
|
@@ -47,11 +53,11 @@ class TestServer {
|
|
|
47
53
|
await this.ssrServer.start()
|
|
48
54
|
|
|
49
55
|
this.expressServer = http.createServer(this.expressApp)
|
|
50
|
-
this.services = this.apiServer.services.getServicesObject()
|
|
56
|
+
this.services = this.apiServer.services.getServicesObject()
|
|
51
57
|
|
|
52
58
|
this.wsServer = await setupApiWs(this.expressServer, this.apiServer)
|
|
53
59
|
this.sockJsServer = await setupApiSockJs(this.expressServer, this.apiServer)
|
|
54
|
-
|
|
60
|
+
|
|
55
61
|
await new Promise((resolve, reject) => {
|
|
56
62
|
this.httpServer = this.expressServer.listen(this.config.port || 0, () => {
|
|
57
63
|
this.port = this.expressServer.address().port,
|
|
@@ -64,11 +70,22 @@ class TestServer {
|
|
|
64
70
|
return await createLoopbackDao(credentials, () => this.apiServer.daoFactory(credentials, ip))
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
dispose() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
+
}
|
|
72
89
|
}
|
|
73
90
|
}
|
|
74
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
|
package/lib/serverDao.js
ADDED
|
@@ -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
|
package/lib/setupApiServer.js
CHANGED
|
@@ -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
|
|
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 =
|
|
48
|
-
|
|
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
|
}
|