s4d 0.1.1 → 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.
- package/dist/DevServer.d.ts +8 -2
- package/dist/DevServer.js +13 -20
- package/package.json +1 -1
package/dist/DevServer.d.ts
CHANGED
|
@@ -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<
|
|
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<
|
|
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
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
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
|
|
314
|
-
this.fileCache.delete(file.replace(/\/index
|
|
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();
|