1
- // Simple server adapted from https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework:
2
- import * as fs from "fs" ;
3
- import * as http from "http" ;
1
+ // Simple local server
4
2
import * as path from "path" ;
5
3
import commandLineArgs from "command-line-args" ;
6
- import esMain from 'es-main' ;
4
+ import esMain from "es-main" ;
5
+ import LocalWebServer from "local-web-server" ;
7
6
8
- const MIME_TYPES = {
9
- default : "application/octet-stream" ,
10
- html : "text/html; charset=UTF-8" ,
11
- js : "application/javascript; charset=UTF-8" ,
12
- mjs : "application/javascript; charset=UTF-8" ,
13
- css : "text/css" ,
14
- png : "image/png" ,
15
- jpg : "image/jpg" ,
16
- gif : "image/gif" ,
17
- ico : "image/x-icon" ,
18
- svg : "image/svg+xml" ,
19
- } ;
7
+ const ROOT_DIR = path . join ( process . cwd ( ) , "./" ) ;
20
8
21
- const STATIC_PATH = path . join ( process . cwd ( ) , "./" ) ;
22
- const toBool = [ ( ) => true , ( ) => false ] ;
23
-
24
- export default function serve ( port ) {
9
+ export default async function serve ( port ) {
25
10
if ( ! port )
26
11
throw new Error ( "Port is required" ) ;
27
-
28
- const prepareFile = async ( url ) => {
29
- const paths = [ STATIC_PATH , url . pathname ] ;
30
- if ( url . pathname . endsWith ( "/" ) )
31
- paths . push ( "index.html" ) ;
32
- const filePath = path . join ( ...paths ) ;
33
- const pathTraversal = ! filePath . startsWith ( STATIC_PATH ) ;
34
- const exists = await fs . promises . access ( filePath ) . then ( ...toBool ) ;
35
- const found = ! pathTraversal && exists ;
36
- const streamPath = found ? filePath : `${ STATIC_PATH } /index.html` ;
37
- const ext = path . extname ( streamPath ) . substring ( 1 ) . toLowerCase ( ) ;
38
- const stream = fs . createReadStream ( streamPath ) ;
39
- return { found, ext, stream } ;
12
+ const ws = await LocalWebServer . create ( {
13
+ port : port ,
14
+ directory : ROOT_DIR ,
15
+ corsOpenerPolicy : "same-origin" ,
16
+ corsEmbedderPolicy : "require-corp" ,
17
+ } ) ;
18
+ console . log ( `Server started on http://localhost:${ port } ` ) ;
19
+ process . on ( "exit" , ( ) => ws . server . close ( ) ) ;
20
+ return {
21
+ close ( ) {
22
+ ws . server . close ( ) ;
23
+ }
40
24
} ;
41
-
42
- const server = http
43
- . createServer ( async ( req , res ) => {
44
- const url = new URL ( `http://localhost${ req . url } ` ) ;
45
- const file = await prepareFile ( url ) ;
46
- const statusCode = file . found ? 200 : 404 ;
47
- const mimeType = MIME_TYPES [ file . ext ] || MIME_TYPES . default ;
48
- res . writeHead ( statusCode , {
49
- "Content-Type" : mimeType ,
50
- "Cross-Origin-Embedder-Policy" : "require-corp" ,
51
- "Cross-Origin-Opener-Policy" : "same-origin" ,
52
- } ) ;
53
- file . stream . pipe ( res ) ;
54
- } )
55
- . listen ( port ) ;
56
-
57
- console . log ( `Server running at http://127.0.0.1:${ port } /` ) ;
58
- return server ;
59
25
}
60
26
61
-
62
27
function main ( ) {
63
28
const optionDefinitions = [
64
29
{ name : "port" , type : Number , defaultValue : 8010 , description : "Set the test-server port, The default value is 8010." } ,
@@ -67,6 +32,5 @@ function main() {
67
32
serve ( options . port ) ;
68
33
}
69
34
70
- if ( esMain ( import . meta) ) {
35
+ if ( esMain ( import . meta) )
71
36
main ( ) ;
72
- }
0 commit comments