node-tool-utils
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -0,1 +1,10 @@ | ||
# [1.3.0](https://github.com/hubcarl/node-tool-utils/compare/1.2.3...1.3.0) (2019-09-07) | ||
### Features | ||
* support http server file download and file content read and dir display tree ([9c24359](https://github.com/hubcarl/node-tool-utils/commit/9c24359)) | ||
## [1.2.3](https://github.com/hubcarl/node-tool-utils/compare/1.2.2...1.2.3) (2019-08-01) | ||
@@ -2,0 +11,0 @@ |
@@ -63,2 +63,4 @@ 'use strict'; | ||
index = files[0]; | ||
} else { | ||
index = '/' | ||
} | ||
@@ -73,2 +75,27 @@ } | ||
}; | ||
httpserver.beforeServe = (request, response, body, encoding) => { | ||
['md', 'lock', 'log', 'ts', 'jsx', 'tsx', 'tpl', 'sql'].forEach(ext => { | ||
httpserver.config.contentType[ext] = 'text/plain'; | ||
}); | ||
'mp4|webm|ogg|mp3|wav|flac|aac|zip|tar|tar.gz|tar.xz|doc|pdf|xls|msi|7z|pkg|ipa|woff|woff2|eot|ttf|otf'.split('|').forEach(v => { | ||
httpserver.config.contentType[v] = 'application/octet-stream'; | ||
}); | ||
if (response.statusCode === 415) { | ||
const pathname = request.uri.pathname; | ||
const filepath = path.join(root, pathname.replace(/^\//, '')); | ||
if (fs.existsSync(filepath)) { | ||
if (fs.statSync(filepath).isDirectory()) { | ||
response.setHeader('Content-Type', httpserver.config.contentType.html); | ||
response.statusCode = 200; | ||
const fileList = exports.getFileList(root, pathname); | ||
const header =`<h1>Index of ${pathname}</h1><hr>`; | ||
const content = exports.createHTMLFileTree(fileList); | ||
body.value = `<div style="margin-left:10px">${header}${content}</div>`; | ||
} else { | ||
body.value = fs.readFileSync(filepath); | ||
} | ||
return; | ||
} | ||
} | ||
}; | ||
httpserver.deploy(options, server => { | ||
@@ -82,2 +109,40 @@ const url = `http://127.0.0.1:${server.config.port}`; | ||
exports.getFileList = (root, pathname) => { | ||
const filename = pathname.replace(/^\//, ''); | ||
const rootFilePath = path.join(root, filename); | ||
if (fs.existsSync(rootFilePath)) { | ||
if (fs.statSync(rootFilePath).isDirectory()) { | ||
const fileList = fs.readdirSync(rootFilePath); | ||
const dirs = []; | ||
const files = []; | ||
fileList.forEach(file => { | ||
const tmpfilepath = path.join(rootFilePath, file); | ||
if (fs.existsSync(tmpfilepath)) { | ||
if (fs.statSync(tmpfilepath).isDirectory()) { | ||
dirs.push(tmpfilepath); | ||
} else { | ||
files.push(tmpfilepath); | ||
} | ||
} | ||
}); | ||
return dirs.concat(files).map(file => { | ||
const pathname = file.replace(root, ''); | ||
return { | ||
pathname, | ||
filepath: file, | ||
filename: path.basename(file), | ||
}; | ||
}); | ||
} | ||
return [{ filename, pathname, filepath: rootFilePath }]; | ||
} | ||
return []; | ||
}; | ||
exports.createHTMLFileTree = fileList => { | ||
return fileList.reduce((html, item)=> { | ||
return `${html}<a href="${item.pathname}">${item.filename}</a><br/>` | ||
}, ""); | ||
}; | ||
exports.exec = cmd => { | ||
@@ -84,0 +149,0 @@ return shell.exec(cmd); |
{ | ||
"name": "node-tool-utils", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "node cross-platform tool library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15501
300