rasengan 1.0.0-beta.21 → 1.0.0-beta.23

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.
@@ -34,13 +34,109 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
+ import fs from "node:fs/promises";
38
+ import fsSync from "node:fs";
39
+ import path, { join, dirname } from "node:path";
40
+ import { fileURLToPath } from "node:url";
41
+ import { createStaticHandler, createStaticRouter, } from "react-router-dom/server.js";
37
42
  // @ts-ignore
38
- import { handleRequest } from "rasengan";
43
+ import { createFetchRequest } from "rasengan";
39
44
  export default (function (req, context) { return __awaiter(void 0, void 0, void 0, function () {
40
- return __generator(this, function (_a) {
41
- switch (_a.label) {
42
- case 0: return [4 /*yield*/, handleRequest(req)];
43
- case 1: return [2 /*return*/, _a.sent()];
45
+ var __filename, __dirname, url, host, appPath, err_1, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler, fetchRequest, context_1, status_1, redirect, helmetContext, router, rendered, html, e_1;
46
+ var _a;
47
+ return __generator(this, function (_b) {
48
+ switch (_b.label) {
49
+ case 0:
50
+ __filename = fileURLToPath(import.meta.url);
51
+ __dirname = dirname(__filename);
52
+ console.log({
53
+ cwd: process.cwd(),
54
+ __dirname: __dirname
55
+ });
56
+ // read dir
57
+ console.log({
58
+ readdir: fsSync.readdirSync(__dirname),
59
+ readdir2: fsSync.readdirSync(join(process.cwd(), "..")),
60
+ readdir3: fsSync.readdirSync(join(process.cwd(), "..", "..")),
61
+ });
62
+ _b.label = 1;
63
+ case 1:
64
+ _b.trys.push([1, 9, , 10]);
65
+ url = req.url;
66
+ host = req.headers.get("host");
67
+ appPath = join(__dirname, "..");
68
+ if (!(url === "/robots.txt")) return [3 /*break*/, 5];
69
+ _b.label = 2;
70
+ case 2:
71
+ _b.trys.push([2, 4, , 5]);
72
+ return [4 /*yield*/, fs.access(path.resolve(join(appPath, "dist/client/robots.txt")))];
73
+ case 3:
74
+ _b.sent();
75
+ return [2 /*return*/, new Response(path.resolve(join(appPath, "dist/client/robots.txt")))];
76
+ case 4:
77
+ err_1 = _b.sent();
78
+ return [2 /*return*/, new Response("\n user-agent: *\n disallow: /downloads/\n disallow: /private/\n allow: /\n \n user-agent: magicsearchbot\n disallow: /uploads/\n ")];
79
+ case 5:
80
+ // ! Sitemap Fix
81
+ if (url === "/sitemap.xml") {
82
+ return [2 /*return*/, new Response(path.resolve(join(appPath, "dist/client/sitemap.xml")))];
83
+ }
84
+ // ! Manifest Fix
85
+ if (url === "/manifest.json") {
86
+ return [2 /*return*/, new Response(path.resolve(join(appPath, "dist/client/manifest.json")))];
87
+ }
88
+ templateHtml = "";
89
+ serverFilePath = join(appPath, "dist/server/entry-server.js");
90
+ bootstrapDirPath = join(appPath, "dist/client/assets");
91
+ return [4 /*yield*/, import(serverFilePath)];
92
+ case 6:
93
+ entry = _b.sent();
94
+ bootstrap = "/assets/" +
95
+ fsSync
96
+ .readdirSync(bootstrapDirPath)
97
+ .filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".js"); })[0];
98
+ styles = "/assets/" +
99
+ fsSync
100
+ .readdirSync(join(appPath, "dist/client/assets"))
101
+ .filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".css"); })[0];
102
+ render = entry.render, staticRoutes = entry.staticRoutes, loadTemplateHtml = entry.loadTemplateHtml;
103
+ handler = createStaticHandler(staticRoutes);
104
+ fetchRequest = createFetchRequest(req, host);
105
+ return [4 /*yield*/, handler.query(fetchRequest)];
106
+ case 7:
107
+ context_1 = _b.sent();
108
+ status_1 = context_1.status;
109
+ if (status_1 === 302) {
110
+ redirect = context_1.headers.get("Location");
111
+ if (redirect) {
112
+ return [2 /*return*/, Response.redirect(redirect)];
113
+ }
114
+ }
115
+ helmetContext = {};
116
+ router = createStaticRouter(handler.dataRoutes, context_1);
117
+ return [4 /*yield*/, render(router, context_1, helmetContext)];
118
+ case 8:
119
+ rendered = _b.sent();
120
+ // Load template html
121
+ if (!templateHtml) {
122
+ templateHtml = loadTemplateHtml(helmetContext, bootstrap, styles);
123
+ }
124
+ html = templateHtml.replace("rasengan-body-app", (_a = rendered.html) !== null && _a !== void 0 ? _a : "");
125
+ // Send the rendered html page
126
+ return [2 /*return*/, new Response(html, {
127
+ status: 200,
128
+ headers: {
129
+ "Content-Type": "text/html",
130
+ "Cache-Control": "max-age=31536000",
131
+ },
132
+ })];
133
+ case 9:
134
+ e_1 = _b.sent();
135
+ console.log(e_1.stack);
136
+ return [2 /*return*/, new Response(e_1.stack, {
137
+ status: 500,
138
+ })];
139
+ case 10: return [2 /*return*/];
44
140
  }
45
141
  });
46
142
  }); });
