@nmtjs/ws-client 0.14.5 → 0.15.0-beta.2
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.js +41 -48
- package/package.json +12 -12
- package/dist/index.d.ts +0 -30
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.js
CHANGED
|
@@ -1,71 +1,64 @@
|
|
|
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
|
-
|
|
41
|
-
this.
|
|
31
|
+
params.onConnect();
|
|
32
|
+
this.connecting = null;
|
|
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
39
|
reject(new Error('WebSocket error', { cause: event }));
|
|
46
|
-
}
|
|
40
|
+
});
|
|
41
|
+
ws.addEventListener('close', (event) => {
|
|
42
|
+
params.onDisconnect(event.reason);
|
|
43
|
+
this.webSocket = null;
|
|
44
|
+
});
|
|
47
45
|
});
|
|
46
|
+
this.webSocket = ws;
|
|
48
47
|
return this.connecting;
|
|
49
48
|
}
|
|
50
49
|
async disconnect() {
|
|
51
50
|
if (this.webSocket === null)
|
|
52
51
|
return;
|
|
52
|
+
const closing = once(this.webSocket, 'close');
|
|
53
53
|
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;
|
|
54
|
+
return closing;
|
|
60
55
|
}
|
|
61
|
-
async send(
|
|
62
|
-
if (this.
|
|
63
|
-
|
|
64
|
-
this.
|
|
56
|
+
async send(message, options) {
|
|
57
|
+
if (this.webSocket === null)
|
|
58
|
+
throw new Error('WebSocket is not connected');
|
|
59
|
+
await this.connecting;
|
|
60
|
+
if (!options.signal?.aborted)
|
|
61
|
+
this.webSocket.send(message);
|
|
65
62
|
}
|
|
66
63
|
}
|
|
67
|
-
|
|
68
|
-
return new Promise((resolve) => {
|
|
69
|
-
target.addEventListener(event, () => resolve(), { once: true });
|
|
70
|
-
});
|
|
71
|
-
}
|
|
64
|
+
export const WsTransportFactory = (params, options) => new WsTransportClient(params.format, params.protocol, options);
|
package/package.json
CHANGED
|
@@ -2,30 +2,30 @@
|
|
|
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/
|
|
13
|
-
"@nmtjs/
|
|
14
|
-
|
|
8
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
9
|
+
"@nmtjs/client": "0.15.0-beta.2"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@nmtjs/protocol": "0.15.0-beta.2"
|
|
15
13
|
},
|
|
16
14
|
"peerDependencies": {
|
|
17
|
-
"@nmtjs/
|
|
18
|
-
"@nmtjs/protocol": "0.
|
|
19
|
-
"@nmtjs/
|
|
15
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
16
|
+
"@nmtjs/protocol": "0.15.0-beta.2",
|
|
17
|
+
"@nmtjs/client": "0.15.0-beta.2"
|
|
20
18
|
},
|
|
21
19
|
"files": [
|
|
22
20
|
"dist",
|
|
23
21
|
"LICENSE.md",
|
|
24
22
|
"README.md"
|
|
25
23
|
],
|
|
26
|
-
"version": "0.
|
|
24
|
+
"version": "0.15.0-beta.2",
|
|
27
25
|
"scripts": {
|
|
26
|
+
"clean-build": "rm -rf ./dist",
|
|
28
27
|
"build": "tsc",
|
|
28
|
+
"dev": "tsc --watch",
|
|
29
29
|
"type-check": "tsc --noEmit"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { Protocol, ProtocolBaseClientCallOptions, ProtocolBaseTransformer } from '@nmtjs/protocol/client';
|
|
2
|
-
import { ClientMessageType } from '@nmtjs/protocol';
|
|
3
|
-
import { ProtocolTransport } from '@nmtjs/protocol/client';
|
|
4
|
-
export type WebSocketClientTransportOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* The origin of the server
|
|
7
|
-
* @example 'http://localhost:3000'
|
|
8
|
-
*/
|
|
9
|
-
origin: string;
|
|
10
|
-
/**
|
|
11
|
-
* Custom WebSocket class
|
|
12
|
-
* @default globalThis.WebSocket
|
|
13
|
-
*/
|
|
14
|
-
wsFactory?: (url: URL) => WebSocket;
|
|
15
|
-
debug?: boolean;
|
|
16
|
-
};
|
|
17
|
-
export declare class WebSocketClientTransport extends ProtocolTransport<WebSocketClientTransportOptions> {
|
|
18
|
-
protected readonly protocol: Protocol;
|
|
19
|
-
protected webSocket: WebSocket | null;
|
|
20
|
-
protected connecting: Promise<void> | null;
|
|
21
|
-
protected options: WebSocketClientTransportOptions;
|
|
22
|
-
constructor(protocol: Protocol, options: WebSocketClientTransportOptions);
|
|
23
|
-
connect(auth: any, transformer: ProtocolBaseTransformer): Promise<void>;
|
|
24
|
-
disconnect(): Promise<void>;
|
|
25
|
-
call(procedure: string, payload: any, options: ProtocolBaseClientCallOptions, transformer: ProtocolBaseTransformer): Promise<import("@nmtjs/common").InteractivePromise<unknown> & {
|
|
26
|
-
procedure: string;
|
|
27
|
-
signal: AbortSignal;
|
|
28
|
-
}>;
|
|
29
|
-
send(messageType: ClientMessageType, buffer: ArrayBuffer): Promise<void>;
|
|
30
|
-
}
|