-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd483ea
commit 1d5e94b
Showing
10 changed files
with
2,909 additions
and
535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { createServer, IncomingMessage, ServerResponse } from "node:http"; | ||
|
||
interface ServerOptions { | ||
port?: number; | ||
path?: string; | ||
message?: string; | ||
debug?: boolean; | ||
} | ||
|
||
class Server { | ||
public port: number; | ||
public customURL: string; | ||
public customResponse: string; | ||
public debug: boolean; | ||
private server: ReturnType<typeof createServer>; | ||
|
||
/** | ||
* @param {ServerOptions} options - The options for the server | ||
* @constructor | ||
* @public | ||
* @returns {void} | ||
* @example | ||
* new Server({ port: 8080, path: "/foo", message: "Hello, world!", debug: true }); | ||
*/ | ||
constructor(options: ServerOptions = {}) { | ||
this.port = options.port || 8080; | ||
this.customURL = options.path ? options.path.toString() : "/"; | ||
this.customResponse = options.message ? options.message.toString() : "200 OK!"; | ||
this.debug = options.debug || false; | ||
this.server = createServer(this.handleRequest.bind(this)); | ||
this.listen(); | ||
} | ||
|
||
/** | ||
* @param {IncomingMessage} req - The request object | ||
* @param {ServerResponse} res - The response object | ||
* @returns {void} | ||
* @private | ||
*/ | ||
handleRequest(req: IncomingMessage, res: ServerResponse): void { | ||
if (this.debug) { | ||
console.log(`Request received: ${req.url}`); | ||
} | ||
|
||
if (req.url === this.customURL) { | ||
res.writeHead(200, { "Content-Type": "text/plain" }); | ||
res.end(this.customResponse); | ||
} else { | ||
res.writeHead(404, { "Content-Type": "text/plain" }); | ||
res.end("Not Found"); | ||
} | ||
} | ||
|
||
/** | ||
* Starts the server listening on the specified port | ||
* @returns {Promise<void>} | ||
*/ | ||
listen(): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.server.on("error", (error) => { | ||
reject(error); | ||
}); | ||
|
||
this.server.listen(this.port, () => { | ||
if (this.debug) console.log(`::debug:: [repl-uptime] => Server listening on port ${this.port}`); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Stops the server | ||
* @returns {Promise<void>} | ||
*/ | ||
public stop(): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.server.close((error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default Server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const config = { | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
|
||
export default { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
testMatch: ["**/test/**/*.mjs"], | ||
testMatch: ["**/test/**/*.ts"], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
{ | ||
"name": "@igorkowalczyk/repl-uptime", | ||
"description": "🤙 Don't let your repl go to sleep! Create a http server with just few lines of code and keep pinging it!", | ||
"version": "1.5.2", | ||
"version": "1.6.0", | ||
"author": "Igor Kowalczyk", | ||
"license": "MIT", | ||
"repository": "IgorKowalczyk/repl-uptime", | ||
"funding": "https://github.com/sponsors/igorkowalczyk", | ||
"bugs": "https://github.com/igorkowalczyk/repl-uptime/issues", | ||
"homepage": "https://github.com/IgorKowalczyk/repl-uptime#readme", | ||
"type": "module", | ||
"types": "./dist/types/index.d.ts", | ||
"exports": { | ||
"import": "./dist/mjs/index.js", | ||
"require": "./dist/cjs/index.cjs" | ||
|
@@ -19,21 +20,30 @@ | |
"format": "prettier . --write --ignore-unknown --cache", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint . --fix", | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage" | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@igorkowalczyk/eslint-config": "2.2.0", | ||
"@igorkowalczyk/prettier-config": "2.2.0", | ||
"@igorkowalczyk/eslint-config": "3.0.0-beta.11", | ||
"@igorkowalczyk/prettier-config": "3.0.0-beta.11", | ||
"@rollup/plugin-node-resolve": "15.3.0", | ||
"@rollup/plugin-typescript": "12.1.0", | ||
"@types/jest": "29.5.13", | ||
"@types/node": "22.7.5", | ||
"@types/supertest": "6.0.2", | ||
"@typescript-eslint/eslint-plugin": "8.8.1", | ||
"@typescript-eslint/parser": "8.8.1", | ||
"eslint": "9.12.0", | ||
"eslint-plugin-jest": "28.8.3", | ||
"jest": "29.7.0", | ||
"prettier": "3.3.3", | ||
"rollup": "4.24.0", | ||
"rollup-plugin-dts": "6.1.1", | ||
"rollup-plugin-uglify": "6.0.4", | ||
"supertest": "7.0.0" | ||
"supertest": "7.0.0", | ||
"ts-jest": "29.2.5", | ||
"typescript": "5.6.3" | ||
}, | ||
"files": [ | ||
"README.md", | ||
"dist" | ||
], | ||
"keywords": [ | ||
|
@@ -45,5 +55,6 @@ | |
"api", | ||
"server", | ||
"uptime" | ||
] | ||
], | ||
"packageManager": "[email protected]" | ||
} |
Oops, something went wrong.