bun-dev-server 0.0.8 → 0.1.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/bunClientHmr.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { type BunDevServerSocketConfig } from "./bunServeConfig";
2
2
 
3
3
  function hotReload() {
4
- if (window.BUN_HMR_INITED) {
4
+ if ((window as any).BUN_HMR_INITED) {
5
5
  return;
6
6
  }
7
- window.BUN_HMR_INITED = true;
7
+ (window as any).BUN_HMR_INITED = true;
8
8
  const hmrSock = new WebSocket("[REPLACE_ENDPOINT]");
9
9
  hmrSock.addEventListener("error", (err) => {
10
10
  console.error("HMR ERROR", err);
package/bunTSWatcher.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { $, type Server, type Subprocess, resolve } from "bun";
2
2
  export async function startTSWatcher(server: Server, watchDir: URL) {
3
- let dstcwd: string | undefined;
3
+ let dstcwd = process.cwd();
4
4
  if (watchDir) {
5
5
  dstcwd = process.platform === "win32" ? watchDir.pathname.substring(1) : watchDir.pathname;
6
6
  }
@@ -10,7 +10,7 @@ export async function startTSWatcher(server: Server, watchDir: URL) {
10
10
  //const tsc = await $`bun run ${tscResolved} --noEmit --watch ${dstcwd}/*.ts`.quiet().arrayBuffer();
11
11
  let tsc: Subprocess | undefined;
12
12
  try {
13
- tsc = Bun.spawn(["tsc", "--watch", "--project", `${import.meta.dir}/tsconfig.json`], { stdout: "pipe", stderr: "pipe" });
13
+ tsc = Bun.spawn(["tsc", "--watch", "--project", `${process.cwd()}/tsconfig.json`], { stdout: "pipe", stderr: "pipe", cwd: dstcwd });
14
14
  } catch (e) {
15
15
  console.error("TSC not found have you installed it globally?");
16
16
  return;
package/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- /// <reference path="./@types/serve.ts" />
1
+ /// <reference path="./@types/fileTypes.d.ts" />
2
2
  import { render } from "ejs";
3
3
  import Bun, { $, ShellError } from "bun";
4
4
  import serveTemplate from "./serveOutputTemplate.ejs" with { type: "text" };
@@ -52,12 +52,12 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
52
52
  async fetch(req, server) {
53
53
  if (req.method === "OPTIONS") {
54
54
  const response = new Response("", { status: 200 });
55
- augumentHeaders(response);
55
+ augumentHeaders(req, response);
56
56
  return response;
57
57
  }
58
58
  if (req.url.toLowerCase().endsWith("/favicon.ico")) {
59
59
  const response = new Response("", { status: 404 });
60
- augumentHeaders(response);
60
+ augumentHeaders(req, response);
61
61
  return response;
62
62
  }
63
63
  if (req.url.toLowerCase().endsWith(finalConfig.websocketPath)) {
@@ -75,17 +75,34 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
75
75
  } catch (e) {
76
76
  if ((e as ErrnoException).code === "EISDIR") {
77
77
  isDirectory = true;
78
- } else {
78
+ }
79
+ else {
79
80
  throw e;
80
81
  }
81
82
  }
83
+ } else {
84
+ const response = new Response("", { status: 404 });
85
+ augumentHeaders(req, response);
86
+ return response;
82
87
  }
83
88
 
84
89
  if (!isDirectory) {
85
- const fl = Bun.file(dst + requestPath);
86
- const response = new Response(fl);
87
- augumentHeaders(response);
88
- return response;
90
+ try {
91
+ const fl = Bun.file(dst + requestPath);
92
+ const response = new Response(fl);
93
+ augumentHeaders(req, response);
94
+ return response;
95
+ }
96
+ catch (e) {
97
+ if ((e as ErrnoException).code === "ENOENT") {
98
+ const response = new Response("", { status: 404 });
99
+ augumentHeaders(req, response);
100
+ return response;
101
+ }
102
+ else {
103
+ throw e;
104
+ }
105
+ }
89
106
  }
90
107
  try {
91
108
  const allEntries = await readdir(dst + requestPath, {
@@ -109,11 +126,11 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
109
126
  });
110
127
  const rnd = render(finalConfig.serveOutputEjs, { dirs, files });
111
128
  const response = new Response(rnd, { headers: { "Content-Type": "text/html" } });
112
- augumentHeaders(response);
129
+ augumentHeaders(req, response);
113
130
  return response;
114
131
  } catch {
115
132
  const response = new Response("Not Found", { status: 404 });
116
- augumentHeaders(response);
133
+ augumentHeaders(req, response);
117
134
  return response;
118
135
  }
119
136
  },
@@ -158,7 +175,7 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
158
175
  if (finalConfig.writeManifest) {
159
176
  writeManifest(output, dst, finalConfig.manifestName);
160
177
  }
161
- if(finalConfig.reloadOnChange) {
178
+ if (finalConfig.reloadOnChange) {
162
179
  bunServer.publish("message", JSON.stringify({ type: "reload" }));
163
180
  }
164
181
 
@@ -194,9 +211,10 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
194
211
 
195
212
  }
196
213
 
197
- function augumentHeaders(response: Response) {
198
- response.headers.set("Access-Control-Allow-Origin", "*");
214
+ function augumentHeaders(request: Request, response: Response) {
215
+ response.headers.set("Access-Control-Allow-Origin", request.headers.get("origin") ?? "*");
199
216
  response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
217
+ response.headers.set("Access-Control-Allow-Credentials", "true");
200
218
  }
201
219
 
202
220
  async function cleanDirectory(dst: string) {
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "type": "git",
7
7
  "url": "https://github.com/SPWizard01/bun-dev-server"
8
8
  },
9
- "version": "0.0.8",
9
+ "version": "0.1.0",
10
10
  "module": "index.ts",
11
11
  "type": "module",
12
12
  "license": "MIT",
@@ -1,6 +0,0 @@
1
- declare global {
2
- interface Window {
3
- BUN_HMR_INITED: boolean;
4
- }
5
- }
6
- export {};
File without changes