@nmtjs/ws-transport 0.14.5 → 0.15.0-beta.10
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.md +1 -1
- package/README.md +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/injectables.d.ts +1 -6
- package/dist/injectables.js +3 -8
- package/dist/injectables.js.map +1 -0
- package/dist/runtimes/bun.d.ts +6 -2
- package/dist/runtimes/bun.js +22 -22
- package/dist/runtimes/bun.js.map +1 -0
- package/dist/runtimes/deno.d.ts +6 -2
- package/dist/runtimes/deno.js +22 -25
- package/dist/runtimes/deno.js.map +1 -0
- package/dist/runtimes/node.d.ts +6 -2
- package/dist/runtimes/node.js +23 -76
- package/dist/runtimes/node.js.map +1 -0
- package/dist/server.d.ts +9 -23
- package/dist/server.js +69 -331
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +5 -16
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.js +1 -0
- package/dist/utils.js.map +1 -0
- package/package.json +20 -19
- package/src/index.ts +4 -0
- package/src/injectables.ts +6 -0
- package/src/runtimes/bun.ts +82 -0
- package/src/runtimes/deno.ts +125 -0
- package/src/runtimes/node.ts +83 -0
- package/src/server.ts +161 -0
- package/src/types.ts +123 -0
- package/src/utils.ts +28 -0
- package/dist/http.d.ts +0 -80
- package/dist/http.js +0 -146
package/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
package/dist/injectables.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
import type { LazyInjectable } from '@nmtjs/core';
|
|
2
|
-
import { Scope } from '@nmtjs/core';
|
|
3
1
|
import type { WsTransportServerRequest } from './types.ts';
|
|
4
|
-
export declare const
|
|
5
|
-
readonly connectionData: LazyInjectable<WsTransportServerRequest, Scope.Connection>;
|
|
6
|
-
readonly httpResponseHeaders: LazyInjectable<Headers, Scope.Call>;
|
|
7
|
-
};
|
|
2
|
+
export declare const connectionData: import("@nmtjs/core").LazyInjectable<WsTransportServerRequest, import("@nmtjs/core").Scope.Connection>;
|
package/dist/injectables.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const httpResponseHeaders = createLazyInjectable(Scope.Call);
|
|
5
|
-
export const WsTransportInjectables = {
|
|
6
|
-
connectionData,
|
|
7
|
-
httpResponseHeaders,
|
|
8
|
-
};
|
|
1
|
+
import { connectionData as connectionDataInjectable } from '@nmtjs/gateway';
|
|
2
|
+
export const connectionData = connectionDataInjectable.$withType();
|
|
3
|
+
//# sourceMappingURL=injectables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectables.js","sourceRoot":"","sources":["../src/injectables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,IAAI,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAI3E,MAAM,CAAC,MAAM,cAAc,GACzB,wBAAwB,CAAC,SAAS,EAA4B,CAAA"}
|
package/dist/runtimes/bun.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { Transport } from '@nmtjs/gateway';
|
|
2
|
+
import type { ConnectionType } from '@nmtjs/protocol';
|
|
3
|
+
import { ProxyableTransportType } from '@nmtjs/gateway';
|
|
4
|
+
import type { WsTransportOptions } from '../types.ts';
|
|
5
|
+
import * as injectables from '../injectables.ts';
|
|
6
|
+
export declare const WsTransport: Transport<ConnectionType.Bidirectional, WsTransportOptions<'bun'>, typeof injectables, ProxyableTransportType.WebSocket>;
|
package/dist/runtimes/bun.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProxyableTransportType } from '@nmtjs/gateway';
|
|
2
2
|
import createAdapter from 'crossws/adapters/bun';
|
|
3
|
-
import
|
|
3
|
+
import * as injectables from "../injectables.js";
|
|
4
|
+
import { createWSTransportWorker } from "../server.js";
|
|
4
5
|
import { InternalServerErrorHttpResponse, NotFoundHttpResponse, StatusResponse, } from "../utils.js";
|
|
5
6
|
function adapterFactory(params) {
|
|
6
7
|
const adapter = createAdapter({ hooks: params.wsHooks });
|
|
7
8
|
let server = null;
|
|
8
9
|
function createServer() {
|
|
9
|
-
return globalThis.Bun.serve(
|
|
10
|
+
return globalThis.Bun.serve(
|
|
11
|
+
// @ts-expect-error ts bs
|
|
12
|
+
{
|
|
10
13
|
...params.runtime?.server,
|
|
11
14
|
unix: params.listen.unix,
|
|
12
15
|
port: params.listen.port,
|
|
@@ -20,25 +23,17 @@ function adapterFactory(params) {
|
|
|
20
23
|
}
|
|
21
24
|
: undefined,
|
|
22
25
|
websocket: { ...params.runtime?.ws, ...adapter.websocket },
|
|
23
|
-
routes: {
|
|
24
|
-
...params.runtime?.server?.routes,
|
|
25
|
-
'/healthy': StatusResponse(),
|
|
26
|
-
},
|
|
26
|
+
routes: { '/healthy': StatusResponse() },
|
|
27
27
|
async fetch(request, server) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (request.headers.get('upgrade') === 'websocket') {
|
|
32
|
-
return await adapter.handleUpgrade(request, server);
|
|
33
|
-
}
|
|
34
|
-
const { body, headers, method } = request;
|
|
35
|
-
return await params.fetchHandler({ url, method, headers }, body, request.signal);
|
|
36
|
-
}
|
|
37
|
-
catch (err) {
|
|
38
|
-
params.logger.error({ err }, 'Error in fetch handler');
|
|
39
|
-
return InternalServerErrorHttpResponse();
|
|
28
|
+
try {
|
|
29
|
+
if (request.headers.get('upgrade') === 'websocket') {
|
|
30
|
+
return await adapter.handleUpgrade(request, server);
|
|
40
31
|
}
|
|
41
32
|
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
console.error('Error in WebSocket fetch handler', err);
|
|
35
|
+
return InternalServerErrorHttpResponse();
|
|
36
|
+
}
|
|
42
37
|
return NotFoundHttpResponse();
|
|
43
38
|
},
|
|
44
39
|
});
|
|
@@ -56,6 +51,11 @@ function adapterFactory(params) {
|
|
|
56
51
|
},
|
|
57
52
|
};
|
|
58
53
|
}
|
|
59
|
-
export const WsTransport =
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
export const WsTransport = {
|
|
55
|
+
proxyable: ProxyableTransportType.WebSocket,
|
|
56
|
+
injectables,
|
|
57
|
+
factory(options) {
|
|
58
|
+
return createWSTransportWorker(adapterFactory, options);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=bun.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun.js","sourceRoot":"","sources":["../../src/runtimes/bun.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAA;AACvD,OAAO,aAAa,MAAM,sBAAsB,CAAA;AAOhD,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AACtD,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EACpB,cAAc,GACf,MAAM,aAAa,CAAA;AAEpB,SAAS,cAAc,CAAC,MAA8B;IACpD,MA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EAAE,CAAA;YAC/B,CAAC;SACF,CACF,CAAA;IACH,CAAC;IAED,OAAO;QACL,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,MAAM,GAAG,YAAY,EAAE,CAAA;YACvB,OAAO,MAAO,CAAC,GAAG,CAAC,IAAI,CAAA;QACzB,CAAC;QACD,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;gBACnB,MAAM,GAAG,IAAI,CAAA;YACf,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAKpB;IACF,SAAS,EAAE,sBAAsB,CAAC,SAAS;IAC3C,WAAW;IACX,OAAO,CAAC,OAAO;QACb,OAAO,uBAAuB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;IACzD,CAAC;CACF,CAAA"}
|
package/dist/runtimes/deno.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { Transport } from '@nmtjs/gateway';
|
|
2
|
+
import type { ConnectionType } from '@nmtjs/protocol';
|
|
3
|
+
import { ProxyableTransportType } from '@nmtjs/gateway';
|
|
4
|
+
import type { WsTransportOptions } from '../types.ts';
|
|
5
|
+
import * as injectables from '../injectables.ts';
|
|
6
|
+
export declare const WsTransport: Transport<ConnectionType.Bidirectional, WsTransportOptions<'deno'>, typeof injectables, ProxyableTransportType.WebSocket>;
|
package/dist/runtimes/deno.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProxyableTransportType } from '@nmtjs/gateway';
|
|
2
2
|
import createAdapter from 'crossws/adapters/deno';
|
|
3
|
-
import
|
|
3
|
+
import * as injectables from "../injectables.js";
|
|
4
|
+
import { createWSTransportWorker } from "../server.js";
|
|
4
5
|
import { InternalServerErrorHttpResponse, NotFoundHttpResponse, StatusResponse, } from "../utils.js";
|
|
5
6
|
function adapterFactory(params) {
|
|
6
7
|
const adapter = createAdapter({ hooks: params.wsHooks });
|
|
@@ -29,21 +30,16 @@ function adapterFactory(params) {
|
|
|
29
30
|
...options,
|
|
30
31
|
handler: async (request, info) => {
|
|
31
32
|
const url = new URL(request.url);
|
|
32
|
-
if (url.pathname
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const { headers, method, body } = request;
|
|
38
|
-
return await params.fetchHandler({ url, method, headers }, body, request.signal);
|
|
39
|
-
}
|
|
40
|
-
catch (err) {
|
|
41
|
-
params.logger.error({ err }, 'Error in fetch handler');
|
|
42
|
-
return InternalServerErrorHttpResponse();
|
|
33
|
+
if (url.pathname === '/healthy')
|
|
34
|
+
return StatusResponse();
|
|
35
|
+
try {
|
|
36
|
+
if (request.headers.get('upgrade') === 'websocket') {
|
|
37
|
+
return await adapter.handleUpgrade(request, info);
|
|
43
38
|
}
|
|
44
39
|
}
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
catch (err) {
|
|
41
|
+
console.error('Error in WebSocket fetch handler', err);
|
|
42
|
+
return InternalServerErrorHttpResponse();
|
|
47
43
|
}
|
|
48
44
|
return NotFoundHttpResponse();
|
|
49
45
|
},
|
|
@@ -59,17 +55,13 @@ function adapterFactory(params) {
|
|
|
59
55
|
start: async () => {
|
|
60
56
|
const { server: _server, addr } = await createServer();
|
|
61
57
|
server = _server;
|
|
58
|
+
const proto = params.tls ? 'https' : 'http';
|
|
62
59
|
switch (addr.transport) {
|
|
63
60
|
case 'unix':
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
case 'tcp':
|
|
67
|
-
case 'udp': {
|
|
68
|
-
const proto = params.tls ? 'https' : 'http';
|
|
61
|
+
return `${proto}+unix://${addr.path}`;
|
|
62
|
+
case 'tcp': {
|
|
69
63
|
return `${proto}://${addr.hostname}:${addr.port}`;
|
|
70
64
|
}
|
|
71
|
-
case 'vsock':
|
|
72
|
-
return `vsock://${addr.cid}:${addr.port}`;
|
|
73
65
|
default:
|
|
74
66
|
throw new Error(`Unsupported address transport`);
|
|
75
67
|
}
|
|
@@ -82,6 +74,11 @@ function adapterFactory(params) {
|
|
|
82
74
|
},
|
|
83
75
|
};
|
|
84
76
|
}
|
|
85
|
-
export const WsTransport =
|
|
86
|
-
|
|
87
|
-
|
|
77
|
+
export const WsTransport = {
|
|
78
|
+
proxyable: ProxyableTransportType.WebSocket,
|
|
79
|
+
injectables,
|
|
80
|
+
factory(options) {
|
|
81
|
+
return createWSTransportWorker(adapterFactory, options);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=deno.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deno.js","sourceRoot":"","sources":["../../src/runtimes/deno.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAA;AACvD,OAAO,aAAa,MAAM,uBAAuB,CAAA;AAOjD,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AACtD,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EACpB,cAAc,GACf,MAAM,aAAa,CAAA;AAsBpB,SAAS,cAAc,CAAC,MAA+B;IACrD,MAAM,OAAO,GAAG,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAA;IAExD,IAAI,MAAM,GAAsB,IAAI,CAAA;IAEpC,SAAS,YAAY;QACnB,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI;YACtC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE;YAC9B,CAAC,CAAC;gBACE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI;gBACxB,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ;gBAChC,SAAS,EAAE,MAAM,CAAC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CAAC,CAAC,CAAA;gBACP,CAAC;aACF,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,OAAO;QACL,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,MAAM,YAAY,EAAE,CAAA;YACtD,MAAM,GAAG,OAAO,CAAA;YAChB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAA;YAE3C,QAAQ,IAAI,CAAC,SAAS,EAAE,CAAC;gBACvB,KAAK,MAAM;oBACT,OAAO,GAAG,KAAK,WAAW,IAAI,CAAC,IAAI,EAAE,CAAA;gBACvC,KAAK,KAAK,CAAC,CAAC,CAAC;oBACX,OAAO,GAAG,KAAK,MAAM,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,EAAE,CAAA;gBACnD,CAAC;gBACD;oBACE,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAA;YACpD,CAAC;QACH,CAAC;QACD,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAA;gBACvB,MAAM,GAAG,IAAI,CAAA;YACf,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAKpB;IACF,SAAS,EAAE,sBAAsB,CAAC,SAAS;IAC3C,WAAW;IACX,OAAO,CAAC,OAAO;QACb,OAAO,uBAAuB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;IACzD,CAAC;CACF,CAAA"}
|
package/dist/runtimes/node.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { Transport } from '@nmtjs/gateway';
|
|
2
|
+
import type { ConnectionType } from '@nmtjs/protocol';
|
|
3
|
+
import { ProxyableTransportType } from '@nmtjs/gateway';
|
|
4
|
+
import type { WsTransportOptions } from '../types.ts';
|
|
5
|
+
import * as injectables from '../injectables.ts';
|
|
6
|
+
export declare const WsTransport: Transport<ConnectionType.Bidirectional, WsTransportOptions<'node'>, typeof injectables, ProxyableTransportType.WebSocket>;
|
package/dist/runtimes/node.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProxyableTransportType } from '@nmtjs/gateway';
|
|
2
2
|
import createAdapter from 'crossws/adapters/uws';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import * as injectables from "../injectables.js";
|
|
4
|
+
import { createWSTransportWorker } from "../server.js";
|
|
5
|
+
import { StatusResponse } from "../utils.js";
|
|
6
|
+
import { App, SSLApp, us_socket_local_port } from 'uWebSockets.js';
|
|
7
|
+
const statusResponse = StatusResponse();
|
|
8
|
+
const statusResponseBuffer = await statusResponse.arrayBuffer();
|
|
6
9
|
function adapterFactory(params) {
|
|
7
10
|
const adapter = createAdapter({ hooks: params.wsHooks });
|
|
8
11
|
const server = params.tls
|
|
@@ -13,81 +16,21 @@ function adapterFactory(params) {
|
|
|
13
16
|
})
|
|
14
17
|
: App();
|
|
15
18
|
server
|
|
16
|
-
.ws(
|
|
17
|
-
.get('/healthy',
|
|
18
|
-
res.
|
|
19
|
-
const response = StatusResponse();
|
|
20
|
-
res.cork(async () => {
|
|
19
|
+
.ws('/*', { ...params.runtime?.ws, ...adapter.websocket })
|
|
20
|
+
.get('/healthy', (res) => {
|
|
21
|
+
res.cork(() => {
|
|
21
22
|
res
|
|
22
|
-
.writeStatus(`${
|
|
23
|
-
.end(
|
|
23
|
+
.writeStatus(`${statusResponse.status} ${statusResponse.statusText}`)
|
|
24
|
+
.end(statusResponseBuffer);
|
|
24
25
|
});
|
|
25
|
-
})
|
|
26
|
-
.any('/*', async (res, req) => {
|
|
27
|
-
const controller = new AbortController();
|
|
28
|
-
res.onAborted(() => controller.abort());
|
|
29
|
-
let response = NotFoundHttpResponse();
|
|
30
|
-
const headers = new Headers();
|
|
31
|
-
const method = req.getMethod();
|
|
32
|
-
req.forEach((k, v) => headers.append(k, v));
|
|
33
|
-
const host = headers.get('host') || 'localhost';
|
|
34
|
-
const proto = headers.get('x-forwarded-proto') || params.tls ? 'https' : 'http';
|
|
35
|
-
const url = new URL(req.getUrl(), `${proto}://${host}`);
|
|
36
|
-
if (url.pathname.startsWith(params.apiPath)) {
|
|
37
|
-
try {
|
|
38
|
-
const body = new ReadableStream({
|
|
39
|
-
start(controller) {
|
|
40
|
-
res.onData((chunk, isLast) => {
|
|
41
|
-
if (chunk)
|
|
42
|
-
controller.enqueue(Buffer.from(chunk.slice(0)));
|
|
43
|
-
if (isLast)
|
|
44
|
-
controller.close();
|
|
45
|
-
});
|
|
46
|
-
res.onAborted(() => controller.error());
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
response = await params.fetchHandler({ url, method, headers }, body, controller.signal);
|
|
50
|
-
}
|
|
51
|
-
catch (err) {
|
|
52
|
-
params.logger.error({ err }, 'Error in fetch handler');
|
|
53
|
-
response = InternalServerErrorHttpResponse();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
if (controller.signal.aborted)
|
|
57
|
-
return undefined;
|
|
58
|
-
else {
|
|
59
|
-
res.cork(() => {
|
|
60
|
-
res.writeStatus(`${response.status.toString()} ${response.statusText}`);
|
|
61
|
-
response.headers.forEach((v, k) => res.writeHeader(k, v));
|
|
62
|
-
});
|
|
63
|
-
if (response.body) {
|
|
64
|
-
try {
|
|
65
|
-
const reader = response.body.getReader();
|
|
66
|
-
let chunk = await reader.read();
|
|
67
|
-
do {
|
|
68
|
-
if (controller.signal.aborted)
|
|
69
|
-
break;
|
|
70
|
-
if (chunk.value)
|
|
71
|
-
res.write(chunk.value);
|
|
72
|
-
chunk = await reader.read();
|
|
73
|
-
} while (!chunk.done);
|
|
74
|
-
res.end();
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
res.close();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
res.end();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
26
|
});
|
|
85
27
|
return {
|
|
86
28
|
start: () => new Promise((resolve, reject) => {
|
|
29
|
+
const proto = params.tls ? 'https' : 'http';
|
|
87
30
|
if (params.listen.unix) {
|
|
88
31
|
server.listen_unix((socket) => {
|
|
89
32
|
if (socket) {
|
|
90
|
-
resolve(
|
|
33
|
+
resolve(`${proto}+unix://` + params.listen.unix);
|
|
91
34
|
}
|
|
92
35
|
else {
|
|
93
36
|
reject(new Error('Failed to start WebSockets server'));
|
|
@@ -95,11 +38,10 @@ function adapterFactory(params) {
|
|
|
95
38
|
}, params.listen.unix);
|
|
96
39
|
}
|
|
97
40
|
else if (typeof params.listen.port === 'number') {
|
|
98
|
-
const proto = params.tls ? 'https' : 'http';
|
|
99
41
|
const hostname = params.listen.hostname || '127.0.0.1';
|
|
100
42
|
server.listen(hostname, params.listen.port, (socket) => {
|
|
101
43
|
if (socket) {
|
|
102
|
-
resolve(`${proto}://${hostname}:${
|
|
44
|
+
resolve(`${proto}://${hostname}:${us_socket_local_port(socket)}`);
|
|
103
45
|
}
|
|
104
46
|
else {
|
|
105
47
|
reject(new Error('Failed to start WebSockets server'));
|
|
@@ -115,6 +57,11 @@ function adapterFactory(params) {
|
|
|
115
57
|
},
|
|
116
58
|
};
|
|
117
59
|
}
|
|
118
|
-
export const WsTransport =
|
|
119
|
-
|
|
120
|
-
|
|
60
|
+
export const WsTransport = {
|
|
61
|
+
proxyable: ProxyableTransportType.WebSocket,
|
|
62
|
+
injectables,
|
|
63
|
+
factory(options) {
|
|
64
|
+
return createWSTransportWorker(adapterFactory, options);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../src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AI,EAAE,GAAG,EAAE;YACT,MAAM,CAAC,KAAK,EAAE,CAAA;QAChB,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAKpB;IACF,SAAS,EAAE,sBAAsB,CAAC,SAAS;IAC3C,WAAW;IACX,OAAO,CAAC,OAAO;QACb,OAAO,uBAAuB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;IACzD,CAAC;CACF,CAAA"}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,32 +1,18 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Transport, TransportPluginContext } from '@nmtjs/protocol/server';
|
|
1
|
+
import type { TransportWorker, TransportWorkerParams } from '@nmtjs/gateway';
|
|
3
2
|
import type { Peer } from 'crossws';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export declare class WsTransportServer implements
|
|
3
|
+
import { ConnectionType } from '@nmtjs/protocol';
|
|
4
|
+
import type { WsAdapterServerFactory, WsTransportOptions } from './types.ts';
|
|
5
|
+
export declare function createWSTransportWorker(adapterFactory: WsAdapterServerFactory<any>, options: WsTransportOptions): TransportWorker<ConnectionType.Bidirectional>;
|
|
6
|
+
export declare class WsTransportServer implements TransportWorker<ConnectionType.Bidirectional> {
|
|
8
7
|
#private;
|
|
9
8
|
protected readonly adapterFactory: WsAdapterServerFactory<any>;
|
|
10
|
-
protected readonly context: TransportPluginContext;
|
|
11
9
|
protected readonly options: WsTransportOptions;
|
|
10
|
+
params: TransportWorkerParams<ConnectionType.Bidirectional>;
|
|
12
11
|
clients: Map<string, Peer<import("crossws").AdapterInternal>>;
|
|
13
|
-
constructor(adapterFactory: WsAdapterServerFactory<any>,
|
|
14
|
-
start(): Promise<
|
|
12
|
+
constructor(adapterFactory: WsAdapterServerFactory<any>, options: WsTransportOptions);
|
|
13
|
+
start(hooks: TransportWorkerParams<ConnectionType.Bidirectional>): Promise<string>;
|
|
15
14
|
stop(): Promise<void>;
|
|
16
|
-
send(
|
|
17
|
-
httpHandler(request: WsTransportServerRequest, body: ReadableStream | null, requestSignal: AbortSignal): Promise<Response>;
|
|
18
|
-
private applyCors;
|
|
19
|
-
protected get protocol(): import("@nmtjs/protocol/server").Protocol;
|
|
20
|
-
protected get logger(): import("@nmtjs/core").Logger;
|
|
21
|
-
protected logError(cause: any, message?: string): Promise<void>;
|
|
22
|
-
protected [ClientMessageType.Rpc](buffer: ArrayBuffer, connectionId: string): void;
|
|
23
|
-
protected [ClientMessageType.RpcAbort](buffer: ArrayBuffer, connectionId: string): void;
|
|
24
|
-
protected [ClientMessageType.RpcStreamAbort](buffer: ArrayBuffer, connectionId: string): void;
|
|
25
|
-
protected [ClientMessageType.ClientStreamPush](buffer: ArrayBuffer, connectionId: string): void;
|
|
26
|
-
protected [ClientMessageType.ClientStreamEnd](buffer: ArrayBuffer, connectionId: string): void;
|
|
27
|
-
protected [ClientMessageType.ClientStreamAbort](buffer: ArrayBuffer, connectionId: string): void;
|
|
28
|
-
protected [ClientMessageType.ServerStreamPull](buffer: ArrayBuffer, connectionId: string): void;
|
|
29
|
-
protected [ClientMessageType.ServerStreamAbort](buffer: ArrayBuffer, connectionId: string): void;
|
|
15
|
+
send(connectionId: string, buffer: ArrayBufferView): boolean;
|
|
30
16
|
private createWsHooks;
|
|
31
17
|
private createServer;
|
|
32
18
|
}
|