@dudousxd/nestjs-catalog-dashboard 0.34.1 → 0.35.0

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 CHANGED
@@ -79,3 +79,10 @@ pulls it in.
79
79
 
80
80
  When the host authenticates, the SPA skips its own sign-in entirely — a console embedded in an
81
81
  application that already knows who you are has no business asking again.
82
+
83
+ ## How the console is served
84
+
85
+ `index.html` is rendered once (asset URLs rewritten to `path`, the API base filled in) and kept in
86
+ memory. Each request checks the file's mtime and size, and renders it again when they change, so a
87
+ rebuilt bundle is picked up without a restart. It is always sent `Cache-Control: no-store`. Files
88
+ under `assets/` are streamed from disk with a `Content-Length`; a path that is not a file is a 404.
@@ -761,6 +761,12 @@ var CatalogUiController = class {
761
761
  apiBasePath;
762
762
  auth;
763
763
  dir = spaDir();
764
+ /**
765
+ * The rewritten index.html, with the file version it was rendered from. A
766
+ * rebuilt bundle changes the file's mtime or size, and the next request
767
+ * renders it again instead of pointing at hashed assets that are gone.
768
+ */
769
+ rendered;
764
770
  constructor(basePath, apiBasePath, auth = null) {
765
771
  this.basePath = basePath;
766
772
  this.apiBasePath = apiBasePath;
@@ -770,10 +776,24 @@ var CatalogUiController = class {
770
776
  // classic "stuck loading after a deploy"). The hashed assets below are immutable.
771
777
  index() {
772
778
  const indexPath = (0, import_node_path.join)(this.dir, "index.html");
773
- if (!(0, import_node_fs.existsSync)(indexPath)) {
779
+ const stat = (0, import_node_fs.statSync)(indexPath, {
780
+ throwIfNoEntry: false
781
+ });
782
+ if (!stat) {
774
783
  throw new import_common3.NotFoundException("Dashboard is not built. Run the package build.");
775
784
  }
776
- const html = (0, import_node_fs.readFileSync)(indexPath, "utf8").replaceAll(`="${BUILD_BASE}/`, `="${this.basePath}/`);
785
+ const cached = this.rendered;
786
+ if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) return cached.html;
787
+ const html = this.render((0, import_node_fs.readFileSync)(indexPath, "utf8"));
788
+ this.rendered = {
789
+ mtimeMs: stat.mtimeMs,
790
+ size: stat.size,
791
+ html
792
+ };
793
+ return html;
794
+ }
795
+ render(source) {
796
+ const html = source.replaceAll(`="${BUILD_BASE}/`, `="${this.basePath}/`);
777
797
  const hostAuth = this.auth ? "true" : "false";
778
798
  const inject = `<script>window.__CATALOG_BASE__='${this.basePath}';window.__CATALOG_API__='${this.apiBasePath}';window.__CATALOG_HOST_AUTH__=${hostAuth};</script>`;
779
799
  return html.includes("</head>") ? html.replace("</head>", `${inject}</head>`) : inject + html;
@@ -783,12 +803,15 @@ var CatalogUiController = class {
783
803
  if (safe !== file) throw new import_common3.NotFoundException();
784
804
  const root = (0, import_node_path.resolve)(this.dir, "assets");
785
805
  const assetPath = (0, import_node_path.resolve)(root, safe);
786
- if (!assetPath.startsWith(root + import_node_path.sep) || !(0, import_node_fs.existsSync)(assetPath)) {
787
- throw new import_common3.NotFoundException();
788
- }
806
+ if (!assetPath.startsWith(root + import_node_path.sep)) throw new import_common3.NotFoundException();
807
+ const stat = (0, import_node_fs.statSync)(assetPath, {
808
+ throwIfNoEntry: false
809
+ });
810
+ if (!stat?.isFile()) throw new import_common3.NotFoundException();
789
811
  const type = CONTENT_TYPES[(0, import_node_path.extname)(safe)] ?? "application/octet-stream";
790
- return new import_common3.StreamableFile((0, import_node_fs.readFileSync)(assetPath), {
791
- type
812
+ return new import_common3.StreamableFile((0, import_node_fs.createReadStream)(assetPath), {
813
+ type,
814
+ length: stat.size
792
815
  });
793
816
  }
794
817
  };