s4d 0.1.0 → 0.1.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.
@@ -7,6 +7,11 @@ export interface DevServerOptions {
7
7
  host?: string;
8
8
  spa?: boolean;
9
9
  }
10
+ interface FileCacheEntry {
11
+ contents: string | Buffer;
12
+ contentType: string;
13
+ version: string;
14
+ }
10
15
  export declare class DevServer {
11
16
  protected webroot: string;
12
17
  protected port: number;
@@ -15,7 +20,7 @@ export declare class DevServer {
15
20
  protected server: http.Server;
16
21
  protected webSocketServer: WebSocketServer;
17
22
  protected fileWatcher?: fs.FSWatcher;
18
- protected fileCache: Map<any, any>;
23
+ protected fileCache: Map<string, Promise<FileCacheEntry>>;
19
24
  protected webSockets: Set<WebSocket>;
20
25
  constructor(options: DevServerOptions);
21
26
  static cli(argv: string[]): Promise<1 | undefined>;
@@ -28,7 +33,7 @@ export declare class DevServer {
28
33
  broadcast(message: Record<string, unknown>): void;
29
34
  transformFileContents(file: string, contents: string | Buffer): Promise<string | Buffer>;
30
35
  getClientScript(): string;
31
- resolveFileCached(file: string): Promise<any>;
36
+ resolveFileCached(file: string): Promise<FileCacheEntry>;
32
37
  resolveFile(file: string): Promise<{
33
38
  contents: string | Buffer;
34
39
  contentType: string;
@@ -37,3 +42,4 @@ export declare class DevServer {
37
42
  invalidateFile(file: string): void;
38
43
  close(): Promise<void>;
39
44
  }
45
+ export {};
package/dist/DevServer.js CHANGED
@@ -128,7 +128,12 @@ export class DevServer {
128
128
  const url = new URL(req.url ?? '/', this.getBaseURL());
129
129
  try {
130
130
  const file = path.join(this.webroot, path.resolve('.', url.pathname));
131
- const { contents, contentType, version } = await this.resolveFileCached(file);
131
+ const { contents, contentType, version } = await this.resolveFileCached(file).catch((err) => {
132
+ if (this.spa && err.code === 'ENOENT') {
133
+ return this.resolveFileCached(path.join(this.webroot, 'index.html'));
134
+ }
135
+ throw err;
136
+ });
132
137
  if (req.headers['if-none-match'] === version) {
133
138
  res.writeHead(304);
134
139
  res.end();
@@ -286,32 +291,20 @@ export class DevServer {
286
291
  version: '1',
287
292
  };
288
293
  }
289
- let contents;
290
- try {
291
- const stat = await fs.promises.lstat(file);
292
- if (stat.isDirectory()) {
293
- file = path.join(file, 'index.html');
294
- }
295
- contents = await fs.promises.readFile(file);
296
- }
297
- catch (err) {
298
- if (this.spa && err.code === 'ENOENT') {
299
- file = path.join(this.webroot, 'index.html');
300
- contents = await fs.promises.readFile(file);
301
- }
302
- else {
303
- throw err;
304
- }
294
+ const stat = await fs.promises.lstat(file);
295
+ if (stat.isDirectory()) {
296
+ file = path.join(file, 'index.html');
305
297
  }
306
- contents = await this.transformFileContents(file, contents);
298
+ const buffer = await fs.promises.readFile(file);
299
+ const contents = await this.transformFileContents(file, buffer);
307
300
  const contentType = mime.getType(file) ?? 'application/octet-stream';
308
301
  const version = crypto.createHash('sha1').update(contents).digest('base64');
309
302
  return { contents, contentType, version };
310
303
  }
311
304
  invalidateFile(file) {
312
305
  this.fileCache.delete(file);
313
- this.fileCache.delete(file.replace(/index.html$/, ''));
314
- this.fileCache.delete(file.replace(/\/index.html$/, ''));
306
+ this.fileCache.delete(file.replace(/index\.html$/, ''));
307
+ this.fileCache.delete(file.replace(/\/index\.html$/, ''));
315
308
  }
316
309
  async close() {
317
310
  this.server.close();
package/dist/bin.d.ts CHANGED
@@ -1 +1,2 @@
1
+ #!/usr/bin/env node
1
2
  export {};
package/dist/bin.js CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env node
1
2
  import { DevServer } from './DevServer.js';
2
3
  DevServer.cli(process.argv.slice(2))
3
4
  .then((exitCode) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s4d",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Minimal web development server with live reload",
5
5
  "keywords": [
6
6
  "local",
@@ -22,7 +22,9 @@
22
22
  "node": ">=20"
23
23
  },
24
24
  "exports": "./dist/index.js",
25
- "bin": "./dist/bin.js",
25
+ "bin": {
26
+ "s4d": "dist/bin.js"
27
+ },
26
28
  "files": [
27
29
  "dist"
28
30
  ],