@webqit/webflo 1.0.22 → 1.0.24

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/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "vanila-javascript"
13
13
  ],
14
14
  "homepage": "https://webqit.io/tooling/webflo",
15
- "version": "1.0.22",
15
+ "version": "1.0.24",
16
16
  "license": "MIT",
17
17
  "repository": {
18
18
  "type": "git",
@@ -49,7 +49,7 @@ export class WebfloServer extends WebfloRuntime {
49
49
  #cx;
50
50
  #servers = new Map;
51
51
  #proxies = new Map;
52
- #capabilitiesPromise;
52
+ #capabilitiesSetup;
53
53
 
54
54
  // Typically for access by Router
55
55
  get cx() { return this.#cx; }
@@ -102,53 +102,6 @@ export class WebfloServer extends WebfloRuntime {
102
102
  }));
103
103
  }
104
104
  // ---------------
105
- const setupCapabilities = async () => {
106
- if (this.#cx.server.capabilities?.database) {
107
- if (this.#cx.server.capabilities.database_dialect !== 'postgres') {
108
- throw new Error(`Only postgres supported for now for database dialect`);
109
- }
110
- if (this.#cx.env.entries[this.#cx.server.capabilities.database_url_variable]) {
111
- console.log('Database capabilities');
112
- const { SQLClient } = await import('@linked-db/linked-ql/sql');
113
- const { default: pg } = await import('pg');
114
- // Obtain pg client
115
- const pgClient = new pg.Pool({
116
- connectionString: this.#cx.env.entries[this.#cx.server.capabilities.database_url_variable],
117
- database: 'postgres',
118
- });
119
- // Connect
120
- await pgClient.connect();
121
- this.#sdk.db = new SQLClient(pgClient, { dialect: 'postgres' });
122
- } else {
123
- console.log('No database capabilities');
124
- //const { ODBClient } = await import('@linked-db/linked-ql/odb');
125
- //this.#sdk.db = new ODBClient({ dialect: 'postgres' });
126
- }
127
- }
128
- if (this.#cx.server.capabilities?.redis && this.#cx.env.entries[this.#cx.server.capabilities.redis_url_variable]) {
129
- const { Redis } = await import('ioredis');
130
- this.#sdk.redis = !this.#cx.env.entries[this.#cx.server.capabilities.redis_url_variable]
131
- ? new Redis : new Redis(this.#cx.env.entries[this.#cx.server.capabilities.redis_url_variable], {
132
- tls: { rejectUnauthorized: false }, // Required for Upstash
133
- });
134
- console.log('Redis capabilities');
135
- }
136
- if (this.#cx.server.capabilities?.webpush) {
137
- const { default: webpush } = await import('web-push');
138
- console.log('Webpuah capabilities');
139
- this.#sdk.webpush = webpush;
140
- if (this.#cx.env.entries[this.#cx.server.capabilities.vapid_public_key_variable]
141
- && this.#cx.env.entries[this.#cx.server.capabilities.vapid_private_key_variable]) {
142
- webpush.setVapidDetails(
143
- this.#cx.server.capabilities.vapid_subject,
144
- this.#cx.env.entries[this.#cx.server.capabilities.vapid_public_key_variable],
145
- this.#cx.env.entries[this.#cx.server.capabilities.vapid_private_key_variable]
146
- );
147
- }
148
- }
149
- };
150
- this.#capabilitiesPromise = setupCapabilities();
151
- // ---------------
152
105
  this.control();
