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

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.
@@ -0,0 +1,146 @@
1
+ import { createMetadataKey } from '@nmtjs/core';
2
+ import { ErrorCode } from '@nmtjs/protocol';
3
+ export var HttpStatus;
4
+ (function (HttpStatus) {
5
+ HttpStatus[HttpStatus["Continue"] = 100] = "Continue";
6
+ HttpStatus[HttpStatus["SwitchingProtocols"] = 101] = "SwitchingProtocols";
7
+ HttpStatus[HttpStatus["Processing"] = 102] = "Processing";
8
+ HttpStatus[HttpStatus["EarlyHints"] = 103] = "EarlyHints";
9
+ HttpStatus[HttpStatus["OK"] = 200] = "OK";
10
+ HttpStatus[HttpStatus["Created"] = 201] = "Created";
11
+ HttpStatus[HttpStatus["Accepted"] = 202] = "Accepted";
12
+ HttpStatus[HttpStatus["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
13
+ HttpStatus[HttpStatus["NoContent"] = 204] = "NoContent";
14
+ HttpStatus[HttpStatus["ResetContent"] = 205] = "ResetContent";
15
+ HttpStatus[HttpStatus["PartialContent"] = 206] = "PartialContent";
16
+ HttpStatus[HttpStatus["MultiStatus"] = 207] = "MultiStatus";
17
+ HttpStatus[HttpStatus["AlreadyReported"] = 208] = "AlreadyReported";
18
+ HttpStatus[HttpStatus["IMUsed"] = 226] = "IMUsed";
19
+ HttpStatus[HttpStatus["MultipleChoices"] = 300] = "MultipleChoices";
20
+ HttpStatus[HttpStatus["MovedPermanently"] = 301] = "MovedPermanently";
21
+ HttpStatus[HttpStatus["Found"] = 302] = "Found";
22
+ HttpStatus[HttpStatus["SeeOther"] = 303] = "SeeOther";
23
+ HttpStatus[HttpStatus["NotModified"] = 304] = "NotModified";
24
+ HttpStatus[HttpStatus["UseProxy"] = 305] = "UseProxy";
25
+ HttpStatus[HttpStatus["TemporaryRedirect"] = 307] = "TemporaryRedirect";
26
+ HttpStatus[HttpStatus["PermanentRedirect"] = 308] = "PermanentRedirect";
27
+ HttpStatus[HttpStatus["BadRequest"] = 400] = "BadRequest";
28
+ HttpStatus[HttpStatus["Unauthorized"] = 401] = "Unauthorized";
29
+ HttpStatus[HttpStatus["PaymentRequired"] = 402] = "PaymentRequired";
30
+ HttpStatus[HttpStatus["Forbidden"] = 403] = "Forbidden";
31
+ HttpStatus[HttpStatus["NotFound"] = 404] = "NotFound";
32
+ HttpStatus[HttpStatus["MethodNotAllowed"] = 405] = "MethodNotAllowed";
33
+ HttpStatus[HttpStatus["NotAcceptable"] = 406] = "NotAcceptable";
34
+ HttpStatus[HttpStatus["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
35
+ HttpStatus[HttpStatus["RequestTimeout"] = 408] = "RequestTimeout";
36
+ HttpStatus[HttpStatus["Conflict"] = 409] = "Conflict";
37
+ HttpStatus[HttpStatus["Gone"] = 410] = "Gone";
38
+ HttpStatus[HttpStatus["LengthRequired"] = 411] = "LengthRequired";
39
+ HttpStatus[HttpStatus["PreconditionFailed"] = 412] = "PreconditionFailed";
40
+ HttpStatus[HttpStatus["PayloadTooLarge"] = 413] = "PayloadTooLarge";
41
+ HttpStatus[HttpStatus["URITooLong"] = 414] = "URITooLong";
42
+ HttpStatus[HttpStatus["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
43
+ HttpStatus[HttpStatus["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
44
+ HttpStatus[HttpStatus["ExpectationFailed"] = 417] = "ExpectationFailed";
45
+ HttpStatus[HttpStatus["ImATeapot"] = 418] = "ImATeapot";
46
+ HttpStatus[HttpStatus["MisdirectedRequest"] = 421] = "MisdirectedRequest";
47
+ HttpStatus[HttpStatus["UnprocessableEntity"] = 422] = "UnprocessableEntity";
48
+ HttpStatus[HttpStatus["Locked"] = 423] = "Locked";
49
+ HttpStatus[HttpStatus["FailedDependency"] = 424] = "FailedDependency";
50
+ HttpStatus[HttpStatus["TooEarly"] = 425] = "TooEarly";
51
+ HttpStatus[HttpStatus["UpgradeRequired"] = 426] = "UpgradeRequired";
52
+ HttpStatus[HttpStatus["PreconditionRequired"] = 428] = "PreconditionRequired";
53
+ HttpStatus[HttpStatus["TooManyRequests"] = 429] = "TooManyRequests";
54
+ HttpStatus[HttpStatus["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
55
+ HttpStatus[HttpStatus["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
56
+ HttpStatus[HttpStatus["InternalServerError"] = 500] = "InternalServerError";
57
+ HttpStatus[HttpStatus["NotImplemented"] = 501] = "NotImplemented";
58
+ HttpStatus[HttpStatus["BadGateway"] = 502] = "BadGateway";
59
+ HttpStatus[HttpStatus["ServiceUnavailable"] = 503] = "ServiceUnavailable";
60
+ HttpStatus[HttpStatus["GatewayTimeout"] = 504] = "GatewayTimeout";
61
+ HttpStatus[HttpStatus["HTTPVersionNotSupported"] = 505] = "HTTPVersionNotSupported";
62
+ HttpStatus[HttpStatus["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
63
+ HttpStatus[HttpStatus["InsufficientStorage"] = 507] = "InsufficientStorage";
64
+ HttpStatus[HttpStatus["LoopDetected"] = 508] = "LoopDetected";
65
+ HttpStatus[HttpStatus["NotExtended"] = 510] = "NotExtended";
66
+ HttpStatus[HttpStatus["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
67
+ })(HttpStatus || (HttpStatus = {}));
68
+ export const HttpStatusText = {
69
+ [HttpStatus.Continue]: 'Continue',
70
+ [HttpStatus.SwitchingProtocols]: 'Switching Protocols',
71
+ [HttpStatus.Processing]: 'Processing',
72
+ [HttpStatus.EarlyHints]: 'Early Hints',
73
+ [HttpStatus.OK]: 'OK',
74
+ [HttpStatus.Created]: 'Created',
75
+ [HttpStatus.Accepted]: 'Accepted',
76
+ [HttpStatus.NonAuthoritativeInformation]: 'Non-Authoritative Information',
77
+ [HttpStatus.NoContent]: 'No Content',
78
+ [HttpStatus.ResetContent]: 'Reset Content',
79
+ [HttpStatus.PartialContent]: 'Partial Content',
80
+ [HttpStatus.MultiStatus]: 'Multi-Status',
81
+ [HttpStatus.AlreadyReported]: 'Already Reported',
82
+ [HttpStatus.IMUsed]: 'IM Used',
83
+ [HttpStatus.MultipleChoices]: 'Multiple Choices',
84
+ [HttpStatus.MovedPermanently]: 'Moved Permanently',
85
+ [HttpStatus.Found]: 'Found',
86
+ [HttpStatus.SeeOther]: 'See Other',
87
+ [HttpStatus.NotModified]: 'Not Modified',
88
+ [HttpStatus.UseProxy]: 'Use Proxy',
89
+ [HttpStatus.TemporaryRedirect]: 'Temporary Redirect',
90
+ [HttpStatus.PermanentRedirect]: 'Permanent Redirect',
91
+ [HttpStatus.BadRequest]: 'Bad Request',
92
+ [HttpStatus.Unauthorized]: 'Unauthorized',
93
+ [HttpStatus.PaymentRequired]: 'Payment Required',
94
+ [HttpStatus.Forbidden]: 'Forbidden',
95
+ [HttpStatus.NotFound]: 'Not Found',
96
+ [HttpStatus.MethodNotAllowed]: 'Method Not Allowed',
97
+ [HttpStatus.NotAcceptable]: 'Not Acceptable',
98
+ [HttpStatus.ProxyAuthenticationRequired]: 'Proxy Authentication Required',
99
+ [HttpStatus.RequestTimeout]: 'Request Timeout',
100
+ [HttpStatus.Conflict]: 'Conflict',
101
+ [HttpStatus.Gone]: 'Gone',
102
+ [HttpStatus.LengthRequired]: 'Length Required',
103
+ [HttpStatus.PreconditionFailed]: 'Precondition Failed',
104
+ [HttpStatus.PayloadTooLarge]: 'Payload Too Large',
105
+ [HttpStatus.URITooLong]: 'URI Too Long',
106
+ [HttpStatus.UnsupportedMediaType]: 'Unsupported Media Type',
107
+ [HttpStatus.RangeNotSatisfiable]: 'Range Not Satisfiable',
108
+ [HttpStatus.ExpectationFailed]: 'Expectation Failed',
109
+ [HttpStatus.ImATeapot]: "I'm a Teapot",
110
+ [HttpStatus.MisdirectedRequest]: 'Misdirected Request',
111
+ [HttpStatus.UnprocessableEntity]: 'Unprocessable Entity',
112
+ [HttpStatus.Locked]: 'Locked',
113
+ [HttpStatus.FailedDependency]: 'Failed Dependency',
114
+ [HttpStatus.TooEarly]: 'Too Early',
115
+ [HttpStatus.UpgradeRequired]: 'Upgrade Required',
116
+ [HttpStatus.PreconditionRequired]: 'Precondition Required',
117
+ [HttpStatus.TooManyRequests]: 'Too Many Requests',
118
+ [HttpStatus.RequestHeaderFieldsTooLarge]: 'Request Header Fields Too Large',
119
+ [HttpStatus.UnavailableForLegalReasons]: 'Unavailable For Legal Reasons',
120
+ [HttpStatus.InternalServerError]: 'Internal Server Error',
121
+ [HttpStatus.NotImplemented]: 'Not Implemented',
122
+ [HttpStatus.BadGateway]: 'Bad Gateway',
123
+ [HttpStatus.ServiceUnavailable]: 'Service Unavailable',
124
+ [HttpStatus.GatewayTimeout]: 'Gateway Timeout',
125
+ [HttpStatus.HTTPVersionNotSupported]: 'HTTP Version Not Supported',
126
+ [HttpStatus.VariantAlsoNegotiates]: 'Variant Also Negotiates',
127
+ [HttpStatus.InsufficientStorage]: 'Insufficient Storage',
128
+ [HttpStatus.LoopDetected]: 'Loop Detected',
129
+ [HttpStatus.NotExtended]: 'Not Extended',
130
+ [HttpStatus.NetworkAuthenticationRequired]: 'Network Authentication Required',
131
+ };
132
+ export const HttpCodeMap = {
133
+ [ErrorCode.ValidationError]: HttpStatus.BadRequest,
134
+ [ErrorCode.BadRequest]: HttpStatus.BadRequest,
135
+ [ErrorCode.NotFound]: HttpStatus.NotFound,
136
+ [ErrorCode.Forbidden]: HttpStatus.Forbidden,
137
+ [ErrorCode.Unauthorized]: HttpStatus.Unauthorized,
138
+ [ErrorCode.InternalServerError]: HttpStatus.InternalServerError,
139
+ [ErrorCode.NotAcceptable]: HttpStatus.NotAcceptable,
140
+ [ErrorCode.RequestTimeout]: HttpStatus.RequestTimeout,
141
+ [ErrorCode.GatewayTimeout]: HttpStatus.GatewayTimeout,
142
+ [ErrorCode.ServiceUnavailable]: HttpStatus.ServiceUnavailable,
143
+ [ErrorCode.ClientRequestError]: HttpStatus.BadRequest,
144
+ [ErrorCode.ConnectionError]: HttpStatus.NotAcceptable,
145
+ };
146
+ export const AllowedHttpMethod = createMetadataKey('http:method');
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./injectables.js";
2
+ export * from "./server.js";
3
+ export * from "./types.js";
4
+ export * from "./utils.js";
@@ -0,0 +1,4 @@
1
+ import { createLazyInjectable, Scope } from '@nmtjs/core';
2
+ import { connectionData as connectionDataInjectable } from '@nmtjs/gateway';
3
+ export const connectionData = connectionDataInjectable.$withType();
4
+ export const httpResponseHeaders = createLazyInjectable(Scope.Call);
@@ -0,0 +1,67 @@
1
+ import { ProxyableTransportType } from '@nmtjs/gateway';
2
+ import * as injectables from "../injectables.js";
3
+ import { createHTTPTransportWorker } from "../server.js";
4
+ import { InternalServerErrorHttpResponse, NotFoundHttpResponse, StatusResponse, } from "../utils.js";
5
+ function adapterFactory(params) {
6
+ let server = null;
7
+ function createServer() {
8
+ return globalThis.Bun.serve({
9
+ ...params.runtime,
10
+ unix: params.listen.unix,
11
+ port: params.listen.port ?? 0,
12
+ hostname: params.listen.hostname,
13
+ reusePort: params.listen.reusePort,
14
+ tls: params.tls
15
+ ? {
16
+ cert: params.tls.cert,
17
+ key: params.tls.key,
18
+ passphrase: params.tls.passphrase,
19
+ }
20
+ : undefined,
21
+ // @ts-expect-error
22
+ routes: {
23
+ ...params.runtime?.routes,
24
+ '/healthy': { GET: StatusResponse },
25
+ },
26
+ async fetch(request, server) {
27
+ const url = new URL(request.url);
28
+ try {
29
+ if (request.headers.get('upgrade') === 'websocket')
30
+ return NotFoundHttpResponse();
31
+ const { body, headers, method } = request;
32
+ return await params.fetchHandler({ url, method, headers }, body, request.signal);
33
+ }
34
+ catch (err) {
35
+ // TODO: proper logging
36
+ console.error(err);
37
+ // params.logger.error({ err }, 'Error in fetch handler')
38
+ return InternalServerErrorHttpResponse();
39
+ }
40
+ },
41
+ });
42
+ }
43
+ return {
44
+ runtime: {
45
+ get bun() {
46
+ return server;
47
+ },
48
+ },
49
+ start: async () => {
50
+ server = createServer();
51
+ return server.url.href;
52
+ },
53
+ stop: async () => {
54
+ if (server) {
55
+ await server.stop();
56
+ server = null;
57
+ }
58
+ },
59
+ };
60
+ }
61
+ export const HttpTransport = {
62
+ proxyable: ProxyableTransportType.HTTP,
63
+ injectables,
64
+ factory(options) {
65
+ return createHTTPTransportWorker(adapterFactory, options);
66
+ },
67
+ };
@@ -0,0 +1,94 @@
1
+ import { ProxyableTransportType } from '@nmtjs/gateway';
2
+ import * as injectables from "../injectables.js";
3
+ import { createHTTPTransportWorker } from "../server.js";
4
+ import { InternalServerErrorHttpResponse, NotFoundHttpResponse, StatusResponse, } from "../utils.js";
5
+ function adapterFactory(params) {
6
+ let server = null;
7
+ function createServer() {
8
+ const listenOptions = params.listen.unix
9
+ ? { path: params.listen.unix }
10
+ : {
11
+ port: params.listen.port,
12
+ hostname: params.listen.hostname,
13
+ reusePort: params.listen.reusePort,
14
+ };
15
+ const options = {
16
+ ...listenOptions,
17
+ tls: params.tls
18
+ ? {
19
+ cert: params.tls.cert,
20
+ key: params.tls.key,
21
+ passphrase: params.tls.passphrase,
22
+ }
23
+ : undefined,
24
+ };
25
+ return new Promise((resolve) => {
26
+ const server = globalThis.Deno.serve({
27
+ ...params.runtime,
28
+ ...options,
29
+ handler: async (request, info) => {
30
+ const url = new URL(request.url);
31
+ if (url.pathname === '/healthy') {
32
+ return StatusResponse();
33
+ }
34
+ try {
35
+ if (request.headers.get('upgrade') === 'websocket') {
36
+ return NotFoundHttpResponse();
37
+ }
38
+ const { headers, method, body } = request;
39
+ return await params.fetchHandler({ url, method, headers }, body, request.signal);
40
+ }
41
+ catch (err) {
42
+ // TODO: proper logging
43
+ console.error(err);
44
+ // params.logger.error({ err }, 'Error in fetch handler')
45
+ return InternalServerErrorHttpResponse();
46
+ }
47
+ },
48
+ onListen(addr) {
49
+ setTimeout(() => {
50
+ resolve({ server, addr });
51
+ }, 1);
52
+ },
53
+ });
54
+ });
55
+ }
56
+ return {
57
+ runtime: {
58
+ get deno() {
59
+ return server;
60
+ },
61
+ },
62
+ start: async () => {
63
+ const { server: _server, addr } = await createServer();
64
+ server = _server;
65
+ switch (addr.transport) {
66
+ case 'unix':
67
+ case 'unixpacket':
68
+ return `unix://${addr.path}`;
69
+ case 'tcp':
70
+ case 'udp': {
71
+ const proto = params.tls ? 'https' : 'http';
72
+ return `${proto}://${addr.hostname}:${addr.port}`;
73
+ }
74
+ case 'vsock':
75
+ return `vsock://${addr.cid}:${addr.port}`;
76
+ default:
77
+ throw new Error(`Unsupported address transport`);
78
+ }
79
+ },
80
+ stop: async () => {
81
+ if (server) {
82
+ await server.shutdown();
83
+ server = null;
84
+ }
85
+ },
86
+ };
87
+ }
88
+ export const HttpTransport = {
89
+ proxyable: ProxyableTransportType.HTTP,
90
+ injectables,
91
+ factory(options) {
92
+ return createHTTPTransportWorker(adapterFactory, options);
93
+ },
94
+ };
@@ -0,0 +1,136 @@
1
+ import {} from 'node:dns';
2
+ import { setTimeout } from 'node:timers/promises';
3
+ import { ProxyableTransportType } from '@nmtjs/gateway';
4
+ import * as injectables from "../injectables.js";
5
+ import { createHTTPTransportWorker } from "../server.js";
6
+ import { InternalServerErrorHttpResponse, NotFoundHttpResponse, StatusResponse, } from "../utils.js";
7
+ import { App, SSLApp, us_socket_local_port } from 'uWebSockets.js';
8
+ function adapterFactory(params) {
9
+ const server = params.tls
10
+ ? SSLApp({
11
+ passphrase: params.tls.passphrase,
12
+ key_file_name: params.tls.key,
13
+ cert_file_name: params.tls.cert,
14
+ })
15
+ : App();
16
+ server
17
+ .get('/healthy', async (res) => {
18
+ res.onAborted(() => { });
19
+ const response = StatusResponse();
20
+ res.cork(async () => {
21
+ res
22
+ .writeStatus(`${response.status} ${response.statusText}`)
23
+ .end(await response.arrayBuffer());
24
+ });
25
+ })
26
+ .any('/*', async (res, req) => {
27
+ const controller = new AbortController();
28
+ res.onAborted(() => {
29
+ res.aborted = true;
30
+ controller.abort();
31
+ });
32
+ let response = NotFoundHttpResponse();
33
+ const headers = new Headers();
34
+ const method = req.getMethod();
35
+ req.forEach((k, v) => headers.append(k, v));
36
+ const host = headers.get('host') || 'localhost';
37
+ const proto = headers.get('x-forwarded-proto') || params.tls ? 'https' : 'http';
38
+ const url = new URL(req.getUrl(), `${proto}://${host}`);
39
+ try {
40
+ const body = new ReadableStream({
41
+ start(controller) {
42
+ res.onData((chunk, isLast) => {
43
+ if (chunk) {
44
+ const copy = Buffer.allocUnsafe(chunk.byteLength);
45
+ copy.set(new Uint8Array(chunk));
46
+ controller.enqueue(copy);
47
+ }
48
+ if (isLast)
49
+ controller.close();
50
+ });
51
+ res.onAborted(() => controller.error());
52
+ },
53
+ });
54
+ response = await params.fetchHandler({ url, method, headers }, body, controller.signal);
55
+ }
56
+ catch (err) {
57
+ // TODO: proper logging
58
+ console.error(err);
59
+ // params.logger.error({ err }, 'Error in fetch handler')
60
+ response = InternalServerErrorHttpResponse();
61
+ }
62
+ if (res.aborted)
63
+ return undefined;
64
+ else {
65
+ res.cork(() => {
66
+ if (res.aborted)
67
+ return undefined;
68
+ res.writeStatus(`${response.status.toString()} ${response.statusText}`);
69
+ response.headers.forEach((v, k) => res.writeHeader(k, v));
70
+ });
71
+ if (response.body) {
72
+ try {
73
+ const reader = response.body.getReader();
74
+ let chunk = await reader.read();
75
+ do {
76
+ if (res.aborted)
77
+ break;
78
+ if (chunk.value)
79
+ res.cork(() => res.write(chunk.value));
80
+ chunk = await reader.read();
81
+ } while (!chunk.done);
82
+ if (!res.aborted)
83
+ res.cork(() => res.end());
84
+ }
85
+ catch {
86
+ if (!res.aborted)
87
+ res.cork(() => res.close());
88
+ }
89
+ }
90
+ else {
91
+ if (!res.aborted)
92
+ res.cork(() => res.end());
93
+ }
94
+ }
95
+ });
96
+ return {
97
+ runtime: { node: server },
98
+ start: () => new Promise((resolve, reject) => {
99
+ if (params.listen.unix) {
100
+ server.listen_unix((socket) => {
101
+ if (socket) {
102
+ resolve('unix://' + params.listen.unix);
103
+ }
104
+ else {
105
+ reject(new Error('Failed to start WebSockets server'));
106
+ }
107
+ }, params.listen.unix);
108
+ }
109
+ else if (typeof params.listen.port === 'number') {
110
+ const proto = params.tls ? 'https' : 'http';
111
+ const hostname = params.listen.hostname || '127.0.0.1';
112
+ server.listen(hostname, params.listen.port, (socket) => {
113
+ if (socket) {
114
+ resolve(`${proto}://${hostname}:${us_socket_local_port(socket)}`);
115
+ }
116
+ else {
117
+ reject(new Error('Failed to start WebSockets server'));
118
+ }
119
+ });
120
+ }
121
+ else {
122
+ reject(new Error('Invalid listen parameters'));
123
+ }
124
+ }),
125
+ stop: () => {
126
+ server.close();
127
+ },
128
+ };
129
+ }
130
+ export const HttpTransport = {
131
+ proxyable: ProxyableTransportType.HTTP,
132
+ injectables,
133
+ factory(options) {
134
+ return createHTTPTransportWorker(adapterFactory, options);
135
+ },
136
+ };
package/dist/server.js ADDED
@@ -0,0 +1,343 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ import { Buffer } from 'node:buffer';
54
+ import { Duplex, Readable } from 'node:stream';
55
+ import { anyAbortSignal, isAbortError, isAsyncIterable } from '@nmtjs/common';
56
+ import { provide } from '@nmtjs/core';
57
+ import { ConnectionType, ErrorCode, ProtocolBlob, ProtocolVersion, } from '@nmtjs/protocol';
58
+ import { ProtocolClientStream, ProtocolError, UnsupportedContentTypeError, UnsupportedFormatError, } from '@nmtjs/protocol/server';
59
+ import { AllowedHttpMethod, HttpCodeMap, HttpStatus, HttpStatusText, } from "./constants.js";
60
+ import * as injections from "./injectables.js";
61
+ const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob';
62
+ const DEFAULT_ALLOWED_METHODS = Object.freeze(['post']);
63
+ const DEFAULT_CORS_PARAMS = Object.freeze({
64
+ allowCredentials: 'true',
65
+ allowMethods: ['GET', 'POST'],
66
+ allowHeaders: [
67
+ 'Content-Type',
68
+ 'Content-Disposition',
69
+ 'Content-Length',
70
+ 'Accept',
71
+ 'Transfer-Encoding',
72
+ ],
73
+ maxAge: undefined,
74
+ requestMethod: undefined,
75
+ exposeHeaders: [],
76
+ requestHeaders: [],
77
+ });
78
+ const CORS_HEADERS_MAP = {
79
+ origin: 'Access-Control-Allow-Origin',
80
+ allowMethods: 'Access-Control-Allow-Methods',
81
+ allowHeaders: 'Access-Control-Allow-Headers',
82
+ allowCredentials: 'Access-Control-Allow-Credentials',
83
+ maxAge: 'Access-Control-Max-Age',
84
+ exposeHeaders: 'Access-Control-Expose-Headers',
85
+ requestHeaders: 'Access-Control-Request-Headers',
86
+ requestMethod: 'Access-Control-Request-Method',
87
+ };
88
+ export function createHTTPTransportWorker(adapterFactory, options) {
89
+ return new HttpTransportServer(adapterFactory, options);
90
+ }
91
+ export class HttpTransportServer {
92
+ adapterFactory;
93
+ options;
94
+ #server;
95
+ #corsOptions;
96
+ params;
97
+ constructor(adapterFactory, options) {
98
+ this.adapterFactory = adapterFactory;
99
+ this.options = options;
100
+ this.#server = this.createServer();
101
+ this.#corsOptions = this.options.cors;
102
+ }
103
+ async start(hooks) {
104
+ this.params = hooks;
105
+ return await this.#server.start();
106
+ }
107
+ async stop() {
108
+ await this.#server.stop();
109
+ }
110
+ async httpHandler(request, body, requestSignal) {
111
+ const env_1 = { stack: [], error: void 0, hasError: false };
112
+ try {
113
+ const url = new URL(request.url);
114
+ const procedure = url.pathname.slice(1); // remove leading '/'
115
+ const method = request.method.toLowerCase();
116
+ const origin = request.headers.get('origin');
117
+ const responseHeaders = new Headers();
118
+ if (origin)
119
+ this.applyCors(origin, request, responseHeaders);
120
+ // Handle preflight requests
121
+ if (method === 'options') {
122
+ return new Response(null, {
123
+ status: HttpStatus.OK,
124
+ headers: responseHeaders,
125
+ });
126
+ }
127
+ const controller = new AbortController();
128
+ const signal = anyAbortSignal(requestSignal, controller.signal);
129
+ const canHaveBody = method !== 'get';
130
+ const isBlob = request.headers.get(NEEMATA_BLOB_HEADER) === 'true';
131
+ const contentType = request.headers.get('content-type');
132
+ const accept = request.headers.get('accept') || '*/*';
133
+ const connection = __addDisposableResource(env_1, await this.params.onConnect({
134
+ accept,
135
+ contentType: isBlob ? '*/*' : contentType,
136
+ data: request,
137
+ protocolVersion: ProtocolVersion.v1,
138
+ type: ConnectionType.Unidirectional,
139
+ }), true);
140
+ try {
141
+ // Parse request body if present
142
+ let payload;
143
+ if (canHaveBody && body) {
144
+ const bodyStream = Readable.fromWeb(body);
145
+ const cannotDecode = !contentType || !this.params.formats.supportsDecoder(contentType);
146
+ if (isBlob || cannotDecode) {
147
+ const type = contentType || 'application/octet-stream';
148
+ const contentLength = request.headers.get('content-length');
149
+ const size = contentLength
150
+ ? Number.parseInt(contentLength)
151
+ : undefined;
152
+ payload = new ProtocolClientStream(-1, { size, type });
153
+ bodyStream.pipe(payload);
154
+ }
155
+ else {
156
+ const buffer = Buffer.concat(await bodyStream.toArray());
157
+ if (buffer.byteLength > 0) {
158
+ payload = connection.decoder.decode(buffer);
159
+ }
160
+ }
161
+ }
162
+ else {
163
+ const querystring = url.searchParams.get('payload');
164
+ if (querystring) {
165
+ payload = JSON.parse(querystring);
166
+ }
167
+ }
168
+ const metadata = (metadata) => {
169
+ const allowHttpMethod = metadata.get(AllowedHttpMethod) ?? DEFAULT_ALLOWED_METHODS;
170
+ if (!allowHttpMethod.includes(method)) {
171
+ throw new ProtocolError(ErrorCode.NotFound);
172
+ }
173
+ };
174
+ const result = await this.params.onRpc(connection, {
175
+ callId: 0, // since the connection is closed after the call, only one call exists per connection
176
+ payload,
177
+ procedure,
178
+ metadata,
179
+ }, signal, provide(injections.httpResponseHeaders, responseHeaders));
180
+ // Handle blob responses
181
+ if (result instanceof ProtocolBlob) {
182
+ const { source, metadata } = result;
183
+ const { type } = metadata;
184
+ responseHeaders.set(NEEMATA_BLOB_HEADER, 'true');
185
+ responseHeaders.set('Content-Type', type);
186
+ if (metadata.size) {
187
+ responseHeaders.set('Content-Length', metadata.size.toString());
188
+ }
189
+ // Convert source to ReadableStream
190
+ let stream;
191
+ if (source instanceof ReadableStream) {
192
+ stream = source;
193
+ }
194
+ else if (source instanceof Readable || source instanceof Duplex) {
195
+ stream = Readable.toWeb(source);
196
+ }
197
+ else {
198
+ throw new Error('Invalid stream source');
199
+ }
200
+ return new Response(stream, {
201
+ status: HttpStatus.OK,
202
+ statusText: HttpStatusText[HttpStatus.OK],
203
+ headers: responseHeaders,
204
+ });
205
+ }
206
+ else if (isAsyncIterable(result)) {
207
+ responseHeaders.set('Content-Type', connection.encoder.contentType);
208
+ responseHeaders.set('Transfer-Encoding', 'chunked');
209
+ const stream = new ReadableStream({
210
+ async start(controller) {
211
+ try {
212
+ for await (const chunk of result) {
213
+ const encoded = connection.encoder.encode(chunk);
214
+ const base64 = Buffer.from(encoded.buffer, encoded.byteOffset, encoded.byteLength).toString('base64');
215
+ controller.enqueue(`data: ${base64}\n\n`);
216
+ }
217
+ controller.close();
218
+ }
219
+ catch (error) {
220
+ if (isAbortError(error))
221
+ controller.close();
222
+ else
223
+ controller.error(error);
224
+ }
225
+ },
226
+ });
227
+ return new Response(stream, {
228
+ status: HttpStatus.OK,
229
+ statusText: HttpStatusText[HttpStatus.OK],
230
+ headers: responseHeaders,
231
+ });
232
+ }
233
+ else {
234
+ // Handle regular responses
235
+ const buffer = connection.encoder.encode(result);
236
+ responseHeaders.set('Content-Type', connection.encoder.contentType);
237
+ // @ts-expect-error
238
+ return new Response(buffer, {
239
+ status: HttpStatus.OK,
240
+ statusText: HttpStatusText[HttpStatus.OK],
241
+ headers: responseHeaders,
242
+ });
243
+ }
244
+ }
245
+ catch (error) {
246
+ console.error(error);
247
+ if (error instanceof UnsupportedFormatError) {
248
+ const status = error instanceof UnsupportedContentTypeError
249
+ ? HttpStatus.UnsupportedMediaType
250
+ : HttpStatus.NotAcceptable;
251
+ const text = HttpStatusText[status];
252
+ return new Response(text, {
253
+ status,
254
+ statusText: text,
255
+ headers: responseHeaders,
256
+ });
257
+ }
258
+ if (error instanceof ProtocolError) {
259
+ const status = error.code in HttpCodeMap
260
+ ? HttpCodeMap[error.code]
261
+ : HttpStatus.InternalServerError;
262
+ const text = HttpStatusText[status];
263
+ const payload = connection.encoder.encode(error);
264
+ responseHeaders.set('Content-Type', connection.encoder.contentType);
265
+ // @ts-expect-error
266
+ return new Response(payload, {
267
+ status,
268
+ statusText: text,
269
+ headers: responseHeaders,
270
+ });
271
+ }
272
+ // Unknown error
273
+ // this.logError(error, 'Unknown error while processing HTTP request')
274
+ console.error(error);
275
+ const payload = connection.encoder.encode(new ProtocolError(ErrorCode.InternalServerError, 'Internal Server Error'));
276
+ responseHeaders.set('Content-Type', connection.encoder.contentType);
277
+ // @ts-expect-error
278
+ return new Response(payload, {
279
+ status: HttpStatus.InternalServerError,
280
+ statusText: HttpStatusText[HttpStatus.InternalServerError],
281
+ headers: responseHeaders,
282
+ });
283
+ }
284
+ }
285
+ catch (e_1) {
286
+ env_1.error = e_1;
287
+ env_1.hasError = true;
288
+ }
289
+ finally {
290
+ const result_1 = __disposeResources(env_1);
291
+ if (result_1)
292
+ await result_1;
293
+ }
294
+ }
295
+ applyCors(origin, request, headers) {
296
+ if (!this.#corsOptions)
297
+ return;
298
+ let params = null;
299
+ if (this.options.cors === true) {
300
+ params = { ...DEFAULT_CORS_PARAMS };
301
+ }
302
+ else if (Array.isArray(this.options.cors) &&
303
+ this.options.cors.includes(origin)) {
304
+ params = { ...DEFAULT_CORS_PARAMS };
305
+ }
306
+ else if (typeof this.options.cors === 'function') {
307
+ const result = this.options.cors(origin, request);
308
+ if (typeof result === 'boolean') {
309
+ if (result) {
310
+ params = { ...DEFAULT_CORS_PARAMS };
311
+ }
312
+ }
313
+ else if (typeof result === 'object') {
314
+ params = { ...DEFAULT_CORS_PARAMS };
315
+ for (const key in DEFAULT_CORS_PARAMS) {
316
+ params[key] = result[key];
317
+ }
318
+ }
319
+ }
320
+ if (params === null)
321
+ return;
322
+ headers.set(CORS_HEADERS_MAP.origin, origin);
323
+ for (const key in params) {
324
+ const header = CORS_HEADERS_MAP[key];
325
+ if (header) {
326
+ let value = params[key];
327
+ if (Array.isArray(value))
328
+ value = value.filter(Boolean).join(', ');
329
+ if (value)
330
+ headers.set(header, value);
331
+ }
332
+ }
333
+ }
334
+ createServer() {
335
+ // const hooks = this.createWsHooks()
336
+ const opts = {
337
+ ...this.options,
338
+ // logger: this.logger.child({ $lable: 'WsServer' }),
339
+ fetchHandler: this.httpHandler.bind(this),
340
+ };
341
+ return this.adapterFactory(opts);
342
+ }
343
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/utils.js ADDED
@@ -0,0 +1,15 @@
1
+ import { ErrorCode } from '@nmtjs/protocol';
2
+ import { ProtocolError } from '@nmtjs/protocol/server';
3
+ export const InternalError = (message = 'Internal Server Error') => new ProtocolError(ErrorCode.InternalServerError, message);
4
+ export const NotFoundError = (message = 'Not Found') => new ProtocolError(ErrorCode.NotFound, message);
5
+ export const ForbiddenError = (message = 'Forbidden') => new ProtocolError(ErrorCode.Forbidden, message);
6
+ export const RequestTimeoutError = (message = 'Request Timeout') => new ProtocolError(ErrorCode.RequestTimeout, message);
7
+ export const NotFoundHttpResponse = () => new Response('Not Found', {
8
+ status: 404,
9
+ headers: { 'Content-Type': 'text/plain' },
10
+ });
11
+ export const InternalServerErrorHttpResponse = () => new Response('Internal Server Error', {
12
+ status: 500,
13
+ headers: { 'Content-Type': 'text/plain' },
14
+ });
15
+ export const StatusResponse = () => new Response('OK', { status: 200 });
package/package.json CHANGED
@@ -9,20 +9,20 @@
9
9
  },
10
10
  "devDependencies": {
11
11
  "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.56.0",
12
- "@nmtjs/common": "0.15.0-beta.1",
13
- "@nmtjs/gateway": "0.15.0-beta.1",
14
- "@nmtjs/protocol": "0.15.0-beta.1",
15
- "@nmtjs/client": "0.15.0-beta.1"
12
+ "@nmtjs/client": "0.15.0-beta.2",
13
+ "@nmtjs/gateway": "0.15.0-beta.2",
14
+ "@nmtjs/common": "0.15.0-beta.2",
15
+ "@nmtjs/protocol": "0.15.0-beta.2"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@types/bun": "^1.3.0",
19
19
  "@types/deno": "^2.3.0",
20
20
  "@types/node": "^24",
21
21
  "uWebSockets.js": "^20.56.0",
22
- "@nmtjs/common": "0.15.0-beta.1",
23
- "@nmtjs/core": "0.15.0-beta.1",
24
- "@nmtjs/gateway": "0.15.0-beta.1",
25
- "@nmtjs/protocol": "0.15.0-beta.1"
22
+ "@nmtjs/common": "0.15.0-beta.2",
23
+ "@nmtjs/core": "0.15.0-beta.2",
24
+ "@nmtjs/protocol": "0.15.0-beta.2",
25
+ "@nmtjs/gateway": "0.15.0-beta.2"
26
26
  },
27
27
  "peerDependenciesMeta": {
28
28
  "@types/bun": {
@@ -48,7 +48,7 @@
48
48
  "LICENSE.md",
49
49
  "README.md"
50
50
  ],
51
- "version": "0.15.0-beta.1",
51
+ "version": "0.15.0-beta.2",
52
52
  "scripts": {
53
53
  "clean-build": "rm -rf ./dist",
54
54
  "build": "tsc",