@@ -34,15 +34,98 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
+ import fs from "node:fs/promises";
38
+ import fsSync from "node:fs";
39
+ import path, { join, dirname } from "node:path";
40
+ import { fileURLToPath } from "node:url";
41
+ import { createStaticHandler, createStaticRouter, } from "react-router-dom/server.js";
37
42
  // @ts-ignore
38
- import { handleRequest } from "rasengan";
43
+ import { createFetchRequest } from "rasengan";
39
44
  // Create server for production only
40
45
  export default function handler(req, res) {
46
+ var _a;
41
47
  return __awaiter(this, void 0, void 0, function () {
42
- return __generator(this, function (_a) {
43
- switch (_a.label) {
44
- case 0: return [4 /*yield*/, handleRequest(req, res)];
45
- case 1: return [2 /*return*/, _a.sent()];
48
+ var __filename, __dirname, url, host, appPath, err_1, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler_1, fetchRequest, context, status_1, redirect, helmetContext, router, rendered, html, e_1;
49
+ return __generator(this, function (_b) {
50
+ switch (_b.label) {
51
+ case 0:
52
+ __filename = fileURLToPath(import.meta.url);
53
+ __dirname = dirname(__filename);
54
+ console.log({ __dirname: __dirname });
55
+ _b.label = 1;
56
+ case 1:
57
+ _b.trys.push([1, 9, , 10]);
58
+ url = req.url;
59
+ host = req.headers.host;
60
+ appPath = join(__dirname, "..");
61
+ if (!(url === "/robots.txt")) return [3 /*break*/, 5];
62
+ _b.label = 2;
63
+ case 2:
64
+ _b.trys.push([2, 4, , 5]);
65
+ return [4 /*yield*/, fs.access(path.resolve(join(appPath, "dist/client/robots.txt")))];
66
+ case 3:
67
+ _b.sent();
68
+ return [2 /*return*/, res.send(path.resolve(join(appPath, "dist/client/robots.txt")))];
69
+ case 4:
70
+ err_1 = _b.sent();
71
+ return [2 /*return*/, res.send("\n user-agent: *\n disallow: /downloads/\n disallow: /private/\n allow: /\n \n user-agent: magicsearchbot\n disallow: /uploads/\n ")];
72
+ case 5:
73
+ // ! Sitemap Fix
74
+ if (url === "/sitemap.xml") {
75
+ return [2 /*return*/, res.send(path.resolve(join(appPath, "dist/client/sitemap.xml")))];
76
+ }
77
+ // ! Manifest Fix
78
+ if (url === "/manifest.json") {
79
+ return [2 /*return*/, res.send(path.resolve(join(appPath, "dist/client/manifest.json")))];
80
+ }
81
+ templateHtml = "";
82
+ serverFilePath = join(appPath, "dist/server/entry-server.js");
83
+ bootstrapDirPath = join(appPath, "dist/client/assets");
84
+ return [4 /*yield*/, import(serverFilePath)];
85
+ case 6:
86
+ entry = _b.sent();
87
+ bootstrap = "/assets/" +
88
+ fsSync
89
+ .readdirSync(bootstrapDirPath)
90
+ .filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".js"); })[0];
91
+ styles = "/assets/" +
92
+ fsSync
93
+ .readdirSync(join(appPath, "dist/client/assets"))
94
+ .filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".css"); })[0];
95
+ render = entry.render, staticRoutes = entry.staticRoutes, loadTemplateHtml = entry.loadTemplateHtml;
96
+ handler_1 = createStaticHandler(staticRoutes);
97
+ fetchRequest = createFetchRequest(req, host);
98
+ return [4 /*yield*/, handler_1.query(fetchRequest)];
99
+ case 7:
100
+ context = _b.sent();
101
+ status_1 = context.status;
102
+ if (status_1 === 302) {
103
+ redirect = context.headers.get("Location");
104
+ if (redirect)
105
+ return [2 /*return*/, res.redirect(redirect)];
106
+ }
107
+ helmetContext = {};
108
+ router = createStaticRouter(handler_1.dataRoutes, context);
109
+ return [4 /*yield*/, render(router, context, helmetContext)];
110
+ case 8:
111
+ rendered = _b.sent();
112
+ // Load template html
113
+ if (!templateHtml) {
114
+ templateHtml = loadTemplateHtml(helmetContext, bootstrap, styles);
115
+ }
116
+ html = templateHtml.replace("rasengan-body-app", (_a = rendered.html) !== null && _a !== void 0 ? _a : "");
117
+ // Send the rendered html page
118
+ return [2 /*return*/, res
119
+ .status(200)
120
+ .setHeader("Content-Type", "text/html")
121
+ .setHeader("Cache-Control", "max-age=31536000")
122
+ .end(html)];
123
+ case 9:
124
+ e_1 = _b.sent();
125
+ console.log(e_1.stack);
126
+ res.status(500).end(e_1.stack);
127
+ return [3 /*break*/, 10];
128
+ case 10: return [2 /*return*/];
46
129
  }
47
130
  });
