-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ty-ras/issue/16-add-custom-listen-function
Issue/16 add custom listen function
- Loading branch information
Showing
8 changed files
with
192 additions
and
267 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@ty-ras/server-koa", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"author": { | ||
"name": "Stanislav Muhametsin", | ||
"email": "[email protected]", | ||
|
@@ -34,7 +34,7 @@ | |
"koa": "^2.14.2" | ||
}, | ||
"dependencies": { | ||
"@ty-ras/server": "^2.0.0" | ||
"@ty-ras/server": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"@ava/get-port": "2.0.0", | ||
|
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,31 @@ | ||
/** | ||
* @file This file contains unit tests for functionality in file `../server-node.ts`. | ||
*/ | ||
|
||
import test from "ava"; | ||
import * as spec from "../listen"; | ||
import * as server from "../server"; | ||
import getPort from "@ava/get-port"; | ||
|
||
test("Verify that listen overload for host and port as in starter template, works", async (c) => { | ||
c.plan(1); | ||
await c.notThrowsAsync( | ||
async () => | ||
await spec.listenAsync( | ||
server.createServer({ endpoints: [] }), | ||
"localhost", | ||
await getPort(), | ||
), | ||
); | ||
}); | ||
|
||
test("Verify that listen overload for listen options works", async (c) => { | ||
c.plan(1); | ||
await c.notThrowsAsync( | ||
async () => | ||
await spec.listenAsync(server.createServer({ endpoints: [] }), { | ||
options: {}, | ||
listen: { host: "localhost", port: await getPort() }, | ||
}), | ||
); | ||
}); |
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,100 @@ | ||
/** | ||
* @file This file contains function that can be used to expose uniform way to listen to TyRAS servers. | ||
*/ | ||
|
||
import type * as server from "./server"; | ||
import * as serverGeneric from "@ty-ras/server"; | ||
import * as net from "node:net"; | ||
|
||
/** | ||
* The helper function to listen to given {@link server.HttpServer} asynchronously. | ||
* @param server The {@link server.HttpServer} to listen to. | ||
* @param host The hostname as string. | ||
* @param port The port as number. | ||
* @param backlog The backlog parameter, if any. | ||
* @returns Asynchronously nothing. | ||
*/ | ||
export function listenAsync( | ||
server: server.HttpServer, | ||
host: string, | ||
port: number, | ||
backlog?: number, | ||
): Promise<void>; | ||
|
||
/** | ||
*The helper function to listen to given {@link server.HttpServer} asynchronously. | ||
* @param server The {@link server.HttpServer} to listen to. | ||
* @param options The {@link ListenOptions1} or {@link ListenOptions2}. | ||
* @returns Asynchronously nothing. | ||
*/ | ||
export function listenAsync( | ||
server: server.HttpServer, | ||
options: ListenOptions1 | ListenOptions2, | ||
): Promise<void>; | ||
|
||
/** | ||
* The helper function to listen to given {@link server.HttpServer} asynchronously. | ||
* @param server The {@link server.HttpServer} to listen to. | ||
* @param hostOrOptions The {@link ListenOptions1} or {@link ListenOptions2}. | ||
* @param port The port to listen to. | ||
* @param backlog The backlog parameter, if any. | ||
* @returns Asynchronously nothing. | ||
*/ | ||
export function listenAsync( | ||
server: server.HttpServer, | ||
hostOrOptions: string | ListenOptions1 | ListenOptions2, | ||
port?: number, | ||
backlog?: number, | ||
) { | ||
const opts: ListenOptions1 | ListenOptions2 = | ||
typeof hostOrOptions === "string" | ||
? { | ||
listen: { | ||
host: hostOrOptions, | ||
port, | ||
backlog, | ||
}, | ||
} | ||
: hostOrOptions; | ||
|
||
return serverGeneric.listenAsyncGeneric( | ||
serverGeneric.createNodeServerGeneric(opts, server.callback()), | ||
typeof hostOrOptions === "string" ? hostOrOptions : hostOrOptions.listen, | ||
port, | ||
backlog, | ||
); | ||
} | ||
|
||
/** | ||
* This interface contains options for both HTTP 1 and 2 servers when listening to them via {@link listenAsync}. | ||
*/ | ||
export interface ListenOptionsBase { | ||
/** | ||
* Options related to listening for connections. | ||
*/ | ||
listen: net.ListenOptions; | ||
} | ||
|
||
/** | ||
* This interface contains options for HTTP 1 servers when listening to them via {@link listenAsync}. | ||
*/ | ||
export interface ListenOptions1 | ||
extends serverGeneric.NodeServerOptions1<boolean>, | ||
ListenOptionsBase { | ||
/** | ||
* Use this property if needed. | ||
*/ | ||
httpVersion?: 1; | ||
} | ||
|
||
/** | ||
* This interface contains options for HTTP 2 servers when listening to them via {@link listenAsync}. | ||
*/ | ||
export interface ListenOptions2 | ||
extends serverGeneric.NodeServerOptions2<boolean>, | ||
ListenOptionsBase { | ||
/** | ||
* Set this property to `2` in order to use HTTP2 server when listening. | ||
*/ | ||
httpVersion: 2; | ||
} |
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
Oops, something went wrong.