Skip to content

Commit

Permalink
Merge pull request #32 from ty-ras/issue/31-update-to-2-0
Browse files Browse the repository at this point in the history
#31 Updating to version 2.0.0
  • Loading branch information
stazz committed Aug 2, 2023
2 parents 3688de2 + 6a51ffd commit 9bad1a4
Show file tree
Hide file tree
Showing 3 changed files with 815 additions and 72 deletions.
16 changes: 11 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ty-ras/server-node",
"version": "1.0.0",
"version": "2.0.0",
"author": {
"name": "Stanislav Muhametsin",
"email": "346799+stazz@users.noreply.github.com",
Expand Down Expand Up @@ -31,14 +31,13 @@
}
},
"dependencies": {
"@ty-ras/server": "^1.0.0",
"@ty-ras/endpoint-prefix": "^1.0.0"
"@ty-ras/server": "^2.0.0"
},
"devDependencies": {
"@ava/get-port": "2.0.0",
"@babel/core": "7.21.5",
"@babel/eslint-parser": "7.21.3",
"@ty-ras/server-test-support": "1.0.0",
"@ty-ras/server-test-support": "2.0.0",
"@typescript-eslint/eslint-plugin": "5.59.2",
"@typescript-eslint/parser": "5.59.2",
"@types/node": "18.15.3",
Expand All @@ -52,6 +51,7 @@
"eslint-config-prettier": "8.8.0",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-sonarjs": "0.19.0",
"madge": "6.1.0",
"node-forge": "1.3.1",
"prettier": "2.8.8",
"ts-node": "10.9.1",
Expand All @@ -66,11 +66,17 @@
"format-output-files-ts": "eslint --no-eslintrc --config '.eslintrc.out-ts.cjs' --fix --fix-type layout './dist-ts/**/*.ts'",
"format-output-files-js": "eslint --no-eslintrc --config '.eslintrc.out.cjs' --fix 'dist-cjs/**/*js' 'dist-esm/**/*js'",
"generate-stub-package-json-for-cjs": "../scripts/generate-stub-package-json.cjs",
"lint": "eslint ./src --ext .ts,.tsx",
"lint": "yarn run lint:eslint && yarn run lint:circular",
"lint:circular": "madge --circular --no-color --no-spinner --extensions ts --warning ./src",
"lint:eslint": "eslint ./src --ext .ts,.tsx",
"remove-empty-js-files": "../scripts/remove-empty-js-files.cjs",
"tsc": "tsc --project tsconfig.build.json",
"tsc:plain": "tsc",
"test:coverage": "c8 --temp-directory /tmp ava",
"test:run": "c8 --temp-directory /tmp --reporter text ava"
},
"resolutions": {
"dependency-tree": "10.0.9",
"precinct": "11.0.5"
}
}
44 changes: 17 additions & 27 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

import * as ep from "@ty-ras/endpoint";
import * as prefix from "@ty-ras/endpoint-prefix";
import * as server from "@ty-ras/server";

import type * as ctx from "./context.types";
Expand Down Expand Up @@ -123,29 +122,29 @@ export function createServer<TStateInfo, TState>(
) {
let retVal;
if ("httpVersion" in opts && opts.httpVersion === 2) {
const { endpoints, options, secure, ...handlerOptions } = opts;
const { options, secure, ...handlerOptions } = opts;
const httpHandler = asyncToVoid(
createHandleHttpRequest<
TStateInfo,
TState,
http2.Http2ServerRequest,
http2.Http2ServerResponse
>(handlerOptions, getRegExpAndHandler(endpoints)),
>(handlerOptions),
);
if (isSecure(secure, options, 2)) {
retVal = http2.createSecureServer(options ?? {}, httpHandler);
} else {
retVal = http2.createServer(options ?? {}, httpHandler);
}
} else {
const { endpoints, options, secure, ...handlerOptions } = opts;
const { options, secure, ...handlerOptions } = opts;
const httpHandler = asyncToVoid(
createHandleHttpRequest<
TStateInfo,
TState,
http.IncomingMessage,
http.ServerResponse
>(handlerOptions, getRegExpAndHandler(endpoints)),
>(handlerOptions),
);
if (isSecure(secure, options, 1)) {
retVal = https.createServer(options ?? {}, httpHandler);
Expand Down Expand Up @@ -262,27 +261,22 @@ const createHandleHttpRequest = <
TState,
TRequest extends http.IncomingMessage | http2.Http2ServerRequest,
TResponse extends http.ServerResponse | http2.Http2ServerResponse,
>(
{
createState,
events,
}: Pick<
ServerCreationOptions<
ctx.ServerContextGeneric<TRequest, TResponse>,
TStateInfo,
TState,
never,
never
>,
"createState" | "events"
>,
regExpAndHandler: ep.FinalizedAppEndpoint<
>({
endpoints,
createState,
events,
}: Pick<
ServerCreationOptions<
ctx.ServerContextGeneric<TRequest, TResponse>,
TStateInfo
TStateInfo,
TState,
never,
never
>,
): HTTP1Or2Handler<TRequest, TResponse> => {
"endpoints" | "createState" | "events"
>): HTTP1Or2Handler<TRequest, TResponse> => {
const flow = server.createTypicalServerFlow(
regExpAndHandler,
endpoints,
{
...internal.staticCallbacks,
getState: async ({ req }, stateInfo) =>
Expand Down Expand Up @@ -319,10 +313,6 @@ const asyncToVoid =
void asyncCallback(...args);
};

const getRegExpAndHandler = <TContext, TStateInfo>(
endpoints: ReadonlyArray<ep.AppEndpoint<TContext, TStateInfo>>,
) => prefix.atPrefix("", ...endpoints).getRegExpAndHandler("");

const isSecure = (
secure: boolean | undefined,
options: object | undefined,
Expand Down

0 comments on commit 9bad1a4

Please sign in to comment.