Skip to content

Commit

Permalink
Rewrite package to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKowalczyk committed Oct 13, 2024
1 parent bd483ea commit 1d5e94b
Show file tree
Hide file tree
Showing 10 changed files with 2,909 additions and 535 deletions.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@

**Node.js v12.0.0 or newer is required.**

```
```sh
npm install @igorkowalczyk/repl-uptime
yarn add @igorkowalczyk/repl-uptime
pnpm add @igorkowalczyk/repl-uptime
```

## ✨ Features

- 🚀 **No dependencies**: Just Node.js.
- ⚙️ **Zero configuration**: Ready out of the box.
- 🎈 **User-friendly**: Minimal code required.
-**Blazing speed**: Lightning-fast HTTP server.
- 🌐 **Versatile**: Perfect for any 24/7 JavaScript project.

## 📦 Examples

**No Config**
Expand Down Expand Up @@ -62,6 +54,7 @@ new Server({

/// CommonJS
const Server = require("@igorkowalczyk/repl-uptime");

new Server({
port: 8080,
path: "/",
Expand Down Expand Up @@ -103,7 +96,7 @@ When submitting a pull request, please follow these steps:

## 📋 License

This project is licensed under the MIT. See the [LICENSE](https://github.com/igorkowalczyk/repl-uptime/blob/master/license.md) file for details
This project is licensed under the MIT. See the [LICENSE](https://github.com/igorkowalczyk/repl-uptime/blob/main/license.md) file for details

---

Expand Down
6 changes: 4 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import eslintConfig from "@igorkowalczyk/eslint-config/flat";
import eslintConfig from "@igorkowalczyk/eslint-config";
import jestPlugin from "eslint-plugin-jest";

export default [
...eslintConfig,
...eslintConfig.base,
...eslintConfig.node,
...eslintConfig.typescript,
{
ignores: ["dist/**", "test/**"],
},
Expand Down
69 changes: 0 additions & 69 deletions index.js

This file was deleted.

88 changes: 88 additions & 0 deletions index.ts
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;
9 changes: 5 additions & 4 deletions jest.config.js
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;
25 changes: 18 additions & 7 deletions package.json
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"
Expand All @@ -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": [
Expand All @@ -45,5 +55,6 @@
"api",
"server",
"uptime"
]
],
"packageManager": "[email protected]"
}
Loading

0 comments on commit 1d5e94b

Please sign in to comment.