48
131
  });
@@ -41,26 +41,23 @@ import { createStaticHandler, createStaticRouter, } from "react-router-dom/serve
41
41
  // @ts-ignore
42
42
  import { createFetchRequest } from "rasengan";
43
43
  import { fileURLToPath } from "node:url";
44
- var __customDirname = "";
45
- if (__dirname === undefined) {
46
- var __filename_1 = fileURLToPath(import.meta.url);
47
- var __customDirname_1 = dirname(__filename_1);
48
- }
49
44
  // Create server for production only
50
45
  export default function handleRequest(req, res) {
51
46
  var _a;
52
47
  return __awaiter(this, void 0, void 0, function () {
53
- var url, host, appPath, err_1, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler, fetchRequest, context, status_1, redirect, helmetContext, router, rendered, html, e_1;
48
+ var __filename, __dirname, url, host, appPath, err_1, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler, fetchRequest, context, status_1, redirect, helmetContext, router, rendered, html, e_1;
54
49
  return __generator(this, function (_b) {
55
50
  switch (_b.label) {
56
51
  case 0:
52
+ __filename = fileURLToPath(import.meta.url);
53
+ __dirname = dirname(__filename);
57
54
  console.log({
58
55
  cwd: process.cwd(),
59
- __customDirname: __customDirname
56
+ __dirname: __dirname
60
57
  });
61
58
  // read dir
62
59
  console.log({
63
- readdir: fsSync.readdirSync(__customDirname),
60
+ readdir: fsSync.readdirSync(__dirname),
64
61
  readdir2: fsSync.readdirSync(join(process.cwd(), "..")),
65
62
  readdir3: fsSync.readdirSync(join(process.cwd(), "..", "..")),
66
63
  });
@@ -69,7 +66,7 @@ export default function handleRequest(req, res) {
69
66
  _b.trys.push([1, 9, , 10]);
70
67
  url = req.url;
71
68
  host = req.headers.host;
72
- appPath = join(__dirname || __customDirname, "..");
69
+ appPath = join(__dirname, "..");
73
70
  if (!(url === "/robots.txt")) return [3 /*break*/, 5];
74
71
  _b.label = 2;
75
72
  case 2:
@@ -1,2 +1,2 @@
1
- declare const _default: (req: Request, context: Context) => Promise<any>;
1
+ declare const _default: (req: Request, context: Context) => Promise<Response>;
2
2
  export default _default;
@@ -1,2 +1,2 @@
1
1
  import type { VercelRequest, VercelResponse } from "@vercel/node";
2
- export default function handler(req: VercelRequest, res: VercelResponse): Promise<any>;
2
+ export default function handler(req: VercelRequest, res: VercelResponse): Promise<VercelResponse>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rasengan",
3
3
  "private": false,
4
- "version": "1.0.0-beta.21",
4
+ "version": "1.0.0-beta.23",
5
5
  "description": "The modern frontend framework for React",
6
6
  "type": "module",
7
7
  "main": "lib/esm/index.js",