chore(project) cleanup; structure adjustments; preparational work
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 gh0stedBuddy
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
0
docs/.gitkeep
Normal file
0
docs/.gitkeep
Normal file
@@ -15,8 +15,6 @@
|
|||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"types": [
|
"types": []
|
||||||
"bun-types" // add Bun global
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "layc",
|
"name": "layc",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "lib/layc.cjs.js",
|
|
||||||
"module": "lib/layc.js",
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bun-types": "latest",
|
"bun-types": "latest",
|
||||||
"chokidar": "^3.5.3"
|
"chokidar": "^3.5.3"
|
||||||
@@ -12,8 +10,5 @@
|
|||||||
"examples": "bun run ./examples/index.js"
|
"examples": "bun run ./examples/index.js"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {}
|
||||||
"@googleapis/youtube": "^12.0.0",
|
|
||||||
"googleapis": "^127.0.0"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,4 +99,11 @@ export default class LayResponse {
|
|||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
render(path, data = {}) {
|
||||||
|
return new Response(this.router.getServer().load(path).parse(data), {
|
||||||
|
status: this.status(),
|
||||||
|
headers: this.headers(),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,17 +5,18 @@ import HtmlEngine from './Views/HtmlEngine';
|
|||||||
|
|
||||||
export default class LayServer {
|
export default class LayServer {
|
||||||
controllers = new Map();
|
controllers = new Map();
|
||||||
|
loaders = new Map();
|
||||||
proc = null;
|
proc = null;
|
||||||
router;
|
router;
|
||||||
publicFiles;
|
publicFiles;
|
||||||
options = new Map([
|
options = new Map([
|
||||||
|
['loaders', undefined],
|
||||||
['publicDir', undefined],
|
['publicDir', undefined],
|
||||||
['viewsPath', undefined],
|
['viewsPath', undefined],
|
||||||
['viewsEngine', undefined],
|
|
||||||
['onlySingleRoutes', true],
|
|
||||||
['preferIndexFiles', false],
|
['preferIndexFiles', false],
|
||||||
['discoverPublicFiles', true],
|
['discoverPublicFiles', false],
|
||||||
['enforceTrailingSlash', true],
|
['enforceTrailingSlash', false],
|
||||||
|
['laycLoading', false],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
@@ -25,7 +26,11 @@ export default class LayServer {
|
|||||||
for (const key of optionKeys) {
|
for (const key of optionKeys) {
|
||||||
if (!this.options.has(key)) continue;
|
if (!this.options.has(key)) continue;
|
||||||
const valueType = typeof this.options.get(key);
|
const valueType = typeof this.options.get(key);
|
||||||
if (typeof options[key] == valueType)
|
if (
|
||||||
|
typeof options[key] !== 'undefined' &&
|
||||||
|
(typeof options[key] == valueType ||
|
||||||
|
valueType == 'undefined')
|
||||||
|
)
|
||||||
this.options.set(key, options[key]);
|
this.options.set(key, options[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,6 +42,7 @@ export default class LayServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.publicFiles) {
|
if (options.publicFiles) {
|
||||||
|
console.log('publicFiles', options.publicFiles);
|
||||||
this.addPublicFileRoutes(options.publicFiles);
|
this.addPublicFileRoutes(options.publicFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +70,11 @@ export default class LayServer {
|
|||||||
throw new Error(
|
throw new Error(
|
||||||
'publicFiles option has to be an array or undefined'
|
'publicFiles option has to be an array or undefined'
|
||||||
);
|
);
|
||||||
|
if (!this.publicFiles || !Array.isArray(this.publicFiles))
|
||||||
|
this.publicFiles = [];
|
||||||
for (let path of filePaths) {
|
for (let path of filePaths) {
|
||||||
|
if (typeof path != 'string') continue;
|
||||||
|
this.publicFiles.push(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,8 +87,7 @@ export default class LayServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
registerControllers(controllers) {
|
registerControllers(controllers) {
|
||||||
if (!Array.isArray(controllers))
|
if (!Array.isArray(controllers)) controllers = [controllers];
|
||||||
throw new Error('controllers is not an Array');
|
|
||||||
|
|
||||||
for (let controller of controllers) {
|
for (let controller of controllers) {
|
||||||
if (typeof controller != 'function') {
|
if (typeof controller != 'function') {
|
||||||
@@ -86,8 +95,14 @@ export default class LayServer {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if the given controller is just an anonymous function defining the routes itself
|
||||||
|
if (!controller?.prototype?.constructor?.name) {
|
||||||
|
controller(this.routeControl);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
this.controllers.set(
|
this.controllers.set(
|
||||||
controller.name,
|
controller?.name,
|
||||||
this.instantiateController(controller)
|
this.instantiateController(controller)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -118,10 +133,16 @@ export default class LayServer {
|
|||||||
if (path) {
|
if (path) {
|
||||||
try {
|
try {
|
||||||
return new Response(
|
return new Response(
|
||||||
Bun.file(resolvePath(import.meta.dir, 'public', path))
|
Bun.file(
|
||||||
|
resolvePath(
|
||||||
|
this.options.get('publicDir') ??
|
||||||
|
`${import.meta.dir}/public`,
|
||||||
|
path
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err, import.meta.dir, 'public', path);
|
||||||
return new Response('Internal Server Error', {
|
return new Response('Internal Server Error', {
|
||||||
status: 500,
|
status: 500,
|
||||||
});
|
});
|
||||||
@@ -133,7 +154,11 @@ export default class LayServer {
|
|||||||
return Response.redirect(`${url.toString()}/`, 302);
|
return Response.redirect(`${url.toString()}/`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.router.handle(req);
|
let resp = await this.router.handle(req);
|
||||||
|
|
||||||
|
if (!resp) return new Response('Not Found', { status: 404 });
|
||||||
|
|
||||||
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
listen(port = 8080) {
|
listen(port = 8080) {
|
||||||
@@ -165,4 +190,15 @@ export default class LayServer {
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
load(filepath) {
|
||||||
|
const filename = filepath.split('/').pop().toString();
|
||||||
|
const ext = filename.split('.').pop().toLowerCase();
|
||||||
|
|
||||||
|
for (const keys of this.loaders.keys()) {
|
||||||
|
if (keys.includes(ext)) {
|
||||||
|
return this.loaders.get(keys)(filepath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,10 @@ export default class Router {
|
|||||||
this.server = server;
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getServer() {
|
||||||
|
return this.server;
|
||||||
|
}
|
||||||
|
|
||||||
async handle(req) {
|
async handle(req) {
|
||||||
if (!(req instanceof Request)) {
|
if (!(req instanceof Request)) {
|
||||||
throw new Error('`req` is not a proper Request');
|
throw new Error('`req` is not a proper Request');
|
||||||
Reference in New Issue
Block a user