zastro-websockets-node 9.5.2-4 → 9.5.3-2

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/dist/serve-app.js CHANGED
@@ -1,5 +1,28 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { createReadStream } from "node:fs";
3
+ import path from "node:path";
4
+ import { Readable } from "node:stream";
2
5
  import { NodeApp } from "astro/app/node";
6
+ import { resolveClientDir } from "./shared.js";
7
+ async function readErrorPageFromDisk(client, status) {
8
+ const filePaths = [`${status}.html`, `${status}/index.html`];
9
+ for (const filePath of filePaths) {
10
+ const fullPath = path.join(client, filePath);
11
+ try {
12
+ const stream = createReadStream(fullPath);
13
+ await new Promise((resolve, reject) => {
14
+ stream.once("open", () => resolve());
15
+ stream.once("error", reject);
16
+ });
17
+ const webStream = Readable.toWeb(stream);
18
+ return new Response(webStream, {
19
+ headers: { "Content-Type": "text/html; charset=utf-8" }
20
+ });
21
+ } catch {
22
+ }
23
+ }
24
+ return void 0;
25
+ }
3
26
  function createAppHandler(app, options) {
4
27
  const als = new AsyncLocalStorage();
5
28
  const logger = app.getAdapterLogger();
@@ -8,13 +31,25 @@ function createAppHandler(app, options) {
8
31
  logger.error(`Unhandled rejection while rendering ${requestUrl}`);
9
32
  console.error(reason);
10
33
  });
11
- const originUrl = options.experimentalErrorPageHost ? new URL(options.experimentalErrorPageHost) : void 0;
12
- const prerenderedErrorPageFetch = originUrl ? (url) => {
13
- const errorPageUrl = new URL(url);
14
- errorPageUrl.protocol = originUrl.protocol;
15
- errorPageUrl.host = originUrl.host;
16
- return fetch(errorPageUrl);
17
- } : void 0;
34
+ const client = resolveClientDir(options);
35
+ const prerenderedErrorPageFetch = async (url) => {
36
+ if (url.includes("/404")) {
37
+ const response = await readErrorPageFromDisk(client, 404);
38
+ if (response) return response;
39
+ }
40
+ if (url.includes("/500")) {
41
+ const response = await readErrorPageFromDisk(client, 500);
42
+ if (response) return response;
43
+ }
44
+ if (options.experimentalErrorPageHost) {
45
+ const originUrl = new URL(options.experimentalErrorPageHost);
46
+ const errorPageUrl = new URL(url);
47
+ errorPageUrl.protocol = originUrl.protocol;
48
+ errorPageUrl.host = originUrl.host;
49
+ return fetch(errorPageUrl);
50
+ }
51
+ return new Response(null, { status: 404 });
52
+ };
18
53
  return async (req, res, next, locals = {}) => {
19
54
  let request;
20
55
  try {
@@ -1,8 +1,8 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import url from "node:url";
4
3
  import { hasFileExtension, isInternalPath } from "@astrojs/internal-helpers/path";
5
4
  import send from "send";
5
+ import { resolveClientDir } from "./shared.js";
6
6
  function createStaticHandler(app, options) {
7
7
  const client = resolveClientDir(options);
8
8
  return (req, res, ssr) => {
@@ -90,26 +90,9 @@ function createStaticHandler(app, options) {
90
90
  }
91
91
  };
92
92
  }
93
- function resolveClientDir(options) {
94
- const clientURLRaw = new URL(options.client);
95
- const serverURLRaw = new URL(options.server);
96
- const rel = path.relative(url.fileURLToPath(serverURLRaw), url.fileURLToPath(clientURLRaw));
97
- const serverFolder = path.basename(options.server);
98
- let serverEntryFolderURL = path.dirname(import.meta.url);
99
- while (!serverEntryFolderURL.endsWith(serverFolder)) {
100
- serverEntryFolderURL = path.dirname(serverEntryFolderURL);
101
- }
102
- const serverEntryURL = serverEntryFolderURL + "/entry.mjs";
103
- const clientURL = new URL(appendForwardSlash(rel), serverEntryURL);
104
- const client = url.fileURLToPath(clientURL);
105
- return client;
106
- }
107
93
  function prependForwardSlash(pth) {
108
94
  return pth.startsWith("/") ? pth : "/" + pth;
109
95
  }
110
- function appendForwardSlash(pth) {
111
- return pth.endsWith("/") ? pth : pth + "/";
112
- }
113
96
  export {
114
97
  createStaticHandler
115
98
  };
package/dist/shared.d.ts CHANGED
@@ -1 +1,9 @@
1
+ import type { Options } from './types.js';
1
2
  export declare const STATIC_HEADERS_FILE = "_experimentalHeaders.json";
3
+ /**
4
+ * Resolves the client directory path at runtime.
5
+ *
6
+ * At build time, we know the relative path between server and client directories.
7
+ * At runtime, we need to find the actual location based on where the server entry is running.
8
+ */
9
+ export declare function resolveClientDir(options: Options): string;
package/dist/shared.js CHANGED
@@ -1,4 +1,21 @@
1
+ import path from "node:path";
2
+ import url from "node:url";
3
+ import { appendForwardSlash } from "@astrojs/internal-helpers/path";
1
4
  const STATIC_HEADERS_FILE = "_experimentalHeaders.json";
5
+ function resolveClientDir(options) {
6
+ const clientURLRaw = new URL(options.client);
7
+ const serverURLRaw = new URL(options.server);
8
+ const rel = path.relative(url.fileURLToPath(serverURLRaw), url.fileURLToPath(clientURLRaw));
9
+ const serverFolder = path.basename(options.server);
10
+ let serverEntryFolderURL = path.dirname(import.meta.url);
11
+ while (!serverEntryFolderURL.endsWith(serverFolder)) {
12
+ serverEntryFolderURL = path.dirname(serverEntryFolderURL);
13
+ }
14
+ const serverEntryURL = serverEntryFolderURL + "/entry.mjs";
15
+ const clientURL = new URL(appendForwardSlash(rel), serverEntryURL);
16
+ return url.fileURLToPath(clientURL);
17
+ }
2
18
  export {
3
- STATIC_HEADERS_FILE
19
+ STATIC_HEADERS_FILE,
20
+ resolveClientDir
4
21
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zastro-websockets-node",
3
3
  "description": "Deploy your site to a Node.js server with WebSocket support",
4
- "version": "9.5.2-4",
4
+ "version": "9.5.3-2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "Zach Handley <zach@zachhandley.com>",