@node-cli/static-server 2.0.2 → 2.1.0

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/README.md CHANGED
@@ -24,18 +24,19 @@ At that point, you should be able to visit `http://localhost:8080` and see the f
24
24
 
25
25
  ### Options
26
26
 
27
- | option | default | description |
28
- | -------------------------- | ------- | ----------------------------------------------------------- |
29
- | `-c` or `--cache <number>` | 0 | Time in seconds for caching files |
30
- | `-C` or `--cors` | false | Set CORS headers to \* to allow requests from any origin |
31
- | `-d` or `--dirs` | true | List the directory's contents |
32
- | `-h` or `--help` | | Display help instructions |
33
- | `-H` or `--http2` | false | Enable HTTP/2 and SSL (https) |
34
- | `-l` or `--logs` | false | Log HTTP requests at the prompt |
35
- | `-o` or `--open` | false | Open in your default browser |
36
- | `-p` or `--port <n>` | 8080 | Port to listen on - Will try next available if already used |
37
- | `-g` or `--gzip` | true | Enable GZIP compression |
38
- | `-v` or `--version` | | Output the current version |
27
+ | option | default | description |
28
+ | -------------------------- | --------- | ----------------------------------------------------------- |
29
+ | `-c` or `--cache <number>` | 0 | Time in seconds for caching files |
30
+ | `-C` or `--cors` | false | Set CORS headers to \* to allow requests from any origin |
31
+ | `-d` or `--dirs` | true | List the directory's contents |
32
+ | `-h` or `--help` | | Display help instructions |
33
+ | `--host` | 127.0.0.1 | Change the default host address to bind to |
34
+ | `-H` or `--http2` | false | Enable HTTP/2 and SSL (https) |
35
+ | `-l` or `--logs` | false | Log HTTP requests at the prompt |
36
+ | `-o` or `--open` | false | Open in your default browser |
37
+ | `-p` or `--port <n>` | 8080 | Port to listen on - Will try next available if already used |
38
+ | `-g` or `--gzip` | true | Enable GZIP compression |
39
+ | `-v` or `--version` | | Output the current version |
39
40
 
40
41
  ## License
41
42
 
package/dist/parse.d.ts CHANGED
@@ -7,6 +7,7 @@ export type Flags = {
7
7
  logs?: boolean;
8
8
  open?: boolean;
9
9
  port?: number;
10
+ host?: string;
10
11
  };
