@nmtjs/ws-client 0.15.0-beta.2 → 0.15.0-beta.20

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,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
@@ -28,19 +28,21 @@ export class WsTransportClient {
28
28
  ws.binaryType = 'arraybuffer';
29
29
  this.connecting = new Promise((resolve, reject) => {
30
30
  ws.addEventListener('open', () => {
31
- params.onConnect();
32
31
  this.connecting = null;
32
+ params.onConnect();
33
33
  resolve();
34
34
  });
35
35
  ws.addEventListener('message', (event) => {
36
36
  params.onMessage(new Uint8Array(event.data));
37
37
  });
38
38
  ws.addEventListener('error', (event) => {
39
+ this.connecting = null;
39
40
  reject(new Error('WebSocket error', { cause: event }));
40
41
  });
41
42
  ws.addEventListener('close', (event) => {
42
- params.onDisconnect(event.reason);
43
43
  this.webSocket = null;
44
+ this.connecting = null;
45
+ params.onDisconnect('server');
44
46
  });
45
47
  });
46
48
  this.webSocket = ws;
@@ -62,3 +64,4 @@ export class WsTransportClient {
62
64
  }
63
65
  }
64
66
  export const WsTransportFactory = (params, options) => new WsTransportClient(params.format, params.protocol, options);
67
+ //# sourceMappingURL=index.js.map
@@ -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,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dist/index.js"
6
6
  },
7
7
  "dependencies": {
8
- "@nmtjs/common": "0.15.0-beta.2",
9
- "@nmtjs/client": "0.15.0-beta.2"
8
+ "@nmtjs/client": "0.15.0-beta.20",
9
+ "@nmtjs/common": "0.15.0-beta.20"
10
10
  },
11
11
  "devDependencies": {
12
- "@nmtjs/protocol": "0.15.0-beta.2"
12
+ "@nmtjs/protocol": "0.15.0-beta.20"
13
13
  },
14
14
  "peerDependencies": {
15
- "@nmtjs/common": "0.15.0-beta.2",
16
- "@nmtjs/protocol": "0.15.0-beta.2",
17
- "@nmtjs/client": "0.15.0-beta.2"
15
+ "@nmtjs/client": "0.15.0-beta.20",
16
+ "@nmtjs/common": "0.15.0-beta.20",
17
+ "@nmtjs/protocol": "0.15.0-beta.20"
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.2",
25
+ "version": "0.15.0-beta.20",
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,108 @@
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
+ this.connecting = null
65
+ params.onConnect()
66
+ resolve()
67
+ })
68
+ ws.addEventListener('message', (event) => {
69
+ params.onMessage(new Uint8Array(event.data as ArrayBuffer))
70
+ })
71
+ ws.addEventListener('error', (event) => {
72
+ this.connecting = null
73
+ reject(new Error('WebSocket error', { cause: event }))
74
+ })
75
+ ws.addEventListener('close', (event) => {
76
+ this.webSocket = null
77
+ this.connecting = null
78
+ params.onDisconnect('server')
79
+ })
80
+ })
81
+
82
+ this.webSocket = ws
83
+
84
+ return this.connecting
85
+ }
86
+
87
+ async disconnect() {
88
+ if (this.webSocket === null) return
89
+ const closing = once(this.webSocket, 'close')
90
+ this.webSocket!.close(1000, 'client')
91
+ return closing
92
+ }
93
+
94
+ async send(message: ArrayBufferView, options: ClientCallOptions) {
95
+ if (this.webSocket === null) throw new Error('WebSocket is not connected')
96
+ await this.connecting
97
+ if (!options.signal?.aborted) this.webSocket!.send(message)
98
+ }
99
+ }
100
+
101
+ export type WsTransportFactory = ClientTransportFactory<
102
+ ConnectionType.Bidirectional,
103
+ WsClientTransportOptions,
104
+ WsTransportClient
105
+ >
106
+
107
+ export const WsTransportFactory: WsTransportFactory = (params, options) =>
108
+ new WsTransportClient(params.format, params.protocol, options)