Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rdilweb/static-server
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.7.0
Choose a base ref
...
head repository: rdilweb/static-server
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 1,074 additions and 2,469 deletions.
  1. +0 −3 .babelrc
  2. +8 −0 .cirrus.yml
  3. +25 −0 .eslintrc.yml
  4. +1 −1 .gitignore
  5. +1 −1 .prettierignore
  6. +1 −1 README.md
  7. +24 −19 package.json
  8. +0 −20 source/headers.js
  9. +23 −0 source/headers.ts
  10. +0 −98 source/index.js
  11. +84 −0 source/index.ts
  12. +10 −10 source/{markdownRendering.js → markdownRendering.ts}
  13. +0 −90 source/serverHandling.js
  14. +132 −0 source/serverHandling.ts
  15. +0 −23 source/utilities.js
  16. +27 −0 tsconfig.json
  17. +738 −2,203 yarn.lock
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

8 changes: 8 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Lint_task:
container:
image: node:latest
dependencies_cache:
folder: node_modules
populate_script: yarn install
fingerprint_script: cat yarn.lock
eslint_script: yarn test
25 changes: 25 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins:
- prettier
- "@typescript-eslint"

parser: "@typescript-eslint/parser"

env:
node: true
es6: true

extends:
- "eslint:recommended"
- "plugin:@typescript-eslint/recommended"

globals:
Atomics: readonly
SharedArrayBuffer: readonly

parserOptions:
ecmaVersion: 2020
sourceType: module

rules: {
"prettier/prettier": "error"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -8,4 +8,4 @@ results
out

node_modules
src
build
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
src
build
node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Static Server

A simple, static web server.
A simple but powerful static web server.

Please see [the doumentation](https://docs.rdil.rocks/libraries/static-server-rdil/) for usage information.
43 changes: 24 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "static-server-rdil",
"version": "1.7.0",
"version": "3.1.1",
"description": "Static web server",
"homepage": "https://github.com/rdilweb/static-server",
"bin": {
"static-server": "./src/index.js"
"static-server-rdil": "./build/source/index.js"
},
"keywords": [
"server",
@@ -15,34 +15,36 @@
"rdilweb"
],
"dependencies": {
"chalk": "^3.0.0",
"mime-types": "^2.1.26",
"node-emoji": "^1.10.0",
"showdown": "^1.9.1"
"chalk": "^4.1.1",
"commander": "^9.4.1",
"mime-types": "^2.1.35",
"showdown": "^2.1.0"
},
"files": [
"src"
"build"
],
"license": "MIT",
"author": "Reece Dunham <me@rdil.rocks>",
"bugs": {
"url": "https://github.com/rdilweb/static-server/issues"
},
"main": "src/index.js",
"main": "build/source/index.js",
"scripts": {
"prettier": "prettier --write **/**.{js,json}",
"build": "babel source -d src",
"dev": "yarn build && node src/index.js"
"prettier": "prettier --write **/**.{js,ts,json}",
"build": "tsc",
"dev": "yarn build && node build/source/index.js",
"test": "eslint source/*.ts"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@types/mime-types": "^2.1.0",
"@types/node": "^13.5.2",
"@types/node-emoji": "^1.8.1",
"@types/showdown": "^1.9.3",
"prettier": "^1.19.1"
"@types/mime-types": "^2.1.1",
"@types/node": "^18.11.0",
"@types/showdown": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"typescript": "^4.9.3"
},
"prettier": {
"semi": false,
@@ -51,5 +53,8 @@
"repository": {
"type": "git",
"url": "https://github.com/rdilweb/static-server.git"
},
"engines": {
"node": ">=10.13.0"
}
}
20 changes: 0 additions & 20 deletions source/headers.js

This file was deleted.

23 changes: 23 additions & 0 deletions source/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OutgoingHttpHeaders } from "http"

export interface KnownHeaders extends OutgoingHttpHeaders {
"Content-Type": string
"X-Content-Type-Options"?: string
"X-Download-Options"?: string
"X-DNS-Prefetch-Control"?: string
"Referrer-Policy"?: string
}

export default (enhancedSecurity: boolean, mimetype: string): KnownHeaders => {
return !enhancedSecurity
? {
"Content-Type": mimetype,
}
: {
"Content-Type": mimetype,
"X-Content-Type-Options": "nosniff",
"X-Download-Options": "noopen",
"X-DNS-Prefetch-Control": "off",
"Referrer-Policy": "no-referrer",
}
}
98 changes: 0 additions & 98 deletions source/index.js

This file was deleted.

84 changes: 84 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env node

import { join } from "path"
import { createServer } from "http"
import { parse } from "url"
import chalk from "chalk"
import handle from "./serverHandling"
import { program } from "commander"

/**
* The root directory the server will serve.
*/
let root = process.cwd()

/**
* If requests should be logged in the console.
*/
let logRequests = true

/**
* If enhanced security headers should be set.
*/
let enhancedSecurity = false

/**
* If Markdown should be rendered (in HTML).
*/
let renderMarkdown = true

program.name("static-server-rdil")

program
.option("--port <number>", "the port to use", "3000")
.option("--root <string>", "the root path of the file tree to serve")
.option("--enhanced-security", "if enhanced security should be used")
.option(
"--no-request-logging",
"if requests to specific resources should be silenced"
)
.option("--no-render-markdown", "don't render markdown files as HTML")

const opts = program.parse(process.argv).opts()

/**
* The port that the server will run on.
*/
const port: number = Number.parseInt(opts.port)

if (opts.root) {
root = opts.root
}

if (opts.noRenderMarkdown) {
renderMarkdown = false
}

if (opts.enhancedSecurity) {
enhancedSecurity = true
}

if (opts.noRequestLogging) {
logRequests = false
}

/**
* The server object.
*/
const server = createServer((request, response) => {
const uriPath = parse(request.url as string).pathname as string
const filePath = join(root, unescape(uriPath))

if (logRequests) {
console.log("Serving " + uriPath)
}

handle(filePath, response, enhancedSecurity, renderMarkdown)
})

server.listen(port)

console.log(chalk`
Using working directory ${root}.
{magenta {bold Server running at http://localhost:${port}/ ⭐}}
`)
Loading