@cocreate/file-server 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,17 @@
1
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.14.1...v1.15.0) (2023-12-09)
2
+
3
+
4
+ ### Features
5
+
6
+ * updated to be used by lazyloader default ([ff8f720](https://github.com/CoCreate-app/CoCreate-file-server/commit/ff8f720ea51c20c6c5f9d44182351180ad320e6a))
7
+
8
+ ## [1.14.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.14.0...v1.14.1) (2023-11-25)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * update licensing details ([a9096d6](https://github.com/CoCreate-app/CoCreate-file-server/commit/a9096d62361d84cdc1e6ff1cebfeb62db4d13a30))
14
+
1
15
  # [1.14.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.13.0...v1.14.0) (2023-11-25)
2
16
 
3
17
 
package/README.md CHANGED
@@ -9,7 +9,6 @@ A simple file-server component in vanilla javascript. Easily configured using HT
9
9
  ![GitHub](https://img.shields.io/github/license/CoCreate-app/CoCreate-file-server?style=flat-square)
10
10
  ![GitHub](https://img.shields.io/static/v1?style=flat-square&label=&message=Hiring&color=blueviolet)
11
11
 
12
-
13
12
  ![CoCreate-file-server](https://cdn.cocreate.app/docs/CoCreate-file-server.gif)
14
13
 
15
14
  ## [Docs & Demo](https://cocreate.app/docs/file-server)
@@ -40,12 +39,12 @@ $ yarn install @cocreate/file-server
40
39
 
41
40
  # Table of Contents
42
41
 
43
- - [Table of Contents](#table-of-contents)
44
- - [Announcements](#announcements)
45
- - [Roadmap](#roadmap)
46
- - [How to Contribute](#how-to-contribute)
47
- - [About](#about)
48
- - [License](#license)
42
+ - [Table of Contents](#table-of-contents)
43
+ - [Announcements](#announcements)
44
+ - [Roadmap](#roadmap)
45
+ - [How to Contribute](#how-to-contribute)
46
+ - [About](#about)
47
+ - [License](#license)
49
48
 
50
49
  <a name="announcements"></a>
51
50
 
@@ -79,6 +78,14 @@ We want this library to be community-driven, and CoCreate led. We need your help
79
78
 
80
79
  We appreciate your continued support, thank you!
81
80
 
81
+ <a name="license"></a>
82
+
82
83
  # License
83
84
 
84
- [The MIT License (MIT)](https://github.com/CoCreate-app/CoCreate-file-server/blob/master/LICENSE)
85
+ This software is dual-licensed under the GNU Affero General Public License version 3 (AGPLv3) and a commercial license.
86
+
87
+ - **Open Source Use**: For open-source projects and non-commercial use, this software is available under the AGPLv3. The AGPLv3 allows you to freely use, modify, and distribute this software, provided that all modifications and derivative works are also licensed under the AGPLv3. For the full license text, see the [LICENSE file](https://github.com/CoCreate-app/CoCreate-file-server/blob/master/LICENSE).
88
+
89
+ - **Commercial Use**: For-profit companies and individuals intending to use this software for commercial purposes must obtain a commercial license. The commercial license is available when you sign up for an API key on our [website](https://cocreate.app). This license permits proprietary use and modification of the software without the copyleft requirements of the AGPLv3. It is ideal for integrating this software into proprietary commercial products and applications.
90
+
91
+ If you have not purchased a commercial license and intend to use this software for commercial purposes, you are required to sign up for an API key on our website.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
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
@@ -25,222 +25,197 @@ const { URL } = require('url');
25
25
  const organizations = new Map();
26
26
 
27
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
- }
28
+ constructor(render) {
29
+ this.render = render
30
+ }
59
31
 
60
- let organization_id = organization._id
32
+ async send(req, res, crud, organization, valideUrl) {
33
+ try {
34
+ const organization_id = organization._id
35
+ const hostname = valideUrl.hostname;
61
36
 
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');
37
+ res.setHeader('organization', organization_id)
38
+ res.setHeader('storage', organization.storage);
39
+ res.setHeader('Access-Control-Allow-Origin', '*');
40
+ res.setHeader('Access-Control-Allow-Methods', '');
41
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
67
42
 
68
- let parameters = valideUrl.searchParams;
69
- if (parameters.size) {
70
- console.log('parameters', parameters)
71
- }
43
+ let parameters = valideUrl.searchParams;
44
+ if (parameters.size) {
45
+ console.log('parameters', parameters)
46
+ }
72
47
 
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
- }
48
+ let pathname = valideUrl.pathname;
49
+ if (pathname.endsWith('/')) {
50
+ pathname += "index.html";
51
+ } else {
52
+ let directory = pathname.split("/").slice(-1)[0];
53
+ if (!directory.includes('.'))
54
+ pathname += "/index.html";
55
+ }
81
56
 
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
- }
57
+ let active = crud.wsManager.organizations.get(organization_id)
58
+ if (active === false) {
59
+ let balanceFalse = await getDefaultFile('/balanceFalse.html')
60
+ return sendResponse(balanceFalse.object[0].src, 403, { 'Content-Type': 'text/html', 'Account-Balance': 'false', 'storage': organization.storage })
61
+ }
87
62
 
88
- let data = {
63
+ let data = {
64
+ method: 'object.read',
65
+ array: 'files',
66
+ $filter: {
67
+ query: [
68
+ { key: "host", value: [hostname, '*'], operator: "$in" },
69
+ { key: "pathname", value: pathname, operator: "$eq" }
70
+ ],
71
+ limit: 1
72
+ },
73
+ organization_id
74
+ }
75
+
76
+ let file
77
+ if (pathname.startsWith('/dist') || pathname.startsWith('/admin') || ['/403.html', '/404.html', '/offline.html', '/manifest.webmanifest', '/service-worker.js'].includes(pathname))
78
+ file = await getDefaultFile(pathname)
79
+ else
80
+ file = await crud.send(data);
81
+
82
+ if (!file || !file.object || !file.object[0]) {
83
+ let pageNotFound = await getDefaultFile('/404.html')
84
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
85
+ }
86
+
87
+ file = file.object[0]
88
+ if (!file['public'] || file['public'] === "false") {
89
+ let pageForbidden = await getDefaultFile('/403.html')
90
+ return sendResponse(pageForbidden.object[0].src, 403, { 'Content-Type': 'text/html' })
91
+ }
92
+
93
+ let src;
94
+ if (file['src'])
95
+ src = file['src'];
96
+ else {
97
+ let fileSrc = await crud.send({
89
98
  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
99
+ array: file['array'],
100
+ object: {
101
+ _id: file._id
97
102
  },
98
103
  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);
104
+ });
105
+ src = fileSrc[file['name']];
106
+ }
106
107
 
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
- }
108
+ if (!src) {
109
+ let pageNotFound = await getDefaultFile('/404.html')
110
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
111
+ }
111
112
 
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
113
 
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
- }
114
+ if (file.modified || file.created) {
115
+ let modifiedOn = file.modified.on || file.created.on
116
+ if (modifiedOn instanceof Date)
117
+ modifiedOn = modifiedOn.toISOString()
118
+ res.setHeader('Last-Modified', modifiedOn);
119
+ }
132
120
 
133
- if (!src) {
134
- let pageNotFound = await getDefaultFile('/404.html')
135
- return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
121
+ let contentType = file['content-type'] || 'text/html';
122
+ if (/^[A-Za-z0-9+/]+[=]{0,2}$/.test(src)) {
123
+ src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
124
+ src = Buffer.from(src, 'base64');
125
+ } else if (contentType === 'text/html') {
126
+ try {
127
+ src = await this.render.HTML(src, organization_id);
128
+ } catch (err) {
129
+ console.warn('server-render: ' + err.message)
136
130
  }
131
+ }
137
132
 
133
+ sendResponse(src, 200, { 'Content-Type': contentType })
138
134
 
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
- }
135
+ function sendResponse(src, statusCode, headers) {
136
+ crud.wsManager.emit("setBandwidth", {
137
+ type: 'out',
138
+ data: src,
139
+ organization_id
140
+ });
145
141
 
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
- }
142
+ res.writeHead(statusCode, headers);
143
+ return res.end(src);
144
+ }
157
145
 
158
- sendResponse(src, 200, { 'Content-Type': contentType })
146
+ async function getDefaultFile(fileName) {
147
+ data.$filter.query[1].value = fileName
148
+ let defaultFile
149
+ if (fileName !== '/hostNotFound.html')
150
+ defaultFile = await crud.send(data);
159
151
 
160
- function sendResponse(src, statusCode, headers) {
161
- crud.wsManager.emit("setBandwidth", {
162
- type: 'out',
163
- data: src,
164
- organization_id
165
- });
152
+ if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
153
+ return defaultFile
154
+ } else {
155
+ data.$filter.query[0].value = ['*']
156
+ data.organization_id = process.env.organization_id
157
+
158
+ if (fileName.startsWith('/admin'))
159
+ data.$filter.query[1].value = '/superadmin' + fileName.replace('/admin', '')
160
+
161
+ defaultFile = await crud.send(data)
162
+
163
+ if (fileName !== '/hostNotFound.html') {
164
+ crud.wsManager.emit("setBandwidth", {
165
+ type: 'out',
166
+ data,
167
+ organization_id
168
+ });
169
+
170
+ crud.wsManager.emit("setBandwidth", {
171
+ type: 'in',
172
+ data: defaultFile,
173
+ organization_id
174
+ });
175
+ }
166
176
 
167
- res.writeHead(statusCode, headers);
168
- return res.end(src);
169
- }
177
+ if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
178
+ if (fileName.startsWith('/admin')) {
179
+ data.object[0].directory = 'admin'
180
+ data.object[0].path = '/admin' + data.object[0].path.replace('/superadmin', '')
181
+ data.object[0].pathname = fileName
182
+ }
170
183
 
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);
184
+ crud.send({
185
+ method: 'object.create',
186
+ array: 'files',
187
+ object: defaultFile.object[0],
188
+ organization_id
189
+ })
176
190
 
177
- if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
178
191
  return defaultFile
179
192
  } 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
193
+ switch (fileName) {
194
+ case '/403.html':
195
+ defaultFile.object = [{ src: `${pathname} access not allowed for ${organization_id}` }]
196
+ break;
197
+ case '/404.html':
198
+ defaultFile.object = [{ src: `${pathname} could not be found for ${organization_id}` }];
199
+ break;
200
+ case '/balanceFalse.html':
201
+ defaultFile.object = [{ src: 'This organizations account balance has fallen bellow 0: ' }];
202
+ break;
203
+ case '/hostNotFound.html':
204
+ defaultFile.object = [{ src: 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id }];
205
+ break;
233
206
  }
207
+ return defaultFile
234
208
  }
235
209
  }
210
+ }
236
211
 
237
212
 
238
- } catch (error) {
239
- res.writeHead(400, { 'Content-Type': 'text/plain' });
240
- res.end('Invalid host format');
241
- }
242
- })
213
+ } catch (error) {
214
+ res.writeHead(400, { 'Content-Type': 'text/plain' });
215
+ res.end('Invalid host format');
216
+ }
243
217
  }
218
+
244
219
  }
245
220
 
246
221
  module.exports = CoCreateFileSystem;