Skip to content

Remove node-static dependency from testkit-backend #1125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 0 additions & 97 deletions packages/testkit-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/testkit-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"neo4j-driver": "5.0.0-dev",
"neo4j-driver-lite": "5.0.0-dev",
"node-static": "^0.7.11",
"ws": "^8.11.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions packages/testkit-backend/src/controller/remote.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Controller from './interface'
import { WebSocketServer } from 'ws'
import { createServer } from 'http'
import { Server } from 'node-static'
import { HttpStaticServer } from '../infrastructure'

/**
* RemoteController handles the requests by sending them a remote client.
Expand All @@ -14,7 +14,9 @@ import { Server } from 'node-static'
export default class RemoteController extends Controller {
constructor (port) {
super()
this._staticServer = new Server('./public')
this._staticServer = new HttpStaticServer({
basePath: './public'
})
this._port = port
this._wss = null
this._ws = null
Expand Down
67 changes: 67 additions & 0 deletions packages/testkit-backend/src/infrastructure/http-static.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs from 'fs'

const DEFAULT_MIME_TYPES = {
html: 'text/html',
js: 'text/javascript'
}

const DEFAULT_MIME_TYPE = 'text/plain'

const DEFAULT_INDEX_FILE = '/index.html'

/**
* This a very minimal implementation of a static files http server.
*
* This is intend to be used only in the ${@link RemoteController} scenario and
* this is not feature complete.
*
* Some security measures to avoid external elements to sniff to files out of the base
* path are in place. Like Directory Transversal.
*/
export class HttpStaticServer {
constructor ({
basePath,
mimeTypes,
defaultMimeType,
indexFile
} = {}) {
this._basePath = basePath
this._mimeTypes = mimeTypes || DEFAULT_MIME_TYPES
this._indexFile = indexFile || DEFAULT_INDEX_FILE
this._defaultMimeType = defaultMimeType || DEFAULT_MIME_TYPE
}

serve (request, response) {
if (request.method === 'GET') {
const file = request.url !== '/' ? request.url : this._indexFile
const filePath = `${this._basePath}${file}`
const [, extension, ...rest] = file.split('.')
if (rest.length > 0) {
writeError(response, 403, '403 Forbidden!')
return
}

if (!fs.existsSync(filePath)) {
writeError(response, 404, '404 Not Found!')
return
}

fs.readFile(filePath, 'utf-8', (err, message) => {
if (err) {
console.error(`HttpStaticServer: Error reading file ${filePath}`, err)
writeError(response, 500, '500 Internal Server Error!')
return
}
response.writeHead(200, { 'Content-Type': this._mimeTypes[extension] || this._defaultMimeType })
response.write(message)
response.end()
})
}
}
}

function writeError (response, code, errorMessage) {
response.writeHead(code, { 'Content-Type': 'text/plain' })
response.write(errorMessage)
response.end()
}
1 change: 1 addition & 0 deletions packages/testkit-backend/src/infrastructure/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HttpStaticServer } from './http-static.server'