| name: Node.js Package | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| jobs: | ||
| publish-gpr: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: 18 | ||
| registry-url: https://npm.pkg.github.com/ | ||
| - run: npm install | ||
| - run: npm publish | ||
| env: | ||
| NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}} |
+2
-1
| { | ||
| "name": "restwave", | ||
| "version": "2.5.4", | ||
| "version": "2.5.5", | ||
| "description": "simplified version of express", | ||
@@ -34,4 +34,5 @@ "main": "./src/index.js", | ||
| "dependencies": { | ||
| "cors": "^2.8.5", | ||
| "nodemon": "^2.0.22" | ||
| } | ||
| } |
+8
-0
@@ -76,2 +76,10 @@ | ||
| ### **Request Body** | ||
| 1. In order to access `request body` we prodvide `data` param . | ||
| - `req.data` | ||
| # | ||
| ### **Response Methods** | ||
@@ -78,0 +86,0 @@ |
+103
-105
@@ -16,45 +16,45 @@ import Methods from "./methods/method.js"; | ||
| constructor() { | ||
| super(); | ||
| this.#setResponseType(); | ||
| this.#request = { | ||
| method: "", | ||
| url: "", | ||
| data: "", | ||
| headers: {}, | ||
| }; | ||
| this.#createServer(); | ||
| } | ||
| constructor() { | ||
| super(); | ||
| this.#setResponseType(); | ||
| this.#request = { | ||
| method: "", | ||
| url: "", | ||
| data: "", | ||
| headers: {}, | ||
| }; | ||
| this.#createServer(); | ||
| } | ||
| #createServer() { | ||
| this.#server = net.createServer((socket) => { | ||
| let body = ""; | ||
| this.#contentLength = 0; | ||
| this.#data = ""; | ||
| socket.on("data", (data) => { | ||
| this.#socket = socket; | ||
| body = data.toString("utf-8"); | ||
| const lines = body.split("\r\n"); | ||
| if (lines.length > 1) { | ||
| const contentTypeHeader = lines.find((line) => | ||
| line.startsWith("Content-Type:") | ||
| ); | ||
| if ( | ||
| contentTypeHeader && | ||
| contentTypeHeader.includes("application/json") | ||
| ) { | ||
| const contentIndex = body.indexOf("\r\n\r\n") + 4; | ||
| this.#request.data = JSON.parse(body.slice(contentIndex)); | ||
| } | ||
| } | ||
| #createServer() { | ||
| this.#server = net.createServer((socket) => { | ||
| let body = ""; | ||
| this.#contentLength = 0; | ||
| this.#data = ""; | ||
| socket.on("data", (data) => { | ||
| this.#socket = socket; | ||
| body = data.toString("utf-8"); | ||
| const lines = body.split("\r\n"); | ||
| if (lines.length > 1) { | ||
| const contentTypeHeader = lines.find((line) => | ||
| line.startsWith("Content-Type:") | ||
| ); | ||
| if ( | ||
| contentTypeHeader && | ||
| contentTypeHeader.includes("application/json") | ||
| ) { | ||
| const contentIndex = body.indexOf("\r\n\r\n") + 4; | ||
| this.#request.data = JSON.parse(body.slice(contentIndex)); | ||
| } | ||
| } | ||
| // Attach headers to req | ||
| for (let i = 0; i < lines.length; i++) { | ||
| const [key, value] = lines[i].split(":"); | ||
| if (!key || !value) continue; | ||
| this.#request.headers[key] = value; | ||
| } | ||
| // Attach headers to req | ||
| for (let i = 0; i < lines.length; i++) { | ||
| const [key, value] = lines[i].split(":"); | ||
| if (!key || !value) continue; | ||
| this.#request.headers[key] = value; | ||
| } | ||
| this.#request.method = body.split(" ")[0]; | ||
| this.#request.url = body.split(" ")[1]; | ||
| this.#request.method = body.split(" ")[0]; | ||
| this.#request.url = body.split(" ")[1]; | ||
@@ -78,7 +78,7 @@ if (this.#request.method === "OPTIONS") { | ||
| #handleRequests() { | ||
| this.#extractQueryParameters(); | ||
| let i = 0; | ||
| const next = () => { | ||
| const currentMiddleware = super.getMiddlewares()[i]; | ||
| #handleRequests() { | ||
| this.#extractQueryParameters(); | ||
| let i = 0; | ||
| const next = () => { | ||
| const currentMiddleware = super.getMiddlewares()[i]; | ||
@@ -128,45 +128,45 @@ i++; | ||
| #handleMethodRequests(currentMiddleware, next) { | ||
| if (currentMiddleware.hasOwnProperty("params")) { | ||
| this.#request.params = {}; | ||
| const rUrl = this.#request.url.split("/"); | ||
| const cUrl = currentMiddleware.route.split("/"); | ||
| let currentParam = 0; | ||
| if (rUrl.length !== cUrl.length) return false; | ||
| for (let i = 0; i < rUrl.length; i++) { | ||
| if ( | ||
| cUrl[i].startsWith(":") && | ||
| currentParam < currentMiddleware.params.length && | ||
| rUrl[i] | ||
| ) { | ||
| this.#request.params[ | ||
| Object.keys(currentMiddleware.params[currentParam])[0] | ||
| ] = rUrl[i]; | ||
| currentParam++; | ||
| } else { | ||
| if (cUrl[i] !== rUrl[i]) { | ||
| this.#request.params = {}; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| currentMiddleware.cb(this.#request, this.#response, next); | ||
| return true; | ||
| } else if (currentMiddleware.route === this.#request.url) { | ||
| currentMiddleware.cb(this.#request, this.#response, next); | ||
| return true; | ||
| } | ||
| } | ||
| #handleMethodRequests(currentMiddleware, next) { | ||
| if (currentMiddleware.hasOwnProperty("params")) { | ||
| this.#request.params = {}; | ||
| const rUrl = this.#request.url.split("/"); | ||
| const cUrl = currentMiddleware.route.split("/"); | ||
| let currentParam = 0; | ||
| if (rUrl.length !== cUrl.length) return false; | ||
| for (let i = 0; i < rUrl.length; i++) { | ||
| if ( | ||
| cUrl[i].startsWith(":") && | ||
| currentParam < currentMiddleware.params.length && | ||
| rUrl[i] | ||
| ) { | ||
| this.#request.params[ | ||
| Object.keys(currentMiddleware.params[currentParam])[0] | ||
| ] = rUrl[i]; | ||
| currentParam++; | ||
| } else { | ||
| if (cUrl[i] !== rUrl[i]) { | ||
| this.#request.params = {}; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| currentMiddleware.cb(this.#request, this.#response, next); | ||
| return true; | ||
| } else if (currentMiddleware.route === this.#request.url) { | ||
| currentMiddleware.cb(this.#request, this.#response, next); | ||
| return true; | ||
| } | ||
| } | ||
| #extractQueryParameters() { | ||
| const queryParameters = {}; | ||
| const [urlString, queryString] = this.#request.url.split("?"); | ||
| if (!queryString) return; | ||
| queryString.split("&").forEach((query) => { | ||
| const [key, value] = query.split("="); | ||
| queryParameters[key] = value; | ||
| }); | ||
| this.#request.query = queryParameters; | ||
| this.#request.url = urlString; | ||
| } | ||
| #extractQueryParameters() { | ||
| const queryParameters = {}; | ||
| const [urlString, queryString] = this.#request.url.split("?"); | ||
| if (!queryString) return; | ||
| queryString.split("&").forEach((query) => { | ||
| const [key, value] = query.split("="); | ||
| queryParameters[key] = value; | ||
| }); | ||
| this.#request.query = queryParameters; | ||
| this.#request.url = urlString; | ||
| } | ||
@@ -266,22 +266,20 @@ #setResponseType() { | ||
| listen(...args) { | ||
| if (args[0] && typeof args[0] !== "number") | ||
| throw new Error("PORT must be a number"); | ||
| let port = 3000; | ||
| let host; | ||
| let cb; | ||
| if (args.length === 1) { | ||
| port = args[0]; | ||
| } else if (args.length === 2) { | ||
| port = args[0]; | ||
| cb = args[1]; | ||
| } else { | ||
| port = args[0]; | ||
| host = args[1]; | ||
| cb = args[2]; | ||
| } | ||
| this.#server.listen(port, host, cb); | ||
| } | ||
| listen(...args) { | ||
| let port = Number(args[0]) || 3000; | ||
| let host; | ||
| let cb; | ||
| if (args.length === 1) { | ||
| port = args[0]; | ||
| } else if (args.length === 2) { | ||
| port = args[0]; | ||
| cb = args[1]; | ||
| } else { | ||
| port = args[0]; | ||
| host = args[1]; | ||
| cb = args[2]; | ||
| } | ||
| this.#server.listen(port, host, cb); | ||
| } | ||
| } | ||
| export default RestWave; |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
30149
2.69%11
10%134
6.35%2
100%694
-0.29%+ Added
+ Added
+ Added
+ Added