Skip to content

Commit 66aeb03

Browse files
committed
feat(debug): debug package was added and was exposed through API
BREAKING CHANGE: The first non-default `export` (`export {debug}`) was added. It causes `default export` to be accessible under `.default`: `const StaticServer = require('simplatic-http-server').default`
1 parent e524050 commit 66aeb03

File tree

5 files changed

+94
-37
lines changed

5 files changed

+94
-37
lines changed

mocha/e2e.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
const puppeteer = require('puppeteer')
88
const {expect} = require('chai')
9-
const StaticServer = require('../dist/main.umd.js')
9+
const StaticServer = require('../dist/main.umd.js').default
10+
// const staticServerDebug = require('../dist/main.umd.js').debug
11+
// staticServerDebug.enable('simplatic-http-server:IIIWE*')
1012

1113
const PORT = 3000
1214
const PORT2 = 3001
@@ -35,7 +37,6 @@ before(async () => {
3537
}, reject)
3638
})
3739

38-
// noinspection JSCheckFunctionSignatures
3940
await Promise.all([staticServer.listen(), staticServer2.listen(), launchPuppeteerPromise])
4041
})
4142

@@ -75,6 +76,5 @@ describe('End-to-end tests using puppeteer', () => {
7576
})
7677

7778
after(async () => {
78-
// noinspection JSCheckFunctionSignatures
7979
await Promise.all([browser.close(), staticServer2.shutdown(), staticServer.shutdown()])
8080
})

package-lock.json

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simplatic-http-server",
3-
"version": "0.0.4",
3+
"version": "1.0.0",
44
"description": "A very light-weight and very simple static HTTP server based on node's built-in http module",
55
"main": "dist/main.umd.js",
66
"scripts": {
@@ -37,7 +37,7 @@
3737
],
3838
"check-coverage": true,
3939
"branches": 90,
40-
"functions": 90,
40+
"functions": 95,
4141
"lines": 90,
4242
"statements": 90,
4343
"temp-dir": "./coverage/.nyc_output"
@@ -71,6 +71,7 @@
7171
},
7272
"//": "devDependencies: `@types/...` is needed to be in dependencies due to some notes in IntelliJ IDEs. See: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206324989/comments/360000603579",
7373
"devDependencies": {
74+
"@types/debug": "^4.1.4",
7475
"@types/chai": "^4.1.7",
7576
"@types/mocha": "^5.2.7",
7677
"@types/node": "^12.0.10",
@@ -85,6 +86,7 @@
8586
"rimraf": "^2.6.3",
8687
"rollup": "^1.12.2",
8788
"rollup-plugin-commonjs": "^10.0.0",
89+
"rollup-plugin-replace": "^2.2.0",
8890
"rollup-plugin-sourcemaps": "^0.4.2",
8991
"rollup-plugin-terser": "^5.0.0",
9092
"rollup-plugin-typescript2": "^0.21.1",
@@ -93,5 +95,7 @@
9395
"tslint-config-standard": "^8.0.1",
9496
"typescript": "^3.5.2"
9597
},
96-
"dependencies": {}
98+
"dependencies": {
99+
"debug": "^4.1.1"
100+
}
97101
}

rollup.config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import sourceMaps from 'rollup-plugin-sourcemaps'
66
import {terser} from 'rollup-plugin-terser'
77
import typescript from 'rollup-plugin-typescript2'
88
import builtins from 'builtin-modules/static'
9+
import replace from 'rollup-plugin-replace'
910

1011
const pkg = require('./package.json')
1112

@@ -17,20 +18,29 @@ const uglify = process.env.UGLIFY
1718

1819
// https://stackoverflow.com/a/56114625/5318303
1920

20-
const externals = [...builtins, ...Object.keys(pkg.dependencies || {})]
21+
const externals = [...builtins, ...Object.keys(pkg.dependencies)]
2122

2223
const globals = {}
2324
externals.map(key => globals[key] = /[-.]/.test(key) ? camelCase(key) : key)
2425

