@jxsuite/server 0.28.2 → 0.28.4

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/src/watch.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /** Watch.js — File watcher + SSE live reload */
2
2
 
3
- import chokidar from "chokidar";
3
+ import { watch as chokidarWatch } from "chokidar";
4
4
  import { relative } from "node:path";
5
5
  import { rebuild } from "./build.ts";
6
6
  import type { BuildEntry } from "./types.ts";
@@ -48,7 +48,7 @@ export const SSE_SCRIPT = `\n<script>new EventSource('/__reload').onmessage=()=>
48
48
  /** @param {string} html */
49
49
  export function injectSSE(html: string) {
50
50
  return html.includes("</body>")
51
- ? html.replace("</body>", SSE_SCRIPT + "\n</body>")
51
+ ? html.replace("</body>", `${SSE_SCRIPT}\n</body>`)
52
52
  : html + SSE_SCRIPT;
53
53
  }
54
54
 
@@ -77,17 +77,24 @@ export function createWatcher(
77
77
  const debounceMs = opts.debounce ?? 50;
78
78
  const reloadOnAnyChange = opts.reloadOnAnyChange ?? false;
79
79
 
80
- const clients: Set<(msg: string) => void> = new Set();
80
+ const clients = new Set<(msg: string) => void>();
81
81
  const encoder = new TextEncoder();
82
82
 
83
83
  function broadcast() {
84
- for (const send of clients) send("data: reload\n\n");
84
+ for (const send of clients) {
85
+ send("data: reload\n\n");
86
+ }
85
87
  }
86
88
 
87
89
  function handleSSE() {
88
90
  /** @type {((msg: string) => void) | undefined} */
89
91
  let send: ((msg: string) => void) | undefined;
90
92
  const stream = new ReadableStream({
93
+ cancel() {
94
+ if (send) {
95
+ clients.delete(send);
96
+ }
97
+ },
91
98
  start(c) {
92
99
  send = (msg: string) => {
93
100
  try {
@@ -103,45 +110,48 @@ export function createWatcher(
103
110
  }
104
111
  }, 15_000);
105
112
  },
106
- cancel() {
107
- if (send) clients.delete(send);
108
- },
109
113
  });
110
114
  return new Response(stream, {
111
115
  headers: {
112
- "Content-Type": "text/event-stream",
113
116
  "Cache-Control": "no-cache",
114
117
  Connection: "keep-alive",
118
+ "Content-Type": "text/event-stream",
115
119
  },
116
120
  });
117
121
  }
118
122
 
119
123
  let timer: ReturnType<typeof setTimeout> | null = null;
120
- const watcher = chokidar.watch(root, {
121
- ignored: (watchedPath) => shouldIgnore(watchedPath, ignore),
122
- ignoreInitial: true,
123
- ignorePermissionErrors: true,
124
+ const watcher = chokidarWatch(root, {
124
125
  awaitWriteFinish: {
125
- stabilityThreshold: debounceMs,
126
126
  pollInterval: 10,
127
+ stabilityThreshold: debounceMs,
127
128
  },
129
+ ignoreInitial: true,
130
+ ignorePermissionErrors: true,
131
+ ignored: (watchedPath) => shouldIgnore(watchedPath, ignore),
128
132
  });
129
133
 
130
134
  watcher.on("all", (_, changedPath) => {
131
135
  const filename = relative(root, changedPath);
132
- if (!filename || filename.startsWith("..")) return;
136
+ if (!filename || filename.startsWith("..")) {
137
+ return;
138
+ }
133
139
  clearTimeout(timer ?? undefined);
134
140
  timer = setTimeout(async () => {
135
141
  if (builds.length > 0) {
136
142
  const result = await rebuild(builds, filename);
137
- if (!result.success) return;
143
+ if (!result.success) {
144
+ return;
145
+ }
138
146
  if (result.rebuilt.length > 0) {
139
147
  broadcast();
140
148
  return;
141
149
  }
142
150
  }
143
151
  console.log(`Changed → ${filename}`);
144
- if (reloadOnAnyChange) broadcast();
152
+ if (reloadOnAnyChange) {
153
+ broadcast();
154
+ }
145
155
  }, debounceMs);
146
156
  });
147
157