@nmtjs/ws-client 0.14.4 → 0.15.0-beta.1
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/package.json +12 -12
- package/dist/index.d.ts +0 -30
- package/dist/index.js +0 -71
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/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/client": "0.15.0-beta.1",
|
|
9
|
+
"@nmtjs/common": "0.15.0-beta.1"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@nmtjs/protocol": "0.15.0-beta.1"
|
|
15
13
|
},
|
|
16
14
|
"peerDependencies": {
|
|
17
|
-
"@nmtjs/client": "0.
|
|
18
|
-
"@nmtjs/
|
|
19
|
-
"@nmtjs/
|
|
15
|
+
"@nmtjs/client": "0.15.0-beta.1",
|
|
16
|
+
"@nmtjs/common": "0.15.0-beta.1",
|
|
17
|
+
"@nmtjs/protocol": "0.15.0-beta.1"
|
|
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.1",
|
|
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
|
-
}
|
package/dist/index.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { ClientMessageType, concat, encodeNumber } from '@nmtjs/protocol';
|
|
2
|
-
import { ProtocolTransport, ProtocolTransportStatus, } from '@nmtjs/protocol/client';
|
|
3
|
-
export class WebSocketClientTransport extends ProtocolTransport {
|
|
4
|
-
protocol;
|
|
5
|
-
webSocket = null;
|
|
6
|
-
connecting = null;
|
|
7
|
-
options;
|
|
8
|
-
constructor(protocol, options) {
|
|
9
|
-
super();
|
|
10
|
-
this.protocol = protocol;
|
|
11
|
-
this.options = { debug: false, ...options };
|
|
12
|
-
}
|
|
13
|
-
connect(auth, transformer) {
|
|
14
|
-
// this.auth = auth
|
|
15
|
-
const wsUrl = new URL('/api', this.options.origin);
|
|
16
|
-
if (this.protocol.contentType) {
|
|
17
|
-
wsUrl.searchParams.set('content-type', this.protocol.contentType);
|
|
18
|
-
wsUrl.searchParams.set('accept', this.protocol.contentType);
|
|
19
|
-
}
|
|
20
|
-
if (auth)
|
|
21
|
-
wsUrl.searchParams.set('auth', auth);
|
|
22
|
-
const ws = this.options.wsFactory?.(wsUrl) ?? new WebSocket(wsUrl.toString());
|
|
23
|
-
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
|
-
this.connecting = new Promise((resolve, reject) => {
|
|
39
|
-
ws.addEventListener('open', () => {
|
|
40
|
-
this.status = ProtocolTransportStatus.CONNECTED;
|
|
41
|
-
this.emit('connected');
|
|
42
|
-
resolve();
|
|
43
|
-
}, { once: true });
|
|
44
|
-
ws.addEventListener('error', (event) => {
|
|
45
|
-
reject(new Error('WebSocket error', { cause: event }));
|
|
46
|
-
}, { once: true });
|
|
47
|
-
});
|
|
48
|
-
return this.connecting;
|
|
49
|
-
}
|
|
50
|
-
async disconnect() {
|
|
51
|
-
if (this.webSocket === null)
|
|
52
|
-
return;
|
|
53
|
-
this.webSocket.close(1000, 'client');
|
|
54
|
-
return _once(this.webSocket, 'close');
|
|
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;
|
|
60
|
-
}
|
|
61
|
-
async send(messageType, buffer) {
|
|
62
|
-
if (this.connecting)
|
|
63
|
-
await this.connecting;
|
|
64
|
-
this.webSocket.send(concat(encodeNumber(messageType, 'Uint8'), buffer));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function _once(target, event) {
|
|
68
|
-
return new Promise((resolve) => {
|
|
69
|
-
target.addEventListener(event, () => resolve(), { once: true });
|
|
70
|
-
});
|
|
71
|
-
}
|