@nmtjs/ws-client 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/dist/index.d.ts +31 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/package.json +9 -8
- package/src/index.ts +106 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ClientCallOptions, ClientTransportFactory, ClientTransportStartParams } from '@nmtjs/client';
|
|
2
|
+
import type { ProtocolVersion } from '@nmtjs/protocol';
|
|
3
|
+
import type { BaseClientFormat } from '@nmtjs/protocol/client';
|
|
4
|
+
import { ConnectionType } from '@nmtjs/protocol';
|
|
5
|
+
export type WsClientTransportOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* The origin of the server
|
|
8
|
+
* @example 'ws://localhost:3000'
|
|
9
|
+
*/
|
|
10
|
+
url: string;
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Custom WebSocket class
|
|
14
|
+
* @default globalThis.WebSocket
|
|
15
|
+
*/
|
|
16
|
+
WebSocket?: typeof WebSocket;
|
|
17
|
+
};
|
|
18
|
+
export declare class WsTransportClient {
|
|
19
|
+
protected readonly format: BaseClientFormat;
|
|
20
|
+
protected readonly protocol: ProtocolVersion;
|
|
21
|
+
protected options: WsClientTransportOptions;
|
|
22
|
+
type: ConnectionType.Bidirectional;
|
|
23
|
+
protected webSocket: WebSocket | null;
|
|
24
|
+
protected connecting: Promise<void> | null;
|
|
25
|
+
constructor(format: BaseClientFormat, protocol: ProtocolVersion, options: WsClientTransportOptions);
|
|
26
|
+
connect(params: ClientTransportStartParams): Promise<void>;
|
|
27
|
+
disconnect(): Promise<void>;
|
|
28
|
+
send(message: ArrayBufferView, options: ClientCallOptions): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
export type WsTransportFactory = ClientTransportFactory<ConnectionType.Bidirectional, WsClientTransportOptions, WsTransportClient>;
|
|
31
|
+
export declare const WsTransportFactory: WsTransportFactory;
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAiBhD,MAAM,OAAO,iBAAiB;IAOP;IACA;IACT;IARZ,IAAI,GAAiC,cAAc,CAAC,aAAa,CAAA;IAEvD,SAAS,GAAqB,IAAI,CAAA;IAClC,UAAU,GAAyB,IAAI,CAAA;IAEjD,YACqB,MAAwB,EACxB,QAAyB,EAClC,OAAiC;QAFxB,WAAM,GAAN,MAAM,CAAkB;QACxB,aAAQ,GAAR,QAAQ,CAAiB;QAClC,YAAO,GAAP,OAAO,CAA0B;QAE3C,IAAI,CAAC,OAAO,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAA;IAC7C,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,MAAkC;QAC9C,MAAM,GAAG,GAAG,IAAI,GAAG,CACjB,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,GAAG,EACnD,IAAI,CAAC,OAAO,CAAC,GAAG,CACjB,CAAA;QAED,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,KAAK,MAAM,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAA;QAEnE,GAAG,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAA;QACtC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,cAAc,EAAE,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;QAC7D,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,MA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CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;QAEF,IAAI,CAAC,SAAS,GAAG,EAAE,CAAA;QAEnB,OAAO,IAAI,CAAC,UAAU,CAAA;IACxB,CAAC;IAED,KAAK,CAAC,UAAU;QACd,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAAE,OAAM;QACnC,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,OAA0B;QAC7D,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;QAC1E,MAAM,IAAI,CAAC,UAAU,CAAA;QACrB,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO;YAAE,IAAI,CAAC,SAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAC7D,CAAC;CACF;AAQD,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
|
@@ -5,26 +5,27 @@
|
|
|
5
5
|
".": "./dist/index.js"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@nmtjs/
|
|
9
|
-
"@nmtjs/
|
|
8
|
+
"@nmtjs/client": "0.15.0-beta.4",
|
|
9
|
+
"@nmtjs/common": "0.15.0-beta.4"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@nmtjs/protocol": "0.15.0-beta.
|
|
12
|
+
"@nmtjs/protocol": "0.15.0-beta.4"
|
|
13
13
|
},
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@nmtjs/
|
|
16
|
-
"@nmtjs/
|
|
17
|
-
"@nmtjs/
|
|
15
|
+
"@nmtjs/client": "0.15.0-beta.4",
|
|
16
|
+
"@nmtjs/common": "0.15.0-beta.4",
|
|
17
|
+
"@nmtjs/protocol": "0.15.0-beta.4"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
|
21
|
+
"src",
|
|
21
22
|
"LICENSE.md",
|
|
22
23
|
"README.md"
|
|
23
24
|
],
|
|
24
|
-
"version": "0.15.0-beta.
|
|
25
|
+
"version": "0.15.0-beta.4",
|
|
25
26
|
"scripts": {
|
|
26
27
|
"clean-build": "rm -rf ./dist",
|
|
27
|
-
"build": "tsc",
|
|
28
|
+
"build": "tsc --declaration --sourcemap",
|
|
28
29
|
"dev": "tsc --watch",
|
|
29
30
|
"type-check": "tsc --noEmit"
|
|
30
31
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClientCallOptions,
|
|
3
|
+
ClientTransportFactory,
|
|
4
|
+
ClientTransportStartParams,
|
|
5
|
+
} from '@nmtjs/client'
|
|
6
|
+
import type { ProtocolVersion } from '@nmtjs/protocol'
|
|
7
|
+
import type { BaseClientFormat } from '@nmtjs/protocol/client'
|
|
8
|
+
import { once } from '@nmtjs/common'
|
|
9
|
+
import { ConnectionType } from '@nmtjs/protocol'
|
|
10
|
+
|
|
11
|
+
export type WsClientTransportOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* The origin of the server
|
|
14
|
+
* @example 'ws://localhost:3000'
|
|
15
|
+
*/
|
|
16
|
+
url: string
|
|
17
|
+
debug?: boolean
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Custom WebSocket class
|
|
21
|
+
* @default globalThis.WebSocket
|
|
22
|
+
*/
|
|
23
|
+
WebSocket?: typeof WebSocket
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class WsTransportClient {
|
|
27
|
+
type: ConnectionType.Bidirectional = ConnectionType.Bidirectional
|
|
28
|
+
|
|
29
|
+
protected webSocket: WebSocket | null = null
|
|
30
|
+
protected connecting: Promise<void> | null = null
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
protected readonly format: BaseClientFormat,
|
|
34
|
+
protected readonly protocol: ProtocolVersion,
|
|
35
|
+
protected options: WsClientTransportOptions,
|
|
36
|
+
) {
|
|
37
|
+
this.options = { debug: false, ...options }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async connect(params: ClientTransportStartParams) {
|
|
41
|
+
const url = new URL(
|
|
42
|
+
params.application ? `/${params.application}` : '/',
|
|
43
|
+
this.options.url,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
const secure = url.protocol === 'wss:' || url.protocol === 'https:'
|
|
47
|
+
|
|
48
|
+
url.protocol = secure ? 'wss:' : 'ws:'
|
|
49
|
+
url.searchParams.set('content-type', this.format.contentType)
|
|
50
|
+
url.searchParams.set('accept', this.format.contentType)
|
|
51
|
+
|
|
52
|
+
if (params.auth) {
|
|
53
|
+
url.searchParams.set('auth', params.auth)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const ws = this.options.WebSocket
|
|
57
|
+
? new this.options.WebSocket(url)
|
|
58
|
+
: new WebSocket(url.toString())
|
|
59
|
+
|
|
60
|
+
ws.binaryType = 'arraybuffer'
|
|
61
|
+
|
|
62
|
+
this.connecting = new Promise((resolve, reject) => {
|
|
63
|
+
ws.addEventListener('open', () => {
|
|
64
|
+
params.onConnect()
|
|
65
|
+
this.connecting = null
|
|
66
|
+
resolve()
|
|
67
|
+
})
|
|
68
|
+
ws.addEventListener('message', (event) => {
|
|
69
|
+
params.onMessage(new Uint8Array(event.data as ArrayBuffer))
|
|
70
|
+
})
|
|
71
|
+
ws.addEventListener('error', (event) => {
|
|
72
|
+
reject(new Error('WebSocket error', { cause: event }))
|
|
73
|
+
})
|
|
74
|
+
ws.addEventListener('close', (event) => {
|
|
75
|
+
params.onDisconnect(event.reason)
|
|
76
|
+
this.webSocket = null
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
this.webSocket = ws
|
|
81
|
+
|
|
82
|
+
return this.connecting
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async disconnect() {
|
|
86
|
+
if (this.webSocket === null) return
|
|
87
|
+
const closing = once(this.webSocket, 'close')
|
|
88
|
+
this.webSocket!.close(1000, 'client')
|
|
89
|
+
return closing
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async send(message: ArrayBufferView, options: ClientCallOptions) {
|
|
93
|
+
if (this.webSocket === null) throw new Error('WebSocket is not connected')
|
|
94
|
+
await this.connecting
|
|
95
|
+
if (!options.signal?.aborted) this.webSocket!.send(message)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type WsTransportFactory = ClientTransportFactory<
|
|
100
|
+
ConnectionType.Bidirectional,
|
|
101
|
+
WsClientTransportOptions,
|
|
102
|
+
WsTransportClient
|
|
103
|
+
>
|
|
104
|
+
|
|
105
|
+
export const WsTransportFactory: WsTransportFactory = (params, options) =>
|
|
106
|
+
new WsTransportClient(params.format, params.protocol, options)
|