Skip to content

Commit 4f54110

Browse files
committed
feat: TypeScript support
1 parent 60ce22c commit 4f54110

File tree

14 files changed

+1333
-305
lines changed

14 files changed

+1333
-305
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
coverage
44

55
.idea
6+
dist

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ benchmark
33
coverage
44
.nyc_output
55
rollup.config.js
6+
*.d.ts.map

bin/cli.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ if (!args) { // cliBasics handled
2121

2222
const dir = args.directory || '.';
2323

24+
/**
25+
* @param {http.IncomingMessage} request
26+
* @param {http.ServerResponse<http.IncomingMessage> & {
27+
* req: http.IncomingMessage;
28+
* }} response
29+
* @param {number} [statusCode]
30+
*/
2431
const log = function(request, response, statusCode) {
2532
const d = new Date();
2633
/* c8 ignore next 3 -- Time-dependent */
@@ -31,10 +38,12 @@ const log = function(request, response, statusCode) {
3138

3239
line = datestr + ' [' + response.statusCode + ']: ' + request.url;
3340
let colorized = line;
34-
/* c8 ignore next 5 -- Environment */
41+
/* c8 ignore next 7 -- Environment */
3542
if (tty.isatty(process.stdout.fd)) {
36-
colorized = (response.statusCode >= 500) ? colors.red.bold(line) :
37-
(response.statusCode >= 400) ? colors.red(line) :
43+
colorized = (response.statusCode >= 500)
44+
// @ts-expect-error TS error
45+
? colors.red.bold(line)
46+
: (response.statusCode >= 400) ? colors.red(line) :
3847
line;
3948
}
4049
console.log(colorized);
@@ -51,7 +60,10 @@ if (args['headers']) {
5160
}
5261

5362
if (args['header-file']) {
54-
options.headers = JSON.parse(fs.readFileSync(args['header-file']));
63+
options.headers = JSON.parse(
64+
// @ts-expect-error Works fine
65+
fs.readFileSync(args['header-file'])
66+
);
5567
}
5668

5769
if (args['gzip']) {
@@ -66,6 +78,10 @@ const file = new(statik.Server)(dir, options);
6678

6779
const server = http.createServer(function (request, response) {
6880
request.addListener('end', function () {
81+
/**
82+
* @param {null|import('../lib/node-static.js').ResultInfo} e
83+
* @param {import('../lib/node-static.js').ResultInfo} [rsp]
84+
*/
6985
const callback = function(e, rsp) {
7086
if (e && e.status === 404) {
7187
response.writeHead(e.status, e.headers);
@@ -76,6 +92,11 @@ const server = http.createServer(function (request, response) {
7692
}
7793
};
7894

95+
/* c8 ignore next 3 -- TS guard */
96+
if (typeof request.url !== 'string') {
97+
return;
98+
}
99+
79100
// Parsing catches:
80101
// npm start -- --spa --index-file test/fixtures/there/index.html
81102
// with http://127.0.0.1:8080/test/fixtures/there?email=john.cena

bin/optionDefinitions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {readFileSync} from 'fs';
22

33
const pkg = JSON.parse(
4+
// @ts-expect-error Works fine
45
readFileSync(new URL('../package.json', import.meta.url))
56
);
67

0 commit comments

Comments
 (0)