@live-change/server 0.1.8 → 0.1.12

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
@@ -1,3 +1,4 @@
1
+ const { def } = require('@vue/shared')
1
2
  const fs = require('fs')
2
3
  const path = require('path')
3
4
  const resolve = require('util').promisify(require('resolve'))
@@ -5,7 +6,7 @@ const app = require("@live-change/framework").app()
5
6
 
6
7
  class Services {
7
8
  constructor(configPath) {
8
- if(!configPath) throw new Error("services config parameter is required")
9
+ if(!configPath) throw new Error("services config parameter is required")
9
10
  this.configPath = path.resolve(configPath)
10
11
  this.config = require(path.resolve(this.configPath))
11
12
  this.servicesDir = path.dirname(this.configPath)
@@ -21,7 +22,7 @@ class Services {
21
22
  return path
22
23
  }
23
24
  async getServiceEntryFile(config) {
24
- const path = await resolve(config.path, { basedir: this.servicesDir })
25
+ const path = await resolve(config.path, { basedir: this.servicesDir })
25
26
  console.log("PATH RESOLVE", config.path, "IN", this.servicesDir, "=>", path)
26
27
  return path
27
28
  }
@@ -104,7 +105,10 @@ class Services {
104
105
 
105
106
  async update() {
106
107
  await Promise.all(this.serviceDefinitions.map(defn => {
107
- app.processServiceDefinition(defn, [ ...app.defaultProcessors ])
108
+ if(!defn.processed) {
109
+ app.processServiceDefinition(defn, [ ...app.defaultProcessors ])
110
+ defn.processed = true
111
+ }
108
112
  return app.updateService(defn)
109
113
  }))
110
114
  }
@@ -112,7 +116,10 @@ class Services {
112
116
  async start(startOptions) {
113
117
  await Promise.all(this.plugins.map(plugin => plugin(app, this)))
114
118
  this.services = await Promise.all(this.serviceDefinitions.map(defn => {
115
- app.processServiceDefinition(defn, [ ...app.defaultProcessors ])
119
+ if(!defn.processed) {
120
+ app.processServiceDefinition(defn, [ ...app.defaultProcessors ])
121
+ defn.processed = true
122
+ }
116
123
  return app.startService(defn, startOptions)
117
124
  }))
118
125
  }
package/lib/SsrServer.js CHANGED
@@ -1,9 +1,10 @@
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
6
  const serverDao = require('@live-change/vue3-ssr/serverDao.js')
6
- const generateUuid = require('@live-change/vue3-ssr/generateUuid.js')
7
+ const { hashCode, encodeNumber, uidGenerator } = require('@live-change/uid')
7
8
  const getIp = require('@live-change/vue3-ssr/getIp.js')
8
9
 
9
10
  const Renderer = require('./Renderer.js')
@@ -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,24 +41,25 @@ 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 || generateUuid() }
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
- console.log("WRC!!!")
47
+ //console.log("WRITE CREDENTIALS", credentials)
43
48
  const cookieExpireDate =
44
49
  this.settings.sessionExpires ? new Date(Date.now() + this.settings.sessionExpires).toUTCString() : null
45
- res.set({
46
- 'Content-Type': 'text/html',
47
- 'Set-Cookie': `sessionKey=${credentials.sessionKey}; path=/`
48
- + cookieExpireDate ? `; expires=${cookieExpireDate}` : ''
49
- })
50
+ if(credentials.sessionKey) {
51
+ res.set({
52
+ 'Set-Cookie': `sessionKey=${credentials.sessionKey}; Path=/`
53
+ + (cookieExpireDate ? `; Expires=${cookieExpireDate}` : '')
54
+ })
55
+ }
50
56
  })
51
57
  this.express.use('*', async (req, res) => {
52
58
  const url = req.originalUrl
53
59
  const clientIp = getIp(req)
54
60
 
55
61
  const credentials = readCredentials(req)
56
- const windowId = generateUuid()
62
+ const windowId = this.uidGenerator()
57
63
  try {
58
64
  let dao
59
65
  if(this.settings.daoFactory) {
@@ -74,6 +80,9 @@ class SsrServer {
74
80
 
75
81
  res.status(200)
76
82
  writeCredentials(res, credentials)
83
+ res.set({
84
+ 'Content-Type': 'text/html'
85
+ })
77
86
  res.end(html)
78
87
  } catch (e) {
79
88
  this.renderer.fixStackTrace(e)
@@ -83,6 +92,9 @@ class SsrServer {
83
92
  })
84
93
  }
85
94
 
95
+ async close() {
96
+ await this.renderer.close()
97
+ }
86
98
  }
87
99
 
88
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
- app.dao.dispose()
28
- app.dao = await createLoopbackDao('local', () => this.dbServer.createDao('local') )
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
- this.httpServer.close()
69
- this.dbServer.close()
70
- //this.wsServer.close()
71
- //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
+ }
72
89
  }
73
90
  }
74
91
 
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
- `${process.pid}${require("os").hostname()} ${process.cwd()}/${process.argv.join(' ')}`))
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.8",
3
+ "version": "0.1.12",
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.2",
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",