@cocreate/file-server 1.4.2 → 1.4.4

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/src/index.js CHANGED
@@ -1,170 +1,170 @@
1
- /********************************************************************************
2
- * Copyright (C) 2022 CoCreate LLC and others.
3
- *
4
- *
5
- * SPDX-License-Identifier: MIT
6
- ********************************************************************************/
7
- const express = require('express');
8
- const router = express.Router();
9
- const mime = require('mime-types');
10
- const organizations = new Map();
11
-
12
- class CoCreateFileSystem {
13
- constructor(crud, render) {
14
-
15
- async function defaultFiles(fileName) {
16
- let file = await crud.readDocument({
17
- collection: 'files',
18
- filter: {
19
- query: [
20
- { name: "path", value: fileName, operator: "$eq" }
21
- ]
22
- },
23
- organization_id: process.env.organization_id
24
- })
25
- if (!file || !file.document || !file.document[0])
26
- return ''
27
- return file.document[0].src
28
- }
29
-
30
- let default403, default404, hostNotFound, signup
31
- defaultFiles('/403.html').then((file) => {
32
- default403 = file
33
- })
34
- defaultFiles('/404.html').then((file) => {
35
- default404 = file
36
- })
37
- defaultFiles('/hostNotFound.html').then((file) => {
38
- hostNotFound = file
39
- })
40
- defaultFiles('/superadmin/signup.html').then((file) => {
41
- signup = file
42
- })
43
-
44
- this.router = router.get('/*', async (req, res) => {
45
- let hostname = req.hostname;
46
- let organization = organizations.get(hostname);
47
- if (!organization) {
48
- let org = await crud.readDocument({
49
- collection: 'organizations',
50
- filter: {
51
- query: [
52
- { name: "hosts", value: [hostname], operator: "$in" }
53
- ]
54
- },
55
- organization_id: process.env.organization_id
56
- })
57
-
58
- if (!org || !org.document || !org.document[0]) {
59
- hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
60
- return res.end(hostNotFound);
61
- } else {
62
- organization = { _id: org.document[0]._id }
63
- organizations.set(hostname, organization)
64
- }
65
- }
66
-
67
- let organization_id = organization._id
68
- res.set('organization', organization_id)
69
-
70
- let [url, parameters] = req.url.split("?");
71
- if (parameters) { }
72
- if (url.endsWith('/')) {
73
- url += "index.html";
74
- } else {
75
- let directory = url.split("/").slice(-1)[0];
76
- if (!directory.includes('.')) {
77
- url += "/index.html";
78
- }
79
- }
80
-
81
- let data = {
82
- collection: 'files',
83
- filter: {
84
- query: [
85
- { name: "hosts", value: [hostname, '*'], operator: "$in" },
86
- { name: "path", value: url, operator: "$eq" }
87
- ]
88
- },
89
- organization_id
90
- }
91
-
92
- if (url.startsWith('/superadmin'))
93
- data.organization_id = process.env.organization_id
94
-
95
- let file = await crud.readDocument(data);
96
-
97
- if (!file || !file.document || !file.document[0]) {
98
- data.filter.query[1].value = '/404.html'
99
- if (data.organization_id !== organization_id)
100
- data.organization_id = organization_id
101
-
102
- let pageNotFound = await crud.readDocument(data);
103
- if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
104
- pageNotFound = default404 || `${url} could not be found for ${organization_id}`
105
- else
106
- pageNotFound = pageNotFound.document[0].src
107
- return res.status(404).send(pageNotFound);
108
- }
109
-
110
- file = file.document[0]
111
- if (!file['public'] || file['public'] === "false") {
112
- data.filter.query[1].value = '/403.html'
113
- if (data.organization_id !== organization_id)
114
- data.organization_id = organization_id
115
-
116
- let pageForbidden = await crud.readDocument(data);
117
- if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
118
- pageForbidden = default403 || `${url} access not allowed for ${organization_id}`
119
- else
120
- pageForbidden = pageForbidden.document[0].src
121
-
122
- return res.status(403).send(pageForbidden);
123
- }
124
-
125
- let src;
126
- if (file['src'])
127
- src = file['src'];
128
- else {
129
- let fileSrc = await crud.readDocument({
130
- collection: file['collection'],
131
- document: {
132
- _id: file._id
133
- },
134
- organization_id
135
- });
136
- src = fileSrc[file['name']];
137
- }
138
-
139
- if (!src) {
140
- data.filter.query[1].value = '/404.html'
141
- if (data.organization_id !== organization_id)
142
- data.organization_id = organization_id
143
-
144
- let pageNotFound = await crud.readDocument(data);
145
- if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
146
- pageNotFound = `${url} could not be found for ${organization_id}`
147
- return res.status(404).send(pageNotFound);
148
- }
149
-
150
- let contentType = file['content-type'] || mime.lookup(url) || 'text/html';
151
-
152
- if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
153
- src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
154
- src = Buffer.from(src, 'base64');
155
- } else if (contentType === 'text/html') {
156
- try {
157
- src = await render.HTML(src, organization_id);
158
- } catch (err) {
159
- console.warn('server-render: ' + err.message)
160
- }
161
- }
162
-
163
- return res.type(contentType).send(src);
164
-
165
- })
166
-
167
- }
168
- }
169
-
170
- module.exports = CoCreateFileSystem;
1
+ /********************************************************************************
2
+ * Copyright (C) 2022 CoCreate LLC and others.
3
+ *
4
+ *
5
+ * SPDX-License-Identifier: MIT
6
+ ********************************************************************************/
7
+ const express = require('express');
8
+ const router = express.Router();
9
+ const mime = require('mime-types');
10
+ const organizations = new Map();
11
+
12
+ class CoCreateFileSystem {
13
+ constructor(crud, render) {
14
+
15
+ async function defaultFiles(fileName) {
16
+ let file = await crud.readDocument({
17
+ collection: 'files',
18
+ filter: {
19
+ query: [
20
+ { name: "path", value: fileName, operator: "$eq" }
21
+ ]
22
+ },
23
+ organization_id: process.env.organization_id
24
+ })
25
+ if (!file || !file.document || !file.document[0])
26
+ return ''
27
+ return file.document[0].src
28
+ }
29
+
30
+ let default403, default404, hostNotFound, signup
31
+ defaultFiles('/403.html').then((file) => {
32
+ default403 = file
33
+ })
34
+ defaultFiles('/404.html').then((file) => {
35
+ default404 = file
36
+ })
37
+ defaultFiles('/hostNotFound.html').then((file) => {
38
+ hostNotFound = file
39
+ })
40
+ defaultFiles('/superadmin/signup.html').then((file) => {
41
+ signup = file
42
+ })
43
+
44
+ this.router = router.get('/*', async (req, res) => {
45
+ let hostname = req.hostname;
46
+ let organization = organizations.get(hostname);
47
+ if (!organization) {
48
+ let org = await crud.readDocument({
49
+ collection: 'organizations',
50
+ filter: {
51
+ query: [
52
+ { name: "hosts", value: [hostname], operator: "$in" }
53
+ ]
54
+ },
55
+ organization_id: process.env.organization_id
56
+ })
57
+
58
+ if (!org || !org.document || !org.document[0]) {
59
+ hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
60
+ return res.end(hostNotFound);
61
+ } else {
62
+ organization = { _id: org.document[0]._id }
63
+ organizations.set(hostname, organization)
64
+ }
65
+ }
66
+
67
+ let organization_id = organization._id
68
+ res.set('organization', organization_id)
69
+
70
+ let [url, parameters] = req.url.split("?");
71
+ if (parameters) { }
72
+ if (url.endsWith('/')) {
73
+ url += "index.html";
74
+ } else {
75
+ let directory = url.split("/").slice(-1)[0];
76
+ if (!directory.includes('.')) {
77
+ url += "/index.html";
78
+ }
79
+ }
80
+
81
+ let data = {
82
+ collection: 'files',
83
+ filter: {
84
+ query: [
85
+ { name: "hosts", value: [hostname, '*'], operator: "$in" },
86
+ { name: "path", value: url, operator: "$eq" }
87
+ ]
88
+ },
89
+ organization_id
90
+ }
91
+
92
+ if (url.startsWith('/superadmin'))
93
+ data.organization_id = process.env.organization_id
94
+
95
+ let file = await crud.readDocument(data);
96
+
97
+ if (!file || !file.document || !file.document[0]) {
98
+ data.filter.query[1].value = '/404.html'
99
+ if (data.organization_id !== organization_id)
100
+ data.organization_id = organization_id
101
+
102
+ let pageNotFound = await crud.readDocument(data);
103
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
104
+ pageNotFound = default404 || `${url} could not be found for ${organization_id}`
105
+ else
106
+ pageNotFound = pageNotFound.document[0].src
107
+ return res.status(404).send(pageNotFound);
108
+ }
109
+
110
+ file = file.document[0]
111
+ if (!file['public'] || file['public'] === "false") {
112
+ data.filter.query[1].value = '/403.html'
113
+ if (data.organization_id !== organization_id)
114
+ data.organization_id = organization_id
115
+
116
+ let pageForbidden = await crud.readDocument(data);
117
+ if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
118
+ pageForbidden = default403 || `${url} access not allowed for ${organization_id}`
119
+ else
120
+ pageForbidden = pageForbidden.document[0].src
121
+
122
+ return res.status(403).send(pageForbidden);
123
+ }
124
+
125
+ let src;
126
+ if (file['src'])
127
+ src = file['src'];
128
+ else {
129
+ let fileSrc = await crud.readDocument({
130
+ collection: file['collection'],
131
+ document: {
132
+ _id: file._id
133
+ },
134
+ organization_id
135
+ });
136
+ src = fileSrc[file['name']];
137
+ }
138
+
139
+ if (!src) {
140
+ data.filter.query[1].value = '/404.html'
141
+ if (data.organization_id !== organization_id)
142
+ data.organization_id = organization_id
143
+
144
+ let pageNotFound = await crud.readDocument(data);
145
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
146
+ pageNotFound = `${url} could not be found for ${organization_id}`
147
+ return res.status(404).send(pageNotFound);
148
+ }
149
+
150
+ let contentType = file['content-type'] || mime.lookup(url) || 'text/html';
151
+
152
+ if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
153
+ src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
154
+ src = Buffer.from(src, 'base64');
155
+ } else if (contentType === 'text/html') {
156
+ try {
157
+ src = await render.HTML(src, organization_id);
158
+ } catch (err) {
159
+ console.warn('server-render: ' + err.message)
160
+ }
161
+ }
162
+
163
+ return res.type(contentType).send(src);
164
+
165
+ })
166
+
167
+ }
168
+ }
169
+
170
+ module.exports = CoCreateFileSystem;
package/webpack.config.js CHANGED
@@ -1,84 +1,84 @@
1
- const path = require("path")
2
- const TerserPlugin = require("terser-webpack-plugin")
3
- const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4
- let isProduction = process.env.NODE_ENV === "production"
5
- const { CleanWebpackPlugin } = require("clean-webpack-plugin")
6
-
7
- module.exports = {
8
- entry: {
9
- "CoCreate-file-server": "./src/index.js",
10
- },
11
- output: {
12
- path: path.resolve(__dirname, "dist"),
13
- filename: isProduction ? "[name].min.js" : "[name].js",
14
- libraryTarget: "umd",
15
- libraryExport: "default",
16
- library: ["CoCreate", "file-server"],
17
- globalObject: "this",
18
- },
19
-
20
- plugins: [
21
- new CleanWebpackPlugin(),
22
- new MiniCssExtractPlugin({
23
- filename: "[name].css",
24
- }),
25
- ],
26
- // Default mode for Webpack is production.
27
- // Depending on mode Webpack will apply different things
28
- // on final bundle. For now we don't need production's JavaScript
29
- // minifying and other thing so let's set mode to development
30
- mode: isProduction ? "production" : "development",
31
- module: {
32
- rules: [
33
- {
34
- test: /.js$/,
35
- exclude: /(node_modules)/,
36
- use: {
37
- loader: "babel-loader",
38
- options: {
39
- plugins: ["@babel/plugin-transform-modules-commonjs"],
40
- },
41
- },
42
- },
43
- {
44
- test: /.css$/i,
45
- use: [
46
- { loader: "style-loader", options: { injectType: "linkTag" } },
47
- "file-loader",
48
- ],
49
- },
50
- ],
51
- },
52
-
53
- // add source map
54
- ...(isProduction ? {} : { devtool: "eval-source-map" }),
55
-
56
- optimization: {
57
- minimize: true,
58
- minimizer: [
59
- new TerserPlugin({
60
- extractComments: true,
61
- // cache: true,
62
- parallel: true,
63
- // sourceMap: true, // Must be set to true if using source-maps in production
64
- terserOptions: {
65
- // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
66
- // extractComments: 'all',
67
- compress: {
68
- drop_console: true,
69
- },
70
- },
71
- }),
72
- ],
73
- splitChunks: {
74
- chunks: "all",
75
- minSize: 200,
76
- // maxSize: 99999,
77
- //minChunks: 1,
78
-
79
- cacheGroups: {
80
- defaultVendors: false,
81
- },
82
- },
83
- },
84
- }
1
+ const path = require("path")
2
+ const TerserPlugin = require("terser-webpack-plugin")
3
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4
+ let isProduction = process.env.NODE_ENV === "production"
5
+ const { CleanWebpackPlugin } = require("clean-webpack-plugin")
6
+
7
+ module.exports = {
8
+ entry: {
9
+ "CoCreate-file-server": "./src/index.js",
10
+ },
11
+ output: {
12
+ path: path.resolve(__dirname, "dist"),
13
+ filename: isProduction ? "[name].min.js" : "[name].js",
14
+ libraryTarget: "umd",
15
+ libraryExport: "default",
16
+ library: ["CoCreate", "file-server"],
17
+ globalObject: "this",
18
+ },
19
+
20
+ plugins: [
21
+ new CleanWebpackPlugin(),
22
+ new MiniCssExtractPlugin({
23
+ filename: "[name].css",
24
+ }),
25
+ ],
26
+ // Default mode for Webpack is production.
27
+ // Depending on mode Webpack will apply different things
28
+ // on final bundle. For now we don't need production's JavaScript
29
+ // minifying and other thing so let's set mode to development
30
+ mode: isProduction ? "production" : "development",
31
+ module: {
32
+ rules: [
33
+ {
34
+ test: /.js$/,
35
+ exclude: /(node_modules)/,
36
+ use: {
37
+ loader: "babel-loader",
38
+ options: {
39
+ plugins: ["@babel/plugin-transform-modules-commonjs"],
40
+ },
41
+ },
42
+ },
43
+ {
44
+ test: /.css$/i,
45
+ use: [
46
+ { loader: "style-loader", options: { injectType: "linkTag" } },
47
+ "file-loader",
48
+ ],
49
+ },
50
+ ],
51
+ },
52
+
53
+ // add source map
54
+ ...(isProduction ? {} : { devtool: "eval-source-map" }),
55
+
56
+ optimization: {
57
+ minimize: true,
58
+ minimizer: [
59
+ new TerserPlugin({
60
+ extractComments: true,
61
+ // cache: true,
62
+ parallel: true,
63
+ // sourceMap: true, // Must be set to true if using source-maps in production
64
+ terserOptions: {
65
+ // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
66
+ // extractComments: 'all',
67
+ compress: {
68
+ drop_console: true,
69
+ },
70
+ },
71
+ }),
72
+ ],
73
+ splitChunks: {
74
+ chunks: "all",
75
+ minSize: 200,
76
+ // maxSize: 99999,
77
+ //minChunks: 1,
78
+
79
+ cacheGroups: {
80
+ defaultVendors: false,
81
+ },
82
+ },
83
+ },
84
+ }