11
12
  export type Parameters = {
12
13
  path?: string;
package/dist/parse.js CHANGED
@@ -3,6 +3,10 @@ import { parser } from "@node-cli/parser";
3
3
  export const config = parser({
4
4
  meta: import.meta,
5
5
  flags: {
6
+ host: {
7
+ description: "Host to listen on",
8
+ type: "string"
9
+ },
6
10
  cache: {
7
11
  shortFlag: "c",
8
12
  default: defaultFlags.cache,
package/dist/parse.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/parse.ts"],"sourcesContent":["import { defaultFlags, defaultParameters } from \"./defaults.js\";\n\nimport { parser } from \"@node-cli/parser\";\n\nexport type Flags = {\n\tcache?: number;\n\tcors?: boolean;\n\tdirs?: boolean;\n\tgzip?: boolean;\n\thttp2?: boolean;\n\tlogs?: boolean;\n\topen?: boolean;\n\tport?: number;\n};\n\nexport type Parameters = {\n\tpath?: string;\n};\n\nexport type Configuration = {\n\tflags?: Flags;\n\tparameters?: Parameters;\n\tusage?: boolean;\n\texamples?: string;\n};\n\nexport const config: Configuration = parser({\n\tmeta: import.meta,\n\tflags: {\n\t\tcache: {\n\t\t\tshortFlag: \"c\",\n\t\t\tdefault: defaultFlags.cache,\n\t\t\tdescription: \"Time in seconds for caching files\",\n\t\t\ttype: \"number\",\n\t\t},\n\t\tcors: {\n\t\t\tshortFlag: \"C\",\n\t\t\tdefault: defaultFlags.cors,\n\t\t\tdescription: \"Set CORS headers to * to allow requests from any origin\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tdirs: {\n\t\t\tshortFlag: \"d\",\n\t\t\tdefault: defaultFlags.dirs,\n\t\t\tdescription: \"List the directory's contents\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tgzip: {\n\t\t\tshortFlag: \"g\",\n\t\t\tdefault: defaultFlags.gzip,\n\t\t\tdescription: \"Enable GZIP compression\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\thelp: {\n\t\t\tshortFlag: \"h\",\n\t\t\tdescription: \"Display help instructions\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\thttp2: {\n\t\t\tshortFlag: \"H\",\n\t\t\tdefault: defaultFlags.http2,\n\t\t\tdescription: \"Use HTTP/2 and SSL (local certificate provided)\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tlogs: {\n\t\t\tshortFlag: \"l\",\n\t\t\tdefault: defaultFlags.logs,\n\t\t\tdescription: \"Log HTTP requests at the prompt\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\topen: {\n\t\t\tshortFlag: \"o\",\n\t\t\tdefault: defaultFlags.open,\n\t\t\tdescription: \"Open in your default browser\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tport: {\n\t\t\tshortFlag: \"p\",\n\t\t\tdefault: defaultFlags.port,\n\t\t\tdescription: \"Port to listen on\",\n\t\t\ttype: \"number\",\n\t\t},\n\t\tversion: {\n\t\t\tshortFlag: \"v\",\n\t\t\tdescription: \"Output the current version\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t},\n\tparameters: {\n\t\tpath: {\n\t\t\tdefault: \"current folder\",\n\t\t\tdescription: \"the path to serve files from\",\n\t\t},\n\t},\n\tusage: true,\n\tdefaultFlags,\n\tdefaultParameters,\n});\n"],"names":["defaultFlags","defaultParameters","parser","config","meta","flags","cache","shortFlag","default","description","type","cors","dirs","gzip","help","http2","logs","open","port","version","parameters","path","usage"],"mappings":"AAAA,SAASA,YAAY,EAAEC,iBAAiB,QAAQ,gBAAgB;AAEhE,SAASC,MAAM,QAAQ,mBAAmB;AAwB1C,OAAO,MAAMC,SAAwBD,OAAO;IAC3CE,MAAM;IACNC,OAAO;QACNC,OAAO;YACNC,WAAW;YACXC,SAASR,aAAaM;YACtBG,aAAa;YACbC,MAAM;QACP;QACAC,MAAM;YACLJ,WAAW;YACXC,SAASR,aAAaW;YACtBF,aAAa;YACbC,MAAM;QACP;QACAE,MAAM;YACLL,WAAW;YACXC,SAASR,aAAaY;YACtBH,aAAa;YACbC,MAAM;QACP;QACAG,MAAM;YACLN,WAAW;YACXC,SAASR,aAAaa;YACtBJ,aAAa;YACbC,MAAM;QACP;QACAI,MAAM;YACLP,WAAW;YACXE,aAAa;YACbC,MAAM;QACP;QACAK,OAAO;YACNR,WAAW;YACXC,SAASR,aAAae;YACtBN,aAAa;YACbC,MAAM;QACP;QACAM,MAAM;YACLT,WAAW;YACXC,SAASR,aAAagB;YACtBP,aAAa;YACbC,MAAM;QACP;QACAO,MAAM;YACLV,WAAW;YACXC,SAASR,aAAaiB;YACtBR,aAAa;YACbC,MAAM;QACP;QACAQ,MAAM;YACLX,WAAW;YACXC,SAASR,aAAakB;YACtBT,aAAa;YACbC,MAAM;QACP;QACAS,SAAS;YACRZ,WAAW;YACXE,aAAa;YACbC,MAAM;QACP;IACD;IACAU,YAAY;QACXC,MAAM;YACLb,SAAS;YACTC,aAAa;QACd;IACD;IACAa,OAAO;IACPtB;IACAC;AACD,GAAG"}
1
+ {"version":3,"sources":["../src/parse.ts"],"sourcesContent":["import { defaultFlags, defaultParameters } from \"./defaults.js\";\n\nimport { parser } from \"@node-cli/parser\";\n\nexport type Flags = {\n\tcache?: number;\n\tcors?: boolean;\n\tdirs?: boolean;\n\tgzip?: boolean;\n\thttp2?: boolean;\n\tlogs?: boolean;\n\topen?: boolean;\n\tport?: number;\n\thost?: string;\n};\n\nexport type Parameters = {\n\tpath?: string;\n};\n\nexport type Configuration = {\n\tflags?: Flags;\n\tparameters?: Parameters;\n\tusage?: boolean;\n\texamples?: string;\n};\n\nexport const config: Configuration = parser({\n\tmeta: import.meta,\n\tflags: {\n\t\thost: {\n\t\t\tdescription: \"Host to listen on\",\n\t\t\ttype: \"string\",\n\t\t},\n\t\tcache: {\n\t\t\tshortFlag: \"c\",\n\t\t\tdefault: defaultFlags.cache,\n\t\t\tdescription: \"Time in seconds for caching files\",\n\t\t\ttype: \"number\",\n\t\t},\n\t\tcors: {\n\t\t\tshortFlag: \"C\",\n\t\t\tdefault: defaultFlags.cors,\n\t\t\tdescription: \"Set CORS headers to * to allow requests from any origin\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tdirs: {\n\t\t\tshortFlag: \"d\",\n\t\t\tdefault: defaultFlags.dirs,\n\t\t\tdescription: \"List the directory's contents\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tgzip: {\n\t\t\tshortFlag: \"g\",\n\t\t\tdefault: defaultFlags.gzip,\n\t\t\tdescription: \"Enable GZIP compression\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\thelp: {\n\t\t\tshortFlag: \"h\",\n\t\t\tdescription: \"Display help instructions\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\thttp2: {\n\t\t\tshortFlag: \"H\",\n\t\t\tdefault: defaultFlags.http2,\n\t\t\tdescription: \"Use HTTP/2 and SSL (local certificate provided)\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tlogs: {\n\t\t\tshortFlag: \"l\",\n\t\t\tdefault: defaultFlags.logs,\n\t\t\tdescription: \"Log HTTP requests at the prompt\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\topen: {\n\t\t\tshortFlag: \"o\",\n\t\t\tdefault: defaultFlags.open,\n\t\t\tdescription: \"Open in your default browser\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tport: {\n\t\t\tshortFlag: \"p\",\n\t\t\tdefault: defaultFlags.port,\n\t\t\tdescription: \"Port to listen on\",\n\t\t\ttype: \"number\",\n\t\t},\n\t\tversion: {\n\t\t\tshortFlag: \"v\",\n\t\t\tdescription: \"Output the current version\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t},\n\tparameters: {\n\t\tpath: {\n\t\t\tdefault: \"current folder\",\n\t\t\tdescription: \"the path to serve files from\",\n\t\t},\n\t},\n\tusage: true,\n\tdefaultFlags,\n\tdefaultParameters,\n});\n"],"names":["defaultFlags","defaultParameters","parser","config","meta","flags","host","description","type","cache","shortFlag","default","cors","dirs","gzip","help","http2","logs","open","port","version","parameters","path","usage"],"mappings":"AAAA,SAASA,YAAY,EAAEC,iBAAiB,QAAQ,gBAAgB;AAEhE,SAASC,MAAM,QAAQ,mBAAmB;AAyB1C,OAAO,MAAMC,SAAwBD,OAAO;IAC3CE,MAAM;IACNC,OAAO;QACNC,MAAM;YACLC,aAAa;YACbC,MAAM;QACP;QACAC,OAAO;YACNC,WAAW;YACXC,SAASX,aAAaS;YACtBF,aAAa;YACbC,MAAM;QACP;QACAI,MAAM;YACLF,WAAW;YACXC,SAASX,aAAaY;YACtBL,aAAa;YACbC,MAAM;QACP;QACAK,MAAM;YACLH,WAAW;YACXC,SAASX,aAAaa;YACtBN,aAAa;YACbC,MAAM;QACP;QACAM,MAAM;YACLJ,WAAW;YACXC,SAASX,aAAac;YACtBP,aAAa;YACbC,MAAM;QACP;QACAO,MAAM;YACLL,WAAW;YACXH,aAAa;YACbC,MAAM;QACP;QACAQ,OAAO;YACNN,WAAW;YACXC,SAASX,aAAagB;YACtBT,aAAa;YACbC,MAAM;QACP;QACAS,MAAM;YACLP,WAAW;YACXC,SAASX,aAAaiB;YACtBV,aAAa;YACbC,MAAM;QACP;QACAU,MAAM;YACLR,WAAW;YACXC,SAASX,aAAakB;YACtBX,aAAa;YACbC,MAAM;QACP;QACAW,MAAM;YACLT,WAAW;YACXC,SAASX,aAAamB;YACtBZ,aAAa;YACbC,MAAM;QACP;QACAY,SAAS;YACRV,WAAW;YACXH,aAAa;YACbC,MAAM;QACP;IACD;IACAa,YAAY;QACXC,MAAM;YACLX,SAAS;YACTJ,aAAa;QACd;IACD;IACAgB,OAAO;IACPvB;IACAC;AACD,GAAG"}
package/dist/server.js CHANGED
@@ -91,7 +91,7 @@ fastify.setNotFoundHandler((request, reply)=>{
91
91
  });
92
92
  /**
93
93
  * Run the server!
94
- */ let port, portMessage = "";
94
+ */ let port, address, portMessage = "";
95
95
  const start = async ()=>{
96
96
  try {
97
97
  port = await portfinder.getPortPromise({
@@ -101,10 +101,17 @@ const start = async ()=>{
101
101
  portMessage = `\n\n${kleur.yellow(`Warning: port ${config.flags.port} was not available!`)}`;
102
102
  config.flags.port = port;
103
103
  }
104
- await fastify.listen({
104
+ const listenOptions = {
105
105
  port: config.flags.port
106
- });
107
- const url = `${config.flags.http2 ? "https" : "http"}://localhost:${config.flags.port}`;
106
+ };
107
+ if (config.flags.host) {
108
+ listenOptions.host = config.flags.host;
109
+ address = config.flags.host;
110
+ } else {
111
+ address = "localhost";
112
+ }
113
+ await fastify.listen(listenOptions);
114
+ const url = `${config.flags.http2 ? "https" : "http"}://${address}:${config.flags.port}`;
108
115
  const messages = [
109
116
  `${kleur.cyan("Static Server")} is up and running!`,
110
117
  "",
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/server.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { cert, key } from \"./certs.js\";\nimport fastifyStatic, { FastifyStaticOptions } from \"@fastify/static\";\n\nimport Fastify from \"fastify\";\nimport { Logger } from \"@node-cli/logger\";\nimport { config } from \"./parse.js\";\nimport fastifyCache from \"@fastify/caching\";\nimport fastifyCompress from \"@fastify/compress\";\nimport fastifyCors from \"@fastify/cors\";\nimport fastifyLogs from \"./logs.js\";\nimport fs from \"fs-extra\";\nimport kleur from \"kleur\";\nimport open from \"open\";\nimport path from \"node:path\";\nimport portfinder from \"portfinder\";\nimport { renderDirectories } from \"./templates/directories.js\";\nimport { renderNotFound } from \"./templates/not-found.js\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nlet customPath = config.parameters[0];\nif (fs.pathExistsSync(customPath)) {\n\tcustomPath = path.resolve(customPath);\n} else {\n\tlogger.printErrorsAndExit([`Folder ${customPath} does not exist!`], 0);\n}\n\nconst fastifyOptions: {\n\tdisableRequestLogging?: boolean;\n\thttp2?: boolean;\n\thttps?: any;\n\tlogger?: any;\n} = {\n\tdisableRequestLogging: true,\n};\n\nif (config.flags.logs) {\n\tfastifyOptions.logger = {\n\t\tlevel: \"info\",\n\t\ttransport: {\n\t\t\ttarget: \"pino-pretty\",\n\t\t\toptions: {\n\t\t\t\thideObject: true,\n\t\t\t\ttranslateTime: \"SYS:standard\",\n\t\t\t\tignore: \"pid,hostname,reqId,resTime,resTimeMs,level\",\n\t\t\t},\n\t\t},\n\t};\n}\n\nif (config.flags.http2) {\n\tfastifyOptions.http2 = true;\n\tfastifyOptions.https = { key, cert };\n}\n\nconst fastify = Fastify(fastifyOptions);\n\nif (config.flags.logs) {\n\tfastify.register(fastifyLogs);\n}\n\nif (config.flags.gzip) {\n\tfastify.register(fastifyCompress, {\n\t\tglobal: true,\n\t\tencodings: [\"gzip\", \"deflate\", \"br\", \"identity\"],\n\t});\n}\n\nif (config.flags.cors) {\n\tfastify.register(fastifyCors);\n}\n\nconst fastifyCacheOptions: {\n\texpiresIn?: number;\n\tserverExpiresIn?: number;\n\tprivacy?: any;\n} = {};\n\nif (config.flags.cache > 0) {\n\tfastifyCacheOptions.expiresIn = config.flags.cache;\n\tfastifyCacheOptions.serverExpiresIn = config.flags.cache;\n\tfastifyCacheOptions.privacy = \"public\";\n} else {\n\tfastifyCacheOptions.privacy = \"no-cache\";\n}\n\nfastify.register(fastifyCache, fastifyCacheOptions);\n\nconst staticOptions: FastifyStaticOptions = {\n\troot: customPath,\n};\nif (config.flags.dirs) {\n\tstaticOptions.list = {\n\t\tformat: \"html\",\n\t\trender: renderDirectories,\n\t};\n}\nfastify.register(fastifyStatic, staticOptions);\n\nfastify.setNotFoundHandler((request, reply) => {\n\treply.code(404).type(\"text/html\").send(renderNotFound(config.flags.dirs));\n});\n\n/**\n * Run the server!\n */\nlet port: number,\n\tportMessage = \"\";\nconst start = async () => {\n\ttry {\n\t\tport = await portfinder.getPortPromise({ port: Number(config.flags.port) });\n\t\tif (port !== config.flags.port) {\n\t\t\tportMessage = `\\n\\n${kleur.yellow(\n\t\t\t\t`Warning: port ${config.flags.port} was not available!`\n\t\t\t)}`;\n\t\t\tconfig.flags.port = port;\n\t\t}\n\t\tawait fastify.listen({ port: config.flags.port });\n\n\t\tconst url = `${config.flags.http2 ? \"https\" : \"http\"}://localhost:${\n\t\t\tconfig.flags.port\n\t\t}`;\n\t\tconst messages = [\n\t\t\t`${kleur.cyan(\"Static Server\")} is up and running!`,\n\t\t\t\"\",\n\t\t\t`${kleur.cyan(url)}`,\n\t\t\t\"\",\n\t\t\t`Hit CTRL+C to shut it down.${portMessage}`,\n\t\t];\n\n\t\tlogger.printBox(messages, { newLineAfter: false });\n\n\t\tif (config.flags.open) {\n\t\t\tawait open(url, {\n\t\t\t\twait: false,\n\t\t\t});\n\t\t}\n\t} catch (error) {\n\t\tfastify.log.error(error);\n\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\tprocess.exit(1);\n\t}\n};\nstart();\n"],"names":["cert","key","fastifyStatic","Fastify","Logger","config","fastifyCache","fastifyCompress","fastifyCors","fastifyLogs","fs","kleur","open","path","portfinder","renderDirectories","renderNotFound","logger","boring","process","env","NODE_ENV","customPath","parameters","pathExistsSync","resolve","printErrorsAndExit","fastifyOptions","disableRequestLogging","flags","logs","level","transport","target","options","hideObject","translateTime","ignore","http2","https","fastify","register","gzip","global","encodings","cors","fastifyCacheOptions","cache","expiresIn","serverExpiresIn","privacy","staticOptions","root","dirs","list","format","render","setNotFoundHandler","request","reply","code","type","send","port","portMessage","start","getPortPromise","Number","yellow","listen","url","messages","cyan","printBox","newLineAfter","wait","error","log","exit"],"mappings":";AAEA,SAASA,IAAI,EAAEC,GAAG,QAAQ,aAAa;AACvC,OAAOC,mBAA6C,kBAAkB;AAEtE,OAAOC,aAAa,UAAU;AAC9B,SAASC,MAAM,QAAQ,mBAAmB;AAC1C,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,kBAAkB,mBAAmB;AAC5C,OAAOC,qBAAqB,oBAAoB;AAChD,OAAOC,iBAAiB,gBAAgB;AACxC,OAAOC,iBAAiB,YAAY;AACpC,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,WAAW,QAAQ;AAC1B,OAAOC,UAAU,OAAO;AACxB,OAAOC,UAAU,YAAY;AAC7B,OAAOC,gBA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AAK;IACd;AACD;AACAb"}
1
+ {"version":3,"sources":["../src/server.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { cert, key } from \"./certs.js\";\nimport fastifyStatic, { FastifyStaticOptions } from \"@fastify/static\";\n\nimport Fastify from \"fastify\";\nimport { Logger } from \"@node-cli/logger\";\nimport { config } from \"./parse.js\";\nimport fastifyCache from \"@fastify/caching\";\nimport fastifyCompress from \"@fastify/compress\";\nimport fastifyCors from \"@fastify/cors\";\nimport fastifyLogs from \"./logs.js\";\nimport fs from \"fs-extra\";\nimport kleur from \"kleur\";\nimport open from \"open\";\nimport path from \"node:path\";\nimport portfinder from \"portfinder\";\nimport { renderDirectories } from \"./templates/directories.js\";\nimport { renderNotFound } from \"./templates/not-found.js\";\n\nexport const logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nlet customPath = config.parameters[0];\nif (fs.pathExistsSync(customPath)) {\n\tcustomPath = path.resolve(customPath);\n} else {\n\tlogger.printErrorsAndExit([`Folder ${customPath} does not exist!`], 0);\n}\n\nconst fastifyOptions: {\n\tdisableRequestLogging?: boolean;\n\thttp2?: boolean;\n\thttps?: any;\n\tlogger?: any;\n} = {\n\tdisableRequestLogging: true,\n};\n\nif (config.flags.logs) {\n\tfastifyOptions.logger = {\n\t\tlevel: \"info\",\n\t\ttransport: {\n\t\t\ttarget: \"pino-pretty\",\n\t\t\toptions: {\n\t\t\t\thideObject: true,\n\t\t\t\ttranslateTime: \"SYS:standard\",\n\t\t\t\tignore: \"pid,hostname,reqId,resTime,resTimeMs,level\",\n\t\t\t},\n\t\t},\n\t};\n}\n\nif (config.flags.http2) {\n\tfastifyOptions.http2 = true;\n\tfastifyOptions.https = { key, cert };\n}\n\nconst fastify = Fastify(fastifyOptions);\n\nif (config.flags.logs) {\n\tfastify.register(fastifyLogs);\n}\n\nif (config.flags.gzip) {\n\tfastify.register(fastifyCompress, {\n\t\tglobal: true,\n\t\tencodings: [\"gzip\", \"deflate\", \"br\", \"identity\"],\n\t});\n}\n\nif (config.flags.cors) {\n\tfastify.register(fastifyCors);\n}\n\nconst fastifyCacheOptions: {\n\texpiresIn?: number;\n\tserverExpiresIn?: number;\n\tprivacy?: any;\n} = {};\n\nif (config.flags.cache > 0) {\n\tfastifyCacheOptions.expiresIn = config.flags.cache;\n\tfastifyCacheOptions.serverExpiresIn = config.flags.cache;\n\tfastifyCacheOptions.privacy = \"public\";\n} else {\n\tfastifyCacheOptions.privacy = \"no-cache\";\n}\n\nfastify.register(fastifyCache, fastifyCacheOptions);\n\nconst staticOptions: FastifyStaticOptions = {\n\troot: customPath,\n};\nif (config.flags.dirs) {\n\tstaticOptions.list = {\n\t\tformat: \"html\",\n\t\trender: renderDirectories,\n\t};\n}\nfastify.register(fastifyStatic, staticOptions);\n\nfastify.setNotFoundHandler((request, reply) => {\n\treply.code(404).type(\"text/html\").send(renderNotFound(config.flags.dirs));\n});\n\n/**\n * Run the server!\n */\nlet port: number,\n\taddress: string,\n\tportMessage = \"\";\nconst start = async () => {\n\ttry {\n\t\tport = await portfinder.getPortPromise({ port: Number(config.flags.port) });\n\t\tif (port !== config.flags.port) {\n\t\t\tportMessage = `\\n\\n${kleur.yellow(\n\t\t\t\t`Warning: port ${config.flags.port} was not available!`\n\t\t\t)}`;\n\t\t\tconfig.flags.port = port;\n\t\t}\n\t\tconst listenOptions: { port?: number; host?: string } = {\n\t\t\tport: config.flags.port,\n\t\t};\n\t\tif (config.flags.host) {\n\t\t\tlistenOptions.host = config.flags.host;\n\t\t\taddress = config.flags.host;\n\t\t} else {\n\t\t\taddress = \"localhost\";\n\t\t}\n\t\tawait fastify.listen(listenOptions);\n\n\t\tconst url = `${config.flags.http2 ? \"https\" : \"http\"}://${address}:${\n\t\t\tconfig.flags.port\n\t\t}`;\n\t\tconst messages = [\n\t\t\t`${kleur.cyan(\"Static Server\")} is up and running!`,\n\t\t\t\"\",\n\t\t\t`${kleur.cyan(url)}`,\n\t\t\t\"\",\n\t\t\t`Hit CTRL+C to shut it down.${portMessage}`,\n\t\t];\n\n\t\tlogger.printBox(messages, { newLineAfter: false });\n\n\t\tif (config.flags.open) {\n\t\t\tawait open(url, {\n\t\t\t\twait: false,\n\t\t\t});\n\t\t}\n\t} catch (error) {\n\t\tfastify.log.error(error);\n\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\tprocess.exit(1);\n\t}\n};\nstart();\n"],"names":["cert","key","fastifyStatic","Fastify","Logger","config","fastifyCache","fastifyCompress","fastifyCors","fastifyLogs","fs","kleur","open","path","portfinder","renderDirectories","renderNotFound","logger","boring","process","env","NODE_ENV","customPath","parameters","pathExistsSync","resolve","printErrorsAndExit","fastifyOptions","disableRequestLogging","flags","logs","level","transport","target","options","hideObject","translateTime","ignore","http2","https","fastify","register","gzip","global","encodings","cors","fastifyCacheOptions","cache","expiresIn","serverExpiresIn","privacy","staticOptions","root","dirs","list","format","render","setNotFoundHandler","request","reply","code","type","send","port","address","portMessage","start","getPortPromise","Number","yellow","listenOptions","host","listen","url","messages","cyan","printBox","newLineAfter","wait","error","log","exit"],"mappings":";AAEA,SAASA,IAAI,EAAEC,GAAG,QAAQ,aAAa;AACvC,OAAOC,mBAA6C,kBAAkB;AAEtE,OAAOC,aAAa,UAAU;AAC9B,SAASC,MAAM,QAAQ,mBAAmB;AAC1C,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,kBAAkB,mBAAmB;AAC5C,OAAOC,qBAAqB,oBAAoB;AAChD,OAAOC,iBAAiB,gBAAgB;AACxC,OAAOC,iBAAiB,YAAY;AACpC,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,WAAW,QAAQ;AAC1B,OAAOC,UAAU,OAAO;AACxB,OAAOC,UAAU,YAAY;AAC7B,OAAOC,gBAAgB,aAAa;AACpC,SAASC,iBAAiB,QAAQ,6BAA6B;AAC/D,SAASC,cAAc,QAAQ,2BAA2B;AAE1D,OAAO,MAAMC,SAAS,IAAIb,OAAO;IAChCc,QAAQC,QAAQC,IAAIC,aAAa;AAClC,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mD;QACnD5D,QAAQ8D,KAAK;IACd;AACD;AACAf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-cli/static-server",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "description": "A simple, zero-configuration, command line HTTP server to serve static files locally",
@@ -41,5 +41,5 @@
41
41
  "publishConfig": {
42
42
  "access": "public"
43
43
  },
44
- "gitHead": "f70aec7d6e950b8c76048917a1fcb01c90a0beb3"
44
+ "gitHead": "6578330199496d4e9a5e2c63e09e8832151a24fe"
45
45
  }