2526
// noinspection JSUnusedGlobalSymbols
2627
export default {
2728
input: `src/main.ts`,
28-
output: {format: 'umd', file: pkg.main, name: libVarName, sourcemap: true, globals: globals},
29+
output: {format: 'umd', file: pkg.main, name: libVarName, sourcemap: true, exports: 'named', globals: globals},
2930
watch: {include: 'src/**'},
3031

3132
external: externals,
3233

3334
plugins: [
35+
replace({
36+
include: [
37+
'src/**/*.ts',
38+
],
39+
delimiters: ['<@', '@>'],
40+
values: {
41+
MODULE_NAME: libraryName, //JSON.stringify(libraryName)
42+
},
43+
}),
3444
commonjs(),
3545
typescript({
3646
useTsconfigDeclarationDir: true,

src/main.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from 'path'
33
import url from 'url'
44
import http from 'http'
55

6+
import debug from 'debug'
7+
68
/**
79
* Created at 1398/4/2 (2019/6/23).
810
* @author {@link https://mirismaili.github.io S. Mahdi Mir-Ismaili}
@@ -12,45 +14,40 @@ export default class StaticServer {
1214

1315
constructor(public readonly port: number, public readonly servePath = '') {
1416
this.staticServer = http.createServer((req, res) => {
15-
//console.debug(req.method + ' ' + req.url)
17+
dbg('%s %s', req.method, req.url)
1618

1719
try {
1820
const uri = url.parse(req.url!).pathname!
1921
let filePath = path.join(servePath === '' ? process.cwd() : servePath, uri)!
2022

21-
e404:{
22-
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) break e404
23-
//console.debug(filePath)
24-
25-
const contentTypesByExtension: { [key: string]: string } = {
26-
'.html': 'text/html',
27-
'.css': 'text/css',
28-
'.js': 'text/javascript'
29-
}
30-
31-
const data = fs.readFileSync(filePath, 'binary')
32-
33-
const headers: { [key: string]: string } = {}
34-
const contentType = contentTypesByExtension[path.extname(filePath)]
35-
if (contentType !== undefined) headers['Content-Type'] = contentType
36-
res.writeHead(200, headers)
37-
res.write(data, 'binary')
23+
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) { // 404:
24+
err('File not found: %s', filePath)
25+
res.writeHead(404, {'Content-Type': 'text/plain'})
26+
res.write('404/ Not Found\n')
3827
res.end()
39-
4028
return
4129
}
42-
//----------------------------------------------------------/e404:
4330

44-
//console.error(`File not found: "${filePath}"`)
45-
// noinspection UnreachableCodeJS
46-
res.writeHead(404, {'Content-Type': 'text/plain'})
47-
res.write('404/ Not Found\n')
48-
res.end()
31+
dbg(filePath)
32+
33+
const contentTypesByExtension: { [key: string]: string } = {
34+
'.html': 'text/html',
35+
'.css': 'text/css',
36+
'.js': 'text/javascript'
37+
}
4938

50-
} catch (exeption) {
51-
console.error(exeption.stack)
39+
const data = fs.readFileSync(filePath, 'binary')
40+
41+
const headers: { [key: string]: string } = {}
42+
const contentType = contentTypesByExtension[path.extname(filePath)]
43+
if (contentType !== undefined) headers['Content-Type'] = contentType
44+
res.writeHead(200, headers)
45+
res.write(data, 'binary')
46+
res.end()
47+
} catch (exception) {
48+
console.error(exception.stack)
5249
res.writeHead(500, {'Content-Type': 'text/plain'})
53-
res.write(exeption.toString())
50+
res.write(exception.toString())
5451
res.end()
5552
}
5653
})
@@ -71,3 +68,20 @@ export default class StaticServer {
7168
)
7269
}
7370
}
71+
//*****************************************************************************************/
72+
73+
const getDebugger = (namespace: string): debug.Debugger => debug(`<@MODULE_NAME@>:${namespace}`)
74+
75+
const trc = getDebugger('I')
76+
const dbg = getDebugger('II')
77+
const inf = getDebugger('III')
78+
const wrn = getDebugger('IIIW')
79+
const err = getDebugger('IIIWE')
80+
const ftl = getDebugger('IIIWEF')
81+
const log = getDebugger('II') // Default level
82+
83+
//debug.log = console.debug.bind(console)
84+
85+
//debug.formatters.c = (f: () => string) => f()
86+
87+
export {debug}

0 commit comments

Comments
 (0)