@nmtjs/ws-client 0.12.6 → 0.12.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.
Files changed (2) hide show
  1. package/package.json +7 -8
  2. package/src/index.ts +0 -134
package/package.json CHANGED
@@ -9,22 +9,21 @@
9
9
  }
10
10
  },
11
11
  "dependencies": {
12
- "@nmtjs/client": "0.12.6",
13
- "@nmtjs/common": "0.12.6",
14
- "@nmtjs/protocol": "0.12.6"
12
+ "@nmtjs/client": "0.12.8",
13
+ "@nmtjs/common": "0.12.8",
14
+ "@nmtjs/protocol": "0.12.8"
15
15
  },
16
16
  "peerDependencies": {
17
- "@nmtjs/client": "0.12.6",
18
- "@nmtjs/common": "0.12.6",
19
- "@nmtjs/protocol": "0.12.6"
17
+ "@nmtjs/client": "0.12.8",
18
+ "@nmtjs/common": "0.12.8",
19
+ "@nmtjs/protocol": "0.12.8"
20
20
  },
21
21
  "files": [
22
- "src",
23
22
  "dist",
24
23
  "LICENSE.md",
25
24
  "README.md"
26
25
  ],
27
- "version": "0.12.6",
26
+ "version": "0.12.8",
28
27
  "scripts": {
29
28
  "build": "tsc",
30
29
  "type-check": "tsc --noEmit"
package/src/index.ts DELETED
@@ -1,134 +0,0 @@
1
- import { ClientMessageType, concat, encodeNumber } from '@nmtjs/protocol'
2
- import {
3
- type Protocol,
4
- type ProtocolBaseClientCallOptions,
5
- type ProtocolBaseTransformer,
6
- ProtocolTransport,
7
- ProtocolTransportStatus,
8
- } from '@nmtjs/protocol/client'
9
-
10
- export type WebSocketClientTransportOptions = {
11
- /**
12
- * The origin of the server
13
- * @example 'http://localhost:3000'
14
- */
15
- origin: string
16
- /**
17
- * Custom WebSocket class
18
- * @default globalThis.WebSocket
19
- */
20
- wsFactory?: (url: URL) => WebSocket
21
-
22
- debug?: boolean
23
- }
24
-
25
- export class WebSocketClientTransport extends ProtocolTransport {
26
- protected webSocket: WebSocket | null = null
27
- protected connecting: Promise<void> | null = null
28
- protected options: WebSocketClientTransportOptions
29
-
30
- constructor(
31
- protected readonly protocol: Protocol,
32
- options: WebSocketClientTransportOptions,
33
- ) {
34
- super()
35
- this.options = {
36
- debug: false,
37
- ...options,
38
- }
39
- }
40
-
41
- connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void> {
42
- // this.auth = auth
43
- const wsUrl = new URL('/api', this.options.origin)
44
- if (this.protocol.contentType) {
45
- wsUrl.searchParams.set('content-type', this.protocol.contentType)
46
- wsUrl.searchParams.set('accept', this.protocol.contentType)
47
- }
48
- if (auth) wsUrl.searchParams.set('auth', auth)
49
-
50
- const ws =
51
- this.options.wsFactory?.(wsUrl) ?? new WebSocket(wsUrl.toString())
52
-
53
- ws.binaryType = 'arraybuffer'
54
-
55
- this.status = ProtocolTransportStatus.CONNECTING
56
-
57
- ws.addEventListener('message', ({ data }) => {
58
- this.protocol.handleServerMessage(data as ArrayBuffer, this, transformer)
59
- })
60
-
61
- ws.addEventListener(
62
- 'close',
63
- (event) => {
64
- console.dir(event)
65
- this.status = ProtocolTransportStatus.DISCONNECTED
66
- if (event.code !== 1000) this.emit('disconnected')
67
- this.webSocket = null
68
- },
69
- { once: true },
70
- )
71
-
72
- this.webSocket = ws
73
-
74
- this.connecting = new Promise((resolve, reject) => {
75
- ws.addEventListener(
76
- 'open',
77
- () => {
78
- this.status = ProtocolTransportStatus.CONNECTED
79
- this.emit('connected')
80
- resolve()
81
- },
82
- { once: true },
83
- )
84
-
85
- ws.addEventListener(
86
- 'error',
87
- (event) => {
88
- reject(new Error('WebSocket error', { cause: event }))
89
- },
90
- { once: true },
91
- )
92
- })
93
-
94
- return this.connecting
95
- }
96
-
97
- async disconnect(): Promise<void> {
98
- if (this.webSocket === null) return
99
- this.webSocket!.close(1000, 'user')
100
- return _once(this.webSocket, 'close')
101
- }
102
-
103
- async call(
104
- namespace: string,
105
- procedure: string,
106
- payload: any,
107
- options: ProtocolBaseClientCallOptions,
108
- transformer: ProtocolBaseTransformer,
109
- ) {
110
- const { call, buffer } = this.protocol.createRpc(
111
- namespace,
112
- procedure,
113
- payload,
114
- options,
115
- transformer,
116
- )
117
- await this.send(ClientMessageType.Rpc, buffer)
118
- return call
119
- }
120
-
121
- async send(
122
- messageType: ClientMessageType,
123
- buffer: ArrayBuffer,
124
- ): Promise<void> {
125
- if (this.connecting) await this.connecting
126
- this.webSocket!.send(concat(encodeNumber(messageType, 'Uint8'), buffer))
127
- }
128
- }
129
-
130
- function _once(target: EventTarget, event: string) {
131
- return new Promise<void>((resolve) => {
132
- target.addEventListener(event, () => resolve(), { once: true })
133
- })
134
- }