@nmtjs/http-transport 0.15.0-beta.2 → 0.15.0-beta.4

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/src/server.ts ADDED
@@ -0,0 +1,365 @@
1
+ import { Buffer } from 'node:buffer'
2
+ import { Duplex, Readable } from 'node:stream'
3
+
4
+ import type {
5
+ GatewayApiCallOptions,
6
+ TransportWorker,
7
+ TransportWorkerParams,
8
+ } from '@nmtjs/gateway'
9
+ import { anyAbortSignal, isAbortError, isAsyncIterable } from '@nmtjs/common'
10
+ import { provide } from '@nmtjs/core'
11
+ import {
12
+ ConnectionType,
13
+ ErrorCode,
14
+ ProtocolBlob,
15
+ ProtocolVersion,
16
+ } from '@nmtjs/protocol'
17
+ import {
18
+ ProtocolClientStream,
19
+ ProtocolError,
20
+ UnsupportedContentTypeError,
21
+ UnsupportedFormatError,
22
+ } from '@nmtjs/protocol/server'
23
+
24
+ import type {
25
+ HttpAdapterParams,
26
+ HttpAdapterServer,
27
+ HttpAdapterServerFactory,
28
+ HttpTransportCorsCustomParams,
29
+ HttpTransportCorsOptions,
30
+ HttpTransportOptions,
31
+ HttpTransportServerRequest,
32
+ } from './types.ts'
33
+ import {
34
+ AllowedHttpMethod,
35
+ HttpCodeMap,
36
+ HttpStatus,
37
+ HttpStatusText,
38
+ } from './constants.ts'
39
+ import * as injections from './injectables.ts'
40
+
41
+ const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob'
42
+ const DEFAULT_ALLOWED_METHODS = Object.freeze(['post']) as ('get' | 'post')[]
43
+ const DEFAULT_CORS_PARAMS = Object.freeze({
44
+ allowCredentials: 'true',
45
+ allowMethods: ['GET', 'POST'],
46
+ allowHeaders: [
47
+ 'Content-Type',
48
+ 'Content-Disposition',
49
+ 'Content-Length',
50
+ 'Accept',
51
+ 'Transfer-Encoding',
52
+ ],
53
+ maxAge: undefined,
54
+ requestMethod: undefined,
55
+ exposeHeaders: [],
56
+ requestHeaders: [],
57
+ }) satisfies HttpTransportCorsCustomParams
58
+ const CORS_HEADERS_MAP: Record<
59
+ keyof HttpTransportCorsCustomParams | 'origin',
60
+ string
61
+ > = {
62
+ origin: 'Access-Control-Allow-Origin',
63
+ allowMethods: 'Access-Control-Allow-Methods',
64
+ allowHeaders: 'Access-Control-Allow-Headers',
65
+ allowCredentials: 'Access-Control-Allow-Credentials',
66
+ maxAge: 'Access-Control-Max-Age',
67
+ exposeHeaders: 'Access-Control-Expose-Headers',
68
+ requestHeaders: 'Access-Control-Request-Headers',
69
+ requestMethod: 'Access-Control-Request-Method',
70
+ }
71
+
72
+ export function createHTTPTransportWorker(
73
+ adapterFactory: HttpAdapterServerFactory<any>,
74
+ options: HttpTransportOptions,
75
+ ): TransportWorker<ConnectionType.Unidirectional> {
76
+ return new HttpTransportServer(adapterFactory, options)
77
+ }
78
+
79
+ export class HttpTransportServer
80
+ implements TransportWorker<ConnectionType.Unidirectional>
81
+ {
82
+ #server: HttpAdapterServer
83
+ #corsOptions?:
84
+ | null
85
+ | true
86
+ | string[]
87
+ | HttpTransportCorsOptions
88
+ | ((origin: string) => boolean | HttpTransportCorsOptions)
89
+
90
+ params!: TransportWorkerParams<ConnectionType.Unidirectional>
91
+
92
+ constructor(
93
+ protected readonly adapterFactory: HttpAdapterServerFactory<any>,
94
+ protected readonly options: HttpTransportOptions,
95
+ ) {
96
+ this.#server = this.createServer()
97
+ this.#corsOptions = this.options.cors
98
+ }
99
+
100
+ async start(hooks: TransportWorkerParams<ConnectionType.Unidirectional>) {
101
+ this.params = hooks
102
+ return await this.#server.start()
103
+ }
104
+
105
+ async stop() {
106
+ await this.#server.stop()
107
+ }
108
+
109
+ async httpHandler(
110
+ request: HttpTransportServerRequest,
111
+ body: ReadableStream | null,
112
+ requestSignal: AbortSignal,
113
+ ): Promise<Response> {
114
+ const url = new URL(request.url)
115
+ const procedure = url.pathname.slice(1) // remove leading '/'
116
+ const method = request.method.toLowerCase()
117
+ const origin = request.headers.get('origin')
118
+ const responseHeaders = new Headers()
119
+ if (origin) this.applyCors(origin, request, responseHeaders)
120
+
121
+ // Handle preflight requests
122
+ if (method === 'options') {
123
+ return new Response(null, {
124
+ status: HttpStatus.OK,
125
+ headers: responseHeaders,
126
+ })
127
+ }
128
+
129
+ const controller = new AbortController()
130
+ const signal = anyAbortSignal(requestSignal, controller.signal)
131
+ const canHaveBody = method !== 'get'
132
+ const isBlob = request.headers.get(NEEMATA_BLOB_HEADER) === 'true'
133
+ const contentType = request.headers.get('content-type')
134
+ const accept = request.headers.get('accept') || '*/*'
135
+
136
+ await using connection = await this.params.onConnect({
137
+ accept,
138
+ contentType: isBlob ? '*/*' : contentType,
139
+ data: request,
140
+ protocolVersion: ProtocolVersion.v1,
141
+ type: ConnectionType.Unidirectional,
142
+ })
143
+
144
+ try {
145
+ // Parse request body if present
146
+ let payload: any
147
+ if (canHaveBody && body) {
148
+ const bodyStream = Readable.fromWeb(body as any)
149
+ const cannotDecode =
150
+ !contentType || !this.params.formats.supportsDecoder(contentType)
151
+ if (isBlob || cannotDecode) {
152
+ const type = contentType || 'application/octet-stream'
153
+ const contentLength = request.headers.get('content-length')
154
+ const size = contentLength
155
+ ? Number.parseInt(contentLength)
156
+ : undefined
157
+ payload = new ProtocolClientStream(-1, { size, type })
158
+ bodyStream.pipe(payload)
159
+ } else {
160
+ const buffer = Buffer.concat(await bodyStream.toArray())
161
+ if (buffer.byteLength > 0) {
162
+ payload = connection.decoder.decode(buffer)
163
+ }
164
+ }
165
+ } else {
166
+ const querystring = url.searchParams.get('payload')
167
+ if (querystring) {
168
+ payload = JSON.parse(querystring)
169
+ }
170
+ }
171
+
172
+ const metadata: GatewayApiCallOptions['metadata'] = (metadata) => {
173
+ const allowHttpMethod =
174
+ metadata.get(AllowedHttpMethod) ?? DEFAULT_ALLOWED_METHODS
175
+ if (!allowHttpMethod.includes(method as any)) {
176
+ throw new ProtocolError(ErrorCode.NotFound)
177
+ }
178
+ }
179
+
180
+ const result = await this.params.onRpc(
181
+ connection,
182
+ {
183
+ callId: 0, // since the connection is closed after the call, only one call exists per connection
184
+ payload,
185
+ procedure,
186
+ metadata,
187
+ },
188
+ signal,
189
+ provide(injections.httpResponseHeaders, responseHeaders),
190
+ )
191
+
192
+ // Handle blob responses
193
+ if (result instanceof ProtocolBlob) {
194
+ const { source, metadata } = result
195
+ const { type } = metadata
196
+
197
+ responseHeaders.set(NEEMATA_BLOB_HEADER, 'true')
198
+ responseHeaders.set('Content-Type', type)
199
+ if (metadata.size) {
200
+ responseHeaders.set('Content-Length', metadata.size.toString())
201
+ }
202
+
203
+ // Convert source to ReadableStream
204
+ let stream: ReadableStream
205
+
206
+ if (source instanceof ReadableStream) {
207
+ stream = source
208
+ } else if (source instanceof Readable || source instanceof Duplex) {
209
+ stream = Readable.toWeb(source) as unknown as ReadableStream
210
+ } else {
211
+ throw new Error('Invalid stream source')
212
+ }
213
+
214
+ return new Response(stream, {
215
+ status: HttpStatus.OK,
216
+ statusText: HttpStatusText[HttpStatus.OK],
217
+ headers: responseHeaders,
218
+ })
219
+ } else if (isAsyncIterable(result)) {
220
+ responseHeaders.set('Content-Type', connection.encoder.contentType)
221
+ responseHeaders.set('Transfer-Encoding', 'chunked')
222
+ const stream = new ReadableStream({
223
+ async start(controller) {
224
+ try {
225
+ for await (const chunk of result) {
226
+ const encoded = connection.encoder.encode(chunk)
227
+ const base64 = Buffer.from(
228
+ encoded.buffer,
229
+ encoded.byteOffset,
230
+ encoded.byteLength,
231
+ ).toString('base64')
232
+ controller.enqueue(`data: ${base64}\n\n`)
233
+ }
234
+ controller.close()
235
+ } catch (error) {
236
+ if (isAbortError(error)) controller.close()
237
+ else controller.error(error)
238
+ }
239
+ },
240
+ })
241
+ return new Response(stream, {
242
+ status: HttpStatus.OK,
243
+ statusText: HttpStatusText[HttpStatus.OK],
244
+ headers: responseHeaders,
245
+ })
246
+ } else {
247
+ // Handle regular responses
248
+ const buffer = connection.encoder.encode(result)
249
+ responseHeaders.set('Content-Type', connection.encoder.contentType)
250
+
251
+ // @ts-expect-error
252
+ return new Response(buffer, {
253
+ status: HttpStatus.OK,
254
+ statusText: HttpStatusText[HttpStatus.OK],
255
+ headers: responseHeaders,
256
+ })
257
+ }
258
+ } catch (error) {
259
+ console.error(error)
260
+ if (error instanceof UnsupportedFormatError) {
261
+ const status =
262
+ error instanceof UnsupportedContentTypeError
263
+ ? HttpStatus.UnsupportedMediaType
264
+ : HttpStatus.NotAcceptable
265
+ const text = HttpStatusText[status]
266
+
267
+ return new Response(text, {
268
+ status,
269
+ statusText: text,
270
+ headers: responseHeaders,
271
+ })
272
+ }
273
+
274
+ if (error instanceof ProtocolError) {
275
+ const status =
276
+ error.code in HttpCodeMap
277
+ ? HttpCodeMap[error.code]
278
+ : HttpStatus.InternalServerError
279
+ const text = HttpStatusText[status]
280
+ const payload = connection.encoder.encode(error)
281
+ responseHeaders.set('Content-Type', connection.encoder.contentType)
282
+
283
+ // @ts-expect-error
284
+ return new Response(payload, {
285
+ status,
286
+ statusText: text,
287
+ headers: responseHeaders,
288
+ })
289
+ }
290
+
291
+ // Unknown error
292
+ // this.logError(error, 'Unknown error while processing HTTP request')
293
+ console.error(error)
294
+
295
+ const payload = connection.encoder.encode(
296
+ new ProtocolError(
297
+ ErrorCode.InternalServerError,
298
+ 'Internal Server Error',
299
+ ),
300
+ )
301
+ responseHeaders.set('Content-Type', connection.encoder.contentType)
302
+
303
+ // @ts-expect-error
304
+ return new Response(payload, {
305
+ status: HttpStatus.InternalServerError,
306
+ statusText: HttpStatusText[HttpStatus.InternalServerError],
307
+ headers: responseHeaders,
308
+ })
309
+ }
310
+ }
311
+
312
+ private applyCors(
313
+ origin: string,
314
+ request: HttpTransportServerRequest,
315
+ headers: Headers,
316
+ ) {
317
+ if (!this.#corsOptions) return
318
+
319
+ let params: HttpTransportCorsCustomParams | null = null
320
+
321
+ if (this.options.cors === true) {
322
+ params = { ...DEFAULT_CORS_PARAMS }
323
+ } else if (
324
+ Array.isArray(this.options.cors) &&
325
+ this.options.cors.includes(origin)
326
+ ) {
327
+ params = { ...DEFAULT_CORS_PARAMS }
328
+ } else if (typeof this.options.cors === 'function') {
329
+ const result = this.options.cors(origin, request)
330
+ if (typeof result === 'boolean') {
331
+ if (result) {
332
+ params = { ...DEFAULT_CORS_PARAMS }
333
+ }
334
+ } else if (typeof result === 'object') {
335
+ params = { ...DEFAULT_CORS_PARAMS }
336
+ for (const key in DEFAULT_CORS_PARAMS) {
337
+ params[key] = result[key]
338
+ }
339
+ }
340
+ }
341
+
342
+ if (params === null) return
343
+
344
+ headers.set(CORS_HEADERS_MAP.origin, origin)
345
+
346
+ for (const key in params) {
347
+ const header = CORS_HEADERS_MAP[key]
348
+ if (header) {
349
+ let value = params[key]
350
+ if (Array.isArray(value)) value = value.filter(Boolean).join(', ')
351
+ if (value) headers.set(header, value)
352
+ }
353
+ }
354
+ }
355
+
356
+ private createServer() {
357
+ // const hooks = this.createWsHooks()
358
+ const opts: HttpAdapterParams = {
359
+ ...this.options,
360
+ // logger: this.logger.child({ $lable: 'WsServer' }),
361
+ fetchHandler: this.httpHandler.bind(this),
362
+ }
363
+ return this.adapterFactory(opts)
364
+ }
365
+ }
package/src/types.ts ADDED
@@ -0,0 +1,102 @@
1
+ import type { Async, OneOf } from '@nmtjs/common'
2
+
3
+ export type HttpTransportServerRequest = {
4
+ url: URL
5
+ method: string
6
+ headers: Headers
7
+ }
8
+
9
+ export type HttpTransportOptions<
10
+ R extends keyof HttpTransportRuntimes = keyof HttpTransportRuntimes,
11
+ > = {
12
+ listen: HttpTransportListenOptions
13
+ cors?: HttpTransportCorsOptions
14
+ tls?: HttpTransportTlsOptions
15
+ runtime?: HttpTransportRuntimes[R]
16
+ }
17
+
18
+ export type HttpTransportCorsCustomParams = {
19
+ allowMethods?: string[]
20
+ allowHeaders?: string[]
21
+ allowCredentials?: string
22
+ maxAge?: string
23
+ exposeHeaders?: string[]
24
+ requestHeaders?: string[]
25
+ requestMethod?: string
26
+ }
27
+
28
+ export type HttpTransportCorsOptions =
29
+ | true
30
+ | string[]
31
+ | HttpTransportCorsCustomParams
32
+ | ((
33
+ origin: string,
34
+ request: HttpTransportServerRequest,
35
+ ) => boolean | HttpTransportCorsCustomParams)
36
+
37
+ export type HttpTransportListenOptions = OneOf<
38
+ [{ port: number; hostname?: string; reusePort?: boolean }, { unix: string }]
39
+ >
40
+
41
+ export type HttpTransportRuntimeBun = Partial<
42
+ Pick<
43
+ import('bun').Serve.Options<undefined>,
44
+ 'development' | 'id' | 'maxRequestBodySize' | 'idleTimeout' | 'ipv6Only'
45
+ > &
46
+ import('bun').Serve.Routes<any, any>
47
+ >
48
+
49
+ export type HttpTransportRuntimeNode = {}
50
+
51
+ export type HttpTransportRuntimeDeno = {}
52
+
53
+ export type HttpTransportRuntimes = {
54
+ bun: HttpTransportRuntimeBun
55
+ node: HttpTransportRuntimeNode
56
+ deno: HttpTransportRuntimeDeno
57
+ }
58
+
59
+ export type HttpTransportTlsOptions = {
60
+ /**
61
+ * File path or inlined TLS certificate in PEM format (required).
62
+ */
63
+ cert?: string
64
+ /**
65
+ * File path or inlined TLS private key in PEM format (required).
66
+ */
67
+ key?: string
68
+ /**
69
+ * Passphrase for the private key (optional).
70
+ */
71
+ passphrase?: string
72
+ }
73
+
74
+ export type HttpAdapterParams<
75
+ R extends keyof HttpTransportRuntimes = keyof HttpTransportRuntimes,
76
+ > = {
77
+ listen: HttpTransportListenOptions
78
+ fetchHandler: (
79
+ request: HttpTransportServerRequest,
80
+ body: ReadableStream | null,
81
+ signal: AbortSignal,
82
+ ) => Async<Response>
83
+ cors?: HttpTransportCorsOptions
84
+ tls?: HttpTransportTlsOptions
85
+ runtime?: HttpTransportRuntimes[R]
86
+ }
87
+
88
+ export type DenoServer = ReturnType<typeof globalThis.Deno.serve>
89
+
90
+ export interface HttpAdapterServer {
91
+ runtime: {
92
+ bun?: import('bun').Server<undefined>
93
+ node?: import('uWebSockets.js').TemplatedApp
94
+ deno?: DenoServer
95
+ }
96
+ stop: () => Async<any>
97
+ start: () => Async<string>
98
+ }
99
+
100
+ export type HttpAdapterServerFactory<
101
+ R extends keyof HttpTransportRuntimes = keyof HttpTransportRuntimes,
102
+ > = (params: HttpAdapterParams<R>) => HttpAdapterServer
package/src/utils.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { ErrorCode } from '@nmtjs/protocol'
2
+ import { ProtocolError } from '@nmtjs/protocol/server'
3
+
4
+ export const InternalError = (message = 'Internal Server Error') =>
5
+ new ProtocolError(ErrorCode.InternalServerError, message)
6
+
7
+ export const NotFoundError = (message = 'Not Found') =>
8
+ new ProtocolError(ErrorCode.NotFound, message)
9
+
10
+ export const ForbiddenError = (message = 'Forbidden') =>
11
+ new ProtocolError(ErrorCode.Forbidden, message)
12
+
13
+ export const RequestTimeoutError = (message = 'Request Timeout') =>
14
+ new ProtocolError(ErrorCode.RequestTimeout, message)
15
+
16
+ export const NotFoundHttpResponse = () =>
17
+ new Response('Not Found', {
18
+ status: 404,
19
+ headers: { 'Content-Type': 'text/plain' },
20
+ })
21
+
22
+ export const InternalServerErrorHttpResponse = () =>
23
+ new Response('Internal Server Error', {
24
+ status: 500,
25
+ headers: { 'Content-Type': 'text/plain' },
26
+ })
27
+
28
+ export const StatusResponse = () => new Response('OK', { status: 200 })