elegance-js 2.1.6 → 2.1.7

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.
@@ -307,13 +307,16 @@ var MIME_TYPES = {
307
307
  };
308
308
  function startServer({
309
309
  root,
310
+ pagesDirectory,
310
311
  port = 3e3,
311
312
  host = "localhost",
312
313
  environment = "production",
313
314
  DIST_DIR: DIST_DIR2
314
315
  }) {
315
316
  if (!root) throw new Error("Root directory must be specified.");
317
+ if (!pagesDirectory) throw new Error("Pages directory must be specified.");
316
318
  root = normalize(root).replace(/[\\/]+$/, "");
319
+ pagesDirectory = normalize(pagesDirectory).replace(/[\\/]+$/, "");
317
320
  const requestHandler = async (req, res) => {
318
321
  try {
319
322
  if (!req.url) {
@@ -333,11 +336,11 @@ function startServer({
333
336
  }
334
337
  const url = new URL(req.url, `http://${req.headers.host}`);
335
338
  if (url.pathname.startsWith("/api/")) {
336
- await handleApiRequest(root, url.pathname, req, res);
339
+ await handleApiRequest(pagesDirectory, url.pathname, req, res);
337
340
  } else if (PAGE_MAP.has(url.pathname)) {
338
- await handlePageRequest(root, url.pathname, req, res, DIST_DIR2, PAGE_MAP.get(url.pathname));
341
+ await handlePageRequest(root, pagesDirectory, url.pathname, req, res, DIST_DIR2, PAGE_MAP.get(url.pathname));
339
342
  } else {
340
- await handleStaticRequest(root, url.pathname, req, res, DIST_DIR2);
343
+ await handleStaticRequest(root, pagesDirectory, url.pathname, req, res, DIST_DIR2);
341
344
  }
342
345
  if (environment === "development") {
343
346
  log.info(req.method, "::", req.url, "-", res.statusCode);
@@ -395,7 +398,7 @@ function getMiddlewareDirs(base, parts) {
395
398
  async function collectMiddlewares(dirs) {
396
399
  const middlewares = [];
397
400
  for (const dir of dirs) {
398
- const mwPath = join(dir, "middleware.mjs");
401
+ const mwPath = join(dir, "middleware.ts");
399
402
  let mwModule;
400
403
  try {
401
404
  await fs.access(mwPath);
@@ -414,12 +417,12 @@ async function collectMiddlewares(dirs) {
414
417
  }
415
418
  return middlewares;
416
419
  }
417
- async function handlePageRequest(root, pathname, req, res, DIST_DIR2, pageInfo) {
420
+ async function handlePageRequest(root, pagesDirectory, pathname, req, res, DIST_DIR2, pageInfo) {
418
421
  try {
419
422
  const { filePath, targetDir, stats } = await getTargetInfo(root, pathname);
420
423
  const relDir = targetDir.slice(root.length).replace(/^[\/\\]+/, "");
421
424
  const parts = relDir.split(/[\\/]/).filter(Boolean);
422
- const middlewareDirs = getMiddlewareDirs(root, parts);
425
+ const middlewareDirs = getMiddlewareDirs(pagesDirectory, parts);
423
426
  const middlewares = await collectMiddlewares(middlewareDirs);
424
427
  let isDynamic = pageInfo.isDynamic;
425
428
  const handlerPath = isDynamic ? pageInfo.filePath : join(filePath, "index.html");
@@ -471,12 +474,12 @@ async function handlePageRequest(root, pathname, req, res, DIST_DIR2, pageInfo)
471
474
  }
472
475
  }
473
476
  }
474
- async function handleStaticRequest(root, pathname, req, res, DIST_DIR2) {
477
+ async function handleStaticRequest(root, pagesDirectory, pathname, req, res, DIST_DIR2) {
475
478
  try {
476
479
  const { filePath, targetDir, stats } = await getTargetInfo(root, pathname);
477
480
  const relDir = targetDir.slice(root.length).replace(/^[\/\\]+/, "");
478
481
  const parts = relDir.split(/[\\/]/).filter(Boolean);
479
- const middlewareDirs = getMiddlewareDirs(root, parts);
482
+ const middlewareDirs = getMiddlewareDirs(pagesDirectory, parts);
480
483
  const middlewares = await collectMiddlewares(middlewareDirs);
481
484
  let handlerPath = filePath;
482
485
  if (stats && stats.isDirectory()) {
@@ -510,13 +513,13 @@ async function handleStaticRequest(root, pathname, req, res, DIST_DIR2) {
510
513
  }
511
514
  }
512
515
  }
513
- async function handleApiRequest(root, pathname, req, res) {
516
+ async function handleApiRequest(pagesDirectory, pathname, req, res) {
514
517
  const apiSubPath = pathname.slice("/api/".length);
515
518
  const parts = apiSubPath.split("/").filter(Boolean);
516
- const middlewareDirs = getMiddlewareDirs(join(root, "api"), parts);
519
+ const middlewareDirs = getMiddlewareDirs(join(pagesDirectory, "api"), parts);
517
520
  const middlewares = await collectMiddlewares(middlewareDirs);
518
521
  const routeDir = middlewareDirs[middlewareDirs.length - 1];
519
- const routePath = join(routeDir, "route.mjs");
522
+ const routePath = join(routeDir, "route.ts");
520
523
  let hasRoute = false;
521
524
  try {
522
525
  await fs.access(routePath);
@@ -1521,7 +1524,8 @@ var build = async () => {
1521
1524
  environment: options.environment,
1522
1525
  port: options.server.port ?? 3e3,
1523
1526
  host: options.server.host ?? "localhost",
1524
- DIST_DIR
1527
+ DIST_DIR,
1528
+ pagesDirectory: options.pagesDirectory
1525
1529
  });
1526
1530
  }
1527
1531
  process.send?.({ event: "message", data: "compile-finish" });
@@ -1,10 +1,11 @@
1
1
  import { IncomingMessage, ServerResponse } from 'http';
2
2
  interface ServerOptions {
3
3
  root: string;
4
+ pagesDirectory: string;
4
5
  port?: number;
5
6
  host?: string;
6
7
  environment?: 'production' | 'development';
7
8
  DIST_DIR: string;
8
9
  }
9
- export declare function startServer({ root, port, host, environment, DIST_DIR }: ServerOptions): import("http").Server<typeof IncomingMessage, typeof ServerResponse>;
10
+ export declare function startServer({ root, pagesDirectory, port, host, environment, DIST_DIR }: ServerOptions): import("http").Server<typeof IncomingMessage, typeof ServerResponse>;
10
11
  export {};
@@ -1154,7 +1154,8 @@ var build = async () => {
1154
1154
  environment: options.environment,
1155
1155
  port: options.server.port ?? 3e3,
1156
1156
  host: options.server.host ?? "localhost",
1157
- DIST_DIR
1157
+ DIST_DIR,
1158
+ pagesDirectory: options.pagesDirectory
1158
1159
  });
1159
1160
  }
1160
1161
  process.send?.({ event: "message", data: "compile-finish" });
@@ -1192,13 +1193,16 @@ var MIME_TYPES = {
1192
1193
  };
1193
1194
  function startServer({
1194
1195
  root,
1196
+ pagesDirectory,
1195
1197
  port = 3e3,
1196
1198
  host = "localhost",
1197
1199
  environment = "production",
1198
1200
  DIST_DIR: DIST_DIR2
1199
1201
  }) {
1200
1202
  if (!root) throw new Error("Root directory must be specified.");
1203
+ if (!pagesDirectory) throw new Error("Pages directory must be specified.");
1201
1204
  root = normalize(root).replace(/[\\/]+$/, "");
1205
+ pagesDirectory = normalize(pagesDirectory).replace(/[\\/]+$/, "");
1202
1206
  const requestHandler = async (req, res) => {
1203
1207
  try {
1204
1208
  if (!req.url) {
@@ -1218,11 +1222,11 @@ function startServer({
1218
1222
  }
1219
1223
  const url = new URL(req.url, `http://${req.headers.host}`);
1220
1224
  if (url.pathname.startsWith("/api/")) {
1221
- await handleApiRequest(root, url.pathname, req, res);
1225
+ await handleApiRequest(pagesDirectory, url.pathname, req, res);
1222
1226
  } else if (PAGE_MAP.has(url.pathname)) {
1223
- await handlePageRequest(root, url.pathname, req, res, DIST_DIR2, PAGE_MAP.get(url.pathname));
1227
+ await handlePageRequest(root, pagesDirectory, url.pathname, req, res, DIST_DIR2, PAGE_MAP.get(url.pathname));
1224
1228
  } else {
1225
- await handleStaticRequest(root, url.pathname, req, res, DIST_DIR2);
1229
+ await handleStaticRequest(root, pagesDirectory, url.pathname, req, res, DIST_DIR2);
1226
1230
  }
1227
1231
  if (environment === "development") {
1228
1232
  log.info(req.method, "::", req.url, "-", res.statusCode);
@@ -1280,7 +1284,7 @@ function getMiddlewareDirs(base, parts) {
1280
1284
  async function collectMiddlewares(dirs) {
1281
1285
  const middlewares = [];
1282
1286
  for (const dir of dirs) {
1283
- const mwPath = join(dir, "middleware.mjs");
1287
+ const mwPath = join(dir, "middleware.ts");
1284
1288
  let mwModule;
1285
1289
  try {
1286
1290
  await fs2.access(mwPath);
@@ -1299,12 +1303,12 @@ async function collectMiddlewares(dirs) {
1299
1303
  }
1300
1304
  return middlewares;
1301
1305
  }
1302
- async function handlePageRequest(root, pathname, req, res, DIST_DIR2, pageInfo) {
1306
+ async function handlePageRequest(root, pagesDirectory, pathname, req, res, DIST_DIR2, pageInfo) {
1303
1307
  try {
1304
1308
  const { filePath, targetDir, stats } = await getTargetInfo(root, pathname);
1305
1309
  const relDir = targetDir.slice(root.length).replace(/^[\/\\]+/, "");
1306
1310
  const parts = relDir.split(/[\\/]/).filter(Boolean);
1307
- const middlewareDirs = getMiddlewareDirs(root, parts);
1311
+ const middlewareDirs = getMiddlewareDirs(pagesDirectory, parts);
1308
1312
  const middlewares = await collectMiddlewares(middlewareDirs);
1309
1313
  let isDynamic = pageInfo.isDynamic;
1310
1314
  const handlerPath = isDynamic ? pageInfo.filePath : join(filePath, "index.html");
@@ -1356,12 +1360,12 @@ async function handlePageRequest(root, pathname, req, res, DIST_DIR2, pageInfo)
1356
1360
  }
1357
1361
  }
1358
1362
  }
1359
- async function handleStaticRequest(root, pathname, req, res, DIST_DIR2) {
1363
+ async function handleStaticRequest(root, pagesDirectory, pathname, req, res, DIST_DIR2) {
1360
1364
  try {
1361
1365
  const { filePath, targetDir, stats } = await getTargetInfo(root, pathname);
1362
1366
  const relDir = targetDir.slice(root.length).replace(/^[\/\\]+/, "");
1363
1367
  const parts = relDir.split(/[\\/]/).filter(Boolean);
1364
- const middlewareDirs = getMiddlewareDirs(root, parts);
1368
+ const middlewareDirs = getMiddlewareDirs(pagesDirectory, parts);
1365
1369
  const middlewares = await collectMiddlewares(middlewareDirs);
1366
1370
  let handlerPath = filePath;
1367
1371
  if (stats && stats.isDirectory()) {
@@ -1395,13 +1399,13 @@ async function handleStaticRequest(root, pathname, req, res, DIST_DIR2) {
1395
1399
  }
1396
1400
  }
1397
1401
  }
1398
- async function handleApiRequest(root, pathname, req, res) {
1402
+ async function handleApiRequest(pagesDirectory, pathname, req, res) {
1399
1403
  const apiSubPath = pathname.slice("/api/".length);
1400
1404
  const parts = apiSubPath.split("/").filter(Boolean);
1401
- const middlewareDirs = getMiddlewareDirs(join(root, "api"), parts);
1405
+ const middlewareDirs = getMiddlewareDirs(join(pagesDirectory, "api"), parts);
1402
1406
  const middlewares = await collectMiddlewares(middlewareDirs);
1403
1407
  const routeDir = middlewareDirs[middlewareDirs.length - 1];
1404
- const routePath = join(routeDir, "route.mjs");
1408
+ const routePath = join(routeDir, "route.ts");
1405
1409
  let hasRoute = false;
1406
1410
  try {
1407
1411
  await fs2.access(routePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "2.1.6",
3
+ "version": "2.1.7",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "bin": {