@cocreate/file-server 1.14.1 → 1.15.1

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +1 -1
  3. package/src/index.js +160 -189
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.15.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.15.0...v1.15.1) (2023-12-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * removed organizations map and url module ([a82bc56](https://github.com/CoCreate-app/CoCreate-file-server/commit/a82bc56a8e82f9fdabfdeb7592e140e4c3ad3aff))
7
+
8
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.14.1...v1.15.0) (2023-12-09)
9
+
10
+
11
+ ### Features
12
+
13
+ * updated to be used by lazyloader default ([ff8f720](https://github.com/CoCreate-app/CoCreate-file-server/commit/ff8f720ea51c20c6c5f9d44182351180ad320e6a))
14
+
1
15
  ## [1.14.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.14.0...v1.14.1) (2023-11-25)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.14.1",
3
+ "version": "1.15.1",
4
4
  "description": "A simple file-server component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "file-server",
package/src/index.js CHANGED
@@ -20,227 +20,198 @@
20
20
  // you must obtain a commercial license from CoCreate LLC.
21
21
  // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
22
22
 
23
- const { URL } = require('url');
23
+ class CoCreateFileSystem {
24
+ constructor(render) {
25
+ this.render = render
26
+ }
24
27
 
25
- const organizations = new Map();
28
+ async send(req, res, crud, organization, valideUrl) {
29
+ try {
30
+ const organization_id = organization._id
31
+ const hostname = valideUrl.hostname;
26
32
 
27
- class CoCreateFileSystem {
28
- constructor(server, crud, render) {
29
-
30
- let hostNotFound
31
-
32
- server.on('request', async (req, res) => {
33
- try {
34
- const valideUrl = new URL(`http://${req.headers.host}${req.url}`);
35
- const hostname = valideUrl.hostname;
36
-
37
- let organization = organizations.get(hostname);
38
- if (!organization) {
39
- let org = await crud.send({
40
- method: 'object.read',
41
- array: 'organizations',
42
- $filter: {
43
- query: [
44
- { key: "host", value: [hostname], operator: "$in" }
45
- ]
46
- },
47
- organization_id: process.env.organization_id
48
- })
49
-
50
- if (!org || !org.object || !org.object[0]) {
51
- if (!hostNotFound)
52
- hostNotFound = await getDefaultFile('/hostNotFound.html')
53
- return sendResponse(hostNotFound.object[0].src, 404, { 'Content-Type': 'text/html', 'storage': organization.storage })
54
- } else {
55
- organization = { _id: org.object[0]._id, storage: !!org.object[0].storage }
56
- organizations.set(hostname, organization)
57
- }
58
- }
33
+ res.setHeader('organization', organization_id)
34
+ res.setHeader('storage', organization.storage);
35
+ res.setHeader('Access-Control-Allow-Origin', '*');
36
+ res.setHeader('Access-Control-Allow-Methods', '');
37
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
59
38
 
60
- let organization_id = organization._id
39
+ let parameters = valideUrl.searchParams;
40
+ if (parameters.size) {
41
+ console.log('parameters', parameters)
42
+ }
61
43
 
62
- res.setHeader('organization', organization_id)
63
- res.setHeader('storage', organization.storage);
64
- res.setHeader('Access-Control-Allow-Origin', '*');
65
- res.setHeader('Access-Control-Allow-Methods', '');
66
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
44
+ let pathname = valideUrl.pathname;
45
+ if (pathname.endsWith('/')) {
46
+ pathname += "index.html";
47
+ } else {
48
+ let directory = pathname.split("/").slice(-1)[0];
49
+ if (!directory.includes('.'))
50
+ pathname += "/index.html";
51
+ }
67
52
 
68
- let parameters = valideUrl.searchParams;
69
- if (parameters.size) {
70
- console.log('parameters', parameters)
71
- }
53
+ let active = crud.wsManager.organizations.get(organization_id)
54
+ if (active === false) {
55
+ let balanceFalse = await getDefaultFile('/balanceFalse.html')
56
+ return sendResponse(balanceFalse.object[0].src, 403, { 'Content-Type': 'text/html', 'Account-Balance': 'false', 'storage': organization.storage })
57
+ }
72
58
 
73
- let pathname = valideUrl.pathname;
74
- if (pathname.endsWith('/')) {
75
- pathname += "index.html";
76
- } else {
77
- let directory = pathname.split("/").slice(-1)[0];
78
- if (!directory.includes('.'))
79
- pathname += "/index.html";
80
- }
59
+ let data = {
60
+ method: 'object.read',
61
+ array: 'files',
62
+ $filter: {
63
+ query: [
64
+ { key: "host", value: [hostname, '*'], operator: "$in" },
65
+ { key: "pathname", value: pathname, operator: "$eq" }
66
+ ],
67
+ limit: 1
68
+ },
69
+ organization_id
70
+ }
81
71
 
82
- let active = crud.wsManager.organizations.get(organization_id)
83
- if (active === false) {
84
- let balanceFalse = await getDefaultFile('/balanceFalse.html')
85
- return sendResponse(balanceFalse.object[0].src, 403, { 'Content-Type': 'text/html', 'Account-Balance': 'false', 'storage': organization.storage })
86
- }
72
+ let file
73
+ if (pathname.startsWith('/dist') || pathname.startsWith('/admin') || ['/403.html', '/404.html', '/offline.html', '/manifest.webmanifest', '/service-worker.js'].includes(pathname))
74
+ file = await getDefaultFile(pathname)
75
+ else
76
+ file = await crud.send(data);
87
77
 
88
- let data = {
78
+ if (!file || !file.object || !file.object[0]) {
79
+ let pageNotFound = await getDefaultFile('/404.html')
80
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
81
+ }
82
+
83
+ file = file.object[0]
84
+ if (!file['public'] || file['public'] === "false") {
85
+ let pageForbidden = await getDefaultFile('/403.html')
86
+ return sendResponse(pageForbidden.object[0].src, 403, { 'Content-Type': 'text/html' })
87
+ }
88
+
89
+ let src;
90
+ if (file['src'])
91
+ src = file['src'];
92
+ else {
93
+ let fileSrc = await crud.send({
89
94
  method: 'object.read',
90
- array: 'files',
91
- $filter: {
92
- query: [
93
- { key: "host", value: [hostname, '*'], operator: "$in" },
94
- { key: "pathname", value: pathname, operator: "$eq" }
95
- ],
96
- limit: 1
95
+ array: file['array'],
96
+ object: {
97
+ _id: file._id
97
98
  },
98
99
  organization_id
99
- }
100
-
101
- let file
102
- if (pathname.startsWith('/dist') || pathname.startsWith('/admin') || ['/403.html', '/404.html', '/offline.html', '/manifest.webmanifest', '/service-worker.js'].includes(pathname))
103
- file = await getDefaultFile(pathname)
104
- else
105
- file = await crud.send(data);
100
+ });
101
+ src = fileSrc[file['name']];
102
+ }
106
103
 
107
- if (!file || !file.object || !file.object[0]) {
108
- let pageNotFound = await getDefaultFile('/404.html')
109
- return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
110
- }
104
+ if (!src) {
105
+ let pageNotFound = await getDefaultFile('/404.html')
106
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
107
+ }
111
108
 
112
- file = file.object[0]
113
- if (!file['public'] || file['public'] === "false") {
114
- let pageForbidden = await getDefaultFile('/403.html')
115
- return sendResponse(pageForbidden.object[0].src, 403, { 'Content-Type': 'text/html' })
116
- }
117
109
 
118
- let src;
119
- if (file['src'])
120
- src = file['src'];
121
- else {
122
- let fileSrc = await crud.send({
123
- method: 'object.read',
124
- array: file['array'],
125
- object: {
126
- _id: file._id
127
- },
128
- organization_id
129
- });
130
- src = fileSrc[file['name']];
131
- }
110
+ if (file.modified || file.created) {
111
+ let modifiedOn = file.modified.on || file.created.on
112
+ if (modifiedOn instanceof Date)
113
+ modifiedOn = modifiedOn.toISOString()
114
+ res.setHeader('Last-Modified', modifiedOn);
115
+ }
132
116
 
133
- if (!src) {
134
- let pageNotFound = await getDefaultFile('/404.html')
135
- return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
117
+ let contentType = file['content-type'] || 'text/html';
118
+ if (/^[A-Za-z0-9+/]+[=]{0,2}$/.test(src)) {
119
+ src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
120
+ src = Buffer.from(src, 'base64');
121
+ } else if (contentType === 'text/html') {
122
+ try {
123
+ src = await this.render.HTML(src, organization_id);
124
+ } catch (err) {
125
+ console.warn('server-render: ' + err.message)
136
126
  }
127
+ }
137
128
 
129
+ sendResponse(src, 200, { 'Content-Type': contentType })
138
130
 
139
- if (file.modified || file.created) {
140
- let modifiedOn = file.modified.on || file.created.on
141
- if (modifiedOn instanceof Date)
142
- modifiedOn = modifiedOn.toISOString()
143
- res.setHeader('Last-Modified', modifiedOn);
144
- }
131
+ function sendResponse(src, statusCode, headers) {
132
+ crud.wsManager.emit("setBandwidth", {
133
+ type: 'out',
134
+ data: src,
135
+ organization_id
136
+ });
145
137
 
146
- let contentType = file['content-type'] || 'text/html';
147
- if (/^[A-Za-z0-9+/]+[=]{0,2}$/.test(src)) {
148
- src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
149
- src = Buffer.from(src, 'base64');
150
- } else if (contentType === 'text/html') {
151
- try {
152
- src = await render.HTML(src, organization_id);
153
- } catch (err) {
154
- console.warn('server-render: ' + err.message)
155
- }
156
- }
138
+ res.writeHead(statusCode, headers);
139
+ return res.end(src);
140
+ }
157
141
 
158
- sendResponse(src, 200, { 'Content-Type': contentType })
142
+ async function getDefaultFile(fileName) {
143
+ data.$filter.query[1].value = fileName
144
+ let defaultFile
145
+ if (fileName !== '/hostNotFound.html')
146
+ defaultFile = await crud.send(data);
159
147
 
160
- function sendResponse(src, statusCode, headers) {
161
- crud.wsManager.emit("setBandwidth", {
162
- type: 'out',
163
- data: src,
164
- organization_id
165
- });
148
+ if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
149
+ return defaultFile
150
+ } else {
151
+ data.$filter.query[0].value = ['*']
152
+ data.organization_id = process.env.organization_id
153
+
154
+ if (fileName.startsWith('/admin'))
155
+ data.$filter.query[1].value = '/superadmin' + fileName.replace('/admin', '')
156
+
157
+ defaultFile = await crud.send(data)
158
+
159
+ if (fileName !== '/hostNotFound.html') {
160
+ crud.wsManager.emit("setBandwidth", {
161
+ type: 'out',
162
+ data,
163
+ organization_id
164
+ });
165
+
166
+ crud.wsManager.emit("setBandwidth", {
167
+ type: 'in',
168
+ data: defaultFile,
169
+ organization_id
170
+ });
171
+ }
166
172
 
167
- res.writeHead(statusCode, headers);
168
- return res.end(src);
169
- }
173
+ if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
174
+ if (fileName.startsWith('/admin')) {
175
+ data.object[0].directory = 'admin'
176
+ data.object[0].path = '/admin' + data.object[0].path.replace('/superadmin', '')
177
+ data.object[0].pathname = fileName
178
+ }
170
179
 
171
- async function getDefaultFile(fileName) {
172
- data.$filter.query[1].value = fileName
173
- let defaultFile
174
- if (fileName !== '/hostNotFound.html')
175
- defaultFile = await crud.send(data);
180
+ crud.send({
181
+ method: 'object.create',
182
+ array: 'files',
183
+ object: defaultFile.object[0],
184
+ organization_id
185
+ })
176
186
 
177
- if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
178
187
  return defaultFile
179
188
  } else {
180
- data.$filter.query[0].value = ['*']
181
- data.organization_id = process.env.organization_id
182
-
183
- if (fileName.startsWith('/admin'))
184
- data.$filter.query[1].value = '/superadmin' + fileName.replace('/admin', '')
185
-
186
- defaultFile = await crud.send(data)
187
-
188
- if (fileName !== '/hostNotFound.html') {
189
- crud.wsManager.emit("setBandwidth", {
190
- type: 'out',
191
- data,
192
- organization_id
193
- });
194
-
195
- crud.wsManager.emit("setBandwidth", {
196
- type: 'in',
197
- data: defaultFile,
198
- organization_id
199
- });
200
- }
201
-
202
- if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
203
- if (fileName.startsWith('/admin')) {
204
- data.object[0].directory = 'admin'
205
- data.object[0].path = '/admin' + data.object[0].path.replace('/superadmin', '')
206
- data.object[0].pathname = fileName
207
- }
208
-
209
- crud.send({
210
- method: 'object.create',
211
- array: 'files',
212
- object: defaultFile.object[0],
213
- organization_id
214
- })
215
-
216
- return defaultFile
217
- } else {
218
- switch (fileName) {
219
- case '/403.html':
220
- defaultFile.object = [{ src: `${pathname} access not allowed for ${organization_id}` }]
221
- break;
222
- case '/404.html':
223
- defaultFile.object = [{ src: `${pathname} could not be found for ${organization_id}` }];
224
- break;
225
- case '/balanceFalse.html':
226
- defaultFile.object = [{ src: 'This organizations account balance has fallen bellow 0: ' }];
227
- break;
228
- case '/hostNotFound.html':
229
- defaultFile.object = [{ src: 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id }];
230
- break;
231
- }
232
- return defaultFile
189
+ switch (fileName) {
190
+ case '/403.html':
191
+ defaultFile.object = [{ src: `${pathname} access not allowed for ${organization_id}` }]
192
+ break;
193
+ case '/404.html':
194
+ defaultFile.object = [{ src: `${pathname} could not be found for ${organization_id}` }];
195
+ break;
196
+ case '/balanceFalse.html':
197
+ defaultFile.object = [{ src: 'This organizations account balance has fallen bellow 0: ' }];
198
+ break;
199
+ case '/hostNotFound.html':
200
+ defaultFile.object = [{ src: 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id }];
201
+ break;
233
202
  }
203
+ return defaultFile
234
204
  }
235
205
  }
206
+ }
236
207
 
237
208
 
238
- } catch (error) {
239
- res.writeHead(400, { 'Content-Type': 'text/plain' });
240
- res.end('Invalid host format');
241
- }
242
- })
209
+ } catch (error) {
210
+ res.writeHead(400, { 'Content-Type': 'text/plain' });
211
+ res.end('Invalid host format');
212
+ }
243
213
  }
214
+
244
215
  }
245
216
 
246
217
  module.exports = CoCreateFileSystem;