@tessellation/http 0.0.0-stage → 2.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 +21 -0
- package/README.md +412 -2
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/lib/auth/api-key.d.ts +17 -0
- package/dist/lib/auth/api-key.d.ts.map +1 -0
- package/dist/lib/auth/api-key.js +48 -0
- package/dist/lib/auth/jwt.d.ts +33 -0
- package/dist/lib/auth/jwt.d.ts.map +1 -0
- package/dist/lib/auth/jwt.js +227 -0
- package/dist/lib/auth/provide-auth.d.ts +30 -0
- package/dist/lib/auth/provide-auth.d.ts.map +1 -0
- package/dist/lib/auth/provide-auth.js +69 -0
- package/dist/lib/auth/strategy.d.ts +47 -0
- package/dist/lib/auth/strategy.d.ts.map +1 -0
- package/dist/lib/auth/strategy.js +18 -0
- package/dist/lib/compile.d.ts +46 -0
- package/dist/lib/compile.d.ts.map +1 -0
- package/dist/lib/compile.js +126 -0
- package/dist/lib/compression.d.ts +23 -0
- package/dist/lib/compression.d.ts.map +1 -0
- package/dist/lib/compression.js +86 -0
- package/dist/lib/config.d.ts +173 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +93 -0
- package/dist/lib/cors.d.ts +41 -0
- package/dist/lib/cors.d.ts.map +1 -0
- package/dist/lib/cors.js +101 -0
- package/dist/lib/docs.d.ts +15 -0
- package/dist/lib/docs.d.ts.map +1 -0
- package/dist/lib/docs.js +93 -0
- package/dist/lib/engine.d.ts +83 -0
- package/dist/lib/engine.d.ts.map +1 -0
- package/dist/lib/engine.js +562 -0
- package/dist/lib/errors.d.ts +31 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +55 -0
- package/dist/lib/etag.d.ts +5 -0
- package/dist/lib/etag.d.ts.map +1 -0
- package/dist/lib/etag.js +30 -0
- package/dist/lib/headers.d.ts +3 -0
- package/dist/lib/headers.d.ts.map +1 -0
- package/dist/lib/headers.js +14 -0
- package/dist/lib/json-schema.d.ts +11 -0
- package/dist/lib/json-schema.d.ts.map +1 -0
- package/dist/lib/json-schema.js +23 -0
- package/dist/lib/openapi.d.ts +18 -0
- package/dist/lib/openapi.d.ts.map +1 -0
- package/dist/lib/openapi.js +218 -0
- package/dist/lib/rate-limit.d.ts +52 -0
- package/dist/lib/rate-limit.d.ts.map +1 -0
- package/dist/lib/rate-limit.js +87 -0
- package/dist/lib/reply.d.ts +27 -0
- package/dist/lib/reply.d.ts.map +1 -0
- package/dist/lib/reply.js +52 -0
- package/dist/lib/request.d.ts +55 -0
- package/dist/lib/request.d.ts.map +1 -0
- package/dist/lib/request.js +36 -0
- package/dist/lib/router.d.ts +35 -0
- package/dist/lib/router.d.ts.map +1 -0
- package/dist/lib/router.js +173 -0
- package/dist/lib/routes.d.ts +196 -0
- package/dist/lib/routes.d.ts.map +1 -0
- package/dist/lib/routes.js +98 -0
- package/dist/lib/security-headers.d.ts +19 -0
- package/dist/lib/security-headers.d.ts.map +1 -0
- package/dist/lib/security-headers.js +22 -0
- package/dist/lib/serializer.d.ts +35 -0
- package/dist/lib/serializer.d.ts.map +1 -0
- package/dist/lib/serializer.js +147 -0
- package/dist/lib/server.d.ts +86 -0
- package/dist/lib/server.d.ts.map +1 -0
- package/dist/lib/server.js +255 -0
- package/dist/lib/testing.d.ts +21 -0
- package/dist/lib/testing.d.ts.map +1 -0
- package/dist/lib/testing.js +62 -0
- package/dist/lib/transports/node.d.ts +4 -0
- package/dist/lib/transports/node.d.ts.map +1 -0
- package/dist/lib/transports/node.js +249 -0
- package/dist/lib/transports/transport.d.ts +32 -0
- package/dist/lib/transports/transport.d.ts.map +1 -0
- package/dist/lib/transports/transport.js +1 -0
- package/dist/lib/transports/uws.d.ts +8 -0
- package/dist/lib/transports/uws.d.ts.map +1 -0
- package/dist/lib/transports/uws.js +246 -0
- package/dist/lib/websocket.d.ts +71 -0
- package/dist/lib/websocket.d.ts.map +1 -0
- package/dist/lib/websocket.js +125 -0
- package/package.json +67 -3
- package/src/index.ts +103 -0
- package/src/lib/auth/api-key.ts +73 -0
- package/src/lib/auth/jwt.ts +339 -0
- package/src/lib/auth/provide-auth.ts +99 -0
- package/src/lib/auth/strategy.ts +66 -0
- package/src/lib/compile.ts +207 -0
- package/src/lib/compression.ts +121 -0
- package/src/lib/config.ts +107 -0
- package/src/lib/cors.ts +146 -0
- package/src/lib/docs.ts +115 -0
- package/src/lib/engine.ts +770 -0
- package/src/lib/errors.ts +84 -0
- package/src/lib/etag.ts +37 -0
- package/src/lib/headers.ts +18 -0
- package/src/lib/json-schema.ts +36 -0
- package/src/lib/openapi.ts +299 -0
- package/src/lib/rate-limit.ts +142 -0
- package/src/lib/reply.ts +70 -0
- package/src/lib/request.ts +83 -0
- package/src/lib/router.ts +217 -0
- package/src/lib/routes.ts +414 -0
- package/src/lib/security-headers.ts +38 -0
- package/src/lib/serializer.ts +203 -0
- package/src/lib/server.ts +352 -0
- package/src/lib/testing.ts +88 -0
- package/src/lib/transports/node.ts +315 -0
- package/src/lib/transports/transport.ts +39 -0
- package/src/lib/transports/uws.ts +310 -0
- package/src/lib/websocket.ts +184 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { randomHex, runInInjectionContext, } from '@tessellation/core';
|
|
3
|
+
import { Reply } from './reply.js';
|
|
4
|
+
/** The reply that accepts an upgrade; the transport completes the handshake. */
|
|
5
|
+
export class UpgradeReply extends Reply {
|
|
6
|
+
data;
|
|
7
|
+
snapshot;
|
|
8
|
+
constructor(data,
|
|
9
|
+
/** Runs a function in the async context of the upgrade request. */
|
|
10
|
+
snapshot) {
|
|
11
|
+
super(101, undefined);
|
|
12
|
+
this.data = data;
|
|
13
|
+
this.snapshot = snapshot;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Captures the current async context (principal, log fields, span, ...). */
|
|
17
|
+
export function captureContext() {
|
|
18
|
+
return AsyncLocalStorage.snapshot();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* One accepted connection: runs its handlers in the upgrade request's async
|
|
22
|
+
* context and the application's injection context, and reports errors.
|
|
23
|
+
*/
|
|
24
|
+
export class WebSocketSession {
|
|
25
|
+
route;
|
|
26
|
+
handlers;
|
|
27
|
+
reply;
|
|
28
|
+
injector;
|
|
29
|
+
logger;
|
|
30
|
+
lifecycle;
|
|
31
|
+
id = randomHex(8);
|
|
32
|
+
socket;
|
|
33
|
+
constructor(route, handlers, reply, injector, logger, lifecycle) {
|
|
34
|
+
this.route = route;
|
|
35
|
+
this.handlers = handlers;
|
|
36
|
+
this.reply = reply;
|
|
37
|
+
this.injector = injector;
|
|
38
|
+
this.logger = logger;
|
|
39
|
+
this.lifecycle = lifecycle;
|
|
40
|
+
}
|
|
41
|
+
/** Called by the transport once the handshake is complete. */
|
|
42
|
+
opened(transport) {
|
|
43
|
+
this.socket = new WebSocketFacade(this.id, this.reply.data, transport);
|
|
44
|
+
this.lifecycle.opened(this);
|
|
45
|
+
this.call('open', (handlers, socket) => handlers.open?.(socket));
|
|
46
|
+
}
|
|
47
|
+
received(message) {
|
|
48
|
+
this.call('message', (handlers, socket) => handlers.message?.(socket, message));
|
|
49
|
+
}
|
|
50
|
+
closed(code, reason) {
|
|
51
|
+
if (this.socket === undefined) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
this.socket.closed = true;
|
|
55
|
+
this.lifecycle.closed(this);
|
|
56
|
+
this.call('close', (handlers, socket) => handlers.close?.(socket, code, reason));
|
|
57
|
+
}
|
|
58
|
+
/** Closes the connection, such as on shutdown. */
|
|
59
|
+
close(code, reason) {
|
|
60
|
+
this.socket?.close(code, reason);
|
|
61
|
+
}
|
|
62
|
+
call(event, invoke) {
|
|
63
|
+
const socket = this.socket;
|
|
64
|
+
if (socket === undefined) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.reply.snapshot(() => {
|
|
68
|
+
let result;
|
|
69
|
+
try {
|
|
70
|
+
result = runInInjectionContext(this.injector, () => invoke(this.handlers, socket));
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
this.fail(event, error);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (result instanceof Promise) {
|
|
77
|
+
result.catch((error) => this.fail(event, error));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
fail(event, error) {
|
|
82
|
+
this.logger.error('WebSocket handler failed', {
|
|
83
|
+
event,
|
|
84
|
+
route: this.route,
|
|
85
|
+
connection: this.id,
|
|
86
|
+
err: error,
|
|
87
|
+
});
|
|
88
|
+
this.socket?.close(1011, 'Internal error');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
class WebSocketFacade {
|
|
92
|
+
id;
|
|
93
|
+
data;
|
|
94
|
+
transport;
|
|
95
|
+
closed = false;
|
|
96
|
+
constructor(id, data, transport) {
|
|
97
|
+
this.id = id;
|
|
98
|
+
this.data = data;
|
|
99
|
+
this.transport = transport;
|
|
100
|
+
}
|
|
101
|
+
get remoteAddress() {
|
|
102
|
+
return this.transport.remoteAddress;
|
|
103
|
+
}
|
|
104
|
+
send(message) {
|
|
105
|
+
return !this.closed && this.transport.send(message);
|
|
106
|
+
}
|
|
107
|
+
close(code = 1000, reason = '') {
|
|
108
|
+
if (!this.closed) {
|
|
109
|
+
this.closed = true;
|
|
110
|
+
this.transport.close(code, reason);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
subscribe(topic) {
|
|
114
|
+
if (!this.closed)
|
|
115
|
+
this.transport.subscribe(topic);
|
|
116
|
+
}
|
|
117
|
+
unsubscribe(topic) {
|
|
118
|
+
if (!this.closed)
|
|
119
|
+
this.transport.unsubscribe(topic);
|
|
120
|
+
}
|
|
121
|
+
publish(topic, message) {
|
|
122
|
+
if (!this.closed)
|
|
123
|
+
this.transport.publish(topic, message);
|
|
124
|
+
}
|
|
125
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tessellation/http",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A fast HTTP layer for Tessellation: typed nested routes, validation, OpenAPI and Swagger UI.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tessellation",
|
|
7
|
+
"typescript",
|
|
8
|
+
"node",
|
|
9
|
+
"framework",
|
|
10
|
+
"http",
|
|
11
|
+
"server",
|
|
12
|
+
"router",
|
|
13
|
+
"openapi",
|
|
14
|
+
"swagger",
|
|
15
|
+
"websocket",
|
|
16
|
+
"uwebsockets",
|
|
17
|
+
"validation"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/ethanelliott/tessellation/tree/main/packages/http#readme",
|
|
20
|
+
"bugs": "https://github.com/ethanelliott/tessellation/issues",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/ethanelliott/tessellation.git",
|
|
24
|
+
"directory": "packages/http"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "Ethan Elliott",
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
"./package.json": "./package.json",
|
|
34
|
+
".": {
|
|
35
|
+
"@tessellation/source": "./src/index.ts",
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"import": "./dist/index.js",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"src",
|
|
44
|
+
"!**/*.tsbuildinfo",
|
|
45
|
+
"!src/**/*.spec.ts",
|
|
46
|
+
"!src/**/*.test.ts"
|
|
47
|
+
],
|
|
48
|
+
"nx": {
|
|
49
|
+
"name": "http"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@tessellation/core": "2.0.0",
|
|
53
|
+
"swagger-ui-dist": "^5.36.0",
|
|
54
|
+
"ws": "^8.23.1"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=26"
|
|
58
|
+
},
|
|
59
|
+
"sideEffects": false,
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@types/ws": "^8.18.2"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public",
|
|
65
|
+
"provenance": true
|
|
66
|
+
},
|
|
67
|
+
"optionalDependencies": {
|
|
68
|
+
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.72.0"
|
|
69
|
+
}
|
|
6
70
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export { apiKeyAuth, type ApiKeyAuthOptions } from './lib/auth/api-key.js';
|
|
2
|
+
export {
|
|
3
|
+
type JsonWebKey,
|
|
4
|
+
type JwtAuthOptions,
|
|
5
|
+
jwtAuth,
|
|
6
|
+
type JwtClaims,
|
|
7
|
+
} from './lib/auth/jwt.js';
|
|
8
|
+
export {
|
|
9
|
+
authenticated,
|
|
10
|
+
authorize,
|
|
11
|
+
provideAuth,
|
|
12
|
+
requireRoles,
|
|
13
|
+
} from './lib/auth/provide-auth.js';
|
|
14
|
+
export {
|
|
15
|
+
AUTH_STRATEGIES,
|
|
16
|
+
type AuthStrategy,
|
|
17
|
+
CurrentPrincipal,
|
|
18
|
+
type Principal,
|
|
19
|
+
principal,
|
|
20
|
+
requirePrincipal,
|
|
21
|
+
} from './lib/auth/strategy.js';
|
|
22
|
+
export {
|
|
23
|
+
type CompressionOptions,
|
|
24
|
+
Compressor,
|
|
25
|
+
type Encoding,
|
|
26
|
+
} from './lib/compression.js';
|
|
27
|
+
export { HttpConfig, type HttpSettings } from './lib/config.js';
|
|
28
|
+
export { type CorsOptions, CorsPolicy } from './lib/cors.js';
|
|
29
|
+
export {
|
|
30
|
+
badRequest,
|
|
31
|
+
conflict,
|
|
32
|
+
forbidden,
|
|
33
|
+
HttpError,
|
|
34
|
+
type HttpErrorOptions,
|
|
35
|
+
notFound,
|
|
36
|
+
problem,
|
|
37
|
+
tooManyRequests,
|
|
38
|
+
unauthorized,
|
|
39
|
+
unprocessable,
|
|
40
|
+
} from './lib/errors.js';
|
|
41
|
+
export {
|
|
42
|
+
accepted,
|
|
43
|
+
created,
|
|
44
|
+
noContent,
|
|
45
|
+
ok,
|
|
46
|
+
redirect,
|
|
47
|
+
Reply,
|
|
48
|
+
reply,
|
|
49
|
+
} from './lib/reply.js';
|
|
50
|
+
export { HttpRequest, type ResponseSink } from './lib/request.js';
|
|
51
|
+
export type { TestRequestInit, TestResponse } from './lib/testing.js';
|
|
52
|
+
export {
|
|
53
|
+
type GroupDefinition,
|
|
54
|
+
type GroupOptions,
|
|
55
|
+
type HandlerResult,
|
|
56
|
+
type HttpMethod,
|
|
57
|
+
type Middleware,
|
|
58
|
+
type MiddlewareContext,
|
|
59
|
+
type PathParams,
|
|
60
|
+
type RawQuery,
|
|
61
|
+
type RequestContext,
|
|
62
|
+
RouteBuilder,
|
|
63
|
+
type RouteDefinition,
|
|
64
|
+
type RouteDocs,
|
|
65
|
+
type RouteNode,
|
|
66
|
+
type RouteOptions,
|
|
67
|
+
routes,
|
|
68
|
+
type RouteTree,
|
|
69
|
+
type WebSocketRouteOptions,
|
|
70
|
+
} from './lib/routes.js';
|
|
71
|
+
export {
|
|
72
|
+
MemoryRateLimitStore,
|
|
73
|
+
RATE_LIMIT_STORE,
|
|
74
|
+
rateLimit,
|
|
75
|
+
type RateLimitHit,
|
|
76
|
+
type RateLimitOptions,
|
|
77
|
+
type RateLimitStore,
|
|
78
|
+
} from './lib/rate-limit.js';
|
|
79
|
+
export {
|
|
80
|
+
type SecurityHeaderOptions,
|
|
81
|
+
securityHeaders,
|
|
82
|
+
} from './lib/security-headers.js';
|
|
83
|
+
export {
|
|
84
|
+
HTTP_MIDDLEWARE,
|
|
85
|
+
HTTP_OPTIONS,
|
|
86
|
+
type HttpOptions,
|
|
87
|
+
HttpServer,
|
|
88
|
+
provideHttp,
|
|
89
|
+
provideHttpMiddleware,
|
|
90
|
+
} from './lib/server.js';
|
|
91
|
+
export { nodeTransport } from './lib/transports/node.js';
|
|
92
|
+
export type {
|
|
93
|
+
ListeningServer,
|
|
94
|
+
RequestHandler,
|
|
95
|
+
Transport,
|
|
96
|
+
TransportOptions,
|
|
97
|
+
} from './lib/transports/transport.js';
|
|
98
|
+
export { uwsTransport } from './lib/transports/uws.js';
|
|
99
|
+
export type {
|
|
100
|
+
TransportSocket,
|
|
101
|
+
WebSocket,
|
|
102
|
+
WebSocketHandlers,
|
|
103
|
+
} from './lib/websocket.js';
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
import { unauthorized } from '../errors.js';
|
|
4
|
+
import type { AuthStrategy, Principal } from './strategy.js';
|
|
5
|
+
|
|
6
|
+
export interface ApiKeyAuthOptions {
|
|
7
|
+
/** Strategy name, used in OpenAPI. Defaults to `apiKey`. */
|
|
8
|
+
name?: string;
|
|
9
|
+
/** Header carrying the key. Defaults to `x-api-key`. */
|
|
10
|
+
header?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Valid keys by principal id, or a function looking a key up (for keys in
|
|
13
|
+
* a database). Keys in a record are compared in constant time.
|
|
14
|
+
*/
|
|
15
|
+
keys:
|
|
16
|
+
| Readonly<Record<string, string>>
|
|
17
|
+
| ((key: string) => Principal | undefined | Promise<Principal | undefined>);
|
|
18
|
+
/** Roles per principal id, for keys given as a record. */
|
|
19
|
+
roles?: Readonly<Record<string, readonly string[]>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Authenticates requests by an API key in a header. */
|
|
23
|
+
export function apiKeyAuth(options: ApiKeyAuthOptions): AuthStrategy {
|
|
24
|
+
const name = options.name ?? 'apiKey';
|
|
25
|
+
const header = (options.header ?? 'x-api-key').toLowerCase();
|
|
26
|
+
const { keys } = options;
|
|
27
|
+
|
|
28
|
+
// Compare digests so neither the key nor its length leaks through timing.
|
|
29
|
+
const digests =
|
|
30
|
+
typeof keys === 'function'
|
|
31
|
+
? []
|
|
32
|
+
: Object.entries(keys).map(([id, key]) => ({ id, digest: digest(key) }));
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
name,
|
|
36
|
+
scheme: { type: 'apiKey', in: 'header', name: header },
|
|
37
|
+
async authenticate(request) {
|
|
38
|
+
const key = request.headers[header];
|
|
39
|
+
if (key === undefined || key === '') {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (typeof keys === 'function') {
|
|
44
|
+
const found = await keys(key);
|
|
45
|
+
if (found === undefined) {
|
|
46
|
+
throw unauthorized('The API key is not valid.');
|
|
47
|
+
}
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const given = digest(key);
|
|
52
|
+
let match: string | undefined;
|
|
53
|
+
for (const entry of digests) {
|
|
54
|
+
if (timingSafeEqual(entry.digest, given)) {
|
|
55
|
+
match ??= entry.id;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (match === undefined) {
|
|
59
|
+
throw unauthorized('The API key is not valid.');
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
id: match,
|
|
63
|
+
strategy: name,
|
|
64
|
+
roles: options.roles?.[match] ?? [],
|
|
65
|
+
claims: {},
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function digest(value: string): Buffer {
|
|
72
|
+
return createHash('sha256').update(value).digest();
|
|
73
|
+
}
|