@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.
Files changed (119) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +412 -2
  3. package/dist/index.d.ts +20 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +16 -0
  6. package/dist/lib/auth/api-key.d.ts +17 -0
  7. package/dist/lib/auth/api-key.d.ts.map +1 -0
  8. package/dist/lib/auth/api-key.js +48 -0
  9. package/dist/lib/auth/jwt.d.ts +33 -0
  10. package/dist/lib/auth/jwt.d.ts.map +1 -0
  11. package/dist/lib/auth/jwt.js +227 -0
  12. package/dist/lib/auth/provide-auth.d.ts +30 -0
  13. package/dist/lib/auth/provide-auth.d.ts.map +1 -0
  14. package/dist/lib/auth/provide-auth.js +69 -0
  15. package/dist/lib/auth/strategy.d.ts +47 -0
  16. package/dist/lib/auth/strategy.d.ts.map +1 -0
  17. package/dist/lib/auth/strategy.js +18 -0
  18. package/dist/lib/compile.d.ts +46 -0
  19. package/dist/lib/compile.d.ts.map +1 -0
  20. package/dist/lib/compile.js +126 -0
  21. package/dist/lib/compression.d.ts +23 -0
  22. package/dist/lib/compression.d.ts.map +1 -0
  23. package/dist/lib/compression.js +86 -0
  24. package/dist/lib/config.d.ts +173 -0
  25. package/dist/lib/config.d.ts.map +1 -0
  26. package/dist/lib/config.js +93 -0
  27. package/dist/lib/cors.d.ts +41 -0
  28. package/dist/lib/cors.d.ts.map +1 -0
  29. package/dist/lib/cors.js +101 -0
  30. package/dist/lib/docs.d.ts +15 -0
  31. package/dist/lib/docs.d.ts.map +1 -0
  32. package/dist/lib/docs.js +93 -0
  33. package/dist/lib/engine.d.ts +83 -0
  34. package/dist/lib/engine.d.ts.map +1 -0
  35. package/dist/lib/engine.js +562 -0
  36. package/dist/lib/errors.d.ts +31 -0
  37. package/dist/lib/errors.d.ts.map +1 -0
  38. package/dist/lib/errors.js +55 -0
  39. package/dist/lib/etag.d.ts +5 -0
  40. package/dist/lib/etag.d.ts.map +1 -0
  41. package/dist/lib/etag.js +30 -0
  42. package/dist/lib/headers.d.ts +3 -0
  43. package/dist/lib/headers.d.ts.map +1 -0
  44. package/dist/lib/headers.js +14 -0
  45. package/dist/lib/json-schema.d.ts +11 -0
  46. package/dist/lib/json-schema.d.ts.map +1 -0
  47. package/dist/lib/json-schema.js +23 -0
  48. package/dist/lib/openapi.d.ts +18 -0
  49. package/dist/lib/openapi.d.ts.map +1 -0
  50. package/dist/lib/openapi.js +218 -0
  51. package/dist/lib/rate-limit.d.ts +52 -0
  52. package/dist/lib/rate-limit.d.ts.map +1 -0
  53. package/dist/lib/rate-limit.js +87 -0
  54. package/dist/lib/reply.d.ts +27 -0
  55. package/dist/lib/reply.d.ts.map +1 -0
  56. package/dist/lib/reply.js +52 -0
  57. package/dist/lib/request.d.ts +55 -0
  58. package/dist/lib/request.d.ts.map +1 -0
  59. package/dist/lib/request.js +36 -0
  60. package/dist/lib/router.d.ts +35 -0
  61. package/dist/lib/router.d.ts.map +1 -0
  62. package/dist/lib/router.js +173 -0
  63. package/dist/lib/routes.d.ts +196 -0
  64. package/dist/lib/routes.d.ts.map +1 -0
  65. package/dist/lib/routes.js +98 -0
  66. package/dist/lib/security-headers.d.ts +19 -0
  67. package/dist/lib/security-headers.d.ts.map +1 -0
  68. package/dist/lib/security-headers.js +22 -0
  69. package/dist/lib/serializer.d.ts +35 -0
  70. package/dist/lib/serializer.d.ts.map +1 -0
  71. package/dist/lib/serializer.js +147 -0
  72. package/dist/lib/server.d.ts +86 -0
  73. package/dist/lib/server.d.ts.map +1 -0
  74. package/dist/lib/server.js +255 -0
  75. package/dist/lib/testing.d.ts +21 -0
  76. package/dist/lib/testing.d.ts.map +1 -0
  77. package/dist/lib/testing.js +62 -0
  78. package/dist/lib/transports/node.d.ts +4 -0
  79. package/dist/lib/transports/node.d.ts.map +1 -0
  80. package/dist/lib/transports/node.js +249 -0
  81. package/dist/lib/transports/transport.d.ts +32 -0
  82. package/dist/lib/transports/transport.d.ts.map +1 -0
  83. package/dist/lib/transports/transport.js +1 -0
  84. package/dist/lib/transports/uws.d.ts +8 -0
  85. package/dist/lib/transports/uws.d.ts.map +1 -0
  86. package/dist/lib/transports/uws.js +246 -0
  87. package/dist/lib/websocket.d.ts +71 -0
  88. package/dist/lib/websocket.d.ts.map +1 -0
  89. package/dist/lib/websocket.js +125 -0
  90. package/package.json +67 -3
  91. package/src/index.ts +103 -0
  92. package/src/lib/auth/api-key.ts +73 -0
  93. package/src/lib/auth/jwt.ts +339 -0
  94. package/src/lib/auth/provide-auth.ts +99 -0
  95. package/src/lib/auth/strategy.ts +66 -0
  96. package/src/lib/compile.ts +207 -0
  97. package/src/lib/compression.ts +121 -0
  98. package/src/lib/config.ts +107 -0
  99. package/src/lib/cors.ts +146 -0
  100. package/src/lib/docs.ts +115 -0
  101. package/src/lib/engine.ts +770 -0
  102. package/src/lib/errors.ts +84 -0
  103. package/src/lib/etag.ts +37 -0
  104. package/src/lib/headers.ts +18 -0
  105. package/src/lib/json-schema.ts +36 -0
  106. package/src/lib/openapi.ts +299 -0
  107. package/src/lib/rate-limit.ts +142 -0
  108. package/src/lib/reply.ts +70 -0
  109. package/src/lib/request.ts +83 -0
  110. package/src/lib/router.ts +217 -0
  111. package/src/lib/routes.ts +414 -0
  112. package/src/lib/security-headers.ts +38 -0
  113. package/src/lib/serializer.ts +203 -0
  114. package/src/lib/server.ts +352 -0
  115. package/src/lib/testing.ts +88 -0
  116. package/src/lib/transports/node.ts +315 -0
  117. package/src/lib/transports/transport.ts +39 -0
  118. package/src/lib/transports/uws.ts +310 -0
  119. package/src/lib/websocket.ts +184 -0
