@sourceregistry/node-webserver 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +452 -0
- package/dist/app.d.ts +12 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.es.js +936 -0
- package/dist/index.es.js.map +1 -0
- package/dist/middlewares/cros/index.d.ts +66 -0
- package/dist/middlewares/index.d.ts +2 -0
- package/dist/middlewares/ratelimiter/InMemory.d.ts +16 -0
- package/dist/middlewares/ratelimiter/index.d.ts +50 -0
- package/dist/middlewares/ratelimiter/storage.d.ts +8 -0
- package/dist/static.d.ts +9 -0
- package/dist/types/Cookies.d.ts +17 -0
- package/dist/types/MaybePromise.d.ts +1 -0
- package/dist/types/RequestEvent.d.ts +48 -0
- package/dist/types/RequestMethod.d.ts +2 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/router.d.ts +109 -0
- package/dist/types/server.d.ts +84 -0
- package/dist/utils.d.ts +2 -0
- package/examples/simple.ts +11 -0
- package/package.json +103 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ServerOptions } from 'http';
|
|
2
|
+
import { ServerOptions as HttpsServerOptions } from 'https';
|
|
3
|
+
import { RequestMethod, RequestEvent, Router } from './';
|
|
4
|
+
import { ListenOptions } from 'net';
|
|
5
|
+
type HostMatcher = string | RegExp | ((host: string) => boolean);
|
|
6
|
+
export type SecurityConfig = {
|
|
7
|
+
/**
|
|
8
|
+
* Trust the incoming Host header when constructing event.url/request.url.
|
|
9
|
+
* Disabled by default to avoid host header poisoning in absolute URL generation.
|
|
10
|
+
*/
|
|
11
|
+
trustHostHeader?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Restrict trusted Host values when trustHostHeader is enabled.
|
|
14
|
+
*/
|
|
15
|
+
allowedHosts?: HostMatcher | HostMatcher[];
|
|
16
|
+
/**
|
|
17
|
+
* Restrict accepted WebSocket Origin values.
|
|
18
|
+
* When omitted, Origin is not enforced by default.
|
|
19
|
+
*/
|
|
20
|
+
allowedWebSocketOrigins?: HostMatcher | HostMatcher[];
|
|
21
|
+
/**
|
|
22
|
+
* Maximum accepted request body size based on Content-Length.
|
|
23
|
+
* Requests above the limit are rejected before the body is read.
|
|
24
|
+
*/
|
|
25
|
+
maxRequestBodySize?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Maximum accepted WebSocket message size in bytes.
|
|
28
|
+
* Passed to ws as maxPayload.
|
|
29
|
+
*/
|
|
30
|
+
maxWebSocketPayload?: number;
|
|
31
|
+
};
|
|
32
|
+
export type HttpServerConfig = {
|
|
33
|
+
type: 'http';
|
|
34
|
+
options?: ServerOptions;
|
|
35
|
+
security?: SecurityConfig;
|
|
36
|
+
};
|
|
37
|
+
export type HttpsServerConfig = {
|
|
38
|
+
type: 'https';
|
|
39
|
+
options: HttpsServerOptions;
|
|
40
|
+
security?: SecurityConfig;
|
|
41
|
+
};
|
|
42
|
+
export type ServerConfig = {
|
|
43
|
+
locals?: (event: RequestEvent) => App.Locals | Record<string, any>;
|
|
44
|
+
platform?: (event: RequestEvent) => App.Platform | Record<string, any>;
|
|
45
|
+
} & (HttpServerConfig | HttpsServerConfig);
|
|
46
|
+
export declare class WebServer<TServerConfig extends ServerConfig = ServerConfig> {
|
|
47
|
+
private _server;
|
|
48
|
+
private readonly config;
|
|
49
|
+
readonly router: Router;
|
|
50
|
+
private upgradeHandlerInstalled;
|
|
51
|
+
private readonly wss;
|
|
52
|
+
constructor(config?: TServerConfig);
|
|
53
|
+
private get server();
|
|
54
|
+
discard(path_or_prefix: string, method?: RequestMethod): this;
|
|
55
|
+
listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
|
|
56
|
+
listen(port?: number, hostname?: string, listeningListener?: () => void): this;
|
|
57
|
+
listen(port?: number, backlog?: number, listeningListener?: () => void): this;
|
|
58
|
+
listen(port?: number, listeningListener?: () => void): this;
|
|
59
|
+
listen(path: string, backlog?: number, listeningListener?: () => void): this;
|
|
60
|
+
listen(path: string, listeningListener?: () => void): this;
|
|
61
|
+
listen(options: ListenOptions, listeningListener?: () => void): this;
|
|
62
|
+
listen(handle: any, backlog?: number, listeningListener?: () => void): this;
|
|
63
|
+
listen(handle: any, listeningListener?: () => void): this;
|
|
64
|
+
private installUpgradeHandler;
|
|
65
|
+
close(callback?: (err?: Error) => void): void;
|
|
66
|
+
address(): string | import('net').AddressInfo | null;
|
|
67
|
+
get listening(): boolean;
|
|
68
|
+
private handleRequest;
|
|
69
|
+
private toWebRequest;
|
|
70
|
+
private toRequest;
|
|
71
|
+
private wrapRequestBody;
|
|
72
|
+
private toURL;
|
|
73
|
+
private resolveAuthority;
|
|
74
|
+
private normalizeTrustedHost;
|
|
75
|
+
private matchesValue;
|
|
76
|
+
private toHeaders;
|
|
77
|
+
private isRequestBodyAllowed;
|
|
78
|
+
private handleError;
|
|
79
|
+
private sendWebResponse;
|
|
80
|
+
private shouldOmitResponseBody;
|
|
81
|
+
private isAllowedWebSocketOrigin;
|
|
82
|
+
private toRequestEvent;
|
|
83
|
+
}
|
|
84
|
+
export {};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {WebServer} from "../src";
|
|
2
|
+
|
|
3
|
+
const server = new WebServer();
|
|
4
|
+
|
|
5
|
+
server.router.pre(({request}) => console.log(`[${request.method}] ${request.url}`));
|
|
6
|
+
|
|
7
|
+
server.router.GET('/', () => new Response('Hello world!'))
|
|
8
|
+
|
|
9
|
+
server.listen(3000, () => {
|
|
10
|
+
console.log("Server listening on port 3000");
|
|
11
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sourceregistry/node-webserver",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript web server for Node.js with web-standard Request and Response APIs",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs.js",
|
|
7
|
+
"module": "./dist/index.es.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.es.js",
|
|
13
|
+
"require": "./dist/index.cjs.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"examples",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc && vite build",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"test:ui": "vitest --ui",
|
|
29
|
+
"test:coverage": "vitest run --coverage",
|
|
30
|
+
"docs:build": "typedoc src/index.ts --out generated/docs --name \"node-webserver\" --excludePrivate --excludeProtected",
|
|
31
|
+
"examples/simple": "tsx examples/simple.ts"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/SourceRegistry/node-webserver.git"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"webserver",
|
|
39
|
+
"http",
|
|
40
|
+
"https",
|
|
41
|
+
"router",
|
|
42
|
+
"middleware",
|
|
43
|
+
"websocket",
|
|
44
|
+
"fetch",
|
|
45
|
+
"request",
|
|
46
|
+
"response",
|
|
47
|
+
"typescript",
|
|
48
|
+
"node",
|
|
49
|
+
"nodejs",
|
|
50
|
+
"web-standard"
|
|
51
|
+
],
|
|
52
|
+
"author": "Alexander Slaa",
|
|
53
|
+
"license": "Apache-2.0",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/SourceRegistry/node-webserver/issues"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://sourceregistry.github.io/node-webserver/",
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"cookie": "^1.0.2",
|
|
63
|
+
"ws": "^8.18.3"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
67
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
68
|
+
"@semantic-release/git": "^10.0.1",
|
|
69
|
+
"@semantic-release/npm": "^13.1.3",
|
|
70
|
+
"@semantic-release/release-notes-generator": "^14.1.0",
|
|
71
|
+
"@types/node": "^24.3.0",
|
|
72
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
73
|
+
"@vitest/ui": "^4.0.16",
|
|
74
|
+
"tsx": "^4.21.0",
|
|
75
|
+
"typedoc": "^0.28.15",
|
|
76
|
+
"typescript": "^5.9.3",
|
|
77
|
+
"vite": "^7.3.0",
|
|
78
|
+
"vite-plugin-dts": "^4.5.4",
|
|
79
|
+
"@types/ws": "^8.18.1",
|
|
80
|
+
"vitest": "^4.0.16"
|
|
81
|
+
},
|
|
82
|
+
"release": {
|
|
83
|
+
"branches": [
|
|
84
|
+
"main"
|
|
85
|
+
],
|
|
86
|
+
"plugins": [
|
|
87
|
+
"@semantic-release/commit-analyzer",
|
|
88
|
+
"@semantic-release/release-notes-generator",
|
|
89
|
+
"@semantic-release/changelog",
|
|
90
|
+
"@semantic-release/npm",
|
|
91
|
+
[
|
|
92
|
+
"@semantic-release/git",
|
|
93
|
+
{
|
|
94
|
+
"assets": [
|
|
95
|
+
"package.json",
|
|
96
|
+
"CHANGELOG.md"
|
|
97
|
+
],
|
|
98
|
+
"message": "node-webserver(release): ${nextRelease.version}\n\n${nextRelease.notes}"
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
}
|