Skip to content

Commit

Permalink
patch: Allow HTTPv1 on server
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadnasriya committed Jun 9, 2024
1 parent a5a24c2 commit 110e283
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ logs*/
# Ignore folders with a specific pattern
*_temp/

dist
dist
**/renderer
22 changes: 10 additions & 12 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nasriya/hypercloud",
"version": "0.0.6beta",
"version": "0.0.7beta",
"description": "Nasriya HyperCloud is a lightwight Node.js HTTP2 framework.",
"main": "./dist/cjs/hypercloud.js",
"module": "./dist/esm/hypercloud.js",
Expand Down Expand Up @@ -39,6 +39,7 @@
"license": "Nasriya License",
"devDependencies": {
"@nasriya/postbuild": "^1.0.4",
"@types/ejs": "^3.1.5",
"@types/jest": "^29.5.12",
"@types/ms": "^0.7.34",
"@types/node": "^20.12.8",
Expand All @@ -48,7 +49,6 @@
"dependencies": {
"@nasriya/cron": "^1.0.5",
"@nasriya/dns": "^1.0.5",
"@nasriya/logify": "^1.0.2",
"ejs": "^3.1.10",
"ms": "^2.1.3",
"openssl-self-signed-certificate": "^1.1.6",
Expand Down
30 changes: 10 additions & 20 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,11 @@ class HyperCloudServer {

/**
* Start listening for incoming requests
* @param protocol Specify the port number of the protocol for the server. Default: `443` for secure servers and `80` for plain HTTP ones. You can pass a callback too.
* @param port Specify the port number of the protocol for the server. Default: `443` for secure servers and `80` for plain HTTP ones.
* @param callback Pass a callback function to run when the server starts listening.
* @returns {Promise<void|http2.Http2SecureServer>} If secure connection is configured, a `Promise<http2.Http2SecureServer>` will be returned, otherwise, a `Promise<void>` will be returned.
*/
async listen(protocol?: OptionalProtocol): Promise<void | http2.Http2SecureServer> {
async listen(port: number, callback: Function): Promise<void | http2.Http2SecureServer> {
try {
if (this.#_config.secure) {
const { cert, key } = await (async () => {
Expand All @@ -552,7 +552,7 @@ class HyperCloudServer {
}
})()

this.#_system.httpsServer = http2.createSecureServer({ cert, key })
this.#_system.httpsServer = http2.createSecureServer({ cert, key, allowHTTP1: true })
} else {
this.#_system.httpServer = http.createServer();
}
Expand Down Expand Up @@ -603,23 +603,13 @@ class HyperCloudServer {
}
})

const { port, callback } = (() => {
if (helpers.is.undefined(protocol)) { return { port: this.#_config.secure ? 443 : 80, callback: undefined } }

if ('port' in protocol) {
if (typeof protocol.port !== 'number') { throw `The port used in the protocol (${protocol.port}) should be a number, instead got ${typeof protocol.port}` }
if (protocol.port <= 0) { throw `The port has been assigned an invalid value (${protocol.port}). Ports are numbers greater than zero` }

if ('callback' in protocol) {
if (typeof protocol.callback !== 'function') { throw `The protocol.callback should be a callback function, instead got ${typeof protocol.callback}` }
return { port: protocol.port, callback: protocol.callback };
} else {
return { port: protocol.port, callback: undefined }
}
} else {
return { port: this.#_config.secure ? 443 : 80, callback: undefined }
}
})();
if (port !== undefined) {
if (typeof port !== 'number') { throw `The port used in the protocol (${port}) should be a number, instead got ${typeof port}` }
if (port <= 0) { throw `The port has been assigned an invalid value (${port}). Ports are numbers greater than zero` }
if (callback !== undefined && typeof callback !== 'function') { throw `The protocol.callback should be a callback function, instead got ${typeof callback}` }
} else {
port = this.#_config.secure ? 443 : 80;
}

return new Promise((resolve, reject) => {
const res = server.listen(port, () => {
Expand Down

0 comments on commit 110e283

Please sign in to comment.