@@ -0,0 +1,184 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+
3
+ import {
4
+ type Injector,
5
+ type Logger,
6
+ randomHex,
7
+ runInInjectionContext,
8
+ } from '@tessellation/core';
9
+
10
+ import { Reply } from './reply.js';
11
+
12
+ /** A WebSocket connection, as seen by handlers. */
13
+ export interface WebSocket<Data = unknown> {
14
+ /** Unique per connection; also on log records written by its handlers. */
15
+ readonly id: string;
16
+ /** What the route's `upgrade` returned. */
17
+ readonly data: Data;
18
+ readonly remoteAddress: string;
19
+ /** Sends a text (string) or binary message. False if the socket is closed or backed up. */
20
+ send(message: string | Uint8Array): boolean;
21
+ close(code?: number, reason?: string): void;
22
+ /** Receives messages published to `topic`. */
23
+ subscribe(topic: string): void;
24
+ unsubscribe(topic: string): void;
25
+ /** Sends to every other subscriber of `topic`. */
26
+ publish(topic: string, message: string | Uint8Array): void;
27
+ }
28
+
29
+ /** What a transport provides for one upgraded connection. */
30
+ export interface TransportSocket {
31
+ readonly remoteAddress: string;
32
+ send(message: string | Uint8Array): boolean;
33
+ close(code?: number, reason?: string): void;
34
+ subscribe(topic: string): void;
35
+ unsubscribe(topic: string): void;
36
+ publish(topic: string, message: string | Uint8Array): void;
37
+ }
38
+
39
+ export interface WebSocketHandlers<Data> {
40
+ open?: (socket: WebSocket<Data>) => unknown;
41
+ /** Text frames arrive as strings, binary frames as bytes. */
42
+ message?: (socket: WebSocket<Data>, message: string | Uint8Array) => unknown;
43
+ close?: (socket: WebSocket<Data>, code: number, reason: string) => unknown;
44
+ }
45
+
46
+ /** The reply that accepts an upgrade; the transport completes the handshake. */
47
+ export class UpgradeReply extends Reply<undefined> {
48
+ constructor(
49
+ readonly data: unknown,
50
+ /** Runs a function in the async context of the upgrade request. */
51
+ readonly snapshot: <R>(fn: () => R) => R,
52
+ ) {
53
+ super(101, undefined);
54
+ }
55
+ }
56
+
57
+ /** Captures the current async context (principal, log fields, span, ...). */
58
+ export function captureContext(): <R>(fn: () => R) => R {
59
+ return AsyncLocalStorage.snapshot();
60
+ }
61
+
62
+ /**
63
+ * One accepted connection: runs its handlers in the upgrade request's async
64
+ * context and the application's injection context, and reports errors.
65
+ */
66
+ export class WebSocketSession {
67
+ readonly id = randomHex(8);
68
+ private socket: WebSocketFacade | undefined;
69
+
70
+ constructor(
71
+ readonly route: string,
72
+ private readonly handlers: WebSocketHandlers<unknown>,
73
+ private readonly reply: UpgradeReply,
74
+ private readonly injector: Injector,
75
+ private readonly logger: Logger,
76
+ private readonly lifecycle: {
77
+ opened(session: WebSocketSession): void;
78
+ closed(session: WebSocketSession): void;
79
+ },
80
+ ) {}
81
+
82
+ /** Called by the transport once the handshake is complete. */
83
+ opened(transport: TransportSocket): void {
84
+ this.socket = new WebSocketFacade(this.id, this.reply.data, transport);
85
+ this.lifecycle.opened(this);
86
+ this.call('open', (handlers, socket) => handlers.open?.(socket));
87
+ }
88
+
89
+ received(message: string | Uint8Array): void {
90
+ this.call('message', (handlers, socket) =>
91
+ handlers.message?.(socket, message),
92
+ );
93
+ }
94
+
95
+ closed(code: number, reason: string): void {
96
+ if (this.socket === undefined) {
97
+ return;
98
+ }
99
+ this.socket.closed = true;
100
+ this.lifecycle.closed(this);
101
+ this.call('close', (handlers, socket) =>
102
+ handlers.close?.(socket, code, reason),
103
+ );
104
+ }
105
+
106
+ /** Closes the connection, such as on shutdown. */
107
+ close(code: number, reason: string): void {
108
+ this.socket?.close(code, reason);
109
+ }
110
+
111
+ private call(
112
+ event: string,
113
+ invoke: (
114
+ handlers: WebSocketHandlers<unknown>,
115
+ socket: WebSocket,
116
+ ) => unknown,
117
+ ): void {
118
+ const socket = this.socket;
119
+ if (socket === undefined) {
120
+ return;
121
+ }
122
+ this.reply.snapshot(() => {
123
+ let result: unknown;
124
+ try {
125
+ result = runInInjectionContext(this.injector, () =>
126
+ invoke(this.handlers, socket),
127
+ );
128
+ } catch (error) {
129
+ this.fail(event, error);
130
+ return;
131
+ }
132
+ if (result instanceof Promise) {
133
+ result.catch((error: unknown) => this.fail(event, error));
134
+ }
135
+ });
136
+ }
137
+
138
+ private fail(event: string, error: unknown): void {
139
+ this.logger.error('WebSocket handler failed', {
140
+ event,
141
+ route: this.route,
142
+ connection: this.id,
143
+ err: error,
144
+ });
145
+ this.socket?.close(1011, 'Internal error');
146
+ }
147
+ }
148
+
149
+ class WebSocketFacade implements WebSocket {
150
+ closed = false;
151
+
152
+ constructor(
153
+ readonly id: string,
154
+ readonly data: unknown,
155
+ private readonly transport: TransportSocket,
156
+ ) {}
157
+
158
+ get remoteAddress(): string {
159
+ return this.transport.remoteAddress;
160
+ }
161
+
162
+ send(message: string | Uint8Array): boolean {
163
+ return !this.closed && this.transport.send(message);
164
+ }
165
+
166
+ close(code = 1000, reason = ''): void {
167
+ if (!this.closed) {
168
+ this.closed = true;
169
+ this.transport.close(code, reason);
170
+ }
171
+ }
172
+
173
+ subscribe(topic: string): void {
174
+ if (!this.closed) this.transport.subscribe(topic);
175
+ }
176
+
177
+ unsubscribe(topic: string): void {
178
+ if (!this.closed) this.transport.unsubscribe(topic);
179
+ }
180
+
181
+ publish(topic: string, message: string | Uint8Array): void {
182
+ if (!this.closed) this.transport.publish(topic, message);
183
+ }
184
+ }