@nmtjs/ws-client 0.17.0-beta.6 → 0.17.0-beta.8
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/index.js +14 -4
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +21 -3
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { once } from '@nmtjs/common';
|
|
2
|
-
import { ConnectionType } from '@nmtjs/protocol';
|
|
2
|
+
import { ConnectionType, ErrorCode } from '@nmtjs/protocol';
|
|
3
|
+
import { ProtocolError } from '@nmtjs/protocol/client';
|
|
4
|
+
// WebSocket.OPEN without touching the global: a custom implementation may be injected
|
|
5
|
+
const WS_OPEN = 1;
|
|
3
6
|
export class WsTransportClient {
|
|
4
7
|
format;
|
|
5
8
|
protocol;
|
|
@@ -83,11 +86,18 @@ export class WsTransportClient {
|
|
|
83
86
|
return closing;
|
|
84
87
|
}
|
|
85
88
|
async send(message, options) {
|
|
86
|
-
if (this.webSocket === null)
|
|
87
|
-
throw new
|
|
89
|
+
if (this.webSocket === null) {
|
|
90
|
+
throw new ProtocolError(ErrorCode.ConnectionError, 'WebSocket is not connected');
|
|
91
|
+
}
|
|
88
92
|
await this.connecting;
|
|
93
|
+
// the close handler may null the socket, or a concurrent disconnect may
|
|
94
|
+
// start closing it, while this send is suspended on the await
|
|
95
|
+
const webSocket = this.webSocket;
|
|
96
|
+
if (webSocket === null || webSocket.readyState !== WS_OPEN) {
|
|
97
|
+
throw new ProtocolError(ErrorCode.ConnectionError, 'WebSocket is not open');
|
|
98
|
+
}
|
|
89
99
|
if (!options.signal?.aborted)
|
|
90
|
-
|
|
100
|
+
webSocket.send(message);
|
|
91
101
|
}
|
|
92
102
|
}
|
|
93
103
|
export const WsTransportFactory = (params, options) => new WsTransportClient(params.format, params.protocol, options);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js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AC3B,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;QAC7C,IAAI,CAAC,SAAU,CAAC,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QACrC,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAwB,EAAE,OAA6B;QAChE,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;YAC5B,MAAM,IAAI,aAAa,CACrB,SAAS,CAAC,eAAe,EACzB,4BAA4B,CAC7B,CAAA;QACH,CAAC;QACD,MAAM,IAAI,CAAC,UAAU,CAAA;QACrB,wEAAwE;QACxE,8DAA8D;QAC9D,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAA;QAChC,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,CAAC,UAAU,KAAK,OAAO,EAAE,CAAC;YAC3D,MAAM,IAAI,aAAa,CACrB,SAAS,CAAC,eAAe,EACzB,uBAAuB,CACxB,CAAA;QACH,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO;YAAE,SAAS,CAAC,IAAI,CAAC,OAAc,CAAC,CAAA;IAC9D,CAAC;CACF;AAOD,MAAM,CAAC,MAAM,kBAAkB,GAAuB,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,CACxE,IAAI,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -10,16 +10,16 @@
|
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@nmtjs/client": "0.17.0-beta.
|
|
14
|
-
"@nmtjs/common": "0.17.0-beta.
|
|
13
|
+
"@nmtjs/client": "0.17.0-beta.8",
|
|
14
|
+
"@nmtjs/common": "0.17.0-beta.8"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@nmtjs/protocol": "0.17.0-beta.
|
|
17
|
+
"@nmtjs/protocol": "0.17.0-beta.8"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@nmtjs/
|
|
21
|
-
"@nmtjs/
|
|
22
|
-
"@nmtjs/
|
|
20
|
+
"@nmtjs/common": "0.17.0-beta.8",
|
|
21
|
+
"@nmtjs/protocol": "0.17.0-beta.8",
|
|
22
|
+
"@nmtjs/client": "0.17.0-beta.8"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"LICENSE.md",
|
|
28
28
|
"README.md"
|
|
29
29
|
],
|
|
30
|
-
"version": "0.17.0-beta.
|
|
30
|
+
"version": "0.17.0-beta.8",
|
|
31
31
|
"repository": {
|
|
32
32
|
"type": "git",
|
|
33
33
|
"url": "https://github.com/neematajs/neemata"
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,11 @@ import type {
|
|
|
7
7
|
import type { ProtocolVersion } from '@nmtjs/protocol'
|
|
8
8
|
import type { BaseClientFormat } from '@nmtjs/protocol/client'
|
|
9
9
|
import { once } from '@nmtjs/common'
|
|
10
|
-
import { ConnectionType } from '@nmtjs/protocol'
|
|
10
|
+
import { ConnectionType, ErrorCode } from '@nmtjs/protocol'
|
|
11
|
+
import { ProtocolError } from '@nmtjs/protocol/client'
|
|
12
|
+
|
|
13
|
+
// WebSocket.OPEN without touching the global: a custom implementation may be injected
|
|
14
|
+
const WS_OPEN = 1
|
|
11
15
|
|
|
12
16
|
export type WsClientTransportOptions = {
|
|
13
17
|
/**
|
|
@@ -126,9 +130,23 @@ export class WsTransportClient implements BidirectionalTransport {
|
|
|
126
130
|
}
|
|
127
131
|
|
|
128
132
|
async send(message: ArrayBufferView, options: TransportSendOptions) {
|
|
129
|
-
if (this.webSocket === null)
|
|
133
|
+
if (this.webSocket === null) {
|
|
134
|
+
throw new ProtocolError(
|
|
135
|
+
ErrorCode.ConnectionError,
|
|
136
|
+
'WebSocket is not connected',
|
|
137
|
+
)
|
|
138
|
+
}
|
|
130
139
|
await this.connecting
|
|
131
|
-
|
|
140
|
+
// the close handler may null the socket, or a concurrent disconnect may
|
|
141
|
+
// start closing it, while this send is suspended on the await
|
|
142
|
+
const webSocket = this.webSocket
|
|
143
|
+
if (webSocket === null || webSocket.readyState !== WS_OPEN) {
|
|
144
|
+
throw new ProtocolError(
|
|
145
|
+
ErrorCode.ConnectionError,
|
|
146
|
+
'WebSocket is not open',
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
if (!options.signal?.aborted) webSocket.send(message as any)
|
|
132
150
|
}
|
|
133
151
|
}
|
|
134
152
|
|