153
106
  if (this.#cx.logger) {
154
107
  if (this.#servers.size) {
@@ -222,13 +175,61 @@ export class WebfloServer extends WebfloRuntime {
222
175
  const wss = new WebSocket.Server({ noServer: true });
223
176
  }
224
177
 
178
+ async setupCapabilities() {
179
+ if (this.#capabilitiesSetup) return;
180
+ this.#capabilitiesSetup = true;
181
+ if (this.#cx.server.capabilities?.database) {
182
+ if (this.#cx.server.capabilities.database_dialect !== 'postgres') {
183
+ throw new Error(`Only postgres supported for now for database dialect`);
184
+ }
185
+ if (process.env[this.#cx.server.capabilities.database_url_variable]) {
186
+ console.log('Database capabilities');
187
+ const { SQLClient } = await import('@linked-db/linked-ql/sql');
188
+ const { default: pg } = await import('pg');
189
+ // Obtain pg client
190
+ const pgClient = new pg.Pool({
191
+ connectionString: process.env[this.#cx.server.capabilities.database_url_variable],
192
+ database: 'postgres',
193
+ });
194
+ // Connect
195
+ await pgClient.connect();
196
+ this.#sdk.db = new SQLClient(pgClient, { dialect: 'postgres' });
197
+ } else {
198
+ console.log('No database capabilities');
199
+ //const { ODBClient } = await import('@linked-db/linked-ql/odb');
200
+ //this.#sdk.db = new ODBClient({ dialect: 'postgres' });
201
+ }
202
+ }
203
+ if (this.#cx.server.capabilities?.redis && process.env[this.#cx.server.capabilities.redis_url_variable]) {
204
+ const { Redis } = await import('ioredis');
205
+ this.#sdk.redis = !process.env[this.#cx.server.capabilities.redis_url_variable]
206
+ ? new Redis : new Redis(process.env[this.#cx.server.capabilities.redis_url_variable], {
207
+ tls: { rejectUnauthorized: false }, // Required for Upstash
208
+ });
209
+ console.log('Redis capabilities');
210
+ }
211
+ if (this.#cx.server.capabilities?.webpush) {
212
+ const { default: webpush } = await import('web-push');
213
+ console.log('Webpuah capabilities');
214
+ this.#sdk.webpush = webpush;
215
+ if (process.env[this.#cx.server.capabilities.vapid_public_key_variable]
216
+ && process.env[this.#cx.server.capabilities.vapid_private_key_variable]) {
217
+ webpush.setVapidDetails(
218
+ this.#cx.server.capabilities.vapid_subject,
219
+ process.env[this.#cx.server.capabilities.vapid_public_key_variable],
220
+ process.env[this.#cx.server.capabilities.vapid_private_key_variable]
221
+ );
222
+ }
223
+ }
224
+ }
225
+
225
226
  getRequestProto(nodeRequest) {
226
227
  return nodeRequest.connection.encrypted ? 'https' : (nodeRequest.headers['x-forwarded-proto'] || 'http');
227
228
  }
228
229
 
229
230
  #globalMessagingRegistry = new Map;
230
231
  async handleNodeWsRequest(wss, nodeRequest, socket, head) {
231
- await this.#capabilitiesPromise;
232
+ await this.setupCapabilities();
232
233
  const proto = this.getRequestProto(nodeRequest).replace('http', 'ws');
233
234
  const [fullUrl, requestInit] = this.parseNodeRequest(proto, nodeRequest, false);
234
235
  const scope = {};
@@ -259,7 +260,7 @@ export class WebfloServer extends WebfloRuntime {
259
260
  // Level 3 validation
260
261
  // and actual processing
261
262
  scope.request = this.createRequest(scope.url.href, requestInit);
262
- scope.session = this.constructor.SessionStorage.create(scope.request, { secret: this.#cx.env.entries[this.#cx.server.session_key_variable] });
263
+ scope.session = this.constructor.SessionStorage.create(scope.request, { secret: process.env[this.#cx.server.session_key_variable] });
263
264
  if (!scope.error) {
264
265
  if (!(scope.clientMessagingRegistry = this.#globalMessagingRegistry.get(scope.session.sessionID))) {
265
266
  scope.error = `Lost or invalid clientID`;
@@ -289,7 +290,7 @@ export class WebfloServer extends WebfloRuntime {
289
290
  }
290
291
 
291
292
  async handleNodeHttpRequest(nodeRequest, nodeResponse) {
292
- await this.#capabilitiesPromise;
293
+ await this.setupCapabilities();
293
294
  const proto = this.getRequestProto(nodeRequest);
294
295
  const [fullUrl, requestInit] = this.parseNodeRequest(proto, nodeRequest);
295
296
  const scope = {};
@@ -559,7 +560,7 @@ export class WebfloServer extends WebfloRuntime {
559
560
  scope.request = this.createRequest(scope.url.href, scope.init, scope.autoHeaders.filter((header) => header.type === 'request'));
560
561
  scope.cookies = this.constructor.CookieStorage.create(scope.request);
561
562
  scope.session = this.constructor.SessionStorage.create(scope.request, {
562
- secret: this.#cx.env.entries[this.#cx.server.session_key_variable],
563
+ secret: process.env[this.#cx.server.session_key_variable],
563
564
  registry: this.#sdk.redis && {
564
565
  get: async (key) => { return await this.#sdk.redis.hgetall(key) },
565
566
  set: async (key, value) => { return await this.#sdk.redis.hset(key, value) },