@awesomeness-js/server 1.0.2 → 1.1.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.
- package/README.md +61 -0
- package/build/build.js +7 -0
- package/build/postBuild.js +7 -0
- package/example-.awesomeness/componentLocations.js +15 -0
- package/example-.awesomeness/config.js +5 -21
- package/example-site/example.awesomenessjs.com/api/readme.md +1 -0
- package/example-site/example.awesomenessjs.com/components/example/index.js +3 -0
- package/example-site/example.awesomenessjs.com/components/notCommonExample/_.css +1 -0
- package/example-site/example.awesomenessjs.com/components/notCommonExample/index.js +3 -0
- package/example-site/example.awesomenessjs.com/pages/_md/_info.js +15 -0
- package/example-site/example.awesomenessjs.com/pages/_md/css/example.css +4 -0
- package/example-site/example.awesomenessjs.com/pages/_md/drafts/example.md +32 -0
- package/example-site/example.awesomenessjs.com/pages/_md/getBlogData.js +117 -0
- package/example-site/example.awesomenessjs.com/pages/_md/getData.js +36 -0
- package/example-site/example.awesomenessjs.com/pages/_md/js/callToAction.js +43 -0
- package/example-site/example.awesomenessjs.com/pages/_md/js/init.js +88 -0
- package/example-site/example.awesomenessjs.com/pages/_md/meta.template.md +33 -0
- package/example-site/example.awesomenessjs.com/pages/_md/pages/mission.md +345 -0
- package/example-site/example.awesomenessjs.com/pages/_template/_info.js +9 -0
- package/example-site/example.awesomenessjs.com/pages/_template/css/_.css +4 -0
- package/example-site/example.awesomenessjs.com/pages/_template/getData.js +6 -0
- package/example-site/example.awesomenessjs.com/pages/_template/js/init.js +3 -0
- package/example-site/example.awesomenessjs.com/pages/blog/_info.js +14 -0
- package/example-site/example.awesomenessjs.com/pages/blog/css/topics.css +17 -0
- package/example-site/example.awesomenessjs.com/pages/blog/getData.js +22 -0
- package/example-site/example.awesomenessjs.com/pages/blog/js/colors.js +159 -0
- package/example-site/example.awesomenessjs.com/pages/blog/js/init.js +242 -0
- package/example-site/example.awesomenessjs.com/pages/blog/js/tag.js +111 -0
- package/example-site/example.awesomenessjs.com/pages/examples/_info.js +9 -0
- package/example-site/example.awesomenessjs.com/pages/examples/css/footer.css +6 -0
- package/example-site/example.awesomenessjs.com/pages/examples/css/intro.css +67 -0
- package/example-site/example.awesomenessjs.com/pages/examples/css/nav.css +221 -0
- package/example-site/example.awesomenessjs.com/pages/examples/getData.js +8 -0
- package/example-site/example.awesomenessjs.com/pages/examples/js/init.js +15 -0
- package/example-site/example.awesomenessjs.com/pages/examples/js/updateState.js +14 -0
- package/example-site/example.awesomenessjs.com/pages/start/_info.js +11 -0
- package/example-site/example.awesomenessjs.com/pages/start/css/footer.css +6 -0
- package/example-site/example.awesomenessjs.com/pages/start/css/intro.css +67 -0
- package/example-site/example.awesomenessjs.com/pages/start/css/nav.css +221 -0
- package/example-site/example.awesomenessjs.com/pages/start/getData.js +8 -0
- package/example-site/example.awesomenessjs.com/pages/start/js/init.js +30 -0
- package/example-site/example.awesomenessjs.com/pages/start/js/updateState.js +14 -0
- package/example-site/example.awesomenessjs.com/public/images/awesomeness.webp +0 -0
- package/example-site/example.awesomenessjs.com/public/manifest.webmanifest.json +8 -0
- package/example-site/example.awesomenessjs.com/specialRoutes.js +14 -0
- package/index.js +105 -25
- package/package.json +5 -2
- package/{server → src}/applicationMap.js +1 -1
- package/{server → src}/awesomenessNormalizeRequest.js +24 -3
- package/{server → src}/componentDependencies.js +11 -5
- package/{config.js → src/config.js} +6 -1
- package/{server → src}/fetchPage.js +6 -6
- package/src/getConfig.js +3 -0
- package/src/init.js +3 -0
- package/{server → src}/koa/attachAwesomenessRequest.js +5 -6
- package/{server → src}/koa/cors.js +2 -4
- package/{server → src}/koa/errorHandler.js +3 -3
- package/{server → src}/koa/finalFormat.js +2 -5
- package/{server → src}/koa/jsonBodyParser.js +3 -3
- package/src/koa/routeRequest.js +334 -0
- package/{server → src}/koa/serverUp.js +2 -4
- package/{server → src}/koa/staticFiles.js +10 -11
- package/{server → src}/koa/timeout.js +3 -3
- package/{server → src}/pageInfo.js +6 -6
- package/{server → src}/reRoute.js +3 -2
- package/{server → src}/resolveRealCasePath.js +1 -1
- package/{start.js → src/start.js} +14 -13
- package/{server → src}/validateRequest.js +1 -1
- package/{server → src}/ws/handlers.js +4 -2
- package/{server → src}/ws/index.js +2 -2
- package/ui/README.md +438 -0
- package/ui/awesomeness-ui.instructions.md +156 -0
- package/ui/build.js +119 -0
- package/ui/commonComponent.test.js +27 -0
- package/ui/components/blogPost/all.css +371 -0
- package/ui/components/blogPost/headerImage.js +153 -0
- package/ui/components/blogPost/index.js +45 -0
- package/ui/components/blogPost/marked.js +74 -0
- package/ui/components/blogPost/md.js +73 -0
- package/ui/components/blogPost/parseAwesomeness.jQuery.js +47 -0
- package/ui/components/blogPost/readme.md +8 -0
- package/ui/components/colors/README.md +65 -0
- package/ui/components/colors/dynamic.css +1884 -0
- package/ui/components/colors/dynamic.css.js +135 -0
- package/ui/components/colors/dynamic.css.php +79 -0
- package/ui/components/colors/example.js +133 -0
- package/ui/components/colors/index.js +65 -0
- package/ui/components/colors/map.js +13 -0
- package/ui/components/colors/random.js +17 -0
- package/ui/components/colors/tailwind-colors.css +247 -0
- package/ui/components/insertIntoList/insertIntoList.jquery.js +150 -0
- package/ui/components/statusPage/index.css +120 -0
- package/ui/components/statusPage/index.js +39 -0
- package/ui/components/tag/category.js +8 -0
- package/ui/components/tag/index.js +33 -0
- package/ui/components/tag/list.js +42 -0
- package/ui/components/tag/map.js +64 -0
- package/ui/components/tag/tagMap.css +60 -0
- package/ui/components/tag/tags.css +20 -0
- package/ui/core/css/_normalize.css +57 -0
- package/ui/core/css/dynamic/flex.js +36 -0
- package/ui/core/css/dynamic/grid.js +185 -0
- package/ui/core/css/dynamic/shortcuts.js +48 -0
- package/ui/core/css/dynamic/spacing.js +61 -0
- package/ui/core/css/dynamic/text.js +80 -0
- package/ui/core/css/dynamic/width.js +75 -0
- package/ui/core/css/dynamic.js +40 -0
- package/ui/core/js/app/api.js +143 -0
- package/ui/core/js/app/init.js +88 -0
- package/ui/core/js/app/initDarkMode.js +26 -0
- package/ui/core/js/app/initialScroll.js +28 -0
- package/ui/core/js/app/meta/update/components.js +65 -0
- package/ui/core/js/app/meta/update/pages.js +86 -0
- package/ui/core/js/app/metaUpdates.js +10 -0
- package/ui/core/js/app/page.js +319 -0
- package/ui/core/js/app/resize.js +52 -0
- package/ui/core/js/app/state/back.js +16 -0
- package/ui/core/js/app/state/create.js +50 -0
- package/ui/core/js/app/state/get.js +5 -0
- package/ui/core/js/app/state/init.js +25 -0
- package/ui/core/js/app.js +41 -0
- package/ui/core/js/jquery-3.7.1.min.js +2 -0
- package/ui/core/public/app.css +1 -0
- package/ui/core/public/app.js +975 -0
- package/ui/core/public/favicon.ico +0 -0
- package/ui/core/public/index.html +40 -0
- package/server/koa/routeRequest.js +0 -286
- /package/{server/errors.js → errors.js} +0 -0
- /package/{server → src}/brotliJsonResponse.js +0 -0
- /package/{server → src}/checkAccess.js +0 -0
- /package/{server → src}/getMD.js +0 -0
- /package/{server → src}/specialPaths.js +0 -0
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import awesomenessNormalizeRequest from '../awesomenessNormalizeRequest.js';
|
|
2
2
|
import specialPaths from '../specialPaths.js';
|
|
3
|
-
import
|
|
3
|
+
import getConfig from "../getConfig.js";
|
|
4
4
|
|
|
5
|
-
async function attachAwesomenessRequest(ctx, next) {
|
|
5
|
+
export default async function attachAwesomenessRequest(ctx, next) {
|
|
6
6
|
|
|
7
7
|
const awesomenessConfig = getConfig();
|
|
8
8
|
|
|
9
9
|
ctx.awesomenessRequest = await awesomenessNormalizeRequest({ req: ctx });
|
|
10
10
|
|
|
11
|
+
ctx.awesomenessRequest.log('Attached awesomenessRequest to Koa ctx');
|
|
12
|
+
|
|
11
13
|
const routes = awesomenessConfig.specialRoutes[ctx.awesomenessRequest.site] || [];
|
|
12
14
|
|
|
13
15
|
if(ctx.awesomenessRequest.awesomenessType === 'page'){
|
|
@@ -18,7 +20,4 @@ async function attachAwesomenessRequest(ctx, next) {
|
|
|
18
20
|
|
|
19
21
|
await next();
|
|
20
22
|
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { attachAwesomenessRequest };
|
|
24
|
-
export default attachAwesomenessRequest;
|
|
23
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export default async function cors(ctx, next){
|
|
2
2
|
|
|
3
3
|
// Set CORS headers
|
|
4
4
|
ctx.set('Access-Control-Allow-Origin', '*');
|
|
@@ -17,6 +17,4 @@ const cors = async (ctx, next) => {
|
|
|
17
17
|
// Proceed to the next middleware
|
|
18
18
|
await next();
|
|
19
19
|
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export { cors };
|
|
20
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function finalFormat(awesomenessRequest, ctx){
|
|
1
|
+
export default function finalFormat(awesomenessRequest, ctx){
|
|
2
2
|
|
|
3
3
|
if(!ctx.body.meta){
|
|
4
4
|
|
|
@@ -28,7 +28,4 @@ function finalFormat(awesomenessRequest, ctx){
|
|
|
28
28
|
|
|
29
29
|
return;
|
|
30
30
|
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export { finalFormat };
|
|
34
|
-
export default finalFormat;
|
|
31
|
+
}
|
|
@@ -136,7 +136,7 @@ const parseMultipart = (req, limit = 10 * 1024 * 1024) => {
|
|
|
136
136
|
|
|
137
137
|
|
|
138
138
|
// ---------- middleware ----------
|
|
139
|
-
|
|
139
|
+
export default async function jsonBodyParser(ctx, next) {
|
|
140
140
|
|
|
141
141
|
const method = ctx.method;
|
|
142
142
|
const isJson =
|
|
@@ -167,6 +167,6 @@ const jsonBodyParser = async (ctx, next) => {
|
|
|
167
167
|
|
|
168
168
|
await next();
|
|
169
169
|
|
|
170
|
-
}
|
|
170
|
+
}
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
;
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { promises as fs } from "fs";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
4
|
+
|
|
5
|
+
import fetchPage from "../fetchPage.js";
|
|
6
|
+
import pageInfo from "../pageInfo.js";
|
|
7
|
+
import validateRequest from "../validateRequest.js";
|
|
8
|
+
import staticFiles from "./staticFiles.js";
|
|
9
|
+
import finalFormat from "./finalFormat.js";
|
|
10
|
+
import getConfig from "../getConfig.js";
|
|
11
|
+
|
|
12
|
+
export default async function routeRequest(ctx, next) {
|
|
13
|
+
|
|
14
|
+
const awesomenessConfig = getConfig();
|
|
15
|
+
|
|
16
|
+
const awesomenessRequest = ctx.awesomenessRequest;
|
|
17
|
+
|
|
18
|
+
// make sure there is no dots in the path
|
|
19
|
+
// regex non alphanumeric or forward slash
|
|
20
|
+
if (awesomenessRequest.pageRoute.match(/[^a-zA-Z0-9\/\_\-]/)) {
|
|
21
|
+
|
|
22
|
+
ctx.status = 404;
|
|
23
|
+
|
|
24
|
+
ctx.body = {
|
|
25
|
+
success: false,
|
|
26
|
+
message: "Invalid path",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
finalFormat(awesomenessRequest, ctx);
|
|
30
|
+
|
|
31
|
+
return;
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (awesomenessRequest.awesomenessType === "page") {
|
|
36
|
+
|
|
37
|
+
awesomenessRequest.log('yeah we are live (PAGE)');
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
|
|
41
|
+
let {
|
|
42
|
+
about, cssPath, jsPath, getData
|
|
43
|
+
} = await pageInfo(awesomenessRequest);
|
|
44
|
+
|
|
45
|
+
awesomenessRequest.routeInfo = about;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
|
|
49
|
+
await validateRequest(awesomenessRequest);
|
|
50
|
+
|
|
51
|
+
} catch (error) {
|
|
52
|
+
|
|
53
|
+
ctx.status = 422;
|
|
54
|
+
|
|
55
|
+
ctx.body = {
|
|
56
|
+
success: false,
|
|
57
|
+
...error,
|
|
58
|
+
stack: error.stack,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
finalFormat(awesomenessRequest, ctx);
|
|
62
|
+
|
|
63
|
+
return;
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const page = awesomenessRequest.mdContent ? "_md" : awesomenessRequest.pageRoute;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
|
|
71
|
+
const pageData = await fetchPage(awesomenessRequest, {
|
|
72
|
+
getData,
|
|
73
|
+
about,
|
|
74
|
+
cssPath,
|
|
75
|
+
jsPath,
|
|
76
|
+
page,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
ctx.body = {
|
|
80
|
+
success: true,
|
|
81
|
+
meta: awesomenessRequest.updatedMeta,
|
|
82
|
+
...pageData,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
finalFormat(awesomenessRequest, ctx);
|
|
86
|
+
|
|
87
|
+
return;
|
|
88
|
+
|
|
89
|
+
} catch (error) {
|
|
90
|
+
|
|
91
|
+
ctx.status = 500;
|
|
92
|
+
|
|
93
|
+
ctx.body = {
|
|
94
|
+
success: false,
|
|
95
|
+
message: "Page exists but error fetching data.",
|
|
96
|
+
meta: awesomenessRequest.updatedMeta,
|
|
97
|
+
error,
|
|
98
|
+
page,
|
|
99
|
+
errMessage: error?.message || "Error in getData function",
|
|
100
|
+
stack: error.stack,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
finalFormat(awesomenessRequest, ctx);
|
|
104
|
+
|
|
105
|
+
return;
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
} catch (error) {
|
|
110
|
+
|
|
111
|
+
ctx.status = 404;
|
|
112
|
+
|
|
113
|
+
ctx.body = {
|
|
114
|
+
success: false,
|
|
115
|
+
error,
|
|
116
|
+
message: error?.message || "Error fetching page",
|
|
117
|
+
stack: error.stack,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
finalFormat(awesomenessRequest, ctx);
|
|
121
|
+
|
|
122
|
+
return;
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
} else if (awesomenessRequest.awesomenessType === "api") {
|
|
127
|
+
|
|
128
|
+
awesomenessRequest.log('yeah we are live (API)');
|
|
129
|
+
|
|
130
|
+
const {
|
|
131
|
+
siteDir__URL,
|
|
132
|
+
commonApiDir__URL
|
|
133
|
+
} = awesomenessConfig;
|
|
134
|
+
|
|
135
|
+
// siteDir__URL should point at the /sites/ directory (as a file: URL)
|
|
136
|
+
if (!(siteDir__URL instanceof URL) || !(commonApiDir__URL instanceof URL)) {
|
|
137
|
+
|
|
138
|
+
ctx.throw(
|
|
139
|
+
500,
|
|
140
|
+
new Error("Config must provide siteDir__URL and commonApiDir__URL as file: URLs")
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return;
|
|
144
|
+
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const sitesRoot = fileURLToPath(siteDir__URL);
|
|
148
|
+
const commonApiRoot = fileURLToPath(commonApiDir__URL);
|
|
149
|
+
|
|
150
|
+
const siteApiRoutesRoot = path.join(sitesRoot, awesomenessRequest.site, "api", "routes");
|
|
151
|
+
const commonApiRoutesRoot = path.join(commonApiRoot, "routes");
|
|
152
|
+
|
|
153
|
+
const routeRel = path.join(awesomenessRequest.pageRoute, "index.js");
|
|
154
|
+
const infoRel = path.join(awesomenessRequest.pageRoute, "_info.js");
|
|
155
|
+
|
|
156
|
+
const siteSpecificIndexPath = path.join(siteApiRoutesRoot, routeRel);
|
|
157
|
+
const siteSpecificInfoPath = path.join(siteApiRoutesRoot, infoRel);
|
|
158
|
+
|
|
159
|
+
const genericIndexPath = path.join(commonApiRoutesRoot, routeRel);
|
|
160
|
+
const genericInfoPath = path.join(commonApiRoutesRoot, infoRel);
|
|
161
|
+
|
|
162
|
+
let routeIndex;
|
|
163
|
+
let routeInfo;
|
|
164
|
+
|
|
165
|
+
let specific = false;
|
|
166
|
+
|
|
167
|
+
// Prefer filesystem existence checks so we don't rely on import() throwing for control flow
|
|
168
|
+
try {
|
|
169
|
+
|
|
170
|
+
awesomenessRequest.log('Trying site specific route.', {
|
|
171
|
+
siteSpecificIndexPath,
|
|
172
|
+
siteSpecificInfoPath
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
await fs.access(siteSpecificIndexPath);
|
|
176
|
+
await fs.access(siteSpecificInfoPath);
|
|
177
|
+
|
|
178
|
+
awesomenessRequest.log('Site specific route found.', {
|
|
179
|
+
siteSpecificIndexPath,
|
|
180
|
+
siteSpecificInfoPath
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
routeIndex = await import(pathToFileURL(siteSpecificIndexPath).href);
|
|
184
|
+
const infoMod = await import(pathToFileURL(siteSpecificInfoPath).href);
|
|
185
|
+
|
|
186
|
+
routeInfo = infoMod.default;
|
|
187
|
+
|
|
188
|
+
awesomenessRequest.log('Site specific route info loaded.', {
|
|
189
|
+
routeInfo
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
specific = true;
|
|
194
|
+
|
|
195
|
+
} catch (error) {
|
|
196
|
+
|
|
197
|
+
awesomenessRequest.log('Site specific route not found, trying generic route.', { error });
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
|
|
201
|
+
await fs.access(genericIndexPath);
|
|
202
|
+
await fs.access(genericInfoPath);
|
|
203
|
+
|
|
204
|
+
routeIndex = await import(pathToFileURL(genericIndexPath).href);
|
|
205
|
+
const infoMod = await import(pathToFileURL(genericInfoPath).href);
|
|
206
|
+
|
|
207
|
+
routeInfo = infoMod.default;
|
|
208
|
+
|
|
209
|
+
specific = false;
|
|
210
|
+
|
|
211
|
+
} catch (error2) {
|
|
212
|
+
|
|
213
|
+
ctx.status = 404;
|
|
214
|
+
|
|
215
|
+
ctx.body = {
|
|
216
|
+
success: false,
|
|
217
|
+
message: "route not found",
|
|
218
|
+
error: error2,
|
|
219
|
+
siteSpecificRoute: siteSpecificIndexPath,
|
|
220
|
+
genericRoute: genericIndexPath,
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
finalFormat(awesomenessRequest, ctx);
|
|
224
|
+
|
|
225
|
+
return;
|
|
226
|
+
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
|
|
233
|
+
// store routeInfo
|
|
234
|
+
awesomenessRequest.specific = specific;
|
|
235
|
+
awesomenessRequest.routeInfo = routeInfo;
|
|
236
|
+
|
|
237
|
+
// validate data
|
|
238
|
+
try {
|
|
239
|
+
|
|
240
|
+
await validateRequest(awesomenessRequest);
|
|
241
|
+
|
|
242
|
+
} catch (error) {
|
|
243
|
+
|
|
244
|
+
ctx.status = 422;
|
|
245
|
+
|
|
246
|
+
ctx.body = {
|
|
247
|
+
success: false,
|
|
248
|
+
...error,
|
|
249
|
+
stack: error.stack,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
finalFormat(awesomenessRequest, ctx);
|
|
253
|
+
|
|
254
|
+
return;
|
|
255
|
+
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const data = await routeIndex.default(awesomenessRequest);
|
|
259
|
+
|
|
260
|
+
ctx.body = {
|
|
261
|
+
success: true,
|
|
262
|
+
meta: awesomenessRequest.updatedMeta,
|
|
263
|
+
...data,
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
finalFormat(awesomenessRequest, ctx);
|
|
267
|
+
|
|
268
|
+
} catch (error) {
|
|
269
|
+
|
|
270
|
+
ctx.status = 500;
|
|
271
|
+
|
|
272
|
+
if (awesomenessRequest.status) {
|
|
273
|
+
|
|
274
|
+
ctx.status = awesomenessRequest.status;
|
|
275
|
+
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
ctx.body = {
|
|
279
|
+
success: false,
|
|
280
|
+
meta: awesomenessRequest.updatedMeta,
|
|
281
|
+
message: error?.message || "Error in route function",
|
|
282
|
+
error,
|
|
283
|
+
stack: error.stack,
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
finalFormat(awesomenessRequest, ctx);
|
|
287
|
+
|
|
288
|
+
return;
|
|
289
|
+
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
} else {
|
|
293
|
+
|
|
294
|
+
// if its a get request
|
|
295
|
+
// a static file should have been served already
|
|
296
|
+
// if the application is working correctly then ctx.awesomenessType === 'page' should be true
|
|
297
|
+
// if it is not true, they are hitting a url cold or reloading
|
|
298
|
+
// so load the app
|
|
299
|
+
|
|
300
|
+
if (awesomenessRequest.method != "GET") {
|
|
301
|
+
|
|
302
|
+
ctx.status = 405;
|
|
303
|
+
ctx.body = {
|
|
304
|
+
success: false,
|
|
305
|
+
message: "Method Not Allowed",
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
finalFormat(awesomenessRequest, ctx);
|
|
309
|
+
|
|
310
|
+
return;
|
|
311
|
+
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (awesomenessRequest.path === "/test") {
|
|
315
|
+
|
|
316
|
+
ctx.body = {
|
|
317
|
+
success: true,
|
|
318
|
+
message: "Hello, World! (just a test)",
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
finalFormat(awesomenessRequest, ctx);
|
|
322
|
+
|
|
323
|
+
return;
|
|
324
|
+
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
awesomenessRequest.path = "/";
|
|
328
|
+
await staticFiles(ctx, next);
|
|
329
|
+
|
|
330
|
+
return;
|
|
331
|
+
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
}
|
|
@@ -2,9 +2,9 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import send from 'koa-send';
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import
|
|
5
|
+
import getConfig from "../getConfig.js";
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
export default async function staticFiles(ctx, next) {
|
|
8
8
|
|
|
9
9
|
const {
|
|
10
10
|
method,
|
|
@@ -12,9 +12,10 @@ const staticFiles = async (ctx, next) => {
|
|
|
12
12
|
site
|
|
13
13
|
} = ctx.awesomenessRequest;
|
|
14
14
|
|
|
15
|
+
|
|
15
16
|
const {
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
siteDir__URL,
|
|
18
|
+
commonPublicDir__URL
|
|
18
19
|
} = getConfig();
|
|
19
20
|
|
|
20
21
|
if (method !== "GET" && method !== "HEAD") {
|
|
@@ -25,16 +26,16 @@ const staticFiles = async (ctx, next) => {
|
|
|
25
26
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
if (!(
|
|
29
|
+
if (!(siteDir__URL instanceof URL) || !(commonPublicDir__URL instanceof URL)) {
|
|
29
30
|
|
|
30
|
-
ctx.throw(500, new Error("Config must provide
|
|
31
|
+
ctx.throw(500, new Error("Config must provide siteDir__URL and commonPublicDir__URL as file: URLs"));
|
|
31
32
|
|
|
32
33
|
return;
|
|
33
34
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
const sitesRoot = fileURLToPath(
|
|
37
|
-
const commonRoot = fileURLToPath(
|
|
37
|
+
const sitesRoot = fileURLToPath(siteDir__URL);
|
|
38
|
+
const commonRoot = fileURLToPath(commonPublicDir__URL);
|
|
38
39
|
|
|
39
40
|
const domainRoot = path.join(sitesRoot, site, "public");
|
|
40
41
|
|
|
@@ -92,6 +93,4 @@ const staticFiles = async (ctx, next) => {
|
|
|
92
93
|
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export { staticFiles };
|
|
96
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import getConfig from "./getConfig.js";
|
|
2
2
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { fileURLToPath, pathToFileURL } from "url";
|
|
5
5
|
import { existsSync } from "fs";
|
|
6
6
|
import { promises as fs } from "fs";
|
|
7
|
-
import
|
|
7
|
+
import resolveRealCasePath from "./resolveRealCasePath.js";
|
|
8
8
|
|
|
9
9
|
export default async function pageInfo(awesomenessRequest, { page = null } = {}) {
|
|
10
10
|
|
|
@@ -18,11 +18,11 @@ export default async function pageInfo(awesomenessRequest, { page = null } = {})
|
|
|
18
18
|
|
|
19
19
|
const slug = page.split("/").pop().split(".")[0].split("?")[0];
|
|
20
20
|
|
|
21
|
-
// awesomenessConfig.
|
|
21
|
+
// awesomenessConfig.siteDir__URL points at the /sites/ directory (as a URL)
|
|
22
22
|
const sitesRootPath =
|
|
23
|
-
awesomenessConfig.
|
|
24
|
-
? fileURLToPath(awesomenessConfig.
|
|
25
|
-
: awesomenessConfig.
|
|
23
|
+
awesomenessConfig.siteDir__URL instanceof URL
|
|
24
|
+
? fileURLToPath(awesomenessConfig.siteDir__URL)
|
|
25
|
+
: awesomenessConfig.siteDir__URL;
|
|
26
26
|
|
|
27
27
|
const siteRootPath = path.join(sitesRootPath, awesomenessRequest.site);
|
|
28
28
|
const pagesRootPath = path.join(siteRootPath, "pages");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import specialPaths from './specialPaths.js';
|
|
2
2
|
import fetchPage from './fetchPage.js';
|
|
3
|
-
import
|
|
3
|
+
import getConfig from "./getConfig.js";
|
|
4
4
|
|
|
5
5
|
export default async function reRoute({
|
|
6
6
|
goToPage,
|
|
@@ -17,7 +17,8 @@ export default async function reRoute({
|
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
|
|
21
|
+
|
|
21
22
|
if(!awesomenessRequest.reRoutes){
|
|
22
23
|
|
|
23
24
|
awesomenessRequest.reRoutes = [];
|
|
@@ -5,7 +5,7 @@ import path from "path";
|
|
|
5
5
|
* Case-insensitive real path resolver.
|
|
6
6
|
* Works correctly on Windows, macOS, and Linux/Docker.
|
|
7
7
|
*/
|
|
8
|
-
export function resolveRealCasePath(inputPath, returnAbsolute = false) {
|
|
8
|
+
export default function resolveRealCasePath(inputPath, returnAbsolute = false) {
|
|
9
9
|
|
|
10
10
|
if (!inputPath) return null;
|
|
11
11
|
|
|
@@ -2,23 +2,24 @@ import Koa from 'koa';
|
|
|
2
2
|
import compress from 'koa-compress';
|
|
3
3
|
import http from "http";
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
5
|
+
import attachAwesomenessRequest from './koa/attachAwesomenessRequest.js';
|
|
6
|
+
import cors from './koa/cors.js';
|
|
7
|
+
import errorHandler from './koa/errorHandler.js';
|
|
8
|
+
import jsonBodyParser from './koa/jsonBodyParser.js';
|
|
9
|
+
import serverUp from './koa/serverUp.js';
|
|
10
|
+
import timeout from './koa/timeout.js';
|
|
11
|
+
import staticFiles from "./koa/staticFiles.js";
|
|
12
|
+
import routeRequest from './koa/routeRequest.js';
|
|
13
|
+
import attachWs from "./ws/index.js";
|
|
14
|
+
import getConfig from './getConfig.js';
|
|
14
15
|
|
|
15
|
-
import
|
|
16
|
-
|
|
17
|
-
import { getConfig } from './config.js';
|
|
16
|
+
import '../errors.js'; // handle errors
|
|
18
17
|
|
|
19
18
|
|
|
20
19
|
export default async function start(){
|
|
21
20
|
|
|
21
|
+
console.log('Initializing Awesomeness server...');
|
|
22
|
+
|
|
22
23
|
const awesomenessConfig = getConfig();
|
|
23
24
|
|
|
24
25
|
console.log(`Starting Awesomeness server on port ${process.env.PORT}`);
|
|
@@ -119,4 +120,4 @@ export default async function start(){
|
|
|
119
120
|
});
|
|
120
121
|
|
|
121
122
|
|
|
122
|
-
}
|
|
123
|
+
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import getConfig from "../getConfig.js";
|
|
2
2
|
|
|
3
|
-
export function handleWsMessage({
|
|
3
|
+
export default function handleWsMessage({
|
|
4
4
|
socket,
|
|
5
5
|
raw
|
|
6
6
|
}) {
|
|
7
7
|
|
|
8
|
+
|
|
8
9
|
const awesomenessConfig = getConfig();
|
|
9
10
|
|
|
11
|
+
|
|
10
12
|
let msg;
|
|
11
13
|
|
|
12
14
|
try {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { WebSocketServer } from "ws";
|
|
2
|
-
import
|
|
2
|
+
import handleWsMessage from "./handlers.js";
|
|
3
3
|
|
|
4
|
-
export function attachWs(server, path = "/ws") {
|
|
4
|
+
export default function attachWs(server, path = "/ws") {
|
|
5
5
|
|
|
6
6
|
// noServer mode so we own upgrades
|
|
7
7
|
const wss = new WebSocketServer({ noServer: true });
|