jq79 0.5.9 → 0.5.11

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/cli.js CHANGED
@@ -4,11 +4,15 @@
4
4
  import { createServer } from "http";
5
5
  import { readFile, realpath, stat } from "fs/promises";
6
6
  import { watch } from "fs";
7
- import { basename, extname, join, relative, resolve, sep } from "path";
7
+ import { basename, dirname, extname, isAbsolute, join, matchesGlob, relative, resolve, sep } from "path";
8
8
  var CONTENT_TYPES = {
9
9
  ".html": "text/html; charset=utf-8",
10
10
  ".js": "text/javascript; charset=utf-8",
11
11
  ".mjs": "text/javascript; charset=utf-8",
12
+ // the streaming WebAssembly APIs reject every other type, including the
13
+ // default below - a module served as octet-stream still runs, but it is
14
+ // buffered whole instead of compiling as it downloads
15
+ ".wasm": "application/wasm",
12
16
  ".json": "application/json; charset=utf-8",
13
17
  ".css": "text/css; charset=utf-8",
14
18
  ".svg": "image/svg+xml",
@@ -54,6 +58,10 @@ var injectClient = (html) => {
54
58
  var devServer = async (options2 = {}) => {
55
59
  const root = await realpath(resolve(options2.rootDir ?? "."));
56
60
  const host = options2.host ?? "localhost";
61
+ const headers = Object.entries(options2.headers ?? {}).filter(
62
+ ([name]) => !["content-type", "content-length"].includes(name.toLowerCase())
63
+ );
64
+ const hooks = options2.beforeResponse ?? [];
57
65
  const clients = /* @__PURE__ */ new Set();
58
66
  const send = (event, data) => {
59
67
  const frame = `event: ${event}
@@ -62,22 +70,28 @@ data: ${JSON.stringify(data)}
62
70
  `;
63
71
  clients.forEach((client) => client.write(frame));
64
72
  };
73
+ const head = (res, status, own = {}) => {
74
+ res.removeHeader("content-length");
75
+ const write = { ...own };
76
+ if (res.hasHeader("content-type")) delete write["content-type"];
77
+ return res.writeHead(status, write);
78
+ };
65
79
  const serveStatic = async (req, res, pathname) => {
66
80
  let file;
67
81
  try {
68
82
  file = resolve(join(root, decodeURIComponent(pathname)));
69
83
  } catch {
70
- res.writeHead(400).end("bad request");
84
+ head(res, 400).end("bad request");
71
85
  return;
72
86
  }
73
87
  if (file !== root && !file.startsWith(root + sep)) {
74
- res.writeHead(403).end("forbidden");
88
+ head(res, 403).end("forbidden");
75
89
  return;
76
90
  }
77
91
  try {
78
92
  if ((await stat(file)).isDirectory()) {
79
93
  if (!pathname.endsWith("/")) {
80
- res.writeHead(301, { location: pathname + "/" }).end();
94
+ head(res, 301, { location: pathname + "/" }).end();
81
95
  return;
82
96
  }
83
97
  file = join(file, "index.html");
@@ -86,62 +100,135 @@ data: ${JSON.stringify(data)}
86
100
  const type = CONTENT_TYPES[extname(file).toLowerCase()] ?? "application/octet-stream";
87
101
  const html = type.startsWith("text/html") && isDocument(req);
88
102
  const payload = html ? Buffer.from(injectClient(body.toString("utf8"))) : body;
89
- res.writeHead(200, {
90
- "content-type": type,
91
- "content-length": payload.byteLength,
92
- "cache-control": "no-store"
93
- // the file on disk is always the truth here
94
- });
103
+ head(res, 200, { "content-type": type, "content-length": payload.byteLength });
95
104
  res.end(payload);
96
105
  } catch {
97
- res.writeHead(404, { "content-type": "text/plain; charset=utf-8" }).end("not found");
106
+ head(res, 404, { "content-type": "text/plain; charset=utf-8" }).end("not found");
98
107
  }
99
108
  };
100
- const server2 = createServer((req, res) => {
109
+ const handle = async (req, res) => {
110
+ res.setHeader("cache-control", "no-store");
111
+ for (const [name, value] of headers) res.setHeader(name, value);
112
+ for (const hook of hooks) await hook(req, res);
113
+ if (res.headersSent || res.writableEnded) return;
101
114
  const pathname = (req.url ?? "/").split(/[?#]/)[0];
102
115
  if (pathname === CLIENT_URL) {
103
- res.writeHead(200, { "content-type": "text/javascript; charset=utf-8", "cache-control": "no-store" });
116
+ head(res, 200, { "content-type": "text/javascript; charset=utf-8" });
104
117
  res.end(CLIENT);
105
118
  return;
106
119
  }
107
120
  if (pathname === EVENTS_URL) {
108
- res.writeHead(200, {
109
- "content-type": "text/event-stream",
110
- "cache-control": "no-store",
111
- connection: "keep-alive"
112
- });
121
+ head(res, 200, { "content-type": "text/event-stream", connection: "keep-alive" });
113
122
  res.write(": jq79\n\n");
114
123
  clients.add(res);
115
124
  req.on("close", () => clients.delete(res));
116
125
  return;
117
126
  }
118
- void serveStatic(req, res, pathname);
127
+ await serveStatic(req, res, pathname);
128
+ };
129
+ const server2 = createServer((req, res) => {
130
+ void handle(req, res).catch((error) => {
131
+ console.error(`jq79 dev: failed to respond to ${req.url}
132
+ `, error);
133
+ if (!res.headersSent) {
134
+ res.removeHeader("content-length");
135
+ res.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
136
+ }
137
+ if (!res.writableEnded) res.end("internal error");
138
+ });
119
139
  });
120
140
  const ignored = (rel) => rel.split(sep).some((part) => part.startsWith(".") || part === "node_modules");
121
141
  const pending = /* @__PURE__ */ new Map();
122
- const changed = async (rel) => {
123
- const file = join(root, rel);
142
+ const cwd = process.cwd();
143
+ const entries = (options2.watch ?? []).map((entry) => ({
144
+ patterns: [entry.pattern].flat(),
145
+ fn: entry.fn,
146
+ batch: /* @__PURE__ */ new Set(),
147
+ running: false
148
+ }));
149
+ const claims = (entry, file) => entry.patterns.some(
150
+ (pattern) => matchesGlob(isAbsolute(pattern) ? posix(file) : posix(relative(cwd, file)), pattern)
151
+ );
152
+ const collect = (entry, file) => {
153
+ entry.batch.add(file);
154
+ clearTimeout(entry.timer);
155
+ entry.timer = setTimeout(() => void fire(entry), 30);
156
+ };
157
+ const fire = async (entry) => {
158
+ if (entry.running || !entry.batch.size) return;
159
+ const files = [...entry.batch];
160
+ entry.batch.clear();
161
+ entry.running = true;
162
+ try {
163
+ await entry.fn?.(files);
164
+ } catch (error) {
165
+ console.error(`jq79 dev: watch handler for ${entry.patterns.join(", ")} failed
166
+ `, error);
167
+ } finally {
168
+ entry.running = false;
169
+ if (entry.batch.size) void fire(entry);
170
+ }
171
+ };
172
+ const changed = async (file, dir) => {
173
+ const rel = relative(root, file);
174
+ const served = !!rel && !rel.startsWith("..");
124
175
  let src = null;
125
176
  try {
126
177
  if ((await stat(file)).isDirectory()) return;
127
- if (rel.endsWith(".html")) src = await readFile(file, "utf8");
178
+ if (served && file.endsWith(".html")) src = await readFile(file, "utf8");
128
179
  } catch {
129
- if (rel === basename(root)) return;
180
+ if (file === join(dir, basename(dir))) return;
181
+ }
182
+ const claimed = entries.filter((entry) => claims(entry, file));
183
+ claimed.forEach((entry) => entry.fn && collect(entry, file));
184
+ if (served) {
185
+ const url = "/" + posix(rel);
186
+ if (src === null) send("reload", { url });
187
+ else send("update", { url, src });
188
+ return;
130
189
  }
131
- const url = "/" + posix(rel);
132
- if (src === null) send("reload", { url });
133
- else send("update", { url, src });
190
+ if (claimed.some((entry) => !entry.fn)) send("reload", { url: posix(rel) });
134
191
  };
135
- const watcher = watch(root, { recursive: true }, (_event, filename) => {
136
- if (!filename) return;
137
- const rel = relative(root, resolve(root, filename.toString()));
138
- if (!rel || rel.startsWith("..") || ignored(rel)) return;
139
- clearTimeout(pending.get(rel));
140
- pending.set(rel, setTimeout(() => {
141
- pending.delete(rel);
142
- void changed(rel);
192
+ const queue = (file, dir) => {
193
+ clearTimeout(pending.get(file));
194
+ pending.set(file, setTimeout(() => {
195
+ pending.delete(file);
196
+ void changed(file, dir);
143
197
  }, 30));
144
- });
198
+ };
199
+ const watchers = [];
200
+ const watchTree = (dir, only) => {
201
+ watchers.push(watch(dir, { recursive: !only }, (_event, filename) => {
202
+ if (!filename) return;
203
+ const file = resolve(dir, filename.toString());
204
+ const rel = relative(dir, file);
205
+ if (!rel || rel.startsWith("..")) return;
206
+ if (only ? rel !== only : ignored(rel)) return;
207
+ queue(file, dir);
208
+ }));
209
+ };
210
+ const literalHead = (pattern) => {
211
+ const parts = pattern.split("/");
212
+ const magic = parts.findIndex((part) => /[*?[\]{}!]/.test(part));
213
+ return resolve(magic === -1 ? pattern : parts.slice(0, magic).join("/") || ".");
214
+ };
215
+ const extra = /* @__PURE__ */ new Map();
216
+ for (const entry of entries) {
217
+ for (const [i, pattern] of entry.patterns.entries()) {
218
+ const head2 = literalHead(pattern);
219
+ const info = await stat(head2).catch(() => null);
220
+ if (!info) throw new Error(`jq79 dev: nothing to watch at ${head2}`);
221
+ if (info.isDirectory() && head2 === resolve(pattern)) {
222
+ entry.patterns[i] = pattern.replace(/\/+$/, "") + "/**";
223
+ }
224
+ const real = await realpath(head2);
225
+ if (real === root || real.startsWith(root + sep)) continue;
226
+ const watched = info.isDirectory() ? { dir: real } : { dir: dirname(real), only: basename(real) };
227
+ extra.set(`${watched.dir}\0${watched.only ?? ""}`, watched);
228
+ }
229
+ }
230
+ watchTree(root);
231
+ extra.forEach(({ dir, only }) => watchTree(dir, only));
145
232
  const heartbeat = setInterval(() => clients.forEach((client) => client.write(": ping\n\n")), 3e4);
146
233
  heartbeat.unref();
147
234
  await new Promise((done, fail) => {
@@ -154,8 +241,9 @@ data: ${JSON.stringify(data)}
154
241
  port,
155
242
  close: () => new Promise((done) => {
156
243
  clearInterval(heartbeat);
157
- watcher.close();
244
+ watchers.forEach((watcher) => watcher.close());
158
245
  pending.forEach(clearTimeout);
246
+ entries.forEach((entry) => clearTimeout(entry.timer));
159
247
  clients.forEach((client) => client.end());
160
248
  clients.clear();
161
249
  server2.close(() => done());
@@ -172,6 +260,9 @@ usage:
172
260
  options:
173
261
  -p, --port <port> port to listen on (default: 4179)
174
262
  -H, --host <host> host to bind (default: localhost)
263
+ -w, --watch <glob> also watch this path or glob, reloading on a change
264
+ (repeatable; handlers need the js api)
265
+ --header <h> response header, "name: value" (repeatable)
175
266
  -h, --help show this message
176
267
  `;
177
268
  var args = process.argv.slice(2);
@@ -188,7 +279,23 @@ for (let i = 0; i < rest.length; i++) {
188
279
  const arg = rest[i];
189
280
  if (arg === "-p" || arg === "--port") options.port = Number(rest[++i]);
190
281
  else if (arg === "-H" || arg === "--host") options.host = rest[++i];
191
- else if (!arg.startsWith("-")) options.rootDir ??= arg;
282
+ else if (arg === "-w" || arg === "--watch") {
283
+ const pattern = rest[++i];
284
+ if (!pattern) {
285
+ console.error("--watch takes a path or glob");
286
+ process.exit(1);
287
+ }
288
+ ;
289
+ (options.watch ??= []).push({ pattern });
290
+ } else if (arg === "--header") {
291
+ const [name, ...value] = (rest[++i] ?? "").split(":");
292
+ if (!name || !value.length) {
293
+ console.error(`--header takes "name: value"`);
294
+ process.exit(1);
295
+ }
296
+ ;
297
+ (options.headers ??= {})[name.trim()] = value.join(":").trim();
298
+ } else if (!arg.startsWith("-")) options.rootDir ??= arg;
192
299
  else {
193
300
  console.error(`unknown option: ${arg}
194
301
  ${USAGE}`);
@@ -199,7 +306,10 @@ if (options.port !== void 0 && !Number.isInteger(options.port)) {
199
306
  console.error("--port takes a number");
200
307
  process.exit(1);
201
308
  }
202
- var server = await devServer(options);
309
+ var server = await devServer(options).catch((error) => {
310
+ console.error(error instanceof Error ? error.message : error);
311
+ process.exit(1);
312
+ });
203
313
  console.log(`jq79 dev \u2192 ${server.url}`);
204
314
  var stop = () => {
205
315
  void server.close().then(() => process.exit(0));
package/dist/dev.cjs CHANGED
@@ -31,6 +31,10 @@ var CONTENT_TYPES = {
31
31
  ".html": "text/html; charset=utf-8",
32
32
  ".js": "text/javascript; charset=utf-8",
33
33
  ".mjs": "text/javascript; charset=utf-8",
34
+ // the streaming WebAssembly APIs reject every other type, including the
35
+ // default below - a module served as octet-stream still runs, but it is
36
+ // buffered whole instead of compiling as it downloads
37
+ ".wasm": "application/wasm",
34
38
  ".json": "application/json; charset=utf-8",
35
39
  ".css": "text/css; charset=utf-8",
36
40
  ".svg": "image/svg+xml",
@@ -76,6 +80,10 @@ var injectClient = (html) => {
76
80
  var devServer = async (options = {}) => {
77
81
  const root = await (0, import_promises.realpath)((0, import_node_path.resolve)(options.rootDir ?? "."));
78
82
  const host = options.host ?? "localhost";
83
+ const headers = Object.entries(options.headers ?? {}).filter(
84
+ ([name]) => !["content-type", "content-length"].includes(name.toLowerCase())
85
+ );
86
+ const hooks = options.beforeResponse ?? [];
79
87
  const clients = /* @__PURE__ */ new Set();
80
88
  const send = (event, data) => {
81
89
  const frame = `event: ${event}
@@ -84,22 +92,28 @@ data: ${JSON.stringify(data)}
84
92
  `;
85
93
  clients.forEach((client) => client.write(frame));
86
94
  };
95
+ const head = (res, status, own = {}) => {
96
+ res.removeHeader("content-length");
97
+ const write = { ...own };
98
+ if (res.hasHeader("content-type")) delete write["content-type"];
99
+ return res.writeHead(status, write);
100
+ };
87
101
  const serveStatic = async (req, res, pathname) => {
88
102
  let file;
89
103
  try {
90
104
  file = (0, import_node_path.resolve)((0, import_node_path.join)(root, decodeURIComponent(pathname)));
91
105
  } catch {
92
- res.writeHead(400).end("bad request");
106
+ head(res, 400).end("bad request");
93
107
  return;
94
108
  }
95
109
  if (file !== root && !file.startsWith(root + import_node_path.sep)) {
96
- res.writeHead(403).end("forbidden");
110
+ head(res, 403).end("forbidden");
97
111
  return;
98
112
  }
99
113
  try {
100
114
  if ((await (0, import_promises.stat)(file)).isDirectory()) {
101
115
  if (!pathname.endsWith("/")) {
102
- res.writeHead(301, { location: pathname + "/" }).end();
116
+ head(res, 301, { location: pathname + "/" }).end();
103
117
  return;
104
118
  }
105
119
  file = (0, import_node_path.join)(file, "index.html");
@@ -108,62 +122,135 @@ data: ${JSON.stringify(data)}
108
122
  const type = CONTENT_TYPES[(0, import_node_path.extname)(file).toLowerCase()] ?? "application/octet-stream";
109
123
  const html = type.startsWith("text/html") && isDocument(req);
110
124
  const payload = html ? Buffer.from(injectClient(body.toString("utf8"))) : body;
111
- res.writeHead(200, {
112
- "content-type": type,
113
- "content-length": payload.byteLength,
114
- "cache-control": "no-store"
115
- // the file on disk is always the truth here
116
- });
125
+ head(res, 200, { "content-type": type, "content-length": payload.byteLength });
117
126
  res.end(payload);
118
127
  } catch {
119
- res.writeHead(404, { "content-type": "text/plain; charset=utf-8" }).end("not found");
128
+ head(res, 404, { "content-type": "text/plain; charset=utf-8" }).end("not found");
120
129
  }
121
130
  };
122
- const server = (0, import_node_http.createServer)((req, res) => {
131
+ const handle = async (req, res) => {
132
+ res.setHeader("cache-control", "no-store");
133
+ for (const [name, value] of headers) res.setHeader(name, value);
134
+ for (const hook of hooks) await hook(req, res);
135
+ if (res.headersSent || res.writableEnded) return;
123
136
  const pathname = (req.url ?? "/").split(/[?#]/)[0];
124
137
  if (pathname === CLIENT_URL) {
125
- res.writeHead(200, { "content-type": "text/javascript; charset=utf-8", "cache-control": "no-store" });
138
+ head(res, 200, { "content-type": "text/javascript; charset=utf-8" });
126
139
  res.end(CLIENT);
127
140
  return;
128
141
  }
129
142
  if (pathname === EVENTS_URL) {
130
- res.writeHead(200, {
131
- "content-type": "text/event-stream",
132
- "cache-control": "no-store",
133
- connection: "keep-alive"
134
- });
143
+ head(res, 200, { "content-type": "text/event-stream", connection: "keep-alive" });
135
144
  res.write(": jq79\n\n");
136
145
  clients.add(res);
137
146
  req.on("close", () => clients.delete(res));
138
147
  return;
139
148
  }
140
- void serveStatic(req, res, pathname);
149
+ await serveStatic(req, res, pathname);
150
+ };
151
+ const server = (0, import_node_http.createServer)((req, res) => {
152
+ void handle(req, res).catch((error) => {
153
+ console.error(`jq79 dev: failed to respond to ${req.url}
154
+ `, error);
155
+ if (!res.headersSent) {
156
+ res.removeHeader("content-length");
157
+ res.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
158
+ }
159
+ if (!res.writableEnded) res.end("internal error");
160
+ });
141
161
  });
142
162
  const ignored = (rel) => rel.split(import_node_path.sep).some((part) => part.startsWith(".") || part === "node_modules");
143
163
  const pending = /* @__PURE__ */ new Map();
144
- const changed = async (rel) => {
145
- const file = (0, import_node_path.join)(root, rel);
164
+ const cwd = process.cwd();
165
+ const entries = (options.watch ?? []).map((entry) => ({
166
+ patterns: [entry.pattern].flat(),
167
+ fn: entry.fn,
168
+ batch: /* @__PURE__ */ new Set(),
169
+ running: false
170
+ }));
171
+ const claims = (entry, file) => entry.patterns.some(
172
+ (pattern) => (0, import_node_path.matchesGlob)((0, import_node_path.isAbsolute)(pattern) ? posix(file) : posix((0, import_node_path.relative)(cwd, file)), pattern)
173
+ );
174
+ const collect = (entry, file) => {
175
+ entry.batch.add(file);
176
+ clearTimeout(entry.timer);
177
+ entry.timer = setTimeout(() => void fire(entry), 30);
178
+ };
179
+ const fire = async (entry) => {
180
+ if (entry.running || !entry.batch.size) return;
181
+ const files = [...entry.batch];
182
+ entry.batch.clear();
183
+ entry.running = true;
184
+ try {
185
+ await entry.fn?.(files);
186
+ } catch (error) {
187
+ console.error(`jq79 dev: watch handler for ${entry.patterns.join(", ")} failed
188
+ `, error);
189
+ } finally {
190
+ entry.running = false;
191
+ if (entry.batch.size) void fire(entry);
192
+ }
193
+ };
194
+ const changed = async (file, dir) => {
195
+ const rel = (0, import_node_path.relative)(root, file);
196
+ const served = !!rel && !rel.startsWith("..");
146
197
  let src = null;
147
198
  try {
148
199
  if ((await (0, import_promises.stat)(file)).isDirectory()) return;
149
- if (rel.endsWith(".html")) src = await (0, import_promises.readFile)(file, "utf8");
200
+ if (served && file.endsWith(".html")) src = await (0, import_promises.readFile)(file, "utf8");
150
201
  } catch {
151
- if (rel === (0, import_node_path.basename)(root)) return;
202
+ if (file === (0, import_node_path.join)(dir, (0, import_node_path.basename)(dir))) return;
152
203
  }
153
- const url = "/" + posix(rel);
154
- if (src === null) send("reload", { url });
155
- else send("update", { url, src });
204
+ const claimed = entries.filter((entry) => claims(entry, file));
205
+ claimed.forEach((entry) => entry.fn && collect(entry, file));
206
+ if (served) {
207
+ const url = "/" + posix(rel);
208
+ if (src === null) send("reload", { url });
209
+ else send("update", { url, src });
210
+ return;
211
+ }
212
+ if (claimed.some((entry) => !entry.fn)) send("reload", { url: posix(rel) });
156
213
  };
157
- const watcher = (0, import_node_fs.watch)(root, { recursive: true }, (_event, filename) => {
158
- if (!filename) return;
159
- const rel = (0, import_node_path.relative)(root, (0, import_node_path.resolve)(root, filename.toString()));
160
- if (!rel || rel.startsWith("..") || ignored(rel)) return;
161
- clearTimeout(pending.get(rel));
162
- pending.set(rel, setTimeout(() => {
163
- pending.delete(rel);
164
- void changed(rel);
214
+ const queue = (file, dir) => {
215
+ clearTimeout(pending.get(file));
216
+ pending.set(file, setTimeout(() => {
217
+ pending.delete(file);
218
+ void changed(file, dir);
165
219
  }, 30));
166
- });
220
+ };
221
+ const watchers = [];
222
+ const watchTree = (dir, only) => {
223
+ watchers.push((0, import_node_fs.watch)(dir, { recursive: !only }, (_event, filename) => {
224
+ if (!filename) return;
225
+ const file = (0, import_node_path.resolve)(dir, filename.toString());
226
+ const rel = (0, import_node_path.relative)(dir, file);
227
+ if (!rel || rel.startsWith("..")) return;
228
+ if (only ? rel !== only : ignored(rel)) return;
229
+ queue(file, dir);
230
+ }));
231
+ };
232
+ const literalHead = (pattern) => {
233
+ const parts = pattern.split("/");
234
+ const magic = parts.findIndex((part) => /[*?[\]{}!]/.test(part));
235
+ return (0, import_node_path.resolve)(magic === -1 ? pattern : parts.slice(0, magic).join("/") || ".");
236
+ };
237
+ const extra = /* @__PURE__ */ new Map();
238
+ for (const entry of entries) {
239
+ for (const [i, pattern] of entry.patterns.entries()) {
240
+ const head2 = literalHead(pattern);
241
+ const info = await (0, import_promises.stat)(head2).catch(() => null);
242
+ if (!info) throw new Error(`jq79 dev: nothing to watch at ${head2}`);
243
+ if (info.isDirectory() && head2 === (0, import_node_path.resolve)(pattern)) {
244
+ entry.patterns[i] = pattern.replace(/\/+$/, "") + "/**";
245
+ }
246
+ const real = await (0, import_promises.realpath)(head2);
247
+ if (real === root || real.startsWith(root + import_node_path.sep)) continue;
248
+ const watched = info.isDirectory() ? { dir: real } : { dir: (0, import_node_path.dirname)(real), only: (0, import_node_path.basename)(real) };
249
+ extra.set(`${watched.dir}\0${watched.only ?? ""}`, watched);
250
+ }
251
+ }
252
+ watchTree(root);
253
+ extra.forEach(({ dir, only }) => watchTree(dir, only));
167
254
  const heartbeat = setInterval(() => clients.forEach((client) => client.write(": ping\n\n")), 3e4);
168
255
  heartbeat.unref();
169
256
  await new Promise((done, fail) => {
@@ -176,8 +263,9 @@ data: ${JSON.stringify(data)}
176
263
  port,
177
264
  close: () => new Promise((done) => {
178
265
  clearInterval(heartbeat);
179
- watcher.close();
266
+ watchers.forEach((watcher) => watcher.close());
180
267
  pending.forEach(clearTimeout);
268
+ entries.forEach((entry) => clearTimeout(entry.timer));
181
269
  clients.forEach((client) => client.end());
182
270
  clients.clear();
183
271
  server.close(() => done());
package/dist/dev.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../dev/dev.ts"],"sourcesContent":["import { createServer, type Server, type IncomingMessage, type ServerResponse } from \"node:http\"\nimport { readFile, realpath, stat } from \"node:fs/promises\"\nimport { watch, type FSWatcher } from \"node:fs\"\nimport { basename, extname, join, relative, resolve, sep } from \"node:path\"\n\n// A dev server for the no-bundle path: serve a directory of .html components\n// over HTTP, watch it, and hot-reload the components that changed.\n//\n// npx jq79 dev // the CLI\n// import { devServer } from \"jq79/dev\" // or from a script\n// await devServer({ rootDir: \".\" })\n//\n// It is a static file server and nothing else - no transforms, no bundling, no\n// module graph. Which is the point: the files it serves are the files you would\n// deploy, so what you develop against and what a static host serves are the same\n// bytes. The one thing it adds is the hot-reload channel, and it adds it to\n// *documents* only (a component fetched by the runtime is served verbatim).\n//\n// The reload is fine-grained. On a change the server pushes the new source down\n// an SSE channel and the runtime swaps it into every live instance of that file,\n// keeping its data - see hotUpdate in jq79.ts. Anything the runtime can't place\n// (a page, a stylesheet, a component nothing has mounted yet) falls back to a\n// full page reload.\n\nexport interface DevServerOptions {\n // the directory to serve, and to watch (default: the current directory)\n rootDir?: string\n // default: 4179, or the first free port after it\n port?: number\n // default: localhost\n host?: string\n}\n\nexport interface DevServer {\n url: string\n port: number\n close: () => Promise<void>\n}\n\nconst CONTENT_TYPES: Record<string, string> = {\n \".html\": \"text/html; charset=utf-8\",\n \".js\": \"text/javascript; charset=utf-8\",\n \".mjs\": \"text/javascript; charset=utf-8\",\n \".json\": \"application/json; charset=utf-8\",\n \".css\": \"text/css; charset=utf-8\",\n \".svg\": \"image/svg+xml\",\n \".png\": \"image/png\",\n \".jpg\": \"image/jpeg\",\n \".jpeg\": \"image/jpeg\",\n \".gif\": \"image/gif\",\n \".webp\": \"image/webp\",\n \".avif\": \"image/avif\",\n \".ico\": \"image/x-icon\",\n \".woff\": \"font/woff\",\n \".woff2\": \"font/woff2\",\n \".map\": \"application/json; charset=utf-8\",\n}\n\nconst CLIENT_URL = \"/__jq79/client.js\"\nconst EVENTS_URL = \"/__jq79/events\"\n\n// Served as a *classic* script, and injected into the <head>: classic scripts\n// run at parse time and module scripts are deferred, so the flag is set before\n// the page's `import ... from \"jq79\"` evaluates - which is what the runtime\n// waits for before it starts tracking instances. The client can't import the\n// runtime itself: the page's copy may come from a CDN or an import map, and a\n// second copy would have a second, empty registry.\nconst CLIENT = `(() => {\n window.__JQ79_HMR_ENABLED__ = true\n\n const events = new EventSource(${JSON.stringify(EVENTS_URL)})\n\n events.addEventListener(\"update\", event => {\n const { url, src } = JSON.parse(event.data)\n const runtime = window.__JQ79_HMR__\n // no runtime (the page doesn't use jq79), or no live instance from this\n // file (it isn't mounted, or it *is* the page) - nothing to swap into\n const patched = runtime ? runtime.update(url, src) : 0\n if (patched) console.log(\"[jq79] hot-updated \" + url + \" (\" + patched + (patched === 1 ? \" instance)\" : \" instances)\"))\n else location.reload()\n })\n\n events.addEventListener(\"reload\", () => location.reload())\n})()`\n\nconst posix = (path: string) => path.split(sep).join(\"/\")\n\nconst isDocument = (req: IncomingMessage) => req.headers[\"sec-fetch-dest\"] === \"document\"\n\n// the client goes in the <head> so it is the first thing the page runs. A file\n// with neither <head> nor <body> is still a document a browser will render, so\n// fall back to the top of it rather than skipping the injection\nconst injectClient = (html: string): string => {\n const tag = `<script src=\"${CLIENT_URL}\"></script>`\n // (\\s[^>]*)? rather than [^>]*, or <header> would pass for <head>\n const open = /<head(\\s[^>]*)?>/i.exec(html) ?? /<body(\\s[^>]*)?>/i.exec(html)\n if (!open) return tag + html\n const at = open.index + open[0].length\n return html.slice(0, at) + tag + html.slice(at)\n}\n\nexport const devServer = async (options: DevServerOptions = {}): Promise<DevServer> => {\n // the *real* path: the watcher reports what changed relative to the directory\n // it actually opened, so a root reached through a symlink (/tmp and /var are\n // symlinks on macOS) would hand back paths that don't line up with it\n const root = await realpath(resolve(options.rootDir ?? \".\"))\n const host = options.host ?? \"localhost\"\n\n const clients = new Set<ServerResponse>()\n\n const send = (event: string, data: unknown) => {\n const frame = `event: ${event}\\ndata: ${JSON.stringify(data)}\\n\\n`\n clients.forEach(client => client.write(frame))\n }\n\n // --- serving ---------------------------------------------------------------\n\n const serveStatic = async (req: IncomingMessage, res: ServerResponse, pathname: string) => {\n // a URL path is not a file path: decode it, then keep the result inside the\n // root (\"..\" in a request must not walk out of the served directory)\n let file: string\n try {\n file = resolve(join(root, decodeURIComponent(pathname)))\n } catch {\n res.writeHead(400).end(\"bad request\")\n return\n }\n if (file !== root && !file.startsWith(root + sep)) {\n res.writeHead(403).end(\"forbidden\")\n return\n }\n\n try {\n if ((await stat(file)).isDirectory()) {\n // a directory is served through its trailing-slash URL, like every\n // static host: /docs rendered as-is would resolve its relative links\n // against the parent (\"img.png\" -> /img.png instead of /docs/img.png)\n if (!pathname.endsWith(\"/\")) {\n res.writeHead(301, { location: pathname + \"/\" }).end()\n return\n }\n file = join(file, \"index.html\")\n }\n const body = await readFile(file)\n const type = CONTENT_TYPES[extname(file).toLowerCase()] ?? \"application/octet-stream\"\n\n // only a navigation gets the hot-reload client. A component is fetched by\n // the runtime (sec-fetch-dest: empty), and it must arrive as written -\n // injecting a <script> into it would make the runtime parse and run it\n const html = type.startsWith(\"text/html\") && isDocument(req)\n const payload = html ? Buffer.from(injectClient(body.toString(\"utf8\"))) : body\n\n res.writeHead(200, {\n \"content-type\": type,\n \"content-length\": payload.byteLength,\n \"cache-control\": \"no-store\", // the file on disk is always the truth here\n })\n res.end(payload)\n } catch {\n res.writeHead(404, { \"content-type\": \"text/plain; charset=utf-8\" }).end(\"not found\")\n }\n }\n\n const server: Server = createServer((req, res) => {\n const pathname = (req.url ?? \"/\").split(/[?#]/)[0]\n\n if (pathname === CLIENT_URL) {\n res.writeHead(200, { \"content-type\": \"text/javascript; charset=utf-8\", \"cache-control\": \"no-store\" })\n res.end(CLIENT)\n return\n }\n\n if (pathname === EVENTS_URL) {\n res.writeHead(200, {\n \"content-type\": \"text/event-stream\",\n \"cache-control\": \"no-store\",\n connection: \"keep-alive\",\n })\n res.write(\": jq79\\n\\n\") // opens the stream, so the browser fires onopen\n clients.add(res)\n req.on(\"close\", () => clients.delete(res))\n return\n }\n\n void serveStatic(req, res, pathname)\n })\n\n // --- watching --------------------------------------------------------------\n\n const ignored = (rel: string) =>\n rel.split(sep).some(part => part.startsWith(\".\") || part === \"node_modules\")\n\n // one save can arrive as several events (a rename plus a change, an editor's\n // atomic write); collapsing per file keeps that down to one push\n const pending = new Map<string, NodeJS.Timeout>()\n\n const changed = async (rel: string) => {\n const file = join(root, rel)\n\n let src: string | null = null\n try {\n // a directory changes whenever anything inside it does, and the event for\n // the file itself is already on its way - acting on both would reload the\n // page every time a component is saved\n if ((await stat(file)).isDirectory()) return\n if (rel.endsWith(\".html\")) src = await readFile(file, \"utf8\")\n } catch {\n // gone: deleted, or renamed away - and there is nothing to swap in, so the\n // page has to reload. Unless it was never there: macOS reports a change to\n // the watched directory *itself* under its own basename, which resolves to\n // a path inside it that does not exist\n if (rel === basename(root)) return\n }\n\n // the url is the one the component was served from, because that is what the\n // runtime resolves its instances' filenames against\n const url = \"/\" + posix(rel)\n if (src === null) send(\"reload\", { url })\n else send(\"update\", { url, src })\n }\n\n const watcher: FSWatcher = watch(root, { recursive: true }, (_event, filename) => {\n if (!filename) return\n const rel = relative(root, resolve(root, filename.toString()))\n if (!rel || rel.startsWith(\"..\") || ignored(rel)) return\n\n clearTimeout(pending.get(rel))\n pending.set(rel, setTimeout(() => {\n pending.delete(rel)\n void changed(rel)\n }, 30))\n })\n\n // proxies and load balancers cut an idle stream; a comment every 30s is the\n // conventional way to keep it open. unref'd, so it never holds the process up\n const heartbeat = setInterval(() => clients.forEach(client => client.write(\": ping\\n\\n\")), 30_000)\n heartbeat.unref()\n\n // --- go --------------------------------------------------------------------\n\n await new Promise<void>((done, fail) => {\n server.once(\"error\", fail)\n server.listen(options.port ?? 4179, host, done)\n })\n const { port } = server.address() as { port: number }\n\n return {\n url: `http://${host}:${port}`,\n port,\n close: () =>\n new Promise(done => {\n clearInterval(heartbeat)\n watcher.close()\n pending.forEach(clearTimeout)\n clients.forEach(client => client.end())\n clients.clear()\n server.close(() => done())\n }),\n }\n}\n\nexport default devServer\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAqF;AACrF,sBAAyC;AACzC,qBAAsC;AACtC,uBAAgE;AAoChE,IAAM,gBAAwC;AAAA,EAC5C,SAAS;AAAA,EACT,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,UAAU;AAAA,EACV,QAAQ;AACV;AAEA,IAAM,aAAa;AACnB,IAAM,aAAa;AAQnB,IAAM,SAAS;AAAA;AAAA;AAAA,mCAGoB,KAAK,UAAU,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAe7D,IAAM,QAAQ,CAAC,SAAiB,KAAK,MAAM,oBAAG,EAAE,KAAK,GAAG;AAExD,IAAM,aAAa,CAAC,QAAyB,IAAI,QAAQ,gBAAgB,MAAM;AAK/E,IAAM,eAAe,CAAC,SAAyB;AAC7C,QAAM,MAAM,gBAAgB,UAAU;AAEtC,QAAM,OAAO,oBAAoB,KAAK,IAAI,KAAK,oBAAoB,KAA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L;AACF;AAEA,IAAO,cAAQ;","names":[]}
1
+ {"version":3,"sources":["../dev/dev.ts"],"sourcesContent":["import { createServer, type Server, type IncomingMessage, type ServerResponse } from \"node:http\"\nimport { readFile, realpath, stat } from \"node:fs/promises\"\nimport { watch, type FSWatcher } from \"node:fs\"\nimport { basename, dirname, extname, isAbsolute, join, matchesGlob, relative, resolve, sep } from \"node:path\"\n\n// A dev server for the no-bundle path: serve a directory of .html components\n// over HTTP, watch it, and hot-reload the components that changed.\n//\n// npx jq79 dev // the CLI\n// import { devServer } from \"jq79/dev\" // or from a script\n// await devServer({ rootDir: \".\" })\n//\n// It is a static file server and nothing else - no transforms, no bundling, no\n// module graph. Which is the point: the files it serves are the files you would\n// deploy, so what you develop against and what a static host serves are the same\n// bytes. The one thing it adds is the hot-reload channel, and it adds it to\n// *documents* only (a component fetched by the runtime is served verbatim).\n//\n// The reload is fine-grained. On a change the server pushes the new source down\n// an SSE channel and the runtime swaps it into every live instance of that file,\n// keeping its data - see hotUpdate in jq79.ts. Anything the runtime can't place\n// (a page, a stylesheet, a component nothing has mounted yet) falls back to a\n// full page reload.\n\nexport interface DevServerOptions {\n // the directory to serve, and to watch (default: the current directory)\n rootDir?: string\n // default: 4179, or the first free port after it\n port?: number\n // default: localhost\n host?: string\n // response headers to send on everything - a static host is configured, and\n // these are that configuration (default: none)\n headers?: Record<string, string>\n // the same thing per request: a hook gets the request and its response before\n // anything has been written to it (default: none)\n beforeResponse?: ResponseHook[]\n // what to watch on top of rootDir, and what to do about it (default: none)\n watch?: WatchEntry[]\n}\n\n// runs on every response the server makes - a page, a component, the client,\n// the event stream, a 404 - with nothing written yet, so `res.setHeader` still\n// applies. A hook that answers the request itself (`res.end`) is left alone\nexport type ResponseHook = (req: IncomingMessage, res: ServerResponse) => void | Promise<void>\n\n// the handler gets every file of the burst that woke it, absolute, so a save\n// that touches three of them is one build rather than three\nexport type WatchHandler = (files: string[]) => void | Promise<void>\n\nexport interface WatchEntry {\n // glob(s) resolved against the cwd, like rootDir: \"styles/**/*.scss\". A bare\n // directory means everything under it\n pattern: string | string[]\n // what a match runs. Without one, a match outside the served root reloads the\n // page - which is the only other thing a file served from nowhere can do\n fn?: WatchHandler\n}\n\nexport interface DevServer {\n url: string\n port: number\n close: () => Promise<void>\n}\n\nconst CONTENT_TYPES: Record<string, string> = {\n \".html\": \"text/html; charset=utf-8\",\n \".js\": \"text/javascript; charset=utf-8\",\n \".mjs\": \"text/javascript; charset=utf-8\",\n // the streaming WebAssembly APIs reject every other type, including the\n // default below - a module served as octet-stream still runs, but it is\n // buffered whole instead of compiling as it downloads\n \".wasm\": \"application/wasm\",\n \".json\": \"application/json; charset=utf-8\",\n \".css\": \"text/css; charset=utf-8\",\n \".svg\": \"image/svg+xml\",\n \".png\": \"image/png\",\n \".jpg\": \"image/jpeg\",\n \".jpeg\": \"image/jpeg\",\n \".gif\": \"image/gif\",\n \".webp\": \"image/webp\",\n \".avif\": \"image/avif\",\n \".ico\": \"image/x-icon\",\n \".woff\": \"font/woff\",\n \".woff2\": \"font/woff2\",\n \".map\": \"application/json; charset=utf-8\",\n}\n\nconst CLIENT_URL = \"/__jq79/client.js\"\nconst EVENTS_URL = \"/__jq79/events\"\n\n// Served as a *classic* script, and injected into the <head>: classic scripts\n// run at parse time and module scripts are deferred, so the flag is set before\n// the page's `import ... from \"jq79\"` evaluates - which is what the runtime\n// waits for before it starts tracking instances. The client can't import the\n// runtime itself: the page's copy may come from a CDN or an import map, and a\n// second copy would have a second, empty registry.\nconst CLIENT = `(() => {\n window.__JQ79_HMR_ENABLED__ = true\n\n const events = new EventSource(${JSON.stringify(EVENTS_URL)})\n\n events.addEventListener(\"update\", event => {\n const { url, src } = JSON.parse(event.data)\n const runtime = window.__JQ79_HMR__\n // no runtime (the page doesn't use jq79), or no live instance from this\n // file (it isn't mounted, or it *is* the page) - nothing to swap into\n const patched = runtime ? runtime.update(url, src) : 0\n if (patched) console.log(\"[jq79] hot-updated \" + url + \" (\" + patched + (patched === 1 ? \" instance)\" : \" instances)\"))\n else location.reload()\n })\n\n events.addEventListener(\"reload\", () => location.reload())\n})()`\n\nconst posix = (path: string) => path.split(sep).join(\"/\")\n\nconst isDocument = (req: IncomingMessage) => req.headers[\"sec-fetch-dest\"] === \"document\"\n\n// the client goes in the <head> so it is the first thing the page runs. A file\n// with neither <head> nor <body> is still a document a browser will render, so\n// fall back to the top of it rather than skipping the injection\nconst injectClient = (html: string): string => {\n const tag = `<script src=\"${CLIENT_URL}\"></script>`\n // (\\s[^>]*)? rather than [^>]*, or <header> would pass for <head>\n const open = /<head(\\s[^>]*)?>/i.exec(html) ?? /<body(\\s[^>]*)?>/i.exec(html)\n if (!open) return tag + html\n const at = open.index + open[0].length\n return html.slice(0, at) + tag + html.slice(at)\n}\n\nexport const devServer = async (options: DevServerOptions = {}): Promise<DevServer> => {\n // the *real* path: the watcher reports what changed relative to the directory\n // it actually opened, so a root reached through a symlink (/tmp and /var are\n // symlinks on macOS) would hand back paths that don't line up with it\n const root = await realpath(resolve(options.rootDir ?? \".\"))\n const host = options.host ?? \"localhost\"\n\n // a configured header goes on every response, which is the one thing\n // content-type and content-length cannot do - each describes the bytes of a\n // single response. Dropped here rather than at write time, so that either of\n // them still standing by then can only have come from a hook, which saw the\n // request and is entitled to say\n const headers = Object.entries(options.headers ?? {}).filter(\n ([name]) => ![\"content-type\", \"content-length\"].includes(name.toLowerCase()),\n )\n const hooks = options.beforeResponse ?? []\n\n const clients = new Set<ServerResponse>()\n\n const send = (event: string, data: unknown) => {\n const frame = `event: ${event}\\ndata: ${JSON.stringify(data)}\\n\\n`\n clients.forEach(client => client.write(frame))\n }\n\n // --- serving ---------------------------------------------------------------\n\n // what the response says about its own bytes, written last so the layers\n // below can't contradict it. A hook is the one exception: it saw the request,\n // so it is entitled to an opinion about what these bytes *are* - never about\n // how many there are, since a wrong content-length truncates the body or\n // hangs the socket\n const head = (res: ServerResponse, status: number, own: Record<string, string | number> = {}) => {\n res.removeHeader(\"content-length\")\n const write = { ...own }\n if (res.hasHeader(\"content-type\")) delete write[\"content-type\"]\n return res.writeHead(status, write)\n }\n\n const serveStatic = async (req: IncomingMessage, res: ServerResponse, pathname: string) => {\n // a URL path is not a file path: decode it, then keep the result inside the\n // root (\"..\" in a request must not walk out of the served directory)\n let file: string\n try {\n file = resolve(join(root, decodeURIComponent(pathname)))\n } catch {\n head(res, 400).end(\"bad request\")\n return\n }\n if (file !== root && !file.startsWith(root + sep)) {\n head(res, 403).end(\"forbidden\")\n return\n }\n\n try {\n if ((await stat(file)).isDirectory()) {\n // a directory is served through its trailing-slash URL, like every\n // static host: /docs rendered as-is would resolve its relative links\n // against the parent (\"img.png\" -> /img.png instead of /docs/img.png)\n if (!pathname.endsWith(\"/\")) {\n head(res, 301, { location: pathname + \"/\" }).end()\n return\n }\n file = join(file, \"index.html\")\n }\n const body = await readFile(file)\n const type = CONTENT_TYPES[extname(file).toLowerCase()] ?? \"application/octet-stream\"\n\n // only a navigation gets the hot-reload client. A component is fetched by\n // the runtime (sec-fetch-dest: empty), and it must arrive as written -\n // injecting a <script> into it would make the runtime parse and run it\n const html = type.startsWith(\"text/html\") && isDocument(req)\n const payload = html ? Buffer.from(injectClient(body.toString(\"utf8\"))) : body\n\n head(res, 200, { \"content-type\": type, \"content-length\": payload.byteLength })\n res.end(payload)\n } catch {\n head(res, 404, { \"content-type\": \"text/plain; charset=utf-8\" }).end(\"not found\")\n }\n }\n\n // three layers, general to specific, each one free to replace the last: the\n // server's own defaults, the configured headers over them, and a hook - which\n // saw the request - over those. They go on with setHeader rather than into an\n // object, because setHeader is case-insensitive where an object is not: a\n // \"Cache-Control\" replaces the cache-control below it instead of arriving\n // beside it, and the same goes for whatever a hook names\n const handle = async (req: IncomingMessage, res: ServerResponse) => {\n res.setHeader(\"cache-control\", \"no-store\") // the file on disk is always the truth here\n for (const [name, value] of headers) res.setHeader(name, value)\n for (const hook of hooks) await hook(req, res)\n\n // a hook can answer the request itself, and one that did needs nothing else\n if (res.headersSent || res.writableEnded) return\n\n const pathname = (req.url ?? \"/\").split(/[?#]/)[0]\n\n if (pathname === CLIENT_URL) {\n head(res, 200, { \"content-type\": \"text/javascript; charset=utf-8\" })\n res.end(CLIENT)\n return\n }\n\n if (pathname === EVENTS_URL) {\n head(res, 200, { \"content-type\": \"text/event-stream\", connection: \"keep-alive\" })\n res.write(\": jq79\\n\\n\") // opens the stream, so the browser fires onopen\n clients.add(res)\n req.on(\"close\", () => clients.delete(res))\n return\n }\n\n await serveStatic(req, res, pathname)\n }\n\n const server: Server = createServer((req, res) => {\n // a hook is someone's code, and the watch handlers already settled what that\n // means around here: report it and stay up. This one owes the browser a\n // reply as well, because a socket nobody answers hangs the page\n void handle(req, res).catch(error => {\n console.error(`jq79 dev: failed to respond to ${req.url}\\n`, error)\n if (!res.headersSent) {\n // whatever the hook had said about the bytes, it is not what is being\n // sent now - and a content-length it set before throwing would hang this\n res.removeHeader(\"content-length\")\n res.writeHead(500, { \"content-type\": \"text/plain; charset=utf-8\" })\n }\n if (!res.writableEnded) res.end(\"internal error\")\n })\n })\n\n // --- watching --------------------------------------------------------------\n\n const ignored = (rel: string) =>\n rel.split(sep).some(part => part.startsWith(\".\") || part === \"node_modules\")\n\n // one save can arrive as several events (a rename plus a change, an editor's\n // atomic write); collapsing per file keeps that down to one push\n const pending = new Map<string, NodeJS.Timeout>()\n\n // a pattern is written against the cwd (`resolve` reads it the same way), and\n // captured here so a later chdir can't move what the globs mean\n const cwd = process.cwd()\n\n type Entry = {\n patterns: string[]\n fn?: WatchHandler\n batch: Set<string>\n timer?: NodeJS.Timeout\n running: boolean\n }\n\n const entries: Entry[] = (options.watch ?? []).map(entry => ({\n patterns: [entry.pattern].flat(),\n fn: entry.fn,\n batch: new Set(),\n running: false,\n }))\n\n const claims = (entry: Entry, file: string) =>\n entry.patterns.some(pattern =>\n matchesGlob(isAbsolute(pattern) ? posix(file) : posix(relative(cwd, file)), pattern),\n )\n\n // a burst is one call: the handler is a build step, and building once per file\n // of a save that touched four is three builds nobody asked for\n const collect = (entry: Entry, file: string) => {\n entry.batch.add(file)\n clearTimeout(entry.timer)\n entry.timer = setTimeout(() => void fire(entry), 30)\n }\n\n const fire = async (entry: Entry) => {\n if (entry.running || !entry.batch.size) return\n const files = [...entry.batch]\n entry.batch.clear()\n entry.running = true\n try {\n await entry.fn?.(files)\n } catch (error) {\n // a handler is someone's build script, and a build that fails is a normal\n // morning. Reporting it and staying up beats taking the server with it\n console.error(`jq79 dev: watch handler for ${entry.patterns.join(\", \")} failed\\n`, error)\n } finally {\n entry.running = false\n if (entry.batch.size) void fire(entry) // saved again while it ran\n }\n }\n\n // `dir` is the directory whose watcher reported this - only needed for the\n // macOS quirk below, and only when the file turns out not to exist\n const changed = async (file: string, dir: string) => {\n // where the browser knows the file from, which only exists for a file under\n // the served root: a watched path outside it is served from nowhere, so\n // there is no url for the runtime to match an instance against\n const rel = relative(root, file)\n const served = !!rel && !rel.startsWith(\"..\")\n\n let src: string | null = null\n try {\n // a directory changes whenever anything inside it does, and the event for\n // the file itself is already on its way - acting on both would reload the\n // page every time a component is saved\n if ((await stat(file)).isDirectory()) return\n if (served && file.endsWith(\".html\")) src = await readFile(file, \"utf8\")\n } catch {\n // gone: deleted, or renamed away - and there is nothing to swap in, so the\n // page has to reload. Unless it was never there: macOS reports a change to\n // a watched directory *itself* under its own basename, which resolves to a\n // path inside it that does not exist\n if (file === join(dir, basename(dir))) return\n }\n\n // every entry that names this file gets it, wherever the file lives. What\n // the root does with its own files is not up for negotiation: no pattern can\n // switch hot reload off, so a handler can never cost you the thing you came\n // for by matching more than its author meant it to\n const claimed = entries.filter(entry => claims(entry, file))\n claimed.forEach(entry => entry.fn && collect(entry, file))\n\n if (served) {\n // the url is the one the component was served from, because that is what\n // the runtime resolves its instances' filenames against\n const url = \"/\" + posix(rel)\n if (src === null) send(\"reload\", { url })\n else send(\"update\", { url, src })\n return\n }\n\n // outside the root a handler *is* the answer: it ran, and whatever it writes\n // into the served directory comes back round as a change of its own, with a\n // url. An entry with no handler is asking for the page, and gets a path\n // relative to the root - the client only logs it, and an absolute one would\n // publish the machine's layout to the page\n if (claimed.some(entry => !entry.fn)) send(\"reload\", { url: posix(rel) })\n }\n\n const queue = (file: string, dir: string) => {\n clearTimeout(pending.get(file))\n pending.set(file, setTimeout(() => {\n pending.delete(file)\n void changed(file, dir)\n }, 30))\n }\n\n const watchers: FSWatcher[] = []\n\n // a directory is watched whole. A single file is watched through the directory\n // it sits in (`only` filtering the rest back out), because a watcher on a file\n // holds its inode, and an editor's atomic save renames a new one over it - the\n // watcher survives as a handle on a file nothing will ever write to again\n const watchTree = (dir: string, only?: string) => {\n watchers.push(watch(dir, { recursive: !only }, (_event, filename) => {\n if (!filename) return\n const file = resolve(dir, filename.toString())\n const rel = relative(dir, file)\n if (!rel || rel.startsWith(\"..\")) return\n if (only ? rel !== only : ignored(rel)) return\n queue(file, dir)\n }))\n }\n\n // a glob is a filter and a watcher needs a directory to open, so the watch\n // starts at the literal head of the pattern - everything before the first\n // magic character. \"styles/**/*.scss\" opens styles/, \"**/*.scss\" opens the cwd\n const literalHead = (pattern: string) => {\n const parts = pattern.split(\"/\")\n const magic = parts.findIndex(part => /[*?[\\]{}!]/.test(part))\n return resolve(magic === -1 ? pattern : parts.slice(0, magic).join(\"/\") || \".\")\n }\n\n // the patterns are resolved like rootDir, against the cwd, and all checked\n // before anything is watched: a path that isn't there is a typo, and a watcher\n // that silently isn't running is worse than a server that won't start\n const extra = new Map<string, { dir: string; only?: string }>()\n for (const entry of entries) {\n for (const [i, pattern] of entry.patterns.entries()) {\n const head = literalHead(pattern)\n const info = await stat(head).catch(() => null)\n if (!info) throw new Error(`jq79 dev: nothing to watch at ${head}`)\n\n // a bare directory means everything under it, which is what it looks like\n // it means - as a pattern it would watch the directory and match nothing\n // in it, and the mistake is invisible until a save doesn't fire\n if (info.isDirectory() && head === resolve(pattern)) {\n entry.patterns[i] = pattern.replace(/\\/+$/, \"\") + \"/**\"\n }\n\n // real paths on both sides, so a symlink pointing out of the root reads as\n // what it is - outside, and not covered by the recursive watch below\n const real = await realpath(head)\n if (real === root || real.startsWith(root + sep)) continue // already watched\n const watched = info.isDirectory() ? { dir: real } : { dir: dirname(real), only: basename(real) }\n extra.set(`${watched.dir}\\0${watched.only ?? \"\"}`, watched) // two patterns can share a head\n }\n }\n\n watchTree(root)\n extra.forEach(({ dir, only }) => watchTree(dir, only))\n\n // proxies and load balancers cut an idle stream; a comment every 30s is the\n // conventional way to keep it open. unref'd, so it never holds the process up\n const heartbeat = setInterval(() => clients.forEach(client => client.write(\": ping\\n\\n\")), 30_000)\n heartbeat.unref()\n\n // --- go --------------------------------------------------------------------\n\n await new Promise<void>((done, fail) => {\n server.once(\"error\", fail)\n server.listen(options.port ?? 4179, host, done)\n })\n const { port } = server.address() as { port: number }\n\n return {\n url: `http://${host}:${port}`,\n port,\n close: () =>\n new Promise(done => {\n clearInterval(heartbeat)\n watchers.forEach(watcher => watcher.close())\n pending.forEach(clearTimeout)\n entries.forEach(entry => clearTimeout(entry.timer))\n clients.forEach(client => client.end())\n clients.clear()\n server.close(() => done())\n }),\n }\n}\n\nexport default devServer\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAqF;AACrF,sBAAyC;AACzC,qBAAsC;AACtC,uBAAkG;AA8DlG,IAAM,gBAAwC;AAAA,EAC5C,SAAS;AAAA,EACT,OAAO;AAAA,EACP,QAAQ;AAAA;AAAA;AAAA;AAAA,EAIR,SAAS;AAAA,EACT,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,UAAU;AAAA,EACV,QAAQ;AACV;AAEA,IAAM,aAAa;AACnB,IAAM,aAAa;AAQnB,IAAM,SAAS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Ac,SAAS;AACvB,eAAS,QAAQ,aAAW,QAAQ,MAAM,CAAC;AAC3C,cAAQ,QAAQ,YAAY;AAC5B,cAAQ,QAAQ,WAAS,aAAa,MAAM,KAAK,CAAC;AAClD,cAAQ,QAAQ,YAAU,OAAO,IAAI,CAAC;AACtC,cAAQ,MAAM;AACd,aAAO,MAAM,MAAM,KAAK,CAAC;AAAA,IAC3B,CAAC;AAAA,EACL;AACF;AAEA,IAAO,cAAQ;","names":["head"]}
package/dist/dev.d.ts CHANGED
@@ -1,7 +1,17 @@
1
+ import { type IncomingMessage, type ServerResponse } from "node:http";
1
2
  export interface DevServerOptions {
2
3
  rootDir?: string;
3
4
  port?: number;
4
5
  host?: string;
6
+ headers?: Record<string, string>;
7
+ beforeResponse?: ResponseHook[];
8
+ watch?: WatchEntry[];
9
+ }
10
+ export type ResponseHook = (req: IncomingMessage, res: ServerResponse) => void | Promise<void>;
11
+ export type WatchHandler = (files: string[]) => void | Promise<void>;
12
+ export interface WatchEntry {
13
+ pattern: string | string[];
14
+ fn?: WatchHandler;
5
15
  }
6
16
  export interface DevServer {
7
17
  url: string;