@live-change/server 0.1.9 → 0.1.13
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 +13 -5
- 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 +1 -4
- package/package.json +2 -2
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
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
const cookie = require('cookie')
|
|
2
2
|
const path = require('path')
|
|
3
3
|
const serveStatic = require('serve-static')
|
|
4
|
+
const crypto = require('crypto')
|
|
4
5
|
|
|
5
|
-
const serverDao = require('
|
|
6
|
-
const
|
|
7
|
-
const getIp = require('
|
|
6
|
+
const serverDao = require('./serverDao.js')
|
|
7
|
+
const { hashCode, encodeNumber, uidGenerator } = require('@live-change/uid')
|
|
8
|
+
const getIp = require('./getIp.js')
|
|
8
9
|
|
|
9
10
|
const Renderer = require('./Renderer.js')
|
|
10
11
|
|
|
@@ -17,6 +18,10 @@ class SsrServer {
|
|
|
17
18
|
this.express = express
|
|
18
19
|
this.renderer = new Renderer(manifest, settings)
|
|
19
20
|
|
|
21
|
+
this.instanceId = encodeNumber(hashCode(
|
|
22
|
+
`ssr${process.pid}${require("os").hostname()} ${process.cwd()}/${process.argv.join(' ')}`))
|
|
23
|
+
this.uidGenerator = uidGenerator(this.instanceId, 1)
|
|
24
|
+
|
|
20
25
|
this.root = this.settings.root || process.cwd()
|
|
21
26
|
}
|
|
22
27
|
|
|
@@ -36,7 +41,7 @@ class SsrServer {
|
|
|
36
41
|
async setupSsr() {
|
|
37
42
|
const readCredentials = this.settings.readCredentials || ((req) => {
|
|
38
43
|
const cookies = cookie.parse(req.headers.cookie || '')
|
|
39
|
-
return { sessionKey: cookies.sessionKey ||
|
|
44
|
+
return { sessionKey: cookies.sessionKey || crypto.randomBytes(64).toString('base64').slice(0, 48) }
|
|
40
45
|
})
|
|
41
46
|
const writeCredentials = this.settings.writeCredentials || ((res, credentials) => {
|
|
42
47
|
//console.log("WRITE CREDENTIALS", credentials)
|
|
@@ -54,7 +59,7 @@ class SsrServer {
|
|
|
54
59
|
const clientIp = getIp(req)
|
|
55
60
|
|
|
56
61
|
const credentials = readCredentials(req)
|
|
57
|
-
const windowId =
|
|
62
|
+
const windowId = this.uidGenerator()
|
|
58
63
|
try {
|
|
59
64
|
let dao
|
|
60
65
|
if(this.settings.daoFactory) {
|
|
@@ -87,6 +92,9 @@ class SsrServer {
|
|
|
87
92
|
})
|
|
88
93
|
}
|
|
89
94
|
|
|
95
|
+
async close() {
|
|
96
|
+
await this.renderer.close()
|
|
97
|
+
}
|
|
90
98
|
}
|
|
91
99
|
|
|
92
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
|
@@ -7,17 +7,14 @@ const createLoopbackDao = require('./createLoopbackDao.js')
|
|
|
7
7
|
async function setupApp(settings, env = process.env) {
|
|
8
8
|
const app = require("@live-change/framework").app()
|
|
9
9
|
app.instanceId = encodeNumber(hashCode(
|
|
10
|
-
|
|
10
|
+
`app${process.pid}${require("os").hostname()} ${process.cwd()}/${process.argv.join(' ')}`))
|
|
11
11
|
app.uidGenerator = uidGenerator(app.instanceId, 1)
|
|
12
12
|
console.log("SETUP APP", settings)
|
|
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.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "Live Change Framework - server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@live-change/db-server": "^0.4.80",
|
|
27
27
|
"@live-change/framework": "^0.5.0",
|
|
28
28
|
"@live-change/vue3-ssr": "^0.1.0",
|
|
29
|
-
"@live-change/uid": "^0.1.
|
|
29
|
+
"@live-change/uid": "^0.1.3",
|
|
30
30
|
"http-proxy-middleware": "2.0.0",
|
|
31
31
|
"dotenv": "^9.0.2",
|
|
32
32
|
"express": "^4.17.1",
|