@cocreate/lazy-loader 1.14.0 → 1.15.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.14.0...v1.15.0) (2024-01-08)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * applied host to define environment/branch ([2e830bf](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/2e830bfbc3a6ab08b4d67b7f756e19f7933d412a))
7
+ * hostNotFound handled by fileServer ([4773bb8](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/4773bb8466e6f5ae469ab9b6687a0c52e74ef992))
8
+ * init logging ([40c1157](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/40c11576f402593c8dff92414959c7f1e96948f7))
9
+
10
+
11
+ ### Features
12
+
13
+ * bumped CoCreate dependencies to their latest versions ([8331382](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/83313829d28f95071cee5704164a35745067278a))
14
+ * use crud.getHost and crud.getorganization ([142715e](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/142715e931e3e56bf841d5c2bcf061a0e3d56af2))
15
+
1
16
  # [1.14.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.13.2...v1.14.0) (2024-01-03)
2
17
 
3
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "A simple lazy-loader component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "lazy-loader",
@@ -59,6 +59,6 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@cocreate/config": "^1.10.0",
62
- "@cocreate/observer": "^1.14.0"
62
+ "@cocreate/observer": "^1.15.0"
63
63
  }
64
64
  }
package/src/server.js CHANGED
@@ -4,8 +4,7 @@ const vm = require('vm');
4
4
  const Config = require("@cocreate/config");
5
5
  const { URL } = require('url');
6
6
 
7
- const organizations = {};
8
- const hosts = {};
7
+ // const organizations = {};
9
8
 
10
9
  class CoCreateLazyLoader {
11
10
  constructor(server, crud, files) {
@@ -22,16 +21,10 @@ class CoCreateLazyLoader {
22
21
  const scriptsDirectory = './scripts';
23
22
 
24
23
  try {
25
- await fs.access(directory);
26
- console.log("The directory exists.");
24
+ await fs.mkdir(scriptsDirectory, { recursive: true });
27
25
  } catch (error) {
28
- try {
29
- await fs.mkdir(scriptsDirectory, { recursive: true });
30
- console.log(`Scripts directory created at ${scriptsDirectory}`);
31
- } catch (error) {
32
- console.error('Error creating scripts directory:', error);
33
- throw error; // Halt execution if directory creation fails
34
- }
26
+ console.error('Error creating scripts directory:', error);
27
+ throw error; // Halt execution if directory creation fails
35
28
  }
36
29
 
37
30
  this.modules = await Config('modules', false, false)
@@ -56,36 +49,14 @@ class CoCreateLazyLoader {
56
49
  const valideUrl = new URL(`http://${req.headers.host}${req.url}`);
57
50
  const hostname = valideUrl.hostname;
58
51
 
59
- let organization = hosts[hostname];
60
- if (!organization) {
61
- let org = await this.crud.send({
62
- method: 'object.read',
63
- array: 'organizations',
64
- $filter: {
65
- query: [
66
- { key: "host", value: [hostname], operator: "$in" }
67
- ]
68
- },
69
- organization_id: process.env.organization_id
70
- })
71
-
72
- if (!org || !org.object || !org.object[0]) {
73
- // TODO: hostNotFound is not defined
74
- if (!hostNotFound)
75
- hostNotFound = await getDefaultFile('/hostNotFound.html')
76
- return sendResponse(hostNotFound.object[0].src, 404, { 'Content-Type': 'text/html', 'storage': organization.storage })
77
- } else {
78
- organization = org.object[0]
79
- organizations[organization._id] = organization
80
- }
81
- }
82
-
83
- hosts[hostname] = organization
52
+ let organization = await this.crud.getHost(hostname);
53
+ if (organization.error)
54
+ return this.files.send(req, res, this.crud, organization, valideUrl)
84
55
 
85
56
  if (valideUrl.pathname.startsWith('/webhooks/')) {
86
57
  let name = req.url.split('/')[2]; // Assuming URL structure is /webhook/name/...
87
58
  if (this.modules[name]) {
88
- this.executeScriptWithTimeout(name, { req, res, organization, valideUrl, organization_id: organization._id })
59
+ this.executeScriptWithTimeout(name, { req, res, host: hostname, organization, valideUrl, organization_id: organization._id })
89
60
  } else {
90
61
  // Handle unknown module or missing webhook method
91
62
  res.writeHead(404, { 'Content-Type': 'application/json' });
@@ -163,31 +134,14 @@ class CoCreateLazyLoader {
163
134
  }
164
135
 
165
136
  async getApiKey(organization_id, name) {
166
- organizations[organization_id] = this.getOrganization(organization_id, name)
167
- organizations[organization_id] = await organizations[organization_id]
168
- return organizations[organization_id][name]
169
- }
170
-
171
- async getOrganization(organization_id) {
172
- let organization = await this.crud.send({
173
- method: 'object.read',
174
- database: organization_id,
175
- array: 'organizations',
176
- object: [{ _id: organization_id }],
177
- organization_id
178
- })
179
-
180
- if (organization
181
- && organization.object
182
- && organization.object[0]) {
183
- if (organization.object[0].apis) {
184
- return organization.object[0].apis
185
- } else
186
- return { error: 'No apis defined could not be found' }
187
- } else {
188
- return { serverOrganization: false, error: 'An organization could not be found' }
189
- }
190
-
137
+ let organization = await this.crud.getOrganization(organization_id, false);
138
+ if (!organization.error)
139
+ return organization.error
140
+ else if (!organization.apis)
141
+ return { error: 'Missing apis object in organization object' }
142
+ if (!organization.apis[name])
143
+ return { error: `Missing ${name} in organization apis object` }
144
+ return organization.apis[name]
191
145
  }
192
146
 
193
147
  }
@@ -250,6 +204,7 @@ function clearModuleCache(name) {
250
204
  async function fetchScriptFromDatabaseAndSave(name, moduleConfig) {
251
205
  let data = {
252
206
  method: 'object.read',
207
+ host: moduleConfig.object.hostname,
253
208
  array: moduleConfig.array,
254
209
  $filter: {
255
210
  query: [