@live-change/server 0.8.9 → 0.8.11

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/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019-2024 Michał Łaszczewski
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/lib/Renderer.js CHANGED
@@ -128,7 +128,7 @@ class Renderer {
128
128
  }
129
129
  }
130
130
 
131
- async getSitemap() {
131
+ async getSitemapRenderFunction() {
132
132
  if(this.settings.dev) {
133
133
  /// Reload every request
134
134
  const entryPath = path.resolve(this.root, this.settings.serverEntry || 'src/entry-server.js')
@@ -138,19 +138,22 @@ class Renderer {
138
138
  }
139
139
  }
140
140
 
141
- async renderSitemap({ dao }, res) {
141
+ async renderSitemap(params, res) {
142
142
  try {
143
+ const { url, headers, dao, clientIp, credentials, windowId, version, now } = params
144
+
143
145
  res.header('Content-Type', 'application/xml')
144
146
  res.status(200)
145
147
  const smStream = new SitemapStream({ hostname: (process.env.BASE_HREF || "https://sitemap.com")+'/' })
146
148
  smStream.pipe(res)
147
- const sitemapFunction = await this.getSitemap()
148
- const { sitemap, router } = await sitemapFunction({ dao })
149
- function route(location, opts) {
150
- smStream.write({ url: router.resolve(location).href, changefreq: 'daily', priority: 0.5, ...opts })
149
+ const sitemapFunction = await this.getSitemapRenderFunction()
150
+
151
+ function write(routeInfo) {
152
+ console.log("SM WRITE", routeInfo)
153
+ smStream.write(routeInfo)
151
154
  }
152
- console.log("SR", sitemap, router)
153
- await sitemap(route)
155
+
156
+ await sitemapFunction(params, write)
154
157
  //route({ name: 'index' })
155
158
  smStream.end()
156
159
  } catch(err) {
package/lib/Services.js CHANGED
@@ -86,6 +86,16 @@ class Services {
86
86
  }
87
87
  }
88
88
  }
89
+ // push validators from services to other services
90
+ for(const sourceServiceDefinition of this.serviceDefinitions) {
91
+ for(const validatorName in sourceServiceDefinition.validators) {
92
+ for(const destinationServiceDefinition of this.serviceDefinitions) {
93
+ if(!destinationServiceDefinition.validators[validatorName]) {
94
+ destinationServiceDefinition.validators[validatorName] = sourceServiceDefinition.validators[validatorName]
95
+ }
96
+ }
97
+ }
98
+ }
89
99
 
90
100
  /// TODO: load dependent services!!!
91
101
  }
package/lib/SsrServer.js CHANGED
@@ -81,7 +81,11 @@ class SsrServer {
81
81
  async setupSsr() {
82
82
  const readCredentials = this.settings.readCredentials || ((req) => {
83
83
  const cookies = cookie.parse(req.headers.cookie || '')
84
- return { sessionKey: cookies.sessionKey || crypto.randomBytes(64).toString('base64').slice(0, 48) }
84
+ return {
85
+ sessionKey: req.query.sessionKey
86
+ || cookies.sessionKey
87
+ || crypto.randomBytes(64).toString('base64').slice(0, 48)
88
+ }
85
89
  })
86
90
  const writeCredentials = this.settings.writeCredentials || ((res, credentials) => {
87
91
  //console.log("WRITE CREDENTIALS", credentials)
@@ -96,10 +100,33 @@ class SsrServer {
96
100
  })
97
101
 
98
102
  this.express.get('/sitemap.xml', async (req, res) => {
99
- const sitemap = await this.renderer.getSitemap()
103
+ if(this.settings.spa) {
104
+ res.status(404).end()
105
+ return
106
+ }
107
+
108
+ const url = req.originalUrl
100
109
  const clientIp = getIp(req)
101
- const dao = await this.createDao({ sessionKey: 'sitemap' })
102
- this.renderer.renderSitemap({ dao, clientIp }, res)
110
+ const credentials = readCredentials(req)
111
+ const windowId = this.uidGenerator()
112
+ const now = Date.now()
113
+
114
+ try {
115
+ const dao = await this.createDao({ sessionKey: 'sitemap' }, clientIp)
116
+ try {
117
+ const version = this.version
118
+ await this.renderer.renderSitemap({
119
+ url, headers: req.headers, dao, clientIp, credentials, windowId, version, now
120
+ }, res)
121
+ } catch (e) {
122
+ console.error("SITEMAP RENDERING ERROR", e.stack || e)
123
+ res.status(500).end(e.stack || e)
124
+ }
125
+ dao.dispose()
126
+ } catch (e) {
127
+ console.error("SITEMAP DAO ERROR", e.stack || e)
128
+ res.status(500).end(e.stack || e)
129
+ }
103
130
  })
104
131
  this.express.use('*', async (req, res) => {
105
132
  if(fbRedirect(req, res)) return
@@ -116,48 +143,55 @@ class SsrServer {
116
143
 
117
144
  const credentials = readCredentials(req)
118
145
  const windowId = this.uidGenerator()
119
- let dao
120
- try {
121
- dao = await this.createDao(credentials, clientIp)
122
- } catch (e) {
123
- console.error("DAO ERROR", e.stack || e)
124
- res.status(500).end(e.stack || e)
125
- }
146
+
126
147
  try {
127
- const version = this.version
148
+ const dao = await this.createDao(credentials, clientIp)
149
+
150
+ //dao.dispose(); return res.end('dao test!!\n')
151
+
152
+ try {
153
+ const version = this.version
154
+
155
+ let result
156
+ let error
157
+
158
+ for(let retry = 0; retry < 3; retry ++) {
159
+ try {
160
+ const now = Date.now()
161
+ result = await this.renderer.renderPage({
162
+ url, headers: req.headers, dao, clientIp, credentials, windowId, version, now
163
+ })
164
+ break
165
+ } catch(e) {
166
+ error = e
167
+ }
168
+ }
128
169
 
129
- let result
130
- let error
170
+ //dao.dispose(); return res.end('render page test!!\n')
131
171
 
132
- for(let retry = 0; retry < 3; retry ++) {
133
- try {
134
- const now = Date.now()
135
- result = await this.renderer.renderPage({
136
- url, headers: req.headers, dao, clientIp, credentials, windowId, version, now
172
+ if(result) {
173
+ const { html, response } = result
174
+ res.status(response?.status || 200)
175
+ writeCredentials(res, credentials)
176
+ res.set(response?.headers ?? {
177
+ 'Content-Type': 'text/html'
137
178
  })
138
- break
139
- } catch(e) {
140
- error = e
179
+ res.end(html)
180
+ } else {
181
+ if(error.stack) this.renderer.fixStackTrace(error)
182
+ console.error("RENDERING ERROR", error.stack || error)
183
+ res.status(500).end(error.stack || error)
141
184
  }
185
+ } catch (e) {
186
+ console.error("ERROR", e.stack || e)
187
+ res.status(500).end(e.stack || e)
142
188
  }
143
- if(result) {
144
- const { html, response } = result
145
- res.status(response?.status || 200)
146
- writeCredentials(res, credentials)
147
- res.set(response?.headers ?? {
148
- 'Content-Type': 'text/html'
149
- })
150
- res.end(html)
151
- } else {
152
- if(error.stack) this.renderer.fixStackTrace(error)
153
- console.error("RENDERING ERROR", error.stack || error)
154
- res.status(500).end(error.stack || error)
155
- }
189
+
190
+ dao.dispose()
156
191
  } catch (e) {
157
- console.error("ERROR", e.stack || e)
192
+ console.error("DAO ERROR", e.stack || e)
158
193
  res.status(500).end(e.stack || e)
159
194
  }
160
- dao.dispose()
161
195
  })
162
196
  }
163
197
 
@@ -3,6 +3,7 @@ import Dao from "@live-change/dao"
3
3
  async function createLoopbackDao(credentials, daoFactory) {
4
4
  const server = new Dao.ReactiveServer(daoFactory)
5
5
  const loopback = new Dao.LoopbackConnection(credentials, server, {})
6
+
6
7
  const dao = new Dao(credentials, {
7
8
  remoteUrl: 'dao',
8
9
  protocols: { local: null },
@@ -15,13 +16,20 @@ async function createLoopbackDao(credentials, daoFactory) {
15
16
  logLevel: 10,
16
17
  },
17
18
  })
19
+
18
20
  dao.connections.set('local:dao', loopback)
21
+
19
22
  await loopback.initialize()
23
+
24
+ //dao.dispose(); return null
25
+
20
26
  if(!loopback.connected) {
21
27
  console.error("LOOPBACK NOT CONNECTED?!")
22
28
  process.exit(1)
23
29
  }
30
+
24
31
  return dao
32
+
25
33
  }
26
34
 
27
35
  export default createLoopbackDao
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/server",
3
- "version": "0.8.9",
3
+ "version": "0.8.11",
4
4
  "description": "Live Change Framework - server",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,12 +22,12 @@
22
22
  "type": "module",
23
23
  "homepage": "https://github.com/live-change/live-change-framework",
24
24
  "dependencies": {
25
- "@live-change/dao": "^0.8.9",
26
- "@live-change/dao-sockjs": "^0.8.9",
27
- "@live-change/db-server": "^0.8.9",
28
- "@live-change/framework": "^0.8.9",
25
+ "@live-change/dao": "^0.8.11",
26
+ "@live-change/dao-sockjs": "^0.8.11",
27
+ "@live-change/db-server": "^0.8.11",
28
+ "@live-change/framework": "^0.8.11",
29
29
  "@live-change/sockjs": "0.4.1",
30
- "@live-change/uid": "^0.8.9",
30
+ "@live-change/uid": "^0.8.11",
31
31
  "dotenv": "^16.4.4",
32
32
  "express": "^4.18.2",
33
33
  "express-static-gzip": "2.1.7",
@@ -39,5 +39,5 @@
39
39
  "websocket": "^1.0.34",
40
40
  "yargs": "^17.7.2"
41
41
  },
42
- "gitHead": "362dd55bd2773195e4cefd84080c10bd9fbaaf45"
42
+ "gitHead": "c69f8c0c5b1f9c8b62d74bd44e97dccbdc789c5a"
43
43
  }