@nmtjs/ws-client 0.15.0-beta.37 → 0.15.0-beta.38
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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/index.d.ts +19 -18
- package/dist/index.js +45 -49
- package/dist/index.js.map +1 -0
- package/package.json +12 -14
- package/src/index.ts +110 -0
package/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
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 = {
|
|
5
6
|
/**
|
|
6
7
|
* The origin of the server
|
|
7
|
-
* @example '
|
|
8
|
+
* @example 'ws://localhost:3000'
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
|
+
url: string;
|
|
11
|
+
debug?: boolean;
|
|
10
12
|
/**
|
|
11
13
|
* Custom WebSocket class
|
|
12
14
|
* @default globalThis.WebSocket
|
|
13
15
|
*/
|
|
14
|
-
|
|
15
|
-
debug?: boolean;
|
|
16
|
+
WebSocket?: typeof WebSocket;
|
|
16
17
|
};
|
|
17
|
-
export declare class
|
|
18
|
-
protected readonly
|
|
18
|
+
export declare class WsTransportClient {
|
|
19
|
+
protected readonly format: BaseClientFormat;
|
|
20
|
+
protected readonly protocol: ProtocolVersion;
|
|
21
|
+
protected options: WsClientTransportOptions;
|
|
22
|
+
type: ConnectionType.Bidirectional;
|
|
19
23
|
protected webSocket: WebSocket | null;
|
|
20
24
|
protected connecting: Promise<void> | null;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void>;
|
|
25
|
+
constructor(format: BaseClientFormat, protocol: ProtocolVersion, options: WsClientTransportOptions);
|
|
26
|
+
connect(params: ClientTransportStartParams): Promise<void>;
|
|
24
27
|
disconnect(): Promise<void>;
|
|
25
|
-
|
|
26
|
-
procedure: string;
|
|
27
|
-
signal: AbortSignal;
|
|
28
|
-
}>;
|
|
29
|
-
send(messageType: ClientMessageType, buffer: ArrayBuffer): Promise<void>;
|
|
28
|
+
send(message: ArrayBufferView, options: ClientCallOptions): Promise<void>;
|
|
30
29
|
}
|
|
30
|
+
export type WsTransportFactory = ClientTransportFactory<ConnectionType.Bidirectional, WsClientTransportOptions, WsTransportClient>;
|
|
31
|
+
export declare const WsTransportFactory: WsTransportFactory;
|
package/dist/index.js
CHANGED
|
@@ -1,71 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export class
|
|
1
|
+
import { once } from '@nmtjs/common';
|
|
2
|
+
import { ConnectionType } from '@nmtjs/protocol';
|
|
3
|
+
export class WsTransportClient {
|
|
4
|
+
format;
|
|
4
5
|
protocol;
|
|
6
|
+
options;
|
|
7
|
+
type = ConnectionType.Bidirectional;
|
|
5
8
|
webSocket = null;
|
|
6
9
|
connecting = null;
|
|
7
|
-
options
|
|
8
|
-
|
|
9
|
-
super();
|
|
10
|
+
constructor(format, protocol, options) {
|
|
11
|
+
this.format = format;
|
|
10
12
|
this.protocol = protocol;
|
|
13
|
+
this.options = options;
|
|
11
14
|
this.options = { debug: false, ...options };
|
|
12
15
|
}
|
|
13
|
-
connect(
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
async connect(params) {
|
|
17
|
+
const url = new URL(params.application ? `/${params.application}` : '/', this.options.url);
|
|
18
|
+
const secure = url.protocol === 'wss:' || url.protocol === 'https:';
|
|
19
|
+
url.protocol = secure ? 'wss:' : 'ws:';
|
|
20
|
+
url.searchParams.set('content-type', this.format.contentType);
|
|
21
|
+
url.searchParams.set('accept', this.format.contentType);
|
|
22
|
+
if (params.auth) {
|
|
23
|
+
url.searchParams.set('auth', params.auth);
|
|
19
24
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
const ws = this.options.WebSocket
|
|
26
|
+
? new this.options.WebSocket(url)
|
|
27
|
+
: new WebSocket(url.toString());
|
|
23
28
|
ws.binaryType = 'arraybuffer';
|
|
24
|
-
this.status = ProtocolTransportStatus.CONNECTING;
|
|
25
|
-
ws.addEventListener('message', ({ data }) => {
|
|
26
|
-
this.protocol.handleServerMessage(data, this, transformer);
|
|
27
|
-
});
|
|
28
|
-
ws.addEventListener('close', (event) => {
|
|
29
|
-
this.status = ProtocolTransportStatus.DISCONNECTED;
|
|
30
|
-
this.emit('disconnected', event.code === 1000
|
|
31
|
-
? event.reason === 'client'
|
|
32
|
-
? 'client'
|
|
33
|
-
: 'server'
|
|
34
|
-
: 'error');
|
|
35
|
-
this.webSocket = null;
|
|
36
|
-
}, { once: true });
|
|
37
|
-
this.webSocket = ws;
|
|
38
29
|
this.connecting = new Promise((resolve, reject) => {
|
|
39
30
|
ws.addEventListener('open', () => {
|
|
40
|
-
this.
|
|
41
|
-
|
|
31
|
+
this.connecting = null;
|
|
32
|
+
params.onConnect();
|
|
42
33
|
resolve();
|
|
43
|
-
}
|
|
34
|
+
});
|
|
35
|
+
ws.addEventListener('message', (event) => {
|
|
36
|
+
params.onMessage(new Uint8Array(event.data));
|
|
37
|
+
});
|
|
44
38
|
ws.addEventListener('error', (event) => {
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
this.connecting = null;
|
|
40
|
+
reject(new Error('WebSocket error', { cause: event.error }));
|
|
41
|
+
});
|
|
42
|
+
ws.addEventListener('close', (event) => {
|
|
43
|
+
this.webSocket = null;
|
|
44
|
+
this.connecting = null;
|
|
45
|
+
params.onDisconnect('server');
|
|
46
|
+
});
|
|
47
47
|
});
|
|
48
|
+
this.webSocket = ws;
|
|
48
49
|
return this.connecting;
|
|
49
50
|
}
|
|
50
51
|
async disconnect() {
|
|
51
52
|
if (this.webSocket === null)
|
|
52
53
|
return;
|
|
54
|
+
const closing = once(this.webSocket, 'close');
|
|
53
55
|
this.webSocket.close(1000, 'client');
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
async call(procedure, payload, options, transformer) {
|
|
57
|
-
const { call, buffer } = this.protocol.createRpc(procedure, payload, options, transformer);
|
|
58
|
-
await this.send(ClientMessageType.Rpc, buffer);
|
|
59
|
-
return call;
|
|
56
|
+
return closing;
|
|
60
57
|
}
|
|
61
|
-
async send(
|
|
62
|
-
if (this.
|
|
63
|
-
|
|
64
|
-
this.
|
|
58
|
+
async send(message, options) {
|
|
59
|
+
if (this.webSocket === null)
|
|
60
|
+
throw new Error('WebSocket is not connected');
|
|
61
|
+
await this.connecting;
|
|
62
|
+
if (!options.signal?.aborted)
|
|
63
|
+
this.webSocket.send(message);
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
target.addEventListener(event, () => resolve(), { once: true });
|
|
70
|
-
});
|
|
71
|
-
}
|
|
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,eA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,UAAU,CAAA;IAAA,CACvB;IAED,KAAK,CAAC,UAAU,GAAG;QACjB,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;IAAA,CACf;IAED,KAAK,CAAC,IAAI,CAAC,OAAwB,EAAE,OAA0B,EAAE;QAC/D,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;IAAA,CAC5D;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
|
@@ -2,30 +2,28 @@
|
|
|
2
2
|
"name": "@nmtjs/ws-client",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
|
-
".":
|
|
6
|
-
"types": "./dist/index.d.ts",
|
|
7
|
-
"import": "./dist/index.js",
|
|
8
|
-
"module-sync": "./dist/index.js"
|
|
9
|
-
}
|
|
5
|
+
".": "./dist/index.js"
|
|
10
6
|
},
|
|
11
7
|
"dependencies": {
|
|
12
|
-
"@nmtjs/client": "0.15.0-beta.
|
|
13
|
-
"@nmtjs/
|
|
14
|
-
|
|
8
|
+
"@nmtjs/client": "0.15.0-beta.38",
|
|
9
|
+
"@nmtjs/common": "0.15.0-beta.38"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@nmtjs/protocol": "0.15.0-beta.38"
|
|
15
13
|
},
|
|
16
14
|
"peerDependencies": {
|
|
17
|
-
"@nmtjs/
|
|
18
|
-
"@nmtjs/
|
|
19
|
-
"@nmtjs/protocol": "0.15.0-beta.
|
|
15
|
+
"@nmtjs/client": "0.15.0-beta.38",
|
|
16
|
+
"@nmtjs/common": "0.15.0-beta.38",
|
|
17
|
+
"@nmtjs/protocol": "0.15.0-beta.38"
|
|
20
18
|
},
|
|
21
19
|
"files": [
|
|
22
20
|
"dist",
|
|
21
|
+
"src",
|
|
23
22
|
"LICENSE.md",
|
|
24
23
|
"README.md"
|
|
25
24
|
],
|
|
26
|
-
"version": "0.15.0-beta.
|
|
25
|
+
"version": "0.15.0-beta.38",
|
|
27
26
|
"scripts": {
|
|
28
|
-
"build": "
|
|
29
|
-
"type-check": "tsc --noEmit"
|
|
27
|
+
"clean-build": "rm -rf ./dist"
|
|
30
28
|
}
|
|
31
29
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
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(
|
|
74
|
+
new Error('WebSocket error', { cause: (event as ErrorEvent).error }),
|
|
75
|
+
)
|
|
76
|
+
})
|
|
77
|
+
ws.addEventListener('close', (event) => {
|
|
78
|
+
this.webSocket = null
|
|
79
|
+
this.connecting = null
|
|
80
|
+
params.onDisconnect('server')
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
this.webSocket = ws
|
|
85
|
+
|
|
86
|
+
return this.connecting
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async disconnect() {
|
|
90
|
+
if (this.webSocket === null) return
|
|
91
|
+
const closing = once(this.webSocket, 'close')
|
|
92
|
+
this.webSocket!.close(1000, 'client')
|
|
93
|
+
return closing
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async send(message: ArrayBufferView, options: ClientCallOptions) {
|
|
97
|
+
if (this.webSocket === null) throw new Error('WebSocket is not connected')
|
|
98
|
+
await this.connecting
|
|
99
|
+
if (!options.signal?.aborted) this.webSocket!.send(message)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type WsTransportFactory = ClientTransportFactory<
|
|
104
|
+
ConnectionType.Bidirectional,
|
|
105
|
+
WsClientTransportOptions,
|
|
106
|
+
WsTransportClient
|
|
107
|
+
>
|
|
108
|
+
|
|
109
|
+
export const WsTransportFactory: WsTransportFactory = (params, options) =>
|
|
110
|
+
new WsTransportClient(params.format, params.protocol, options)
|