@nmtjs/ws-transport 0.1.7 → 0.2.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/dist/lib/injectables.js +5 -2
- package/dist/lib/injectables.js.map +1 -1
- package/dist/lib/server.js +84 -271
- package/dist/lib/server.js.map +1 -1
- package/dist/lib/transport.js +3 -9
- package/dist/lib/transport.js.map +1 -1
- package/dist/lib/types.js.map +1 -1
- package/dist/lib/utils.js +10 -27
- package/dist/lib/utils.js.map +1 -1
- package/lib/injectables.ts +12 -10
- package/lib/server.ts +112 -329
- package/lib/transport.ts +8 -14
- package/lib/types.ts +12 -55
- package/lib/utils.ts +10 -49
- package/package.json +5 -8
package/lib/types.ts
CHANGED
|
@@ -1,55 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
Connection,
|
|
4
|
-
Container,
|
|
5
|
-
ServerDownStream,
|
|
6
|
-
ServerUpStream,
|
|
7
|
-
Subscription,
|
|
8
|
-
} from '@nmtjs/application'
|
|
9
|
-
import type {
|
|
10
|
-
BaseServerDecoder,
|
|
11
|
-
BaseServerEncoder,
|
|
12
|
-
BaseServerFormat,
|
|
13
|
-
DecodeRpcContext,
|
|
14
|
-
EncodeRpcContext,
|
|
15
|
-
} from '@nmtjs/common'
|
|
1
|
+
import type { InteractivePromise } from '@nmtjs/common'
|
|
2
|
+
import type { Connection, ConnectionContext } from '@nmtjs/protocol/server'
|
|
16
3
|
import type { AppOptions, WebSocket } from 'uWebSockets.js'
|
|
17
4
|
|
|
18
5
|
export type WsUserData = {
|
|
19
6
|
id: Connection['id']
|
|
20
|
-
|
|
21
|
-
backpressure:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*/
|
|
30
|
-
up: Map<number, ServerUpStream>
|
|
31
|
-
/**
|
|
32
|
-
* Server to client streams
|
|
33
|
-
*/
|
|
34
|
-
down: Map<number, ServerDownStream>
|
|
35
|
-
streamId: number
|
|
36
|
-
}
|
|
37
|
-
subscriptions: Map<string, Subscription>
|
|
38
|
-
container: Container
|
|
39
|
-
data: WsConnectionData
|
|
40
|
-
format: {
|
|
41
|
-
encoder: BaseServerEncoder
|
|
42
|
-
decoder: BaseServerDecoder
|
|
43
|
-
}
|
|
44
|
-
context: {
|
|
45
|
-
decode: Omit<DecodeRpcContext, 'addStream'> & {
|
|
46
|
-
addStream: (
|
|
47
|
-
signal: AbortSignal,
|
|
48
|
-
...args: Parameters<DecodeRpcContext['addStream']>
|
|
49
|
-
) => any
|
|
50
|
-
}
|
|
51
|
-
encode: EncodeRpcContext
|
|
7
|
+
opening: InteractivePromise<void>
|
|
8
|
+
backpressure: InteractivePromise<void> | null
|
|
9
|
+
request: {
|
|
10
|
+
headers: Map<string, string>
|
|
11
|
+
query: URLSearchParams
|
|
12
|
+
remoteAddress: string
|
|
13
|
+
proxiedRemoteAddress: string
|
|
14
|
+
acceptType: string | null
|
|
15
|
+
contentType: string | null
|
|
52
16
|
}
|
|
17
|
+
context: ConnectionContext
|
|
53
18
|
}
|
|
54
19
|
|
|
55
20
|
export type WsTransportSocket = WebSocket<WsUserData>
|
|
@@ -61,12 +26,4 @@ export type WsTransportOptions = {
|
|
|
61
26
|
tls?: AppOptions
|
|
62
27
|
maxPayloadLength?: number
|
|
63
28
|
maxStreamChunkLength?: number
|
|
64
|
-
// cors?: ServerOptions['cors']
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export type WsConnectionData = {
|
|
68
|
-
headers: Map<string, string>
|
|
69
|
-
query: URLSearchParams
|
|
70
|
-
remoteAddress: string
|
|
71
|
-
proxiedRemoteAddress: string
|
|
72
29
|
}
|
package/lib/utils.ts
CHANGED
|
@@ -1,30 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type RpcResponse,
|
|
5
|
-
concat,
|
|
6
|
-
encodeNumber,
|
|
7
|
-
} from '@nmtjs/common'
|
|
1
|
+
import { createPromise } from '@nmtjs/common'
|
|
2
|
+
import { ErrorCode, concat, encodeNumber } from '@nmtjs/protocol/common'
|
|
3
|
+
import { ProtocolError } from '@nmtjs/protocol/server'
|
|
8
4
|
import type { HttpRequest } from 'uWebSockets.js'
|
|
9
5
|
import type { WsTransportSocket } from './types.ts'
|
|
10
6
|
|
|
11
|
-
export const sendMessage = (
|
|
12
|
-
ws: WsTransportSocket,
|
|
13
|
-
type: number,
|
|
14
|
-
payload: any,
|
|
15
|
-
) => {
|
|
16
|
-
return send(ws, type, ws.getUserData().format.encoder.encode(payload))
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const sendRpcMessage = (
|
|
20
|
-
ws: WsTransportSocket,
|
|
21
|
-
type: number,
|
|
22
|
-
rpc: RpcResponse,
|
|
23
|
-
) => {
|
|
24
|
-
const data = ws.getUserData()
|
|
25
|
-
return send(ws, type, data.format.encoder.encodeRpc(rpc, data.context.encode))
|
|
26
|
-
}
|
|
27
|
-
|
|
28
7
|
export const send = (
|
|
29
8
|
ws: WsTransportSocket,
|
|
30
9
|
type: number,
|
|
@@ -32,12 +11,10 @@ export const send = (
|
|
|
32
11
|
): boolean | null => {
|
|
33
12
|
const data = ws.getUserData()
|
|
34
13
|
try {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
true,
|
|
38
|
-
)
|
|
14
|
+
const buffer = concat(encodeNumber(type, 'Uint8'), ...buffers)
|
|
15
|
+
const result = ws.send(buffer, true)
|
|
39
16
|
if (result === 0) {
|
|
40
|
-
data.backpressure =
|
|
17
|
+
data.backpressure = createPromise()
|
|
41
18
|
return false
|
|
42
19
|
}
|
|
43
20
|
if (result === 2) {
|
|
@@ -49,22 +26,6 @@ export const send = (
|
|
|
49
26
|
}
|
|
50
27
|
}
|
|
51
28
|
|
|
52
|
-
export const getFormat = ({ headers, query }: RequestData, format: Format) => {
|
|
53
|
-
const contentType = headers.get('content-type') || query.get('content-type')
|
|
54
|
-
const acceptType = headers.get('accept') || query.get('accept')
|
|
55
|
-
|
|
56
|
-
const encoder = contentType ? format.supportsEncoder(contentType) : undefined
|
|
57
|
-
if (!encoder) throw new Error('Unsupported content-type')
|
|
58
|
-
|
|
59
|
-
const decoder = acceptType ? format.supportsDecoder(acceptType) : undefined
|
|
60
|
-
if (!decoder) throw new Error('Unsupported accept')
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
encoder,
|
|
64
|
-
decoder,
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
29
|
export const toRecord = (input: {
|
|
69
30
|
forEach: (cb: (value, key) => void) => void
|
|
70
31
|
}) => {
|
|
@@ -103,13 +64,13 @@ export const getRequestData = (req: HttpRequest): RequestData => {
|
|
|
103
64
|
}
|
|
104
65
|
|
|
105
66
|
export const InternalError = (message = 'Internal Server Error') =>
|
|
106
|
-
new
|
|
67
|
+
new ProtocolError(ErrorCode.InternalServerError, message)
|
|
107
68
|
|
|
108
69
|
export const NotFoundError = (message = 'Not Found') =>
|
|
109
|
-
new
|
|
70
|
+
new ProtocolError(ErrorCode.NotFound, message)
|
|
110
71
|
|
|
111
72
|
export const ForbiddenError = (message = 'Forbidden') =>
|
|
112
|
-
new
|
|
73
|
+
new ProtocolError(ErrorCode.Forbidden, message)
|
|
113
74
|
|
|
114
75
|
export const RequestTimeoutError = (message = 'Request Timeout') =>
|
|
115
|
-
new
|
|
76
|
+
new ProtocolError(ErrorCode.RequestTimeout, message)
|
package/package.json
CHANGED
|
@@ -8,15 +8,12 @@
|
|
|
8
8
|
}
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"@nmtjs/
|
|
15
|
-
"@nmtjs/common": "^0.3.9"
|
|
11
|
+
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.51.0",
|
|
12
|
+
"@nmtjs/core": "^0.6.1",
|
|
13
|
+
"@nmtjs/protocol": "^0.6.1",
|
|
14
|
+
"@nmtjs/common": "^0.6.1"
|
|
16
15
|
},
|
|
17
16
|
"devDependencies": {
|
|
18
|
-
"@nmtjs/application": "^0.3.9",
|
|
19
|
-
"@nmtjs/common": "^0.3.9",
|
|
20
17
|
"@types/node": "^18.0.0"
|
|
21
18
|
},
|
|
22
19
|
"engines": {
|
|
@@ -30,7 +27,7 @@
|
|
|
30
27
|
"LICENSE.md",
|
|
31
28
|
"README.md"
|
|
32
29
|
],
|
|
33
|
-
"version": "0.
|
|
30
|
+
"version": "0.2.0",
|
|
34
31
|
"scripts": {
|
|
35
32
|
"build": "neemata-build ./index.ts './lib/**/*.ts'",
|
|
36
33
|
"type-check": "tsc --noEmit"
|